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