@@ -2248,28 +2248,24 @@ static int do_sign_init(EVP_MD_CTX *ctx, EVP_PKEY *pkey,
22482248}
22492249
22502250static int adapt_keyid_ext (X509 * cert , X509V3_CTX * ext_ctx ,
2251- const char * name , const char * value , int add_default )
2251+ const char * name , const char * value )
22522252{
22532253 const STACK_OF (X509_EXTENSION ) * exts = X509_get0_extensions (cert );
22542254 X509_EXTENSION * new_ext = X509V3_EXT_nconf (NULL , ext_ctx , name , value );
2255- int idx , rv = 0 ;
2255+ ASN1_OCTET_STRING * encoded ;
2256+ int disabled , idx , rv = 0 ;
22562257
22572258 if (new_ext == NULL )
22582259 return rv ;
22592260
22602261 idx = X509v3_get_ext_by_OBJ (exts , X509_EXTENSION_get_object (new_ext ), -1 );
22612262 if (idx >= 0 ) {
2262- X509_EXTENSION * found_ext = X509v3_get_ext (exts , idx );
2263- ASN1_OCTET_STRING * encoded = X509_EXTENSION_get_data (found_ext );
2264- int disabled = ASN1_STRING_length (encoded ) <= 2 ; /* indicating "none" */
2265-
2266- if (disabled ) {
2267- X509_delete_ext (cert , idx );
2268- X509_EXTENSION_free (found_ext );
2269- } /* else keep existing key identifier, which might be outdated */
2263+ /* keep existing key identifier, which might be outdated */
22702264 rv = 1 ;
22712265 } else {
2272- rv = !add_default || X509_add_ext (cert , new_ext , -1 );
2266+ encoded = X509_EXTENSION_get_data (new_ext );
2267+ disabled = ASN1_STRING_length (encoded ) <= 2 ; /* indicating "none" */
2268+ rv = disabled || X509_add_ext (cert , new_ext , -1 );
22732269 }
22742270 X509_EXTENSION_free (new_ext );
22752271 return rv ;
@@ -2285,30 +2281,40 @@ int cert_matches_key(const X509 *cert, const EVP_PKEY *pkey)
22852281 return match ;
22862282}
22872283
2288- /* Ensure RFC 5280 compliance, adapt keyIDs as needed, and sign the cert info */
2284+ /* Add default keyIDs as needed */
2285+ int add_X509_default_keyids (X509 * cert , EVP_PKEY * pkey , X509V3_CTX * ext_ctx )
2286+ {
2287+ int self_sign = 0 ;
2288+ int rv = 0 ;
2289+
2290+ /*
2291+ * Add default SKID before AKID such that AKID can make use of it
2292+ * in case the certificate is self-signed
2293+ */
2294+ if (X509_get_extension_flags (cert ) & EXFLAG_SI )
2295+ self_sign = cert_matches_key (cert , pkey );
2296+ /* Prevent X509_V_ERR_MISSING_SUBJECT_KEY_IDENTIFIER */
2297+ if (!adapt_keyid_ext (cert , ext_ctx , "subjectKeyIdentifier" , "hash" ))
2298+ goto end ;
2299+ /* Prevent X509_V_ERR_MISSING_AUTHORITY_KEY_IDENTIFIER */
2300+ if (!adapt_keyid_ext (cert , ext_ctx , "authorityKeyIdentifier" ,
2301+ self_sign ? "none" : "keyid, issuer" ))
2302+ goto end ;
2303+ rv = 1 ;
2304+ end :
2305+ return rv ;
2306+ }
2307+
2308+ /* Ensure RFC 5280 compliance, and sign the cert info */
22892309int do_X509_sign (X509 * cert , int force_v1 , EVP_PKEY * pkey , const char * md ,
22902310 STACK_OF (OPENSSL_STRING ) * sigopts , X509V3_CTX * ext_ctx )
22912311{
22922312 EVP_MD_CTX * mctx = EVP_MD_CTX_new ();
2293- int self_sign ;
22942313 int rv = 0 ;
22952314
22962315 if (!force_v1 ) {
22972316 if (!X509_set_version (cert , X509_VERSION_3 ))
22982317 goto end ;
2299-
2300- /*
2301- * Add default SKID before AKID such that AKID can make use of it
2302- * in case the certificate is self-signed
2303- */
2304- /* Prevent X509_V_ERR_MISSING_SUBJECT_KEY_IDENTIFIER */
2305- if (!adapt_keyid_ext (cert , ext_ctx , "subjectKeyIdentifier" , "hash" , 1 ))
2306- goto end ;
2307- /* Prevent X509_V_ERR_MISSING_AUTHORITY_KEY_IDENTIFIER */
2308- self_sign = cert_matches_key (cert , pkey );
2309- if (!adapt_keyid_ext (cert , ext_ctx , "authorityKeyIdentifier" ,
2310- "keyid, issuer" , !self_sign ))
2311- goto end ;
23122318 }
23132319 /* May add further measures for ensuring RFC 5280 compliance, see #19805 */
23142320
0 commit comments