Coverage Report

Created: 2025-06-13 06:58

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