tls1_clnt.c 15 KB

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