Debian Codegraph @

ccast/axTLS/tls1_svr.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2007-2017, 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 <stdio.h>
34 #include "os_port.h"
35 #include "ssl.h"
36
37 static const uint8_t g_hello_done[] = { HS_SERVER_HELLO_DONE, 0, 0, 0 };
38 static const uint8_t g_asn1_sha256[] =
39 {
40 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
41 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20
42 };
43
44 static int process_client_hello(SSL *ssl);
45 static int send_server_hello_sequence(SSL *ssl);
46 static int send_server_hello(SSL *ssl);
47 static int send_server_hello_done(SSL *ssl);
48 static int process_client_key_xchg(SSL *ssl);
49 #ifdef CONFIG_SSL_CERT_VERIFICATION
50 static int send_certificate_request(SSL *ssl);
51 static int process_cert_verify(SSL *ssl);
52 #endif
53
54 /*
55 * Establish a new SSL connection to an SSL client.
56 */
57 EXP_FUNC SSL * STDCALL ssl_server_new(SSL_CTX *ssl_ctx, int client_fd)
58 {
59 SSL *ssl;
60
61 ssl = ssl_new(ssl_ctx, client_fd);
62 ssl->next_state = HS_CLIENT_HELLO;
63
64 #ifdef CONFIG_DEBUG
65 if (ssl_ctx->chain_length == 0)
66 printf("Warning - no server certificate defined\n"); TTY_FLUSH();
67 #endif
68
69 return ssl;
70 }
71
72 /*
73 * Process the handshake record.
74 */
75 int do_svr_handshake(SSL *ssl, int handshake_type, uint8_t *buf, int hs_len)
76 {
77 int ret = SSL_OK;
78 ssl->hs_status = SSL_NOT_OK; /* not connected */
79
80 /* To get here the state must be valid */
81 switch (handshake_type)
82 {
83 case HS_CLIENT_HELLO:
84 if ((ret = process_client_hello(ssl)) == SSL_OK)
85 ret = send_server_hello_sequence(ssl);
86 break;
87
88 #ifdef CONFIG_SSL_CERT_VERIFICATION
89 case HS_CERTIFICATE:/* the client sends its cert */
90 ret = process_certificate(ssl, &ssl->x509_ctx);
91
92 if (ret == SSL_OK) /* verify the cert */
93 {
94 int cert_res;
95 int pathLenConstraint = 0;
96
97 cert_res = x509_verify(ssl->ssl_ctx->ca_cert_ctx,
98 ssl->x509_ctx, &pathLenConstraint);
99 ret = (cert_res == 0) ? SSL_OK : SSL_X509_ERROR(cert_res);
100 }
101 break;
102
103 case HS_CERT_VERIFY:
104 ret = process_cert_verify(ssl);
105 add_packet(ssl, buf, hs_len); /* needs to be done after */
106 break;
107 #endif
108 case HS_CLIENT_KEY_XCHG:
109 ret = process_client_key_xchg(ssl);
110 break;
111
112 case HS_FINISHED:
113 ret = process_finished(ssl, buf, hs_len);
114 disposable_free(ssl); /* free up some memory */
115 break;
116 }
117
118 return ret;
119 }
120
121 /*
122 * Process a client hello message.
123 */
124 static int process_client_hello(SSL *ssl)
125 {
126 uint8_t *buf = ssl->bm_data;
127 int pkt_size = ssl->bm_index;
128 int i, j, cs_len, id_len, offset = 6 + SSL_RANDOM_SIZE;
129 int ret = SSL_OK;
130
131 uint8_t version = (buf[4] << 4) + buf[5];
132 ssl->version = ssl->client_version = version;
133
134 if (version > SSL_PROTOCOL_VERSION_MAX)
135 {
136 /* use client's version instead */
137 ssl->version = SSL_PROTOCOL_VERSION_MAX;
138 }
139 else if (version < SSL_PROTOCOL_MIN_VERSION) /* old version supported? */
140 {
141 ret = SSL_ERROR_INVALID_VERSION;
142 ssl_display_error(ret);
143 goto error;
144 }
145
146 memcpy(ssl->dc->client_random, &buf[6], SSL_RANDOM_SIZE);
147
148 /* process the session id */
149 id_len = buf[offset++];
150 if (id_len > SSL_SESSION_ID_SIZE)
151 {
152 return SSL_ERROR_INVALID_SESSION;
153 }
154
155 #ifndef CONFIG_SSL_SKELETON_MODE
156 ssl->session = ssl_session_update(ssl->ssl_ctx->num_sessions,
157 ssl->ssl_ctx->ssl_sessions, ssl, id_len ? &buf[offset] : NULL);
158 #endif
159
160 offset += id_len;
161 cs_len = (buf[offset]<<8) + buf[offset+1];
162 offset += 2;
163
164 PARANOIA_CHECK(pkt_size, offset + cs_len);
165
166 /* work out what cipher suite we are going to use - client defines
167 the preference */
168 for (i = 0; i < cs_len; i += 2)
169 {
170 /* only support ciphersuites with the form (0x00, xxxx) */
171 if (buf[offset+i])
172 continue;
173
174 for (j = 0; j < NUM_PROTOCOLS; j++)
175 {
176 if (ssl_prot_prefs[j] == buf[offset+i+1]) /* got a match? */
177 {
178 ssl->cipher = ssl_prot_prefs[j];
179 goto do_compression;
180 }
181 }
182 }
183
184 /* ouch! protocol is not supported */
185 return SSL_ERROR_NO_CIPHER;
186
187 /* completely ignore compression */
188 do_compression:
189 offset += cs_len;
190 id_len = buf[offset++];
191 offset += id_len;
192 PARANOIA_CHECK(pkt_size, offset + id_len);
193
194 if (offset == pkt_size)
195 {
196 /* no extensions */
197 goto error;
198 }
199
200 /* extension size */
201 id_len = buf[offset++] << 8;
202 id_len += buf[offset++];
203 PARANOIA_CHECK(pkt_size, offset + id_len);
204
205 // Check for extensions from the client - only the signature algorithm
206 // is supported
207 while (offset < pkt_size)
208 {
209 int ext, ext_len;
210
211 ext = buf[offset++] << 8;
212 ext += buf[offset++];
213 ext_len = buf[offset++] << 8;
214 ext_len += buf[offset++];
215 PARANOIA_CHECK(pkt_size, offset + ext_len);
216
217 if (ext == SSL_EXT_SIG_ALG)
218 {
219 while (ext_len > 0)
220 {
221 uint8_t hash_alg = buf[offset++];
222 uint8_t sig_alg = buf[offset++];
223 ext_len -= 2;
224
225 if (sig_alg == SIG_ALG_RSA &&
226 (hash_alg == SIG_ALG_SHA1 ||
227 hash_alg == SIG_ALG_SHA256 ||
228 hash_alg == SIG_ALG_SHA384 ||
229 hash_alg == SIG_ALG_SHA512))
230 {
231 ssl->sig_algs[ssl->num_sig_algs++] = hash_alg;
232 }
233 }
234 }
235 else
236 {
237 offset += ext_len;
238 }
239 }
240
241 /* default is RSA/SHA1 */
242 if (ssl->num_sig_algs == 0)
243 {
244 ssl->sig_algs[ssl->num_sig_algs++] = SIG_ALG_SHA1;
245 }
246
247 error:
248 return ret;
249 }
250
251 /*
252 * Send the entire server hello sequence
253 */
254 static int send_server_hello_sequence(SSL *ssl)
255 {
256 int ret;
257
258 if ((ret = send_server_hello(ssl)) == SSL_OK)
259 {
260 #ifndef CONFIG_SSL_SKELETON_MODE
261 /* resume handshake? */
262 if (IS_SET_SSL_FLAG(SSL_SESSION_RESUME))
263 {
264 if ((ret = send_change_cipher_spec(ssl)) == SSL_OK)
265 {
266 ret = send_finished(ssl);
267 ssl->next_state = HS_FINISHED;
268 }
269 }
270 else
271 #endif
272 if ((ret = send_certificate(ssl)) == SSL_OK)
273 {
274 #ifdef CONFIG_SSL_CERT_VERIFICATION
275 /* ask the client for its certificate */
276 if (IS_SET_SSL_FLAG(SSL_CLIENT_AUTHENTICATION))
277 {
278 if ((ret = send_certificate_request(ssl)) == SSL_OK)
279 {
280 ret = send_server_hello_done(ssl);
281 ssl->next_state = HS_CERTIFICATE;
282 }
283 }
284 else
285 #endif
286 {
287 ret = send_server_hello_done(ssl);
288 ssl->next_state = HS_CLIENT_KEY_XCHG;
289 }
290 }
291 }
292
293 return ret;
294 }
295
296 /*
297 * Send a server hello message.
298 */
299 static int send_server_hello(SSL *ssl)
300 {
301 uint8_t *buf = ssl->bm_data;
302 int offset = 0;
303
304 buf[0] = HS_SERVER_HELLO;
305 buf[1] = 0;
306 buf[2] = 0;
307 /* byte 3 is calculated later */
308 buf[4] = 0x03;
309 buf[5] = ssl->version & 0x0f;
310
311 /* server random value */
312 if (get_random(SSL_RANDOM_SIZE, &buf[6]) < 0)
313 return SSL_NOT_OK;
314
315 memcpy(ssl->dc->server_random, &buf[6], SSL_RANDOM_SIZE);
316 offset = 6 + SSL_RANDOM_SIZE;
317
318 #ifndef CONFIG_SSL_SKELETON_MODE
319 if (IS_SET_SSL_FLAG(SSL_SESSION_RESUME))
320 {
321 /* retrieve id from session cache */
322 buf[offset++] = SSL_SESSION_ID_SIZE;
323 memcpy(&buf[offset], ssl->session->session_id, SSL_SESSION_ID_SIZE);
324 memcpy(ssl->session_id, ssl->session->session_id, SSL_SESSION_ID_SIZE);
325 ssl->sess_id_size = SSL_SESSION_ID_SIZE;
326 offset += SSL_SESSION_ID_SIZE;
327 }
328 else /* generate our own session id */
329 #endif
330 {
331 #ifndef CONFIG_SSL_SKELETON_MODE
332 buf[offset++] = SSL_SESSION_ID_SIZE;
333 get_random(SSL_SESSION_ID_SIZE, &buf[offset]);
334 memcpy(ssl->session_id, &buf[offset], SSL_SESSION_ID_SIZE);
335 ssl->sess_id_size = SSL_SESSION_ID_SIZE;
336
337 /* store id in session cache */
338 if (ssl->ssl_ctx->num_sessions)
339 {
340 memcpy(ssl->session->session_id,
341 ssl->session_id, SSL_SESSION_ID_SIZE);
342 }
343
344 offset += SSL_SESSION_ID_SIZE;
345 #else
346 buf[offset++] = 0; /* don't bother with session id in skelton mode */
347 #endif
348 }
349
350 buf[offset++] = 0; /* cipher we are using */
351 buf[offset++] = ssl->cipher;
352 buf[offset++] = 0; /* no compression and no extensions supported */
353 buf[3] = offset - 4; /* handshake size */
354 return send_packet(ssl, PT_HANDSHAKE_PROTOCOL, NULL, offset);
355 }
356
357 /*
358 * Send the server hello done message.
359 */
360 static int send_server_hello_done(SSL *ssl)
361 {
362 return send_packet(ssl, PT_HANDSHAKE_PROTOCOL,
363 g_hello_done, sizeof(g_hello_done));
364 }
365
366 /*
367 * Pull apart a client key exchange message. Decrypt the pre-master key (using
368 * our RSA private key) and then work out the master key. Initialise the
369 * ciphers.
370 */
371 static int process_client_key_xchg(SSL *ssl)
372 {
373 uint8_t *buf = &ssl->bm_data[ssl->dc->bm_proc_index];
374 int pkt_size = ssl->bm_index;
375 int premaster_size, secret_length = (buf[2] << 8) + buf[3];
376 uint8_t premaster_secret[MAX_KEY_BYTE_SIZE];
377 RSA_CTX *rsa_ctx = ssl->ssl_ctx->rsa_ctx;
378 int offset = 4;
379 int ret = SSL_OK;
380
381 if (rsa_ctx == NULL)
382 {
383 ret = SSL_ERROR_NO_CERT_DEFINED;
384 goto error;
385 }
386
387 /* is there an extra size field? */
388 if ((secret_length - 2) == rsa_ctx->num_octets)
389 offset += 2;
390
391 PARANOIA_CHECK(pkt_size, rsa_ctx->num_octets+offset);
392
393 /* rsa_ctx->bi_ctx is not thread-safe */
394 SSL_CTX_LOCK(ssl->ssl_ctx->mutex);
395 premaster_size = RSA_decrypt(rsa_ctx, &buf[offset], premaster_secret,
396 sizeof(premaster_secret), 1);
397 SSL_CTX_UNLOCK(ssl->ssl_ctx->mutex);
398
399 if (premaster_size != SSL_SECRET_SIZE ||
400 premaster_secret[0] != 0x03 || /* must be the same as client
401 offered version */
402 premaster_secret[1] != (ssl->client_version & 0x0f))
403 {
404 /* guard against a Bleichenbacher attack */
405 if (get_random(SSL_SECRET_SIZE, premaster_secret) < 0)
406 return SSL_NOT_OK;
407
408 /* and continue - will die eventually when checking the mac */
409 }
410
411 generate_master_secret(ssl, premaster_secret);
412
413 #ifdef CONFIG_SSL_CERT_VERIFICATION
414 ssl->next_state = IS_SET_SSL_FLAG(SSL_CLIENT_AUTHENTICATION) ?
415 HS_CERT_VERIFY : HS_FINISHED;
416 #else
417 ssl->next_state = HS_FINISHED;
418 #endif
419
420 ssl->dc->bm_proc_index += rsa_ctx->num_octets+offset;
421 error:
422 return ret;
423 }
424
425 #ifdef CONFIG_SSL_CERT_VERIFICATION
426 static const uint8_t g_cert_request[] = { HS_CERT_REQ, 0,
427 0, 0x0e,
428 1, 1, // rsa sign
429 0x00, 0x08,
430 SIG_ALG_SHA256, SIG_ALG_RSA,
431 SIG_ALG_SHA512, SIG_ALG_RSA,
432 SIG_ALG_SHA384, SIG_ALG_RSA,
433 SIG_ALG_SHA1, SIG_ALG_RSA,
434 0, 0
435 };
436
437 static const uint8_t g_cert_request_v1[] = { HS_CERT_REQ, 0, 0, 4, 1, 0, 0, 0 };
438
439 /*
440 * Send the certificate request message.
441 */
442 static int send_certificate_request(SSL *ssl)
443 {
444 if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2) // TLS1.2+
445 {
446 return send_packet(ssl, PT_HANDSHAKE_PROTOCOL,
447 g_cert_request, sizeof(g_cert_request));
448 }
449 else
450 {
451 return send_packet(ssl, PT_HANDSHAKE_PROTOCOL,
452 g_cert_request_v1, sizeof(g_cert_request_v1));
453 }
454 }
455
456 /*
457 * Ensure the client has the private key by first decrypting the packet and
458 * then checking the packet digests.
459 */
460 static int process_cert_verify(SSL *ssl)
461 {
462 uint8_t *buf = &ssl->bm_data[ssl->dc->bm_proc_index];
463 int pkt_size = ssl->bm_index;
464 uint8_t dgst_buf[MAX_KEY_BYTE_SIZE];
465 uint8_t dgst[MD5_SIZE + SHA1_SIZE];
466 X509_CTX *x509_ctx = ssl->x509_ctx;
467 int ret = SSL_OK;
468 int offset = 6;
469 int rsa_len;
470 int n;
471
472 DISPLAY_RSA(ssl, x509_ctx->rsa_ctx);
473
474 if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2) // TLS1.2+
475 {
476 // TODO: should really need to be able to handle other algorihms. An
477 // assumption is made on RSA/SHA256 and appears to be OK.
478 //uint8_t hash_alg = buf[4];
479 //uint8_t sig_alg = buf[5];
480 offset = 8;
481 rsa_len = (buf[6] << 8) + buf[7];
482 }
483 else
484 {
485 rsa_len = (buf[4] << 8) + buf[5];
486 }
487
488 PARANOIA_CHECK(pkt_size, offset + rsa_len);
489
490 /* rsa_ctx->bi_ctx is not thread-safe */
491 SSL_CTX_LOCK(ssl->ssl_ctx->mutex);
492 n = RSA_decrypt(x509_ctx->rsa_ctx, &buf[offset], dgst_buf,
493 sizeof(dgst_buf), 0);
494 SSL_CTX_UNLOCK(ssl->ssl_ctx->mutex);
495
496 if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2) // TLS1.2+
497 {
498 if (memcmp(dgst_buf, g_asn1_sha256, sizeof(g_asn1_sha256)))
499 {
500 ret = SSL_ERROR_INVALID_KEY;
501 goto error;
502 }
503
504 finished_digest(ssl, NULL, dgst); /* calculate the digest */
505 if (memcmp(&dgst_buf[sizeof(g_asn1_sha256)], dgst, SHA256_SIZE))
506 {
507 ret = SSL_ERROR_INVALID_KEY;
508 goto error;
509 }
510 }
511 else // TLS1.0/1.1
512 {
513 if (n != SHA1_SIZE + MD5_SIZE)
514 {
515 ret = SSL_ERROR_INVALID_KEY;
516 goto end_cert_vfy;
517 }
518
519 finished_digest(ssl, NULL, dgst); /* calculate the digest */
520 if (memcmp(dgst_buf, dgst, MD5_SIZE + SHA1_SIZE))
521 {
522 ret = SSL_ERROR_INVALID_KEY;
523 }
524 }
525
526 end_cert_vfy:
527 ssl->next_state = HS_FINISHED;
528 error:
529 return ret;
530 }
531
532 #endif