1/*
2 * Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10#include <stdio.h>
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"
17#include "ext_dat.h"
18
19static STACK_OF(CONF_VALUE) *i2v_AUTHORITY_KEYID(X509V3_EXT_METHOD *method,
20                                                 AUTHORITY_KEYID *akeyid,
21                                                 STACK_OF(CONF_VALUE)
22                                                 *extlist);
23static AUTHORITY_KEYID *v2i_AUTHORITY_KEYID(X509V3_EXT_METHOD *method,
24                                            X509V3_CTX *ctx,
25                                            STACK_OF(CONF_VALUE) *values);
26
27const X509V3_EXT_METHOD ossl_v3_akey_id = {
28    NID_authority_key_identifier,
29    X509V3_EXT_MULTILINE, ASN1_ITEM_ref(AUTHORITY_KEYID),
30    0, 0, 0, 0,
31    0, 0,
32    (X509V3_EXT_I2V) i2v_AUTHORITY_KEYID,
33    (X509V3_EXT_V2I)v2i_AUTHORITY_KEYID,
34    0, 0,
35    NULL
36};
37
38static STACK_OF(CONF_VALUE) *i2v_AUTHORITY_KEYID(X509V3_EXT_METHOD *method,
39                                                 AUTHORITY_KEYID *akeyid,
40                                                 STACK_OF(CONF_VALUE)
41                                                 *extlist)
42{
43    char *tmp = NULL;
44    STACK_OF(CONF_VALUE) *origextlist = extlist, *tmpextlist;
45
46    if (akeyid->keyid) {
47        tmp = OPENSSL_buf2hexstr(akeyid->keyid->data, akeyid->keyid->length);
48        if (tmp == NULL) {
49            ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
50            return NULL;
51        }
52        if (!X509V3_add_value((akeyid->issuer || akeyid->serial) ? "keyid" : NULL,
53                              tmp, &extlist)) {
54            OPENSSL_free(tmp);
55            ERR_raise(ERR_LIB_X509V3, ERR_R_X509_LIB);
56            goto err;
57        }
58        OPENSSL_free(tmp);
59    }
60    if (akeyid->issuer) {
61        tmpextlist = i2v_GENERAL_NAMES(NULL, akeyid->issuer, extlist);
62        if (tmpextlist == NULL) {
63            ERR_raise(ERR_LIB_X509V3, ERR_R_X509_LIB);
64            goto err;
65        }
66        extlist = tmpextlist;
67    }
68    if (akeyid->serial) {
69        tmp = OPENSSL_buf2hexstr(akeyid->serial->data, akeyid->serial->length);
70        if (tmp == NULL) {
71            ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
72            goto err;
73        }
74        if (!X509V3_add_value("serial", tmp, &extlist)) {
75            OPENSSL_free(tmp);
76            goto err;
77        }
78        OPENSSL_free(tmp);
79    }
80    return extlist;
81 err:
82    if (origextlist == NULL)
83        sk_CONF_VALUE_pop_free(extlist, X509V3_conf_free);
84    return NULL;
85}
86
87/*-
88 * Currently two options:
89 * keyid: use the issuers subject keyid, the value 'always' means its is
90 * an error if the issuer certificate doesn't have a key id.
91 * issuer: use the issuers cert issuer and serial number. The default is
92 * to only use this if keyid is not present. With the option 'always'
93 * this is always included.
94 */
95
96static AUTHORITY_KEYID *v2i_AUTHORITY_KEYID(X509V3_EXT_METHOD *method,
97                                            X509V3_CTX *ctx,
98                                            STACK_OF(CONF_VALUE) *values)
99{
100    char keyid = 0, issuer = 0;
101    int i, n = sk_CONF_VALUE_num(values);
102    CONF_VALUE *cnf;
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;
108    X509_EXTENSION *ext;
109    X509 *issuer_cert;
110    int same_issuer, ss;
111    AUTHORITY_KEYID *akeyid = AUTHORITY_KEYID_new();
112
113    if (akeyid == NULL)
114        goto err;
115
116    if (n == 1 && strcmp(sk_CONF_VALUE_value(values, 0)->name, "none") == 0) {
117        return akeyid;
118    }
119
120    for (i = 0; i < n; i++) {
121        cnf = sk_CONF_VALUE_value(values, i);
122        if (strcmp(cnf->name, "keyid") == 0) {
123            keyid = 1;
124            if (cnf->value && strcmp(cnf->value, "always") == 0)
125                keyid = 2;
126        } else if (strcmp(cnf->name, "issuer") == 0) {
127            issuer = 1;
128            if (cnf->value && strcmp(cnf->value, "always") == 0)
129                issuer = 2;
130        } else {
131            ERR_raise_data(ERR_LIB_X509V3, X509V3_R_UNKNOWN_OPTION,
132                           "name=%s", cnf->name);
133            goto err;
134        }
135    }
136
137    if (ctx != NULL && (ctx->flags & X509V3_CTX_TEST) != 0)
138        return akeyid;
139
140    if (ctx == NULL) {
141        ERR_raise(ERR_LIB_X509V3, ERR_R_PASSED_NULL_PARAMETER);
142        goto err;
143    }
144    if ((issuer_cert = ctx->issuer_cert) == NULL) {
145        ERR_raise(ERR_LIB_X509V3, X509V3_R_NO_ISSUER_CERTIFICATE);
146        goto err;
147    }
148    same_issuer = ctx->subject_cert == ctx->issuer_cert;
149    ERR_set_mark();
150    if (ctx->issuer_pkey != NULL)
151        ss = X509_check_private_key(ctx->subject_cert, ctx->issuer_pkey);
152    else
153        ss = same_issuer;
154    ERR_pop_to_mark();
155
156    /* unless forced with "always", AKID is suppressed for self-signed certs */
157    if (keyid == 2 || (keyid == 1 && !ss)) {
158        /*
159         * prefer any pre-existing subject key identifier of the issuer cert
160         * except issuer cert is same as subject cert and is not self-signed
161         */
162        i = X509_get_ext_by_NID(issuer_cert, NID_subject_key_identifier, -1);
163        if (i >= 0 && (ext = X509_get_ext(issuer_cert, i)) != NULL
164            && !(same_issuer && !ss))
165            ikeyid = X509V3_EXT_d2i(ext);
166        if (ikeyid == NULL && same_issuer && ctx->issuer_pkey != NULL) {
167            /* generate fallback AKID, emulating s2i_skey_id(..., "hash") */
168            X509_PUBKEY *pubkey = NULL;
169
170            if (X509_PUBKEY_set(&pubkey, ctx->issuer_pkey))
171                ikeyid = ossl_x509_pubkey_hash(pubkey);
172            X509_PUBKEY_free(pubkey);
173        }
174        if ((keyid == 2 || issuer == 0)
175            && (ikeyid == NULL
176                || ASN1_STRING_length(ikeyid) <= 2) /* indicating "none" */) {
177            ERR_raise(ERR_LIB_X509V3, X509V3_R_UNABLE_TO_GET_ISSUER_KEYID);
178            goto err;
179        }
180    }
181
182    if (issuer == 2 || (issuer == 1 && ikeyid == NULL)) {
183        isname = X509_NAME_dup(X509_get_issuer_name(issuer_cert));
184        serial = ASN1_INTEGER_dup(X509_get0_serialNumber(issuer_cert));
185        if (isname == NULL || serial == NULL) {
186            ERR_raise(ERR_LIB_X509V3, X509V3_R_UNABLE_TO_GET_ISSUER_DETAILS);
187            goto err;
188        }
189    }
190
191    if (isname != NULL) {
192        if ((gens = sk_GENERAL_NAME_new_null()) == NULL
193            || (gen = GENERAL_NAME_new()) == NULL
194            || !sk_GENERAL_NAME_push(gens, gen)) {
195            ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
196            goto err;
197        }
198        gen->type = GEN_DIRNAME;
199        gen->d.dirn = isname;
200    }
201
202    akeyid->issuer = gens;
203    gen = NULL;
204    gens = NULL;
205    akeyid->serial = serial;
206    akeyid->keyid = ikeyid;
207
208    return akeyid;
209
210 err:
211    sk_GENERAL_NAME_free(gens);
212    GENERAL_NAME_free(gen);
213    X509_NAME_free(isname);
214    ASN1_INTEGER_free(serial);
215    ASN1_OCTET_STRING_free(ikeyid);
216    AUTHORITY_KEYID_free(akeyid);
217    return NULL;
218}
219