From 1c3853b953508716d9375fc784570f6a1da57c0f Mon Sep 17 00:00:00 2001 From: Paul Bakker Date: Tue, 10 Sep 2013 11:43:44 +0200 Subject: [PATCH] oid_get_oid_by_*() now give back oid length as well --- include/polarssl/oid.h | 6 ++++-- library/oid.c | 11 +++++++---- library/rsa.c | 3 +-- library/x509write.c | 19 +++++++++++++------ 4 files changed, 25 insertions(+), 14 deletions(-) diff --git a/include/polarssl/oid.h b/include/polarssl/oid.h index 025a15f0a..b0e781fc1 100644 --- a/include/polarssl/oid.h +++ b/include/polarssl/oid.h @@ -409,11 +409,12 @@ int oid_get_sig_alg_desc( const asn1_buf *oid, const char **desc ); * \param md_alg message digest algorithm * \param pk_alg public key algorithm * \param oid place to store ASN.1 OID string pointer + * \param olen length of the OID * * \return 0 if successful, or POLARSSL_ERR_OID_NOT_FOUND */ int oid_get_oid_by_sig_alg( pk_type_t pk_alg, md_type_t md_alg, - const char **oid_str ); + const char **oid, size_t *olen ); /** * \brief Translate hash algorithm OID into md_type @@ -441,10 +442,11 @@ int oid_get_extended_key_usage( const asn1_buf *oid, const char **desc ); * * \param md_alg message digest algorithm * \param oid place to store ASN.1 OID string pointer + * \param olen length of the OID * * \return 0 if successful, or POLARSSL_ERR_OID_NOT_FOUND */ -int oid_get_oid_by_md( md_type_t md_alg, const char **oid_str ); +int oid_get_oid_by_md( md_type_t md_alg, const char **oid, size_t *olen ); #if defined(POLARSSL_CIPHER_C) /** diff --git a/library/oid.c b/library/oid.c index c5608c3a1..f37bbf6e7 100644 --- a/library/oid.c +++ b/library/oid.c @@ -93,12 +93,13 @@ int FN_NAME( const asn1_buf *oid, ATTR1_TYPE * ATTR1, ATTR2_TYPE * ATTR2 ) \ * attribute from a oid_descriptor_t wrapper. */ #define FN_OID_GET_OID_BY_ATTR1(FN_NAME, TYPE_T, LIST, ATTR1_TYPE, ATTR1) \ -int FN_NAME( ATTR1_TYPE ATTR1, const char **oid_str ) \ +int FN_NAME( ATTR1_TYPE ATTR1, const char **oid, size_t *olen ) \ { \ const TYPE_T *cur = LIST; \ while( cur->descriptor.asn1 != NULL ) { \ if( cur->ATTR1 == ATTR1 ) { \ - *oid_str = cur->descriptor.asn1; \ + *oid = cur->descriptor.asn1; \ + *olen = cur->descriptor.asn1_len; \ return( 0 ); \ } \ cur++; \ @@ -112,12 +113,14 @@ int FN_NAME( ATTR1_TYPE ATTR1, const char **oid_str ) \ */ #define FN_OID_GET_OID_BY_ATTR2(FN_NAME, TYPE_T, LIST, ATTR1_TYPE, ATTR1, \ ATTR2_TYPE, ATTR2) \ -int FN_NAME( ATTR1_TYPE ATTR1, ATTR2_TYPE ATTR2, const char **oid_str ) \ +int FN_NAME( ATTR1_TYPE ATTR1, ATTR2_TYPE ATTR2, const char **oid , \ + size_t *olen ) \ { \ const TYPE_T *cur = LIST; \ while( cur->descriptor.asn1 != NULL ) { \ if( cur->ATTR1 == ATTR1 && cur->ATTR2 == ATTR2 ) { \ - *oid_str = cur->descriptor.asn1; \ + *oid = cur->descriptor.asn1; \ + *olen = cur->descriptor.asn1_len; \ return( 0 ); \ } \ cur++; \ diff --git a/library/rsa.c b/library/rsa.c index c39a338a3..10fd04099 100644 --- a/library/rsa.c +++ b/library/rsa.c @@ -907,10 +907,9 @@ int rsa_rsassa_pkcs1_v15_sign( rsa_context *ctx, if( md_info == NULL ) return( POLARSSL_ERR_RSA_BAD_INPUT_DATA ); - if( oid_get_oid_by_md( md_alg, &oid ) != 0 ) + if( oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 ) return( POLARSSL_ERR_RSA_BAD_INPUT_DATA ); - oid_size = strlen( oid ); nb_pad -= 10 + oid_size; hashlen = md_get_size( md_info ); diff --git a/library/x509write.c b/library/x509write.c index aed66240e..f4f3c4dcc 100644 --- a/library/x509write.c +++ b/library/x509write.c @@ -581,7 +581,8 @@ static int x509_write_names( unsigned char **p, unsigned char *start, } static int x509_write_sig( unsigned char **p, unsigned char *start, - const char *oid, unsigned char *sig, size_t size ) + const char *oid, size_t oid_len, + unsigned char *sig, size_t size ) { int ret; size_t len = 0; @@ -602,7 +603,7 @@ static int x509_write_sig( unsigned char **p, unsigned char *start, // Write OID // ASN1_CHK_ADD( len, asn1_write_algorithm_identifier( p, start, oid, - strlen( oid ) ) ); + oid_len ) ); return( len ); } @@ -693,6 +694,7 @@ int x509write_csr_der( x509write_csr *ctx, unsigned char *buf, size_t size ) { int ret; const char *sig_oid; + size_t sig_oid_len = 0; unsigned char *c, *c2; unsigned char hash[64]; unsigned char sig[POLARSSL_MPI_MAX_SIZE]; @@ -768,10 +770,12 @@ int x509write_csr_der( x509write_csr *ctx, unsigned char *buf, size_t size ) // Generate correct OID // - ret = oid_get_oid_by_sig_alg( POLARSSL_PK_RSA, ctx->md_alg, &sig_oid ); + ret = oid_get_oid_by_sig_alg( POLARSSL_PK_RSA, ctx->md_alg, &sig_oid, + &sig_oid_len ); c2 = buf + size - 1; - ASN1_CHK_ADD( sig_len, x509_write_sig( &c2, buf, sig_oid, sig, ctx->rsa->len ) ); + ASN1_CHK_ADD( sig_len, x509_write_sig( &c2, buf, sig_oid, sig_oid_len, + sig, ctx->rsa->len ) ); c2 -= len; memcpy( c2, c, len ); @@ -787,6 +791,7 @@ int x509write_crt_der( x509write_cert *ctx, unsigned char *buf, size_t size ) { int ret; const char *sig_oid; + size_t sig_oid_len = 0; unsigned char *c, *c2; unsigned char hash[64]; unsigned char sig[POLARSSL_MPI_MAX_SIZE]; @@ -798,7 +803,8 @@ int x509write_crt_der( x509write_cert *ctx, unsigned char *buf, size_t size ) // Generate correct OID // - ret = oid_get_oid_by_sig_alg( POLARSSL_PK_RSA, ctx->md_alg, &sig_oid ); + ret = oid_get_oid_by_sig_alg( POLARSSL_PK_RSA, ctx->md_alg, &sig_oid, + &sig_oid_len ); if( ret != 0 ) return( ret ); @@ -893,7 +899,8 @@ int x509write_crt_der( x509write_cert *ctx, unsigned char *buf, size_t size ) rsa_pkcs1_sign( ctx->issuer_key, NULL, NULL, RSA_PRIVATE, ctx->md_alg, 0, hash, sig ); c2 = buf + size - 1; - ASN1_CHK_ADD( sig_len, x509_write_sig( &c2, buf, sig_oid, sig, ctx->issuer_key->len ) ); + ASN1_CHK_ADD( sig_len, x509_write_sig( &c2, buf, sig_oid, sig_oid_len, + sig, ctx->issuer_key->len ) ); c2 -= len; memcpy( c2, c, len );