GNU libmicrohttpd  0.9.29
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
io_openssl.c
Go to the documentation of this file.
1 /*
2  This file is part of libmicrospdy
3  Copyright (C) 2012 Andrey Uzunov
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
26 #include "platform.h"
27 #include "internal.h"
28 #include "session.h"
29 #include "io_openssl.h"
30 
31 
41 static int
42 spdyf_next_protos_advertised_cb (SSL *ssl, const unsigned char **out, unsigned int *outlen, void *arg)
43 {
44  (void)ssl;
45  (void)arg;
46  static unsigned char npn_spdy3[] = {0x06, // length of "spdy/3"
47  0x73,0x70,0x64,0x79,0x2f,0x33};// spdy/3
48 
49  *out = npn_spdy3;
50  *outlen = 7; // total length of npn_spdy3
51  return SSL_TLSEXT_ERR_OK;
52 }
53 
54 
55 void
57 {
58  //error strings are now not used by the lib
59  //SSL_load_error_strings();
60  //init libssl
61  SSL_library_init(); //always returns 1
62  //the table for looking up algos is not used now by the lib
63  //OpenSSL_add_all_algorithms();
64 }
65 
66 
67 void
69 {
70  //if SSL_load_error_strings was called
71  //ERR_free_strings();
72  //if OpenSSL_add_all_algorithms was called
73  //EVP_cleanup();
74 }
75 
76 
77 int
79 {
80  int options;
81  //create ssl context. TLSv1 used
82  if(NULL == (daemon->io_context = SSL_CTX_new(TLSv1_server_method())))
83  {
84  SPDYF_DEBUG("Couldn't create ssl context");
85  return SPDY_NO;
86  }
87  //set options for tls
88  //TODO DH is not enabled for easier debugging
89  //SSL_CTX_set_options(daemon->io_context, SSL_OP_SINGLE_DH_USE);
90 
91  //TODO here session tickets are disabled for easier debuging with
92  //wireshark when using Chrome
93  // SSL_OP_NO_COMPRESSION disables TLS compression to avoid CRIME attack
94  options = SSL_OP_NO_TICKET;
95 #ifdef SSL_OP_NO_COMPRESSION
96  options |= SSL_OP_NO_COMPRESSION;
97 #elif OPENSSL_VERSION_NUMBER >= 0x00908000L /* workaround for OpenSSL 0.9.8 */
98  sk_SSL_COMP_zero(SSL_COMP_get_compression_methods());
99 #endif
100 
101  SSL_CTX_set_options(daemon->io_context, options);
102  if(1 != SSL_CTX_use_certificate_file(daemon->io_context, daemon->certfile , SSL_FILETYPE_PEM))
103  {
104  SPDYF_DEBUG("Couldn't load the cert file");
105  SSL_CTX_free(daemon->io_context);
106  return SPDY_NO;
107  }
108  if(1 != SSL_CTX_use_PrivateKey_file(daemon->io_context, daemon->keyfile, SSL_FILETYPE_PEM))
109  {
110  SPDYF_DEBUG("Couldn't load the name file");
111  SSL_CTX_free(daemon->io_context);
112  return SPDY_NO;
113  }
114  SSL_CTX_set_next_protos_advertised_cb(daemon->io_context, &spdyf_next_protos_advertised_cb, NULL);
115  //TODO only RC4-SHA is used to make it easy to debug with wireshark
116  if (1 != SSL_CTX_set_cipher_list(daemon->io_context, "RC4-SHA"))
117  {
118  SPDYF_DEBUG("Couldn't set the desired cipher list");
119  SSL_CTX_free(daemon->io_context);
120  return SPDY_NO;
121  }
122 
123  return SPDY_YES;
124 }
125 
126 
127 void
129 {
130  SSL_CTX_free(daemon->io_context);
131 }
132 
133 
134 int
136 {
137  int ret;
138 
139  if(NULL == (session->io_context = SSL_new(session->daemon->io_context)))
140  {
141  SPDYF_DEBUG("Couldn't create ssl structure");
142  return SPDY_NO;
143  }
144  if(1 != (ret = SSL_set_fd(session->io_context, session->socket_fd)))
145  {
146  SPDYF_DEBUG("SSL_set_fd %i",ret);
147  SSL_free(session->io_context);
148  session->io_context = NULL;
149  return SPDY_NO;
150  }
151 
152  //for non-blocking I/O SSL_accept may return -1
153  //and this function won't work
154  if(1 != (ret = SSL_accept(session->io_context)))
155  {
156  SPDYF_DEBUG("SSL_accept %i",ret);
157  SSL_free(session->io_context);
158  session->io_context = NULL;
159  return SPDY_NO;
160  }
161  /* alternatively
162  SSL_set_accept_state(session->io_context);
163  * may be called and then the negotiation will be done on reading
164  */
165 
166  return SPDY_YES;
167 }
168 
169 
170 void
172 {
173  //SSL_shutdown sends TLS "close notify" as in TLS standard.
174  //The function may fail as it waits for the other party to also close
175  //the TLS session. The lib just sends it and will close the socket
176  //after that because the browsers don't seem to care much about
177  //"close notify"
178  SSL_shutdown(session->io_context);
179 
180  SSL_free(session->io_context);
181 }
182 
183 
184 int
186  void * buffer,
187  size_t size)
188 {
189  int ret;
190  int n = SSL_read(session->io_context,
191  buffer,
192  size);
193  //if(n > 0) SPDYF_DEBUG("recvd: %i",n);
194  if (n <= 0)
195  {
196  ret = SSL_get_error(session->io_context, n);
197  switch(ret)
198  {
199  case SSL_ERROR_ZERO_RETURN:
200  return 0;
201 
202  case SSL_ERROR_WANT_READ:
203  case SSL_ERROR_WANT_WRITE:
204  return SPDY_IO_ERROR_AGAIN;
205 
206  case SSL_ERROR_SYSCALL:
207  if(EINTR == errno)
208  return SPDY_IO_ERROR_AGAIN;
209 
210  default:
211  return SPDY_IO_ERROR_ERROR;
212  }
213  }
214 
215  return n;
216 }
217 
218 
219 int
221  const void * buffer,
222  size_t size)
223 {
224  int ret;
225 
226  int n = SSL_write(session->io_context,
227  buffer,
228  size);
229  //if(n > 0) SPDYF_DEBUG("sent: %i",n);
230  if (n <= 0)
231  {
232  ret = SSL_get_error(session->io_context, n);
233  switch(ret)
234  {
235  case SSL_ERROR_ZERO_RETURN:
236  return 0;
237 
238  case SSL_ERROR_WANT_READ:
239  case SSL_ERROR_WANT_WRITE:
240  return SPDY_IO_ERROR_AGAIN;
241 
242  case SSL_ERROR_SYSCALL:
243  if(EINTR == errno)
244  return SPDY_IO_ERROR_AGAIN;
245 
246  default:
247  return SPDY_IO_ERROR_ERROR;
248  }
249  }
250 
251  return n;
252 }
253 
254 
255 int
257 {
258  /* From openssl docs:
259  * BUGS
260 SSL_pending() takes into account only bytes from the TLS/SSL record that is currently being processed (if any). If the SSL object's read_ahead flag is set, additional protocol bytes may have been read containing more TLS/SSL records; these are ignored by SSL_pending().
261  */
262  return SSL_pending(session->io_context) > 0 ? SPDY_YES : SPDY_NO;
263 }
264 
265 
266 int
268 {
269  (void)session;
270 
271  return SPDY_YES;
272 }
273 
274 
275 int
276 SPDYF_openssl_after_write(struct SPDY_Session *session, int was_written)
277 {
278  (void)session;
279 
280  return was_written;
281 }