2015-01-22 11:40:55 +08:00
|
|
|
/*
|
2021-01-28 20:54:57 +08:00
|
|
|
* Copyright 1999-2021 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/x509v3.h>
|
2019-09-28 06:45:33 +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
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
static ASN1_OCTET_STRING *s2i_skey_id(X509V3_EXT_METHOD *method,
|
|
|
|
X509V3_CTX *ctx, char *str);
|
2021-03-09 08:52:15 +08:00
|
|
|
const X509V3_EXT_METHOD ossl_v3_skey_id = {
|
2015-01-22 11:40:55 +08:00
|
|
|
NID_subject_key_identifier, 0, ASN1_ITEM_ref(ASN1_OCTET_STRING),
|
|
|
|
0, 0, 0, 0,
|
|
|
|
(X509V3_EXT_I2S)i2s_ASN1_OCTET_STRING,
|
|
|
|
(X509V3_EXT_S2I)s2i_skey_id,
|
|
|
|
0, 0, 0, 0,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2016-10-11 00:01:24 +08:00
|
|
|
char *i2s_ASN1_OCTET_STRING(X509V3_EXT_METHOD *method,
|
2016-08-12 06:40:49 +08:00
|
|
|
const ASN1_OCTET_STRING *oct)
|
1999-02-10 09:12:59 +08:00
|
|
|
{
|
2016-04-14 03:58:28 +08:00
|
|
|
return OPENSSL_buf2hexstr(oct->data, oct->length);
|
1999-02-10 09:12:59 +08:00
|
|
|
}
|
|
|
|
|
1999-04-20 05:31:43 +08:00
|
|
|
ASN1_OCTET_STRING *s2i_ASN1_OCTET_STRING(X509V3_EXT_METHOD *method,
|
2016-08-12 06:40:49 +08:00
|
|
|
X509V3_CTX *ctx, const char *str)
|
1999-02-10 09:12:59 +08:00
|
|
|
{
|
2015-01-22 11:40:55 +08:00
|
|
|
ASN1_OCTET_STRING *oct;
|
|
|
|
long length;
|
1999-02-10 09:12:59 +08:00
|
|
|
|
2015-05-07 01:43:59 +08:00
|
|
|
if ((oct = ASN1_OCTET_STRING_new()) == NULL) {
|
2022-09-29 19:57:34 +08:00
|
|
|
ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
|
2015-01-22 11:40:55 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
1999-02-10 09:12:59 +08:00
|
|
|
|
2016-04-14 03:58:28 +08:00
|
|
|
if ((oct->data = OPENSSL_hexstr2buf(str, &length)) == NULL) {
|
2015-03-14 12:16:42 +08:00
|
|
|
ASN1_OCTET_STRING_free(oct);
|
2015-01-22 11:40:55 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
1999-02-10 09:12:59 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
oct->length = length;
|
1999-02-10 09:12:59 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
return oct;
|
1999-02-10 09:12:59 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-03-09 08:52:15 +08:00
|
|
|
ASN1_OCTET_STRING *ossl_x509_pubkey_hash(X509_PUBKEY *pubkey)
|
1999-02-10 09:12:59 +08:00
|
|
|
{
|
2015-01-22 11:40:55 +08:00
|
|
|
ASN1_OCTET_STRING *oct;
|
2016-03-22 21:16:42 +08:00
|
|
|
const unsigned char *pk;
|
|
|
|
int pklen;
|
2015-01-22 11:40:55 +08:00
|
|
|
unsigned char pkey_dig[EVP_MAX_MD_SIZE];
|
|
|
|
unsigned int diglen;
|
2021-04-15 08:31:58 +08:00
|
|
|
const char *propq;
|
|
|
|
OSSL_LIB_CTX *libctx;
|
|
|
|
EVP_MD *md;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2020-12-24 18:25:47 +08:00
|
|
|
if (pubkey == NULL) {
|
|
|
|
ERR_raise(ERR_LIB_X509V3, X509V3_R_NO_PUBLIC_KEY);
|
2015-01-22 11:40:55 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2021-04-15 08:31:58 +08:00
|
|
|
if (!ossl_x509_PUBKEY_get0_libctx(&libctx, &propq, pubkey))
|
2020-12-24 18:25:47 +08:00
|
|
|
return NULL;
|
2021-04-15 08:31:58 +08:00
|
|
|
if ((md = EVP_MD_fetch(libctx, SN_sha1, propq)) == NULL)
|
|
|
|
return NULL;
|
|
|
|
if ((oct = ASN1_OCTET_STRING_new()) == NULL) {
|
|
|
|
EVP_MD_free(md);
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2020-12-24 18:25:47 +08:00
|
|
|
X509_PUBKEY_get0_param(NULL, &pk, &pklen, NULL, pubkey);
|
2021-04-15 08:31:58 +08:00
|
|
|
if (EVP_Digest(pk, pklen, pkey_dig, &diglen, md, NULL)
|
|
|
|
&& ASN1_OCTET_STRING_set(oct, pkey_dig, diglen)) {
|
|
|
|
EVP_MD_free(md);
|
2015-01-22 11:40:55 +08:00
|
|
|
return oct;
|
2021-04-15 08:31:58 +08:00
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2021-04-15 08:31:58 +08:00
|
|
|
EVP_MD_free(md);
|
2020-12-24 18:25:47 +08:00
|
|
|
ASN1_OCTET_STRING_free(oct);
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2020-12-24 18:25:47 +08:00
|
|
|
static ASN1_OCTET_STRING *s2i_skey_id(X509V3_EXT_METHOD *method,
|
|
|
|
X509V3_CTX *ctx, char *str)
|
|
|
|
{
|
|
|
|
if (strcmp(str, "none") == 0)
|
|
|
|
return ASN1_OCTET_STRING_new(); /* dummy */
|
2016-03-22 21:16:42 +08:00
|
|
|
|
2020-12-24 18:25:47 +08:00
|
|
|
if (strcmp(str, "hash") != 0)
|
|
|
|
return s2i_ASN1_OCTET_STRING(method, ctx /* not used */, str);
|
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 ASN1_OCTET_STRING_new();
|
|
|
|
if (ctx == NULL
|
|
|
|
|| (ctx->subject_cert == NULL && ctx->subject_req == NULL)) {
|
|
|
|
ERR_raise(ERR_LIB_X509V3, X509V3_R_NO_SUBJECT_DETAILS);
|
|
|
|
return NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
|
2021-11-10 16:31:11 +08:00
|
|
|
return ossl_x509_pubkey_hash(ctx->subject_cert != NULL ?
|
|
|
|
ctx->subject_cert->cert_info.key :
|
|
|
|
ctx->subject_req->req_info.pubkey);
|
1999-02-10 09:12:59 +08:00
|
|
|
}
|