statem_lib.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  1. /* ssl/statem/statem_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. /* ====================================================================
  59. * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
  60. *
  61. * Redistribution and use in source and binary forms, with or without
  62. * modification, are permitted provided that the following conditions
  63. * are met:
  64. *
  65. * 1. Redistributions of source code must retain the above copyright
  66. * notice, this list of conditions and the following disclaimer.
  67. *
  68. * 2. Redistributions in binary form must reproduce the above copyright
  69. * notice, this list of conditions and the following disclaimer in
  70. * the documentation and/or other materials provided with the
  71. * distribution.
  72. *
  73. * 3. All advertising materials mentioning features or use of this
  74. * software must display the following acknowledgment:
  75. * "This product includes software developed by the OpenSSL Project
  76. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  77. *
  78. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  79. * endorse or promote products derived from this software without
  80. * prior written permission. For written permission, please contact
  81. * openssl-core@openssl.org.
  82. *
  83. * 5. Products derived from this software may not be called "OpenSSL"
  84. * nor may "OpenSSL" appear in their names without prior written
  85. * permission of the OpenSSL Project.
  86. *
  87. * 6. Redistributions of any form whatsoever must retain the following
  88. * acknowledgment:
  89. * "This product includes software developed by the OpenSSL Project
  90. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  91. *
  92. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  93. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  94. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  95. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  96. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  97. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  98. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  99. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  100. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  101. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  102. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  103. * OF THE POSSIBILITY OF SUCH DAMAGE.
  104. * ====================================================================
  105. *
  106. * This product includes cryptographic software written by Eric Young
  107. * (eay@cryptsoft.com). This product includes software written by Tim
  108. * Hudson (tjh@cryptsoft.com).
  109. *
  110. */
  111. /* ====================================================================
  112. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  113. * ECC cipher suite support in OpenSSL originally developed by
  114. * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
  115. */
  116. #include <limits.h>
  117. #include <string.h>
  118. #include <stdio.h>
  119. #include "../ssl_locl.h"
  120. #include "statem_locl.h"
  121. #include <openssl/buffer.h>
  122. #include <openssl/rand.h>
  123. #include <openssl/objects.h>
  124. #include <openssl/evp.h>
  125. #include <openssl/x509.h>
  126. /*
  127. * send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or
  128. * SSL3_RT_CHANGE_CIPHER_SPEC)
  129. */
  130. int ssl3_do_write(SSL *s, int type)
  131. {
  132. int ret;
  133. ret = ssl3_write_bytes(s, type, &s->init_buf->data[s->init_off],
  134. s->init_num);
  135. if (ret < 0)
  136. return (-1);
  137. if (type == SSL3_RT_HANDSHAKE)
  138. /*
  139. * should not be done for 'Hello Request's, but in that case we'll
  140. * ignore the result anyway
  141. */
  142. ssl3_finish_mac(s, (unsigned char *)&s->init_buf->data[s->init_off],
  143. ret);
  144. if (ret == s->init_num) {
  145. if (s->msg_callback)
  146. s->msg_callback(1, s->version, type, s->init_buf->data,
  147. (size_t)(s->init_off + s->init_num), s,
  148. s->msg_callback_arg);
  149. return (1);
  150. }
  151. s->init_off += ret;
  152. s->init_num -= ret;
  153. return (0);
  154. }
  155. int tls_construct_finished(SSL *s, const char *sender, int slen)
  156. {
  157. unsigned char *p;
  158. int i;
  159. unsigned long l;
  160. p = ssl_handshake_start(s);
  161. i = s->method->ssl3_enc->final_finish_mac(s,
  162. sender, slen,
  163. s->s3->tmp.finish_md);
  164. if (i <= 0)
  165. return 0;
  166. s->s3->tmp.finish_md_len = i;
  167. memcpy(p, s->s3->tmp.finish_md, i);
  168. l = i;
  169. /*
  170. * Copy the finished so we can use it for renegotiation checks
  171. */
  172. if (!s->server) {
  173. OPENSSL_assert(i <= EVP_MAX_MD_SIZE);
  174. memcpy(s->s3->previous_client_finished, s->s3->tmp.finish_md, i);
  175. s->s3->previous_client_finished_len = i;
  176. } else {
  177. OPENSSL_assert(i <= EVP_MAX_MD_SIZE);
  178. memcpy(s->s3->previous_server_finished, s->s3->tmp.finish_md, i);
  179. s->s3->previous_server_finished_len = i;
  180. }
  181. if (!ssl_set_handshake_header(s, SSL3_MT_FINISHED, l)) {
  182. SSLerr(SSL_F_TLS_CONSTRUCT_FINISHED, ERR_R_INTERNAL_ERROR);
  183. return 0;
  184. }
  185. return 1;
  186. }
  187. #ifndef OPENSSL_NO_NEXTPROTONEG
  188. /*
  189. * ssl3_take_mac calculates the Finished MAC for the handshakes messages seen
  190. * to far.
  191. */
  192. static void ssl3_take_mac(SSL *s)
  193. {
  194. const char *sender;
  195. int slen;
  196. /*
  197. * If no new cipher setup return immediately: other functions will set
  198. * the appropriate error.
  199. */
  200. if (s->s3->tmp.new_cipher == NULL)
  201. return;
  202. if (!s->server) {
  203. sender = s->method->ssl3_enc->server_finished_label;
  204. slen = s->method->ssl3_enc->server_finished_label_len;
  205. } else {
  206. sender = s->method->ssl3_enc->client_finished_label;
  207. slen = s->method->ssl3_enc->client_finished_label_len;
  208. }
  209. s->s3->tmp.peer_finish_md_len = s->method->ssl3_enc->final_finish_mac(s,
  210. sender,
  211. slen,
  212. s->s3->tmp.peer_finish_md);
  213. }
  214. #endif
  215. MSG_PROCESS_RETURN tls_process_change_cipher_spec(SSL *s, PACKET *pkt)
  216. {
  217. int al;
  218. long remain;
  219. remain = PACKET_remaining(pkt);
  220. /*
  221. * 'Change Cipher Spec' is just a single byte, which should already have
  222. * been consumed by ssl_get_message() so there should be no bytes left,
  223. * unless we're using DTLS1_BAD_VER, which has an extra 2 bytes
  224. */
  225. if (SSL_IS_DTLS(s)) {
  226. if ((s->version == DTLS1_BAD_VER
  227. && remain != DTLS1_CCS_HEADER_LENGTH + 1)
  228. || (s->version != DTLS1_BAD_VER
  229. && remain != DTLS1_CCS_HEADER_LENGTH - 1)) {
  230. al = SSL_AD_ILLEGAL_PARAMETER;
  231. SSLerr(SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC,
  232. SSL_R_BAD_CHANGE_CIPHER_SPEC);
  233. goto f_err;
  234. }
  235. } else {
  236. if (remain != 0) {
  237. al = SSL_AD_ILLEGAL_PARAMETER;
  238. SSLerr(SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC,
  239. SSL_R_BAD_CHANGE_CIPHER_SPEC);
  240. goto f_err;
  241. }
  242. }
  243. /* Check we have a cipher to change to */
  244. if (s->s3->tmp.new_cipher == NULL) {
  245. al = SSL_AD_UNEXPECTED_MESSAGE;
  246. SSLerr(SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC, SSL_R_CCS_RECEIVED_EARLY);
  247. goto f_err;
  248. }
  249. s->s3->change_cipher_spec = 1;
  250. if (!ssl3_do_change_cipher_spec(s)) {
  251. al = SSL_AD_INTERNAL_ERROR;
  252. SSLerr(SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC, ERR_R_INTERNAL_ERROR);
  253. goto f_err;
  254. }
  255. if (SSL_IS_DTLS(s)) {
  256. dtls1_reset_seq_numbers(s, SSL3_CC_READ);
  257. if (s->version == DTLS1_BAD_VER)
  258. s->d1->handshake_read_seq++;
  259. #ifndef OPENSSL_NO_SCTP
  260. /*
  261. * Remember that a CCS has been received, so that an old key of
  262. * SCTP-Auth can be deleted when a CCS is sent. Will be ignored if no
  263. * SCTP is used
  264. */
  265. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD, 1, NULL);
  266. #endif
  267. }
  268. return MSG_PROCESS_CONTINUE_READING;
  269. f_err:
  270. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  271. ossl_statem_set_error(s);
  272. return MSG_PROCESS_ERROR;
  273. }
  274. MSG_PROCESS_RETURN tls_process_finished(SSL *s, PACKET *pkt)
  275. {
  276. int al, i;
  277. /* If this occurs, we have missed a message */
  278. if (!s->s3->change_cipher_spec) {
  279. al = SSL_AD_UNEXPECTED_MESSAGE;
  280. SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_GOT_A_FIN_BEFORE_A_CCS);
  281. goto f_err;
  282. }
  283. s->s3->change_cipher_spec = 0;
  284. i = s->s3->tmp.peer_finish_md_len;
  285. if ((unsigned long)i != PACKET_remaining(pkt)) {
  286. al = SSL_AD_DECODE_ERROR;
  287. SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_BAD_DIGEST_LENGTH);
  288. goto f_err;
  289. }
  290. if (CRYPTO_memcmp(PACKET_data(pkt), s->s3->tmp.peer_finish_md, i) != 0) {
  291. al = SSL_AD_DECRYPT_ERROR;
  292. SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_DIGEST_CHECK_FAILED);
  293. goto f_err;
  294. }
  295. /*
  296. * Copy the finished so we can use it for renegotiation checks
  297. */
  298. if (s->server) {
  299. OPENSSL_assert(i <= EVP_MAX_MD_SIZE);
  300. memcpy(s->s3->previous_client_finished, s->s3->tmp.peer_finish_md, i);
  301. s->s3->previous_client_finished_len = i;
  302. } else {
  303. OPENSSL_assert(i <= EVP_MAX_MD_SIZE);
  304. memcpy(s->s3->previous_server_finished, s->s3->tmp.peer_finish_md, i);
  305. s->s3->previous_server_finished_len = i;
  306. }
  307. return MSG_PROCESS_FINISHED_READING;
  308. f_err:
  309. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  310. ossl_statem_set_error(s);
  311. return MSG_PROCESS_ERROR;
  312. }
  313. int tls_construct_change_cipher_spec(SSL *s)
  314. {
  315. unsigned char *p;
  316. p = (unsigned char *)s->init_buf->data;
  317. *p = SSL3_MT_CCS;
  318. s->init_num = 1;
  319. s->init_off = 0;
  320. return 1;
  321. }
  322. unsigned long ssl3_output_cert_chain(SSL *s, CERT_PKEY *cpk)
  323. {
  324. unsigned char *p;
  325. unsigned long l = 3 + SSL_HM_HEADER_LENGTH(s);
  326. if (!ssl_add_cert_chain(s, cpk, &l))
  327. return 0;
  328. l -= 3 + SSL_HM_HEADER_LENGTH(s);
  329. p = ssl_handshake_start(s);
  330. l2n3(l, p);
  331. l += 3;
  332. if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE, l)) {
  333. SSLerr(SSL_F_SSL3_OUTPUT_CERT_CHAIN, ERR_R_INTERNAL_ERROR);
  334. return 0;
  335. }
  336. return l + SSL_HM_HEADER_LENGTH(s);
  337. }
  338. WORK_STATE tls_finish_handshake(SSL *s, WORK_STATE wst)
  339. {
  340. void (*cb) (const SSL *ssl, int type, int val) = NULL;
  341. #ifndef OPENSSL_NO_SCTP
  342. if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))) {
  343. WORK_STATE ret;
  344. ret = dtls_wait_for_dry(s);
  345. if (ret != WORK_FINISHED_CONTINUE)
  346. return ret;
  347. }
  348. #endif
  349. /* clean a few things up */
  350. ssl3_cleanup_key_block(s);
  351. if (!SSL_IS_DTLS(s)) {
  352. /*
  353. * We don't do this in DTLS because we may still need the init_buf
  354. * in case there are any unexpected retransmits
  355. */
  356. BUF_MEM_free(s->init_buf);
  357. s->init_buf = NULL;
  358. }
  359. ssl_free_wbio_buffer(s);
  360. s->init_num = 0;
  361. if (!s->server || s->renegotiate == 2) {
  362. /* skipped if we just sent a HelloRequest */
  363. s->renegotiate = 0;
  364. s->new_session = 0;
  365. if (s->server) {
  366. ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
  367. s->ctx->stats.sess_accept_good++;
  368. s->handshake_func = ossl_statem_accept;
  369. } else {
  370. ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
  371. if (s->hit)
  372. s->ctx->stats.sess_hit++;
  373. s->handshake_func = ossl_statem_connect;
  374. s->ctx->stats.sess_connect_good++;
  375. }
  376. if (s->info_callback != NULL)
  377. cb = s->info_callback;
  378. else if (s->ctx->info_callback != NULL)
  379. cb = s->ctx->info_callback;
  380. if (cb != NULL)
  381. cb(s, SSL_CB_HANDSHAKE_DONE, 1);
  382. if (SSL_IS_DTLS(s)) {
  383. /* done with handshaking */
  384. s->d1->handshake_read_seq = 0;
  385. s->d1->handshake_write_seq = 0;
  386. s->d1->next_handshake_write_seq = 0;
  387. }
  388. }
  389. return WORK_FINISHED_STOP;
  390. }
  391. int tls_get_message_header(SSL *s, int *mt)
  392. {
  393. /* s->init_num < SSL3_HM_HEADER_LENGTH */
  394. int skip_message, i, recvd_type, al;
  395. unsigned char *p;
  396. unsigned long l;
  397. p = (unsigned char *)s->init_buf->data;
  398. do {
  399. while (s->init_num < SSL3_HM_HEADER_LENGTH) {
  400. i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, &recvd_type,
  401. &p[s->init_num], SSL3_HM_HEADER_LENGTH - s->init_num, 0);
  402. if (i <= 0) {
  403. s->rwstate = SSL_READING;
  404. return 0;
  405. }
  406. if (recvd_type == SSL3_RT_CHANGE_CIPHER_SPEC) {
  407. s->s3->tmp.message_type = *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
  408. s->init_num = i - 1;
  409. s->s3->tmp.message_size = i;
  410. return 1;
  411. } else if (recvd_type != SSL3_RT_HANDSHAKE) {
  412. al = SSL_AD_UNEXPECTED_MESSAGE;
  413. SSLerr(SSL_F_TLS_GET_MESSAGE_HEADER, SSL_R_CCS_RECEIVED_EARLY);
  414. goto f_err;
  415. }
  416. s->init_num += i;
  417. }
  418. skip_message = 0;
  419. if (!s->server)
  420. if (p[0] == SSL3_MT_HELLO_REQUEST)
  421. /*
  422. * The server may always send 'Hello Request' messages --
  423. * we are doing a handshake anyway now, so ignore them if
  424. * their format is correct. Does not count for 'Finished'
  425. * MAC.
  426. */
  427. if (p[1] == 0 && p[2] == 0 && p[3] == 0) {
  428. s->init_num = 0;
  429. skip_message = 1;
  430. if (s->msg_callback)
  431. s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
  432. p, SSL3_HM_HEADER_LENGTH, s,
  433. s->msg_callback_arg);
  434. }
  435. } while (skip_message);
  436. /* s->init_num == SSL3_HM_HEADER_LENGTH */
  437. *mt = *p;
  438. s->s3->tmp.message_type = *(p++);
  439. if(RECORD_LAYER_is_sslv2_record(&s->rlayer)) {
  440. /*
  441. * Only happens with SSLv3+ in an SSLv2 backward compatible
  442. * ClientHello
  443. */
  444. /*
  445. * Total message size is the remaining record bytes to read
  446. * plus the SSL3_HM_HEADER_LENGTH bytes that we already read
  447. */
  448. l = RECORD_LAYER_get_rrec_length(&s->rlayer)
  449. + SSL3_HM_HEADER_LENGTH;
  450. if (l && !BUF_MEM_grow_clean(s->init_buf, (int)l)) {
  451. SSLerr(SSL_F_TLS_GET_MESSAGE_HEADER, ERR_R_BUF_LIB);
  452. goto err;
  453. }
  454. s->s3->tmp.message_size = l;
  455. s->init_msg = s->init_buf->data;
  456. s->init_num = SSL3_HM_HEADER_LENGTH;
  457. } else {
  458. n2l3(p, l);
  459. /* BUF_MEM_grow takes an 'int' parameter */
  460. if (l > (INT_MAX - SSL3_HM_HEADER_LENGTH)) {
  461. al = SSL_AD_ILLEGAL_PARAMETER;
  462. SSLerr(SSL_F_TLS_GET_MESSAGE_HEADER, SSL_R_EXCESSIVE_MESSAGE_SIZE);
  463. goto f_err;
  464. }
  465. if (l && !BUF_MEM_grow_clean(s->init_buf,
  466. (int)l + SSL3_HM_HEADER_LENGTH)) {
  467. SSLerr(SSL_F_TLS_GET_MESSAGE_HEADER, ERR_R_BUF_LIB);
  468. goto err;
  469. }
  470. s->s3->tmp.message_size = l;
  471. s->init_msg = s->init_buf->data + SSL3_HM_HEADER_LENGTH;
  472. s->init_num = 0;
  473. }
  474. return 1;
  475. f_err:
  476. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  477. err:
  478. return 0;
  479. }
  480. int tls_get_message_body(SSL *s, unsigned long *len)
  481. {
  482. long n;
  483. unsigned char *p;
  484. int i;
  485. if (s->s3->tmp.message_type == SSL3_MT_CHANGE_CIPHER_SPEC) {
  486. /* We've already read everything in */
  487. *len = (unsigned long)s->init_num;
  488. return 1;
  489. }
  490. p = s->init_msg;
  491. n = s->s3->tmp.message_size - s->init_num;
  492. while (n > 0) {
  493. i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL,
  494. &p[s->init_num], n, 0);
  495. if (i <= 0) {
  496. s->rwstate = SSL_READING;
  497. *len = 0;
  498. return 0;
  499. }
  500. s->init_num += i;
  501. n -= i;
  502. }
  503. #ifndef OPENSSL_NO_NEXTPROTONEG
  504. /*
  505. * If receiving Finished, record MAC of prior handshake messages for
  506. * Finished verification.
  507. */
  508. if (*s->init_buf->data == SSL3_MT_FINISHED)
  509. ssl3_take_mac(s);
  510. #endif
  511. /* Feed this message into MAC computation. */
  512. if(RECORD_LAYER_is_sslv2_record(&s->rlayer)) {
  513. ssl3_finish_mac(s, (unsigned char *)s->init_buf->data, s->init_num);
  514. if (s->msg_callback)
  515. s->msg_callback(0, SSL2_VERSION, 0, s->init_buf->data,
  516. (size_t)s->init_num, s, s->msg_callback_arg);
  517. } else {
  518. ssl3_finish_mac(s, (unsigned char *)s->init_buf->data,
  519. s->init_num + SSL3_HM_HEADER_LENGTH);
  520. if (s->msg_callback)
  521. s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, s->init_buf->data,
  522. (size_t)s->init_num + SSL3_HM_HEADER_LENGTH, s,
  523. s->msg_callback_arg);
  524. }
  525. /*
  526. * init_num should never be negative...should probably be declared
  527. * unsigned
  528. */
  529. if (s->init_num < 0) {
  530. SSLerr(SSL_F_TLS_GET_MESSAGE_BODY, ERR_R_INTERNAL_ERROR);
  531. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  532. *len = 0;
  533. return 0;
  534. }
  535. *len = (unsigned long)s->init_num;
  536. return 1;
  537. }
  538. int ssl_cert_type(X509 *x, EVP_PKEY *pkey)
  539. {
  540. EVP_PKEY *pk;
  541. int ret = -1, i;
  542. if (pkey == NULL)
  543. pk = X509_get_pubkey(x);
  544. else
  545. pk = pkey;
  546. if (pk == NULL)
  547. goto err;
  548. i = EVP_PKEY_id(pk);
  549. if (i == EVP_PKEY_RSA) {
  550. ret = SSL_PKEY_RSA_ENC;
  551. } else if (i == EVP_PKEY_DSA) {
  552. ret = SSL_PKEY_DSA_SIGN;
  553. }
  554. #ifndef OPENSSL_NO_EC
  555. else if (i == EVP_PKEY_EC) {
  556. ret = SSL_PKEY_ECC;
  557. }
  558. #endif
  559. #ifndef OPENSSL_NO_GOST
  560. else if (i == NID_id_GostR3410_2001) {
  561. ret = SSL_PKEY_GOST01;
  562. } else if (i == NID_id_GostR3410_2012_256) {
  563. ret = SSL_PKEY_GOST12_256;
  564. } else if (i == NID_id_GostR3410_2012_512) {
  565. ret = SSL_PKEY_GOST12_512;
  566. }
  567. #endif
  568. err:
  569. if (!pkey)
  570. EVP_PKEY_free(pk);
  571. return (ret);
  572. }
  573. int ssl_verify_alarm_type(long type)
  574. {
  575. int al;
  576. switch (type) {
  577. case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
  578. case X509_V_ERR_UNABLE_TO_GET_CRL:
  579. case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
  580. al = SSL_AD_UNKNOWN_CA;
  581. break;
  582. case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
  583. case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
  584. case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
  585. case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
  586. case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
  587. case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
  588. case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
  589. case X509_V_ERR_CERT_NOT_YET_VALID:
  590. case X509_V_ERR_CRL_NOT_YET_VALID:
  591. case X509_V_ERR_CERT_UNTRUSTED:
  592. case X509_V_ERR_CERT_REJECTED:
  593. al = SSL_AD_BAD_CERTIFICATE;
  594. break;
  595. case X509_V_ERR_CERT_SIGNATURE_FAILURE:
  596. case X509_V_ERR_CRL_SIGNATURE_FAILURE:
  597. al = SSL_AD_DECRYPT_ERROR;
  598. break;
  599. case X509_V_ERR_CERT_HAS_EXPIRED:
  600. case X509_V_ERR_CRL_HAS_EXPIRED:
  601. al = SSL_AD_CERTIFICATE_EXPIRED;
  602. break;
  603. case X509_V_ERR_CERT_REVOKED:
  604. al = SSL_AD_CERTIFICATE_REVOKED;
  605. break;
  606. case X509_V_ERR_OUT_OF_MEM:
  607. al = SSL_AD_INTERNAL_ERROR;
  608. break;
  609. case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
  610. case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
  611. case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
  612. case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
  613. case X509_V_ERR_CERT_CHAIN_TOO_LONG:
  614. case X509_V_ERR_PATH_LENGTH_EXCEEDED:
  615. case X509_V_ERR_INVALID_CA:
  616. al = SSL_AD_UNKNOWN_CA;
  617. break;
  618. case X509_V_ERR_APPLICATION_VERIFICATION:
  619. al = SSL_AD_HANDSHAKE_FAILURE;
  620. break;
  621. case X509_V_ERR_INVALID_PURPOSE:
  622. al = SSL_AD_UNSUPPORTED_CERTIFICATE;
  623. break;
  624. default:
  625. al = SSL_AD_CERTIFICATE_UNKNOWN;
  626. break;
  627. }
  628. return (al);
  629. }
  630. int ssl_allow_compression(SSL *s)
  631. {
  632. if (s->options & SSL_OP_NO_COMPRESSION)
  633. return 0;
  634. return ssl_security(s, SSL_SECOP_COMPRESSION, 0, 0, NULL);
  635. }
  636. static int version_cmp(SSL *s, int a, int b)
  637. {
  638. int dtls = SSL_IS_DTLS(s);
  639. if (a == b)
  640. return 0;
  641. if (!dtls)
  642. return a < b ? -1 : 1;
  643. return DTLS_VERSION_LT(a, b) ? -1 : 1;
  644. }
  645. typedef struct {
  646. int version;
  647. const SSL_METHOD *(*cmeth)(void);
  648. const SSL_METHOD *(*smeth)(void);
  649. } version_info;
  650. #if TLS_MAX_VERSION != TLS1_2_VERSION
  651. # error Code needs update for TLS_method() support beyond TLS1_2_VERSION.
  652. #endif
  653. static const version_info tls_version_table[] = {
  654. #ifndef OPENSSL_NO_TLS1_2
  655. { TLS1_2_VERSION, TLSv1_2_client_method, TLSv1_2_server_method },
  656. #else
  657. { TLS1_2_VERSION, NULL, NULL },
  658. #endif
  659. #ifndef OPENSSL_NO_TLS1_1
  660. { TLS1_1_VERSION, TLSv1_1_client_method, TLSv1_1_server_method },
  661. #else
  662. { TLS1_1_VERSION, NULL, NULL },
  663. #endif
  664. #ifndef OPENSSL_NO_TLS1
  665. { TLS1_VERSION, TLSv1_client_method, TLSv1_server_method },
  666. #else
  667. { TLS1_VERSION, NULL, NULL },
  668. #endif
  669. #ifndef OPENSSL_NO_SSL3
  670. { SSL3_VERSION, SSLv3_client_method, SSLv3_server_method },
  671. #else
  672. { SSL3_VERSION, NULL, NULL },
  673. #endif
  674. { 0, NULL, NULL },
  675. };
  676. #if DTLS_MAX_VERSION != DTLS1_2_VERSION
  677. # error Code needs update for DTLS_method() support beyond DTLS1_2_VERSION.
  678. #endif
  679. static const version_info dtls_version_table[] = {
  680. #ifndef OPENSSL_NO_DTLS1_2
  681. { DTLS1_2_VERSION, DTLSv1_2_client_method, DTLSv1_2_server_method },
  682. #else
  683. { DTLS1_2_VERSION, NULL, NULL },
  684. #endif
  685. #ifndef OPENSSL_NO_DTLS1
  686. { DTLS1_VERSION, DTLSv1_client_method, DTLSv1_server_method },
  687. #else
  688. { DTLS1_VERSION, NULL, NULL },
  689. #endif
  690. { 0, NULL, NULL },
  691. };
  692. /*
  693. * ssl_method_error - Check whether an SSL_METHOD is enabled.
  694. *
  695. * @s: The SSL handle for the candidate method
  696. * @method: the intended method.
  697. *
  698. * Returns 0 on success, or an SSL error reason on failure.
  699. */
  700. static int ssl_method_error(SSL *s, const SSL_METHOD *method)
  701. {
  702. int version = method->version;
  703. if ((s->min_proto_version != 0 &&
  704. version_cmp(s, version, s->min_proto_version) < 0) ||
  705. ssl_security(s, SSL_SECOP_VERSION, 0, version, NULL) == 0)
  706. return SSL_R_VERSION_TOO_LOW;
  707. if (s->max_proto_version != 0 &&
  708. version_cmp(s, version, s->max_proto_version) > 0)
  709. return SSL_R_VERSION_TOO_HIGH;
  710. if ((s->options & method->mask) != 0)
  711. return SSL_R_UNSUPPORTED_PROTOCOL;
  712. if ((method->flags & SSL_METHOD_NO_SUITEB) != 0 && tls1_suiteb(s))
  713. return SSL_R_AT_LEAST_TLS_1_2_NEEDED_IN_SUITEB_MODE;
  714. else if ((method->flags & SSL_METHOD_NO_FIPS) != 0 && FIPS_mode())
  715. return SSL_R_AT_LEAST_TLS_1_0_NEEDED_IN_FIPS_MODE;
  716. return 0;
  717. }
  718. /*
  719. * ssl_check_version_downgrade - In response to RFC7507 SCSV version
  720. * fallback indication from a client check whether we're using the highest
  721. * supported protocol version.
  722. *
  723. * @s server SSL handle.
  724. *
  725. * Returns 1 when using the highest enabled version, 0 otherwise.
  726. */
  727. int ssl_check_version_downgrade(SSL *s)
  728. {
  729. const version_info *vent;
  730. const version_info *table;
  731. /*
  732. * Check that the current protocol is the highest enabled version
  733. * (according to s->ctx->method, as version negotiation may have changed
  734. * s->method).
  735. */
  736. if (s->version == s->ctx->method->version)
  737. return 1;
  738. /*
  739. * Apparently we're using a version-flexible SSL_METHOD (not at its
  740. * highest protocol version).
  741. */
  742. if (s->ctx->method->version == TLS_method()->version)
  743. table = tls_version_table;
  744. else if (s->ctx->method->version == DTLS_method()->version)
  745. table = dtls_version_table;
  746. else {
  747. /* Unexpected state; fail closed. */
  748. return 0;
  749. }
  750. for (vent = table; vent->version != 0; ++vent) {
  751. if (vent->smeth != NULL &&
  752. ssl_method_error(s, vent->smeth()) == 0)
  753. return s->version == vent->version;
  754. }
  755. return 0;
  756. }
  757. /*
  758. * ssl_set_version_bound - set an upper or lower bound on the supported (D)TLS
  759. * protocols, provided the initial (D)TLS method is version-flexible. This
  760. * function sanity-checks the proposed value and makes sure the method is
  761. * version-flexible, then sets the limit if all is well.
  762. *
  763. * @method_version: The version of the current SSL_METHOD.
  764. * @version: the intended limit.
  765. * @bound: pointer to limit to be updated.
  766. *
  767. * Returns 1 on success, 0 on failure.
  768. */
  769. int ssl_set_version_bound(int method_version, int version, int *bound)
  770. {
  771. if (version == 0) {
  772. *bound = version;
  773. return 1;
  774. }
  775. /*-
  776. * Restrict TLS methods to TLS protocol versions.
  777. * Restrict DTLS methods to DTLS protocol versions.
  778. * Note, DTLS version numbers are decreasing, use comparison macros.
  779. *
  780. * Note that for both lower-bounds we use explicit versions, not
  781. * (D)TLS_MIN_VERSION. This is because we don't want to break user
  782. * configurations. If the MIN (supported) version ever rises, the user's
  783. * "floor" remains valid even if no longer available. We don't expect the
  784. * MAX ceiling to ever get lower, so making that variable makes sense.
  785. */
  786. switch (method_version) {
  787. default:
  788. /*
  789. * XXX For fixed version methods, should we always fail and not set any
  790. * bounds, always succeed and not set any bounds, or set the bounds and
  791. * arrange to fail later if they are not met? At present fixed-version
  792. * methods are not subject to controls that disable individual protocol
  793. * versions.
  794. */
  795. return 0;
  796. case TLS_ANY_VERSION:
  797. if (version < SSL3_VERSION || version > TLS_MAX_VERSION)
  798. return 0;
  799. break;
  800. case DTLS_ANY_VERSION:
  801. if (DTLS_VERSION_GT(version, DTLS_MAX_VERSION) ||
  802. DTLS_VERSION_LT(version, DTLS1_VERSION))
  803. return 0;
  804. break;
  805. }
  806. *bound = version;
  807. return 1;
  808. }
  809. /*
  810. * ssl_choose_server_version - Choose server (D)TLS version. Called when the
  811. * client HELLO is received to select the final server protocol version and
  812. * the version specific method.
  813. *
  814. * @s: server SSL handle.
  815. *
  816. * Returns 0 on success or an SSL error reason number on failure.
  817. */
  818. int ssl_choose_server_version(SSL *s)
  819. {
  820. /*-
  821. * With version-flexible methods we have an initial state with:
  822. *
  823. * s->method->version == (D)TLS_ANY_VERSION,
  824. * s->version == (D)TLS_MAX_VERSION.
  825. *
  826. * So we detect version-flexible methods via the method version, not the
  827. * handle version.
  828. */
  829. int server_version = s->method->version;
  830. int client_version = s->client_version;
  831. const version_info *vent;
  832. const version_info *table;
  833. int disabled = 0;
  834. switch (server_version) {
  835. default:
  836. if (version_cmp(s, client_version, s->version) < 0)
  837. return SSL_R_WRONG_SSL_VERSION;
  838. /*
  839. * If this SSL handle is not from a version flexible method we don't
  840. * (and never did) check min/max FIPS or Suite B constraints. Hope
  841. * that's OK. It is up to the caller to not choose fixed protocol
  842. * versions they don't want. If not, then easy to fix, just return
  843. * ssl_method_error(s, s->method)
  844. */
  845. return 0;
  846. case TLS_ANY_VERSION:
  847. table = tls_version_table;
  848. break;
  849. case DTLS_ANY_VERSION:
  850. table = dtls_version_table;
  851. break;
  852. }
  853. for (vent = table; vent->version != 0; ++vent) {
  854. const SSL_METHOD *method;
  855. if (vent->smeth == NULL ||
  856. version_cmp(s, client_version, vent->version) < 0)
  857. continue;
  858. method = vent->smeth();
  859. if (ssl_method_error(s, method) == 0) {
  860. s->version = vent->version;
  861. s->method = method;
  862. return 0;
  863. }
  864. disabled = 1;
  865. }
  866. return disabled ? SSL_R_UNSUPPORTED_PROTOCOL : SSL_R_VERSION_TOO_LOW;
  867. }
  868. /*
  869. * ssl_choose_client_version - Choose client (D)TLS version. Called when the
  870. * server HELLO is received to select the final client protocol version and
  871. * the version specific method.
  872. *
  873. * @s: client SSL handle.
  874. * @version: The proposed version from the server's HELLO.
  875. *
  876. * Returns 0 on success or an SSL error reason number on failure.
  877. */
  878. int ssl_choose_client_version(SSL *s, int version)
  879. {
  880. const version_info *vent;
  881. const version_info *table;
  882. switch (s->method->version) {
  883. default:
  884. if (version != s->version)
  885. return SSL_R_WRONG_SSL_VERSION;
  886. /*
  887. * If this SSL handle is not from a version flexible method we don't
  888. * (and never did) check min/max, FIPS or Suite B constraints. Hope
  889. * that's OK. It is up to the caller to not choose fixed protocol
  890. * versions they don't want. If not, then easy to fix, just return
  891. * ssl_method_error(s, s->method)
  892. */
  893. s->session->ssl_version = s->version;
  894. return 0;
  895. case TLS_ANY_VERSION:
  896. table = tls_version_table;
  897. break;
  898. case DTLS_ANY_VERSION:
  899. table = dtls_version_table;
  900. break;
  901. }
  902. for (vent = table; vent->version != 0; ++vent) {
  903. const SSL_METHOD *method;
  904. int err;
  905. if (version != vent->version)
  906. continue;
  907. if (vent->cmeth == NULL)
  908. break;
  909. method = vent->cmeth();
  910. err = ssl_method_error(s, method);
  911. if (err != 0)
  912. return err;
  913. s->method = method;
  914. s->session->ssl_version = s->version = version;
  915. return 0;
  916. }
  917. return SSL_R_UNSUPPORTED_PROTOCOL;
  918. }
  919. /*-
  920. * ssl_set_client_hello_version - Work out what version we should be using for
  921. * the initial ClientHello if the version is initially (D)TLS_ANY_VERSION. We
  922. * apply any explicit SSL_OP_NO_xxx options, the MinProtocol and MaxProtocol
  923. * configuration commands, any Suite B or FIPS_mode() constraints and any floor
  924. * imposed by the security level here, so we don't advertise the wrong protocol
  925. * version to only reject the outcome later.
  926. *
  927. * Computing the right floor matters. If, e.g., TLS 1.0 and 1.2 are enabled,
  928. * TLS 1.1 is disabled, but the security level, Suite-B and/or MinProtocol
  929. * only allow TLS 1.2, we want to advertise TLS1.2, *not* TLS1.
  930. *
  931. * @s: client SSL handle.
  932. *
  933. * Returns 0 on success or an SSL error reason number on failure.
  934. */
  935. int ssl_set_client_hello_version(SSL *s)
  936. {
  937. int version;
  938. int hole;
  939. const SSL_METHOD *single = NULL;
  940. const SSL_METHOD *method;
  941. const version_info *table;
  942. const version_info *vent;
  943. switch (s->method->version) {
  944. default:
  945. /*
  946. * If this SSL handle is not from a version flexible method we don't
  947. * (and never did) check min/max FIPS or Suite B constraints. Hope
  948. * that's OK. It is up to the caller to not choose fixed protocol
  949. * versions they don't want. If not, then easy to fix, just return
  950. * ssl_method_error(s, s->method)
  951. */
  952. s->client_version = s->version;
  953. return 0;
  954. case TLS_ANY_VERSION:
  955. table = tls_version_table;
  956. break;
  957. case DTLS_ANY_VERSION:
  958. table = dtls_version_table;
  959. break;
  960. }
  961. /*
  962. * SSL_OP_NO_X disables all protocols above X *if* there are some protocols
  963. * below X enabled. This is required in order to maintain the "version
  964. * capability" vector contiguous. Any versions with a NULL client method
  965. * (protocol version client is disabled at compile-time) is also a "hole".
  966. *
  967. * Our initial state is hole == 1, version == 0. That is, versions above
  968. * the first version in the method table are disabled (a "hole" above
  969. * the valid protocol entries) and we don't have a selected version yet.
  970. *
  971. * Whenever "hole == 1", and we hit an enabled method, its version becomes
  972. * the selected version, and the method becomes a candidate "single"
  973. * method. We're no longer in a hole, so "hole" becomes 0.
  974. *
  975. * If "hole == 0" and we hit an enabled method, then "single" is cleared,
  976. * as we support a contiguous range of at least two methods. If we hit
  977. * a disabled method, then hole becomes true again, but nothing else
  978. * changes yet, because all the remaining methods may be disabled too.
  979. * If we again hit an enabled method after the new hole, it becomes
  980. * selected, as we start from scratch.
  981. */
  982. version = 0;
  983. hole = 1;
  984. for (vent = table; vent->version != 0; ++vent) {
  985. /*
  986. * A table entry with a NULL client method is still a hole in the
  987. * "version capability" vector.
  988. */
  989. if (vent->cmeth == NULL) {
  990. hole = 1;
  991. continue;
  992. }
  993. method = vent->cmeth();
  994. if (ssl_method_error(s, method) != 0) {
  995. hole = 1;
  996. } else if (!hole) {
  997. single = NULL;
  998. } else {
  999. version = (single = method)->version;
  1000. hole = 0;
  1001. }
  1002. }
  1003. /* Fail if everything is disabled */
  1004. if (version == 0)
  1005. return SSL_R_NO_PROTOCOLS_AVAILABLE;
  1006. if (single != NULL)
  1007. s->method = single;
  1008. s->client_version = s->version = version;
  1009. return 0;
  1010. }