1 /* SSL support - wrappers for SSL routines */
8 #include <openssl/ssl.h>
9 #include <openssl/rand.h>
11 #elif defined(CONFIG_NSS_COMPAT_OSSL)
12 #include <nss_compat_ossl/nss_compat_ossl.h>
14 #elif defined(CONFIG_GNUTLS)
16 #include <gnutls/gnutls.h>
17 #include <gnutls/x509.h>
19 #error "Huh?! You have SSL enabled, but not OPENSSL nor GNUTLS!! And then you want exactly *what* from me?"
28 #include "intl/gettext/libintl.h"
29 #include "main/module.h"
30 #include "network/connection.h"
31 #include "network/socket.h"
32 #include "network/ssl/ssl.h"
33 #include "util/conv.h"
34 #include "util/error.h"
35 #include "util/string.h"
36 #include "util/random.h"
39 /* FIXME: As you can see, SSL is currently implemented in very, erm,
40 * decentralized manner. */
45 #define PATH_MAX 256 /* according to my /usr/include/bits/posix1_lim.h */
48 static SSL_CTX
*context
= NULL
;
49 int socket_SSL_ex_data_idx
= -1;
51 /** Prevent SSL_dup() if the SSL is associated with struct socket.
52 * We cannot copy struct socket and it doesn't have a reference count
55 socket_SSL_ex_data_dup(CRYPTO_EX_DATA
*to
, CRYPTO_EX_DATA
*from
,
56 void *from_d
, int idx
, long argl
, void *argp
)
58 /* The documentation of from_d in RSA_get_ex_new_index(3)
59 * is a bit unclear. The caller does something like:
61 * void *data = CRYPTO_get_ex_data(from, idx);
62 * socket_SSL_dup(to, from, &data, idx, argl, argp);
63 * CRYPTO_set_ex_data(to, idx, data);
65 * i.e., from_d always points to a pointer, even though
66 * it is just a void * in the prototype. */
67 struct socket
*socket
= *(void **) from_d
;
69 assert(idx
== socket_SSL_ex_data_idx
);
70 if_assert_failed
return 0;
73 return 0; /* prevent SSL_dup() */
75 return 1; /* allow SSL_dup() */
79 init_openssl(struct module
*module
)
81 unsigned char f_randfile
[PATH_MAX
];
83 /* In a nutshell, on OS's without a /dev/urandom, the OpenSSL library
84 * cannot initialize the PRNG and so every attempt to use SSL fails.
85 * It's actually an OpenSSL FAQ, and according to them, it's up to the
86 * application coders to seed the RNG. -- William Yodlowsky */
87 if (RAND_egd(RAND_file_name(f_randfile
, sizeof(f_randfile
))) < 0) {
88 /* Not an EGD, so read and write to it */
89 if (RAND_load_file(f_randfile
, -1))
90 RAND_write_file(f_randfile
);
93 SSLeay_add_ssl_algorithms();
94 context
= SSL_CTX_new(SSLv23_client_method());
95 SSL_CTX_set_options(context
, SSL_OP_ALL
);
96 SSL_CTX_set_default_verify_paths(context
);
97 socket_SSL_ex_data_idx
= SSL_get_ex_new_index(0, NULL
,
99 socket_SSL_ex_data_dup
,
104 done_openssl(struct module
*module
)
106 if (context
) SSL_CTX_free(context
);
107 /* There is no function that undoes SSL_get_ex_new_index. */
110 static struct option_info openssl_options
[] = {
111 INIT_OPT_BOOL("connection.ssl", N_("Verify certificates"),
113 N_("Verify the peer's SSL certificate. Note that this "
114 "needs extensive configuration of OpenSSL by the user.")),
116 INIT_OPT_TREE("connection.ssl", N_("Client Certificates"),
117 "client_cert", OPT_SORT
,
118 N_("X509 client certificate options.")),
120 INIT_OPT_BOOL("connection.ssl.client_cert", N_("Enable"),
122 N_("Enable or not the sending of X509 client certificates "
123 "to servers which request them.")),
125 #ifdef CONFIG_NSS_COMPAT_OSSL
126 INIT_OPT_STRING("connection.ssl.client_cert", N_("Certificate nickname"),
128 N_("The nickname of the client certificate stored in NSS "
129 "database. If this value is unset, the nickname from "
130 "the X509_CLIENT_CERT variable is used instead. If you "
131 "have a PKCS#12 file containing client certificate, you "
132 "can import it into your NSS database with:\n"
134 "$ pk12util -i mycert.p12 -d /path/to/database\n"
136 "The NSS database location can be changed by SSL_DIR "
137 "environment variable. The database can be also shared "
138 "with Mozilla browsers.")),
140 INIT_OPT_STRING("connection.ssl.client_cert", N_("Certificate File"),
142 N_("The location of a file containing the client certificate "
143 "and unencrypted private key in PEM format. If unset, the "
144 "file pointed to by the X509_CLIENT_CERT variable is used "
151 static struct module openssl_module
= struct_module(
152 /* name: */ "OpenSSL",
153 /* options: */ openssl_options
,
155 /* submodules: */ NULL
,
157 /* init: */ init_openssl
,
158 /* done: */ done_openssl
161 #elif defined(CONFIG_GNUTLS)
163 static gnutls_anon_client_credentials_t anon_cred
= NULL
;
164 static gnutls_certificate_credentials_t xcred
= NULL
;
167 const static int kx_priority
[16] = {
168 GNUTLS_KX_RSA
, GNUTLS_KX_DHE_DSS
, GNUTLS_KX_DHE_RSA
, GNUTLS_KX_SRP
,
169 /* Do not use anonymous authentication, unless you know what that means */
170 GNUTLS_KX_ANON_DH
, GNUTLS_KX_RSA_EXPORT
, 0
172 const static int cipher_priority
[16] = {
173 GNUTLS_CIPHER_RIJNDAEL_128_CBC
, GNUTLS_CIPHER_ARCFOUR_128
,
174 GNUTLS_CIPHER_3DES_CBC
, GNUTLS_CIPHER_AES_256_CBC
, GNUTLS_CIPHER_ARCFOUR_40
, 0
176 const static int cert_type_priority
[16] = { GNUTLS_CRT_X509
, GNUTLS_CRT_OPENPGP
, 0 };
180 init_gnutls(struct module
*module
)
182 int ret
= gnutls_global_init();
183 unsigned char *ca_file
= get_opt_str("connection.ssl.trusted_ca_file",
187 INTERNAL("GNUTLS init failed: %s", gnutls_strerror(ret
));
189 ret
= gnutls_anon_allocate_client_credentials(&anon_cred
);
191 INTERNAL("GNUTLS anon credentials alloc failed: %s",
192 gnutls_strerror(ret
));
194 ret
= gnutls_certificate_allocate_credentials(&xcred
);
196 INTERNAL("GNUTLS X509 credentials alloc failed: %s",
197 gnutls_strerror(ret
));
198 /* Here, we should load certificate files etc. */
200 /* FIXME: check returned values. --witekfl */
201 gnutls_certificate_set_x509_trust_file(xcred
, ca_file
,
202 GNUTLS_X509_FMT_PEM
);
204 gnutls_certificate_set_verify_flags(xcred
,
205 GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT
);
211 done_gnutls(struct module
*module
)
213 if (xcred
) gnutls_certificate_free_credentials(xcred
);
214 if (anon_cred
) gnutls_anon_free_client_credentials(anon_cred
);
215 gnutls_global_deinit();
218 static struct option_info gnutls_options
[] = {
219 INIT_OPT_BOOL("connection.ssl", N_("Verify certificates"),
221 N_("Verify the peer's SSL certificate. If you enable "
222 "this, set also \"Trusted CA file\".")),
224 /* The default value of the following option points to a file
225 * generated by the ca-certificates Debian package. Don't use
226 * CONFDIR here: if someone installs ELinks in $HOME and wants
227 * to have a user-specific trust list, he or she can just
228 * change the file name via the option manager. Distributors
229 * of binary packages should of course change the default to
230 * suit their systems.
231 * TODO: If the file name is relative, look in elinks_home? */
232 INIT_OPT_STRING("connection.ssl", N_("Trusted CA file"),
233 "trusted_ca_file", 0, "/etc/ssl/certs/ca-certificates.crt",
234 N_("The location of a file containing certificates of "
235 "trusted certification authorities in PEM format. "
236 "ELinks then trusts certificates issued by these CAs.\n"
238 "If you change this option or the file, you must "
239 "restart ELinks for the changes to take effect. "
240 "This option affects GnuTLS but not OpenSSL.")),
245 static struct module gnutls_module
= struct_module(
246 /* name: */ "GnuTLS",
247 /* options: */ gnutls_options
,
249 /* submodules: */ NULL
,
251 /* init: */ init_gnutls
,
252 /* done: */ done_gnutls
255 #endif /* USE_OPENSSL or CONFIG_GNUTLS */
257 static struct option_info ssl_options
[] = {
258 INIT_OPT_TREE("connection", N_("SSL"),
265 static struct module
*ssl_modules
[] = {
268 #elif defined(CONFIG_GNUTLS)
274 struct module ssl_module
= struct_module(
275 /* name: */ N_("SSL"),
276 /* options: */ ssl_options
,
278 /* submodules: */ ssl_modules
,
285 init_ssl_connection(struct socket
*socket
,
286 const unsigned char *server_name
)
289 socket
->ssl
= SSL_new(context
);
290 if (!socket
->ssl
) return S_SSL_ERROR
;
292 if (!SSL_set_ex_data(socket
->ssl
, socket_SSL_ex_data_idx
, socket
)) {
293 SSL_free(socket
->ssl
);
298 /* If the server name is known, pass it to OpenSSL.
300 * The return value of SSL_set_tlsext_host_name is not
301 * documented. The source shows that it returns 1 if
302 * successful; on error, it calls SSLerr and returns 0. */
304 && !SSL_set_tlsext_host_name(socket
->ssl
, server_name
)) {
305 SSL_free(socket
->ssl
);
310 #elif defined(CONFIG_GNUTLS)
311 ssl_t
*state
= mem_alloc(sizeof(ssl_t
));
313 if (!state
) return S_SSL_ERROR
;
315 if (gnutls_init(state
, GNUTLS_CLIENT
) < 0) {
316 /* DBG("sslinit %s", gnutls_strerror(ret)); */
321 if (gnutls_cred_set(*state
, GNUTLS_CRD_ANON
, anon_cred
) < 0) {
322 /* DBG("sslanoncred %s", gnutls_strerror(ret)); */
323 gnutls_deinit(*state
);
328 if (gnutls_cred_set(*state
, GNUTLS_CRD_CERTIFICATE
, xcred
) < 0) {
329 /* DBG("sslx509cred %s", gnutls_strerror(ret)); */
330 gnutls_deinit(*state
);
335 #ifdef HAVE_GNUTLS_PRIORITY_SET_DIRECT
336 /* Disable OpenPGP certificates because they are not widely
337 * used and ELinks does not yet support verifying them.
338 * Besides, in GnuTLS < 2.4.0, they require the gnutls-extra
339 * library, whose GPLv3+ is not compatible with GPLv2 of
342 * Disable TLS1.1 because https://bugzilla.novell.com/ does
343 * not reply to it and leaves the connection open so that
344 * ELinks does not detect an SSL error but rather times out.
345 * http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=528661#25
347 * There is another gnutls_priority_set_direct call elsewhere
348 * in ELinks. If you change the priorities here, please check
349 * whether that one needs to be changed as well. */
350 if (gnutls_priority_set_direct(*state
,
351 "NORMAL:-CTYPE-OPENPGP:-VERS-TLS1.1",
353 gnutls_deinit(*state
);
358 gnutls_set_default_priority(*state
);
361 /* Deprecated functions */
362 /* gnutls_handshake_set_private_extensions(*state, 1); */
363 gnutls_cipher_set_priority(*state
, cipher_priority
);
364 gnutls_kx_set_priority(*state
, kx_priority
);
365 /* gnutls_certificate_type_set_priority(*state, cert_type_priority); */
369 && gnutls_server_name_set(*state
, GNUTLS_NAME_DNS
, server_name
,
370 strlen(server_name
))) {
371 gnutls_deinit(*state
);
383 done_ssl_connection(struct socket
*socket
)
385 ssl_t
*ssl
= socket
->ssl
;
390 #elif defined(CONFIG_GNUTLS)
398 get_ssl_connection_cipher(struct socket
*socket
)
400 ssl_t
*ssl
= socket
->ssl
;
403 if (!init_string(&str
)) return NULL
;
406 add_format_to_string(&str
, "%ld-bit %s %s",
407 SSL_get_cipher_bits(ssl
, NULL
),
408 SSL_get_cipher_version(ssl
),
409 SSL_get_cipher_name(ssl
));
410 #elif defined(CONFIG_GNUTLS)
411 /* XXX: How to get other relevant parameters? */
412 add_format_to_string(&str
, "%s - %s - %s - %s - %s (compr: %s)",
413 gnutls_protocol_get_name(gnutls_protocol_get_version(*ssl
)),
414 gnutls_kx_get_name(gnutls_kx_get(*ssl
)),
415 gnutls_cipher_get_name(gnutls_cipher_get(*ssl
)),
416 gnutls_mac_get_name(gnutls_mac_get(*ssl
)),
417 gnutls_certificate_type_get_name(gnutls_certificate_type_get(*ssl
)),
418 gnutls_compression_get_name(gnutls_compression_get(*ssl
)));
424 /* When CONFIG_SSL is defined, this implementation replaces the one in
425 * src/util/random.c. */
427 random_nonce(unsigned char buf
[], size_t size
)
430 RAND_pseudo_bytes(buf
, size
);
431 #elif defined(CONFIG_GNUTLS)
432 gcry_create_nonce(buf
, size
);
434 # error unsupported SSL library