diff --git a/include/polarssl/x509.h b/include/polarssl/x509.h index 6f0fe02709..1f062e9b74 100644 --- a/include/polarssl/x509.h +++ b/include/polarssl/x509.h @@ -593,6 +593,19 @@ int x509parse_dhmfile( dhm_context *rsa, const char *path ); */ int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn ); +/** + * \brief Store the certificate serial in printable form into buf; + * no more than size characters will be written. + * + * \param buf Buffer to write to + * \param size Maximum size of buffer + * \param serial The X509 serial to represent + * + * \return The amount of data written to the buffer, or -1 in + * case of an error. + */ +int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial ); + /** * \brief Returns an informational string about the * certificate. diff --git a/library/x509parse.c b/library/x509parse.c index 6dae943048..b37c4c2176 100644 --- a/library/x509parse.c +++ b/library/x509parse.c @@ -2305,13 +2305,38 @@ int x509parse_dn_gets( char *buf, size_t size, const x509_name *dn ) return( size - n ); } +/* + * Store the serial in printable form into buf; no more + * than size characters will be written + */ +int x509parse_serial_gets( char *buf, size_t size, const x509_buf *serial ) +{ + int i, ret, nr, n; + char *p; + + p = buf; + n = size; + + nr = ( serial->len <= 32 ) + ? serial->len : 32; + + for( i = 0; i < nr; i++ ) + { + ret = snprintf( p, n, "%02X%s", + serial->p[i], ( i < nr - 1 ) ? ":" : "" ); + SAFE_SNPRINTF(); + } + + return( size - n ); +} + /* * Return an informational string about the certificate. */ int x509parse_cert_info( char *buf, size_t size, const char *prefix, const x509_cert *crt ) { - int i, n, nr, ret; + int n, ret; char *p; p = buf; @@ -2324,15 +2349,8 @@ int x509parse_cert_info( char *buf, size_t size, const char *prefix, prefix ); SAFE_SNPRINTF(); - nr = ( crt->serial.len <= 32 ) - ? crt->serial.len : 32; - - for( i = 0; i < nr; i++ ) - { - ret = snprintf( p, n, "%02X%s", - crt->serial.p[i], ( i < nr - 1 ) ? ":" : "" ); - SAFE_SNPRINTF(); - } + ret = x509parse_serial_gets( p, n, &crt->serial); + SAFE_SNPRINTF(); ret = snprintf( p, n, "\n%sissuer name : ", prefix ); SAFE_SNPRINTF();