1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ OpenSSL client implementation according to: John Viega, Matt Messier,
3 *@ Pravir Chandra: Network Security with OpenSSL. Sebastopol, CA 2002.
4 *@ TODO This needs an overhaul -- there _are_ stack leaks!?
6 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
7 * Copyright (c) 2012 - 2018 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
11 * Gunnar Ritter. All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by Gunnar Ritter
24 * and his contributors.
25 * 4. Neither the name of Gunnar Ritter nor the names of his contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY GUNNAR RITTER AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL GUNNAR RITTER OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 #ifndef HAVE_AMALGAMATION
50 #include <sys/socket.h>
52 #include <openssl/crypto.h>
53 #include <openssl/err.h>
54 #include <openssl/evp.h>
55 #include <openssl/opensslv.h>
56 #include <openssl/pem.h>
57 #include <openssl/rand.h>
58 #include <openssl/ssl.h>
59 #include <openssl/x509v3.h>
60 #include <openssl/x509.h>
62 #ifdef HAVE_XSSL_CONFIG
63 # include <openssl/conf.h>
66 #if defined X509_V_FLAG_CRL_CHECK && defined X509_V_FLAG_CRL_CHECK_ALL
70 /* Compatibility shims which assume 0/-1 cannot really happen */
71 #ifndef HAVE_XSSL_CONF_CTX
72 # ifndef SSL_OP_NO_SSLv2
73 # define SSL_OP_NO_SSLv2 0
75 # ifndef SSL_OP_NO_SSLv3
76 # define SSL_OP_NO_SSLv3 0
78 # ifndef SSL_OP_NO_TLSv1
79 # define SSL_OP_NO_TLSv1 0
81 # ifndef SSL_OP_NO_TLSv1_1
82 # define SSL_OP_NO_TLSv1_1 0
84 # ifndef SSL_OP_NO_TLSv1_2
85 # define SSL_OP_NO_TLSv1_2 0
87 /* SSL_CONF_CTX and _OP_NO_SSL_MASK were both introduced with 1.0.2!?! */
88 # ifndef SSL_OP_NO_SSL_MASK
89 # define SSL_OP_NO_SSL_MASK \
90 (SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 |\
91 SSL_OP_NO_TLSv1 | SSL_OP_NO_TLSv1_1 | SSL_OP_NO_TLSv1_2)
95 # define SSL2_VERSION 0
98 # define SSL3_VERSION 0
100 # ifndef TLS1_VERSION
101 # define TLS1_VERSION 0
103 # ifndef TLS1_1_VERSION
104 # define TLS1_1_VERSION 0
106 # ifndef TLS1_2_VERSION
107 # define TLS1_2_VERSION 0
111 #ifdef HAVE_XSSL_STACK_OF
112 # define n_XSSL_STACKOF(X) STACK_OF(X)
114 # define n_XSSL_STACKOF(X) /*X*/STACK
117 #if OPENSSL_VERSION_NUMBER + 0 >= 0x0090581fL
118 # define a_XSSL_RAND_LOAD_FILE_MAXBYTES -1
120 # define a_XSSL_RAND_LOAD_FILE_MAXBYTES 1024
123 /* Compatibility sighs (that sigh is _really_ a cute one) */
124 #if HAVE_XSSL_OPENSSL >= 0x10100
125 # define a_xssl_X509_get_notBefore X509_get0_notBefore
126 # define a_xssl_X509_get_notAfter X509_get0_notAfter
128 # define a_xssl_X509_get_notBefore X509_get_notBefore
129 # define a_xssl_X509_get_notAfter X509_get_notAfter
132 /* X509_STORE_set_flags */
133 #undef a_XSSL_X509_V_ANY
134 #ifndef X509_V_FLAG_NO_ALT_CHAINS
135 # define X509_V_FLAG_NO_ALT_CHAINS -1
137 # undef a_XSSL_X509_V_ANY
138 # define a_XSSL_X509_V_ANY
140 #ifndef X509_V_FLAG_NO_CHECK_TIME
141 # define X509_V_FLAG_NO_CHECK_TIME -1
143 # undef a_XSSL_X509_V_ANY
144 # define a_XSSL_X509_V_ANY
146 #ifndef X509_V_FLAG_PARTIAL_CHAIN
147 # define X509_V_FLAG_PARTIAL_CHAIN -1
149 # undef a_XSSL_X509_V_ANY
150 # define a_XSSL_X509_V_ANY
152 #ifndef X509_V_FLAG_X509_STRICT
153 # define X509_V_FLAG_X509_STRICT -1
155 # undef a_XSSL_X509_V_ANY
156 # define a_XSSL_X509_V_ANY
158 #ifndef X509_V_FLAG_TRUSTED_FIRST
159 # define X509_V_FLAG_TRUSTED_FIRST -1
161 # undef a_XSSL_X509_V_ANY
162 # define a_XSSL_X509_V_ANY
166 a_XSSL_S_INIT
= 1u<<0,
167 a_XSSL_S_RAND_INIT
= 1u<<1,
168 a_XSSL_S_CONF_LOAD
= 1u<<2,
170 #if HAVE_XSSL_OPENSSL < 0x10100
171 a_XSSL_S_EXIT_HDL
= 1u<<8,
172 a_XSSL_S_ALGO_LOAD
= 1u<<9,
175 a_XSSL_S_VERIFY_ERROR
= 1u<<16
178 struct ssl_method
{ /* TODO v15 obsolete */
179 char const sm_name
[8];
180 char const sm_map
[16];
183 #ifndef HAVE_XSSL_CONF_CTX
184 struct a_xssl_protocol
{
185 char const sp_name
[8];
186 sl_i sp_op_no
; /* SSL_OP_NO_* bit */
187 ui16_t sp_version
; /* *_VERSION number */
188 bool_t sp_ok_minmaxproto
; /* Valid for {Min,Max}Protocol= */
189 bool_t sp_ok_proto
; /* Valid for Protocol= */
194 struct a_xssl_smime_cipher
{
195 char const sc_name
[8];
196 EVP_CIPHER
const *(*sc_fun
)(void);
199 struct a_xssl_smime_digest
{
200 char const sd_name
[8];
201 EVP_MD
const *(*sd_fun
)(void);
204 struct a_xssl_x509_v_flags
{
205 char const xvf_name
[20];
209 /* Supported SSL/TLS methods: update manual on change! */
210 static struct ssl_method
const _ssl_methods
[] = { /* TODO obsolete */
211 {"auto", "ALL,-SSLv2"},
212 {"ssl3", "-ALL,SSLv3"},
213 {"tls1", "-ALL,TLSv1"},
214 {"tls1.1", "-ALL,TLSv1.1"},
215 {"tls1.2", "-ALL,TLSv1.2"}
218 /* Update manual on change!
219 * Ensure array size by adding \0 to longest entry.
220 * Strictly to be sorted new/up to old/down, [0]=ALL, [x-1]=None! */
221 #ifndef HAVE_XSSL_CONF_CTX
222 static struct a_xssl_protocol
const a_xssl_protocols
[] = {
223 {"ALL", SSL_OP_NO_SSL_MASK
, 0, FAL0
, TRU1
, {0}},
224 {"TLSv1.2\0", SSL_OP_NO_TLSv1_2
, TLS1_2_VERSION
, TRU1
, TRU1
, {0}},
225 {"TLSv1.1", SSL_OP_NO_TLSv1_1
, TLS1_1_VERSION
, TRU1
, TRU1
, {0}},
226 {"TLSv1", SSL_OP_NO_TLSv1
, TLS1_VERSION
, TRU1
, TRU1
, {0}},
227 {"SSLv3", SSL_OP_NO_SSLv3
, SSL3_VERSION
, TRU1
, TRU1
, {0}},
228 {"SSLv2", SSL_OP_NO_SSLv2
, SSL2_VERSION
, TRU1
, TRU1
, {0}},
229 {"None", SSL_OP_NO_SSL_MASK
, 0, TRU1
, FAL0
, {0}}
231 #endif /* HAVE_XSSL_CONF_CTX */
233 /* Supported S/MIME cipher algorithms */
234 static struct a_xssl_smime_cipher
const a_xssl_smime_ciphers
[] = { /* Manual! */
235 #ifndef OPENSSL_NO_AES
236 # define a_XSSL_SMIME_DEFAULT_CIPHER EVP_aes_128_cbc /* According to RFC 5751 */
237 {"aes128", &EVP_aes_128_cbc
},
238 {"aes256", &EVP_aes_256_cbc
},
239 {"aes192", &EVP_aes_192_cbc
},
241 #ifndef OPENSSL_NO_DES
242 # ifndef a_XSSL_SMIME_DEFAULT_CIPHER
243 # define a_XSSL_SMIME_DEFAULT_CIPHER EVP_des_ede3_cbc
245 {"des3", &EVP_des_ede3_cbc
},
246 {"des", &EVP_des_cbc
},
249 #ifndef a_XSSL_SMIME_DEFAULT_CIPHER
250 # error Your OpenSSL library does not include the necessary
251 # error cipher algorithms that are required to support S/MIME
254 #ifndef OPENSSL_NO_AES
255 /* TODO obsolete a_xssl_smime_ciphers_obs */
256 static struct a_xssl_smime_cipher
const a_xssl_smime_ciphers_obs
[] = {
257 {"aes-128", &EVP_aes_128_cbc
},
258 {"aes-256", &EVP_aes_256_cbc
},
259 {"aes-192", &EVP_aes_192_cbc
}
263 /* Supported S/MIME message digest algorithms */
264 static struct a_xssl_smime_digest
const a_xssl_smime_digests
[] = { /* Manual! */
265 #define a_XSSL_SMIME_DEFAULT_DIGEST EVP_sha1 /* According to RFC 5751 */
266 #define a_XSSL_SMIME_DEFAULT_DIGEST_S "sha1"
268 {"sha256", &EVP_sha256
},
269 {"sha512", &EVP_sha512
},
270 {"sha384", &EVP_sha384
},
271 {"sha224", &EVP_sha224
},
272 #ifndef OPENSSL_NO_MD5
277 /* X509_STORE_set_flags() for *{smime,ssl}-ca-flags* */
278 static struct a_xssl_x509_v_flags
const a_xssl_x509_v_flags
[] = { /* Manual! */
279 {"no-alt-chains", X509_V_FLAG_NO_ALT_CHAINS
},
280 {"no-check-time", X509_V_FLAG_NO_CHECK_TIME
},
281 {"partial-chain", X509_V_FLAG_PARTIAL_CHAIN
},
282 {"strict", X509_V_FLAG_X509_STRICT
},
283 {"trusted-first", X509_V_FLAG_TRUSTED_FIRST
},
286 static enum a_xssl_state a_xssl_state
;
287 static size_t a_xssl_msgno
;
289 static void a_xssl_rand_init(void);
290 static void a_xssl_init(void);
292 #if HAVE_XSSL_OPENSSL < 0x10100
293 # ifdef HAVE_SSL_ALL_ALGORITHMS
294 static void a_xssl__load_algos(void);
295 # define a_xssl_load_algos a_xssl__load_algos
297 # if defined HAVE_XSSL_CONFIG || defined HAVE_SSL_ALL_ALGORITHMS
298 static void a_xssl_atexit(void);
301 #ifndef a_xssl_load_algos
302 # define a_xssl_load_algos() do{;}while(0)
305 static bool_t
_ssl_parse_asn1_time(ASN1_TIME
const *atp
,
306 char *bdat
, size_t blen
);
307 static int _ssl_verify_cb(int success
, X509_STORE_CTX
*store
);
309 /* *smime-ca-flags*, *ssl-ca-flags* */
310 static void a_xssl_ca_flags(X509_STORE
*store
, char const *flags
);
312 /* SSL_CTX configuration */
313 static void * a_xssl_conf_setup(SSL_CTX
*ctxp
, struct url
const *urlp
);
314 static bool_t
a_xssl_conf(void *confp
, char const *cmd
, char const *value
);
315 static bool_t
a_xssl_conf_finish(void **confp
, bool_t error
);
317 static bool_t
a_xssl_obsolete_conf_vars(void *confp
, struct url
const *urlp
);
318 static bool_t
a_xssl_config_pairs(void *confp
, struct url
const *urlp
);
319 static bool_t
a_xssl_load_verifications(SSL_CTX
*ctxp
, struct url
const *urlp
);
321 static enum okay
ssl_check_host(struct sock
*sp
, struct url
const *urlp
);
323 static int smime_verify(struct message
*m
, int n
,
324 n_XSSL_STACKOF(X509
) *chain
, X509_STORE
*store
);
325 static EVP_CIPHER
const * _smime_cipher(char const *name
);
326 static int ssl_password_cb(char *buf
, int size
, int rwflag
,
328 static FILE * smime_sign_cert(char const *xname
, char const *xname2
,
329 bool_t dowarn
, char const **match
);
330 static char const * _smime_sign_include_certs(char const *name
);
331 static bool_t
_smime_sign_include_chain_creat(n_XSSL_STACKOF(X509
) **chain
,
332 char const *cfiles
, char const *addr
);
333 static EVP_MD
const * _smime_sign_digest(char const *name
,
334 char const **digname
);
335 #if defined X509_V_FLAG_CRL_CHECK && defined X509_V_FLAG_CRL_CHECK_ALL
336 static enum okay
load_crl1(X509_STORE
*store
, char const *name
);
338 static enum okay
load_crls(X509_STORE
*store
, enum okeys fok
, enum okeys dok
);
341 a_xssl_rand_init(void){
342 #define a_XSSL_RAND_ENTROPY 32
343 char b64buf
[a_XSSL_RAND_ENTROPY
* 5 +1], *randfile
;
348 a_xssl_state
|= a_XSSL_S_RAND_INIT
;
353 #ifdef HAVE_XSSL_CONFIG
354 if(!(a_xssl_state
& a_XSSL_S_INIT
))
358 /* Shall use some external daemon? */
359 if((cp
= ok_vlook(ssl_rand_egd
)) != NULL
){
360 #ifdef HAVE_XSSL_RAND_EGD
361 if((x
= fexpand(cp
, FEXP_LOCAL
| FEXP_NOPROTO
)) != NULL
&&
362 RAND_egd(cp
= x
) != -1){
366 n_err(_("*ssl_rand_egd* daemon at %s not available\n"),
367 n_shexp_quote_cp(cp
, FAL0
));
369 if(n_poption
& n_PO_D_VV
)
370 n_err(_("*ssl_rand_egd* (%s): unsupported by SSL library\n"),
371 n_shexp_quote_cp(cp
, FAL0
));
375 /* Prefer possible user setting */
376 if((cp
= ok_vlook(ssl_rand_file
)) != NULL
){
379 if((x
= fexpand(cp
, FEXP_LOCAL
| FEXP_NOPROTO
)) == NULL
)
380 n_err(_("*ssl-rand-file*: expansion of %s failed "
381 "(using OpenSSL default)\n"),
382 n_shexp_quote_cp(cp
, FAL0
));
387 randfile
= n_lofi_alloc(PATH_MAX
);
388 if((cp
= RAND_file_name(randfile
, PATH_MAX
)) == NULL
){
389 n_err(_("*ssl-rand-file*: no SSL entropy file, can't seed PRNG\n"));
394 (void)RAND_load_file(cp
, a_XSSL_RAND_LOAD_FILE_MAXBYTES
);
396 /* And feed in some data, then write the updated file.
397 * While this rather feeds the PRNG with itself in the HAVE_SSL_RANDOM
398 * case, let us stir the buffer a little bit.
399 * Estimate a low but likely still too high number of entropy bytes, use
400 * 20%: base64 uses 3 input = 4 output bytes relation, and the base64
401 * alphabet is a 6 bit one */
402 for(x
= (char*)-1;;){
403 RAND_add(n_random_create_buf(b64buf
, sizeof(b64buf
) -1, NULL
),
404 sizeof(b64buf
) -1, a_XSSL_RAND_ENTROPY
);
405 if((x
= (char*)((uintptr_t)x
>> (1
406 #ifdef HAVE_SSL_RANDOM
410 err
= (RAND_status() == 0);
413 #ifndef HAVE_SSL_RANDOM
414 if(!(err
= (RAND_status() == 0)))
420 err
= (RAND_write_file(cp
) == -1);
424 n_lofi_free(randfile
);
426 n_panic(_("Cannot seed the *SSL PseudoRandomNumberGenerator, "
427 "RAND_status() is 0!\n"
428 " Please set *ssl-rand-file* to a file with sufficient entropy.\n"
429 " On a machine with entropy: "
430 "\"$ dd if=/dev/urandom of=FILE bs=1024 count=1\"\n"));
436 #ifdef HAVE_XSSL_CONFIG
441 if(a_xssl_state
& a_XSSL_S_INIT
)
444 #if HAVE_XSSL_OPENSSL >= 0x10100
445 OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS
|
446 OPENSSL_INIT_LOAD_CRYPTO_STRINGS
447 # ifdef HAVE_SSL_ALL_ALGORITHMS
448 | OPENSSL_INIT_ADD_ALL_CIPHERS
| OPENSSL_INIT_ADD_ALL_DIGESTS
452 SSL_load_error_strings();
456 a_xssl_state
|= a_XSSL_S_INIT
;
459 /* Load openssl.cnf or whatever was given in *ssl-config-file* */
460 #ifdef HAVE_XSSL_CONFIG
461 if((cp
= ok_vlook(ssl_config_file
)) != NULL
){
468 flags
= CONF_MFLAGS_IGNORE_MISSING_FILE
;
469 }else if((msg
= cp
, cp
= fexpand(cp
, FEXP_LOCAL
| FEXP_NOPROTO
)) != NULL
)
472 n_err(_("*ssl-config-file*: file expansion failed: %s\n"),
473 n_shexp_quote_cp(msg
, FAL0
));
477 if(CONF_modules_load_file(cp
, n_uagent
, flags
) == 1){
478 a_xssl_state
|= a_XSSL_S_CONF_LOAD
;
479 # if HAVE_XSSL_OPENSSL < 0x10100
480 if(!(a_xssl_state
& a_XSSL_S_EXIT_HDL
)){
481 a_xssl_state
|= a_XSSL_S_EXIT_HDL
;
482 atexit(&a_xssl_atexit
); /* TODO generic program-wide event mech. */
485 if(n_poption
& n_PO_D_V
)
486 n_err(_("Loaded SSL/TLS configuration for %s from %s\n"), n_uagent
,
487 n_shexp_quote_cp(msg
, FAL0
));
490 ssl_gen_err(_("SSL/TLS CONF_modules_load_file() load error"));
492 #endif /* HAVE_XSSL_CONFIG */
494 if(!(a_xssl_state
& a_XSSL_S_RAND_INIT
))
500 #if HAVE_XSSL_OPENSSL < 0x10100
501 # ifdef HAVE_SSL_ALL_ALGORITHMS
503 a_xssl__load_algos(void){
505 if(!(a_xssl_state
& a_XSSL_S_ALGO_LOAD
)){
506 a_xssl_state
|= a_XSSL_S_ALGO_LOAD
;
507 OpenSSL_add_all_algorithms();
509 if(!(a_xssl_state
& a_XSSL_S_EXIT_HDL
)){
510 a_xssl_state
|= a_XSSL_S_EXIT_HDL
;
511 atexit(&a_xssl_atexit
); /* TODO generic program-wide event mech. */
518 # if defined HAVE_XSSL_CONFIG || defined HAVE_SSL_ALL_ALGORITHMS
522 # ifdef HAVE_XSSL_CONFIG
523 if(a_xssl_state
& a_XSSL_S_CONF_LOAD
)
527 # ifdef HAVE_SSL_ALL_ALGORITHMS
528 if(a_xssl_state
& a_XSSL_S_ALGO_LOAD
)
534 #endif /* HAVE_XSSL_OPENSSL < 0x10100 */
537 _ssl_parse_asn1_time(ASN1_TIME
const *atp
, char *bdat
, size_t blen
)
544 mbp
= BIO_new(BIO_s_mem());
546 if (ASN1_TIME_print(mbp
, atp
) && (l
= BIO_get_mem_data(mbp
, &mcp
)) > 0)
547 snprintf(bdat
, blen
, "%.*s", (int)l
, mcp
);
549 snprintf(bdat
, blen
, _("Bogus certificate date: %.*s"),
550 /*is (int)*/atp
->length
, (char const*)atp
->data
);
556 return (mcp
!= NULL
);
560 _ssl_verify_cb(int success
, X509_STORE_CTX
*store
)
567 if (success
&& !(n_poption
& n_PO_D_V
))
570 if (a_xssl_msgno
!= 0) {
571 n_err(_("Message %lu:\n"), (ul_i
)a_xssl_msgno
);
574 n_err(_(" Certificate depth %d %s\n"),
575 X509_STORE_CTX_get_error_depth(store
), (success
? n_empty
: V_(n_error
)));
577 if ((cert
= X509_STORE_CTX_get_current_cert(store
)) != NULL
) {
578 X509_NAME_oneline(X509_get_subject_name(cert
), data
, sizeof data
);
579 n_err(_(" subject = %s\n"), data
);
581 _ssl_parse_asn1_time(a_xssl_X509_get_notBefore(cert
), data
, sizeof data
);
582 n_err(_(" notBefore = %s\n"), data
);
584 _ssl_parse_asn1_time(a_xssl_X509_get_notAfter(cert
), data
, sizeof data
);
585 n_err(_(" notAfter = %s\n"), data
);
587 X509_NAME_oneline(X509_get_issuer_name(cert
), data
, sizeof data
);
588 n_err(_(" issuer = %s\n"), data
);
592 int err
= X509_STORE_CTX_get_error(store
);
594 n_err(_(" err %i: %s\n"), err
, X509_verify_cert_error_string(err
));
595 a_xssl_state
|= a_XSSL_S_VERIFY_ERROR
;
598 if (!success
&& ssl_verify_decide() != OKAY
)
606 a_xssl_ca_flags(X509_STORE
*store
, char const *flags
){
611 iolist
= savestr(flags
);
613 while((cp
= n_strsep(&iolist
, ',', TRU1
)) != NULL
){
614 struct a_xssl_x509_v_flags
const *xvfp
;
616 for(xvfp
= &a_xssl_x509_v_flags
[0];
617 xvfp
< &a_xssl_x509_v_flags
[n_NELEM(a_xssl_x509_v_flags
)];
619 if(!asccasecmp(cp
, xvfp
->xvf_name
)){
620 if(xvfp
->xvf_flag
!= -1){
621 #ifdef a_XSSL_X509_V_ANY
622 X509_STORE_set_flags(store
, xvfp
->xvf_flag
);
624 }else if(n_poption
& n_PO_D_V
)
625 n_err(_("*{smime,ssl}-ca-flags*: "
626 "directive not supported: %s\n"), cp
);
629 n_err(_("*{smime,ssl}-ca-flags*: invalid directive: %s\n"), cp
);
635 #ifdef HAVE_XSSL_CONF_CTX
637 a_xssl_conf_setup(SSL_CTX
*ctxp
, struct url
const *urlp
){
644 if((cp
= xok_vlook(ssl_config_module
, urlp
, OXM_ALL
)) != NULL
){
645 # ifdef HAVE_XSSL_CTX_CONFIG
646 if(!(a_xssl_state
& a_XSSL_S_CONF_LOAD
)){
647 n_err(_("*ssl-config-module*: no *ssl-config-file* loaded: %s\n"),
648 n_shexp_quote_cp(cp
, FAL0
));
650 }else if(!SSL_CTX_config(ctxp
, cp
)){
651 ssl_gen_err(_("*ssl-config-module*: load error for %s, section [%s]"),
652 n_uagent
, n_shexp_quote_cp(cp
, FAL0
));
656 n_err(_("*ssl-config-module*: set but not supported: %s\n"),
657 n_shexp_quote_cp(cp
, FAL0
));
662 if((sccp
= SSL_CONF_CTX_new()) != NULL
){
663 SSL_CONF_CTX_set_flags(sccp
,
664 SSL_CONF_FLAG_FILE
| SSL_CONF_FLAG_CLIENT
|
665 SSL_CONF_FLAG_CERTIFICATE
| SSL_CONF_FLAG_SHOW_ERRORS
);
667 SSL_CONF_CTX_set_ssl_ctx(sccp
, ctxp
);
669 ssl_gen_err(_("SSL_CONF_CTX_new() failed"));
676 a_xssl_conf(void *confp
, char const *cmd
, char const *value
){
681 if(n_poption
& n_PO_D_V
)
682 n_err(_("SSL/TLS: applying config: %s = %s\n"),
683 n_shexp_quote_cp(cmd
, FAL0
), n_shexp_quote_cp(value
, FAL0
));
685 rv
= SSL_CONF_cmd(sccp
= confp
, cmd
, value
);
689 cmd
= n_shexp_quote_cp(cmd
, FAL0
);
690 value
= n_shexp_quote_cp(value
, FAL0
);
692 ssl_gen_err(_("SSL/TLS: config failure: %s = %s"), cmd
, value
);
694 n_err(_("SSL/TLS: please report this config error: %s = %s\n"),
703 a_xssl_conf_finish(void **confp
, bool_t error
){
708 sccp
= (SSL_CONF_CTX
**)confp
;
711 rv
= (SSL_CONF_CTX_finish(*sccp
) != 0);
713 SSL_CONF_CTX_free(*sccp
);
720 #else /* HAVE_XSSL_CONF_CTX */
721 # ifdef HAVE_XSSL_CTX_CONFIG
722 # error SSL_CTX_config(3) support unexpected without SSL_CONF_CTX support
726 a_xssl_conf_setup(SSL_CTX
* ctxp
, struct url
const *urlp
){
730 if((cp
= xok_vlook(ssl_config_module
, urlp
, OXM_ALL
)) != NULL
){
731 n_err(_("*ssl-config-module*: set but not supported: %s\n"),
732 n_shexp_quote_cp(cp
, FAL0
));
740 a_xssl_conf(void *confp
, char const *cmd
, char const *value
){
741 char const *xcmd
, *emsg
;
745 if(n_poption
& n_PO_D_V
)
746 n_err(_("SSL/TLS: applying config: %s = %s\n"),
747 n_shexp_quote_cp(cmd
, FAL0
), n_shexp_quote_cp(value
, FAL0
));
751 if(!asccasecmp(cmd
, xcmd
= "Certificate")){
752 if(SSL_CTX_use_certificate_chain_file(ctxp
, value
) != 1){
753 emsg
= N_("SSL/TLS: %s: cannot load from file %s\n");
756 }else if(!asccasecmp(cmd
, xcmd
= "CipherList")){
757 if(SSL_CTX_set_cipher_list(ctxp
, value
) != 1){
758 emsg
= N_("SSL/TLS: %s: invalid: %s\n");
761 }else if(!asccasecmp(cmd
, xcmd
= "Curves")){
762 #ifdef SSL_CTRL_SET_CURVES_LIST
763 if(SSL_CTX_set1_curves_list(ctxp
, value
) != 1){
764 emsg
= N_("SSL/TLS: %s: invalid: %s\n");
769 emsg
= N_("SSL/TLS: %s: directive not supported\n");
772 }else if((emsg
= NULL
, !asccasecmp(cmd
, xcmd
= "MaxProtocol")) ||
773 (emsg
= (char*)-1, !asccasecmp(cmd
, xcmd
= "MinProtocol"))){
774 #ifndef HAVE_XSSL_SET_MIN_PROTO_VERSION
776 emsg
= N_("SSL/TLS: %s: directive not supported\n");
779 struct a_xssl_protocol
const *xpp
;
782 for(i
= 1 /* [0] == ALL */;;){
783 xpp
= &a_xssl_protocols
[i
];
785 if(xpp
->sp_ok_minmaxproto
&& !asccasecmp(value
, xpp
->sp_name
))
788 if(++i
>= n_NELEM(a_xssl_protocols
)){
789 emsg
= N_("SSL/TLS: %s: unsupported element: %s\n");
794 if((emsg
== NULL
? SSL_CTX_set_max_proto_version(ctxp
, xpp
->sp_version
)
795 : SSL_CTX_set_min_proto_version(ctxp
, xpp
->sp_version
)) != 1){
796 emsg
= N_("SSL/TLS: %s: invalid protocol: %s\n");
799 #endif /* !HAVE_XSSL_SET_MIN_PROTO_VERSION */
800 }else if(!asccasecmp(cmd
, xcmd
= "Options")){
801 if(asccasecmp(value
, "Bugs")){
802 emsg
= N_("SSL/TLS: %s: fallback only supports value \"Bugs\": %s\n");
805 SSL_CTX_set_options(ctxp
, SSL_OP_ALL
);
806 }else if(!asccasecmp(cmd
, xcmd
= "PrivateKey")){
807 if(SSL_CTX_use_PrivateKey_file(ctxp
, value
, SSL_FILETYPE_PEM
) != 1){
808 emsg
= N_("%s: cannot load from file %s\n");
811 }else if(!asccasecmp(cmd
, xcmd
= "Protocol")){
812 char *iolist
, *cp
, addin
;
818 for(iolist
= cp
= savestr(value
);
819 (cp
= n_strsep(&iolist
, ',', FAL0
)) != NULL
;){
822 emsg
= N_("SSL/TLS: %s: empty elements are not supported\n");
828 case '-': addin
= FAL0
; /* FALLTHRU */
829 case '+': ++cp
; /* FALLTHRU */
834 struct a_xssl_protocol
const *xpp
;
836 xpp
= &a_xssl_protocols
[i
];
838 if(xpp
->sp_ok_proto
&& !asccasecmp(cp
, xpp
->sp_name
)){
839 /* We need to inverse the meaning of the _NO_s */
841 opts
|= xpp
->sp_op_no
;
843 opts
&= ~xpp
->sp_op_no
;
847 if(++i
>= n_NELEM(a_xssl_protocols
)){
848 emsg
= N_("SSL/TLS: %s: unsupported element: %s\n");
854 SSL_CTX_clear_options(ctxp
, SSL_OP_NO_SSL_MASK
);
855 SSL_CTX_set_options(ctxp
, opts
);
857 xcmd
= n_shexp_quote_cp(cmd
, FAL0
);
858 emsg
= N_("SSL/TLS: unsupported directive: %s: value: %s\n");
864 return (confp
!= NULL
);
866 ssl_gen_err(V_(emsg
), xcmd
, n_shexp_quote_cp(value
, FAL0
));
871 value
= n_shexp_quote_cp(value
, FAL0
);
872 n_err(V_(emsg
), xcmd
, value
);
878 a_xssl_conf_finish(void **confp
, bool_t error
){
883 #endif /* !HAVE_XSSL_CONF_CTX */
886 a_xssl_obsolete_conf_vars(void *confp
, struct url
const *urlp
){
887 char const *cp
, *cp_base
, *certchain
;
893 /* Certificate via ssl-cert */
894 if((certchain
= cp
= xok_vlook(ssl_cert
, urlp
, OXM_ALL
)) != NULL
){
895 n_OBSOLETE(_("please use *ssl-config-pairs* instead of *ssl-cert*"));
896 if((cp_base
= fexpand(cp
, FEXP_LOCAL
| FEXP_NOPROTO
)) == NULL
){
897 n_err(_("*ssl-cert* value expansion failed: %s\n"),
898 n_shexp_quote_cp(cp
, FAL0
));
901 if(!a_xssl_conf(confp
, "Certificate", certchain
= cp_base
))
905 /* CipherList via ssl-ciper-list */
906 if((cp
= xok_vlook(ssl_cipher_list
, urlp
, OXM_ALL
)) != NULL
){
907 n_OBSOLETE(_("please use *ssl-config-pairs* instead of "
908 "*ssl-cipher-list*"));
909 if(!a_xssl_conf(confp
, "CipherList", cp
))
913 /* Curves via ssl-curves */
914 if((cp
= xok_vlook(ssl_curves
, urlp
, OXM_ALL
)) != NULL
){
915 n_OBSOLETE(_("please use *ssl-config-pairs* instead of *ssl-curves*"));
916 if(!a_xssl_conf(confp
, "Curves", cp
))
920 /* PrivateKey via ssl-key */
921 if((cp
= xok_vlook(ssl_key
, urlp
, OXM_ALL
)) != NULL
){
922 n_OBSOLETE(_("please use *ssl-config-pairs* instead of *ssl-curves*"));
923 if((cp_base
= fexpand(cp
, FEXP_LOCAL
| FEXP_NOPROTO
)) == NULL
){
924 n_err(_("*ssl-key* value expansion failed: %s\n"),
925 n_shexp_quote_cp(cp
, FAL0
));
929 if(certchain
== NULL
){
930 n_err(_("*ssl-key* can only be used together with *ssl-cert*! "
931 "And use *ssl-config-pairs*!\n"));
935 if((cp
!= NULL
|| (cp
= certchain
) != NULL
) &&
936 !a_xssl_conf(confp
, "PrivateKey", cp
))
939 /* Protocol via ssl-method or ssl-protocol */
940 if((cp
= xok_vlook(ssl_method
, urlp
, OXM_ALL
)) != NULL
){
943 n_OBSOLETE(_("please use *ssl-config-pairs* instead of *ssl-method*"));
945 if(!asccasecmp(_ssl_methods
[i
].sm_name
, cp
)){
946 cp
= _ssl_methods
[i
].sm_map
;
949 if(++i
== n_NELEM(_ssl_methods
)){
950 n_err(_("Unsupported TLS/SSL method: %s\n"), cp
);
955 if((cp_base
= xok_vlook(ssl_protocol
, urlp
, OXM_ALL
)) != NULL
){
956 n_OBSOLETE(_("please use *ssl-config-pairs* instead of *ssl-protocol*"));
957 if(cp
!= NULL
&& (n_poption
& n_PO_D_V
))
958 n_err(_("*ssl-protocol* overrides *ssl-method*! "
959 "And please use *ssl-config-pairs* instead!\n"));
962 if(cp
!= NULL
&& !a_xssl_conf(confp
, "Protocol", cp
))
972 a_xssl_config_pairs(void *confp
, struct url
const *urlp
){
973 /* Due to interdependencies some commands have to be delayed a bit */
974 static char const cmdcert
[] = "Certificate", cmdprivkey
[] = "PrivateKey";
975 char const *valcert
, *valprivkey
;
976 char *pairs
, *cp
, *cmd
, *val
;
979 if((pairs
= n_UNCONST(xok_vlook(ssl_config_pairs
, urlp
, OXM_ALL
))) == NULL
)
981 pairs
= savestr(pairs
);
983 valcert
= valprivkey
= NULL
;
985 while((cp
= n_strsep_esc(&pairs
, ',', FAL0
)) != NULL
){
992 a_EXPAND_MASK
= a_EXPAND
| a_CERT
| a_PRIVKEY
995 /* Directive, space trimmed */
996 if((cmd
= strchr(cp
, '=')) == NULL
){
999 pairs
= n_UNCONST(n_empty
);
1000 n_err(_("*ssl-config-pairs*: missing directive: %s; rest: %s\n"),
1001 n_shexp_quote_cp(cp
, FAL0
), n_shexp_quote_cp(pairs
, FAL0
));
1006 if((cmd
> cp
&& cmd
[-1] == '*')){
1011 while(cmd
> cp
&& (c
= cmd
[-1], blankspacechar(c
)))
1018 /* Command with special treatment? */
1019 if(!asccasecmp(cmd
, cmdcert
))
1021 else if(!asccasecmp(cmd
, cmdprivkey
))
1024 /* Value, space trimmed */
1025 while((c
= *val
) != '\0' && blankspacechar(c
))
1027 cp
= &val
[strlen(val
)];
1028 while(cp
> val
&& (c
= cp
[-1], blankspacechar(c
)))
1033 pairs
= n_UNCONST(n_empty
);
1034 n_err(_("*ssl-config-pairs*: missing value: %s; rest: %s\n"),
1035 n_shexp_quote_cp(cmd
, FAL0
), n_shexp_quote_cp(pairs
, FAL0
));
1039 /* Filename transformations to be applied? */
1040 if(f
& a_EXPAND_MASK
){
1041 if((cp
= fexpand(val
, FEXP_LOCAL
| FEXP_NOPROTO
)) == NULL
){
1043 pairs
= n_UNCONST(n_empty
);
1044 n_err(_("*ssl-config-pairs*: value expansion failed: %s: %s; "
1046 n_shexp_quote_cp(cmd
, FAL0
), n_shexp_quote_cp(val
, FAL0
),
1047 n_shexp_quote_cp(pairs
, FAL0
));
1053 /* Some things have to be delayed */
1056 else if(f
& a_PRIVKEY
)
1058 else if(!a_xssl_conf(confp
, cmd
, val
)){
1059 pairs
= n_UNCONST(n_empty
);
1064 /* Work the delayed ones */
1065 if((valcert
!= NULL
&& !a_xssl_conf(confp
, cmdcert
, valcert
)) ||
1066 ((valprivkey
!= NULL
|| (valprivkey
= valcert
) != NULL
) &&
1067 !a_xssl_conf(confp
, cmdprivkey
, valprivkey
)))
1068 pairs
= n_UNCONST(n_empty
);
1072 return (pairs
== NULL
);
1076 a_xssl_load_verifications(SSL_CTX
*ctxp
, struct url
const *urlp
){
1077 char *ca_dir
, *ca_file
;
1082 if(ssl_verify_level
== SSL_VERIFY_IGNORE
){
1088 if((ca_dir
= xok_vlook(ssl_ca_dir
, urlp
, OXM_ALL
)) != NULL
)
1089 ca_dir
= fexpand(ca_dir
, FEXP_LOCAL
| FEXP_NOPROTO
);
1090 if((ca_file
= xok_vlook(ssl_ca_file
, urlp
, OXM_ALL
)) != NULL
)
1091 ca_file
= fexpand(ca_file
, FEXP_LOCAL
| FEXP_NOPROTO
);
1093 if((ca_dir
!= NULL
|| ca_file
!= NULL
) &&
1094 SSL_CTX_load_verify_locations(ctxp
, ca_file
, ca_dir
) != 1){
1095 char const *m1
, *m2
, *m3
;
1099 m2
= (ca_file
!= NULL
) ? _(" or ") : n_empty
;
1102 m3
= (ca_file
!= NULL
) ? ca_file
: n_empty
;
1103 ssl_gen_err(_("Error loading %s%s%s\n"), m1
, m2
, m3
);
1110 if((xv15
= ok_blook(ssl_no_default_ca
)))
1111 n_OBSOLETE(_("please use *ssl-ca-no-defaults*, "
1112 "not *ssl-no-default-ca*"));
1113 if(!xok_blook(ssl_ca_no_defaults
, urlp
, OXM_ALL
) && !xv15
&&
1114 SSL_CTX_set_default_verify_paths(ctxp
) != 1) {
1115 ssl_gen_err(_("Error loading built-in default CA locations\n"));
1120 a_xssl_state
&= ~a_XSSL_S_VERIFY_ERROR
;
1122 SSL_CTX_set_verify(ctxp
, SSL_VERIFY_PEER
, &_ssl_verify_cb
);
1123 store
= SSL_CTX_get_cert_store(ctxp
);
1124 load_crls(store
, ok_v_ssl_crl_file
, ok_v_ssl_crl_dir
);
1125 a_xssl_ca_flags(store
, xok_vlook(ssl_ca_flags
, urlp
, OXM_ALL
));
1134 ssl_check_host(struct sock
*sp
, struct url
const *urlp
)
1138 n_XSSL_STACKOF(GENERAL_NAME
) *gens
;
1141 enum okay rv
= STOP
;
1144 if ((cert
= SSL_get_peer_certificate(sp
->s_ssl
)) == NULL
) {
1145 n_err(_("No certificate from: %s\n"), urlp
->url_h_p
.s
);
1149 gens
= X509_get_ext_d2i(cert
, NID_subject_alt_name
, NULL
, NULL
);
1153 for (i
= 0; i
< sk_GENERAL_NAME_num(gens
); ++i
) {
1154 gen
= sk_GENERAL_NAME_value(gens
, i
);
1155 if (gen
->type
== GEN_DNS
) {
1156 if (n_poption
& n_PO_D_V
)
1157 n_err(_("Comparing subject_alt_name: need<%s> is<%s>\n"),
1158 urlp
->url_host
.s
, (char*)gen
->d
.ia5
->data
);
1159 rv
= rfc2595_hostname_match(urlp
->url_host
.s
,
1160 (char*)gen
->d
.ia5
->data
);
1167 if ((subj
= X509_get_subject_name(cert
)) != NULL
&&
1168 X509_NAME_get_text_by_NID(subj
, NID_commonName
, data
, sizeof data
)
1170 data
[sizeof data
- 1] = '\0';
1171 if (n_poption
& n_PO_D_V
)
1172 n_err(_("Comparing commonName: need<%s> is<%s>\n"),
1173 urlp
->url_host
.s
, data
);
1174 rv
= rfc2595_hostname_match(urlp
->url_host
.s
, data
);
1185 smime_verify(struct message
*m
, int n
, n_XSSL_STACKOF(X509
) *chain
,
1188 char data
[LINESIZE
], *sender
, *to
, *cc
, *cnttype
;
1195 n_XSSL_STACKOF(X509
) *certs
;
1196 n_XSSL_STACKOF(GENERAL_NAME
) *gens
;
1207 a_xssl_state
&= ~a_XSSL_S_VERIFY_ERROR
;
1208 a_xssl_msgno
= (size_t)n
;
1211 sender
= getsender(m
);
1212 to
= hfield1("to", m
);
1213 cc
= hfield1("cc", m
);
1214 cnttype
= hfield1("content-type", m
);
1218 #define _X (sizeof("application/") -1)
1219 #define _Y(X) X, sizeof(X) -1
1220 if (cnttype
&& is_asccaseprefix("application/", cnttype
) &&
1221 (!ascncasecmp(cnttype
+ _X
, _Y("pkcs7-mime")) ||
1222 !ascncasecmp(cnttype
+ _X
, _Y("x-pkcs7-mime")))) {
1225 if ((x
= smime_decrypt(m
, to
, cc
, 1)) == NULL
)
1227 if (x
!= (struct message
*)-1) {
1233 if ((ip
= setinput(&mb
, m
, NEED_BODY
)) == NULL
)
1239 if ((fp
= Ftmp(NULL
, "smimever", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==
1241 n_perr(_("tempfile"), 0);
1244 while (size
-- > 0) {
1250 if ((fb
= BIO_new_fp(fp
, BIO_NOCLOSE
)) == NULL
) {
1252 "Error creating BIO verification object for message %d"), n
);
1256 if ((pkcs7
= SMIME_read_PKCS7(fb
, &pb
)) == NULL
) {
1257 ssl_gen_err(_("Error reading PKCS#7 object for message %d"), n
);
1260 if (PKCS7_verify(pkcs7
, chain
, store
, pb
, NULL
, 0) != 1) {
1261 ssl_gen_err(_("Error verifying message %d"), n
);
1265 if (sender
== NULL
) {
1266 n_err(_("Warning: Message %d has no sender\n"), n
);
1271 certs
= PKCS7_get0_signers(pkcs7
, chain
, 0);
1272 if (certs
== NULL
) {
1273 n_err(_("No certificates found in message %d\n"), n
);
1277 for (i
= 0; i
< sk_X509_num(certs
); ++i
) {
1278 cert
= sk_X509_value(certs
, i
);
1279 gens
= X509_get_ext_d2i(cert
, NID_subject_alt_name
, NULL
, NULL
);
1281 for (j
= 0; j
< sk_GENERAL_NAME_num(gens
); ++j
) {
1282 gen
= sk_GENERAL_NAME_value(gens
, j
);
1283 if (gen
->type
== GEN_EMAIL
) {
1284 if (n_poption
& n_PO_D_V
)
1285 n_err(_("Comparing subject_alt_name: need<%s> is<%s>)\n"),
1286 sender
, (char*)gen
->d
.ia5
->data
);
1287 if (!asccasecmp((char*)gen
->d
.ia5
->data
, sender
))
1293 if ((subj
= X509_get_subject_name(cert
)) != NULL
&&
1294 X509_NAME_get_text_by_NID(subj
, NID_pkcs9_emailAddress
,
1295 data
, sizeof data
) > 0) {
1296 data
[sizeof data
-1] = '\0';
1297 if (n_poption
& n_PO_D_V
)
1298 n_err(_("Comparing emailAddress: need<%s> is<%s>\n"),
1300 if (!asccasecmp(data
, sender
))
1304 n_err(_("Message %d: certificate does not match <%s>\n"), n
, sender
);
1307 rv
= ((a_xssl_state
& a_XSSL_S_VERIFY_ERROR
) != 0);
1309 fprintf(n_stdout
, _("Message %d was verified successfully\n"), n
);
1312 sk_X509_free(certs
);
1325 static EVP_CIPHER
const *
1326 _smime_cipher(char const *name
)
1328 EVP_CIPHER
const *cipher
;
1334 vn
= ac_alloc(i
= strlen(name
) + sizeof("smime-cipher-") -1 +1);
1335 snprintf(vn
, (int)i
, "smime-cipher-%s", name
);
1336 cp
= n_var_vlook(vn
, FAL0
);
1339 if (cp
== NULL
&& (cp
= ok_vlook(smime_cipher
)) == NULL
) {
1340 cipher
= a_XSSL_SMIME_DEFAULT_CIPHER();
1345 for (i
= 0; i
< n_NELEM(a_xssl_smime_ciphers
); ++i
)
1346 if (!asccasecmp(a_xssl_smime_ciphers
[i
].sc_name
, cp
)) {
1347 cipher
= (*a_xssl_smime_ciphers
[i
].sc_fun
)();
1350 #ifndef OPENSSL_NO_AES
1351 for (i
= 0; i
< n_NELEM(a_xssl_smime_ciphers_obs
); ++i
) /* TODO obsolete */
1352 if (!asccasecmp(a_xssl_smime_ciphers_obs
[i
].sc_name
, cp
)) {
1353 n_OBSOLETE2(_("*smime-cipher* names with hyphens will vanish"), cp
);
1354 cipher
= (*a_xssl_smime_ciphers_obs
[i
].sc_fun
)();
1359 /* Not a built-in algorithm, but we may have dynamic support for more */
1360 #ifdef HAVE_SSL_ALL_ALGORITHMS
1361 if((cipher
= EVP_get_cipherbyname(cp
)) != NULL
)
1365 n_err(_("Invalid S/MIME cipher(s): %s\n"), cp
);
1372 ssl_password_cb(char *buf
, int size
, int rwflag
, void *userdata
)
1381 if(userdata
!= NULL
){
1385 if(url_parse(&url
, CPROTO_CCRED
, userdata
)){
1386 if(ccred_lookup(&cred
, &url
)){
1389 if((slen
= n_strscpy(buf
, cred
.cc_pass
.s
, size
)) >= 0){
1400 if ((pass
= getpassword("PEM pass phrase:")) != NULL
) {
1402 if (UICMP(z
, len
, >=, size
))
1404 memcpy(buf
, pass
, len
);
1415 smime_sign_cert(char const *xname
, char const *xname2
, bool_t dowarn
,
1421 char const *name
= xname
, *name2
= xname2
, *cp
;
1427 np
= lextract(name
, GTO
| GSKIN
);
1428 while (np
!= NULL
) {
1429 /* This needs to be more intelligent since it will currently take the
1430 * first name for which a private key is available regardless of
1431 * whether it is the right one for the message */
1432 vn
= ac_alloc(vs
= strlen(np
->n_name
) + 30);
1433 snprintf(vn
, vs
, "smime-sign-cert-%s", np
->n_name
);
1434 cp
= n_var_vlook(vn
, FAL0
);
1438 *match
= np
->n_name
;
1443 if (name2
!= NULL
) {
1450 if ((cp
= ok_vlook(smime_sign_cert
)) == NULL
)
1455 if ((cp
= fexpand(cp
, FEXP_LOCAL
| FEXP_NOPROTO
)) == NULL
)
1457 if ((fp
= Fopen(cp
, "r")) == NULL
)
1464 n_err(_("Could not find a certificate for %s%s%s\n"),
1465 xname
, (xname2
!= NULL
? _("or ") : n_empty
),
1466 (xname2
!= NULL
? xname2
: n_empty
));
1471 _smime_sign_include_certs(char const *name
)
1476 /* See comments in smime_sign_cert() for algorithm pitfalls */
1480 for (np
= lextract(name
, GTO
| GSKIN
); np
!= NULL
; np
= np
->n_flink
) {
1484 vn
= ac_alloc(vs
= strlen(np
->n_name
) + 30);
1485 snprintf(vn
, vs
, "smime-sign-include-certs-%s", np
->n_name
);
1486 rv
= n_var_vlook(vn
, FAL0
);
1492 rv
= ok_vlook(smime_sign_include_certs
);
1499 _smime_sign_include_chain_creat(n_XSSL_STACKOF(X509
) **chain
,
1500 char const *cfiles
, char const *addr
)
1504 char *nfield
, *cfield
, *x
;
1507 *chain
= sk_X509_new_null();
1509 for (nfield
= savestr(cfiles
);
1510 (cfield
= n_strsep(&nfield
, ',', TRU1
)) != NULL
;) {
1511 if ((x
= fexpand(cfield
, FEXP_LOCAL
| FEXP_NOPROTO
)) == NULL
||
1512 (fp
= Fopen(cfield
= x
, "r")) == NULL
) {
1516 if ((tmp
= PEM_read_X509(fp
, NULL
, &ssl_password_cb
, n_UNCONST(addr
))
1518 ssl_gen_err(_("Error reading certificate from %s"),
1519 n_shexp_quote_cp(cfield
, FAL0
));
1523 sk_X509_push(*chain
, tmp
);
1527 if (sk_X509_num(*chain
) == 0) {
1528 n_err(_("*smime-sign-include-certs* defined but empty\n"));
1533 return (*chain
!= NULL
);
1535 sk_X509_pop_free(*chain
, X509_free
);
1540 static EVP_MD
const *
1541 _smime_sign_digest(char const *name
, char const **digname
)
1543 EVP_MD
const *digest
;
1548 /* See comments in smime_sign_cert() for algorithm pitfalls */
1552 for (np
= lextract(name
, GTO
| GSKIN
); np
!= NULL
; np
= np
->n_flink
) {
1554 char *vn
= ac_alloc(vs
= strlen(np
->n_name
) + 30);
1555 snprintf(vn
, vs
, "smime-sign-message-digest-%s", np
->n_name
);
1556 cp
= n_var_vlook(vn
, FAL0
);
1563 if ((cp
= ok_vlook(smime_sign_message_digest
)) == NULL
) {
1564 digest
= a_XSSL_SMIME_DEFAULT_DIGEST();
1565 *digname
= a_XSSL_SMIME_DEFAULT_DIGEST_S
;
1571 { char *x
= salloc(i
+1);
1572 i_strcpy(x
, cp
, i
+1);
1577 for (i
= 0; i
< n_NELEM(a_xssl_smime_digests
); ++i
)
1578 if (!asccasecmp(a_xssl_smime_digests
[i
].sd_name
, cp
)) {
1579 digest
= (*a_xssl_smime_digests
[i
].sd_fun
)();
1583 /* Not a built-in algorithm, but we may have dynamic support for more */
1584 #ifdef HAVE_SSL_ALL_ALGORITHMS
1585 if((digest
= EVP_get_digestbyname(cp
)) != NULL
)
1589 n_err(_("Invalid message digest: %s\n"), cp
);
1596 #if defined X509_V_FLAG_CRL_CHECK && defined X509_V_FLAG_CRL_CHECK_ALL
1598 load_crl1(X509_STORE
*store
, char const *name
)
1600 X509_LOOKUP
*lookup
;
1601 enum okay rv
= STOP
;
1604 if (n_poption
& n_PO_D_V
)
1605 n_err(_("Loading CRL from %s\n"), n_shexp_quote_cp(name
, FAL0
));
1606 if ((lookup
= X509_STORE_add_lookup(store
, X509_LOOKUP_file())) == NULL
) {
1607 ssl_gen_err(_("Error creating X509 lookup object"));
1610 if (X509_load_crl_file(lookup
, name
, X509_FILETYPE_PEM
) != 1) {
1611 ssl_gen_err(_("Error loading CRL from %s"), n_shexp_quote_cp(name
, FAL0
));
1619 #endif /* new OpenSSL */
1622 load_crls(X509_STORE
*store
, enum okeys fok
, enum okeys dok
)
1624 char *crl_file
, *crl_dir
;
1625 #if defined X509_V_FLAG_CRL_CHECK && defined X509_V_FLAG_CRL_CHECK_ALL
1631 enum okay rv
= STOP
;
1634 if ((crl_file
= n_var_oklook(fok
)) != NULL
) {
1635 #if defined X509_V_FLAG_CRL_CHECK && defined X509_V_FLAG_CRL_CHECK_ALL
1636 if ((crl_file
= fexpand(crl_file
, FEXP_LOCAL
| FEXP_NOPROTO
)) == NULL
||
1637 load_crl1(store
, crl_file
) != OKAY
)
1640 n_err(_("This OpenSSL version is too old to use CRLs\n"));
1645 if ((crl_dir
= n_var_oklook(dok
)) != NULL
) {
1646 #if defined X509_V_FLAG_CRL_CHECK && defined X509_V_FLAG_CRL_CHECK_ALL
1648 if ((x
= fexpand(crl_dir
, FEXP_LOCAL
| FEXP_NOPROTO
)) == NULL
||
1649 (dirp
= opendir(crl_dir
= x
)) == NULL
) {
1654 ds
= strlen(crl_dir
);
1655 fn
= smalloc(fs
= ds
+ 20);
1656 memcpy(fn
, crl_dir
, ds
);
1658 while ((dp
= readdir(dirp
)) != NULL
) {
1659 if (dp
->d_name
[0] == '.' && (dp
->d_name
[1] == '\0' ||
1660 (dp
->d_name
[1] == '.' && dp
->d_name
[2] == '\0')))
1662 if (dp
->d_name
[0] == '.')
1664 if (ds
+ (es
= strlen(dp
->d_name
)) + 2 < fs
)
1665 fn
= srealloc(fn
, fs
= ds
+ es
+ 20);
1666 memcpy(fn
+ ds
+ 1, dp
->d_name
, es
+ 1);
1667 if (load_crl1(store
, fn
) != OKAY
) {
1675 #else /* old OpenSSL */
1676 n_err(_("This OpenSSL version is too old to use CRLs\n"));
1680 #if defined X509_V_FLAG_CRL_CHECK && defined X509_V_FLAG_CRL_CHECK_ALL
1681 if (crl_file
|| crl_dir
)
1682 X509_STORE_set_flags(store
, X509_V_FLAG_CRL_CHECK
|
1683 X509_V_FLAG_CRL_CHECK_ALL
);
1691 #ifdef HAVE_SSL_RANDOM
1693 ssl_rand_bytes(void *buf
, size_t blen
){
1696 if(!(a_xssl_state
& a_XSSL_S_RAND_INIT
))
1702 i
= n_MIN(SI32_MAX
, blen
);
1705 buf
= (ui8_t
*)buf
+ i
;
1712 ssl_open(struct url
const *urlp
, struct sock
*sp
){
1721 ssl_set_verify_level(urlp
);
1723 if((ctxp
= SSL_CTX_new(n_XSSL_CLIENT_METHOD())) == NULL
){
1724 ssl_gen_err(_("SSL_CTX_new() failed"));
1728 /* Available with OpenSSL 0.9.6 or later */
1729 #ifdef SSL_MODE_AUTO_RETRY
1730 SSL_CTX_set_mode(ctxp
, SSL_MODE_AUTO_RETRY
);
1733 if((confp
= a_xssl_conf_setup(ctxp
, urlp
)) == NULL
)
1736 if(!a_xssl_obsolete_conf_vars(confp
, urlp
))
1738 if(!a_xssl_config_pairs(confp
, urlp
))
1740 if(!a_xssl_load_verifications(ctxp
, urlp
))
1743 /* Done with context setup, create our new per-connection structure */
1744 if(!a_xssl_conf_finish(&confp
, FAL0
))
1747 if ((sp
->s_ssl
= SSL_new(ctxp
)) == NULL
) {
1748 ssl_gen_err(_("SSL_new() failed"));
1752 /* Try establish SNI extension; even though this is a TLS extension the
1753 * protocol isn't checked once the host name is set, and therefore i've
1754 * refrained from changing so much code just to check out whether we are
1755 * using SSLv3, which should become more and more rare */
1756 #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1757 if((urlp
->url_flags
& n_URL_TLS_MASK
) &&
1758 (urlp
->url_flags
& n_URL_HOST_IS_NAME
)){
1759 if(!SSL_set_tlsext_host_name(sp
->s_ssl
, urlp
->url_host
.s
) &&
1760 (n_poption
& n_PO_D_V
))
1761 n_err(_("Hostname cannot be used with ServerNameIndication "
1762 "TLS extension: %s\n"),
1763 n_shexp_quote_cp(urlp
->url_host
.s
, FAL0
));
1767 SSL_set_fd(sp
->s_ssl
, sp
->s_fd
);
1769 if (SSL_connect(sp
->s_ssl
) < 0) {
1770 ssl_gen_err(_("could not initiate SSL/TLS connection"));
1774 if (ssl_verify_level
!= SSL_VERIFY_IGNORE
) {
1775 if (ssl_check_host(sp
, urlp
) != OKAY
) {
1776 n_err(_("Host certificate does not match: %s\n"), urlp
->url_h_p
.s
);
1777 if (ssl_verify_decide() != OKAY
)
1782 /* We're fully setup: since we don't reuse the SSL_CTX (pooh) keep it local
1783 * and free it right now -- it is reference counted by sp->s_ssl.. */
1791 SSL_free(sp
->s_ssl
);
1795 a_xssl_conf_finish(&confp
, TRU1
);
1802 ssl_gen_err(char const *fmt
, ...)
1811 n_err(_(": %s\n"), ERR_error_string(ERR_get_error(), NULL
));
1818 int *msgvec
= vp
, *ip
, ec
= 0, rv
= 1;
1819 X509_STORE
*store
= NULL
;
1820 char *ca_dir
, *ca_file
;
1825 ssl_verify_level
= SSL_VERIFY_STRICT
;
1826 if ((store
= X509_STORE_new()) == NULL
) {
1827 ssl_gen_err(_("Error creating X509 store"));
1830 X509_STORE_set_verify_cb_func(store
, &_ssl_verify_cb
);
1832 if ((ca_dir
= ok_vlook(smime_ca_dir
)) != NULL
)
1833 ca_dir
= fexpand(ca_dir
, FEXP_LOCAL
| FEXP_NOPROTO
);
1834 if ((ca_file
= ok_vlook(smime_ca_file
)) != NULL
)
1835 ca_file
= fexpand(ca_file
, FEXP_LOCAL
| FEXP_NOPROTO
);
1837 if (ca_dir
!= NULL
|| ca_file
!= NULL
) {
1838 if (X509_STORE_load_locations(store
, ca_file
, ca_dir
) != 1) {
1839 ssl_gen_err(_("Error loading %s"),
1840 (ca_file
!= NULL
) ? ca_file
: ca_dir
);
1848 if((xv15
= ok_blook(smime_no_default_ca
)))
1849 n_OBSOLETE(_("please use *smime-ca-no-defaults*, "
1850 "not *smime-no-default-ca*"));
1851 if(!ok_blook(smime_ca_no_defaults
) && !xv15
&&
1852 X509_STORE_set_default_paths(store
) != 1) {
1853 ssl_gen_err(_("Error loading built-in default CA locations\n"));
1858 if (load_crls(store
, ok_v_smime_crl_file
, ok_v_smime_crl_dir
) != OKAY
)
1861 a_xssl_ca_flags(store
, ok_vlook(smime_ca_flags
));
1864 for (ip
= msgvec
; *ip
!= 0; ++ip
) {
1865 struct message
*mp
= message
+ *ip
- 1;
1867 ec
|= smime_verify(mp
, *ip
, NULL
, store
);
1873 n_exit_status
|= n_EXIT_ERR
;
1876 X509_STORE_free(store
);
1882 smime_sign(FILE *ip
, char const *addr
)
1884 FILE *rv
, *sp
, *fp
, *bp
, *hp
;
1886 n_XSSL_STACKOF(X509
) *chain
= NULL
;
1887 EVP_PKEY
*pkey
= NULL
;
1895 assert(addr
!= NULL
);
1896 rv
= sp
= fp
= bp
= hp
= NULL
;
1901 n_err(_("No *from* address for signing specified\n"));
1904 if ((fp
= smime_sign_cert(addr
, NULL
, 1, NULL
)) == NULL
)
1907 if ((pkey
= PEM_read_PrivateKey(fp
, NULL
, &ssl_password_cb
,
1908 savecat(addr
, ".smime-cert-key"))) == NULL
) {
1909 ssl_gen_err(_("Error reading private key from"));
1914 if ((cert
= PEM_read_X509(fp
, NULL
, &ssl_password_cb
,
1915 savecat(addr
, ".smime-cert-cert"))) == NULL
) {
1916 ssl_gen_err(_("Error reading signer certificate from"));
1922 if ((name
= _smime_sign_include_certs(addr
)) != NULL
&&
1923 !_smime_sign_include_chain_creat(&chain
, name
,
1924 savecat(addr
, ".smime-include-certs")))
1928 if ((md
= _smime_sign_digest(addr
, &name
)) == NULL
)
1931 if ((sp
= Ftmp(NULL
, "smimesign", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==
1933 n_perr(_("tempfile"), 0);
1938 if (smime_split(ip
, &hp
, &bp
, -1, 0) == STOP
)
1944 if ((bb
= BIO_new_fp(bp
, BIO_NOCLOSE
)) == NULL
||
1945 (sb
= BIO_new_fp(sp
, BIO_NOCLOSE
)) == NULL
) {
1946 ssl_gen_err(_("Error creating BIO signing objects"));
1952 #define _X PKCS7_DETACHED | PKCS7_PARTIAL
1953 if ((pkcs7
= PKCS7_sign(NULL
, NULL
, chain
, bb
, _X
)) == NULL
) {
1954 ssl_gen_err(_("Error creating the PKCS#7 signing object"));
1958 if (PKCS7_sign_add_signer(pkcs7
, cert
, pkey
, md
, _X
) == NULL
) {
1959 ssl_gen_err(_("Error setting PKCS#7 signing object signer"));
1963 if (!PKCS7_final(pkcs7
, bb
, _X
)) {
1964 ssl_gen_err(_("Error finalizing the PKCS#7 signing object"));
1970 if (PEM_write_bio_PKCS7(sb
, pkcs7
) == 0) {
1971 ssl_gen_err(_("Error writing signed S/MIME data"));
1985 rv
= smime_sign_assemble(hp
, bp
, sp
, name
);
1986 hp
= bp
= sp
= NULL
;
1991 sk_X509_pop_free(chain
, X509_free
);
1995 EVP_PKEY_free(pkey
);
2009 smime_encrypt(FILE *ip
, char const *xcertfile
, char const *to
)
2011 FILE *rv
, *yp
, *fp
, *bp
, *hp
;
2015 n_XSSL_STACKOF(X509
) *certs
;
2016 EVP_CIPHER
const *cipher
;
2022 rv
= yp
= fp
= bp
= hp
= NULL
;
2024 if ((certfile
= fexpand(xcertfile
, FEXP_LOCAL
| FEXP_NOPROTO
)) == NULL
)
2029 if ((cipher
= _smime_cipher(to
)) == NULL
)
2032 if ((fp
= Fopen(certfile
, "r")) == NULL
) {
2033 n_perr(certfile
, 0);
2036 if ((cert
= PEM_read_X509(fp
, NULL
, &ssl_password_cb
, NULL
)) == NULL
) {
2037 ssl_gen_err(_("Error reading encryption certificate from %s"),
2038 n_shexp_quote_cp(certfile
, FAL0
));
2047 certs
= sk_X509_new_null();
2048 sk_X509_push(certs
, cert
);
2050 if ((yp
= Ftmp(NULL
, "smimeenc", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==
2052 n_perr(_("tempfile"), 0);
2057 if (smime_split(ip
, &hp
, &bp
, -1, 0) == STOP
)
2061 if ((bb
= BIO_new_fp(bp
, BIO_NOCLOSE
)) == NULL
||
2062 (yb
= BIO_new_fp(yp
, BIO_NOCLOSE
)) == NULL
) {
2063 ssl_gen_err(_("Error creating BIO encryption objects"));
2067 if ((pkcs7
= PKCS7_encrypt(certs
, bb
, cipher
, 0)) == NULL
) {
2068 ssl_gen_err(_("Error creating the PKCS#7 encryption object"));
2072 if (PEM_write_bio_PKCS7(yb
, pkcs7
) == 0) {
2073 ssl_gen_err(_("Error writing encrypted S/MIME data"));
2088 rv
= smime_encrypt_assemble(hp
, yp
);
2092 sk_X509_pop_free(certs
, X509_free
);
2107 smime_decrypt(struct message
*m
, char const *to
, char const *cc
,
2123 ob
= bb
= pb
= NULL
;
2125 bp
= hp
= op
= NULL
;
2129 if((yp
= setinput(&mb
, m
, NEED_BODY
)) == NULL
)
2134 if((op
= smime_sign_cert(to
, cc
, 0, &myaddr
)) != NULL
){
2135 pkey
= PEM_read_PrivateKey(op
, NULL
, &ssl_password_cb
,
2136 savecat(myaddr
, ".smime-cert-key"));
2138 ssl_gen_err(_("Error reading private key"));
2143 if((cert
= PEM_read_X509(op
, NULL
, &ssl_password_cb
,
2144 savecat(myaddr
, ".smime-cert-cert"))) == NULL
){
2145 ssl_gen_err(_("Error reading decryption certificate"));
2153 if((op
= Ftmp(NULL
, "smimedec", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
){
2154 n_perr(_("tempfile"), 0);
2158 if(smime_split(yp
, &hp
, &bp
, size
, 1) == STOP
)
2161 if((ob
= BIO_new_fp(op
, BIO_NOCLOSE
)) == NULL
||
2162 (bb
= BIO_new_fp(bp
, BIO_NOCLOSE
)) == NULL
){
2163 ssl_gen_err(_("Error creating BIO decryption objects"));
2167 if((pkcs7
= SMIME_read_PKCS7(bb
, &pb
)) == NULL
){
2168 ssl_gen_err(_("Error reading PKCS#7 object"));
2172 if(PKCS7_type_is_signed(pkcs7
)){
2174 setinput(&mb
, m
, NEED_BODY
);
2175 rv
= (struct message
*)-1;
2178 if(PKCS7_verify(pkcs7
, NULL
, NULL
, NULL
, ob
,
2179 PKCS7_NOVERIFY
| PKCS7_NOSIGS
) != 1)
2181 fseek(hp
, 0L, SEEK_END
);
2182 fprintf(hp
, "X-Encryption-Cipher: none\n");
2184 }else if(pkey
== NULL
){
2185 n_err(_("No appropriate private key found\n"));
2187 }else if(cert
== NULL
){
2188 n_err(_("No appropriate certificate found\n"));
2190 }else if(PKCS7_decrypt(pkcs7
, pkey
, cert
, ob
, 0) != 1){
2192 ssl_gen_err(_("Error decrypting PKCS#7 object"));
2199 rv
= smime_decrypt_assemble(m
, hp
, op
);
2200 hp
= op
= NULL
; /* xxx closed by decrypt_assemble */
2217 EVP_PKEY_free(pkey
);
2223 smime_certsave(struct message
*m
, int n
, FILE *op
)
2226 char *to
, *cc
, *cnttype
;
2232 n_XSSL_STACKOF(X509
) *certs
, *chain
= NULL
;
2234 enum okay rv
= STOP
;
2239 a_xssl_msgno
= (size_t)n
;
2241 to
= hfield1("to", m
);
2242 cc
= hfield1("cc", m
);
2243 cnttype
= hfield1("content-type", m
);
2245 if ((ip
= setinput(&mb
, m
, NEED_BODY
)) == NULL
)
2250 #define _X (sizeof("application/") -1)
2251 #define _Y(X) X, sizeof(X) -1
2252 if (cnttype
&& is_asccaseprefix("application/", cnttype
) &&
2253 (!ascncasecmp(cnttype
+ _X
, _Y("pkcs7-mime")) ||
2254 !ascncasecmp(cnttype
+ _X
, _Y("x-pkcs7-mime")))) {
2257 if ((x
= smime_decrypt(m
, to
, cc
, 1)) == NULL
)
2259 if (x
!= (struct message
*)-1) {
2266 if ((fp
= Ftmp(NULL
, "smimecert", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==
2268 n_perr(_("tempfile"), 0);
2272 while (size
-- > 0) {
2279 if ((fb
= BIO_new_fp(fp
, BIO_NOCLOSE
)) == NULL
) {
2280 ssl_gen_err("Error creating BIO object for message %d", n
);
2285 if ((pkcs7
= SMIME_read_PKCS7(fb
, &pb
)) == NULL
) {
2286 ssl_gen_err(_("Error reading PKCS#7 object for message %d"), n
);
2294 certs
= PKCS7_get0_signers(pkcs7
, chain
, 0);
2295 if (certs
== NULL
) {
2296 n_err(_("No certificates found in message %d\n"), n
);
2300 for (i
= 0; i
< sk_X509_num(certs
); ++i
) {
2301 cert
= sk_X509_value(certs
, i
);
2302 if (X509_print_fp(op
, cert
) == 0 || PEM_write_X509(op
, cert
) == 0) {
2303 ssl_gen_err(_("Error writing certificate %d from message %d"),
2315 #endif /* HAVE_XSSL */