2 * Heirloom mailx - a mail user agent derived from Berkeley Mail.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 Steffen "Daode" Nurpmeso.
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
42 static char sccsid
[] = "@(#)openssl.c 1.26 (gritter) 5/26/09";
57 static struct termios otio
;
58 static sigjmp_buf ssljmp
;
60 #include <openssl/crypto.h>
61 #include <openssl/ssl.h>
62 #include <openssl/err.h>
63 #include <openssl/x509v3.h>
64 #include <openssl/x509.h>
65 #include <openssl/pem.h>
66 #include <openssl/rand.h>
74 #include <sys/socket.h>
76 #include <netinet/in.h>
77 #ifdef HAVE_ARPA_INET_H
78 #include <arpa/inet.h>
79 #endif /* HAVE_ARPA_INET_H */
86 * Mail -- a mail program
92 * OpenSSL client implementation according to: John Viega, Matt Messier,
93 * Pravir Chandra: Network Security with OpenSSL. Sebastopol, CA 2002.
96 static int initialized
;
98 static int message_number
;
99 static int verify_error_found
;
101 static void sslcatch(int s
);
102 static int ssl_rand_init(void);
103 static void ssl_init(void);
104 static int ssl_verify_cb(int success
, X509_STORE_CTX
*store
);
105 static const SSL_METHOD
*ssl_select_method(const char *uhp
);
106 static void ssl_load_verifications(struct sock
*sp
);
107 static void ssl_certificate(struct sock
*sp
, const char *uhp
);
108 static enum okay
ssl_check_host(const char *server
, struct sock
*sp
);
110 static int smime_verify(struct message
*m
, int n
, STACK_OF(X509
) *chain
,
113 static int smime_verify(struct message
*m
, int n
, STACK
*chain
,
116 static EVP_CIPHER
*smime_cipher(const char *name
);
117 static int ssl_password_cb(char *buf
, int size
, int rwflag
, void *userdata
);
118 static FILE *smime_sign_cert(const char *xname
, const char *xname2
, int warn
);
119 static char *smime_sign_include_certs(char *name
);
121 static int smime_sign_include_chain_creat(STACK_OF(X509
) **chain
, char *cfiles
);
123 static int smime_sign_include_chain_creat(STACK
**chain
, char *cfiles
);
125 #if defined (X509_V_FLAG_CRL_CHECK) && defined (X509_V_FLAG_CRL_CHECK_ALL)
126 static enum okay
load_crl1(X509_STORE
*store
, const char *name
);
128 static enum okay
load_crls(X509_STORE
*store
, const char *vfile
,
135 tcsetattr(0, TCSADRAIN
, &otio
);
136 siglongjmp(ssljmp
, s
);
145 if ((cp
= value("ssl-rand-egd")) != NULL
) {
147 if (RAND_egd(cp
) == -1) {
148 fprintf(stderr
, catgets(catd
, CATSET
, 245,
149 "entropy daemon at \"%s\" not available\n"),
153 } else if ((cp
= value("ssl-rand-file")) != NULL
) {
155 if (RAND_load_file(cp
, 1024) == -1) {
156 fprintf(stderr
, catgets(catd
, CATSET
, 246,
157 "entropy file at \"%s\" not available\n"), cp
);
161 if (stat(cp
, &st
) == 0 && S_ISREG(st
.st_mode
) &&
162 access(cp
, W_OK
) == 0) {
163 if (RAND_write_file(cp
) == -1) {
164 fprintf(stderr
, catgets(catd
, CATSET
,
166 "writing entropy data to \"%s\" failed\n"), cp
);
178 verbose
= value("verbose") != NULL
;
179 if (initialized
== 0) {
184 rand_init
= ssl_rand_init();
188 ssl_verify_cb(int success
, X509_STORE_CTX
*store
)
192 X509
*cert
= X509_STORE_CTX_get_current_cert(store
);
193 int depth
= X509_STORE_CTX_get_error_depth(store
);
194 int err
= X509_STORE_CTX_get_error(store
);
196 verify_error_found
= 1;
198 fprintf(stderr
, "Message %d: ", message_number
);
199 fprintf(stderr
, catgets(catd
, CATSET
, 229,
200 "Error with certificate at depth: %i\n"),
202 X509_NAME_oneline(X509_get_issuer_name(cert
), data
,
204 fprintf(stderr
, catgets(catd
, CATSET
, 230, " issuer = %s\n"),
206 X509_NAME_oneline(X509_get_subject_name(cert
), data
,
208 fprintf(stderr
, catgets(catd
, CATSET
, 231, " subject = %s\n"),
210 fprintf(stderr
, catgets(catd
, CATSET
, 232, " err %i: %s\n"),
211 err
, X509_verify_cert_error_string(err
));
212 if (ssl_vrfy_decide() != OKAY
)
218 static const SSL_METHOD
*
219 ssl_select_method(const char *uhp
)
221 const SSL_METHOD
*method
;
224 cp
= ssl_method_string(uhp
);
226 #ifndef OPENSSL_NO_SSL2
227 if (equal(cp
, "ssl2"))
228 method
= SSLv2_client_method();
231 if (equal(cp
, "ssl3"))
232 method
= SSLv3_client_method();
233 else if (equal(cp
, "tls1"))
234 method
= TLSv1_client_method();
236 fprintf(stderr
, catgets(catd
, CATSET
, 244,
237 "Invalid SSL method \"%s\"\n"), cp
);
238 method
= SSLv23_client_method();
241 method
= SSLv23_client_method();
246 ssl_load_verifications(struct sock
*sp
)
248 char *ca_dir
, *ca_file
;
251 if (ssl_vrfy_level
== VRFY_IGNORE
)
253 if ((ca_dir
= value("ssl-ca-dir")) != NULL
)
254 ca_dir
= expand(ca_dir
);
255 if ((ca_file
= value("ssl-ca-file")) != NULL
)
256 ca_file
= expand(ca_file
);
257 if (ca_dir
|| ca_file
) {
258 if (SSL_CTX_load_verify_locations(sp
->s_ctx
,
259 ca_file
, ca_dir
) != 1) {
260 fprintf(stderr
, catgets(catd
, CATSET
, 233,
263 fprintf(stderr
, catgets(catd
, CATSET
, 234,
266 fprintf(stderr
, catgets(catd
, CATSET
,
270 fprintf(stderr
, catgets(catd
, CATSET
, 236,
272 fprintf(stderr
, catgets(catd
, CATSET
, 237, "\n"));
275 if (value("ssl-no-default-ca") == NULL
) {
276 if (SSL_CTX_set_default_verify_paths(sp
->s_ctx
) != 1)
277 fprintf(stderr
, catgets(catd
, CATSET
, 243,
278 "Error loading default CA locations\n"));
280 verify_error_found
= 0;
282 SSL_CTX_set_verify(sp
->s_ctx
, SSL_VERIFY_PEER
, ssl_verify_cb
);
283 store
= SSL_CTX_get_cert_store(sp
->s_ctx
);
284 load_crls(store
, "ssl-crl-file", "ssl-crl-dir");
288 ssl_certificate(struct sock
*sp
, const char *uhp
)
290 char *certvar
, *keyvar
, *cert
, *key
;
292 certvar
= ac_alloc(strlen(uhp
) + 10);
293 strcpy(certvar
, "ssl-cert-");
294 strcpy(&certvar
[9], uhp
);
295 if ((cert
= value(certvar
)) != NULL
||
296 (cert
= value("ssl-cert")) != NULL
) {
298 if (SSL_CTX_use_certificate_chain_file(sp
->s_ctx
, cert
) == 1) {
299 keyvar
= ac_alloc(strlen(uhp
) + 9);
300 strcpy(keyvar
, "ssl-key-");
301 if ((key
= value(keyvar
)) == NULL
&&
302 (key
= value("ssl-key")) == NULL
)
306 if (SSL_CTX_use_PrivateKey_file(sp
->s_ctx
, key
,
307 SSL_FILETYPE_PEM
) != 1)
308 fprintf(stderr
, catgets(catd
, CATSET
, 238,
309 "cannot load private key from file %s\n"),
313 fprintf(stderr
, catgets(catd
, CATSET
, 239,
314 "cannot load certificate from file %s\n"),
321 ssl_check_host(const char *server
, struct sock
*sp
)
327 STACK_OF(GENERAL_NAME
) *gens
;
329 /*GENERAL_NAMES*/STACK
*gens
;
334 if ((cert
= SSL_get_peer_certificate(sp
->s_ssl
)) == NULL
) {
335 fprintf(stderr
, catgets(catd
, CATSET
, 248,
336 "no certificate from \"%s\"\n"), server
);
339 gens
= X509_get_ext_d2i(cert
, NID_subject_alt_name
, NULL
, NULL
);
341 for (i
= 0; i
< sk_GENERAL_NAME_num(gens
); i
++) {
342 gen
= sk_GENERAL_NAME_value(gens
, i
);
343 if (gen
->type
== GEN_DNS
) {
346 "Comparing DNS name: \"%s\"\n",
348 if (rfc2595_hostname_match(server
,
349 (char *)gen
->d
.ia5
->data
)
355 if ((subj
= X509_get_subject_name(cert
)) != NULL
&&
356 X509_NAME_get_text_by_NID(subj
, NID_commonName
,
357 data
, sizeof data
) > 0) {
358 data
[sizeof data
- 1] = 0;
360 fprintf(stderr
, "Comparing common name: \"%s\"\n",
362 if (rfc2595_hostname_match(server
, data
) == OKAY
)
367 found
: X509_free(cert
);
372 ssl_open(const char *server
, struct sock
*sp
, const char *uhp
)
378 ssl_set_vrfy_level(uhp
);
380 SSL_CTX_new((SSL_METHOD
*)ssl_select_method(uhp
))) == NULL
) {
381 ssl_gen_err(catgets(catd
, CATSET
, 261, "SSL_CTX_new() failed"));
384 #ifdef SSL_MODE_AUTO_RETRY
385 /* available with OpenSSL 0.9.6 or later */
386 SSL_CTX_set_mode(sp
->s_ctx
, SSL_MODE_AUTO_RETRY
);
387 #endif /* SSL_MODE_AUTO_RETRY */
388 options
= SSL_OP_ALL
;
389 if (value("ssl-v2-allow") == NULL
)
390 options
|= SSL_OP_NO_SSLv2
;
391 SSL_CTX_set_options(sp
->s_ctx
, options
);
392 ssl_load_verifications(sp
);
393 ssl_certificate(sp
, uhp
);
394 if ((cp
= value("ssl-cipher-list")) != NULL
) {
395 if (SSL_CTX_set_cipher_list(sp
->s_ctx
, cp
) != 1)
396 fprintf(stderr
, catgets(catd
, CATSET
, 240,
397 "invalid ciphers: %s\n"), cp
);
399 if ((sp
->s_ssl
= SSL_new(sp
->s_ctx
)) == NULL
) {
400 ssl_gen_err(catgets(catd
, CATSET
, 262, "SSL_new() failed"));
403 SSL_set_fd(sp
->s_ssl
, sp
->s_fd
);
404 if (SSL_connect(sp
->s_ssl
) < 0) {
405 ssl_gen_err(catgets(catd
, CATSET
, 263,
406 "could not initiate SSL/TLS connection"));
409 if (ssl_vrfy_level
!= VRFY_IGNORE
) {
410 if (ssl_check_host(server
, sp
) != OKAY
) {
411 fprintf(stderr
, catgets(catd
, CATSET
, 249,
412 "host certificate does not match \"%s\"\n"),
414 if (ssl_vrfy_decide() != OKAY
)
423 ssl_gen_err(const char *fmt
, ...)
428 vfprintf(stderr
, fmt
, ap
);
430 SSL_load_error_strings();
431 fprintf(stderr
, ": %s\n",
432 (ERR_error_string(ERR_get_error(), NULL
)));
436 smime_sign(FILE *ip
, struct header
*headp
)
438 FILE *sp
, *fp
, *bp
, *hp
;
442 STACK_OF(X509
) *chain
= NULL
;
451 if ((addr
= myorigin(headp
)) == NULL
) {
452 fprintf(stderr
, "No \"from\" address for signing specified\n");
455 if ((fp
= smime_sign_cert(addr
, NULL
, 1)) == NULL
)
457 if ((pkey
= PEM_read_PrivateKey(fp
, NULL
, ssl_password_cb
, NULL
))
459 ssl_gen_err("Error reading private key from");
464 if ((cert
= PEM_read_X509(fp
, NULL
, ssl_password_cb
, NULL
)) == NULL
) {
465 ssl_gen_err("Error reading signer certificate from");
471 if ((cp
= smime_sign_include_certs(addr
)) != NULL
&&
472 !smime_sign_include_chain_creat(&chain
, cp
)) {
477 if ((sp
= Ftemp(&cp
, "Rs", "w+", 0600, 1)) == NULL
) {
480 sk_X509_pop_free(chain
, X509_free
);
488 if (smime_split(ip
, &hp
, &bp
, -1, 0) == STOP
) {
491 sk_X509_pop_free(chain
, X509_free
);
496 if ((bb
= BIO_new_fp(bp
, BIO_NOCLOSE
)) == NULL
||
497 (sb
= BIO_new_fp(sp
, BIO_NOCLOSE
)) == NULL
) {
498 ssl_gen_err("Error creating BIO signing objects");
501 sk_X509_pop_free(chain
, X509_free
);
506 if ((pkcs7
= PKCS7_sign(cert
, pkey
, chain
, bb
,
507 PKCS7_DETACHED
)) == NULL
) {
508 ssl_gen_err("Error creating the PKCS#7 signing object");
513 sk_X509_pop_free(chain
, X509_free
);
518 if (PEM_write_bio_PKCS7(sb
, pkcs7
) == 0) {
519 ssl_gen_err("Error writing signed S/MIME data");
524 sk_X509_pop_free(chain
, X509_free
);
532 sk_X509_pop_free(chain
, X509_free
);
538 return smime_sign_assemble(hp
, bp
, sp
);
543 smime_verify(struct message
*m
, int n
, STACK_OF(X509
) *chain
, X509_STORE
*store
)
545 smime_verify(struct message
*m
, int n
, STACK
*chain
, X509_STORE
*store
)
549 char *cp
, *sender
, *to
, *cc
, *cnttype
;
556 STACK_OF(X509
) *certs
;
557 STACK_OF(GENERAL_NAME
) *gens
;
566 verify_error_found
= 0;
568 loop
: sender
= getsender(m
);
569 to
= hfield("to", m
);
570 cc
= hfield("cc", m
);
571 cnttype
= hfield("content-type", m
);
572 if ((ip
= setinput(&mb
, m
, NEED_BODY
)) == NULL
)
574 if (cnttype
&& strncmp(cnttype
, "application/x-pkcs7-mime", 24) == 0) {
575 if ((x
= smime_decrypt(m
, to
, cc
, 1)) == NULL
)
577 if (x
!= (struct message
*)-1) {
583 if ((fp
= Ftemp(&cp
, "Rv", "w+", 0600, 1)) == NULL
) {
595 if ((fb
= BIO_new_fp(fp
, BIO_NOCLOSE
)) == NULL
) {
596 ssl_gen_err("Error creating BIO verification object "
597 "for message %d", n
);
601 if ((pkcs7
= SMIME_read_PKCS7(fb
, &pb
)) == NULL
) {
602 ssl_gen_err("Error reading PKCS#7 object for message %d", n
);
607 if (PKCS7_verify(pkcs7
, chain
, store
, pb
, NULL
, 0) != 1) {
608 ssl_gen_err("Error verifying message %d", n
);
615 if (sender
== NULL
) {
617 "Warning: Message %d has no sender.\n", n
);
620 certs
= PKCS7_get0_signers(pkcs7
, chain
, 0);
622 fprintf(stderr
, "No certificates found in message %d.\n", n
);
625 for (i
= 0; i
< sk_X509_num(certs
); i
++) {
626 cert
= sk_X509_value(certs
, i
);
627 gens
= X509_get_ext_d2i(cert
, NID_subject_alt_name
, NULL
, NULL
);
629 for (j
= 0; j
< sk_GENERAL_NAME_num(gens
); j
++) {
630 gen
= sk_GENERAL_NAME_value(gens
, j
);
631 if (gen
->type
== GEN_EMAIL
) {
637 if (!asccasecmp((char *)
644 if ((subj
= X509_get_subject_name(cert
)) != NULL
&&
645 X509_NAME_get_text_by_NID(subj
,
646 NID_pkcs9_emailAddress
,
647 data
, sizeof data
) > 0) {
648 data
[sizeof data
- 1] = 0;
650 fprintf(stderr
, "Comparing address: \"%s\"\n",
652 if (asccasecmp(data
, sender
) == 0)
656 fprintf(stderr
, "Message %d: certificate does not match <%s>\n",
659 found
: if (verify_error_found
== 0)
660 printf("Message %d was verified successfully.\n", n
);
661 return verify_error_found
;
667 int *msgvec
= vp
, *ip
;
670 STACK_OF(X509
) *chain
= NULL
;
675 char *ca_dir
, *ca_file
;
678 ssl_vrfy_level
= VRFY_STRICT
;
679 if ((store
= X509_STORE_new()) == NULL
) {
680 ssl_gen_err("Error creating X509 store");
683 X509_STORE_set_verify_cb_func(store
, ssl_verify_cb
);
684 if ((ca_dir
= value("smime-ca-dir")) != NULL
)
685 ca_dir
= expand(ca_dir
);
686 if ((ca_file
= value("smime-ca-file")) != NULL
)
687 ca_file
= expand(ca_file
);
688 if (ca_dir
|| ca_file
) {
689 if (X509_STORE_load_locations(store
, ca_file
, ca_dir
) != 1) {
690 ssl_gen_err("Error loading %s",
691 ca_file
? ca_file
: ca_dir
);
695 if (value("smime-no-default-ca") == NULL
) {
696 if (X509_STORE_set_default_paths(store
) != 1) {
697 ssl_gen_err("Error loading default CA locations");
701 if (load_crls(store
, "smime-crl-file", "smime-crl-dir") != OKAY
)
703 for (ip
= msgvec
; *ip
; ip
++) {
704 setdot(&message
[*ip
-1]);
705 ec
|= smime_verify(&message
[*ip
-1], *ip
, chain
, store
);
711 smime_cipher(const char *name
)
713 const EVP_CIPHER
*cipher
;
717 vn
= ac_alloc(vs
= strlen(name
) + 30);
718 snprintf(vn
, vs
, "smime-cipher-%s", name
);
719 if ((cp
= value(vn
)) != NULL
) {
720 if (strcmp(cp
, "rc2-40") == 0)
721 cipher
= EVP_rc2_40_cbc();
722 else if (strcmp(cp
, "rc2-64") == 0)
723 cipher
= EVP_rc2_64_cbc();
724 else if (strcmp(cp
, "des") == 0)
725 cipher
= EVP_des_cbc();
726 else if (strcmp(cp
, "des-ede3") == 0)
727 cipher
= EVP_des_ede3_cbc();
729 fprintf(stderr
, "Invalid cipher \"%s\".\n", cp
);
733 cipher
= EVP_des_ede3_cbc();
735 return (EVP_CIPHER
*)cipher
;
739 smime_encrypt(FILE *ip
, const char *certfile
, const char *to
)
741 FILE *yp
, *fp
, *bp
, *hp
;
747 STACK_OF(X509
) *certs
;
753 certfile
= expand((char *)certfile
);
755 if ((cipher
= smime_cipher(to
)) == NULL
)
757 if ((fp
= Fopen(certfile
, "r")) == NULL
) {
761 if ((cert
= PEM_read_X509(fp
, NULL
, ssl_password_cb
, NULL
)) == NULL
) {
762 ssl_gen_err("Error reading encryption certificate from \"%s\"",
768 certs
= sk_X509_new_null();
769 sk_X509_push(certs
, cert
);
770 if ((yp
= Ftemp(&cp
, "Ry", "w+", 0600, 1)) == NULL
) {
777 if (smime_split(ip
, &hp
, &bp
, -1, 0) == STOP
) {
781 if ((bb
= BIO_new_fp(bp
, BIO_NOCLOSE
)) == NULL
||
782 (yb
= BIO_new_fp(yp
, BIO_NOCLOSE
)) == NULL
) {
783 ssl_gen_err("Error creating BIO encryption objects");
787 if ((pkcs7
= PKCS7_encrypt(certs
, bb
, cipher
, 0)) == NULL
) {
788 ssl_gen_err("Error creating the PKCS#7 encryption object");
794 if (PEM_write_bio_PKCS7(yb
, pkcs7
) == 0) {
795 ssl_gen_err("Error writing encrypted S/MIME data");
806 return smime_encrypt_assemble(hp
, yp
);
810 smime_decrypt(struct message
*m
, const char *to
, const char *cc
, int signcall
)
812 FILE *fp
, *bp
, *hp
, *op
;
816 EVP_PKEY
*pkey
= NULL
;
818 long size
= m
->m_size
;
821 if ((yp
= setinput(&mb
, m
, NEED_BODY
)) == NULL
)
824 if ((fp
= smime_sign_cert(to
, cc
, 0)) != NULL
) {
825 if ((pkey
= PEM_read_PrivateKey(fp
, NULL
, ssl_password_cb
,
827 ssl_gen_err("Error reading private key");
832 if ((cert
= PEM_read_X509(fp
, NULL
, ssl_password_cb
,
834 ssl_gen_err("Error reading decryption certificate");
841 if ((op
= Ftemp(&cp
, "Rp", "w+", 0600, 1)) == NULL
) {
851 if (smime_split(yp
, &hp
, &bp
, size
, 1) == STOP
) {
859 if ((ob
= BIO_new_fp(op
, BIO_NOCLOSE
)) == NULL
||
860 (bb
= BIO_new_fp(bp
, BIO_NOCLOSE
)) == NULL
) {
861 ssl_gen_err("Error creating BIO decryption objects");
869 if ((pkcs7
= SMIME_read_PKCS7(bb
, &pb
)) == NULL
) {
870 ssl_gen_err("Error reading PKCS#7 object");
878 if (PKCS7_type_is_signed(pkcs7
)) {
889 setinput(&mb
, m
, NEED_BODY
);
890 return (struct message
*)-1;
892 if (PKCS7_verify(pkcs7
, NULL
, NULL
, NULL
, ob
,
893 PKCS7_NOVERIFY
|PKCS7_NOSIGS
) != 1)
895 fseek(hp
, 0L, SEEK_END
);
896 fprintf(hp
, "X-Encryption-Cipher: none\n");
899 } else if (pkey
== NULL
) {
900 fprintf(stderr
, "No appropriate private key found.\n");
902 } else if (cert
== NULL
) {
903 fprintf(stderr
, "No appropriate certificate found.\n");
905 } else if (PKCS7_decrypt(pkcs7
, pkey
, cert
, ob
, 0) != 1) {
906 err
: ssl_gen_err("Error decrypting PKCS#7 object");
927 return smime_decrypt_assemble(m
, hp
, op
);
932 ssl_password_cb(char *buf
, int size
, int rwflag
, void *userdata
)
934 sighandler_type saveint
;
940 saveint
= safe_signal(SIGINT
, SIG_IGN
);
941 if (sigsetjmp(ssljmp
, 1) == 0) {
942 if (saveint
!= SIG_IGN
)
943 safe_signal(SIGINT
, sslcatch
);
944 pass
= getpassword(&otio
, &reset_tio
, "PEM pass phrase:");
946 safe_signal(SIGINT
, saveint
);
952 memcpy(buf
, pass
, len
);
957 smime_sign_cert(const char *xname
, const char *xname2
, int warn
)
963 const char *name
= xname
, *name2
= xname2
;
966 np
= sextract(savestr(name
), GTO
|GSKIN
);
969 * This needs to be more intelligent since it will
970 * currently take the first name for which a private
971 * key is available regardless of whether it is the
972 * right one for the message.
974 vn
= ac_alloc(vs
= strlen(np
->n_name
) + 30);
975 snprintf(vn
, vs
, "smime-sign-cert-%s", np
->n_name
);
976 if ((cp
= value(vn
)) != NULL
)
986 if ((cp
= value("smime-sign-cert")) != NULL
)
989 fprintf(stderr
, "Could not find a certificate for %s", xname
);
991 fprintf(stderr
, "or %s", xname2
);
995 open
: cp
= expand(cp
);
996 if ((fp
= Fopen(cp
, "r")) == NULL
) {
1004 smime_sign_include_certs(char *name
)
1006 /* See comments in smime_sign_cert() for algorithm pitfalls */
1008 struct name
*np
= sextract(savestr(name
), GTO
|GSKIN
);
1011 char *vn
= ac_alloc(vs
= strlen(np
->n_name
) + 30);
1012 snprintf(vn
, vs
, "smime-sign-include-certs-%s",
1014 if ((name
= value(vn
)) != NULL
)
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 *exp
, *ncf
= strchr(cfiles
, ',');
1039 /* This fails for '=,file' constructs, but those are sick */
1043 if ((exp
= expand(cfiles
)) != NULL
)
1045 if ((fp
= Fopen(cfiles
, "r")) == NULL
) {
1049 if ((tmp
= PEM_read_X509(fp
, NULL
, ssl_password_cb
, NULL
)
1051 ssl_gen_err("Error reading certificate from \"%s\"",
1056 sk_X509_push(*chain
, tmp
);
1064 if (sk_X509_num(*chain
) == 0) {
1065 fprintf(stderr
, "smime-sign-include-certs defined but empty\n");
1069 jleave
: return (*chain
!= NULL
);
1071 jerr
: sk_X509_pop_free(*chain
, X509_free
);
1077 smime_certsave(struct message
*m
, int n
, FILE *op
)
1080 char *cp
, *to
, *cc
, *cnttype
;
1086 #ifdef HAVE_STACK_OF
1087 STACK_OF(X509
) *certs
;
1088 STACK_OF(X509
) *chain
= NULL
;
1091 STACK
*chain
= NULL
;
1094 enum okay ok
= OKAY
;
1097 loop
: to
= hfield("to", m
);
1098 cc
= hfield("cc", m
);
1099 cnttype
= hfield("content-type", m
);
1100 if ((ip
= setinput(&mb
, m
, NEED_BODY
)) == NULL
)
1102 if (cnttype
&& strncmp(cnttype
, "application/x-pkcs7-mime", 24) == 0) {
1103 if ((x
= smime_decrypt(m
, to
, cc
, 1)) == NULL
)
1105 if (x
!= (struct message
*)-1) {
1111 if ((fp
= Ftemp(&cp
, "Rv", "w+", 0600, 1)) == NULL
) {
1117 while (size
-- > 0) {
1123 if ((fb
= BIO_new_fp(fp
, BIO_NOCLOSE
)) == NULL
) {
1124 ssl_gen_err("Error creating BIO object for message %d", n
);
1128 if ((pkcs7
= SMIME_read_PKCS7(fb
, &pb
)) == NULL
) {
1129 ssl_gen_err("Error reading PKCS#7 object for message %d", n
);
1136 certs
= PKCS7_get0_signers(pkcs7
, chain
, 0);
1137 if (certs
== NULL
) {
1138 fprintf(stderr
, "No certificates found in message %d.\n", n
);
1141 for (i
= 0; i
< sk_X509_num(certs
); i
++) {
1142 cert
= sk_X509_value(certs
, i
);
1143 if (X509_print_fp(op
, cert
) == 0 ||
1144 PEM_write_X509(op
, cert
) == 0) {
1145 ssl_gen_err("Error writing certificate %d from "
1146 "message %d", i
, n
);
1153 #if defined (X509_V_FLAG_CRL_CHECK) && defined (X509_V_FLAG_CRL_CHECK_ALL)
1155 load_crl1(X509_STORE
*store
, const char *name
)
1157 X509_LOOKUP
*lookup
;
1160 printf("Loading CRL from \"%s\".\n", name
);
1161 if ((lookup
= X509_STORE_add_lookup(store
,
1162 X509_LOOKUP_file())) == NULL
) {
1163 ssl_gen_err("Error creating X509 lookup object");
1166 if (X509_load_crl_file(lookup
, name
, X509_FILETYPE_PEM
) != 1) {
1167 ssl_gen_err("Error loading CRL from \"%s\"", name
);
1172 #endif /* new OpenSSL */
1175 load_crls(X509_STORE
*store
, const char *vfile
, const char *vdir
)
1177 char *crl_file
, *crl_dir
;
1178 #if defined (X509_V_FLAG_CRL_CHECK) && defined (X509_V_FLAG_CRL_CHECK_ALL)
1183 #endif /* new OpenSSL */
1185 if ((crl_file
= value(vfile
)) != NULL
) {
1186 #if defined (X509_V_FLAG_CRL_CHECK) && defined (X509_V_FLAG_CRL_CHECK_ALL)
1187 crl_file
= expand(crl_file
);
1188 if (load_crl1(store
, crl_file
) != OKAY
)
1190 #else /* old OpenSSL */
1192 "This OpenSSL version is too old to use CRLs.\n");
1194 #endif /* old OpenSSL */
1196 if ((crl_dir
= value(vdir
)) != NULL
) {
1197 #if defined (X509_V_FLAG_CRL_CHECK) && defined (X509_V_FLAG_CRL_CHECK_ALL)
1198 crl_dir
= expand(crl_dir
);
1199 ds
= strlen(crl_dir
);
1200 if ((dirfd
= opendir(crl_dir
)) == NULL
) {
1204 fn
= smalloc(fs
= ds
+ 20);
1205 strcpy(fn
, crl_dir
);
1207 while ((dp
= readdir(dirfd
)) != 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 strcpy(&fn
[ds
+1], dp
->d_name
);
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 */
1240 #else /* !USE_OPENSSL */
1247 fprintf(stderr
, "No S/MIME support compiled in.\n");
1252 smime_sign(FILE *fp
)
1268 smime_encrypt(FILE *fp
, const char *certfile
, const char *to
)
1276 smime_decrypt(struct message
*m
, const char *to
, const char *cc
, int signcall
)
1289 #endif /* !USE_OPENSSL */
1290 #endif /* !USE_NSS */