1 /* pk7_smime.c */
2 /*
3  * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
4  * project.
5  */
6 /* ====================================================================
7  * Copyright (c) 1999-2004 The OpenSSL Project.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in
18  *    the documentation and/or other materials provided with the
19  *    distribution.
20  *
21  * 3. All advertising materials mentioning features or use of this
22  *    software must display the following acknowledgment:
23  *    "This product includes software developed by the OpenSSL Project
24  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25  *
26  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27  *    endorse or promote products derived from this software without
28  *    prior written permission. For written permission, please contact
29  *    licensing@OpenSSL.org.
30  *
31  * 5. Products derived from this software may not be called "OpenSSL"
32  *    nor may "OpenSSL" appear in their names without prior written
33  *    permission of the OpenSSL Project.
34  *
35  * 6. Redistributions of any form whatsoever must retain the following
36  *    acknowledgment:
37  *    "This product includes software developed by the OpenSSL Project
38  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51  * OF THE POSSIBILITY OF SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This product includes cryptographic software written by Eric Young
55  * (eay@cryptsoft.com).  This product includes software written by Tim
56  * Hudson (tjh@cryptsoft.com).
57  *
58  */
59 
60 /* Simple PKCS#7 processing functions */
61 
62 #include <stdio.h>
63 #include "cryptlib.h"
64 #include <openssl/x509.h>
65 #include <openssl/x509v3.h>
66 
PKCS7_sign(X509 * signcert,EVP_PKEY * pkey,STACK_OF (X509)* certs,BIO * data,int flags)67 PKCS7 *PKCS7_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
68                   BIO *data, int flags)
69 {
70     PKCS7 *p7 = NULL;
71     PKCS7_SIGNER_INFO *si;
72     BIO *p7bio = NULL;
73     STACK_OF(X509_ALGOR) *smcap = NULL;
74     int i;
75 
76     if (!X509_check_private_key(signcert, pkey)) {
77         PKCS7err(PKCS7_F_PKCS7_SIGN,
78                  PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
79         return NULL;
80     }
81 
82     if (!(p7 = PKCS7_new())) {
83         PKCS7err(PKCS7_F_PKCS7_SIGN, ERR_R_MALLOC_FAILURE);
84         return NULL;
85     }
86 
87     if (!PKCS7_set_type(p7, NID_pkcs7_signed))
88         goto err;
89 
90     if (!PKCS7_content_new(p7, NID_pkcs7_data))
91         goto err;
92 
93 #if defined(OPENSSL_SYS_UEFI)
94     /*
95      * NOTE: Update to SHA-256 digest algorithm for UEFI version.
96      */
97     if (!(si = PKCS7_add_signature(p7, signcert, pkey, EVP_sha256()))) {
98 #else
99     if (!(si = PKCS7_add_signature(p7, signcert, pkey, EVP_sha1()))) {
100 #endif
101         PKCS7err(PKCS7_F_PKCS7_SIGN, PKCS7_R_PKCS7_ADD_SIGNATURE_ERROR);
102         goto err;
103     }
104 
105     if (!(flags & PKCS7_NOCERTS)) {
106         if (!PKCS7_add_certificate(p7, signcert))
107             goto err;
108         if (certs)
109             for (i = 0; i < sk_X509_num(certs); i++)
110                 if (!PKCS7_add_certificate(p7, sk_X509_value(certs, i)))
111                     goto err;
112     }
113 
114     if (!(flags & PKCS7_NOATTR)) {
115         if (!PKCS7_add_signed_attribute(si, NID_pkcs9_contentType,
116                                         V_ASN1_OBJECT,
117                                         OBJ_nid2obj(NID_pkcs7_data)))
118             goto err;
119         /* Add SMIMECapabilities */
120         if (!(flags & PKCS7_NOSMIMECAP)) {
121             if (!(smcap = sk_X509_ALGOR_new_null())) {
122                 PKCS7err(PKCS7_F_PKCS7_SIGN, ERR_R_MALLOC_FAILURE);
123                 goto err;
124             }
125 #ifndef OPENSSL_NO_DES
126             if (!PKCS7_simple_smimecap(smcap, NID_des_ede3_cbc, -1))
127                 goto err;
128 #endif
129 #ifndef OPENSSL_NO_RC2
130             if (!PKCS7_simple_smimecap(smcap, NID_rc2_cbc, 128))
131                 goto err;
132             if (!PKCS7_simple_smimecap(smcap, NID_rc2_cbc, 64))
133                 goto err;
134 #endif
135 #ifndef OPENSSL_NO_DES
136             if (!PKCS7_simple_smimecap(smcap, NID_des_cbc, -1))
137                 goto err;
138 #endif
139 #ifndef OPENSSL_NO_RC2
140             if (!PKCS7_simple_smimecap(smcap, NID_rc2_cbc, 40))
141                 goto err;
142 #endif
143             if (!PKCS7_add_attrib_smimecap(si, smcap))
144                 goto err;
145             sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
146             smcap = NULL;
147         }
148     }
149 
150     if (flags & PKCS7_DETACHED)
151         PKCS7_set_detached(p7, 1);
152 
153     if (flags & PKCS7_STREAM)
154         return p7;
155 
156     if (!(p7bio = PKCS7_dataInit(p7, NULL))) {
157         PKCS7err(PKCS7_F_PKCS7_SIGN, ERR_R_MALLOC_FAILURE);
158         goto err;
159     }
160 
161     SMIME_crlf_copy(data, p7bio, flags);
162 
163     if (!PKCS7_dataFinal(p7, p7bio)) {
164         PKCS7err(PKCS7_F_PKCS7_SIGN, PKCS7_R_PKCS7_DATASIGN);
165         goto err;
166     }
167 
168     BIO_free_all(p7bio);
169     return p7;
170  err:
171     sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
172     BIO_free_all(p7bio);
173     PKCS7_free(p7);
174     return NULL;
175 }
176 
177 int PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store,
178                  BIO *indata, BIO *out, int flags)
179 {
180     STACK_OF(X509) *signers;
181     X509 *signer;
182     STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
183     PKCS7_SIGNER_INFO *si;
184     X509_STORE_CTX cert_ctx;
185     char *buf = NULL;
186     int bufsiz;
187     int i, j = 0, k, ret = 0;
188     BIO *p7bio;
189     BIO *tmpin, *tmpout;
190 
191     if (!p7) {
192         PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_INVALID_NULL_POINTER);
193         return 0;
194     }
195 
196     if (!PKCS7_type_is_signed(p7)) {
197         PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_WRONG_CONTENT_TYPE);
198         return 0;
199     }
200 
201     /* Check for no data and no content: no data to verify signature */
202     if (PKCS7_get_detached(p7) && !indata) {
203         PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_NO_CONTENT);
204         return 0;
205     }
206 #if 0
207     /*
208      * NB: this test commented out because some versions of Netscape
209      * illegally include zero length content when signing data.
210      */
211 
212     /* Check for data and content: two sets of data */
213     if (!PKCS7_get_detached(p7) && indata) {
214         PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_CONTENT_AND_DATA_PRESENT);
215         return 0;
216     }
217 #endif
218 
219     sinfos = PKCS7_get_signer_info(p7);
220 
221     if (!sinfos || !sk_PKCS7_SIGNER_INFO_num(sinfos)) {
222         PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_NO_SIGNATURES_ON_DATA);
223         return 0;
224     }
225 
226     signers = PKCS7_get0_signers(p7, certs, flags);
227 
228     if (!signers)
229         return 0;
230 
231     /* Now verify the certificates */
232 
233     if (!(flags & PKCS7_NOVERIFY))
234         for (k = 0; k < sk_X509_num(signers); k++) {
235             signer = sk_X509_value(signers, k);
236             if (!(flags & PKCS7_NOCHAIN)) {
237                 if (!X509_STORE_CTX_init(&cert_ctx, store, signer,
238                                          p7->d.sign->cert)) {
239                     PKCS7err(PKCS7_F_PKCS7_VERIFY, ERR_R_X509_LIB);
240                     sk_X509_free(signers);
241                     return 0;
242                 }
243                 X509_STORE_CTX_set_default(&cert_ctx, "smime_sign");
244             } else if (!X509_STORE_CTX_init(&cert_ctx, store, signer, NULL)) {
245                 PKCS7err(PKCS7_F_PKCS7_VERIFY, ERR_R_X509_LIB);
246                 sk_X509_free(signers);
247                 return 0;
248             }
249             if (!(flags & PKCS7_NOCRL))
250                 X509_STORE_CTX_set0_crls(&cert_ctx, p7->d.sign->crl);
251             i = X509_verify_cert(&cert_ctx);
252             if (i <= 0)
253                 j = X509_STORE_CTX_get_error(&cert_ctx);
254             X509_STORE_CTX_cleanup(&cert_ctx);
255             if (i <= 0) {
256                 PKCS7err(PKCS7_F_PKCS7_VERIFY,
257                          PKCS7_R_CERTIFICATE_VERIFY_ERROR);
258                 ERR_add_error_data(2, "Verify error:",
259                                    X509_verify_cert_error_string(j));
260                 sk_X509_free(signers);
261                 return 0;
262             }
263             /* Check for revocation status here */
264         }
265 
266     /*
267      * Performance optimization: if the content is a memory BIO then store
268      * its contents in a temporary read only memory BIO. This avoids
269      * potentially large numbers of slow copies of data which will occur when
270      * reading from a read write memory BIO when signatures are calculated.
271      */
272 
273     if (indata && (BIO_method_type(indata) == BIO_TYPE_MEM)) {
274         char *ptr;
275         long len;
276         len = BIO_get_mem_data(indata, &ptr);
277         tmpin = BIO_new_mem_buf(ptr, len);
278         if (tmpin == NULL) {
279             PKCS7err(PKCS7_F_PKCS7_VERIFY, ERR_R_MALLOC_FAILURE);
280             return 0;
281         }
282     } else
283         tmpin = indata;
284 
285     if (!(p7bio = PKCS7_dataInit(p7, tmpin)))
286         goto err;
287 
288     if (flags & PKCS7_TEXT) {
289         if (!(tmpout = BIO_new(BIO_s_mem()))) {
290             PKCS7err(PKCS7_F_PKCS7_VERIFY, ERR_R_MALLOC_FAILURE);
291             goto err;
292         }
293         BIO_set_mem_eof_return(tmpout, 0);
294     } else
295         tmpout = out;
296 
297     bufsiz = 4096;
298     buf = OPENSSL_malloc (bufsiz);
299     if (buf == NULL) {
300       goto err;
301     }
302 
303     /* We now have to 'read' from p7bio to calculate digests etc. */
304     for (;;) {
305         i = BIO_read(p7bio, buf, sizeof(buf));
306         if (i <= 0)
307             break;
308         if (tmpout)
309             BIO_write(tmpout, buf, i);
310     }
311 
312     if (flags & PKCS7_TEXT) {
313         if (!SMIME_text(tmpout, out)) {
314             PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_SMIME_TEXT_ERROR);
315             BIO_free(tmpout);
316             goto err;
317         }
318         BIO_free(tmpout);
319     }
320 
321     /* Now Verify All Signatures */
322     if (!(flags & PKCS7_NOSIGS))
323         for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) {
324             si = sk_PKCS7_SIGNER_INFO_value(sinfos, i);
325             signer = sk_X509_value(signers, i);
326             j = PKCS7_signatureVerify(p7bio, p7, si, signer);
327             if (j <= 0) {
328                 PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_SIGNATURE_FAILURE);
329                 goto err;
330             }
331         }
332 
333     ret = 1;
334 
335  err:
336 
337     if (tmpin == indata) {
338         if (indata)
339             BIO_pop(p7bio);
340     }
341     BIO_free_all(p7bio);
342 
343     sk_X509_free(signers);
344 
345     if (buf != NULL) {
346       OPENSSL_free (buf);
347     }
348 
349     return ret;
350 }
351 
352 STACK_OF(X509) *PKCS7_get0_signers(PKCS7 *p7, STACK_OF(X509) *certs,
353                                    int flags)
354 {
355     STACK_OF(X509) *signers;
356     STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
357     PKCS7_SIGNER_INFO *si;
358     PKCS7_ISSUER_AND_SERIAL *ias;
359     X509 *signer;
360     int i;
361 
362     if (!p7) {
363         PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, PKCS7_R_INVALID_NULL_POINTER);
364         return NULL;
365     }
366 
367     if (!PKCS7_type_is_signed(p7)) {
368         PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, PKCS7_R_WRONG_CONTENT_TYPE);
369         return NULL;
370     }
371 
372     /* Collect all the signers together */
373 
374     sinfos = PKCS7_get_signer_info(p7);
375 
376     if (sk_PKCS7_SIGNER_INFO_num(sinfos) <= 0) {
377         PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, PKCS7_R_NO_SIGNERS);
378         return NULL;
379     }
380 
381     if (!(signers = sk_X509_new_null())) {
382         PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, ERR_R_MALLOC_FAILURE);
383         return NULL;
384     }
385 
386     for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) {
387         si = sk_PKCS7_SIGNER_INFO_value(sinfos, i);
388         ias = si->issuer_and_serial;
389         signer = NULL;
390         /* If any certificates passed they take priority */
391         if (certs)
392             signer = X509_find_by_issuer_and_serial(certs,
393                                                     ias->issuer, ias->serial);
394         if (!signer && !(flags & PKCS7_NOINTERN)
395             && p7->d.sign->cert)
396             signer =
397                 X509_find_by_issuer_and_serial(p7->d.sign->cert,
398                                                ias->issuer, ias->serial);
399         if (!signer) {
400             PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS,
401                      PKCS7_R_SIGNER_CERTIFICATE_NOT_FOUND);
402             sk_X509_free(signers);
403             return NULL;
404         }
405 
406         if (!sk_X509_push(signers, signer)) {
407             sk_X509_free(signers);
408             return NULL;
409         }
410     }
411     return signers;
412 }
413 
414 /* Build a complete PKCS#7 enveloped data */
415 
416 PKCS7 *PKCS7_encrypt(STACK_OF(X509) *certs, BIO *in, const EVP_CIPHER *cipher,
417                      int flags)
418 {
419     PKCS7 *p7;
420     BIO *p7bio = NULL;
421     int i;
422     X509 *x509;
423     if (!(p7 = PKCS7_new())) {
424         PKCS7err(PKCS7_F_PKCS7_ENCRYPT, ERR_R_MALLOC_FAILURE);
425         return NULL;
426     }
427 
428     if (!PKCS7_set_type(p7, NID_pkcs7_enveloped))
429         goto err;
430     if (!PKCS7_set_cipher(p7, cipher)) {
431         PKCS7err(PKCS7_F_PKCS7_ENCRYPT, PKCS7_R_ERROR_SETTING_CIPHER);
432         goto err;
433     }
434 
435     for (i = 0; i < sk_X509_num(certs); i++) {
436         x509 = sk_X509_value(certs, i);
437         if (!PKCS7_add_recipient(p7, x509)) {
438             PKCS7err(PKCS7_F_PKCS7_ENCRYPT, PKCS7_R_ERROR_ADDING_RECIPIENT);
439             goto err;
440         }
441     }
442 
443     if (!(p7bio = PKCS7_dataInit(p7, NULL))) {
444         PKCS7err(PKCS7_F_PKCS7_ENCRYPT, ERR_R_MALLOC_FAILURE);
445         goto err;
446     }
447 
448     SMIME_crlf_copy(in, p7bio, flags);
449 
450     (void)BIO_flush(p7bio);
451 
452     if (!PKCS7_dataFinal(p7, p7bio)) {
453         PKCS7err(PKCS7_F_PKCS7_ENCRYPT, PKCS7_R_PKCS7_DATAFINAL_ERROR);
454         goto err;
455     }
456     BIO_free_all(p7bio);
457 
458     return p7;
459 
460  err:
461 
462     BIO_free_all(p7bio);
463     PKCS7_free(p7);
464     return NULL;
465 
466 }
467 
468 int PKCS7_decrypt(PKCS7 *p7, EVP_PKEY *pkey, X509 *cert, BIO *data, int flags)
469 {
470     BIO *tmpmem;
471     int ret, i;
472     char buf[4096];
473 
474     if (!p7) {
475         PKCS7err(PKCS7_F_PKCS7_DECRYPT, PKCS7_R_INVALID_NULL_POINTER);
476         return 0;
477     }
478 
479     if (!PKCS7_type_is_enveloped(p7)) {
480         PKCS7err(PKCS7_F_PKCS7_DECRYPT, PKCS7_R_WRONG_CONTENT_TYPE);
481         return 0;
482     }
483 
484     if (cert && !X509_check_private_key(cert, pkey)) {
485         PKCS7err(PKCS7_F_PKCS7_DECRYPT,
486                  PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
487         return 0;
488     }
489 
490     if (!(tmpmem = PKCS7_dataDecode(p7, pkey, NULL, cert))) {
491         PKCS7err(PKCS7_F_PKCS7_DECRYPT, PKCS7_R_DECRYPT_ERROR);
492         return 0;
493     }
494 
495     if (flags & PKCS7_TEXT) {
496         BIO *tmpbuf, *bread;
497         /* Encrypt BIOs can't do BIO_gets() so add a buffer BIO */
498         if (!(tmpbuf = BIO_new(BIO_f_buffer()))) {
499             PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE);
500             BIO_free_all(tmpmem);
501             return 0;
502         }
503         if (!(bread = BIO_push(tmpbuf, tmpmem))) {
504             PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE);
505             BIO_free_all(tmpbuf);
506             BIO_free_all(tmpmem);
507             return 0;
508         }
509         ret = SMIME_text(bread, data);
510         if (ret > 0 && BIO_method_type(tmpmem) == BIO_TYPE_CIPHER) {
511             if (!BIO_get_cipher_status(tmpmem))
512                 ret = 0;
513         }
514         BIO_free_all(bread);
515         return ret;
516     } else {
517         for (;;) {
518             i = BIO_read(tmpmem, buf, sizeof(buf));
519             if (i <= 0) {
520                 ret = 1;
521                 if (BIO_method_type(tmpmem) == BIO_TYPE_CIPHER) {
522                     if (!BIO_get_cipher_status(tmpmem))
523                         ret = 0;
524                 }
525 
526                 break;
527             }
528             if (BIO_write(data, buf, i) != i) {
529                 ret = 0;
530                 break;
531             }
532         }
533         BIO_free_all(tmpmem);
534         return ret;
535     }
536 }
537