]> sjero.net Git - wget/blob - src/gen_sslfunc.c
[svn] New files.
[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 int verify_callback(int ok, X509_STORE_CTX *ctx);
34
35 /* Creates a SSL Context and sets some defaults for it */
36 int init_ssl(SSL_CTX **ctx)
37 {
38 SSL_METHOD *meth=NULL;
39 int verify=SSL_VERIFY_NONE;
40   SSL_library_init();
41   SSL_load_error_strings();
42   SSLeay_add_all_algorithms();
43   SSLeay_add_ssl_algorithms();
44   meth = SSLv23_client_method();
45   *ctx = SSL_CTX_new(meth);
46   SSL_CTX_set_verify(*ctx,verify,verify_callback);
47   if (*ctx==NULL) return SSL_ERR_CTX_CREATION;
48 return 0; /* Succeded */
49 }
50
51 /* Sets up a SSL structure and performs the handshake on fd 
52    Returns 0 if everything went right
53         Returns 1 if something went wrong ----- TODO: More exit codes
54 */
55 int connect_ssl (SSL **con, SSL_CTX *ctx, int fd) 
56 {
57   *con=(SSL *)SSL_new(ctx);
58   SSL_set_fd(*con,fd);
59   SSL_set_connect_state(*con); 
60   SSL_connect(*con);  
61   if ((*con)->state !=SSL_ST_OK)
62     return 1;
63 return 0;
64 }
65
66 void shutdown_ssl (SSL* con)
67 {
68   SSL_shutdown(con);
69   if (con != NULL) SSL_free(con);
70 }
71
72 void free_ssl_ctx (SSL_CTX * ctx) {
73   SSL_CTX_free(ctx);
74 }
75
76 int verify_callback(int ok, X509_STORE_CTX *ctx){
77 char *s,buf[256];
78   s=X509_NAME_oneline(X509_get_subject_name(ctx->current_cert),buf,256);
79   if (ok == 0) {
80     switch (ctx->error) {
81         case X509_V_ERR_CERT_NOT_YET_VALID:
82         case X509_V_ERR_CERT_HAS_EXPIRED:
83         case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
84                 ok=1;
85     }
86   }
87   return(ok);
88 }
89
90 /* SSL version of iread. Only exchanged read for SSL_read 
91    Read at most LEN bytes from FD, storing them to BUF.  This is
92    virtually the same as read(), but takes care of EINTR braindamage
93    and uses select() to timeout the stale connections (a connection is
94    stale if more than OPT.TIMEOUT time is spent in select() or
95    read()).  */
96 int
97 ssl_iread (SSL *con, char *buf, int len)
98 {
99   int res;
100   int fd;
101   BIO_get_fd(con->rbio,&fd);
102   do
103     {
104 #ifdef HAVE_SELECT
105       if (opt.timeout)
106         {
107           
108           do
109             {
110               res = select_fd (fd, opt.timeout, 0);
111             }
112           while (res == -1 && errno == EINTR);
113           if (res <= 0)
114             {
115               /* Set errno to ETIMEDOUT on timeout.  */
116               if (res == 0)
117                 /* #### Potentially evil!  */
118                 errno = ETIMEDOUT;
119               return -1;
120             }
121         }
122 #endif
123       res = SSL_read (con, buf, len);
124     }
125   while (res == -1 && errno == EINTR);
126
127   return res;
128 }
129
130 /* SSL version of iwrite. Only exchanged write for SSL_write 
131    Write LEN bytes from BUF to FD.  This is similar to iread(), but
132    doesn't bother with select().  Unlike iread(), it makes sure that
133    all of BUF is actually written to FD, so callers needn't bother
134    with checking that the return value equals to LEN.  Instead, you
135    should simply check for -1.  */
136 int
137 ssl_iwrite (SSL *con, char *buf, int len)
138 {
139   int res = 0;
140   int fd;
141   BIO_get_fd(con->rbio,&fd);
142   /* `write' may write less than LEN bytes, thus the outward loop
143      keeps trying it until all was written, or an error occurred.  The
144      inner loop is reserved for the usual EINTR f*kage, and the
145      innermost loop deals with the same during select().  */
146   while (len > 0)
147     {
148       do
149         {
150 #ifdef HAVE_SELECT
151           if (opt.timeout)
152             {
153               do
154                 {
155                   res = select_fd (fd, opt.timeout, 1);
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_write (con, buf, len);
169         }
170       while (res == -1 && errno == EINTR);
171       if (res <= 0)
172         break;
173       buf += res;
174       len -= res;
175     }
176   return res;
177 }
178 #endif /* HAVE_SSL */