Coverage Report

Created: 2026-03-09 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/ct/ct_sct_ctx.c
Line
Count
Source
1
/*
2
 * Copyright 2016-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
#ifdef OPENSSL_NO_CT
11
#error "CT is disabled"
12
#endif
13
14
#include <stddef.h>
15
#include <string.h>
16
17
#include <openssl/err.h>
18
#include <openssl/obj_mac.h>
19
#include <openssl/x509.h>
20
21
#include "ct_local.h"
22
23
SCT_CTX *SCT_CTX_new(OSSL_LIB_CTX *libctx, const char *propq)
24
0
{
25
0
    SCT_CTX *sctx = OPENSSL_zalloc(sizeof(*sctx));
26
27
0
    if (sctx == NULL)
28
0
        return NULL;
29
30
0
    sctx->libctx = libctx;
31
0
    if (propq != NULL) {
32
0
        sctx->propq = OPENSSL_strdup(propq);
33
0
        if (sctx->propq == NULL) {
34
0
            OPENSSL_free(sctx);
35
0
            return NULL;
36
0
        }
37
0
    }
38
39
0
    return sctx;
40
0
}
41
42
void SCT_CTX_free(SCT_CTX *sctx)
43
0
{
44
0
    if (sctx == NULL)
45
0
        return;
46
0
    EVP_PKEY_free(sctx->pkey);
47
0
    OPENSSL_free(sctx->pkeyhash);
48
0
    OPENSSL_free(sctx->ihash);
49
0
    OPENSSL_free(sctx->certder);
50
0
    OPENSSL_free(sctx->preder);
51
0
    OPENSSL_free(sctx->propq);
52
0
    OPENSSL_free(sctx);
53
0
}
54
55
/*
56
 * Finds the index of the first extension with the given NID in cert.
57
 * If there is more than one extension with that NID, *is_duplicated is set to
58
 * 1, otherwise 0 (unless it is NULL).
59
 */
60
static int ct_x509_get_ext(X509 *cert, int nid, int *is_duplicated)
61
0
{
62
0
    int ret = X509_get_ext_by_NID(cert, nid, -1);
63
64
0
    if (is_duplicated != NULL)
65
0
        *is_duplicated = ret >= 0 && X509_get_ext_by_NID(cert, nid, ret) >= 0;
66
67
0
    return ret;
68
0
}
69
70
/*
71
 * Modifies a certificate by deleting extensions and copying the issuer and
72
 * AKID from the presigner certificate, if necessary.
73
 * Returns 1 on success, 0 otherwise.
74
 */
75
__owur static int ct_x509_cert_fixup(X509 *cert, X509 *presigner)
76
0
{
77
0
    int ret = 0;
78
0
    int preidx, certidx;
79
0
    int pre_akid_ext_is_dup, cert_akid_ext_is_dup;
80
0
    X509_EXTENSION *new = NULL;
81
82
0
    if (presigner == NULL) {
83
0
        ret = 1;
84
0
        goto done;
85
0
    }
86
87
0
    preidx = ct_x509_get_ext(presigner, NID_authority_key_identifier,
88
0
        &pre_akid_ext_is_dup);
89
0
    certidx = ct_x509_get_ext(cert, NID_authority_key_identifier,
90
0
        &cert_akid_ext_is_dup);
91
92
    /* An error occurred whilst searching for the extension */
93
0
    if (preidx < -1 || certidx < -1)
94
0
        goto done;
95
    /* Invalid certificate if they contain duplicate extensions */
96
0
    if (pre_akid_ext_is_dup || cert_akid_ext_is_dup)
97
0
        goto done;
98
    /* AKID must be present in both certificate or absent in both */
99
0
    if (preidx >= 0 && certidx == -1)
100
0
        goto done;
101
0
    if (preidx == -1 && certidx >= 0)
102
0
        goto done;
103
    /* Copy issuer name */
104
0
    if (!X509_set_issuer_name(cert, X509_get_issuer_name(presigner)))
105
0
        goto done;
106
0
    if (preidx != -1) {
107
        /* Retrieve and copy AKID encoding */
108
0
        const X509_EXTENSION *preext = X509_get_ext(presigner, preidx);
109
0
        const X509_EXTENSION *certext = X509_get_ext(cert, certidx);
110
0
        const ASN1_OCTET_STRING *preextdata;
111
112
        /* Should never happen */
113
0
        if (preext == NULL || certext == NULL)
114
0
            goto done;
115
0
        if ((new = X509_EXTENSION_dup(certext)) == NULL)
116
0
            goto done;
117
0
        preextdata = X509_EXTENSION_get_data(preext);
118
0
        if (preextdata == NULL || !X509_EXTENSION_set_data(new, preextdata))
119
0
            goto done;
120
0
        X509_EXTENSION_free(X509_delete_ext(cert, certidx));
121
0
        certext = NULL;
122
0
        if (!X509_add_ext(cert, new, certidx))
123
0
            goto done;
124
0
        ret = 1;
125
0
    }
126
0
done:
127
0
    X509_EXTENSION_free(new);
128
0
    return ret;
129
0
}
130
131
int SCT_CTX_set1_cert(SCT_CTX *sctx, X509 *cert, X509 *presigner)
132
0
{
133
0
    unsigned char *certder = NULL, *preder = NULL;
134
0
    X509 *pretmp = NULL;
135
0
    int certderlen = 0, prederlen = 0;
136
0
    int idx = -1;
137
0
    int poison_ext_is_dup, sct_ext_is_dup;
138
0
    int poison_idx = ct_x509_get_ext(cert, NID_ct_precert_poison, &poison_ext_is_dup);
139
140
    /* Duplicate poison extensions are present - error */
141
0
    if (poison_ext_is_dup)
142
0
        goto err;
143
144
    /* If *cert doesn't have a poison extension, it isn't a precert */
145
0
    if (poison_idx == -1) {
146
        /* cert isn't a precert, so we shouldn't have a presigner */
147
0
        if (presigner != NULL)
148
0
            goto err;
149
150
0
        certderlen = i2d_X509(cert, &certder);
151
0
        if (certderlen < 0)
152
0
            goto err;
153
0
    }
154
155
    /* See if cert has a precert SCTs extension */
156
0
    idx = ct_x509_get_ext(cert, NID_ct_precert_scts, &sct_ext_is_dup);
157
    /* Duplicate SCT extensions are present - error */
158
0
    if (sct_ext_is_dup)
159
0
        goto err;
160
161
0
    if (idx >= 0 && poison_idx >= 0) {
162
        /*
163
         * cert can't both contain SCTs (i.e. have an SCT extension) and be a
164
         * precert (i.e. have a poison extension).
165
         */
166
0
        goto err;
167
0
    }
168
169
0
    if (idx == -1) {
170
0
        idx = poison_idx;
171
0
    }
172
173
    /*
174
     * If either a poison or SCT extension is present, remove it before encoding
175
     * cert. This, along with ct_x509_cert_fixup(), gets a TBSCertificate (see
176
     * RFC5280) from cert, which is what the CT log signed when it produced the
177
     * SCT.
178
     */
179
0
    if (idx >= 0) {
180
        /* Take a copy of certificate so we don't modify passed version */
181
0
        pretmp = X509_dup(cert);
182
0
        if (pretmp == NULL)
183
0
            goto err;
184
185
0
        X509_EXTENSION_free(X509_delete_ext(pretmp, idx));
186
187
0
        if (!ct_x509_cert_fixup(pretmp, presigner))
188
0
            goto err;
189
190
0
        prederlen = i2d_re_X509_tbs(pretmp, &preder);
191
0
        if (prederlen <= 0)
192
0
            goto err;
193
0
    }
194
195
0
    X509_free(pretmp);
196
197
0
    OPENSSL_free(sctx->certder);
198
0
    sctx->certder = certder;
199
0
    sctx->certderlen = certderlen;
200
201
0
    OPENSSL_free(sctx->preder);
202
0
    sctx->preder = preder;
203
0
    sctx->prederlen = prederlen;
204
205
0
    return 1;
206
0
err:
207
0
    OPENSSL_free(certder);
208
0
    OPENSSL_free(preder);
209
0
    X509_free(pretmp);
210
0
    return 0;
211
0
}
212
213
__owur static int ct_public_key_hash(SCT_CTX *sctx, const X509_PUBKEY *pkey,
214
    unsigned char **hash, size_t *hash_len)
215
0
{
216
0
    int ret = 0;
217
0
    unsigned char *md = NULL, *der = NULL;
218
0
    int der_len;
219
0
    unsigned int md_len;
220
0
    EVP_MD *sha256 = EVP_MD_fetch(sctx->libctx, "SHA2-256", sctx->propq);
221
222
0
    if (sha256 == NULL)
223
0
        goto err;
224
225
    /* Reuse buffer if possible */
226
0
    if (*hash != NULL && *hash_len >= SHA256_DIGEST_LENGTH) {
227
0
        md = *hash;
228
0
    } else {
229
0
        md = OPENSSL_malloc(SHA256_DIGEST_LENGTH);
230
0
        if (md == NULL)
231
0
            goto err;
232
0
    }
233
234
    /* Calculate key hash */
235
0
    der_len = i2d_X509_PUBKEY(pkey, &der);
236
0
    if (der_len <= 0)
237
0
        goto err;
238
239
0
    if (!EVP_Digest(der, der_len, md, &md_len, sha256, NULL))
240
0
        goto err;
241
242
0
    if (md != *hash) {
243
0
        OPENSSL_free(*hash);
244
0
        *hash = md;
245
0
        *hash_len = SHA256_DIGEST_LENGTH;
246
0
    }
247
248
0
    md = NULL;
249
0
    ret = 1;
250
0
err:
251
0
    EVP_MD_free(sha256);
252
0
    OPENSSL_free(md);
253
0
    OPENSSL_free(der);
254
0
    return ret;
255
0
}
256
257
int SCT_CTX_set1_issuer(SCT_CTX *sctx, X509 *issuer)
258
0
{
259
0
    return SCT_CTX_set1_issuer_pubkey(sctx, X509_get_X509_PUBKEY(issuer));
260
0
}
261
262
int SCT_CTX_set1_issuer_pubkey(SCT_CTX *sctx, const X509_PUBKEY *pubkey)
263
0
{
264
0
    return ct_public_key_hash(sctx, pubkey, &sctx->ihash, &sctx->ihashlen);
265
0
}
266
267
int SCT_CTX_set1_pubkey(SCT_CTX *sctx, X509_PUBKEY *pubkey)
268
0
{
269
0
    EVP_PKEY *pkey = X509_PUBKEY_get(pubkey);
270
271
0
    if (pkey == NULL)
272
0
        return 0;
273
274
0
    if (!ct_public_key_hash(sctx, pubkey, &sctx->pkeyhash, &sctx->pkeyhashlen)) {
275
0
        EVP_PKEY_free(pkey);
276
0
        return 0;
277
0
    }
278
279
0
    EVP_PKEY_free(sctx->pkey);
280
0
    sctx->pkey = pkey;
281
0
    return 1;
282
0
}
283
284
void SCT_CTX_set_time(SCT_CTX *sctx, uint64_t time_in_ms)
285
0
{
286
0
    sctx->epoch_time_in_ms = time_in_ms;
287
0
}