@@ -2334,28 +2334,24 @@ static int do_sign_init(EVP_MD_CTX *ctx, EVP_PKEY *pkey,
23342334}
23352335
23362336static int adapt_keyid_ext (X509 * cert , X509V3_CTX * ext_ctx ,
2337- const char * name , const char * value , int add_default )
2337+ const char * name , const char * value )
23382338{
23392339 const STACK_OF (X509_EXTENSION ) * exts = X509_get0_extensions (cert );
23402340 X509_EXTENSION * new_ext = X509V3_EXT_nconf (NULL , ext_ctx , name , value );
2341- int idx , rv = 0 ;
2341+ ASN1_OCTET_STRING * encoded ;
2342+ int disabled , idx , rv = 0 ;
23422343
23432344 if (new_ext == NULL )
23442345 return rv ;
23452346
23462347 idx = X509v3_get_ext_by_OBJ (exts , X509_EXTENSION_get_object (new_ext ), -1 );
23472348 if (idx >= 0 ) {
2348- X509_EXTENSION * found_ext = X509v3_get_ext (exts , idx );
2349- ASN1_OCTET_STRING * encoded = X509_EXTENSION_get_data (found_ext );
2350- int disabled = ASN1_STRING_length (encoded ) <= 2 ; /* indicating "none" */
2351-
2352- if (disabled ) {
2353- X509_delete_ext (cert , idx );
2354- X509_EXTENSION_free (found_ext );
2355- } /* else keep existing key identifier, which might be outdated */
2349+ /* keep existing key identifier, which might be outdated */
23562350 rv = 1 ;
23572351 } else {
2358- rv = !add_default || X509_add_ext (cert , new_ext , -1 );
2352+ encoded = X509_EXTENSION_get_data (new_ext );
2353+ disabled = ASN1_STRING_length (encoded ) <= 2 ; /* indicating "none" */
2354+ rv = disabled || X509_add_ext (cert , new_ext , -1 );
23592355 }
23602356 X509_EXTENSION_free (new_ext );
23612357 return rv ;
@@ -2371,30 +2367,40 @@ int cert_matches_key(const X509 *cert, const EVP_PKEY *pkey)
23712367 return match ;
23722368}
23732369
2374- /* Ensure RFC 5280 compliance, adapt keyIDs as needed, and sign the cert info */
2370+ /* Add default keyIDs as needed */
2371+ int add_X509_default_keyids (X509 * cert , EVP_PKEY * pkey , X509V3_CTX * ext_ctx )
2372+ {
2373+ int self_sign = 0 ;
2374+ int rv = 0 ;
2375+
2376+ /*
2377+ * Add default SKID before AKID such that AKID can make use of it
2378+ * in case the certificate is self-signed
2379+ */
2380+ if (X509_get_extension_flags (cert ) & EXFLAG_SI )
2381+ self_sign = cert_matches_key (cert , pkey );
2382+ /* Prevent X509_V_ERR_MISSING_SUBJECT_KEY_IDENTIFIER */
2383+ if (!adapt_keyid_ext (cert , ext_ctx , "subjectKeyIdentifier" , "hash" ))
2384+ goto end ;
2385+ /* Prevent X509_V_ERR_MISSING_AUTHORITY_KEY_IDENTIFIER */
2386+ if (!adapt_keyid_ext (cert , ext_ctx , "authorityKeyIdentifier" ,
2387+ self_sign ? "none" : "keyid, issuer" ))
2388+ goto end ;
2389+ rv = 1 ;
2390+ end :
2391+ return rv ;
2392+ }
2393+
2394+ /* Ensure RFC 5280 compliance, and sign the cert info */
23752395int do_X509_sign (X509 * cert , int force_v1 , EVP_PKEY * pkey , const char * md ,
23762396 STACK_OF (OPENSSL_STRING ) * sigopts , X509V3_CTX * ext_ctx )
23772397{
23782398 EVP_MD_CTX * mctx = EVP_MD_CTX_new ();
2379- int self_sign ;
23802399 int rv = 0 ;
23812400
23822401 if (!force_v1 ) {
23832402 if (!X509_set_version (cert , X509_VERSION_3 ))
23842403 goto end ;
2385-
2386- /*
2387- * Add default SKID before AKID such that AKID can make use of it
2388- * in case the certificate is self-signed
2389- */
2390- /* Prevent X509_V_ERR_MISSING_SUBJECT_KEY_IDENTIFIER */
2391- if (!adapt_keyid_ext (cert , ext_ctx , "subjectKeyIdentifier" , "hash" , 1 ))
2392- goto end ;
2393- /* Prevent X509_V_ERR_MISSING_AUTHORITY_KEY_IDENTIFIER */
2394- self_sign = cert_matches_key (cert , pkey );
2395- if (!adapt_keyid_ext (cert , ext_ctx , "authorityKeyIdentifier" ,
2396- "keyid, issuer" , !self_sign ))
2397- goto end ;
23982404 }
23992405 /* May add further measures for ensuring RFC 5280 compliance, see #19805 */
24002406
0 commit comments