Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/ct/ct_sct.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 disabled"
12
#endif
13
14
#include <openssl/ct.h>
15
#include <openssl/err.h>
16
#include <openssl/evp.h>
17
#include <openssl/tls1.h>
18
#include <openssl/x509.h>
19
20
#include "ct_local.h"
21
22
SCT *SCT_new(void)
23
959k
{
24
959k
    SCT *sct = OPENSSL_zalloc(sizeof(*sct));
25
26
959k
    if (sct == NULL) {
27
0
        ERR_raise(ERR_LIB_CT, ERR_R_MALLOC_FAILURE);
28
0
        return NULL;
29
0
    }
30
31
959k
    sct->entry_type = CT_LOG_ENTRY_TYPE_NOT_SET;
32
959k
    sct->version = SCT_VERSION_NOT_SET;
33
959k
    return sct;
34
959k
}
35
36
void SCT_free(SCT *sct)
37
959k
{
38
959k
    if (sct == NULL)
39
0
        return;
40
41
959k
    OPENSSL_free(sct->log_id);
42
959k
    OPENSSL_free(sct->ext);
43
959k
    OPENSSL_free(sct->sig);
44
959k
    OPENSSL_free(sct->sct);
45
959k
    OPENSSL_free(sct);
46
959k
}
47
48
void SCT_LIST_free(STACK_OF(SCT) *a)
49
290k
{
50
290k
    sk_SCT_pop_free(a, SCT_free);
51
290k
}
52
53
int SCT_set_version(SCT *sct, sct_version_t version)
54
0
{
55
0
    if (version != SCT_VERSION_V1) {
56
0
        ERR_raise(ERR_LIB_CT, CT_R_UNSUPPORTED_VERSION);
57
0
        return 0;
58
0
    }
59
0
    sct->version = version;
60
0
    sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
61
0
    return 1;
62
0
}
63
64
int SCT_set_log_entry_type(SCT *sct, ct_log_entry_type_t entry_type)
65
106k
{
66
106k
    sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
67
68
106k
    switch (entry_type) {
69
68.7k
    case CT_LOG_ENTRY_TYPE_X509:
70
106k
    case CT_LOG_ENTRY_TYPE_PRECERT:
71
106k
        sct->entry_type = entry_type;
72
106k
        return 1;
73
0
    case CT_LOG_ENTRY_TYPE_NOT_SET:
74
0
        break;
75
106k
    }
76
106k
    ERR_raise(ERR_LIB_CT, CT_R_UNSUPPORTED_ENTRY_TYPE);
77
0
    return 0;
78
106k
}
79
80
int SCT_set0_log_id(SCT *sct, unsigned char *log_id, size_t log_id_len)
81
0
{
82
0
    if (sct->version == SCT_VERSION_V1 && log_id_len != CT_V1_HASHLEN) {
83
0
        ERR_raise(ERR_LIB_CT, CT_R_INVALID_LOG_ID_LENGTH);
84
0
        return 0;
85
0
    }
86
87
0
    OPENSSL_free(sct->log_id);
88
0
    sct->log_id = log_id;
89
0
    sct->log_id_len = log_id_len;
90
0
    sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
91
0
    return 1;
92
0
}
93
94
int SCT_set1_log_id(SCT *sct, const unsigned char *log_id, size_t log_id_len)
95
0
{
96
0
    if (sct->version == SCT_VERSION_V1 && log_id_len != CT_V1_HASHLEN) {
97
0
        ERR_raise(ERR_LIB_CT, CT_R_INVALID_LOG_ID_LENGTH);
98
0
        return 0;
99
0
    }
100
101
0
    OPENSSL_free(sct->log_id);
102
0
    sct->log_id = NULL;
103
0
    sct->log_id_len = 0;
104
0
    sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
105
106
0
    if (log_id != NULL && log_id_len > 0) {
107
0
        sct->log_id = OPENSSL_memdup(log_id, log_id_len);
108
0
        if (sct->log_id == NULL) {
109
0
            ERR_raise(ERR_LIB_CT, ERR_R_MALLOC_FAILURE);
110
0
            return 0;
111
0
        }
112
0
        sct->log_id_len = log_id_len;
113
0
    }
114
0
    return 1;
115
0
}
116
117
void SCT_set_timestamp(SCT *sct, uint64_t timestamp)
118
0
{
119
0
    sct->timestamp = timestamp;
120
0
    sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
121
0
}
122
123
int SCT_set_signature_nid(SCT *sct, int nid)
124
0
{
125
0
    switch (nid) {
126
0
    case NID_sha256WithRSAEncryption:
127
0
        sct->hash_alg = TLSEXT_hash_sha256;
128
0
        sct->sig_alg = TLSEXT_signature_rsa;
129
0
        sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
130
0
        return 1;
131
0
    case NID_ecdsa_with_SHA256:
132
0
        sct->hash_alg = TLSEXT_hash_sha256;
133
0
        sct->sig_alg = TLSEXT_signature_ecdsa;
134
0
        sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
135
0
        return 1;
136
0
    default:
137
0
        ERR_raise(ERR_LIB_CT, CT_R_UNRECOGNIZED_SIGNATURE_NID);
138
0
        return 0;
139
0
    }
140
0
}
141
142
void SCT_set0_extensions(SCT *sct, unsigned char *ext, size_t ext_len)
143
0
{
144
0
    OPENSSL_free(sct->ext);
145
0
    sct->ext = ext;
146
0
    sct->ext_len = ext_len;
147
0
    sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
148
0
}
149
150
int SCT_set1_extensions(SCT *sct, const unsigned char *ext, size_t ext_len)
151
0
{
152
0
    OPENSSL_free(sct->ext);
153
0
    sct->ext = NULL;
154
0
    sct->ext_len = 0;
155
0
    sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
156
157
0
    if (ext != NULL && ext_len > 0) {
158
0
        sct->ext = OPENSSL_memdup(ext, ext_len);
159
0
        if (sct->ext == NULL) {
160
0
            ERR_raise(ERR_LIB_CT, ERR_R_MALLOC_FAILURE);
161
0
            return 0;
162
0
        }
163
0
        sct->ext_len = ext_len;
164
0
    }
165
0
    return 1;
166
0
}
167
168
void SCT_set0_signature(SCT *sct, unsigned char *sig, size_t sig_len)
169
0
{
170
0
    OPENSSL_free(sct->sig);
171
0
    sct->sig = sig;
172
0
    sct->sig_len = sig_len;
173
0
    sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
174
0
}
175
176
int SCT_set1_signature(SCT *sct, const unsigned char *sig, size_t sig_len)
177
96.6k
{
178
96.6k
    OPENSSL_free(sct->sig);
179
96.6k
    sct->sig = NULL;
180
96.6k
    sct->sig_len = 0;
181
96.6k
    sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
182
183
96.6k
    if (sig != NULL && sig_len > 0) {
184
40.7k
        sct->sig = OPENSSL_memdup(sig, sig_len);
185
40.7k
        if (sct->sig == NULL) {
186
0
            ERR_raise(ERR_LIB_CT, ERR_R_MALLOC_FAILURE);
187
0
            return 0;
188
0
        }
189
40.7k
        sct->sig_len = sig_len;
190
40.7k
    }
191
96.6k
    return 1;
192
96.6k
}
193
194
sct_version_t SCT_get_version(const SCT *sct)
195
0
{
196
0
    return sct->version;
197
0
}
198
199
ct_log_entry_type_t SCT_get_log_entry_type(const SCT *sct)
200
0
{
201
0
    return sct->entry_type;
202
0
}
203
204
size_t SCT_get0_log_id(const SCT *sct, unsigned char **log_id)
205
0
{
206
0
    *log_id = sct->log_id;
207
0
    return sct->log_id_len;
208
0
}
209
210
uint64_t SCT_get_timestamp(const SCT *sct)
211
0
{
212
0
    return sct->timestamp;
213
0
}
214
215
int SCT_get_signature_nid(const SCT *sct)
216
252k
{
217
252k
    if (sct->version == SCT_VERSION_V1) {
218
252k
        if (sct->hash_alg == TLSEXT_hash_sha256) {
219
243k
            switch (sct->sig_alg) {
220
48.7k
            case TLSEXT_signature_ecdsa:
221
48.7k
                return NID_ecdsa_with_SHA256;
222
192k
            case TLSEXT_signature_rsa:
223
192k
                return NID_sha256WithRSAEncryption;
224
1.69k
            default:
225
1.69k
                return NID_undef;
226
243k
            }
227
243k
        }
228
252k
    }
229
8.72k
    return NID_undef;
230
252k
}
231
232
size_t SCT_get0_extensions(const SCT *sct, unsigned char **ext)
233
0
{
234
0
    *ext = sct->ext;
235
0
    return sct->ext_len;
236
0
}
237
238
size_t SCT_get0_signature(const SCT *sct, unsigned char **sig)
239
0
{
240
0
    *sig = sct->sig;
241
0
    return sct->sig_len;
242
0
}
243
244
int SCT_is_complete(const SCT *sct)
245
859k
{
246
859k
    switch (sct->version) {
247
0
    case SCT_VERSION_NOT_SET:
248
0
        return 0;
249
28.9k
    case SCT_VERSION_V1:
250
28.9k
        return sct->log_id != NULL && SCT_signature_is_complete(sct);
251
830k
    default:
252
830k
        return sct->sct != NULL; /* Just need cached encoding */
253
859k
    }
254
859k
}
255
256
int SCT_signature_is_complete(const SCT *sct)
257
41.9k
{
258
41.9k
    return SCT_get_signature_nid(sct) != NID_undef && sct->sig != NULL && sct->sig_len > 0;
259
41.9k
}
260
261
sct_source_t SCT_get_source(const SCT *sct)
262
0
{
263
0
    return sct->source;
264
0
}
265
266
int SCT_set_source(SCT *sct, sct_source_t source)
267
106k
{
268
106k
    sct->source = source;
269
106k
    sct->validation_status = SCT_VALIDATION_STATUS_NOT_SET;
270
106k
    switch (source) {
271
0
    case SCT_SOURCE_TLS_EXTENSION:
272
68.7k
    case SCT_SOURCE_OCSP_STAPLED_RESPONSE:
273
68.7k
        return SCT_set_log_entry_type(sct, CT_LOG_ENTRY_TYPE_X509);
274
37.2k
    case SCT_SOURCE_X509V3_EXTENSION:
275
37.2k
        return SCT_set_log_entry_type(sct, CT_LOG_ENTRY_TYPE_PRECERT);
276
0
    case SCT_SOURCE_UNKNOWN:
277
0
        break;
278
106k
    }
279
    /* if we aren't sure, leave the log entry type alone */
280
0
    return 1;
281
106k
}
282
283
sct_validation_status_t SCT_get_validation_status(const SCT *sct)
284
0
{
285
0
    return sct->validation_status;
286
0
}
287
288
int SCT_validate(SCT *sct, const CT_POLICY_EVAL_CTX *ctx)
289
0
{
290
0
    int is_sct_valid = -1;
291
0
    SCT_CTX *sctx = NULL;
292
0
    X509_PUBKEY *pub = NULL, *log_pkey = NULL;
293
0
    const CTLOG *log;
294
295
    /*
296
     * With an unrecognized SCT version we don't know what such an SCT means,
297
     * let alone validate one.  So we return validation failure (0).
298
     */
299
0
    if (sct->version != SCT_VERSION_V1) {
300
0
        sct->validation_status = SCT_VALIDATION_STATUS_UNKNOWN_VERSION;
301
0
        return 0;
302
0
    }
303
304
0
    log = CTLOG_STORE_get0_log_by_id(ctx->log_store,
305
0
        sct->log_id, sct->log_id_len);
306
307
    /* Similarly, an SCT from an unknown log also cannot be validated. */
308
0
    if (log == NULL) {
309
0
        sct->validation_status = SCT_VALIDATION_STATUS_UNKNOWN_LOG;
310
0
        return 0;
311
0
    }
312
313
0
    sctx = SCT_CTX_new(ctx->libctx, ctx->propq);
314
0
    if (sctx == NULL)
315
0
        goto err;
316
317
0
    if (X509_PUBKEY_set(&log_pkey, CTLOG_get0_public_key(log)) != 1)
318
0
        goto err;
319
0
    if (SCT_CTX_set1_pubkey(sctx, log_pkey) != 1)
320
0
        goto err;
321
322
0
    if (SCT_get_log_entry_type(sct) == CT_LOG_ENTRY_TYPE_PRECERT) {
323
0
        EVP_PKEY *issuer_pkey;
324
325
0
        if (ctx->issuer == NULL) {
326
0
            sct->validation_status = SCT_VALIDATION_STATUS_UNVERIFIED;
327
0
            goto end;
328
0
        }
329
330
0
        issuer_pkey = X509_get0_pubkey(ctx->issuer);
331
332
0
        if (X509_PUBKEY_set(&pub, issuer_pkey) != 1)
333
0
            goto err;
334
0
        if (SCT_CTX_set1_issuer_pubkey(sctx, pub) != 1)
335
0
            goto err;
336
0
    }
337
338
0
    SCT_CTX_set_time(sctx, ctx->epoch_time_in_ms);
339
340
    /*
341
     * XXX: Potential for optimization.  This repeats some idempotent heavy
342
     * lifting on the certificate for each candidate SCT, and appears to not
343
     * use any information in the SCT itself, only the certificate is
344
     * processed.  So it may make more sense to do this just once, perhaps
345
     * associated with the shared (by all SCTs) policy eval ctx.
346
     *
347
     * XXX: Failure here is global (SCT independent) and represents either an
348
     * issue with the certificate (e.g. duplicate extensions) or an out of
349
     * memory condition.  When the certificate is incompatible with CT, we just
350
     * mark the SCTs invalid, rather than report a failure to determine the
351
     * validation status.  That way, callbacks that want to do "soft" SCT
352
     * processing will not abort handshakes with false positive internal
353
     * errors.  Since the function does not distinguish between certificate
354
     * issues (peer's fault) and internal problems (out fault) the safe thing
355
     * to do is to report a validation failure and let the callback or
356
     * application decide what to do.
357
     */
358
0
    if (SCT_CTX_set1_cert(sctx, ctx->cert, NULL) != 1)
359
0
        sct->validation_status = SCT_VALIDATION_STATUS_UNVERIFIED;
360
0
    else
361
0
        sct->validation_status = SCT_CTX_verify(sctx, sct) == 1 ? SCT_VALIDATION_STATUS_VALID : SCT_VALIDATION_STATUS_INVALID;
362
363
0
end:
364
0
    is_sct_valid = sct->validation_status == SCT_VALIDATION_STATUS_VALID;
365
0
err:
366
0
    X509_PUBKEY_free(pub);
367
0
    X509_PUBKEY_free(log_pkey);
368
0
    SCT_CTX_free(sctx);
369
370
0
    return is_sct_valid;
371
0
}
372
373
int SCT_LIST_validate(const STACK_OF(SCT) *scts, CT_POLICY_EVAL_CTX *ctx)
374
0
{
375
0
    int are_scts_valid = 1;
376
0
    int sct_count = scts != NULL ? sk_SCT_num(scts) : 0;
377
0
    int i;
378
379
0
    for (i = 0; i < sct_count; ++i) {
380
0
        int is_sct_valid = -1;
381
0
        SCT *sct = sk_SCT_value(scts, i);
382
383
0
        if (sct == NULL)
384
0
            continue;
385
386
0
        is_sct_valid = SCT_validate(sct, ctx);
387
0
        if (is_sct_valid < 0)
388
0
            return is_sct_valid;
389
0
        are_scts_valid &= is_sct_valid;
390
0
    }
391
392
0
    return are_scts_valid;
393
0
}