Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl33/crypto/cms/cms_sd.c
Line
Count
Source
1
/*
2
 * Copyright 2008-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 "internal/cryptlib.h"
11
#include <openssl/asn1t.h>
12
#include <openssl/pem.h>
13
#include <openssl/x509.h>
14
#include <openssl/x509v3.h>
15
#include <openssl/err.h>
16
#include <openssl/cms.h>
17
#include <openssl/ess.h>
18
#include "internal/sizes.h"
19
#include "crypto/asn1.h"
20
#include "crypto/evp.h"
21
#include "crypto/ess.h"
22
#include "crypto/x509.h" /* for ossl_x509_add_cert_new() */
23
#include "cms_local.h"
24
25
/* CMS SignedData Utilities */
26
27
static CMS_SignedData *cms_get0_signed(CMS_ContentInfo *cms)
28
6.85k
{
29
6.85k
    if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_signed) {
30
5.29k
        ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_TYPE_NOT_SIGNED_DATA);
31
5.29k
        return NULL;
32
5.29k
    }
33
1.56k
    return cms->d.signedData;
34
6.85k
}
35
36
static CMS_SignedData *cms_signed_data_init(CMS_ContentInfo *cms)
37
0
{
38
0
    if (cms->d.other == NULL) {
39
0
        cms->d.signedData = M_ASN1_new_of(CMS_SignedData);
40
0
        if (!cms->d.signedData) {
41
0
            ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
42
0
            return NULL;
43
0
        }
44
0
        cms->d.signedData->version = 1;
45
0
        cms->d.signedData->encapContentInfo->eContentType = OBJ_nid2obj(NID_pkcs7_data);
46
0
        cms->d.signedData->encapContentInfo->partial = 1;
47
0
        ASN1_OBJECT_free(cms->contentType);
48
0
        cms->contentType = OBJ_nid2obj(NID_pkcs7_signed);
49
0
        return cms->d.signedData;
50
0
    }
51
0
    return cms_get0_signed(cms);
52
0
}
53
54
/* Just initialise SignedData e.g. for certs only structure */
55
int CMS_SignedData_init(CMS_ContentInfo *cms)
56
0
{
57
0
    if (cms_signed_data_init(cms))
58
0
        return 1;
59
0
    else
60
0
        return 0;
61
0
}
62
63
/* Check structures and fixup version numbers (if necessary) */
64
static void cms_sd_set_version(CMS_SignedData *sd)
65
0
{
66
0
    int i;
67
0
    CMS_CertificateChoices *cch;
68
0
    CMS_RevocationInfoChoice *rch;
69
0
    CMS_SignerInfo *si;
70
71
0
    for (i = 0; i < sk_CMS_CertificateChoices_num(sd->certificates); i++) {
72
0
        cch = sk_CMS_CertificateChoices_value(sd->certificates, i);
73
0
        if (cch->type == CMS_CERTCHOICE_OTHER) {
74
0
            if (sd->version < 5)
75
0
                sd->version = 5;
76
0
        } else if (cch->type == CMS_CERTCHOICE_V2ACERT) {
77
0
            if (sd->version < 4)
78
0
                sd->version = 4;
79
0
        } else if (cch->type == CMS_CERTCHOICE_V1ACERT) {
80
0
            if (sd->version < 3)
81
0
                sd->version = 3;
82
0
        }
83
0
    }
84
85
0
    for (i = 0; i < sk_CMS_RevocationInfoChoice_num(sd->crls); i++) {
86
0
        rch = sk_CMS_RevocationInfoChoice_value(sd->crls, i);
87
0
        if (rch->type == CMS_REVCHOICE_OTHER) {
88
0
            if (sd->version < 5)
89
0
                sd->version = 5;
90
0
        }
91
0
    }
92
93
0
    if ((OBJ_obj2nid(sd->encapContentInfo->eContentType) != NID_pkcs7_data)
94
0
        && (sd->version < 3))
95
0
        sd->version = 3;
96
97
0
    for (i = 0; i < sk_CMS_SignerInfo_num(sd->signerInfos); i++) {
98
0
        si = sk_CMS_SignerInfo_value(sd->signerInfos, i);
99
0
        if (si->sid->type == CMS_SIGNERINFO_KEYIDENTIFIER) {
100
0
            if (si->version < 3)
101
0
                si->version = 3;
102
0
            if (sd->version < 3)
103
0
                sd->version = 3;
104
0
        } else if (si->version < 1) {
105
0
            si->version = 1;
106
0
        }
107
0
    }
108
109
0
    if (sd->version < 1)
110
0
        sd->version = 1;
111
0
}
112
113
/*
114
 * RFC 5652 Section 11.1 Content Type
115
 * The content-type attribute within signed-data MUST
116
 *   1) be present if there are signed attributes
117
 *   2) match the content type in the signed-data,
118
 *   3) be a signed attribute.
119
 *   4) not have more than one copy of the attribute.
120
 *
121
 * Note that since the CMS_SignerInfo_sign() always adds the "signing time"
122
 * attribute, the content type attribute MUST be added also.
123
 * Assumptions: This assumes that the attribute does not already exist.
124
 */
125
static int cms_set_si_contentType_attr(CMS_ContentInfo *cms, CMS_SignerInfo *si)
126
0
{
127
0
    ASN1_OBJECT *ctype = cms->d.signedData->encapContentInfo->eContentType;
128
129
    /* Add the contentType attribute */
130
0
    return CMS_signed_add1_attr_by_NID(si, NID_pkcs9_contentType,
131
0
               V_ASN1_OBJECT, ctype, -1)
132
0
        > 0;
133
0
}
134
135
/* Copy an existing messageDigest value */
136
static int cms_copy_messageDigest(CMS_ContentInfo *cms, CMS_SignerInfo *si)
137
0
{
138
0
    STACK_OF(CMS_SignerInfo) *sinfos;
139
0
    CMS_SignerInfo *sitmp;
140
0
    int i;
141
142
0
    sinfos = CMS_get0_SignerInfos(cms);
143
0
    for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
144
0
        ASN1_OCTET_STRING *messageDigest;
145
146
0
        sitmp = sk_CMS_SignerInfo_value(sinfos, i);
147
0
        if (sitmp == si)
148
0
            continue;
149
0
        if (CMS_signed_get_attr_count(sitmp) < 0)
150
0
            continue;
151
0
        if (OBJ_cmp(si->digestAlgorithm->algorithm,
152
0
                sitmp->digestAlgorithm->algorithm))
153
0
            continue;
154
0
        messageDigest = CMS_signed_get0_data_by_OBJ(sitmp,
155
0
            OBJ_nid2obj(NID_pkcs9_messageDigest),
156
0
            -3, V_ASN1_OCTET_STRING);
157
0
        if (!messageDigest) {
158
0
            ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE);
159
0
            return 0;
160
0
        }
161
162
0
        if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest,
163
0
                V_ASN1_OCTET_STRING,
164
0
                messageDigest, -1))
165
0
            return 1;
166
0
        else
167
0
            return 0;
168
0
    }
169
0
    ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_DIGEST);
170
0
    return 0;
171
0
}
172
173
int ossl_cms_set1_SignerIdentifier(CMS_SignerIdentifier *sid, X509 *cert,
174
    int type, const CMS_CTX *ctx)
175
0
{
176
0
    switch (type) {
177
0
    case CMS_SIGNERINFO_ISSUER_SERIAL:
178
0
        if (!ossl_cms_set1_ias(&sid->d.issuerAndSerialNumber, cert))
179
0
            return 0;
180
0
        break;
181
182
0
    case CMS_SIGNERINFO_KEYIDENTIFIER:
183
0
        if (!ossl_cms_set1_keyid(&sid->d.subjectKeyIdentifier, cert))
184
0
            return 0;
185
0
        break;
186
187
0
    default:
188
0
        ERR_raise(ERR_LIB_CMS, CMS_R_UNKNOWN_ID);
189
0
        return 0;
190
0
    }
191
192
0
    sid->type = type;
193
194
0
    return 1;
195
0
}
196
197
int ossl_cms_SignerIdentifier_get0_signer_id(CMS_SignerIdentifier *sid,
198
    ASN1_OCTET_STRING **keyid,
199
    X509_NAME **issuer,
200
    ASN1_INTEGER **sno)
201
0
{
202
0
    if (sid->type == CMS_SIGNERINFO_ISSUER_SERIAL) {
203
0
        if (issuer)
204
0
            *issuer = sid->d.issuerAndSerialNumber->issuer;
205
0
        if (sno)
206
0
            *sno = sid->d.issuerAndSerialNumber->serialNumber;
207
0
    } else if (sid->type == CMS_SIGNERINFO_KEYIDENTIFIER) {
208
0
        if (keyid)
209
0
            *keyid = sid->d.subjectKeyIdentifier;
210
0
    } else {
211
0
        return 0;
212
0
    }
213
0
    return 1;
214
0
}
215
216
int ossl_cms_SignerIdentifier_cert_cmp(CMS_SignerIdentifier *sid, X509 *cert)
217
0
{
218
0
    if (sid->type == CMS_SIGNERINFO_ISSUER_SERIAL)
219
0
        return ossl_cms_ias_cert_cmp(sid->d.issuerAndSerialNumber, cert);
220
0
    else if (sid->type == CMS_SIGNERINFO_KEYIDENTIFIER)
221
0
        return ossl_cms_keyid_cert_cmp(sid->d.subjectKeyIdentifier, cert);
222
0
    else
223
0
        return -1;
224
0
}
225
226
/* Method to map any, incl. provider-implemented PKEY types to OIDs */
227
/* (EC)DSA and all provider-delivered signatures implementation is the same */
228
static int cms_generic_sign(CMS_SignerInfo *si, int verify)
229
0
{
230
0
    if (!ossl_assert(verify == 0 || verify == 1))
231
0
        return -1;
232
233
0
    if (!verify) {
234
0
        EVP_PKEY *pkey = si->pkey;
235
0
        int snid, hnid, pknid = EVP_PKEY_get_id(pkey);
236
0
        X509_ALGOR *alg1, *alg2;
237
238
0
        CMS_SignerInfo_get0_algs(si, NULL, NULL, &alg1, &alg2);
239
0
        if (alg1 == NULL || alg1->algorithm == NULL)
240
0
            return -1;
241
0
        hnid = OBJ_obj2nid(alg1->algorithm);
242
0
        if (hnid == NID_undef)
243
0
            return -1;
244
0
        if (pknid <= 0) { /* check whether a provider registered a NID */
245
0
            const char *typename = EVP_PKEY_get0_type_name(pkey);
246
247
0
            if (typename != NULL)
248
0
                pknid = OBJ_txt2nid(typename);
249
0
        }
250
0
        if (!OBJ_find_sigid_by_algs(&snid, hnid, pknid))
251
0
            return -1;
252
0
        return X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, NULL);
253
0
    }
254
0
    return 1;
255
0
}
256
257
static int cms_sd_asn1_ctrl(CMS_SignerInfo *si, int cmd)
258
0
{
259
0
    EVP_PKEY *pkey = si->pkey;
260
0
    int i;
261
262
0
    if (EVP_PKEY_is_a(pkey, "DSA") || EVP_PKEY_is_a(pkey, "EC"))
263
0
        return cms_generic_sign(si, cmd) > 0;
264
0
    else if (EVP_PKEY_is_a(pkey, "RSA") || EVP_PKEY_is_a(pkey, "RSA-PSS"))
265
0
        return ossl_cms_rsa_sign(si, cmd) > 0;
266
267
    /* Now give engines, providers, etc a chance to handle this */
268
0
    if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL)
269
0
        return cms_generic_sign(si, cmd) > 0;
270
0
    i = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_CMS_SIGN, cmd, si);
271
0
    if (i == -2) {
272
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
273
0
        return 0;
274
0
    }
275
0
    if (i <= 0) {
276
0
        ERR_raise(ERR_LIB_CMS, CMS_R_CTRL_FAILURE);
277
0
        return 0;
278
0
    }
279
0
    return 1;
280
0
}
281
282
/* Add SigningCertificate signed attribute to the signer info. */
283
static int ossl_cms_add1_signing_cert(CMS_SignerInfo *si,
284
    const ESS_SIGNING_CERT *sc)
285
0
{
286
0
    ASN1_STRING *seq = NULL;
287
0
    unsigned char *p, *pp = NULL;
288
0
    int ret, len = i2d_ESS_SIGNING_CERT(sc, NULL);
289
290
0
    if (len <= 0 || (pp = OPENSSL_malloc(len)) == NULL)
291
0
        return 0;
292
293
0
    p = pp;
294
0
    i2d_ESS_SIGNING_CERT(sc, &p);
295
0
    if (!(seq = ASN1_STRING_new()) || !ASN1_STRING_set(seq, pp, len)) {
296
0
        ASN1_STRING_free(seq);
297
0
        OPENSSL_free(pp);
298
0
        return 0;
299
0
    }
300
0
    OPENSSL_free(pp);
301
0
    ret = CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_signingCertificate,
302
0
        V_ASN1_SEQUENCE, seq, -1);
303
0
    ASN1_STRING_free(seq);
304
0
    return ret;
305
0
}
306
307
/* Add SigningCertificateV2 signed attribute to the signer info. */
308
static int ossl_cms_add1_signing_cert_v2(CMS_SignerInfo *si,
309
    const ESS_SIGNING_CERT_V2 *sc)
310
0
{
311
0
    ASN1_STRING *seq = NULL;
312
0
    unsigned char *p, *pp = NULL;
313
0
    int ret, len = i2d_ESS_SIGNING_CERT_V2(sc, NULL);
314
315
0
    if (len <= 0 || (pp = OPENSSL_malloc(len)) == NULL)
316
0
        return 0;
317
318
0
    p = pp;
319
0
    i2d_ESS_SIGNING_CERT_V2(sc, &p);
320
0
    if (!(seq = ASN1_STRING_new()) || !ASN1_STRING_set(seq, pp, len)) {
321
0
        ASN1_STRING_free(seq);
322
0
        OPENSSL_free(pp);
323
0
        return 0;
324
0
    }
325
0
    OPENSSL_free(pp);
326
0
    ret = CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_signingCertificateV2,
327
0
        V_ASN1_SEQUENCE, seq, -1);
328
0
    ASN1_STRING_free(seq);
329
0
    return ret;
330
0
}
331
332
CMS_SignerInfo *CMS_add1_signer(CMS_ContentInfo *cms,
333
    X509 *signer, EVP_PKEY *pk, const EVP_MD *md,
334
    unsigned int flags)
335
0
{
336
0
    CMS_SignedData *sd;
337
0
    CMS_SignerInfo *si = NULL;
338
0
    X509_ALGOR *alg;
339
0
    int i, type;
340
0
    const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
341
342
0
    if (!X509_check_private_key(signer, pk)) {
343
0
        ERR_raise(ERR_LIB_CMS, CMS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
344
0
        return NULL;
345
0
    }
346
0
    sd = cms_signed_data_init(cms);
347
0
    if (!sd)
348
0
        goto err;
349
0
    si = M_ASN1_new_of(CMS_SignerInfo);
350
0
    if (!si) {
351
0
        ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
352
0
        goto err;
353
0
    }
354
    /* Call for side-effect of computing hash and caching extensions */
355
0
    X509_check_purpose(signer, -1, -1);
356
357
0
    X509_up_ref(signer);
358
0
    EVP_PKEY_up_ref(pk);
359
360
0
    si->cms_ctx = ctx;
361
0
    si->pkey = pk;
362
0
    si->signer = signer;
363
0
    si->mctx = EVP_MD_CTX_new();
364
0
    si->pctx = NULL;
365
366
0
    if (si->mctx == NULL) {
367
0
        ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
368
0
        goto err;
369
0
    }
370
371
0
    if (flags & CMS_USE_KEYID) {
372
0
        si->version = 3;
373
0
        if (sd->version < 3)
374
0
            sd->version = 3;
375
0
        type = CMS_SIGNERINFO_KEYIDENTIFIER;
376
0
    } else {
377
0
        type = CMS_SIGNERINFO_ISSUER_SERIAL;
378
0
        si->version = 1;
379
0
    }
380
381
0
    if (!ossl_cms_set1_SignerIdentifier(si->sid, signer, type, ctx))
382
0
        goto err;
383
384
0
    if (md == NULL) {
385
0
        int def_nid;
386
387
0
        if (EVP_PKEY_get_default_digest_nid(pk, &def_nid) <= 0) {
388
0
            ERR_raise_data(ERR_LIB_CMS, CMS_R_NO_DEFAULT_DIGEST,
389
0
                "pkey nid=%d", EVP_PKEY_get_id(pk));
390
0
            goto err;
391
0
        }
392
0
        md = EVP_get_digestbynid(def_nid);
393
0
        if (md == NULL) {
394
0
            ERR_raise_data(ERR_LIB_CMS, CMS_R_NO_DEFAULT_DIGEST,
395
0
                "default md nid=%d", def_nid);
396
0
            goto err;
397
0
        }
398
0
    }
399
400
0
    X509_ALGOR_set_md(si->digestAlgorithm, md);
401
402
    /* See if digest is present in digestAlgorithms */
403
0
    for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++) {
404
0
        const ASN1_OBJECT *aoid;
405
0
        char name[OSSL_MAX_NAME_SIZE];
406
407
0
        alg = sk_X509_ALGOR_value(sd->digestAlgorithms, i);
408
0
        X509_ALGOR_get0(&aoid, NULL, NULL, alg);
409
0
        OBJ_obj2txt(name, sizeof(name), aoid, 0);
410
0
        if (EVP_MD_is_a(md, name))
411
0
            break;
412
0
    }
413
414
0
    if (i == sk_X509_ALGOR_num(sd->digestAlgorithms)) {
415
0
        if ((alg = X509_ALGOR_new()) == NULL) {
416
0
            ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
417
0
            goto err;
418
0
        }
419
0
        X509_ALGOR_set_md(alg, md);
420
0
        if (!sk_X509_ALGOR_push(sd->digestAlgorithms, alg)) {
421
0
            X509_ALGOR_free(alg);
422
0
            ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB);
423
0
            goto err;
424
0
        }
425
0
    }
426
427
0
    if (!(flags & CMS_KEY_PARAM) && !cms_sd_asn1_ctrl(si, 0)) {
428
0
        ERR_raise_data(ERR_LIB_CMS, CMS_R_UNSUPPORTED_SIGNATURE_ALGORITHM,
429
0
            "pkey nid=%d", EVP_PKEY_get_id(pk));
430
0
        goto err;
431
0
    }
432
0
    if (!(flags & CMS_NOATTR)) {
433
        /*
434
         * Initialize signed attributes structure so other attributes
435
         * such as signing time etc are added later even if we add none here.
436
         */
437
0
        if (!si->signedAttrs) {
438
0
            si->signedAttrs = sk_X509_ATTRIBUTE_new_null();
439
0
            if (!si->signedAttrs) {
440
0
                ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB);
441
0
                goto err;
442
0
            }
443
0
        }
444
445
0
        if (!(flags & CMS_NOSMIMECAP)) {
446
0
            STACK_OF(X509_ALGOR) *smcap = NULL;
447
448
0
            i = CMS_add_standard_smimecap(&smcap);
449
0
            if (i)
450
0
                i = CMS_add_smimecap(si, smcap);
451
0
            sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
452
0
            if (!i) {
453
0
                ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
454
0
                goto err;
455
0
            }
456
0
        }
457
0
        if (flags & CMS_CADES) {
458
0
            ESS_SIGNING_CERT *sc = NULL;
459
0
            ESS_SIGNING_CERT_V2 *sc2 = NULL;
460
0
            int add_sc;
461
462
0
            if (md == NULL || EVP_MD_is_a(md, SN_sha1)) {
463
0
                if ((sc = OSSL_ESS_signing_cert_new_init(signer,
464
0
                         NULL, 1))
465
0
                    == NULL)
466
0
                    goto err;
467
0
                add_sc = ossl_cms_add1_signing_cert(si, sc);
468
0
                ESS_SIGNING_CERT_free(sc);
469
0
            } else {
470
0
                if ((sc2 = OSSL_ESS_signing_cert_v2_new_init(md, signer,
471
0
                         NULL, 1))
472
0
                    == NULL)
473
0
                    goto err;
474
0
                add_sc = ossl_cms_add1_signing_cert_v2(si, sc2);
475
0
                ESS_SIGNING_CERT_V2_free(sc2);
476
0
            }
477
0
            if (!add_sc)
478
0
                goto err;
479
0
        }
480
0
        if (flags & CMS_REUSE_DIGEST) {
481
0
            if (!cms_copy_messageDigest(cms, si))
482
0
                goto err;
483
0
            if (!cms_set_si_contentType_attr(cms, si))
484
0
                goto err;
485
0
            if (!(flags & (CMS_PARTIAL | CMS_KEY_PARAM)) && !CMS_SignerInfo_sign(si))
486
0
                goto err;
487
0
        }
488
0
    }
489
490
0
    if (!(flags & CMS_NOCERTS)) {
491
        /* NB ignore -1 return for duplicate cert */
492
0
        if (!CMS_add1_cert(cms, signer)) {
493
0
            ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
494
0
            goto err;
495
0
        }
496
0
    }
497
498
0
    if (flags & CMS_KEY_PARAM) {
499
0
        if (flags & CMS_NOATTR) {
500
0
            si->pctx = EVP_PKEY_CTX_new_from_pkey(ossl_cms_ctx_get0_libctx(ctx),
501
0
                si->pkey,
502
0
                ossl_cms_ctx_get0_propq(ctx));
503
0
            if (si->pctx == NULL)
504
0
                goto err;
505
0
            if (EVP_PKEY_sign_init(si->pctx) <= 0)
506
0
                goto err;
507
0
            if (EVP_PKEY_CTX_set_signature_md(si->pctx, md) <= 0)
508
0
                goto err;
509
0
        } else if (EVP_DigestSignInit_ex(si->mctx, &si->pctx,
510
0
                       EVP_MD_get0_name(md),
511
0
                       ossl_cms_ctx_get0_libctx(ctx),
512
0
                       ossl_cms_ctx_get0_propq(ctx),
513
0
                       pk, NULL)
514
0
            <= 0) {
515
0
            si->pctx = NULL;
516
0
            goto err;
517
0
        } else {
518
0
            EVP_MD_CTX_set_flags(si->mctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
519
0
        }
520
0
    }
521
522
0
    if (sd->signerInfos == NULL)
523
0
        sd->signerInfos = sk_CMS_SignerInfo_new_null();
524
0
    if (sd->signerInfos == NULL || !sk_CMS_SignerInfo_push(sd->signerInfos, si)) {
525
0
        ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB);
526
0
        goto err;
527
0
    }
528
529
0
    return si;
530
531
0
err:
532
0
    M_ASN1_free_of(si, CMS_SignerInfo);
533
0
    return NULL;
534
0
}
535
536
void ossl_cms_SignerInfos_set_cmsctx(CMS_ContentInfo *cms)
537
6.85k
{
538
6.85k
    int i;
539
6.85k
    CMS_SignerInfo *si;
540
6.85k
    STACK_OF(CMS_SignerInfo) *sinfos;
541
6.85k
    const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
542
543
6.85k
    ERR_set_mark();
544
6.85k
    sinfos = CMS_get0_SignerInfos(cms);
545
6.85k
    ERR_pop_to_mark(); /* removes error in case sinfos == NULL */
546
547
14.9k
    for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
548
8.08k
        si = sk_CMS_SignerInfo_value(sinfos, i);
549
8.08k
        if (si != NULL)
550
8.08k
            si->cms_ctx = ctx;
551
8.08k
    }
552
6.85k
}
553
554
static int cms_add1_signingTime(CMS_SignerInfo *si, ASN1_TIME *t)
555
0
{
556
0
    ASN1_TIME *tt;
557
0
    int r = 0;
558
559
0
    if (t != NULL)
560
0
        tt = t;
561
0
    else
562
0
        tt = X509_gmtime_adj(NULL, 0);
563
564
0
    if (tt == NULL) {
565
0
        ERR_raise(ERR_LIB_CMS, ERR_R_X509_LIB);
566
0
        goto err;
567
0
    }
568
569
0
    if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_signingTime,
570
0
            tt->type, tt, -1)
571
0
        <= 0) {
572
0
        ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
573
0
        goto err;
574
0
    }
575
576
0
    r = 1;
577
0
err:
578
0
    if (t == NULL)
579
0
        ASN1_TIME_free(tt);
580
581
0
    return r;
582
0
}
583
584
EVP_PKEY_CTX *CMS_SignerInfo_get0_pkey_ctx(CMS_SignerInfo *si)
585
0
{
586
0
    return si->pctx;
587
0
}
588
589
EVP_MD_CTX *CMS_SignerInfo_get0_md_ctx(CMS_SignerInfo *si)
590
0
{
591
0
    return si->mctx;
592
0
}
593
594
STACK_OF(CMS_SignerInfo) *CMS_get0_SignerInfos(CMS_ContentInfo *cms)
595
6.85k
{
596
6.85k
    CMS_SignedData *sd = cms_get0_signed(cms);
597
598
6.85k
    return sd != NULL ? sd->signerInfos : NULL;
599
6.85k
}
600
601
STACK_OF(X509) *CMS_get0_signers(CMS_ContentInfo *cms)
602
0
{
603
0
    STACK_OF(X509) *signers = NULL;
604
0
    STACK_OF(CMS_SignerInfo) *sinfos;
605
0
    CMS_SignerInfo *si;
606
0
    int i;
607
608
0
    sinfos = CMS_get0_SignerInfos(cms);
609
0
    for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
610
0
        si = sk_CMS_SignerInfo_value(sinfos, i);
611
0
        if (si->signer != NULL) {
612
0
            if (!ossl_x509_add_cert_new(&signers, si->signer,
613
0
                    X509_ADD_FLAG_DEFAULT)) {
614
0
                sk_X509_free(signers);
615
0
                return NULL;
616
0
            }
617
0
        }
618
0
    }
619
0
    return signers;
620
0
}
621
622
void CMS_SignerInfo_set1_signer_cert(CMS_SignerInfo *si, X509 *signer)
623
0
{
624
0
    if (signer != NULL) {
625
0
        X509_up_ref(signer);
626
0
        EVP_PKEY_free(si->pkey);
627
0
        si->pkey = X509_get_pubkey(signer);
628
0
    }
629
0
    X509_free(si->signer);
630
0
    si->signer = signer;
631
0
}
632
633
int CMS_SignerInfo_get0_signer_id(CMS_SignerInfo *si,
634
    ASN1_OCTET_STRING **keyid,
635
    X509_NAME **issuer, ASN1_INTEGER **sno)
636
0
{
637
0
    return ossl_cms_SignerIdentifier_get0_signer_id(si->sid, keyid, issuer, sno);
638
0
}
639
640
int CMS_SignerInfo_cert_cmp(CMS_SignerInfo *si, X509 *cert)
641
0
{
642
0
    return ossl_cms_SignerIdentifier_cert_cmp(si->sid, cert);
643
0
}
644
645
int CMS_set1_signers_certs(CMS_ContentInfo *cms, STACK_OF(X509) *scerts,
646
    unsigned int flags)
647
0
{
648
0
    CMS_SignedData *sd;
649
0
    CMS_SignerInfo *si;
650
0
    CMS_CertificateChoices *cch;
651
0
    STACK_OF(CMS_CertificateChoices) *certs;
652
0
    X509 *x;
653
0
    int i, j;
654
0
    int ret = 0;
655
656
0
    sd = cms_get0_signed(cms);
657
0
    if (sd == NULL)
658
0
        return -1;
659
0
    certs = sd->certificates;
660
0
    for (i = 0; i < sk_CMS_SignerInfo_num(sd->signerInfos); i++) {
661
0
        si = sk_CMS_SignerInfo_value(sd->signerInfos, i);
662
0
        if (si->signer != NULL)
663
0
            continue;
664
665
0
        for (j = 0; j < sk_X509_num(scerts); j++) {
666
0
            x = sk_X509_value(scerts, j);
667
0
            if (CMS_SignerInfo_cert_cmp(si, x) == 0) {
668
0
                CMS_SignerInfo_set1_signer_cert(si, x);
669
0
                ret++;
670
0
                break;
671
0
            }
672
0
        }
673
674
0
        if (si->signer != NULL || (flags & CMS_NOINTERN))
675
0
            continue;
676
677
0
        for (j = 0; j < sk_CMS_CertificateChoices_num(certs); j++) {
678
0
            cch = sk_CMS_CertificateChoices_value(certs, j);
679
0
            if (cch->type != CMS_CERTCHOICE_CERT)
680
0
                continue;
681
0
            x = cch->d.certificate;
682
0
            if (CMS_SignerInfo_cert_cmp(si, x) == 0) {
683
0
                CMS_SignerInfo_set1_signer_cert(si, x);
684
0
                ret++;
685
0
                break;
686
0
            }
687
0
        }
688
0
    }
689
0
    return ret;
690
0
}
691
692
void CMS_SignerInfo_get0_algs(CMS_SignerInfo *si, EVP_PKEY **pk,
693
    X509 **signer, X509_ALGOR **pdig,
694
    X509_ALGOR **psig)
695
0
{
696
0
    if (pk != NULL)
697
0
        *pk = si->pkey;
698
0
    if (signer != NULL)
699
0
        *signer = si->signer;
700
0
    if (pdig != NULL)
701
0
        *pdig = si->digestAlgorithm;
702
0
    if (psig != NULL)
703
0
        *psig = si->signatureAlgorithm;
704
0
}
705
706
ASN1_OCTET_STRING *CMS_SignerInfo_get0_signature(CMS_SignerInfo *si)
707
0
{
708
0
    return si->signature;
709
0
}
710
711
static int cms_SignerInfo_content_sign(CMS_ContentInfo *cms,
712
    CMS_SignerInfo *si, BIO *chain,
713
    const unsigned char *md,
714
    unsigned int mdlen)
715
0
{
716
0
    EVP_MD_CTX *mctx = EVP_MD_CTX_new();
717
0
    int r = 0;
718
0
    EVP_PKEY_CTX *pctx = NULL;
719
0
    const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
720
721
0
    if (mctx == NULL) {
722
0
        ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
723
0
        return 0;
724
0
    }
725
726
0
    if (si->pkey == NULL) {
727
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NO_PRIVATE_KEY);
728
0
        goto err;
729
0
    }
730
731
0
    if (!ossl_cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm))
732
0
        goto err;
733
    /* Set SignerInfo algorithm details if we used custom parameter */
734
0
    if (si->pctx && !cms_sd_asn1_ctrl(si, 0))
735
0
        goto err;
736
737
    /*
738
     * If any signed attributes calculate and add messageDigest attribute
739
     */
740
0
    if (CMS_signed_get_attr_count(si) >= 0) {
741
0
        unsigned char computed_md[EVP_MAX_MD_SIZE];
742
743
0
        if (md == NULL) {
744
0
            if (!EVP_DigestFinal_ex(mctx, computed_md, &mdlen))
745
0
                goto err;
746
0
            md = computed_md;
747
0
        }
748
0
        if (!CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest,
749
0
                V_ASN1_OCTET_STRING, md, mdlen))
750
0
            goto err;
751
        /* Copy content type across */
752
0
        if (!cms_set_si_contentType_attr(cms, si))
753
0
            goto err;
754
755
0
        if (!CMS_SignerInfo_sign(si))
756
0
            goto err;
757
0
    } else if (si->pctx) {
758
0
        unsigned char *sig;
759
0
        size_t siglen;
760
0
        unsigned char computed_md[EVP_MAX_MD_SIZE];
761
762
0
        pctx = si->pctx;
763
0
        si->pctx = NULL;
764
0
        if (md == NULL) {
765
0
            if (!EVP_DigestFinal_ex(mctx, computed_md, &mdlen))
766
0
                goto err;
767
0
            md = computed_md;
768
0
        }
769
0
        siglen = EVP_PKEY_get_size(si->pkey);
770
0
        if (siglen == 0 || (sig = OPENSSL_malloc(siglen)) == NULL)
771
0
            goto err;
772
0
        if (EVP_PKEY_sign(pctx, sig, &siglen, md, mdlen) <= 0) {
773
0
            OPENSSL_free(sig);
774
0
            goto err;
775
0
        }
776
0
        ASN1_STRING_set0(si->signature, sig, siglen);
777
0
    } else {
778
0
        unsigned char *sig;
779
0
        unsigned int siglen;
780
781
0
        if (md != NULL) {
782
0
            ERR_raise(ERR_LIB_CMS, CMS_R_OPERATION_UNSUPPORTED);
783
0
            goto err;
784
0
        }
785
0
        siglen = EVP_PKEY_get_size(si->pkey);
786
0
        if (siglen == 0 || (sig = OPENSSL_malloc(siglen)) == NULL)
787
0
            goto err;
788
0
        if (!EVP_SignFinal_ex(mctx, sig, &siglen, si->pkey,
789
0
                ossl_cms_ctx_get0_libctx(ctx),
790
0
                ossl_cms_ctx_get0_propq(ctx))) {
791
0
            ERR_raise(ERR_LIB_CMS, CMS_R_SIGNFINAL_ERROR);
792
0
            OPENSSL_free(sig);
793
0
            goto err;
794
0
        }
795
0
        ASN1_STRING_set0(si->signature, sig, siglen);
796
0
    }
797
798
0
    r = 1;
799
800
0
err:
801
0
    EVP_MD_CTX_free(mctx);
802
0
    EVP_PKEY_CTX_free(pctx);
803
0
    return r;
804
0
}
805
806
int ossl_cms_SignedData_final(CMS_ContentInfo *cms, BIO *chain,
807
    const unsigned char *precomp_md,
808
    unsigned int precomp_mdlen)
809
0
{
810
0
    STACK_OF(CMS_SignerInfo) *sinfos;
811
0
    CMS_SignerInfo *si;
812
0
    int i;
813
814
0
    sinfos = CMS_get0_SignerInfos(cms);
815
0
    for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
816
0
        si = sk_CMS_SignerInfo_value(sinfos, i);
817
0
        if (!cms_SignerInfo_content_sign(cms, si, chain,
818
0
                precomp_md, precomp_mdlen))
819
0
            return 0;
820
0
    }
821
0
    cms->d.signedData->encapContentInfo->partial = 0;
822
0
    return 1;
823
0
}
824
825
int CMS_SignerInfo_sign(CMS_SignerInfo *si)
826
0
{
827
0
    EVP_MD_CTX *mctx = si->mctx;
828
0
    EVP_PKEY_CTX *pctx = NULL;
829
0
    unsigned char *abuf = NULL;
830
0
    int alen;
831
0
    size_t siglen;
832
0
    const CMS_CTX *ctx = si->cms_ctx;
833
0
    char md_name[OSSL_MAX_NAME_SIZE];
834
835
0
    if (OBJ_obj2txt(md_name, sizeof(md_name),
836
0
            si->digestAlgorithm->algorithm, 0)
837
0
        <= 0)
838
0
        return 0;
839
840
0
    if (CMS_signed_get_attr_by_NID(si, NID_pkcs9_signingTime, -1) < 0) {
841
0
        if (!cms_add1_signingTime(si, NULL))
842
0
            goto err;
843
0
    }
844
845
0
    if (!ossl_cms_si_check_attributes(si))
846
0
        goto err;
847
848
0
    if (si->pctx) {
849
0
        pctx = si->pctx;
850
0
    } else {
851
0
        EVP_MD_CTX_reset(mctx);
852
0
        if (EVP_DigestSignInit_ex(mctx, &pctx, md_name,
853
0
                ossl_cms_ctx_get0_libctx(ctx),
854
0
                ossl_cms_ctx_get0_propq(ctx), si->pkey,
855
0
                NULL)
856
0
            <= 0)
857
0
            goto err;
858
0
        EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
859
0
        si->pctx = pctx;
860
0
    }
861
862
0
    alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs, &abuf,
863
0
        ASN1_ITEM_rptr(CMS_Attributes_Sign));
864
0
    if (!abuf)
865
0
        goto err;
866
0
    if (EVP_DigestSignUpdate(mctx, abuf, alen) <= 0)
867
0
        goto err;
868
0
    if (EVP_DigestSignFinal(mctx, NULL, &siglen) <= 0)
869
0
        goto err;
870
0
    OPENSSL_free(abuf);
871
0
    abuf = OPENSSL_malloc(siglen);
872
0
    if (abuf == NULL)
873
0
        goto err;
874
0
    if (EVP_DigestSignFinal(mctx, abuf, &siglen) <= 0)
875
0
        goto err;
876
877
0
    EVP_MD_CTX_reset(mctx);
878
879
0
    ASN1_STRING_set0(si->signature, abuf, siglen);
880
881
0
    return 1;
882
883
0
err:
884
0
    OPENSSL_free(abuf);
885
0
    EVP_MD_CTX_reset(mctx);
886
0
    return 0;
887
0
}
888
889
int CMS_SignerInfo_verify(CMS_SignerInfo *si)
890
0
{
891
0
    EVP_MD_CTX *mctx = NULL;
892
0
    unsigned char *abuf = NULL;
893
0
    int alen, r = -1;
894
0
    char name[OSSL_MAX_NAME_SIZE];
895
0
    const EVP_MD *md;
896
0
    EVP_MD *fetched_md = NULL;
897
0
    const CMS_CTX *ctx = si->cms_ctx;
898
0
    OSSL_LIB_CTX *libctx = ossl_cms_ctx_get0_libctx(ctx);
899
0
    const char *propq = ossl_cms_ctx_get0_propq(ctx);
900
901
0
    if (si->pkey == NULL) {
902
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NO_PUBLIC_KEY);
903
0
        return -1;
904
0
    }
905
906
0
    if (!ossl_cms_si_check_attributes(si))
907
0
        return -1;
908
909
0
    OBJ_obj2txt(name, sizeof(name), si->digestAlgorithm->algorithm, 0);
910
911
0
    (void)ERR_set_mark();
912
0
    fetched_md = EVP_MD_fetch(libctx, name, propq);
913
914
0
    if (fetched_md != NULL)
915
0
        md = fetched_md;
916
0
    else
917
0
        md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
918
0
    if (md == NULL) {
919
0
        (void)ERR_clear_last_mark();
920
0
        ERR_raise(ERR_LIB_CMS, CMS_R_UNKNOWN_DIGEST_ALGORITHM);
921
0
        return -1;
922
0
    }
923
0
    (void)ERR_pop_to_mark();
924
925
0
    if (si->mctx == NULL && (si->mctx = EVP_MD_CTX_new()) == NULL) {
926
0
        ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
927
0
        goto err;
928
0
    }
929
0
    mctx = si->mctx;
930
0
    if (si->pctx != NULL) {
931
0
        EVP_PKEY_CTX_free(si->pctx);
932
0
        si->pctx = NULL;
933
0
    }
934
0
    if (EVP_DigestVerifyInit_ex(mctx, &si->pctx, EVP_MD_get0_name(md), libctx,
935
0
            propq, si->pkey, NULL)
936
0
        <= 0) {
937
0
        si->pctx = NULL;
938
0
        goto err;
939
0
    }
940
0
    EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
941
942
0
    if (!cms_sd_asn1_ctrl(si, 1))
943
0
        goto err;
944
945
0
    alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs, &abuf,
946
0
        ASN1_ITEM_rptr(CMS_Attributes_Verify));
947
0
    if (abuf == NULL || alen < 0)
948
0
        goto err;
949
0
    r = EVP_DigestVerifyUpdate(mctx, abuf, alen);
950
0
    OPENSSL_free(abuf);
951
0
    if (r <= 0) {
952
0
        r = -1;
953
0
        goto err;
954
0
    }
955
0
    r = EVP_DigestVerifyFinal(mctx,
956
0
        si->signature->data, si->signature->length);
957
0
    if (r <= 0)
958
0
        ERR_raise(ERR_LIB_CMS, CMS_R_VERIFICATION_FAILURE);
959
0
err:
960
0
    EVP_MD_free(fetched_md);
961
0
    EVP_MD_CTX_reset(mctx);
962
0
    return r;
963
0
}
964
965
/* Create a chain of digest BIOs from a CMS ContentInfo */
966
BIO *ossl_cms_SignedData_init_bio(CMS_ContentInfo *cms)
967
0
{
968
0
    int i;
969
0
    CMS_SignedData *sd;
970
0
    BIO *chain = NULL;
971
972
0
    sd = cms_get0_signed(cms);
973
0
    if (sd == NULL)
974
0
        return NULL;
975
0
    if (cms->d.signedData->encapContentInfo->partial)
976
0
        cms_sd_set_version(sd);
977
0
    for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++) {
978
0
        X509_ALGOR *digestAlgorithm;
979
0
        BIO *mdbio;
980
981
0
        digestAlgorithm = sk_X509_ALGOR_value(sd->digestAlgorithms, i);
982
0
        mdbio = ossl_cms_DigestAlgorithm_init_bio(digestAlgorithm,
983
0
            ossl_cms_get0_cmsctx(cms));
984
0
        if (mdbio == NULL)
985
0
            goto err;
986
0
        if (chain != NULL)
987
0
            BIO_push(chain, mdbio);
988
0
        else
989
0
            chain = mdbio;
990
0
    }
991
0
    return chain;
992
0
err:
993
0
    BIO_free_all(chain);
994
0
    return NULL;
995
0
}
996
997
int CMS_SignerInfo_verify_content(CMS_SignerInfo *si, BIO *chain)
998
0
{
999
0
    ASN1_OCTET_STRING *os = NULL;
1000
0
    EVP_MD_CTX *mctx = EVP_MD_CTX_new();
1001
0
    EVP_PKEY_CTX *pkctx = NULL;
1002
0
    int r = -1;
1003
0
    unsigned char mval[EVP_MAX_MD_SIZE];
1004
0
    unsigned int mlen;
1005
1006
0
    if (mctx == NULL) {
1007
0
        ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
1008
0
        goto err;
1009
0
    }
1010
    /* If we have any signed attributes look for messageDigest value */
1011
0
    if (CMS_signed_get_attr_count(si) >= 0) {
1012
0
        os = CMS_signed_get0_data_by_OBJ(si,
1013
0
            OBJ_nid2obj(NID_pkcs9_messageDigest),
1014
0
            -3, V_ASN1_OCTET_STRING);
1015
0
        if (os == NULL) {
1016
0
            ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE);
1017
0
            goto err;
1018
0
        }
1019
0
    }
1020
1021
0
    if (!ossl_cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm))
1022
0
        goto err;
1023
1024
0
    if (EVP_DigestFinal_ex(mctx, mval, &mlen) <= 0) {
1025
0
        ERR_raise(ERR_LIB_CMS, CMS_R_UNABLE_TO_FINALIZE_CONTEXT);
1026
0
        goto err;
1027
0
    }
1028
1029
    /* If messageDigest found compare it */
1030
0
    if (os != NULL) {
1031
0
        if (mlen != (unsigned int)os->length) {
1032
0
            ERR_raise(ERR_LIB_CMS, CMS_R_MESSAGEDIGEST_ATTRIBUTE_WRONG_LENGTH);
1033
0
            goto err;
1034
0
        }
1035
1036
0
        if (memcmp(mval, os->data, mlen)) {
1037
0
            ERR_raise(ERR_LIB_CMS, CMS_R_VERIFICATION_FAILURE);
1038
0
            r = 0;
1039
0
        } else {
1040
0
            r = 1;
1041
0
        }
1042
0
    } else {
1043
0
        const EVP_MD *md = EVP_MD_CTX_get0_md(mctx);
1044
0
        const CMS_CTX *ctx = si->cms_ctx;
1045
1046
0
        pkctx = EVP_PKEY_CTX_new_from_pkey(ossl_cms_ctx_get0_libctx(ctx),
1047
0
            si->pkey,
1048
0
            ossl_cms_ctx_get0_propq(ctx));
1049
0
        if (pkctx == NULL)
1050
0
            goto err;
1051
0
        if (EVP_PKEY_verify_init(pkctx) <= 0)
1052
0
            goto err;
1053
0
        if (EVP_PKEY_CTX_set_signature_md(pkctx, md) <= 0)
1054
0
            goto err;
1055
0
        si->pctx = pkctx;
1056
0
        if (!cms_sd_asn1_ctrl(si, 1)) {
1057
0
            si->pctx = NULL;
1058
0
            goto err;
1059
0
        }
1060
0
        si->pctx = NULL;
1061
0
        r = EVP_PKEY_verify(pkctx, si->signature->data,
1062
0
            si->signature->length, mval, mlen);
1063
0
        if (r <= 0) {
1064
0
            ERR_raise(ERR_LIB_CMS, CMS_R_VERIFICATION_FAILURE);
1065
0
            r = 0;
1066
0
        }
1067
0
    }
1068
1069
0
err:
1070
0
    EVP_PKEY_CTX_free(pkctx);
1071
0
    EVP_MD_CTX_free(mctx);
1072
0
    return r;
1073
0
}
1074
1075
BIO *CMS_SignedData_verify(CMS_SignedData *sd, BIO *detached_data,
1076
    STACK_OF(X509) *scerts, X509_STORE *store,
1077
    STACK_OF(X509) *extra, STACK_OF(X509_CRL) *crls,
1078
    unsigned int flags,
1079
    OSSL_LIB_CTX *libctx, const char *propq)
1080
0
{
1081
0
    CMS_ContentInfo *ci;
1082
0
    BIO *bio = NULL;
1083
0
    int i, res = 0;
1084
1085
0
    if (sd == NULL) {
1086
0
        ERR_raise(ERR_LIB_CMS, ERR_R_PASSED_NULL_PARAMETER);
1087
0
        return NULL;
1088
0
    }
1089
1090
0
    if ((ci = CMS_ContentInfo_new_ex(libctx, propq)) == NULL)
1091
0
        return NULL;
1092
0
    if ((bio = BIO_new(BIO_s_mem())) == NULL)
1093
0
        goto end;
1094
0
    ci->contentType = OBJ_nid2obj(NID_pkcs7_signed);
1095
0
    ci->d.signedData = sd;
1096
1097
0
    for (i = 0; i < sk_X509_num(extra); i++)
1098
0
        if (!CMS_add1_cert(ci, sk_X509_value(extra, i)))
1099
0
            goto end;
1100
0
    for (i = 0; i < sk_X509_CRL_num(crls); i++)
1101
0
        if (!CMS_add1_crl(ci, sk_X509_CRL_value(crls, i)))
1102
0
            goto end;
1103
0
    res = CMS_verify(ci, scerts, store, detached_data, bio, flags);
1104
1105
0
end:
1106
0
    if (ci != NULL)
1107
0
        ci->d.signedData = NULL; /* do not indirectly free |sd| */
1108
0
    CMS_ContentInfo_free(ci);
1109
0
    if (!res) {
1110
0
        BIO_free(bio);
1111
0
        bio = NULL;
1112
0
    }
1113
0
    return bio;
1114
0
}
1115
1116
int CMS_add_smimecap(CMS_SignerInfo *si, STACK_OF(X509_ALGOR) *algs)
1117
0
{
1118
0
    unsigned char *smder = NULL;
1119
0
    int smderlen, r;
1120
1121
0
    smderlen = i2d_X509_ALGORS(algs, &smder);
1122
0
    if (smderlen <= 0)
1123
0
        return 0;
1124
0
    r = CMS_signed_add1_attr_by_NID(si, NID_SMIMECapabilities,
1125
0
        V_ASN1_SEQUENCE, smder, smderlen);
1126
0
    OPENSSL_free(smder);
1127
0
    return r;
1128
0
}
1129
1130
int CMS_add_simple_smimecap(STACK_OF(X509_ALGOR) **algs,
1131
    int algnid, int keysize)
1132
0
{
1133
0
    X509_ALGOR *alg;
1134
0
    ASN1_INTEGER *key = NULL;
1135
1136
0
    if (keysize > 0) {
1137
0
        key = ASN1_INTEGER_new();
1138
0
        if (key == NULL || !ASN1_INTEGER_set(key, keysize)) {
1139
0
            ASN1_INTEGER_free(key);
1140
0
            return 0;
1141
0
        }
1142
0
    }
1143
0
    alg = ossl_X509_ALGOR_from_nid(algnid, key != NULL ? V_ASN1_INTEGER : V_ASN1_UNDEF, key);
1144
0
    if (alg == NULL) {
1145
0
        ASN1_INTEGER_free(key);
1146
0
        return 0;
1147
0
    }
1148
1149
0
    if (*algs == NULL)
1150
0
        *algs = sk_X509_ALGOR_new_null();
1151
0
    if (*algs == NULL || !sk_X509_ALGOR_push(*algs, alg)) {
1152
0
        X509_ALGOR_free(alg);
1153
0
        return 0;
1154
0
    }
1155
0
    return 1;
1156
0
}
1157
1158
/* Check to see if a cipher exists and if so add S/MIME capabilities */
1159
static int cms_add_cipher_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg)
1160
0
{
1161
0
    if (EVP_get_cipherbynid(nid))
1162
0
        return CMS_add_simple_smimecap(sk, nid, arg);
1163
0
    return 1;
1164
0
}
1165
1166
static int cms_add_digest_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg)
1167
0
{
1168
0
    if (EVP_get_digestbynid(nid))
1169
0
        return CMS_add_simple_smimecap(sk, nid, arg);
1170
0
    return 1;
1171
0
}
1172
1173
int CMS_add_standard_smimecap(STACK_OF(X509_ALGOR) **smcap)
1174
0
{
1175
0
    if (!cms_add_cipher_smcap(smcap, NID_aes_256_cbc, -1)
1176
0
        || !cms_add_digest_smcap(smcap, NID_id_GostR3411_2012_256, -1)
1177
0
        || !cms_add_digest_smcap(smcap, NID_id_GostR3411_2012_512, -1)
1178
0
        || !cms_add_digest_smcap(smcap, NID_id_GostR3411_94, -1)
1179
0
        || !cms_add_cipher_smcap(smcap, NID_id_Gost28147_89, -1)
1180
0
        || !cms_add_cipher_smcap(smcap, NID_aes_192_cbc, -1)
1181
0
        || !cms_add_cipher_smcap(smcap, NID_aes_128_cbc, -1)
1182
0
        || !cms_add_cipher_smcap(smcap, NID_des_ede3_cbc, -1)
1183
0
        || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 128)
1184
0
        || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 64)
1185
0
        || !cms_add_cipher_smcap(smcap, NID_des_cbc, -1)
1186
0
        || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 40))
1187
0
        return 0;
1188
0
    return 1;
1189
0
}