Encrypted Data type processing. Add options to cms utility and run section 7
[openssl.git] / crypto / cms / cms_lib.c
1 /* crypto/cms/cms_lib.c */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3  * project.
4  */
5 /* ====================================================================
6  * Copyright (c) 2008 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer. 
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  */
53
54 #include <openssl/asn1t.h>
55 #include <openssl/x509.h>
56 #include <openssl/err.h>
57 #include <openssl/pem.h>
58 #include <openssl/bio.h>
59 #include <openssl/asn1.h>
60 #include "cms.h"
61 #include "cms_lcl.h"
62
63 IMPLEMENT_ASN1_FUNCTIONS(CMS_ContentInfo)
64 IMPLEMENT_ASN1_PRINT_FUNCTION(CMS_ContentInfo)
65
66 DECLARE_ASN1_ITEM(CMS_CertificateChoices)
67 DECLARE_ASN1_ITEM(CMS_RevocationInfoChoice)
68 DECLARE_STACK_OF(CMS_CertificateChoices)
69 DECLARE_STACK_OF(CMS_RevocationInfoChoice)
70
71 #if 0
72 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(CMS_CertificateChoices)
73 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(CMS_RevocationInfoChoice)
74 #endif
75
76
77 const ASN1_OBJECT *CMS_get0_type(CMS_ContentInfo *cms)
78         {
79         return cms->contentType;
80         }
81
82 CMS_ContentInfo *cms_Data_create(void)
83         {
84         CMS_ContentInfo *cms;
85         cms = CMS_ContentInfo_new();
86         if (cms)
87                 {
88                 cms->contentType = OBJ_nid2obj(NID_pkcs7_data);
89                 /* Never detached */
90                 CMS_set_detached(cms, 0);
91                 }
92         return cms;
93         }
94
95 BIO *cms_content_bio(CMS_ContentInfo *cms)
96         {
97         ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
98         if (!pos)
99                 return NULL;
100         /* If content detached data goes nowhere: create NULL BIO */
101         if (!*pos)
102                 return BIO_new(BIO_s_null());
103         /* If content not detached and created return memory BIO
104          */
105         if (!*pos || ((*pos)->flags == ASN1_STRING_FLAG_CONT))
106                 return BIO_new(BIO_s_mem());
107         /* Else content was read in: return read only BIO for it */
108         return BIO_new_mem_buf((*pos)->data, (*pos)->length);
109         }
110
111 BIO *CMS_dataInit(CMS_ContentInfo *cms, BIO *icont)
112         {
113         BIO *cmsbio, *cont;
114         if (icont)
115                 cont = icont;
116         else
117                 cont = cms_content_bio(cms);
118         if (!cont)
119                 {
120                 CMSerr(CMS_F_CMS_DATAINIT, CMS_R_NO_CONTENT);
121                 return NULL;
122                 }
123         switch (OBJ_obj2nid(cms->contentType))
124                 {
125
126                 case NID_pkcs7_data:
127                 return cont;
128
129                 case NID_pkcs7_signed:
130                 cmsbio = cms_SignedData_init_bio(cms);
131                 break;
132
133                 case NID_pkcs7_digest:
134                 cmsbio = cms_DigestedData_init_bio(cms);
135                 break;
136 #ifdef ZLIB
137                 case NID_id_smime_ct_compressedData:
138                 cmsbio = cms_CompressedData_init_bio(cms);
139                 break;
140 #endif
141
142                 case NID_pkcs7_encrypted:
143                 cmsbio = BIO_new(BIO_f_cipher());
144                 break;
145
146                 default:
147                 CMSerr(CMS_F_CMS_DATAINIT, CMS_R_UNSUPPORTED_TYPE);
148                 return NULL;
149                 }
150
151         if (cmsbio)
152                 return BIO_push(cmsbio, cont);
153
154         if (!icont)
155                 BIO_free(cont);
156         return NULL;
157
158         }
159
160 int CMS_dataFinal(CMS_ContentInfo *cms, BIO *cmsbio)
161         {
162         ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
163         if (!pos)
164                 return 0;
165         /* If ebmedded content find memory BIO and set content */
166         if (*pos && ((*pos)->flags & ASN1_STRING_FLAG_CONT))
167                 {
168                 BIO *mbio;
169                 unsigned char *cont;
170                 long contlen;
171                 mbio = BIO_find_type(cmsbio, BIO_TYPE_MEM);
172                 if (!mbio)
173                         {
174                         CMSerr(CMS_F_CMS_DATAFINAL, CMS_R_CONTENT_NOT_FOUND);
175                         return 0;
176                         }
177                 contlen = BIO_get_mem_data(mbio, &cont);
178                 /* Set bio as read only so its content can't be clobbered */
179                 BIO_set_flags(mbio, BIO_FLAGS_MEM_RDONLY);
180                 BIO_set_mem_eof_return(mbio, 0);
181                 ASN1_STRING_set0(*pos, cont, contlen);
182                 (*pos)->flags &= ~ASN1_STRING_FLAG_CONT;
183                 }
184
185         switch (OBJ_obj2nid(cms->contentType))
186                 {
187
188                 case NID_pkcs7_data:
189                 case NID_id_smime_ct_compressedData:
190                 /* Nothing to do */
191                 return 1;
192
193                 case NID_pkcs7_signed:
194                 return cms_SignedData_final(cms, cmsbio);
195
196                 case NID_pkcs7_digest:
197                 return cms_DigestedData_do_final(cms, cmsbio, 0);
198
199                 default:
200                 CMSerr(CMS_F_CMS_DATAFINAL, CMS_R_UNSUPPORTED_TYPE);
201                 return 0;
202                 }
203         }
204
205 /* Return an OCTET STRING pointer to content. This allows it to
206  * be accessed or set later.
207  */
208
209 ASN1_OCTET_STRING **CMS_get0_content(CMS_ContentInfo *cms)
210         {
211         switch (OBJ_obj2nid(cms->contentType))
212                 {
213
214                 case NID_pkcs7_data:
215                 return &cms->d.data;
216
217                 case NID_pkcs7_signed:
218                 return &cms->d.signedData->encapContentInfo->eContent;
219
220                 case NID_pkcs7_enveloped:
221                 return &cms->d.envelopedData->encryptedContentInfo->encryptedContent;
222
223                 case NID_pkcs7_digest:
224                 return &cms->d.digestedData->encapContentInfo->eContent;
225
226                 case NID_pkcs7_encrypted:
227                 return &cms->d.encryptedData->encryptedContentInfo->encryptedContent;
228
229                 case NID_id_smime_ct_authData:
230                 return &cms->d.authenticatedData->encapContentInfo->eContent;
231
232                 case NID_id_smime_ct_compressedData:
233                 return &cms->d.compressedData->encapContentInfo->eContent;
234
235                 default:
236                 if (cms->d.other->type == V_ASN1_OCTET_STRING)
237                         return &cms->d.other->value.octet_string;
238                 CMSerr(CMS_F_CMS_GET0_CONTENT, CMS_R_UNSUPPORTED_CONTENT_TYPE);
239                 return NULL;
240
241                 }
242         }
243
244 /* Return an ASN1_OBJECT pointer to content type. This allows it to
245  * be accessed or set later.
246  */
247
248 static ASN1_OBJECT **cms_get0_econtent_type(CMS_ContentInfo *cms)
249         {
250         switch (OBJ_obj2nid(cms->contentType))
251                 {
252
253                 case NID_pkcs7_signed:
254                 return &cms->d.signedData->encapContentInfo->eContentType;
255
256                 case NID_pkcs7_enveloped:
257                 return &cms->d.envelopedData->encryptedContentInfo->contentType;
258
259                 case NID_pkcs7_digest:
260                 return &cms->d.digestedData->encapContentInfo->eContentType;
261
262                 case NID_pkcs7_encrypted:
263                 return &cms->d.encryptedData->encryptedContentInfo->contentType;
264
265                 case NID_id_smime_ct_authData:
266                 return &cms->d.authenticatedData->encapContentInfo->eContentType;
267
268                 case NID_id_smime_ct_compressedData:
269                 return &cms->d.compressedData->encapContentInfo->eContentType;
270
271                 default:
272                 CMSerr(CMS_F_CMS_GET0_ECONTENT_TYPE,
273                                         CMS_R_UNSUPPORTED_CONTENT_TYPE);
274                 return NULL;
275
276                 }
277         }
278
279 const ASN1_OBJECT *CMS_get0_eContentType(CMS_ContentInfo *cms)
280         {
281         ASN1_OBJECT **petype;
282         petype = cms_get0_econtent_type(cms);
283         if (petype)
284                 return *petype;
285         return NULL;
286         }
287
288 int CMS_set1_eContentType(CMS_ContentInfo *cms, const ASN1_OBJECT *oid)
289         {
290         ASN1_OBJECT **petype, *etype;
291         petype = cms_get0_econtent_type(cms);
292         if (!petype)
293                 return 0;
294         if (!oid)
295                 return 1;
296         etype = OBJ_dup(oid);
297         if (!etype)
298                 return 0;
299         ASN1_OBJECT_free(*petype);
300         *petype = etype;
301         return 1;
302         }
303
304 int CMS_is_detached(CMS_ContentInfo *cms)
305         {
306         ASN1_OCTET_STRING **pos;
307         pos = CMS_get0_content(cms);
308         if (!pos)
309                 return -1;
310         if (*pos)
311                 return 0;
312         return 1;
313         }
314
315 int CMS_set_detached(CMS_ContentInfo *cms, int detached)
316         {
317         ASN1_OCTET_STRING **pos;
318         pos = CMS_get0_content(cms);
319         if (!pos)
320                 return 0;
321         if (detached)
322                 {
323                 if (*pos)
324                         {
325                         ASN1_OCTET_STRING_free(*pos);
326                         *pos = NULL;
327                         }
328                 return 1;
329                 }
330         if (!*pos)
331                 *pos = ASN1_OCTET_STRING_new();
332         if (*pos)
333                 {
334                 /* NB: special flag to show content is created and not
335                  * read in.
336                  */
337                 (*pos)->flags |= ASN1_STRING_FLAG_CONT;
338                 return 1;
339                 }
340         CMSerr(CMS_F_CMS_SET_DETACHED, ERR_R_MALLOC_FAILURE);
341         return 0;
342         }
343
344 /* Set up an X509_ALGOR DigestAlgorithmIdentifier from an EVP_MD */
345
346 void cms_DigestAlgorithm_set(X509_ALGOR *alg, const EVP_MD *md)
347         {
348         int param_type;
349
350         if (md->flags & EVP_MD_FLAG_DIGALGID_ABSENT)
351                 param_type = V_ASN1_UNDEF;
352         else
353                 param_type = V_ASN1_NULL;
354
355         X509_ALGOR_set0(alg, OBJ_nid2obj(EVP_MD_type(md)), param_type, NULL);
356
357         }
358
359 /* Create a digest BIO from an X509_ALGOR structure */
360
361 BIO *cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm)
362         {
363         BIO *mdbio = NULL;
364         ASN1_OBJECT *digestoid;
365         const EVP_MD *digest;
366         X509_ALGOR_get0(&digestoid, NULL, NULL, digestAlgorithm);
367         digest = EVP_get_digestbyobj(digestoid);
368         if (!digest)
369                 {
370                 CMSerr(CMS_F_CMS_DIGESTALGORITHM_INIT_BIO,
371                                 CMS_R_UNKNOWN_DIGEST_ALGORIHM);
372                 goto err;       
373                 }
374         mdbio = BIO_new(BIO_f_md());
375         if (!mdbio || !BIO_set_md(mdbio, digest))
376                 {
377                 CMSerr(CMS_F_CMS_DIGESTALGORITHM_INIT_BIO,
378                                 CMS_R_MD_BIO_INIT_ERROR);
379                 goto err;       
380                 }
381         return mdbio;
382         err:
383         if (mdbio)
384                 BIO_free(mdbio);
385         return NULL;
386         }
387
388 /* Locate a message digest content from a BIO chain based on SignerInfo */
389
390 int cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
391                                         X509_ALGOR *mdalg)
392         {
393         int nid;
394         ASN1_OBJECT *mdoid;
395         X509_ALGOR_get0(&mdoid, NULL, NULL, mdalg);
396         nid = OBJ_obj2nid(mdoid);
397         /* Look for digest type to match signature */
398         for (;;)
399                 {
400                 EVP_MD_CTX *mtmp;
401                 chain = BIO_find_type(chain, BIO_TYPE_MD);
402                 if (chain == NULL)
403                         {
404                         CMSerr(CMS_F_CMS_DIGESTALGORITHM_FIND_CTX,
405                                                 CMS_R_NO_MATCHING_DIGEST);
406                         return 0;
407                         }
408                 BIO_get_md_ctx(chain, &mtmp);
409                 if (EVP_MD_CTX_type(mtmp) == nid)
410                         {
411                         EVP_MD_CTX_copy_ex(mctx, mtmp);
412                         return 1;
413                         }
414                 chain = BIO_next(chain);
415                 }
416         }
417
418 STACK_OF(CMS_CertificateChoices) **cms_get0_certificate_choices(CMS_ContentInfo *cms)
419         {
420         switch (OBJ_obj2nid(cms->contentType))
421                 {
422
423                 case NID_pkcs7_signed:
424                 return &cms->d.signedData->certificates;
425
426                 case NID_pkcs7_enveloped:
427                 return &cms->d.envelopedData->originatorInfo->certificates;
428
429                 default:
430                 CMSerr(CMS_F_CMS_GET0_CERTIFICATE_CHOICES,
431                                         CMS_R_UNSUPPORTED_CONTENT_TYPE);
432                 return NULL;
433
434                 }
435         }
436
437 CMS_CertificateChoices *CMS_add0_CertificateChoices(CMS_ContentInfo *cms)
438         {
439         STACK_OF(CMS_CertificateChoices) **pcerts;
440         CMS_CertificateChoices *cch;
441         pcerts = cms_get0_certificate_choices(cms);
442         if (!pcerts)
443                 return NULL;
444         if (!*pcerts)
445                 *pcerts = sk_CMS_CertificateChoices_new_null();
446         if (!*pcerts)
447                 return NULL;
448         cch = M_ASN1_new_of(CMS_CertificateChoices);
449         if (!cch)
450                 return NULL;
451         if (!sk_CMS_CertificateChoices_push(*pcerts, cch))
452                 {
453                 M_ASN1_free_of(cch, CMS_CertificateChoices);
454                 return NULL;
455                 }
456         return cch;
457         }
458
459 int CMS_add0_cert(CMS_ContentInfo *cms, X509 *cert)
460         {
461         CMS_CertificateChoices *cch;
462         STACK_OF(CMS_CertificateChoices) **pcerts;
463         int i;
464         pcerts = cms_get0_certificate_choices(cms);
465         if (!pcerts)
466                 return 0;
467         if (!pcerts)
468                 return 0;
469         for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++)
470                 {
471                 cch = sk_CMS_CertificateChoices_value(*pcerts, i);
472                 if (cch->type == CMS_CERTCHOICE_CERT)
473                         {
474                         if (!X509_cmp(cch->d.certificate, cert))
475                                 return -1;
476                                 
477                         }
478                 }
479         cch = CMS_add0_CertificateChoices(cms);
480         if (!cch)
481                 return 0;
482         cch->type = CMS_CERTCHOICE_CERT;
483         cch->d.certificate = cert;
484         return 1;
485         }
486
487 int CMS_add1_cert(CMS_ContentInfo *cms, X509 *cert)
488         {
489         int r;
490         r = CMS_add0_cert(cms, cert);
491         if (r > 0)
492                 CRYPTO_add(&cert->references, 1, CRYPTO_LOCK_X509);
493         return r;
494         }
495
496 STACK_OF(CMS_RevocationInfoChoice) **cms_get0_revocation_choices(CMS_ContentInfo *cms)
497         {
498         switch (OBJ_obj2nid(cms->contentType))
499                 {
500
501                 case NID_pkcs7_signed:
502                 return &cms->d.signedData->crls;
503
504                 case NID_pkcs7_enveloped:
505                 return &cms->d.envelopedData->originatorInfo->crls;
506
507                 default:
508                 CMSerr(CMS_F_CMS_GET0_REVOCATION_CHOICES,
509                                         CMS_R_UNSUPPORTED_CONTENT_TYPE);
510                 return NULL;
511
512                 }
513         }
514
515 CMS_RevocationInfoChoice *CMS_add0_RevocationInfoChoice(CMS_ContentInfo *cms)
516         {
517         STACK_OF(CMS_RevocationInfoChoice) **pcrls;
518         CMS_RevocationInfoChoice *rch;
519         pcrls = cms_get0_revocation_choices(cms);
520         if (!pcrls)
521                 return NULL;
522         if (!*pcrls)
523                 *pcrls = sk_CMS_RevocationInfoChoice_new_null();
524         if (!*pcrls)
525                 return NULL;
526         rch = M_ASN1_new_of(CMS_RevocationInfoChoice);
527         if (!rch)
528                 return NULL;
529         if (!sk_CMS_RevocationInfoChoice_push(*pcrls, rch))
530                 {
531                 M_ASN1_free_of(rch, CMS_RevocationInfoChoice);
532                 return NULL;
533                 }
534         return rch;
535         }
536
537 int CMS_add0_crl(CMS_ContentInfo *cms, X509_CRL *crl)
538         {
539         CMS_RevocationInfoChoice *rch;
540         rch = CMS_add0_RevocationInfoChoice(cms);
541         if (!rch)
542                 return 0;
543         rch->type = CMS_REVCHOICE_CRL;
544         rch->d.crl = crl;
545         return 1;
546         }
547
548 STACK_OF(X509) *CMS_get1_certs(CMS_ContentInfo *cms)
549         {
550         STACK_OF(X509) *certs = NULL;
551         CMS_CertificateChoices *cch;
552         STACK_OF(CMS_CertificateChoices) **pcerts;
553         int i;
554         pcerts = cms_get0_certificate_choices(cms);
555         if (!pcerts)
556                 return NULL;
557         for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++)
558                 {
559                 cch = sk_CMS_CertificateChoices_value(*pcerts, i);
560                 if (cch->type == 0)
561                         {
562                         if (!certs)
563                                 {
564                                 certs = sk_X509_new_null();
565                                 if (!certs)
566                                         return NULL;
567                                 }
568                         if (!sk_X509_push(certs, cch->d.certificate))
569                                 {
570                                 sk_X509_pop_free(certs, X509_free);
571                                 return NULL;
572                                 }
573                         CRYPTO_add(&cch->d.certificate->references,
574                                                 1, CRYPTO_LOCK_X509);
575                         }
576                 }
577         return certs;
578
579         }
580
581 STACK_OF(X509_CRL) *CMS_get1_crls(CMS_ContentInfo *cms)
582         {
583         STACK_OF(X509_CRL) *crls = NULL;
584         STACK_OF(CMS_RevocationInfoChoice) **pcrls;
585         CMS_RevocationInfoChoice *rch;
586         int i;
587         pcrls = cms_get0_revocation_choices(cms);
588         if (!pcrls)
589                 return NULL;
590         for (i = 0; i < sk_CMS_RevocationInfoChoice_num(*pcrls); i++)
591                 {
592                 rch = sk_CMS_RevocationInfoChoice_value(*pcrls, i);
593                 if (rch->type == 0)
594                         {
595                         if (!crls)
596                                 {
597                                 crls = sk_X509_CRL_new_null();
598                                 if (!crls)
599                                         return NULL;
600                                 }
601                         if (!sk_X509_CRL_push(crls, rch->d.crl))
602                                 {
603                                 sk_X509_CRL_pop_free(crls, X509_CRL_free);
604                                 return NULL;
605                                 }
606                         CRYPTO_add(&rch->d.crl->references,
607                                                 1, CRYPTO_LOCK_X509_CRL);
608                         }
609                 }
610         return crls;
611         }