From cac31eed9e70cb7f8d9727af94c373c877b88896 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Sat, 25 Jan 2014 11:50:59 +0100 Subject: [PATCH] Factor common code for printing sig_alg --- include/polarssl/x509.h | 2 ++ library/x509.c | 46 +++++++++++++++++++++++++++++++++++++++++ library/x509_crl.c | 34 ++++++------------------------ library/x509_crt.c | 34 ++++++------------------------ library/x509_csr.c | 34 ++++++------------------------ 5 files changed, 66 insertions(+), 84 deletions(-) diff --git a/include/polarssl/x509.h b/include/polarssl/x509.h index 16b1edae2..a21c1f0e0 100644 --- a/include/polarssl/x509.h +++ b/include/polarssl/x509.h @@ -293,6 +293,8 @@ int x509_get_serial( unsigned char **p, const unsigned char *end, int x509_get_ext( unsigned char **p, const unsigned char *end, x509_buf *ext, int tag ); int x509_load_file( const char *path, unsigned char **buf, size_t *n ); +int x509_sig_alg_gets( char *buf, size_t size, const x509_buf *sig_oid, + pk_type_t pk_alg, const x509_buf *sig_params ); int x509_key_size_helper( char *buf, size_t size, const char *name ); int x509_string_to_names( asn1_named_data **head, const char *name ); int x509_set_extension( asn1_named_data **head, const char *oid, size_t oid_len, diff --git a/library/x509.c b/library/x509.c index 3e93c3a2a..02756f87b 100644 --- a/library/x509.c +++ b/library/x509.c @@ -815,6 +815,52 @@ int x509_serial_gets( char *buf, size_t size, const x509_buf *serial ) return( (int) ( size - n ) ); } +/* + * Helper for writing signature alrogithms + */ +int x509_sig_alg_gets( char *buf, size_t size, const x509_buf *sig_oid, + pk_type_t pk_alg, const x509_buf *sig_params ) +{ + int ret; + char *p = buf; + size_t n = size; + const char *desc = NULL; + + ret = oid_get_sig_alg_desc( sig_oid, &desc ); + if( ret != 0 ) + ret = snprintf( p, n, "???" ); + else + ret = snprintf( p, n, "%s", desc ); + SAFE_SNPRINTF(); + +#if defined(POLARSSL_RSASSA_PSS_CERTIFICATES) + if( pk_alg == POLARSSL_PK_RSASSA_PSS ) + { + md_type_t md_alg, mgf_md; + const md_info_t *md_info, *mgf_md_info; + int salt_len, trailer_field; + + if( ( ret = x509_get_rsassa_pss_params( sig_params, + &md_alg, &mgf_md, &salt_len, &trailer_field ) ) != 0 ) + return( ret ); + + md_info = md_info_from_type( md_alg ); + mgf_md_info = md_info_from_type( mgf_md ); + + ret = snprintf( p, n, " (%s, MGF1-%s, 0x%02X, %d)", + md_info ? md_info->name : "???", + mgf_md_info ? mgf_md_info->name : "???", + salt_len, trailer_field ); + SAFE_SNPRINTF(); + } +#else + ((void) pk_alg); + ((void) sig_params); +#endif /* POLARSSL_RSASSA_PSS_CERTIFICATES */ + + return( (int) size - n ); +} + /* * Helper for writing "RSA key size", "EC key size", etc */ diff --git a/library/x509_crl.c b/library/x509_crl.c index f2bd0c14b..986fc26d4 100644 --- a/library/x509_crl.c +++ b/library/x509_crl.c @@ -631,8 +631,12 @@ int x509_crl_info( char *buf, size_t size, const char *prefix, int ret; size_t n; char *p; - const char *desc; const x509_crl_entry *entry; +#if defined(POLARSSL_RSASSA_PSS_CERTIFICATES) + const x509_buf *sig_params = &crl->sig_params; +#else + const x509_buf *sig_params = NULL; +#endif p = buf; n = size; @@ -688,35 +692,9 @@ int x509_crl_info( char *buf, size_t size, const char *prefix, ret = snprintf( p, n, "\n%ssigned using : ", prefix ); SAFE_SNPRINTF(); - ret = oid_get_sig_alg_desc( &crl->sig_oid1, &desc ); - if( ret != 0 ) - ret = snprintf( p, n, "???" ); - else - ret = snprintf( p, n, "%s", desc ); + ret = x509_sig_alg_gets( p, n, &crl->sig_oid1, crl->sig_pk, sig_params ); SAFE_SNPRINTF(); -#if defined(POLARSSL_RSASSA_PSS_CERTIFICATES) - if( crl->sig_pk == POLARSSL_PK_RSASSA_PSS ) - { - md_type_t md_alg, mgf_md; - const md_info_t *md_info, *mgf_md_info; - int salt_len, trailer_field; - - if( ( ret = x509_get_rsassa_pss_params( &crl->sig_params, - &md_alg, &mgf_md, &salt_len, &trailer_field ) ) != 0 ) - return( ret ); - - md_info = md_info_from_type( md_alg ); - mgf_md_info = md_info_from_type( mgf_md ); - - ret = snprintf( p, n, " (%s, MGF1-%s, 0x%02X, %d)", - md_info ? md_info->name : "???", - mgf_md_info ? mgf_md_info->name : "???", - salt_len, trailer_field ); - SAFE_SNPRINTF(); - } -#endif /* POLARSSL_RSASSA_PSS_CERTIFICATES */ - ret = snprintf( p, n, "\n" ); SAFE_SNPRINTF(); diff --git a/library/x509_crt.c b/library/x509_crt.c index 3fda8641f..de95c0f59 100644 --- a/library/x509_crt.c +++ b/library/x509_crt.c @@ -1258,8 +1258,12 @@ int x509_crt_info( char *buf, size_t size, const char *prefix, int ret; size_t n; char *p; - const char *desc = NULL; char key_size_str[BEFORE_COLON]; +#if defined(POLARSSL_RSASSA_PSS_CERTIFICATES) + const x509_buf *sig_params = &crt->sig_params; +#else + const x509_buf *sig_params = NULL; +#endif p = buf; n = size; @@ -1301,35 +1305,9 @@ int x509_crt_info( char *buf, size_t size, const char *prefix, ret = snprintf( p, n, "\n%ssigned using : ", prefix ); SAFE_SNPRINTF(); - ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc ); - if( ret != 0 ) - ret = snprintf( p, n, "???" ); - else - ret = snprintf( p, n, "%s", desc ); + ret = x509_sig_alg_gets( p, n, &crt->sig_oid1, crt->sig_pk, sig_params ); SAFE_SNPRINTF(); -#if defined(POLARSSL_RSASSA_PSS_CERTIFICATES) - if( crt->sig_pk == POLARSSL_PK_RSASSA_PSS ) - { - md_type_t md_alg, mgf_md; - const md_info_t *md_info, *mgf_md_info; - int salt_len, trailer_field; - - if( ( ret = x509_get_rsassa_pss_params( &crt->sig_params, - &md_alg, &mgf_md, &salt_len, &trailer_field ) ) != 0 ) - return( ret ); - - md_info = md_info_from_type( md_alg ); - mgf_md_info = md_info_from_type( mgf_md ); - - ret = snprintf( p, n, " (%s, MGF1-%s, 0x%02X, %d)", - md_info ? md_info->name : "???", - mgf_md_info ? mgf_md_info->name : "???", - salt_len, trailer_field ); - SAFE_SNPRINTF(); - } -#endif /* POLARSSL_RSASSA_PSS_CERTIFICATES */ - /* Key size */ if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON, pk_get_name( &crt->pk ) ) ) != 0 ) diff --git a/library/x509_csr.c b/library/x509_csr.c index a0d4c363a..082e46191 100644 --- a/library/x509_csr.c +++ b/library/x509_csr.c @@ -366,8 +366,12 @@ int x509_csr_info( char *buf, size_t size, const char *prefix, int ret; size_t n; char *p; - const char *desc; char key_size_str[BEFORE_COLON]; +#if defined(POLARSSL_RSASSA_PSS_CERTIFICATES) + const x509_buf *sig_params = &csr->sig_params; +#else + const x509_buf *sig_params = NULL; +#endif p = buf; n = size; @@ -384,35 +388,9 @@ int x509_csr_info( char *buf, size_t size, const char *prefix, ret = snprintf( p, n, "\n%ssigned using : ", prefix ); SAFE_SNPRINTF(); - ret = oid_get_sig_alg_desc( &csr->sig_oid, &desc ); - if( ret != 0 ) - ret = snprintf( p, n, "???" ); - else - ret = snprintf( p, n, "%s", desc ); + ret = x509_sig_alg_gets( p, n, &csr->sig_oid, csr->sig_pk, sig_params ); SAFE_SNPRINTF(); -#if defined(POLARSSL_RSASSA_PSS_CERTIFICATES) - if( csr->sig_pk == POLARSSL_PK_RSASSA_PSS ) - { - md_type_t md_alg, mgf_md; - const md_info_t *md_info, *mgf_md_info; - int salt_len, trailer_field; - - if( ( ret = x509_get_rsassa_pss_params( &csr->sig_params, - &md_alg, &mgf_md, &salt_len, &trailer_field ) ) != 0 ) - return( ret ); - - md_info = md_info_from_type( md_alg ); - mgf_md_info = md_info_from_type( mgf_md ); - - ret = snprintf( p, n, " (%s, MGF1-%s, 0x%02X, %d)", - md_info ? md_info->name : "???", - mgf_md_info ? mgf_md_info->name : "???", - salt_len, trailer_field ); - SAFE_SNPRINTF(); - } -#endif /* POLARSSL_RSASSA_PSS_CERTIFICATES */ - if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON, pk_get_name( &csr->pk ) ) ) != 0 ) {