| 1 |
// Written by Dr Stephen N Henson ([email protected]) for the OpenSSL project 1999. |
| 2 |
// Copyright (c) 1999 The OpenSSL Project. All rights reserved. |
| 3 |
// SPDX-License-Identifier: Apache-2.0 |
| 4 |
|
| 5 |
#include <limits.h> |
| 6 |
#include <stdio.h> |
| 7 |
#include <string.h> |
| 8 |
|
| 9 |
#include <openssl/digest.h> |
| 10 |
#include <openssl/err.h> |
| 11 |
#include <openssl/obj.h> |
| 12 |
#include <openssl/mem.h> |
| 13 |
#include <openssl/x509.h> |
| 14 |
|
| 15 |
#include "internal.h" |
| 16 |
|
| 17 |
|
| 18 |
char *i2s_ASN1_OCTET_STRING(const X509V3_EXT_METHOD *method, |
| 19 |
const ASN1_OCTET_STRING *oct) { |
| 20 |
return x509v3_bytes_to_hex(oct->data, oct->length); |
| 21 |
} |
| 22 |
|
| 23 |
ASN1_OCTET_STRING *s2i_ASN1_OCTET_STRING(const X509V3_EXT_METHOD *method, |
| 24 |
const X509V3_CTX *ctx, |
| 25 |
const char *str) { |
| 26 |
size_t len; |
| 27 |
uint8_t *data = x509v3_hex_to_bytes(str, &len); |
| 28 |
if (data == NULL) { |
| 29 |
return NULL; |
| 30 |
} |
| 31 |
if (len > INT_MAX) { |
| 32 |
OPENSSL_PUT_ERROR(X509V3, ERR_R_OVERFLOW); |
| 33 |
goto err; |
| 34 |
} |
| 35 |
|
| 36 |
ASN1_OCTET_STRING *oct = ASN1_OCTET_STRING_new(); |
| 37 |
if (oct == NULL) { |
| 38 |
goto err; |
| 39 |
} |
| 40 |
ASN1_STRING_set0(oct, data, (int)len); |
| 41 |
return oct; |
| 42 |
|
| 43 |
err: |
| 44 |
OPENSSL_free(data); |
| 45 |
return NULL; |
| 46 |
} |
| 47 |
|
| 48 |
static char *i2s_ASN1_OCTET_STRING_cb(const X509V3_EXT_METHOD *method, |
| 49 |
void *ext) { |
| 50 |
return i2s_ASN1_OCTET_STRING(method, ext); |
| 51 |
} |
| 52 |
|
| 53 |
static void *s2i_skey_id(const X509V3_EXT_METHOD *method, const X509V3_CTX *ctx, |
| 54 |
const char *str) { |
| 55 |
ASN1_OCTET_STRING *oct; |
| 56 |
ASN1_BIT_STRING *pk; |
| 57 |
unsigned char pkey_dig[EVP_MAX_MD_SIZE]; |
| 58 |
unsigned int diglen; |
| 59 |
|
| 60 |
if (strcmp(str, "hash")) { |
| 61 |
return s2i_ASN1_OCTET_STRING(method, ctx, str); |
| 62 |
} |
| 63 |
|
| 64 |
if (!(oct = ASN1_OCTET_STRING_new())) { |
| 65 |
return NULL; |
| 66 |
} |
| 67 |
|
| 68 |
if (ctx && (ctx->flags == X509V3_CTX_TEST)) { |
| 69 |
return oct; |
| 70 |
} |
| 71 |
|
| 72 |
if (!ctx || (!ctx->subject_req && !ctx->subject_cert)) { |
| 73 |
OPENSSL_PUT_ERROR(X509V3, X509V3_R_NO_PUBLIC_KEY); |
| 74 |
goto err; |
| 75 |
} |
| 76 |
|
| 77 |
if (ctx->subject_req) { |
| 78 |
pk = ctx->subject_req->req_info->pubkey->public_key; |
| 79 |
} else { |
| 80 |
pk = ctx->subject_cert->cert_info->key->public_key; |
| 81 |
} |
| 82 |
|
| 83 |
if (!pk) { |
| 84 |
OPENSSL_PUT_ERROR(X509V3, X509V3_R_NO_PUBLIC_KEY); |
| 85 |
goto err; |
| 86 |
} |
| 87 |
|
| 88 |
if (!EVP_Digest(pk->data, pk->length, pkey_dig, &diglen, EVP_sha1(), NULL)) { |
| 89 |
goto err; |
| 90 |
} |
| 91 |
|
| 92 |
if (!ASN1_OCTET_STRING_set(oct, pkey_dig, diglen)) { |
| 93 |
goto err; |
| 94 |
} |
| 95 |
|
| 96 |
return oct; |
| 97 |
|
| 98 |
err: |
| 99 |
ASN1_OCTET_STRING_free(oct); |
| 100 |
return NULL; |
| 101 |
} |
| 102 |
|
| 103 |
const X509V3_EXT_METHOD v3_skey_id = { |
| 104 |
NID_subject_key_identifier, |
| 105 |
0, |
| 106 |
ASN1_ITEM_ref(ASN1_OCTET_STRING), |
| 107 |
0, |
| 108 |
0, |
| 109 |
0, |
| 110 |
0, |
| 111 |
i2s_ASN1_OCTET_STRING_cb, |
| 112 |
s2i_skey_id, |
| 113 |
0, |
| 114 |
0, |
| 115 |
0, |
| 116 |
0, |
| 117 |
NULL, |
| 118 |
}; |