Skip to content

Commit d904905

Browse files
committed
[crypto] Use fingerprint when no common name is available for debug messages
Signed-off-by: Michael Brown <mcb30@ipxe.org>
1 parent bc8ca6b commit d904905

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

src/crypto/x509.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
2424
#include <errno.h>
2525
#include <assert.h>
2626
#include <ipxe/list.h>
27+
#include <ipxe/base16.h>
2728
#include <ipxe/asn1.h>
2829
#include <ipxe/crypto.h>
2930
#include <ipxe/md5.h>
@@ -120,14 +121,23 @@ FILE_LICENCE ( GPL2_OR_LATER );
120121
*/
121122
const char * x509_name ( struct x509_certificate *cert ) {
122123
struct asn1_cursor *common_name = &cert->subject.common_name;
124+
struct digest_algorithm *digest = &sha1_algorithm;
123125
static char buf[64];
126+
uint8_t fingerprint[ digest->digestsize ];
124127
size_t len;
125128

126129
len = common_name->len;
127-
if ( len > ( sizeof ( buf ) - 1 /* NUL */ ) )
128-
len = ( sizeof ( buf ) - 1 /* NUL */ );
129-
memcpy ( buf, common_name->data, len );
130-
buf[len] = '\0';
130+
if ( len ) {
131+
/* Certificate has a commonName: use that */
132+
if ( len > ( sizeof ( buf ) - 1 /* NUL */ ) )
133+
len = ( sizeof ( buf ) - 1 /* NUL */ );
134+
memcpy ( buf, common_name->data, len );
135+
buf[len] = '\0';
136+
} else {
137+
/* Certificate has no commonName: use SHA-1 fingerprint */
138+
x509_fingerprint ( cert, digest, fingerprint );
139+
base16_encode ( fingerprint, sizeof ( fingerprint ), buf );
140+
}
131141
return buf;
132142
}
133143

0 commit comments

Comments
 (0)