Debian Codegraph @

ccast/axTLS/tls1.h

raw symbols SCIP
1 /*
2 * Copyright (c) 2007-2016, Cameron Rich
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 are met:
8 *
9 * * Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * * Neither the name of the axTLS project nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 /**
32 * @file tls1.h
33 *
34 * @brief The definitions for the TLS library.
35 */
36 #ifndef HEADER_SSL_LIB_H
37 #define HEADER_SSL_LIB_H
38
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42
43 #include "axTLS_version.h"
44 #include "axTLS_config.h"
45 #include "os_int.h"
46 #include "os_port.h"
47 #include "crypto.h"
48 #include "crypto_misc.h"
49
50 #define SSL_PROTOCOL_MIN_VERSION 0x31 /* TLS v1.0 */
51 #define SSL_PROTOCOL_VERSION_MAX 0x33 /* TLS v1.3 */
52 #define SSL_PROTOCOL_VERSION_TLS1_1 0x32 /* TLS v1.1 */
53 #define SSL_PROTOCOL_VERSION_TLS1_2 0x33 /* TLS v1.2 */
54 #define SSL_RANDOM_SIZE 32
55 #define SSL_SECRET_SIZE 48
56 #define SSL_FINISHED_HASH_SIZE 12
57 #define SSL_RECORD_SIZE 5
58 #define SSL_SERVER_READ 0
59 #define SSL_SERVER_WRITE 1
60 #define SSL_CLIENT_READ 2
61 #define SSL_CLIENT_WRITE 3
62 #define SSL_HS_HDR_SIZE 4
63
64 /* the flags we use while establishing a connection */
65 #define SSL_NEED_RECORD 0x0001
66 #define SSL_TX_ENCRYPTED 0x0002
67 #define SSL_RX_ENCRYPTED 0x0004
68 #define SSL_SESSION_RESUME 0x0008
69 #define SSL_IS_CLIENT 0x0010
70 #define SSL_HAS_CERT_REQ 0x0020
71 #define SSL_SENT_CLOSE_NOTIFY 0x0040
72
73 /* some macros to muck around with flag bits */
74 #define SET_SSL_FLAG(A) (ssl->flag |= A)
75 #define CLR_SSL_FLAG(A) (ssl->flag &= ~A)
76 #define IS_SET_SSL_FLAG(A) (ssl->flag & A)
77
78 #define MAX_KEY_BYTE_SIZE 512 /* for a 4096 bit key */
79 #define RT_MAX_PLAIN_LENGTH 16384
80 #define RT_EXTRA 1024
81 #define BM_RECORD_OFFSET 5
82
83 #define NUM_PROTOCOLS 4
84
85 #define MAX_SIG_ALGORITHMS 4
86 #define SIG_ALG_SHA1 2
87 #define SIG_ALG_SHA256 4
88 #define SIG_ALG_SHA384 5
89 #define SIG_ALG_SHA512 6
90 #define SIG_ALG_RSA 1
91
92 #define PARANOIA_CHECK(A, B) if (A < B) { \
93 ret = SSL_ERROR_INVALID_HANDSHAKE; goto error; }
94
95 /* protocol types */
96 enum
97 {
98 PT_CHANGE_CIPHER_SPEC = 20,
99 PT_ALERT_PROTOCOL,
100 PT_HANDSHAKE_PROTOCOL,
101 PT_APP_PROTOCOL_DATA
102 };
103
104 /* handshaking types */
105 enum
106 {
107 HS_HELLO_REQUEST,
108 HS_CLIENT_HELLO,
109 HS_SERVER_HELLO,
110 HS_CERTIFICATE = 11,
111 HS_SERVER_KEY_XCHG,
112 HS_CERT_REQ,
113 HS_SERVER_HELLO_DONE,
114 HS_CERT_VERIFY,
115 HS_CLIENT_KEY_XCHG,
116 HS_FINISHED = 20
117 };
118
119 /* SSL extension types */
120 enum
121 {
122 SSL_EXT_SERVER_NAME = 0,
123 SSL_EXT_MAX_FRAGMENT_SIZE,
124 SSL_EXT_SIG_ALG = 0x0d,
125 };
126
127 typedef struct
128 {
129 uint8_t cipher;
130 uint8_t key_size;
131 uint8_t iv_size;
132 uint8_t padding_size;
133 uint8_t digest_size;
134 uint8_t key_block_size;
135 hmac_func hmac;
136 crypt_func encrypt;
137 crypt_func decrypt;
138 } cipher_info_t;
139
140 struct _SSLObjLoader
141 {
142 uint8_t *buf;
143 int len;
144 };
145
146 typedef struct _SSLObjLoader SSLObjLoader;
147
148 typedef struct
149 {
150 time_t conn_time;
151 uint8_t session_id[SSL_SESSION_ID_SIZE];
152 uint8_t master_secret[SSL_SECRET_SIZE];
153 } SSL_SESSION;
154
155 typedef struct
156 {
157 uint8_t *buf;
158 int size;
159 uint8_t hash_alg;
160 } SSL_CERT;
161
162 typedef struct
163 {
164 MD5_CTX md5_ctx;
165 SHA1_CTX sha1_ctx;
166 SHA256_CTX sha256_ctx;
167 uint8_t client_random[SSL_RANDOM_SIZE]; /* client's random sequence */
168 uint8_t server_random[SSL_RANDOM_SIZE]; /* server's random sequence */
169 uint8_t final_finish_mac[128];
170 uint8_t master_secret[SSL_SECRET_SIZE];
171 uint8_t key_block[256];
172 uint16_t bm_proc_index;
173 uint8_t key_block_generated;
174 } DISPOSABLE_CTX;
175
176 typedef struct
177 {
178 const char *host_name; /* Needed for the SNI support */
179 /* Needed for the Max Fragment Size Extension.
180 Allowed values: 2^9, 2^10 .. 2^14 */
181 uint16_t max_fragment_size;
182 } SSL_EXTENSIONS;
183
184 struct _SSL
185 {
186 uint32_t flag;
187 uint16_t need_bytes;
188 uint16_t got_bytes;
189 uint8_t record_type;
190 uint8_t cipher;
191 uint8_t sess_id_size;
192 uint8_t version;
193 uint8_t client_version;
194 int16_t next_state;
195 int16_t hs_status;
196 DISPOSABLE_CTX *dc; /* temporary data which we'll get rid of soon */
197 int client_fd;
198 const cipher_info_t *cipher_info;
199 void *encrypt_ctx;
200 void *decrypt_ctx;
201 uint8_t bm_all_data[RT_MAX_PLAIN_LENGTH+RT_EXTRA];
202 uint8_t *bm_data;
203 uint16_t bm_index;
204 uint16_t bm_read_index;
205 uint8_t sig_algs[MAX_SIG_ALGORITHMS];
206 uint8_t num_sig_algs;
207 struct _SSL *next; /* doubly linked list */
208 struct _SSL *prev;
209 struct _SSL_CTX *ssl_ctx; /* back reference to a clnt/svr ctx */
210
211 /* asynchronous client ssl_read specific duplicates of above: */
212 uint16_t rneed_bytes;
213 uint16_t rgot_bytes;
214 uint8_t bm_read_data[RT_MAX_PLAIN_LENGTH+RT_EXTRA];
215 uint8_t *bm_rdata;
216 uint16_t bm_rread_index;
217 uint8_t hmac_rheader[SSL_RECORD_SIZE]; /* client rx hmac */
218 uint8_t rrecord_type;
219 int need_record;
220
221 #ifndef CONFIG_SSL_SKELETON_MODE
222 uint16_t session_index;
223 SSL_SESSION *session;
224 #endif
225 //#ifdef CONFIG_SSL_CERT_VERIFICATION
226 X509_CTX *x509_ctx;
227 //#endif
228
229 uint8_t session_id[SSL_SESSION_ID_SIZE];
230 uint8_t client_mac[SHA256_SIZE]; /* for HMAC verification */
231 uint8_t server_mac[SHA256_SIZE]; /* for HMAC verification */
232 uint8_t read_sequence[8]; /* 64 bit sequence number */
233 uint8_t write_sequence[8]; /* 64 bit sequence number */
234 uint8_t hmac_header[SSL_RECORD_SIZE]; /* rx hmac */
235 SSL_EXTENSIONS *extensions; /* Contains the SSL (client) extensions */
236 };
237
238 typedef struct _SSL SSL;
239
240 struct _SSL_CTX
241 {
242 uint32_t options;
243 uint8_t chain_length;
244 RSA_CTX *rsa_ctx;
245 #ifdef CONFIG_SSL_CERT_VERIFICATION
246 CA_CERT_CTX *ca_cert_ctx;
247 #endif
248 SSL *head;
249 SSL *tail;
250 SSL_CERT certs[CONFIG_SSL_MAX_CERTS];
251 #ifndef CONFIG_SSL_SKELETON_MODE
252 uint16_t num_sessions;
253 SSL_SESSION **ssl_sessions;
254 #endif
255 #ifdef CONFIG_SSL_CTX_MUTEXING
256 SSL_CTX_MUTEX_TYPE mutex;
257 #endif
258 #ifdef CONFIG_OPENSSL_COMPATIBLE
259 void *bonus_attr;
260 #endif
261 };
262
263 typedef struct _SSL_CTX SSL_CTX;
264
265 /* backwards compatibility */
266 typedef struct _SSL_CTX SSLCTX;
267
268 extern const uint8_t ssl_prot_prefs[NUM_PROTOCOLS];
269
270 SSL *ssl_new(SSL_CTX *ssl_ctx, int client_fd);
271 void disposable_new(SSL *ssl);
272 void disposable_free(SSL *ssl);
273 int send_packet(SSL *ssl, uint8_t protocol,
274 const uint8_t *in, int length);
275 int do_svr_handshake(SSL *ssl, int handshake_type, uint8_t *buf, int hs_len);
276 int do_clnt_handshake(SSL *ssl, int handshake_type, uint8_t *buf, int hs_len);
277 int process_finished(SSL *ssl, uint8_t *buf, int hs_len);
278 int process_sslv23_client_hello(SSL *ssl);
279 int send_alert(SSL *ssl, int error_code);
280 int send_finished(SSL *ssl);
281 int send_certificate(SSL *ssl);
282 int basic_read(SSL *ssl, uint8_t **in_data);
283 int basic_readi(SSL *ssl, uint8_t **in_data);
284 int send_change_cipher_spec(SSL *ssl);
285 int finished_digest(SSL *ssl, const char *label, uint8_t *digest);
286 void generate_master_secret(SSL *ssl, const uint8_t *premaster_secret);
287 void add_packet(SSL *ssl, const uint8_t *pkt, int len);
288 int add_cert(SSL_CTX *ssl_ctx, const uint8_t *buf, int len);
289 int add_private_key(SSL_CTX *ssl_ctx, SSLObjLoader *ssl_obj);
290 void ssl_obj_free(SSLObjLoader *ssl_obj);
291 int pkcs8_decode(SSL_CTX *ssl_ctx, SSLObjLoader *ssl_obj, const char *password);
292 int pkcs12_decode(SSL_CTX *ssl_ctx, SSLObjLoader *ssl_obj, const char *password);
293 int load_key_certs(SSL_CTX *ssl_ctx);
294 #ifdef CONFIG_SSL_CERT_VERIFICATION
295 int add_cert_auth(SSL_CTX *ssl_ctx, const uint8_t *buf, int len);
296 void remove_ca_certs(CA_CERT_CTX *ca_cert_ctx);
297 #endif
298 #ifdef CONFIG_SSL_ENABLE_CLIENT
299 int do_client_connect(SSL *ssl);
300 #endif
301
302 #ifdef CONFIG_DEBUG
303 void DISPLAY_STATE(SSL *ssl, int is_send, uint8_t state, int not_ok);
304 void DISPLAY_BYTES(SSL *ssl, const char *format,
305 const uint8_t *data, int size, ...);
306 void DISPLAY_CERT(SSL *ssl, const X509_CTX *x509_ctx);
307 void DISPLAY_RSA(SSL *ssl, const RSA_CTX *rsa_ctx);
308 void DISPLAY_ALERT(SSL *ssl, int alert);
309 #else
310 #define DISPLAY_STATE(A,B,C,D)
311 #define DISPLAY_CERT(A,B)
312 #define DISPLAY_RSA(A,B)
313 #define DISPLAY_ALERT(A, B)
314 #ifdef WIN32
315 void DISPLAY_BYTES(SSL *ssl, const char *format,/* win32 has no variadic macros */
316 const uint8_t *data, int size, ...);
317 #else
318 #define DISPLAY_BYTES(A,B,C,D,...)
319 #endif
320 #endif
321
322 int process_certificate(SSL *ssl, X509_CTX **x509_ctx);
323
324 SSL_SESSION *ssl_session_update(int max_sessions,
325 SSL_SESSION *ssl_sessions[], SSL *ssl,
326 const uint8_t *session_id);
327 void kill_ssl_session(SSL_SESSION **ssl_sessions, SSL *ssl);
328
329 #ifdef __cplusplus
330 }
331 #endif
332
333 #endif