1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2013 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.
70 static sigjmp_buf ssljmp
;
72 static int initialized
;
74 static int message_number
;
75 static int verify_error_found
;
77 static void sslcatch(int s
);
78 static int ssl_rand_init(void);
79 static void ssl_init(void);
80 static int ssl_verify_cb(int success
, X509_STORE_CTX
*store
);
81 static const SSL_METHOD
*ssl_select_method(const char *uhp
);
82 static void ssl_load_verifications(struct sock
*sp
);
83 static void ssl_certificate(struct sock
*sp
, const char *uhp
);
84 static enum okay
ssl_check_host(const char *server
, struct sock
*sp
);
86 static int smime_verify(struct message
*m
, int n
, STACK_OF(X509
) *chain
,
89 static int smime_verify(struct message
*m
, int n
, STACK
*chain
,
92 static EVP_CIPHER
*smime_cipher(const char *name
);
93 static int ssl_password_cb(char *buf
, int size
, int rwflag
, void *userdata
);
94 static FILE *smime_sign_cert(const char *xname
, const char *xname2
,
96 static char *smime_sign_include_certs(char const *name
);
98 static int smime_sign_include_chain_creat(STACK_OF(X509
) **chain
, char *cfiles
);
100 static int smime_sign_include_chain_creat(STACK
**chain
, char *cfiles
);
102 #if defined (X509_V_FLAG_CRL_CHECK) && defined (X509_V_FLAG_CRL_CHECK_ALL)
103 static enum okay
load_crl1(X509_STORE
*store
, const char *name
);
105 static enum okay
load_crls(X509_STORE
*store
, const char *vfile
,
111 termios_state_reset();
112 siglongjmp(ssljmp
, s
);
121 if ((cp
= value("ssl-rand-egd")) != NULL
) {
122 if ((x
= file_expand(cp
)) == NULL
|| RAND_egd(cp
= x
) == -1)
123 fprintf(stderr
, tr(245,
124 "entropy daemon at \"%s\" not available\n"),
128 } else if ((cp
= value("ssl-rand-file")) != NULL
) {
129 if ((x
= file_expand(cp
)) == NULL
||
130 RAND_load_file(cp
= x
, 1024) == -1)
131 fprintf(stderr
, tr(246,
132 "entropy file at \"%s\" not available\n"), cp
);
136 if (stat(cp
, &st
) == 0 && S_ISREG(st
.st_mode
) &&
137 access(cp
, W_OK
) == 0) {
138 if (RAND_write_file(cp
) == -1) {
139 fprintf(stderr
, tr(247,
140 "writing entropy data to \"%s\" failed\n"), cp
);
152 if (initialized
== 0) {
157 rand_init
= ssl_rand_init();
161 ssl_verify_cb(int success
, X509_STORE_CTX
*store
)
165 X509
*cert
= X509_STORE_CTX_get_current_cert(store
);
166 int depth
= X509_STORE_CTX_get_error_depth(store
);
167 int err
= X509_STORE_CTX_get_error(store
);
169 verify_error_found
= 1;
171 fprintf(stderr
, "Message %d: ", message_number
);
172 fprintf(stderr
, tr(229,
173 "Error with certificate at depth: %i\n"), depth
);
174 X509_NAME_oneline(X509_get_issuer_name(cert
), data
,
176 fprintf(stderr
, tr(230, " issuer = %s\n"), data
);
177 X509_NAME_oneline(X509_get_subject_name(cert
), data
,
179 fprintf(stderr
, tr(231, " subject = %s\n"),
181 fprintf(stderr
, tr(232, " err %i: %s\n"),
182 err
, X509_verify_cert_error_string(err
));
183 if (ssl_vrfy_decide() != OKAY
)
189 static const SSL_METHOD
*
190 ssl_select_method(const char *uhp
)
192 SSL_METHOD
const *method
= NULL
;
195 cp
= ssl_method_string(uhp
);
197 #ifndef OPENSSL_NO_SSL2
198 if (strcmp(cp
, "ssl2") == 0)
199 method
= SSLv2_client_method();
202 #ifndef OPENSSL_NO_SSL3
203 if (strcmp(cp
, "ssl3") == 0)
204 method
= SSLv3_client_method();
207 #ifndef OPENSSL_NO_TLS1
208 if (strcmp(cp
, "tls1") == 0)
209 method
= TLSv1_client_method();
211 # ifdef TLS1_1_VERSION
212 if (strcmp(cp
, "tls1.1") == 0)
213 method
= TLSv1_1_client_method();
216 # ifdef TLS1_2_VERSION
217 if (strcmp(cp
, "tls1.2") == 0)
218 method
= TLSv1_2_client_method();
222 fprintf(stderr
, tr(244, "Invalid SSL method \"%s\"\n"),
227 method
= SSLv23_client_method();
232 ssl_load_verifications(struct sock
*sp
)
234 char *ca_dir
, *ca_file
;
237 if (ssl_vrfy_level
== VRFY_IGNORE
)
239 if ((ca_dir
= value("ssl-ca-dir")) != NULL
)
240 ca_dir
= file_expand(ca_dir
);
241 if ((ca_file
= value("ssl-ca-file")) != NULL
)
242 ca_file
= file_expand(ca_file
);
243 if (ca_dir
!= NULL
|| ca_file
!= NULL
) {
244 if (SSL_CTX_load_verify_locations(sp
->s_ctx
,
245 ca_file
, ca_dir
) != 1) {
246 fprintf(stderr
, tr(233, "Error loading "));
248 fputs(ca_dir
, stderr
);
250 fputs(tr(234, " or "), stderr
);
253 fputs(ca_file
, stderr
);
257 if (value("ssl-no-default-ca") == NULL
) {
258 if (SSL_CTX_set_default_verify_paths(sp
->s_ctx
) != 1)
259 fprintf(stderr
, tr(243,
260 "Error loading default CA locations\n"));
262 verify_error_found
= 0;
264 SSL_CTX_set_verify(sp
->s_ctx
, SSL_VERIFY_PEER
, ssl_verify_cb
);
265 store
= SSL_CTX_get_cert_store(sp
->s_ctx
);
266 load_crls(store
, "ssl-crl-file", "ssl-crl-dir");
270 ssl_certificate(struct sock
*sp
, const char *uhp
)
273 char *certvar
, *keyvar
, *cert
, *key
, *x
;
276 certvar
= ac_alloc(i
+ 9 + 1);
277 memcpy(certvar
, "ssl-cert-", 9);
278 memcpy(certvar
+ 9, uhp
, i
+ 1);
279 if ((cert
= value(certvar
)) != NULL
||
280 (cert
= value("ssl-cert")) != NULL
) {
282 if ((cert
= file_expand(cert
)) == NULL
) {
285 } else if (SSL_CTX_use_certificate_chain_file(sp
->s_ctx
, cert
)
287 keyvar
= ac_alloc(strlen(uhp
) + 9);
288 memcpy(keyvar
, "ssl-key-", 8);
289 memcpy(keyvar
+ 8, uhp
, i
+ 1);
290 if ((key
= value(keyvar
)) == NULL
&&
291 (key
= value("ssl-key")) == NULL
)
293 else if ((x
= key
, key
= file_expand(key
)) == NULL
) {
297 if (SSL_CTX_use_PrivateKey_file(sp
->s_ctx
, key
,
298 SSL_FILETYPE_PEM
) != 1)
299 jbkey
: fprintf(stderr
, tr(238,
300 "cannot load private key from file "
304 jbcert
: fprintf(stderr
, tr(239,
305 "cannot load certificate from file %s\n"),
312 ssl_check_host(const char *server
, struct sock
*sp
)
318 STACK_OF(GENERAL_NAME
) *gens
;
320 /*GENERAL_NAMES*/STACK
*gens
;
325 if ((cert
= SSL_get_peer_certificate(sp
->s_ssl
)) == NULL
) {
326 fprintf(stderr
, tr(248, "no certificate from \"%s\"\n"),
330 gens
= X509_get_ext_d2i(cert
, NID_subject_alt_name
, NULL
, NULL
);
332 for (i
= 0; i
< sk_GENERAL_NAME_num(gens
); i
++) {
333 gen
= sk_GENERAL_NAME_value(gens
, i
);
334 if (gen
->type
== GEN_DNS
) {
335 if (options
& OPT_VERBOSE
)
337 "Comparing DNS name: \"%s\"\n",
339 if (rfc2595_hostname_match(server
,
340 (char *)gen
->d
.ia5
->data
)
346 if ((subj
= X509_get_subject_name(cert
)) != NULL
&&
347 X509_NAME_get_text_by_NID(subj
, NID_commonName
,
348 data
, sizeof data
) > 0) {
349 data
[sizeof data
- 1] = 0;
350 if (options
& OPT_VERBOSE
)
351 fprintf(stderr
, "Comparing common name: \"%s\"\n",
353 if (rfc2595_hostname_match(server
, data
) == OKAY
)
358 found
: X509_free(cert
);
363 ssl_open(const char *server
, struct sock
*sp
, const char *uhp
)
369 ssl_set_vrfy_level(uhp
);
371 SSL_CTX_new(UNCONST(ssl_select_method(uhp
))))
373 ssl_gen_err(tr(261, "SSL_CTX_new() failed"));
376 #ifdef SSL_MODE_AUTO_RETRY
377 /* available with OpenSSL 0.9.6 or later */
378 SSL_CTX_set_mode(sp
->s_ctx
, SSL_MODE_AUTO_RETRY
);
379 #endif /* SSL_MODE_AUTO_RETRY */
381 if (value("ssl-v2-allow") == NULL
)
382 opts
|= SSL_OP_NO_SSLv2
;
383 SSL_CTX_set_options(sp
->s_ctx
, opts
);
384 ssl_load_verifications(sp
);
385 ssl_certificate(sp
, uhp
);
386 if ((cp
= value("ssl-cipher-list")) != NULL
) {
387 if (SSL_CTX_set_cipher_list(sp
->s_ctx
, cp
) != 1)
388 fprintf(stderr
, tr(240, "invalid ciphers: %s\n"), cp
);
390 if ((sp
->s_ssl
= SSL_new(sp
->s_ctx
)) == NULL
) {
391 ssl_gen_err(tr(262, "SSL_new() failed"));
394 SSL_set_fd(sp
->s_ssl
, sp
->s_fd
);
395 if (SSL_connect(sp
->s_ssl
) < 0) {
396 ssl_gen_err(tr(263, "could not initiate SSL/TLS connection"));
399 if (ssl_vrfy_level
!= VRFY_IGNORE
) {
400 if (ssl_check_host(server
, sp
) != OKAY
) {
401 fprintf(stderr
, tr(249,
402 "host certificate does not match \"%s\"\n"),
404 if (ssl_vrfy_decide() != OKAY
)
413 ssl_gen_err(const char *fmt
, ...)
418 vfprintf(stderr
, fmt
, ap
);
420 SSL_load_error_strings();
421 fprintf(stderr
, ": %s\n",
422 (ERR_error_string(ERR_get_error(), NULL
)));
426 smime_sign(FILE *ip
, struct header
*headp
)
428 FILE *sp
, *fp
, *bp
, *hp
;
433 STACK_OF(X509
) *chain
= NULL
;
442 if ((addr
= myorigin(headp
)) == NULL
) {
443 fprintf(stderr
, "No \"from\" address for signing specified\n");
446 if ((fp
= smime_sign_cert(addr
, NULL
, 1)) == NULL
)
448 if ((pkey
= PEM_read_PrivateKey(fp
, NULL
, ssl_password_cb
, NULL
))
450 ssl_gen_err("Error reading private key from");
455 if ((cert
= PEM_read_X509(fp
, NULL
, ssl_password_cb
, NULL
)) == NULL
) {
456 ssl_gen_err("Error reading signer certificate from");
462 if ((cp
= smime_sign_include_certs(addr
)) != NULL
&&
463 !smime_sign_include_chain_creat(&chain
, cp
)) {
468 if ((sp
= Ftemp(&cp
, "Rs", "w+", 0600, 1)) == NULL
) {
471 sk_X509_pop_free(chain
, X509_free
);
479 if (smime_split(ip
, &hp
, &bp
, -1, 0) == STOP
) {
482 sk_X509_pop_free(chain
, X509_free
);
487 if ((bb
= BIO_new_fp(bp
, BIO_NOCLOSE
)) == NULL
||
488 (sb
= BIO_new_fp(sp
, BIO_NOCLOSE
)) == NULL
) {
489 ssl_gen_err("Error creating BIO signing objects");
492 sk_X509_pop_free(chain
, X509_free
);
497 if ((pkcs7
= PKCS7_sign(cert
, pkey
, chain
, bb
,
498 PKCS7_DETACHED
)) == NULL
) {
499 ssl_gen_err("Error creating the PKCS#7 signing object");
504 sk_X509_pop_free(chain
, X509_free
);
509 if (PEM_write_bio_PKCS7(sb
, pkcs7
) == 0) {
510 ssl_gen_err("Error writing signed S/MIME data");
515 sk_X509_pop_free(chain
, X509_free
);
523 sk_X509_pop_free(chain
, X509_free
);
529 return smime_sign_assemble(hp
, bp
, sp
);
534 smime_verify(struct message
*m
, int n
, STACK_OF(X509
) *chain
, X509_STORE
*store
)
536 smime_verify(struct message
*m
, int n
, STACK
*chain
, X509_STORE
*store
)
540 char *cp
, *sender
, *to
, *cc
, *cnttype
;
547 STACK_OF(X509
) *certs
;
548 STACK_OF(GENERAL_NAME
) *gens
;
557 verify_error_found
= 0;
559 loop
: sender
= getsender(m
);
560 to
= hfield1("to", m
);
561 cc
= hfield1("cc", m
);
562 cnttype
= hfield1("content-type", m
);
563 if ((ip
= setinput(&mb
, m
, NEED_BODY
)) == NULL
)
565 if (cnttype
&& strncmp(cnttype
, "application/x-pkcs7-mime", 24) == 0) {
566 if ((x
= smime_decrypt(m
, to
, cc
, 1)) == NULL
)
568 if (x
!= (struct message
*)-1) {
574 if ((fp
= Ftemp(&cp
, "Rv", "w+", 0600, 1)) == NULL
) {
586 if ((fb
= BIO_new_fp(fp
, BIO_NOCLOSE
)) == NULL
) {
587 ssl_gen_err("Error creating BIO verification object "
588 "for message %d", n
);
592 if ((pkcs7
= SMIME_read_PKCS7(fb
, &pb
)) == NULL
) {
593 ssl_gen_err("Error reading PKCS#7 object for message %d", n
);
598 if (PKCS7_verify(pkcs7
, chain
, store
, pb
, NULL
, 0) != 1) {
599 ssl_gen_err("Error verifying message %d", n
);
606 if (sender
== NULL
) {
608 "Warning: Message %d has no sender.\n", n
);
611 certs
= PKCS7_get0_signers(pkcs7
, chain
, 0);
613 fprintf(stderr
, "No certificates found in message %d.\n", n
);
616 for (i
= 0; i
< sk_X509_num(certs
); i
++) {
617 cert
= sk_X509_value(certs
, i
);
618 gens
= X509_get_ext_d2i(cert
, NID_subject_alt_name
, NULL
, NULL
);
620 for (j
= 0; j
< sk_GENERAL_NAME_num(gens
); j
++) {
621 gen
= sk_GENERAL_NAME_value(gens
, j
);
622 if (gen
->type
== GEN_EMAIL
) {
623 if (options
& OPT_VERBOSE
)
628 if (!asccasecmp((char *)
635 if ((subj
= X509_get_subject_name(cert
)) != NULL
&&
636 X509_NAME_get_text_by_NID(subj
,
637 NID_pkcs9_emailAddress
,
638 data
, sizeof data
) > 0) {
639 data
[sizeof data
- 1] = 0;
640 if (options
& OPT_VERBOSE
)
641 fprintf(stderr
, "Comparing address: \"%s\"\n",
643 if (asccasecmp(data
, sender
) == 0)
647 fprintf(stderr
, "Message %d: certificate does not match <%s>\n",
650 found
: if (verify_error_found
== 0)
651 printf("Message %d was verified successfully.\n", n
);
652 return verify_error_found
;
658 int *msgvec
= vp
, *ip
;
661 STACK_OF(X509
) *chain
= NULL
;
666 char *ca_dir
, *ca_file
;
669 ssl_vrfy_level
= VRFY_STRICT
;
670 if ((store
= X509_STORE_new()) == NULL
) {
671 ssl_gen_err("Error creating X509 store");
674 X509_STORE_set_verify_cb_func(store
, ssl_verify_cb
);
675 if ((ca_dir
= value("smime-ca-dir")) != NULL
)
676 ca_dir
= file_expand(ca_dir
);
677 if ((ca_file
= value("smime-ca-file")) != NULL
)
678 ca_file
= file_expand(ca_file
);
679 if (ca_dir
!= NULL
|| ca_file
!= NULL
) {
680 if (X509_STORE_load_locations(store
, ca_file
, ca_dir
) != 1) {
681 ssl_gen_err("Error loading %s",
682 ca_file
? ca_file
: ca_dir
);
686 if (value("smime-no-default-ca") == NULL
) {
687 if (X509_STORE_set_default_paths(store
) != 1) {
688 ssl_gen_err("Error loading default CA locations");
692 if (load_crls(store
, "smime-crl-file", "smime-crl-dir") != OKAY
)
694 for (ip
= msgvec
; *ip
; ip
++) {
695 setdot(&message
[*ip
-1]);
696 ec
|= smime_verify(&message
[*ip
-1], *ip
, chain
, store
);
702 smime_cipher(const char *name
)
704 const EVP_CIPHER
*cipher
;
708 vn
= ac_alloc(vs
= strlen(name
) + 30);
709 snprintf(vn
, vs
, "smime-cipher-%s", name
);
710 if ((cp
= value(vn
)) != NULL
) {
711 if (strcmp(cp
, "rc2-40") == 0)
712 cipher
= EVP_rc2_40_cbc();
713 else if (strcmp(cp
, "rc2-64") == 0)
714 cipher
= EVP_rc2_64_cbc();
715 else if (strcmp(cp
, "des") == 0)
716 cipher
= EVP_des_cbc();
717 else if (strcmp(cp
, "des-ede3") == 0)
718 cipher
= EVP_des_ede3_cbc();
720 fprintf(stderr
, "Invalid cipher \"%s\".\n", cp
);
724 cipher
= EVP_des_ede3_cbc();
726 return UNCONST(cipher
);
730 smime_encrypt(FILE *ip
, const char *xcertfile
, const char *to
)
732 char *certfile
= UNCONST(xcertfile
), *cp
;
733 FILE *yp
, *fp
, *bp
, *hp
;
738 STACK_OF(X509
) *certs
;
744 if ((certfile
= file_expand(certfile
)) == NULL
)
748 if ((cipher
= smime_cipher(to
)) == NULL
)
750 if ((fp
= Fopen(certfile
, "r")) == NULL
) {
754 if ((cert
= PEM_read_X509(fp
, NULL
, ssl_password_cb
, NULL
)) == NULL
) {
755 ssl_gen_err("Error reading encryption certificate from \"%s\"",
761 certs
= sk_X509_new_null();
762 sk_X509_push(certs
, cert
);
763 if ((yp
= Ftemp(&cp
, "Ry", "w+", 0600, 1)) == NULL
) {
770 if (smime_split(ip
, &hp
, &bp
, -1, 0) == STOP
) {
774 if ((bb
= BIO_new_fp(bp
, BIO_NOCLOSE
)) == NULL
||
775 (yb
= BIO_new_fp(yp
, BIO_NOCLOSE
)) == NULL
) {
776 ssl_gen_err("Error creating BIO encryption objects");
780 if ((pkcs7
= PKCS7_encrypt(certs
, bb
, cipher
, 0)) == NULL
) {
781 ssl_gen_err("Error creating the PKCS#7 encryption object");
787 if (PEM_write_bio_PKCS7(yb
, pkcs7
) == 0) {
788 ssl_gen_err("Error writing encrypted S/MIME data");
799 return smime_encrypt_assemble(hp
, yp
);
803 smime_decrypt(struct message
*m
, const char *to
, const char *cc
, int signcall
)
805 FILE *fp
, *bp
, *hp
, *op
;
809 EVP_PKEY
*pkey
= NULL
;
811 long size
= m
->m_size
;
814 if ((yp
= setinput(&mb
, m
, NEED_BODY
)) == NULL
)
817 if ((fp
= smime_sign_cert(to
, cc
, 0)) != NULL
) {
818 if ((pkey
= PEM_read_PrivateKey(fp
, NULL
, ssl_password_cb
,
820 ssl_gen_err("Error reading private key");
825 if ((cert
= PEM_read_X509(fp
, NULL
, ssl_password_cb
,
827 ssl_gen_err("Error reading decryption certificate");
834 if ((op
= Ftemp(&cp
, "Rp", "w+", 0600, 1)) == NULL
) {
844 if (smime_split(yp
, &hp
, &bp
, size
, 1) == STOP
) {
852 if ((ob
= BIO_new_fp(op
, BIO_NOCLOSE
)) == NULL
||
853 (bb
= BIO_new_fp(bp
, BIO_NOCLOSE
)) == NULL
) {
854 ssl_gen_err("Error creating BIO decryption objects");
862 if ((pkcs7
= SMIME_read_PKCS7(bb
, &pb
)) == NULL
) {
863 ssl_gen_err("Error reading PKCS#7 object");
871 if (PKCS7_type_is_signed(pkcs7
)) {
882 setinput(&mb
, m
, NEED_BODY
);
883 return (struct message
*)-1;
885 if (PKCS7_verify(pkcs7
, NULL
, NULL
, NULL
, ob
,
886 PKCS7_NOVERIFY
|PKCS7_NOSIGS
) != 1)
888 fseek(hp
, 0L, SEEK_END
);
889 fprintf(hp
, "X-Encryption-Cipher: none\n");
892 } else if (pkey
== NULL
) {
893 fprintf(stderr
, "No appropriate private key found.\n");
895 } else if (cert
== NULL
) {
896 fprintf(stderr
, "No appropriate certificate found.\n");
898 } else if (PKCS7_decrypt(pkcs7
, pkey
, cert
, ob
, 0) != 1) {
899 err
: ssl_gen_err("Error decrypting PKCS#7 object");
920 return smime_decrypt_assemble(m
, hp
, op
);
925 ssl_password_cb(char *buf
, int size
, int rwflag
, void *userdata
)
927 sighandler_type
volatile saveint
;
933 saveint
= safe_signal(SIGINT
, SIG_IGN
);
934 if (sigsetjmp(ssljmp
, 1) == 0) {
935 if (saveint
!= SIG_IGN
)
936 safe_signal(SIGINT
, sslcatch
);
937 pass
= getpassword("PEM pass phrase:");
939 safe_signal(SIGINT
, saveint
);
943 if (UICMP(z
, len
, >, size
))
945 memcpy(buf
, pass
, len
);
950 smime_sign_cert(const char *xname
, const char *xname2
, bool_t dowarn
)
956 const char *name
= xname
, *name2
= xname2
;
959 np
= lextract(name
, GTO
|GSKIN
);
962 * This needs to be more intelligent since it will
963 * currently take the first name for which a private
964 * key is available regardless of whether it is the
965 * right one for the message.
967 vn
= ac_alloc(vs
= strlen(np
->n_name
) + 30);
968 snprintf(vn
, vs
, "smime-sign-cert-%s", np
->n_name
);
981 if ((cp
= value("smime-sign-cert")) != NULL
)
984 fprintf(stderr
, "Could not find a certificate for %s", xname
);
986 fprintf(stderr
, "or %s", xname2
);
991 if ((cp
= file_expand(cp
)) == NULL
)
993 if ((fp
= Fopen(cp
, "r")) == NULL
) {
1001 smime_sign_include_certs(char const *name
)
1004 /* See comments in smime_sign_cert() for algorithm pitfalls */
1006 struct name
*np
= lextract(name
, GTO
|GSKIN
);
1009 char *vn
= ac_alloc(vs
= strlen(np
->n_name
) + 30);
1010 snprintf(vn
, vs
, "smime-sign-include-certs-%s",
1019 return value("smime-sign-include-certs");
1023 smime_sign_include_chain_creat(
1024 #ifdef HAVE_STACK_OF
1025 STACK_OF(X509
) **chain
,
1031 *chain
= sk_X509_new_null();
1036 char *x
, *ncf
= strchr(cfiles
, ',');
1039 /* This fails for '=,file' constructs, but those are sick */
1043 if ((x
= file_expand(cfiles
)) == NULL
||
1044 (fp
= Fopen(cfiles
= x
, "r")) == NULL
) {
1048 if ((tmp
= PEM_read_X509(fp
, NULL
, ssl_password_cb
, NULL
)
1050 ssl_gen_err("Error reading certificate from \"%s\"",
1055 sk_X509_push(*chain
, tmp
);
1063 if (sk_X509_num(*chain
) == 0) {
1064 fprintf(stderr
, "smime-sign-include-certs defined but empty\n");
1068 jleave
: return (*chain
!= NULL
);
1070 jerr
: sk_X509_pop_free(*chain
, X509_free
);
1076 smime_certsave(struct message
*m
, int n
, FILE *op
)
1079 char *cp
, *to
, *cc
, *cnttype
;
1085 #ifdef HAVE_STACK_OF
1086 STACK_OF(X509
) *certs
;
1087 STACK_OF(X509
) *chain
= NULL
;
1090 STACK
*chain
= NULL
;
1093 enum okay ok
= OKAY
;
1096 loop
: to
= hfield1("to", m
);
1097 cc
= hfield1("cc", m
);
1098 cnttype
= hfield1("content-type", m
);
1099 if ((ip
= setinput(&mb
, m
, NEED_BODY
)) == NULL
)
1101 if (cnttype
&& strncmp(cnttype
, "application/x-pkcs7-mime", 24) == 0) {
1102 if ((x
= smime_decrypt(m
, to
, cc
, 1)) == NULL
)
1104 if (x
!= (struct message
*)-1) {
1110 if ((fp
= Ftemp(&cp
, "Rv", "w+", 0600, 1)) == NULL
) {
1116 while (size
-- > 0) {
1122 if ((fb
= BIO_new_fp(fp
, BIO_NOCLOSE
)) == NULL
) {
1123 ssl_gen_err("Error creating BIO object for message %d", n
);
1127 if ((pkcs7
= SMIME_read_PKCS7(fb
, &pb
)) == NULL
) {
1128 ssl_gen_err("Error reading PKCS#7 object for message %d", n
);
1135 certs
= PKCS7_get0_signers(pkcs7
, chain
, 0);
1136 if (certs
== NULL
) {
1137 fprintf(stderr
, "No certificates found in message %d.\n", n
);
1140 for (i
= 0; i
< sk_X509_num(certs
); i
++) {
1141 cert
= sk_X509_value(certs
, i
);
1142 if (X509_print_fp(op
, cert
) == 0 ||
1143 PEM_write_X509(op
, cert
) == 0) {
1144 ssl_gen_err("Error writing certificate %d from "
1145 "message %d", i
, n
);
1152 #if defined (X509_V_FLAG_CRL_CHECK) && defined (X509_V_FLAG_CRL_CHECK_ALL)
1154 load_crl1(X509_STORE
*store
, const char *name
)
1156 X509_LOOKUP
*lookup
;
1158 if (options
& OPT_VERBOSE
)
1159 printf("Loading CRL from \"%s\".\n", name
);
1160 if ((lookup
= X509_STORE_add_lookup(store
,
1161 X509_LOOKUP_file())) == NULL
) {
1162 ssl_gen_err("Error creating X509 lookup object");
1165 if (X509_load_crl_file(lookup
, name
, X509_FILETYPE_PEM
) != 1) {
1166 ssl_gen_err("Error loading CRL from \"%s\"", name
);
1171 #endif /* new OpenSSL */
1174 load_crls(X509_STORE
*store
, const char *vfile
, const char *vdir
)
1176 char *crl_file
, *crl_dir
;
1177 #if defined (X509_V_FLAG_CRL_CHECK) && defined (X509_V_FLAG_CRL_CHECK_ALL)
1182 #endif /* new OpenSSL */
1184 if ((crl_file
= value(vfile
)) != NULL
) {
1185 #if defined (X509_V_FLAG_CRL_CHECK) && defined (X509_V_FLAG_CRL_CHECK_ALL)
1186 if ((crl_file
= file_expand(crl_file
)) == NULL
||
1187 load_crl1(store
, crl_file
) != OKAY
)
1189 #else /* old OpenSSL */
1191 "This OpenSSL version is too old to use CRLs.\n");
1193 #endif /* old OpenSSL */
1195 if ((crl_dir
= value(vdir
)) != NULL
) {
1196 #if defined (X509_V_FLAG_CRL_CHECK) && defined (X509_V_FLAG_CRL_CHECK_ALL)
1198 if ((x
= file_expand(crl_dir
)) == NULL
||
1199 (dirp
= opendir(crl_dir
= x
)) == NULL
) {
1203 ds
= strlen(crl_dir
);
1204 fn
= smalloc(fs
= ds
+ 20);
1205 memcpy(fn
, crl_dir
, ds
);
1207 while ((dp
= readdir(dirp
)) != NULL
) {
1208 if (dp
->d_name
[0] == '.' &&
1209 (dp
->d_name
[1] == '\0' ||
1210 (dp
->d_name
[1] == '.' &&
1211 dp
->d_name
[2] == '\0')))
1213 if (dp
->d_name
[0] == '.')
1215 if (ds
+ (es
= strlen(dp
->d_name
)) + 2 < fs
)
1216 fn
= srealloc(fn
, fs
= ds
+ es
+ 20);
1217 memcpy(fn
+ ds
+ 1, dp
->d_name
, es
+ 1);
1218 if (load_crl1(store
, fn
) != OKAY
) {
1226 #else /* old OpenSSL */
1228 "This OpenSSL version is too old to use CRLs.\n");
1230 #endif /* old OpenSSL */
1232 #if defined (X509_V_FLAG_CRL_CHECK) && defined (X509_V_FLAG_CRL_CHECK_ALL)
1233 if (crl_file
|| crl_dir
)
1234 X509_STORE_set_flags(store
, X509_V_FLAG_CRL_CHECK
|
1235 X509_V_FLAG_CRL_CHECK_ALL
);
1236 #endif /* old OpenSSL */
1239 #endif /* HAVE_OPENSSL */