Cleanup old support for pre PolarSSL 1.0.0
[umurmur.git] / src / ssl.h
1 /* Copyright (C) 2009-2012, Martin Johansson <martin@fatbob.nu>
2    Copyright (C) 2005-2012, Thorvald Natvig <thorvald@natvig.com>
3
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 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #ifdef USE_POLARSSL
40 #include <polarssl/ssl.h>
41 #include <polarssl/version.h>
42
43 #if (POLARSSL_VERSION_MAJOR < 1)
44 #error PolarSSL version 1.0.0 or later is required!
45 #endif
46
47 #ifdef USE_POLARSSL_HAVEGE
48     #if (POLARSSL_VERSION_MINOR >= 1)
49         #define HAVEGE_RAND (havege_random)
50         #define RAND_bytes(_dst_, _size_) do { \
51                 havege_random(&hs, _dst_, _size_); \
52         } while (0)
53     #else
54         #define HAVEGE_RAND (havege_rand)
55         #define RAND_bytes(_dst_, _size_) do { \
56             int i; \
57                 for (i = 0; i < _size_; i++) { \
58                     _dst_[i] = havege_rand(&hs); \
59                 } \
60         } while (0)
61     #endif
62 #else
63 #define RAND_bytes(_dst_, _size_) do { urandom_bytes(NULL, _dst_, _size_); } while (0)
64 int urandom_bytes(void *ctx, unsigned char *dest, size_t len);
65 #endif
66
67 #if (POLARSSL_VERSION_MINOR >= 2)
68     #define POLARSSL_API_V1_2
69 #endif
70
71 #else /* OpenSSL */
72 #include <openssl/x509v3.h>
73 #include <openssl/ssl.h>
74 #endif
75
76 #include "types.h"
77 #include <inttypes.h>
78
79 #ifdef USE_POLARSSL
80 #define SSLI_ERROR_WANT_READ -0x0F300 /* PolarSSL v0.x.x uses -0x0f00 -> --0x0f90, v1.x.x uses -0x7080 -> -0x7e80 */
81 #define SSLI_ERROR_WANT_WRITE -0x0F310
82
83 #define SSLI_ERROR_ZERO_RETURN 0
84 #define SSLI_ERROR_CONNRESET POLARSSL_ERR_NET_CONN_RESET
85 #define SSLI_ERROR_SYSCALL POLARSSL_ERR_NET_RECV_FAILED
86
87 typedef ssl_context SSL_handle_t;
88
89 #else
90
91 #define SSLI_ERROR_WANT_READ SSL_ERROR_WANT_READ
92 #define SSLI_ERROR_WANT_WRITE SSL_ERROR_WANT_WRITE
93 #define SSLI_ERROR_ZERO_RETURN SSL_ERROR_ZERO_RETURN
94 #define SSLI_ERROR_CONNRESET SSL_ERROR_ZERO_RETURN
95 #define SSLI_ERROR_SYSCALL SSL_ERROR_SYSCALL
96
97 typedef SSL SSL_handle_t;
98
99 #endif
100
101 void SSLi_init(void);
102 void SSLi_deinit(void);
103 SSL_handle_t *SSLi_newconnection(int *fd, bool_t *SSLready);
104 bool_t SSLi_getSHA1Hash(SSL_handle_t *ssl, uint8_t *hash);
105 void SSLi_closeconnection(SSL_handle_t *ssl);
106 int SSLi_nonblockaccept(SSL_handle_t *ssl, bool_t *SSLready);
107 int SSLi_read(SSL_handle_t *ssl, uint8_t *buf, int len);
108 int SSLi_write(SSL_handle_t *ssl, uint8_t *buf, int len);
109 int SSLi_get_error(SSL_handle_t *ssl, int code);
110 bool_t SSLi_data_pending(SSL_handle_t *ssl);
111 void SSLi_shutdown(SSL_handle_t *ssl);
112 void SSLi_free(SSL_handle_t *ssl);
113
114 static inline void SSLi_hash2hex(uint8_t *hash, char *out)
115 {
116         int i, offset = 0;
117         for (i = 0; i < 20; i++)
118                 offset += sprintf(out + offset, "%02x", hash[i]);
119 }
120 static inline void SSLi_hex2hash(char *in, uint8_t *hash)
121 {
122         int i, offset = 0;
123         char byte[3];
124         int scanned;
125         
126         byte[2] = '\0';
127         for (i = 0; i < 20; i++) {
128                 memcpy(byte, &in[i * 2], 2);
129                 sscanf(byte, "%02x", &scanned);
130                 hash[i] = scanned;
131         }
132 }
133 #endif