/src/openssl31/crypto/cms/cms_lib.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2008-2023 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 "internal/sizes.h" |
18 | | #include "crypto/x509.h" |
19 | | #include "cms_local.h" |
20 | | |
21 | | static STACK_OF(CMS_CertificateChoices) |
22 | | **cms_get0_certificate_choices(CMS_ContentInfo *cms); |
23 | | |
24 | | IMPLEMENT_ASN1_PRINT_FUNCTION(CMS_ContentInfo) |
25 | | |
26 | | CMS_ContentInfo *d2i_CMS_ContentInfo(CMS_ContentInfo **a, |
27 | | const unsigned char **in, long len) |
28 | 0 | { |
29 | 0 | CMS_ContentInfo *ci; |
30 | 0 | const CMS_CTX *ctx = ossl_cms_get0_cmsctx(a == NULL ? NULL : *a); |
31 | |
|
32 | 0 | ci = (CMS_ContentInfo *)ASN1_item_d2i_ex((ASN1_VALUE **)a, in, len, |
33 | 0 | (CMS_ContentInfo_it()), |
34 | 0 | ossl_cms_ctx_get0_libctx(ctx), |
35 | 0 | ossl_cms_ctx_get0_propq(ctx)); |
36 | 0 | if (ci != NULL) { |
37 | 0 | ERR_set_mark(); |
38 | 0 | ossl_cms_resolve_libctx(ci); |
39 | 0 | ERR_pop_to_mark(); |
40 | 0 | } |
41 | 0 | return ci; |
42 | 0 | } |
43 | | |
44 | | int i2d_CMS_ContentInfo(const CMS_ContentInfo *a, unsigned char **out) |
45 | 0 | { |
46 | 0 | return ASN1_item_i2d((const ASN1_VALUE *)a, out, (CMS_ContentInfo_it())); |
47 | 0 | } |
48 | | |
49 | | CMS_ContentInfo *CMS_ContentInfo_new_ex(OSSL_LIB_CTX *libctx, const char *propq) |
50 | 0 | { |
51 | 0 | CMS_ContentInfo *ci; |
52 | |
|
53 | 0 | ci = (CMS_ContentInfo *)ASN1_item_new_ex(ASN1_ITEM_rptr(CMS_ContentInfo), |
54 | 0 | libctx, propq); |
55 | 0 | if (ci != NULL) { |
56 | 0 | ci->ctx.libctx = libctx; |
57 | 0 | ci->ctx.propq = NULL; |
58 | 0 | if (propq != NULL) { |
59 | 0 | ci->ctx.propq = OPENSSL_strdup(propq); |
60 | 0 | if (ci->ctx.propq == NULL) { |
61 | 0 | CMS_ContentInfo_free(ci); |
62 | 0 | ci = NULL; |
63 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE); |
64 | 0 | } |
65 | 0 | } |
66 | 0 | } |
67 | 0 | return ci; |
68 | 0 | } |
69 | | |
70 | | CMS_ContentInfo *CMS_ContentInfo_new(void) |
71 | 0 | { |
72 | 0 | return CMS_ContentInfo_new_ex(NULL, NULL); |
73 | 0 | } |
74 | | |
75 | | void CMS_ContentInfo_free(CMS_ContentInfo *cms) |
76 | 2.05k | { |
77 | 2.05k | if (cms != NULL) { |
78 | 2.05k | ossl_cms_env_enc_content_free(cms); |
79 | 2.05k | OPENSSL_free(cms->ctx.propq); |
80 | 2.05k | ASN1_item_free((ASN1_VALUE *)cms, ASN1_ITEM_rptr(CMS_ContentInfo)); |
81 | 2.05k | } |
82 | 2.05k | } |
83 | | |
84 | | const CMS_CTX *ossl_cms_get0_cmsctx(const CMS_ContentInfo *cms) |
85 | 47.4k | { |
86 | 47.4k | return cms != NULL ? &cms->ctx : NULL; |
87 | 47.4k | } |
88 | | |
89 | | OSSL_LIB_CTX *ossl_cms_ctx_get0_libctx(const CMS_CTX *ctx) |
90 | 40.0k | { |
91 | 40.0k | return ctx != NULL ? ctx->libctx : NULL; |
92 | 40.0k | } |
93 | | |
94 | | const char *ossl_cms_ctx_get0_propq(const CMS_CTX *ctx) |
95 | 40.0k | { |
96 | 40.0k | return ctx != NULL ? ctx->propq : NULL; |
97 | 40.0k | } |
98 | | |
99 | | void ossl_cms_resolve_libctx(CMS_ContentInfo *ci) |
100 | 4.17k | { |
101 | 4.17k | int i; |
102 | 4.17k | CMS_CertificateChoices *cch; |
103 | 4.17k | STACK_OF(CMS_CertificateChoices) **pcerts; |
104 | 4.17k | const CMS_CTX *ctx = ossl_cms_get0_cmsctx(ci); |
105 | 4.17k | OSSL_LIB_CTX *libctx = ossl_cms_ctx_get0_libctx(ctx); |
106 | 4.17k | const char *propq = ossl_cms_ctx_get0_propq(ctx); |
107 | | |
108 | 4.17k | ossl_cms_SignerInfos_set_cmsctx(ci); |
109 | 4.17k | ossl_cms_RecipientInfos_set_cmsctx(ci); |
110 | | |
111 | 4.17k | pcerts = cms_get0_certificate_choices(ci); |
112 | 4.17k | if (pcerts != NULL) { |
113 | 1.69M | for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) { |
114 | 1.69M | cch = sk_CMS_CertificateChoices_value(*pcerts, i); |
115 | 1.69M | if (cch->type == CMS_CERTCHOICE_CERT) |
116 | 618 | ossl_x509_set0_libctx(cch->d.certificate, libctx, propq); |
117 | 1.69M | } |
118 | 1.09k | } |
119 | 4.17k | } |
120 | | |
121 | | const ASN1_OBJECT *CMS_get0_type(const CMS_ContentInfo *cms) |
122 | 0 | { |
123 | 0 | return cms->contentType; |
124 | 0 | } |
125 | | |
126 | | CMS_ContentInfo *ossl_cms_Data_create(OSSL_LIB_CTX *libctx, const char *propq) |
127 | 0 | { |
128 | 0 | CMS_ContentInfo *cms = CMS_ContentInfo_new_ex(libctx, propq); |
129 | |
|
130 | 0 | if (cms != NULL) { |
131 | 0 | cms->contentType = OBJ_nid2obj(NID_pkcs7_data); |
132 | | /* Never detached */ |
133 | 0 | CMS_set_detached(cms, 0); |
134 | 0 | } |
135 | 0 | return cms; |
136 | 0 | } |
137 | | |
138 | | BIO *ossl_cms_content_bio(CMS_ContentInfo *cms) |
139 | 0 | { |
140 | 0 | ASN1_OCTET_STRING **pos = CMS_get0_content(cms); |
141 | |
|
142 | 0 | if (pos == NULL) |
143 | 0 | return NULL; |
144 | | /* If content detached data goes nowhere: create NULL BIO */ |
145 | 0 | if (*pos == NULL) |
146 | 0 | return BIO_new(BIO_s_null()); |
147 | | /* |
148 | | * If content not detached and created return memory BIO |
149 | | */ |
150 | 0 | if (*pos == NULL || ((*pos)->flags == ASN1_STRING_FLAG_CONT)) |
151 | 0 | return BIO_new(BIO_s_mem()); |
152 | | /* Else content was read in: return read only BIO for it */ |
153 | 0 | return BIO_new_mem_buf((*pos)->data, (*pos)->length); |
154 | 0 | } |
155 | | |
156 | | BIO *CMS_dataInit(CMS_ContentInfo *cms, BIO *icont) |
157 | 0 | { |
158 | 0 | BIO *cmsbio, *cont; |
159 | 0 | if (icont) |
160 | 0 | cont = icont; |
161 | 0 | else |
162 | 0 | cont = ossl_cms_content_bio(cms); |
163 | 0 | if (!cont) { |
164 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_NO_CONTENT); |
165 | 0 | return NULL; |
166 | 0 | } |
167 | 0 | switch (OBJ_obj2nid(cms->contentType)) { |
168 | | |
169 | 0 | case NID_pkcs7_data: |
170 | 0 | return cont; |
171 | | |
172 | 0 | case NID_pkcs7_signed: |
173 | 0 | cmsbio = ossl_cms_SignedData_init_bio(cms); |
174 | 0 | break; |
175 | | |
176 | 0 | case NID_pkcs7_digest: |
177 | 0 | cmsbio = ossl_cms_DigestedData_init_bio(cms); |
178 | 0 | break; |
179 | | #ifdef ZLIB |
180 | | case NID_id_smime_ct_compressedData: |
181 | | cmsbio = ossl_cms_CompressedData_init_bio(cms); |
182 | | break; |
183 | | #endif |
184 | | |
185 | 0 | case NID_pkcs7_encrypted: |
186 | 0 | cmsbio = ossl_cms_EncryptedData_init_bio(cms); |
187 | 0 | break; |
188 | | |
189 | 0 | case NID_pkcs7_enveloped: |
190 | 0 | cmsbio = ossl_cms_EnvelopedData_init_bio(cms); |
191 | 0 | break; |
192 | | |
193 | 0 | case NID_id_smime_ct_authEnvelopedData: |
194 | 0 | cmsbio = ossl_cms_AuthEnvelopedData_init_bio(cms); |
195 | 0 | break; |
196 | | |
197 | 0 | default: |
198 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_TYPE); |
199 | 0 | goto err; |
200 | 0 | } |
201 | | |
202 | 0 | if (cmsbio) |
203 | 0 | return BIO_push(cmsbio, cont); |
204 | 0 | err: |
205 | 0 | if (!icont) |
206 | 0 | BIO_free(cont); |
207 | 0 | return NULL; |
208 | |
|
209 | 0 | } |
210 | | |
211 | | /* unfortunately cannot constify SMIME_write_ASN1() due to this function */ |
212 | | int CMS_dataFinal(CMS_ContentInfo *cms, BIO *cmsbio) |
213 | 0 | { |
214 | 0 | ASN1_OCTET_STRING **pos = CMS_get0_content(cms); |
215 | |
|
216 | 0 | if (pos == NULL) |
217 | 0 | return 0; |
218 | | /* If embedded content find memory BIO and set content */ |
219 | 0 | if (*pos && ((*pos)->flags & ASN1_STRING_FLAG_CONT)) { |
220 | 0 | BIO *mbio; |
221 | 0 | unsigned char *cont; |
222 | 0 | long contlen; |
223 | 0 | mbio = BIO_find_type(cmsbio, BIO_TYPE_MEM); |
224 | 0 | if (!mbio) { |
225 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_NOT_FOUND); |
226 | 0 | return 0; |
227 | 0 | } |
228 | 0 | contlen = BIO_get_mem_data(mbio, &cont); |
229 | | /* Set bio as read only so its content can't be clobbered */ |
230 | 0 | BIO_set_flags(mbio, BIO_FLAGS_MEM_RDONLY); |
231 | 0 | BIO_set_mem_eof_return(mbio, 0); |
232 | 0 | ASN1_STRING_set0(*pos, cont, contlen); |
233 | 0 | (*pos)->flags &= ~ASN1_STRING_FLAG_CONT; |
234 | 0 | } |
235 | | |
236 | 0 | switch (OBJ_obj2nid(cms->contentType)) { |
237 | | |
238 | 0 | case NID_pkcs7_data: |
239 | 0 | case NID_pkcs7_encrypted: |
240 | 0 | case NID_id_smime_ct_compressedData: |
241 | | /* Nothing to do */ |
242 | 0 | return 1; |
243 | | |
244 | 0 | case NID_pkcs7_enveloped: |
245 | 0 | return ossl_cms_EnvelopedData_final(cms, cmsbio); |
246 | | |
247 | 0 | case NID_id_smime_ct_authEnvelopedData: |
248 | 0 | return ossl_cms_AuthEnvelopedData_final(cms, cmsbio); |
249 | | |
250 | 0 | case NID_pkcs7_signed: |
251 | 0 | return ossl_cms_SignedData_final(cms, cmsbio); |
252 | | |
253 | 0 | case NID_pkcs7_digest: |
254 | 0 | return ossl_cms_DigestedData_do_final(cms, cmsbio, 0); |
255 | | |
256 | 0 | default: |
257 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_TYPE); |
258 | 0 | return 0; |
259 | 0 | } |
260 | 0 | } |
261 | | |
262 | | /* |
263 | | * Return an OCTET STRING pointer to content. This allows it to be accessed |
264 | | * or set later. |
265 | | */ |
266 | | |
267 | | ASN1_OCTET_STRING **CMS_get0_content(CMS_ContentInfo *cms) |
268 | 0 | { |
269 | 0 | switch (OBJ_obj2nid(cms->contentType)) { |
270 | | |
271 | 0 | case NID_pkcs7_data: |
272 | 0 | return &cms->d.data; |
273 | | |
274 | 0 | case NID_pkcs7_signed: |
275 | 0 | return &cms->d.signedData->encapContentInfo->eContent; |
276 | | |
277 | 0 | case NID_pkcs7_enveloped: |
278 | 0 | return &cms->d.envelopedData->encryptedContentInfo->encryptedContent; |
279 | | |
280 | 0 | case NID_pkcs7_digest: |
281 | 0 | return &cms->d.digestedData->encapContentInfo->eContent; |
282 | | |
283 | 0 | case NID_pkcs7_encrypted: |
284 | 0 | return &cms->d.encryptedData->encryptedContentInfo->encryptedContent; |
285 | | |
286 | 0 | case NID_id_smime_ct_authEnvelopedData: |
287 | 0 | return &cms->d.authEnvelopedData->authEncryptedContentInfo |
288 | 0 | ->encryptedContent; |
289 | | |
290 | 0 | case NID_id_smime_ct_authData: |
291 | 0 | return &cms->d.authenticatedData->encapContentInfo->eContent; |
292 | | |
293 | 0 | case NID_id_smime_ct_compressedData: |
294 | 0 | return &cms->d.compressedData->encapContentInfo->eContent; |
295 | | |
296 | 0 | default: |
297 | 0 | if (cms->d.other->type == V_ASN1_OCTET_STRING) |
298 | 0 | return &cms->d.other->value.octet_string; |
299 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_TYPE); |
300 | 0 | return NULL; |
301 | |
|
302 | 0 | } |
303 | 0 | } |
304 | | |
305 | | /* |
306 | | * Return an ASN1_OBJECT pointer to content type. This allows it to be |
307 | | * accessed or set later. |
308 | | */ |
309 | | |
310 | | static ASN1_OBJECT **cms_get0_econtent_type(CMS_ContentInfo *cms) |
311 | 0 | { |
312 | 0 | switch (OBJ_obj2nid(cms->contentType)) { |
313 | | |
314 | 0 | case NID_pkcs7_signed: |
315 | 0 | return &cms->d.signedData->encapContentInfo->eContentType; |
316 | | |
317 | 0 | case NID_pkcs7_enveloped: |
318 | 0 | return &cms->d.envelopedData->encryptedContentInfo->contentType; |
319 | | |
320 | 0 | case NID_pkcs7_digest: |
321 | 0 | return &cms->d.digestedData->encapContentInfo->eContentType; |
322 | | |
323 | 0 | case NID_pkcs7_encrypted: |
324 | 0 | return &cms->d.encryptedData->encryptedContentInfo->contentType; |
325 | | |
326 | 0 | case NID_id_smime_ct_authEnvelopedData: |
327 | 0 | return &cms->d.authEnvelopedData->authEncryptedContentInfo |
328 | 0 | ->contentType; |
329 | 0 | case NID_id_smime_ct_authData: |
330 | 0 | return &cms->d.authenticatedData->encapContentInfo->eContentType; |
331 | | |
332 | 0 | case NID_id_smime_ct_compressedData: |
333 | 0 | return &cms->d.compressedData->encapContentInfo->eContentType; |
334 | | |
335 | 0 | default: |
336 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_TYPE); |
337 | 0 | return NULL; |
338 | |
|
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_MALLOC_FAILURE); |
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 | const EVP_MD *digest = NULL; |
413 | 0 | EVP_MD *fetched_digest = NULL; |
414 | 0 | char alg[OSSL_MAX_NAME_SIZE]; |
415 | |
|
416 | 0 | X509_ALGOR_get0(&digestoid, NULL, NULL, digestAlgorithm); |
417 | 0 | OBJ_obj2txt(alg, sizeof(alg), digestoid, 0); |
418 | |
|
419 | 0 | (void)ERR_set_mark(); |
420 | 0 | fetched_digest = EVP_MD_fetch(ossl_cms_ctx_get0_libctx(ctx), alg, |
421 | 0 | ossl_cms_ctx_get0_propq(ctx)); |
422 | |
|
423 | 0 | if (fetched_digest != NULL) |
424 | 0 | digest = fetched_digest; |
425 | 0 | else |
426 | 0 | digest = EVP_get_digestbyobj(digestoid); |
427 | 0 | if (digest == NULL) { |
428 | 0 | (void)ERR_clear_last_mark(); |
429 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_UNKNOWN_DIGEST_ALGORITHM); |
430 | 0 | goto err; |
431 | 0 | } |
432 | 0 | (void)ERR_pop_to_mark(); |
433 | |
|
434 | 0 | mdbio = BIO_new(BIO_f_md()); |
435 | 0 | if (mdbio == NULL || BIO_set_md(mdbio, digest) <= 0) { |
436 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_MD_BIO_INIT_ERROR); |
437 | 0 | goto err; |
438 | 0 | } |
439 | 0 | EVP_MD_free(fetched_digest); |
440 | 0 | return mdbio; |
441 | 0 | err: |
442 | 0 | EVP_MD_free(fetched_digest); |
443 | 0 | BIO_free(mdbio); |
444 | 0 | return NULL; |
445 | 0 | } |
446 | | |
447 | | /* Locate a message digest content from a BIO chain based on SignerInfo */ |
448 | | |
449 | | int ossl_cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain, |
450 | | X509_ALGOR *mdalg) |
451 | 0 | { |
452 | 0 | int nid; |
453 | 0 | const ASN1_OBJECT *mdoid; |
454 | 0 | X509_ALGOR_get0(&mdoid, NULL, NULL, mdalg); |
455 | 0 | nid = OBJ_obj2nid(mdoid); |
456 | | /* Look for digest type to match signature */ |
457 | 0 | for (;;) { |
458 | 0 | EVP_MD_CTX *mtmp; |
459 | 0 | chain = BIO_find_type(chain, BIO_TYPE_MD); |
460 | 0 | if (chain == NULL) { |
461 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_DIGEST); |
462 | 0 | return 0; |
463 | 0 | } |
464 | 0 | BIO_get_md_ctx(chain, &mtmp); |
465 | 0 | if (EVP_MD_CTX_get_type(mtmp) == nid |
466 | | /* |
467 | | * Workaround for broken implementations that use signature |
468 | | * algorithm OID instead of digest. |
469 | | */ |
470 | 0 | || EVP_MD_get_pkey_type(EVP_MD_CTX_get0_md(mtmp)) == nid) |
471 | 0 | return EVP_MD_CTX_copy_ex(mctx, mtmp); |
472 | 0 | chain = BIO_next(chain); |
473 | 0 | } |
474 | 0 | } |
475 | | |
476 | | static STACK_OF(CMS_CertificateChoices) |
477 | | **cms_get0_certificate_choices(CMS_ContentInfo *cms) |
478 | 4.17k | { |
479 | 4.17k | switch (OBJ_obj2nid(cms->contentType)) { |
480 | | |
481 | 1.08k | case NID_pkcs7_signed: |
482 | 1.08k | return &cms->d.signedData->certificates; |
483 | | |
484 | 1.25k | case NID_pkcs7_enveloped: |
485 | 1.25k | if (cms->d.envelopedData->originatorInfo == NULL) |
486 | 1.25k | return NULL; |
487 | 4 | return &cms->d.envelopedData->originatorInfo->certificates; |
488 | | |
489 | 0 | case NID_id_smime_ct_authEnvelopedData: |
490 | 0 | if (cms->d.authEnvelopedData->originatorInfo == NULL) |
491 | 0 | return NULL; |
492 | 0 | return &cms->d.authEnvelopedData->originatorInfo->certificates; |
493 | | |
494 | 1.83k | default: |
495 | 1.83k | ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_TYPE); |
496 | 1.83k | return NULL; |
497 | | |
498 | 4.17k | } |
499 | 4.17k | } |
500 | | |
501 | | CMS_CertificateChoices *CMS_add0_CertificateChoices(CMS_ContentInfo *cms) |
502 | 0 | { |
503 | 0 | STACK_OF(CMS_CertificateChoices) **pcerts; |
504 | 0 | CMS_CertificateChoices *cch; |
505 | |
|
506 | 0 | pcerts = cms_get0_certificate_choices(cms); |
507 | 0 | if (pcerts == NULL) |
508 | 0 | return NULL; |
509 | 0 | if (*pcerts == NULL) |
510 | 0 | *pcerts = sk_CMS_CertificateChoices_new_null(); |
511 | 0 | if (*pcerts == NULL) |
512 | 0 | return NULL; |
513 | 0 | cch = M_ASN1_new_of(CMS_CertificateChoices); |
514 | 0 | if (!cch) |
515 | 0 | return NULL; |
516 | 0 | if (!sk_CMS_CertificateChoices_push(*pcerts, cch)) { |
517 | 0 | M_ASN1_free_of(cch, CMS_CertificateChoices); |
518 | 0 | return NULL; |
519 | 0 | } |
520 | 0 | return cch; |
521 | 0 | } |
522 | | |
523 | | int CMS_add0_cert(CMS_ContentInfo *cms, X509 *cert) |
524 | 0 | { |
525 | 0 | CMS_CertificateChoices *cch; |
526 | 0 | STACK_OF(CMS_CertificateChoices) **pcerts; |
527 | 0 | int i; |
528 | |
|
529 | 0 | pcerts = cms_get0_certificate_choices(cms); |
530 | 0 | if (pcerts == NULL) |
531 | 0 | return 0; |
532 | 0 | for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) { |
533 | 0 | cch = sk_CMS_CertificateChoices_value(*pcerts, i); |
534 | 0 | if (cch->type == CMS_CERTCHOICE_CERT) { |
535 | 0 | if (!X509_cmp(cch->d.certificate, cert)) { |
536 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_CERTIFICATE_ALREADY_PRESENT); |
537 | 0 | return 0; |
538 | 0 | } |
539 | 0 | } |
540 | 0 | } |
541 | 0 | cch = CMS_add0_CertificateChoices(cms); |
542 | 0 | if (!cch) |
543 | 0 | return 0; |
544 | 0 | cch->type = CMS_CERTCHOICE_CERT; |
545 | 0 | cch->d.certificate = cert; |
546 | 0 | return 1; |
547 | 0 | } |
548 | | |
549 | | int CMS_add1_cert(CMS_ContentInfo *cms, X509 *cert) |
550 | 0 | { |
551 | 0 | int r; |
552 | 0 | r = CMS_add0_cert(cms, cert); |
553 | 0 | if (r > 0) |
554 | 0 | X509_up_ref(cert); |
555 | 0 | return r; |
556 | 0 | } |
557 | | |
558 | | static STACK_OF(CMS_RevocationInfoChoice) |
559 | | **cms_get0_revocation_choices(CMS_ContentInfo *cms) |
560 | 0 | { |
561 | 0 | switch (OBJ_obj2nid(cms->contentType)) { |
562 | | |
563 | 0 | case NID_pkcs7_signed: |
564 | 0 | return &cms->d.signedData->crls; |
565 | | |
566 | 0 | case NID_pkcs7_enveloped: |
567 | 0 | if (cms->d.envelopedData->originatorInfo == NULL) |
568 | 0 | return NULL; |
569 | 0 | return &cms->d.envelopedData->originatorInfo->crls; |
570 | | |
571 | 0 | case NID_id_smime_ct_authEnvelopedData: |
572 | 0 | if (cms->d.authEnvelopedData->originatorInfo == NULL) |
573 | 0 | return NULL; |
574 | 0 | return &cms->d.authEnvelopedData->originatorInfo->crls; |
575 | | |
576 | 0 | default: |
577 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_CONTENT_TYPE); |
578 | 0 | return NULL; |
579 | |
|
580 | 0 | } |
581 | 0 | } |
582 | | |
583 | | CMS_RevocationInfoChoice *CMS_add0_RevocationInfoChoice(CMS_ContentInfo *cms) |
584 | 0 | { |
585 | 0 | STACK_OF(CMS_RevocationInfoChoice) **pcrls; |
586 | 0 | CMS_RevocationInfoChoice *rch; |
587 | |
|
588 | 0 | pcrls = cms_get0_revocation_choices(cms); |
589 | 0 | if (pcrls == NULL) |
590 | 0 | return NULL; |
591 | 0 | if (*pcrls == NULL) |
592 | 0 | *pcrls = sk_CMS_RevocationInfoChoice_new_null(); |
593 | 0 | if (*pcrls == NULL) |
594 | 0 | return NULL; |
595 | 0 | rch = M_ASN1_new_of(CMS_RevocationInfoChoice); |
596 | 0 | if (rch == NULL) |
597 | 0 | return NULL; |
598 | 0 | if (!sk_CMS_RevocationInfoChoice_push(*pcrls, rch)) { |
599 | 0 | M_ASN1_free_of(rch, CMS_RevocationInfoChoice); |
600 | 0 | return NULL; |
601 | 0 | } |
602 | 0 | return rch; |
603 | 0 | } |
604 | | |
605 | | int CMS_add0_crl(CMS_ContentInfo *cms, X509_CRL *crl) |
606 | 0 | { |
607 | 0 | CMS_RevocationInfoChoice *rch; |
608 | 0 | rch = CMS_add0_RevocationInfoChoice(cms); |
609 | 0 | if (!rch) |
610 | 0 | return 0; |
611 | 0 | rch->type = CMS_REVCHOICE_CRL; |
612 | 0 | rch->d.crl = crl; |
613 | 0 | return 1; |
614 | 0 | } |
615 | | |
616 | | int CMS_add1_crl(CMS_ContentInfo *cms, X509_CRL *crl) |
617 | 0 | { |
618 | 0 | if (!X509_CRL_up_ref(crl)) |
619 | 0 | return 0; |
620 | 0 | if (CMS_add0_crl(cms, crl)) |
621 | 0 | return 1; |
622 | 0 | X509_CRL_free(crl); |
623 | 0 | return 0; |
624 | 0 | } |
625 | | |
626 | | STACK_OF(X509) *CMS_get1_certs(CMS_ContentInfo *cms) |
627 | 0 | { |
628 | 0 | STACK_OF(X509) *certs = NULL; |
629 | 0 | CMS_CertificateChoices *cch; |
630 | 0 | STACK_OF(CMS_CertificateChoices) **pcerts; |
631 | 0 | int i; |
632 | |
|
633 | 0 | pcerts = cms_get0_certificate_choices(cms); |
634 | 0 | if (pcerts == NULL) |
635 | 0 | return NULL; |
636 | 0 | for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) { |
637 | 0 | cch = sk_CMS_CertificateChoices_value(*pcerts, i); |
638 | 0 | if (cch->type == 0) { |
639 | 0 | if (!ossl_x509_add_cert_new(&certs, cch->d.certificate, |
640 | 0 | X509_ADD_FLAG_UP_REF)) { |
641 | 0 | sk_X509_pop_free(certs, X509_free); |
642 | 0 | return NULL; |
643 | 0 | } |
644 | 0 | } |
645 | 0 | } |
646 | 0 | return certs; |
647 | |
|
648 | 0 | } |
649 | | |
650 | | STACK_OF(X509_CRL) *CMS_get1_crls(CMS_ContentInfo *cms) |
651 | 0 | { |
652 | 0 | STACK_OF(X509_CRL) *crls = NULL; |
653 | 0 | STACK_OF(CMS_RevocationInfoChoice) **pcrls; |
654 | 0 | CMS_RevocationInfoChoice *rch; |
655 | 0 | int i; |
656 | |
|
657 | 0 | pcrls = cms_get0_revocation_choices(cms); |
658 | 0 | if (pcrls == NULL) |
659 | 0 | return NULL; |
660 | 0 | for (i = 0; i < sk_CMS_RevocationInfoChoice_num(*pcrls); i++) { |
661 | 0 | rch = sk_CMS_RevocationInfoChoice_value(*pcrls, i); |
662 | 0 | if (rch->type == 0) { |
663 | 0 | if (!crls) { |
664 | 0 | crls = sk_X509_CRL_new_null(); |
665 | 0 | if (!crls) |
666 | 0 | return NULL; |
667 | 0 | } |
668 | 0 | if (!sk_X509_CRL_push(crls, rch->d.crl)) { |
669 | 0 | sk_X509_CRL_pop_free(crls, X509_CRL_free); |
670 | 0 | return NULL; |
671 | 0 | } |
672 | 0 | X509_CRL_up_ref(rch->d.crl); |
673 | 0 | } |
674 | 0 | } |
675 | 0 | return crls; |
676 | 0 | } |
677 | | |
678 | | int ossl_cms_ias_cert_cmp(CMS_IssuerAndSerialNumber *ias, X509 *cert) |
679 | 0 | { |
680 | 0 | int ret; |
681 | 0 | ret = X509_NAME_cmp(ias->issuer, X509_get_issuer_name(cert)); |
682 | 0 | if (ret) |
683 | 0 | return ret; |
684 | 0 | return ASN1_INTEGER_cmp(ias->serialNumber, X509_get0_serialNumber(cert)); |
685 | 0 | } |
686 | | |
687 | | int ossl_cms_keyid_cert_cmp(ASN1_OCTET_STRING *keyid, X509 *cert) |
688 | 0 | { |
689 | 0 | const ASN1_OCTET_STRING *cert_keyid = X509_get0_subject_key_id(cert); |
690 | |
|
691 | 0 | if (cert_keyid == NULL) |
692 | 0 | return -1; |
693 | 0 | return ASN1_OCTET_STRING_cmp(keyid, cert_keyid); |
694 | 0 | } |
695 | | |
696 | | int ossl_cms_set1_ias(CMS_IssuerAndSerialNumber **pias, X509 *cert) |
697 | 0 | { |
698 | 0 | CMS_IssuerAndSerialNumber *ias; |
699 | 0 | ias = M_ASN1_new_of(CMS_IssuerAndSerialNumber); |
700 | 0 | if (!ias) |
701 | 0 | goto err; |
702 | 0 | if (!X509_NAME_set(&ias->issuer, X509_get_issuer_name(cert))) |
703 | 0 | goto err; |
704 | 0 | if (!ASN1_STRING_copy(ias->serialNumber, X509_get0_serialNumber(cert))) |
705 | 0 | goto err; |
706 | 0 | M_ASN1_free_of(*pias, CMS_IssuerAndSerialNumber); |
707 | 0 | *pias = ias; |
708 | 0 | return 1; |
709 | 0 | err: |
710 | 0 | M_ASN1_free_of(ias, CMS_IssuerAndSerialNumber); |
711 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE); |
712 | 0 | return 0; |
713 | 0 | } |
714 | | |
715 | | int ossl_cms_set1_keyid(ASN1_OCTET_STRING **pkeyid, X509 *cert) |
716 | 0 | { |
717 | 0 | ASN1_OCTET_STRING *keyid = NULL; |
718 | 0 | const ASN1_OCTET_STRING *cert_keyid; |
719 | 0 | cert_keyid = X509_get0_subject_key_id(cert); |
720 | 0 | if (cert_keyid == NULL) { |
721 | 0 | ERR_raise(ERR_LIB_CMS, CMS_R_CERTIFICATE_HAS_NO_KEYID); |
722 | 0 | return 0; |
723 | 0 | } |
724 | 0 | keyid = ASN1_STRING_dup(cert_keyid); |
725 | 0 | if (!keyid) { |
726 | 0 | ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE); |
727 | 0 | return 0; |
728 | 0 | } |
729 | 0 | ASN1_OCTET_STRING_free(*pkeyid); |
730 | 0 | *pkeyid = keyid; |
731 | 0 | return 1; |
732 | 0 | } |