Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/ess/ess_lib.c
Line
Count
Source
1
/*
2
 * Copyright 2019-2024 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
#include <string.h>
11
#include <openssl/x509v3.h>
12
#include <openssl/err.h>
13
#include <openssl/ess.h>
14
#include "internal/sizes.h"
15
#include "crypto/ess.h"
16
#include "crypto/x509.h"
17
18
static ESS_CERT_ID *ESS_CERT_ID_new_init(const X509 *cert,
19
    int set_issuer_serial);
20
static ESS_CERT_ID_V2 *ESS_CERT_ID_V2_new_init(const EVP_MD *hash_alg,
21
    const X509 *cert,
22
    int set_issuer_serial);
23
24
ESS_SIGNING_CERT *OSSL_ESS_signing_cert_new_init(const X509 *signcert,
25
    const STACK_OF(X509) *certs,
26
    int set_issuer_serial)
27
0
{
28
0
    ESS_CERT_ID *cid = NULL;
29
0
    ESS_SIGNING_CERT *sc;
30
0
    int i;
31
32
0
    if ((sc = ESS_SIGNING_CERT_new()) == NULL)
33
0
        goto err;
34
0
    if (sc->cert_ids == NULL
35
0
        && (sc->cert_ids = sk_ESS_CERT_ID_new_null()) == NULL)
36
0
        goto err;
37
38
0
    if ((cid = ESS_CERT_ID_new_init(signcert, set_issuer_serial)) == NULL
39
0
        || !sk_ESS_CERT_ID_push(sc->cert_ids, cid))
40
0
        goto err;
41
0
    for (i = 0; i < sk_X509_num(certs); ++i) {
42
0
        X509 *cert = sk_X509_value(certs, i);
43
44
0
        if ((cid = ESS_CERT_ID_new_init(cert, 1)) == NULL
45
0
            || !sk_ESS_CERT_ID_push(sc->cert_ids, cid))
46
0
            goto err;
47
0
    }
48
49
0
    return sc;
50
0
err:
51
0
    ESS_SIGNING_CERT_free(sc);
52
0
    ESS_CERT_ID_free(cid);
53
0
    ERR_raise(ERR_LIB_ESS, ERR_R_MALLOC_FAILURE);
54
0
    return NULL;
55
0
}
56
57
static ESS_CERT_ID *ESS_CERT_ID_new_init(const X509 *cert,
58
    int set_issuer_serial)
59
0
{
60
0
    ESS_CERT_ID *cid = NULL;
61
0
    GENERAL_NAME *name = NULL;
62
0
    unsigned char cert_sha1[SHA_DIGEST_LENGTH];
63
64
0
    if ((cid = ESS_CERT_ID_new()) == NULL)
65
0
        goto err;
66
0
    if (!X509_digest(cert, EVP_sha1(), cert_sha1, NULL))
67
0
        goto err;
68
0
    if (!ASN1_OCTET_STRING_set(cid->hash, cert_sha1, SHA_DIGEST_LENGTH))
69
0
        goto err;
70
71
    /* Setting the issuer/serial if requested. */
72
0
    if (!set_issuer_serial)
73
0
        return cid;
74
75
0
    if (cid->issuer_serial == NULL
76
0
        && (cid->issuer_serial = ESS_ISSUER_SERIAL_new()) == NULL)
77
0
        goto err;
78
0
    if ((name = GENERAL_NAME_new()) == NULL)
79
0
        goto err;
80
0
    name->type = GEN_DIRNAME;
81
0
    if ((name->d.dirn = X509_NAME_dup(X509_get_issuer_name(cert))) == NULL)
82
0
        goto err;
83
0
    if (!sk_GENERAL_NAME_push(cid->issuer_serial->issuer, name))
84
0
        goto err;
85
0
    name = NULL; /* Ownership is lost. */
86
0
    ASN1_INTEGER_free(cid->issuer_serial->serial);
87
0
    if ((cid->issuer_serial->serial = ASN1_INTEGER_dup(X509_get0_serialNumber(cert))) == NULL)
88
0
        goto err;
89
90
0
    return cid;
91
0
err:
92
0
    GENERAL_NAME_free(name);
93
0
    ESS_CERT_ID_free(cid);
94
0
    ERR_raise(ERR_LIB_ESS, ERR_R_MALLOC_FAILURE);
95
0
    return NULL;
96
0
}
97
98
ESS_SIGNING_CERT_V2 *OSSL_ESS_signing_cert_v2_new_init(const EVP_MD *hash_alg,
99
    const X509 *signcert,
100
    const STACK_OF(X509) *certs,
101
    int set_issuer_serial)
102
0
{
103
0
    ESS_CERT_ID_V2 *cid = NULL;
104
0
    ESS_SIGNING_CERT_V2 *sc;
105
0
    int i;
106
107
0
    if ((sc = ESS_SIGNING_CERT_V2_new()) == NULL)
108
0
        goto err;
109
0
    cid = ESS_CERT_ID_V2_new_init(hash_alg, signcert, set_issuer_serial);
110
0
    if (cid == NULL)
111
0
        goto err;
112
0
    if (!sk_ESS_CERT_ID_V2_push(sc->cert_ids, cid))
113
0
        goto err;
114
0
    cid = NULL;
115
116
0
    for (i = 0; i < sk_X509_num(certs); ++i) {
117
0
        X509 *cert = sk_X509_value(certs, i);
118
119
0
        if ((cid = ESS_CERT_ID_V2_new_init(hash_alg, cert, 1)) == NULL)
120
0
            goto err;
121
0
        if (!sk_ESS_CERT_ID_V2_push(sc->cert_ids, cid))
122
0
            goto err;
123
0
        cid = NULL;
124
0
    }
125
126
0
    return sc;
127
0
err:
128
0
    ESS_SIGNING_CERT_V2_free(sc);
129
0
    ESS_CERT_ID_V2_free(cid);
130
0
    ERR_raise(ERR_LIB_ESS, ERR_R_MALLOC_FAILURE);
131
0
    return NULL;
132
0
}
133
134
static ESS_CERT_ID_V2 *ESS_CERT_ID_V2_new_init(const EVP_MD *hash_alg,
135
    const X509 *cert,
136
    int set_issuer_serial)
137
0
{
138
0
    ESS_CERT_ID_V2 *cid;
139
0
    GENERAL_NAME *name = NULL;
140
0
    unsigned char hash[EVP_MAX_MD_SIZE];
141
0
    unsigned int hash_len = sizeof(hash);
142
0
    X509_ALGOR *alg = NULL;
143
144
0
    memset(hash, 0, sizeof(hash));
145
146
0
    if ((cid = ESS_CERT_ID_V2_new()) == NULL)
147
0
        goto err;
148
149
0
    if (!EVP_MD_is_a(hash_alg, SN_sha256)) {
150
0
        alg = X509_ALGOR_new();
151
0
        if (alg == NULL)
152
0
            goto err;
153
0
        X509_ALGOR_set_md(alg, hash_alg);
154
0
        if (alg->algorithm == NULL)
155
0
            goto err;
156
0
        cid->hash_alg = alg;
157
0
        alg = NULL;
158
0
    } else {
159
0
        cid->hash_alg = NULL;
160
0
    }
161
162
0
    if (!X509_digest(cert, hash_alg, hash, &hash_len))
163
0
        goto err;
164
165
0
    if (!ASN1_OCTET_STRING_set(cid->hash, hash, hash_len))
166
0
        goto err;
167
168
0
    if (!set_issuer_serial)
169
0
        return cid;
170
171
0
    if ((cid->issuer_serial = ESS_ISSUER_SERIAL_new()) == NULL)
172
0
        goto err;
173
0
    if ((name = GENERAL_NAME_new()) == NULL)
174
0
        goto err;
175
0
    name->type = GEN_DIRNAME;
176
0
    if ((name->d.dirn = X509_NAME_dup(X509_get_issuer_name(cert))) == NULL)
177
0
        goto err;
178
0
    if (!sk_GENERAL_NAME_push(cid->issuer_serial->issuer, name))
179
0
        goto err;
180
0
    name = NULL; /* Ownership is lost. */
181
0
    ASN1_INTEGER_free(cid->issuer_serial->serial);
182
0
    cid->issuer_serial->serial = ASN1_INTEGER_dup(X509_get0_serialNumber(cert));
183
0
    if (cid->issuer_serial->serial == NULL)
184
0
        goto err;
185
186
0
    return cid;
187
0
err:
188
0
    X509_ALGOR_free(alg);
189
0
    GENERAL_NAME_free(name);
190
0
    ESS_CERT_ID_V2_free(cid);
191
0
    ERR_raise(ERR_LIB_ESS, ERR_R_MALLOC_FAILURE);
192
0
    return NULL;
193
0
}
194
195
static int ess_issuer_serial_cmp(const ESS_ISSUER_SERIAL *is, const X509 *cert)
196
0
{
197
0
    GENERAL_NAME *issuer;
198
199
0
    if (is == NULL || cert == NULL || sk_GENERAL_NAME_num(is->issuer) != 1)
200
0
        return -1;
201
202
0
    issuer = sk_GENERAL_NAME_value(is->issuer, 0);
203
0
    if (issuer->type != GEN_DIRNAME
204
0
        || X509_NAME_cmp(issuer->d.dirn, X509_get_issuer_name(cert)) != 0)
205
0
        return -1;
206
207
0
    return ASN1_INTEGER_cmp(is->serial, X509_get0_serialNumber(cert));
208
0
}
209
210
/*
211
 * Find the cert in |certs| referenced by |cid| if not NULL, else by |cid_v2|.
212
 * The cert must be the first one in |certs| if and only if |index| is 0.
213
 * Return 0 on not found, -1 on error, else 1 + the position in |certs|.
214
 */
215
static int find(const ESS_CERT_ID *cid, const ESS_CERT_ID_V2 *cid_v2,
216
    int index, const STACK_OF(X509) *certs)
217
0
{
218
0
    const X509 *cert;
219
0
    EVP_MD *md = NULL;
220
0
    char name[OSSL_MAX_NAME_SIZE];
221
0
    unsigned char cert_digest[EVP_MAX_MD_SIZE];
222
0
    unsigned int len, cid_hash_len;
223
0
    const ESS_ISSUER_SERIAL *is;
224
0
    int i;
225
0
    int ret = -1;
226
227
0
    if (cid == NULL && cid_v2 == NULL) {
228
0
        ERR_raise(ERR_LIB_ESS, ERR_R_PASSED_INVALID_ARGUMENT);
229
0
        return -1;
230
0
    }
231
232
0
    if (cid != NULL)
233
0
        strcpy(name, "SHA1");
234
0
    else if (cid_v2->hash_alg == NULL)
235
0
        strcpy(name, "SHA256");
236
0
    else
237
0
        OBJ_obj2txt(name, sizeof(name), cid_v2->hash_alg->algorithm, 0);
238
239
0
    (void)ERR_set_mark();
240
0
    md = EVP_MD_fetch(NULL, name, NULL);
241
242
0
    if (md == NULL)
243
0
        md = (EVP_MD *)EVP_get_digestbyname(name);
244
245
0
    if (md == NULL) {
246
0
        (void)ERR_clear_last_mark();
247
0
        ERR_raise(ERR_LIB_ESS, ESS_R_ESS_DIGEST_ALG_UNKNOWN);
248
0
        goto end;
249
0
    }
250
0
    (void)ERR_pop_to_mark();
251
252
0
    for (i = 0; i < sk_X509_num(certs); ++i) {
253
0
        cert = sk_X509_value(certs, i);
254
255
0
        cid_hash_len = cid != NULL ? cid->hash->length : cid_v2->hash->length;
256
0
        if (!X509_digest(cert, md, cert_digest, &len)
257
0
            || cid_hash_len != len) {
258
0
            ERR_raise(ERR_LIB_ESS, ESS_R_ESS_CERT_DIGEST_ERROR);
259
0
            goto end;
260
0
        }
261
262
0
        if (memcmp(cid != NULL ? cid->hash->data : cid_v2->hash->data,
263
0
                cert_digest, len)
264
0
            == 0) {
265
0
            is = cid != NULL ? cid->issuer_serial : cid_v2->issuer_serial;
266
            /* Well, it's not really required to match the serial numbers. */
267
0
            if (is == NULL || ess_issuer_serial_cmp(is, cert) == 0) {
268
0
                if ((i == 0) == (index == 0)) {
269
0
                    ret = i + 1;
270
0
                    goto end;
271
0
                }
272
0
                ERR_raise(ERR_LIB_ESS, ESS_R_ESS_CERT_ID_WRONG_ORDER);
273
0
                goto end;
274
0
            }
275
0
        }
276
0
    }
277
278
0
    ret = 0;
279
0
    ERR_raise(ERR_LIB_ESS, ESS_R_ESS_CERT_ID_NOT_FOUND);
280
0
end:
281
0
    EVP_MD_free(md);
282
0
    return ret;
283
0
}
284
285
int OSSL_ESS_check_signing_certs(const ESS_SIGNING_CERT *ss,
286
    const ESS_SIGNING_CERT_V2 *ssv2,
287
    const STACK_OF(X509) *chain,
288
    int require_signing_cert)
289
0
{
290
0
    int n_v1 = ss == NULL ? -1 : sk_ESS_CERT_ID_num(ss->cert_ids);
291
0
    int n_v2 = ssv2 == NULL ? -1 : sk_ESS_CERT_ID_V2_num(ssv2->cert_ids);
292
0
    int i, ret;
293
294
0
    if (require_signing_cert && ss == NULL && ssv2 == NULL) {
295
0
        ERR_raise(ERR_LIB_ESS, ESS_R_MISSING_SIGNING_CERTIFICATE_ATTRIBUTE);
296
0
        return -1;
297
0
    }
298
0
    if (n_v1 == 0 || n_v2 == 0) {
299
0
        ERR_raise(ERR_LIB_ESS, ESS_R_EMPTY_ESS_CERT_ID_LIST);
300
0
        return -1;
301
0
    }
302
    /* If both ss and ssv2 exist, as required evaluate them independently. */
303
0
    for (i = 0; i < n_v1; i++) {
304
0
        ret = find(sk_ESS_CERT_ID_value(ss->cert_ids, i), NULL, i, chain);
305
0
        if (ret <= 0)
306
0
            return ret;
307
0
    }
308
0
    for (i = 0; i < n_v2; i++) {
309
0
        ret = find(NULL, sk_ESS_CERT_ID_V2_value(ssv2->cert_ids, i), i, chain);
310
0
        if (ret <= 0)
311
0
            return ret;
312
0
    }
313
0
    return 1;
314
0
}