2 * Copyright 1999-2020 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 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,
45 tmp = OPENSSL_buf2hexstr(akeyid->keyid->data, akeyid->keyid->length);
47 ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
50 X509V3_add_value((akeyid->issuer || akeyid->serial) ? "keyid" : NULL,
55 extlist = i2v_GENERAL_NAMES(NULL, akeyid->issuer, extlist);
57 tmp = OPENSSL_buf2hexstr(akeyid->serial->data, akeyid->serial->length);
59 ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
62 X509V3_add_value("serial", tmp, &extlist);
69 * Currently two options:
70 * keyid: use the issuers subject keyid, the value 'always' means its is
71 * an error if the issuer certificate doesn't have a key id.
72 * issuer: use the issuers cert issuer and serial number. The default is
73 * to only use this if keyid is not present. With the option 'always'
74 * this is always included.
77 static AUTHORITY_KEYID *v2i_AUTHORITY_KEYID(X509V3_EXT_METHOD *method,
79 STACK_OF(CONF_VALUE) *values)
81 char keyid = 0, issuer = 0;
82 int i, n = sk_CONF_VALUE_num(values);
84 ASN1_OCTET_STRING *ikeyid = NULL;
85 X509_NAME *isname = NULL;
86 GENERAL_NAMES *gens = NULL;
87 GENERAL_NAME *gen = NULL;
88 ASN1_INTEGER *serial = NULL;
91 AUTHORITY_KEYID *akeyid = AUTHORITY_KEYID_new();
96 if (n == 1 && strcmp(sk_CONF_VALUE_value(values, 0)->name, "none") == 0) {
100 for (i = 0; i < n; i++) {
101 cnf = sk_CONF_VALUE_value(values, i);
102 if (strcmp(cnf->name, "keyid") == 0) {
104 if (cnf->value && strcmp(cnf->value, "always") == 0)
106 } else if (strcmp(cnf->name, "issuer") == 0) {
108 if (cnf->value && strcmp(cnf->value, "always") == 0)
111 ERR_raise_data(ERR_LIB_X509V3, X509V3_R_UNKNOWN_OPTION,
112 "name=%s", cnf->name);
117 if (ctx != NULL && (ctx->flags & X509V3_CTX_TEST) != 0)
121 ERR_raise(ERR_LIB_X509V3, ERR_R_PASSED_NULL_PARAMETER);
124 if ((issuer_cert = ctx->issuer_cert) == NULL) {
125 ERR_raise(ERR_LIB_X509V3, X509V3_R_NO_ISSUER_CERTIFICATE);
130 /* prefer any pre-existing subject key identifier of the issuer cert */
131 i = X509_get_ext_by_NID(issuer_cert, NID_subject_key_identifier, -1);
132 if (i >= 0 && (ext = X509_get_ext(issuer_cert, i)) != NULL)
133 ikeyid = X509V3_EXT_d2i(ext);
134 if (ikeyid == NULL && ctx->issuer_pkey != NULL) { /* fallback */
135 /* generate AKID from scratch, emulating s2i_skey_id(..., "hash") */
136 X509_PUBKEY *pubkey = NULL;
138 if (X509_PUBKEY_set(&pubkey, ctx->issuer_pkey))
139 ikeyid = x509_pubkey_hash(pubkey);
140 X509_PUBKEY_free(pubkey);
142 if ((keyid == 2 || issuer == 0)
144 || ASN1_STRING_length(ikeyid) <= 2) /* indicating "none" */) {
145 ERR_raise(ERR_LIB_X509V3, X509V3_R_UNABLE_TO_GET_ISSUER_KEYID);
150 if (issuer == 2 || (issuer == 1 && ikeyid == NULL)) {
151 isname = X509_NAME_dup(X509_get_issuer_name(issuer_cert));
152 serial = ASN1_INTEGER_dup(X509_get0_serialNumber(issuer_cert));
153 if (isname == NULL || serial == NULL) {
154 ERR_raise(ERR_LIB_X509V3, X509V3_R_UNABLE_TO_GET_ISSUER_DETAILS);
159 if (isname != NULL) {
160 if ((gens = sk_GENERAL_NAME_new_null()) == NULL
161 || (gen = GENERAL_NAME_new()) == NULL
162 || !sk_GENERAL_NAME_push(gens, gen)) {
163 ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
166 gen->type = GEN_DIRNAME;
167 gen->d.dirn = isname;
170 akeyid->issuer = gens;
173 akeyid->serial = serial;
174 akeyid->keyid = ikeyid;
179 sk_GENERAL_NAME_free(gens);
180 GENERAL_NAME_free(gen);
181 X509_NAME_free(isname);
182 ASN1_INTEGER_free(serial);
183 ASN1_OCTET_STRING_free(ikeyid);
184 AUTHORITY_KEYID_free(akeyid);