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