From 27b93ade6e1d9daf14ec3dd7a56cd627e84994e3 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 22ba1563e..6e8641fdb 100644 --- a/include/polarssl/x509.h +++ b/include/polarssl/x509.h @@ -271,6 +271,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, int critical, const unsigned char *val, size_t val_len ); diff --git a/library/x509.c b/library/x509.c index dbc2e0276..74a8f8e0d 100644 --- a/library/x509.c +++ b/library/x509.c @@ -810,6 +810,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 6cb3f5f72..964aa7e83 100644 --- a/library/x509_crl.c +++ b/library/x509_crl.c @@ -625,8 +625,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; @@ -682,35 +686,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 d222944bf..585972542 100644 --- a/library/x509_crt.c +++ b/library/x509_crt.c @@ -1124,8 +1124,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; @@ -1167,35 +1171,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 */ - 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 acb16af76..a5cef4c30 100644 --- a/library/x509_csr.c +++ b/library/x509_csr.c @@ -362,8 +362,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; @@ -380,35 +384,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 ) {