0b1283f0afaf80c11b8d67bf524ae3656a9250f3
[openssl.git] / crypto / x509 / v3_akid.c
1 /*
2  * Copyright 1999-2020 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
19 static STACK_OF(CONF_VALUE) *i2v_AUTHORITY_KEYID(X509V3_EXT_METHOD *method,
20                                                  AUTHORITY_KEYID *akeyid,
21                                                  STACK_OF(CONF_VALUE)
22                                                  *extlist);
23 static AUTHORITY_KEYID *v2i_AUTHORITY_KEYID(X509V3_EXT_METHOD *method,
24                                             X509V3_CTX *ctx,
25                                             STACK_OF(CONF_VALUE) *values);
26
27 const X509V3_EXT_METHOD 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
38 static 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;
44     if (akeyid->keyid) {
45         tmp = OPENSSL_buf2hexstr(akeyid->keyid->data, akeyid->keyid->length);
46         if (tmp == NULL) {
47             ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
48             return NULL;
49         }
50         X509V3_add_value((akeyid->issuer || akeyid->serial) ? "keyid" : NULL,
51                          tmp, &extlist);
52         OPENSSL_free(tmp);
53     }
54     if (akeyid->issuer)
55         extlist = i2v_GENERAL_NAMES(NULL, akeyid->issuer, extlist);
56     if (akeyid->serial) {
57         tmp = OPENSSL_buf2hexstr(akeyid->serial->data, akeyid->serial->length);
58         if (tmp == NULL) {
59             ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
60             return NULL;
61         }
62         X509V3_add_value("serial", tmp, &extlist);
63         OPENSSL_free(tmp);
64     }
65     return extlist;
66 }
67
68 /*-
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.
75  */
76
77 static AUTHORITY_KEYID *v2i_AUTHORITY_KEYID(X509V3_EXT_METHOD *method,
78                                             X509V3_CTX *ctx,
79                                             STACK_OF(CONF_VALUE) *values)
80 {
81     char keyid = 0, issuer = 0;
82     int i, n = sk_CONF_VALUE_num(values);
83     CONF_VALUE *cnf;
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;
89     X509_EXTENSION *ext;
90     X509 *issuer_cert;
91     AUTHORITY_KEYID *akeyid = AUTHORITY_KEYID_new();
92
93     if (akeyid == NULL)
94         goto err;
95
96     if (n == 1 && strcmp(sk_CONF_VALUE_value(values, 0)->name, "none") == 0) {
97         return akeyid;
98     }
99
100     for (i = 0; i < n; i++) {
101         cnf = sk_CONF_VALUE_value(values, i);
102         if (strcmp(cnf->name, "keyid") == 0) {
103             keyid = 1;
104             if (cnf->value && strcmp(cnf->value, "always") == 0)
105                 keyid = 2;
106         } else if (strcmp(cnf->name, "issuer") == 0) {
107             issuer = 1;
108             if (cnf->value && strcmp(cnf->value, "always") == 0)
109                 issuer = 2;
110         } else {
111             ERR_raise_data(ERR_LIB_X509V3, X509V3_R_UNKNOWN_OPTION,
112                            "name=%s", cnf->name);
113             goto err;
114         }
115     }
116
117     if (ctx != NULL && (ctx->flags & X509V3_CTX_TEST) != 0)
118         return akeyid;
119
120     if (ctx == NULL) {
121         ERR_raise(ERR_LIB_X509V3, ERR_R_PASSED_NULL_PARAMETER);
122         goto err;
123     }
124     if ((issuer_cert = ctx->issuer_cert) == NULL) {
125         ERR_raise(ERR_LIB_X509V3, X509V3_R_NO_ISSUER_CERTIFICATE);
126         goto err;
127     }
128
129     if (keyid != 0) {
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;
137
138             if (X509_PUBKEY_set(&pubkey, ctx->issuer_pkey))
139                 ikeyid = x509_pubkey_hash(pubkey);
140             X509_PUBKEY_free(pubkey);
141         }
142         if ((keyid == 2 || issuer == 0)
143             && (ikeyid == NULL
144                 || ASN1_STRING_length(ikeyid) <= 2) /* indicating "none" */) {
145             ERR_raise(ERR_LIB_X509V3, X509V3_R_UNABLE_TO_GET_ISSUER_KEYID);
146             goto err;
147         }
148     }
149
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);
155             goto err;
156         }
157     }
158
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);
164             goto err;
165         }
166         gen->type = GEN_DIRNAME;
167         gen->d.dirn = isname;
168     }
169
170     akeyid->issuer = gens;
171     gen = NULL;
172     gens = NULL;
173     akeyid->serial = serial;
174     akeyid->keyid = ikeyid;
175
176     return akeyid;
177
178  err:
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);
185     return NULL;
186 }