@@ -2300,28 +2300,24 @@ static int do_sign_init(EVP_MD_CTX *ctx, EVP_PKEY *pkey,
23002300}
23012301
23022302static int adapt_keyid_ext (X509 * cert , X509V3_CTX * ext_ctx ,
2303- const char * name , const char * value , int add_default )
2303+ const char * name , const char * value )
23042304{
23052305 const STACK_OF (X509_EXTENSION ) * exts = X509_get0_extensions (cert );
23062306 X509_EXTENSION * new_ext = X509V3_EXT_nconf (NULL , ext_ctx , name , value );
2307- int idx , rv = 0 ;
2307+ ASN1_OCTET_STRING * encoded ;
2308+ int disabled , idx , rv = 0 ;
23082309
23092310 if (new_ext == NULL )
23102311 return rv ;
23112312
23122313 idx = X509v3_get_ext_by_OBJ (exts , X509_EXTENSION_get_object (new_ext ), -1 );
23132314 if (idx >= 0 ) {
2314- X509_EXTENSION * found_ext = X509v3_get_ext (exts , idx );
2315- ASN1_OCTET_STRING * encoded = X509_EXTENSION_get_data (found_ext );
2316- int disabled = ASN1_STRING_length (encoded ) <= 2 ; /* indicating "none" */
2317-
2318- if (disabled ) {
2319- X509_delete_ext (cert , idx );
2320- X509_EXTENSION_free (found_ext );
2321- } /* else keep existing key identifier, which might be outdated */
2315+ /* keep existing key identifier, which might be outdated */
23222316 rv = 1 ;
23232317 } else {
2324- rv = !add_default || X509_add_ext (cert , new_ext , -1 );
2318+ encoded = X509_EXTENSION_get_data (new_ext );
2319+ disabled = ASN1_STRING_length (encoded ) <= 2 ; /* indicating "none" */
2320+ rv = disabled || X509_add_ext (cert , new_ext , -1 );
23252321 }
23262322 X509_EXTENSION_free (new_ext );
23272323 return rv ;
@@ -2337,30 +2333,40 @@ int cert_matches_key(const X509 *cert, const EVP_PKEY *pkey)
23372333 return match ;
23382334}
23392335
2340- /* Ensure RFC 5280 compliance, adapt keyIDs as needed, and sign the cert info */
2336+ /* Add default keyIDs as needed */
2337+ int add_X509_default_keyids (X509 * cert , EVP_PKEY * pkey , X509V3_CTX * ext_ctx )
2338+ {
2339+ int self_sign = 0 ;
2340+ int rv = 0 ;
2341+
2342+ /*
2343+ * Add default SKID before AKID such that AKID can make use of it
2344+ * in case the certificate is self-signed
2345+ */
2346+ if (X509_get_extension_flags (cert ) & EXFLAG_SI )
2347+ self_sign = cert_matches_key (cert , pkey );
2348+ /* Prevent X509_V_ERR_MISSING_SUBJECT_KEY_IDENTIFIER */
2349+ if (!adapt_keyid_ext (cert , ext_ctx , "subjectKeyIdentifier" , "hash" ))
2350+ goto end ;
2351+ /* Prevent X509_V_ERR_MISSING_AUTHORITY_KEY_IDENTIFIER */
2352+ if (!adapt_keyid_ext (cert , ext_ctx , "authorityKeyIdentifier" ,
2353+ self_sign ? "none" : "keyid, issuer" ))
2354+ goto end ;
2355+ rv = 1 ;
2356+ end :
2357+ return rv ;
2358+ }
2359+
2360+ /* Ensure RFC 5280 compliance, and sign the cert info */
23412361int do_X509_sign (X509 * cert , int force_v1 , EVP_PKEY * pkey , const char * md ,
23422362 STACK_OF (OPENSSL_STRING ) * sigopts , X509V3_CTX * ext_ctx )
23432363{
23442364 EVP_MD_CTX * mctx = EVP_MD_CTX_new ();
2345- int self_sign ;
23462365 int rv = 0 ;
23472366
23482367 if (!force_v1 ) {
23492368 if (!X509_set_version (cert , X509_VERSION_3 ))
23502369 goto end ;
2351-
2352- /*
2353- * Add default SKID before AKID such that AKID can make use of it
2354- * in case the certificate is self-signed
2355- */
2356- /* Prevent X509_V_ERR_MISSING_SUBJECT_KEY_IDENTIFIER */
2357- if (!adapt_keyid_ext (cert , ext_ctx , "subjectKeyIdentifier" , "hash" , 1 ))
2358- goto end ;
2359- /* Prevent X509_V_ERR_MISSING_AUTHORITY_KEY_IDENTIFIER */
2360- self_sign = cert_matches_key (cert , pkey );
2361- if (!adapt_keyid_ext (cert , ext_ctx , "authorityKeyIdentifier" ,
2362- "keyid, issuer" , !self_sign ))
2363- goto end ;
23642370 }
23652371 /* May add further measures for ensuring RFC 5280 compliance, see #19805 */
23662372
0 commit comments