| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <stdio.h> |
| 16 | #include <string.h> |
| 17 |
| 18 | #include <openssl/asn1.h> |
| 19 | #include <openssl/asn1t.h> |
| 20 | #include <openssl/conf.h> |
| 21 | #include <openssl/err.h> |
| 22 | #include <openssl/mem.h> |
| 23 | #include <openssl/obj.h> |
| 24 | #include <openssl/x509.h> |
| 25 |
| 26 | #include "ext_dat.h" |
| 27 | #include "internal.h" |
| 28 |
| 29 |
| 30 | static STACK_OF(CONF_VALUE) *i2v_AUTHORITY_KEYID( |
| 31 | const X509V3_EXT_METHOD *method, void *ext, STACK_OF(CONF_VALUE) *extlist); |
| 32 | static void *v2i_AUTHORITY_KEYID(const X509V3_EXT_METHOD *method, |
| 33 | const X509V3_CTX *ctx, |
| 34 | const STACK_OF(CONF_VALUE) *values); |
| 35 |
| 36 | const X509V3_EXT_METHOD v3_akey_id = { |
| 37 | NID_authority_key_identifier, |
| 38 | X509V3_EXT_MULTILINE, |
| 39 | ASN1_ITEM_ref(AUTHORITY_KEYID), |
| 40 | 0, |
| 41 | 0, |
| 42 | 0, |
| 43 | 0, |
| 44 | 0, |
| 45 | 0, |
| 46 | i2v_AUTHORITY_KEYID, |
| 47 | v2i_AUTHORITY_KEYID, |
| 48 | 0, |
| 49 | 0, |
| 50 | NULL, |
| 51 | }; |
| 52 |
| 53 | static STACK_OF(CONF_VALUE) *i2v_AUTHORITY_KEYID( |
| 54 | const X509V3_EXT_METHOD *method, void *ext, STACK_OF(CONF_VALUE) *extlist) { |
| 55 | const AUTHORITY_KEYID *akeyid = |
| 56 | reinterpret_cast<const AUTHORITY_KEYID *>(ext); |
| 57 | int extlist_was_null = extlist == NULL; |
| 58 | if (akeyid->keyid) { |
| 59 | char *tmp = x509v3_bytes_to_hex(akeyid->keyid->data, akeyid->keyid->length); |
| 60 | int ok = tmp != NULL && X509V3_add_value("keyid", tmp, &extlist); |
| 61 | OPENSSL_free(tmp); |
| 62 | if (!ok) { |
| 63 | goto err; |
| 64 | } |
| 65 | } |
| 66 | if (akeyid->issuer) { |
| 67 | STACK_OF(CONF_VALUE) *tmpextlist = |
| 68 | i2v_GENERAL_NAMES(NULL, akeyid->issuer, extlist); |
| 69 | if (tmpextlist == NULL) { |
| 70 | goto err; |
| 71 | } |
| 72 | extlist = tmpextlist; |
| 73 | } |
| 74 | if (akeyid->serial) { |
| 75 | if (!X509V3_add_value_int("serial", akeyid->serial, &extlist)) { |
| 76 | goto err; |
| 77 | } |
| 78 | } |
| 79 | return extlist; |
| 80 |
| 81 | err: |
| 82 | if (extlist_was_null) { |
| 83 | sk_CONF_VALUE_pop_free(extlist, X509V3_conf_free); |
| 84 | } |
| 85 | return NULL; |
| 86 | } |
| 87 |
| 88 | // Currently two options: keyid: use the issuers subject keyid, the value |
| 89 | // 'always' means its is an error if the issuer certificate doesn't have a |
| 90 | // key id. issuer: use the issuers cert issuer and serial number. The default |
| 91 | // is to only use this if keyid is not present. With the option 'always' this |
| 92 | // is always included. |
| 93 |
| 94 | static void *v2i_AUTHORITY_KEYID(const X509V3_EXT_METHOD *method, |
| 95 | const X509V3_CTX *ctx, |
| 96 | const STACK_OF(CONF_VALUE) *values) { |
| 97 | char keyid = 0, issuer = 0; |
| 98 | int j; |
| 99 | ASN1_OCTET_STRING *ikeyid = NULL; |
| 100 | X509_NAME *isname = NULL; |
| 101 | GENERAL_NAMES *gens = NULL; |
| 102 | GENERAL_NAME *gen = NULL; |
| 103 | ASN1_INTEGER *serial = NULL; |
| 104 | const X509 *cert; |
| 105 | AUTHORITY_KEYID *akeyid; |
| 106 |
| 107 | for (size_t i = 0; i < sk_CONF_VALUE_num(values); i++) { |
| 108 | const CONF_VALUE *cnf = sk_CONF_VALUE_value(values, i); |
| 109 | if (!strcmp(cnf->name, "keyid")) { |
| 110 | keyid = 1; |
| 111 | if (cnf->value && !strcmp(cnf->value, "always")) { |
| 112 | keyid = 2; |
| 113 | } |
| 114 | } else if (!strcmp(cnf->name, "issuer")) { |
| 115 | issuer = 1; |
| 116 | if (cnf->value && !strcmp(cnf->value, "always")) { |
| 117 | issuer = 2; |
| 118 | } |
| 119 | } else { |
| 120 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_UNKNOWN_OPTION); |
| 121 | ERR_add_error_data(2, "name=", cnf->name); |
| 122 | return NULL; |
| 123 | } |
| 124 | } |
| 125 |
| 126 | if (!ctx || !ctx->issuer_cert) { |
| 127 | if (ctx && (ctx->flags == X509V3_CTX_TEST)) { |
| 128 | return AUTHORITY_KEYID_new(); |
| 129 | } |
| 130 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_NO_ISSUER_CERTIFICATE); |
| 131 | return NULL; |
| 132 | } |
| 133 |
| 134 | cert = ctx->issuer_cert; |
| 135 |
| 136 | if (keyid) { |
| 137 | j = X509_get_ext_by_NID(cert, NID_subject_key_identifier, -1); |
| 138 | const X509_EXTENSION *ext; |
| 139 | if ((j >= 0) && (ext = X509_get_ext(cert, j))) { |
| 140 | ikeyid = reinterpret_cast<ASN1_OCTET_STRING *>(X509V3_EXT_d2i(ext)); |
| 141 | } |
| 142 | if (keyid == 2 && !ikeyid) { |
| 143 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_UNABLE_TO_GET_ISSUER_KEYID); |
| 144 | return NULL; |
| 145 | } |
| 146 | } |
| 147 |
| 148 | if ((issuer && !ikeyid) || (issuer == 2)) { |
| 149 | isname = X509_NAME_dup(X509_get_issuer_name(cert)); |
| 150 | serial = ASN1_INTEGER_dup(X509_get0_serialNumber(cert)); |
| 151 | if (!isname || !serial) { |
| 152 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_UNABLE_TO_GET_ISSUER_DETAILS); |
| 153 | goto err; |
| 154 | } |
| 155 | } |
| 156 |
| 157 | if (!(akeyid = AUTHORITY_KEYID_new())) { |
| 158 | goto err; |
| 159 | } |
| 160 |
| 161 | if (isname) { |
| 162 | if (!(gens = sk_GENERAL_NAME_new_null()) || !(gen = GENERAL_NAME_new()) || |
| 163 | !sk_GENERAL_NAME_push(gens, gen)) { |
| 164 | goto err; |
| 165 | } |
| 166 | gen->type = GEN_DIRNAME; |
| 167 | gen->d.dirn = isname; |
| 168 | } |
| 169 |
| 170 | akeyid->issuer = gens; |
| 171 | akeyid->serial = serial; |
| 172 | akeyid->keyid = ikeyid; |
| 173 |
| 174 | return akeyid; |
| 175 |
| 176 | err: |
| 177 | X509_NAME_free(isname); |
| 178 | ASN1_INTEGER_free(serial); |
| 179 | ASN1_OCTET_STRING_free(ikeyid); |
| 180 | return NULL; |
| 181 | } |
| 182 |