]> sjero.net Git - wget/blob - src/gen_sslfunc.c
5dc8cca9e6a2f42d55d680d6b8090859c8fc8f91
[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
22 #ifdef HAVE_SSL
23
24 #include <assert.h>
25 #include <errno.h>
26
27 #include <openssl/bio.h>
28 #include <openssl/crypto.h>
29 #include <openssl/x509.h>
30 #include <openssl/ssl.h>
31 #include <openssl/err.h>
32 #include <openssl/pem.h>
33
34 #include "wget.h"
35 #include "connect.h"
36
37 #ifndef errno
38 extern int errno;
39 #endif
40
41 static int verify_callback PARAMS ((int, X509_STORE_CTX *));
42
43 /* Creates a SSL Context and sets some defaults for it */
44 uerr_t
45 init_ssl (SSL_CTX **ctx)
46 {
47   SSL_METHOD *meth = NULL;
48   int verify = SSL_VERIFY_NONE;
49   SSL_library_init ();
50   SSL_load_error_strings ();
51   SSLeay_add_all_algorithms ();
52   SSLeay_add_ssl_algorithms ();
53   meth = SSLv23_client_method ();
54   *ctx = SSL_CTX_new (meth);
55   SSL_CTX_set_verify (*ctx, verify, verify_callback);
56   if (*ctx == NULL) return SSLERRCTXCREATE;
57   if (opt.sslcertfile)
58     {
59       if (SSL_CTX_use_certificate_file (*ctx, opt.sslcertfile,
60                                         SSL_FILETYPE_PEM) <= 0)
61         return SSLERRCERTFILE;
62       if (opt.sslcertkey == NULL) 
63         opt.sslcertkey=opt.sslcertfile;
64       if (SSL_CTX_use_PrivateKey_file (*ctx, opt.sslcertkey,
65                                        SSL_FILETYPE_PEM) <= 0)
66         return SSLERRCERTKEY;
67   }
68   return 0; /* Succeded */
69 }
70
71 /* Sets up a SSL structure and performs the handshake on fd 
72    Returns 0 if everything went right
73    Returns 1 if something went wrong ----- TODO: More exit codes
74 */
75 int
76 connect_ssl (SSL **con, SSL_CTX *ctx, int fd) 
77 {
78   *con = (SSL *)SSL_new (ctx);
79   SSL_set_fd (*con, fd);
80   SSL_set_connect_state (*con); 
81   SSL_connect (*con);  
82   if ((*con)->state != SSL_ST_OK)
83     return 1;
84   return 0;
85 }
86
87 void
88 shutdown_ssl (SSL* con)
89 {
90   SSL_shutdown (con);
91   if (con != NULL)
92     SSL_free (con);
93 }
94
95 void
96 free_ssl_ctx (SSL_CTX * ctx)
97 {
98   SSL_CTX_free (ctx);
99 }
100
101 int
102 verify_callback (int ok, X509_STORE_CTX *ctx)
103 {
104   char *s, buf[256];
105   s = X509_NAME_oneline (X509_get_subject_name (ctx->current_cert), buf, 256);
106   if (ok == 0) {
107     switch (ctx->error) {
108     case X509_V_ERR_CERT_NOT_YET_VALID:
109     case X509_V_ERR_CERT_HAS_EXPIRED:
110     case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
111       ok = 1;
112     }
113   }
114   return ok;
115 }
116
117 /* pass all ssl errors to DEBUGP
118    returns the number of printed errors */
119 int
120 ssl_printerrors (void) 
121 {
122   int ocerr = 0;
123   unsigned long curerr = 0;
124   char errbuff[1024];
125   memset(errbuff, 0, sizeof(errbuff));
126   for (curerr = ERR_get_error (); curerr; curerr = ERR_get_error ())
127     {
128       DEBUGP (("OpenSSL: %s\n", ERR_error_string (curerr, errbuff)));
129       ++ocerr;
130     }
131   return ocerr;
132 }
133
134 /* SSL version of iread. Only exchanged read for SSL_read 
135    Read at most LEN bytes from FD, storing them to BUF.  This is
136    virtually the same as read(), but takes care of EINTR braindamage
137    and uses select() to timeout the stale connections (a connection is
138    stale if more than OPT.TIMEOUT time is spent in select() or
139    read()).  */
140 int
141 ssl_iread (SSL *con, char *buf, int len)
142 {
143   int res;
144   int fd;
145   BIO_get_fd (con->rbio, &fd);
146   do
147     {
148 #ifdef HAVE_SELECT
149       if (opt.timeout)
150         {
151           
152           do
153             {
154               res = select_fd (fd, opt.timeout, 0);
155             }
156           while (res == -1 && errno == EINTR);
157           if (res <= 0)
158             {
159               /* Set errno to ETIMEDOUT on timeout.  */
160               if (res == 0)
161                 /* #### Potentially evil!  */
162                 errno = ETIMEDOUT;
163               return -1;
164             }
165         }
166 #endif
167       res = SSL_read (con, buf, len);
168     }
169   while (res == -1 && errno == EINTR);
170
171   return res;
172 }
173
174 /* SSL version of iwrite. Only exchanged write for SSL_write 
175    Write LEN bytes from BUF to FD.  This is similar to iread(), but
176    doesn't bother with select().  Unlike iread(), it makes sure that
177    all of BUF is actually written to FD, so callers needn't bother
178    with checking that the return value equals to LEN.  Instead, you
179    should simply check for -1.  */
180 int
181 ssl_iwrite (SSL *con, char *buf, int len)
182 {
183   int res = 0;
184   int fd;
185   BIO_get_fd (con->rbio, &fd);
186   /* `write' may write less than LEN bytes, thus the outward loop
187      keeps trying it until all was written, or an error occurred.  The
188      inner loop is reserved for the usual EINTR f*kage, and the
189      innermost loop deals with the same during select().  */
190   while (len > 0)
191     {
192       do
193         {
194 #ifdef HAVE_SELECT
195           if (opt.timeout)
196             {
197               do
198                 {
199                   res = select_fd (fd, opt.timeout, 1);
200                 }
201               while (res == -1 && errno == EINTR);
202               if (res <= 0)
203                 {
204                   /* Set errno to ETIMEDOUT on timeout.  */
205                   if (res == 0)
206                     /* #### Potentially evil!  */
207                     errno = ETIMEDOUT;
208                   return -1;
209                 }
210             }
211 #endif
212           res = SSL_write (con, buf, len);
213         }
214       while (res == -1 && errno == EINTR);
215       if (res <= 0)
216         break;
217       buf += res;
218       len -= res;
219     }
220   return res;
221 }
222 #endif /* HAVE_SSL */