Debian Codegraph @

ccast/axTLS/tls1_clnt.c

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 #include <stdlib.h>
32 #include <string.h>
33 #include <time.h>
34 #include <stdio.h>
35 #include "os_port.h"
36 #include "ssl.h"
37
38 #ifdef CONFIG_SSL_ENABLE_CLIENT /* all commented out if no client */
39
40 /* support sha512/384/256/1 RSA */
41 static const uint8_t g_sig_alg[] = {
42 0x00, SSL_EXT_SIG_ALG,
43 0x00, 0x0a, 0x00, 0x08,
44 SIG_ALG_SHA512, SIG_ALG_RSA,
45 SIG_ALG_SHA384, SIG_ALG_RSA,
46 SIG_ALG_SHA256, SIG_ALG_RSA,
47 SIG_ALG_SHA1, SIG_ALG_RSA
48 };
49
50 static const uint8_t g_asn1_sha256[] =
51 {
52 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
53 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20
54 };
55
56 static int send_client_hello(SSL *ssl);
57 static int process_server_hello(SSL *ssl);
58 static int process_server_hello_done(SSL *ssl);
59 static int send_client_key_xchg(SSL *ssl);
60 static int process_cert_req(SSL *ssl);
61 static int send_cert_verify(SSL *ssl);
62
63 /*
64 * Establish a new SSL connection to an SSL server.
65 */
66 EXP_FUNC SSL * STDCALL ssl_client_new(SSL_CTX *ssl_ctx, int client_fd, const
67 uint8_t *session_id, uint8_t sess_id_size, SSL_EXTENSIONS* ssl_ext)
68 {
69 SSL *ssl = ssl_new(ssl_ctx, client_fd);
70 ssl->version = SSL_PROTOCOL_VERSION_MAX; /* try top version first */
71
72 if (session_id && ssl_ctx->num_sessions)
73 {
74 if (sess_id_size > SSL_SESSION_ID_SIZE) /* validity check */
75 {
76 ssl_free(ssl);
77 return NULL;
78 }
79
80 memcpy(ssl->session_id, session_id, sess_id_size);
81 ssl->sess_id_size = sess_id_size;
82 SET_SSL_FLAG(SSL_SESSION_RESUME); /* just flag for later */
83 }
84
85 ssl->extensions = ssl_ext;
86
87 SET_SSL_FLAG(SSL_IS_CLIENT);
88 do_client_connect(ssl);
89 return ssl;
90 }
91
92 /*
93 * Process the handshake record.
94 */
95 int do_clnt_handshake(SSL *ssl, int handshake_type, uint8_t *buf, int hs_len)
96 {
97 int ret = SSL_OK;
98
99 /* To get here the state must be valid */
100 switch (handshake_type)
101 {
102 case HS_SERVER_HELLO:
103 ret = process_server_hello(ssl);
104 break;
105
106 case HS_CERTIFICATE:
107 //#ifdef CONFIG_SSL_CERT_VERIFICATION
108 ret = process_certificate(ssl, &ssl->x509_ctx);
109 //#endif
110 break;
111
112 case HS_SERVER_HELLO_DONE:
113 if ((ret = process_server_hello_done(ssl)) == SSL_OK)
114 {
115 if (IS_SET_SSL_FLAG(SSL_HAS_CERT_REQ))
116 {
117 if ((ret = send_certificate(ssl)) == SSL_OK &&
118 (ret = send_client_key_xchg(ssl)) == SSL_OK)
119 {
120 send_cert_verify(ssl);
121 }
122 }
123 else
124 {
125 ret = send_client_key_xchg(ssl);
126 }
127
128 if (ret == SSL_OK &&
129 (ret = send_change_cipher_spec(ssl)) == SSL_OK)
130 {
131 ret = send_finished(ssl);
132 }
133 }
134 break;
135
136 case HS_CERT_REQ:
137 ret = process_cert_req(ssl);
138 break;
139
140 case HS_FINISHED:
141 ret = process_finished(ssl, buf, hs_len);
142 disposable_free(ssl); /* free up some memory */
143 /* note: client renegotiation is not allowed after this */
144 break;
145
146 case HS_HELLO_REQUEST:
147 disposable_new(ssl);
148 ret = do_client_connect(ssl);
149 break;
150
151 default:
152 ret = SSL_ERROR_INVALID_HANDSHAKE;
153 break;
154 }
155
156 return ret;
157 }
158
159 /*
160 * Do the handshaking from the beginning.
161 */
162 int do_client_connect(SSL *ssl)
163 {
164 int ret = SSL_OK;
165
166 send_client_hello(ssl); /* send the client hello */
167 ssl->bm_read_index = 0;
168 ssl->next_state = HS_SERVER_HELLO;
169 ssl->hs_status = SSL_NOT_OK; /* not connected */
170
171 /* sit in a loop until it all looks good */
172 if (!IS_SET_SSL_FLAG(SSL_CONNECT_IN_PARTS))
173 {
174 while (ssl->hs_status != SSL_OK)
175 {
176 /* Use internal, synchronous ssl_read - GWG */
177 ret = ssl_readi(ssl, NULL);
178
179 if (ret < SSL_OK)
180 break;
181 }
182
183 ssl->hs_status = ret; /* connected? */
184 }
185
186 return ret;
187 }
188
189 /*
190 * Send the initial client hello.
191 */
192 static int send_client_hello(SSL *ssl)
193 {
194 uint8_t *buf = ssl->bm_data;
195 time_t tm = time(NULL);
196 uint8_t *tm_ptr = &buf[6]; /* time will go here */
197 int i, offset, ext_offset;
198 int ext_len = 0;
199
200
201 buf[0] = HS_CLIENT_HELLO;
202 buf[1] = 0;
203 buf[2] = 0;
204 /* byte 3 is calculated later */
205 buf[4] = 0x03;
206 buf[5] = ssl->version & 0x0f;
207
208 /* client random value - spec says that 1st 4 bytes are big endian time */
209 *tm_ptr++ = (uint8_t)(((long)tm & 0xff000000) >> 24);
210 *tm_ptr++ = (uint8_t)(((long)tm & 0x00ff0000) >> 16);
211 *tm_ptr++ = (uint8_t)(((long)tm & 0x0000ff00) >> 8);
212 *tm_ptr++ = (uint8_t)(((long)tm & 0x000000ff));
213 if (get_random(SSL_RANDOM_SIZE-4, &buf[10]) < 0)
214 return SSL_NOT_OK;
215
216 memcpy(ssl->dc->client_random, &buf[6], SSL_RANDOM_SIZE);
217 offset = 6 + SSL_RANDOM_SIZE;
218
219 /* give session resumption a go */
220 if (IS_SET_SSL_FLAG(SSL_SESSION_RESUME)) /* set initially by user */
221 {
222 buf[offset++] = ssl->sess_id_size;
223 memcpy(&buf[offset], ssl->session_id, ssl->sess_id_size);
224 offset += ssl->sess_id_size;
225 CLR_SSL_FLAG(SSL_SESSION_RESUME); /* clear so we can set later */
226 }
227 else
228 {
229 /* no session id - because no session resumption just yet */
230 buf[offset++] = 0;
231 }
232
233 buf[offset++] = 0; /* number of ciphers */
234 buf[offset++] = NUM_PROTOCOLS*2;/* number of ciphers */
235
236 /* put all our supported protocols in our request */
237 for (i = 0; i < NUM_PROTOCOLS; i++)
238 {
239 buf[offset++] = 0; /* cipher we are using */
240 buf[offset++] = ssl_prot_prefs[i];
241 }
242
243 buf[offset++] = 1; /* no compression */
244 buf[offset++] = 0;
245
246 ext_offset = offset;
247
248 buf[offset++] = 0; /* total length of extensions */
249 buf[offset++] = 0;
250
251 /* send the signature algorithm extension for TLS 1.2+ */
252 if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2)
253 {
254 memcpy(&buf[offset], g_sig_alg, sizeof(g_sig_alg));
255 offset += sizeof(g_sig_alg);
256 ext_len += sizeof(g_sig_alg);
257 }
258
259 if (ssl->extensions != NULL)
260 {
261 /* send the host name if specified */
262 if (ssl->extensions->host_name != NULL)
263 {
264 size_t host_len = strlen(ssl->extensions->host_name);
265 buf[offset++] = 0;
266 buf[offset++] = SSL_EXT_SERVER_NAME; /* server_name(0) (65535) */
267 buf[offset++] = 0;
268 buf[offset++] = host_len + 5; /* server_name length */
269 buf[offset++] = 0;
270 buf[offset++] = host_len + 3; /* server_list length */
271 buf[offset++] = 0; /* host_name(0) (255) */
272 buf[offset++] = 0;
273 buf[offset++] = host_len; /* host_name length */
274 strncpy((char*) &buf[offset], ssl->extensions->host_name, host_len);
275 offset += host_len;
276 ext_len += host_len + 9;
277 }
278
279 if (ssl->extensions->max_fragment_size)
280 {
281 buf[offset++] = 0;
282 buf[offset++] = SSL_EXT_MAX_FRAGMENT_SIZE;
283
284 buf[offset++] = 0; // size of data
285 buf[offset++] = 2;
286
287 buf[offset++] = (uint8_t)
288 ((ssl->extensions->max_fragment_size >> 8) & 0xff);
289 buf[offset++] = (uint8_t)
290 (ssl->extensions->max_fragment_size & 0xff);
291 ext_len += 6;
292 }
293 }
294
295 if (ext_len > 0)
296 {
297 // update the extensions length value
298 buf[ext_offset] = (uint8_t) ((ext_len >> 8) & 0xff);
299 buf[ext_offset + 1] = (uint8_t) (ext_len & 0xff);
300 }
301
302 buf[3] = offset - 4; /* handshake size */
303 return send_packet(ssl, PT_HANDSHAKE_PROTOCOL, NULL, offset);
304 }
305
306 /*
307 * Process the server hello.
308 */
309 static int process_server_hello(SSL *ssl)
310 {
311 uint8_t *buf = ssl->bm_data;
312 int pkt_size = ssl->bm_index;
313 int num_sessions = ssl->ssl_ctx->num_sessions;
314 uint8_t sess_id_size;
315 int offset, ret = SSL_OK;
316
317 /* check that we are talking to a TLSv1 server */
318 uint8_t version = (buf[4] << 4) + buf[5];
319 if (version > SSL_PROTOCOL_VERSION_MAX)
320 {
321 version = SSL_PROTOCOL_VERSION_MAX;
322 }
323 else if (ssl->version < SSL_PROTOCOL_MIN_VERSION)
324 {
325 ret = SSL_ERROR_INVALID_VERSION;
326 ssl_display_error(ret);
327 goto error;
328 }
329
330 ssl->version = version;
331
332 /* get the server random value */
333 memcpy(ssl->dc->server_random, &buf[6], SSL_RANDOM_SIZE);
334 offset = 6 + SSL_RANDOM_SIZE; /* skip of session id size */
335 sess_id_size = buf[offset++];
336
337 if (sess_id_size > SSL_SESSION_ID_SIZE)
338 {
339 ret = SSL_ERROR_INVALID_SESSION;
340 goto error;
341 }
342
343 if (num_sessions)
344 {
345 ssl->session = ssl_session_update(num_sessions,
346 ssl->ssl_ctx->ssl_sessions, ssl, &buf[offset]);
347 memcpy(ssl->session->session_id, &buf[offset], sess_id_size);
348
349 /* pad the rest with 0's */
350 if (sess_id_size < SSL_SESSION_ID_SIZE)
351 {
352 memset(&ssl->session->session_id[sess_id_size], 0,
353 SSL_SESSION_ID_SIZE-sess_id_size);
354 }
355 }
356
357 memcpy(ssl->session_id, &buf[offset], sess_id_size);
358 ssl->sess_id_size = sess_id_size;
359 offset += sess_id_size;
360
361 /* get the real cipher we are using - ignore MSB */
362 ssl->cipher = buf[++offset];
363 ssl->next_state = IS_SET_SSL_FLAG(SSL_SESSION_RESUME) ?
364 HS_FINISHED : HS_CERTIFICATE;
365
366 offset += 2; // ignore compression
367 PARANOIA_CHECK(pkt_size, offset);
368
369 ssl->dc->bm_proc_index = offset;
370 PARANOIA_CHECK(pkt_size, offset);
371
372 // no extensions
373 error:
374 return ret;
375 }
376
377 /**
378 * Process the server hello done message.
379 */
380 static int process_server_hello_done(SSL *ssl)
381 {
382 ssl->next_state = HS_FINISHED;
383 return SSL_OK;
384 }
385
386 /*
387 * Send a client key exchange message.
388 */
389 static int send_client_key_xchg(SSL *ssl)
390 {
391 uint8_t *buf = ssl->bm_data;
392 uint8_t premaster_secret[SSL_SECRET_SIZE];
393 int enc_secret_size = -1;
394
395 buf[0] = HS_CLIENT_KEY_XCHG;
396 buf[1] = 0;
397
398 // spec says client must use the what is initially negotiated -
399 // and this is our current version
400 premaster_secret[0] = 0x03;
401 premaster_secret[1] = SSL_PROTOCOL_VERSION_MAX & 0x0f;
402 if (get_random(SSL_SECRET_SIZE-2, &premaster_secret[2]) < 0)
403 return SSL_NOT_OK;
404
405 DISPLAY_RSA(ssl, ssl->x509_ctx->rsa_ctx);
406
407 /* rsa_ctx->bi_ctx is not thread-safe */
408 SSL_CTX_LOCK(ssl->ssl_ctx->mutex);
409 enc_secret_size = RSA_encrypt(ssl->x509_ctx->rsa_ctx, premaster_secret,
410 SSL_SECRET_SIZE, &buf[6], 0);
411 SSL_CTX_UNLOCK(ssl->ssl_ctx->mutex);
412
413 buf[2] = (enc_secret_size + 2) >> 8;
414 buf[3] = (enc_secret_size + 2) & 0xff;
415 buf[4] = enc_secret_size >> 8;
416 buf[5] = enc_secret_size & 0xff;
417
418 generate_master_secret(ssl, premaster_secret);
419 return send_packet(ssl, PT_HANDSHAKE_PROTOCOL, NULL, enc_secret_size+6);
420 }
421
422 /*
423 * Process the certificate request.
424 */
425 static int process_cert_req(SSL *ssl)
426 {
427 uint8_t *buf = &ssl->bm_data[ssl->dc->bm_proc_index];
428 int ret = SSL_OK;
429 int cert_req_size = (buf[2]<<8) + buf[3];
430 int offset = 4;
431 int pkt_size = ssl->bm_index;
432 uint8_t cert_type_len, sig_alg_len;
433
434 PARANOIA_CHECK(pkt_size, offset + cert_req_size);
435 ssl->dc->bm_proc_index = cert_req_size;
436
437 /* don't do any processing - we will send back an RSA certificate anyway */
438 ssl->next_state = HS_SERVER_HELLO_DONE;
439 SET_SSL_FLAG(SSL_HAS_CERT_REQ);
440
441 if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2) // TLS1.2+
442 {
443 // supported certificate types
444 cert_type_len = buf[offset++];
445 PARANOIA_CHECK(pkt_size, offset + cert_type_len);
446 offset += cert_type_len;
447
448 // supported signature algorithms
449 sig_alg_len = buf[offset++] << 8;
450 sig_alg_len += buf[offset++];
451 PARANOIA_CHECK(pkt_size, offset + sig_alg_len);
452
453 while (sig_alg_len > 0)
454 {
455 uint8_t hash_alg = buf[offset++];
456 uint8_t sig_alg = buf[offset++];
457 sig_alg_len -= 2;
458
459 if (sig_alg == SIG_ALG_RSA &&
460 (hash_alg == SIG_ALG_SHA1 ||
461 hash_alg == SIG_ALG_SHA256 ||
462 hash_alg == SIG_ALG_SHA384 ||
463 hash_alg == SIG_ALG_SHA512))
464 {
465 ssl->sig_algs[ssl->num_sig_algs++] = hash_alg;
466 }
467 }
468 }
469
470 error:
471 return ret;
472 }
473
474 /*
475 * Send a certificate verify message.
476 */
477 static int send_cert_verify(SSL *ssl)
478 {
479 uint8_t *buf = ssl->bm_data;
480 uint8_t dgst[SHA1_SIZE+MD5_SIZE+15];
481 RSA_CTX *rsa_ctx = ssl->ssl_ctx->rsa_ctx;
482 int n = 0, ret;
483 int offset = 0;
484 uint16_t dgst_len;
485
486 if (rsa_ctx == NULL)
487 return SSL_OK;
488
489 DISPLAY_RSA(ssl, rsa_ctx);
490
491 buf[0] = HS_CERT_VERIFY;
492 buf[1] = 0;
493
494 if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2) // TLS1.2+
495 {
496 buf[4] = SIG_ALG_SHA256;
497 buf[5] = SIG_ALG_RSA;
498 offset = 6;
499 memcpy(dgst, g_asn1_sha256, sizeof(g_asn1_sha256));
500 dgst_len = finished_digest(ssl, NULL, &dgst[sizeof(g_asn1_sha256)]) +
501 sizeof(g_asn1_sha256);
502 }
503 else
504 {
505 offset = 4;
506 dgst_len = finished_digest(ssl, NULL, dgst);
507 }
508
509 /* rsa_ctx->bi_ctx is not thread-safe */
510 if (rsa_ctx)
511 {
512 SSL_CTX_LOCK(ssl->ssl_ctx->mutex);
513 n = RSA_encrypt(rsa_ctx, dgst, dgst_len, &buf[offset + 2], 1);
514 SSL_CTX_UNLOCK(ssl->ssl_ctx->mutex);
515
516 if (n == 0)
517 {
518 ret = SSL_ERROR_INVALID_KEY;
519 goto error;
520 }
521 }
522
523 buf[offset] = n >> 8; /* add the RSA size */
524 buf[offset+1] = n & 0xff;
525 n += 2;
526
527 if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2) // TLS1.2+
528 {
529 n += 2; // sig/alg
530 offset -= 2;
531 }
532
533 buf[2] = n >> 8;
534 buf[3] = n & 0xff;
535 ret = send_packet(ssl, PT_HANDSHAKE_PROTOCOL, NULL, n + offset);
536
537 error:
538 return ret;
539 }
540
541 #endif /* CONFIG_SSL_ENABLE_CLIENT */