kodi
tls1.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007, 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 
36 #ifndef HEADER_SSL_LIB_H
37 #define HEADER_SSL_LIB_H
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 #include "version.h"
44 #include "crypto.h"
45 #include "crypto_misc.h"
46 
47 #define SSL_PROTOCOL_MIN_VERSION 0x31 /* TLS v1.0 */
48 #define SSL_PROTOCOL_MINOR_VERSION 0x02 /* TLS v1.1 */
49 #define SSL_PROTOCOL_VERSION_MAX 0x32 /* TLS v1.1 */
50 #define SSL_PROTOCOL_VERSION1_1 0x32 /* TLS v1.1 */
51 #define SSL_RANDOM_SIZE 32
52 #define SSL_SECRET_SIZE 48
53 #define SSL_FINISHED_HASH_SIZE 12
54 #define SSL_RECORD_SIZE 5
55 #define SSL_SERVER_READ 0
56 #define SSL_SERVER_WRITE 1
57 #define SSL_CLIENT_READ 2
58 #define SSL_CLIENT_WRITE 3
59 #define SSL_HS_HDR_SIZE 4
60 
61 /* the flags we use while establishing a connection */
62 #define SSL_NEED_RECORD 0x0001
63 #define SSL_TX_ENCRYPTED 0x0002
64 #define SSL_RX_ENCRYPTED 0x0004
65 #define SSL_SESSION_RESUME 0x0008
66 #define SSL_IS_CLIENT 0x0010
67 #define SSL_HAS_CERT_REQ 0x0020
68 #define SSL_SENT_CLOSE_NOTIFY 0x0040
69 
70 /* some macros to muck around with flag bits */
71 #define SET_SSL_FLAG(A) (ssl->flag |= A)
72 #define CLR_SSL_FLAG(A) (ssl->flag &= ~A)
73 #define IS_SET_SSL_FLAG(A) (ssl->flag & A)
74 
75 #define MAX_KEY_BYTE_SIZE 512 /* for a 4096 bit key */
76 #define RT_MAX_PLAIN_LENGTH 16384
77 #define RT_EXTRA 1024
78 #define BM_RECORD_OFFSET 5
79 
80 #ifdef CONFIG_SSL_SKELETON_MODE
81 #define NUM_PROTOCOLS 1
82 #else
83 #define NUM_PROTOCOLS 4
84 #endif
85 
86 #define PARANOIA_CHECK(A, B) if (A < B) { \
87  ret = SSL_ERROR_INVALID_HANDSHAKE; goto error; }
88 
89 /* protocol types */
90 enum
91 {
92  PT_CHANGE_CIPHER_SPEC = 20,
93  PT_ALERT_PROTOCOL,
94  PT_HANDSHAKE_PROTOCOL,
95  PT_APP_PROTOCOL_DATA
96 };
97 
98 /* handshaking types */
99 enum
100 {
101  HS_HELLO_REQUEST,
102  HS_CLIENT_HELLO,
103  HS_SERVER_HELLO,
104  HS_CERTIFICATE = 11,
105  HS_SERVER_KEY_XCHG,
106  HS_CERT_REQ,
107  HS_SERVER_HELLO_DONE,
108  HS_CERT_VERIFY,
109  HS_CLIENT_KEY_XCHG,
110  HS_FINISHED = 20
111 };
112 
113 typedef struct
114 {
115  uint8_t cipher;
116  uint8_t key_size;
117  uint8_t iv_size;
118  uint8_t key_block_size;
119  uint8_t padding_size;
120  uint8_t digest_size;
121  hmac_func hmac;
122  crypt_func encrypt;
123  crypt_func decrypt;
124 } cipher_info_t;
125 
127 {
128  uint8_t *buf;
129  int len;
130 };
131 
132 typedef struct _SSLObjLoader SSLObjLoader;
133 
134 typedef struct
135 {
136  time_t conn_time;
137  uint8_t session_id[SSL_SESSION_ID_SIZE];
138  uint8_t master_secret[SSL_SECRET_SIZE];
139 } SSL_SESSION;
140 
141 typedef struct _SSL_CERT /* GBG: added */
142 {
143  uint8_t *buf;
144  int size;
145  struct _SSL_CERT* next; /* GBG: added */
146 } SSL_CERT;
147 
148 typedef X509_CTX SSL_X509_CERT;
149 
150 typedef struct
151 {
152  MD5_CTX md5_ctx;
153  SHA1_CTX sha1_ctx;
154  uint8_t final_finish_mac[SSL_FINISHED_HASH_SIZE];
155  uint8_t *key_block;
156  uint8_t master_secret[SSL_SECRET_SIZE];
157  uint8_t client_random[SSL_RANDOM_SIZE]; /* client's random sequence */
158  uint8_t server_random[SSL_RANDOM_SIZE]; /* server's random sequence */
159  uint16_t bm_proc_index;
161 
162 struct _SSL
163 {
164  uint32_t flag;
165  uint16_t need_bytes;
166  uint16_t got_bytes;
167  uint8_t record_type;
168  uint8_t cipher;
169  uint8_t sess_id_size;
170  uint8_t version;
171  uint8_t client_version;
172  int16_t next_state;
173  int16_t hs_status;
174  DISPOSABLE_CTX *dc; /* temporary data which we'll get rid of soon */
175  SSL_SOCKET* client_fd;
176  const cipher_info_t *cipher_info;
177  void *encrypt_ctx;
178  void *decrypt_ctx;
179  uint8_t bm_all_data[RT_MAX_PLAIN_LENGTH+RT_EXTRA];
180  uint8_t *bm_data;
181  uint16_t bm_index;
182  uint16_t bm_read_index;
183  struct _SSL *next; /* doubly linked list */
184  struct _SSL *prev;
185  struct _SSL_CTX *ssl_ctx; /* back reference to a clnt/svr ctx */
186 #ifndef CONFIG_SSL_SKELETON_MODE
187  uint16_t session_index;
188  SSL_SESSION *session;
189 #endif
190 #ifdef CONFIG_SSL_CERT_VERIFICATION
191  X509_CTX *x509_ctx;
192 #endif
193 
194  uint8_t session_id[SSL_SESSION_ID_SIZE];
195  uint8_t client_mac[SHA1_SIZE]; /* for HMAC verification */
196  uint8_t server_mac[SHA1_SIZE]; /* for HMAC verification */
197  uint8_t read_sequence[8]; /* 64 bit sequence number */
198  uint8_t write_sequence[8]; /* 64 bit sequence number */
199  uint8_t hmac_header[SSL_RECORD_SIZE]; /* rx hmac */
200 };
201 
202 typedef struct _SSL SSL;
203 
204 struct _SSL_CTX
205 {
206  uint32_t options;
207  /* GBG: removed - uint8_t chain_length; */
208  RSA_CTX *rsa_ctx;
209 #ifdef CONFIG_SSL_CERT_VERIFICATION
210  /* GBG: removed CA_CERT_CTX *ca_cert_ctx; */
211  X509_CTX* ca_certs; /* GBG: added */
212 #endif
213  SSL *head;
214  SSL *tail;
215  SSL_CERT* certs; /* GBG: modified */
216 #ifndef CONFIG_SSL_SKELETON_MODE
217  uint16_t num_sessions;
218  SSL_SESSION **ssl_sessions;
219 #endif
220 #ifdef CONFIG_SSL_CTX_MUTEXING
221  SSL_CTX_MUTEX_TYPE mutex;
222 #endif
223 #ifdef CONFIG_OPENSSL_COMPATIBLE
224  void *bonus_attr;
225 #endif
226 };
227 
228 typedef struct _SSL_CTX SSL_CTX;
229 
230 /* backwards compatibility */
231 typedef struct _SSL_CTX SSLCTX;
232 
233 extern const uint8_t ssl_prot_prefs[NUM_PROTOCOLS];
234 
235 SSL *ssl_new(SSL_CTX *ssl_ctx, void* client_fd);
236 void disposable_new(SSL *ssl);
237 void disposable_free(SSL *ssl);
238 int send_packet(SSL *ssl, uint8_t protocol,
239  const uint8_t *in, int length);
240 int do_svr_handshake(SSL *ssl, int handshake_type, uint8_t *buf, int hs_len);
241 int do_clnt_handshake(SSL *ssl, int handshake_type, uint8_t *buf, int hs_len);
242 int process_finished(SSL *ssl, uint8_t *buf, int hs_len);
243 int process_sslv23_client_hello(SSL *ssl);
244 int send_alert(SSL *ssl, int error_code);
245 int send_finished(SSL *ssl);
246 int send_certificate(SSL *ssl);
247 int basic_read(SSL *ssl, uint8_t **in_data);
248 int send_change_cipher_spec(SSL *ssl);
249 void finished_digest(SSL *ssl, const char *label, uint8_t *digest);
250 void generate_master_secret(SSL *ssl, const uint8_t *premaster_secret);
251 void add_packet(SSL *ssl, const uint8_t *pkt, int len);
252 int add_cert(SSL_CTX *ssl_ctx, const uint8_t *buf, int len);
253 int add_private_key(SSL_CTX *ssl_ctx, SSLObjLoader *ssl_obj);
254 void ssl_obj_free(SSLObjLoader *ssl_obj);
255 int pkcs8_decode(SSL_CTX *ssl_ctx, SSLObjLoader *ssl_obj, const char *password);
256 int pkcs12_decode(SSL_CTX *ssl_ctx, SSLObjLoader *ssl_obj, const char *password);
257 int load_key_certs(SSL_CTX *ssl_ctx);
258 #ifdef CONFIG_SSL_CERT_VERIFICATION
259 int add_cert_auth(SSL_CTX *ssl_ctx, const uint8_t *buf, int len);
260 /* GBG: removed - void remove_ca_certs(CA_CERT_CTX *ca_cert_ctx); */
261 #endif
262 #ifdef CONFIG_SSL_ENABLE_CLIENT
263 int do_client_connect(SSL *ssl);
264 #endif
265 
266 #ifdef CONFIG_SSL_FULL_MODE
267 void DISPLAY_STATE(SSL *ssl, int is_send, uint8_t state, int not_ok);
268 void DISPLAY_BYTES(SSL *ssl, const char *format,
269  const uint8_t *data, int size, ...);
270 void DISPLAY_CERT(SSL *ssl, const X509_CTX *x509_ctx);
271 void DISPLAY_RSA(SSL *ssl, const RSA_CTX *rsa_ctx);
272 void DISPLAY_ALERT(SSL *ssl, int alert);
273 #else
274 #define DISPLAY_STATE(A,B,C,D)
275 #define DISPLAY_CERT(A,B)
276 #define DISPLAY_RSA(A,B)
277 #define DISPLAY_ALERT(A, B)
278 #ifdef WIN32
279 void DISPLAY_BYTES(SSL *ssl, const char *format,/* win32 has no variadic macros */
280  const uint8_t *data, int size, ...);
281 #else
282 void DISPLAY_BYTES(SSL *ssl, const char *format,/* win32 has no variadic macros */
283  const uint8_t *data, int size, ...);
284 #endif
285 #endif
286 
287 #ifdef CONFIG_SSL_CERT_VERIFICATION
288 int process_certificate(SSL *ssl, X509_CTX **x509_ctx);
289 #endif
290 
291 SSL_SESSION *ssl_session_update(int max_sessions,
292  SSL_SESSION *ssl_sessions[], SSL *ssl,
293  const uint8_t *session_id);
294 void kill_ssl_session(SSL_SESSION **ssl_sessions, SSL *ssl);
295 
296 #ifdef __cplusplus
297 }
298 #endif
299 
300 #endif
int add_cert(SSL_CTX *ssl_ctx, const uint8_t *buf, int len)
Add a certificate to the certificate chain.
Definition: tls1.c:358
int send_finished(SSL *ssl)
Send a "finished" message.
Definition: tls1.c:1527
Definition: tls1.h:150
int send_change_cipher_spec(SSL *ssl)
Sends the change cipher spec message.
Definition: tls1.c:1511
void finished_digest(SSL *ssl, const char *label, uint8_t *digest)
Calculate the digest used in the finished message.
Definition: tls1.c:951
void add_packet(SSL *ssl, const uint8_t *pkt, int len)
Add a packet to the end of our sent and received packets, so that we may use it to calculate the hash...
Definition: tls1.c:835
void generate_master_secret(SSL *ssl, const uint8_t *premaster_secret)
Generate a master secret based on the client/server random data and the premaster secret...
Definition: tls1.c:924
Definition: tls1.h:113
int send_alert(SSL *ssl, int error_code)
Send an alert message.
Definition: tls1.c:1554
Definition: tls1.h:162
Definition: tls1.h:126
void disposable_free(SSL *ssl)
Remove the temporary blob of memory.
Definition: tls1.c:1705
int process_finished(SSL *ssl, uint8_t *buf, int hs_len)
Process a client finished message.
Definition: tls1.c:1627
Definition: crypto.h:170
Definition: tls1.h:141
Definition: crypto_misc.h:68
Definition: tls1.h:204
int basic_read(SSL *ssl, uint8_t **in_data)
Read the SSL connection.
Definition: tls1.c:1267
Definition: crypto.h:110
int send_certificate(SSL *ssl)
Send a certificate.
Definition: tls1.c:1657
Definition: tls1.h:134
Definition: os_port.h:58
int send_packet(SSL *ssl, uint8_t protocol, const uint8_t *in, int length)
Send an encrypted packet with padding bytes if necessary.
Definition: tls1.c:1084
SSL_SESSION * ssl_session_update(int max_sessions, SSL_SESSION *ssl_sessions[], SSL *ssl, const uint8_t *session_id)
Find if an existing session has the same session id.
Definition: tls1.c:1722
Definition: crypto.h:147
void kill_ssl_session(SSL_SESSION **ssl_sessions, SSL *ssl)
This ssl object doesn&#39;t want this session anymore.
Definition: tls1.c:1810
void disposable_new(SSL *ssl)
Create a blob of memory that we&#39;ll get rid of once the handshake is complete.
Definition: tls1.c:1692