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