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