Coverage Report

Created: 2025-06-13 06:58

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