Skip to content

Commit

Permalink
[crypto] Add x509_is_self_signed() helper function
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Brown <[email protected]>
  • Loading branch information
mcb30 committed Feb 15, 2024
1 parent 3e721e0 commit 943d75b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/include/ipxe/x509.h
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,16 @@ x509_root_put ( struct x509_root *root ) {
ref_put ( &root->refcnt );
}

/**
* Check if X.509 certificate is self-signed
*
* @v cert X.509 certificate
* @ret is_self_signed X.509 certificate is self-signed
*/
static inline int x509_is_self_signed ( struct x509_certificate *cert ) {
return ( asn1_compare ( &cert->issuer.raw, &cert->subject.raw ) == 0 );
}

extern const char * x509_name ( struct x509_certificate *cert );
extern int x509_parse ( struct x509_certificate *cert,
const struct asn1_cursor *raw );
Expand Down
2 changes: 1 addition & 1 deletion src/net/validator.c
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ static void validator_step ( struct validator *validator ) {
* nothing more to do.
*/
last = x509_last ( validator->chain );
if ( asn1_compare ( &last->issuer.raw, &last->subject.raw ) == 0 ) {
if ( x509_is_self_signed ( last ) ) {
validator_finished ( validator, rc );
return;
}
Expand Down
4 changes: 4 additions & 0 deletions src/tests/x509_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,10 @@ static void x509_test_exec ( void ) {
x509_validate_chain_fail_ok ( &server_chain, test_time,
&empty_store, &test_root );

/* Check self-signedess */
ok ( x509_is_self_signed ( root_crt.cert ) );
ok ( ! x509_is_self_signed ( intermediate_crt.cert ) );

/* Sanity check */
assert ( list_empty ( &empty_store.links ) );

Expand Down

0 comments on commit 943d75b

Please sign in to comment.