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