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