source: trunk/third-party/shttpd/io_ssl.c @ 6644

Last change on this file since 6644 was 6644, checked in by charles, 13 years ago

upgrade to shttpd 1.42

File size: 1.9 KB
Line 
1/*
2 * Copyright (c) 2004-2005 Sergey Lyubka <valenok@gmail.com>
3 * All rights reserved
4 *
5 * "THE BEER-WARE LICENSE" (Revision 42):
6 * Sergey Lyubka wrote this file.  As long as you retain this notice you
7 * can do whatever you want with this stuff. If we meet some day, and you think
8 * this stuff is worth it, you can buy me a beer in return.
9 */
10
11#include "defs.h"
12
13#if !defined(NO_SSL)
14struct ssl_func ssl_sw[] = {
15        {"SSL_free",                    {0}},
16        {"SSL_accept",                  {0}},
17        {"SSL_connect",                 {0}},
18        {"SSL_read",                    {0}},
19        {"SSL_write",                   {0}},
20        {"SSL_get_error",               {0}},
21        {"SSL_set_fd",                  {0}},
22        {"SSL_new",                     {0}},
23        {"SSL_CTX_new",                 {0}},
24        {"SSLv23_server_method",        {0}},
25        {"SSL_library_init",            {0}},
26        {"SSL_CTX_use_PrivateKey_file", {0}},
27        {"SSL_CTX_use_certificate_file",{0}},
28        {NULL,                          {0}}
29};
30
31void
32_shttpd_ssl_handshake(struct stream *stream)
33{
34        int     n;
35
36        if ((n = SSL_accept(stream->chan.ssl.ssl)) == 1) {
37                DBG(("handshake: SSL accepted"));
38                stream->flags |= FLAG_SSL_ACCEPTED;
39        } else {
40                n = SSL_get_error(stream->chan.ssl.ssl, n);
41                if (n != SSL_ERROR_WANT_READ && n != SSL_ERROR_WANT_WRITE)
42                        stream->flags |= FLAG_CLOSED;
43                DBG(("SSL_accept error %d", n));
44        }
45}
46
47static int
48read_ssl(struct stream *stream, void *buf, size_t len)
49{
50        int     nread = -1;
51
52        assert(stream->chan.ssl.ssl != NULL);
53
54        if (!(stream->flags & FLAG_SSL_ACCEPTED))
55                _shttpd_ssl_handshake(stream);
56
57        if (stream->flags & FLAG_SSL_ACCEPTED)
58                nread = SSL_read(stream->chan.ssl.ssl, buf, len);
59
60        return (nread);
61}
62
63static int
64write_ssl(struct stream *stream, const void *buf, size_t len)
65{
66        assert(stream->chan.ssl.ssl != NULL);
67        return (SSL_write(stream->chan.ssl.ssl, buf, len));
68}
69
70static void
71close_ssl(struct stream *stream)
72{
73        assert(stream->chan.ssl.sock != -1);
74        assert(stream->chan.ssl.ssl != NULL);
75        (void) closesocket(stream->chan.ssl.sock);
76        SSL_free(stream->chan.ssl.ssl);
77}
78
79const struct io_class   _shttpd_io_ssl =  {
80        "ssl",
81        read_ssl,
82        write_ssl,
83        close_ssl
84};
85#endif /* !NO_SSL */
Note: See TracBrowser for help on using the repository browser.