Coverage Report

Created: 2025-12-10 06:24

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