diff --git a/os_stub/cryptlib_mbedtls/pk/x509.c b/os_stub/cryptlib_mbedtls/pk/x509.c index e2120df66bc..afa85835799 100644 --- a/os_stub/cryptlib_mbedtls/pk/x509.c +++ b/os_stub/cryptlib_mbedtls/pk/x509.c @@ -1249,6 +1249,9 @@ bool libspdm_x509_get_extension_data(const uint8_t *cert, size_t cert_size, if (cert == NULL || cert_size == 0 || oid == NULL || oid_size == 0 || extension_data_size == NULL) { + if (extension_data_size != NULL) { + *extension_data_size = 0; + } return false; } @@ -1269,6 +1272,11 @@ bool libspdm_x509_get_extension_data(const uint8_t *cert, size_t cert_size, if (ret == 0) { status = libspdm_internal_x509_find_extension_data( ptr, end, oid, oid_size, &ptr, &obj_len); + if (!status) { + status = true; + *extension_data_size = 0; + goto cleanup; + } } if (status) { diff --git a/os_stub/cryptlib_openssl/pk/x509.c b/os_stub/cryptlib_openssl/pk/x509.c index 14bdc8504c7..c0803a6caf9 100644 --- a/os_stub/cryptlib_openssl/pk/x509.c +++ b/os_stub/cryptlib_openssl/pk/x509.c @@ -1351,22 +1351,23 @@ bool libspdm_x509_get_extension_data(const uint8_t *cert, size_t cert_size, if (cert == NULL || cert_size == 0 || oid == NULL || oid_size == 0 || extension_data_size == NULL) { + if (extension_data_size != NULL) { + *extension_data_size = 0; + } return false; } x509_cert = NULL; status = false; - /* Read DER-encoded X509 Certificate and Construct X509 object.*/ status = libspdm_x509_construct_certificate(cert, cert_size, (uint8_t **)&x509_cert); - if ((x509_cert == NULL) || (!status)) { + if (!status) { *extension_data_size = 0; goto cleanup; } - /* Retrieve extensions from certificate object.*/ extensions = X509_get0_extensions(x509_cert); @@ -1375,7 +1376,6 @@ bool libspdm_x509_get_extension_data(const uint8_t *cert, size_t cert_size, goto cleanup; } - /* Traverse extensions*/ status = false; @@ -1410,6 +1410,7 @@ bool libspdm_x509_get_extension_data(const uint8_t *cert, size_t cert_size, /* reset to 0 if not found */ oct_length = 0; } + if (status) { if (*extension_data_size < oct_length) { *extension_data_size = oct_length; @@ -1422,6 +1423,8 @@ bool libspdm_x509_get_extension_data(const uint8_t *cert, size_t cert_size, } *extension_data_size = oct_length; } else { + /* the cert extension is found, but the oid extension is not found; */ + status = true; *extension_data_size = 0; }