2015-01-22 11:40:55 +08:00
|
|
|
/*
|
2022-05-03 18:52:38 +08:00
|
|
|
* Copyright 1999-2022 The OpenSSL Project Authors. All Rights Reserved.
|
1999-02-10 09:12:59 +08:00
|
|
|
*
|
2018-12-06 21:00:54 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:51:26 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1999-02-10 09:12:59 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-05-14 22:56:48 +08:00
|
|
|
#include "internal/cryptlib.h"
|
1999-04-24 06:13:45 +08:00
|
|
|
#include <openssl/conf.h>
|
|
|
|
#include <openssl/asn1.h>
|
2000-12-09 03:09:35 +08:00
|
|
|
#include <openssl/asn1t.h>
|
1999-04-24 06:13:45 +08:00
|
|
|
#include <openssl/x509v3.h>
|
2020-12-24 18:25:47 +08:00
|
|
|
#include "crypto/x509.h"
|
2015-09-05 20:32:58 +08:00
|
|
|
#include "ext_dat.h"
|
1999-02-10 09:12:59 +08:00
|
|
|
|
1999-06-21 06:18:16 +08:00
|
|
|
static STACK_OF(CONF_VALUE) *i2v_AUTHORITY_KEYID(X509V3_EXT_METHOD *method,
|
2015-01-22 11:40:55 +08:00
|
|
|
AUTHORITY_KEYID *akeyid,
|
|
|
|
STACK_OF(CONF_VALUE)
|
|
|
|
*extlist);
|
1999-06-21 06:18:16 +08:00
|
|
|
static AUTHORITY_KEYID *v2i_AUTHORITY_KEYID(X509V3_EXT_METHOD *method,
|
2015-01-22 11:40:55 +08:00
|
|
|
X509V3_CTX *ctx,
|
|
|
|
STACK_OF(CONF_VALUE) *values);
|
|
|
|
|
2021-03-09 08:52:15 +08:00
|
|
|
const X509V3_EXT_METHOD ossl_v3_akey_id = {
|
2015-01-22 11:40:55 +08:00
|
|
|
NID_authority_key_identifier,
|
|
|
|
X509V3_EXT_MULTILINE, ASN1_ITEM_ref(AUTHORITY_KEYID),
|
|
|
|
0, 0, 0, 0,
|
|
|
|
0, 0,
|
|
|
|
(X509V3_EXT_I2V) i2v_AUTHORITY_KEYID,
|
|
|
|
(X509V3_EXT_V2I)v2i_AUTHORITY_KEYID,
|
|
|
|
0, 0,
|
|
|
|
NULL
|
|
|
|
};
|
1999-02-10 09:12:59 +08:00
|
|
|
|
1999-06-21 06:18:16 +08:00
|
|
|
static STACK_OF(CONF_VALUE) *i2v_AUTHORITY_KEYID(X509V3_EXT_METHOD *method,
|
2015-01-22 11:40:55 +08:00
|
|
|
AUTHORITY_KEYID *akeyid,
|
|
|
|
STACK_OF(CONF_VALUE)
|
|
|
|
*extlist)
|
1999-02-10 09:12:59 +08:00
|
|
|
{
|
2021-08-20 22:23:32 +08:00
|
|
|
char *tmp = NULL;
|
|
|
|
STACK_OF(CONF_VALUE) *origextlist = extlist, *tmpextlist;
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (akeyid->keyid) {
|
2021-12-07 20:11:27 +08:00
|
|
|
tmp = i2s_ASN1_OCTET_STRING(NULL, akeyid->keyid);
|
2019-11-27 01:15:20 +08:00
|
|
|
if (tmp == NULL) {
|
|
|
|
ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-08-20 22:23:32 +08:00
|
|
|
if (!X509V3_add_value((akeyid->issuer || akeyid->serial) ? "keyid" : NULL,
|
|
|
|
tmp, &extlist)) {
|
|
|
|
OPENSSL_free(tmp);
|
|
|
|
ERR_raise(ERR_LIB_X509V3, ERR_R_X509_LIB);
|
|
|
|
goto err;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
OPENSSL_free(tmp);
|
|
|
|
}
|
2021-08-20 22:23:32 +08:00
|
|
|
if (akeyid->issuer) {
|
|
|
|
tmpextlist = i2v_GENERAL_NAMES(NULL, akeyid->issuer, extlist);
|
|
|
|
if (tmpextlist == NULL) {
|
|
|
|
ERR_raise(ERR_LIB_X509V3, ERR_R_X509_LIB);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
extlist = tmpextlist;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
if (akeyid->serial) {
|
2021-12-07 20:11:27 +08:00
|
|
|
tmp = i2s_ASN1_OCTET_STRING(NULL, akeyid->serial);
|
2019-11-27 01:15:20 +08:00
|
|
|
if (tmp == NULL) {
|
|
|
|
ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
|
2021-08-20 22:23:32 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (!X509V3_add_value("serial", tmp, &extlist)) {
|
|
|
|
OPENSSL_free(tmp);
|
|
|
|
goto err;
|
2019-11-27 01:15:20 +08:00
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
OPENSSL_free(tmp);
|
|
|
|
}
|
|
|
|
return extlist;
|
2021-08-20 22:23:32 +08:00
|
|
|
err:
|
|
|
|
if (origextlist == NULL)
|
|
|
|
sk_CONF_VALUE_pop_free(extlist, X509V3_conf_free);
|
|
|
|
return NULL;
|
1999-02-10 09:12:59 +08:00
|
|
|
}
|
|
|
|
|
2015-01-16 17:21:50 +08:00
|
|
|
/*-
|
2021-08-18 01:12:55 +08:00
|
|
|
* Three explicit tags may be given, where 'keyid' and 'issuer' may be combined:
|
|
|
|
* 'none': do not add any authority key identifier.
|
|
|
|
* 'keyid': use the issuer's subject keyid; the option 'always' means its is
|
|
|
|
* an error if the issuer certificate doesn't have a subject key id.
|
|
|
|
* 'issuer': use the issuer's cert issuer and serial number. The default is
|
|
|
|
* to only use this if 'keyid' is not present. With the option 'always'
|
1999-02-18 07:22:57 +08:00
|
|
|
* this is always included.
|
|
|
|
*/
|
1999-04-20 05:31:43 +08:00
|
|
|
static AUTHORITY_KEYID *v2i_AUTHORITY_KEYID(X509V3_EXT_METHOD *method,
|
2015-01-22 11:40:55 +08:00
|
|
|
X509V3_CTX *ctx,
|
|
|
|
STACK_OF(CONF_VALUE) *values)
|
|
|
|
{
|
|
|
|
char keyid = 0, issuer = 0;
|
2020-12-10 22:23:41 +08:00
|
|
|
int i, n = sk_CONF_VALUE_num(values);
|
2015-01-22 11:40:55 +08:00
|
|
|
CONF_VALUE *cnf;
|
|
|
|
ASN1_OCTET_STRING *ikeyid = NULL;
|
|
|
|
X509_NAME *isname = NULL;
|
|
|
|
GENERAL_NAMES *gens = NULL;
|
|
|
|
GENERAL_NAME *gen = NULL;
|
|
|
|
ASN1_INTEGER *serial = NULL;
|
|
|
|
X509_EXTENSION *ext;
|
2020-12-24 18:25:47 +08:00
|
|
|
X509 *issuer_cert;
|
2021-08-27 17:34:23 +08:00
|
|
|
int same_issuer, ss;
|
2020-12-08 00:45:09 +08:00
|
|
|
AUTHORITY_KEYID *akeyid = AUTHORITY_KEYID_new();
|
|
|
|
|
|
|
|
if (akeyid == NULL)
|
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2020-12-10 22:23:41 +08:00
|
|
|
if (n == 1 && strcmp(sk_CONF_VALUE_value(values, 0)->name, "none") == 0) {
|
|
|
|
return akeyid;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++) {
|
2015-01-22 11:40:55 +08:00
|
|
|
cnf = sk_CONF_VALUE_value(values, i);
|
2021-08-18 01:12:55 +08:00
|
|
|
if (cnf->value != NULL && strcmp(cnf->value, "always") != 0) {
|
|
|
|
ERR_raise_data(ERR_LIB_X509V3, X509V3_R_UNKNOWN_OPTION,
|
|
|
|
"name=%s option=%s", cnf->name, cnf->value);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (strcmp(cnf->name, "keyid") == 0 && keyid == 0) {
|
2015-01-22 11:40:55 +08:00
|
|
|
keyid = 1;
|
2021-08-18 01:12:55 +08:00
|
|
|
if (cnf->value != NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
keyid = 2;
|
2021-08-18 01:12:55 +08:00
|
|
|
} else if (strcmp(cnf->name, "issuer") == 0 && issuer == 0) {
|
2015-01-22 11:40:55 +08:00
|
|
|
issuer = 1;
|
2021-08-18 01:12:55 +08:00
|
|
|
if (cnf->value != NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
issuer = 2;
|
2021-08-18 01:12:55 +08:00
|
|
|
} else if (strcmp(cnf->name, "none") == 0
|
|
|
|
|| strcmp(cnf->name, "keyid") == 0
|
|
|
|
|| strcmp(cnf->name, "issuer") == 0) {
|
|
|
|
ERR_raise_data(ERR_LIB_X509V3, X509V3_R_BAD_VALUE,
|
|
|
|
"name=%s", cnf->name);
|
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
} else {
|
2021-08-18 01:12:55 +08:00
|
|
|
ERR_raise_data(ERR_LIB_X509V3, X509V3_R_UNKNOWN_VALUE,
|
2020-11-04 23:14:00 +08:00
|
|
|
"name=%s", cnf->name);
|
2020-12-08 00:45:09 +08:00
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-11 14:52:45 +08:00
|
|
|
if (ctx != NULL && (ctx->flags & X509V3_CTX_TEST) != 0)
|
2020-12-24 18:25:47 +08:00
|
|
|
return akeyid;
|
|
|
|
|
|
|
|
if (ctx == NULL) {
|
|
|
|
ERR_raise(ERR_LIB_X509V3, ERR_R_PASSED_NULL_PARAMETER);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if ((issuer_cert = ctx->issuer_cert) == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_X509V3, X509V3_R_NO_ISSUER_CERTIFICATE);
|
2020-12-08 00:45:09 +08:00
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2021-08-27 17:34:23 +08:00
|
|
|
same_issuer = ctx->subject_cert == ctx->issuer_cert;
|
|
|
|
ERR_set_mark();
|
|
|
|
if (ctx->issuer_pkey != NULL)
|
|
|
|
ss = X509_check_private_key(ctx->subject_cert, ctx->issuer_pkey);
|
|
|
|
else
|
|
|
|
ss = same_issuer;
|
|
|
|
ERR_pop_to_mark();
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2021-08-27 17:34:23 +08:00
|
|
|
/* unless forced with "always", AKID is suppressed for self-signed certs */
|
|
|
|
if (keyid == 2 || (keyid == 1 && !ss)) {
|
|
|
|
/*
|
|
|
|
* prefer any pre-existing subject key identifier of the issuer cert
|
|
|
|
* except issuer cert is same as subject cert and is not self-signed
|
|
|
|
*/
|
2020-12-24 18:25:47 +08:00
|
|
|
i = X509_get_ext_by_NID(issuer_cert, NID_subject_key_identifier, -1);
|
2021-08-27 17:34:23 +08:00
|
|
|
if (i >= 0 && (ext = X509_get_ext(issuer_cert, i)) != NULL
|
2021-08-18 05:13:28 +08:00
|
|
|
&& !(same_issuer && !ss)) {
|
2015-01-22 11:40:55 +08:00
|
|
|
ikeyid = X509V3_EXT_d2i(ext);
|
2021-08-18 05:13:28 +08:00
|
|
|
if (ASN1_STRING_length(ikeyid) == 0) /* indicating "none" */ {
|
|
|
|
ASN1_OCTET_STRING_free(ikeyid);
|
|
|
|
ikeyid = NULL;
|
|
|
|
}
|
|
|
|
}
|
2021-08-27 17:34:23 +08:00
|
|
|
if (ikeyid == NULL && same_issuer && ctx->issuer_pkey != NULL) {
|
|
|
|
/* generate fallback AKID, emulating s2i_skey_id(..., "hash") */
|
2020-12-24 18:25:47 +08:00
|
|
|
X509_PUBKEY *pubkey = NULL;
|
|
|
|
|
|
|
|
if (X509_PUBKEY_set(&pubkey, ctx->issuer_pkey))
|
2021-03-09 08:52:15 +08:00
|
|
|
ikeyid = ossl_x509_pubkey_hash(pubkey);
|
2020-12-24 18:25:47 +08:00
|
|
|
X509_PUBKEY_free(pubkey);
|
|
|
|
}
|
2021-08-18 05:13:28 +08:00
|
|
|
if (keyid == 2 && ikeyid == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_X509V3, X509V3_R_UNABLE_TO_GET_ISSUER_KEYID);
|
2020-12-08 00:45:09 +08:00
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-18 05:13:28 +08:00
|
|
|
if (issuer == 2 || (issuer == 1 && !ss && ikeyid == NULL)) {
|
2020-12-24 18:25:47 +08:00
|
|
|
isname = X509_NAME_dup(X509_get_issuer_name(issuer_cert));
|
|
|
|
serial = ASN1_INTEGER_dup(X509_get0_serialNumber(issuer_cert));
|
|
|
|
if (isname == NULL || serial == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_X509V3, X509V3_R_UNABLE_TO_GET_ISSUER_DETAILS);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-24 18:25:47 +08:00
|
|
|
if (isname != NULL) {
|
2015-05-07 01:43:59 +08:00
|
|
|
if ((gens = sk_GENERAL_NAME_new_null()) == NULL
|
|
|
|
|| (gen = GENERAL_NAME_new()) == NULL
|
2015-01-22 11:40:55 +08:00
|
|
|
|| !sk_GENERAL_NAME_push(gens, gen)) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
gen->type = GEN_DIRNAME;
|
|
|
|
gen->d.dirn = isname;
|
|
|
|
}
|
|
|
|
|
|
|
|
akeyid->issuer = gens;
|
2016-04-27 22:03:26 +08:00
|
|
|
gen = NULL;
|
|
|
|
gens = NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
akeyid->serial = serial;
|
|
|
|
akeyid->keyid = ikeyid;
|
|
|
|
|
|
|
|
return akeyid;
|
2003-04-04 03:10:32 +08:00
|
|
|
|
|
|
|
err:
|
2016-04-27 22:03:26 +08:00
|
|
|
sk_GENERAL_NAME_free(gens);
|
|
|
|
GENERAL_NAME_free(gen);
|
2015-01-22 11:40:55 +08:00
|
|
|
X509_NAME_free(isname);
|
2015-03-14 12:16:42 +08:00
|
|
|
ASN1_INTEGER_free(serial);
|
|
|
|
ASN1_OCTET_STRING_free(ikeyid);
|
2020-12-08 00:45:09 +08:00
|
|
|
AUTHORITY_KEYID_free(akeyid);
|
2015-01-22 11:40:55 +08:00
|
|
|
return NULL;
|
|
|
|
}
|