mirror of
				https://github.com/cuberite/polarssl.git
				synced 2025-11-03 12:11:27 -05:00 
			
		
		
		
	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:
		
							parent
							
								
									384d4351ce
								
							
						
					
					
						commit
						82e2945ed2
					
				@ -29,8 +29,6 @@
 | 
			
		||||
 | 
			
		||||
#include "config.h"
 | 
			
		||||
 | 
			
		||||
#if defined(POLARSSL_X509_WRITE_C)
 | 
			
		||||
 | 
			
		||||
#include "rsa.h"
 | 
			
		||||
 | 
			
		||||
#define POLARSSL_ERR_X509_WRITE_UNKNOWN_OID             -1
 | 
			
		||||
@ -50,28 +48,26 @@ typedef struct _x509_req_name
 | 
			
		||||
}
 | 
			
		||||
x509_req_name;
 | 
			
		||||
 | 
			
		||||
typedef struct _x509_cert_req
 | 
			
		||||
typedef struct _x509_csr
 | 
			
		||||
{
 | 
			
		||||
    rsa_context *rsa;
 | 
			
		||||
    x509_req_name *subject;
 | 
			
		||||
    md_type_t md_alg;
 | 
			
		||||
}
 | 
			
		||||
x509_cert_req;
 | 
			
		||||
x509_csr;
 | 
			
		||||
 | 
			
		||||
void x509cert_req_init( x509_cert_req *ctx );
 | 
			
		||||
int x509cert_req_set_subject_name( x509_cert_req *ctx, char *subject_name );
 | 
			
		||||
void x509cert_req_set_rsa_key( x509_cert_req *ctx, rsa_context *rsa );
 | 
			
		||||
void x509cert_req_set_md_alg( x509_cert_req *ctx, md_type_t md_alg );
 | 
			
		||||
void x509cert_req_free( x509_cert_req *ctx );
 | 
			
		||||
void x509write_csr_init( x509_csr *ctx );
 | 
			
		||||
int x509write_csr_set_subject_name( x509_csr *ctx, char *subject_name );
 | 
			
		||||
void x509write_csr_set_rsa_key( x509_csr *ctx, rsa_context *rsa );
 | 
			
		||||
void x509write_csr_set_md_alg( x509_csr *ctx, md_type_t md_alg );
 | 
			
		||||
void x509write_csr_free( x509_csr *ctx );
 | 
			
		||||
 | 
			
		||||
int x509_write_pubkey_der( unsigned char *buf, size_t size, rsa_context *rsa );
 | 
			
		||||
int x509_write_key_der( unsigned char *buf, size_t size, rsa_context *rsa );
 | 
			
		||||
int x509_write_cert_req( x509_cert_req *ctx, unsigned char *buf, size_t size );
 | 
			
		||||
int x509write_pubkey_der( rsa_context *rsa, unsigned char *buf, size_t size );
 | 
			
		||||
int x509write_key_der( rsa_context *rsa, unsigned char *buf, size_t size );
 | 
			
		||||
int x509write_csr_der( x509_csr *ctx, unsigned char *buf, size_t size );
 | 
			
		||||
 | 
			
		||||
#ifdef __cplusplus
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#endif /* POLARSSL_X509_WRITE_C */
 | 
			
		||||
 | 
			
		||||
#endif /* POLARSSL_X509_WRITE_H */
 | 
			
		||||
 | 
			
		||||
@ -41,12 +41,12 @@
 | 
			
		||||
#define polarssl_free       free
 | 
			
		||||
#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;
 | 
			
		||||
 | 
			
		||||
@ -56,20 +56,20 @@ void x509cert_req_free( x509_cert_req *ctx )
 | 
			
		||||
        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;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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;
 | 
			
		||||
    char *s = subject_name, *c = s;
 | 
			
		||||
@ -148,7 +148,7 @@ exit:
 | 
			
		||||
    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;
 | 
			
		||||
    unsigned char *c;
 | 
			
		||||
@ -190,7 +190,7 @@ int x509_write_pubkey_der( unsigned char *buf, size_t size, rsa_context *rsa )
 | 
			
		||||
    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;
 | 
			
		||||
    unsigned char *c;
 | 
			
		||||
@ -293,8 +293,7 @@ static int x509_write_sig( unsigned char **p, unsigned char *start,
 | 
			
		||||
    return( len );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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 )
 | 
			
		||||
{
 | 
			
		||||
    int ret;
 | 
			
		||||
    const char *sig_oid;
 | 
			
		||||
 | 
			
		||||
@ -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;
 | 
			
		||||
    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;
 | 
			
		||||
 | 
			
		||||
    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 )
 | 
			
		||||
        return( ret );
 | 
			
		||||
@ -135,13 +135,13 @@ int main( int argc, char *argv[] )
 | 
			
		||||
    char buf[1024];
 | 
			
		||||
    int i, j, n;
 | 
			
		||||
    char *p, *q;
 | 
			
		||||
    x509_cert_req req;
 | 
			
		||||
    x509_csr req;
 | 
			
		||||
 | 
			
		||||
    /*
 | 
			
		||||
     * Set to sane values
 | 
			
		||||
     */
 | 
			
		||||
    x509cert_req_init( &req );
 | 
			
		||||
    x509cert_req_set_md_alg( &req, POLARSSL_MD_SHA1 );
 | 
			
		||||
    x509write_csr_init( &req );
 | 
			
		||||
    x509write_csr_set_md_alg( &req, POLARSSL_MD_SHA1 );
 | 
			
		||||
    memset( &rsa, 0, sizeof( rsa_context ) );
 | 
			
		||||
    memset( buf, 0, 1024 );
 | 
			
		||||
 | 
			
		||||
@ -193,12 +193,12 @@ int main( int argc, char *argv[] )
 | 
			
		||||
    /*
 | 
			
		||||
     * 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
 | 
			
		||||
        error_strerror( ret, buf, 1024 );
 | 
			
		||||
#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;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -219,7 +219,7 @@ int main( int argc, char *argv[] )
 | 
			
		||||
        goto exit;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    x509cert_req_set_rsa_key( &req, &rsa );
 | 
			
		||||
    x509write_csr_set_rsa_key( &req, &rsa );
 | 
			
		||||
 | 
			
		||||
    printf( " ok\n" );
 | 
			
		||||
 | 
			
		||||
@ -241,7 +241,7 @@ int main( int argc, char *argv[] )
 | 
			
		||||
    printf( " ok\n" );
 | 
			
		||||
 | 
			
		||||
exit:
 | 
			
		||||
    x509cert_req_free( &req );
 | 
			
		||||
    x509write_csr_free( &req );
 | 
			
		||||
    rsa_free( &rsa );
 | 
			
		||||
 | 
			
		||||
#if defined(_WIN32)
 | 
			
		||||
 | 
			
		||||
@ -1,27 +1,27 @@
 | 
			
		||||
Certificate Request check Server1 SHA1
 | 
			
		||||
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
 | 
			
		||||
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
 | 
			
		||||
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
 | 
			
		||||
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
 | 
			
		||||
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
 | 
			
		||||
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
 | 
			
		||||
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"
 | 
			
		||||
 | 
			
		||||
@ -11,12 +11,12 @@
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/* 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 )
 | 
			
		||||
{
 | 
			
		||||
    rsa_context rsa;
 | 
			
		||||
    pem_context pem;
 | 
			
		||||
    x509_cert_req req;
 | 
			
		||||
    x509_csr req;
 | 
			
		||||
    unsigned char *c;
 | 
			
		||||
    unsigned char 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 )
 | 
			
		||||
        return;
 | 
			
		||||
 | 
			
		||||
    x509cert_req_init( &req );
 | 
			
		||||
    x509cert_req_set_md_alg( &req, md_type );
 | 
			
		||||
    x509cert_req_set_rsa_key( &req, &rsa );
 | 
			
		||||
    TEST_ASSERT( x509cert_req_set_subject_name( &req, subject_name ) == 0 );
 | 
			
		||||
    x509write_csr_init( &req );
 | 
			
		||||
    x509write_csr_set_md_alg( &req, md_type );
 | 
			
		||||
    x509write_csr_set_rsa_key( &req, &rsa );
 | 
			
		||||
    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 );
 | 
			
		||||
 | 
			
		||||
    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( pem.buflen == (size_t) ret );
 | 
			
		||||
 | 
			
		||||
    x509cert_req_free( &req );
 | 
			
		||||
    x509write_csr_free( &req );
 | 
			
		||||
    rsa_free( &rsa );
 | 
			
		||||
    pem_free( &pem );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user