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