From 369d2eb2a27a7401f996dd696890b7092677a3d2 Mon Sep 17 00:00:00 2001
From: Paul Bakker
Date: Wed, 18 Sep 2013 11:58:25 +0200
Subject: [PATCH] Introduced x509_crt_init(), x509_crl_init() and
x509_csr_init()
---
include/polarssl/x509_crl.h | 7 +++++++
include/polarssl/x509_crt.h | 7 +++++++
include/polarssl/x509_csr.h | 7 +++++++
library/x509_crl.c | 12 ++++++++++--
library/x509_crt.c | 10 +++++++++-
library/x509_csr.c | 10 +++++++++-
programs/ssl/ssl_client1.c | 2 +-
programs/ssl/ssl_client2.c | 4 ++--
programs/ssl/ssl_fork_server.c | 2 +-
programs/ssl/ssl_mail_client.c | 4 ++--
programs/ssl/ssl_server.c | 2 +-
programs/ssl/ssl_server2.c | 4 ++--
programs/test/ssl_cert_test.c | 6 +++---
programs/test/ssl_test.c | 2 +-
programs/x509/cert_app.c | 6 +++---
programs/x509/cert_write.c | 4 ++--
programs/x509/crl_app.c | 2 +-
programs/x509/req_app.c | 2 +-
tests/suites/test_suite_debug.function | 2 +-
tests/suites/test_suite_x509parse.function | 18 +++++++++---------
20 files changed, 79 insertions(+), 34 deletions(-)
diff --git a/include/polarssl/x509_crl.h b/include/polarssl/x509_crl.h
index bae81823e..2bc7cd821 100644
--- a/include/polarssl/x509_crl.h
+++ b/include/polarssl/x509_crl.h
@@ -134,6 +134,13 @@ int x509parse_crlfile( x509_crl *chain, const char *path );
int x509parse_crl_info( char *buf, size_t size, const char *prefix,
const x509_crl *crl );
+/**
+ * \brief Initialize a CRL (chain)
+ *
+ * \param crl CRL chain to initialize
+ */
+void x509_crl_init( x509_crl *crl );
+
/**
* \brief Unallocate all CRL data
*
diff --git a/include/polarssl/x509_crt.h b/include/polarssl/x509_crt.h
index 55042ec53..637819126 100644
--- a/include/polarssl/x509_crt.h
+++ b/include/polarssl/x509_crt.h
@@ -254,6 +254,13 @@ int x509parse_verify( x509_cert *crt,
int x509parse_revoked( const x509_cert *crt, const x509_crl *crl );
#endif /* POLARSSL_X509_CRL_PARSE_C */
+/**
+ * \brief Initialize a certificate (chain)
+ *
+ * \param crt Certificate chain to initialize
+ */
+void x509_crt_init( x509_cert *crt );
+
/**
* \brief Unallocate all certificate data
*
diff --git a/include/polarssl/x509_csr.h b/include/polarssl/x509_csr.h
index 32befdb4c..5b4b1baa5 100644
--- a/include/polarssl/x509_csr.h
+++ b/include/polarssl/x509_csr.h
@@ -117,6 +117,13 @@ int x509parse_csrfile( x509_csr *csr, const char *path );
int x509parse_csr_info( char *buf, size_t size, const char *prefix,
const x509_csr *csr );
+/**
+ * \brief Initialize a CSR
+ *
+ * \param csr CSR to initialize
+ */
+void x509_csr_init( x509_csr *csr );
+
/**
* \brief Unallocate all CSR data
*
diff --git a/library/x509_crl.c b/library/x509_crl.c
index 1a10bc499..e327726ac 100644
--- a/library/x509_crl.c
+++ b/library/x509_crl.c
@@ -279,7 +279,7 @@ int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
}
crl = crl->next;
- memset( crl, 0, sizeof( x509_crl ) );
+ x509_crl_init( crl );
}
#if defined(POLARSSL_PEM_PARSE_C)
@@ -514,7 +514,7 @@ int x509parse_crl( x509_crl *chain, const unsigned char *buf, size_t buflen )
}
crl = crl->next;
- memset( crl, 0, sizeof( x509_crl ) );
+ x509_crl_init( crl );
return( x509parse_crl( crl, buf, buflen ) );
}
@@ -679,6 +679,14 @@ int x509parse_crl_info( char *buf, size_t size, const char *prefix,
return( (int) ( size - n ) );
}
+/*
+ * Initialize a CRL chain
+ */
+void x509_crl_init( x509_crl *crl )
+{
+ memset( crl, 0, sizeof(x509_crl) );
+}
+
/*
* Unallocate all CRL data
*/
diff --git a/library/x509_crt.c b/library/x509_crt.c
index f57fddc93..f73724e98 100644
--- a/library/x509_crt.c
+++ b/library/x509_crt.c
@@ -785,7 +785,7 @@ int x509parse_crt_der( x509_cert *chain, const unsigned char *buf, size_t buflen
prev = crt;
crt = crt->next;
- memset( crt, 0, sizeof( x509_cert ) );
+ x509_crt_init( crt );
}
if( ( ret = x509parse_crt_der_core( crt, buf, buflen ) ) != 0 )
@@ -1602,6 +1602,14 @@ int x509parse_verify( x509_cert *crt,
return( 0 );
}
+/*
+ * Initialize a certificate chain
+ */
+void x509_crt_init( x509_cert *crt )
+{
+ memset( crt, 0, sizeof(x509_cert) );
+}
+
/*
* Unallocate all certificate data
*/
diff --git a/library/x509_csr.c b/library/x509_csr.c
index 30cd1c106..65bc63c11 100644
--- a/library/x509_csr.c
+++ b/library/x509_csr.c
@@ -103,7 +103,7 @@ int x509parse_csr( x509_csr *csr, const unsigned char *buf, size_t buflen )
if( csr == NULL || buf == NULL )
return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
- memset( csr, 0, sizeof( x509_csr ) );
+ x509_csr_init( csr );
#if defined(POLARSSL_PEM_PARSE_C)
pem_init( &pem );
@@ -405,6 +405,14 @@ int x509parse_csr_info( char *buf, size_t size, const char *prefix,
return( (int) ( size - n ) );
}
+/*
+ * Initialize a CSR
+ */
+void x509_csr_init( x509_csr *csr )
+{
+ memset( csr, 0, sizeof(x509_csr) );
+}
+
/*
* Unallocate all CSR data
*/
diff --git a/programs/ssl/ssl_client1.c b/programs/ssl/ssl_client1.c
index b7a1e9a81..da4fe823a 100644
--- a/programs/ssl/ssl_client1.c
+++ b/programs/ssl/ssl_client1.c
@@ -90,7 +90,7 @@ int main( int argc, char *argv[] )
* 0. Initialize the RNG and the session data
*/
memset( &ssl, 0, sizeof( ssl_context ) );
- memset( &cacert, 0, sizeof( x509_cert ) );
+ x509_crt_init( &cacert );
printf( "\n . Seeding the random number generator..." );
fflush( stdout );
diff --git a/programs/ssl/ssl_client2.c b/programs/ssl/ssl_client2.c
index 5ee97b738..d5e43f685 100644
--- a/programs/ssl/ssl_client2.c
+++ b/programs/ssl/ssl_client2.c
@@ -269,8 +269,8 @@ int main( int argc, char *argv[] )
memset( &ssl, 0, sizeof( ssl_context ) );
memset( &saved_session, 0, sizeof( ssl_session ) );
#if defined(POLARSSL_X509_CRT_PARSE_C)
- memset( &cacert, 0, sizeof( x509_cert ) );
- memset( &clicert, 0, sizeof( x509_cert ) );
+ x509_crt_init( &cacert );
+ x509_crt_init( &clicert );
pk_init( &pkey );
#endif
diff --git a/programs/ssl/ssl_fork_server.c b/programs/ssl/ssl_fork_server.c
index af9cef647..df75d9205 100644
--- a/programs/ssl/ssl_fork_server.c
+++ b/programs/ssl/ssl_fork_server.c
@@ -134,7 +134,7 @@ int main( int argc, char *argv[] )
printf( " . Loading the server cert. and key..." );
fflush( stdout );
- memset( &srvcert, 0, sizeof( x509_cert ) );
+ x509_crt_init( &srvcert );
/*
* This demonstration program uses embedded test certificates.
diff --git a/programs/ssl/ssl_mail_client.c b/programs/ssl/ssl_mail_client.c
index f9465caa0..a95e2dab5 100644
--- a/programs/ssl/ssl_mail_client.c
+++ b/programs/ssl/ssl_mail_client.c
@@ -363,8 +363,8 @@ int main( int argc, char *argv[] )
* Make sure memory references are valid.
*/
server_fd = 0;
- memset( &cacert, 0, sizeof( x509_cert ) );
- memset( &clicert, 0, sizeof( x509_cert ) );
+ x509_crt_init( &cacert );
+ x509_crt_init( &clicert );
pk_init( &pkey );
if( argc == 0 )
diff --git a/programs/ssl/ssl_server.c b/programs/ssl/ssl_server.c
index 38fa2f263..1929c9eb8 100644
--- a/programs/ssl/ssl_server.c
+++ b/programs/ssl/ssl_server.c
@@ -114,7 +114,7 @@ int main( int argc, char *argv[] )
printf( "\n . Loading the server cert. and key..." );
fflush( stdout );
- memset( &srvcert, 0, sizeof( x509_cert ) );
+ x509_crt_init( &srvcert );
/*
* This demonstration program uses embedded test certificates.
diff --git a/programs/ssl/ssl_server2.c b/programs/ssl/ssl_server2.c
index a7dfa5f8a..b024e4bce 100644
--- a/programs/ssl/ssl_server2.c
+++ b/programs/ssl/ssl_server2.c
@@ -237,8 +237,8 @@ int main( int argc, char *argv[] )
*/
listen_fd = 0;
#if defined(POLARSSL_X509_CRT_PARSE_C)
- memset( &cacert, 0, sizeof( x509_cert ) );
- memset( &srvcert, 0, sizeof( x509_cert ) );
+ x509_crt_init( &cacert );
+ x509_crt_init( &srvcert );
pk_init( &pkey );
#endif
#if defined(POLARSSL_SSL_CACHE_C)
diff --git a/programs/test/ssl_cert_test.c b/programs/test/ssl_cert_test.c
index f1044cf15..9b58a6dc9 100644
--- a/programs/test/ssl_cert_test.c
+++ b/programs/test/ssl_cert_test.c
@@ -89,8 +89,8 @@ int main( int argc, char *argv[] )
((void) argc);
((void) argv);
- memset( &cacert, 0, sizeof( x509_cert ) );
- memset( &crl, 0, sizeof( x509_crl ) );
+ x509_crt_init( &cacert );
+ x509_crl_init( &crl );
/*
* 1.1. Load the trusted CA
@@ -142,7 +142,7 @@ int main( int argc, char *argv[] )
x509_cert clicert;
pk_context pk;
- memset( &clicert, 0, sizeof( x509_cert ) );
+ x509_crt_init( &clicert );
pk_init( &pk );
snprintf(name, 512, "ssl/test-ca/%s", client_certificates[i]);
diff --git a/programs/test/ssl_test.c b/programs/test/ssl_test.c
index 9d6391d54..1677aa99e 100644
--- a/programs/test/ssl_test.c
+++ b/programs/test/ssl_test.c
@@ -187,7 +187,7 @@ static int ssl_test( struct options *opt )
memset( read_state, 0, sizeof( read_state ) );
memset( write_state, 0, sizeof( write_state ) );
- memset( &srvcert, 0, sizeof( x509_cert ) );
+ x509_crt_init( &srvcert );
pk_init( &pkey );
if( opt->opmode == OPMODE_CLIENT )
diff --git a/programs/x509/cert_app.c b/programs/x509/cert_app.c
index add75a0a6..160e65d90 100644
--- a/programs/x509/cert_app.c
+++ b/programs/x509/cert_app.c
@@ -168,8 +168,8 @@ int main( int argc, char *argv[] )
* Set to sane values
*/
server_fd = 0;
- memset( &cacert, 0, sizeof( x509_cert ) );
- memset( &clicert, 0, sizeof( x509_cert ) );
+ x509_crt_init( &cacert );
+ x509_crt_init( &clicert );
pk_init( &pkey );
if( argc == 0 )
@@ -269,7 +269,7 @@ int main( int argc, char *argv[] )
{
x509_cert crt;
x509_cert *cur = &crt;
- memset( &crt, 0, sizeof( x509_cert ) );
+ x509_crt_init( &crt );
/*
* 1.1. Load the certificate(s)
diff --git a/programs/x509/cert_write.c b/programs/x509/cert_write.c
index 84b12f667..c50cf815d 100644
--- a/programs/x509/cert_write.c
+++ b/programs/x509/cert_write.c
@@ -208,9 +208,9 @@ int main( int argc, char *argv[] )
pk_init( &loaded_subject_key );
mpi_init( &serial );
#if defined(POLARSSL_X509_CSR_PARSE_C)
- memset( &csr, 0, sizeof(x509_csr) );
+ x509_csr_init( &csr );
#endif
- memset( &issuer_crt, 0, sizeof(x509_cert) );
+ x509_crt_init( &issuer_crt );
memset( buf, 0, 1024 );
if( argc == 0 )
diff --git a/programs/x509/crl_app.c b/programs/x509/crl_app.c
index 1cb9828d1..2213f8196 100644
--- a/programs/x509/crl_app.c
+++ b/programs/x509/crl_app.c
@@ -76,7 +76,7 @@ int main( int argc, char *argv[] )
/*
* Set to sane values
*/
- memset( &crl, 0, sizeof( x509_crl ) );
+ x509_crl_init( &crl );
if( argc == 0 )
{
diff --git a/programs/x509/req_app.c b/programs/x509/req_app.c
index 5e05d60fe..3d3552451 100644
--- a/programs/x509/req_app.c
+++ b/programs/x509/req_app.c
@@ -76,7 +76,7 @@ int main( int argc, char *argv[] )
/*
* Set to sane values
*/
- memset( &csr, 0, sizeof( x509_csr ) );
+ x509_csr_init( &csr );
if( argc == 0 )
{
diff --git a/tests/suites/test_suite_debug.function b/tests/suites/test_suite_debug.function
index eb916baca..6bc524b0d 100644
--- a/tests/suites/test_suite_debug.function
+++ b/tests/suites/test_suite_debug.function
@@ -30,7 +30,7 @@ void debug_print_crt( char *crt_file, char *file, int line, char *prefix,
ssl_context ssl;
struct buffer_data buffer;
- memset( &crt, 0, sizeof( x509_cert ) );
+ x509_crt_init( &crt );
memset( &ssl, 0, sizeof( ssl_context ) );
memset( buffer.buf, 0, 2000 );
buffer.ptr = buffer.buf;
diff --git a/tests/suites/test_suite_x509parse.function b/tests/suites/test_suite_x509parse.function
index 62a6fd52f..082dd33c6 100644
--- a/tests/suites/test_suite_x509parse.function
+++ b/tests/suites/test_suite_x509parse.function
@@ -38,7 +38,7 @@ void x509_cert_info( char *crt_file, char *result_str )
char buf[2000];
int res;
- memset( &crt, 0, sizeof( x509_cert ) );
+ x509_crt_init( &crt );
memset( buf, 0, 2000 );
TEST_ASSERT( x509parse_crtfile( &crt, crt_file ) == 0 );
@@ -60,7 +60,7 @@ void x509_crl_info( char *crl_file, char *result_str )
char buf[2000];
int res;
- memset( &crl, 0, sizeof( x509_crl ) );
+ x509_crl_init( &crl );
memset( buf, 0, 2000 );
TEST_ASSERT( x509parse_crlfile( &crl, crl_file ) == 0 );
@@ -88,9 +88,9 @@ void x509_verify( char *crt_file, char *ca_file, char *crl_file,
int (*f_vrfy)(void *, x509_cert *, int, int *) = NULL;
char * cn_name = NULL;
- memset( &crt, 0, sizeof( x509_cert ) );
- memset( &ca, 0, sizeof( x509_cert ) );
- memset( &crl, 0, sizeof( x509_crl ) );
+ x509_crt_init( &crt );
+ x509_crt_init( &ca );
+ x509_crl_init( &crl );
if( strcmp( cn_name_str, "NULL" ) != 0 )
cn_name = cn_name_str;
@@ -126,7 +126,7 @@ void x509_dn_gets( char *crt_file, char *entity, char *result_str )
char buf[2000];
int res = 0;
- memset( &crt, 0, sizeof( x509_cert ) );
+ x509_crt_init( &crt );
memset( buf, 0, 2000 );
TEST_ASSERT( x509parse_crtfile( &crt, crt_file ) == 0 );
@@ -151,7 +151,7 @@ void x509_time_expired( char *crt_file, char *entity, int result )
{
x509_cert crt;
- memset( &crt, 0, sizeof( x509_cert ) );
+ x509_crt_init( &crt );
TEST_ASSERT( x509parse_crtfile( &crt, crt_file ) == 0 );
@@ -174,7 +174,7 @@ void x509parse_crt( char *crt_data, char *result_str, int result )
unsigned char output[2000];
int data_len, res;
- memset( &crt, 0, sizeof( x509_cert ) );
+ x509_crt_init( &crt );
memset( buf, 0, 2000 );
memset( output, 0, 2000 );
@@ -203,7 +203,7 @@ void x509parse_crl( char *crl_data, char *result_str, int result )
unsigned char output[2000];
int data_len, res;
- memset( &crl, 0, sizeof( x509_crl ) );
+ x509_crl_init( &crl );
memset( buf, 0, 2000 );
memset( output, 0, 2000 );