]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
454afd98 | 2 | * Copyright 2008-2020 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> |
706457b7 | 18 | #include "cms_local.h" |
25f2138b DMSP |
19 | #include "crypto/asn1.h" |
20 | #include "crypto/evp.h" | |
21 | #include "crypto/cms.h" | |
22 | #include "crypto/ess.h" | |
eeccc237 | 23 | #include "crypto/x509.h" /* for X509_add_cert_new() */ |
8931b30d | 24 | |
852c2ed2 RS |
25 | DEFINE_STACK_OF(CMS_RevocationInfoChoice) |
26 | DEFINE_STACK_OF(CMS_SignerInfo) | |
852c2ed2 | 27 | |
8931b30d DSH |
28 | /* CMS SignedData Utilities */ |
29 | ||
8931b30d | 30 | static CMS_SignedData *cms_get0_signed(CMS_ContentInfo *cms) |
0f113f3e MC |
31 | { |
32 | if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_signed) { | |
33 | CMSerr(CMS_F_CMS_GET0_SIGNED, CMS_R_CONTENT_TYPE_NOT_SIGNED_DATA); | |
34 | return NULL; | |
35 | } | |
36 | return cms->d.signedData; | |
37 | } | |
8931b30d DSH |
38 | |
39 | static CMS_SignedData *cms_signed_data_init(CMS_ContentInfo *cms) | |
0f113f3e MC |
40 | { |
41 | if (cms->d.other == NULL) { | |
42 | cms->d.signedData = M_ASN1_new_of(CMS_SignedData); | |
43 | if (!cms->d.signedData) { | |
44 | CMSerr(CMS_F_CMS_SIGNED_DATA_INIT, ERR_R_MALLOC_FAILURE); | |
45 | return NULL; | |
46 | } | |
47 | cms->d.signedData->version = 1; | |
48 | cms->d.signedData->encapContentInfo->eContentType = | |
49 | OBJ_nid2obj(NID_pkcs7_data); | |
50 | cms->d.signedData->encapContentInfo->partial = 1; | |
51 | ASN1_OBJECT_free(cms->contentType); | |
52 | cms->contentType = OBJ_nid2obj(NID_pkcs7_signed); | |
53 | return cms->d.signedData; | |
54 | } | |
55 | return cms_get0_signed(cms); | |
56 | } | |
8931b30d | 57 | |
0d4fb843 | 58 | /* Just initialise SignedData e.g. for certs only structure */ |
8931b30d DSH |
59 | |
60 | int CMS_SignedData_init(CMS_ContentInfo *cms) | |
0f113f3e MC |
61 | { |
62 | if (cms_signed_data_init(cms)) | |
63 | return 1; | |
64 | else | |
65 | return 0; | |
66 | } | |
8931b30d | 67 | |
c1669f41 | 68 | |
8931b30d DSH |
69 | /* Check structures and fixup version numbers (if necessary) */ |
70 | ||
71 | static void cms_sd_set_version(CMS_SignedData *sd) | |
0f113f3e MC |
72 | { |
73 | int i; | |
74 | CMS_CertificateChoices *cch; | |
75 | CMS_RevocationInfoChoice *rch; | |
76 | CMS_SignerInfo *si; | |
77 | ||
78 | for (i = 0; i < sk_CMS_CertificateChoices_num(sd->certificates); i++) { | |
79 | cch = sk_CMS_CertificateChoices_value(sd->certificates, i); | |
80 | if (cch->type == CMS_CERTCHOICE_OTHER) { | |
81 | if (sd->version < 5) | |
82 | sd->version = 5; | |
83 | } else if (cch->type == CMS_CERTCHOICE_V2ACERT) { | |
84 | if (sd->version < 4) | |
85 | sd->version = 4; | |
86 | } else if (cch->type == CMS_CERTCHOICE_V1ACERT) { | |
87 | if (sd->version < 3) | |
88 | sd->version = 3; | |
89 | } | |
90 | } | |
91 | ||
92 | for (i = 0; i < sk_CMS_RevocationInfoChoice_num(sd->crls); i++) { | |
93 | rch = sk_CMS_RevocationInfoChoice_value(sd->crls, i); | |
94 | if (rch->type == CMS_REVCHOICE_OTHER) { | |
95 | if (sd->version < 5) | |
96 | sd->version = 5; | |
97 | } | |
98 | } | |
99 | ||
100 | if ((OBJ_obj2nid(sd->encapContentInfo->eContentType) != NID_pkcs7_data) | |
101 | && (sd->version < 3)) | |
102 | sd->version = 3; | |
103 | ||
104 | for (i = 0; i < sk_CMS_SignerInfo_num(sd->signerInfos); i++) { | |
105 | si = sk_CMS_SignerInfo_value(sd->signerInfos, i); | |
106 | if (si->sid->type == CMS_SIGNERINFO_KEYIDENTIFIER) { | |
107 | if (si->version < 3) | |
108 | si->version = 3; | |
109 | if (sd->version < 3) | |
110 | sd->version = 3; | |
111 | } else if (si->version < 1) | |
112 | si->version = 1; | |
113 | } | |
114 | ||
115 | if (sd->version < 1) | |
116 | sd->version = 1; | |
117 | ||
118 | } | |
119 | ||
19e512a8 SL |
120 | /* |
121 | * RFC 5652 Section 11.1 Content Type | |
122 | * The content-type attribute within signed-data MUST | |
123 | * 1) be present if there are signed attributes | |
124 | * 2) match the content type in the signed-data, | |
125 | * 3) be a signed attribute. | |
126 | * 4) not have more than one copy of the attribute. | |
127 | * | |
128 | * Note that since the CMS_SignerInfo_sign() always adds the "signing time" | |
129 | * attribute, the content type attribute MUST be added also. | |
130 | * Assumptions: This assumes that the attribute does not already exist. | |
131 | */ | |
132 | static int cms_set_si_contentType_attr(CMS_ContentInfo *cms, CMS_SignerInfo *si) | |
133 | { | |
134 | ASN1_OBJECT *ctype = cms->d.signedData->encapContentInfo->eContentType; | |
135 | ||
136 | /* Add the contentType attribute */ | |
137 | return CMS_signed_add1_attr_by_NID(si, NID_pkcs9_contentType, | |
138 | V_ASN1_OBJECT, ctype, -1) > 0; | |
139 | } | |
140 | ||
8931b30d DSH |
141 | /* Copy an existing messageDigest value */ |
142 | ||
143 | static int cms_copy_messageDigest(CMS_ContentInfo *cms, CMS_SignerInfo *si) | |
0f113f3e MC |
144 | { |
145 | STACK_OF(CMS_SignerInfo) *sinfos; | |
146 | CMS_SignerInfo *sitmp; | |
147 | int i; | |
c1669f41 | 148 | |
0f113f3e MC |
149 | sinfos = CMS_get0_SignerInfos(cms); |
150 | for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) { | |
151 | ASN1_OCTET_STRING *messageDigest; | |
c1669f41 | 152 | |
0f113f3e MC |
153 | sitmp = sk_CMS_SignerInfo_value(sinfos, i); |
154 | if (sitmp == si) | |
155 | continue; | |
156 | if (CMS_signed_get_attr_count(sitmp) < 0) | |
157 | continue; | |
158 | if (OBJ_cmp(si->digestAlgorithm->algorithm, | |
159 | sitmp->digestAlgorithm->algorithm)) | |
160 | continue; | |
161 | messageDigest = CMS_signed_get0_data_by_OBJ(sitmp, | |
162 | OBJ_nid2obj | |
163 | (NID_pkcs9_messageDigest), | |
164 | -3, V_ASN1_OCTET_STRING); | |
165 | if (!messageDigest) { | |
166 | CMSerr(CMS_F_CMS_COPY_MESSAGEDIGEST, | |
167 | CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE); | |
168 | return 0; | |
169 | } | |
170 | ||
171 | if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest, | |
172 | V_ASN1_OCTET_STRING, | |
173 | messageDigest, -1)) | |
174 | return 1; | |
175 | else | |
176 | return 0; | |
177 | } | |
178 | CMSerr(CMS_F_CMS_COPY_MESSAGEDIGEST, CMS_R_NO_MATCHING_DIGEST); | |
179 | return 0; | |
180 | } | |
8931b30d | 181 | |
c1669f41 SL |
182 | int cms_set1_SignerIdentifier(CMS_SignerIdentifier *sid, X509 *cert, int type, |
183 | const CMS_CTX *ctx) | |
0f113f3e MC |
184 | { |
185 | switch (type) { | |
186 | case CMS_SIGNERINFO_ISSUER_SERIAL: | |
187 | if (!cms_set1_ias(&sid->d.issuerAndSerialNumber, cert)) | |
188 | return 0; | |
189 | break; | |
8931b30d | 190 | |
0f113f3e MC |
191 | case CMS_SIGNERINFO_KEYIDENTIFIER: |
192 | if (!cms_set1_keyid(&sid->d.subjectKeyIdentifier, cert)) | |
193 | return 0; | |
194 | break; | |
8931b30d | 195 | |
0f113f3e MC |
196 | default: |
197 | CMSerr(CMS_F_CMS_SET1_SIGNERIDENTIFIER, CMS_R_UNKNOWN_ID); | |
198 | return 0; | |
199 | } | |
8931b30d | 200 | |
0f113f3e | 201 | sid->type = type; |
8931b30d | 202 | |
0f113f3e MC |
203 | return 1; |
204 | } | |
8931b30d DSH |
205 | |
206 | int cms_SignerIdentifier_get0_signer_id(CMS_SignerIdentifier *sid, | |
0f113f3e MC |
207 | ASN1_OCTET_STRING **keyid, |
208 | X509_NAME **issuer, | |
209 | ASN1_INTEGER **sno) | |
210 | { | |
211 | if (sid->type == CMS_SIGNERINFO_ISSUER_SERIAL) { | |
212 | if (issuer) | |
213 | *issuer = sid->d.issuerAndSerialNumber->issuer; | |
214 | if (sno) | |
215 | *sno = sid->d.issuerAndSerialNumber->serialNumber; | |
216 | } else if (sid->type == CMS_SIGNERINFO_KEYIDENTIFIER) { | |
217 | if (keyid) | |
218 | *keyid = sid->d.subjectKeyIdentifier; | |
219 | } else | |
220 | return 0; | |
221 | return 1; | |
222 | } | |
8931b30d DSH |
223 | |
224 | int cms_SignerIdentifier_cert_cmp(CMS_SignerIdentifier *sid, X509 *cert) | |
0f113f3e MC |
225 | { |
226 | if (sid->type == CMS_SIGNERINFO_ISSUER_SERIAL) | |
227 | return cms_ias_cert_cmp(sid->d.issuerAndSerialNumber, cert); | |
228 | else if (sid->type == CMS_SIGNERINFO_KEYIDENTIFIER) | |
229 | return cms_keyid_cert_cmp(sid->d.subjectKeyIdentifier, cert); | |
230 | else | |
231 | return -1; | |
232 | } | |
8931b30d | 233 | |
e365352d | 234 | static int cms_sd_asn1_ctrl(CMS_SignerInfo *si, int cmd) |
0f113f3e MC |
235 | { |
236 | EVP_PKEY *pkey = si->pkey; | |
237 | int i; | |
c1669f41 | 238 | |
12a765a5 | 239 | if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL) |
0f113f3e MC |
240 | return 1; |
241 | i = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_CMS_SIGN, cmd, si); | |
242 | if (i == -2) { | |
243 | CMSerr(CMS_F_CMS_SD_ASN1_CTRL, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE); | |
244 | return 0; | |
245 | } | |
246 | if (i <= 0) { | |
247 | CMSerr(CMS_F_CMS_SD_ASN1_CTRL, CMS_R_CTRL_FAILURE); | |
248 | return 0; | |
249 | } | |
250 | return 1; | |
251 | } | |
e365352d | 252 | |
8931b30d | 253 | CMS_SignerInfo *CMS_add1_signer(CMS_ContentInfo *cms, |
0f113f3e MC |
254 | X509 *signer, EVP_PKEY *pk, const EVP_MD *md, |
255 | unsigned int flags) | |
256 | { | |
257 | CMS_SignedData *sd; | |
258 | CMS_SignerInfo *si = NULL; | |
259 | X509_ALGOR *alg; | |
260 | int i, type; | |
c1669f41 SL |
261 | const CMS_CTX *ctx = cms_get0_cmsctx(cms); |
262 | ||
0f113f3e MC |
263 | if (!X509_check_private_key(signer, pk)) { |
264 | CMSerr(CMS_F_CMS_ADD1_SIGNER, | |
265 | CMS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE); | |
266 | return NULL; | |
267 | } | |
268 | sd = cms_signed_data_init(cms); | |
269 | if (!sd) | |
270 | goto err; | |
271 | si = M_ASN1_new_of(CMS_SignerInfo); | |
272 | if (!si) | |
273 | goto merr; | |
109f8b5d | 274 | /* Call for side-effect of computing hash and caching extensions */ |
0f113f3e MC |
275 | X509_check_purpose(signer, -1, -1); |
276 | ||
05f0fb9f | 277 | X509_up_ref(signer); |
03273d61 | 278 | EVP_PKEY_up_ref(pk); |
0f113f3e | 279 | |
c1669f41 | 280 | si->cms_ctx = ctx; |
0f113f3e MC |
281 | si->pkey = pk; |
282 | si->signer = signer; | |
bfb0641f | 283 | si->mctx = EVP_MD_CTX_new(); |
0f113f3e MC |
284 | si->pctx = NULL; |
285 | ||
6e59a892 RL |
286 | if (si->mctx == NULL) { |
287 | CMSerr(CMS_F_CMS_ADD1_SIGNER, ERR_R_MALLOC_FAILURE); | |
288 | goto err; | |
289 | } | |
290 | ||
0f113f3e MC |
291 | if (flags & CMS_USE_KEYID) { |
292 | si->version = 3; | |
293 | if (sd->version < 3) | |
294 | sd->version = 3; | |
295 | type = CMS_SIGNERINFO_KEYIDENTIFIER; | |
296 | } else { | |
297 | type = CMS_SIGNERINFO_ISSUER_SERIAL; | |
298 | si->version = 1; | |
299 | } | |
300 | ||
c1669f41 | 301 | if (!cms_set1_SignerIdentifier(si->sid, signer, type, ctx)) |
0f113f3e MC |
302 | goto err; |
303 | ||
304 | if (md == NULL) { | |
305 | int def_nid; | |
306 | if (EVP_PKEY_get_default_digest_nid(pk, &def_nid) <= 0) | |
307 | goto err; | |
308 | md = EVP_get_digestbynid(def_nid); | |
309 | if (md == NULL) { | |
310 | CMSerr(CMS_F_CMS_ADD1_SIGNER, CMS_R_NO_DEFAULT_DIGEST); | |
311 | goto err; | |
312 | } | |
313 | } | |
314 | ||
315 | if (!md) { | |
316 | CMSerr(CMS_F_CMS_ADD1_SIGNER, CMS_R_NO_DIGEST_SET); | |
317 | goto err; | |
318 | } | |
319 | ||
c1669f41 SL |
320 | if (md == NULL) { |
321 | CMSerr(CMS_F_CMS_ADD1_SIGNER, CMS_R_NO_DIGEST_SET); | |
322 | goto err; | |
323 | } | |
324 | ||
0f113f3e MC |
325 | X509_ALGOR_set_md(si->digestAlgorithm, md); |
326 | ||
327 | /* See if digest is present in digestAlgorithms */ | |
328 | for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++) { | |
ac4e2577 | 329 | const ASN1_OBJECT *aoid; |
0f113f3e MC |
330 | alg = sk_X509_ALGOR_value(sd->digestAlgorithms, i); |
331 | X509_ALGOR_get0(&aoid, NULL, NULL, alg); | |
332 | if (OBJ_obj2nid(aoid) == EVP_MD_type(md)) | |
333 | break; | |
334 | } | |
335 | ||
336 | if (i == sk_X509_ALGOR_num(sd->digestAlgorithms)) { | |
337 | alg = X509_ALGOR_new(); | |
90945fa3 | 338 | if (alg == NULL) |
0f113f3e MC |
339 | goto merr; |
340 | X509_ALGOR_set_md(alg, md); | |
341 | if (!sk_X509_ALGOR_push(sd->digestAlgorithms, alg)) { | |
342 | X509_ALGOR_free(alg); | |
343 | goto merr; | |
344 | } | |
345 | } | |
346 | ||
347 | if (!(flags & CMS_KEY_PARAM) && !cms_sd_asn1_ctrl(si, 0)) | |
348 | goto err; | |
349 | if (!(flags & CMS_NOATTR)) { | |
350 | /* | |
0d4fb843 | 351 | * Initialize signed attributes structure so other attributes |
0f113f3e MC |
352 | * such as signing time etc are added later even if we add none here. |
353 | */ | |
354 | if (!si->signedAttrs) { | |
355 | si->signedAttrs = sk_X509_ATTRIBUTE_new_null(); | |
356 | if (!si->signedAttrs) | |
357 | goto merr; | |
358 | } | |
359 | ||
360 | if (!(flags & CMS_NOSMIMECAP)) { | |
361 | STACK_OF(X509_ALGOR) *smcap = NULL; | |
362 | i = CMS_add_standard_smimecap(&smcap); | |
363 | if (i) | |
364 | i = CMS_add_smimecap(si, smcap); | |
365 | sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free); | |
366 | if (!i) | |
367 | goto merr; | |
368 | } | |
e85d19c6 AI |
369 | if (flags & CMS_CADES) { |
370 | ESS_SIGNING_CERT *sc = NULL; | |
371 | ESS_SIGNING_CERT_V2 *sc2 = NULL; | |
372 | int add_sc; | |
373 | ||
374 | if (md == EVP_sha1() || md == NULL) { | |
375 | if ((sc = ESS_SIGNING_CERT_new_init(signer, | |
376 | NULL, 1)) == NULL) | |
377 | goto err; | |
8c00f267 | 378 | add_sc = cms_add1_signing_cert(si, sc); |
e85d19c6 AI |
379 | ESS_SIGNING_CERT_free(sc); |
380 | } else { | |
381 | if ((sc2 = ESS_SIGNING_CERT_V2_new_init(md, signer, | |
382 | NULL, 1)) == NULL) | |
383 | goto err; | |
8c00f267 | 384 | add_sc = cms_add1_signing_cert_v2(si, sc2); |
e85d19c6 AI |
385 | ESS_SIGNING_CERT_V2_free(sc2); |
386 | } | |
387 | if (!add_sc) | |
388 | goto err; | |
389 | } | |
8c89c80a F |
390 | if (flags & CMS_REUSE_DIGEST) { |
391 | if (!cms_copy_messageDigest(cms, si)) | |
392 | goto err; | |
19e512a8 SL |
393 | if (!cms_set_si_contentType_attr(cms, si)) |
394 | goto err; | |
8c89c80a F |
395 | if (!(flags & (CMS_PARTIAL | CMS_KEY_PARAM)) && |
396 | !CMS_SignerInfo_sign(si)) | |
397 | goto err; | |
398 | } | |
0f113f3e MC |
399 | } |
400 | ||
401 | if (!(flags & CMS_NOCERTS)) { | |
402 | /* NB ignore -1 return for duplicate cert */ | |
403 | if (!CMS_add1_cert(cms, signer)) | |
404 | goto merr; | |
405 | } | |
406 | ||
407 | if (flags & CMS_KEY_PARAM) { | |
408 | if (flags & CMS_NOATTR) { | |
c1669f41 SL |
409 | si->pctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, si->pkey, |
410 | ctx->propq); | |
90945fa3 | 411 | if (si->pctx == NULL) |
0f113f3e MC |
412 | goto err; |
413 | if (EVP_PKEY_sign_init(si->pctx) <= 0) | |
414 | goto err; | |
415 | if (EVP_PKEY_CTX_set_signature_md(si->pctx, md) <= 0) | |
416 | goto err; | |
c1669f41 SL |
417 | } else if (EVP_DigestSignInit_with_libctx(si->mctx, &si->pctx, |
418 | EVP_MD_name(md), | |
419 | ctx->libctx, ctx->propq, | |
420 | pk) <= 0) { | |
0f113f3e | 421 | goto err; |
c1669f41 | 422 | } |
0f113f3e MC |
423 | } |
424 | ||
425 | if (!sd->signerInfos) | |
426 | sd->signerInfos = sk_CMS_SignerInfo_new_null(); | |
427 | if (!sd->signerInfos || !sk_CMS_SignerInfo_push(sd->signerInfos, si)) | |
428 | goto merr; | |
429 | ||
430 | return si; | |
431 | ||
432 | merr: | |
433 | CMSerr(CMS_F_CMS_ADD1_SIGNER, ERR_R_MALLOC_FAILURE); | |
434 | err: | |
2ace7450 | 435 | M_ASN1_free_of(si, CMS_SignerInfo); |
0f113f3e MC |
436 | return NULL; |
437 | ||
438 | } | |
8931b30d | 439 | |
c1669f41 SL |
440 | void cms_SignerInfos_set_cmsctx(CMS_ContentInfo *cms) |
441 | { | |
442 | int i; | |
443 | CMS_SignerInfo *si; | |
444 | STACK_OF(CMS_SignerInfo) *sinfos = CMS_get0_SignerInfos(cms); | |
445 | const CMS_CTX *ctx = cms_get0_cmsctx(cms); | |
446 | ||
447 | for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) { | |
448 | si = sk_CMS_SignerInfo_value(sinfos, i); | |
449 | if (si != NULL) | |
450 | si->cms_ctx = ctx; | |
451 | } | |
452 | } | |
453 | ||
1e26a8ba | 454 | static int cms_add1_signingTime(CMS_SignerInfo *si, ASN1_TIME *t) |
0f113f3e MC |
455 | { |
456 | ASN1_TIME *tt; | |
457 | int r = 0; | |
c1669f41 SL |
458 | |
459 | if (t != NULL) | |
0f113f3e MC |
460 | tt = t; |
461 | else | |
462 | tt = X509_gmtime_adj(NULL, 0); | |
8931b30d | 463 | |
c1669f41 | 464 | if (tt == NULL) |
0f113f3e | 465 | goto merr; |
8931b30d | 466 | |
0f113f3e MC |
467 | if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_signingTime, |
468 | tt->type, tt, -1) <= 0) | |
469 | goto merr; | |
8931b30d | 470 | |
0f113f3e | 471 | r = 1; |
0f113f3e | 472 | merr: |
c1669f41 | 473 | if (t == NULL) |
0f113f3e | 474 | ASN1_TIME_free(tt); |
8931b30d | 475 | |
0f113f3e MC |
476 | if (!r) |
477 | CMSerr(CMS_F_CMS_ADD1_SIGNINGTIME, ERR_R_MALLOC_FAILURE); | |
8931b30d | 478 | |
0f113f3e | 479 | return r; |
8931b30d | 480 | |
0f113f3e | 481 | } |
8931b30d | 482 | |
e365352d | 483 | EVP_PKEY_CTX *CMS_SignerInfo_get0_pkey_ctx(CMS_SignerInfo *si) |
0f113f3e MC |
484 | { |
485 | return si->pctx; | |
486 | } | |
e365352d DSH |
487 | |
488 | EVP_MD_CTX *CMS_SignerInfo_get0_md_ctx(CMS_SignerInfo *si) | |
0f113f3e | 489 | { |
6e59a892 | 490 | return si->mctx; |
0f113f3e | 491 | } |
e365352d | 492 | |
8931b30d | 493 | STACK_OF(CMS_SignerInfo) *CMS_get0_SignerInfos(CMS_ContentInfo *cms) |
0f113f3e | 494 | { |
c1669f41 SL |
495 | CMS_SignedData *sd = cms_get0_signed(cms); |
496 | ||
497 | return sd != NULL ? sd->signerInfos : NULL; | |
0f113f3e | 498 | } |
8931b30d DSH |
499 | |
500 | STACK_OF(X509) *CMS_get0_signers(CMS_ContentInfo *cms) | |
0f113f3e MC |
501 | { |
502 | STACK_OF(X509) *signers = NULL; | |
503 | STACK_OF(CMS_SignerInfo) *sinfos; | |
504 | CMS_SignerInfo *si; | |
505 | int i; | |
c1669f41 | 506 | |
0f113f3e MC |
507 | sinfos = CMS_get0_SignerInfos(cms); |
508 | for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) { | |
509 | si = sk_CMS_SignerInfo_value(sinfos, i); | |
c1669f41 | 510 | if (si->signer != NULL) { |
eeccc237 DDO |
511 | if (!X509_add_cert_new(&signers, si->signer, |
512 | X509_ADD_FLAG_DEFAULT)) { | |
0f113f3e MC |
513 | sk_X509_free(signers); |
514 | return NULL; | |
515 | } | |
516 | } | |
517 | } | |
518 | return signers; | |
519 | } | |
8931b30d DSH |
520 | |
521 | void CMS_SignerInfo_set1_signer_cert(CMS_SignerInfo *si, X509 *signer) | |
0f113f3e | 522 | { |
c1669f41 | 523 | if (signer != NULL) { |
05f0fb9f | 524 | X509_up_ref(signer); |
c5ba2d99 | 525 | EVP_PKEY_free(si->pkey); |
0f113f3e MC |
526 | si->pkey = X509_get_pubkey(signer); |
527 | } | |
222561fe | 528 | X509_free(si->signer); |
0f113f3e MC |
529 | si->signer = signer; |
530 | } | |
8931b30d DSH |
531 | |
532 | int CMS_SignerInfo_get0_signer_id(CMS_SignerInfo *si, | |
0f113f3e MC |
533 | ASN1_OCTET_STRING **keyid, |
534 | X509_NAME **issuer, ASN1_INTEGER **sno) | |
535 | { | |
536 | return cms_SignerIdentifier_get0_signer_id(si->sid, keyid, issuer, sno); | |
537 | } | |
8931b30d DSH |
538 | |
539 | int CMS_SignerInfo_cert_cmp(CMS_SignerInfo *si, X509 *cert) | |
0f113f3e MC |
540 | { |
541 | return cms_SignerIdentifier_cert_cmp(si->sid, cert); | |
542 | } | |
8931b30d DSH |
543 | |
544 | int CMS_set1_signers_certs(CMS_ContentInfo *cms, STACK_OF(X509) *scerts, | |
0f113f3e MC |
545 | unsigned int flags) |
546 | { | |
547 | CMS_SignedData *sd; | |
548 | CMS_SignerInfo *si; | |
549 | CMS_CertificateChoices *cch; | |
550 | STACK_OF(CMS_CertificateChoices) *certs; | |
551 | X509 *x; | |
552 | int i, j; | |
553 | int ret = 0; | |
c1669f41 | 554 | |
0f113f3e | 555 | sd = cms_get0_signed(cms); |
c1669f41 | 556 | if (sd == NULL) |
0f113f3e MC |
557 | return -1; |
558 | certs = sd->certificates; | |
559 | for (i = 0; i < sk_CMS_SignerInfo_num(sd->signerInfos); i++) { | |
560 | si = sk_CMS_SignerInfo_value(sd->signerInfos, i); | |
c1669f41 | 561 | if (si->signer != NULL) |
0f113f3e MC |
562 | continue; |
563 | ||
564 | for (j = 0; j < sk_X509_num(scerts); j++) { | |
565 | x = sk_X509_value(scerts, j); | |
566 | if (CMS_SignerInfo_cert_cmp(si, x) == 0) { | |
567 | CMS_SignerInfo_set1_signer_cert(si, x); | |
568 | ret++; | |
569 | break; | |
570 | } | |
571 | } | |
572 | ||
c1669f41 | 573 | if (si->signer != NULL || (flags & CMS_NOINTERN)) |
0f113f3e MC |
574 | continue; |
575 | ||
576 | for (j = 0; j < sk_CMS_CertificateChoices_num(certs); j++) { | |
577 | cch = sk_CMS_CertificateChoices_value(certs, j); | |
578 | if (cch->type != 0) | |
579 | continue; | |
580 | x = cch->d.certificate; | |
581 | if (CMS_SignerInfo_cert_cmp(si, x) == 0) { | |
582 | CMS_SignerInfo_set1_signer_cert(si, x); | |
583 | ret++; | |
584 | break; | |
585 | } | |
586 | } | |
587 | } | |
588 | return ret; | |
589 | } | |
590 | ||
591 | void CMS_SignerInfo_get0_algs(CMS_SignerInfo *si, EVP_PKEY **pk, | |
592 | X509 **signer, X509_ALGOR **pdig, | |
593 | X509_ALGOR **psig) | |
594 | { | |
c1669f41 | 595 | if (pk != NULL) |
0f113f3e | 596 | *pk = si->pkey; |
c1669f41 | 597 | if (signer != NULL) |
0f113f3e | 598 | *signer = si->signer; |
c1669f41 | 599 | if (pdig != NULL) |
0f113f3e | 600 | *pdig = si->digestAlgorithm; |
c1669f41 | 601 | if (psig != NULL) |
0f113f3e MC |
602 | *psig = si->signatureAlgorithm; |
603 | } | |
8931b30d | 604 | |
da15c616 | 605 | ASN1_OCTET_STRING *CMS_SignerInfo_get0_signature(CMS_SignerInfo *si) |
0f113f3e MC |
606 | { |
607 | return si->signature; | |
608 | } | |
da15c616 | 609 | |
ff80280b | 610 | static int cms_SignerInfo_content_sign(CMS_ContentInfo *cms, |
0f113f3e MC |
611 | CMS_SignerInfo *si, BIO *chain) |
612 | { | |
bfb0641f | 613 | EVP_MD_CTX *mctx = EVP_MD_CTX_new(); |
0f113f3e MC |
614 | int r = 0; |
615 | EVP_PKEY_CTX *pctx = NULL; | |
c1669f41 | 616 | const CMS_CTX *ctx = cms_get0_cmsctx(cms); |
6e59a892 RL |
617 | |
618 | if (mctx == NULL) { | |
619 | CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, ERR_R_MALLOC_FAILURE); | |
620 | return 0; | |
621 | } | |
0f113f3e | 622 | |
c1669f41 | 623 | if (si->pkey == NULL) { |
0f113f3e | 624 | CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, CMS_R_NO_PRIVATE_KEY); |
22803581 | 625 | goto err; |
0f113f3e MC |
626 | } |
627 | ||
6e59a892 | 628 | if (!cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm)) |
0f113f3e | 629 | goto err; |
0d4fb843 | 630 | /* Set SignerInfo algorithm details if we used custom parameter */ |
0f113f3e MC |
631 | if (si->pctx && !cms_sd_asn1_ctrl(si, 0)) |
632 | goto err; | |
633 | ||
634 | /* | |
635 | * If any signed attributes calculate and add messageDigest attribute | |
636 | */ | |
637 | ||
638 | if (CMS_signed_get_attr_count(si) >= 0) { | |
0f113f3e MC |
639 | unsigned char md[EVP_MAX_MD_SIZE]; |
640 | unsigned int mdlen; | |
c1669f41 | 641 | |
6e59a892 | 642 | if (!EVP_DigestFinal_ex(mctx, md, &mdlen)) |
0f113f3e MC |
643 | goto err; |
644 | if (!CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest, | |
645 | V_ASN1_OCTET_STRING, md, mdlen)) | |
646 | goto err; | |
647 | /* Copy content type across */ | |
19e512a8 | 648 | if (!cms_set_si_contentType_attr(cms, si)) |
0f113f3e | 649 | goto err; |
19e512a8 | 650 | |
0f113f3e MC |
651 | if (!CMS_SignerInfo_sign(si)) |
652 | goto err; | |
653 | } else if (si->pctx) { | |
654 | unsigned char *sig; | |
655 | size_t siglen; | |
656 | unsigned char md[EVP_MAX_MD_SIZE]; | |
657 | unsigned int mdlen; | |
c1669f41 | 658 | |
0f113f3e | 659 | pctx = si->pctx; |
6e59a892 | 660 | if (!EVP_DigestFinal_ex(mctx, md, &mdlen)) |
0f113f3e MC |
661 | goto err; |
662 | siglen = EVP_PKEY_size(si->pkey); | |
663 | sig = OPENSSL_malloc(siglen); | |
90945fa3 | 664 | if (sig == NULL) { |
0f113f3e MC |
665 | CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, ERR_R_MALLOC_FAILURE); |
666 | goto err; | |
667 | } | |
668 | if (EVP_PKEY_sign(pctx, sig, &siglen, md, mdlen) <= 0) { | |
669 | OPENSSL_free(sig); | |
670 | goto err; | |
671 | } | |
672 | ASN1_STRING_set0(si->signature, sig, siglen); | |
673 | } else { | |
674 | unsigned char *sig; | |
675 | unsigned int siglen; | |
c1669f41 | 676 | |
0f113f3e | 677 | sig = OPENSSL_malloc(EVP_PKEY_size(si->pkey)); |
90945fa3 | 678 | if (sig == NULL) { |
0f113f3e MC |
679 | CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, ERR_R_MALLOC_FAILURE); |
680 | goto err; | |
681 | } | |
c1669f41 SL |
682 | if (!EVP_SignFinal_with_libctx(mctx, sig, &siglen, si->pkey, |
683 | ctx->libctx, ctx->propq)) { | |
0f113f3e MC |
684 | CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, CMS_R_SIGNFINAL_ERROR); |
685 | OPENSSL_free(sig); | |
686 | goto err; | |
687 | } | |
688 | ASN1_STRING_set0(si->signature, sig, siglen); | |
689 | } | |
690 | ||
691 | r = 1; | |
692 | ||
693 | err: | |
bfb0641f | 694 | EVP_MD_CTX_free(mctx); |
c5ba2d99 | 695 | EVP_PKEY_CTX_free(pctx); |
0f113f3e MC |
696 | return r; |
697 | ||
698 | } | |
8931b30d DSH |
699 | |
700 | int cms_SignedData_final(CMS_ContentInfo *cms, BIO *chain) | |
0f113f3e MC |
701 | { |
702 | STACK_OF(CMS_SignerInfo) *sinfos; | |
703 | CMS_SignerInfo *si; | |
704 | int i; | |
c1669f41 | 705 | |
0f113f3e MC |
706 | sinfos = CMS_get0_SignerInfos(cms); |
707 | for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) { | |
708 | si = sk_CMS_SignerInfo_value(sinfos, i); | |
709 | if (!cms_SignerInfo_content_sign(cms, si, chain)) | |
710 | return 0; | |
711 | } | |
712 | cms->d.signedData->encapContentInfo->partial = 0; | |
713 | return 1; | |
714 | } | |
8931b30d DSH |
715 | |
716 | int CMS_SignerInfo_sign(CMS_SignerInfo *si) | |
0f113f3e | 717 | { |
6e59a892 | 718 | EVP_MD_CTX *mctx = si->mctx; |
e6803117 | 719 | EVP_PKEY_CTX *pctx = NULL; |
0f113f3e MC |
720 | unsigned char *abuf = NULL; |
721 | int alen; | |
722 | size_t siglen; | |
c1669f41 SL |
723 | const CMS_CTX *ctx = si->cms_ctx; |
724 | const char *md_name = OBJ_nid2sn(OBJ_obj2nid(si->digestAlgorithm->algorithm)); | |
0f113f3e | 725 | |
c1669f41 | 726 | if (md_name == NULL) |
0f113f3e MC |
727 | return 0; |
728 | ||
729 | if (CMS_signed_get_attr_by_NID(si, NID_pkcs9_signingTime, -1) < 0) { | |
730 | if (!cms_add1_signingTime(si, NULL)) | |
731 | goto err; | |
732 | } | |
733 | ||
19e512a8 SL |
734 | if (!CMS_si_check_attributes(si)) |
735 | goto err; | |
736 | ||
0f113f3e MC |
737 | if (si->pctx) |
738 | pctx = si->pctx; | |
739 | else { | |
bfb0641f | 740 | EVP_MD_CTX_reset(mctx); |
c1669f41 SL |
741 | if (EVP_DigestSignInit_with_libctx(mctx, &pctx, |
742 | md_name, ctx->libctx, ctx->propq, | |
743 | si->pkey) <= 0) | |
0f113f3e | 744 | goto err; |
f7a21d85 | 745 | si->pctx = pctx; |
0f113f3e MC |
746 | } |
747 | ||
dfcb5d29 MC |
748 | /* |
749 | * TODO(3.0): This causes problems when providers are in use, so disabled | |
750 | * for now. Can we get rid of this completely? AFAICT this ctrl has been | |
751 | * present since CMS was first put in - but has never been used to do | |
752 | * anything. All internal implementations just return 1 and ignore this ctrl | |
753 | * and have always done so by the looks of things. To fix this we could | |
754 | * convert this ctrl into a param, which would require us to send all the | |
755 | * signer info data as a set of params...but that is non-trivial and since | |
756 | * this isn't used by anything it may be better just to remove it. | |
757 | */ | |
758 | #if 0 | |
0f113f3e MC |
759 | if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN, |
760 | EVP_PKEY_CTRL_CMS_SIGN, 0, si) <= 0) { | |
761 | CMSerr(CMS_F_CMS_SIGNERINFO_SIGN, CMS_R_CTRL_ERROR); | |
762 | goto err; | |
763 | } | |
dfcb5d29 | 764 | #endif |
0f113f3e MC |
765 | |
766 | alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs, &abuf, | |
767 | ASN1_ITEM_rptr(CMS_Attributes_Sign)); | |
768 | if (!abuf) | |
769 | goto err; | |
770 | if (EVP_DigestSignUpdate(mctx, abuf, alen) <= 0) | |
771 | goto err; | |
772 | if (EVP_DigestSignFinal(mctx, NULL, &siglen) <= 0) | |
773 | goto err; | |
774 | OPENSSL_free(abuf); | |
775 | abuf = OPENSSL_malloc(siglen); | |
90945fa3 | 776 | if (abuf == NULL) |
0f113f3e MC |
777 | goto err; |
778 | if (EVP_DigestSignFinal(mctx, abuf, &siglen) <= 0) | |
779 | goto err; | |
780 | ||
dfcb5d29 MC |
781 | /* |
782 | * TODO(3.0): This causes problems when providers are in use, so disabled | |
783 | * for now. Can we get rid of this completely? AFAICT this ctrl has been | |
784 | * present since CMS was first put in - but has never been used to do | |
785 | * anything. All internal implementations just return 1 and ignore this ctrl | |
786 | * and have always done so by the looks of things. To fix this we could | |
787 | * convert this ctrl into a param, which would require us to send all the | |
788 | * signer info data as a set of params...but that is non-trivial and since | |
789 | * this isn't used by anything it may be better just to remove it. | |
790 | */ | |
791 | #if 0 | |
0f113f3e MC |
792 | if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN, |
793 | EVP_PKEY_CTRL_CMS_SIGN, 1, si) <= 0) { | |
794 | CMSerr(CMS_F_CMS_SIGNERINFO_SIGN, CMS_R_CTRL_ERROR); | |
795 | goto err; | |
796 | } | |
dfcb5d29 | 797 | #endif |
0f113f3e | 798 | |
bfb0641f | 799 | EVP_MD_CTX_reset(mctx); |
0f113f3e MC |
800 | |
801 | ASN1_STRING_set0(si->signature, abuf, siglen); | |
802 | ||
803 | return 1; | |
804 | ||
805 | err: | |
b548a1f1 | 806 | OPENSSL_free(abuf); |
bfb0641f | 807 | EVP_MD_CTX_reset(mctx); |
0f113f3e | 808 | return 0; |
0f113f3e | 809 | } |
8931b30d DSH |
810 | |
811 | int CMS_SignerInfo_verify(CMS_SignerInfo *si) | |
0f113f3e | 812 | { |
6e59a892 | 813 | EVP_MD_CTX *mctx = NULL; |
0f113f3e MC |
814 | unsigned char *abuf = NULL; |
815 | int alen, r = -1; | |
c1669f41 | 816 | const char *name; |
1acb2e6f SL |
817 | const EVP_MD *md; |
818 | EVP_MD *fetched_md = NULL; | |
c1669f41 | 819 | const CMS_CTX *ctx = si->cms_ctx; |
0f113f3e | 820 | |
c1669f41 | 821 | if (si->pkey == NULL) { |
0f113f3e MC |
822 | CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, CMS_R_NO_PUBLIC_KEY); |
823 | return -1; | |
824 | } | |
825 | ||
19e512a8 SL |
826 | if (!CMS_si_check_attributes(si)) |
827 | return -1; | |
828 | ||
c1669f41 | 829 | name = OBJ_nid2sn(OBJ_obj2nid(si->digestAlgorithm->algorithm)); |
1acb2e6f SL |
830 | |
831 | (void)ERR_set_mark(); | |
832 | fetched_md = EVP_MD_fetch(ctx->libctx, name, ctx->propq); | |
833 | ||
834 | if (fetched_md != NULL) | |
835 | md = fetched_md; | |
836 | else | |
837 | md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm); | |
838 | if (md == NULL) { | |
839 | (void)ERR_clear_last_mark(); | |
840 | CMSerr(0, CMS_R_UNKNOWN_DIGEST_ALGORITHM); | |
0f113f3e | 841 | return -1; |
1acb2e6f SL |
842 | } |
843 | (void)ERR_pop_to_mark(); | |
844 | ||
378db52b RS |
845 | if (si->mctx == NULL && (si->mctx = EVP_MD_CTX_new()) == NULL) { |
846 | CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, ERR_R_MALLOC_FAILURE); | |
c1669f41 | 847 | goto err; |
378db52b | 848 | } |
6e59a892 | 849 | mctx = si->mctx; |
c1669f41 SL |
850 | if (EVP_DigestVerifyInit_with_libctx(mctx, &si->pctx, |
851 | EVP_MD_name(md), ctx->libctx, NULL, | |
852 | si->pkey) <= 0) | |
0f113f3e MC |
853 | goto err; |
854 | ||
855 | if (!cms_sd_asn1_ctrl(si, 1)) | |
856 | goto err; | |
857 | ||
858 | alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs, &abuf, | |
859 | ASN1_ITEM_rptr(CMS_Attributes_Verify)); | |
860 | if (!abuf) | |
861 | goto err; | |
862 | r = EVP_DigestVerifyUpdate(mctx, abuf, alen); | |
863 | OPENSSL_free(abuf); | |
864 | if (r <= 0) { | |
865 | r = -1; | |
866 | goto err; | |
867 | } | |
868 | r = EVP_DigestVerifyFinal(mctx, | |
869 | si->signature->data, si->signature->length); | |
870 | if (r <= 0) | |
871 | CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, CMS_R_VERIFICATION_FAILURE); | |
872 | err: | |
1acb2e6f | 873 | EVP_MD_free(fetched_md); |
bfb0641f | 874 | EVP_MD_CTX_reset(mctx); |
0f113f3e MC |
875 | return r; |
876 | } | |
8931b30d DSH |
877 | |
878 | /* Create a chain of digest BIOs from a CMS ContentInfo */ | |
879 | ||
880 | BIO *cms_SignedData_init_bio(CMS_ContentInfo *cms) | |
0f113f3e MC |
881 | { |
882 | int i; | |
883 | CMS_SignedData *sd; | |
884 | BIO *chain = NULL; | |
c1669f41 | 885 | |
0f113f3e | 886 | sd = cms_get0_signed(cms); |
c1669f41 | 887 | if (sd == NULL) |
0f113f3e MC |
888 | return NULL; |
889 | if (cms->d.signedData->encapContentInfo->partial) | |
890 | cms_sd_set_version(sd); | |
891 | for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++) { | |
892 | X509_ALGOR *digestAlgorithm; | |
893 | BIO *mdbio; | |
c1669f41 | 894 | |
0f113f3e | 895 | digestAlgorithm = sk_X509_ALGOR_value(sd->digestAlgorithms, i); |
c1669f41 SL |
896 | mdbio = cms_DigestAlgorithm_init_bio(digestAlgorithm, cms_get0_cmsctx(cms)); |
897 | if (mdbio == NULL) | |
0f113f3e | 898 | goto err; |
c1669f41 | 899 | if (chain != NULL) |
0f113f3e MC |
900 | BIO_push(chain, mdbio); |
901 | else | |
902 | chain = mdbio; | |
903 | } | |
904 | return chain; | |
905 | err: | |
ca3a82c3 | 906 | BIO_free_all(chain); |
0f113f3e MC |
907 | return NULL; |
908 | } | |
8931b30d DSH |
909 | |
910 | int CMS_SignerInfo_verify_content(CMS_SignerInfo *si, BIO *chain) | |
0f113f3e MC |
911 | { |
912 | ASN1_OCTET_STRING *os = NULL; | |
bfb0641f | 913 | EVP_MD_CTX *mctx = EVP_MD_CTX_new(); |
0f113f3e MC |
914 | EVP_PKEY_CTX *pkctx = NULL; |
915 | int r = -1; | |
916 | unsigned char mval[EVP_MAX_MD_SIZE]; | |
917 | unsigned int mlen; | |
6e59a892 RL |
918 | |
919 | if (mctx == NULL) { | |
920 | CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT, ERR_R_MALLOC_FAILURE); | |
921 | goto err; | |
922 | } | |
0f113f3e MC |
923 | /* If we have any signed attributes look for messageDigest value */ |
924 | if (CMS_signed_get_attr_count(si) >= 0) { | |
925 | os = CMS_signed_get0_data_by_OBJ(si, | |
926 | OBJ_nid2obj(NID_pkcs9_messageDigest), | |
927 | -3, V_ASN1_OCTET_STRING); | |
c1669f41 | 928 | if (os == NULL) { |
0f113f3e MC |
929 | CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT, |
930 | CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE); | |
931 | goto err; | |
932 | } | |
933 | } | |
934 | ||
6e59a892 | 935 | if (!cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm)) |
0f113f3e MC |
936 | goto err; |
937 | ||
6e59a892 | 938 | if (EVP_DigestFinal_ex(mctx, mval, &mlen) <= 0) { |
0f113f3e MC |
939 | CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT, |
940 | CMS_R_UNABLE_TO_FINALIZE_CONTEXT); | |
941 | goto err; | |
942 | } | |
943 | ||
944 | /* If messageDigest found compare it */ | |
945 | ||
c1669f41 | 946 | if (os != NULL) { |
0f113f3e MC |
947 | if (mlen != (unsigned int)os->length) { |
948 | CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT, | |
949 | CMS_R_MESSAGEDIGEST_ATTRIBUTE_WRONG_LENGTH); | |
950 | goto err; | |
951 | } | |
952 | ||
953 | if (memcmp(mval, os->data, mlen)) { | |
954 | CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT, | |
955 | CMS_R_VERIFICATION_FAILURE); | |
956 | r = 0; | |
957 | } else | |
958 | r = 1; | |
959 | } else { | |
6e59a892 | 960 | const EVP_MD *md = EVP_MD_CTX_md(mctx); |
c1669f41 SL |
961 | const CMS_CTX *ctx = si->cms_ctx; |
962 | ||
963 | pkctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, si->pkey, ctx->propq); | |
90945fa3 MC |
964 | if (pkctx == NULL) |
965 | goto err; | |
0f113f3e MC |
966 | if (EVP_PKEY_verify_init(pkctx) <= 0) |
967 | goto err; | |
968 | if (EVP_PKEY_CTX_set_signature_md(pkctx, md) <= 0) | |
969 | goto err; | |
970 | si->pctx = pkctx; | |
971 | if (!cms_sd_asn1_ctrl(si, 1)) | |
972 | goto err; | |
973 | r = EVP_PKEY_verify(pkctx, si->signature->data, | |
974 | si->signature->length, mval, mlen); | |
975 | if (r <= 0) { | |
976 | CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT, | |
977 | CMS_R_VERIFICATION_FAILURE); | |
978 | r = 0; | |
979 | } | |
980 | } | |
981 | ||
982 | err: | |
c5ba2d99 | 983 | EVP_PKEY_CTX_free(pkctx); |
bfb0641f | 984 | EVP_MD_CTX_free(mctx); |
0f113f3e MC |
985 | return r; |
986 | ||
987 | } | |
8931b30d DSH |
988 | |
989 | int CMS_add_smimecap(CMS_SignerInfo *si, STACK_OF(X509_ALGOR) *algs) | |
0f113f3e MC |
990 | { |
991 | unsigned char *smder = NULL; | |
992 | int smderlen, r; | |
c1669f41 | 993 | |
0f113f3e MC |
994 | smderlen = i2d_X509_ALGORS(algs, &smder); |
995 | if (smderlen <= 0) | |
996 | return 0; | |
997 | r = CMS_signed_add1_attr_by_NID(si, NID_SMIMECapabilities, | |
998 | V_ASN1_SEQUENCE, smder, smderlen); | |
999 | OPENSSL_free(smder); | |
1000 | return r; | |
1001 | } | |
8931b30d DSH |
1002 | |
1003 | int CMS_add_simple_smimecap(STACK_OF(X509_ALGOR) **algs, | |
0f113f3e MC |
1004 | int algnid, int keysize) |
1005 | { | |
1006 | X509_ALGOR *alg; | |
1007 | ASN1_INTEGER *key = NULL; | |
c1669f41 | 1008 | |
0f113f3e MC |
1009 | if (keysize > 0) { |
1010 | key = ASN1_INTEGER_new(); | |
209c3d3e P |
1011 | if (key == NULL || !ASN1_INTEGER_set(key, keysize)) { |
1012 | ASN1_INTEGER_free(key); | |
0f113f3e | 1013 | return 0; |
209c3d3e | 1014 | } |
0f113f3e MC |
1015 | } |
1016 | alg = X509_ALGOR_new(); | |
90945fa3 | 1017 | if (alg == NULL) { |
2ace7450 | 1018 | ASN1_INTEGER_free(key); |
0f113f3e MC |
1019 | return 0; |
1020 | } | |
1021 | ||
1022 | X509_ALGOR_set0(alg, OBJ_nid2obj(algnid), | |
1023 | key ? V_ASN1_INTEGER : V_ASN1_UNDEF, key); | |
90945fa3 | 1024 | if (*algs == NULL) |
0f113f3e | 1025 | *algs = sk_X509_ALGOR_new_null(); |
90945fa3 | 1026 | if (*algs == NULL || !sk_X509_ALGOR_push(*algs, alg)) { |
0f113f3e MC |
1027 | X509_ALGOR_free(alg); |
1028 | return 0; | |
1029 | } | |
1030 | return 1; | |
1031 | } | |
8931b30d DSH |
1032 | |
1033 | /* Check to see if a cipher exists and if so add S/MIME capabilities */ | |
1034 | ||
1035 | static int cms_add_cipher_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg) | |
0f113f3e MC |
1036 | { |
1037 | if (EVP_get_cipherbynid(nid)) | |
1038 | return CMS_add_simple_smimecap(sk, nid, arg); | |
1039 | return 1; | |
1040 | } | |
8931b30d DSH |
1041 | |
1042 | static int cms_add_digest_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg) | |
0f113f3e MC |
1043 | { |
1044 | if (EVP_get_digestbynid(nid)) | |
1045 | return CMS_add_simple_smimecap(sk, nid, arg); | |
1046 | return 1; | |
1047 | } | |
8931b30d DSH |
1048 | |
1049 | int CMS_add_standard_smimecap(STACK_OF(X509_ALGOR) **smcap) | |
0f113f3e MC |
1050 | { |
1051 | if (!cms_add_cipher_smcap(smcap, NID_aes_256_cbc, -1) | |
c58f3e42 MC |
1052 | || !cms_add_digest_smcap(smcap, NID_id_GostR3411_2012_256, -1) |
1053 | || !cms_add_digest_smcap(smcap, NID_id_GostR3411_2012_512, -1) | |
0f113f3e MC |
1054 | || !cms_add_digest_smcap(smcap, NID_id_GostR3411_94, -1) |
1055 | || !cms_add_cipher_smcap(smcap, NID_id_Gost28147_89, -1) | |
1056 | || !cms_add_cipher_smcap(smcap, NID_aes_192_cbc, -1) | |
1057 | || !cms_add_cipher_smcap(smcap, NID_aes_128_cbc, -1) | |
1058 | || !cms_add_cipher_smcap(smcap, NID_des_ede3_cbc, -1) | |
1059 | || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 128) | |
1060 | || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 64) | |
1061 | || !cms_add_cipher_smcap(smcap, NID_des_cbc, -1) | |
1062 | || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 40)) | |
1063 | return 0; | |
1064 | return 1; | |
1065 | } |