Coverage Report

Created: 2026-05-24 07:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/cms/cms_ess.c
Line
Count
Source
1
/*
2
 * Copyright 2008-2026 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 "internal/cryptlib.h"
11
#include <openssl/asn1t.h>
12
#include <openssl/pem.h>
13
#include <openssl/rand.h>
14
#include <openssl/x509v3.h>
15
#include <openssl/err.h>
16
#include <openssl/cms.h>
17
#include <openssl/ess.h>
18
#include "crypto/ess.h"
19
#include "crypto/x509.h"
20
#include "cms_local.h"
21
22
IMPLEMENT_ASN1_FUNCTIONS(CMS_ReceiptRequest)
23
24
/* ESS services */
25
26
int CMS_get1_ReceiptRequest(CMS_SignerInfo *si, CMS_ReceiptRequest **prr)
27
0
{
28
0
    const ASN1_STRING *str;
29
0
    CMS_ReceiptRequest *rr;
30
0
    ASN1_OBJECT *obj = OBJ_nid2obj(NID_id_smime_aa_receiptRequest);
31
32
0
    if (prr != NULL)
33
0
        *prr = NULL;
34
0
    str = CMS_signed_get0_data_by_OBJ(si, obj, -3, V_ASN1_SEQUENCE);
35
0
    if (str == NULL)
36
0
        return 0;
37
38
0
    rr = ASN1_item_unpack(str, ASN1_ITEM_rptr(CMS_ReceiptRequest));
39
0
    if (rr == NULL)
40
0
        return -1;
41
0
    if (prr != NULL)
42
0
        *prr = rr;
43
0
    else
44
0
        CMS_ReceiptRequest_free(rr);
45
0
    return 1;
46
0
}
47
48
/*
49
 * Returns 0 if attribute is not found, 1 if found,
50
 * or -1 on attribute parsing failure.
51
 */
52
static int ossl_cms_signerinfo_get_signing_cert(const CMS_SignerInfo *si,
53
    ESS_SIGNING_CERT **psc)
54
0
{
55
0
    const ASN1_STRING *str;
56
0
    ESS_SIGNING_CERT *sc;
57
0
    ASN1_OBJECT *obj = OBJ_nid2obj(NID_id_smime_aa_signingCertificate);
58
59
0
    if (psc != NULL)
60
0
        *psc = NULL;
61
0
    str = CMS_signed_get0_data_by_OBJ(si, obj, -3, V_ASN1_SEQUENCE);
62
0
    if (str == NULL)
63
0
        return 0;
64
65
0
    sc = ASN1_item_unpack(str, ASN1_ITEM_rptr(ESS_SIGNING_CERT));
66
0
    if (sc == NULL)
67
0
        return -1;
68
0
    if (psc != NULL)
69
0
        *psc = sc;
70
0
    else
71
0
        ESS_SIGNING_CERT_free(sc);
72
0
    return 1;
73
0
}
74
75
/*
76
 * Returns 0 if attribute is not found, 1 if found,
77
 * or -1 on attribute parsing failure.
78
 */
79
static int ossl_cms_signerinfo_get_signing_cert_v2(const CMS_SignerInfo *si,
80
    ESS_SIGNING_CERT_V2 **psc)
81
0
{
82
0
    const ASN1_STRING *str;
83
0
    ESS_SIGNING_CERT_V2 *sc;
84
0
    ASN1_OBJECT *obj = OBJ_nid2obj(NID_id_smime_aa_signingCertificateV2);
85
86
0
    if (psc != NULL)
87
0
        *psc = NULL;
88
0
    str = CMS_signed_get0_data_by_OBJ(si, obj, -3, V_ASN1_SEQUENCE);
89
0
    if (str == NULL)
90
0
        return 0;
91
92
0
    sc = ASN1_item_unpack(str, ASN1_ITEM_rptr(ESS_SIGNING_CERT_V2));
93
0
    if (sc == NULL)
94
0
        return -1;
95
0
    if (psc != NULL)
96
0
        *psc = sc;
97
0
    else
98
0
        ESS_SIGNING_CERT_V2_free(sc);
99
0
    return 1;
100
0
}
101
102
int ossl_cms_check_signing_certs(const CMS_SignerInfo *si,
103
    const STACK_OF(X509) *chain)
104
0
{
105
0
    ESS_SIGNING_CERT *ss = NULL;
106
0
    ESS_SIGNING_CERT_V2 *ssv2 = NULL;
107
0
    int ret = ossl_cms_signerinfo_get_signing_cert(si, &ss) >= 0
108
0
        && ossl_cms_signerinfo_get_signing_cert_v2(si, &ssv2) >= 0
109
0
        && OSSL_ESS_check_signing_certs_ex(ss, ssv2, chain,
110
0
               ossl_cms_ctx_get0_libctx(si->cms_ctx),
111
0
               ossl_cms_ctx_get0_propq(si->cms_ctx), 1)
112
0
            > 0;
113
114
0
    ESS_SIGNING_CERT_free(ss);
115
0
    ESS_SIGNING_CERT_V2_free(ssv2);
116
0
    return ret;
117
0
}
118
119
CMS_ReceiptRequest *CMS_ReceiptRequest_create0_ex(
120
    unsigned char *id, int idlen, int allorfirst,
121
    STACK_OF(GENERAL_NAMES) *receiptList, STACK_OF(GENERAL_NAMES) *receiptsTo,
122
    OSSL_LIB_CTX *libctx)
123
0
{
124
0
    CMS_ReceiptRequest *rr;
125
126
0
    rr = CMS_ReceiptRequest_new();
127
0
    if (rr == NULL) {
128
0
        ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
129
0
        goto err;
130
0
    }
131
0
    if (id)
132
0
        ASN1_STRING_set0(rr->signedContentIdentifier, id, idlen);
133
0
    else {
134
0
        if (!ASN1_STRING_set(rr->signedContentIdentifier, NULL, 32)) {
135
0
            ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
136
0
            goto err;
137
0
        }
138
0
        if (RAND_bytes_ex(libctx, rr->signedContentIdentifier->data, 32,
139
0
                0)
140
0
            <= 0)
141
0
            goto err;
142
0
    }
143
144
0
    sk_GENERAL_NAMES_pop_free(rr->receiptsTo, GENERAL_NAMES_free);
145
0
    rr->receiptsTo = receiptsTo;
146
147
0
    if (receiptList != NULL) {
148
0
        rr->receiptsFrom->type = 1;
149
0
        rr->receiptsFrom->d.receiptList = receiptList;
150
0
    } else {
151
0
        rr->receiptsFrom->type = 0;
152
0
        rr->receiptsFrom->d.allOrFirstTier = allorfirst;
153
0
    }
154
155
0
    return rr;
156
157
0
err:
158
0
    CMS_ReceiptRequest_free(rr);
159
0
    return NULL;
160
0
}
161
162
CMS_ReceiptRequest *CMS_ReceiptRequest_create0(
163
    unsigned char *id, int idlen, int allorfirst,
164
    STACK_OF(GENERAL_NAMES) *receiptList, STACK_OF(GENERAL_NAMES) *receiptsTo)
165
0
{
166
0
    return CMS_ReceiptRequest_create0_ex(id, idlen, allorfirst, receiptList,
167
0
        receiptsTo, NULL);
168
0
}
169
170
int CMS_add1_ReceiptRequest(CMS_SignerInfo *si, CMS_ReceiptRequest *rr)
171
0
{
172
0
    unsigned char *rrder = NULL;
173
0
    int rrderlen, r = 0;
174
175
0
    rrderlen = i2d_CMS_ReceiptRequest(rr, &rrder);
176
0
    if (rrderlen < 0) {
177
0
        ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
178
0
        goto err;
179
0
    }
180
181
0
    if (!CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_receiptRequest,
182
0
            V_ASN1_SEQUENCE, rrder, rrderlen)) {
183
0
        ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
184
0
        goto err;
185
0
    }
186
187
0
    r = 1;
188
189
0
err:
190
0
    OPENSSL_free(rrder);
191
192
0
    return r;
193
0
}
194
195
void CMS_ReceiptRequest_get0_values(CMS_ReceiptRequest *rr,
196
    ASN1_STRING **pcid,
197
    int *pallorfirst,
198
    STACK_OF(GENERAL_NAMES) **plist,
199
    STACK_OF(GENERAL_NAMES) **prto)
200
0
{
201
0
    if (pcid != NULL)
202
0
        *pcid = rr->signedContentIdentifier;
203
0
    if (rr->receiptsFrom->type == 0) {
204
0
        if (pallorfirst != NULL)
205
0
            *pallorfirst = (int)rr->receiptsFrom->d.allOrFirstTier;
206
0
        if (plist != NULL)
207
0
            *plist = NULL;
208
0
    } else {
209
0
        if (pallorfirst != NULL)
210
0
            *pallorfirst = -1;
211
0
        if (plist != NULL)
212
0
            *plist = rr->receiptsFrom->d.receiptList;
213
0
    }
214
0
    if (prto != NULL)
215
0
        *prto = rr->receiptsTo;
216
0
}
217
218
/* Digest a SignerInfo structure for msgSigDigest attribute processing */
219
220
static int cms_msgSigDigest(CMS_SignerInfo *si,
221
    unsigned char *dig, unsigned int *diglen)
222
0
{
223
0
    const EVP_MD *md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
224
225
0
    if (md == NULL)
226
0
        return 0;
227
0
    if (!ossl_asn1_item_digest_ex(ASN1_ITEM_rptr(CMS_Attributes_Verify), md,
228
0
            si->signedAttrs, dig, diglen,
229
0
            ossl_cms_ctx_get0_libctx(si->cms_ctx),
230
0
            ossl_cms_ctx_get0_propq(si->cms_ctx)))
231
0
        return 0;
232
0
    return 1;
233
0
}
234
235
/* Add a msgSigDigest attribute to a SignerInfo */
236
237
int ossl_cms_msgSigDigest_add1(CMS_SignerInfo *dest, CMS_SignerInfo *src)
238
0
{
239
0
    unsigned char dig[EVP_MAX_MD_SIZE];
240
0
    unsigned int diglen;
241
242
0
    if (!cms_msgSigDigest(src, dig, &diglen)) {
243
0
        ERR_raise(ERR_LIB_CMS, CMS_R_MSGSIGDIGEST_ERROR);
244
0
        return 0;
245
0
    }
246
0
    if (!CMS_signed_add1_attr_by_NID(dest, NID_id_smime_aa_msgSigDigest,
247
0
            V_ASN1_OCTET_STRING, dig, diglen)) {
248
0
        ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
249
0
        return 0;
250
0
    }
251
0
    return 1;
252
0
}
253
254
/* Verify signed receipt after it has already passed normal CMS verify */
255
256
int ossl_cms_Receipt_verify(CMS_ContentInfo *cms, CMS_ContentInfo *req_cms)
257
0
{
258
0
    int r = 0, i;
259
0
    CMS_ReceiptRequest *rr = NULL;
260
0
    CMS_Receipt *rct = NULL;
261
0
    STACK_OF(CMS_SignerInfo) *sis, *osis;
262
0
    CMS_SignerInfo *si, *osi = NULL;
263
0
    const ASN1_OCTET_STRING *msig;
264
0
    ASN1_OCTET_STRING **pcont;
265
0
    const ASN1_OBJECT *octype;
266
0
    unsigned char dig[EVP_MAX_MD_SIZE];
267
0
    unsigned int diglen;
268
269
    /* Get SignerInfos, also checks SignedData content type */
270
0
    osis = CMS_get0_SignerInfos(req_cms);
271
0
    sis = CMS_get0_SignerInfos(cms);
272
0
    if (!osis || !sis)
273
0
        goto err;
274
275
0
    if (sk_CMS_SignerInfo_num(sis) != 1) {
276
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NEED_ONE_SIGNER);
277
0
        goto err;
278
0
    }
279
280
    /* Check receipt content type */
281
0
    if (OBJ_obj2nid(CMS_get0_eContentType(cms)) != NID_id_smime_ct_receipt) {
282
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NOT_A_SIGNED_RECEIPT);
283
0
        goto err;
284
0
    }
285
286
    /* Extract and decode receipt content */
287
0
    pcont = CMS_get0_content(cms);
288
0
    if (pcont == NULL || *pcont == NULL) {
289
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NO_CONTENT);
290
0
        goto err;
291
0
    }
292
293
0
    rct = ASN1_item_unpack(*pcont, ASN1_ITEM_rptr(CMS_Receipt));
294
295
0
    if (!rct) {
296
0
        ERR_raise(ERR_LIB_CMS, CMS_R_RECEIPT_DECODE_ERROR);
297
0
        goto err;
298
0
    }
299
300
    /* Locate original request */
301
302
0
    for (i = 0; i < sk_CMS_SignerInfo_num(osis); i++) {
303
0
        osi = sk_CMS_SignerInfo_value(osis, i);
304
0
        if (!ASN1_STRING_cmp(osi->signature, rct->originatorSignatureValue))
305
0
            break;
306
0
    }
307
308
0
    if (i == sk_CMS_SignerInfo_num(osis)) {
309
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_SIGNATURE);
310
0
        goto err;
311
0
    }
312
313
0
    si = sk_CMS_SignerInfo_value(sis, 0);
314
315
    /* Get msgSigDigest value and compare */
316
317
0
    msig = CMS_signed_get0_data_by_OBJ(si,
318
0
        OBJ_nid2obj(NID_id_smime_aa_msgSigDigest), -3,
319
0
        V_ASN1_OCTET_STRING);
320
321
0
    if (!msig) {
322
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NO_MSGSIGDIGEST);
323
0
        goto err;
324
0
    }
325
326
0
    if (!cms_msgSigDigest(osi, dig, &diglen)) {
327
0
        ERR_raise(ERR_LIB_CMS, CMS_R_MSGSIGDIGEST_ERROR);
328
0
        goto err;
329
0
    }
330
331
0
    if (diglen != (unsigned int)msig->length) {
332
0
        ERR_raise(ERR_LIB_CMS, CMS_R_MSGSIGDIGEST_WRONG_LENGTH);
333
0
        goto err;
334
0
    }
335
336
0
    if (memcmp(dig, msig->data, diglen)) {
337
0
        ERR_raise(ERR_LIB_CMS, CMS_R_MSGSIGDIGEST_VERIFICATION_FAILURE);
338
0
        goto err;
339
0
    }
340
341
    /* Compare content types */
342
343
0
    octype = CMS_signed_get0_data_by_OBJ(osi,
344
0
        OBJ_nid2obj(NID_pkcs9_contentType),
345
0
        -3, V_ASN1_OBJECT);
346
0
    if (!octype) {
347
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NO_CONTENT_TYPE);
348
0
        goto err;
349
0
    }
350
351
    /* Compare details in receipt request */
352
353
0
    if (OBJ_cmp(octype, rct->contentType)) {
354
0
        ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_TYPE_MISMATCH);
355
0
        goto err;
356
0
    }
357
358
    /* Get original receipt request details */
359
360
0
    if (CMS_get1_ReceiptRequest(osi, &rr) <= 0) {
361
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NO_RECEIPT_REQUEST);
362
0
        goto err;
363
0
    }
364
365
0
    if (ASN1_STRING_cmp(rr->signedContentIdentifier,
366
0
            rct->signedContentIdentifier)) {
367
0
        ERR_raise(ERR_LIB_CMS, CMS_R_CONTENTIDENTIFIER_MISMATCH);
368
0
        goto err;
369
0
    }
370
371
0
    r = 1;
372
373
0
err:
374
0
    CMS_ReceiptRequest_free(rr);
375
0
    M_ASN1_free_of(rct, CMS_Receipt);
376
0
    return r;
377
0
}
378
379
/*
380
 * Encode a Receipt into an OCTET STRING read for including into content of a
381
 * SignedData ContentInfo.
382
 */
383
384
ASN1_OCTET_STRING *ossl_cms_encode_Receipt(CMS_SignerInfo *si)
385
0
{
386
0
    CMS_Receipt rct;
387
0
    CMS_ReceiptRequest *rr = NULL;
388
0
    const ASN1_OBJECT *ctype;
389
0
    ASN1_OCTET_STRING *os = NULL;
390
391
    /* Get original receipt request */
392
393
    /* Get original receipt request details */
394
395
0
    if (CMS_get1_ReceiptRequest(si, &rr) <= 0) {
396
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NO_RECEIPT_REQUEST);
397
0
        goto err;
398
0
    }
399
400
    /* Get original content type */
401
402
0
    ctype = CMS_signed_get0_data_by_OBJ(si,
403
0
        OBJ_nid2obj(NID_pkcs9_contentType),
404
0
        -3, V_ASN1_OBJECT);
405
0
    if (!ctype) {
406
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NO_CONTENT_TYPE);
407
0
        goto err;
408
0
    }
409
410
0
    rct.version = 1;
411
0
    rct.contentType = ctype;
412
0
    rct.signedContentIdentifier = rr->signedContentIdentifier;
413
0
    rct.originatorSignatureValue = si->signature;
414
415
0
    os = ASN1_item_pack(&rct, ASN1_ITEM_rptr(CMS_Receipt), NULL);
416
417
0
err:
418
0
    CMS_ReceiptRequest_free(rr);
419
0
    return os;
420
0
}