/src/openssl36/crypto/cms/cms_lib.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 <openssl/asn1t.h> |
11 | | #include <openssl/x509v3.h> |
12 | | #include <openssl/err.h> |
13 | | #include <openssl/pem.h> |
14 | | #include <openssl/bio.h> |
15 | | #include <openssl/asn1.h> |
16 | | #include <openssl/cms.h> |
17 | | #include <openssl/core_names.h> |
18 | | #include "internal/sizes.h" |
19 | | #include "internal/cryptlib.h" |
20 | | #include "crypto/x509.h" |
21 | | #include "cms_local.h" |
22 | | #include "internal/cms.h" |
23 | | |
24 | | static STACK_OF(CMS_CertificateChoices) |
25 | | ** |
26 | | cms_get0_certificate_choices(CMS_ContentInfo *cms); |
27 | | |
28 | | IMPLEMENT_ASN1_ALLOC_FUNCTIONS(CMS_ContentInfo) |
29 | | IMPLEMENT_ASN1_PRINT_FUNCTION(CMS_ContentInfo) |
30 | | |
31 | | CMS_ContentInfo *d2i_CMS_ContentInfo(CMS_ContentInfo **a, |
32 | | const unsigned char **in, long len) |
33 | 0 | { |
34 | 0 | CMS_ContentInfo *ci; |
35 | 0 | const CMS_CTX *ctx = ossl_cms_get0_cmsctx(a == NULL ? NULL : *a); |
36 | |
|
37 | 0 | ci = (CMS_ContentInfo *)ASN1_item_d2i_ex((ASN1_VALUE **)a, in, len, |
38 | 0 | (CMS_ContentInfo_it()), |
39 | 0 | ossl_cms_ctx_get0_libctx(ctx), |
40 | 0 | ossl_cms_ctx_get0_propq(ctx)); |
41 | 0 | if (ci != NULL) { |
42 | 0 | ERR_set_mark(); |
43 | 0 | ossl_cms_resolve_libctx(ci); |
44 | 0 | ERR_pop_to_mark(); |
45 | 0 | } |
46 | 0 | return ci; |
47 | 0 | } |
48 | | |
49 | | int i2d_CMS_ContentInfo(const CMS_ContentInfo *a, unsigned char **out) |
50 | 0 | { |
51 | 0 | return ASN1_item_i2d((const ASN1_VALUE *)a, out, (CMS_ContentInfo_it())); |
52 | 0 | } |
53 | | |
54 | | CMS_ContentInfo *CMS_ContentInfo_new_ex(OSSL_LIB_CTX *libctx, const char *propq) |
55 | 0 | { |
56 | 0 | CMS_ContentInfo *ci; |
57 | |
|
58 | 0 | ci = (CMS_ContentInfo *)ASN1_item_new_ex(ASN1_ITEM_rptr(CMS_ContentInfo), |
59 | 0 | libctx, propq); |
60 | 0 | if (ci != NULL) { |
61 | 0 | ci->ctx.libctx = libctx; |
62 | 0 | ci->ctx.propq = NULL; |
63 | 0 | if (propq != NULL) { |
64 | 0 | ci->ctx.propq = OPENSSL_strdup(propq); |
65 | 0 | if (ci->ctx.propq == NULL) { |
66 | 0 | CMS_ContentInfo_free(ci); |
67 | 0 | ci = NULL; |
68 | 0 | } |
69 | 0 | } |
70 | 0 | } |
71 | 0 | return ci; |
72 | 0 | } |
73 | | |
74 | | const CMS_CTX *ossl_cms_get0_cmsctx(const CMS_ContentInfo *cms) |
75 | 69.1k | { |
76 | 69.1k | return cms != NULL ? &cms->ctx : NULL; |
77 | 69.1k | } |
78 | | |
79 | | OSSL_LIB_CTX *ossl_cms_ctx_get0_libctx(const CMS_CTX *ctx) |
80 | 58.0k | { |
81 | 58.0k | return ctx != NULL ? ctx->libctx : NULL; |
82 | 58.0k | } |
83 | | |
84 | | const char *ossl_cms_ctx_get0_propq(const CMS_CTX *ctx) |
85 | 58.0k | { |
86 | 58.0k | return ctx != NULL ? ctx->propq : NULL; |
87 | 58.0k | } |
88 | | |
89 | | void ossl_cms_resolve_libctx(CMS_ContentInfo *ci) |
90 | 6.85k | { |
91 | 6.85k | int i; |
92 | 6.85k | CMS_CertificateChoices *cch; |
93 | 6.85k | STACK_OF(CMS_CertificateChoices) **pcerts; |
94 | 6.85k | const CMS_CTX *ctx = ossl_cms_get0_cmsctx(ci); |
95 | 6.85k | OSSL_LIB_CTX *libctx = ossl_cms_ctx_get0_libctx(ctx); |
96 | 6.85k | const char *propq = ossl_cms_ctx_get0_propq(ctx); |
97 | | |
98 | 6.85k | ossl_cms_SignerInfos_set_cmsctx(ci); |
99 | 6.85k | ossl_cms_RecipientInfos_set_cmsctx(ci); |
100 | | |
101 | 6.85k | pcerts = cms_get0_certificate_choices(ci); |
102 | 6.85k | if (pcerts != NULL) { |
103 | 501k | for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) { |
104 | 499k | cch = sk_CMS_CertificateChoices_value(*pcerts, i); |
105 | 499k | if (cch->type == CMS_CERTCHOICE_CERT) |
106 | 996 | ossl_x509_set0_libctx(cch->d.certificate, libctx, propq); |
107 | 499k | } |
108 | 1.57k | } |
109 | 6.85k | } |
110 | | |
111 | | const ASN1_OBJECT *CMS_get0_type(const CMS_ContentInfo *cms) |
112 | 0 | { |
113 | 0 | return cms->contentType; |
114 | 0 | } |
115 | | |
116 | | CMS_ContentInfo *ossl_cms_Data_create(OSSL_LIB_CTX *libctx, const char *propq) |
117 | 0 | { |
118 | 0 | CMS_ContentInfo *cms = CMS_ContentInfo_new_ex(libctx, propq); |
119 | |
|
120 | 0 | if (cms != NULL) { |
121 | 0 | cms->contentType = OBJ_nid2obj(NID_pkcs7_data); |
122 | | /* Never detached */ |
123 | 0 | CMS_set_detached(cms, 0); |
124 | 0 | } |
125 | 0 | return cms; |
126 | 0 | } |
127 | | |
128 | | BIO *ossl_cms_content_bio(CMS_ContentInfo *cms) |
129 | 0 | { |
130 | 0 | ASN1_OCTET_STRING **pos = CMS_get0_content(cms); |
131 | |
|
132 | 0 | if (pos == NULL) |
133 | 0 | return NULL; |
134 | | /* If content detached data goes nowhere: create NULL BIO */ |
135 | 0 | if (*pos == NULL) |
136 | 0 | return BIO_new(BIO_s_null()); |
137 | | /* |
138 | | * If content not detached and created return memory BIO |
139 | | */ |
140 | 0 | if (*pos == NULL || ((*pos)->flags == ASN1_STRING_FLAG_CONT)) |
141 | 0 | return BIO_new(BIO_s_mem()); |
142 | | /* Else content was read in: return read only BIO for it */ |
143 | 0 | return BIO_new_mem_buf((*pos)->data, (*pos)->length); |
144 | 0 | } |
145 | | |
146 | | BIO *CMS_dataInit(CMS_ContentInfo *cms, BIO *icont) |
147 | 0 | { |
148 | 0 | BIO *cmsbio, *cont; |
149 | 0 | if (icont) |
150 | 0 | cont = icont; |
151 | 0 | else |
152 | 0 | cont = ossl_cms_content_bio(cms); |
153 | 0 | if (!cont) { |
154 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_NO_CONTENT); |
155 | 0 | return NULL; |
156 | 0 | } |
157 | 0 | switch (OBJ_obj2nid(cms->contentType)) { |
158 | | |
159 | 0 | case NID_pkcs7_data: |
160 | 0 | return cont; |
161 | | |
162 | 0 | case NID_pkcs7_signed: |
163 | 0 | cmsbio = ossl_cms_SignedData_init_bio(cms); |
164 | 0 | break; |
165 | | |
166 | 0 | case NID_pkcs7_digest: |
167 | 0 | cmsbio = ossl_cms_DigestedData_init_bio(cms); |
168 | 0 | break; |
169 | | #ifndef OPENSSL_NO_ZLIB |
170 | | case NID_id_smime_ct_compressedData: |
171 | | cmsbio = ossl_cms_CompressedData_init_bio(cms); |
172 | | break; |
173 | | #endif |
174 | | |
175 | 0 | case NID_pkcs7_encrypted: |
176 | 0 | cmsbio = ossl_cms_EncryptedData_init_bio(cms); |
177 | 0 | break; |
178 | | |
179 | 0 | case NID_pkcs7_enveloped: |
180 | 0 | cmsbio = ossl_cms_EnvelopedData_init_bio(cms); |
181 | 0 | break; |
182 | | |
183 | 0 | case NID_id_smime_ct_authEnvelopedData: |
184 | 0 | cmsbio = ossl_cms_AuthEnvelopedData_init_bio(cms); |
185 | 0 | break; |
186 | | |
187 | 0 | default: |
188 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_TYPE); |
189 | 0 | goto err; |
190 | 0 | } |
191 | | |
192 | 0 | if (cmsbio) |
193 | 0 | return BIO_push(cmsbio, cont); |
194 | 0 | err: |
195 | 0 | if (!icont) |
196 | 0 | BIO_free(cont); |
197 | 0 | return NULL; |
198 | 0 | } |
199 | | |
200 | | /* unfortunately cannot constify SMIME_write_ASN1() due to this function */ |
201 | | int CMS_dataFinal(CMS_ContentInfo *cms, BIO *cmsbio) |
202 | 0 | { |
203 | 0 | return ossl_cms_DataFinal(cms, cmsbio, NULL, 0); |
204 | 0 | } |
205 | | |
206 | | int ossl_cms_DataFinal(CMS_ContentInfo *cms, BIO *cmsbio, |
207 | | const unsigned char *precomp_md, |
208 | | unsigned int precomp_mdlen) |
209 | 0 | { |
210 | 0 | ASN1_OCTET_STRING **pos = CMS_get0_content(cms); |
211 | |
|
212 | 0 | if (pos == NULL) |
213 | 0 | return 0; |
214 | | /* If embedded content find memory BIO and set content */ |
215 | 0 | if (*pos && ((*pos)->flags & ASN1_STRING_FLAG_CONT)) { |
216 | 0 | BIO *mbio; |
217 | 0 | unsigned char *cont; |
218 | 0 | long contlen; |
219 | 0 | mbio = BIO_find_type(cmsbio, BIO_TYPE_MEM); |
220 | 0 | if (!mbio) { |
221 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_NOT_FOUND); |
222 | 0 | return 0; |
223 | 0 | } |
224 | 0 | contlen = BIO_get_mem_data(mbio, &cont); |
225 | | /* Set bio as read only so its content can't be clobbered */ |
226 | 0 | BIO_set_flags(mbio, BIO_FLAGS_MEM_RDONLY); |
227 | 0 | BIO_set_mem_eof_return(mbio, 0); |
228 | 0 | ASN1_STRING_set0(*pos, cont, contlen); |
229 | 0 | (*pos)->flags &= ~ASN1_STRING_FLAG_CONT; |
230 | 0 | } |
231 | | |
232 | 0 | switch (OBJ_obj2nid(cms->contentType)) { |
233 | | |
234 | 0 | case NID_pkcs7_data: |
235 | 0 | case NID_pkcs7_encrypted: |
236 | 0 | case NID_id_smime_ct_compressedData: |
237 | | /* Nothing to do */ |
238 | 0 | return 1; |
239 | | |
240 | 0 | case NID_pkcs7_enveloped: |
241 | 0 | return ossl_cms_EnvelopedData_final(cms, cmsbio); |
242 | | |
243 | 0 | case NID_id_smime_ct_authEnvelopedData: |
244 | 0 | return ossl_cms_AuthEnvelopedData_final(cms, cmsbio); |
245 | | |
246 | 0 | case NID_pkcs7_signed: |
247 | 0 | return ossl_cms_SignedData_final(cms, cmsbio, precomp_md, precomp_mdlen); |
248 | | |
249 | 0 | case NID_pkcs7_digest: |
250 | 0 | return ossl_cms_DigestedData_do_final(cms, cmsbio, 0); |
251 | | |
252 | 0 | default: |
253 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_TYPE); |
254 | 0 | return 0; |
255 | 0 | } |
256 | 0 | } |
257 | | |
258 | | /* |
259 | | * Return an OCTET STRING pointer to content. This allows it to be accessed |
260 | | * or set later. |
261 | | */ |
262 | | |
263 | | ASN1_OCTET_STRING **CMS_get0_content(CMS_ContentInfo *cms) |
264 | 0 | { |
265 | 0 | switch (OBJ_obj2nid(cms->contentType)) { |
266 | | |
267 | 0 | case NID_pkcs7_data: |
268 | 0 | return &cms->d.data; |
269 | | |
270 | 0 | case NID_pkcs7_signed: |
271 | 0 | return &cms->d.signedData->encapContentInfo->eContent; |
272 | | |
273 | 0 | case NID_pkcs7_enveloped: |
274 | 0 | return &cms->d.envelopedData->encryptedContentInfo->encryptedContent; |
275 | | |
276 | 0 | case NID_pkcs7_digest: |
277 | 0 | return &cms->d.digestedData->encapContentInfo->eContent; |
278 | | |
279 | 0 | case NID_pkcs7_encrypted: |
280 | 0 | return &cms->d.encryptedData->encryptedContentInfo->encryptedContent; |
281 | | |
282 | 0 | case NID_id_smime_ct_authEnvelopedData: |
283 | 0 | return &cms->d.authEnvelopedData->authEncryptedContentInfo |
284 | 0 | ->encryptedContent; |
285 | | |
286 | 0 | case NID_id_smime_ct_authData: |
287 | 0 | return &cms->d.authenticatedData->encapContentInfo->eContent; |
288 | | |
289 | 0 | case NID_id_smime_ct_compressedData: |
290 | 0 | return &cms->d.compressedData->encapContentInfo->eContent; |
291 | | |
292 | 0 | default: |
293 | 0 | if (cms->d.other->type == V_ASN1_OCTET_STRING) |
294 | 0 | return &cms->d.other->value.octet_string; |
295 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_TYPE); |
296 | 0 | return NULL; |
297 | 0 | } |
298 | 0 | } |
299 | | |
300 | | /* |
301 | | * Return an ASN1_OBJECT pointer to content type. This allows it to be |
302 | | * accessed or set later. |
303 | | */ |
304 | | |
305 | | static ASN1_OBJECT **cms_get0_econtent_type(CMS_ContentInfo *cms) |
306 | 0 | { |
307 | 0 | switch (OBJ_obj2nid(cms->contentType)) { |
308 | | |
309 | 0 | case NID_pkcs7_signed: |
310 | 0 | return &cms->d.signedData->encapContentInfo->eContentType; |
311 | | |
312 | 0 | case NID_pkcs7_enveloped: |
313 | 0 | return &cms->d.envelopedData->encryptedContentInfo->contentType; |
314 | | |
315 | 0 | case NID_pkcs7_digest: |
316 | 0 | return &cms->d.digestedData->encapContentInfo->eContentType; |
317 | | |
318 | 0 | case NID_pkcs7_encrypted: |
319 | 0 | return &cms->d.encryptedData->encryptedContentInfo->contentType; |
320 | | |
321 | 0 | case NID_id_smime_ct_authEnvelopedData: |
322 | 0 | return &cms->d.authEnvelopedData->authEncryptedContentInfo |
323 | 0 | ->contentType; |
324 | 0 | case NID_id_smime_ct_authData: |
325 | 0 | return &cms->d.authenticatedData->encapContentInfo->eContentType; |
326 | | |
327 | 0 | case NID_id_smime_ct_compressedData: |
328 | 0 | return &cms->d.compressedData->encapContentInfo->eContentType; |
329 | | |
330 | 0 | default: |
331 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_TYPE); |
332 | 0 | return NULL; |
333 | 0 | } |
334 | 0 | } |
335 | | |
336 | | const ASN1_OBJECT *CMS_get0_eContentType(CMS_ContentInfo *cms) |
337 | 0 | { |
338 | 0 | ASN1_OBJECT **petype; |
339 | 0 | petype = cms_get0_econtent_type(cms); |
340 | 0 | if (petype) |
341 | 0 | return *petype; |
342 | 0 | return NULL; |
343 | 0 | } |
344 | | |
345 | | int CMS_set1_eContentType(CMS_ContentInfo *cms, const ASN1_OBJECT *oid) |
346 | 0 | { |
347 | 0 | ASN1_OBJECT **petype, *etype; |
348 | |
|
349 | 0 | petype = cms_get0_econtent_type(cms); |
350 | 0 | if (petype == NULL) |
351 | 0 | return 0; |
352 | 0 | if (oid == NULL) |
353 | 0 | return 1; |
354 | 0 | etype = OBJ_dup(oid); |
355 | 0 | if (etype == NULL) |
356 | 0 | return 0; |
357 | 0 | ASN1_OBJECT_free(*petype); |
358 | 0 | *petype = etype; |
359 | 0 | return 1; |
360 | 0 | } |
361 | | |
362 | | int CMS_is_detached(CMS_ContentInfo *cms) |
363 | 0 | { |
364 | 0 | ASN1_OCTET_STRING **pos; |
365 | |
|
366 | 0 | pos = CMS_get0_content(cms); |
367 | 0 | if (pos == NULL) |
368 | 0 | return -1; |
369 | 0 | if (*pos != NULL) |
370 | 0 | return 0; |
371 | 0 | return 1; |
372 | 0 | } |
373 | | |
374 | | int CMS_set_detached(CMS_ContentInfo *cms, int detached) |
375 | 0 | { |
376 | 0 | ASN1_OCTET_STRING **pos; |
377 | |
|
378 | 0 | pos = CMS_get0_content(cms); |
379 | 0 | if (pos == NULL) |
380 | 0 | return 0; |
381 | 0 | if (detached) { |
382 | 0 | ASN1_OCTET_STRING_free(*pos); |
383 | 0 | *pos = NULL; |
384 | 0 | return 1; |
385 | 0 | } |
386 | 0 | if (*pos == NULL) |
387 | 0 | *pos = ASN1_OCTET_STRING_new(); |
388 | 0 | if (*pos != NULL) { |
389 | | /* |
390 | | * NB: special flag to show content is created and not read in. |
391 | | */ |
392 | 0 | (*pos)->flags |= ASN1_STRING_FLAG_CONT; |
393 | 0 | return 1; |
394 | 0 | } |
395 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB); |
396 | 0 | return 0; |
397 | 0 | } |
398 | | |
399 | | /* Create a digest BIO from an X509_ALGOR structure */ |
400 | | |
401 | | BIO *ossl_cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm, |
402 | | const CMS_CTX *ctx) |
403 | 0 | { |
404 | 0 | BIO *mdbio = NULL; |
405 | 0 | const ASN1_OBJECT *digestoid; |
406 | 0 | const EVP_MD *digest = NULL; |
407 | 0 | EVP_MD *fetched_digest = NULL; |
408 | 0 | char alg[OSSL_MAX_NAME_SIZE]; |
409 | 0 | size_t xof_len = 0; |
410 | |
|
411 | 0 | X509_ALGOR_get0(&digestoid, NULL, NULL, digestAlgorithm); |
412 | 0 | OBJ_obj2txt(alg, sizeof(alg), digestoid, 0); |
413 | |
|
414 | 0 | (void)ERR_set_mark(); |
415 | 0 | fetched_digest = EVP_MD_fetch(ossl_cms_ctx_get0_libctx(ctx), alg, |
416 | 0 | ossl_cms_ctx_get0_propq(ctx)); |
417 | |
|
418 | 0 | if (fetched_digest != NULL) |
419 | 0 | digest = fetched_digest; |
420 | 0 | else |
421 | 0 | digest = EVP_get_digestbyobj(digestoid); |
422 | 0 | if (digest == NULL) { |
423 | 0 | (void)ERR_clear_last_mark(); |
424 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_UNKNOWN_DIGEST_ALGORITHM); |
425 | 0 | goto err; |
426 | 0 | } |
427 | 0 | (void)ERR_pop_to_mark(); |
428 | |
|
429 | 0 | mdbio = BIO_new(BIO_f_md()); |
430 | 0 | if (mdbio == NULL || BIO_set_md(mdbio, digest) <= 0) { |
431 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_MD_BIO_INIT_ERROR); |
432 | 0 | goto err; |
433 | 0 | } |
434 | 0 | if (EVP_MD_xof(digest)) { |
435 | 0 | if (EVP_MD_is_a(digest, SN_shake128)) |
436 | 0 | xof_len = 32; |
437 | 0 | else if (EVP_MD_is_a(digest, SN_shake256)) |
438 | 0 | xof_len = 64; |
439 | 0 | if (xof_len > 0) { |
440 | 0 | EVP_MD_CTX *mdctx; |
441 | 0 | OSSL_PARAM params[2]; |
442 | |
|
443 | 0 | if (BIO_get_md_ctx(mdbio, &mdctx) <= 0 || mdctx == NULL) |
444 | 0 | goto err; |
445 | 0 | params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, |
446 | 0 | &xof_len); |
447 | 0 | params[1] = OSSL_PARAM_construct_end(); |
448 | 0 | if (!EVP_MD_CTX_set_params(mdctx, params)) |
449 | 0 | goto err; |
450 | 0 | } |
451 | 0 | } |
452 | 0 | EVP_MD_free(fetched_digest); |
453 | 0 | return mdbio; |
454 | 0 | err: |
455 | 0 | EVP_MD_free(fetched_digest); |
456 | 0 | BIO_free(mdbio); |
457 | 0 | return NULL; |
458 | 0 | } |
459 | | |
460 | | /* Locate a message digest content from a BIO chain based on SignerInfo */ |
461 | | |
462 | | int ossl_cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain, |
463 | | X509_ALGOR *mdalg) |
464 | 0 | { |
465 | 0 | int nid; |
466 | 0 | const ASN1_OBJECT *mdoid; |
467 | 0 | X509_ALGOR_get0(&mdoid, NULL, NULL, mdalg); |
468 | 0 | nid = OBJ_obj2nid(mdoid); |
469 | | /* Look for digest type to match signature */ |
470 | 0 | for (;;) { |
471 | 0 | EVP_MD_CTX *mtmp; |
472 | 0 | chain = BIO_find_type(chain, BIO_TYPE_MD); |
473 | 0 | if (chain == NULL) { |
474 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_DIGEST); |
475 | 0 | return 0; |
476 | 0 | } |
477 | 0 | BIO_get_md_ctx(chain, &mtmp); |
478 | 0 | if (EVP_MD_CTX_get_type(mtmp) == nid |
479 | | /* |
480 | | * Workaround for broken implementations that use signature |
481 | | * algorithm OID instead of digest. |
482 | | */ |
483 | 0 | || EVP_MD_get_pkey_type(EVP_MD_CTX_get0_md(mtmp)) == nid) |
484 | 0 | return EVP_MD_CTX_copy_ex(mctx, mtmp); |
485 | 0 | chain = BIO_next(chain); |
486 | 0 | } |
487 | 0 | } |
488 | | |
489 | | static STACK_OF(CMS_CertificateChoices) |
490 | | ** |
491 | | cms_get0_certificate_choices(CMS_ContentInfo *cms) |
492 | 6.85k | { |
493 | 6.85k | switch (OBJ_obj2nid(cms->contentType)) { |
494 | | |
495 | 1.56k | case NID_pkcs7_signed: |
496 | 1.56k | return &cms->d.signedData->certificates; |
497 | | |
498 | 2.52k | case NID_pkcs7_enveloped: |
499 | 2.52k | if (cms->d.envelopedData->originatorInfo == NULL) |
500 | 2.51k | return NULL; |
501 | 6 | return &cms->d.envelopedData->originatorInfo->certificates; |
502 | | |
503 | 0 | case NID_id_smime_ct_authEnvelopedData: |
504 | 0 | if (cms->d.authEnvelopedData->originatorInfo == NULL) |
505 | 0 | return NULL; |
506 | 0 | return &cms->d.authEnvelopedData->originatorInfo->certificates; |
507 | | |
508 | 2.77k | default: |
509 | 2.77k | ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_TYPE); |
510 | 2.77k | return NULL; |
511 | 6.85k | } |
512 | 6.85k | } |
513 | | |
514 | | CMS_CertificateChoices *CMS_add0_CertificateChoices(CMS_ContentInfo *cms) |
515 | 0 | { |
516 | 0 | STACK_OF(CMS_CertificateChoices) **pcerts; |
517 | 0 | CMS_CertificateChoices *cch; |
518 | |
|
519 | 0 | pcerts = cms_get0_certificate_choices(cms); |
520 | 0 | if (pcerts == NULL) |
521 | 0 | return NULL; |
522 | 0 | if (*pcerts == NULL) |
523 | 0 | *pcerts = sk_CMS_CertificateChoices_new_null(); |
524 | 0 | if (*pcerts == NULL) |
525 | 0 | return NULL; |
526 | 0 | cch = M_ASN1_new_of(CMS_CertificateChoices); |
527 | 0 | if (!cch) |
528 | 0 | return NULL; |
529 | 0 | if (!sk_CMS_CertificateChoices_push(*pcerts, cch)) { |
530 | 0 | M_ASN1_free_of(cch, CMS_CertificateChoices); |
531 | 0 | return NULL; |
532 | 0 | } |
533 | 0 | return cch; |
534 | 0 | } |
535 | | |
536 | | int CMS_add0_cert(CMS_ContentInfo *cms, X509 *cert) |
537 | 0 | { |
538 | 0 | CMS_CertificateChoices *cch; |
539 | 0 | STACK_OF(CMS_CertificateChoices) **pcerts; |
540 | 0 | int i; |
541 | |
|
542 | 0 | pcerts = cms_get0_certificate_choices(cms); |
543 | 0 | if (pcerts == NULL) |
544 | 0 | return 0; |
545 | 0 | for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) { |
546 | 0 | cch = sk_CMS_CertificateChoices_value(*pcerts, i); |
547 | 0 | if (cch->type == CMS_CERTCHOICE_CERT) { |
548 | 0 | if (X509_cmp(cch->d.certificate, cert) == 0) { |
549 | 0 | X509_free(cert); |
550 | 0 | return 1; /* cert already present */ |
551 | 0 | } |
552 | 0 | } |
553 | 0 | } |
554 | 0 | cch = CMS_add0_CertificateChoices(cms); |
555 | 0 | if (!cch) |
556 | 0 | return 0; |
557 | 0 | cch->type = CMS_CERTCHOICE_CERT; |
558 | 0 | cch->d.certificate = cert; |
559 | 0 | return 1; |
560 | 0 | } |
561 | | |
562 | | int CMS_add1_cert(CMS_ContentInfo *cms, X509 *cert) |
563 | 0 | { |
564 | 0 | if (!X509_up_ref(cert)) |
565 | 0 | return 0; |
566 | 0 | if (CMS_add0_cert(cms, cert)) |
567 | 0 | return 1; |
568 | 0 | X509_free(cert); |
569 | 0 | return 0; |
570 | 0 | } |
571 | | |
572 | | static STACK_OF(CMS_RevocationInfoChoice) |
573 | | ** |
574 | | cms_get0_revocation_choices(CMS_ContentInfo *cms) |
575 | 0 | { |
576 | 0 | switch (OBJ_obj2nid(cms->contentType)) { |
577 | | |
578 | 0 | case NID_pkcs7_signed: |
579 | 0 | return &cms->d.signedData->crls; |
580 | | |
581 | 0 | case NID_pkcs7_enveloped: |
582 | 0 | if (cms->d.envelopedData->originatorInfo == NULL) |
583 | 0 | return NULL; |
584 | 0 | return &cms->d.envelopedData->originatorInfo->crls; |
585 | | |
586 | 0 | case NID_id_smime_ct_authEnvelopedData: |
587 | 0 | if (cms->d.authEnvelopedData->originatorInfo == NULL) |
588 | 0 | return NULL; |
589 | 0 | return &cms->d.authEnvelopedData->originatorInfo->crls; |
590 | | |
591 | 0 | default: |
592 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_TYPE); |
593 | 0 | return NULL; |
594 | 0 | } |
595 | 0 | } |
596 | | |
597 | | CMS_RevocationInfoChoice *CMS_add0_RevocationInfoChoice(CMS_ContentInfo *cms) |
598 | 0 | { |
599 | 0 | STACK_OF(CMS_RevocationInfoChoice) **pcrls; |
600 | 0 | CMS_RevocationInfoChoice *rch; |
601 | |
|
602 | 0 | pcrls = cms_get0_revocation_choices(cms); |
603 | 0 | if (pcrls == NULL) |
604 | 0 | return NULL; |
605 | 0 | if (*pcrls == NULL) |
606 | 0 | *pcrls = sk_CMS_RevocationInfoChoice_new_null(); |
607 | 0 | if (*pcrls == NULL) |
608 | 0 | return NULL; |
609 | 0 | rch = M_ASN1_new_of(CMS_RevocationInfoChoice); |
610 | 0 | if (rch == NULL) |
611 | 0 | return NULL; |
612 | 0 | if (!sk_CMS_RevocationInfoChoice_push(*pcrls, rch)) { |
613 | 0 | M_ASN1_free_of(rch, CMS_RevocationInfoChoice); |
614 | 0 | return NULL; |
615 | 0 | } |
616 | 0 | return rch; |
617 | 0 | } |
618 | | |
619 | | int CMS_add0_crl(CMS_ContentInfo *cms, X509_CRL *crl) |
620 | 0 | { |
621 | 0 | CMS_RevocationInfoChoice *rch = CMS_add0_RevocationInfoChoice(cms); |
622 | |
|
623 | 0 | if (rch == NULL) |
624 | 0 | return 0; |
625 | 0 | rch->type = CMS_REVCHOICE_CRL; |
626 | 0 | rch->d.crl = crl; |
627 | 0 | return 1; |
628 | 0 | } |
629 | | |
630 | | int CMS_add1_crl(CMS_ContentInfo *cms, X509_CRL *crl) |
631 | 0 | { |
632 | 0 | if (!X509_CRL_up_ref(crl)) |
633 | 0 | return 0; |
634 | 0 | if (CMS_add0_crl(cms, crl)) |
635 | 0 | return 1; |
636 | 0 | X509_CRL_free(crl); |
637 | 0 | return 0; |
638 | 0 | } |
639 | | |
640 | | STACK_OF(X509) *CMS_get1_certs(CMS_ContentInfo *cms) |
641 | 0 | { |
642 | 0 | STACK_OF(X509) *certs = NULL; |
643 | |
|
644 | 0 | if (!ossl_cms_get1_certs_ex(cms, &certs)) |
645 | 0 | return NULL; |
646 | 0 | if (sk_X509_num(certs) == 0) { |
647 | 0 | sk_X509_free(certs); |
648 | 0 | return NULL; |
649 | 0 | } |
650 | 0 | return certs; |
651 | 0 | } |
652 | | |
653 | | int ossl_cms_get1_certs_ex(CMS_ContentInfo *cms, STACK_OF(X509) **certs) |
654 | 0 | { |
655 | 0 | CMS_CertificateChoices *cch; |
656 | 0 | STACK_OF(CMS_CertificateChoices) **pcerts; |
657 | 0 | int i, n; |
658 | |
|
659 | 0 | if (certs == NULL) |
660 | 0 | return 0; |
661 | 0 | *certs = NULL; |
662 | 0 | pcerts = cms_get0_certificate_choices(cms); |
663 | 0 | if (pcerts == NULL) |
664 | 0 | return 0; |
665 | | |
666 | | /* make sure to return NULL *certs only on error */ |
667 | 0 | n = sk_CMS_CertificateChoices_num(*pcerts); |
668 | 0 | if ((*certs = sk_X509_new_reserve(NULL, n)) == NULL) |
669 | 0 | return 0; |
670 | | |
671 | 0 | for (i = 0; i < n; i++) { |
672 | 0 | cch = sk_CMS_CertificateChoices_value(*pcerts, i); |
673 | 0 | if (cch->type == 0) { |
674 | 0 | if (!X509_add_cert(*certs, cch->d.certificate, |
675 | 0 | X509_ADD_FLAG_UP_REF)) { |
676 | 0 | OSSL_STACK_OF_X509_free(*certs); |
677 | 0 | *certs = NULL; |
678 | 0 | return 0; |
679 | 0 | } |
680 | 0 | } |
681 | 0 | } |
682 | 0 | return 1; |
683 | 0 | } |
684 | | |
685 | | STACK_OF(X509_CRL) *CMS_get1_crls(CMS_ContentInfo *cms) |
686 | 0 | { |
687 | 0 | STACK_OF(X509_CRL) *crls = NULL; |
688 | |
|
689 | 0 | if (!ossl_cms_get1_crls_ex(cms, &crls)) |
690 | 0 | return NULL; |
691 | 0 | if (sk_X509_CRL_num(crls) == 0) { |
692 | 0 | sk_X509_CRL_free(crls); |
693 | 0 | return NULL; |
694 | 0 | } |
695 | 0 | return crls; |
696 | 0 | } |
697 | | |
698 | | int ossl_cms_get1_crls_ex(CMS_ContentInfo *cms, STACK_OF(X509_CRL) **crls) |
699 | 0 | { |
700 | 0 | STACK_OF(CMS_RevocationInfoChoice) **pcrls; |
701 | 0 | CMS_RevocationInfoChoice *rch; |
702 | 0 | int i, n; |
703 | |
|
704 | 0 | if (crls == NULL) |
705 | 0 | return 0; |
706 | 0 | *crls = NULL; |
707 | 0 | pcrls = cms_get0_revocation_choices(cms); |
708 | 0 | if (pcrls == NULL) |
709 | 0 | return 0; |
710 | | |
711 | | /* make sure to return NULL *crls only on error */ |
712 | 0 | n = sk_CMS_RevocationInfoChoice_num(*pcrls); |
713 | 0 | if ((*crls = sk_X509_CRL_new_reserve(NULL, n)) == NULL) |
714 | 0 | return 0; |
715 | | |
716 | 0 | for (i = 0; i < n; i++) { |
717 | 0 | rch = sk_CMS_RevocationInfoChoice_value(*pcrls, i); |
718 | 0 | if (rch->type == 0) { |
719 | 0 | if (!X509_CRL_up_ref(rch->d.crl) |
720 | 0 | || !ossl_assert(sk_X509_CRL_push(*crls, rch->d.crl))) { |
721 | | /* push cannot fail on reserved stack */ |
722 | 0 | sk_X509_CRL_pop_free(*crls, X509_CRL_free); |
723 | 0 | *crls = NULL; |
724 | 0 | return 0; |
725 | 0 | } |
726 | 0 | } |
727 | 0 | } |
728 | 0 | return 1; |
729 | 0 | } |
730 | | |
731 | | int ossl_cms_ias_cert_cmp(CMS_IssuerAndSerialNumber *ias, X509 *cert) |
732 | 0 | { |
733 | 0 | int ret; |
734 | 0 | ret = X509_NAME_cmp(ias->issuer, X509_get_issuer_name(cert)); |
735 | 0 | if (ret) |
736 | 0 | return ret; |
737 | 0 | return ASN1_INTEGER_cmp(ias->serialNumber, X509_get0_serialNumber(cert)); |
738 | 0 | } |
739 | | |
740 | | int ossl_cms_keyid_cert_cmp(ASN1_OCTET_STRING *keyid, X509 *cert) |
741 | 0 | { |
742 | 0 | const ASN1_OCTET_STRING *cert_keyid = X509_get0_subject_key_id(cert); |
743 | |
|
744 | 0 | if (cert_keyid == NULL) |
745 | 0 | return -1; |
746 | 0 | return ASN1_OCTET_STRING_cmp(keyid, cert_keyid); |
747 | 0 | } |
748 | | |
749 | | int ossl_cms_set1_ias(CMS_IssuerAndSerialNumber **pias, X509 *cert) |
750 | 0 | { |
751 | 0 | CMS_IssuerAndSerialNumber *ias; |
752 | 0 | ias = M_ASN1_new_of(CMS_IssuerAndSerialNumber); |
753 | 0 | if (!ias) { |
754 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB); |
755 | 0 | goto err; |
756 | 0 | } |
757 | 0 | if (!X509_NAME_set(&ias->issuer, X509_get_issuer_name(cert))) { |
758 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_X509_LIB); |
759 | 0 | goto err; |
760 | 0 | } |
761 | 0 | if (!ASN1_STRING_copy(ias->serialNumber, X509_get0_serialNumber(cert))) { |
762 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB); |
763 | 0 | goto err; |
764 | 0 | } |
765 | 0 | M_ASN1_free_of(*pias, CMS_IssuerAndSerialNumber); |
766 | 0 | *pias = ias; |
767 | 0 | return 1; |
768 | 0 | err: |
769 | 0 | M_ASN1_free_of(ias, CMS_IssuerAndSerialNumber); |
770 | 0 | return 0; |
771 | 0 | } |
772 | | |
773 | | int ossl_cms_set1_keyid(ASN1_OCTET_STRING **pkeyid, X509 *cert) |
774 | 0 | { |
775 | 0 | ASN1_OCTET_STRING *keyid = NULL; |
776 | 0 | const ASN1_OCTET_STRING *cert_keyid; |
777 | 0 | cert_keyid = X509_get0_subject_key_id(cert); |
778 | 0 | if (cert_keyid == NULL) { |
779 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_CERTIFICATE_HAS_NO_KEYID); |
780 | 0 | return 0; |
781 | 0 | } |
782 | 0 | keyid = ASN1_STRING_dup(cert_keyid); |
783 | 0 | if (!keyid) { |
784 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB); |
785 | 0 | return 0; |
786 | 0 | } |
787 | 0 | ASN1_OCTET_STRING_free(*pkeyid); |
788 | 0 | *pkeyid = keyid; |
789 | 0 | return 1; |
790 | 0 | } |
791 | | |
792 | | CMS_EnvelopedData *ossl_cms_sign_encrypt(BIO *data, X509 *sign_cert, STACK_OF(X509) *certs, |
793 | | EVP_PKEY *sign_key, unsigned int sign_flags, |
794 | | STACK_OF(X509) *enc_recip, const EVP_CIPHER *cipher, |
795 | | unsigned int enc_flags, OSSL_LIB_CTX *libctx, |
796 | | const char *propq) |
797 | 0 | { |
798 | 0 | CMS_EnvelopedData *evd = NULL; |
799 | 0 | BIO *privbio = NULL, *signbio = NULL; |
800 | 0 | CMS_ContentInfo *signcms = NULL, *evpcms = NULL; |
801 | |
|
802 | 0 | if (data == NULL || sign_key == NULL || sign_cert == NULL || enc_recip == NULL) { |
803 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_PASSED_NULL_PARAMETER); |
804 | 0 | return NULL; |
805 | 0 | } |
806 | 0 | signcms = CMS_sign_ex(sign_cert, sign_key, certs, data, sign_flags, libctx, propq); |
807 | 0 | if (signcms == NULL) |
808 | 0 | goto err; |
809 | | |
810 | 0 | signbio = BIO_new(BIO_s_mem()); |
811 | 0 | if (signbio == NULL |
812 | 0 | || ASN1_item_i2d_bio(ASN1_ITEM_rptr(CMS_SignedData), signbio, signcms->d.signedData) <= 0) |
813 | 0 | goto err; |
814 | | |
815 | 0 | evpcms = CMS_encrypt_ex(enc_recip, signbio, cipher, enc_flags, libctx, propq); |
816 | 0 | if (evpcms == NULL) |
817 | 0 | goto err; |
818 | 0 | evd = CMS_EnvelopedData_dup(evpcms->d.envelopedData); |
819 | |
|
820 | 0 | err: |
821 | 0 | BIO_free(privbio); |
822 | 0 | BIO_free(signbio); |
823 | 0 | CMS_ContentInfo_free(signcms); |
824 | 0 | CMS_ContentInfo_free(evpcms); |
825 | |
|
826 | 0 | return evd; |
827 | 0 | } |