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