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