Coverage Report

Created: 2025-08-28 07:07

/src/openssl35/crypto/cms/cms_sd.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2008-2025 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include "internal/cryptlib.h"
11
#include <openssl/asn1t.h>
12
#include <openssl/pem.h>
13
#include <openssl/x509.h>
14
#include <openssl/x509v3.h>
15
#include <openssl/err.h>
16
#include <openssl/cms.h>
17
#include <openssl/ess.h>
18
#include "internal/sizes.h"
19
#include "crypto/asn1.h"
20
#include "crypto/evp.h"
21
#include "crypto/ess.h"
22
#include "crypto/x509.h" /* for ossl_x509_add_cert_new() */
23
#include "cms_local.h"
24
25
/* CMS SignedData Utilities */
26
27
static CMS_SignedData *cms_get0_signed(CMS_ContentInfo *cms)
28
6.63k
{
29
6.63k
    if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_signed) {
30
5.14k
        ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_TYPE_NOT_SIGNED_DATA);
31
5.14k
        return NULL;
32
5.14k
    }
33
1.48k
    return cms->d.signedData;
34
6.63k
}
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
static int cms_signature_nomd(EVP_PKEY *pkey)
229
0
{
230
0
    char def_md[80];
231
232
0
    return EVP_PKEY_get_default_digest_name(pkey, def_md, sizeof(def_md)) == 2
233
0
        && strcmp(def_md, "UNDEF") == 0 ? 1 : 0;
234
0
}
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
0
{
240
0
    if (!ossl_assert(verify == 0 || verify == 1))
241
0
        return -1;
242
243
0
    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
0
    return 1;
267
0
}
268
269
static int cms_sd_asn1_ctrl(CMS_SignerInfo *si, int cmd)
270
0
{
271
0
    EVP_PKEY *pkey = si->pkey;
272
0
    int i;
273
274
0
    if (EVP_PKEY_is_a(pkey, "DSA") || EVP_PKEY_is_a(pkey, "EC"))
275
0
        return cms_generic_sign(si, cmd) > 0;
276
0
    else if (EVP_PKEY_is_a(pkey, "RSA") || EVP_PKEY_is_a(pkey, "RSA-PSS"))
277
0
        return ossl_cms_rsa_sign(si, cmd) > 0;
278
279
    /* Now give engines, providers, etc a chance to handle this */
280
0
    if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL)
281
0
        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()) || !ASN1_STRING_set(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()) || !ASN1_STRING_set(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
CMS_SignerInfo *CMS_add1_signer(CMS_ContentInfo *cms,
345
                                X509 *signer, EVP_PKEY *pk, const EVP_MD *md,
346
                                unsigned int flags)
347
0
{
348
0
    CMS_SignedData *sd;
349
0
    CMS_SignerInfo *si = NULL;
350
0
    X509_ALGOR *alg;
351
0
    int i, type;
352
0
    const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
353
354
0
    if (!X509_check_private_key(signer, pk)) {
355
0
        ERR_raise(ERR_LIB_CMS, CMS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
356
0
        return NULL;
357
0
    }
358
0
    sd = cms_signed_data_init(cms);
359
0
    if (!sd)
360
0
        goto err;
361
0
    si = M_ASN1_new_of(CMS_SignerInfo);
362
0
    if (!si) {
363
0
        ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
364
0
        goto err;
365
0
    }
366
    /* Call for side-effect of computing hash and caching extensions */
367
0
    X509_check_purpose(signer, -1, -1);
368
369
0
    if (!X509_up_ref(signer))
370
0
        goto err;
371
0
    if (!EVP_PKEY_up_ref(pk)) {
372
0
        X509_free(signer);
373
0
        goto err;
374
0
    }
375
376
0
    si->cms_ctx = ctx;
377
0
    si->pkey = pk;
378
0
    si->signer = signer;
379
0
    si->mctx = EVP_MD_CTX_new();
380
0
    si->pctx = NULL;
381
0
    si->omit_signing_time = 0;
382
383
0
    if (si->mctx == NULL) {
384
0
        ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
385
0
        goto err;
386
0
    }
387
388
0
    if (flags & CMS_USE_KEYID) {
389
0
        si->version = 3;
390
0
        if (sd->version < 3)
391
0
            sd->version = 3;
392
0
        type = CMS_SIGNERINFO_KEYIDENTIFIER;
393
0
    } else {
394
0
        type = CMS_SIGNERINFO_ISSUER_SERIAL;
395
0
        si->version = 1;
396
0
    }
397
398
0
    if (!ossl_cms_set1_SignerIdentifier(si->sid, signer, type, ctx))
399
0
        goto err;
400
401
0
    if (md == NULL) {
402
0
        int def_nid;
403
404
0
        if (EVP_PKEY_get_default_digest_nid(pk, &def_nid) <= 0) {
405
0
            ERR_raise_data(ERR_LIB_CMS, CMS_R_NO_DEFAULT_DIGEST,
406
0
                           "pkey nid=%d", EVP_PKEY_get_id(pk));
407
0
            goto err;
408
0
        }
409
0
        md = EVP_get_digestbynid(def_nid);
410
0
        if (md == NULL) {
411
0
            ERR_raise_data(ERR_LIB_CMS, CMS_R_NO_DEFAULT_DIGEST,
412
0
                           "default md nid=%d", def_nid);
413
0
            goto err;
414
0
        }
415
0
    }
416
417
0
    X509_ALGOR_set_md(si->digestAlgorithm, md);
418
419
    /* See if digest is present in digestAlgorithms */
420
0
    for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++) {
421
0
        const ASN1_OBJECT *aoid;
422
0
        char name[OSSL_MAX_NAME_SIZE];
423
424
0
        alg = sk_X509_ALGOR_value(sd->digestAlgorithms, i);
425
0
        X509_ALGOR_get0(&aoid, NULL, NULL, alg);
426
0
        OBJ_obj2txt(name, sizeof(name), aoid, 0);
427
0
        if (EVP_MD_is_a(md, name))
428
0
            break;
429
0
    }
430
431
0
    if (i == sk_X509_ALGOR_num(sd->digestAlgorithms)) {
432
0
        if ((alg = X509_ALGOR_new()) == NULL) {
433
0
            ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB);
434
0
            goto err;
435
0
        }
436
0
        X509_ALGOR_set_md(alg, md);
437
0
        if (!sk_X509_ALGOR_push(sd->digestAlgorithms, alg)) {
438
0
            X509_ALGOR_free(alg);
439
0
            ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB);
440
0
            goto err;
441
0
        }
442
0
    }
443
444
0
    if (!(flags & CMS_KEY_PARAM) && !cms_sd_asn1_ctrl(si, 0)) {
445
0
        ERR_raise_data(ERR_LIB_CMS, CMS_R_UNSUPPORTED_SIGNATURE_ALGORITHM,
446
0
                       "pkey nid=%d", EVP_PKEY_get_id(pk));
447
0
        goto err;
448
0
    }
449
0
    if (!(flags & CMS_NOATTR)) {
450
        /*
451
         * Initialize signed attributes structure so other attributes
452
         * such as signing time etc are added later even if we add none here.
453
         */
454
0
        if (!si->signedAttrs) {
455
0
            si->signedAttrs = sk_X509_ATTRIBUTE_new_null();
456
0
            if (!si->signedAttrs) {
457
0
                ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB);
458
0
                goto err;
459
0
            }
460
0
        }
461
462
0
        if (!(flags & CMS_NOSMIMECAP)) {
463
0
            STACK_OF(X509_ALGOR) *smcap = NULL;
464
465
0
            i = CMS_add_standard_smimecap(&smcap);
466
0
            if (i)
467
0
                i = CMS_add_smimecap(si, smcap);
468
0
            sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
469
0
            if (!i) {
470
0
                ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
471
0
                goto err;
472
0
            }
473
0
        }
474
0
        if ((flags & CMS_NO_SIGNING_TIME) != 0) {
475
            /*
476
             * The signing-time signed attribute (NID_pkcs9_signingTime)
477
             * would normally be added later, in CMS_SignerInfo_sign(),
478
             * unless we set this flag here
479
             */
480
0
            si->omit_signing_time = 1;
481
0
        }
482
0
        if (flags & CMS_CADES) {
483
0
            ESS_SIGNING_CERT *sc = NULL;
484
0
            ESS_SIGNING_CERT_V2 *sc2 = NULL;
485
0
            int add_sc;
486
487
0
            if (md == NULL || EVP_MD_is_a(md, SN_sha1)) {
488
0
                if ((sc = OSSL_ESS_signing_cert_new_init(signer,
489
0
                                                         NULL, 1)) == NULL)
490
0
                    goto err;
491
0
                add_sc = ossl_cms_add1_signing_cert(si, sc);
492
0
                ESS_SIGNING_CERT_free(sc);
493
0
            } else {
494
0
                if ((sc2 = OSSL_ESS_signing_cert_v2_new_init(md, signer,
495
0
                                                             NULL, 1)) == NULL)
496
0
                    goto err;
497
0
                add_sc = ossl_cms_add1_signing_cert_v2(si, sc2);
498
0
                ESS_SIGNING_CERT_V2_free(sc2);
499
0
            }
500
0
            if (!add_sc)
501
0
                goto err;
502
0
        }
503
0
        if (flags & CMS_REUSE_DIGEST) {
504
0
            if (!cms_copy_messageDigest(cms, si))
505
0
                goto err;
506
0
            if (!cms_set_si_contentType_attr(cms, si))
507
0
                goto err;
508
0
            if (!(flags & (CMS_PARTIAL | CMS_KEY_PARAM)) &&
509
0
                !CMS_SignerInfo_sign(si))
510
0
                goto err;
511
0
        }
512
0
    }
513
514
0
    if (!(flags & CMS_NOCERTS)) {
515
        /* NB ignore -1 return for duplicate cert */
516
0
        if (!CMS_add1_cert(cms, signer)) {
517
0
            ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
518
0
            goto err;
519
0
        }
520
0
    }
521
522
0
    if (flags & CMS_KEY_PARAM) {
523
0
        if (flags & CMS_NOATTR) {
524
0
            si->pctx = EVP_PKEY_CTX_new_from_pkey(ossl_cms_ctx_get0_libctx(ctx),
525
0
                                                  si->pkey,
526
0
                                                  ossl_cms_ctx_get0_propq(ctx));
527
0
            if (si->pctx == NULL)
528
0
                goto err;
529
0
            if (EVP_PKEY_sign_init(si->pctx) <= 0)
530
0
                goto err;
531
0
            if (EVP_PKEY_CTX_set_signature_md(si->pctx, md) <= 0)
532
0
                goto err;
533
0
        } else if (EVP_DigestSignInit_ex(si->mctx, &si->pctx,
534
0
                                         EVP_MD_get0_name(md),
535
0
                                         ossl_cms_ctx_get0_libctx(ctx),
536
0
                                         ossl_cms_ctx_get0_propq(ctx),
537
0
                                         pk, NULL) <= 0) {
538
0
            si->pctx = NULL;
539
0
            goto err;
540
0
        }
541
0
        else {
542
0
            EVP_MD_CTX_set_flags(si->mctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
543
0
        }
544
0
    }
545
546
0
    if (sd->signerInfos == NULL)
547
0
        sd->signerInfos = sk_CMS_SignerInfo_new_null();
548
0
    if (sd->signerInfos == NULL || !sk_CMS_SignerInfo_push(sd->signerInfos, si)) {
549
0
        ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB);
550
0
        goto err;
551
0
    }
552
553
0
    return si;
554
555
0
 err:
556
0
    M_ASN1_free_of(si, CMS_SignerInfo);
557
0
    return NULL;
558
559
0
}
560
561
void ossl_cms_SignerInfos_set_cmsctx(CMS_ContentInfo *cms)
562
6.63k
{
563
6.63k
    int i;
564
6.63k
    CMS_SignerInfo *si;
565
6.63k
    STACK_OF(CMS_SignerInfo) *sinfos;
566
6.63k
    const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
567
568
6.63k
    ERR_set_mark();
569
6.63k
    sinfos = CMS_get0_SignerInfos(cms);
570
6.63k
    ERR_pop_to_mark(); /* removes error in case sinfos == NULL */
571
572
14.3k
    for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
573
7.75k
        si = sk_CMS_SignerInfo_value(sinfos, i);
574
7.75k
        if (si != NULL)
575
7.75k
            si->cms_ctx = ctx;
576
7.75k
    }
577
6.63k
}
578
579
static int cms_add1_signingTime(CMS_SignerInfo *si, ASN1_TIME *t)
580
0
{
581
0
    ASN1_TIME *tt;
582
0
    int r = 0;
583
584
0
    if (t != NULL)
585
0
        tt = t;
586
0
    else
587
0
        tt = X509_gmtime_adj(NULL, 0);
588
589
0
    if (tt == NULL) {
590
0
        ERR_raise(ERR_LIB_CMS, ERR_R_X509_LIB);
591
0
        goto err;
592
0
    }
593
594
0
    if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_signingTime,
595
0
                                    tt->type, tt, -1) <= 0) {
596
0
        ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
597
0
        goto err;
598
0
    }
599
600
0
    r = 1;
601
0
 err:
602
0
    if (t == NULL)
603
0
        ASN1_TIME_free(tt);
604
605
0
    return r;
606
607
0
}
608
609
EVP_PKEY_CTX *CMS_SignerInfo_get0_pkey_ctx(CMS_SignerInfo *si)
610
0
{
611
0
    return si->pctx;
612
0
}
613
614
EVP_MD_CTX *CMS_SignerInfo_get0_md_ctx(CMS_SignerInfo *si)
615
0
{
616
0
    return si->mctx;
617
0
}
618
619
STACK_OF(CMS_SignerInfo) *CMS_get0_SignerInfos(CMS_ContentInfo *cms)
620
6.63k
{
621
6.63k
    CMS_SignedData *sd = cms_get0_signed(cms);
622
623
6.63k
    return sd != NULL ? sd->signerInfos : NULL;
624
6.63k
}
625
626
STACK_OF(X509) *CMS_get0_signers(CMS_ContentInfo *cms)
627
0
{
628
0
    STACK_OF(X509) *signers = NULL;
629
0
    STACK_OF(CMS_SignerInfo) *sinfos;
630
0
    CMS_SignerInfo *si;
631
0
    int i;
632
633
0
    sinfos = CMS_get0_SignerInfos(cms);
634
0
    for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
635
0
        si = sk_CMS_SignerInfo_value(sinfos, i);
636
0
        if (si->signer != NULL) {
637
0
            if (!ossl_x509_add_cert_new(&signers, si->signer,
638
0
                                        X509_ADD_FLAG_DEFAULT)) {
639
0
                sk_X509_free(signers);
640
0
                return NULL;
641
0
            }
642
0
        }
643
0
    }
644
0
    return signers;
645
0
}
646
647
void CMS_SignerInfo_set1_signer_cert(CMS_SignerInfo *si, X509 *signer)
648
0
{
649
0
    if (signer != NULL) {
650
0
        if (!X509_up_ref(signer))
651
0
            return;
652
0
        EVP_PKEY_free(si->pkey);
653
0
        si->pkey = X509_get_pubkey(signer);
654
0
    }
655
0
    X509_free(si->signer);
656
0
    si->signer = signer;
657
0
}
658
659
int CMS_SignerInfo_get0_signer_id(CMS_SignerInfo *si,
660
                                  ASN1_OCTET_STRING **keyid,
661
                                  X509_NAME **issuer, ASN1_INTEGER **sno)
662
0
{
663
0
    return ossl_cms_SignerIdentifier_get0_signer_id(si->sid, keyid, issuer, sno);
664
0
}
665
666
int CMS_SignerInfo_cert_cmp(CMS_SignerInfo *si, X509 *cert)
667
0
{
668
0
    return ossl_cms_SignerIdentifier_cert_cmp(si->sid, cert);
669
0
}
670
671
int CMS_set1_signers_certs(CMS_ContentInfo *cms, STACK_OF(X509) *scerts,
672
                           unsigned int flags)
673
0
{
674
0
    CMS_SignedData *sd;
675
0
    CMS_SignerInfo *si;
676
0
    CMS_CertificateChoices *cch;
677
0
    STACK_OF(CMS_CertificateChoices) *certs;
678
0
    X509 *x;
679
0
    int i, j;
680
0
    int ret = 0;
681
682
0
    sd = cms_get0_signed(cms);
683
0
    if (sd == NULL)
684
0
        return -1;
685
0
    certs = sd->certificates;
686
0
    for (i = 0; i < sk_CMS_SignerInfo_num(sd->signerInfos); i++) {
687
0
        si = sk_CMS_SignerInfo_value(sd->signerInfos, i);
688
0
        if (si->signer != NULL)
689
0
            continue;
690
691
0
        for (j = 0; j < sk_X509_num(scerts); j++) {
692
0
            x = sk_X509_value(scerts, j);
693
0
            if (CMS_SignerInfo_cert_cmp(si, x) == 0) {
694
0
                CMS_SignerInfo_set1_signer_cert(si, x);
695
0
                ret++;
696
0
                break;
697
0
            }
698
0
        }
699
700
0
        if (si->signer != NULL || (flags & CMS_NOINTERN))
701
0
            continue;
702
703
0
        for (j = 0; j < sk_CMS_CertificateChoices_num(certs); j++) {
704
0
            cch = sk_CMS_CertificateChoices_value(certs, j);
705
0
            if (cch->type != CMS_CERTCHOICE_CERT)
706
0
                continue;
707
0
            x = cch->d.certificate;
708
0
            if (CMS_SignerInfo_cert_cmp(si, x) == 0) {
709
0
                CMS_SignerInfo_set1_signer_cert(si, x);
710
0
                ret++;
711
0
                break;
712
0
            }
713
0
        }
714
0
    }
715
0
    return ret;
716
0
}
717
718
void CMS_SignerInfo_get0_algs(CMS_SignerInfo *si, EVP_PKEY **pk,
719
                              X509 **signer, X509_ALGOR **pdig,
720
                              X509_ALGOR **psig)
721
0
{
722
0
    if (pk != NULL)
723
0
        *pk = si->pkey;
724
0
    if (signer != NULL)
725
0
        *signer = si->signer;
726
0
    if (pdig != NULL)
727
0
        *pdig = si->digestAlgorithm;
728
0
    if (psig != NULL)
729
0
        *psig = si->signatureAlgorithm;
730
0
}
731
732
ASN1_OCTET_STRING *CMS_SignerInfo_get0_signature(CMS_SignerInfo *si)
733
0
{
734
0
    return si->signature;
735
0
}
736
737
static int cms_SignerInfo_content_sign(CMS_ContentInfo *cms,
738
                                       CMS_SignerInfo *si, BIO *chain,
739
                                       const unsigned char *md,
740
                                       unsigned int mdlen)
741
0
{
742
0
    EVP_MD_CTX *mctx = EVP_MD_CTX_new();
743
0
    int r = 0;
744
0
    EVP_PKEY_CTX *pctx = NULL;
745
0
    const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
746
747
0
    if (mctx == NULL) {
748
0
        ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
749
0
        return 0;
750
0
    }
751
752
0
    if (si->pkey == NULL) {
753
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NO_PRIVATE_KEY);
754
0
        goto err;
755
0
    }
756
757
0
    if (!ossl_cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm))
758
0
        goto err;
759
    /* Set SignerInfo algorithm details if we used custom parameter */
760
0
    if (si->pctx && !cms_sd_asn1_ctrl(si, 0))
761
0
        goto err;
762
763
    /*
764
     * If any signed attributes calculate and add messageDigest attribute
765
     */
766
0
    if (CMS_signed_get_attr_count(si) >= 0) {
767
0
        unsigned char computed_md[EVP_MAX_MD_SIZE];
768
769
0
        if (md == NULL) {
770
0
            if (!EVP_DigestFinal_ex(mctx, computed_md, &mdlen))
771
0
                goto err;
772
0
            md = computed_md;
773
0
        }
774
0
        if (!CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest,
775
0
                                         V_ASN1_OCTET_STRING, md, mdlen))
776
0
            goto err;
777
        /* Copy content type across */
778
0
        if (!cms_set_si_contentType_attr(cms, si))
779
0
            goto err;
780
781
0
        if (!CMS_SignerInfo_sign(si))
782
0
            goto err;
783
0
    } else if (si->pctx) {
784
0
        unsigned char *sig;
785
0
        size_t siglen;
786
0
        unsigned char computed_md[EVP_MAX_MD_SIZE];
787
788
0
        pctx = si->pctx;
789
0
        si->pctx = NULL;
790
0
        if (md == NULL) {
791
0
            if (!EVP_DigestFinal_ex(mctx, computed_md, &mdlen))
792
0
                goto err;
793
0
            md = computed_md;
794
0
        }
795
0
        siglen = EVP_PKEY_get_size(si->pkey);
796
0
        if (siglen == 0 || (sig = OPENSSL_malloc(siglen)) == NULL)
797
0
            goto err;
798
0
        if (EVP_PKEY_sign(pctx, sig, &siglen, md, mdlen) <= 0) {
799
0
            OPENSSL_free(sig);
800
0
            goto err;
801
0
        }
802
0
        ASN1_STRING_set0(si->signature, sig, siglen);
803
0
    } else {
804
0
        unsigned char *sig;
805
0
        unsigned int siglen;
806
807
0
        if (md != NULL) {
808
0
            ERR_raise(ERR_LIB_CMS, CMS_R_OPERATION_UNSUPPORTED);
809
0
            goto err;
810
0
        }
811
0
        siglen = EVP_PKEY_get_size(si->pkey);
812
0
        if (siglen == 0 || (sig = OPENSSL_malloc(siglen)) == NULL)
813
0
            goto err;
814
0
        if (!EVP_SignFinal_ex(mctx, sig, &siglen, si->pkey,
815
0
                              ossl_cms_ctx_get0_libctx(ctx),
816
0
                              ossl_cms_ctx_get0_propq(ctx))) {
817
0
            ERR_raise(ERR_LIB_CMS, CMS_R_SIGNFINAL_ERROR);
818
0
            OPENSSL_free(sig);
819
0
            goto err;
820
0
        }
821
0
        ASN1_STRING_set0(si->signature, sig, siglen);
822
0
    }
823
824
0
    r = 1;
825
826
0
 err:
827
0
    EVP_MD_CTX_free(mctx);
828
0
    EVP_PKEY_CTX_free(pctx);
829
0
    return r;
830
831
0
}
832
833
int ossl_cms_SignedData_final(CMS_ContentInfo *cms, BIO *chain,
834
                              const unsigned char *precomp_md,
835
                              unsigned int precomp_mdlen)
836
0
{
837
0
    STACK_OF(CMS_SignerInfo) *sinfos;
838
0
    CMS_SignerInfo *si;
839
0
    int i;
840
841
0
    sinfos = CMS_get0_SignerInfos(cms);
842
0
    for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
843
0
        si = sk_CMS_SignerInfo_value(sinfos, i);
844
0
        if (!cms_SignerInfo_content_sign(cms, si, chain,
845
0
                                         precomp_md, precomp_mdlen))
846
0
            return 0;
847
0
    }
848
0
    cms->d.signedData->encapContentInfo->partial = 0;
849
0
    return 1;
850
0
}
851
852
int CMS_SignerInfo_sign(CMS_SignerInfo *si)
853
0
{
854
0
    EVP_MD_CTX *mctx = si->mctx;
855
0
    EVP_PKEY_CTX *pctx = NULL;
856
0
    unsigned char *abuf = NULL;
857
0
    int alen;
858
0
    size_t siglen;
859
0
    const CMS_CTX *ctx = si->cms_ctx;
860
0
    char md_name_buf[OSSL_MAX_NAME_SIZE], *md_name;
861
862
0
    if (OBJ_obj2txt(md_name_buf, sizeof(md_name_buf),
863
0
                    si->digestAlgorithm->algorithm, 0) <= 0)
864
0
        return 0;
865
0
    md_name = cms_signature_nomd(si->pkey) ? NULL : md_name_buf;
866
867
0
    if (!si->omit_signing_time
868
0
        && CMS_signed_get_attr_by_NID(si, NID_pkcs9_signingTime, -1) < 0) {
869
0
        if (!cms_add1_signingTime(si, NULL))
870
0
            goto err;
871
0
    }
872
873
0
    if (!ossl_cms_si_check_attributes(si))
874
0
        goto err;
875
876
0
    if (si->pctx) {
877
0
        pctx = si->pctx;
878
0
    } else {
879
0
        EVP_MD_CTX_reset(mctx);
880
0
        if (EVP_DigestSignInit_ex(mctx, &pctx, md_name,
881
0
                                  ossl_cms_ctx_get0_libctx(ctx),
882
0
                                  ossl_cms_ctx_get0_propq(ctx), si->pkey,
883
0
                                  NULL) <= 0)
884
0
            goto err;
885
0
        EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
886
0
        si->pctx = pctx;
887
0
    }
888
889
0
    if (md_name == NULL) {
890
0
        if (ASN1_item_sign_ctx(ASN1_ITEM_rptr(CMS_Attributes_Sign), NULL,
891
0
                               NULL, si->signature, si->signedAttrs, mctx) <= 0)
892
0
            goto err;
893
0
        return 1;
894
0
    }
895
896
0
    alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs, &abuf,
897
0
                         ASN1_ITEM_rptr(CMS_Attributes_Sign));
898
0
    if (alen < 0 || abuf == NULL)
899
0
        goto err;
900
0
    if (EVP_DigestSignUpdate(mctx, abuf, alen) <= 0)
901
0
        goto err;
902
0
    if (EVP_DigestSignFinal(mctx, NULL, &siglen) <= 0)
903
0
        goto err;
904
0
    OPENSSL_free(abuf);
905
0
    abuf = OPENSSL_malloc(siglen);
906
0
    if (abuf == NULL)
907
0
        goto err;
908
0
    if (EVP_DigestSignFinal(mctx, abuf, &siglen) <= 0)
909
0
        goto err;
910
911
0
    EVP_MD_CTX_reset(mctx);
912
913
0
    ASN1_STRING_set0(si->signature, abuf, siglen);
914
915
0
    return 1;
916
917
0
 err:
918
0
    OPENSSL_free(abuf);
919
0
    EVP_MD_CTX_reset(mctx);
920
0
    return 0;
921
0
}
922
923
int CMS_SignerInfo_verify(CMS_SignerInfo *si)
924
0
{
925
0
    EVP_MD_CTX *mctx = NULL;
926
0
    unsigned char *abuf = NULL;
927
0
    int alen, r = -1;
928
0
    char name[OSSL_MAX_NAME_SIZE];
929
0
    const EVP_MD *md;
930
0
    EVP_MD *fetched_md = NULL;
931
0
    const CMS_CTX *ctx = si->cms_ctx;
932
0
    OSSL_LIB_CTX *libctx = ossl_cms_ctx_get0_libctx(ctx);
933
0
    const char *propq = ossl_cms_ctx_get0_propq(ctx);
934
935
0
    if (si->pkey == NULL) {
936
0
        ERR_raise(ERR_LIB_CMS, CMS_R_NO_PUBLIC_KEY);
937
0
        return -1;
938
0
    }
939
940
0
    if (!ossl_cms_si_check_attributes(si))
941
0
        return -1;
942
943
0
    if (cms_signature_nomd(si->pkey)) {
944
0
        r = ASN1_item_verify_ex(ASN1_ITEM_rptr(CMS_Attributes_Sign),
945
0
                                si->signatureAlgorithm, si->signature,
946
0
                                si->signedAttrs, NULL, si->pkey,
947
0
                                libctx, propq);
948
0
        if (r <= 0)
949
0
            ERR_raise(ERR_LIB_CMS, CMS_R_VERIFICATION_FAILURE);
950
0
        return r;
951
0
    }
952
953
0
    OBJ_obj2txt(name, sizeof(name), si->digestAlgorithm->algorithm, 0);
954
955
0
    (void)ERR_set_mark();
956
0
    fetched_md = EVP_MD_fetch(libctx, name, propq);
957
958
0
    if (fetched_md != NULL)
959
0
        md = fetched_md;
960
0
    else
961
0
        md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
962
0
    if (md == NULL) {
963
0
        (void)ERR_clear_last_mark();
964
0
        ERR_raise(ERR_LIB_CMS, CMS_R_UNKNOWN_DIGEST_ALGORITHM);
965
0
        return -1;
966
0
    }
967
0
    (void)ERR_pop_to_mark();
968
969
0
    if (si->mctx == NULL && (si->mctx = EVP_MD_CTX_new()) == NULL) {
970
0
        ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
971
0
        goto err;
972
0
    }
973
0
    mctx = si->mctx;
974
0
    if (si->pctx != NULL) {
975
0
        EVP_PKEY_CTX_free(si->pctx);
976
0
        si->pctx = NULL;
977
0
    }
978
0
    if (EVP_DigestVerifyInit_ex(mctx, &si->pctx, EVP_MD_get0_name(md), libctx,
979
0
                                propq, si->pkey, NULL) <= 0) {
980
0
        si->pctx = NULL;
981
0
        goto err;
982
0
    }
983
0
    EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_KEEP_PKEY_CTX);
984
985
0
    if (!cms_sd_asn1_ctrl(si, 1))
986
0
        goto err;
987
988
0
    alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs, &abuf,
989
0
                         ASN1_ITEM_rptr(CMS_Attributes_Verify));
990
0
    if (abuf == NULL || alen < 0)
991
0
        goto err;
992
0
    r = EVP_DigestVerifyUpdate(mctx, abuf, alen);
993
0
    OPENSSL_free(abuf);
994
0
    if (r <= 0) {
995
0
        r = -1;
996
0
        goto err;
997
0
    }
998
0
    r = EVP_DigestVerifyFinal(mctx,
999
0
                              si->signature->data, si->signature->length);
1000
0
    if (r <= 0)
1001
0
        ERR_raise(ERR_LIB_CMS, CMS_R_VERIFICATION_FAILURE);
1002
0
 err:
1003
0
    EVP_MD_free(fetched_md);
1004
0
    EVP_MD_CTX_reset(mctx);
1005
0
    return r;
1006
0
}
1007
1008
/* Create a chain of digest BIOs from a CMS ContentInfo */
1009
BIO *ossl_cms_SignedData_init_bio(CMS_ContentInfo *cms)
1010
0
{
1011
0
    int i;
1012
0
    CMS_SignedData *sd;
1013
0
    BIO *chain = NULL;
1014
1015
0
    sd = cms_get0_signed(cms);
1016
0
    if (sd == NULL)
1017
0
        return NULL;
1018
0
    if (cms->d.signedData->encapContentInfo->partial)
1019
0
        cms_sd_set_version(sd);
1020
0
    for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++) {
1021
0
        X509_ALGOR *digestAlgorithm;
1022
0
        BIO *mdbio;
1023
1024
0
        digestAlgorithm = sk_X509_ALGOR_value(sd->digestAlgorithms, i);
1025
0
        mdbio = ossl_cms_DigestAlgorithm_init_bio(digestAlgorithm,
1026
0
                                                  ossl_cms_get0_cmsctx(cms));
1027
0
        if (mdbio == NULL)
1028
0
            goto err;
1029
0
        if (chain != NULL)
1030
0
            BIO_push(chain, mdbio);
1031
0
        else
1032
0
            chain = mdbio;
1033
0
    }
1034
0
    return chain;
1035
0
 err:
1036
0
    BIO_free_all(chain);
1037
0
    return NULL;
1038
0
}
1039
1040
int CMS_SignerInfo_verify_content(CMS_SignerInfo *si, BIO *chain)
1041
0
{
1042
0
    ASN1_OCTET_STRING *os = NULL;
1043
0
    EVP_MD_CTX *mctx = EVP_MD_CTX_new();
1044
0
    EVP_PKEY_CTX *pkctx = NULL;
1045
0
    int r = -1;
1046
0
    unsigned char mval[EVP_MAX_MD_SIZE];
1047
0
    unsigned int mlen;
1048
1049
0
    if (mctx == NULL) {
1050
0
        ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
1051
0
        goto err;
1052
0
    }
1053
    /* If we have any signed attributes look for messageDigest value */
1054
0
    if (CMS_signed_get_attr_count(si) >= 0) {
1055
0
        os = CMS_signed_get0_data_by_OBJ(si,
1056
0
                                         OBJ_nid2obj(NID_pkcs9_messageDigest),
1057
0
                                         -3, V_ASN1_OCTET_STRING);
1058
0
        if (os == NULL) {
1059
0
            ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE);
1060
0
            goto err;
1061
0
        }
1062
0
    }
1063
1064
0
    if (!ossl_cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm))
1065
0
        goto err;
1066
1067
0
    if (EVP_DigestFinal_ex(mctx, mval, &mlen) <= 0) {
1068
0
        ERR_raise(ERR_LIB_CMS, CMS_R_UNABLE_TO_FINALIZE_CONTEXT);
1069
0
        goto err;
1070
0
    }
1071
1072
    /* If messageDigest found compare it */
1073
0
    if (os != NULL) {
1074
0
        if (mlen != (unsigned int)os->length) {
1075
0
            ERR_raise(ERR_LIB_CMS, CMS_R_MESSAGEDIGEST_ATTRIBUTE_WRONG_LENGTH);
1076
0
            goto err;
1077
0
        }
1078
1079
0
        if (memcmp(mval, os->data, mlen)) {
1080
0
            ERR_raise(ERR_LIB_CMS, CMS_R_VERIFICATION_FAILURE);
1081
0
            r = 0;
1082
0
        } else {
1083
0
            r = 1;
1084
0
        }
1085
0
    } else {
1086
0
        const EVP_MD *md = EVP_MD_CTX_get0_md(mctx);
1087
0
        const CMS_CTX *ctx = si->cms_ctx;
1088
1089
0
        pkctx = EVP_PKEY_CTX_new_from_pkey(ossl_cms_ctx_get0_libctx(ctx),
1090
0
                                           si->pkey,
1091
0
                                           ossl_cms_ctx_get0_propq(ctx));
1092
0
        if (pkctx == NULL)
1093
0
            goto err;
1094
0
        if (EVP_PKEY_verify_init(pkctx) <= 0)
1095
0
            goto err;
1096
0
        if (EVP_PKEY_CTX_set_signature_md(pkctx, md) <= 0)
1097
0
            goto err;
1098
0
        si->pctx = pkctx;
1099
0
        if (!cms_sd_asn1_ctrl(si, 1)) {
1100
0
            si->pctx = NULL;
1101
0
            goto err;
1102
0
        }
1103
0
        si->pctx = NULL;
1104
0
        r = EVP_PKEY_verify(pkctx, si->signature->data,
1105
0
                            si->signature->length, mval, mlen);
1106
0
        if (r <= 0) {
1107
0
            ERR_raise(ERR_LIB_CMS, CMS_R_VERIFICATION_FAILURE);
1108
0
            r = 0;
1109
0
        }
1110
0
    }
1111
1112
0
 err:
1113
0
    EVP_PKEY_CTX_free(pkctx);
1114
0
    EVP_MD_CTX_free(mctx);
1115
0
    return r;
1116
1117
0
}
1118
1119
BIO *CMS_SignedData_verify(CMS_SignedData *sd, BIO *detached_data,
1120
                           STACK_OF(X509) *scerts, X509_STORE *store,
1121
                           STACK_OF(X509) *extra, STACK_OF(X509_CRL) *crls,
1122
                           unsigned int flags,
1123
                           OSSL_LIB_CTX *libctx, const char *propq)
1124
0
{
1125
0
    CMS_ContentInfo *ci;
1126
0
    BIO *bio = NULL;
1127
0
    int i, res = 0;
1128
1129
0
    if (sd == NULL) {
1130
0
        ERR_raise(ERR_LIB_CMS, ERR_R_PASSED_NULL_PARAMETER);
1131
0
        return NULL;
1132
0
    }
1133
1134
0
    if ((ci = CMS_ContentInfo_new_ex(libctx, propq)) == NULL)
1135
0
        return NULL;
1136
0
    if ((bio = BIO_new(BIO_s_mem())) == NULL)
1137
0
        goto end;
1138
0
    ci->contentType = OBJ_nid2obj(NID_pkcs7_signed);
1139
0
    ci->d.signedData = sd;
1140
1141
0
    for (i = 0; i < sk_X509_num(extra); i++)
1142
0
        if (!CMS_add1_cert(ci, sk_X509_value(extra, i)))
1143
0
            goto end;
1144
0
    for (i = 0; i < sk_X509_CRL_num(crls); i++)
1145
0
        if (!CMS_add1_crl(ci, sk_X509_CRL_value(crls, i)))
1146
0
            goto end;
1147
0
    res = CMS_verify(ci, scerts, store, detached_data, bio, flags);
1148
1149
0
 end:
1150
0
    if (ci != NULL)
1151
0
        ci->d.signedData = NULL; /* do not indirectly free |sd| */
1152
0
    CMS_ContentInfo_free(ci);
1153
0
    if (!res) {
1154
0
        BIO_free(bio);
1155
0
        bio = NULL;
1156
0
    }
1157
0
    return bio;
1158
0
}
1159
1160
int CMS_add_smimecap(CMS_SignerInfo *si, STACK_OF(X509_ALGOR) *algs)
1161
0
{
1162
0
    unsigned char *smder = NULL;
1163
0
    int smderlen, r;
1164
1165
0
    smderlen = i2d_X509_ALGORS(algs, &smder);
1166
0
    if (smderlen <= 0)
1167
0
        return 0;
1168
0
    r = CMS_signed_add1_attr_by_NID(si, NID_SMIMECapabilities,
1169
0
                                    V_ASN1_SEQUENCE, smder, smderlen);
1170
0
    OPENSSL_free(smder);
1171
0
    return r;
1172
0
}
1173
1174
int CMS_add_simple_smimecap(STACK_OF(X509_ALGOR) **algs,
1175
                            int algnid, int keysize)
1176
0
{
1177
0
    X509_ALGOR *alg;
1178
0
    ASN1_INTEGER *key = NULL;
1179
1180
0
    if (keysize > 0) {
1181
0
        key = ASN1_INTEGER_new();
1182
0
        if (key == NULL || !ASN1_INTEGER_set(key, keysize)) {
1183
0
            ASN1_INTEGER_free(key);
1184
0
            return 0;
1185
0
        }
1186
0
    }
1187
0
    alg = ossl_X509_ALGOR_from_nid(algnid, key != NULL ? V_ASN1_INTEGER :
1188
0
                                   V_ASN1_UNDEF, key);
1189
0
    if (alg == NULL) {
1190
0
        ASN1_INTEGER_free(key);
1191
0
        return 0;
1192
0
    }
1193
1194
0
    if (*algs == NULL)
1195
0
        *algs = sk_X509_ALGOR_new_null();
1196
0
    if (*algs == NULL || !sk_X509_ALGOR_push(*algs, alg)) {
1197
0
        X509_ALGOR_free(alg);
1198
0
        return 0;
1199
0
    }
1200
0
    return 1;
1201
0
}
1202
1203
/* Check to see if a cipher exists and if so add S/MIME capabilities */
1204
static int cms_add_cipher_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg)
1205
0
{
1206
0
    if (EVP_get_cipherbynid(nid))
1207
0
        return CMS_add_simple_smimecap(sk, nid, arg);
1208
0
    return 1;
1209
0
}
1210
1211
static int cms_add_digest_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg)
1212
0
{
1213
0
    if (EVP_get_digestbynid(nid))
1214
0
        return CMS_add_simple_smimecap(sk, nid, arg);
1215
0
    return 1;
1216
0
}
1217
1218
int CMS_add_standard_smimecap(STACK_OF(X509_ALGOR) **smcap)
1219
0
{
1220
0
    if (!cms_add_cipher_smcap(smcap, NID_aes_256_cbc, -1)
1221
0
        || !cms_add_digest_smcap(smcap, NID_id_GostR3411_2012_256, -1)
1222
0
        || !cms_add_digest_smcap(smcap, NID_id_GostR3411_2012_512, -1)
1223
0
        || !cms_add_digest_smcap(smcap, NID_id_GostR3411_94, -1)
1224
0
        || !cms_add_cipher_smcap(smcap, NID_id_Gost28147_89, -1)
1225
0
        || !cms_add_cipher_smcap(smcap, NID_aes_192_cbc, -1)
1226
0
        || !cms_add_cipher_smcap(smcap, NID_aes_128_cbc, -1)
1227
0
        || !cms_add_cipher_smcap(smcap, NID_des_ede3_cbc, -1)
1228
0
        || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 128)
1229
0
        || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 64)
1230
0
        || !cms_add_cipher_smcap(smcap, NID_des_cbc, -1)
1231
0
        || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 40))
1232
0
        return 0;
1233
0
    return 1;
1234
0
}