Coverage Report

Created: 2023-09-25 06:45

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