t1_lib.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. /* ssl/t1_lib.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #include <stdio.h>
  59. #include <openssl/objects.h>
  60. #include <openssl/evp.h>
  61. #include <openssl/hmac.h>
  62. #include "ssl_locl.h"
  63. const char tls1_version_str[]="TLSv1" OPENSSL_VERSION_PTEXT;
  64. #ifndef OPENSSL_NO_TLSEXT
  65. static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, int ticklen,
  66. const unsigned char *sess_id, int sesslen,
  67. SSL_SESSION **psess);
  68. #endif
  69. SSL3_ENC_METHOD TLSv1_enc_data={
  70. tls1_enc,
  71. tls1_mac,
  72. tls1_setup_key_block,
  73. tls1_generate_master_secret,
  74. tls1_change_cipher_state,
  75. tls1_final_finish_mac,
  76. TLS1_FINISH_MAC_LENGTH,
  77. tls1_cert_verify_mac,
  78. TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
  79. TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
  80. tls1_alert_code,
  81. };
  82. long tls1_default_timeout(void)
  83. {
  84. /* 2 hours, the 24 hours mentioned in the TLSv1 spec
  85. * is way too long for http, the cache would over fill */
  86. return(60*60*2);
  87. }
  88. IMPLEMENT_tls1_meth_func(tlsv1_base_method,
  89. ssl_undefined_function,
  90. ssl_undefined_function,
  91. ssl_bad_method)
  92. int tls1_new(SSL *s)
  93. {
  94. if (!ssl3_new(s)) return(0);
  95. s->method->ssl_clear(s);
  96. return(1);
  97. }
  98. void tls1_free(SSL *s)
  99. {
  100. ssl3_free(s);
  101. }
  102. void tls1_clear(SSL *s)
  103. {
  104. ssl3_clear(s);
  105. s->version=TLS1_VERSION;
  106. }
  107. #if 0
  108. long tls1_ctrl(SSL *s, int cmd, long larg, char *parg)
  109. {
  110. return(0);
  111. }
  112. long tls1_callback_ctrl(SSL *s, int cmd, void *(*fp)())
  113. {
  114. return(0);
  115. }
  116. #endif
  117. #ifndef OPENSSL_NO_TLSEXT
  118. unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
  119. {
  120. int extdatalen=0;
  121. unsigned char *ret = p;
  122. ret+=2;
  123. if (ret>=limit) return NULL; /* this really never occurs, but ... */
  124. if (s->tlsext_hostname != NULL)
  125. {
  126. /* Add TLS extension servername to the Client Hello message */
  127. unsigned long size_str;
  128. long lenmax;
  129. /* check for enough space.
  130. 4 for the servername type and entension length
  131. 2 for servernamelist length
  132. 1 for the hostname type
  133. 2 for hostname length
  134. + hostname length
  135. */
  136. if ((lenmax = limit - ret - 9) < 0
  137. || (size_str = strlen(s->tlsext_hostname)) > (unsigned long)lenmax)
  138. return NULL;
  139. /* extension type and length */
  140. s2n(TLSEXT_TYPE_server_name,ret);
  141. s2n(size_str+5,ret);
  142. /* length of servername list */
  143. s2n(size_str+3,ret);
  144. /* hostname type, length and hostname */
  145. *(ret++) = (unsigned char) TLSEXT_NAMETYPE_host_name;
  146. s2n(size_str,ret);
  147. memcpy(ret, s->tlsext_hostname, size_str);
  148. ret+=size_str;
  149. }
  150. if (!(SSL_get_options(s) & SSL_OP_NO_TICKET))
  151. {
  152. int ticklen;
  153. if (s->session && s->session->tlsext_tick)
  154. ticklen = s->session->tlsext_ticklen;
  155. else
  156. ticklen = 0;
  157. /* Check for enough room 2 for extension type, 2 for len
  158. * rest for ticket
  159. */
  160. if (limit - ret - 4 - ticklen < 0)
  161. return NULL;
  162. s2n(TLSEXT_TYPE_session_ticket,ret);
  163. s2n(ticklen,ret);
  164. if (ticklen)
  165. {
  166. memcpy(ret, s->session->tlsext_tick, ticklen);
  167. ret += ticklen;
  168. }
  169. }
  170. if ((extdatalen = ret-p-2)== 0)
  171. return p;
  172. s2n(extdatalen,p);
  173. return ret;
  174. }
  175. unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
  176. {
  177. int extdatalen=0;
  178. unsigned char *ret = p;
  179. ret+=2;
  180. if (ret>=limit) return NULL; /* this really never occurs, but ... */
  181. if (!s->hit && s->servername_done == 1 && s->session->tlsext_hostname != NULL)
  182. {
  183. if (limit - ret - 4 < 0) return NULL;
  184. s2n(TLSEXT_TYPE_server_name,ret);
  185. s2n(0,ret);
  186. }
  187. if (s->tlsext_ticket_expected
  188. && !(SSL_get_options(s) & SSL_OP_NO_TICKET))
  189. {
  190. if (limit - ret - 4 < 0) return NULL;
  191. s2n(TLSEXT_TYPE_session_ticket,ret);
  192. s2n(0,ret);
  193. }
  194. if ((extdatalen = ret-p-2)== 0)
  195. return p;
  196. s2n(extdatalen,p);
  197. return ret;
  198. }
  199. int ssl_parse_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
  200. {
  201. unsigned short type;
  202. unsigned short size;
  203. unsigned short len;
  204. unsigned char *data = *p;
  205. s->servername_done = 0;
  206. if (data >= (d+n-2))
  207. return 1;
  208. n2s(data,len);
  209. if (data > (d+n-len))
  210. return 1;
  211. while (data <= (d+n-4))
  212. {
  213. n2s(data,type);
  214. n2s(data,size);
  215. if (data+size > (d+n))
  216. return 1;
  217. if (s->tlsext_debug_cb)
  218. s->tlsext_debug_cb(s, 0, type, data, size,
  219. s->tlsext_debug_arg);
  220. /* The servername extension is treated as follows:
  221. - Only the hostname type is supported with a maximum length of 255.
  222. - The servername is rejected if too long or if it contains zeros,
  223. in which case an fatal alert is generated.
  224. - The servername field is maintained together with the session cache.
  225. - When a session is resumed, the servername call back invoked in order
  226. to allow the application to position itself to the right context.
  227. - The servername is acknowledged if it is new for a session or when
  228. it is identical to a previously used for the same session.
  229. Applications can control the behaviour. They can at any time
  230. set a 'desirable' servername for a new SSL object. This can be the
  231. case for example with HTTPS when a Host: header field is received and
  232. a renegotiation is requested. In this case, a possible servername
  233. presented in the new client hello is only acknowledged if it matches
  234. the value of the Host: field.
  235. - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
  236. if they provide for changing an explicit servername context for the session,
  237. i.e. when the session has been established with a servername extension.
  238. - On session reconnect, the servername extension may be absent.
  239. */
  240. if (type == TLSEXT_TYPE_server_name)
  241. {
  242. unsigned char *sdata;
  243. int servname_type;
  244. int dsize;
  245. if (size < 2)
  246. {
  247. *al = SSL_AD_DECODE_ERROR;
  248. return 0;
  249. }
  250. n2s(data,dsize);
  251. size -= 2;
  252. if (dsize > size )
  253. {
  254. *al = SSL_AD_DECODE_ERROR;
  255. return 0;
  256. }
  257. sdata = data;
  258. while (dsize > 3)
  259. {
  260. servname_type = *(sdata++);
  261. n2s(sdata,len);
  262. dsize -= 3;
  263. if (len > dsize)
  264. {
  265. *al = SSL_AD_DECODE_ERROR;
  266. return 0;
  267. }
  268. if (s->servername_done == 0)
  269. switch (servname_type)
  270. {
  271. case TLSEXT_NAMETYPE_host_name:
  272. if (s->session->tlsext_hostname == NULL)
  273. {
  274. if (len > TLSEXT_MAXLEN_host_name ||
  275. ((s->session->tlsext_hostname = OPENSSL_malloc(len+1)) == NULL))
  276. {
  277. *al = TLS1_AD_UNRECOGNIZED_NAME;
  278. return 0;
  279. }
  280. memcpy(s->session->tlsext_hostname, sdata, len);
  281. s->session->tlsext_hostname[len]='\0';
  282. if (strlen(s->session->tlsext_hostname) != len) {
  283. OPENSSL_free(s->session->tlsext_hostname);
  284. *al = TLS1_AD_UNRECOGNIZED_NAME;
  285. return 0;
  286. }
  287. s->servername_done = 1;
  288. }
  289. else
  290. s->servername_done = strlen(s->session->tlsext_hostname) == len
  291. && strncmp(s->session->tlsext_hostname, (char *)sdata, len) == 0;
  292. break;
  293. default:
  294. break;
  295. }
  296. dsize -= len;
  297. }
  298. if (dsize != 0)
  299. {
  300. *al = SSL_AD_DECODE_ERROR;
  301. return 0;
  302. }
  303. }
  304. /* session ticket processed earlier */
  305. data+=size;
  306. }
  307. *p = data;
  308. return 1;
  309. }
  310. int ssl_parse_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
  311. {
  312. unsigned short type;
  313. unsigned short size;
  314. unsigned short len;
  315. unsigned char *data = *p;
  316. int tlsext_servername = 0;
  317. if (data >= (d+n-2))
  318. return 1;
  319. n2s(data,len);
  320. while(data <= (d+n-4))
  321. {
  322. n2s(data,type);
  323. n2s(data,size);
  324. if (data+size > (d+n))
  325. return 1;
  326. if (s->tlsext_debug_cb)
  327. s->tlsext_debug_cb(s, 1, type, data, size,
  328. s->tlsext_debug_arg);
  329. if (type == TLSEXT_TYPE_server_name)
  330. {
  331. if (s->tlsext_hostname == NULL || size > 0)
  332. {
  333. *al = TLS1_AD_UNRECOGNIZED_NAME;
  334. return 0;
  335. }
  336. tlsext_servername = 1;
  337. }
  338. else if (type == TLSEXT_TYPE_session_ticket)
  339. {
  340. if ((SSL_get_options(s) & SSL_OP_NO_TICKET)
  341. || (size > 0))
  342. {
  343. *al = TLS1_AD_UNSUPPORTED_EXTENSION;
  344. return 0;
  345. }
  346. s->tlsext_ticket_expected = 1;
  347. }
  348. data+=size;
  349. }
  350. if (data != d+n)
  351. {
  352. *al = SSL_AD_DECODE_ERROR;
  353. return 0;
  354. }
  355. if (!s->hit && tlsext_servername == 1)
  356. {
  357. if (s->tlsext_hostname)
  358. {
  359. if (s->session->tlsext_hostname == NULL)
  360. {
  361. s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
  362. if (!s->session->tlsext_hostname)
  363. {
  364. *al = SSL_AD_UNRECOGNIZED_NAME;
  365. return 0;
  366. }
  367. }
  368. else
  369. {
  370. *al = SSL_AD_DECODE_ERROR;
  371. return 0;
  372. }
  373. }
  374. }
  375. *p = data;
  376. return 1;
  377. }
  378. int ssl_check_clienthello_tlsext(SSL *s)
  379. {
  380. int ret=SSL_TLSEXT_ERR_NOACK;
  381. int al = SSL_AD_UNRECOGNIZED_NAME;
  382. if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
  383. ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
  384. else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
  385. ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
  386. switch (ret)
  387. {
  388. case SSL_TLSEXT_ERR_ALERT_FATAL:
  389. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  390. return -1;
  391. case SSL_TLSEXT_ERR_ALERT_WARNING:
  392. ssl3_send_alert(s,SSL3_AL_WARNING,al);
  393. return 1;
  394. case SSL_TLSEXT_ERR_NOACK:
  395. s->servername_done=0;
  396. default:
  397. return 1;
  398. }
  399. }
  400. int ssl_check_serverhello_tlsext(SSL *s)
  401. {
  402. int ret=SSL_TLSEXT_ERR_NOACK;
  403. int al = SSL_AD_UNRECOGNIZED_NAME;
  404. if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
  405. ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
  406. else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
  407. ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
  408. switch (ret)
  409. {
  410. case SSL_TLSEXT_ERR_ALERT_FATAL:
  411. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  412. return -1;
  413. case SSL_TLSEXT_ERR_ALERT_WARNING:
  414. ssl3_send_alert(s,SSL3_AL_WARNING,al);
  415. return 1;
  416. case SSL_TLSEXT_ERR_NOACK:
  417. s->servername_done=0;
  418. default:
  419. return 1;
  420. }
  421. }
  422. /* Since the server cache lookup is done early on in the processing of client
  423. * hello and other operations depend on the result we need to handle any TLS
  424. * session ticket extension at the same time.
  425. */
  426. int tls1_process_ticket(SSL *s, unsigned char *session_id, int len,
  427. const unsigned char *limit, SSL_SESSION **ret)
  428. {
  429. /* Point after session ID in client hello */
  430. const unsigned char *p = session_id + len;
  431. unsigned short i;
  432. if ((s->version <= SSL3_VERSION) || !limit)
  433. return 1;
  434. if (p >= limit)
  435. return -1;
  436. /* Skip past cipher list */
  437. n2s(p, i);
  438. p+= i;
  439. if (p >= limit)
  440. return -1;
  441. /* Skip past compression algorithm list */
  442. i = *(p++);
  443. p += i;
  444. if (p > limit)
  445. return -1;
  446. /* Now at start of extensions */
  447. if ((p + 2) >= limit)
  448. return 1;
  449. n2s(p, i);
  450. while ((p + 4) <= limit)
  451. {
  452. unsigned short type, size;
  453. n2s(p, type);
  454. n2s(p, size);
  455. if (p + size > limit)
  456. return 1;
  457. if (type == TLSEXT_TYPE_session_ticket)
  458. {
  459. /* If tickets disabled indicate cache miss which will
  460. * trigger a full handshake
  461. */
  462. if (SSL_get_options(s) & SSL_OP_NO_TICKET)
  463. return 0;
  464. /* If zero length not client will accept a ticket
  465. * and indicate cache miss to trigger full handshake
  466. */
  467. if (size == 0)
  468. {
  469. s->tlsext_ticket_expected = 1;
  470. return 0; /* Cache miss */
  471. }
  472. return tls_decrypt_ticket(s, p, size, session_id, len,
  473. ret);
  474. }
  475. p += size;
  476. }
  477. return 1;
  478. }
  479. static int tls_decrypt_ticket(SSL *s, const unsigned char *etick, int eticklen,
  480. const unsigned char *sess_id, int sesslen,
  481. SSL_SESSION **psess)
  482. {
  483. SSL_SESSION *sess;
  484. unsigned char *sdec;
  485. const unsigned char *p;
  486. int slen, mlen;
  487. unsigned char tick_hmac[EVP_MAX_MD_SIZE];
  488. HMAC_CTX hctx;
  489. EVP_CIPHER_CTX ctx;
  490. /* Attempt to process session ticket, first conduct sanity and
  491. * integrity checks on ticket.
  492. */
  493. mlen = EVP_MD_size(tlsext_tick_md());
  494. eticklen -= mlen;
  495. /* Need at least keyname + iv + some encrypted data */
  496. if (eticklen < 48)
  497. goto tickerr;
  498. /* Check key name matches */
  499. if (memcmp(etick, s->ctx->tlsext_tick_key_name, 16))
  500. goto tickerr;
  501. /* Check HMAC of encrypted ticket */
  502. HMAC_CTX_init(&hctx);
  503. HMAC_Init_ex(&hctx, s->ctx->tlsext_tick_hmac_key, 16,
  504. tlsext_tick_md(), NULL);
  505. HMAC_Update(&hctx, etick, eticklen);
  506. HMAC_Final(&hctx, tick_hmac, NULL);
  507. HMAC_CTX_cleanup(&hctx);
  508. if (memcmp(tick_hmac, etick + eticklen, mlen))
  509. goto tickerr;
  510. /* Set p to start of IV */
  511. p = etick + 16;
  512. EVP_CIPHER_CTX_init(&ctx);
  513. /* Attempt to decrypt session data */
  514. EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
  515. s->ctx->tlsext_tick_aes_key, p);
  516. /* Move p after IV to start of encrypted ticket, update length */
  517. p += 16;
  518. eticklen -= 32;
  519. sdec = OPENSSL_malloc(eticklen);
  520. if (!sdec)
  521. {
  522. EVP_CIPHER_CTX_cleanup(&ctx);
  523. return -1;
  524. }
  525. EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
  526. if (EVP_DecryptFinal(&ctx, sdec + slen, &mlen) <= 0)
  527. goto tickerr;
  528. slen += mlen;
  529. EVP_CIPHER_CTX_cleanup(&ctx);
  530. p = sdec;
  531. sess = d2i_SSL_SESSION(NULL, &p, slen);
  532. OPENSSL_free(sdec);
  533. if (sess)
  534. {
  535. /* The session ID if non-empty is used by some clients to
  536. * detect that the ticket has been accepted. So we copy it to
  537. * the session structure. If it is empty set length to zero
  538. * as required by standard.
  539. */
  540. if (sesslen)
  541. memcpy(sess->session_id, sess_id, sesslen);
  542. sess->session_id_length = sesslen;
  543. *psess = sess;
  544. s->tlsext_ticket_expected = 0;
  545. return 1;
  546. }
  547. /* If session decrypt failure indicate a cache miss and set state to
  548. * send a new ticket
  549. */
  550. tickerr:
  551. s->tlsext_ticket_expected = 1;
  552. return 0;
  553. }
  554. #endif