tls1_svr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. #include <stdlib.h>
  31. #include <string.h>
  32. #include <stdio.h>
  33. #include "ssl.h"
  34. static const uint8_t g_hello_done[] = { HS_SERVER_HELLO_DONE, 0, 0, 0 };
  35. static int process_client_hello(SSL *ssl);
  36. static int send_server_hello_sequence(SSL *ssl);
  37. static int send_server_hello(SSL *ssl);
  38. static int send_server_hello_done(SSL *ssl);
  39. static int process_client_key_xchg(SSL *ssl);
  40. #ifdef CONFIG_SSL_CERT_VERIFICATION
  41. static int send_certificate_request(SSL *ssl);
  42. static int process_cert_verify(SSL *ssl);
  43. #endif
  44. /*
  45. * Establish a new SSL connection to an SSL client.
  46. */
  47. EXP_FUNC SSL * STDCALL ssl_server_new(SSL_CTX *ssl_ctx, int client_fd)
  48. {
  49. SSL *ssl;
  50. ssl = ssl_new(ssl_ctx, client_fd);
  51. ssl->next_state = HS_CLIENT_HELLO;
  52. #ifdef CONFIG_SSL_FULL_MODE
  53. if (ssl_ctx->chain_length == 0)
  54. printf("Warning - no server certificate defined\n"); TTY_FLUSH();
  55. #endif
  56. return ssl;
  57. }
  58. /*
  59. * Process the handshake record.
  60. */
  61. int do_svr_handshake(SSL *ssl, int handshake_type, uint8_t *buf, int hs_len)
  62. {
  63. int ret = SSL_OK;
  64. ssl->hs_status = SSL_NOT_OK; /* not connected */
  65. /* To get here the state must be valid */
  66. switch (handshake_type)
  67. {
  68. case HS_CLIENT_HELLO:
  69. if ((ret = process_client_hello(ssl)) == SSL_OK)
  70. ret = send_server_hello_sequence(ssl);
  71. break;
  72. #ifdef CONFIG_SSL_CERT_VERIFICATION
  73. case HS_CERTIFICATE:/* the client sends its cert */
  74. ret = process_certificate(ssl, &ssl->x509_ctx);
  75. if (ret == SSL_OK) /* verify the cert */
  76. {
  77. int cert_res;
  78. cert_res = x509_verify(
  79. ssl->ssl_ctx->ca_cert_ctx, ssl->x509_ctx);
  80. ret = (cert_res == 0) ? SSL_OK : SSL_X509_ERROR(cert_res);
  81. }
  82. break;
  83. case HS_CERT_VERIFY:
  84. ret = process_cert_verify(ssl);
  85. add_packet(ssl, buf, hs_len); /* needs to be done after */
  86. break;
  87. #endif
  88. case HS_CLIENT_KEY_XCHG:
  89. ret = process_client_key_xchg(ssl);
  90. break;
  91. case HS_FINISHED:
  92. ret = process_finished(ssl, hs_len);
  93. disposable_free(ssl); /* free up some memory */
  94. break;
  95. }
  96. return ret;
  97. }
  98. /*
  99. * Process a client hello message.
  100. */
  101. static int process_client_hello(SSL *ssl)
  102. {
  103. uint8_t *buf = ssl->bm_data;
  104. uint8_t *record_buf = ssl->hmac_header;
  105. int pkt_size = ssl->bm_index;
  106. int i, j, cs_len, id_len, offset = 6 + SSL_RANDOM_SIZE;
  107. int version = (record_buf[1] << 4) + record_buf[2];
  108. int ret = SSL_OK;
  109. /* should be v3.1 (TLSv1) or better - we'll send in v3.1 mode anyway */
  110. if (version < 0x31)
  111. {
  112. ret = SSL_ERROR_INVALID_VERSION;
  113. ssl_display_error(ret);
  114. goto error;
  115. }
  116. memcpy(ssl->dc->client_random, &buf[6], SSL_RANDOM_SIZE);
  117. /* process the session id */
  118. id_len = buf[offset++];
  119. if (id_len > SSL_SESSION_ID_SIZE)
  120. {
  121. return SSL_ERROR_INVALID_SESSION;
  122. }
  123. #ifndef CONFIG_SSL_SKELETON_MODE
  124. ssl->session = ssl_session_update(ssl->ssl_ctx->num_sessions,
  125. ssl->ssl_ctx->ssl_sessions, ssl, id_len ? &buf[offset] : NULL);
  126. #endif
  127. offset += id_len;
  128. cs_len = (buf[offset]<<8) + buf[offset+1];
  129. offset += 3; /* add 1 due to all cipher suites being 8 bit */
  130. PARANOIA_CHECK(pkt_size, offset);
  131. /* work out what cipher suite we are going to use */
  132. for (j = 0; j < NUM_PROTOCOLS; j++)
  133. {
  134. for (i = 0; i < cs_len; i += 2)
  135. {
  136. if (ssl_prot_prefs[j] == buf[offset+i]) /* got a match? */
  137. {
  138. ssl->cipher = ssl_prot_prefs[j];
  139. goto do_state;
  140. }
  141. }
  142. }
  143. /* ouch! protocol is not supported */
  144. ret = SSL_ERROR_NO_CIPHER;
  145. do_state:
  146. error:
  147. return ret;
  148. }
  149. #ifdef CONFIG_SSL_ENABLE_V23_HANDSHAKE
  150. /*
  151. * Some browsers use a hybrid SSLv2 "client hello"
  152. */
  153. int process_sslv23_client_hello(SSL *ssl)
  154. {
  155. uint8_t *buf = ssl->bm_data;
  156. int bytes_needed = ((buf[0] & 0x7f) << 8) + buf[1];
  157. int version = (buf[3] << 4) + buf[4];
  158. int ret = SSL_OK;
  159. /* we have already read 3 extra bytes so far */
  160. int read_len = SOCKET_READ(ssl->client_fd, buf, bytes_needed-3);
  161. int cs_len = buf[1];
  162. int id_len = buf[3];
  163. int ch_len = buf[5];
  164. int i, j, offset = 8; /* start at first cipher */
  165. int random_offset = 0;
  166. DISPLAY_BYTES(ssl, "received %d bytes", buf, read_len, read_len);
  167. /* should be v3.1 (TLSv1) or better - we'll send in v3.1 mode anyway */
  168. if (version < 0x31)
  169. {
  170. return SSL_ERROR_INVALID_VERSION;
  171. }
  172. add_packet(ssl, buf, read_len);
  173. /* connection has gone, so die */
  174. if (bytes_needed < 0)
  175. {
  176. return SSL_ERROR_CONN_LOST;
  177. }
  178. /* now work out what cipher suite we are going to use */
  179. for (j = 0; j < NUM_PROTOCOLS; j++)
  180. {
  181. for (i = 0; i < cs_len; i += 3)
  182. {
  183. if (ssl_prot_prefs[j] == buf[offset+i])
  184. {
  185. ssl->cipher = ssl_prot_prefs[j];
  186. goto server_hello;
  187. }
  188. }
  189. }
  190. /* ouch! protocol is not supported */
  191. ret = SSL_ERROR_NO_CIPHER;
  192. goto error;
  193. server_hello:
  194. /* get the session id */
  195. offset += cs_len - 2; /* we've gone 2 bytes past the end */
  196. #ifndef CONFIG_SSL_SKELETON_MODE
  197. ssl->session = ssl_session_update(ssl->ssl_ctx->num_sessions,
  198. ssl->ssl_ctx->ssl_sessions, ssl, id_len ? &buf[offset] : NULL);
  199. #endif
  200. /* get the client random data */
  201. offset += id_len;
  202. /* random can be anywhere between 16 and 32 bytes long - so it is padded
  203. * with 0's to the left */
  204. if (ch_len == 0x10)
  205. {
  206. random_offset += 0x10;
  207. }
  208. memcpy(&ssl->dc->client_random[random_offset], &buf[offset], ch_len);
  209. ret = send_server_hello_sequence(ssl);
  210. error:
  211. return ret;
  212. }
  213. #endif
  214. /*
  215. * Send the entire server hello sequence
  216. */
  217. static int send_server_hello_sequence(SSL *ssl)
  218. {
  219. int ret;
  220. if ((ret = send_server_hello(ssl)) == SSL_OK)
  221. {
  222. #ifndef CONFIG_SSL_SKELETON_MODE
  223. /* resume handshake? */
  224. if (IS_SET_SSL_FLAG(SSL_SESSION_RESUME))
  225. {
  226. if ((ret = send_change_cipher_spec(ssl)) == SSL_OK)
  227. {
  228. ret = send_finished(ssl);
  229. ssl->next_state = HS_FINISHED;
  230. }
  231. }
  232. else
  233. #endif
  234. if ((ret = send_certificate(ssl)) == SSL_OK)
  235. {
  236. #ifdef CONFIG_SSL_CERT_VERIFICATION
  237. /* ask the client for its certificate */
  238. if (IS_SET_SSL_FLAG(SSL_CLIENT_AUTHENTICATION))
  239. {
  240. if ((ret = send_certificate_request(ssl)) == SSL_OK)
  241. {
  242. ret = send_server_hello_done(ssl);
  243. ssl->next_state = HS_CERTIFICATE;
  244. }
  245. }
  246. else
  247. #endif
  248. {
  249. ret = send_server_hello_done(ssl);
  250. ssl->next_state = HS_CLIENT_KEY_XCHG;
  251. }
  252. }
  253. }
  254. return ret;
  255. }
  256. /*
  257. * Send a server hello message.
  258. */
  259. static int send_server_hello(SSL *ssl)
  260. {
  261. uint8_t *buf = ssl->bm_data;
  262. int offset = 0;
  263. buf[0] = HS_SERVER_HELLO;
  264. buf[1] = 0;
  265. buf[2] = 0;
  266. /* byte 3 is calculated later */
  267. buf[4] = 0x03;
  268. buf[5] = 0x01;
  269. /* server random value */
  270. get_random(SSL_RANDOM_SIZE, &buf[6]);
  271. memcpy(ssl->dc->server_random, &buf[6], SSL_RANDOM_SIZE);
  272. offset = 6 + SSL_RANDOM_SIZE;
  273. #ifndef CONFIG_SSL_SKELETON_MODE
  274. if (IS_SET_SSL_FLAG(SSL_SESSION_RESUME))
  275. {
  276. /* retrieve id from session cache */
  277. buf[offset++] = SSL_SESSION_ID_SIZE;
  278. memcpy(&buf[offset], ssl->session->session_id, SSL_SESSION_ID_SIZE);
  279. memcpy(ssl->session_id, ssl->session->session_id, SSL_SESSION_ID_SIZE);
  280. ssl->sess_id_size = SSL_SESSION_ID_SIZE;
  281. offset += SSL_SESSION_ID_SIZE;
  282. }
  283. else /* generate our own session id */
  284. #endif
  285. {
  286. #ifndef CONFIG_SSL_SKELETON_MODE
  287. buf[offset++] = SSL_SESSION_ID_SIZE;
  288. get_random(SSL_SESSION_ID_SIZE, &buf[offset]);
  289. memcpy(ssl->session_id, &buf[offset], SSL_SESSION_ID_SIZE);
  290. ssl->sess_id_size = SSL_SESSION_ID_SIZE;
  291. /* store id in session cache */
  292. if (ssl->ssl_ctx->num_sessions)
  293. {
  294. memcpy(ssl->session->session_id,
  295. ssl->session_id, SSL_SESSION_ID_SIZE);
  296. }
  297. offset += SSL_SESSION_ID_SIZE;
  298. #else
  299. buf[offset++] = 0; /* don't bother with session id in skelton mode */
  300. #endif
  301. }
  302. buf[offset++] = 0; /* cipher we are using */
  303. buf[offset++] = ssl->cipher;
  304. buf[offset++] = 0; /* no compression */
  305. buf[3] = offset - 4; /* handshake size */
  306. return send_packet(ssl, PT_HANDSHAKE_PROTOCOL, NULL, offset);
  307. }
  308. /*
  309. * Send the server hello done message.
  310. */
  311. static int send_server_hello_done(SSL *ssl)
  312. {
  313. return send_packet(ssl, PT_HANDSHAKE_PROTOCOL,
  314. g_hello_done, sizeof(g_hello_done));
  315. }
  316. /*
  317. * Pull apart a client key exchange message. Decrypt the pre-master key (using
  318. * our RSA private key) and then work out the master key. Initialise the
  319. * ciphers.
  320. */
  321. static int process_client_key_xchg(SSL *ssl)
  322. {
  323. uint8_t *buf = &ssl->bm_data[ssl->dc->bm_proc_index];
  324. int pkt_size = ssl->bm_index;
  325. int premaster_size, secret_length = (buf[2] << 8) + buf[3];
  326. uint8_t premaster_secret[MAX_KEY_BYTE_SIZE];
  327. RSA_CTX *rsa_ctx = ssl->ssl_ctx->rsa_ctx;
  328. int offset = 4;
  329. int ret = SSL_OK;
  330. if (rsa_ctx == NULL)
  331. {
  332. ret = SSL_ERROR_NO_CERT_DEFINED;
  333. goto error;
  334. }
  335. /* is there an extra size field? */
  336. if ((secret_length - 2) == rsa_ctx->num_octets)
  337. offset += 2;
  338. PARANOIA_CHECK(pkt_size, rsa_ctx->num_octets+offset);
  339. /* rsa_ctx->bi_ctx is not thread-safe */
  340. SSL_CTX_LOCK(ssl->ssl_ctx->mutex);
  341. premaster_size = RSA_decrypt(rsa_ctx, &buf[offset], premaster_secret, 1);
  342. SSL_CTX_UNLOCK(ssl->ssl_ctx->mutex);
  343. if (premaster_size != SSL_SECRET_SIZE ||
  344. premaster_secret[0] != 0x03 || /* check version is 3.1 (TLS) */
  345. premaster_secret[1] != 0x01)
  346. {
  347. /* guard against a Bleichenbacher attack */
  348. memset(premaster_secret, 0, SSL_SECRET_SIZE);
  349. /* and continue - will die eventually when checking the mac */
  350. }
  351. #if 0
  352. print_blob("pre-master", premaster_secret, SSL_SECRET_SIZE);
  353. #endif
  354. generate_master_secret(ssl, premaster_secret);
  355. #ifdef CONFIG_SSL_CERT_VERIFICATION
  356. ssl->next_state = IS_SET_SSL_FLAG(SSL_CLIENT_AUTHENTICATION) ?
  357. HS_CERT_VERIFY : HS_FINISHED;
  358. #else
  359. ssl->next_state = HS_FINISHED;
  360. #endif
  361. error:
  362. ssl->dc->bm_proc_index += rsa_ctx->num_octets+offset;
  363. return ret;
  364. }
  365. #ifdef CONFIG_SSL_CERT_VERIFICATION
  366. static const uint8_t g_cert_request[] = { HS_CERT_REQ, 0, 0, 4, 1, 0, 0, 0 };
  367. /*
  368. * Send the certificate request message.
  369. */
  370. static int send_certificate_request(SSL *ssl)
  371. {
  372. return send_packet(ssl, PT_HANDSHAKE_PROTOCOL,
  373. g_cert_request, sizeof(g_cert_request));
  374. }
  375. /*
  376. * Ensure the client has the private key by first decrypting the packet and
  377. * then checking the packet digests.
  378. */
  379. static int process_cert_verify(SSL *ssl)
  380. {
  381. uint8_t *buf = &ssl->bm_data[ssl->dc->bm_proc_index];
  382. int pkt_size = ssl->bm_index;
  383. uint8_t dgst_buf[MAX_KEY_BYTE_SIZE];
  384. uint8_t dgst[MD5_SIZE+SHA1_SIZE];
  385. X509_CTX *x509_ctx = ssl->x509_ctx;
  386. int ret = SSL_OK;
  387. int n;
  388. PARANOIA_CHECK(pkt_size, x509_ctx->rsa_ctx->num_octets+6);
  389. DISPLAY_RSA(ssl, x509_ctx->rsa_ctx);
  390. /* rsa_ctx->bi_ctx is not thread-safe */
  391. SSL_CTX_LOCK(ssl->ssl_ctx->mutex);
  392. n = RSA_decrypt(x509_ctx->rsa_ctx, &buf[6], dgst_buf, 0);
  393. SSL_CTX_UNLOCK(ssl->ssl_ctx->mutex);
  394. if (n != SHA1_SIZE + MD5_SIZE)
  395. {
  396. ret = SSL_ERROR_INVALID_KEY;
  397. goto end_cert_vfy;
  398. }
  399. finished_digest(ssl, NULL, dgst); /* calculate the digest */
  400. if (memcmp(dgst_buf, dgst, MD5_SIZE + SHA1_SIZE))
  401. {
  402. ret = SSL_ERROR_INVALID_KEY;
  403. }
  404. end_cert_vfy:
  405. ssl->next_state = HS_FINISHED;
  406. error:
  407. return ret;
  408. }
  409. #endif