tls1_svr.c 15 KB

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