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