Changed naming and prototype convention for x509write functions

CSR writing functions now start with x509write_csr_*()
DER writing functions now have the context at the start instead of the
end conforming to other modules.
This commit is contained in:
Paul Bakker 2013-08-25 11:01:31 +02:00
parent 384d4351ce
commit 82e2945ed2
5 changed files with 44 additions and 49 deletions

View file

@ -29,8 +29,6 @@
#include "config.h" #include "config.h"
#if defined(POLARSSL_X509_WRITE_C)
#include "rsa.h" #include "rsa.h"
#define POLARSSL_ERR_X509_WRITE_UNKNOWN_OID -1 #define POLARSSL_ERR_X509_WRITE_UNKNOWN_OID -1
@ -50,28 +48,26 @@ typedef struct _x509_req_name
} }
x509_req_name; x509_req_name;
typedef struct _x509_cert_req typedef struct _x509_csr
{ {
rsa_context *rsa; rsa_context *rsa;
x509_req_name *subject; x509_req_name *subject;
md_type_t md_alg; md_type_t md_alg;
} }
x509_cert_req; x509_csr;
void x509cert_req_init( x509_cert_req *ctx ); void x509write_csr_init( x509_csr *ctx );
int x509cert_req_set_subject_name( x509_cert_req *ctx, char *subject_name ); int x509write_csr_set_subject_name( x509_csr *ctx, char *subject_name );
void x509cert_req_set_rsa_key( x509_cert_req *ctx, rsa_context *rsa ); void x509write_csr_set_rsa_key( x509_csr *ctx, rsa_context *rsa );
void x509cert_req_set_md_alg( x509_cert_req *ctx, md_type_t md_alg ); void x509write_csr_set_md_alg( x509_csr *ctx, md_type_t md_alg );
void x509cert_req_free( x509_cert_req *ctx ); void x509write_csr_free( x509_csr *ctx );
int x509_write_pubkey_der( unsigned char *buf, size_t size, rsa_context *rsa ); int x509write_pubkey_der( rsa_context *rsa, unsigned char *buf, size_t size );
int x509_write_key_der( unsigned char *buf, size_t size, rsa_context *rsa ); int x509write_key_der( rsa_context *rsa, unsigned char *buf, size_t size );
int x509_write_cert_req( x509_cert_req *ctx, unsigned char *buf, size_t size ); int x509write_csr_der( x509_csr *ctx, unsigned char *buf, size_t size );
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif /* POLARSSL_X509_WRITE_C */
#endif /* POLARSSL_X509_WRITE_H */ #endif /* POLARSSL_X509_WRITE_H */

View file

@ -41,12 +41,12 @@
#define polarssl_free free #define polarssl_free free
#endif #endif
void x509cert_req_init( x509_cert_req *ctx ) void x509write_csr_init( x509_csr *ctx )
{ {
memset( ctx, 0, sizeof(x509_cert_req) ); memset( ctx, 0, sizeof(x509_csr) );
} }
void x509cert_req_free( x509_cert_req *ctx ) void x509write_csr_free( x509_csr *ctx )
{ {
x509_req_name *cur; x509_req_name *cur;
@ -56,20 +56,20 @@ void x509cert_req_free( x509_cert_req *ctx )
polarssl_free( cur ); polarssl_free( cur );
} }
memset( ctx, 0, sizeof(x509_cert_req) ); memset( ctx, 0, sizeof(x509_csr) );
} }
void x509cert_req_set_md_alg( x509_cert_req *ctx, md_type_t md_alg ) void x509write_csr_set_md_alg( x509_csr *ctx, md_type_t md_alg )
{ {
ctx->md_alg = md_alg; ctx->md_alg = md_alg;
} }
void x509cert_req_set_rsa_key( x509_cert_req *ctx, rsa_context *rsa ) void x509write_csr_set_rsa_key( x509_csr *ctx, rsa_context *rsa )
{ {
ctx->rsa = rsa; ctx->rsa = rsa;
} }
int x509cert_req_set_subject_name( x509_cert_req *ctx, char *subject_name ) int x509write_csr_set_subject_name( x509_csr *ctx, char *subject_name )
{ {
int ret = 0; int ret = 0;
char *s = subject_name, *c = s; char *s = subject_name, *c = s;
@ -148,7 +148,7 @@ exit:
return( ret ); return( ret );
} }
int x509_write_pubkey_der( unsigned char *buf, size_t size, rsa_context *rsa ) int x509write_pubkey_der( rsa_context *rsa, unsigned char *buf, size_t size )
{ {
int ret; int ret;
unsigned char *c; unsigned char *c;
@ -190,7 +190,7 @@ int x509_write_pubkey_der( unsigned char *buf, size_t size, rsa_context *rsa )
return( len ); return( len );
} }
int x509_write_key_der( unsigned char *buf, size_t size, rsa_context *rsa ) int x509write_key_der( rsa_context *rsa, unsigned char *buf, size_t size )
{ {
int ret; int ret;
unsigned char *c; unsigned char *c;
@ -293,8 +293,7 @@ static int x509_write_sig( unsigned char **p, unsigned char *start,
return( len ); return( len );
} }
int x509_write_cert_req( x509_cert_req *ctx, unsigned char *buf, int x509write_csr_der( x509_csr *ctx, unsigned char *buf, size_t size )
size_t size )
{ {
int ret; int ret;
const char *sig_oid; const char *sig_oid;

View file

@ -65,7 +65,7 @@ void my_debug( void *ctx, int level, const char *str )
} }
} }
int write_certificate_request( x509_cert_req *req, char *output_file ) int write_certificate_request( x509_csr *req, char *output_file )
{ {
FILE *f; FILE *f;
unsigned char output_buf[4096]; unsigned char output_buf[4096];
@ -75,7 +75,7 @@ int write_certificate_request( x509_cert_req *req, char *output_file )
size_t len = 0, olen = 4096; size_t len = 0, olen = 4096;
memset(output_buf, 0, 4096); memset(output_buf, 0, 4096);
ret = x509_write_cert_req( req, output_buf, 4096 ); ret = x509write_csr_der( req, output_buf, 4096 );
if( ret < 0 ) if( ret < 0 )
return( ret ); return( ret );
@ -135,13 +135,13 @@ int main( int argc, char *argv[] )
char buf[1024]; char buf[1024];
int i, j, n; int i, j, n;
char *p, *q; char *p, *q;
x509_cert_req req; x509_csr req;
/* /*
* Set to sane values * Set to sane values
*/ */
x509cert_req_init( &req ); x509write_csr_init( &req );
x509cert_req_set_md_alg( &req, POLARSSL_MD_SHA1 ); x509write_csr_set_md_alg( &req, POLARSSL_MD_SHA1 );
memset( &rsa, 0, sizeof( rsa_context ) ); memset( &rsa, 0, sizeof( rsa_context ) );
memset( buf, 0, 1024 ); memset( buf, 0, 1024 );
@ -193,12 +193,12 @@ int main( int argc, char *argv[] )
/* /*
* 1.0. Check the subject name for validity * 1.0. Check the subject name for validity
*/ */
if( ( ret = x509cert_req_set_subject_name( &req, opt.subject_name ) ) != 0 ) if( ( ret = x509write_csr_set_subject_name( &req, opt.subject_name ) ) != 0 )
{ {
#ifdef POLARSSL_ERROR_C #ifdef POLARSSL_ERROR_C
error_strerror( ret, buf, 1024 ); error_strerror( ret, buf, 1024 );
#endif #endif
printf( " failed\n ! x509cert_req_set_subject_name returned %d - %s\n\n", ret, buf ); printf( " failed\n ! x509write_csr_set_subject_name returned %d - %s\n\n", ret, buf );
goto exit; goto exit;
} }
@ -219,7 +219,7 @@ int main( int argc, char *argv[] )
goto exit; goto exit;
} }
x509cert_req_set_rsa_key( &req, &rsa ); x509write_csr_set_rsa_key( &req, &rsa );
printf( " ok\n" ); printf( " ok\n" );
@ -241,7 +241,7 @@ int main( int argc, char *argv[] )
printf( " ok\n" ); printf( " ok\n" );
exit: exit:
x509cert_req_free( &req ); x509write_csr_free( &req );
rsa_free( &rsa ); rsa_free( &rsa );
#if defined(_WIN32) #if defined(_WIN32)

View file

@ -1,27 +1,27 @@
Certificate Request check Server1 SHA1 Certificate Request check Server1 SHA1
depends_on:POLARSSL_SHA1_C depends_on:POLARSSL_SHA1_C
x509_cert_req_check:"data_files/server1.key":POLARSSL_MD_SHA1:"data_files/server1.req.sha1" x509_csr_check:"data_files/server1.key":POLARSSL_MD_SHA1:"data_files/server1.req.sha1"
Certificate Request check Server1 SHA224 Certificate Request check Server1 SHA224
depends_on:POLARSSL_SHA256_C depends_on:POLARSSL_SHA256_C
x509_cert_req_check:"data_files/server1.key":POLARSSL_MD_SHA224:"data_files/server1.req.sha224" x509_csr_check:"data_files/server1.key":POLARSSL_MD_SHA224:"data_files/server1.req.sha224"
Certificate Request check Server1 SHA256 Certificate Request check Server1 SHA256
depends_on:POLARSSL_SHA256_C depends_on:POLARSSL_SHA256_C
x509_cert_req_check:"data_files/server1.key":POLARSSL_MD_SHA256:"data_files/server1.req.sha256" x509_csr_check:"data_files/server1.key":POLARSSL_MD_SHA256:"data_files/server1.req.sha256"
Certificate Request check Server1 SHA384 Certificate Request check Server1 SHA384
depends_on:POLARSSL_SHA512_C depends_on:POLARSSL_SHA512_C
x509_cert_req_check:"data_files/server1.key":POLARSSL_MD_SHA384:"data_files/server1.req.sha384" x509_csr_check:"data_files/server1.key":POLARSSL_MD_SHA384:"data_files/server1.req.sha384"
Certificate Request check Server1 SHA512 Certificate Request check Server1 SHA512
depends_on:POLARSSL_SHA512_C depends_on:POLARSSL_SHA512_C
x509_cert_req_check:"data_files/server1.key":POLARSSL_MD_SHA512:"data_files/server1.req.sha512" x509_csr_check:"data_files/server1.key":POLARSSL_MD_SHA512:"data_files/server1.req.sha512"
Certificate Request check Server1 MD4 Certificate Request check Server1 MD4
depends_on:POLARSSL_MD4_C depends_on:POLARSSL_MD4_C
x509_cert_req_check:"data_files/server1.key":POLARSSL_MD_MD4:"data_files/server1.req.md4" x509_csr_check:"data_files/server1.key":POLARSSL_MD_MD4:"data_files/server1.req.md4"
Certificate Request check Server1 MD5 Certificate Request check Server1 MD5
depends_on:POLARSSL_MD5_C depends_on:POLARSSL_MD5_C
x509_cert_req_check:"data_files/server1.key":POLARSSL_MD_MD5:"data_files/server1.req.md5" x509_csr_check:"data_files/server1.key":POLARSSL_MD_MD5:"data_files/server1.req.md5"

View file

@ -11,12 +11,12 @@
*/ */
/* BEGIN_CASE */ /* BEGIN_CASE */
void x509_cert_req_check( char *key_file, int md_type, void x509_csr_check( char *key_file, int md_type,
char *cert_req_check_file ) char *cert_req_check_file )
{ {
rsa_context rsa; rsa_context rsa;
pem_context pem; pem_context pem;
x509_cert_req req; x509_csr req;
unsigned char *c; unsigned char *c;
unsigned char buf[4000]; unsigned char buf[4000];
unsigned char check_buf[4000]; unsigned char check_buf[4000];
@ -31,12 +31,12 @@ void x509_cert_req_check( char *key_file, int md_type,
if( ret != 0 ) if( ret != 0 )
return; return;
x509cert_req_init( &req ); x509write_csr_init( &req );
x509cert_req_set_md_alg( &req, md_type ); x509write_csr_set_md_alg( &req, md_type );
x509cert_req_set_rsa_key( &req, &rsa ); x509write_csr_set_rsa_key( &req, &rsa );
TEST_ASSERT( x509cert_req_set_subject_name( &req, subject_name ) == 0 ); TEST_ASSERT( x509write_csr_set_subject_name( &req, subject_name ) == 0 );
ret = x509_write_cert_req( &req, buf, 4000 ); ret = x509write_csr_der( &req, buf, 4000 );
TEST_ASSERT( ret >= 0 ); TEST_ASSERT( ret >= 0 );
c = buf + 3999 - ret; c = buf + 3999 - ret;
@ -52,7 +52,7 @@ void x509_cert_req_check( char *key_file, int md_type,
TEST_ASSERT( memcmp( c, pem.buf, pem.buflen ) == 0 ); TEST_ASSERT( memcmp( c, pem.buf, pem.buflen ) == 0 );
TEST_ASSERT( pem.buflen == (size_t) ret ); TEST_ASSERT( pem.buflen == (size_t) ret );
x509cert_req_free( &req ); x509write_csr_free( &req );
rsa_free( &rsa ); rsa_free( &rsa );
pem_free( &pem ); pem_free( &pem );
} }