/src/openssl/crypto/cms/cms_lib.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2008-2016 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the OpenSSL license (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 "cms_lcl.h" |
18 | | |
19 | | IMPLEMENT_ASN1_FUNCTIONS(CMS_ContentInfo) |
20 | | IMPLEMENT_ASN1_PRINT_FUNCTION(CMS_ContentInfo) |
21 | | |
22 | | const ASN1_OBJECT *CMS_get0_type(const CMS_ContentInfo *cms) |
23 | 0 | { |
24 | 0 | return cms->contentType; |
25 | 0 | } |
26 | | |
27 | | CMS_ContentInfo *cms_Data_create(void) |
28 | 0 | { |
29 | 0 | CMS_ContentInfo *cms; |
30 | 0 | cms = CMS_ContentInfo_new(); |
31 | 0 | if (cms != NULL) { |
32 | 0 | cms->contentType = OBJ_nid2obj(NID_pkcs7_data); |
33 | 0 | /* Never detached */ |
34 | 0 | CMS_set_detached(cms, 0); |
35 | 0 | } |
36 | 0 | return cms; |
37 | 0 | } |
38 | | |
39 | | BIO *cms_content_bio(CMS_ContentInfo *cms) |
40 | 0 | { |
41 | 0 | ASN1_OCTET_STRING **pos = CMS_get0_content(cms); |
42 | 0 | if (!pos) |
43 | 0 | return NULL; |
44 | 0 | /* If content detached data goes nowhere: create NULL BIO */ |
45 | 0 | if (!*pos) |
46 | 0 | return BIO_new(BIO_s_null()); |
47 | 0 | /* |
48 | 0 | * If content not detached and created return memory BIO |
49 | 0 | */ |
50 | 0 | if (!*pos || ((*pos)->flags == ASN1_STRING_FLAG_CONT)) |
51 | 0 | return BIO_new(BIO_s_mem()); |
52 | 0 | /* Else content was read in: return read only BIO for it */ |
53 | 0 | return BIO_new_mem_buf((*pos)->data, (*pos)->length); |
54 | 0 | } |
55 | | |
56 | | BIO *CMS_dataInit(CMS_ContentInfo *cms, BIO *icont) |
57 | 0 | { |
58 | 0 | BIO *cmsbio, *cont; |
59 | 0 | if (icont) |
60 | 0 | cont = icont; |
61 | 0 | else |
62 | 0 | cont = cms_content_bio(cms); |
63 | 0 | if (!cont) { |
64 | 0 | CMSerr(CMS_F_CMS_DATAINIT, CMS_R_NO_CONTENT); |
65 | 0 | return NULL; |
66 | 0 | } |
67 | 0 | switch (OBJ_obj2nid(cms->contentType)) { |
68 | 0 |
|
69 | 0 | case NID_pkcs7_data: |
70 | 0 | return cont; |
71 | 0 |
|
72 | 0 | case NID_pkcs7_signed: |
73 | 0 | cmsbio = cms_SignedData_init_bio(cms); |
74 | 0 | break; |
75 | 0 |
|
76 | 0 | case NID_pkcs7_digest: |
77 | 0 | cmsbio = cms_DigestedData_init_bio(cms); |
78 | 0 | break; |
79 | | #ifdef ZLIB |
80 | | case NID_id_smime_ct_compressedData: |
81 | | cmsbio = cms_CompressedData_init_bio(cms); |
82 | | break; |
83 | | #endif |
84 | |
|
85 | 0 | case NID_pkcs7_encrypted: |
86 | 0 | cmsbio = cms_EncryptedData_init_bio(cms); |
87 | 0 | break; |
88 | 0 |
|
89 | 0 | case NID_pkcs7_enveloped: |
90 | 0 | cmsbio = cms_EnvelopedData_init_bio(cms); |
91 | 0 | break; |
92 | 0 |
|
93 | 0 | default: |
94 | 0 | CMSerr(CMS_F_CMS_DATAINIT, CMS_R_UNSUPPORTED_TYPE); |
95 | 0 | return NULL; |
96 | 0 | } |
97 | 0 |
|
98 | 0 | if (cmsbio) |
99 | 0 | return BIO_push(cmsbio, cont); |
100 | 0 | |
101 | 0 | if (!icont) |
102 | 0 | BIO_free(cont); |
103 | 0 | return NULL; |
104 | 0 |
|
105 | 0 | } |
106 | | |
107 | | int CMS_dataFinal(CMS_ContentInfo *cms, BIO *cmsbio) |
108 | 0 | { |
109 | 0 | ASN1_OCTET_STRING **pos = CMS_get0_content(cms); |
110 | 0 | if (!pos) |
111 | 0 | return 0; |
112 | 0 | /* If embedded content find memory BIO and set content */ |
113 | 0 | if (*pos && ((*pos)->flags & ASN1_STRING_FLAG_CONT)) { |
114 | 0 | BIO *mbio; |
115 | 0 | unsigned char *cont; |
116 | 0 | long contlen; |
117 | 0 | mbio = BIO_find_type(cmsbio, BIO_TYPE_MEM); |
118 | 0 | if (!mbio) { |
119 | 0 | CMSerr(CMS_F_CMS_DATAFINAL, CMS_R_CONTENT_NOT_FOUND); |
120 | 0 | return 0; |
121 | 0 | } |
122 | 0 | contlen = BIO_get_mem_data(mbio, &cont); |
123 | 0 | /* Set bio as read only so its content can't be clobbered */ |
124 | 0 | BIO_set_flags(mbio, BIO_FLAGS_MEM_RDONLY); |
125 | 0 | BIO_set_mem_eof_return(mbio, 0); |
126 | 0 | ASN1_STRING_set0(*pos, cont, contlen); |
127 | 0 | (*pos)->flags &= ~ASN1_STRING_FLAG_CONT; |
128 | 0 | } |
129 | 0 |
|
130 | 0 | switch (OBJ_obj2nid(cms->contentType)) { |
131 | 0 |
|
132 | 0 | case NID_pkcs7_data: |
133 | 0 | case NID_pkcs7_enveloped: |
134 | 0 | case NID_pkcs7_encrypted: |
135 | 0 | case NID_id_smime_ct_compressedData: |
136 | 0 | /* Nothing to do */ |
137 | 0 | return 1; |
138 | 0 |
|
139 | 0 | case NID_pkcs7_signed: |
140 | 0 | return cms_SignedData_final(cms, cmsbio); |
141 | 0 |
|
142 | 0 | case NID_pkcs7_digest: |
143 | 0 | return cms_DigestedData_do_final(cms, cmsbio, 0); |
144 | 0 |
|
145 | 0 | default: |
146 | 0 | CMSerr(CMS_F_CMS_DATAFINAL, CMS_R_UNSUPPORTED_TYPE); |
147 | 0 | return 0; |
148 | 0 | } |
149 | 0 | } |
150 | | |
151 | | /* |
152 | | * Return an OCTET STRING pointer to content. This allows it to be accessed |
153 | | * or set later. |
154 | | */ |
155 | | |
156 | | ASN1_OCTET_STRING **CMS_get0_content(CMS_ContentInfo *cms) |
157 | 0 | { |
158 | 0 | switch (OBJ_obj2nid(cms->contentType)) { |
159 | 0 |
|
160 | 0 | case NID_pkcs7_data: |
161 | 0 | return &cms->d.data; |
162 | 0 |
|
163 | 0 | case NID_pkcs7_signed: |
164 | 0 | return &cms->d.signedData->encapContentInfo->eContent; |
165 | 0 |
|
166 | 0 | case NID_pkcs7_enveloped: |
167 | 0 | return &cms->d.envelopedData->encryptedContentInfo->encryptedContent; |
168 | 0 |
|
169 | 0 | case NID_pkcs7_digest: |
170 | 0 | return &cms->d.digestedData->encapContentInfo->eContent; |
171 | 0 |
|
172 | 0 | case NID_pkcs7_encrypted: |
173 | 0 | return &cms->d.encryptedData->encryptedContentInfo->encryptedContent; |
174 | 0 |
|
175 | 0 | case NID_id_smime_ct_authData: |
176 | 0 | return &cms->d.authenticatedData->encapContentInfo->eContent; |
177 | 0 |
|
178 | 0 | case NID_id_smime_ct_compressedData: |
179 | 0 | return &cms->d.compressedData->encapContentInfo->eContent; |
180 | 0 |
|
181 | 0 | default: |
182 | 0 | if (cms->d.other->type == V_ASN1_OCTET_STRING) |
183 | 0 | return &cms->d.other->value.octet_string; |
184 | 0 | CMSerr(CMS_F_CMS_GET0_CONTENT, CMS_R_UNSUPPORTED_CONTENT_TYPE); |
185 | 0 | return NULL; |
186 | 0 |
|
187 | 0 | } |
188 | 0 | } |
189 | | |
190 | | /* |
191 | | * Return an ASN1_OBJECT pointer to content type. This allows it to be |
192 | | * accessed or set later. |
193 | | */ |
194 | | |
195 | | static ASN1_OBJECT **cms_get0_econtent_type(CMS_ContentInfo *cms) |
196 | 0 | { |
197 | 0 | switch (OBJ_obj2nid(cms->contentType)) { |
198 | 0 |
|
199 | 0 | case NID_pkcs7_signed: |
200 | 0 | return &cms->d.signedData->encapContentInfo->eContentType; |
201 | 0 |
|
202 | 0 | case NID_pkcs7_enveloped: |
203 | 0 | return &cms->d.envelopedData->encryptedContentInfo->contentType; |
204 | 0 |
|
205 | 0 | case NID_pkcs7_digest: |
206 | 0 | return &cms->d.digestedData->encapContentInfo->eContentType; |
207 | 0 |
|
208 | 0 | case NID_pkcs7_encrypted: |
209 | 0 | return &cms->d.encryptedData->encryptedContentInfo->contentType; |
210 | 0 |
|
211 | 0 | case NID_id_smime_ct_authData: |
212 | 0 | return &cms->d.authenticatedData->encapContentInfo->eContentType; |
213 | 0 |
|
214 | 0 | case NID_id_smime_ct_compressedData: |
215 | 0 | return &cms->d.compressedData->encapContentInfo->eContentType; |
216 | 0 |
|
217 | 0 | default: |
218 | 0 | CMSerr(CMS_F_CMS_GET0_ECONTENT_TYPE, CMS_R_UNSUPPORTED_CONTENT_TYPE); |
219 | 0 | return NULL; |
220 | 0 |
|
221 | 0 | } |
222 | 0 | } |
223 | | |
224 | | const ASN1_OBJECT *CMS_get0_eContentType(CMS_ContentInfo *cms) |
225 | 0 | { |
226 | 0 | ASN1_OBJECT **petype; |
227 | 0 | petype = cms_get0_econtent_type(cms); |
228 | 0 | if (petype) |
229 | 0 | return *petype; |
230 | 0 | return NULL; |
231 | 0 | } |
232 | | |
233 | | int CMS_set1_eContentType(CMS_ContentInfo *cms, const ASN1_OBJECT *oid) |
234 | 0 | { |
235 | 0 | ASN1_OBJECT **petype, *etype; |
236 | 0 | petype = cms_get0_econtent_type(cms); |
237 | 0 | if (!petype) |
238 | 0 | return 0; |
239 | 0 | if (!oid) |
240 | 0 | return 1; |
241 | 0 | etype = OBJ_dup(oid); |
242 | 0 | if (!etype) |
243 | 0 | return 0; |
244 | 0 | ASN1_OBJECT_free(*petype); |
245 | 0 | *petype = etype; |
246 | 0 | return 1; |
247 | 0 | } |
248 | | |
249 | | int CMS_is_detached(CMS_ContentInfo *cms) |
250 | 0 | { |
251 | 0 | ASN1_OCTET_STRING **pos; |
252 | 0 | pos = CMS_get0_content(cms); |
253 | 0 | if (!pos) |
254 | 0 | return -1; |
255 | 0 | if (*pos) |
256 | 0 | return 0; |
257 | 0 | return 1; |
258 | 0 | } |
259 | | |
260 | | int CMS_set_detached(CMS_ContentInfo *cms, int detached) |
261 | 0 | { |
262 | 0 | ASN1_OCTET_STRING **pos; |
263 | 0 | pos = CMS_get0_content(cms); |
264 | 0 | if (!pos) |
265 | 0 | return 0; |
266 | 0 | if (detached) { |
267 | 0 | ASN1_OCTET_STRING_free(*pos); |
268 | 0 | *pos = NULL; |
269 | 0 | return 1; |
270 | 0 | } |
271 | 0 | if (*pos == NULL) |
272 | 0 | *pos = ASN1_OCTET_STRING_new(); |
273 | 0 | if (*pos != NULL) { |
274 | 0 | /* |
275 | 0 | * NB: special flag to show content is created and not read in. |
276 | 0 | */ |
277 | 0 | (*pos)->flags |= ASN1_STRING_FLAG_CONT; |
278 | 0 | return 1; |
279 | 0 | } |
280 | 0 | CMSerr(CMS_F_CMS_SET_DETACHED, ERR_R_MALLOC_FAILURE); |
281 | 0 | return 0; |
282 | 0 | } |
283 | | |
284 | | /* Create a digest BIO from an X509_ALGOR structure */ |
285 | | |
286 | | BIO *cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm) |
287 | 0 | { |
288 | 0 | BIO *mdbio = NULL; |
289 | 0 | const ASN1_OBJECT *digestoid; |
290 | 0 | const EVP_MD *digest; |
291 | 0 | X509_ALGOR_get0(&digestoid, NULL, NULL, digestAlgorithm); |
292 | 0 | digest = EVP_get_digestbyobj(digestoid); |
293 | 0 | if (!digest) { |
294 | 0 | CMSerr(CMS_F_CMS_DIGESTALGORITHM_INIT_BIO, |
295 | 0 | CMS_R_UNKNOWN_DIGEST_ALGORITHM); |
296 | 0 | goto err; |
297 | 0 | } |
298 | 0 | mdbio = BIO_new(BIO_f_md()); |
299 | 0 | if (mdbio == NULL || !BIO_set_md(mdbio, digest)) { |
300 | 0 | CMSerr(CMS_F_CMS_DIGESTALGORITHM_INIT_BIO, CMS_R_MD_BIO_INIT_ERROR); |
301 | 0 | goto err; |
302 | 0 | } |
303 | 0 | return mdbio; |
304 | 0 | err: |
305 | 0 | BIO_free(mdbio); |
306 | 0 | return NULL; |
307 | 0 | } |
308 | | |
309 | | /* Locate a message digest content from a BIO chain based on SignerInfo */ |
310 | | |
311 | | int cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain, |
312 | | X509_ALGOR *mdalg) |
313 | 0 | { |
314 | 0 | int nid; |
315 | 0 | const ASN1_OBJECT *mdoid; |
316 | 0 | X509_ALGOR_get0(&mdoid, NULL, NULL, mdalg); |
317 | 0 | nid = OBJ_obj2nid(mdoid); |
318 | 0 | /* Look for digest type to match signature */ |
319 | 0 | for (;;) { |
320 | 0 | EVP_MD_CTX *mtmp; |
321 | 0 | chain = BIO_find_type(chain, BIO_TYPE_MD); |
322 | 0 | if (chain == NULL) { |
323 | 0 | CMSerr(CMS_F_CMS_DIGESTALGORITHM_FIND_CTX, |
324 | 0 | CMS_R_NO_MATCHING_DIGEST); |
325 | 0 | return 0; |
326 | 0 | } |
327 | 0 | BIO_get_md_ctx(chain, &mtmp); |
328 | 0 | if (EVP_MD_CTX_type(mtmp) == nid |
329 | 0 | /* |
330 | 0 | * Workaround for broken implementations that use signature |
331 | 0 | * algorithm OID instead of digest. |
332 | 0 | */ |
333 | 0 | || EVP_MD_pkey_type(EVP_MD_CTX_md(mtmp)) == nid) |
334 | 0 | return EVP_MD_CTX_copy_ex(mctx, mtmp); |
335 | 0 | chain = BIO_next(chain); |
336 | 0 | } |
337 | 0 | } |
338 | | |
339 | | static STACK_OF(CMS_CertificateChoices) |
340 | | **cms_get0_certificate_choices(CMS_ContentInfo *cms) |
341 | 0 | { |
342 | 0 | switch (OBJ_obj2nid(cms->contentType)) { |
343 | 0 |
|
344 | 0 | case NID_pkcs7_signed: |
345 | 0 | return &cms->d.signedData->certificates; |
346 | 0 |
|
347 | 0 | case NID_pkcs7_enveloped: |
348 | 0 | if (cms->d.envelopedData->originatorInfo == NULL) |
349 | 0 | return NULL; |
350 | 0 | return &cms->d.envelopedData->originatorInfo->certificates; |
351 | 0 |
|
352 | 0 | default: |
353 | 0 | CMSerr(CMS_F_CMS_GET0_CERTIFICATE_CHOICES, |
354 | 0 | CMS_R_UNSUPPORTED_CONTENT_TYPE); |
355 | 0 | return NULL; |
356 | 0 |
|
357 | 0 | } |
358 | 0 | } |
359 | | |
360 | | CMS_CertificateChoices *CMS_add0_CertificateChoices(CMS_ContentInfo *cms) |
361 | 0 | { |
362 | 0 | STACK_OF(CMS_CertificateChoices) **pcerts; |
363 | 0 | CMS_CertificateChoices *cch; |
364 | 0 | pcerts = cms_get0_certificate_choices(cms); |
365 | 0 | if (!pcerts) |
366 | 0 | return NULL; |
367 | 0 | if (!*pcerts) |
368 | 0 | *pcerts = sk_CMS_CertificateChoices_new_null(); |
369 | 0 | if (!*pcerts) |
370 | 0 | return NULL; |
371 | 0 | cch = M_ASN1_new_of(CMS_CertificateChoices); |
372 | 0 | if (!cch) |
373 | 0 | return NULL; |
374 | 0 | if (!sk_CMS_CertificateChoices_push(*pcerts, cch)) { |
375 | 0 | M_ASN1_free_of(cch, CMS_CertificateChoices); |
376 | 0 | return NULL; |
377 | 0 | } |
378 | 0 | return cch; |
379 | 0 | } |
380 | | |
381 | | int CMS_add0_cert(CMS_ContentInfo *cms, X509 *cert) |
382 | 0 | { |
383 | 0 | CMS_CertificateChoices *cch; |
384 | 0 | STACK_OF(CMS_CertificateChoices) **pcerts; |
385 | 0 | int i; |
386 | 0 | pcerts = cms_get0_certificate_choices(cms); |
387 | 0 | if (!pcerts) |
388 | 0 | return 0; |
389 | 0 | for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) { |
390 | 0 | cch = sk_CMS_CertificateChoices_value(*pcerts, i); |
391 | 0 | if (cch->type == CMS_CERTCHOICE_CERT) { |
392 | 0 | if (!X509_cmp(cch->d.certificate, cert)) { |
393 | 0 | CMSerr(CMS_F_CMS_ADD0_CERT, |
394 | 0 | CMS_R_CERTIFICATE_ALREADY_PRESENT); |
395 | 0 | return 0; |
396 | 0 | } |
397 | 0 | } |
398 | 0 | } |
399 | 0 | cch = CMS_add0_CertificateChoices(cms); |
400 | 0 | if (!cch) |
401 | 0 | return 0; |
402 | 0 | cch->type = CMS_CERTCHOICE_CERT; |
403 | 0 | cch->d.certificate = cert; |
404 | 0 | return 1; |
405 | 0 | } |
406 | | |
407 | | int CMS_add1_cert(CMS_ContentInfo *cms, X509 *cert) |
408 | 0 | { |
409 | 0 | int r; |
410 | 0 | r = CMS_add0_cert(cms, cert); |
411 | 0 | if (r > 0) |
412 | 0 | X509_up_ref(cert); |
413 | 0 | return r; |
414 | 0 | } |
415 | | |
416 | | static STACK_OF(CMS_RevocationInfoChoice) |
417 | | **cms_get0_revocation_choices(CMS_ContentInfo *cms) |
418 | 0 | { |
419 | 0 | switch (OBJ_obj2nid(cms->contentType)) { |
420 | 0 |
|
421 | 0 | case NID_pkcs7_signed: |
422 | 0 | return &cms->d.signedData->crls; |
423 | 0 |
|
424 | 0 | case NID_pkcs7_enveloped: |
425 | 0 | if (cms->d.envelopedData->originatorInfo == NULL) |
426 | 0 | return NULL; |
427 | 0 | return &cms->d.envelopedData->originatorInfo->crls; |
428 | 0 |
|
429 | 0 | default: |
430 | 0 | CMSerr(CMS_F_CMS_GET0_REVOCATION_CHOICES, |
431 | 0 | CMS_R_UNSUPPORTED_CONTENT_TYPE); |
432 | 0 | return NULL; |
433 | 0 |
|
434 | 0 | } |
435 | 0 | } |
436 | | |
437 | | CMS_RevocationInfoChoice *CMS_add0_RevocationInfoChoice(CMS_ContentInfo *cms) |
438 | 0 | { |
439 | 0 | STACK_OF(CMS_RevocationInfoChoice) **pcrls; |
440 | 0 | CMS_RevocationInfoChoice *rch; |
441 | 0 | pcrls = cms_get0_revocation_choices(cms); |
442 | 0 | if (!pcrls) |
443 | 0 | return NULL; |
444 | 0 | if (!*pcrls) |
445 | 0 | *pcrls = sk_CMS_RevocationInfoChoice_new_null(); |
446 | 0 | if (!*pcrls) |
447 | 0 | return NULL; |
448 | 0 | rch = M_ASN1_new_of(CMS_RevocationInfoChoice); |
449 | 0 | if (!rch) |
450 | 0 | return NULL; |
451 | 0 | if (!sk_CMS_RevocationInfoChoice_push(*pcrls, rch)) { |
452 | 0 | M_ASN1_free_of(rch, CMS_RevocationInfoChoice); |
453 | 0 | return NULL; |
454 | 0 | } |
455 | 0 | return rch; |
456 | 0 | } |
457 | | |
458 | | int CMS_add0_crl(CMS_ContentInfo *cms, X509_CRL *crl) |
459 | 0 | { |
460 | 0 | CMS_RevocationInfoChoice *rch; |
461 | 0 | rch = CMS_add0_RevocationInfoChoice(cms); |
462 | 0 | if (!rch) |
463 | 0 | return 0; |
464 | 0 | rch->type = CMS_REVCHOICE_CRL; |
465 | 0 | rch->d.crl = crl; |
466 | 0 | return 1; |
467 | 0 | } |
468 | | |
469 | | int CMS_add1_crl(CMS_ContentInfo *cms, X509_CRL *crl) |
470 | 0 | { |
471 | 0 | int r; |
472 | 0 | r = CMS_add0_crl(cms, crl); |
473 | 0 | if (r > 0) |
474 | 0 | X509_CRL_up_ref(crl); |
475 | 0 | return r; |
476 | 0 | } |
477 | | |
478 | | STACK_OF(X509) *CMS_get1_certs(CMS_ContentInfo *cms) |
479 | 0 | { |
480 | 0 | STACK_OF(X509) *certs = NULL; |
481 | 0 | CMS_CertificateChoices *cch; |
482 | 0 | STACK_OF(CMS_CertificateChoices) **pcerts; |
483 | 0 | int i; |
484 | 0 | pcerts = cms_get0_certificate_choices(cms); |
485 | 0 | if (!pcerts) |
486 | 0 | return NULL; |
487 | 0 | for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) { |
488 | 0 | cch = sk_CMS_CertificateChoices_value(*pcerts, i); |
489 | 0 | if (cch->type == 0) { |
490 | 0 | if (!certs) { |
491 | 0 | certs = sk_X509_new_null(); |
492 | 0 | if (!certs) |
493 | 0 | return NULL; |
494 | 0 | } |
495 | 0 | if (!sk_X509_push(certs, cch->d.certificate)) { |
496 | 0 | sk_X509_pop_free(certs, X509_free); |
497 | 0 | return NULL; |
498 | 0 | } |
499 | 0 | X509_up_ref(cch->d.certificate); |
500 | 0 | } |
501 | 0 | } |
502 | 0 | return certs; |
503 | 0 |
|
504 | 0 | } |
505 | | |
506 | | STACK_OF(X509_CRL) *CMS_get1_crls(CMS_ContentInfo *cms) |
507 | 0 | { |
508 | 0 | STACK_OF(X509_CRL) *crls = NULL; |
509 | 0 | STACK_OF(CMS_RevocationInfoChoice) **pcrls; |
510 | 0 | CMS_RevocationInfoChoice *rch; |
511 | 0 | int i; |
512 | 0 | pcrls = cms_get0_revocation_choices(cms); |
513 | 0 | if (!pcrls) |
514 | 0 | return NULL; |
515 | 0 | for (i = 0; i < sk_CMS_RevocationInfoChoice_num(*pcrls); i++) { |
516 | 0 | rch = sk_CMS_RevocationInfoChoice_value(*pcrls, i); |
517 | 0 | if (rch->type == 0) { |
518 | 0 | if (!crls) { |
519 | 0 | crls = sk_X509_CRL_new_null(); |
520 | 0 | if (!crls) |
521 | 0 | return NULL; |
522 | 0 | } |
523 | 0 | if (!sk_X509_CRL_push(crls, rch->d.crl)) { |
524 | 0 | sk_X509_CRL_pop_free(crls, X509_CRL_free); |
525 | 0 | return NULL; |
526 | 0 | } |
527 | 0 | X509_CRL_up_ref(rch->d.crl); |
528 | 0 | } |
529 | 0 | } |
530 | 0 | return crls; |
531 | 0 | } |
532 | | |
533 | | int cms_ias_cert_cmp(CMS_IssuerAndSerialNumber *ias, X509 *cert) |
534 | 0 | { |
535 | 0 | int ret; |
536 | 0 | ret = X509_NAME_cmp(ias->issuer, X509_get_issuer_name(cert)); |
537 | 0 | if (ret) |
538 | 0 | return ret; |
539 | 0 | return ASN1_INTEGER_cmp(ias->serialNumber, X509_get_serialNumber(cert)); |
540 | 0 | } |
541 | | |
542 | | int cms_keyid_cert_cmp(ASN1_OCTET_STRING *keyid, X509 *cert) |
543 | 0 | { |
544 | 0 | const ASN1_OCTET_STRING *cert_keyid = X509_get0_subject_key_id(cert); |
545 | 0 |
|
546 | 0 | if (cert_keyid == NULL) |
547 | 0 | return -1; |
548 | 0 | return ASN1_OCTET_STRING_cmp(keyid, cert_keyid); |
549 | 0 | } |
550 | | |
551 | | int cms_set1_ias(CMS_IssuerAndSerialNumber **pias, X509 *cert) |
552 | 0 | { |
553 | 0 | CMS_IssuerAndSerialNumber *ias; |
554 | 0 | ias = M_ASN1_new_of(CMS_IssuerAndSerialNumber); |
555 | 0 | if (!ias) |
556 | 0 | goto err; |
557 | 0 | if (!X509_NAME_set(&ias->issuer, X509_get_issuer_name(cert))) |
558 | 0 | goto err; |
559 | 0 | if (!ASN1_STRING_copy(ias->serialNumber, X509_get_serialNumber(cert))) |
560 | 0 | goto err; |
561 | 0 | M_ASN1_free_of(*pias, CMS_IssuerAndSerialNumber); |
562 | 0 | *pias = ias; |
563 | 0 | return 1; |
564 | 0 | err: |
565 | 0 | M_ASN1_free_of(ias, CMS_IssuerAndSerialNumber); |
566 | 0 | CMSerr(CMS_F_CMS_SET1_IAS, ERR_R_MALLOC_FAILURE); |
567 | 0 | return 0; |
568 | 0 | } |
569 | | |
570 | | int cms_set1_keyid(ASN1_OCTET_STRING **pkeyid, X509 *cert) |
571 | 0 | { |
572 | 0 | ASN1_OCTET_STRING *keyid = NULL; |
573 | 0 | const ASN1_OCTET_STRING *cert_keyid; |
574 | 0 | cert_keyid = X509_get0_subject_key_id(cert); |
575 | 0 | if (cert_keyid == NULL) { |
576 | 0 | CMSerr(CMS_F_CMS_SET1_KEYID, CMS_R_CERTIFICATE_HAS_NO_KEYID); |
577 | 0 | return 0; |
578 | 0 | } |
579 | 0 | keyid = ASN1_STRING_dup(cert_keyid); |
580 | 0 | if (!keyid) { |
581 | 0 | CMSerr(CMS_F_CMS_SET1_KEYID, ERR_R_MALLOC_FAILURE); |
582 | 0 | return 0; |
583 | 0 | } |
584 | 0 | ASN1_OCTET_STRING_free(*pkeyid); |
585 | 0 | *pkeyid = keyid; |
586 | 0 | return 1; |
587 | 0 | } |