2 * Copyright 1999-2022 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include "internal/cryptlib.h"
12 #include <openssl/conf.h>
13 #include <openssl/asn1.h>
14 #include <openssl/asn1t.h>
15 #include <openssl/x509v3.h>
16 #include "crypto/x509.h"
19 static STACK_OF(CONF_VALUE
) *i2v_AUTHORITY_KEYID(X509V3_EXT_METHOD
*method
,
20 AUTHORITY_KEYID
*akeyid
,
23 static AUTHORITY_KEYID
*v2i_AUTHORITY_KEYID(X509V3_EXT_METHOD
*method
,
25 STACK_OF(CONF_VALUE
) *values
);
27 const X509V3_EXT_METHOD ossl_v3_akey_id
= {
28 NID_authority_key_identifier
,
29 X509V3_EXT_MULTILINE
, ASN1_ITEM_ref(AUTHORITY_KEYID
),
32 (X509V3_EXT_I2V
) i2v_AUTHORITY_KEYID
,
33 (X509V3_EXT_V2I
)v2i_AUTHORITY_KEYID
,
38 static STACK_OF(CONF_VALUE
) *i2v_AUTHORITY_KEYID(X509V3_EXT_METHOD
*method
,
39 AUTHORITY_KEYID
*akeyid
,
44 STACK_OF(CONF_VALUE
) *origextlist
= extlist
, *tmpextlist
;
47 tmp
= i2s_ASN1_OCTET_STRING(NULL
, akeyid
->keyid
);
49 ERR_raise(ERR_LIB_X509V3
, ERR_R_MALLOC_FAILURE
);
52 if (!X509V3_add_value((akeyid
->issuer
|| akeyid
->serial
) ? "keyid" : NULL
,
55 ERR_raise(ERR_LIB_X509V3
, ERR_R_X509_LIB
);
61 tmpextlist
= i2v_GENERAL_NAMES(NULL
, akeyid
->issuer
, extlist
);
62 if (tmpextlist
== NULL
) {
63 ERR_raise(ERR_LIB_X509V3
, ERR_R_X509_LIB
);
69 tmp
= i2s_ASN1_OCTET_STRING(NULL
, akeyid
->serial
);
71 ERR_raise(ERR_LIB_X509V3
, ERR_R_MALLOC_FAILURE
);
74 if (!X509V3_add_value("serial", tmp
, &extlist
)) {
82 if (origextlist
== NULL
)
83 sk_CONF_VALUE_pop_free(extlist
, X509V3_conf_free
);
88 * Three explicit tags may be given, where 'keyid' and 'issuer' may be combined:
89 * 'none': do not add any authority key identifier.
90 * 'keyid': use the issuer's subject keyid; the option 'always' means its is
91 * an error if the issuer certificate doesn't have a subject key id.
92 * 'issuer': use the issuer's cert issuer and serial number. The default is
93 * to only use this if 'keyid' is not present. With the option 'always'
94 * this is always included.
96 static AUTHORITY_KEYID
*v2i_AUTHORITY_KEYID(X509V3_EXT_METHOD
*method
,
98 STACK_OF(CONF_VALUE
) *values
)
100 char keyid
= 0, issuer
= 0;
101 int i
, n
= sk_CONF_VALUE_num(values
);
103 ASN1_OCTET_STRING
*ikeyid
= NULL
;
104 X509_NAME
*isname
= NULL
;
105 GENERAL_NAMES
*gens
= NULL
;
106 GENERAL_NAME
*gen
= NULL
;
107 ASN1_INTEGER
*serial
= NULL
;
111 AUTHORITY_KEYID
*akeyid
= AUTHORITY_KEYID_new();
116 if (n
== 1 && strcmp(sk_CONF_VALUE_value(values
, 0)->name
, "none") == 0) {
120 for (i
= 0; i
< n
; i
++) {
121 cnf
= sk_CONF_VALUE_value(values
, i
);
122 if (cnf
->value
!= NULL
&& strcmp(cnf
->value
, "always") != 0) {
123 ERR_raise_data(ERR_LIB_X509V3
, X509V3_R_UNKNOWN_OPTION
,
124 "name=%s option=%s", cnf
->name
, cnf
->value
);
127 if (strcmp(cnf
->name
, "keyid") == 0 && keyid
== 0) {
129 if (cnf
->value
!= NULL
)
131 } else if (strcmp(cnf
->name
, "issuer") == 0 && issuer
== 0) {
133 if (cnf
->value
!= NULL
)
135 } else if (strcmp(cnf
->name
, "none") == 0
136 || strcmp(cnf
->name
, "keyid") == 0
137 || strcmp(cnf
->name
, "issuer") == 0) {
138 ERR_raise_data(ERR_LIB_X509V3
, X509V3_R_BAD_VALUE
,
139 "name=%s", cnf
->name
);
142 ERR_raise_data(ERR_LIB_X509V3
, X509V3_R_UNKNOWN_VALUE
,
143 "name=%s", cnf
->name
);
148 if (ctx
!= NULL
&& (ctx
->flags
& X509V3_CTX_TEST
) != 0)
152 ERR_raise(ERR_LIB_X509V3
, ERR_R_PASSED_NULL_PARAMETER
);
155 if ((issuer_cert
= ctx
->issuer_cert
) == NULL
) {
156 ERR_raise(ERR_LIB_X509V3
, X509V3_R_NO_ISSUER_CERTIFICATE
);
159 same_issuer
= ctx
->subject_cert
== ctx
->issuer_cert
;
161 if (ctx
->issuer_pkey
!= NULL
)
162 ss
= X509_check_private_key(ctx
->subject_cert
, ctx
->issuer_pkey
);
167 /* unless forced with "always", AKID is suppressed for self-signed certs */
168 if (keyid
== 2 || (keyid
== 1 && !ss
)) {
170 * prefer any pre-existing subject key identifier of the issuer cert
171 * except issuer cert is same as subject cert and is not self-signed
173 i
= X509_get_ext_by_NID(issuer_cert
, NID_subject_key_identifier
, -1);
174 if (i
>= 0 && (ext
= X509_get_ext(issuer_cert
, i
)) != NULL
175 && !(same_issuer
&& !ss
)) {
176 ikeyid
= X509V3_EXT_d2i(ext
);
177 if (ASN1_STRING_length(ikeyid
) == 0) /* indicating "none" */ {
178 ASN1_OCTET_STRING_free(ikeyid
);
182 if (ikeyid
== NULL
&& same_issuer
&& ctx
->issuer_pkey
!= NULL
) {
183 /* generate fallback AKID, emulating s2i_skey_id(..., "hash") */
184 X509_PUBKEY
*pubkey
= NULL
;
186 if (X509_PUBKEY_set(&pubkey
, ctx
->issuer_pkey
))
187 ikeyid
= ossl_x509_pubkey_hash(pubkey
);
188 X509_PUBKEY_free(pubkey
);
190 if (keyid
== 2 && ikeyid
== NULL
) {
191 ERR_raise(ERR_LIB_X509V3
, X509V3_R_UNABLE_TO_GET_ISSUER_KEYID
);
196 if (issuer
== 2 || (issuer
== 1 && !ss
&& ikeyid
== NULL
)) {
197 isname
= X509_NAME_dup(X509_get_issuer_name(issuer_cert
));
198 serial
= ASN1_INTEGER_dup(X509_get0_serialNumber(issuer_cert
));
199 if (isname
== NULL
|| serial
== NULL
) {
200 ERR_raise(ERR_LIB_X509V3
, X509V3_R_UNABLE_TO_GET_ISSUER_DETAILS
);
205 if (isname
!= NULL
) {
206 if ((gens
= sk_GENERAL_NAME_new_null()) == NULL
207 || (gen
= GENERAL_NAME_new()) == NULL
208 || !sk_GENERAL_NAME_push(gens
, gen
)) {
209 ERR_raise(ERR_LIB_X509V3
, ERR_R_MALLOC_FAILURE
);
212 gen
->type
= GEN_DIRNAME
;
213 gen
->d
.dirn
= isname
;
216 akeyid
->issuer
= gens
;
219 akeyid
->serial
= serial
;
220 akeyid
->keyid
= ikeyid
;
225 sk_GENERAL_NAME_free(gens
);
226 GENERAL_NAME_free(gen
);
227 X509_NAME_free(isname
);
228 ASN1_INTEGER_free(serial
);
229 ASN1_OCTET_STRING_free(ikeyid
);
230 AUTHORITY_KEYID_free(akeyid
);