3d402830142b3dcacf313910e571724527b38527
[umurmur.git] / src / ssl.h
1 /* Copyright (C) 2009-2014, Martin Johansson <martin@fatbob.nu>
2
3    Copyright (C) 2005-2014, Thorvald Natvig <thorvald@natvig.com>
4    All rights reserved.
5
6    Redistribution and use in source and binary forms, with or without
7    modification, are permitted provided that the following conditions
8    are met:
9
10    - Redistributions of source code must retain the above copyright notice,
11      this list of conditions and the following disclaimer.
12    - Redistributions in binary form must reproduce the above copyright notice,
13      this list of conditions and the following disclaimer in the documentation
14      and/or other materials provided with the distribution.
15    - Neither the name of the Developers nor the names of its contributors may
16      be used to endorse or promote products derived from this software without
17      specific prior written permission.
18
19    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
23    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifndef SSL_H_987698
33 #define SSL_H_987698
34
35 #include "config.h"
36 #include "types.h"
37
38 #include <inttypes.h>
39 #include <stdio.h>
40 #include <string.h>
41
42 #if defined(USE_POLARSSL)
43 #include <polarssl/ssl.h>
44 #include <polarssl/version.h>
45
46 #if defined(POLARSSL_VERSION_MAJOR)
47 #if (POLARSSL_VERSION_MAJOR < 1)
48 #error PolarSSL version 1.0.0 or greater is required!
49 #endif
50 #else
51 #error PolarSSL version 1.0.0 or greater is required!
52 #endif
53
54 #if defined(USE_POLARSSL_HAVEGE)
55 #include <polarssl/havege.h>
56     #if (POLARSSL_VERSION_MINOR >= 1)
57         #define HAVEGE_RAND (havege_random)
58         #define RAND_bytes(_dst_, _size_) do { \
59                 havege_random(&hs, _dst_, _size_); \
60         } while (0)
61     #else
62         #define HAVEGE_RAND (havege_rand)
63         #define RAND_bytes(_dst_, _size_) do { \
64             int i; \
65                 for (i = 0; i < _size_; i++) { \
66                     _dst_[i] = havege_rand(&hs); \
67                 } \
68         } while (0)
69     #endif
70 #else
71 #define RAND_bytes(_dst_, _size_) do { urandom_bytes(NULL, _dst_, _size_); } while (0)
72 int urandom_bytes(void *ctx, unsigned char *dest, size_t len);
73 #endif
74
75 #if (POLARSSL_VERSION_MINOR >= 2)
76     #define POLARSSL_API_V1_2_ABOVE
77 #endif
78 #if (POLARSSL_VERSION_MINOR == 3)
79     #define POLARSSL_API_V1_3_ABOVE
80 #endif
81
82 #define SSLI_ERROR_WANT_READ -0x0F300 /* PolarSSL v0.x.x uses -0x0f00 -> --0x0f90, v1.x.x uses -0x7080 -> -0x7e80 */
83 #define SSLI_ERROR_WANT_WRITE -0x0F310
84
85 #define SSLI_ERROR_ZERO_RETURN 0
86 #define SSLI_ERROR_CONNRESET POLARSSL_ERR_NET_CONN_RESET
87 #define SSLI_ERROR_SYSCALL POLARSSL_ERR_NET_RECV_FAILED
88
89 typedef ssl_context SSL_handle_t;
90
91 #elif defined(USE_MBEDTLS)
92 #include <mbedtls/ssl.h>
93 #if (MBEDTLS_VERSION_MINOR > 3)
94 #include <mbedtls/net_socket.h>
95 #else
96 #include <mbedtls/net.h>
97 #endif
98 #include <mbedtls/version.h>
99
100 #if defined(MBEDTLS_VERSION_MAJOR)
101 #if (MBEDTLS_VERSION_MAJOR < 2)
102 #error mbedTLS version 2.0.0 or greater is required!
103 #endif
104 #else
105 #error mbedTLS version 2.0.0 or greater is required!
106 #endif
107
108 #if defined(USE_MBEDTLS_HAVEGE)
109 #include <mbedtls/havege.h>
110     #define HAVEGE_RAND (havege_random)
111     #define RAND_bytes(_dst_, _size_) do { \
112         mbedtls_havege_random(&hs, _dst_, _size_); \
113     } while (0)
114 #else
115 #define RAND_bytes(_dst_, _size_) do { urandom_bytes(NULL, _dst_, _size_); } while (0)
116 int urandom_bytes(void *ctx, unsigned char *dest, size_t len);
117 #endif
118
119 #define SSLI_ERROR_WANT_READ -0x0F300 /* mbedTLS v0.x.x uses -0x0f00 -> --0x0f90, v1.x.x uses -0x7080 -> -0x7e80 */
120 #define SSLI_ERROR_WANT_WRITE -0x0F310
121
122 #define SSLI_ERROR_ZERO_RETURN 0
123 #define SSLI_ERROR_CONNRESET MBEDTLS_ERR_NET_CONN_RESET
124 #define SSLI_ERROR_SYSCALL MBEDTLS_ERR_NET_RECV_FAILED
125
126 typedef mbedtls_ssl_context SSL_handle_t;
127
128 #elif defined(USE_GNUTLS)
129
130 #include <gnutls/gnutls.h>
131
132 #define SSLI_ERROR_WANT_READ GNUTLS_E_AGAIN
133 #define SSLI_ERROR_WANT_WRITE GNUTLS_E_AGAIN
134 #define SSLI_ERROR_ZERO_RETURN GNUTLS_E_PREMATURE_TERMINATION
135 #define SSLI_ERROR_CONNRESET GNUTLS_E_PREMATURE_TERMINATION
136 #define SSLI_ERROR_SYSCALL GNUTLS_E_PREMATURE_TERMINATION
137
138 typedef gnutls_session_t SSL_handle_t;
139
140 #else /* OpenSSL */
141 #include <openssl/x509v3.h>
142 #include <openssl/ssl.h>
143
144 #define SSLI_ERROR_WANT_READ SSL_ERROR_WANT_READ
145 #define SSLI_ERROR_WANT_WRITE SSL_ERROR_WANT_WRITE
146 #define SSLI_ERROR_ZERO_RETURN SSL_ERROR_ZERO_RETURN
147 #define SSLI_ERROR_CONNRESET SSL_ERROR_ZERO_RETURN
148 #define SSLI_ERROR_SYSCALL SSL_ERROR_SYSCALL
149
150 typedef SSL SSL_handle_t;
151
152 #endif
153
154 void SSLi_init(void);
155 void SSLi_deinit(void);
156 SSL_handle_t *SSLi_newconnection(int *fd, bool_t *SSLready);
157 bool_t SSLi_getSHA1Hash(SSL_handle_t *ssl, uint8_t *hash);
158 void SSLi_closeconnection(SSL_handle_t *ssl);
159 int SSLi_nonblockaccept(SSL_handle_t *ssl, bool_t *SSLready);
160 int SSLi_read(SSL_handle_t *ssl, uint8_t *buf, int len);
161 int SSLi_write(SSL_handle_t *ssl, uint8_t *buf, int len);
162 int SSLi_get_error(SSL_handle_t *ssl, int code);
163 bool_t SSLi_data_pending(SSL_handle_t *ssl);
164 void SSLi_shutdown(SSL_handle_t *ssl);
165 void SSLi_free(SSL_handle_t *ssl);
166
167 static inline void SSLi_hash2hex(uint8_t *hash, char *out)
168 {
169         int i, offset = 0;
170         for (i = 0; i < 20; i++)
171                 offset += sprintf(out + offset, "%02x", hash[i]);
172 }
173
174 static inline void SSLi_hex2hash(char *in, uint8_t *hash)
175 {
176         int i;
177         char byte[3];
178         int scanned;
179
180         byte[2] = '\0';
181         for (i = 0; i < 20; i++) {
182                 memcpy(byte, &in[i * 2], 2);
183                 sscanf(byte, "%02x", &scanned);
184                 hash[i] = scanned;
185         }
186 }
187 #endif
188