Coverage Report

Created: 2025-12-10 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/x509/v3_skid.c
Line
Count
Source
1
/*
2
 * Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved.
3
 *
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
8
 */
9
10
#include <stdio.h>
11
#include "internal/cryptlib.h"
12
#include <openssl/x509v3.h>
13
#include "crypto/x509.h"
14
#include "ext_dat.h"
15
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),
20
    0, 0, 0, 0,
21
    (X509V3_EXT_I2S)i2s_ASN1_OCTET_STRING,
22
    (X509V3_EXT_S2I)s2i_skey_id,
23
    0, 0, 0, 0,
24
    NULL
25
};
26
27
char *i2s_ASN1_OCTET_STRING(X509V3_EXT_METHOD *method,
28
    const ASN1_OCTET_STRING *oct)
29
0
{
30
0
    return OPENSSL_buf2hexstr(oct->data, oct->length);
31
0
}
32
33
ASN1_OCTET_STRING *s2i_ASN1_OCTET_STRING(X509V3_EXT_METHOD *method,
34
    X509V3_CTX *ctx, const char *str)
35
0
{
36
0
    ASN1_OCTET_STRING *oct;
37
0
    long length;
38
39
0
    if ((oct = ASN1_OCTET_STRING_new()) == NULL) {
40
0
        ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
41
0
        return NULL;
42
0
    }
43
44
0
    if ((oct->data = OPENSSL_hexstr2buf(str, &length)) == NULL) {
45
0
        ASN1_OCTET_STRING_free(oct);
46
0
        return NULL;
47
0
    }
48
49
0
    oct->length = length;
50
51
0
    return oct;
52
0
}
53
54
ASN1_OCTET_STRING *ossl_x509_pubkey_hash(X509_PUBKEY *pubkey)
55
0
{
56
0
    ASN1_OCTET_STRING *oct;
57
0
    const unsigned char *pk;
58
0
    int pklen;
59
0
    unsigned char pkey_dig[EVP_MAX_MD_SIZE];
60
0
    unsigned int diglen;
61
0
    const char *propq;
62
0
    OSSL_LIB_CTX *libctx;
63
0
    EVP_MD *md;
64
65
0
    if (pubkey == NULL) {
66
0
        ERR_raise(ERR_LIB_X509V3, X509V3_R_NO_PUBLIC_KEY);
67
0
        return NULL;
68
0
    }
69
0
    if (!ossl_x509_PUBKEY_get0_libctx(&libctx, &propq, pubkey))
70
0
        return NULL;
71
0
    if ((md = EVP_MD_fetch(libctx, SN_sha1, propq)) == NULL)
72
0
        return NULL;
73
0
    if ((oct = ASN1_OCTET_STRING_new()) == NULL) {
74
0
        EVP_MD_free(md);
75
0
        return NULL;
76
0
    }
77
78
0
    X509_PUBKEY_get0_param(NULL, &pk, &pklen, NULL, pubkey);
79
0
    if (EVP_Digest(pk, pklen, pkey_dig, &diglen, md, NULL)
80
0
        && ASN1_OCTET_STRING_set(oct, pkey_dig, diglen)) {
81
0
        EVP_MD_free(md);
82
0
        return oct;
83
0
    }
84
85
0
    EVP_MD_free(md);
86
0
    ASN1_OCTET_STRING_free(oct);
87
0
    return NULL;
88
0
}
89
90
static ASN1_OCTET_STRING *s2i_skey_id(X509V3_EXT_METHOD *method,
91
    X509V3_CTX *ctx, char *str)
92
0
{
93
0
    if (strcmp(str, "none") == 0)
94
0
        return ASN1_OCTET_STRING_new(); /* dummy */
95
96
0
    if (strcmp(str, "hash") != 0)
97
0
        return s2i_ASN1_OCTET_STRING(method, ctx /* not used */, str);
98
99
0
    if (ctx != NULL && (ctx->flags & X509V3_CTX_TEST) != 0)
100
0
        return ASN1_OCTET_STRING_new();
101
0
    if (ctx == NULL
102
0
        || (ctx->subject_cert == NULL && ctx->subject_req == NULL)) {
103
0
        ERR_raise(ERR_LIB_X509V3, X509V3_R_NO_SUBJECT_DETAILS);
104
0
        return NULL;
105
0
    }
106
107
0
    return ossl_x509_pubkey_hash(ctx->subject_cert != NULL ? ctx->subject_cert->cert_info.key : ctx->subject_req->req_info.pubkey);
108
0
}