1e1051a39Sopenharmony_ci/* 2e1051a39Sopenharmony_ci * Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved. 3e1051a39Sopenharmony_ci * 4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License"). You may not use 5e1051a39Sopenharmony_ci * this file except in compliance with the License. You can obtain a copy 6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at 7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html 8e1051a39Sopenharmony_ci */ 9e1051a39Sopenharmony_ci 10e1051a39Sopenharmony_ci#include <stdio.h> 11e1051a39Sopenharmony_ci#include "internal/cryptlib.h" 12e1051a39Sopenharmony_ci#include <openssl/x509v3.h> 13e1051a39Sopenharmony_ci#include "crypto/x509.h" 14e1051a39Sopenharmony_ci#include "ext_dat.h" 15e1051a39Sopenharmony_ci 16e1051a39Sopenharmony_cistatic ASN1_OCTET_STRING *s2i_skey_id(X509V3_EXT_METHOD *method, 17e1051a39Sopenharmony_ci X509V3_CTX *ctx, char *str); 18e1051a39Sopenharmony_ciconst X509V3_EXT_METHOD ossl_v3_skey_id = { 19e1051a39Sopenharmony_ci NID_subject_key_identifier, 0, ASN1_ITEM_ref(ASN1_OCTET_STRING), 20e1051a39Sopenharmony_ci 0, 0, 0, 0, 21e1051a39Sopenharmony_ci (X509V3_EXT_I2S)i2s_ASN1_OCTET_STRING, 22e1051a39Sopenharmony_ci (X509V3_EXT_S2I)s2i_skey_id, 23e1051a39Sopenharmony_ci 0, 0, 0, 0, 24e1051a39Sopenharmony_ci NULL 25e1051a39Sopenharmony_ci}; 26e1051a39Sopenharmony_ci 27e1051a39Sopenharmony_cichar *i2s_ASN1_OCTET_STRING(X509V3_EXT_METHOD *method, 28e1051a39Sopenharmony_ci const ASN1_OCTET_STRING *oct) 29e1051a39Sopenharmony_ci{ 30e1051a39Sopenharmony_ci return OPENSSL_buf2hexstr(oct->data, oct->length); 31e1051a39Sopenharmony_ci} 32e1051a39Sopenharmony_ci 33e1051a39Sopenharmony_ciASN1_OCTET_STRING *s2i_ASN1_OCTET_STRING(X509V3_EXT_METHOD *method, 34e1051a39Sopenharmony_ci X509V3_CTX *ctx, const char *str) 35e1051a39Sopenharmony_ci{ 36e1051a39Sopenharmony_ci ASN1_OCTET_STRING *oct; 37e1051a39Sopenharmony_ci long length; 38e1051a39Sopenharmony_ci 39e1051a39Sopenharmony_ci if ((oct = ASN1_OCTET_STRING_new()) == NULL) { 40e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE); 41e1051a39Sopenharmony_ci return NULL; 42e1051a39Sopenharmony_ci } 43e1051a39Sopenharmony_ci 44e1051a39Sopenharmony_ci if ((oct->data = OPENSSL_hexstr2buf(str, &length)) == NULL) { 45e1051a39Sopenharmony_ci ASN1_OCTET_STRING_free(oct); 46e1051a39Sopenharmony_ci return NULL; 47e1051a39Sopenharmony_ci } 48e1051a39Sopenharmony_ci 49e1051a39Sopenharmony_ci oct->length = length; 50e1051a39Sopenharmony_ci 51e1051a39Sopenharmony_ci return oct; 52e1051a39Sopenharmony_ci 53e1051a39Sopenharmony_ci} 54e1051a39Sopenharmony_ci 55e1051a39Sopenharmony_ciASN1_OCTET_STRING *ossl_x509_pubkey_hash(X509_PUBKEY *pubkey) 56e1051a39Sopenharmony_ci{ 57e1051a39Sopenharmony_ci ASN1_OCTET_STRING *oct; 58e1051a39Sopenharmony_ci const unsigned char *pk; 59e1051a39Sopenharmony_ci int pklen; 60e1051a39Sopenharmony_ci unsigned char pkey_dig[EVP_MAX_MD_SIZE]; 61e1051a39Sopenharmony_ci unsigned int diglen; 62e1051a39Sopenharmony_ci const char *propq; 63e1051a39Sopenharmony_ci OSSL_LIB_CTX *libctx; 64e1051a39Sopenharmony_ci EVP_MD *md; 65e1051a39Sopenharmony_ci 66e1051a39Sopenharmony_ci if (pubkey == NULL) { 67e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_X509V3, X509V3_R_NO_PUBLIC_KEY); 68e1051a39Sopenharmony_ci return NULL; 69e1051a39Sopenharmony_ci } 70e1051a39Sopenharmony_ci if (!ossl_x509_PUBKEY_get0_libctx(&libctx, &propq, pubkey)) 71e1051a39Sopenharmony_ci return NULL; 72e1051a39Sopenharmony_ci if ((md = EVP_MD_fetch(libctx, SN_sha1, propq)) == NULL) 73e1051a39Sopenharmony_ci return NULL; 74e1051a39Sopenharmony_ci if ((oct = ASN1_OCTET_STRING_new()) == NULL) { 75e1051a39Sopenharmony_ci EVP_MD_free(md); 76e1051a39Sopenharmony_ci return NULL; 77e1051a39Sopenharmony_ci } 78e1051a39Sopenharmony_ci 79e1051a39Sopenharmony_ci X509_PUBKEY_get0_param(NULL, &pk, &pklen, NULL, pubkey); 80e1051a39Sopenharmony_ci if (EVP_Digest(pk, pklen, pkey_dig, &diglen, md, NULL) 81e1051a39Sopenharmony_ci && ASN1_OCTET_STRING_set(oct, pkey_dig, diglen)) { 82e1051a39Sopenharmony_ci EVP_MD_free(md); 83e1051a39Sopenharmony_ci return oct; 84e1051a39Sopenharmony_ci } 85e1051a39Sopenharmony_ci 86e1051a39Sopenharmony_ci EVP_MD_free(md); 87e1051a39Sopenharmony_ci ASN1_OCTET_STRING_free(oct); 88e1051a39Sopenharmony_ci return NULL; 89e1051a39Sopenharmony_ci} 90e1051a39Sopenharmony_ci 91e1051a39Sopenharmony_cistatic ASN1_OCTET_STRING *s2i_skey_id(X509V3_EXT_METHOD *method, 92e1051a39Sopenharmony_ci X509V3_CTX *ctx, char *str) 93e1051a39Sopenharmony_ci{ 94e1051a39Sopenharmony_ci if (strcmp(str, "none") == 0) 95e1051a39Sopenharmony_ci return ASN1_OCTET_STRING_new(); /* dummy */ 96e1051a39Sopenharmony_ci 97e1051a39Sopenharmony_ci if (strcmp(str, "hash") != 0) 98e1051a39Sopenharmony_ci return s2i_ASN1_OCTET_STRING(method, ctx /* not used */, str); 99e1051a39Sopenharmony_ci 100e1051a39Sopenharmony_ci if (ctx != NULL && (ctx->flags & X509V3_CTX_TEST) != 0) 101e1051a39Sopenharmony_ci return ASN1_OCTET_STRING_new(); 102e1051a39Sopenharmony_ci if (ctx == NULL 103e1051a39Sopenharmony_ci || (ctx->subject_cert == NULL && ctx->subject_req == NULL)) { 104e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_X509V3, X509V3_R_NO_SUBJECT_DETAILS); 105e1051a39Sopenharmony_ci return NULL; 106e1051a39Sopenharmony_ci } 107e1051a39Sopenharmony_ci 108e1051a39Sopenharmony_ci return ossl_x509_pubkey_hash(ctx->subject_cert != NULL ? 109e1051a39Sopenharmony_ci ctx->subject_cert->cert_info.key : 110e1051a39Sopenharmony_ci ctx->subject_req->req_info.pubkey); 111e1051a39Sopenharmony_ci} 112