/src/openssl30/crypto/x509/v3_akid.c
| Line | Count | Source (jump to first uncovered line) | 
| 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 |  |  | 
| 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 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 |  |  | 
| 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 | 15.5k | { | 
| 43 | 15.5k |     char *tmp = NULL; | 
| 44 | 15.5k |     STACK_OF(CONF_VALUE) *origextlist = extlist, *tmpextlist; | 
| 45 |  |  | 
| 46 | 15.5k |     if (akeyid->keyid) { | 
| 47 | 5.18k |         tmp = OPENSSL_buf2hexstr(akeyid->keyid->data, akeyid->keyid->length); | 
| 48 | 5.18k |         if (tmp == NULL) { | 
| 49 | 0 |             ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE); | 
| 50 | 0 |             return NULL; | 
| 51 | 0 |         } | 
| 52 | 5.18k |         if (!X509V3_add_value((akeyid->issuer || akeyid->serial) ? "keyid" : NULL, | 
| 53 | 5.18k |                               tmp, &extlist)) { | 
| 54 | 0 |             OPENSSL_free(tmp); | 
| 55 | 0 |             ERR_raise(ERR_LIB_X509V3, ERR_R_X509_LIB); | 
| 56 | 0 |             goto err; | 
| 57 | 0 |         } | 
| 58 | 5.18k |         OPENSSL_free(tmp); | 
| 59 | 5.18k |     } | 
| 60 | 15.5k |     if (akeyid->issuer) { | 
| 61 | 10.2k |         tmpextlist = i2v_GENERAL_NAMES(NULL, akeyid->issuer, extlist); | 
| 62 | 10.2k |         if (tmpextlist == NULL) { | 
| 63 | 1.81k |             ERR_raise(ERR_LIB_X509V3, ERR_R_X509_LIB); | 
| 64 | 1.81k |             goto err; | 
| 65 | 1.81k |         } | 
| 66 | 8.43k |         extlist = tmpextlist; | 
| 67 | 8.43k |     } | 
| 68 | 13.7k |     if (akeyid->serial) { | 
| 69 | 1.49k |         tmp = OPENSSL_buf2hexstr(akeyid->serial->data, akeyid->serial->length); | 
| 70 | 1.49k |         if (tmp == NULL) { | 
| 71 | 0 |             ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE); | 
| 72 | 0 |             goto err; | 
| 73 | 0 |         } | 
| 74 | 1.49k |         if (!X509V3_add_value("serial", tmp, &extlist)) { | 
| 75 | 0 |             OPENSSL_free(tmp); | 
| 76 | 0 |             goto err; | 
| 77 | 0 |         } | 
| 78 | 1.49k |         OPENSSL_free(tmp); | 
| 79 | 1.49k |     } | 
| 80 | 13.7k |     return extlist; | 
| 81 | 1.81k |  err: | 
| 82 | 1.81k |     if (origextlist == NULL) | 
| 83 | 1.81k |         sk_CONF_VALUE_pop_free(extlist, X509V3_conf_free); | 
| 84 | 1.81k |     return NULL; | 
| 85 | 13.7k | } | 
| 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 |  |  | 
| 96 |  | static AUTHORITY_KEYID *v2i_AUTHORITY_KEYID(X509V3_EXT_METHOD *method, | 
| 97 |  |                                             X509V3_CTX *ctx, | 
| 98 |  |                                             STACK_OF(CONF_VALUE) *values) | 
| 99 | 0 | { | 
| 100 | 0 |     char keyid = 0, issuer = 0; | 
| 101 | 0 |     int i, n = sk_CONF_VALUE_num(values); | 
| 102 | 0 |     CONF_VALUE *cnf; | 
| 103 | 0 |     ASN1_OCTET_STRING *ikeyid = NULL; | 
| 104 | 0 |     X509_NAME *isname = NULL; | 
| 105 | 0 |     GENERAL_NAMES *gens = NULL; | 
| 106 | 0 |     GENERAL_NAME *gen = NULL; | 
| 107 | 0 |     ASN1_INTEGER *serial = NULL; | 
| 108 | 0 |     X509_EXTENSION *ext; | 
| 109 | 0 |     X509 *issuer_cert; | 
| 110 | 0 |     int same_issuer, ss; | 
| 111 | 0 |     AUTHORITY_KEYID *akeyid = AUTHORITY_KEYID_new(); | 
| 112 |  | 
 | 
| 113 | 0 |     if (akeyid == NULL) | 
| 114 | 0 |         goto err; | 
| 115 |  |  | 
| 116 | 0 |     if (n == 1 && strcmp(sk_CONF_VALUE_value(values, 0)->name, "none") == 0) { | 
| 117 | 0 |         return akeyid; | 
| 118 | 0 |     } | 
| 119 |  |  | 
| 120 | 0 |     for (i = 0; i < n; i++) { | 
| 121 | 0 |         cnf = sk_CONF_VALUE_value(values, i); | 
| 122 | 0 |         if (strcmp(cnf->name, "keyid") == 0) { | 
| 123 | 0 |             keyid = 1; | 
| 124 | 0 |             if (cnf->value && strcmp(cnf->value, "always") == 0) | 
| 125 | 0 |                 keyid = 2; | 
| 126 | 0 |         } else if (strcmp(cnf->name, "issuer") == 0) { | 
| 127 | 0 |             issuer = 1; | 
| 128 | 0 |             if (cnf->value && strcmp(cnf->value, "always") == 0) | 
| 129 | 0 |                 issuer = 2; | 
| 130 | 0 |         } else { | 
| 131 | 0 |             ERR_raise_data(ERR_LIB_X509V3, X509V3_R_UNKNOWN_OPTION, | 
| 132 | 0 |                            "name=%s", cnf->name); | 
| 133 | 0 |             goto err; | 
| 134 | 0 |         } | 
| 135 | 0 |     } | 
| 136 |  |  | 
| 137 | 0 |     if (ctx != NULL && (ctx->flags & X509V3_CTX_TEST) != 0) | 
| 138 | 0 |         return akeyid; | 
| 139 |  |  | 
| 140 | 0 |     if (ctx == NULL) { | 
| 141 | 0 |         ERR_raise(ERR_LIB_X509V3, ERR_R_PASSED_NULL_PARAMETER); | 
| 142 | 0 |         goto err; | 
| 143 | 0 |     } | 
| 144 | 0 |     if ((issuer_cert = ctx->issuer_cert) == NULL) { | 
| 145 | 0 |         ERR_raise(ERR_LIB_X509V3, X509V3_R_NO_ISSUER_CERTIFICATE); | 
| 146 | 0 |         goto err; | 
| 147 | 0 |     } | 
| 148 | 0 |     same_issuer = ctx->subject_cert == ctx->issuer_cert; | 
| 149 | 0 |     ERR_set_mark(); | 
| 150 | 0 |     if (ctx->issuer_pkey != NULL) | 
| 151 | 0 |         ss = X509_check_private_key(ctx->subject_cert, ctx->issuer_pkey); | 
| 152 | 0 |     else | 
| 153 | 0 |         ss = same_issuer; | 
| 154 | 0 |     ERR_pop_to_mark(); | 
| 155 |  |  | 
| 156 |  |     /* unless forced with "always", AKID is suppressed for self-signed certs */ | 
| 157 | 0 |     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 | 0 |         i = X509_get_ext_by_NID(issuer_cert, NID_subject_key_identifier, -1); | 
| 163 | 0 |         if (i >= 0 && (ext = X509_get_ext(issuer_cert, i)) != NULL | 
| 164 | 0 |             && !(same_issuer && !ss)) | 
| 165 | 0 |             ikeyid = X509V3_EXT_d2i(ext); | 
| 166 | 0 |         if (ikeyid == NULL && same_issuer && ctx->issuer_pkey != NULL) { | 
| 167 |  |             /* generate fallback AKID, emulating s2i_skey_id(..., "hash") */ | 
| 168 | 0 |             X509_PUBKEY *pubkey = NULL; | 
| 169 |  | 
 | 
| 170 | 0 |             if (X509_PUBKEY_set(&pubkey, ctx->issuer_pkey)) | 
| 171 | 0 |                 ikeyid = ossl_x509_pubkey_hash(pubkey); | 
| 172 | 0 |             X509_PUBKEY_free(pubkey); | 
| 173 | 0 |         } | 
| 174 | 0 |         if ((keyid == 2 || issuer == 0) | 
| 175 | 0 |             && (ikeyid == NULL | 
| 176 | 0 |                 || ASN1_STRING_length(ikeyid) <= 2) /* indicating "none" */) { | 
| 177 | 0 |             ERR_raise(ERR_LIB_X509V3, X509V3_R_UNABLE_TO_GET_ISSUER_KEYID); | 
| 178 | 0 |             goto err; | 
| 179 | 0 |         } | 
| 180 | 0 |     } | 
| 181 |  |  | 
| 182 | 0 |     if (issuer == 2 || (issuer == 1 && ikeyid == NULL)) { | 
| 183 | 0 |         isname = X509_NAME_dup(X509_get_issuer_name(issuer_cert)); | 
| 184 | 0 |         serial = ASN1_INTEGER_dup(X509_get0_serialNumber(issuer_cert)); | 
| 185 | 0 |         if (isname == NULL || serial == NULL) { | 
| 186 | 0 |             ERR_raise(ERR_LIB_X509V3, X509V3_R_UNABLE_TO_GET_ISSUER_DETAILS); | 
| 187 | 0 |             goto err; | 
| 188 | 0 |         } | 
| 189 | 0 |     } | 
| 190 |  |  | 
| 191 | 0 |     if (isname != NULL) { | 
| 192 | 0 |         if ((gens = sk_GENERAL_NAME_new_null()) == NULL | 
| 193 | 0 |             || (gen = GENERAL_NAME_new()) == NULL | 
| 194 | 0 |             || !sk_GENERAL_NAME_push(gens, gen)) { | 
| 195 | 0 |             ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE); | 
| 196 | 0 |             goto err; | 
| 197 | 0 |         } | 
| 198 | 0 |         gen->type = GEN_DIRNAME; | 
| 199 | 0 |         gen->d.dirn = isname; | 
| 200 | 0 |     } | 
| 201 |  |  | 
| 202 | 0 |     akeyid->issuer = gens; | 
| 203 | 0 |     gen = NULL; | 
| 204 | 0 |     gens = NULL; | 
| 205 | 0 |     akeyid->serial = serial; | 
| 206 | 0 |     akeyid->keyid = ikeyid; | 
| 207 |  | 
 | 
| 208 | 0 |     return akeyid; | 
| 209 |  |  | 
| 210 | 0 |  err: | 
| 211 | 0 |     sk_GENERAL_NAME_free(gens); | 
| 212 | 0 |     GENERAL_NAME_free(gen); | 
| 213 | 0 |     X509_NAME_free(isname); | 
| 214 | 0 |     ASN1_INTEGER_free(serial); | 
| 215 | 0 |     ASN1_OCTET_STRING_free(ikeyid); | 
| 216 | 0 |     AUTHORITY_KEYID_free(akeyid); | 
| 217 | 0 |     return NULL; | 
| 218 | 0 | } |