1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ OpenSSL functions. TODO this needs an overhaul -- there _are_ stack leaks!?
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2014 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
9 * Gunnar Ritter. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by Gunnar Ritter
22 * and his contributors.
23 * 4. Neither the name of Gunnar Ritter nor the names of his contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY GUNNAR RITTER AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL GUNNAR RITTER OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #ifndef HAVE_AMALGAMATION
46 #include <sys/socket.h>
51 #include <netinet/in.h>
53 #include <openssl/crypto.h>
54 #include <openssl/ssl.h>
55 #include <openssl/err.h>
56 #include <openssl/x509v3.h>
57 #include <openssl/x509.h>
58 #include <openssl/pem.h>
59 #include <openssl/rand.h>
61 #ifdef HAVE_ARPA_INET_H
62 # include <arpa/inet.h>
66 * OpenSSL client implementation according to: John Viega, Matt Messier,
67 * Pravir Chandra: Network Security with OpenSSL. Sebastopol, CA 2002.
71 # define _STACKOF(X) STACK_OF(X)
73 # define _STACKOF(X) /*X*/STACK
77 char const sm_name
[8];
78 SSL_METHOD
const * (*sm_fun
)(void);
82 char const sc_name
[8];
83 EVP_CIPHER
const * (*sc_fun
)(void);
86 /* Supported SSL/TLS methods: update manual on change! */
87 static struct ssl_method
const _ssl_methods
[] = {
88 {"auto", &SSLv23_client_method
},
89 #define _SSL_DEFAULT_METHOD SSLv23_client_method
90 #ifndef OPENSSL_NO_TLS1
91 # ifdef TLS1_2_VERSION
92 {"tls1.2", &TLSv1_2_client_method
},
94 # ifdef TLS1_1_VERSION
95 {"tls1.1", &TLSv1_1_client_method
},
97 {"tls1", &TLSv1_client_method
},
99 #ifndef OPENSSL_NO_SSL3
100 {"ssl3", &SSLv3_client_method
},
102 #ifndef OPENSSL_NO_SSL2
103 {"ssl2", &SSLv2_client_method
}
107 /* Supported S/MIME cipher algorithms: update manual on change! */
108 static struct smime_cipher
const _smime_ciphers
[] = {
109 #ifndef OPENSSL_NO_AES
110 # define _SMIME_DEFAULT_CIPHER EVP_aes_128_cbc /* According to RFC 5751 */
111 {"aes-128", &EVP_aes_128_cbc
},
112 {"aes-256", &EVP_aes_256_cbc
},
113 {"aes-192", &EVP_aes_192_cbc
},
115 #ifndef OPENSSL_NO_DES
116 # ifndef _SMIME_DEFAULT_CIPHER
117 # define _SMIME_DEFAULT_CIPHER EVP_des_ede3_cbc
119 {"des3", &EVP_des_ede3_cbc
},
120 {"des", &EVP_des_cbc
},
122 #ifndef OPENSSL_NO_RC2
123 {"rc2-40", &EVP_rc2_40_cbc
},
124 {"rc2-64", &EVP_rc2_64_cbc
},
127 #ifndef _SMIME_DEFAULT_CIPHER
128 # error Your OpenSSL library does not include the necessary
129 # error cipher algorithms that are required to support S/MIME
132 static int initialized
;
133 static int rand_init
;
134 static int message_number
;
135 static int verify_error_found
;
137 static int ssl_rand_init(void);
138 static void ssl_init(void);
139 static int ssl_verify_cb(int success
, X509_STORE_CTX
*store
);
140 static const SSL_METHOD
*ssl_select_method(char const *uhp
);
141 static void ssl_load_verifications(struct sock
*sp
);
142 static void ssl_certificate(struct sock
*sp
, char const *uhp
);
143 static enum okay
ssl_check_host(char const *server
, struct sock
*sp
);
144 static int smime_verify(struct message
*m
, int n
, _STACKOF(X509
) *chain
,
146 static EVP_CIPHER
const * _smime_cipher(char const *name
);
147 static int ssl_password_cb(char *buf
, int size
, int rwflag
,
149 static FILE * smime_sign_cert(char const *xname
, char const *xname2
,
151 static char * smime_sign_include_certs(char const *name
);
152 static int smime_sign_include_chain_creat(_STACKOF(X509
) **chain
,
154 #if defined X509_V_FLAG_CRL_CHECK && defined X509_V_FLAG_CRL_CHECK_ALL
155 static enum okay
load_crl1(X509_STORE
*store
, char const *name
);
157 static enum okay
load_crls(X509_STORE
*store
, enum okeys fok
, enum okeys dok
);
166 if ((cp
= ok_vlook(ssl_rand_egd
)) != NULL
) {
167 if ((x
= file_expand(cp
)) == NULL
|| RAND_egd(cp
= x
) == -1)
168 fprintf(stderr
, tr(245, "entropy daemon at \"%s\" not available\n"),
172 } else if ((cp
= ok_vlook(ssl_rand_file
)) != NULL
) {
173 if ((x
= file_expand(cp
)) == NULL
|| RAND_load_file(cp
= x
, 1024) == -1)
174 fprintf(stderr
, tr(246, "entropy file at \"%s\" not available\n"), cp
);
178 if (!stat(cp
, &st
) && S_ISREG(st
.st_mode
) && !access(cp
, W_OK
)) {
179 if (RAND_write_file(cp
) == -1) {
180 fprintf(stderr
, tr(247,
181 "writing entropy data to \"%s\" failed\n"), cp
);
195 if (initialized
== 0) {
200 rand_init
= ssl_rand_init();
205 ssl_verify_cb(int success
, X509_STORE_CTX
*store
)
212 X509
*cert
= X509_STORE_CTX_get_current_cert(store
);
213 int depth
= X509_STORE_CTX_get_error_depth(store
);
214 int err
= X509_STORE_CTX_get_error(store
);
216 verify_error_found
= 1;
218 fprintf(stderr
, "Message %d: ", message_number
);
219 fprintf(stderr
, tr(229, "Error with certificate at depth: %i\n"), depth
);
220 X509_NAME_oneline(X509_get_issuer_name(cert
), data
, sizeof data
);
221 fprintf(stderr
, tr(230, " issuer = %s\n"), data
);
222 X509_NAME_oneline(X509_get_subject_name(cert
), data
, sizeof data
);
223 fprintf(stderr
, tr(231, " subject = %s\n"), data
);
224 fprintf(stderr
, tr(232, " err %i: %s\n"),
225 err
, X509_verify_cert_error_string(err
));
226 if (ssl_verify_decide() != OKAY
)
233 static SSL_METHOD
const *
234 ssl_select_method(char const *uhp
)
236 SSL_METHOD
const *method
;
241 if ((cp
= ssl_method_string(uhp
)) != NULL
) {
243 for (i
= 0; i
< NELEM(_ssl_methods
); ++i
)
244 if (!strcmp(_ssl_methods
[i
].sm_name
, cp
)) {
245 method
= (*_ssl_methods
[i
].sm_fun
)();
248 fprintf(stderr
, tr(244, "Invalid SSL method \"%s\"\n"), cp
);
250 method
= _SSL_DEFAULT_METHOD();
257 ssl_load_verifications(struct sock
*sp
)
259 char *ca_dir
, *ca_file
;
263 if (ssl_verify_level
== SSL_VERIFY_IGNORE
)
266 if ((ca_dir
= ok_vlook(ssl_ca_dir
)) != NULL
)
267 ca_dir
= file_expand(ca_dir
);
268 if ((ca_file
= ok_vlook(ssl_ca_file
)) != NULL
)
269 ca_file
= file_expand(ca_file
);
271 if (ca_dir
!= NULL
|| ca_file
!= NULL
) {
272 if (SSL_CTX_load_verify_locations(sp
->s_ctx
, ca_file
, ca_dir
) != 1) {
273 fprintf(stderr
, tr(233, "Error loading "));
275 fputs(ca_dir
, stderr
);
277 fputs(tr(234, " or "), stderr
);
280 fputs(ca_file
, stderr
);
285 if (!ok_blook(ssl_no_default_ca
)) {
286 if (SSL_CTX_set_default_verify_paths(sp
->s_ctx
) != 1)
287 fprintf(stderr
, tr(243, "Error loading default CA locations\n"));
290 verify_error_found
= 0;
292 SSL_CTX_set_verify(sp
->s_ctx
, SSL_VERIFY_PEER
, ssl_verify_cb
);
293 store
= SSL_CTX_get_cert_store(sp
->s_ctx
);
294 load_crls(store
, ok_v_ssl_crl_file
, ok_v_ssl_crl_dir
);
300 ssl_certificate(struct sock
*sp
, char const *uhp
)
303 char *certvar
, *keyvar
, *cert
, *key
, *x
;
307 certvar
= ac_alloc(i
+ 9 +1);
308 memcpy(certvar
, "ssl-cert-", 9);
309 memcpy(certvar
+ 9, uhp
, i
+1);
311 if ((cert
= vok_vlook(certvar
)) != NULL
||
312 (cert
= ok_vlook(ssl_cert
)) != NULL
) {
314 if ((cert
= file_expand(cert
)) == NULL
) {
317 } else if (SSL_CTX_use_certificate_chain_file(sp
->s_ctx
, cert
) == 1) {
318 keyvar
= ac_alloc(strlen(uhp
) + 8 +1);
319 memcpy(keyvar
, "ssl-key-", 8);
320 memcpy(keyvar
+ 8, uhp
, i
+1);
321 if ((key
= vok_vlook(keyvar
)) == NULL
&&
322 (key
= ok_vlook(ssl_key
)) == NULL
)
324 else if ((x
= key
, key
= file_expand(key
)) == NULL
) {
328 if (SSL_CTX_use_PrivateKey_file(sp
->s_ctx
, key
, SSL_FILETYPE_PEM
) != 1)
330 fprintf(stderr
, tr(238, "cannot load private key from file %s\n"),
335 fprintf(stderr
, tr(239, "cannot load certificate from file %s\n"),
343 ssl_check_host(char const *server
, struct sock
*sp
)
348 _STACKOF(GENERAL_NAME
) *gens
;
354 if ((cert
= SSL_get_peer_certificate(sp
->s_ssl
)) == NULL
) {
355 fprintf(stderr
, tr(248, "no certificate from \"%s\"\n"), server
);
359 gens
= X509_get_ext_d2i(cert
, NID_subject_alt_name
, NULL
, NULL
);
361 for (i
= 0; i
< sk_GENERAL_NAME_num(gens
); ++i
) {
362 gen
= sk_GENERAL_NAME_value(gens
, i
);
363 if (gen
->type
== GEN_DNS
) {
364 if (options
& OPT_VERBOSE
)
365 fprintf(stderr
, "Comparing DNS name: \"%s\"\n",
367 rv
= rfc2595_hostname_match(server
, (char*)gen
->d
.ia5
->data
);
374 if ((subj
= X509_get_subject_name(cert
)) != NULL
&&
375 X509_NAME_get_text_by_NID(subj
, NID_commonName
, data
, sizeof data
)
377 data
[sizeof data
- 1] = '\0';
378 if (options
& OPT_VERBOSE
)
379 fprintf(stderr
, "Comparing common name: \"%s\"\n", data
);
380 rv
= rfc2595_hostname_match(server
, data
);
390 smime_verify(struct message
*m
, int n
, _STACKOF(X509
) *chain
, X509_STORE
*store
)
392 char data
[LINESIZE
], *sender
, *to
, *cc
, *cnttype
;
399 _STACKOF(X509
) *certs
;
400 _STACKOF(GENERAL_NAME
) *gens
;
409 verify_error_found
= 0;
413 sender
= getsender(m
);
414 to
= hfield1("to", m
);
415 cc
= hfield1("cc", m
);
416 cnttype
= hfield1("content-type", m
);
417 if ((ip
= setinput(&mb
, m
, NEED_BODY
)) == NULL
)
419 if (cnttype
&& !strncmp(cnttype
, "application/x-pkcs7-mime", 24)) {
420 if ((x
= smime_decrypt(m
, to
, cc
, 1)) == NULL
)
422 if (x
!= (struct message
*)-1) {
431 if ((fp
= Ftmp(NULL
, "smimever", OF_RDWR
| OF_UNLINK
| OF_REGISTER
, 0600)) ==
442 if ((fb
= BIO_new_fp(fp
, BIO_NOCLOSE
)) == NULL
) {
444 "Error creating BIO verification object for message %d"), n
);
448 if ((pkcs7
= SMIME_read_PKCS7(fb
, &pb
)) == NULL
) {
449 ssl_gen_err(tr(538, "Error reading PKCS#7 object for message %d"), n
);
452 if (PKCS7_verify(pkcs7
, chain
, store
, pb
, NULL
, 0) != 1) {
453 ssl_gen_err(tr(539, "Error verifying message %d"), n
);
457 if (sender
== NULL
) {
458 fprintf(stderr
, tr(540, "Warning: Message %d has no sender.\n"), n
);
463 certs
= PKCS7_get0_signers(pkcs7
, chain
, 0);
465 fprintf(stderr
, tr(541, "No certificates found in message %d.\n"), n
);
469 for (i
= 0; i
< sk_X509_num(certs
); ++i
) {
470 cert
= sk_X509_value(certs
, i
);
471 gens
= X509_get_ext_d2i(cert
, NID_subject_alt_name
, NULL
, NULL
);
473 for (j
= 0; j
< sk_GENERAL_NAME_num(gens
); ++j
) {
474 gen
= sk_GENERAL_NAME_value(gens
, j
);
475 if (gen
->type
== GEN_EMAIL
) {
476 if (options
& OPT_VERBOSE
)
477 fprintf(stderr
, "Comparing alt. address: %s\"\n", data
);
478 if (!asccasecmp((char*)gen
->d
.ia5
->data
, sender
))
484 if ((subj
= X509_get_subject_name(cert
)) != NULL
&&
485 X509_NAME_get_text_by_NID(subj
, NID_pkcs9_emailAddress
,
486 data
, sizeof data
) > 0) {
487 data
[sizeof data
-1] = '\0';
488 if (options
& OPT_VERBOSE
)
489 fprintf(stderr
, "Comparing address: \"%s\"\n", data
);
490 if (!asccasecmp(data
, sender
))
494 fprintf(stderr
, tr(542, "Message %d: certificate does not match <%s>\n"),
498 if (verify_error_found
== 0)
499 printf(tr(543, "Message %d was verified successfully.\n"), n
);
500 rv
= verify_error_found
;
510 static EVP_CIPHER
const *
511 _smime_cipher(char const *name
)
513 EVP_CIPHER
const *cipher
;
518 vn
= ac_alloc(i
= strlen(name
) + 13 +1);
519 snprintf(vn
, (int)i
, "smime-cipher-%s", name
);
525 for (i
= 0; i
< NELEM(_smime_ciphers
); ++i
)
526 if (!strcmp(_smime_ciphers
[i
].sc_name
, cp
)) {
527 cipher
= (*_smime_ciphers
[i
].sc_fun
)();
530 fprintf(stderr
, tr(240, "Invalid cipher(s): %s\n"), cp
);
532 cipher
= _SMIME_DEFAULT_CIPHER();
539 ssl_password_cb(char *buf
, int size
, int rwflag
, void *userdata
)
547 if ((pass
= getpassword("PEM pass phrase:")) != NULL
) {
549 if (UICMP(z
, len
, >=, size
))
551 memcpy(buf
, pass
, len
);
560 smime_sign_cert(char const *xname
, char const *xname2
, bool_t dowarn
)
565 char const *name
= xname
, *name2
= xname2
;
571 np
= lextract(name
, GTO
| GSKIN
);
573 /* This needs to be more intelligent since it will currently take the
574 * first name for which a private key is available regardless of
575 * whether it is the right one for the message */
576 vn
= ac_alloc(vs
= strlen(np
->n_name
) + 30);
577 snprintf(vn
, vs
, "smime-sign-cert-%s", np
->n_name
);
591 if ((cp
= ok_vlook(smime_sign_cert
)) == NULL
)
594 if ((cp
= file_expand(cp
)) == NULL
)
596 if ((fp
= Fopen(cp
, "r")) == NULL
)
603 fprintf(stderr
, tr(558, "Could not find a certificate for %s"), xname
);
605 fprintf(stderr
, tr(559, "or %s"), xname2
);
612 smime_sign_include_certs(char const *name
)
617 /* See comments in smime_sign_cert() for algorithm pitfalls */
621 for (np
= lextract(name
, GTO
| GSKIN
); np
!= NULL
; np
= np
->n_flink
) {
623 char *vn
= ac_alloc(vs
= strlen(np
->n_name
) + 30);
624 snprintf(vn
, vs
, "smime-sign-include-certs-%s", np
->n_name
);
631 rv
= ok_vlook(smime_sign_include_certs
);
638 smime_sign_include_chain_creat(_STACKOF(X509
) **chain
, char const *cfiles
)
642 char *rest
, *x
, *ncf
;
645 *chain
= sk_X509_new_null();
647 for (rest
= savestr(cfiles
);;) {
648 ncf
= strchr(rest
, ',');/* FIXME use n_strsep(): this FIXES behaviour! */
651 /* This fails for '=,file' constructs, but those are sick */
655 if ((x
= file_expand(rest
)) == NULL
||
656 (fp
= Fopen(rest
= x
, "r")) == NULL
) {
660 if ((tmp
= PEM_read_X509(fp
, NULL
, &ssl_password_cb
, NULL
)) == NULL
) {
661 ssl_gen_err(tr(560, "Error reading certificate from \"%s\""), rest
);
665 sk_X509_push(*chain
, tmp
);
673 if (sk_X509_num(*chain
) == 0) {
674 fprintf(stderr
, tr(561, "smime-sign-include-certs defined but empty\n"));
679 return (*chain
!= NULL
);
681 sk_X509_pop_free(*chain
, X509_free
);
686 #if defined X509_V_FLAG_CRL_CHECK && defined X509_V_FLAG_CRL_CHECK_ALL
688 load_crl1(X509_STORE
*store
, char const *name
)
694 if (options
& OPT_VERBOSE
)
695 printf("Loading CRL from \"%s\".\n", name
);
696 if ((lookup
= X509_STORE_add_lookup(store
, X509_LOOKUP_file())) == NULL
) {
697 ssl_gen_err(tr(565, "Error creating X509 lookup object"));
700 if (X509_load_crl_file(lookup
, name
, X509_FILETYPE_PEM
) != 1) {
701 ssl_gen_err(tr(566, "Error loading CRL from \"%s\""), name
);
709 #endif /* new OpenSSL */
712 load_crls(X509_STORE
*store
, enum okeys fok
, enum okeys dok
)
714 char *crl_file
, *crl_dir
;
715 #if defined X509_V_FLAG_CRL_CHECK && defined X509_V_FLAG_CRL_CHECK_ALL
724 if ((crl_file
= _var_oklook(fok
)) != NULL
) {
725 #if defined X509_V_FLAG_CRL_CHECK && defined X509_V_FLAG_CRL_CHECK_ALL
726 if ((crl_file
= file_expand(crl_file
)) == NULL
||
727 load_crl1(store
, crl_file
) != OKAY
)
730 fprintf(stderr
, tr(567,
731 "This OpenSSL version is too old to use CRLs.\n"));
736 if ((crl_dir
= _var_oklook(dok
)) != NULL
) {
737 #if defined X509_V_FLAG_CRL_CHECK && defined X509_V_FLAG_CRL_CHECK_ALL
739 if ((x
= file_expand(crl_dir
)) == NULL
||
740 (dirp
= opendir(crl_dir
= x
)) == NULL
) {
745 ds
= strlen(crl_dir
);
746 fn
= smalloc(fs
= ds
+ 20);
747 memcpy(fn
, crl_dir
, ds
);
749 while ((dp
= readdir(dirp
)) != NULL
) {
750 if (dp
->d_name
[0] == '.' && (dp
->d_name
[1] == '\0' ||
751 (dp
->d_name
[1] == '.' && dp
->d_name
[2] == '\0')))
753 if (dp
->d_name
[0] == '.')
755 if (ds
+ (es
= strlen(dp
->d_name
)) + 2 < fs
)
756 fn
= srealloc(fn
, fs
= ds
+ es
+ 20);
757 memcpy(fn
+ ds
+ 1, dp
->d_name
, es
+ 1);
758 if (load_crl1(store
, fn
) != OKAY
) {
766 #else /* old OpenSSL */
767 fprintf(stderr
, tr(567,
768 "This OpenSSL version is too old to use CRLs.\n"));
772 #if defined X509_V_FLAG_CRL_CHECK && defined X509_V_FLAG_CRL_CHECK_ALL
773 if (crl_file
|| crl_dir
)
774 X509_STORE_set_flags(store
, X509_V_FLAG_CRL_CHECK
|
775 X509_V_FLAG_CRL_CHECK_ALL
);
784 ssl_open(char const *server
, struct sock
*sp
, char const *uhp
)
792 ssl_set_verify_level(uhp
);
793 if ((sp
->s_ctx
= SSL_CTX_new(UNCONST(ssl_select_method(uhp
)))) == NULL
) {
794 ssl_gen_err(tr(261, "SSL_CTX_new() failed"));
798 #ifdef SSL_MODE_AUTO_RETRY
799 /* available with OpenSSL 0.9.6 or later */
800 SSL_CTX_set_mode(sp
->s_ctx
, SSL_MODE_AUTO_RETRY
);
801 #endif /* SSL_MODE_AUTO_RETRY */
803 if (!ok_blook(ssl_v2_allow
))
804 opts
|= SSL_OP_NO_SSLv2
;
805 SSL_CTX_set_options(sp
->s_ctx
, opts
);
806 ssl_load_verifications(sp
);
807 ssl_certificate(sp
, uhp
);
808 if ((cp
= ok_vlook(ssl_cipher_list
)) != NULL
) {
809 if (SSL_CTX_set_cipher_list(sp
->s_ctx
, cp
) != 1)
810 fprintf(stderr
, tr(240, "Invalid cipher(s): %s\n"), cp
);
813 if ((sp
->s_ssl
= SSL_new(sp
->s_ctx
)) == NULL
) {
814 ssl_gen_err(tr(262, "SSL_new() failed"));
817 SSL_set_fd(sp
->s_ssl
, sp
->s_fd
);
818 if (SSL_connect(sp
->s_ssl
) < 0) {
819 ssl_gen_err(tr(263, "could not initiate SSL/TLS connection"));
822 if (ssl_verify_level
!= SSL_VERIFY_IGNORE
) {
823 if (ssl_check_host(server
, sp
) != OKAY
) {
824 fprintf(stderr
, tr(249, "host certificate does not match \"%s\"\n"),
826 if (ssl_verify_decide() != OKAY
)
838 ssl_gen_err(char const *fmt
, ...)
844 vfprintf(stderr
, fmt
, ap
);
846 SSL_load_error_strings();
847 fprintf(stderr
, ": %s\n", ERR_error_string(ERR_get_error(), NULL
));
854 int *msgvec
= vp
, *ip
, ec
= 0, rv
= 1;
855 _STACKOF(X509
) *chain
= NULL
;
857 char *ca_dir
, *ca_file
;
862 ssl_verify_level
= SSL_VERIFY_STRICT
;
863 if ((store
= X509_STORE_new()) == NULL
) {
864 ssl_gen_err(tr(544, "Error creating X509 store"));
867 X509_STORE_set_verify_cb_func(store
, ssl_verify_cb
);
869 if ((ca_dir
= ok_vlook(smime_ca_dir
)) != NULL
)
870 ca_dir
= file_expand(ca_dir
);
871 if ((ca_file
= ok_vlook(smime_ca_file
)) != NULL
)
872 ca_file
= file_expand(ca_file
);
874 if (ca_dir
!= NULL
|| ca_file
!= NULL
) {
875 if (X509_STORE_load_locations(store
, ca_file
, ca_dir
) != 1) {
876 ssl_gen_err(tr(545, "Error loading %s"),
877 (ca_file
!= NULL
) ? ca_file
: ca_dir
);
881 if (!ok_blook(smime_no_default_ca
)) {
882 if (X509_STORE_set_default_paths(store
) != 1) {
883 ssl_gen_err(tr(546, "Error loading default CA locations"));
888 if (load_crls(store
, ok_v_smime_crl_file
, ok_v_smime_crl_dir
) != OKAY
)
890 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
891 struct message
*mp
= message
+ *ip
- 1;
893 ec
|= smime_verify(mp
, *ip
, chain
, store
);
896 exit_status
|= EXIT_ERR
;
903 smime_sign(FILE *ip
, struct header
*headp
)
905 FILE *rv
= NULL
, *sp
= NULL
, *fp
= NULL
, *bp
, *hp
;
908 _STACKOF(X509
) *chain
= NULL
;
910 EVP_PKEY
*pkey
= NULL
;
917 if ((addr
= myorigin(headp
)) == NULL
) {
918 fprintf(stderr
, tr(531, "No \"from\" address for signing specified\n"));
921 if ((fp
= smime_sign_cert(addr
, NULL
, 1)) == NULL
)
924 if ((pkey
= PEM_read_PrivateKey(fp
, NULL
, &ssl_password_cb
, NULL
)) == NULL
) {
925 ssl_gen_err(tr(532, "Error reading private key from"));
930 if ((cert
= PEM_read_X509(fp
, NULL
, &ssl_password_cb
, NULL
)) == NULL
) {
931 ssl_gen_err(tr(533, "Error reading signer certificate from"));
937 if ((addr
= smime_sign_include_certs(addr
)) != NULL
&&
938 !smime_sign_include_chain_creat(&chain
, addr
))
941 if ((sp
= Ftmp(NULL
, "smimesign", OF_RDWR
| OF_UNLINK
| OF_REGISTER
, 0600))
948 if (smime_split(ip
, &hp
, &bp
, -1, 0) == STOP
) {
954 if ((bb
= BIO_new_fp(bp
, BIO_NOCLOSE
)) == NULL
||
955 (sb
= BIO_new_fp(sp
, BIO_NOCLOSE
)) == NULL
) {
956 ssl_gen_err(tr(534, "Error creating BIO signing objects"));
961 if ((pkcs7
= PKCS7_sign(cert
, pkey
, chain
, bb
,
962 PKCS7_DETACHED
)) == NULL
) {
963 ssl_gen_err(tr(535, "Error creating the PKCS#7 signing object"));
967 if (PEM_write_bio_PKCS7(sb
, pkcs7
) == 0) {
968 ssl_gen_err(tr(536, "Error writing signed S/MIME data"));
980 rv
= smime_sign_assemble(hp
, bp
, sp
);
987 sk_X509_pop_free(chain
, X509_free
);
999 smime_encrypt(FILE *ip
, char const *xcertfile
, char const *to
)
1001 char *certfile
= UNCONST(xcertfile
);
1002 FILE *rv
= NULL
, *yp
, *fp
, *bp
, *hp
;
1006 _STACKOF(X509
) *certs
;
1007 EVP_CIPHER
const *cipher
;
1011 if ((certfile
= file_expand(certfile
)) == NULL
)
1015 if ((cipher
= _smime_cipher(to
)) == NULL
)
1017 if ((fp
= Fopen(certfile
, "r")) == NULL
) {
1022 if ((cert
= PEM_read_X509(fp
, NULL
, &ssl_password_cb
, NULL
)) == NULL
) {
1023 ssl_gen_err(tr(547, "Error reading encryption certificate from \"%s\""),
1032 certs
= sk_X509_new_null();
1033 sk_X509_push(certs
, cert
);
1035 if ((yp
= Ftmp(NULL
, "smimeenc", OF_RDWR
| OF_UNLINK
| OF_REGISTER
, 0600)) ==
1042 if (smime_split(ip
, &hp
, &bp
, -1, 0) == STOP
) {
1048 if ((bb
= BIO_new_fp(bp
, BIO_NOCLOSE
)) == NULL
||
1049 (yb
= BIO_new_fp(yp
, BIO_NOCLOSE
)) == NULL
) {
1050 ssl_gen_err(tr(548, "Error creating BIO encryption objects"));
1054 if ((pkcs7
= PKCS7_encrypt(certs
, bb
, cipher
, 0)) == NULL
) {
1055 ssl_gen_err(tr(549, "Error creating the PKCS#7 encryption object"));
1059 if (PEM_write_bio_PKCS7(yb
, pkcs7
) == 0) {
1060 ssl_gen_err(tr(550, "Error writing encrypted S/MIME data"));
1074 rv
= smime_encrypt_assemble(hp
, yp
);
1082 smime_decrypt(struct message
*m
, char const *to
, char const *cc
, int signcall
)
1085 FILE *fp
, *bp
, *hp
, *op
;
1099 if ((yp
= setinput(&mb
, m
, NEED_BODY
)) == NULL
)
1103 if ((fp
= smime_sign_cert(to
, cc
, 0)) != NULL
) {
1104 pkey
= PEM_read_PrivateKey(fp
, NULL
, &ssl_password_cb
, NULL
);
1106 ssl_gen_err(tr(551, "Error reading private key"));
1112 if ((cert
= PEM_read_X509(fp
, NULL
, &ssl_password_cb
, NULL
)) == NULL
) {
1113 ssl_gen_err(tr(552, "Error reading decryption certificate"));
1115 EVP_PKEY_free(pkey
);
1121 if ((op
= Ftmp(NULL
, "smimedec", OF_RDWR
| OF_UNLINK
| OF_REGISTER
, 0600)) ==
1127 if (smime_split(yp
, &hp
, &bp
, size
, 1) == STOP
)
1130 if ((ob
= BIO_new_fp(op
, BIO_NOCLOSE
)) == NULL
||
1131 (bb
= BIO_new_fp(bp
, BIO_NOCLOSE
)) == NULL
) {
1132 ssl_gen_err(tr(553, "Error creating BIO decryption objects"));
1135 if ((pkcs7
= SMIME_read_PKCS7(bb
, &pb
)) == NULL
) {
1136 ssl_gen_err(tr(554, "Error reading PKCS#7 object"));
1143 EVP_PKEY_free(pkey
);
1147 if (PKCS7_type_is_signed(pkcs7
)) {
1149 setinput(&mb
, m
, NEED_BODY
);
1150 rv
= (struct message
*)-1;
1153 if (PKCS7_verify(pkcs7
, NULL
, NULL
, NULL
, ob
,
1154 PKCS7_NOVERIFY
| PKCS7_NOSIGS
) != 1)
1156 fseek(hp
, 0L, SEEK_END
);
1157 fprintf(hp
, "X-Encryption-Cipher: none\n");
1160 } else if (pkey
== NULL
) {
1161 fprintf(stderr
, tr(555, "No appropriate private key found.\n"));
1163 } else if (cert
== NULL
) {
1164 fprintf(stderr
, tr(556, "No appropriate certificate found.\n"));
1166 } else if (PKCS7_decrypt(pkcs7
, pkey
, cert
, ob
, 0) != 1) {
1168 ssl_gen_err(tr(557, "Error decrypting PKCS#7 object"));
1178 EVP_PKEY_free(pkey
);
1186 EVP_PKEY_free(pkey
);
1190 rv
= smime_decrypt_assemble(m
, hp
, op
);
1197 smime_certsave(struct message
*m
, int n
, FILE *op
)
1200 char *to
, *cc
, *cnttype
;
1206 _STACKOF(X509
) *certs
, *chain
= NULL
;
1208 enum okay rv
= STOP
;
1213 to
= hfield1("to", m
);
1214 cc
= hfield1("cc", m
);
1215 cnttype
= hfield1("content-type", m
);
1216 if ((ip
= setinput(&mb
, m
, NEED_BODY
)) == NULL
)
1218 if (cnttype
&& !strncmp(cnttype
, "application/x-pkcs7-mime", 24)) {
1219 if ((x
= smime_decrypt(m
, to
, cc
, 1)) == NULL
)
1221 if (x
!= (struct message
*)-1) {
1228 if ((fp
= Ftmp(NULL
, "smimecert", OF_RDWR
| OF_UNLINK
| OF_REGISTER
, 0600))
1234 while (size
-- > 0) {
1241 if ((fb
= BIO_new_fp(fp
, BIO_NOCLOSE
)) == NULL
) {
1242 ssl_gen_err("Error creating BIO object for message %d", n
);
1247 if ((pkcs7
= SMIME_read_PKCS7(fb
, &pb
)) == NULL
) {
1248 ssl_gen_err(tr(562, "Error reading PKCS#7 object for message %d"), n
);
1255 certs
= PKCS7_get0_signers(pkcs7
, chain
, 0);
1256 if (certs
== NULL
) {
1257 fprintf(stderr
, tr(563, "No certificates found in message %d\n"), n
);
1261 for (i
= 0; i
< sk_X509_num(certs
); ++i
) {
1262 cert
= sk_X509_value(certs
, i
);
1263 if (X509_print_fp(op
, cert
) == 0 || PEM_write_X509(op
, cert
) == 0) {
1264 ssl_gen_err(tr(564, "Error writing certificate %d from message %d"),
1274 #endif /* HAVE_OPENSSL */
1276 /* vim:set fenc=utf-8:s-it-mode */