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