2015-01-22 11:40:55 +08:00
|
|
|
/*
|
2020-05-15 21:09:49 +08:00
|
|
|
* Copyright 1999-2020 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>
|
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);
|
|
|
|
|
|
|
|
const X509V3_EXT_METHOD v3_akey_id = {
|
|
|
|
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
|
|
|
{
|
2015-01-22 11:40:55 +08:00
|
|
|
char *tmp;
|
|
|
|
if (akeyid->keyid) {
|
2016-04-14 03:58:28 +08:00
|
|
|
tmp = OPENSSL_buf2hexstr(akeyid->keyid->data, akeyid->keyid->length);
|
2019-11-27 01:15:20 +08:00
|
|
|
if (tmp == NULL) {
|
|
|
|
ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
X509V3_add_value((akeyid->issuer || akeyid->serial) ? "keyid" : NULL,
|
|
|
|
tmp, &extlist);
|
2015-01-22 11:40:55 +08:00
|
|
|
OPENSSL_free(tmp);
|
|
|
|
}
|
|
|
|
if (akeyid->issuer)
|
|
|
|
extlist = i2v_GENERAL_NAMES(NULL, akeyid->issuer, extlist);
|
|
|
|
if (akeyid->serial) {
|
2016-04-14 03:58:28 +08:00
|
|
|
tmp = OPENSSL_buf2hexstr(akeyid->serial->data, akeyid->serial->length);
|
2019-11-27 01:15:20 +08:00
|
|
|
if (tmp == NULL) {
|
|
|
|
ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
X509V3_add_value("serial", tmp, &extlist);
|
|
|
|
OPENSSL_free(tmp);
|
|
|
|
}
|
|
|
|
return extlist;
|
1999-02-10 09:12:59 +08:00
|
|
|
}
|
|
|
|
|
2015-01-16 17:21:50 +08:00
|
|
|
/*-
|
|
|
|
* Currently two options:
|
1999-02-18 07:22:57 +08:00
|
|
|
* keyid: use the issuers subject keyid, the value 'always' means its is
|
|
|
|
* an error if the issuer certificate doesn't have a key id.
|
|
|
|
* issuer: use the issuers cert issuer and serial number. The default is
|
|
|
|
* to only use this if keyid is not present. With the option 'always'
|
|
|
|
* 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;
|
|
|
|
int i;
|
|
|
|
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;
|
|
|
|
X509 *cert;
|
|
|
|
AUTHORITY_KEYID *akeyid;
|
|
|
|
|
|
|
|
for (i = 0; i < sk_CONF_VALUE_num(values); i++) {
|
|
|
|
cnf = sk_CONF_VALUE_value(values, i);
|
2015-05-07 02:56:14 +08:00
|
|
|
if (strcmp(cnf->name, "keyid") == 0) {
|
2015-01-22 11:40:55 +08:00
|
|
|
keyid = 1;
|
2015-05-07 02:56:14 +08:00
|
|
|
if (cnf->value && strcmp(cnf->value, "always") == 0)
|
2015-01-22 11:40:55 +08:00
|
|
|
keyid = 2;
|
2015-05-07 02:56:14 +08:00
|
|
|
} else if (strcmp(cnf->name, "issuer") == 0) {
|
2015-01-22 11:40:55 +08:00
|
|
|
issuer = 1;
|
2015-05-07 02:56:14 +08:00
|
|
|
if (cnf->value && strcmp(cnf->value, "always") == 0)
|
2015-01-22 11:40:55 +08:00
|
|
|
issuer = 2;
|
|
|
|
} else {
|
2020-11-04 23:14:00 +08:00
|
|
|
ERR_raise_data(ERR_LIB_X509V3, X509V3_R_UNKNOWN_OPTION,
|
|
|
|
"name=%s", cnf->name);
|
2015-01-22 11:40:55 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ctx || !ctx->issuer_cert) {
|
|
|
|
if (ctx && (ctx->flags == CTX_TEST))
|
|
|
|
return AUTHORITY_KEYID_new();
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_X509V3, X509V3_R_NO_ISSUER_CERTIFICATE);
|
2015-01-22 11:40:55 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
cert = ctx->issuer_cert;
|
|
|
|
|
|
|
|
if (keyid) {
|
|
|
|
i = X509_get_ext_by_NID(cert, NID_subject_key_identifier, -1);
|
|
|
|
if ((i >= 0) && (ext = X509_get_ext(cert, i)))
|
|
|
|
ikeyid = X509V3_EXT_d2i(ext);
|
2020-12-04 16:26:25 +08:00
|
|
|
if ((keyid == 2 || issuer == 0) && ikeyid == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_X509V3, X509V3_R_UNABLE_TO_GET_ISSUER_KEYID);
|
2015-01-22 11:40:55 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((issuer && !ikeyid) || (issuer == 2)) {
|
|
|
|
isname = X509_NAME_dup(X509_get_issuer_name(cert));
|
2020-07-13 23:13:48 +08:00
|
|
|
serial = ASN1_INTEGER_dup(X509_get0_serialNumber(cert));
|
2015-01-22 11:40:55 +08:00
|
|
|
if (!isname || !serial) {
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-07 01:43:59 +08:00
|
|
|
if ((akeyid = AUTHORITY_KEYID_new()) == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (isname) {
|
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);
|
2015-01-22 11:40:55 +08:00
|
|
|
return NULL;
|
|
|
|
}
|