Coverage Report

Created: 2025-06-13 06:58

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