Coverage Report

Created: 2023-09-25 06:42

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