]> sjero.net Git - wget/blob - src/gen_sslfunc.c
[svn] Cosmetic cleanup of SSL stuff.
[wget] / src / gen_sslfunc.c
1 /* SSL support.
2    Copyright (C) 2000 Christian Fraenkel.
3
4 This file is part of Wget.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 #include <config.h>
21 #ifdef HAVE_SSL
22 #include <openssl/bio.h>
23 #include <openssl/crypto.h>
24 #include <openssl/x509.h>
25 #include <openssl/ssl.h>
26 #include <openssl/err.h>
27 #include <openssl/pem.h>
28 #include <assert.h>
29 #define SSL_ERR_CTX_CREATION -2
30 #include <sys/time.h>
31 #include "wget.h"
32 #include "connect.h"
33
34 /* #### Shouldn't this be static?  --hniksic */
35 int verify_callback PARAMS ((int, X509_STORE_CTX *));
36
37 /* Creates a SSL Context and sets some defaults for it */
38 int
39 init_ssl (SSL_CTX **ctx)
40 {
41   SSL_METHOD *meth = NULL;
42   int verify = SSL_VERIFY_NONE;
43   SSL_library_init ();
44   SSL_load_error_strings ();
45   SSLeay_add_all_algorithms ();
46   SSLeay_add_ssl_algorithms ();
47   meth = SSLv23_client_method ();
48   *ctx = SSL_CTX_new (meth);
49   SSL_CTX_set_verify (*ctx, verify, verify_callback);
50   if (*ctx == NULL) return SSL_ERR_CTX_CREATION;
51   return 0; /* Succeded */
52 }
53
54 /* Sets up a SSL structure and performs the handshake on fd 
55    Returns 0 if everything went right
56    Returns 1 if something went wrong ----- TODO: More exit codes
57 */
58 int
59 connect_ssl (SSL **con, SSL_CTX *ctx, int fd) 
60 {
61   *con = (SSL *)SSL_new (ctx);
62   SSL_set_fd (*con, fd);
63   SSL_set_connect_state (*con); 
64   SSL_connect (*con);  
65   if ((*con)->state != SSL_ST_OK)
66     return 1;
67   return 0;
68 }
69
70 void
71 shutdown_ssl (SSL* con)
72 {
73   SSL_shutdown (con);
74   if (con != NULL)
75     SSL_free (con);
76 }
77
78 void
79 free_ssl_ctx (SSL_CTX * ctx)
80 {
81   SSL_CTX_free (ctx);
82 }
83
84 int
85 verify_callback (int ok, X509_STORE_CTX *ctx)
86 {
87   char *s, buf[256];
88   s = X509_NAME_oneline (X509_get_subject_name (ctx->current_cert), buf, 256);
89   if (ok == 0) {
90     switch (ctx->error) {
91     case X509_V_ERR_CERT_NOT_YET_VALID:
92     case X509_V_ERR_CERT_HAS_EXPIRED:
93     case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
94       ok = 1;
95     }
96   }
97   return ok;
98 }
99
100 /* SSL version of iread. Only exchanged read for SSL_read 
101    Read at most LEN bytes from FD, storing them to BUF.  This is
102    virtually the same as read(), but takes care of EINTR braindamage
103    and uses select() to timeout the stale connections (a connection is
104    stale if more than OPT.TIMEOUT time is spent in select() or
105    read()).  */
106 int
107 ssl_iread (SSL *con, char *buf, int len)
108 {
109   int res;
110   int fd;
111   BIO_get_fd (con->rbio, &fd);
112   do
113     {
114 #ifdef HAVE_SELECT
115       if (opt.timeout)
116         {
117           
118           do
119             {
120               res = select_fd (fd, opt.timeout, 0);
121             }
122           while (res == -1 && errno == EINTR);
123           if (res <= 0)
124             {
125               /* Set errno to ETIMEDOUT on timeout.  */
126               if (res == 0)
127                 /* #### Potentially evil!  */
128                 errno = ETIMEDOUT;
129               return -1;
130             }
131         }
132 #endif
133       res = SSL_read (con, buf, len);
134     }
135   while (res == -1 && errno == EINTR);
136
137   return res;
138 }
139
140 /* SSL version of iwrite. Only exchanged write for SSL_write 
141    Write LEN bytes from BUF to FD.  This is similar to iread(), but
142    doesn't bother with select().  Unlike iread(), it makes sure that
143    all of BUF is actually written to FD, so callers needn't bother
144    with checking that the return value equals to LEN.  Instead, you
145    should simply check for -1.  */
146 int
147 ssl_iwrite (SSL *con, char *buf, int len)
148 {
149   int res = 0;
150   int fd;
151   BIO_get_fd (con->rbio, &fd);
152   /* `write' may write less than LEN bytes, thus the outward loop
153      keeps trying it until all was written, or an error occurred.  The
154      inner loop is reserved for the usual EINTR f*kage, and the
155      innermost loop deals with the same during select().  */
156   while (len > 0)
157     {
158       do
159         {
160 #ifdef HAVE_SELECT
161           if (opt.timeout)
162             {
163               do
164                 {
165                   res = select_fd (fd, opt.timeout, 1);
166                 }
167               while (res == -1 && errno == EINTR);
168               if (res <= 0)
169                 {
170                   /* Set errno to ETIMEDOUT on timeout.  */
171                   if (res == 0)
172                     /* #### Potentially evil!  */
173                     errno = ETIMEDOUT;
174                   return -1;
175                 }
176             }
177 #endif
178           res = SSL_write (con, buf, len);
179         }
180       while (res == -1 && errno == EINTR);
181       if (res <= 0)
182         break;
183       buf += res;
184       len -= res;
185     }
186   return res;
187 }
188 #endif /* HAVE_SSL */