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