2 Copyright (C) 2000 Free Software Foundation, Inc.
3 Contributed by Christian Fraenkel.
5 This file is part of GNU Wget.
7 GNU Wget is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GNU Wget is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Wget; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
36 #include <openssl/bio.h>
37 #include <openssl/crypto.h>
38 #include <openssl/x509.h>
39 #include <openssl/ssl.h>
40 #include <openssl/err.h>
41 #include <openssl/pem.h>
42 #include <openssl/rand.h>
53 static int verify_callback PARAMS ((int, X509_STORE_CTX *));
58 /* It is likely that older versions of OpenSSL will fail on
59 non-Linux machines because this code is unable to seed the PRNG
60 on older versions of the library. */
62 #if SSLEAY_VERSION_NUMBER >= 0x00905100
66 /* First, seed from a file specified by the user. This will be
67 $RANDFILE, if set, or ~/.rnd. */
68 RAND_file_name (rand_file, sizeof (rand_file));
70 /* Seed at most 16k (value borrowed from curl) from random file. */
71 RAND_load_file (rand_file, 16384);
76 /* Get random data from EGD if opt.sslegdsock was set. */
77 if (opt.sslegdsock && *opt.sslegdsock)
78 RAND_egd (opt.sslegdsock);
84 /* Under Windows, we can try to seed the PRNG using screen content.
85 This may or may not work, depending on whether we'll calling Wget
93 /* Still not enough randomness, presumably because neither random
94 file nor EGD have been available. Use the stupidest possible
95 method -- seed OpenSSL's PRNG with the system's PRNG. This is
96 insecure in the cryptographic sense, but people who care about
97 security will use /dev/random or their own source of randomness
100 while (RAND_status () == 0 && maxrand-- > 0)
102 unsigned char rnd = random_number (256);
103 RAND_seed (&rnd, sizeof (rnd));
106 if (RAND_status () == 0)
108 logprintf (LOG_NOTQUIET,
109 _("Could not seed OpenSSL PRNG; disabling SSL.\n"));
110 scheme_disable (SCHEME_HTTPS);
112 #endif /* SSLEAY_VERSION_NUMBER >= 0x00905100 */
115 /* Creates a SSL Context and sets some defaults for it */
117 init_ssl (SSL_CTX **ctx)
119 SSL_METHOD *meth = NULL;
120 int verify = SSL_VERIFY_NONE;
122 SSL_load_error_strings ();
123 SSLeay_add_all_algorithms ();
124 SSLeay_add_ssl_algorithms ();
125 meth = SSLv23_client_method ();
126 *ctx = SSL_CTX_new (meth);
127 SSL_CTX_set_verify (*ctx, verify, verify_callback);
129 return SSLERRCTXCREATE;
132 if (SSL_CTX_use_certificate_file (*ctx, opt.sslcertfile,
133 SSL_FILETYPE_PEM) <= 0)
134 return SSLERRCERTFILE;
135 if (opt.sslcertkey == NULL)
136 opt.sslcertkey=opt.sslcertfile;
137 if (SSL_CTX_use_PrivateKey_file (*ctx, opt.sslcertkey,
138 SSL_FILETYPE_PEM) <= 0)
139 return SSLERRCERTKEY;
141 return 0; /* Succeded */
144 /* Sets up a SSL structure and performs the handshake on fd
145 Returns 0 if everything went right
146 Returns 1 if something went wrong ----- TODO: More exit codes
149 connect_ssl (SSL **con, SSL_CTX *ctx, int fd)
151 *con = (SSL *)SSL_new (ctx);
152 SSL_set_fd (*con, fd);
153 SSL_set_connect_state (*con);
155 if ((*con)->state != SSL_ST_OK)
157 /*while((SSLerror=ERR_get_error())!=0)
158 printf("%s\n", ERR_error_string(SSLerror,NULL));*/
164 shutdown_ssl (SSL* con)
172 free_ssl_ctx (SSL_CTX * ctx)
178 verify_callback (int ok, X509_STORE_CTX *ctx)
181 s = X509_NAME_oneline (X509_get_subject_name (ctx->current_cert), buf, 256);
183 switch (ctx->error) {
184 case X509_V_ERR_CERT_NOT_YET_VALID:
185 case X509_V_ERR_CERT_HAS_EXPIRED:
186 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
193 /* pass all ssl errors to DEBUGP
194 returns the number of printed errors */
196 ssl_printerrors (void)
199 unsigned long curerr = 0;
201 memset(errbuff, 0, sizeof(errbuff));
202 for (curerr = ERR_get_error (); curerr; curerr = ERR_get_error ())
204 DEBUGP (("OpenSSL: %s\n", ERR_error_string (curerr, errbuff)));
210 /* SSL version of iread. Only exchanged read for SSL_read
211 Read at most LEN bytes from FD, storing them to BUF. This is
212 virtually the same as read(), but takes care of EINTR braindamage
213 and uses select() to timeout the stale connections (a connection is
214 stale if more than OPT.TIMEOUT time is spent in select() or
217 ssl_iread (SSL *con, char *buf, int len)
221 BIO_get_fd (con->rbio, &fd);
225 if (opt.timeout && !SSL_pending (con))
229 res = select_fd (fd, opt.timeout, 0);
231 while (res == -1 && errno == EINTR);
234 /* Set errno to ETIMEDOUT on timeout. */
236 /* #### Potentially evil! */
242 res = SSL_read (con, buf, len);
244 while (res == -1 && errno == EINTR);
249 /* SSL version of iwrite. Only exchanged write for SSL_write
250 Write LEN bytes from BUF to FD. This is similar to iread(), but
251 doesn't bother with select(). Unlike iread(), it makes sure that
252 all of BUF is actually written to FD, so callers needn't bother
253 with checking that the return value equals to LEN. Instead, you
254 should simply check for -1. */
256 ssl_iwrite (SSL *con, char *buf, int len)
260 BIO_get_fd (con->rbio, &fd);
261 /* `write' may write less than LEN bytes, thus the outward loop
262 keeps trying it until all was written, or an error occurred. The
263 inner loop is reserved for the usual EINTR f*kage, and the
264 innermost loop deals with the same during select(). */
274 res = select_fd (fd, opt.timeout, 1);
276 while (res == -1 && errno == EINTR);
279 /* Set errno to ETIMEDOUT on timeout. */
281 /* #### Potentially evil! */
287 res = SSL_write (con, buf, len);
289 while (res == -1 && errno == EINTR);
297 #endif /* HAVE_SSL */