]> sjero.net Git - wget/blob - src/gen_sslfunc.c
[svn] Doc fix.
[wget] / src / gen_sslfunc.c
1 /* SSL support.
2    Copyright (C) 2000 Free Software Foundation, Inc.
3    Contributed by Christian Fraenkel.
4
5 This file is part of GNU Wget.
6
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.
11
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.
16
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.  */
20
21 #include <config.h>
22
23 #ifdef HAVE_SSL
24
25 #include <assert.h>
26 #include <errno.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #ifdef HAVE_STRING_H
31 # include <string.h>
32 #else
33 # include <strings.h>
34 #endif
35
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>
43
44 #include "wget.h"
45 #include "connect.h"
46
47 #ifndef errno
48 extern int errno;
49 #endif
50
51 static int verify_callback PARAMS ((int, X509_STORE_CTX *));
52
53 static void
54 ssl_init_prng (void)
55 {
56 #if SSLEAY_VERSION_NUMBER >= 0x00905100
57   if (RAND_status () == 0)
58     {
59       char rand_file[256];
60       time_t t;
61       long l,seed;
62
63       t = time(NULL);
64       /* gets random data from egd if opt.sslegdsock was set */
65       if (opt.sslegdsock != NULL)
66         RAND_egd(opt.sslegdsock);
67       /* gets the file ~/.rnd or $RANDFILE if set */
68       RAND_file_name(rand_file, 256);
69       if (rand_file != NULL)
70         {
71           /* Seed as much as 1024 bytes from RAND_file_name */
72           RAND_load_file(rand_file, 1024);
73         }
74       /* Seed in time (mod_ssl does this) */
75       RAND_seed((unsigned char *)&t, sizeof(time_t));
76       /* Initialize system's random number generator */
77       RAND_bytes((unsigned char *)&seed, sizeof(long));
78 #ifndef WINDOWS
79       srand48(seed);
80       while (RAND_status () == 0)
81         {
82           /* Repeatedly seed the PRNG using the system's random number
83              generator until it has been seeded with enough data.  */
84           l = lrand48();
85           RAND_seed((unsigned char *)&l, sizeof(long));
86         }
87 #else /* WINDOWS */
88       RAND_screen();
89       if (RAND_status() == 0)
90         /* Here we should probably disable the whole ssl protocol ? HEH */
91         DEBUGP (("SSL random data generator not seeded correctly, %i",RAND_status()));
92 #endif /* WINDOWS */
93       if (rand_file != NULL)
94         {
95           /* Write a rand_file */
96           RAND_write_file(rand_file);
97         }
98     }
99 #endif /* SSLEAY_VERSION_NUMBER >= 0x00905100 */
100   return;
101 }
102
103
104 /* Creates a SSL Context and sets some defaults for it */
105 uerr_t
106 init_ssl (SSL_CTX **ctx)
107 {
108   SSL_METHOD *meth = NULL;
109   int verify = SSL_VERIFY_NONE;
110   SSL_library_init ();
111   SSL_load_error_strings ();
112   SSLeay_add_all_algorithms ();
113   SSLeay_add_ssl_algorithms ();
114   meth = SSLv23_client_method ();
115   *ctx = SSL_CTX_new (meth);
116   SSL_CTX_set_verify (*ctx, verify, verify_callback);
117   if (*ctx == NULL) return SSLERRCTXCREATE;
118   if (opt.sslcertfile)
119     {
120       if (SSL_CTX_use_certificate_file (*ctx, opt.sslcertfile,
121                                         SSL_FILETYPE_PEM) <= 0)
122         return SSLERRCERTFILE;
123       if (opt.sslcertkey == NULL) 
124         opt.sslcertkey=opt.sslcertfile;
125       if (SSL_CTX_use_PrivateKey_file (*ctx, opt.sslcertkey,
126                                        SSL_FILETYPE_PEM) <= 0)
127         return SSLERRCERTKEY;
128   }
129   ssl_init_prng();
130   return 0; /* Succeded */
131 }
132
133 /* Sets up a SSL structure and performs the handshake on fd 
134    Returns 0 if everything went right
135    Returns 1 if something went wrong ----- TODO: More exit codes
136 */
137 int
138 connect_ssl (SSL **con, SSL_CTX *ctx, int fd) 
139 {
140   *con = (SSL *)SSL_new (ctx);
141   SSL_set_fd (*con, fd);
142   SSL_set_connect_state (*con); 
143   SSL_connect (*con);  
144   if ((*con)->state != SSL_ST_OK)
145     return 1;
146   /*while((SSLerror=ERR_get_error())!=0)
147     printf("%s\n", ERR_error_string(SSLerror,NULL));*/
148
149   return 0;
150 }
151
152 void
153 shutdown_ssl (SSL* con)
154 {
155   SSL_shutdown (con);
156   if (con != NULL)
157     SSL_free (con);
158 }
159
160 void
161 free_ssl_ctx (SSL_CTX * ctx)
162 {
163   SSL_CTX_free (ctx);
164 }
165
166 int
167 verify_callback (int ok, X509_STORE_CTX *ctx)
168 {
169   char *s, buf[256];
170   s = X509_NAME_oneline (X509_get_subject_name (ctx->current_cert), buf, 256);
171   if (ok == 0) {
172     switch (ctx->error) {
173     case X509_V_ERR_CERT_NOT_YET_VALID:
174     case X509_V_ERR_CERT_HAS_EXPIRED:
175     case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
176       ok = 1;
177     }
178   }
179   return ok;
180 }
181
182 /* pass all ssl errors to DEBUGP
183    returns the number of printed errors */
184 int
185 ssl_printerrors (void) 
186 {
187   int ocerr = 0;
188   unsigned long curerr = 0;
189   char errbuff[1024];
190   memset(errbuff, 0, sizeof(errbuff));
191   for (curerr = ERR_get_error (); curerr; curerr = ERR_get_error ())
192     {
193       DEBUGP (("OpenSSL: %s\n", ERR_error_string (curerr, errbuff)));
194       ++ocerr;
195     }
196   return ocerr;
197 }
198
199 /* SSL version of iread. Only exchanged read for SSL_read 
200    Read at most LEN bytes from FD, storing them to BUF.  This is
201    virtually the same as read(), but takes care of EINTR braindamage
202    and uses select() to timeout the stale connections (a connection is
203    stale if more than OPT.TIMEOUT time is spent in select() or
204    read()).  */
205 int
206 ssl_iread (SSL *con, char *buf, int len)
207 {
208   int res;
209   int fd;
210   BIO_get_fd (con->rbio, &fd);
211   do
212     {
213 #ifdef HAVE_SELECT
214       if (opt.timeout)
215         {
216           
217           do
218             {
219               res = select_fd (fd, opt.timeout, 0);
220             }
221           while (res == -1 && errno == EINTR);
222           if (res <= 0)
223             {
224               /* Set errno to ETIMEDOUT on timeout.  */
225               if (res == 0)
226                 /* #### Potentially evil!  */
227                 errno = ETIMEDOUT;
228               return -1;
229             }
230         }
231 #endif
232       res = SSL_read (con, buf, len);
233     }
234   while (res == -1 && errno == EINTR);
235
236   return res;
237 }
238
239 /* SSL version of iwrite. Only exchanged write for SSL_write 
240    Write LEN bytes from BUF to FD.  This is similar to iread(), but
241    doesn't bother with select().  Unlike iread(), it makes sure that
242    all of BUF is actually written to FD, so callers needn't bother
243    with checking that the return value equals to LEN.  Instead, you
244    should simply check for -1.  */
245 int
246 ssl_iwrite (SSL *con, char *buf, int len)
247 {
248   int res = 0;
249   int fd;
250   BIO_get_fd (con->rbio, &fd);
251   /* `write' may write less than LEN bytes, thus the outward loop
252      keeps trying it until all was written, or an error occurred.  The
253      inner loop is reserved for the usual EINTR f*kage, and the
254      innermost loop deals with the same during select().  */
255   while (len > 0)
256     {
257       do
258         {
259 #ifdef HAVE_SELECT
260           if (opt.timeout)
261             {
262               do
263                 {
264                   res = select_fd (fd, opt.timeout, 1);
265                 }
266               while (res == -1 && errno == EINTR);
267               if (res <= 0)
268                 {
269                   /* Set errno to ETIMEDOUT on timeout.  */
270                   if (res == 0)
271                     /* #### Potentially evil!  */
272                     errno = ETIMEDOUT;
273                   return -1;
274                 }
275             }
276 #endif
277           res = SSL_write (con, buf, len);
278         }
279       while (res == -1 && errno == EINTR);
280       if (res <= 0)
281         break;
282       buf += res;
283       len -= res;
284     }
285   return res;
286 }
287 #endif /* HAVE_SSL */