Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/crypto/cms/cms_sd.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/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
19.4k
{
29
19.4k
    if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_signed) {
30
5.60k
        ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_TYPE_NOT_SIGNED_DATA);
31
5.60k
        return NULL;
32
5.60k
    }
33
13.8k
    return cms->d.signedData;
34
19.4k
}
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
        const 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
4.60k
{
218
4.60k
    if (sid->type == CMS_SIGNERINFO_ISSUER_SERIAL)
219
2.90k
        return ossl_cms_ias_cert_cmp(sid->d.issuerAndSerialNumber, cert);
220
1.69k
    else if (sid->type == CMS_SIGNERINFO_KEYIDENTIFIER)
221
1.69k
        return ossl_cms_keyid_cert_cmp(sid->d.subjectKeyIdentifier, cert);
222
0
    else
223
0
        return -1;
224
4.60k
}
225
226
static int cms_signature_nomd(EVP_PKEY *pkey)
227
1.12k
{
228
1.12k
    char def_md[80];
229
230
1.12k
    return EVP_PKEY_get_default_digest_name(pkey, def_md, sizeof(def_md)) == 2
231
44
            && strcmp(def_md, "UNDEF") == 0
232
1.12k
        ? 1
233
1.12k
        : 0;
234
1.12k
}
235
236
/* Method to map any, incl. provider-implemented PKEY types to OIDs */
237
/* (EC)DSA and all provider-delivered signatures implementation is the same */
238
static int cms_generic_sign(CMS_SignerInfo *si, int verify)
239
1.50k
{
240
1.50k
    if (!ossl_assert(verify == 0 || verify == 1))
241
0
        return -1;
242
243
1.50k
    if (!verify) {
244
0
        EVP_PKEY *pkey = si->pkey;
245
0
        int snid, hnid, pknid = EVP_PKEY_get_id(pkey);
246
0
        X509_ALGOR *alg1, *alg2;
247
248
0
        CMS_SignerInfo_get0_algs(si, NULL, NULL, &alg1, &alg2);
249
0
        if (alg1 == NULL || alg1->algorithm == NULL)
250
0
            return -1;
251
0
        hnid = OBJ_obj2nid(alg1->algorithm);
252
0
        if (hnid == NID_undef)
253
0
            return -1;
254
0
        if (pknid <= 0) { /* check whether a provider registered a NID */
255
0
            const char *typename = EVP_PKEY_get0_type_name(pkey);
256
257
0
            if (typename != NULL)
258
0
                pknid = OBJ_txt2nid(typename);
259
0
        }
260
0
        if (pknid > 0 && cms_signature_nomd(pkey))
261
0
            snid = pknid;
262
0
        else if (!OBJ_find_sigid_by_algs(&snid, hnid, pknid))
263
0
            return -1;
264
0
        return X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, NULL);
265
0
    }
266
1.50k
    return 1;
267
1.50k
}
268
269
static int cms_sd_asn1_ctrl(CMS_SignerInfo *si, int cmd)
270
2.28k
{
271
2.28k
    EVP_PKEY *pkey = si->pkey;
272
2.28k
    int i;
273
274
2.28k
    if (EVP_PKEY_is_a(pkey, "DSA") || EVP_PKEY_is_a(pkey, "EC"))
275
1.39k
        return cms_generic_sign(si, cmd) > 0;
276
897
    else if (EVP_PKEY_is_a(pkey, "RSA") || EVP_PKEY_is_a(pkey, "RSA-PSS"))
277
778
        return ossl_cms_rsa_sign(si, cmd) > 0;
278
279
    /* Now give engines, providers, etc a chance to handle this */
280
119
    if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL)
281
119
        return cms_generic_sign(si, cmd) > 0;
282
0
    i = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_CMS_SIGN, cmd, si);
283
0
    if (i == -2) {
284
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
285
0
        return 0;
286
0
    }
287
0
    if (i <= 0) {
288
0
        ERR_raise(ERR_LIB_CMS, CMS_R_CTRL_FAILURE);
289
0
        return 0;
290
0
    }
291
0
    return 1;
292
0
}
293
294
/* Add SigningCertificate signed attribute to the signer info. */
295
static int ossl_cms_add1_signing_cert(CMS_SignerInfo *si,
296
    const ESS_SIGNING_CERT *sc)
297
0
{
298
0
    ASN1_STRING *seq = NULL;
299
0
    unsigned char *p, *pp = NULL;
300
0
    int ret, len = i2d_ESS_SIGNING_CERT(sc, NULL);
301
302
0
    if (len <= 0 || (pp = OPENSSL_malloc(len)) == NULL)
303
0
        return 0;
304
305
0
    p = pp;
306
0
    i2d_ESS_SIGNING_CERT(sc, &p);
307
0
    if (!(seq = ASN1_STRING_new()) || !ossl_asn1_string_set1_data(seq, pp, len)) {
308
0
        ASN1_STRING_free(seq);
309
0
        OPENSSL_free(pp);
310
0
        return 0;
311
0
    }
312
0
    OPENSSL_free(pp);
313
0
    ret = CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_signingCertificate,
314
0
        V_ASN1_SEQUENCE, seq, -1);
315
0
    ASN1_STRING_free(seq);
316
0
    return ret;
317
0
}
318
319
/* Add SigningCertificateV2 signed attribute to the signer info. */
320
static int ossl_cms_add1_signing_cert_v2(CMS_SignerInfo *si,
321
    const ESS_SIGNING_CERT_V2 *sc)
322
0
{
323
0
    ASN1_STRING *seq = NULL;
324
0
    unsigned char *p, *pp = NULL;
325
0
    int ret, len = i2d_ESS_SIGNING_CERT_V2(sc, NULL);
326
327
0
    if (len <= 0 || (pp = OPENSSL_malloc(len)) == NULL)
328
0
        return 0;
329
330
0
    p = pp;
331
0
    i2d_ESS_SIGNING_CERT_V2(sc, &p);
332
0
    if (!(seq = ASN1_STRING_new()) || !ossl_asn1_string_set1_data(seq, pp, len)) {
333
0
        ASN1_STRING_free(seq);
334
0
        OPENSSL_free(pp);
335
0
        return 0;
336
0
    }
337
0
    OPENSSL_free(pp);
338
0
    ret = CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_signingCertificateV2,
339
0
        V_ASN1_SEQUENCE, seq, -1);
340
0
    ASN1_STRING_free(seq);
341
0
    return ret;
342
0
}
343
344
static const struct {
345
    const char *name;
346
    int md_nid;
347
    int md_a_must; /* md is a 'MUST' */
348
    int noattr_md_nid; /* in case of 'without signed attributes' */
349
    int noattr_md_a_must; /* noattr_md is a 'MUST' not a 'SHOULD' */
350
} key2data[] = {
351
    {
352
        "ML-DSA-44",
353
        NID_shake256,
354
        0,
355
        NID_sha512,
356
        0,
357
    },
358
    {
359
        "ML-DSA-65",
360
        NID_shake256,
361
        0,
362
        NID_sha512,
363
        0,
364
    },
365
    {
366
        "ML-DSA-87",
367
        NID_shake256,
368
        0,
369
        NID_sha512,
370
        0,
371
    },
372
    {
373
        "ED25519",
374
        NID_sha512,
375
        1,
376
        NID_sha512,
377
        1,
378
    }, /* RFC 8419 */
379
    /* RFC 8419 3.1 ED448: id-shake256-len MUST be used => NID_undef for now */
380
    {
381
        "ED448",
382
        NID_undef,
383
        1,
384
        NID_shake256,
385
        1,
386
    },
387
    {
388
        "SLH-DSA-SHA2-128f",
389
        NID_sha256,
390
        0,
391
        NID_sha256,
392
        0,
393
    }, /* RFC 9814 */
394
    {
395
        "SLH-DSA-SHA2-128s",
396
        NID_sha256,
397
        0,
398
        NID_sha256,
399
        0,
400
    },
401
    {
402
        "SLH-DSA-SHA2-192f",
403
        NID_sha512,
404
        0,
405
        NID_sha512,
406
        0,
407
    },
408
    {
409
        "SLH-DSA-SHA2-192s",
410
        NID_sha512,
411
        0,
412
        NID_sha512,
413
        0,
414
    },
415
    {
416
        "SLH-DSA-SHA2-256f",
417
        NID_sha512,
418
        0,
419
        NID_sha512,
420
        0,
421
    },
422
    {
423
        "SLH-DSA-SHA2-256s",
424
        NID_sha512,
425
        0,
426
        NID_sha512,
427
        0,
428
    },
429
    {
430
        "SLH-DSA-SHAKE-128f",
431
        NID_shake128,
432
        0,
433
        NID_shake128,
434
        0,
435
    },
436
    {
437
        "SLH-DSA-SHAKE-128s",
438
        NID_shake128,
439
        0,
440
        NID_shake128,
441
        0,
442
    },
443
    {
444
        "SLH-DSA-SHAKE-192f",
445
        NID_shake256,
446
        0,
447
        NID_shake256,
448
        0,
449
    },
450
    {
451
        "SLH-DSA-SHAKE-192s",
452
        NID_shake256,
453
        0,
454
        NID_shake256,
455
        0,
456
    },
457
    {
458
        "SLH-DSA-SHAKE-256f",
459
        NID_shake256,
460
        0,
461
        NID_shake256,
462
        0,
463
    },
464
    {
465
        "SLH-DSA-SHAKE-256s",
466
        NID_shake256,
467
        0,
468
        NID_shake256,
469
        0,
470
    },
471
    {
472
        NULL,
473
        NID_undef,
474
        0,
475
        NID_undef,
476
        0,
477
    } /* last */
478
};
479
480
static const char *cms_mdless_signing(EVP_PKEY *pkey)
481
1.85k
{
482
1.85k
    unsigned int i;
483
1.85k
    int def_nid = NID_undef;
484
485
25.1k
    for (i = 0; key2data[i].name != NULL; i++) {
486
23.9k
        if (EVP_PKEY_is_a(pkey, key2data[i].name))
487
616
            return key2data[i].name;
488
23.9k
    }
489
1.24k
    if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) <= 0) {
490
        /* Key doesn't have default digest, it's mdless */
491
3
        return EVP_PKEY_get0_type_name(pkey);
492
3
    }
493
1.23k
    return NULL;
494
1.24k
}
495
496
static EVP_MD *ossl_cms_get_default_md(const CMS_CTX *ctx, EVP_PKEY *pk, int *md_a_must)
497
0
{
498
0
    EVP_MD *md = NULL;
499
0
    unsigned int i;
500
0
    int def_nid = NID_undef;
501
502
0
    *md_a_must = 0;
503
504
0
    for (i = 0; key2data[i].name != NULL; i++) {
505
0
        if (EVP_PKEY_is_a(pk, key2data[i].name)) {
506
0
            def_nid = key2data[i].md_nid;
507
0
            *md_a_must = key2data[i].md_a_must;
508
0
            break;
509
0
        }
510
0
    }
511
512
0
    if (def_nid == NID_undef && EVP_PKEY_get_default_digest_nid(pk, &def_nid) <= 0) {
513
0
        ERR_raise_data(ERR_LIB_CMS, CMS_R_NO_DEFAULT_DIGEST,
514
0
            "pkey nid=%d", EVP_PKEY_get_id(pk));
515
0
        return NULL;
516
0
    }
517
0
    md = EVP_MD_fetch(ossl_cms_ctx_get0_libctx(ctx), OBJ_nid2sn(def_nid), ossl_cms_ctx_get0_propq(ctx));
518
0
    if (md == NULL)
519
0
        ERR_raise_data(ERR_LIB_CMS, CMS_R_NO_DEFAULT_DIGEST,
520
0
            "default md nid=%d", def_nid);
521
0
    return md;
522
0
}
523
524
static EVP_MD *ossl_cms_get_noattr_md(const CMS_CTX *ctx, EVP_PKEY *pk, int *noattr_md_a_must)
525
0
{
526
0
    unsigned int i;
527
528
0
    for (i = 0; key2data[i].name != NULL; i++) {
529
0
        if (EVP_PKEY_is_a(pk, key2data[i].name)) {
530
0
            *noattr_md_a_must = key2data[i].noattr_md_a_must;
531
0
            return EVP_MD_fetch(ossl_cms_ctx_get0_libctx(ctx), OBJ_nid2sn(key2data[i].noattr_md_nid), ossl_cms_ctx_get0_propq(ctx));
532
0
        }
533
0
    }
534
0
    return NULL;
535
0
}
536
537
static int ossl_cms_adjust_md(const CMS_CTX *ctx, EVP_PKEY *pk, const EVP_MD **md, EVP_MD **fetched_md, unsigned int flags)
538
0
{
539
0
    int md_a_must = 0;
540
541
0
    while ((flags & CMS_NOATTR) != 0) {
542
        /*
543
         * Some key types require that a certain type of hash is being used in the
544
         * case of signed attributes being absent, others have a suggested hash.
545
         * In the latter case only use the suggested one if no hash is given.
546
         */
547
0
        int noattr_md_a_must = 0;
548
549
0
        *fetched_md = ossl_cms_get_noattr_md(ctx, pk, &noattr_md_a_must);
550
0
        if (*fetched_md == NULL)
551
0
            break; /* key type not listed - use the default */
552
553
0
        if (noattr_md_a_must)
554
0
            *md = *fetched_md;
555
0
        else if (*md == NULL)
556
0
            *md = *fetched_md;
557
0
        return 1;
558
0
    }
559
560
0
    if (*md != NULL)
561
0
        (void)ERR_set_mark(); /* No error if no default md and user-supplied md is set */
562
0
    *fetched_md = ossl_cms_get_default_md(ctx, pk, &md_a_must);
563
0
    if (*md != NULL)
564
0
        (void)ERR_pop_to_mark();
565
0
    if (md_a_must)
566
0
        *md = *fetched_md;
567
0
    else if (*md == NULL)
568
0
        *md = *fetched_md;
569
570
0
    if (*md == NULL) /* ED448 case */
571
0
        return 0;
572
573
0
    return 1;
574
0
}
575
576
CMS_SignerInfo *CMS_add1_signer(CMS_ContentInfo *cms,
577
    X509 *signer, EVP_PKEY *pk, const EVP_MD *md,
578
    unsigned int flags)
579
0
{
580
0
    EVP_MD *local_md = NULL;
581
0
    CMS_SignedData *sd;
582
0
    CMS_SignerInfo *si = NULL;
583
0
    X509_ALGOR *alg;
584
0
    int i, type;
585
0
    const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
586
587
0
    if (!X509_check_private_key(signer, pk)) {
588
0
        ERR_raise(ERR_LIB_CMS, CMS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
589
0
        return NULL;
590
0
    }
591
0
    sd = cms_signed_data_init(cms);
592
0
    if (sd == NULL)
593
0
        goto err;
594
0
    si = M_ASN1_new_of(CMS_SignerInfo);
595
0
    if (si == NULL) {
596
0
        ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
597
0
        goto err;
598
0
    }
599
    /* Call for side-effect of computing hash and caching extensions */
600
0
    X509_check_purpose(signer, -1, -1);
601
602
0
    if (!X509_up_ref(signer))
603
0
        goto err;
604
0
    if (!EVP_PKEY_up_ref(pk)) {
605
0
        X509_free(signer);
606
0
        goto err;
607
0
    }
608
609
0
    si->cms_ctx = ctx;
610
0
    si->pkey = pk;
611
0
    si->signer = signer;
612
0
    si->mctx = EVP_MD_CTX_new();
613
0
    si->pctx = NULL;
614
0
    si->omit_signing_time = 0;
615
616
0
    if (si->mctx == NULL) {
617
0
        ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
618
0
        goto err;
619
0
    }
620
621
0
    if (flags & CMS_USE_KEYID) {
622
0
        si->version = 3;
623
0
        if (sd->version < 3)
624
0
            sd->version = 3;
625
0
        type = CMS_SIGNERINFO_KEYIDENTIFIER;
626
0
    } else {
627
0
        type = CMS_SIGNERINFO_ISSUER_SERIAL;
628
0
        si->version = 1;
629
0
    }
630
631
0
    if (!ossl_cms_set1_SignerIdentifier(si->sid, signer, type, ctx))
632
0
        goto err;
633
634
0
    if (ossl_cms_adjust_md(ctx, pk, &md, &local_md, flags) != 1)
635
0
        goto err;
636
637
0
    if (!X509_ALGOR_set_md(si->digestAlgorithm, md))
638
0
        goto err;
639
640
    /* See if digest is present in digestAlgorithms */
641
0
    for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++) {
642
0
        const ASN1_OBJECT *aoid;
643
0
        char name[OSSL_MAX_NAME_SIZE];
644
645
0
        alg = sk_X509_ALGOR_value(sd->digestAlgorithms, i);
646
0
        X509_ALGOR_get0(&aoid, NULL, NULL, alg);
647
0
        OBJ_obj2txt(name, sizeof(name), aoid, 0);
648
0
        if (EVP_MD_is_a(md, name))
649
0
            break;
650
0
    }
651
0
    if (i == sk_X509_ALGOR_num(sd->digestAlgorithms)) {
652
0
        if ((alg = X509_ALGOR_new()) == NULL
653
0
            || !X509_ALGOR_set_md(alg, md)
654
0
            || !sk_X509_ALGOR_push(sd->digestAlgorithms, alg)) {
655
0
            X509_ALGOR_free(alg);
656
0
            ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB);
657
0
            goto err;
658
0
        }
659
0
    }
660
0
    if (!(flags & CMS_KEY_PARAM) && !cms_sd_asn1_ctrl(si, 0)) {
661
0
        ERR_raise_data(ERR_LIB_CMS, CMS_R_UNSUPPORTED_SIGNATURE_ALGORITHM,
662
0
            "pkey nid=%d", EVP_PKEY_get_id(pk));
663
0
        goto err;
664
0
    }
665
0
    if (!(flags & CMS_NOATTR)) {
666
        /*
667
         * Initialize signed attributes structure so other attributes
668
         * such as signing time etc are added later even if we add none here.
669
         */
670
0
        if (!si->signedAttrs) {
671
0
            si->signedAttrs = sk_X509_ATTRIBUTE_new_null();
672
0
            if (!si->signedAttrs) {
673
0
                ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB);
674
0
                goto err;
675
0
            }
676
0
        }
677
0
        if (!(flags & CMS_NOSMIMECAP)) {
678
0
            STACK_OF(X509_ALGOR) *smcap = NULL;
679
680
0
            i = CMS_add_standard_smimecap_ex(&smcap, ossl_cms_ctx_get0_libctx(ctx),
681
0
                ossl_cms_ctx_get0_propq(ctx));
682
0
            if (i)
683
0
                i = CMS_add_smimecap(si, smcap);
684
0
            sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
685
0
            if (!i) {
686
0
                ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
687
0
                goto err;
688
0
            }
689
0
        }
690
0
        if ((flags & CMS_NO_SIGNING_TIME) != 0) {
691
            /*
692
             * The signing-time signed attribute (NID_pkcs9_signingTime)
693
             * would normally be added later, in CMS_SignerInfo_sign(),
694
             * unless we set this flag here
695
             */
696
0
            si->omit_signing_time = 1;
697
0
        }
698
0
        if (flags & CMS_CADES) {
699
0
            ESS_SIGNING_CERT *sc = NULL;
700
0
            ESS_SIGNING_CERT_V2 *sc2 = NULL;
701
0
            int add_sc;
702
703
0
            if (EVP_MD_is_a(md, SN_sha1)) {
704
0
                if ((sc = OSSL_ESS_signing_cert_new_init(signer,
705
0
                         NULL, 1))
706
0
                    == NULL)
707
0
                    goto err;
708
0
                add_sc = ossl_cms_add1_signing_cert(si, sc);
709
0
                ESS_SIGNING_CERT_free(sc);
710
0
            } else {
711
0
                if ((sc2 = OSSL_ESS_signing_cert_v2_new_init(md, signer,
712
0
                         NULL, 1))
713
0
                    == NULL)
714
0
                    goto err;
715
0
                add_sc = ossl_cms_add1_signing_cert_v2(si, sc2);
716
0
                ESS_SIGNING_CERT_V2_free(sc2);
717
0
            }
718
0
            if (!add_sc)
719
0
                goto err;
720
0
        }
721
0
        if (flags & CMS_REUSE_DIGEST) {
722
0
            if (!cms_copy_messageDigest(cms, si))
723
0
                goto err;
724
0
            if (!cms_set_si_contentType_attr(cms, si))
725
0
                goto err;
726
0
            if (!(flags & (CMS_PARTIAL | CMS_KEY_PARAM)) && !CMS_SignerInfo_sign(si))
727
0
                goto err;
728
0
        }
729
0
    }
730
731
0
    if (!(flags & CMS_NOCERTS)) {
732
0
        if (!CMS_add1_cert(cms, signer)) {
733
0
            ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
734
0
            goto err;
735
0
        }
736
0
    }
737
0
    if (flags & CMS_KEY_PARAM) {
738
0
        if (flags & CMS_NOATTR) {
739
0
            si->pctx = EVP_PKEY_CTX_new_from_pkey(ossl_cms_ctx_get0_libctx(ctx),
740
0
                si->pkey,
741
0
                ossl_cms_ctx_get0_propq(ctx));
742
0
            if (si->pctx == NULL)
743
0
                goto err;
744
0
            if (EVP_PKEY_sign_init(si->pctx) <= 0)
745
0
                goto err;
746
0
            if (EVP_PKEY_CTX_set_signature_md(si->pctx, md) <= 0)
747
0
                goto err;
748
0
        } else if (EVP_DigestSignInit_ex(si->mctx, &si->pctx,
749
0
                       EVP_MD_get0_name(md),
750
0
                       ossl_cms_ctx_get0_libctx(ctx),
751
0
                       ossl_cms_ctx_get0_propq(ctx),
752
0
                       pk, NULL)
753
0
            <= 0) {
754
0
            si->pctx = NULL;
755
0
            goto err;
756
0
        } else {
757
0
            EVP_MD_CTX_set_flags(si->mctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
758
0
        }
759
0
    }
760
0
    if (sd->signerInfos == NULL)
761
0
        sd->signerInfos = sk_CMS_SignerInfo_new_null();
762
0
    if (sd->signerInfos == NULL || !sk_CMS_SignerInfo_push(sd->signerInfos, si)) {
763
0
        ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB);
764
0
        goto err;
765
0
    }
766
0
    EVP_MD_free(local_md);
767
0
    return si;
768
769
0
err:
770
0
    EVP_MD_free(local_md);
771
0
    M_ASN1_free_of(si, CMS_SignerInfo);
772
0
    return NULL;
773
0
}
774
775
void ossl_cms_SignerInfos_set_cmsctx(CMS_ContentInfo *cms)
776
10.4k
{
777
10.4k
    int i;
778
10.4k
    CMS_SignerInfo *si;
779
10.4k
    STACK_OF(CMS_SignerInfo) *sinfos;
780
10.4k
    const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
781
782
10.4k
    ERR_set_mark();
783
10.4k
    sinfos = CMS_get0_SignerInfos(cms);
784
10.4k
    ERR_pop_to_mark(); /* removes error in case sinfos == NULL */
785
786
22.6k
    for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
787
12.2k
        si = sk_CMS_SignerInfo_value(sinfos, i);
788
12.2k
        if (si != NULL)
789
12.2k
            si->cms_ctx = ctx;
790
12.2k
    }
791
10.4k
}
792
793
static int cms_add1_signingTime(CMS_SignerInfo *si, ASN1_TIME *t)
794
0
{
795
0
    ASN1_TIME *tt;
796
0
    int r = 0;
797
798
0
    if (t != NULL)
799
0
        tt = t;
800
0
    else
801
0
        tt = X509_gmtime_adj(NULL, 0);
802
803
0
    if (tt == NULL) {
804
0
        ERR_raise(ERR_LIB_CMS, ERR_R_X509_LIB);
805
0
        goto err;
806
0
    }
807
808
0
    if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_signingTime,
809
0
            tt->type, tt, -1)
810
0
        <= 0) {
811
0
        ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
812
0
        goto err;
813
0
    }
814
815
0
    r = 1;
816
0
err:
817
0
    if (t == NULL)
818
0
        ASN1_TIME_free(tt);
819
820
0
    return r;
821
0
}
822
823
EVP_PKEY_CTX *CMS_SignerInfo_get0_pkey_ctx(CMS_SignerInfo *si)
824
778
{
825
778
    return si->pctx;
826
778
}
827
828
EVP_MD_CTX *CMS_SignerInfo_get0_md_ctx(CMS_SignerInfo *si)
829
0
{
830
0
    return si->mctx;
831
0
}
832
833
STACK_OF(CMS_SignerInfo) *CMS_get0_SignerInfos(CMS_ContentInfo *cms)
834
14.0k
{
835
14.0k
    CMS_SignedData *sd = cms_get0_signed(cms);
836
837
14.0k
    return sd != NULL ? sd->signerInfos : NULL;
838
14.0k
}
839
840
STACK_OF(X509) *CMS_get0_signers(CMS_ContentInfo *cms)
841
0
{
842
0
    STACK_OF(X509) *signers = NULL;
843
0
    STACK_OF(CMS_SignerInfo) *sinfos;
844
0
    CMS_SignerInfo *si;
845
0
    int i;
846
847
0
    sinfos = CMS_get0_SignerInfos(cms);
848
0
    for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
849
0
        si = sk_CMS_SignerInfo_value(sinfos, i);
850
0
        if (si->signer != NULL && si->verify_result) {
851
0
            if (!ossl_x509_add_cert_new(&signers, si->signer,
852
0
                    X509_ADD_FLAG_DEFAULT)) {
853
0
                sk_X509_free(signers);
854
0
                return NULL;
855
0
            }
856
0
        }
857
0
    }
858
0
    return signers;
859
0
}
860
861
void CMS_SignerInfo_set1_signer_cert(CMS_SignerInfo *si, X509 *signer)
862
3.25k
{
863
3.25k
    if (signer != NULL) {
864
3.25k
        if (!X509_up_ref(signer))
865
0
            return;
866
3.25k
        EVP_PKEY_free(si->pkey);
867
3.25k
        si->pkey = X509_get_pubkey(signer);
868
3.25k
    }
869
3.25k
    X509_free(si->signer);
870
3.25k
    si->signer = signer;
871
3.25k
}
872
873
X509 *CMS_SignerInfo_get0_signer_cert(const CMS_SignerInfo *si)
874
0
{
875
0
    return si->signer;
876
0
}
877
878
int CMS_SignerInfo_get0_signer_id(CMS_SignerInfo *si,
879
    ASN1_OCTET_STRING **keyid,
880
    X509_NAME **issuer, ASN1_INTEGER **sno)
881
0
{
882
0
    return ossl_cms_SignerIdentifier_get0_signer_id(si->sid, keyid, issuer, sno);
883
0
}
884
885
int CMS_SignerInfo_cert_cmp(CMS_SignerInfo *si, X509 *cert)
886
4.60k
{
887
4.60k
    return ossl_cms_SignerIdentifier_cert_cmp(si->sid, cert);
888
4.60k
}
889
890
int CMS_set1_signers_certs(CMS_ContentInfo *cms, const STACK_OF(X509) *scerts,
891
    unsigned int flags)
892
3.36k
{
893
3.36k
    CMS_SignedData *sd;
894
3.36k
    CMS_SignerInfo *si;
895
3.36k
    CMS_CertificateChoices *cch;
896
3.36k
    STACK_OF(CMS_CertificateChoices) *certs;
897
3.36k
    X509 *x;
898
3.36k
    int i, j;
899
3.36k
    int ret = 0;
900
901
3.36k
    sd = cms_get0_signed(cms);
902
3.36k
    if (sd == NULL)
903
0
        return -1;
904
3.36k
    certs = sd->certificates;
905
7.75k
    for (i = 0; i < sk_CMS_SignerInfo_num(sd->signerInfos); i++) {
906
4.38k
        si = sk_CMS_SignerInfo_value(sd->signerInfos, i);
907
4.38k
        if (si->signer != NULL)
908
0
            continue;
909
910
        /* If any certificates passed they take priority */
911
4.38k
        for (j = 0; j < sk_X509_num(scerts); j++) {
912
0
            x = sk_X509_value(scerts, j);
913
0
            if (CMS_SignerInfo_cert_cmp(si, x) == 0) {
914
0
                CMS_SignerInfo_set1_signer_cert(si, x);
915
0
                ret++;
916
0
                break;
917
0
            }
918
0
        }
919
920
4.38k
        if (si->signer != NULL || (flags & CMS_NOINTERN))
921
0
            continue;
922
923
5.86k
        for (j = 0; j < sk_CMS_CertificateChoices_num(certs); j++) {
924
4.73k
            cch = sk_CMS_CertificateChoices_value(certs, j);
925
4.73k
            if (cch->type != CMS_CERTCHOICE_CERT)
926
133
                continue;
927
4.60k
            x = cch->d.certificate;
928
4.60k
            if (CMS_SignerInfo_cert_cmp(si, x) == 0) {
929
3.25k
                CMS_SignerInfo_set1_signer_cert(si, x);
930
3.25k
                ret++;
931
3.25k
                break;
932
3.25k
            }
933
4.60k
        }
934
4.38k
    }
935
3.36k
    return ret;
936
3.36k
}
937
938
void CMS_SignerInfo_get0_algs(CMS_SignerInfo *si, EVP_PKEY **pk,
939
    X509 **signer, X509_ALGOR **pdig,
940
    X509_ALGOR **psig)
941
5.16k
{
942
5.16k
    if (pk != NULL)
943
0
        *pk = si->pkey;
944
5.16k
    if (signer != NULL)
945
4.38k
        *signer = si->signer;
946
5.16k
    if (pdig != NULL)
947
0
        *pdig = si->digestAlgorithm;
948
5.16k
    if (psig != NULL)
949
778
        *psig = si->signatureAlgorithm;
950
5.16k
}
951
952
ASN1_OCTET_STRING *CMS_SignerInfo_get0_signature(CMS_SignerInfo *si)
953
0
{
954
0
    return si->signature;
955
0
}
956
957
static int cms_bio_read(BIO *in,
958
    unsigned char **buffer, size_t *buffer_len)
959
492
{
960
492
    unsigned char *tmp;
961
492
    size_t offset = 0;
962
492
    int n;
963
964
492
    if (BIO_seek(in, 0) < 0)
965
0
        return -1;
966
967
492
    *buffer = NULL;
968
492
    *buffer_len = 0;
969
970
    /* read data from BIO into memory */
971
2.52k
    do {
972
2.52k
        *buffer_len += 10240;
973
2.52k
        tmp = OPENSSL_realloc(*buffer, *buffer_len);
974
2.52k
        if (!tmp)
975
0
            goto err;
976
2.52k
        *buffer = tmp;
977
978
2.52k
        if ((n = BIO_read(in, &(*buffer)[offset], 10240)) <= 0)
979
0
            goto err;
980
2.52k
        offset += n;
981
2.52k
    } while (BIO_eof(in) != 1);
982
983
492
    *buffer_len = offset;
984
985
492
    return 1;
986
987
0
err:
988
0
    OPENSSL_free(*buffer);
989
0
    *buffer = NULL;
990
0
    return -1;
991
492
}
992
993
static int cms_EVP_PKEY_sign(EVP_PKEY_CTX *pctx, BIO *in,
994
    unsigned char *sig, size_t *sig_len,
995
    int has_msg_update)
996
0
{
997
0
    size_t buffer_len;
998
0
    int ret;
999
1000
0
    if (!has_msg_update) {
1001
0
        unsigned char *buffer = NULL;
1002
1003
0
        if (cms_bio_read(in, &buffer, &buffer_len) != 1)
1004
0
            return 0;
1005
0
        ret = EVP_PKEY_sign(pctx, sig, sig_len, buffer, buffer_len);
1006
0
        OPENSSL_free(buffer);
1007
0
    } else {
1008
0
        unsigned char buffer[1024];
1009
0
        int n;
1010
1011
0
        if (BIO_seek(in, 0) < 0)
1012
0
            return 0;
1013
1014
0
        do {
1015
0
            n = BIO_read(in, buffer, sizeof(buffer));
1016
0
            if (n <= 0)
1017
0
                break;
1018
0
            if (EVP_PKEY_sign_message_update(pctx, buffer, n) != 1)
1019
0
                return 0;
1020
0
        } while (!BIO_eof(in));
1021
0
        ret = EVP_PKEY_sign_message_final(pctx, sig, sig_len);
1022
0
    }
1023
0
    return ret;
1024
0
}
1025
1026
static int cms_EVP_PKEY_verify(EVP_PKEY_CTX *pctx, BIO *in, unsigned char *sig,
1027
    size_t siglen, int has_msg_update)
1028
614
{
1029
614
    size_t buffer_len;
1030
614
    int ret;
1031
1032
614
    if (!has_msg_update) {
1033
492
        unsigned char *buffer = NULL;
1034
1035
492
        if (cms_bio_read(in, &buffer, &buffer_len) != 1)
1036
0
            return 0;
1037
1038
492
        ret = EVP_PKEY_verify(pctx, sig, siglen, buffer, buffer_len);
1039
492
        OPENSSL_free(buffer);
1040
492
    } else {
1041
122
        unsigned char buffer[1024];
1042
122
        int n;
1043
1044
122
        if (BIO_seek(in, 0) < 0)
1045
0
            return 0;
1046
1047
3.91k
        do {
1048
3.91k
            n = BIO_read(in, buffer, sizeof(buffer));
1049
3.91k
            if (n <= 0)
1050
0
                break;
1051
3.91k
            if (EVP_PKEY_verify_message_update(pctx, buffer, n) != 1)
1052
0
                return 0;
1053
3.91k
        } while (!BIO_eof(in));
1054
122
        if (EVP_PKEY_CTX_set_signature(pctx, sig, siglen) != 1)
1055
0
            return 0;
1056
122
        ret = EVP_PKEY_verify_message_final(pctx);
1057
122
    }
1058
614
    return ret;
1059
614
}
1060
1061
int CMS_SignerInfo_get_verification_result(const CMS_SignerInfo *si, int type)
1062
0
{
1063
0
    switch (type) {
1064
0
    case CMS_VERIFY_RESULT:
1065
0
        return si->verify_result;
1066
1067
0
    case CMS_VERIFY_CERT:
1068
0
        return si->cert_verified;
1069
1070
0
    case CMS_VERIFY_ATTR:
1071
0
        return si->attr_verified;
1072
1073
0
    case CMS_VERIFY_CONTENT:
1074
0
        return si->content_verified;
1075
1076
0
    default:
1077
0
        ERR_raise(ERR_LIB_CMS, CMS_R_UNKNOWN_ID);
1078
0
        return 0;
1079
0
    }
1080
0
}
1081
1082
static int cms_SignerInfo_content_sign(CMS_ContentInfo *cms,
1083
    CMS_SignerInfo *si, BIO *chain,
1084
    BIO *data,
1085
    const unsigned char *md,
1086
    unsigned int mdlen)
1087
0
{
1088
0
    EVP_MD_CTX *mctx = EVP_MD_CTX_new();
1089
0
    int r = 0;
1090
0
    EVP_PKEY_CTX *pctx = NULL;
1091
0
    const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
1092
0
    EVP_SIGNATURE *sig_alg = NULL;
1093
0
    unsigned char *sig = NULL;
1094
1095
0
    if (mctx == NULL) {
1096
0
        ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
1097
0
        return 0;
1098
0
    }
1099
1100
0
    if (si->pkey == NULL) {
1101
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NO_PRIVATE_KEY);
1102
0
        goto err;
1103
0
    }
1104
1105
0
    if (!ossl_cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm))
1106
0
        goto err;
1107
    /* Set SignerInfo algorithm details if we used custom parameter */
1108
0
    if (si->pctx && !cms_sd_asn1_ctrl(si, 0))
1109
0
        goto err;
1110
1111
    /*
1112
     * If any signed attributes calculate and add messageDigest attribute
1113
     */
1114
0
    if (CMS_signed_get_attr_count(si) >= 0) {
1115
0
        unsigned char computed_md[EVP_MAX_MD_SIZE];
1116
1117
0
        if (md == NULL) {
1118
0
            if (!EVP_DigestFinal_ex(mctx, computed_md, &mdlen))
1119
0
                goto err;
1120
0
            md = computed_md;
1121
0
        }
1122
0
        if (!CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest,
1123
0
                V_ASN1_OCTET_STRING, md, mdlen))
1124
0
            goto err;
1125
        /* Copy content type across */
1126
0
        if (!cms_set_si_contentType_attr(cms, si))
1127
0
            goto err;
1128
1129
0
        if (!CMS_SignerInfo_sign(si))
1130
0
            goto err;
1131
0
    } else if (si->pctx) {
1132
0
        size_t siglen;
1133
0
        unsigned char computed_md[EVP_MAX_MD_SIZE];
1134
1135
0
        pctx = si->pctx;
1136
0
        si->pctx = NULL;
1137
0
        if (md == NULL) {
1138
0
            if (!EVP_DigestFinal_ex(mctx, computed_md, &mdlen))
1139
0
                goto err;
1140
0
            md = computed_md;
1141
0
        }
1142
0
        siglen = EVP_PKEY_get_size(si->pkey);
1143
0
        if (siglen == 0 || (sig = OPENSSL_malloc(siglen)) == NULL)
1144
0
            goto err;
1145
0
        if (EVP_PKEY_sign(pctx, sig, &siglen, md, mdlen) <= 0)
1146
0
            goto err;
1147
0
        ASN1_STRING_set0(si->signature, sig, (int)siglen);
1148
0
        sig = NULL;
1149
0
    } else {
1150
0
        OSSL_LIB_CTX *libctx = ossl_cms_ctx_get0_libctx(ctx);
1151
0
        const char *algorithm;
1152
0
        unsigned int siglen;
1153
0
        int has_msg_update;
1154
1155
0
        if (md != NULL) {
1156
0
            ERR_raise(ERR_LIB_CMS, CMS_R_OPERATION_UNSUPPORTED);
1157
0
            goto err;
1158
0
        }
1159
0
        siglen = EVP_PKEY_get_size(si->pkey);
1160
0
        if (siglen == 0 || (sig = OPENSSL_malloc(siglen)) == NULL)
1161
0
            goto err;
1162
1163
0
        if ((algorithm = cms_mdless_signing(si->pkey)) != NULL) {
1164
0
            size_t sig_len;
1165
1166
0
            if (!data) {
1167
0
                ERR_raise(ERR_LIB_CMS, CMS_R_NO_CONTENT);
1168
0
                goto err;
1169
0
            }
1170
1171
0
            pctx = EVP_PKEY_CTX_new(si->pkey, NULL);
1172
0
            if (!pctx)
1173
0
                goto err;
1174
1175
0
            sig_alg = EVP_SIGNATURE_fetch(libctx, algorithm, NULL);
1176
0
            if (!sig_alg)
1177
0
                goto err;
1178
1179
0
            if (EVP_PKEY_sign_message_init(pctx, sig_alg, NULL) != 1)
1180
0
                goto err;
1181
1182
0
            sig_len = siglen;
1183
0
            has_msg_update = EVP_SIGNATURE_has_message_update(sig_alg);
1184
0
            if (cms_EVP_PKEY_sign(pctx, data, sig, &sig_len,
1185
0
                    has_msg_update)
1186
0
                != 1)
1187
0
                goto err;
1188
0
            siglen = (unsigned int)sig_len;
1189
0
        } else {
1190
0
            if (!EVP_SignFinal_ex(mctx, sig, &siglen, si->pkey, libctx,
1191
0
                    ossl_cms_ctx_get0_propq(ctx))) {
1192
0
                ERR_raise(ERR_LIB_CMS, CMS_R_SIGNFINAL_ERROR);
1193
0
                goto err;
1194
0
            }
1195
0
        }
1196
0
        ASN1_STRING_set0(si->signature, sig, siglen);
1197
0
        sig = NULL;
1198
0
    }
1199
1200
0
    r = 1;
1201
1202
0
err:
1203
0
    OPENSSL_free(sig);
1204
0
    EVP_MD_CTX_free(mctx);
1205
0
    EVP_PKEY_CTX_free(pctx);
1206
0
    EVP_SIGNATURE_free(sig_alg);
1207
0
    return r;
1208
0
}
1209
1210
int ossl_cms_SignedData_final(CMS_ContentInfo *cms, BIO *chain, BIO *data,
1211
    const unsigned char *precomp_md,
1212
    unsigned int precomp_mdlen)
1213
0
{
1214
0
    STACK_OF(CMS_SignerInfo) *sinfos;
1215
0
    CMS_SignerInfo *si;
1216
0
    int i;
1217
1218
0
    sinfos = CMS_get0_SignerInfos(cms);
1219
0
    for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
1220
0
        si = sk_CMS_SignerInfo_value(sinfos, i);
1221
0
        if (!cms_SignerInfo_content_sign(cms, si, chain, data,
1222
0
                precomp_md, precomp_mdlen))
1223
0
            return 0;
1224
0
    }
1225
0
    cms->d.signedData->encapContentInfo->partial = 0;
1226
0
    return 1;
1227
0
}
1228
1229
int CMS_SignerInfo_sign(CMS_SignerInfo *si)
1230
0
{
1231
0
    EVP_MD_CTX *mctx = si->mctx;
1232
0
    EVP_PKEY_CTX *pctx = NULL;
1233
0
    unsigned char *abuf = NULL;
1234
0
    int alen;
1235
0
    size_t siglen;
1236
0
    const CMS_CTX *ctx = si->cms_ctx;
1237
0
    char md_name_buf[OSSL_MAX_NAME_SIZE], *md_name;
1238
1239
0
    if (OBJ_obj2txt(md_name_buf, sizeof(md_name_buf),
1240
0
            si->digestAlgorithm->algorithm, 0)
1241
0
        <= 0)
1242
0
        return 0;
1243
0
    md_name = cms_signature_nomd(si->pkey) ? NULL : md_name_buf;
1244
1245
0
    if (!si->omit_signing_time
1246
0
        && CMS_signed_get_attr_by_NID(si, NID_pkcs9_signingTime, -1) < 0) {
1247
0
        if (!cms_add1_signingTime(si, NULL))
1248
0
            goto err;
1249
0
    }
1250
1251
0
    if (!ossl_cms_si_check_attributes(si))
1252
0
        goto err;
1253
1254
0
    if (si->pctx) {
1255
0
        pctx = si->pctx;
1256
0
    } else {
1257
0
        EVP_MD_CTX_reset(mctx);
1258
0
        if (EVP_DigestSignInit_ex(mctx, &pctx, md_name,
1259
0
                ossl_cms_ctx_get0_libctx(ctx),
1260
0
                ossl_cms_ctx_get0_propq(ctx), si->pkey,
1261
0
                NULL)
1262
0
            <= 0)
1263
0
            goto err;
1264
0
        EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
1265
0
        si->pctx = pctx;
1266
0
    }
1267
1268
0
    if (md_name == NULL) {
1269
0
        if (ASN1_item_sign_ctx(ASN1_ITEM_rptr(CMS_Attributes_Sign), NULL,
1270
0
                NULL, si->signature, si->signedAttrs, mctx)
1271
0
            <= 0)
1272
0
            goto err;
1273
0
        return 1;
1274
0
    }
1275
1276
0
    alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs, &abuf,
1277
0
        ASN1_ITEM_rptr(CMS_Attributes_Sign));
1278
0
    if (alen < 0 || abuf == NULL)
1279
0
        goto err;
1280
0
    if (EVP_DigestSignUpdate(mctx, abuf, alen) <= 0)
1281
0
        goto err;
1282
0
    if (EVP_DigestSignFinal(mctx, NULL, &siglen) <= 0)
1283
0
        goto err;
1284
0
    OPENSSL_free(abuf);
1285
0
    abuf = OPENSSL_malloc(siglen);
1286
0
    if (abuf == NULL)
1287
0
        goto err;
1288
0
    if (EVP_DigestSignFinal(mctx, abuf, &siglen) <= 0)
1289
0
        goto err;
1290
1291
0
    EVP_MD_CTX_reset(mctx);
1292
1293
0
    ASN1_STRING_set0(si->signature, abuf, (int)siglen);
1294
1295
0
    return 1;
1296
1297
0
err:
1298
0
    OPENSSL_free(abuf);
1299
0
    EVP_MD_CTX_reset(mctx);
1300
0
    return 0;
1301
0
}
1302
1303
int CMS_SignerInfo_verify(CMS_SignerInfo *si)
1304
1.14k
{
1305
1.14k
    EVP_MD_CTX *mctx = NULL;
1306
1.14k
    unsigned char *abuf = NULL;
1307
1.14k
    int alen, r = -1;
1308
1.14k
    char name[OSSL_MAX_NAME_SIZE];
1309
1.14k
    const EVP_MD *md;
1310
1.14k
    EVP_MD *fetched_md = NULL;
1311
1.14k
    const CMS_CTX *ctx = si->cms_ctx;
1312
1.14k
    OSSL_LIB_CTX *libctx = ossl_cms_ctx_get0_libctx(ctx);
1313
1.14k
    const char *propq = ossl_cms_ctx_get0_propq(ctx);
1314
1315
1.14k
    if (si->pkey == NULL) {
1316
7
        ERR_raise(ERR_LIB_CMS, CMS_R_NO_PUBLIC_KEY);
1317
7
        return -1;
1318
7
    }
1319
1320
1.14k
    if (!ossl_cms_si_check_attributes(si))
1321
17
        return -1;
1322
1323
1.12k
    if (cms_signature_nomd(si->pkey)) {
1324
44
        r = ASN1_item_verify_ex(ASN1_ITEM_rptr(CMS_Attributes_Sign),
1325
44
            si->signatureAlgorithm, si->signature,
1326
44
            si->signedAttrs, NULL, si->pkey,
1327
44
            libctx, propq);
1328
44
        if (r <= 0)
1329
44
            ERR_raise(ERR_LIB_CMS, CMS_R_VERIFICATION_FAILURE);
1330
44
        return r;
1331
44
    }
1332
1333
1.07k
    OBJ_obj2txt(name, sizeof(name), si->digestAlgorithm->algorithm, 0);
1334
1335
1.07k
    (void)ERR_set_mark();
1336
1.07k
    fetched_md = EVP_MD_fetch(libctx, name, propq);
1337
1338
1.07k
    if (fetched_md != NULL) {
1339
1.06k
        md = fetched_md;
1340
1.06k
    } else {
1341
10
        md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
1342
        /* Reject aliases such as signature algorithm OIDs */
1343
10
        if (md != NULL
1344
2
            && EVP_MD_get_type(md) != OBJ_obj2nid(si->digestAlgorithm->algorithm))
1345
1
            md = NULL;
1346
10
    }
1347
1.07k
    if (md == NULL) {
1348
9
        (void)ERR_clear_last_mark();
1349
9
        ERR_raise(ERR_LIB_CMS, CMS_R_UNKNOWN_DIGEST_ALGORITHM);
1350
9
        return -1;
1351
9
    }
1352
1.07k
    (void)ERR_pop_to_mark();
1353
1354
1.07k
    if (si->mctx == NULL && (si->mctx = EVP_MD_CTX_new()) == NULL) {
1355
0
        ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
1356
0
        goto err;
1357
0
    }
1358
1.07k
    mctx = si->mctx;
1359
1.07k
    if (si->pctx != NULL) {
1360
0
        EVP_PKEY_CTX_free(si->pctx);
1361
0
        si->pctx = NULL;
1362
0
    }
1363
1.07k
    if (EVP_DigestVerifyInit_ex(mctx, &si->pctx, EVP_MD_get0_name(md), libctx,
1364
1.07k
            propq, si->pkey, NULL)
1365
1.07k
        <= 0) {
1366
17
        si->pctx = NULL;
1367
17
        goto err;
1368
17
    }
1369
1.05k
    EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
1370
1371
1.05k
    if (!cms_sd_asn1_ctrl(si, 1))
1372
9
        goto err;
1373
1374
1.04k
    alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs, &abuf,
1375
1.04k
        ASN1_ITEM_rptr(CMS_Attributes_Verify));
1376
1.04k
    if (abuf == NULL || alen < 0)
1377
0
        goto err;
1378
1.04k
    r = EVP_DigestVerifyUpdate(mctx, abuf, alen);
1379
1.04k
    OPENSSL_free(abuf);
1380
1.04k
    if (r <= 0) {
1381
0
        r = -1;
1382
0
        goto err;
1383
0
    }
1384
1.04k
    r = EVP_DigestVerifyFinal(mctx,
1385
1.04k
        si->signature->data, si->signature->length);
1386
1.04k
    if (r <= 0)
1387
1.04k
        ERR_raise(ERR_LIB_CMS, CMS_R_VERIFICATION_FAILURE);
1388
1.07k
err:
1389
1.07k
    EVP_MD_free(fetched_md);
1390
1.07k
    EVP_MD_CTX_reset(mctx);
1391
1.07k
    return r;
1392
1.04k
}
1393
1394
/* Create a chain of digest BIOs from a CMS ContentInfo */
1395
BIO *ossl_cms_SignedData_init_bio(CMS_ContentInfo *cms)
1396
2.10k
{
1397
2.10k
    int i;
1398
2.10k
    CMS_SignedData *sd;
1399
2.10k
    BIO *chain = NULL;
1400
1401
2.10k
    sd = cms_get0_signed(cms);
1402
2.10k
    if (sd == NULL)
1403
0
        return NULL;
1404
2.10k
    if (cms->d.signedData->encapContentInfo->partial)
1405
0
        cms_sd_set_version(sd);
1406
4.13k
    for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++) {
1407
2.09k
        X509_ALGOR *digestAlgorithm;
1408
2.09k
        BIO *mdbio;
1409
1410
2.09k
        digestAlgorithm = sk_X509_ALGOR_value(sd->digestAlgorithms, i);
1411
2.09k
        mdbio = ossl_cms_DigestAlgorithm_init_bio(digestAlgorithm,
1412
2.09k
            ossl_cms_get0_cmsctx(cms));
1413
2.09k
        if (mdbio == NULL)
1414
64
            goto err;
1415
2.03k
        if (chain != NULL)
1416
0
            BIO_push(chain, mdbio);
1417
2.03k
        else
1418
2.03k
            chain = mdbio;
1419
2.03k
    }
1420
2.03k
    return chain;
1421
64
err:
1422
64
    BIO_free_all(chain);
1423
64
    return NULL;
1424
2.10k
}
1425
1426
int CMS_SignerInfo_verify_content(CMS_SignerInfo *si, BIO *chain)
1427
0
{
1428
0
    return CMS_SignerInfo_verify_ex(si, chain, NULL);
1429
0
}
1430
1431
int CMS_SignerInfo_verify_ex(CMS_SignerInfo *si, BIO *chain, BIO *data)
1432
2.07k
{
1433
2.07k
    const ASN1_OCTET_STRING *os = NULL;
1434
2.07k
    EVP_MD_CTX *mctx = EVP_MD_CTX_new();
1435
2.07k
    EVP_PKEY_CTX *pkctx = NULL;
1436
2.07k
    int r = -1;
1437
2.07k
    unsigned char mval[EVP_MAX_MD_SIZE];
1438
2.07k
    unsigned int mlen;
1439
2.07k
    EVP_SIGNATURE *sig_alg = NULL;
1440
2.07k
    unsigned char *buffer = NULL;
1441
1442
2.07k
    if (mctx == NULL) {
1443
0
        ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
1444
0
        goto err;
1445
0
    }
1446
    /* If we have any signed attributes look for messageDigest value */
1447
2.07k
    if (CMS_signed_get_attr_count(si) >= 0) {
1448
114
        os = CMS_signed_get0_data_by_OBJ(si,
1449
114
            OBJ_nid2obj(NID_pkcs9_messageDigest),
1450
114
            -3, V_ASN1_OCTET_STRING);
1451
114
        if (os == NULL) {
1452
0
            ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE);
1453
0
            goto err;
1454
0
        }
1455
114
    }
1456
1457
2.07k
    if (!ossl_cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm))
1458
38
        goto err;
1459
1460
2.03k
    if (EVP_DigestFinal_ex(mctx, mval, &mlen) <= 0) {
1461
0
        ERR_raise(ERR_LIB_CMS, CMS_R_UNABLE_TO_FINALIZE_CONTEXT);
1462
0
        goto err;
1463
0
    }
1464
1465
    /* If messageDigest found compare it */
1466
2.03k
    if (os != NULL) {
1467
114
        if (mlen != (unsigned int)os->length) {
1468
0
            ERR_raise(ERR_LIB_CMS, CMS_R_MESSAGEDIGEST_ATTRIBUTE_WRONG_LENGTH);
1469
0
            goto err;
1470
0
        }
1471
1472
114
        if (memcmp(mval, os->data, mlen)) {
1473
78
            ERR_raise(ERR_LIB_CMS, CMS_R_VERIFICATION_FAILURE);
1474
78
            r = 0;
1475
78
        } else {
1476
36
            r = 1;
1477
36
        }
1478
1.92k
    } else {
1479
1.92k
        const EVP_MD *md = EVP_MD_CTX_get0_md(mctx);
1480
1.92k
        const CMS_CTX *ctx = si->cms_ctx;
1481
1.92k
        OSSL_LIB_CTX *libctx = ossl_cms_ctx_get0_libctx(ctx);
1482
1.92k
        const char *algorithm;
1483
1.92k
        int has_msg_update;
1484
1485
1.92k
        pkctx = EVP_PKEY_CTX_new_from_pkey(libctx, si->pkey,
1486
1.92k
            ossl_cms_ctx_get0_propq(ctx));
1487
1.92k
        if (pkctx == NULL)
1488
64
            goto err;
1489
1490
1.85k
        if ((algorithm = cms_mdless_signing(si->pkey)) != NULL) {
1491
619
            if (!data) {
1492
2
                ERR_raise(ERR_LIB_CMS, CMS_R_NO_CONTENT);
1493
2
                goto err;
1494
2
            }
1495
1496
617
            sig_alg = EVP_SIGNATURE_fetch(libctx, algorithm, NULL);
1497
617
            if (!sig_alg)
1498
3
                goto err;
1499
614
            if (EVP_PKEY_verify_message_init(pkctx, sig_alg, NULL) != 1)
1500
0
                goto err;
1501
1502
614
            has_msg_update = EVP_SIGNATURE_has_message_update(sig_alg);
1503
614
            r = cms_EVP_PKEY_verify(pkctx, data, si->signature->data,
1504
614
                si->signature->length, has_msg_update);
1505
1.23k
        } else {
1506
1.23k
            if (EVP_PKEY_verify_init(pkctx) <= 0)
1507
0
                goto err;
1508
1.23k
            if (EVP_PKEY_CTX_set_signature_md(pkctx, md) <= 0)
1509
5
                goto err;
1510
1.23k
            si->pctx = pkctx;
1511
1.23k
            if (!cms_sd_asn1_ctrl(si, 1)) {
1512
21
                si->pctx = NULL;
1513
21
                goto err;
1514
21
            }
1515
1.21k
            si->pctx = NULL;
1516
1517
1.21k
            r = EVP_PKEY_verify(pkctx, si->signature->data,
1518
1.21k
                si->signature->length, mval, mlen);
1519
1.21k
        }
1520
1.82k
        if (r <= 0) {
1521
1.76k
            ERR_raise(ERR_LIB_CMS, CMS_R_VERIFICATION_FAILURE);
1522
1.76k
            r = 0;
1523
1.76k
        }
1524
1.82k
    }
1525
1526
2.07k
err:
1527
2.07k
    EVP_PKEY_CTX_free(pkctx);
1528
2.07k
    EVP_MD_CTX_free(mctx);
1529
2.07k
    EVP_SIGNATURE_free(sig_alg);
1530
2.07k
    OPENSSL_free(buffer);
1531
2.07k
    return r;
1532
2.03k
}
1533
1534
BIO *CMS_SignedData_verify(CMS_SignedData *sd, BIO *detached_data,
1535
    const STACK_OF(X509) *scerts, X509_STORE *store,
1536
    const STACK_OF(X509) *extra,
1537
    const STACK_OF(X509_CRL) *crls,
1538
    unsigned int flags,
1539
    OSSL_LIB_CTX *libctx, const char *propq)
1540
0
{
1541
0
    CMS_ContentInfo *ci;
1542
0
    BIO *bio = NULL;
1543
0
    int i, res = 0;
1544
1545
0
    if (sd == NULL) {
1546
0
        ERR_raise(ERR_LIB_CMS, ERR_R_PASSED_NULL_PARAMETER);
1547
0
        return NULL;
1548
0
    }
1549
1550
0
    if ((ci = CMS_ContentInfo_new_ex(libctx, propq)) == NULL)
1551
0
        return NULL;
1552
0
    if ((bio = BIO_new(BIO_s_mem())) == NULL)
1553
0
        goto end;
1554
0
    ci->contentType = OBJ_nid2obj(NID_pkcs7_signed);
1555
0
    ci->d.signedData = sd;
1556
1557
0
    for (i = 0; i < sk_X509_num(extra); i++)
1558
0
        if (!CMS_add1_cert(ci, sk_X509_value(extra, i)))
1559
0
            goto end;
1560
0
    for (i = 0; i < sk_X509_CRL_num(crls); i++)
1561
0
        if (!CMS_add1_crl(ci, sk_X509_CRL_value(crls, i)))
1562
0
            goto end;
1563
0
    res = CMS_verify(ci, scerts, store, detached_data, bio, flags);
1564
1565
0
end:
1566
0
    if (ci != NULL)
1567
0
        ci->d.signedData = NULL; /* do not indirectly free |sd| */
1568
0
    CMS_ContentInfo_free(ci);
1569
0
    if (!res) {
1570
0
        BIO_free(bio);
1571
0
        bio = NULL;
1572
0
    }
1573
0
    return bio;
1574
0
}
1575
1576
int CMS_add_smimecap(CMS_SignerInfo *si, STACK_OF(X509_ALGOR) *algs)
1577
0
{
1578
0
    unsigned char *smder = NULL;
1579
0
    int smderlen, r;
1580
1581
0
    smderlen = i2d_X509_ALGORS(algs, &smder);
1582
0
    if (smderlen <= 0)
1583
0
        return 0;
1584
0
    r = CMS_signed_add1_attr_by_NID(si, NID_SMIMECapabilities,
1585
0
        V_ASN1_SEQUENCE, smder, smderlen);
1586
0
    OPENSSL_free(smder);
1587
0
    return r;
1588
0
}
1589
1590
int CMS_add_simple_smimecap(STACK_OF(X509_ALGOR) **algs,
1591
    int algnid, int keysize)
1592
0
{
1593
0
    X509_ALGOR *alg;
1594
0
    ASN1_INTEGER *key = NULL;
1595
1596
0
    if (keysize > 0) {
1597
0
        key = ASN1_INTEGER_new();
1598
0
        if (key == NULL || !ASN1_INTEGER_set(key, keysize)) {
1599
0
            ASN1_INTEGER_free(key);
1600
0
            return 0;
1601
0
        }
1602
0
    }
1603
0
    alg = ossl_X509_ALGOR_from_nid(algnid, key != NULL ? V_ASN1_INTEGER : V_ASN1_UNDEF, key);
1604
0
    if (alg == NULL) {
1605
0
        ASN1_INTEGER_free(key);
1606
0
        return 0;
1607
0
    }
1608
1609
0
    if (*algs == NULL)
1610
0
        *algs = sk_X509_ALGOR_new_null();
1611
0
    if (*algs == NULL || !sk_X509_ALGOR_push(*algs, alg)) {
1612
0
        X509_ALGOR_free(alg);
1613
0
        return 0;
1614
0
    }
1615
0
    return 1;
1616
0
}
1617
1618
/* Check to see if a cipher exists and if so add S/MIME capabilities */
1619
static int cms_add_cipher_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg,
1620
    OSSL_LIB_CTX *libctx, const char *propq)
1621
0
{
1622
0
    EVP_CIPHER *cipher;
1623
1624
0
    ERR_set_mark();
1625
0
    cipher = EVP_CIPHER_fetch(libctx, OBJ_nid2sn(nid), propq);
1626
0
    ERR_pop_to_mark();
1627
0
    if (cipher != NULL) {
1628
0
        EVP_CIPHER_free(cipher);
1629
0
        return CMS_add_simple_smimecap(sk, nid, arg);
1630
0
    }
1631
0
    return 1;
1632
0
}
1633
1634
static int cms_add_digest_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg,
1635
    OSSL_LIB_CTX *libctx, const char *propq)
1636
0
{
1637
0
    EVP_MD *md;
1638
1639
0
    ERR_set_mark();
1640
0
    md = EVP_MD_fetch(libctx, OBJ_nid2sn(nid), propq);
1641
0
    ERR_pop_to_mark();
1642
0
    if (md != NULL) {
1643
0
        EVP_MD_free(md);
1644
0
        return CMS_add_simple_smimecap(sk, nid, arg);
1645
0
    }
1646
0
    return 1;
1647
0
}
1648
1649
int CMS_add_standard_smimecap_ex(STACK_OF(X509_ALGOR) **smcap,
1650
    OSSL_LIB_CTX *libctx, const char *propq)
1651
0
{
1652
0
    if (!cms_add_cipher_smcap(smcap, NID_aes_256_cbc, -1, libctx, propq)
1653
0
        || !cms_add_digest_smcap(smcap, NID_id_GostR3411_2012_256, -1, libctx, propq)
1654
0
        || !cms_add_digest_smcap(smcap, NID_id_GostR3411_2012_512, -1, libctx, propq)
1655
0
        || !cms_add_digest_smcap(smcap, NID_id_GostR3411_94, -1, libctx, propq)
1656
0
        || !cms_add_cipher_smcap(smcap, NID_id_Gost28147_89, -1, libctx, propq)
1657
0
        || !cms_add_cipher_smcap(smcap, NID_aes_192_cbc, -1, libctx, propq)
1658
0
        || !cms_add_cipher_smcap(smcap, NID_aes_128_cbc, -1, libctx, propq)
1659
0
        || !cms_add_cipher_smcap(smcap, NID_des_ede3_cbc, -1, libctx, propq)
1660
0
        || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 128, libctx, propq))
1661
0
        return 0;
1662
0
    return 1;
1663
0
}
1664
1665
int CMS_add_standard_smimecap(STACK_OF(X509_ALGOR) **smcap)
1666
0
{
1667
0
    return CMS_add_standard_smimecap_ex(smcap, NULL, NULL);
1668
0
}