7d19126da5e03bd48bb3dff3e2bd07fd4ea49169
[openssl.git] / crypto / pkcs7 / pk7_lib.c
1 /* crypto/pkcs7/pk7_lib.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58
59 #include <stdio.h>
60 #include "cryptlib.h"
61 #include <openssl/objects.h>
62 #include <openssl/x509.h>
63
64 long PKCS7_ctrl(PKCS7 *p7, int cmd, long larg, char *parg)
65 {
66     int nid;
67     long ret;
68
69     nid = OBJ_obj2nid(p7->type);
70
71     switch (cmd) {
72     case PKCS7_OP_SET_DETACHED_SIGNATURE:
73         if (nid == NID_pkcs7_signed) {
74             ret = p7->detached = (int)larg;
75             if (ret && PKCS7_type_is_data(p7->d.sign->contents)) {
76                 ASN1_OCTET_STRING *os;
77                 os = p7->d.sign->contents->d.data;
78                 ASN1_OCTET_STRING_free(os);
79                 p7->d.sign->contents->d.data = NULL;
80             }
81         } else {
82             PKCS7err(PKCS7_F_PKCS7_CTRL,
83                      PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE);
84             ret = 0;
85         }
86         break;
87     case PKCS7_OP_GET_DETACHED_SIGNATURE:
88         if (nid == NID_pkcs7_signed) {
89             if (!p7->d.sign || !p7->d.sign->contents->d.ptr)
90                 ret = 1;
91             else
92                 ret = 0;
93
94             p7->detached = ret;
95         } else {
96             PKCS7err(PKCS7_F_PKCS7_CTRL,
97                      PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE);
98             ret = 0;
99         }
100
101         break;
102     default:
103         PKCS7err(PKCS7_F_PKCS7_CTRL, PKCS7_R_UNKNOWN_OPERATION);
104         ret = 0;
105     }
106     return (ret);
107 }
108
109 int PKCS7_content_new(PKCS7 *p7, int type)
110 {
111     PKCS7 *ret = NULL;
112
113     if ((ret = PKCS7_new()) == NULL)
114         goto err;
115     if (!PKCS7_set_type(ret, type))
116         goto err;
117     if (!PKCS7_set_content(p7, ret))
118         goto err;
119
120     return (1);
121  err:
122     if (ret != NULL)
123         PKCS7_free(ret);
124     return (0);
125 }
126
127 int PKCS7_set_content(PKCS7 *p7, PKCS7 *p7_data)
128 {
129     int i;
130
131     i = OBJ_obj2nid(p7->type);
132     switch (i) {
133     case NID_pkcs7_signed:
134         if (p7->d.sign->contents != NULL)
135             PKCS7_free(p7->d.sign->contents);
136         p7->d.sign->contents = p7_data;
137         break;
138     case NID_pkcs7_digest:
139         if (p7->d.digest->contents != NULL)
140             PKCS7_free(p7->d.digest->contents);
141         p7->d.digest->contents = p7_data;
142         break;
143     case NID_pkcs7_data:
144     case NID_pkcs7_enveloped:
145     case NID_pkcs7_signedAndEnveloped:
146     case NID_pkcs7_encrypted:
147     default:
148         PKCS7err(PKCS7_F_PKCS7_SET_CONTENT, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
149         goto err;
150     }
151     return (1);
152  err:
153     return (0);
154 }
155
156 int PKCS7_set_type(PKCS7 *p7, int type)
157 {
158     ASN1_OBJECT *obj;
159
160     /*
161      * PKCS7_content_free(p7);
162      */
163     obj = OBJ_nid2obj(type);    /* will not fail */
164
165     switch (type) {
166     case NID_pkcs7_signed:
167         p7->type = obj;
168         if ((p7->d.sign = PKCS7_SIGNED_new()) == NULL)
169             goto err;
170         if (!ASN1_INTEGER_set(p7->d.sign->version, 1)) {
171             PKCS7_SIGNED_free(p7->d.sign);
172             p7->d.sign = NULL;
173             goto err;
174         }
175         break;
176     case NID_pkcs7_data:
177         p7->type = obj;
178         if ((p7->d.data = M_ASN1_OCTET_STRING_new()) == NULL)
179             goto err;
180         break;
181     case NID_pkcs7_signedAndEnveloped:
182         p7->type = obj;
183         if ((p7->d.signed_and_enveloped = PKCS7_SIGN_ENVELOPE_new())
184             == NULL)
185             goto err;
186         ASN1_INTEGER_set(p7->d.signed_and_enveloped->version, 1);
187         if (!ASN1_INTEGER_set(p7->d.signed_and_enveloped->version, 1))
188             goto err;
189         p7->d.signed_and_enveloped->enc_data->content_type
190             = OBJ_nid2obj(NID_pkcs7_data);
191         break;
192     case NID_pkcs7_enveloped:
193         p7->type = obj;
194         if ((p7->d.enveloped = PKCS7_ENVELOPE_new())
195             == NULL)
196             goto err;
197         if (!ASN1_INTEGER_set(p7->d.enveloped->version, 0))
198             goto err;
199         p7->d.enveloped->enc_data->content_type = OBJ_nid2obj(NID_pkcs7_data);
200         break;
201     case NID_pkcs7_encrypted:
202         p7->type = obj;
203         if ((p7->d.encrypted = PKCS7_ENCRYPT_new())
204             == NULL)
205             goto err;
206         if (!ASN1_INTEGER_set(p7->d.encrypted->version, 0))
207             goto err;
208         p7->d.encrypted->enc_data->content_type = OBJ_nid2obj(NID_pkcs7_data);
209         break;
210
211     case NID_pkcs7_digest:
212         p7->type = obj;
213         if ((p7->d.digest = PKCS7_DIGEST_new())
214             == NULL)
215             goto err;
216         if (!ASN1_INTEGER_set(p7->d.digest->version, 0))
217             goto err;
218         break;
219     default:
220         PKCS7err(PKCS7_F_PKCS7_SET_TYPE, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
221         goto err;
222     }
223     return (1);
224  err:
225     return (0);
226 }
227
228 int PKCS7_set0_type_other(PKCS7 *p7, int type, ASN1_TYPE *other)
229 {
230     p7->type = OBJ_nid2obj(type);
231     p7->d.other = other;
232     return 1;
233 }
234
235 int PKCS7_add_signer(PKCS7 *p7, PKCS7_SIGNER_INFO *psi)
236 {
237     int i, j, nid;
238     X509_ALGOR *alg;
239     STACK_OF(PKCS7_SIGNER_INFO) *signer_sk;
240     STACK_OF(X509_ALGOR) *md_sk;
241
242     i = OBJ_obj2nid(p7->type);
243     switch (i) {
244     case NID_pkcs7_signed:
245         signer_sk = p7->d.sign->signer_info;
246         md_sk = p7->d.sign->md_algs;
247         break;
248     case NID_pkcs7_signedAndEnveloped:
249         signer_sk = p7->d.signed_and_enveloped->signer_info;
250         md_sk = p7->d.signed_and_enveloped->md_algs;
251         break;
252     default:
253         PKCS7err(PKCS7_F_PKCS7_ADD_SIGNER, PKCS7_R_WRONG_CONTENT_TYPE);
254         return (0);
255     }
256
257     nid = OBJ_obj2nid(psi->digest_alg->algorithm);
258
259     /* If the digest is not currently listed, add it */
260     j = 0;
261     for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++) {
262         alg = sk_X509_ALGOR_value(md_sk, i);
263         if (OBJ_obj2nid(alg->algorithm) == nid) {
264             j = 1;
265             break;
266         }
267     }
268     if (!j) {                   /* we need to add another algorithm */
269         if (!(alg = X509_ALGOR_new())
270             || !(alg->parameter = ASN1_TYPE_new())) {
271             X509_ALGOR_free(alg);
272             PKCS7err(PKCS7_F_PKCS7_ADD_SIGNER, ERR_R_MALLOC_FAILURE);
273             return (0);
274         }
275         alg->algorithm = OBJ_nid2obj(nid);
276         alg->parameter->type = V_ASN1_NULL;
277         if (!sk_X509_ALGOR_push(md_sk, alg)) {
278             X509_ALGOR_free(alg);
279             return 0;
280         }
281     }
282
283     if (!sk_PKCS7_SIGNER_INFO_push(signer_sk, psi))
284         return 0;
285     return (1);
286 }
287
288 int PKCS7_add_certificate(PKCS7 *p7, X509 *x509)
289 {
290     int i;
291     STACK_OF(X509) **sk;
292
293     i = OBJ_obj2nid(p7->type);
294     switch (i) {
295     case NID_pkcs7_signed:
296         sk = &(p7->d.sign->cert);
297         break;
298     case NID_pkcs7_signedAndEnveloped:
299         sk = &(p7->d.signed_and_enveloped->cert);
300         break;
301     default:
302         PKCS7err(PKCS7_F_PKCS7_ADD_CERTIFICATE, PKCS7_R_WRONG_CONTENT_TYPE);
303         return (0);
304     }
305
306     if (*sk == NULL)
307         *sk = sk_X509_new_null();
308     if (*sk == NULL) {
309         PKCS7err(PKCS7_F_PKCS7_ADD_CERTIFICATE, ERR_R_MALLOC_FAILURE);
310         return 0;
311     }
312     CRYPTO_add(&x509->references, 1, CRYPTO_LOCK_X509);
313     if (!sk_X509_push(*sk, x509)) {
314         X509_free(x509);
315         return 0;
316     }
317     return (1);
318 }
319
320 int PKCS7_add_crl(PKCS7 *p7, X509_CRL *crl)
321 {
322     int i;
323     STACK_OF(X509_CRL) **sk;
324
325     i = OBJ_obj2nid(p7->type);
326     switch (i) {
327     case NID_pkcs7_signed:
328         sk = &(p7->d.sign->crl);
329         break;
330     case NID_pkcs7_signedAndEnveloped:
331         sk = &(p7->d.signed_and_enveloped->crl);
332         break;
333     default:
334         PKCS7err(PKCS7_F_PKCS7_ADD_CRL, PKCS7_R_WRONG_CONTENT_TYPE);
335         return (0);
336     }
337
338     if (*sk == NULL)
339         *sk = sk_X509_CRL_new_null();
340     if (*sk == NULL) {
341         PKCS7err(PKCS7_F_PKCS7_ADD_CRL, ERR_R_MALLOC_FAILURE);
342         return 0;
343     }
344
345     CRYPTO_add(&crl->references, 1, CRYPTO_LOCK_X509_CRL);
346     if (!sk_X509_CRL_push(*sk, crl)) {
347         X509_CRL_free(crl);
348         return 0;
349     }
350     return (1);
351 }
352
353 int PKCS7_SIGNER_INFO_set(PKCS7_SIGNER_INFO *p7i, X509 *x509, EVP_PKEY *pkey,
354                           const EVP_MD *dgst)
355 {
356     int nid;
357     char is_dsa;
358
359     if (pkey->type == EVP_PKEY_DSA || pkey->type == EVP_PKEY_EC)
360         is_dsa = 1;
361     else
362         is_dsa = 0;
363     /* We now need to add another PKCS7_SIGNER_INFO entry */
364     if (!ASN1_INTEGER_set(p7i->version, 1))
365         goto err;
366     if (!X509_NAME_set(&p7i->issuer_and_serial->issuer,
367                        X509_get_issuer_name(x509)))
368         goto err;
369
370     /*
371      * because ASN1_INTEGER_set is used to set a 'long' we will do things the
372      * ugly way.
373      */
374     M_ASN1_INTEGER_free(p7i->issuer_and_serial->serial);
375     if (!(p7i->issuer_and_serial->serial =
376           M_ASN1_INTEGER_dup(X509_get_serialNumber(x509))))
377         goto err;
378
379     /* lets keep the pkey around for a while */
380     CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
381     p7i->pkey = pkey;
382
383     /* Set the algorithms */
384     if (is_dsa)
385         p7i->digest_alg->algorithm = OBJ_nid2obj(NID_sha1);
386     else
387         p7i->digest_alg->algorithm = OBJ_nid2obj(EVP_MD_type(dgst));
388
389     if (p7i->digest_alg->parameter != NULL)
390         ASN1_TYPE_free(p7i->digest_alg->parameter);
391     if ((p7i->digest_alg->parameter = ASN1_TYPE_new()) == NULL)
392         goto err;
393     p7i->digest_alg->parameter->type = V_ASN1_NULL;
394
395     if (p7i->digest_enc_alg->parameter != NULL)
396         ASN1_TYPE_free(p7i->digest_enc_alg->parameter);
397     nid = EVP_PKEY_type(pkey->type);
398     if (nid == EVP_PKEY_RSA) {
399         p7i->digest_enc_alg->algorithm = OBJ_nid2obj(NID_rsaEncryption);
400         if (!(p7i->digest_enc_alg->parameter = ASN1_TYPE_new()))
401             goto err;
402         p7i->digest_enc_alg->parameter->type = V_ASN1_NULL;
403     } else if (nid == EVP_PKEY_DSA) {
404 #if 1
405         /*
406          * use 'dsaEncryption' OID for compatibility with other software
407          * (PKCS #7 v1.5 does specify how to handle DSA) ...
408          */
409         p7i->digest_enc_alg->algorithm = OBJ_nid2obj(NID_dsa);
410 #else
411         /*
412          * ... although the 'dsaWithSHA1' OID (as required by RFC 2630 for
413          * CMS) would make more sense.
414          */
415         p7i->digest_enc_alg->algorithm = OBJ_nid2obj(NID_dsaWithSHA1);
416 #endif
417         p7i->digest_enc_alg->parameter = NULL; /* special case for DSA: omit
418                                                 * 'parameter'! */
419     } else if (nid == EVP_PKEY_EC) {
420         p7i->digest_enc_alg->algorithm = OBJ_nid2obj(NID_ecdsa_with_SHA1);
421         if (!(p7i->digest_enc_alg->parameter = ASN1_TYPE_new()))
422             goto err;
423         p7i->digest_enc_alg->parameter->type = V_ASN1_NULL;
424     } else
425         return (0);
426
427     return (1);
428  err:
429     return (0);
430 }
431
432 PKCS7_SIGNER_INFO *PKCS7_add_signature(PKCS7 *p7, X509 *x509, EVP_PKEY *pkey,
433                                        const EVP_MD *dgst)
434 {
435     PKCS7_SIGNER_INFO *si;
436
437     if ((si = PKCS7_SIGNER_INFO_new()) == NULL)
438         goto err;
439     if (!PKCS7_SIGNER_INFO_set(si, x509, pkey, dgst))
440         goto err;
441     if (!PKCS7_add_signer(p7, si))
442         goto err;
443     return (si);
444  err:
445     PKCS7_SIGNER_INFO_free(si);
446     return (NULL);
447 }
448
449 int PKCS7_set_digest(PKCS7 *p7, const EVP_MD *md)
450 {
451     if (PKCS7_type_is_digest(p7)) {
452         if (!(p7->d.digest->md->parameter = ASN1_TYPE_new())) {
453             PKCS7err(PKCS7_F_PKCS7_SET_DIGEST, ERR_R_MALLOC_FAILURE);
454             return 0;
455         }
456         p7->d.digest->md->parameter->type = V_ASN1_NULL;
457         p7->d.digest->md->algorithm = OBJ_nid2obj(EVP_MD_nid(md));
458         return 1;
459     }
460
461     PKCS7err(PKCS7_F_PKCS7_SET_DIGEST, PKCS7_R_WRONG_CONTENT_TYPE);
462     return 1;
463 }
464
465 STACK_OF(PKCS7_SIGNER_INFO) *PKCS7_get_signer_info(PKCS7 *p7)
466 {
467     if (PKCS7_type_is_signed(p7)) {
468         return (p7->d.sign->signer_info);
469     } else if (PKCS7_type_is_signedAndEnveloped(p7)) {
470         return (p7->d.signed_and_enveloped->signer_info);
471     } else
472         return (NULL);
473 }
474
475 PKCS7_RECIP_INFO *PKCS7_add_recipient(PKCS7 *p7, X509 *x509)
476 {
477     PKCS7_RECIP_INFO *ri;
478
479     if ((ri = PKCS7_RECIP_INFO_new()) == NULL)
480         goto err;
481     if (!PKCS7_RECIP_INFO_set(ri, x509))
482         goto err;
483     if (!PKCS7_add_recipient_info(p7, ri))
484         goto err;
485     return (ri);
486  err:
487     PKCS7_RECIP_INFO_free(ri);
488     return (NULL);
489 }
490
491 int PKCS7_add_recipient_info(PKCS7 *p7, PKCS7_RECIP_INFO *ri)
492 {
493     int i;
494     STACK_OF(PKCS7_RECIP_INFO) *sk;
495
496     i = OBJ_obj2nid(p7->type);
497     switch (i) {
498     case NID_pkcs7_signedAndEnveloped:
499         sk = p7->d.signed_and_enveloped->recipientinfo;
500         break;
501     case NID_pkcs7_enveloped:
502         sk = p7->d.enveloped->recipientinfo;
503         break;
504     default:
505         PKCS7err(PKCS7_F_PKCS7_ADD_RECIPIENT_INFO,
506                  PKCS7_R_WRONG_CONTENT_TYPE);
507         return (0);
508     }
509
510     if (!sk_PKCS7_RECIP_INFO_push(sk, ri))
511         return 0;
512     return (1);
513 }
514
515 int PKCS7_RECIP_INFO_set(PKCS7_RECIP_INFO *p7i, X509 *x509)
516 {
517     if (!ASN1_INTEGER_set(p7i->version, 0))
518         return 0;
519     if (!X509_NAME_set(&p7i->issuer_and_serial->issuer,
520                        X509_get_issuer_name(x509)))
521         return 0;
522
523     M_ASN1_INTEGER_free(p7i->issuer_and_serial->serial);
524     if (!(p7i->issuer_and_serial->serial =
525           M_ASN1_INTEGER_dup(X509_get_serialNumber(x509))))
526         return 0;
527
528     X509_ALGOR_free(p7i->key_enc_algor);
529     if (!(p7i->key_enc_algor = X509_ALGOR_dup(x509->cert_info->key->algor)))
530         return 0;
531
532     CRYPTO_add(&x509->references, 1, CRYPTO_LOCK_X509);
533     p7i->cert = x509;
534
535     return (1);
536 }
537
538 X509 *PKCS7_cert_from_signer_info(PKCS7 *p7, PKCS7_SIGNER_INFO *si)
539 {
540     if (PKCS7_type_is_signed(p7))
541         return (X509_find_by_issuer_and_serial(p7->d.sign->cert,
542                                                si->issuer_and_serial->issuer,
543                                                si->
544                                                issuer_and_serial->serial));
545     else
546         return (NULL);
547 }
548
549 int PKCS7_set_cipher(PKCS7 *p7, const EVP_CIPHER *cipher)
550 {
551     int i;
552     PKCS7_ENC_CONTENT *ec;
553
554     i = OBJ_obj2nid(p7->type);
555     switch (i) {
556     case NID_pkcs7_signedAndEnveloped:
557         ec = p7->d.signed_and_enveloped->enc_data;
558         break;
559     case NID_pkcs7_enveloped:
560         ec = p7->d.enveloped->enc_data;
561         break;
562     default:
563         PKCS7err(PKCS7_F_PKCS7_SET_CIPHER, PKCS7_R_WRONG_CONTENT_TYPE);
564         return (0);
565     }
566
567     /* Check cipher OID exists and has data in it */
568     i = EVP_CIPHER_type(cipher);
569     if (i == NID_undef) {
570         PKCS7err(PKCS7_F_PKCS7_SET_CIPHER,
571                  PKCS7_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER);
572         return (0);
573     }
574
575     ec->cipher = cipher;
576     return 1;
577 }