2 * Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include "internal/cryptlib.h"
12 #include <openssl/x509v3.h>
13 #include "crypto/x509.h"
16 static ASN1_OCTET_STRING *s2i_skey_id(X509V3_EXT_METHOD *method,
17 X509V3_CTX *ctx, char *str);
18 const X509V3_EXT_METHOD ossl_v3_skey_id = {
19 NID_subject_key_identifier, 0, ASN1_ITEM_ref(ASN1_OCTET_STRING),
21 (X509V3_EXT_I2S)i2s_ASN1_OCTET_STRING,
22 (X509V3_EXT_S2I)s2i_skey_id,
27 char *i2s_ASN1_OCTET_STRING(X509V3_EXT_METHOD *method,
28 const ASN1_OCTET_STRING *oct)
30 return OPENSSL_buf2hexstr(oct->data, oct->length);
33 ASN1_OCTET_STRING *s2i_ASN1_OCTET_STRING(X509V3_EXT_METHOD *method,
34 X509V3_CTX *ctx, const char *str)
36 ASN1_OCTET_STRING *oct;
39 if ((oct = ASN1_OCTET_STRING_new()) == NULL) {
40 ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
44 if ((oct->data = OPENSSL_hexstr2buf(str, &length)) == NULL) {
45 ASN1_OCTET_STRING_free(oct);
55 ASN1_OCTET_STRING *ossl_x509_pubkey_hash(X509_PUBKEY *pubkey)
57 ASN1_OCTET_STRING *oct;
58 const unsigned char *pk;
60 unsigned char pkey_dig[EVP_MAX_MD_SIZE];
67 ERR_raise(ERR_LIB_X509V3, X509V3_R_NO_PUBLIC_KEY);
70 if (!ossl_x509_PUBKEY_get0_libctx(&libctx, &propq, pubkey))
72 if ((md = EVP_MD_fetch(libctx, SN_sha1, propq)) == NULL)
74 if ((oct = ASN1_OCTET_STRING_new()) == NULL) {
79 X509_PUBKEY_get0_param(NULL, &pk, &pklen, NULL, pubkey);
80 if (EVP_Digest(pk, pklen, pkey_dig, &diglen, md, NULL)
81 && ASN1_OCTET_STRING_set(oct, pkey_dig, diglen)) {
87 ASN1_OCTET_STRING_free(oct);
91 static ASN1_OCTET_STRING *s2i_skey_id(X509V3_EXT_METHOD *method,
92 X509V3_CTX *ctx, char *str)
94 if (strcmp(str, "none") == 0)
95 return ASN1_OCTET_STRING_new(); /* dummy */
97 if (strcmp(str, "hash") != 0)
98 return s2i_ASN1_OCTET_STRING(method, ctx /* not used */, str);
100 if (ctx != NULL && (ctx->flags & X509V3_CTX_TEST) != 0)
101 return ASN1_OCTET_STRING_new();
103 || (ctx->subject_cert == NULL && ctx->subject_req == NULL)) {
104 ERR_raise(ERR_LIB_X509V3, X509V3_R_NO_SUBJECT_DETAILS);
108 return ossl_x509_pubkey_hash(ctx->subject_req != NULL ?
109 ctx->subject_req->req_info.pubkey :
110 ctx->subject_cert->cert_info.key);