当前位置:   article > 正文

解决吉大正源(身份认证网关|USBKey)和gmssl(server|client)使用gmtl协议交叉互通报错tlsv1 alert decrypt error

tlsv1 alert decrypt error

报错内容

  • SSL_connect:error in SSLv3/TLS write finished
  • 140057291788288:error:1409441B:SSL routines:ssl3_read_bytes:tlsv1 alert decrypt error:ssl/record/rec_layer_s3.c:1385:SSL alert number 51

报错原因

  • gmssl库生成 certificate verify 消息时,对自客户端Hello消息开始到本消息为止(不包括 certificate verify 消息)的所有握手相关信息内容,使用SM3计算摘要,再使用私钥对该摘要签名,但是签名时没有使用SM2_DEFAULT_ID(1234567812345678)在内部做HASH。
    • gmssl实现gmtls协议的步骤中缺少使用SM3计算摘要信息,再使用私钥对摘要进行签名
  • 前面你们客户端能连上gmssl服务端,应该是解决了第一个问题。虽然第二个问题没解决,也能连成功,是因为gmssl客户端和服务端计算过程是一样,才没报错,但是这样的计算过程不符合规范的,跟其他对接就会出问题了。
    • gmssl无法和其余实现上述功能的平台进行对接

相关内容

  • 版本:GmSSL 2.5.4 - OpenSSL 1.1.0d  19 Jun 2019 
  • 工作路径:/home/chy-cpabe/tmp/first   /home/chy-cpabe/tmp/second
    • 每个路径下包含 签名私钥 签名证书 加密私钥 加密证书 根证书 五个文件
    • 根秘钥和身份认证网关保持一致
    • first 、second、身份认证网关三个地方的证书文件均有同一个PKI进行签发

 修改文件

  • 目前仅仅修改了 statem_clnt 和 statem_srvr 两个文件,后期手写自己的客户端和服务端代码进行函数调用的时候,还需要修改底层函数的实现细节

statem_clnt

  1. /* ====================================================================
  2. * Copyright (c) 2014 - 2017 The GmSSL Project. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in
  13. * the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * 3. All advertising materials mentioning features or use of this
  17. * software must display the following acknowledgment:
  18. * "This product includes software developed by the GmSSL Project.
  19. * (http://gmssl.org/)"
  20. *
  21. * 4. The name "GmSSL Project" must not be used to endorse or promote
  22. * products derived from this software without prior written
  23. * permission. For written permission, please contact
  24. * guanzhi1980@gmail.com.
  25. *
  26. * 5. Products derived from this software may not be called "GmSSL"
  27. * nor may "GmSSL" appear in their names without prior written
  28. * permission of the GmSSL Project.
  29. *
  30. * 6. Redistributions of any form whatsoever must retain the following
  31. * acknowledgment:
  32. * "This product includes software developed by the GmSSL Project
  33. * (http://gmssl.org/)"
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
  36. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46. * OF THE POSSIBILITY OF SUCH DAMAGE.
  47. * ====================================================================
  48. */
  49. /*
  50. * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  51. *
  52. * Licensed under the OpenSSL license (the "License"). You may not use
  53. * this file except in compliance with the License. You can obtain a copy
  54. * in the file LICENSE in the source distribution or at
  55. * https://www.openssl.org/source/license.html
  56. */
  57. /* ====================================================================
  58. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  59. *
  60. * Portions of the attached software ("Contribution") are developed by
  61. * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
  62. *
  63. * The Contribution is licensed pursuant to the OpenSSL open source
  64. * license provided above.
  65. *
  66. * ECC cipher suite support in OpenSSL originally written by
  67. * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
  68. *
  69. */
  70. /* ====================================================================
  71. * Copyright 2005 Nokia. All rights reserved.
  72. *
  73. * The portions of the attached software ("Contribution") is developed by
  74. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  75. * license.
  76. *
  77. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  78. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  79. * support (see RFC 4279) to OpenSSL.
  80. *
  81. * No patent licenses or other rights except those expressly stated in
  82. * the OpenSSL open source license shall be deemed granted or received
  83. * expressly, by implication, estoppel, or otherwise.
  84. *
  85. * No assurances are provided by Nokia that the Contribution does not
  86. * infringe the patent or other intellectual property rights of any third
  87. * party or that the license provides you with all the necessary rights
  88. * to make use of the Contribution.
  89. *
  90. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  91. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  92. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  93. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  94. * OTHERWISE.
  95. */
  96. #include <stdio.h>
  97. #include "../ssl_locl.h"
  98. #include "statem_locl.h"
  99. #include <openssl/buffer.h>
  100. #include <openssl/rand.h>
  101. #include <openssl/objects.h>
  102. #include <openssl/evp.h>
  103. #ifndef OPENSSL_NO_MD5
  104. # include <openssl/md5.h>
  105. #endif
  106. #ifndef OPENSSL_NO_DH
  107. # include <openssl/dh.h>
  108. #endif
  109. #ifndef OPENSSL_NO_SM2
  110. # include <openssl/sm2.h>
  111. #endif
  112. #include <openssl/bn.h>
  113. #ifndef OPENSSL_NO_ENGINE
  114. # include <openssl/engine.h>
  115. #endif
  116. static ossl_inline int cert_req_allowed(SSL *s);
  117. static int key_exchange_expected(SSL *s);
  118. static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b);
  119. static int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
  120. unsigned char *p);
  121. /*
  122. * Is a CertificateRequest message allowed at the moment or not?
  123. *
  124. * Return values are:
  125. * 1: Yes
  126. * 0: No
  127. */
  128. static ossl_inline int cert_req_allowed(SSL *s)
  129. {
  130. /* TLS does not like anon-DH with client cert */
  131. if ((s->version > SSL3_VERSION
  132. && (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL))
  133. || (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aSRP | SSL_aPSK)))
  134. return 0;
  135. return 1;
  136. }
  137. /*
  138. * Should we expect the ServerKeyExchange message or not?
  139. *
  140. * Return values are:
  141. * 1: Yes
  142. * 0: No
  143. */
  144. static int key_exchange_expected(SSL *s)
  145. {
  146. long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  147. #ifndef OPENSSL_NO_GMTLS
  148. if (SSL_IS_GMTLS(s))
  149. return 1;
  150. #endif
  151. /*
  152. * Can't skip server key exchange if this is an ephemeral
  153. * ciphersuite or for SRP
  154. */
  155. if (alg_k & (SSL_kDHE | SSL_kECDHE | SSL_kDHEPSK | SSL_kECDHEPSK |
  156. SSL_kSM2DHE | SSL_kSM2PSK | SSL_kSRP)) {
  157. return 1;
  158. }
  159. return 0;
  160. }
  161. /*
  162. * ossl_statem_client_read_transition() encapsulates the logic for the allowed
  163. * handshake state transitions when the client is reading messages from the
  164. * server. The message type that the server has sent is provided in |mt|. The
  165. * current state is in |s->statem.hand_state|.
  166. *
  167. * Return values are:
  168. * 1: Success (transition allowed)
  169. * 0: Error (transition not allowed)
  170. */
  171. int ossl_statem_client_read_transition(SSL *s, int mt)
  172. {
  173. OSSL_STATEM *st = &s->statem;
  174. int ske_expected;
  175. switch (st->hand_state) {
  176. case TLS_ST_CW_CLNT_HELLO:
  177. if (mt == SSL3_MT_SERVER_HELLO) {
  178. st->hand_state = TLS_ST_CR_SRVR_HELLO;
  179. return 1;
  180. }
  181. if (SSL_IS_DTLS(s)) {
  182. if (mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
  183. st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
  184. return 1;
  185. }
  186. }
  187. break;
  188. case TLS_ST_CR_SRVR_HELLO:
  189. if (s->hit) {
  190. if (s->tlsext_ticket_expected) {
  191. if (mt == SSL3_MT_NEWSESSION_TICKET) {
  192. st->hand_state = TLS_ST_CR_SESSION_TICKET;
  193. return 1;
  194. }
  195. } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  196. st->hand_state = TLS_ST_CR_CHANGE;
  197. return 1;
  198. }
  199. } else {
  200. if (SSL_IS_DTLS(s) && mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
  201. st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
  202. return 1;
  203. } else if (s->version >= TLS1_VERSION
  204. && s->tls_session_secret_cb != NULL
  205. && s->session->tlsext_tick != NULL
  206. && mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  207. /*
  208. * Normally, we can tell if the server is resuming the session
  209. * from the session ID. EAP-FAST (RFC 4851), however, relies on
  210. * the next server message after the ServerHello to determine if
  211. * the server is resuming.
  212. */
  213. s->hit = 1;
  214. st->hand_state = TLS_ST_CR_CHANGE;
  215. return 1;
  216. } else if (!(s->s3->tmp.new_cipher->algorithm_auth
  217. & (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
  218. if (mt == SSL3_MT_CERTIFICATE) {
  219. st->hand_state = TLS_ST_CR_CERT;
  220. return 1;
  221. }
  222. } else {
  223. ske_expected = key_exchange_expected(s);
  224. /* SKE is optional for some PSK ciphersuites */
  225. if (ske_expected
  226. || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)
  227. && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
  228. if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
  229. st->hand_state = TLS_ST_CR_KEY_EXCH;
  230. return 1;
  231. }
  232. } else if (mt == SSL3_MT_CERTIFICATE_REQUEST
  233. && cert_req_allowed(s)) {
  234. st->hand_state = TLS_ST_CR_CERT_REQ;
  235. return 1;
  236. } else if (mt == SSL3_MT_SERVER_DONE) {
  237. st->hand_state = TLS_ST_CR_SRVR_DONE;
  238. return 1;
  239. }
  240. }
  241. }
  242. break;
  243. case TLS_ST_CR_CERT:
  244. /*
  245. * The CertificateStatus message is optional even if
  246. * |tlsext_status_expected| is set
  247. */
  248. if (s->tlsext_status_expected && mt == SSL3_MT_CERTIFICATE_STATUS) {
  249. st->hand_state = TLS_ST_CR_CERT_STATUS;
  250. return 1;
  251. }
  252. /* Fall through */
  253. case TLS_ST_CR_CERT_STATUS:
  254. ske_expected = key_exchange_expected(s);
  255. /* SKE is optional for some PSK ciphersuites */
  256. if (ske_expected || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)
  257. && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
  258. if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
  259. st->hand_state = TLS_ST_CR_KEY_EXCH;
  260. return 1;
  261. }
  262. goto err;
  263. }
  264. /* Fall through */
  265. case TLS_ST_CR_KEY_EXCH:
  266. if (mt == SSL3_MT_CERTIFICATE_REQUEST) {
  267. if (cert_req_allowed(s)) {
  268. st->hand_state = TLS_ST_CR_CERT_REQ;
  269. return 1;
  270. }
  271. goto err;
  272. }
  273. /* Fall through */
  274. case TLS_ST_CR_CERT_REQ:
  275. if (mt == SSL3_MT_SERVER_DONE) {
  276. st->hand_state = TLS_ST_CR_SRVR_DONE;
  277. return 1;
  278. }
  279. break;
  280. case TLS_ST_CW_FINISHED:
  281. if (s->tlsext_ticket_expected) {
  282. if (mt == SSL3_MT_NEWSESSION_TICKET) {
  283. st->hand_state = TLS_ST_CR_SESSION_TICKET;
  284. return 1;
  285. }
  286. } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  287. st->hand_state = TLS_ST_CR_CHANGE;
  288. return 1;
  289. }
  290. break;
  291. case TLS_ST_CR_SESSION_TICKET:
  292. if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  293. st->hand_state = TLS_ST_CR_CHANGE;
  294. return 1;
  295. }
  296. break;
  297. case TLS_ST_CR_CHANGE:
  298. if (mt == SSL3_MT_FINISHED) {
  299. st->hand_state = TLS_ST_CR_FINISHED;
  300. return 1;
  301. }
  302. break;
  303. default:
  304. break;
  305. }
  306. err:
  307. /* No valid transition found */
  308. ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_UNEXPECTED_MESSAGE);
  309. SSLerr(SSL_F_OSSL_STATEM_CLIENT_READ_TRANSITION, SSL_R_UNEXPECTED_MESSAGE);
  310. return 0;
  311. }
  312. /*
  313. * client_write_transition() works out what handshake state to move to next
  314. * when the client is writing messages to be sent to the server.
  315. */
  316. WRITE_TRAN ossl_statem_client_write_transition(SSL *s)
  317. {
  318. OSSL_STATEM *st = &s->statem;
  319. switch (st->hand_state) {
  320. case TLS_ST_OK:
  321. /* Renegotiation - fall through */
  322. case TLS_ST_BEFORE:
  323. st->hand_state = TLS_ST_CW_CLNT_HELLO;
  324. return WRITE_TRAN_CONTINUE;
  325. case TLS_ST_CW_CLNT_HELLO:
  326. /*
  327. * No transition at the end of writing because we don't know what
  328. * we will be sent
  329. */
  330. return WRITE_TRAN_FINISHED;
  331. case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
  332. st->hand_state = TLS_ST_CW_CLNT_HELLO;
  333. return WRITE_TRAN_CONTINUE;
  334. case TLS_ST_CR_SRVR_DONE:
  335. if (s->s3->tmp.cert_req)
  336. st->hand_state = TLS_ST_CW_CERT;
  337. else
  338. st->hand_state = TLS_ST_CW_KEY_EXCH;
  339. return WRITE_TRAN_CONTINUE;
  340. case TLS_ST_CW_CERT:
  341. st->hand_state = TLS_ST_CW_KEY_EXCH;
  342. return WRITE_TRAN_CONTINUE;
  343. case TLS_ST_CW_KEY_EXCH:
  344. /*
  345. * For TLS, cert_req is set to 2, so a cert chain of nothing is
  346. * sent, but no verify packet is sent
  347. */
  348. /*
  349. * XXX: For now, we do not support client authentication in ECDH
  350. * cipher suites with ECDH (rather than ECDSA) certificates. We
  351. * need to skip the certificate verify message when client's
  352. * ECDH public key is sent inside the client certificate.
  353. */
  354. if (s->s3->tmp.cert_req == 1) {
  355. st->hand_state = TLS_ST_CW_CERT_VRFY;
  356. } else {
  357. st->hand_state = TLS_ST_CW_CHANGE;
  358. }
  359. if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
  360. st->hand_state = TLS_ST_CW_CHANGE;
  361. }
  362. return WRITE_TRAN_CONTINUE;
  363. case TLS_ST_CW_CERT_VRFY:
  364. st->hand_state = TLS_ST_CW_CHANGE;
  365. return WRITE_TRAN_CONTINUE;
  366. case TLS_ST_CW_CHANGE:
  367. #if defined(OPENSSL_NO_NEXTPROTONEG)
  368. st->hand_state = TLS_ST_CW_FINISHED;
  369. #else
  370. if (!SSL_IS_DTLS(s) && s->s3->next_proto_neg_seen)
  371. st->hand_state = TLS_ST_CW_NEXT_PROTO;
  372. else
  373. st->hand_state = TLS_ST_CW_FINISHED;
  374. #endif
  375. return WRITE_TRAN_CONTINUE;
  376. #if !defined(OPENSSL_NO_NEXTPROTONEG)
  377. case TLS_ST_CW_NEXT_PROTO:
  378. st->hand_state = TLS_ST_CW_FINISHED;
  379. return WRITE_TRAN_CONTINUE;
  380. #endif
  381. case TLS_ST_CW_FINISHED:
  382. if (s->hit) {
  383. st->hand_state = TLS_ST_OK;
  384. ossl_statem_set_in_init(s, 0);
  385. return WRITE_TRAN_CONTINUE;
  386. } else {
  387. return WRITE_TRAN_FINISHED;
  388. }
  389. case TLS_ST_CR_FINISHED:
  390. if (s->hit) {
  391. st->hand_state = TLS_ST_CW_CHANGE;
  392. return WRITE_TRAN_CONTINUE;
  393. } else {
  394. st->hand_state = TLS_ST_OK;
  395. ossl_statem_set_in_init(s, 0);
  396. return WRITE_TRAN_CONTINUE;
  397. }
  398. default:
  399. /* Shouldn't happen */
  400. return WRITE_TRAN_ERROR;
  401. }
  402. }
  403. /*
  404. * Perform any pre work that needs to be done prior to sending a message from
  405. * the client to the server.
  406. */
  407. WORK_STATE ossl_statem_client_pre_work(SSL *s, WORK_STATE wst)
  408. {
  409. OSSL_STATEM *st = &s->statem;
  410. switch (st->hand_state) {
  411. case TLS_ST_CW_CLNT_HELLO:
  412. s->shutdown = 0;
  413. if (SSL_IS_DTLS(s)) {
  414. /* every DTLS ClientHello resets Finished MAC */
  415. if (!ssl3_init_finished_mac(s)) {
  416. ossl_statem_set_error(s);
  417. return WORK_ERROR;
  418. }
  419. }
  420. break;
  421. case TLS_ST_CW_CHANGE:
  422. if (SSL_IS_DTLS(s)) {
  423. if (s->hit) {
  424. /*
  425. * We're into the last flight so we don't retransmit these
  426. * messages unless we need to.
  427. */
  428. st->use_timer = 0;
  429. }
  430. #ifndef OPENSSL_NO_SCTP
  431. if (BIO_dgram_is_sctp(SSL_get_wbio(s)))
  432. return dtls_wait_for_dry(s);
  433. #endif
  434. }
  435. return WORK_FINISHED_CONTINUE;
  436. case TLS_ST_OK:
  437. return tls_finish_handshake(s, wst);
  438. default:
  439. /* No pre work to be done */
  440. break;
  441. }
  442. return WORK_FINISHED_CONTINUE;
  443. }
  444. /*
  445. * Perform any work that needs to be done after sending a message from the
  446. * client to the server.
  447. */
  448. WORK_STATE ossl_statem_client_post_work(SSL *s, WORK_STATE wst)
  449. {
  450. OSSL_STATEM *st = &s->statem;
  451. s->init_num = 0;
  452. switch (st->hand_state) {
  453. case TLS_ST_CW_CLNT_HELLO:
  454. if (wst == WORK_MORE_A && statem_flush(s) != 1)
  455. return WORK_MORE_A;
  456. if (SSL_IS_DTLS(s)) {
  457. /* Treat the next message as the first packet */
  458. s->first_packet = 1;
  459. }
  460. break;
  461. case TLS_ST_CW_KEY_EXCH:
  462. if (tls_client_key_exchange_post_work(s) == 0)
  463. return WORK_ERROR;
  464. break;
  465. case TLS_ST_CW_CHANGE:
  466. s->session->cipher = s->s3->tmp.new_cipher;
  467. #ifdef OPENSSL_NO_COMP
  468. s->session->compress_meth = 0;
  469. #else
  470. if (s->s3->tmp.new_compression == NULL)
  471. s->session->compress_meth = 0;
  472. else
  473. s->session->compress_meth = s->s3->tmp.new_compression->id;
  474. #endif
  475. if (!s->method->ssl3_enc->setup_key_block(s))
  476. return WORK_ERROR;
  477. if (!s->method->ssl3_enc->change_cipher_state(s,
  478. SSL3_CHANGE_CIPHER_CLIENT_WRITE))
  479. return WORK_ERROR;
  480. if (SSL_IS_DTLS(s)) {
  481. #ifndef OPENSSL_NO_SCTP
  482. if (s->hit) {
  483. /*
  484. * Change to new shared key of SCTP-Auth, will be ignored if
  485. * no SCTP used.
  486. */
  487. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
  488. 0, NULL);
  489. }
  490. #endif
  491. dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
  492. }
  493. break;
  494. case TLS_ST_CW_FINISHED:
  495. #ifndef OPENSSL_NO_SCTP
  496. if (wst == WORK_MORE_A && SSL_IS_DTLS(s) && s->hit == 0) {
  497. /*
  498. * Change to new shared key of SCTP-Auth, will be ignored if
  499. * no SCTP used.
  500. */
  501. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
  502. 0, NULL);
  503. }
  504. #endif
  505. if (statem_flush(s) != 1)
  506. return WORK_MORE_B;
  507. break;
  508. default:
  509. /* No post work to be done */
  510. break;
  511. }
  512. return WORK_FINISHED_CONTINUE;
  513. }
  514. /*
  515. * Construct a message to be sent from the client to the server.
  516. *
  517. * Valid return values are:
  518. * 1: Success
  519. * 0: Error
  520. */
  521. int ossl_statem_client_construct_message(SSL *s)
  522. {
  523. OSSL_STATEM *st = &s->statem;
  524. switch (st->hand_state) {
  525. case TLS_ST_CW_CLNT_HELLO:
  526. return tls_construct_client_hello(s);
  527. case TLS_ST_CW_CERT:
  528. #ifndef OPENSSL_NO_GMTLS
  529. if (SSL_IS_GMTLS(s))
  530. return gmtls_construct_client_certificate(s);
  531. else
  532. #endif
  533. return tls_construct_client_certificate(s);
  534. case TLS_ST_CW_KEY_EXCH:
  535. #ifndef OPENSSL_NO_GMTLS
  536. if (SSL_IS_GMTLS(s))
  537. return gmtls_construct_client_key_exchange(s);
  538. else
  539. #endif
  540. return tls_construct_client_key_exchange(s);
  541. case TLS_ST_CW_CERT_VRFY:
  542. return tls_construct_client_verify(s);
  543. case TLS_ST_CW_CHANGE:
  544. if (SSL_IS_DTLS(s))
  545. return dtls_construct_change_cipher_spec(s);
  546. else
  547. return tls_construct_change_cipher_spec(s);
  548. #if !defined(OPENSSL_NO_NEXTPROTONEG)
  549. case TLS_ST_CW_NEXT_PROTO:
  550. return tls_construct_next_proto(s);
  551. #endif
  552. case TLS_ST_CW_FINISHED:
  553. return tls_construct_finished(s,
  554. s->method->
  555. ssl3_enc->client_finished_label,
  556. s->method->
  557. ssl3_enc->client_finished_label_len);
  558. default:
  559. /* Shouldn't happen */
  560. break;
  561. }
  562. return 0;
  563. }
  564. /*
  565. * Returns the maximum allowed length for the current message that we are
  566. * reading. Excludes the message header.
  567. */
  568. unsigned long ossl_statem_client_max_message_size(SSL *s)
  569. {
  570. OSSL_STATEM *st = &s->statem;
  571. switch (st->hand_state) {
  572. case TLS_ST_CR_SRVR_HELLO:
  573. return SERVER_HELLO_MAX_LENGTH;
  574. case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
  575. return HELLO_VERIFY_REQUEST_MAX_LENGTH;
  576. case TLS_ST_CR_CERT:
  577. return s->max_cert_list;
  578. case TLS_ST_CR_CERT_STATUS:
  579. return SSL3_RT_MAX_PLAIN_LENGTH;
  580. case TLS_ST_CR_KEY_EXCH:
  581. return SERVER_KEY_EXCH_MAX_LENGTH;
  582. case TLS_ST_CR_CERT_REQ:
  583. /*
  584. * Set to s->max_cert_list for compatibility with previous releases. In
  585. * practice these messages can get quite long if servers are configured
  586. * to provide a long list of acceptable CAs
  587. */
  588. return s->max_cert_list;
  589. case TLS_ST_CR_SRVR_DONE:
  590. return SERVER_HELLO_DONE_MAX_LENGTH;
  591. case TLS_ST_CR_CHANGE:
  592. if (s->version == DTLS1_BAD_VER)
  593. return 3;
  594. return CCS_MAX_LENGTH;
  595. case TLS_ST_CR_SESSION_TICKET:
  596. return SSL3_RT_MAX_PLAIN_LENGTH;
  597. case TLS_ST_CR_FINISHED:
  598. return FINISHED_MAX_LENGTH;
  599. default:
  600. /* Shouldn't happen */
  601. break;
  602. }
  603. return 0;
  604. }
  605. /*
  606. * Process a message that the client has been received from the server.
  607. */
  608. MSG_PROCESS_RETURN ossl_statem_client_process_message(SSL *s, PACKET *pkt)
  609. {
  610. OSSL_STATEM *st = &s->statem;
  611. switch (st->hand_state) {
  612. case TLS_ST_CR_SRVR_HELLO:
  613. return tls_process_server_hello(s, pkt);
  614. case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
  615. return dtls_process_hello_verify(s, pkt);
  616. case TLS_ST_CR_CERT:
  617. #ifndef OPENSSL_NO_GMTLS
  618. if (SSL_IS_GMTLS(s))
  619. return tls_process_server_certificate(s, pkt);
  620. #endif
  621. return tls_process_server_certificate(s, pkt);
  622. case TLS_ST_CR_CERT_STATUS:
  623. return tls_process_cert_status(s, pkt);
  624. case TLS_ST_CR_KEY_EXCH:
  625. #ifndef OPENSSL_NO_GMTLS
  626. if (SSL_IS_GMTLS(s))
  627. return gmtls_process_server_key_exchange(s, pkt);
  628. #endif
  629. return tls_process_server_key_exchange(s, pkt);
  630. case TLS_ST_CR_CERT_REQ:
  631. return tls_process_certificate_request(s, pkt);
  632. case TLS_ST_CR_SRVR_DONE:
  633. return tls_process_server_done(s, pkt);
  634. case TLS_ST_CR_CHANGE:
  635. return tls_process_change_cipher_spec(s, pkt);
  636. case TLS_ST_CR_SESSION_TICKET:
  637. return tls_process_new_session_ticket(s, pkt);
  638. case TLS_ST_CR_FINISHED:
  639. return tls_process_finished(s, pkt);
  640. default:
  641. /* Shouldn't happen */
  642. break;
  643. }
  644. return MSG_PROCESS_ERROR;
  645. }
  646. /*
  647. * Perform any further processing required following the receipt of a message
  648. * from the server
  649. */
  650. WORK_STATE ossl_statem_client_post_process_message(SSL *s, WORK_STATE wst)
  651. {
  652. OSSL_STATEM *st = &s->statem;
  653. switch (st->hand_state) {
  654. case TLS_ST_CR_CERT_REQ:
  655. return tls_prepare_client_certificate(s, wst);
  656. #ifndef OPENSSL_NO_SCTP
  657. case TLS_ST_CR_SRVR_DONE:
  658. /* We only get here if we are using SCTP and we are renegotiating */
  659. if (BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
  660. s->s3->in_read_app_data = 2;
  661. s->rwstate = SSL_READING;
  662. BIO_clear_retry_flags(SSL_get_rbio(s));
  663. BIO_set_retry_read(SSL_get_rbio(s));
  664. ossl_statem_set_sctp_read_sock(s, 1);
  665. return WORK_MORE_A;
  666. }
  667. ossl_statem_set_sctp_read_sock(s, 0);
  668. return WORK_FINISHED_STOP;
  669. #endif
  670. default:
  671. break;
  672. }
  673. /* Shouldn't happen */
  674. return WORK_ERROR;
  675. }
  676. int tls_construct_client_hello(SSL *s)
  677. {
  678. unsigned char *buf;
  679. unsigned char *p, *d;
  680. int i;
  681. int protverr;
  682. unsigned long l;
  683. int al = 0;
  684. #ifndef OPENSSL_NO_COMP
  685. int j;
  686. SSL_COMP *comp;
  687. #endif
  688. SSL_SESSION *sess = s->session;
  689. buf = (unsigned char *)s->init_buf->data;
  690. /* Work out what SSL/TLS/DTLS version to use */
  691. protverr = ssl_set_client_hello_version(s);
  692. if (protverr != 0) {
  693. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, protverr);
  694. goto err;
  695. }
  696. if ((sess == NULL) || !ssl_version_supported(s, sess->ssl_version) ||
  697. /*
  698. * In the case of EAP-FAST, we can have a pre-shared
  699. * "ticket" without a session ID.
  700. */
  701. (!sess->session_id_length && !sess->tlsext_tick) ||
  702. (sess->not_resumable)) {
  703. if (!ssl_get_new_session(s, 0))
  704. goto err;
  705. }
  706. /* else use the pre-loaded session */
  707. p = s->s3->client_random;
  708. /*
  709. * for DTLS if client_random is initialized, reuse it, we are
  710. * required to use same upon reply to HelloVerify
  711. */
  712. if (SSL_IS_DTLS(s)) {
  713. size_t idx;
  714. i = 1;
  715. for (idx = 0; idx < sizeof(s->s3->client_random); idx++) {
  716. if (p[idx]) {
  717. i = 0;
  718. break;
  719. }
  720. }
  721. } else
  722. i = 1;
  723. if (i && ssl_fill_hello_random(s, 0, p, sizeof(s->s3->client_random)) <= 0)
  724. goto err;
  725. /* Do the message type and length last */
  726. d = p = ssl_handshake_start(s);
  727. /*-
  728. * version indicates the negotiated version: for example from
  729. * an SSLv2/v3 compatible client hello). The client_version
  730. * field is the maximum version we permit and it is also
  731. * used in RSA encrypted premaster secrets. Some servers can
  732. * choke if we initially report a higher version then
  733. * renegotiate to a lower one in the premaster secret. This
  734. * didn't happen with TLS 1.0 as most servers supported it
  735. * but it can with TLS 1.1 or later if the server only supports
  736. * 1.0.
  737. *
  738. * Possible scenario with previous logic:
  739. * 1. Client hello indicates TLS 1.2
  740. * 2. Server hello says TLS 1.0
  741. * 3. RSA encrypted premaster secret uses 1.2.
  742. * 4. Handshake proceeds using TLS 1.0.
  743. * 5. Server sends hello request to renegotiate.
  744. * 6. Client hello indicates TLS v1.0 as we now
  745. * know that is maximum server supports.
  746. * 7. Server chokes on RSA encrypted premaster secret
  747. * containing version 1.0.
  748. *
  749. * For interoperability it should be OK to always use the
  750. * maximum version we support in client hello and then rely
  751. * on the checking of version to ensure the servers isn't
  752. * being inconsistent: for example initially negotiating with
  753. * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
  754. * client_version in client hello and not resetting it to
  755. * the negotiated version.
  756. */
  757. *(p++) = s->client_version >> 8;
  758. *(p++) = s->client_version & 0xff;
  759. /* Random stuff */
  760. memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
  761. p += SSL3_RANDOM_SIZE;
  762. /* Session ID */
  763. if (s->new_session)
  764. i = 0;
  765. else
  766. i = s->session->session_id_length;
  767. *(p++) = i;
  768. if (i != 0) {
  769. if (i > (int)sizeof(s->session->session_id)) {
  770. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
  771. goto err;
  772. }
  773. memcpy(p, s->session->session_id, i);
  774. p += i;
  775. }
  776. /* cookie stuff for DTLS */
  777. if (SSL_IS_DTLS(s)) {
  778. if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
  779. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
  780. goto err;
  781. }
  782. *(p++) = s->d1->cookie_len;
  783. memcpy(p, s->d1->cookie, s->d1->cookie_len);
  784. p += s->d1->cookie_len;
  785. }
  786. /* Ciphers supported */
  787. i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]));
  788. if (i == 0) {
  789. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
  790. goto err;
  791. }
  792. #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
  793. /*
  794. * Some servers hang if client hello > 256 bytes as hack workaround
  795. * chop number of supported ciphers to keep it well below this if we
  796. * use TLS v1.2
  797. */
  798. if (TLS1_get_version(s) >= TLS1_2_VERSION
  799. && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
  800. i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
  801. #endif
  802. s2n(i, p);
  803. p += i;
  804. /* COMPRESSION */
  805. #ifdef OPENSSL_NO_COMP
  806. *(p++) = 1;
  807. #else
  808. if (!ssl_allow_compression(s) || !s->ctx->comp_methods)
  809. j = 0;
  810. else
  811. j = sk_SSL_COMP_num(s->ctx->comp_methods);
  812. *(p++) = 1 + j;
  813. for (i = 0; i < j; i++) {
  814. comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
  815. *(p++) = comp->id;
  816. }
  817. #endif
  818. *(p++) = 0; /* Add the NULL method */
  819. /* TLS extensions */
  820. if (ssl_prepare_clienthello_tlsext(s) <= 0) {
  821. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
  822. goto err;
  823. }
  824. if ((s->version != GMTLS_VERSION) && (p =
  825. ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
  826. &al)) == NULL) {
  827. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  828. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
  829. goto err;
  830. }
  831. l = p - d;
  832. if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) {
  833. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  834. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
  835. goto err;
  836. }
  837. return 1;
  838. err:
  839. ossl_statem_set_error(s);
  840. return 0;
  841. }
  842. MSG_PROCESS_RETURN dtls_process_hello_verify(SSL *s, PACKET *pkt)
  843. {
  844. int al;
  845. unsigned int cookie_len;
  846. PACKET cookiepkt;
  847. if (!PACKET_forward(pkt, 2)
  848. || !PACKET_get_length_prefixed_1(pkt, &cookiepkt)) {
  849. al = SSL_AD_DECODE_ERROR;
  850. SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH);
  851. goto f_err;
  852. }
  853. cookie_len = PACKET_remaining(&cookiepkt);
  854. if (cookie_len > sizeof(s->d1->cookie)) {
  855. al = SSL_AD_ILLEGAL_PARAMETER;
  856. SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_TOO_LONG);
  857. goto f_err;
  858. }
  859. if (!PACKET_copy_bytes(&cookiepkt, s->d1->cookie, cookie_len)) {
  860. al = SSL_AD_DECODE_ERROR;
  861. SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH);
  862. goto f_err;
  863. }
  864. s->d1->cookie_len = cookie_len;
  865. return MSG_PROCESS_FINISHED_READING;
  866. f_err:
  867. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  868. ossl_statem_set_error(s);
  869. return MSG_PROCESS_ERROR;
  870. }
  871. MSG_PROCESS_RETURN tls_process_server_hello(SSL *s, PACKET *pkt)
  872. {
  873. STACK_OF(SSL_CIPHER) *sk;
  874. const SSL_CIPHER *c;
  875. PACKET session_id;
  876. size_t session_id_len;
  877. const unsigned char *cipherchars;
  878. int i, al = SSL_AD_INTERNAL_ERROR;
  879. unsigned int compression;
  880. unsigned int sversion;
  881. int protverr;
  882. #ifndef OPENSSL_NO_COMP
  883. SSL_COMP *comp;
  884. #endif
  885. if (!PACKET_get_net_2(pkt, &sversion)) {
  886. al = SSL_AD_DECODE_ERROR;
  887. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
  888. goto f_err;
  889. }
  890. protverr = ssl_choose_client_version(s, sversion);
  891. if (protverr != 0) {
  892. al = SSL_AD_PROTOCOL_VERSION;
  893. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, protverr);
  894. goto f_err;
  895. }
  896. /* load the server hello data */
  897. /* load the server random */
  898. if (!PACKET_copy_bytes(pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) {
  899. al = SSL_AD_DECODE_ERROR;
  900. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
  901. goto f_err;
  902. }
  903. s->hit = 0;
  904. /* Get the session-id. */
  905. if (!PACKET_get_length_prefixed_1(pkt, &session_id)) {
  906. al = SSL_AD_DECODE_ERROR;
  907. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
  908. goto f_err;
  909. }
  910. session_id_len = PACKET_remaining(&session_id);
  911. if (session_id_len > sizeof s->session->session_id
  912. || session_id_len > SSL3_SESSION_ID_SIZE) {
  913. al = SSL_AD_ILLEGAL_PARAMETER;
  914. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);
  915. goto f_err;
  916. }
  917. if (!PACKET_get_bytes(pkt, &cipherchars, TLS_CIPHER_LEN)) {
  918. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
  919. al = SSL_AD_DECODE_ERROR;
  920. goto f_err;
  921. }
  922. /*
  923. * Check if we can resume the session based on external pre-shared secret.
  924. * EAP-FAST (RFC 4851) supports two types of session resumption.
  925. * Resumption based on server-side state works with session IDs.
  926. * Resumption based on pre-shared Protected Access Credentials (PACs)
  927. * works by overriding the SessionTicket extension at the application
  928. * layer, and does not send a session ID. (We do not know whether EAP-FAST
  929. * servers would honour the session ID.) Therefore, the session ID alone
  930. * is not a reliable indicator of session resumption, so we first check if
  931. * we can resume, and later peek at the next handshake message to see if the
  932. * server wants to resume.
  933. */
  934. if (s->version >= TLS1_VERSION && s->tls_session_secret_cb &&
  935. s->session->tlsext_tick) {
  936. const SSL_CIPHER *pref_cipher = NULL;
  937. s->session->master_key_length = sizeof(s->session->master_key);
  938. if (s->tls_session_secret_cb(s, s->session->master_key,
  939. &s->session->master_key_length,
  940. NULL, &pref_cipher,
  941. s->tls_session_secret_cb_arg)) {
  942. s->session->cipher = pref_cipher ?
  943. pref_cipher : ssl_get_cipher_by_char(s, cipherchars);
  944. } else {
  945. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
  946. al = SSL_AD_INTERNAL_ERROR;
  947. goto f_err;
  948. }
  949. }
  950. if (session_id_len != 0 && session_id_len == s->session->session_id_length
  951. && memcmp(PACKET_data(&session_id), s->session->session_id,
  952. session_id_len) == 0) {
  953. if (s->sid_ctx_length != s->session->sid_ctx_length
  954. || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
  955. /* actually a client application bug */
  956. al = SSL_AD_ILLEGAL_PARAMETER;
  957. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
  958. SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
  959. goto f_err;
  960. }
  961. s->hit = 1;
  962. } else {
  963. /*
  964. * If we were trying for session-id reuse but the server
  965. * didn't echo the ID, make a new SSL_SESSION.
  966. * In the case of EAP-FAST and PAC, we do not send a session ID,
  967. * so the PAC-based session secret is always preserved. It'll be
  968. * overwritten if the server refuses resumption.
  969. */
  970. if (s->session->session_id_length > 0) {
  971. s->ctx->stats.sess_miss++;
  972. if (!ssl_get_new_session(s, 0)) {
  973. goto f_err;
  974. }
  975. }
  976. s->session->ssl_version = s->version;
  977. s->session->session_id_length = session_id_len;
  978. /* session_id_len could be 0 */
  979. if (session_id_len > 0)
  980. memcpy(s->session->session_id, PACKET_data(&session_id),
  981. session_id_len);
  982. }
  983. /* Session version and negotiated protocol version should match */
  984. if (s->version != s->session->ssl_version) {
  985. al = SSL_AD_PROTOCOL_VERSION;
  986. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
  987. SSL_R_SSL_SESSION_VERSION_MISMATCH);
  988. goto f_err;
  989. }
  990. c = ssl_get_cipher_by_char(s, cipherchars);
  991. if (c == NULL) {
  992. /* unknown cipher */
  993. al = SSL_AD_ILLEGAL_PARAMETER;
  994. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
  995. goto f_err;
  996. }
  997. /*
  998. * Now that we know the version, update the check to see if it's an allowed
  999. * version.
  1000. */
  1001. s->s3->tmp.min_ver = s->version;
  1002. s->s3->tmp.max_ver = s->version;
  1003. /*
  1004. * If it is a disabled cipher we either didn't send it in client hello,
  1005. * or it's not allowed for the selected protocol. So we return an error.
  1006. */
  1007. if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) {
  1008. al = SSL_AD_ILLEGAL_PARAMETER;
  1009. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
  1010. goto f_err;
  1011. }
  1012. sk = ssl_get_ciphers_by_id(s);
  1013. i = sk_SSL_CIPHER_find(sk, c);
  1014. if (i < 0) {
  1015. /* we did not say we would use this cipher */
  1016. al = SSL_AD_ILLEGAL_PARAMETER;
  1017. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
  1018. goto f_err;
  1019. }
  1020. /*
  1021. * Depending on the session caching (internal/external), the cipher
  1022. * and/or cipher_id values may not be set. Make sure that cipher_id is
  1023. * set and use it for comparison.
  1024. */
  1025. if (s->session->cipher)
  1026. s->session->cipher_id = s->session->cipher->id;
  1027. if (s->hit && (s->session->cipher_id != c->id)) {
  1028. al = SSL_AD_ILLEGAL_PARAMETER;
  1029. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
  1030. SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
  1031. goto f_err;
  1032. }
  1033. s->s3->tmp.new_cipher = c;
  1034. /* lets get the compression algorithm */
  1035. /* COMPRESSION */
  1036. if (!PACKET_get_1(pkt, &compression)) {
  1037. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
  1038. al = SSL_AD_DECODE_ERROR;
  1039. goto f_err;
  1040. }
  1041. #ifdef OPENSSL_NO_COMP
  1042. if (compression != 0) {
  1043. al = SSL_AD_ILLEGAL_PARAMETER;
  1044. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
  1045. SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  1046. goto f_err;
  1047. }
  1048. /*
  1049. * If compression is disabled we'd better not try to resume a session
  1050. * using compression.
  1051. */
  1052. if (s->session->compress_meth != 0) {
  1053. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
  1054. goto f_err;
  1055. }
  1056. #else
  1057. if (s->hit && compression != s->session->compress_meth) {
  1058. al = SSL_AD_ILLEGAL_PARAMETER;
  1059. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
  1060. SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
  1061. goto f_err;
  1062. }
  1063. if (compression == 0)
  1064. comp = NULL;
  1065. else if (!ssl_allow_compression(s)) {
  1066. al = SSL_AD_ILLEGAL_PARAMETER;
  1067. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
  1068. goto f_err;
  1069. } else {
  1070. comp = ssl3_comp_find(s->ctx->comp_methods, compression);
  1071. }
  1072. if (compression != 0 && comp == NULL) {
  1073. al = SSL_AD_ILLEGAL_PARAMETER;
  1074. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
  1075. SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  1076. goto f_err;
  1077. } else {
  1078. s->s3->tmp.new_compression = comp;
  1079. }
  1080. #endif
  1081. /* TLS extensions */
  1082. if (!ssl_parse_serverhello_tlsext(s, pkt)) {
  1083. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_PARSE_TLSEXT);
  1084. goto err;
  1085. }
  1086. if (PACKET_remaining(pkt) != 0) {
  1087. /* wrong packet length */
  1088. al = SSL_AD_DECODE_ERROR;
  1089. SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
  1090. goto f_err;
  1091. }
  1092. #ifndef OPENSSL_NO_SCTP
  1093. if (SSL_IS_DTLS(s) && s->hit) {
  1094. unsigned char sctpauthkey[64];
  1095. char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
  1096. /*
  1097. * Add new shared key for SCTP-Auth, will be ignored if
  1098. * no SCTP used.
  1099. */
  1100. memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
  1101. sizeof(DTLS1_SCTP_AUTH_LABEL));
  1102. if (SSL_export_keying_material(s, sctpauthkey,
  1103. sizeof(sctpauthkey),
  1104. labelbuffer,
  1105. sizeof(labelbuffer), NULL, 0, 0) <= 0)
  1106. goto err;
  1107. BIO_ctrl(SSL_get_wbio(s),
  1108. BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
  1109. sizeof(sctpauthkey), sctpauthkey);
  1110. }
  1111. #endif
  1112. return MSG_PROCESS_CONTINUE_READING;
  1113. f_err:
  1114. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  1115. err:
  1116. ossl_statem_set_error(s);
  1117. return MSG_PROCESS_ERROR;
  1118. }
  1119. MSG_PROCESS_RETURN tls_process_server_certificate(SSL *s, PACKET *pkt)
  1120. {
  1121. int al, i, ret = MSG_PROCESS_ERROR, exp_idx;
  1122. unsigned long cert_list_len, cert_len;
  1123. X509 *x = NULL;
  1124. const unsigned char *certstart, *certbytes;
  1125. STACK_OF(X509) *sk = NULL;
  1126. EVP_PKEY *pkey = NULL;
  1127. if ((sk = sk_X509_new_null()) == NULL) {
  1128. SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
  1129. goto err;
  1130. }
  1131. if (!PACKET_get_net_3(pkt, &cert_list_len)
  1132. || PACKET_remaining(pkt) != cert_list_len) {
  1133. al = SSL_AD_DECODE_ERROR;
  1134. SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
  1135. goto f_err;
  1136. }
  1137. while (PACKET_remaining(pkt)) {
  1138. if (!PACKET_get_net_3(pkt, &cert_len)
  1139. || !PACKET_get_bytes(pkt, &certbytes, cert_len)) {
  1140. al = SSL_AD_DECODE_ERROR;
  1141. SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
  1142. SSL_R_CERT_LENGTH_MISMATCH);
  1143. goto f_err;
  1144. }
  1145. certstart = certbytes;
  1146. x = d2i_X509(NULL, (const unsigned char **)&certbytes, cert_len);
  1147. if (x == NULL) {
  1148. al = SSL_AD_BAD_CERTIFICATE;
  1149. SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_ASN1_LIB);
  1150. goto f_err;
  1151. }
  1152. if (certbytes != (certstart + cert_len)) {
  1153. al = SSL_AD_DECODE_ERROR;
  1154. SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
  1155. SSL_R_CERT_LENGTH_MISMATCH);
  1156. goto f_err;
  1157. }
  1158. if (!sk_X509_push(sk, x)) {
  1159. SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
  1160. goto err;
  1161. }
  1162. x = NULL;
  1163. }
  1164. i = ssl_verify_cert_chain(s, sk);
  1165. /*
  1166. * The documented interface is that SSL_VERIFY_PEER should be set in order
  1167. * for client side verification of the server certificate to take place.
  1168. * However, historically the code has only checked that *any* flag is set
  1169. * to cause server verification to take place. Use of the other flags makes
  1170. * no sense in client mode. An attempt to clean up the semantics was
  1171. * reverted because at least one application *only* set
  1172. * SSL_VERIFY_FAIL_IF_NO_PEER_CERT. Prior to the clean up this still caused
  1173. * server verification to take place, after the clean up it silently did
  1174. * nothing. SSL_CTX_set_verify()/SSL_set_verify() cannot validate the flags
  1175. * sent to them because they are void functions. Therefore, we now use the
  1176. * (less clean) historic behaviour of performing validation if any flag is
  1177. * set. The *documented* interface remains the same.
  1178. */
  1179. if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
  1180. al = ssl_verify_alarm_type(s->verify_result);
  1181. SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
  1182. SSL_R_CERTIFICATE_VERIFY_FAILED);
  1183. goto f_err;
  1184. }
  1185. ERR_clear_error(); /* but we keep s->verify_result */
  1186. if (i > 1) {
  1187. SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, i);
  1188. al = SSL_AD_HANDSHAKE_FAILURE;
  1189. goto f_err;
  1190. }
  1191. s->session->peer_chain = sk;
  1192. /*
  1193. * Inconsistency alert: cert_chain does include the peer's certificate,
  1194. * which we don't include in statem_srvr.c
  1195. */
  1196. x = sk_X509_value(sk, 0);
  1197. sk = NULL;
  1198. /*
  1199. * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
  1200. */
  1201. pkey = X509_get0_pubkey(x);
  1202. if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
  1203. x = NULL;
  1204. al = SSL3_AL_FATAL;
  1205. SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
  1206. SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
  1207. goto f_err;
  1208. }
  1209. i = ssl_cert_type(x, pkey);
  1210. if (i < 0) {
  1211. x = NULL;
  1212. al = SSL3_AL_FATAL;
  1213. SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
  1214. SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  1215. goto f_err;
  1216. }
  1217. exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
  1218. if (exp_idx >= 0 && i != exp_idx
  1219. && (exp_idx != SSL_PKEY_GOST_EC ||
  1220. (i != SSL_PKEY_GOST12_512 && i != SSL_PKEY_GOST12_256
  1221. && i != SSL_PKEY_GOST01))) {
  1222. x = NULL;
  1223. al = SSL_AD_ILLEGAL_PARAMETER;
  1224. SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
  1225. SSL_R_WRONG_CERTIFICATE_TYPE);
  1226. goto f_err;
  1227. }
  1228. s->session->peer_type = i;
  1229. X509_free(s->session->peer);
  1230. X509_up_ref(x);
  1231. s->session->peer = x;
  1232. s->session->verify_result = s->verify_result;
  1233. x = NULL;
  1234. ret = MSG_PROCESS_CONTINUE_READING;
  1235. goto done;
  1236. f_err:
  1237. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  1238. err:
  1239. ossl_statem_set_error(s);
  1240. done:
  1241. X509_free(x);
  1242. sk_X509_pop_free(sk, X509_free);
  1243. return ret;
  1244. }
  1245. static int tls_process_ske_psk_preamble(SSL *s, PACKET *pkt, int *al)
  1246. {
  1247. #ifndef OPENSSL_NO_PSK
  1248. PACKET psk_identity_hint;
  1249. /* PSK ciphersuites are preceded by an identity hint */
  1250. if (!PACKET_get_length_prefixed_2(pkt, &psk_identity_hint)) {
  1251. *al = SSL_AD_DECODE_ERROR;
  1252. SSLerr(SSL_F_TLS_PROCESS_SKE_PSK_PREAMBLE, SSL_R_LENGTH_MISMATCH);
  1253. return 0;
  1254. }
  1255. /*
  1256. * Store PSK identity hint for later use, hint is used in
  1257. * tls_construct_client_key_exchange. Assume that the maximum length of
  1258. * a PSK identity hint can be as long as the maximum length of a PSK
  1259. * identity.
  1260. */
  1261. if (PACKET_remaining(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN) {
  1262. *al = SSL_AD_HANDSHAKE_FAILURE;
  1263. SSLerr(SSL_F_TLS_PROCESS_SKE_PSK_PREAMBLE, SSL_R_DATA_LENGTH_TOO_LONG);
  1264. return 0;
  1265. }
  1266. if (PACKET_remaining(&psk_identity_hint) == 0) {
  1267. OPENSSL_free(s->session->psk_identity_hint);
  1268. s->session->psk_identity_hint = NULL;
  1269. } else if (!PACKET_strndup(&psk_identity_hint,
  1270. &s->session->psk_identity_hint)) {
  1271. *al = SSL_AD_INTERNAL_ERROR;
  1272. return 0;
  1273. }
  1274. return 1;
  1275. #else
  1276. SSLerr(SSL_F_TLS_PROCESS_SKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
  1277. *al = SSL_AD_INTERNAL_ERROR;
  1278. return 0;
  1279. #endif
  1280. }
  1281. static int tls_process_ske_srp(SSL *s, PACKET *pkt, EVP_PKEY **pkey, int *al)
  1282. {
  1283. #ifndef OPENSSL_NO_SRP
  1284. PACKET prime, generator, salt, server_pub;
  1285. if (!PACKET_get_length_prefixed_2(pkt, &prime)
  1286. || !PACKET_get_length_prefixed_2(pkt, &generator)
  1287. || !PACKET_get_length_prefixed_1(pkt, &salt)
  1288. || !PACKET_get_length_prefixed_2(pkt, &server_pub)) {
  1289. *al = SSL_AD_DECODE_ERROR;
  1290. SSLerr(SSL_F_TLS_PROCESS_SKE_SRP, SSL_R_LENGTH_MISMATCH);
  1291. return 0;
  1292. }
  1293. if ((s->srp_ctx.N =
  1294. BN_bin2bn(PACKET_data(&prime),
  1295. PACKET_remaining(&prime), NULL)) == NULL
  1296. || (s->srp_ctx.g =
  1297. BN_bin2bn(PACKET_data(&generator),
  1298. PACKET_remaining(&generator), NULL)) == NULL
  1299. || (s->srp_ctx.s =
  1300. BN_bin2bn(PACKET_data(&salt),
  1301. PACKET_remaining(&salt), NULL)) == NULL
  1302. || (s->srp_ctx.B =
  1303. BN_bin2bn(PACKET_data(&server_pub),
  1304. PACKET_remaining(&server_pub), NULL)) == NULL) {
  1305. *al = SSL_AD_INTERNAL_ERROR;
  1306. SSLerr(SSL_F_TLS_PROCESS_SKE_SRP, ERR_R_BN_LIB);
  1307. return 0;
  1308. }
  1309. if (!srp_verify_server_param(s, al)) {
  1310. *al = SSL_AD_DECODE_ERROR;
  1311. SSLerr(SSL_F_TLS_PROCESS_SKE_SRP, SSL_R_BAD_SRP_PARAMETERS);
  1312. return 0;
  1313. }
  1314. /* We must check if there is a certificate */
  1315. if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aRSA | SSL_aDSS))
  1316. *pkey = X509_get0_pubkey(s->session->peer);
  1317. return 1;
  1318. #else
  1319. SSLerr(SSL_F_TLS_PROCESS_SKE_SRP, ERR_R_INTERNAL_ERROR);
  1320. *al = SSL_AD_INTERNAL_ERROR;
  1321. return 0;
  1322. #endif
  1323. }
  1324. static int tls_process_ske_dhe(SSL *s, PACKET *pkt, EVP_PKEY **pkey, int *al)
  1325. {
  1326. #ifndef OPENSSL_NO_DH
  1327. PACKET prime, generator, pub_key;
  1328. EVP_PKEY *peer_tmp = NULL;
  1329. DH *dh = NULL;
  1330. BIGNUM *p = NULL, *g = NULL, *bnpub_key = NULL;
  1331. int check_bits = 0;
  1332. if (!PACKET_get_length_prefixed_2(pkt, &prime)
  1333. || !PACKET_get_length_prefixed_2(pkt, &generator)
  1334. || !PACKET_get_length_prefixed_2(pkt, &pub_key)) {
  1335. *al = SSL_AD_DECODE_ERROR;
  1336. SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, SSL_R_LENGTH_MISMATCH);
  1337. return 0;
  1338. }
  1339. peer_tmp = EVP_PKEY_new();
  1340. dh = DH_new();
  1341. if (peer_tmp == NULL || dh == NULL) {
  1342. *al = SSL_AD_INTERNAL_ERROR;
  1343. SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_MALLOC_FAILURE);
  1344. goto err;
  1345. }
  1346. p = BN_bin2bn(PACKET_data(&prime), PACKET_remaining(&prime), NULL);
  1347. g = BN_bin2bn(PACKET_data(&generator), PACKET_remaining(&generator), NULL);
  1348. bnpub_key = BN_bin2bn(PACKET_data(&pub_key), PACKET_remaining(&pub_key),
  1349. NULL);
  1350. if (p == NULL || g == NULL || bnpub_key == NULL) {
  1351. *al = SSL_AD_INTERNAL_ERROR;
  1352. SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_BN_LIB);
  1353. goto err;
  1354. }
  1355. /* test non-zero pupkey */
  1356. if (BN_is_zero(bnpub_key)) {
  1357. *al = SSL_AD_DECODE_ERROR;
  1358. SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, SSL_R_BAD_DH_VALUE);
  1359. goto err;
  1360. }
  1361. if (!DH_set0_pqg(dh, p, NULL, g)) {
  1362. *al = SSL_AD_INTERNAL_ERROR;
  1363. SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_BN_LIB);
  1364. goto err;
  1365. }
  1366. p = g = NULL;
  1367. if (DH_check_params(dh, &check_bits) == 0 || check_bits != 0) {
  1368. *al = SSL_AD_DECODE_ERROR;
  1369. SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, SSL_R_BAD_DH_VALUE);
  1370. goto err;
  1371. }
  1372. if (!DH_set0_key(dh, bnpub_key, NULL)) {
  1373. *al = SSL_AD_INTERNAL_ERROR;
  1374. SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_BN_LIB);
  1375. goto err;
  1376. }
  1377. bnpub_key = NULL;
  1378. if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) {
  1379. *al = SSL_AD_HANDSHAKE_FAILURE;
  1380. SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, SSL_R_DH_KEY_TOO_SMALL);
  1381. goto err;
  1382. }
  1383. if (EVP_PKEY_assign_DH(peer_tmp, dh) == 0) {
  1384. *al = SSL_AD_INTERNAL_ERROR;
  1385. SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_EVP_LIB);
  1386. goto err;
  1387. }
  1388. s->s3->peer_tmp = peer_tmp;
  1389. /*
  1390. * FIXME: This makes assumptions about which ciphersuites come with
  1391. * public keys. We should have a less ad-hoc way of doing this
  1392. */
  1393. if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aRSA | SSL_aDSS))
  1394. *pkey = X509_get0_pubkey(s->session->peer);
  1395. /* else anonymous DH, so no certificate or pkey. */
  1396. return 1;
  1397. err:
  1398. BN_free(p);
  1399. BN_free(g);
  1400. BN_free(bnpub_key);
  1401. DH_free(dh);
  1402. EVP_PKEY_free(peer_tmp);
  1403. return 0;
  1404. #else
  1405. SSLerr(SSL_F_TLS_PROCESS_SKE_DHE, ERR_R_INTERNAL_ERROR);
  1406. *al = SSL_AD_INTERNAL_ERROR;
  1407. return 0;
  1408. #endif
  1409. }
  1410. static int tls_process_ske_ecdhe(SSL *s, PACKET *pkt, EVP_PKEY **pkey, int *al)
  1411. {
  1412. #ifndef OPENSSL_NO_EC
  1413. PACKET encoded_pt;
  1414. const unsigned char *ecparams;
  1415. int curve_nid;
  1416. unsigned int curve_flags;
  1417. EVP_PKEY_CTX *pctx = NULL;
  1418. /*
  1419. * Extract elliptic curve parameters and the server's ephemeral ECDH
  1420. * public key. For now we only support named (not generic) curves and
  1421. * ECParameters in this case is just three bytes.
  1422. */
  1423. if (!PACKET_get_bytes(pkt, &ecparams, 3)) {
  1424. *al = SSL_AD_DECODE_ERROR;
  1425. SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, SSL_R_LENGTH_TOO_SHORT);
  1426. return 0;
  1427. }
  1428. /*
  1429. * Check curve is one of our preferences, if not server has sent an
  1430. * invalid curve. ECParameters is 3 bytes.
  1431. */
  1432. if (!tls1_check_curve(s, ecparams, 3)) {
  1433. *al = SSL_AD_DECODE_ERROR;
  1434. SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, SSL_R_WRONG_CURVE);
  1435. return 0;
  1436. }
  1437. curve_nid = tls1_ec_curve_id2nid(*(ecparams + 2), &curve_flags);
  1438. if (curve_nid == 0) {
  1439. *al = SSL_AD_INTERNAL_ERROR;
  1440. SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE,
  1441. SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
  1442. return 0;
  1443. }
  1444. if ((curve_flags & TLS_CURVE_TYPE) == TLS_CURVE_CUSTOM) {
  1445. EVP_PKEY *key = EVP_PKEY_new();
  1446. if (key == NULL || !EVP_PKEY_set_type(key, curve_nid)) {
  1447. *al = SSL_AD_INTERNAL_ERROR;
  1448. SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, ERR_R_EVP_LIB);
  1449. EVP_PKEY_free(key);
  1450. return 0;
  1451. }
  1452. s->s3->peer_tmp = key;
  1453. } else {
  1454. /* Set up EVP_PKEY with named curve as parameters */
  1455. pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL);
  1456. if (pctx == NULL
  1457. || EVP_PKEY_paramgen_init(pctx) <= 0
  1458. || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, curve_nid) <= 0
  1459. || EVP_PKEY_paramgen(pctx, &s->s3->peer_tmp) <= 0) {
  1460. *al = SSL_AD_INTERNAL_ERROR;
  1461. SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, ERR_R_EVP_LIB);
  1462. EVP_PKEY_CTX_free(pctx);
  1463. return 0;
  1464. }
  1465. EVP_PKEY_CTX_free(pctx);
  1466. pctx = NULL;
  1467. }
  1468. if (!PACKET_get_length_prefixed_1(pkt, &encoded_pt)) {
  1469. *al = SSL_AD_DECODE_ERROR;
  1470. SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, SSL_R_LENGTH_MISMATCH);
  1471. return 0;
  1472. }
  1473. /* parse remote ephem point */
  1474. if (!EVP_PKEY_set1_tls_encodedpoint(s->s3->peer_tmp,
  1475. PACKET_data(&encoded_pt),
  1476. PACKET_remaining(&encoded_pt))) {
  1477. *al = SSL_AD_DECODE_ERROR;
  1478. SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, SSL_R_BAD_ECPOINT);
  1479. return 0;
  1480. }
  1481. /*
  1482. * The ECC/TLS specification does not mention the use of DSA to sign
  1483. * ECParameters in the server key exchange message. We do support RSA
  1484. * and ECDSA.
  1485. */
  1486. if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aECDSA)
  1487. *pkey = X509_get0_pubkey(s->session->peer);
  1488. #ifndef OPENSSL_NO_SM2
  1489. else if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aSM2)
  1490. *pkey = X509_get0_pubkey(s->session->peer);
  1491. #endif
  1492. else if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aRSA)
  1493. *pkey = X509_get0_pubkey(s->session->peer);
  1494. /* else anonymous ECDH, so no certificate or pkey. */
  1495. return 1;
  1496. #else
  1497. SSLerr(SSL_F_TLS_PROCESS_SKE_ECDHE, ERR_R_INTERNAL_ERROR);
  1498. *al = SSL_AD_INTERNAL_ERROR;
  1499. return 0;
  1500. #endif
  1501. }
  1502. MSG_PROCESS_RETURN tls_process_server_key_exchange(SSL *s, PACKET *pkt)
  1503. {
  1504. int al = -1;
  1505. long alg_k;
  1506. EVP_PKEY *pkey = NULL;
  1507. PACKET save_param_start, signature;
  1508. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  1509. save_param_start = *pkt;
  1510. #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
  1511. EVP_PKEY_free(s->s3->peer_tmp);
  1512. s->s3->peer_tmp = NULL;
  1513. #endif
  1514. if (alg_k & SSL_PSK) {
  1515. if (!tls_process_ske_psk_preamble(s, pkt, &al))
  1516. goto err;
  1517. }
  1518. /* Nothing else to do for plain PSK or RSAPSK */
  1519. if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) {
  1520. } else if (alg_k & SSL_kSRP) {
  1521. if (!tls_process_ske_srp(s, pkt, &pkey, &al))
  1522. goto err;
  1523. } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
  1524. if (!tls_process_ske_dhe(s, pkt, &pkey, &al))
  1525. goto err;
  1526. } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK | SSL_kSM2DHE | SSL_kSM2PSK)) {
  1527. if (!tls_process_ske_ecdhe(s, pkt, &pkey, &al))
  1528. goto err;
  1529. } else if (alg_k) {
  1530. al = SSL_AD_UNEXPECTED_MESSAGE;
  1531. SSLerr(SSL_F_TLS_PROCESS_SERVER_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
  1532. goto err;
  1533. }
  1534. /* if it was signed, check the signature */
  1535. if (pkey != NULL) {
  1536. PACKET params;
  1537. int maxsig;
  1538. const EVP_MD *md = NULL;
  1539. EVP_MD_CTX *md_ctx;
  1540. /*
  1541. * |pkt| now points to the beginning of the signature, so the difference
  1542. * equals the length of the parameters.
  1543. */
  1544. if (!PACKET_get_sub_packet(&save_param_start, &params,
  1545. PACKET_remaining(&save_param_start) -
  1546. PACKET_remaining(pkt))) {
  1547. al = SSL_AD_INTERNAL_ERROR;
  1548. SSLerr(SSL_F_TLS_PROCESS_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  1549. goto err;
  1550. }
  1551. if (SSL_USE_SIGALGS(s)) {
  1552. const unsigned char *sigalgs;
  1553. int rv;
  1554. if (!PACKET_get_bytes(pkt, &sigalgs, 2)) {
  1555. al = SSL_AD_DECODE_ERROR;
  1556. SSLerr(SSL_F_TLS_PROCESS_SERVER_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
  1557. goto err;
  1558. }
  1559. rv = tls12_check_peer_sigalg(&md, s, sigalgs, pkey);
  1560. if (rv == -1) {
  1561. al = SSL_AD_INTERNAL_ERROR;
  1562. goto err;
  1563. } else if (rv == 0) {
  1564. al = SSL_AD_DECODE_ERROR;
  1565. goto err;
  1566. }
  1567. #ifdef SSL_DEBUG
  1568. fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
  1569. #endif
  1570. #ifndef OPENSSL_NO_RSA
  1571. } else if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA) {
  1572. # if !defined(OPENSSL_NO_MD5) && !defined(OPENSSL_NO_SHA)
  1573. md = EVP_md5_sha1();
  1574. # elif !defined(OPENSSL_NO_SM3)
  1575. md = EVP_sm3();
  1576. # else
  1577. should_not_happen!!
  1578. # endif
  1579. #endif
  1580. #ifndef OPENSSL_NO_GMTLS
  1581. } else if (s->method->version == GMTLS_VERSION
  1582. && s->s3->tmp.new_cipher->algorithm_mac & SSL_SM3) {
  1583. md = EVP_sm3();
  1584. #endif
  1585. #ifndef OPENSSL_NO_SHA
  1586. } else {
  1587. md = EVP_sha1();
  1588. #endif
  1589. }
  1590. if (!PACKET_get_length_prefixed_2(pkt, &signature)
  1591. || PACKET_remaining(pkt) != 0) {
  1592. al = SSL_AD_DECODE_ERROR;
  1593. SSLerr(SSL_F_TLS_PROCESS_SERVER_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
  1594. goto err;
  1595. }
  1596. maxsig = EVP_PKEY_size(pkey);
  1597. if (maxsig < 0) {
  1598. al = SSL_AD_INTERNAL_ERROR;
  1599. SSLerr(SSL_F_TLS_PROCESS_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  1600. goto err;
  1601. }
  1602. /*
  1603. * Check signature length
  1604. */
  1605. if (PACKET_remaining(&signature) > (size_t)maxsig) {
  1606. /* wrong packet length */
  1607. al = SSL_AD_DECODE_ERROR;
  1608. SSLerr(SSL_F_TLS_PROCESS_SERVER_KEY_EXCHANGE,
  1609. SSL_R_WRONG_SIGNATURE_LENGTH);
  1610. goto err;
  1611. }
  1612. md_ctx = EVP_MD_CTX_new();
  1613. if (md_ctx == NULL) {
  1614. al = SSL_AD_INTERNAL_ERROR;
  1615. SSLerr(SSL_F_TLS_PROCESS_SERVER_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
  1616. goto err;
  1617. }
  1618. if (EVP_VerifyInit_ex(md_ctx, md, NULL) <= 0) {
  1619. EVP_MD_CTX_free(md_ctx);
  1620. al = SSL_AD_INTERNAL_ERROR;
  1621. SSLerr(SSL_F_TLS_PROCESS_SERVER_KEY_EXCHANGE, ERR_R_EVP_LIB);
  1622. goto err;
  1623. }
  1624. #ifndef OPENSSL_NO_SM2
  1625. if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aSM2) {
  1626. unsigned char z[EVP_MAX_MD_SIZE];
  1627. size_t zlen = sizeof(z);
  1628. char *id = SM2_DEFAULT_ID;
  1629. if (!SM2_compute_id_digest(md, id, strlen(id), z, &zlen,
  1630. EVP_PKEY_get0_EC_KEY(pkey))) {
  1631. al = SSL_AD_INTERNAL_ERROR;
  1632. SSLerr(SSL_F_TLS_PROCESS_SERVER_KEY_EXCHANGE, ERR_R_SM2_LIB);
  1633. goto err;
  1634. }
  1635. if (EVP_VerifyUpdate(md_ctx, z, zlen) <= 0) {
  1636. al = SSL_AD_INTERNAL_ERROR;
  1637. SSLerr(SSL_F_TLS_PROCESS_SERVER_KEY_EXCHANGE, ERR_R_EVP_LIB);
  1638. goto err;
  1639. }
  1640. }
  1641. #endif
  1642. if (EVP_VerifyUpdate(md_ctx, &(s->s3->client_random[0]),
  1643. SSL3_RANDOM_SIZE) <= 0
  1644. || EVP_VerifyUpdate(md_ctx, &(s->s3->server_random[0]),
  1645. SSL3_RANDOM_SIZE) <= 0
  1646. || EVP_VerifyUpdate(md_ctx, PACKET_data(&params),
  1647. PACKET_remaining(&params)) <= 0) {
  1648. EVP_MD_CTX_free(md_ctx);
  1649. al = SSL_AD_INTERNAL_ERROR;
  1650. SSLerr(SSL_F_TLS_PROCESS_SERVER_KEY_EXCHANGE, ERR_R_EVP_LIB);
  1651. goto err;
  1652. }
  1653. if (EVP_VerifyFinal(md_ctx, PACKET_data(&signature),
  1654. PACKET_remaining(&signature), pkey) <= 0) {
  1655. /* bad signature */
  1656. EVP_MD_CTX_free(md_ctx);
  1657. al = SSL_AD_DECRYPT_ERROR;
  1658. SSLerr(SSL_F_TLS_PROCESS_SERVER_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
  1659. goto err;
  1660. }
  1661. EVP_MD_CTX_free(md_ctx);
  1662. } else {
  1663. /* aNULL, aSRP or PSK do not need public keys */
  1664. if (!(s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
  1665. && !(alg_k & SSL_PSK)) {
  1666. /* Might be wrong key type, check it */
  1667. if (ssl3_check_cert_and_algorithm(s)) {
  1668. /* Otherwise this shouldn't happen */
  1669. al = SSL_AD_INTERNAL_ERROR;
  1670. SSLerr(SSL_F_TLS_PROCESS_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  1671. } else {
  1672. al = SSL_AD_DECODE_ERROR;
  1673. }
  1674. goto err;
  1675. }
  1676. /* still data left over */
  1677. if (PACKET_remaining(pkt) != 0) {
  1678. al = SSL_AD_DECODE_ERROR;
  1679. SSLerr(SSL_F_TLS_PROCESS_SERVER_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
  1680. goto err;
  1681. }
  1682. }
  1683. return MSG_PROCESS_CONTINUE_READING;
  1684. err:
  1685. if (al != -1)
  1686. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  1687. ossl_statem_set_error(s);
  1688. return MSG_PROCESS_ERROR;
  1689. }
  1690. MSG_PROCESS_RETURN tls_process_certificate_request(SSL *s, PACKET *pkt)
  1691. {
  1692. int ret = MSG_PROCESS_ERROR;
  1693. unsigned int list_len, ctype_num, i, name_len;
  1694. X509_NAME *xn = NULL;
  1695. const unsigned char *data;
  1696. const unsigned char *namestart, *namebytes;
  1697. STACK_OF(X509_NAME) *ca_sk = NULL;
  1698. if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
  1699. SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
  1700. goto err;
  1701. }
  1702. /* get the certificate types */
  1703. if (!PACKET_get_1(pkt, &ctype_num)
  1704. || !PACKET_get_bytes(pkt, &data, ctype_num)) {
  1705. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1706. SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
  1707. goto err;
  1708. }
  1709. OPENSSL_free(s->cert->ctypes);
  1710. s->cert->ctypes = NULL;
  1711. if (ctype_num > SSL3_CT_NUMBER) {
  1712. /* If we exceed static buffer copy all to cert structure */
  1713. s->cert->ctypes = OPENSSL_malloc(ctype_num);
  1714. if (s->cert->ctypes == NULL) {
  1715. SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
  1716. goto err;
  1717. }
  1718. memcpy(s->cert->ctypes, data, ctype_num);
  1719. s->cert->ctype_num = (size_t)ctype_num;
  1720. ctype_num = SSL3_CT_NUMBER;
  1721. }
  1722. for (i = 0; i < ctype_num; i++)
  1723. s->s3->tmp.ctype[i] = data[i];
  1724. if (SSL_USE_SIGALGS(s)) {
  1725. if (!PACKET_get_net_2(pkt, &list_len)
  1726. || !PACKET_get_bytes(pkt, &data, list_len)) {
  1727. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1728. SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
  1729. SSL_R_LENGTH_MISMATCH);
  1730. goto err;
  1731. }
  1732. /* Clear certificate digests and validity flags */
  1733. for (i = 0; i < SSL_PKEY_NUM; i++) {
  1734. s->s3->tmp.md[i] = NULL;
  1735. s->s3->tmp.valid_flags[i] = 0;
  1736. }
  1737. if ((list_len & 1) || !tls1_save_sigalgs(s, data, list_len)) {
  1738. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1739. SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
  1740. SSL_R_SIGNATURE_ALGORITHMS_ERROR);
  1741. goto err;
  1742. }
  1743. if (!tls1_process_sigalgs(s)) {
  1744. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1745. SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
  1746. goto err;
  1747. }
  1748. } else {
  1749. ssl_set_default_md(s);
  1750. }
  1751. /* get the CA RDNs */
  1752. if (!PACKET_get_net_2(pkt, &list_len)
  1753. || PACKET_remaining(pkt) != list_len) {
  1754. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1755. SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
  1756. goto err;
  1757. }
  1758. while (PACKET_remaining(pkt)) {
  1759. if (!PACKET_get_net_2(pkt, &name_len)
  1760. || !PACKET_get_bytes(pkt, &namebytes, name_len)) {
  1761. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1762. SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
  1763. SSL_R_LENGTH_MISMATCH);
  1764. goto err;
  1765. }
  1766. namestart = namebytes;
  1767. if ((xn = d2i_X509_NAME(NULL, (const unsigned char **)&namebytes,
  1768. name_len)) == NULL) {
  1769. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1770. SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
  1771. goto err;
  1772. }
  1773. if (namebytes != (namestart + name_len)) {
  1774. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1775. SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
  1776. SSL_R_CA_DN_LENGTH_MISMATCH);
  1777. goto err;
  1778. }
  1779. if (!sk_X509_NAME_push(ca_sk, xn)) {
  1780. SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
  1781. goto err;
  1782. }
  1783. xn = NULL;
  1784. }
  1785. /* we should setup a certificate to return.... */
  1786. s->s3->tmp.cert_req = 1;
  1787. s->s3->tmp.ctype_num = ctype_num;
  1788. sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
  1789. s->s3->tmp.ca_names = ca_sk;
  1790. ca_sk = NULL;
  1791. ret = MSG_PROCESS_CONTINUE_PROCESSING;
  1792. goto done;
  1793. err:
  1794. ossl_statem_set_error(s);
  1795. done:
  1796. X509_NAME_free(xn);
  1797. sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
  1798. return ret;
  1799. }
  1800. static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
  1801. {
  1802. return (X509_NAME_cmp(*a, *b));
  1803. }
  1804. MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL *s, PACKET *pkt)
  1805. {
  1806. int al;
  1807. unsigned int ticklen;
  1808. unsigned long ticket_lifetime_hint;
  1809. if (!PACKET_get_net_4(pkt, &ticket_lifetime_hint)
  1810. || !PACKET_get_net_2(pkt, &ticklen)
  1811. || PACKET_remaining(pkt) != ticklen) {
  1812. al = SSL_AD_DECODE_ERROR;
  1813. SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
  1814. goto f_err;
  1815. }
  1816. /* Server is allowed to change its mind and send an empty ticket. */
  1817. if (ticklen == 0)
  1818. return MSG_PROCESS_CONTINUE_READING;
  1819. if (s->session->session_id_length > 0) {
  1820. int i = s->session_ctx->session_cache_mode;
  1821. SSL_SESSION *new_sess;
  1822. /*
  1823. * We reused an existing session, so we need to replace it with a new
  1824. * one
  1825. */
  1826. if (i & SSL_SESS_CACHE_CLIENT) {
  1827. /*
  1828. * Remove the old session from the cache. We carry on if this fails
  1829. */
  1830. SSL_CTX_remove_session(s->session_ctx, s->session);
  1831. }
  1832. if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
  1833. al = SSL_AD_INTERNAL_ERROR;
  1834. SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
  1835. goto f_err;
  1836. }
  1837. SSL_SESSION_free(s->session);
  1838. s->session = new_sess;
  1839. }
  1840. OPENSSL_free(s->session->tlsext_tick);
  1841. s->session->tlsext_ticklen = 0;
  1842. s->session->tlsext_tick = OPENSSL_malloc(ticklen);
  1843. if (s->session->tlsext_tick == NULL) {
  1844. SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
  1845. goto err;
  1846. }
  1847. if (!PACKET_copy_bytes(pkt, s->session->tlsext_tick, ticklen)) {
  1848. al = SSL_AD_DECODE_ERROR;
  1849. SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
  1850. goto f_err;
  1851. }
  1852. s->session->tlsext_tick_lifetime_hint = ticket_lifetime_hint;
  1853. s->session->tlsext_ticklen = ticklen;
  1854. /*
  1855. * There are two ways to detect a resumed ticket session. One is to set
  1856. * an appropriate session ID and then the server must return a match in
  1857. * ServerHello. This allows the normal client session ID matching to work
  1858. * and we know much earlier that the ticket has been accepted. The
  1859. * other way is to set zero length session ID when the ticket is
  1860. * presented and rely on the handshake to determine session resumption.
  1861. * We choose the former approach because this fits in with assumptions
  1862. * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
  1863. * SHA256 is disabled) hash of the ticket.
  1864. */
  1865. if (!EVP_Digest(s->session->tlsext_tick, ticklen,
  1866. s->session->session_id, &s->session->session_id_length,
  1867. EVP_get_digestbynid(NID_sha256), NULL)) {
  1868. SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_EVP_LIB);
  1869. goto err;
  1870. }
  1871. return MSG_PROCESS_CONTINUE_READING;
  1872. f_err:
  1873. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  1874. err:
  1875. ossl_statem_set_error(s);
  1876. return MSG_PROCESS_ERROR;
  1877. }
  1878. MSG_PROCESS_RETURN tls_process_cert_status(SSL *s, PACKET *pkt)
  1879. {
  1880. int al;
  1881. unsigned long resplen;
  1882. unsigned int type;
  1883. if (!PACKET_get_1(pkt, &type)
  1884. || type != TLSEXT_STATUSTYPE_ocsp) {
  1885. al = SSL_AD_DECODE_ERROR;
  1886. SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
  1887. goto f_err;
  1888. }
  1889. if (!PACKET_get_net_3(pkt, &resplen)
  1890. || PACKET_remaining(pkt) != resplen) {
  1891. al = SSL_AD_DECODE_ERROR;
  1892. SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
  1893. goto f_err;
  1894. }
  1895. s->tlsext_ocsp_resp = OPENSSL_malloc(resplen);
  1896. if (s->tlsext_ocsp_resp == NULL) {
  1897. al = SSL_AD_INTERNAL_ERROR;
  1898. SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, ERR_R_MALLOC_FAILURE);
  1899. goto f_err;
  1900. }
  1901. if (!PACKET_copy_bytes(pkt, s->tlsext_ocsp_resp, resplen)) {
  1902. al = SSL_AD_DECODE_ERROR;
  1903. SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
  1904. goto f_err;
  1905. }
  1906. s->tlsext_ocsp_resplen = resplen;
  1907. return MSG_PROCESS_CONTINUE_READING;
  1908. f_err:
  1909. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  1910. ossl_statem_set_error(s);
  1911. return MSG_PROCESS_ERROR;
  1912. }
  1913. MSG_PROCESS_RETURN tls_process_server_done(SSL *s, PACKET *pkt)
  1914. {
  1915. if (PACKET_remaining(pkt) > 0) {
  1916. /* should contain no data */
  1917. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1918. SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
  1919. ossl_statem_set_error(s);
  1920. return MSG_PROCESS_ERROR;
  1921. }
  1922. #ifndef OPENSSL_NO_SRP
  1923. if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
  1924. if (SRP_Calc_A_param(s) <= 0) {
  1925. SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_SRP_A_CALC);
  1926. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1927. ossl_statem_set_error(s);
  1928. return MSG_PROCESS_ERROR;
  1929. }
  1930. }
  1931. #endif
  1932. /*
  1933. * at this point we check that we have the required stuff from
  1934. * the server
  1935. */
  1936. if (!ssl3_check_cert_and_algorithm(s)) {
  1937. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1938. ossl_statem_set_error(s);
  1939. return MSG_PROCESS_ERROR;
  1940. }
  1941. /*
  1942. * Call the ocsp status callback if needed. The |tlsext_ocsp_resp| and
  1943. * |tlsext_ocsp_resplen| values will be set if we actually received a status
  1944. * message, or NULL and -1 otherwise
  1945. */
  1946. if (s->tlsext_status_type != -1 && s->ctx->tlsext_status_cb != NULL) {
  1947. int ret;
  1948. ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
  1949. if (ret == 0) {
  1950. ssl3_send_alert(s, SSL3_AL_FATAL,
  1951. SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE);
  1952. SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE,
  1953. SSL_R_INVALID_STATUS_RESPONSE);
  1954. return MSG_PROCESS_ERROR;
  1955. }
  1956. if (ret < 0) {
  1957. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1958. SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, ERR_R_MALLOC_FAILURE);
  1959. return MSG_PROCESS_ERROR;
  1960. }
  1961. }
  1962. #ifndef OPENSSL_NO_CT
  1963. if (s->ct_validation_callback != NULL) {
  1964. /* Note we validate the SCTs whether or not we abort on error */
  1965. if (!ssl_validate_ct(s) && (s->verify_mode & SSL_VERIFY_PEER)) {
  1966. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1967. return MSG_PROCESS_ERROR;
  1968. }
  1969. }
  1970. #endif
  1971. #ifndef OPENSSL_NO_SCTP
  1972. /* Only applies to renegotiation */
  1973. if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))
  1974. && s->renegotiate != 0)
  1975. return MSG_PROCESS_CONTINUE_PROCESSING;
  1976. else
  1977. #endif
  1978. return MSG_PROCESS_FINISHED_READING;
  1979. }
  1980. static int tls_construct_cke_psk_preamble(SSL *s, unsigned char **p,
  1981. size_t *pskhdrlen, int *al)
  1982. {
  1983. #ifndef OPENSSL_NO_PSK
  1984. int ret = 0;
  1985. /*
  1986. * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
  1987. * \0-terminated identity. The last byte is for us for simulating
  1988. * strnlen.
  1989. */
  1990. char identity[PSK_MAX_IDENTITY_LEN + 1];
  1991. size_t identitylen = 0;
  1992. unsigned char psk[PSK_MAX_PSK_LEN];
  1993. unsigned char *tmppsk = NULL;
  1994. char *tmpidentity = NULL;
  1995. size_t psklen = 0;
  1996. if (s->psk_client_callback == NULL) {
  1997. SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, SSL_R_PSK_NO_CLIENT_CB);
  1998. *al = SSL_AD_INTERNAL_ERROR;
  1999. goto err;
  2000. }
  2001. memset(identity, 0, sizeof(identity));
  2002. psklen = s->psk_client_callback(s, s->session->psk_identity_hint,
  2003. identity, sizeof(identity) - 1,
  2004. psk, sizeof(psk));
  2005. if (psklen > PSK_MAX_PSK_LEN) {
  2006. SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
  2007. *al = SSL_AD_HANDSHAKE_FAILURE;
  2008. goto err;
  2009. } else if (psklen == 0) {
  2010. SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE,
  2011. SSL_R_PSK_IDENTITY_NOT_FOUND);
  2012. *al = SSL_AD_HANDSHAKE_FAILURE;
  2013. goto err;
  2014. }
  2015. identitylen = strlen(identity);
  2016. if (identitylen > PSK_MAX_IDENTITY_LEN) {
  2017. SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
  2018. *al = SSL_AD_HANDSHAKE_FAILURE;
  2019. goto err;
  2020. }
  2021. tmppsk = OPENSSL_memdup(psk, psklen);
  2022. tmpidentity = OPENSSL_strdup(identity);
  2023. if (tmppsk == NULL || tmpidentity == NULL) {
  2024. SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_MALLOC_FAILURE);
  2025. *al = SSL_AD_INTERNAL_ERROR;
  2026. goto err;
  2027. }
  2028. OPENSSL_free(s->s3->tmp.psk);
  2029. s->s3->tmp.psk = tmppsk;
  2030. s->s3->tmp.psklen = psklen;
  2031. tmppsk = NULL;
  2032. OPENSSL_free(s->session->psk_identity);
  2033. s->session->psk_identity = tmpidentity;
  2034. tmpidentity = NULL;
  2035. s2n(identitylen, *p);
  2036. memcpy(*p, identity, identitylen);
  2037. *pskhdrlen = 2 + identitylen;
  2038. *p += identitylen;
  2039. ret = 1;
  2040. err:
  2041. OPENSSL_cleanse(psk, psklen);
  2042. OPENSSL_cleanse(identity, sizeof(identity));
  2043. OPENSSL_clear_free(tmppsk, psklen);
  2044. OPENSSL_clear_free(tmpidentity, identitylen);
  2045. return ret;
  2046. #else
  2047. SSLerr(SSL_F_TLS_CONSTRUCT_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
  2048. *al = SSL_AD_INTERNAL_ERROR;
  2049. return 0;
  2050. #endif
  2051. }
  2052. static int tls_construct_cke_rsa(SSL *s, unsigned char **p, int *len, int *al)
  2053. {
  2054. #ifndef OPENSSL_NO_RSA
  2055. unsigned char *q;
  2056. EVP_PKEY *pkey = NULL;
  2057. EVP_PKEY_CTX *pctx = NULL;
  2058. size_t enclen;
  2059. unsigned char *pms = NULL;
  2060. size_t pmslen = 0;
  2061. if (s->session->peer == NULL) {
  2062. /*
  2063. * We should always have a server certificate with SSL_kRSA.
  2064. */
  2065. SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);
  2066. return 0;
  2067. }
  2068. pkey = X509_get0_pubkey(s->session->peer);
  2069. if (EVP_PKEY_get0_RSA(pkey) == NULL) {
  2070. SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);
  2071. return 0;
  2072. }
  2073. pmslen = SSL_MAX_MASTER_KEY_LENGTH;
  2074. pms = OPENSSL_malloc(pmslen);
  2075. if (pms == NULL) {
  2076. SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_MALLOC_FAILURE);
  2077. *al = SSL_AD_INTERNAL_ERROR;
  2078. return 0;
  2079. }
  2080. pms[0] = s->client_version >> 8;
  2081. pms[1] = s->client_version & 0xff;
  2082. if (RAND_bytes(pms + 2, pmslen - 2) <= 0) {
  2083. goto err;
  2084. }
  2085. q = *p;
  2086. /* Fix buf for TLS, GMTLS and beyond */
  2087. if (s->version > SSL3_VERSION || SSL_IS_GMTLS(s))
  2088. *p += 2;
  2089. pctx = EVP_PKEY_CTX_new(pkey, NULL);
  2090. if (pctx == NULL || EVP_PKEY_encrypt_init(pctx) <= 0
  2091. || EVP_PKEY_encrypt(pctx, NULL, &enclen, pms, pmslen) <= 0) {
  2092. SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_EVP_LIB);
  2093. goto err;
  2094. }
  2095. if (EVP_PKEY_encrypt(pctx, *p, &enclen, pms, pmslen) <= 0) {
  2096. SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, SSL_R_BAD_RSA_ENCRYPT);
  2097. goto err;
  2098. }
  2099. *len = enclen;
  2100. EVP_PKEY_CTX_free(pctx);
  2101. pctx = NULL;
  2102. # ifdef PKCS1_CHECK
  2103. if (s->options & SSL_OP_PKCS1_CHECK_1)
  2104. (*p)[1]++;
  2105. if (s->options & SSL_OP_PKCS1_CHECK_2)
  2106. tmp_buf[0] = 0x70;
  2107. # endif
  2108. /* Fix buf for TLS and beyond */
  2109. if (s->version > SSL3_VERSION || SSL_IS_GMTLS(s)) {
  2110. s2n(*len, q);
  2111. *len += 2;
  2112. }
  2113. s->s3->tmp.pms = pms;
  2114. s->s3->tmp.pmslen = pmslen;
  2115. return 1;
  2116. err:
  2117. OPENSSL_clear_free(pms, pmslen);
  2118. EVP_PKEY_CTX_free(pctx);
  2119. return 0;
  2120. #else
  2121. SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);
  2122. *al = SSL_AD_INTERNAL_ERROR;
  2123. return 0;
  2124. #endif
  2125. }
  2126. static int tls_construct_cke_dhe(SSL *s, unsigned char **p, int *len, int *al)
  2127. {
  2128. #ifndef OPENSSL_NO_DH
  2129. DH *dh_clnt = NULL;
  2130. const BIGNUM *pub_key;
  2131. EVP_PKEY *ckey = NULL, *skey = NULL;
  2132. skey = s->s3->peer_tmp;
  2133. if (skey == NULL) {
  2134. SSLerr(SSL_F_TLS_CONSTRUCT_CKE_DHE, ERR_R_INTERNAL_ERROR);
  2135. return 0;
  2136. }
  2137. ckey = ssl_generate_pkey(skey);
  2138. if (ckey == NULL) {
  2139. SSLerr(SSL_F_TLS_CONSTRUCT_CKE_DHE, ERR_R_INTERNAL_ERROR);
  2140. return 0;
  2141. }
  2142. dh_clnt = EVP_PKEY_get0_DH(ckey);
  2143. if (dh_clnt == NULL || ssl_derive(s, ckey, skey) == 0) {
  2144. SSLerr(SSL_F_TLS_CONSTRUCT_CKE_DHE, ERR_R_INTERNAL_ERROR);
  2145. EVP_PKEY_free(ckey);
  2146. return 0;
  2147. }
  2148. /* send off the data */
  2149. DH_get0_key(dh_clnt, &pub_key, NULL);
  2150. *len = BN_num_bytes(pub_key);
  2151. s2n(*len, *p);
  2152. BN_bn2bin(pub_key, *p);
  2153. *len += 2;
  2154. EVP_PKEY_free(ckey);
  2155. return 1;
  2156. #else
  2157. SSLerr(SSL_F_TLS_CONSTRUCT_CKE_DHE, ERR_R_INTERNAL_ERROR);
  2158. *al = SSL_AD_INTERNAL_ERROR;
  2159. return 0;
  2160. #endif
  2161. }
  2162. static int tls_construct_cke_ecdhe(SSL *s, unsigned char **p, int *len, int *al)
  2163. {
  2164. #ifndef OPENSSL_NO_EC
  2165. unsigned char *encodedPoint = NULL;
  2166. int encoded_pt_len = 0;
  2167. EVP_PKEY *ckey = NULL, *skey = NULL;
  2168. skey = s->s3->peer_tmp;
  2169. if (skey == NULL) {
  2170. SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_INTERNAL_ERROR);
  2171. return 0;
  2172. }
  2173. ckey = ssl_generate_pkey(skey);
  2174. if (ckey == NULL) {
  2175. SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_INTERNAL_ERROR);
  2176. goto err;
  2177. }
  2178. if (ssl_derive(s, ckey, skey) == 0) {
  2179. SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_EVP_LIB);
  2180. goto err;
  2181. }
  2182. /* Generate encoding of client key */
  2183. encoded_pt_len = EVP_PKEY_get1_tls_encodedpoint(ckey, &encodedPoint);
  2184. if (encoded_pt_len == 0) {
  2185. SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_EC_LIB);
  2186. goto err;
  2187. }
  2188. EVP_PKEY_free(ckey);
  2189. ckey = NULL;
  2190. *len = encoded_pt_len;
  2191. /* length of encoded point */
  2192. **p = *len;
  2193. *p += 1;
  2194. /* copy the point */
  2195. memcpy(*p, encodedPoint, *len);
  2196. /* increment len to account for length field */
  2197. *len += 1;
  2198. OPENSSL_free(encodedPoint);
  2199. return 1;
  2200. err:
  2201. EVP_PKEY_free(ckey);
  2202. return 0;
  2203. #else
  2204. SSLerr(SSL_F_TLS_CONSTRUCT_CKE_ECDHE, ERR_R_INTERNAL_ERROR);
  2205. *al = SSL_AD_INTERNAL_ERROR;
  2206. return 0;
  2207. #endif
  2208. }
  2209. static int tls_construct_cke_gost(SSL *s, unsigned char **p, int *len, int *al)
  2210. {
  2211. #ifndef OPENSSL_NO_GOST
  2212. /* GOST key exchange message creation */
  2213. EVP_PKEY_CTX *pkey_ctx = NULL;
  2214. X509 *peer_cert;
  2215. size_t msglen;
  2216. unsigned int md_len;
  2217. unsigned char shared_ukm[32], tmp[256];
  2218. EVP_MD_CTX *ukm_hash = NULL;
  2219. int dgst_nid = NID_id_GostR3411_94;
  2220. unsigned char *pms = NULL;
  2221. size_t pmslen = 0;
  2222. if ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aGOST12) != 0)
  2223. dgst_nid = NID_id_GostR3411_2012_256;
  2224. /*
  2225. * Get server sertificate PKEY and create ctx from it
  2226. */
  2227. peer_cert = s->session->peer;
  2228. if (!peer_cert) {
  2229. *al = SSL_AD_HANDSHAKE_FAILURE;
  2230. SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST,
  2231. SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
  2232. return 0;
  2233. }
  2234. pkey_ctx = EVP_PKEY_CTX_new(X509_get0_pubkey(peer_cert), NULL);
  2235. if (pkey_ctx == NULL) {
  2236. *al = SSL_AD_INTERNAL_ERROR;
  2237. SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_MALLOC_FAILURE);
  2238. return 0;
  2239. }
  2240. /*
  2241. * If we have send a certificate, and certificate key
  2242. * parameters match those of server certificate, use
  2243. * certificate key for key exchange
  2244. */
  2245. /* Otherwise, generate ephemeral key pair */
  2246. pmslen = 32;
  2247. pms = OPENSSL_malloc(pmslen);
  2248. if (pms == NULL) {
  2249. *al = SSL_AD_INTERNAL_ERROR;
  2250. SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_MALLOC_FAILURE);
  2251. goto err;
  2252. }
  2253. if (EVP_PKEY_encrypt_init(pkey_ctx) <= 0
  2254. /* Generate session key */
  2255. || RAND_bytes(pms, pmslen) <= 0) {
  2256. *al = SSL_AD_INTERNAL_ERROR;
  2257. SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR);
  2258. goto err;
  2259. };
  2260. /*
  2261. * Compute shared IV and store it in algorithm-specific context
  2262. * data
  2263. */
  2264. ukm_hash = EVP_MD_CTX_new();
  2265. if (ukm_hash == NULL
  2266. || EVP_DigestInit(ukm_hash, EVP_get_digestbynid(dgst_nid)) <= 0
  2267. || EVP_DigestUpdate(ukm_hash, s->s3->client_random,
  2268. SSL3_RANDOM_SIZE) <= 0
  2269. || EVP_DigestUpdate(ukm_hash, s->s3->server_random,
  2270. SSL3_RANDOM_SIZE) <= 0
  2271. || EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len) <= 0) {
  2272. *al = SSL_AD_INTERNAL_ERROR;
  2273. SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR);
  2274. goto err;
  2275. }
  2276. EVP_MD_CTX_free(ukm_hash);
  2277. ukm_hash = NULL;
  2278. if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT,
  2279. EVP_PKEY_CTRL_SET_IV, 8, shared_ukm) < 0) {
  2280. *al = SSL_AD_INTERNAL_ERROR;
  2281. SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, SSL_R_LIBRARY_BUG);
  2282. goto err;
  2283. }
  2284. /* Make GOST keytransport blob message */
  2285. /*
  2286. * Encapsulate it into sequence
  2287. */
  2288. *((*p)++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
  2289. msglen = 255;
  2290. if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) <= 0) {
  2291. *al = SSL_AD_INTERNAL_ERROR;
  2292. SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, SSL_R_LIBRARY_BUG);
  2293. goto err;
  2294. }
  2295. if (msglen >= 0x80) {
  2296. *((*p)++) = 0x81;
  2297. *((*p)++) = msglen & 0xff;
  2298. *len = msglen + 3;
  2299. } else {
  2300. *((*p)++) = msglen & 0xff;
  2301. *len = msglen + 2;
  2302. }
  2303. memcpy(*p, tmp, msglen);
  2304. EVP_PKEY_CTX_free(pkey_ctx);
  2305. s->s3->tmp.pms = pms;
  2306. s->s3->tmp.pmslen = pmslen;
  2307. return 1;
  2308. err:
  2309. EVP_PKEY_CTX_free(pkey_ctx);
  2310. OPENSSL_clear_free(pms, pmslen);
  2311. EVP_MD_CTX_free(ukm_hash);
  2312. return 0;
  2313. #else
  2314. SSLerr(SSL_F_TLS_CONSTRUCT_CKE_GOST, ERR_R_INTERNAL_ERROR);
  2315. *al = SSL_AD_INTERNAL_ERROR;
  2316. return 0;
  2317. #endif
  2318. }
  2319. static int tls_construct_cke_srp(SSL *s, unsigned char **p, int *len, int *al)
  2320. {
  2321. #ifndef OPENSSL_NO_SRP
  2322. if (s->srp_ctx.A != NULL) {
  2323. /* send off the data */
  2324. *len = BN_num_bytes(s->srp_ctx.A);
  2325. s2n(*len, *p);
  2326. BN_bn2bin(s->srp_ctx.A, *p);
  2327. *len += 2;
  2328. } else {
  2329. SSLerr(SSL_F_TLS_CONSTRUCT_CKE_SRP, ERR_R_INTERNAL_ERROR);
  2330. return 0;
  2331. }
  2332. OPENSSL_free(s->session->srp_username);
  2333. s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
  2334. if (s->session->srp_username == NULL) {
  2335. SSLerr(SSL_F_TLS_CONSTRUCT_CKE_SRP, ERR_R_MALLOC_FAILURE);
  2336. return 0;
  2337. }
  2338. return 1;
  2339. #else
  2340. SSLerr(SSL_F_TLS_CONSTRUCT_CKE_SRP, ERR_R_INTERNAL_ERROR);
  2341. *al = SSL_AD_INTERNAL_ERROR;
  2342. return 0;
  2343. #endif
  2344. }
  2345. int tls_construct_client_key_exchange(SSL *s)
  2346. {
  2347. unsigned char *p;
  2348. int len;
  2349. size_t pskhdrlen = 0;
  2350. unsigned long alg_k;
  2351. int al = -1;
  2352. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  2353. p = ssl_handshake_start(s);
  2354. if ((alg_k & SSL_PSK)
  2355. && !tls_construct_cke_psk_preamble(s, &p, &pskhdrlen, &al))
  2356. goto err;
  2357. if (alg_k & SSL_kPSK) {
  2358. len = 0;
  2359. } else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
  2360. if (!tls_construct_cke_rsa(s, &p, &len, &al))
  2361. goto err;
  2362. } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
  2363. if (!tls_construct_cke_dhe(s, &p, &len, &al))
  2364. goto err;
  2365. } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK | SSL_kSM2DHE |
  2366. SSL_kSM2PSK)) {
  2367. if (!tls_construct_cke_ecdhe(s, &p, &len, &al))
  2368. goto err;
  2369. } else if (alg_k & SSL_kGOST) {
  2370. if (!tls_construct_cke_gost(s, &p, &len, &al))
  2371. goto err;
  2372. } else if (alg_k & SSL_kSRP) {
  2373. if (!tls_construct_cke_srp(s, &p, &len, &al))
  2374. goto err;
  2375. } else {
  2376. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  2377. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  2378. goto err;
  2379. }
  2380. len += pskhdrlen;
  2381. if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, len)) {
  2382. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  2383. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  2384. goto err;
  2385. }
  2386. return 1;
  2387. err:
  2388. if (al != -1)
  2389. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  2390. OPENSSL_clear_free(s->s3->tmp.pms, s->s3->tmp.pmslen);
  2391. s->s3->tmp.pms = NULL;
  2392. #ifndef OPENSSL_NO_PSK
  2393. OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
  2394. s->s3->tmp.psk = NULL;
  2395. #endif
  2396. ossl_statem_set_error(s);
  2397. return 0;
  2398. }
  2399. int tls_client_key_exchange_post_work(SSL *s)
  2400. {
  2401. unsigned char *pms = NULL;
  2402. size_t pmslen = 0;
  2403. pms = s->s3->tmp.pms;
  2404. pmslen = s->s3->tmp.pmslen;
  2405. #ifndef OPENSSL_NO_SRP
  2406. /* Check for SRP */
  2407. if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
  2408. if (!srp_generate_client_master_secret(s)) {
  2409. SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK,
  2410. ERR_R_INTERNAL_ERROR);
  2411. goto err;
  2412. }
  2413. return 1;
  2414. }
  2415. #endif
  2416. if (pms == NULL && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
  2417. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  2418. SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_MALLOC_FAILURE);
  2419. goto err;
  2420. }
  2421. if (!ssl_generate_master_secret(s, pms, pmslen, 1)) {
  2422. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  2423. SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_INTERNAL_ERROR);
  2424. /* ssl_generate_master_secret frees the pms even on error */
  2425. pms = NULL;
  2426. pmslen = 0;
  2427. goto err;
  2428. }
  2429. pms = NULL;
  2430. pmslen = 0;
  2431. #ifndef OPENSSL_NO_SCTP
  2432. if (SSL_IS_DTLS(s)) {
  2433. unsigned char sctpauthkey[64];
  2434. char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
  2435. /*
  2436. * Add new shared key for SCTP-Auth, will be ignored if no SCTP
  2437. * used.
  2438. */
  2439. memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
  2440. sizeof(DTLS1_SCTP_AUTH_LABEL));
  2441. if (SSL_export_keying_material(s, sctpauthkey,
  2442. sizeof(sctpauthkey), labelbuffer,
  2443. sizeof(labelbuffer), NULL, 0, 0) <= 0)
  2444. goto err;
  2445. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
  2446. sizeof(sctpauthkey), sctpauthkey);
  2447. }
  2448. #endif
  2449. return 1;
  2450. err:
  2451. OPENSSL_clear_free(pms, pmslen);
  2452. s->s3->tmp.pms = NULL;
  2453. return 0;
  2454. }
  2455. int tls_construct_client_verify(SSL *s)
  2456. {
  2457. unsigned char *p;
  2458. EVP_PKEY *pkey;
  2459. const EVP_MD *md = s->s3->tmp.md[s->cert->key - s->cert->pkeys];
  2460. EVP_MD_CTX *mctx;
  2461. unsigned u = 0;
  2462. unsigned long n = 0;
  2463. long hdatalen = 0;
  2464. void *hdata;
  2465. int ret;
  2466. unsigned char out[81920];
  2467. mctx = EVP_MD_CTX_new();
  2468. if (mctx == NULL) {
  2469. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_MALLOC_FAILURE);
  2470. goto err;
  2471. }
  2472. p = ssl_handshake_start(s);
  2473. // modify begin 使用gmtls 生成 certificate verify 消息,应该使用签名证书私钥
  2474. #ifndef OPENSSL_NO_GMTLS
  2475. if (SSL_IS_GMTLS(s) && s->cert->pkeys[SSL_PKEY_SM2].privatekey)
  2476. pkey = s->cert->pkeys[SSL_PKEY_SM2].privatekey;
  2477. else
  2478. #endif
  2479. pkey = s->cert->key->privatekey;
  2480. // modify end
  2481. hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
  2482. if (hdatalen <= 0) {
  2483. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
  2484. goto err;
  2485. }
  2486. memcpy(out, (unsigned char *)hdata, hdatalen);
  2487. out[hdatalen] = 0;
  2488. ret = hdatalen;
  2489. // modify begin 使用gmtls 对从 client hello 消息到 client CertificateVerify 消息(不包括client CertificateVerify消息)所有内容做SM3摘要计算
  2490. #ifndef OPENSSL_NO_SM2
  2491. EVP_MD_CTX *mctx1 = NULL;
  2492. if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aSM2)
  2493. {
  2494. // from client hello to client CertificateVerify(not include CertificateVerify) make sm3
  2495. mctx1 = EVP_MD_CTX_new();
  2496. if (mctx1 == NULL) {
  2497. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_MALLOC_FAILURE);
  2498. goto err;
  2499. }
  2500. EVP_MD_CTX_init(mctx1);
  2501. if (!EVP_DigestInit(mctx1, md)
  2502. || EVP_DigestUpdate(mctx1, (unsigned char *)hdata,
  2503. hdatalen) <= 0
  2504. || EVP_DigestFinal_ex(mctx1, out, &ret) <= 0)
  2505. {
  2506. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
  2507. goto err;
  2508. }
  2509. out[ret] = 0;
  2510. }
  2511. if (mctx1 != NULL)
  2512. EVP_MD_CTX_free(mctx1);
  2513. #endif
  2514. // modify end
  2515. if (SSL_USE_SIGALGS(s)) {
  2516. if (!tls12_get_sigandhash(p, pkey, md)) {
  2517. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
  2518. goto err;
  2519. }
  2520. p += 2;
  2521. n = 2;
  2522. }
  2523. #ifdef SSL_DEBUG
  2524. fprintf(stderr, "Using client alg %s\n", EVP_MD_name(md));
  2525. #endif
  2526. // modify begin 使用gmtls 从上面得到的SM3摘要结果,还需要使用 SM2_DEFAULT_ID(1234567812345678) 做内部哈希,再签名
  2527. if (!EVP_SignInit_ex(mctx, md, NULL))
  2528. {
  2529. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_EVP_LIB);
  2530. goto err;
  2531. }
  2532. #ifndef OPENSSL_NO_SM2
  2533. if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aSM2)
  2534. {
  2535. // SM2_DEFAULT_ID to assign, when compute CertificateVerify sign
  2536. unsigned char z[EVP_MAX_MD_SIZE];
  2537. size_t zlen;
  2538. char *id = NULL;
  2539. id = SM2_DEFAULT_ID;
  2540. zlen = sizeof(z);
  2541. if (!SM2_compute_id_digest(EVP_sm3(), id, strlen(id), z, &zlen,
  2542. EVP_PKEY_get0_EC_KEY(pkey))) {
  2543. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_SM2_LIB);
  2544. goto err;
  2545. }
  2546. if (!EVP_SignUpdate(mctx, z, zlen))
  2547. {
  2548. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_EVP_LIB);
  2549. goto err;
  2550. }
  2551. }
  2552. #endif
  2553. //if (!EVP_SignUpdate(mctx, hdata, hdatalen) || (s->version == SSL3_VERSION && !EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET,s->session->master_key_length,s->session->master_key)) || !EVP_SignFinal(mctx, p + 2, &u, pkey))
  2554. if (!EVP_SignUpdate(mctx, out, ret) || (s->version == SSL3_VERSION && !EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET,s->session->master_key_length,s->session->master_key)) || !EVP_SignFinal(mctx, p + 2, &u, pkey))
  2555. {
  2556. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_EVP_LIB);
  2557. goto err;
  2558. }
  2559. // modify end
  2560. #ifndef OPENSSL_NO_GOST
  2561. {
  2562. int pktype = EVP_PKEY_id(pkey);
  2563. if (pktype == NID_id_GostR3410_2001
  2564. || pktype == NID_id_GostR3410_2012_256
  2565. || pktype == NID_id_GostR3410_2012_512)
  2566. BUF_reverse(p + 2, NULL, u);
  2567. }
  2568. #endif
  2569. s2n(u, p);
  2570. n += u + 2;
  2571. /* Digest cached records and discard handshake buffer */
  2572. if (!ssl3_digest_cached_records(s, 0))
  2573. goto err;
  2574. if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
  2575. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
  2576. goto err;
  2577. }
  2578. EVP_MD_CTX_free(mctx);
  2579. return 1;
  2580. err:
  2581. EVP_MD_CTX_free(mctx);
  2582. return 0;
  2583. }
  2584. /*
  2585. * Check a certificate can be used for client authentication. Currently check
  2586. * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
  2587. * certificates can be used and optionally checks suitability for Suite B.
  2588. */
  2589. static int ssl3_check_client_certificate(SSL *s)
  2590. {
  2591. if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
  2592. return 0;
  2593. /* If no suitable signature algorithm can't use certificate */
  2594. if (SSL_USE_SIGALGS(s) && !s->s3->tmp.md[s->cert->key - s->cert->pkeys])
  2595. return 0;
  2596. /*
  2597. * If strict mode check suitability of chain before using it. This also
  2598. * adjusts suite B digest if necessary.
  2599. */
  2600. if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
  2601. !tls1_check_chain(s, NULL, NULL, NULL, -2))
  2602. return 0;
  2603. return 1;
  2604. }
  2605. WORK_STATE tls_prepare_client_certificate(SSL *s, WORK_STATE wst)
  2606. {
  2607. X509 *x509 = NULL;
  2608. EVP_PKEY *pkey = NULL;
  2609. int i;
  2610. if (wst == WORK_MORE_A) {
  2611. /* Let cert callback update client certificates if required */
  2612. if (s->cert->cert_cb) {
  2613. i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
  2614. if (i < 0) {
  2615. s->rwstate = SSL_X509_LOOKUP;
  2616. return WORK_MORE_A;
  2617. }
  2618. if (i == 0) {
  2619. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  2620. ossl_statem_set_error(s);
  2621. return 0;
  2622. }
  2623. s->rwstate = SSL_NOTHING;
  2624. }
  2625. if (ssl3_check_client_certificate(s))
  2626. return WORK_FINISHED_CONTINUE;
  2627. /* Fall through to WORK_MORE_B */
  2628. wst = WORK_MORE_B;
  2629. }
  2630. /* We need to get a client cert */
  2631. if (wst == WORK_MORE_B) {
  2632. /*
  2633. * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
  2634. * return(-1); We then get retied later
  2635. */
  2636. i = ssl_do_client_cert_cb(s, &x509, &pkey);
  2637. if (i < 0) {
  2638. s->rwstate = SSL_X509_LOOKUP;
  2639. return WORK_MORE_B;
  2640. }
  2641. s->rwstate = SSL_NOTHING;
  2642. if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
  2643. if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
  2644. i = 0;
  2645. } else if (i == 1) {
  2646. i = 0;
  2647. SSLerr(SSL_F_TLS_PREPARE_CLIENT_CERTIFICATE,
  2648. SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
  2649. }
  2650. X509_free(x509);
  2651. EVP_PKEY_free(pkey);
  2652. if (i && !ssl3_check_client_certificate(s))
  2653. i = 0;
  2654. if (i == 0) {
  2655. if (s->version == SSL3_VERSION) {
  2656. s->s3->tmp.cert_req = 0;
  2657. ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
  2658. return WORK_FINISHED_CONTINUE;
  2659. } else {
  2660. s->s3->tmp.cert_req = 2;
  2661. if (!ssl3_digest_cached_records(s, 0)) {
  2662. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  2663. ossl_statem_set_error(s);
  2664. return 0;
  2665. }
  2666. }
  2667. }
  2668. return WORK_FINISHED_CONTINUE;
  2669. }
  2670. /* Shouldn't ever get here */
  2671. return WORK_ERROR;
  2672. }
  2673. int tls_construct_client_certificate(SSL *s)
  2674. {
  2675. if (!ssl3_output_cert_chain(s,
  2676. (s->s3->tmp.cert_req ==
  2677. 2) ? NULL : s->cert->key)) {
  2678. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
  2679. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  2680. ossl_statem_set_error(s);
  2681. return 0;
  2682. }
  2683. return 1;
  2684. }
  2685. #define has_bits(i,m) (((i)&(m)) == (m))
  2686. int ssl3_check_cert_and_algorithm(SSL *s)
  2687. {
  2688. int i;
  2689. #ifndef OPENSSL_NO_EC
  2690. int idx;
  2691. #endif
  2692. long alg_k, alg_a;
  2693. EVP_PKEY *pkey = NULL;
  2694. int al = SSL_AD_HANDSHAKE_FAILURE;
  2695. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  2696. alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  2697. /* we don't have a certificate */
  2698. if ((alg_a & SSL_aNULL) || (alg_k & SSL_kPSK))
  2699. return (1);
  2700. /* This is the passed certificate */
  2701. #ifndef OPENSSL_NO_EC
  2702. idx = s->session->peer_type;
  2703. if ((idx == SSL_PKEY_ECC) || (idx == SSL_PKEY_SM2)) { /* GMTLS */
  2704. if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s) == 0) {
  2705. /* check failed */
  2706. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
  2707. goto f_err;
  2708. } else {
  2709. return 1;
  2710. }
  2711. } else if ((alg_a & SSL_aECDSA) || (alg_a & SSL_aSM2)) { /* GMTLS */
  2712. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  2713. SSL_R_MISSING_ECDSA_SIGNING_CERT);
  2714. goto f_err;
  2715. }
  2716. #endif
  2717. pkey = X509_get0_pubkey(s->session->peer);
  2718. i = X509_certificate_type(s->session->peer, pkey);
  2719. /* Check that we have a certificate if we require one */
  2720. if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
  2721. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  2722. SSL_R_MISSING_RSA_SIGNING_CERT);
  2723. goto f_err;
  2724. }
  2725. #ifndef OPENSSL_NO_DSA
  2726. else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
  2727. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  2728. SSL_R_MISSING_DSA_SIGNING_CERT);
  2729. goto f_err;
  2730. }
  2731. #endif
  2732. #ifndef OPENSSL_NO_RSA
  2733. if (alg_k & (SSL_kRSA | SSL_kRSAPSK) &&
  2734. !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
  2735. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
  2736. SSL_R_MISSING_RSA_ENCRYPTING_CERT);
  2737. goto f_err;
  2738. }
  2739. #endif
  2740. #ifndef OPENSSL_NO_DH
  2741. if ((alg_k & SSL_kDHE) && (s->s3->peer_tmp == NULL)) {
  2742. al = SSL_AD_INTERNAL_ERROR;
  2743. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
  2744. goto f_err;
  2745. }
  2746. #endif
  2747. return (1);
  2748. f_err:
  2749. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  2750. return (0);
  2751. }
  2752. #ifndef OPENSSL_NO_NEXTPROTONEG
  2753. int tls_construct_next_proto(SSL *s)
  2754. {
  2755. unsigned int len, padding_len;
  2756. unsigned char *d;
  2757. len = s->next_proto_negotiated_len;
  2758. padding_len = 32 - ((len + 2) % 32);
  2759. d = (unsigned char *)s->init_buf->data;
  2760. d[4] = len;
  2761. memcpy(d + 5, s->next_proto_negotiated, len);
  2762. d[5 + len] = padding_len;
  2763. memset(d + 6 + len, 0, padding_len);
  2764. *(d++) = SSL3_MT_NEXT_PROTO;
  2765. l2n3(2 + len + padding_len, d);
  2766. s->init_num = 4 + 2 + len + padding_len;
  2767. s->init_off = 0;
  2768. return 1;
  2769. }
  2770. #endif
  2771. int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
  2772. {
  2773. int i = 0;
  2774. #ifndef OPENSSL_NO_ENGINE
  2775. if (s->ctx->client_cert_engine) {
  2776. i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
  2777. SSL_get_client_CA_list(s),
  2778. px509, ppkey, NULL, NULL, NULL);
  2779. if (i != 0)
  2780. return i;
  2781. }
  2782. #endif
  2783. if (s->ctx->client_cert_cb)
  2784. i = s->ctx->client_cert_cb(s, px509, ppkey);
  2785. return i;
  2786. }
  2787. int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, unsigned char *p)
  2788. {
  2789. int i, j = 0;
  2790. const SSL_CIPHER *c;
  2791. unsigned char *q;
  2792. int empty_reneg_info_scsv = !s->renegotiate;
  2793. /* Set disabled masks for this session */
  2794. ssl_set_client_disabled(s);
  2795. if (sk == NULL)
  2796. return (0);
  2797. q = p;
  2798. for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
  2799. c = sk_SSL_CIPHER_value(sk, i);
  2800. /* Skip disabled ciphers */
  2801. if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED))
  2802. continue;
  2803. j = s->method->put_cipher_by_char(c, p);
  2804. p += j;
  2805. }
  2806. /*
  2807. * If p == q, no ciphers; caller indicates an error. Otherwise, add
  2808. * applicable SCSVs.
  2809. */
  2810. if (p != q) {
  2811. if (empty_reneg_info_scsv) {
  2812. static SSL_CIPHER scsv = {
  2813. 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
  2814. };
  2815. j = s->method->put_cipher_by_char(&scsv, p);
  2816. p += j;
  2817. }
  2818. if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
  2819. static SSL_CIPHER scsv = {
  2820. 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
  2821. };
  2822. j = s->method->put_cipher_by_char(&scsv, p);
  2823. p += j;
  2824. }
  2825. }
  2826. return (p - q);
  2827. }

statem_srvr

  1. /* ====================================================================
  2. * Copyright (c) 2014 - 2017 The GmSSL Project. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in
  13. * the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * 3. All advertising materials mentioning features or use of this
  17. * software must display the following acknowledgment:
  18. * "This product includes software developed by the GmSSL Project.
  19. * (http://gmssl.org/)"
  20. *
  21. * 4. The name "GmSSL Project" must not be used to endorse or promote
  22. * products derived from this software without prior written
  23. * permission. For written permission, please contact
  24. * guanzhi1980@gmail.com.
  25. *
  26. * 5. Products derived from this software may not be called "GmSSL"
  27. * nor may "GmSSL" appear in their names without prior written
  28. * permission of the GmSSL Project.
  29. *
  30. * 6. Redistributions of any form whatsoever must retain the following
  31. * acknowledgment:
  32. * "This product includes software developed by the GmSSL Project
  33. * (http://gmssl.org/)"
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
  36. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46. * OF THE POSSIBILITY OF SUCH DAMAGE.
  47. * ====================================================================
  48. */
  49. /*
  50. * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  51. *
  52. * Licensed under the OpenSSL license (the "License"). You may not use
  53. * this file except in compliance with the License. You can obtain a copy
  54. * in the file LICENSE in the source distribution or at
  55. * https://www.openssl.org/source/license.html
  56. */
  57. /* ====================================================================
  58. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  59. *
  60. * Portions of the attached software ("Contribution") are developed by
  61. * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
  62. *
  63. * The Contribution is licensed pursuant to the OpenSSL open source
  64. * license provided above.
  65. *
  66. * ECC cipher suite support in OpenSSL originally written by
  67. * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
  68. *
  69. */
  70. /* ====================================================================
  71. * Copyright 2005 Nokia. All rights reserved.
  72. *
  73. * The portions of the attached software ("Contribution") is developed by
  74. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  75. * license.
  76. *
  77. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  78. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  79. * support (see RFC 4279) to OpenSSL.
  80. *
  81. * No patent licenses or other rights except those expressly stated in
  82. * the OpenSSL open source license shall be deemed granted or received
  83. * expressly, by implication, estoppel, or otherwise.
  84. *
  85. * No assurances are provided by Nokia that the Contribution does not
  86. * infringe the patent or other intellectual property rights of any third
  87. * party or that the license provides you with all the necessary rights
  88. * to make use of the Contribution.
  89. *
  90. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  91. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  92. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  93. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  94. * OTHERWISE.
  95. */
  96. #include <stdio.h>
  97. #include "../ssl_locl.h"
  98. #include "statem_locl.h"
  99. #include "internal/constant_time_locl.h"
  100. #include <openssl/buffer.h>
  101. #include <openssl/rand.h>
  102. #include <openssl/objects.h>
  103. #include <openssl/evp.h>
  104. #include <openssl/hmac.h>
  105. #include <openssl/x509.h>
  106. #include <openssl/bn.h>
  107. #ifndef OPENSSL_NO_SM2
  108. # include <openssl/sm2.h>
  109. #endif
  110. static STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s,
  111. PACKET *cipher_suites,
  112. STACK_OF(SSL_CIPHER)
  113. **skp, int sslv2format,
  114. int *al);
  115. /*
  116. * server_read_transition() encapsulates the logic for the allowed handshake
  117. * state transitions when the server is reading messages from the client. The
  118. * message type that the client has sent is provided in |mt|. The current state
  119. * is in |s->statem.hand_state|.
  120. *
  121. * Valid return values are:
  122. * 1: Success (transition allowed)
  123. * 0: Error (transition not allowed)
  124. */
  125. int ossl_statem_server_read_transition(SSL *s, int mt)
  126. {
  127. OSSL_STATEM *st = &s->statem;
  128. switch (st->hand_state) {
  129. case TLS_ST_BEFORE:
  130. case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
  131. if (mt == SSL3_MT_CLIENT_HELLO) {
  132. st->hand_state = TLS_ST_SR_CLNT_HELLO;
  133. return 1;
  134. }
  135. break;
  136. case TLS_ST_SW_SRVR_DONE:
  137. /*
  138. * If we get a CKE message after a ServerDone then either
  139. * 1) We didn't request a Certificate
  140. * OR
  141. * 2) If we did request one then
  142. * a) We allow no Certificate to be returned
  143. * AND
  144. * b) We are running SSL3 (in TLS1.0+ the client must return a 0
  145. * list if we requested a certificate)
  146. */
  147. if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) {
  148. if (s->s3->tmp.cert_request) {
  149. if (s->version == SSL3_VERSION) {
  150. if ((s->verify_mode & SSL_VERIFY_PEER)
  151. && (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
  152. /*
  153. * This isn't an unexpected message as such - we're just
  154. * not going to accept it because we require a client
  155. * cert.
  156. */
  157. ssl3_send_alert(s, SSL3_AL_FATAL,
  158. SSL3_AD_HANDSHAKE_FAILURE);
  159. SSLerr(SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION,
  160. SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  161. return 0;
  162. }
  163. st->hand_state = TLS_ST_SR_KEY_EXCH;
  164. return 1;
  165. }
  166. } else {
  167. st->hand_state = TLS_ST_SR_KEY_EXCH;
  168. return 1;
  169. }
  170. } else if (s->s3->tmp.cert_request) {
  171. if (mt == SSL3_MT_CERTIFICATE) {
  172. st->hand_state = TLS_ST_SR_CERT;
  173. return 1;
  174. }
  175. }
  176. break;
  177. case TLS_ST_SR_CERT:
  178. if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) {
  179. st->hand_state = TLS_ST_SR_KEY_EXCH;
  180. return 1;
  181. }
  182. break;
  183. case TLS_ST_SR_KEY_EXCH:
  184. /*
  185. * We should only process a CertificateVerify message if we have
  186. * received a Certificate from the client. If so then |s->session->peer|
  187. * will be non NULL. In some instances a CertificateVerify message is
  188. * not required even if the peer has sent a Certificate (e.g. such as in
  189. * the case of static DH). In that case |st->no_cert_verify| should be
  190. * set.
  191. */
  192. if (s->session->peer == NULL || st->no_cert_verify) {
  193. if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  194. /*
  195. * For the ECDH ciphersuites when the client sends its ECDH
  196. * pub key in a certificate, the CertificateVerify message is
  197. * not sent. Also for GOST ciphersuites when the client uses
  198. * its key from the certificate for key exchange.
  199. */
  200. st->hand_state = TLS_ST_SR_CHANGE;
  201. return 1;
  202. }
  203. } else {
  204. if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
  205. st->hand_state = TLS_ST_SR_CERT_VRFY;
  206. return 1;
  207. }
  208. }
  209. break;
  210. case TLS_ST_SR_CERT_VRFY:
  211. if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  212. st->hand_state = TLS_ST_SR_CHANGE;
  213. return 1;
  214. }
  215. break;
  216. case TLS_ST_SR_CHANGE:
  217. #ifndef OPENSSL_NO_NEXTPROTONEG
  218. if (s->s3->next_proto_neg_seen) {
  219. if (mt == SSL3_MT_NEXT_PROTO) {
  220. st->hand_state = TLS_ST_SR_NEXT_PROTO;
  221. return 1;
  222. }
  223. } else {
  224. #endif
  225. if (mt == SSL3_MT_FINISHED) {
  226. st->hand_state = TLS_ST_SR_FINISHED;
  227. return 1;
  228. }
  229. #ifndef OPENSSL_NO_NEXTPROTONEG
  230. }
  231. #endif
  232. break;
  233. #ifndef OPENSSL_NO_NEXTPROTONEG
  234. case TLS_ST_SR_NEXT_PROTO:
  235. if (mt == SSL3_MT_FINISHED) {
  236. st->hand_state = TLS_ST_SR_FINISHED;
  237. return 1;
  238. }
  239. break;
  240. #endif
  241. case TLS_ST_SW_FINISHED:
  242. if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
  243. st->hand_state = TLS_ST_SR_CHANGE;
  244. return 1;
  245. }
  246. break;
  247. default:
  248. break;
  249. }
  250. /* No valid transition found */
  251. ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_UNEXPECTED_MESSAGE);
  252. SSLerr(SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION, SSL_R_UNEXPECTED_MESSAGE);
  253. return 0;
  254. }
  255. /*
  256. * Should we send a ServerKeyExchange message?
  257. *
  258. * Valid return values are:
  259. * 1: Yes
  260. * 0: No
  261. */
  262. static int send_server_key_exchange(SSL *s)
  263. {
  264. unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  265. #ifndef OPENSSL_NO_GMTLS
  266. if (SSL_IS_GMTLS(s))
  267. return 1;
  268. #endif
  269. /*
  270. * only send a ServerKeyExchange if DH or fortezza but we have a
  271. * sign only certificate PSK: may send PSK identity hints For
  272. * ECC ciphersuites, we send a serverKeyExchange message only if
  273. * the cipher suite is either ECDH-anon or ECDHE. In other cases,
  274. * the server certificate contains the server's public key for
  275. * key exchange.
  276. */
  277. if (alg_k & (SSL_kDHE | SSL_kECDHE | SSL_kSM2DHE)
  278. /*
  279. * PSK: send ServerKeyExchange if PSK identity hint if
  280. * provided
  281. */
  282. #ifndef OPENSSL_NO_PSK
  283. /* Only send SKE if we have identity hint for plain PSK */
  284. || ((alg_k & (SSL_kPSK | SSL_kRSAPSK))
  285. && s->cert->psk_identity_hint)
  286. /* For other PSK always send SKE */
  287. || (alg_k & (SSL_PSK & (SSL_kDHEPSK | SSL_kECDHEPSK | SSL_kSM2PSK)))
  288. #endif
  289. #ifndef OPENSSL_NO_SRP
  290. /* SRP: send ServerKeyExchange */
  291. || (alg_k & SSL_kSRP)
  292. #endif
  293. ) {
  294. return 1;
  295. }
  296. return 0;
  297. }
  298. /*
  299. * Should we send a CertificateRequest message?
  300. *
  301. * Valid return values are:
  302. * 1: Yes
  303. * 0: No
  304. */
  305. static int send_certificate_request(SSL *s)
  306. {
  307. if (
  308. /* don't request cert unless asked for it: */
  309. s->verify_mode & SSL_VERIFY_PEER
  310. /*
  311. * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
  312. * during re-negotiation:
  313. */
  314. && (s->s3->tmp.finish_md_len == 0 ||
  315. !(s->verify_mode & SSL_VERIFY_CLIENT_ONCE))
  316. /*
  317. * never request cert in anonymous ciphersuites (see
  318. * section "Certificate request" in SSL 3 drafts and in
  319. * RFC 2246):
  320. */
  321. && (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
  322. /*
  323. * ... except when the application insists on
  324. * verification (against the specs, but statem_clnt.c accepts
  325. * this for SSL 3)
  326. */
  327. || (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
  328. /* don't request certificate for SRP auth */
  329. && !(s->s3->tmp.new_cipher->algorithm_auth & SSL_aSRP)
  330. /*
  331. * With normal PSK Certificates and Certificate Requests
  332. * are omitted
  333. */
  334. && !(s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK)) {
  335. return 1;
  336. }
  337. return 0;
  338. }
  339. /*
  340. * server_write_transition() works out what handshake state to move to next
  341. * when the server is writing messages to be sent to the client.
  342. */
  343. WRITE_TRAN ossl_statem_server_write_transition(SSL *s)
  344. {
  345. OSSL_STATEM *st = &s->statem;
  346. switch (st->hand_state) {
  347. case TLS_ST_BEFORE:
  348. /* Just go straight to trying to read from the client */
  349. return WRITE_TRAN_FINISHED;
  350. case TLS_ST_OK:
  351. /* We must be trying to renegotiate */
  352. st->hand_state = TLS_ST_SW_HELLO_REQ;
  353. return WRITE_TRAN_CONTINUE;
  354. case TLS_ST_SW_HELLO_REQ:
  355. st->hand_state = TLS_ST_OK;
  356. ossl_statem_set_in_init(s, 0);
  357. return WRITE_TRAN_CONTINUE;
  358. case TLS_ST_SR_CLNT_HELLO:
  359. if (SSL_IS_DTLS(s) && !s->d1->cookie_verified
  360. && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE))
  361. st->hand_state = DTLS_ST_SW_HELLO_VERIFY_REQUEST;
  362. else
  363. st->hand_state = TLS_ST_SW_SRVR_HELLO;
  364. return WRITE_TRAN_CONTINUE;
  365. case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
  366. return WRITE_TRAN_FINISHED;
  367. case TLS_ST_SW_SRVR_HELLO:
  368. if (s->hit) {
  369. if (s->tlsext_ticket_expected)
  370. st->hand_state = TLS_ST_SW_SESSION_TICKET;
  371. else
  372. st->hand_state = TLS_ST_SW_CHANGE;
  373. } else {
  374. /* Check if it is anon DH or anon ECDH, */
  375. /* normal PSK or SRP */
  376. if (!(s->s3->tmp.new_cipher->algorithm_auth &
  377. (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
  378. st->hand_state = TLS_ST_SW_CERT;
  379. } else if (send_server_key_exchange(s)) {
  380. st->hand_state = TLS_ST_SW_KEY_EXCH;
  381. } else if (send_certificate_request(s)) {
  382. st->hand_state = TLS_ST_SW_CERT_REQ;
  383. } else {
  384. st->hand_state = TLS_ST_SW_SRVR_DONE;
  385. }
  386. }
  387. return WRITE_TRAN_CONTINUE;
  388. case TLS_ST_SW_CERT:
  389. if (s->tlsext_status_expected) {
  390. st->hand_state = TLS_ST_SW_CERT_STATUS;
  391. return WRITE_TRAN_CONTINUE;
  392. }
  393. /* Fall through */
  394. case TLS_ST_SW_CERT_STATUS:
  395. if (send_server_key_exchange(s)) {
  396. st->hand_state = TLS_ST_SW_KEY_EXCH;
  397. return WRITE_TRAN_CONTINUE;
  398. }
  399. /* Fall through */
  400. case TLS_ST_SW_KEY_EXCH:
  401. if (send_certificate_request(s)) {
  402. st->hand_state = TLS_ST_SW_CERT_REQ;
  403. return WRITE_TRAN_CONTINUE;
  404. }
  405. /* Fall through */
  406. case TLS_ST_SW_CERT_REQ:
  407. st->hand_state = TLS_ST_SW_SRVR_DONE;
  408. return WRITE_TRAN_CONTINUE;
  409. case TLS_ST_SW_SRVR_DONE:
  410. return WRITE_TRAN_FINISHED;
  411. case TLS_ST_SR_FINISHED:
  412. if (s->hit) {
  413. st->hand_state = TLS_ST_OK;
  414. ossl_statem_set_in_init(s, 0);
  415. return WRITE_TRAN_CONTINUE;
  416. } else if (s->tlsext_ticket_expected) {
  417. st->hand_state = TLS_ST_SW_SESSION_TICKET;
  418. } else {
  419. st->hand_state = TLS_ST_SW_CHANGE;
  420. }
  421. return WRITE_TRAN_CONTINUE;
  422. case TLS_ST_SW_SESSION_TICKET:
  423. st->hand_state = TLS_ST_SW_CHANGE;
  424. return WRITE_TRAN_CONTINUE;
  425. case TLS_ST_SW_CHANGE:
  426. st->hand_state = TLS_ST_SW_FINISHED;
  427. return WRITE_TRAN_CONTINUE;
  428. case TLS_ST_SW_FINISHED:
  429. if (s->hit) {
  430. return WRITE_TRAN_FINISHED;
  431. }
  432. st->hand_state = TLS_ST_OK;
  433. ossl_statem_set_in_init(s, 0);
  434. return WRITE_TRAN_CONTINUE;
  435. default:
  436. /* Shouldn't happen */
  437. return WRITE_TRAN_ERROR;
  438. }
  439. }
  440. /*
  441. * Perform any pre work that needs to be done prior to sending a message from
  442. * the server to the client.
  443. */
  444. WORK_STATE ossl_statem_server_pre_work(SSL *s, WORK_STATE wst)
  445. {
  446. OSSL_STATEM *st = &s->statem;
  447. switch (st->hand_state) {
  448. case TLS_ST_SW_HELLO_REQ:
  449. s->shutdown = 0;
  450. if (SSL_IS_DTLS(s))
  451. dtls1_clear_sent_buffer(s);
  452. break;
  453. case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
  454. s->shutdown = 0;
  455. if (SSL_IS_DTLS(s)) {
  456. dtls1_clear_sent_buffer(s);
  457. /* We don't buffer this message so don't use the timer */
  458. st->use_timer = 0;
  459. }
  460. break;
  461. case TLS_ST_SW_SRVR_HELLO:
  462. if (SSL_IS_DTLS(s)) {
  463. /*
  464. * Messages we write from now on should be bufferred and
  465. * retransmitted if necessary, so we need to use the timer now
  466. */
  467. st->use_timer = 1;
  468. }
  469. break;
  470. case TLS_ST_SW_SRVR_DONE:
  471. #ifndef OPENSSL_NO_SCTP
  472. if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s)))
  473. return dtls_wait_for_dry(s);
  474. #endif
  475. return WORK_FINISHED_CONTINUE;
  476. case TLS_ST_SW_SESSION_TICKET:
  477. if (SSL_IS_DTLS(s)) {
  478. /*
  479. * We're into the last flight. We don't retransmit the last flight
  480. * unless we need to, so we don't use the timer
  481. */
  482. st->use_timer = 0;
  483. }
  484. break;
  485. case TLS_ST_SW_CHANGE:
  486. s->session->cipher = s->s3->tmp.new_cipher;
  487. if (!s->method->ssl3_enc->setup_key_block(s)) {
  488. ossl_statem_set_error(s);
  489. return WORK_ERROR;
  490. }
  491. if (SSL_IS_DTLS(s)) {
  492. /*
  493. * We're into the last flight. We don't retransmit the last flight
  494. * unless we need to, so we don't use the timer. This might have
  495. * already been set to 0 if we sent a NewSessionTicket message,
  496. * but we'll set it again here in case we didn't.
  497. */
  498. st->use_timer = 0;
  499. }
  500. return WORK_FINISHED_CONTINUE;
  501. case TLS_ST_OK:
  502. return tls_finish_handshake(s, wst);
  503. default:
  504. /* No pre work to be done */
  505. break;
  506. }
  507. return WORK_FINISHED_CONTINUE;
  508. }
  509. /*
  510. * Perform any work that needs to be done after sending a message from the
  511. * server to the client.
  512. */
  513. WORK_STATE ossl_statem_server_post_work(SSL *s, WORK_STATE wst)
  514. {
  515. OSSL_STATEM *st = &s->statem;
  516. s->init_num = 0;
  517. switch (st->hand_state) {
  518. case TLS_ST_SW_HELLO_REQ:
  519. if (statem_flush(s) != 1)
  520. return WORK_MORE_A;
  521. if (!ssl3_init_finished_mac(s)) {
  522. ossl_statem_set_error(s);
  523. return WORK_ERROR;
  524. }
  525. break;
  526. case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
  527. if (statem_flush(s) != 1)
  528. return WORK_MORE_A;
  529. /* HelloVerifyRequest resets Finished MAC */
  530. if (s->version != DTLS1_BAD_VER && !ssl3_init_finished_mac(s)) {
  531. ossl_statem_set_error(s);
  532. return WORK_ERROR;
  533. }
  534. /*
  535. * The next message should be another ClientHello which we need to
  536. * treat like it was the first packet
  537. */
  538. s->first_packet = 1;
  539. break;
  540. case TLS_ST_SW_SRVR_HELLO:
  541. #ifndef OPENSSL_NO_SCTP
  542. if (SSL_IS_DTLS(s) && s->hit) {
  543. unsigned char sctpauthkey[64];
  544. char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
  545. /*
  546. * Add new shared key for SCTP-Auth, will be ignored if no
  547. * SCTP used.
  548. */
  549. memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
  550. sizeof(DTLS1_SCTP_AUTH_LABEL));
  551. if (SSL_export_keying_material(s, sctpauthkey,
  552. sizeof(sctpauthkey), labelbuffer,
  553. sizeof(labelbuffer), NULL, 0,
  554. 0) <= 0) {
  555. ossl_statem_set_error(s);
  556. return WORK_ERROR;
  557. }
  558. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
  559. sizeof(sctpauthkey), sctpauthkey);
  560. }
  561. #endif
  562. break;
  563. case TLS_ST_SW_CHANGE:
  564. #ifndef OPENSSL_NO_SCTP
  565. if (SSL_IS_DTLS(s) && !s->hit) {
  566. /*
  567. * Change to new shared key of SCTP-Auth, will be ignored if
  568. * no SCTP used.
  569. */
  570. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
  571. 0, NULL);
  572. }
  573. #endif
  574. if (!s->method->ssl3_enc->change_cipher_state(s,
  575. SSL3_CHANGE_CIPHER_SERVER_WRITE))
  576. {
  577. ossl_statem_set_error(s);
  578. return WORK_ERROR;
  579. }
  580. if (SSL_IS_DTLS(s))
  581. dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
  582. break;
  583. case TLS_ST_SW_SRVR_DONE:
  584. if (statem_flush(s) != 1)
  585. return WORK_MORE_A;
  586. break;
  587. case TLS_ST_SW_FINISHED:
  588. if (statem_flush(s) != 1)
  589. return WORK_MORE_A;
  590. #ifndef OPENSSL_NO_SCTP
  591. if (SSL_IS_DTLS(s) && s->hit) {
  592. /*
  593. * Change to new shared key of SCTP-Auth, will be ignored if
  594. * no SCTP used.
  595. */
  596. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
  597. 0, NULL);
  598. }
  599. #endif
  600. break;
  601. default:
  602. /* No post work to be done */
  603. break;
  604. }
  605. return WORK_FINISHED_CONTINUE;
  606. }
  607. /*
  608. * Construct a message to be sent from the server to the client.
  609. *
  610. * Valid return values are:
  611. * 1: Success
  612. * 0: Error
  613. */
  614. int ossl_statem_server_construct_message(SSL *s)
  615. {
  616. OSSL_STATEM *st = &s->statem;
  617. switch (st->hand_state) {
  618. case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
  619. return dtls_construct_hello_verify_request(s);
  620. case TLS_ST_SW_HELLO_REQ:
  621. return tls_construct_hello_request(s);
  622. case TLS_ST_SW_SRVR_HELLO:
  623. return tls_construct_server_hello(s);
  624. case TLS_ST_SW_CERT:
  625. #ifndef OPENSSL_NO_GMTLS
  626. if (SSL_IS_GMTLS(s))
  627. return gmtls_construct_server_certificate(s);
  628. #endif
  629. return tls_construct_server_certificate(s);
  630. case TLS_ST_SW_KEY_EXCH:
  631. #ifndef OPENSSL_NO_GMTLS
  632. if (SSL_IS_GMTLS(s))
  633. return gmtls_construct_server_key_exchange(s);
  634. #endif
  635. return tls_construct_server_key_exchange(s);
  636. case TLS_ST_SW_CERT_REQ:
  637. return tls_construct_certificate_request(s);
  638. case TLS_ST_SW_SRVR_DONE:
  639. return tls_construct_server_done(s);
  640. case TLS_ST_SW_SESSION_TICKET:
  641. return tls_construct_new_session_ticket(s);
  642. case TLS_ST_SW_CERT_STATUS:
  643. return tls_construct_cert_status(s);
  644. case TLS_ST_SW_CHANGE:
  645. if (SSL_IS_DTLS(s))
  646. return dtls_construct_change_cipher_spec(s);
  647. else
  648. return tls_construct_change_cipher_spec(s);
  649. case TLS_ST_SW_FINISHED:
  650. return tls_construct_finished(s,
  651. s->method->
  652. ssl3_enc->server_finished_label,
  653. s->method->
  654. ssl3_enc->server_finished_label_len);
  655. default:
  656. /* Shouldn't happen */
  657. break;
  658. }
  659. return 0;
  660. }
  661. /*
  662. * Maximum size (excluding the Handshake header) of a ClientHello message,
  663. * calculated as follows:
  664. *
  665. * 2 + # client_version
  666. * 32 + # only valid length for random
  667. * 1 + # length of session_id
  668. * 32 + # maximum size for session_id
  669. * 2 + # length of cipher suites
  670. * 2^16-2 + # maximum length of cipher suites array
  671. * 1 + # length of compression_methods
  672. * 2^8-1 + # maximum length of compression methods
  673. * 2 + # length of extensions
  674. * 2^16-1 # maximum length of extensions
  675. */
  676. #define CLIENT_HELLO_MAX_LENGTH 131396
  677. #define CLIENT_KEY_EXCH_MAX_LENGTH 2048
  678. #define NEXT_PROTO_MAX_LENGTH 514
  679. /*
  680. * Returns the maximum allowed length for the current message that we are
  681. * reading. Excludes the message header.
  682. */
  683. unsigned long ossl_statem_server_max_message_size(SSL *s)
  684. {
  685. OSSL_STATEM *st = &s->statem;
  686. switch (st->hand_state) {
  687. case TLS_ST_SR_CLNT_HELLO:
  688. return CLIENT_HELLO_MAX_LENGTH;
  689. case TLS_ST_SR_CERT:
  690. return s->max_cert_list;
  691. case TLS_ST_SR_KEY_EXCH:
  692. return CLIENT_KEY_EXCH_MAX_LENGTH;
  693. case TLS_ST_SR_CERT_VRFY:
  694. return SSL3_RT_MAX_PLAIN_LENGTH;
  695. #ifndef OPENSSL_NO_NEXTPROTONEG
  696. case TLS_ST_SR_NEXT_PROTO:
  697. return NEXT_PROTO_MAX_LENGTH;
  698. #endif
  699. case TLS_ST_SR_CHANGE:
  700. return CCS_MAX_LENGTH;
  701. case TLS_ST_SR_FINISHED:
  702. return FINISHED_MAX_LENGTH;
  703. default:
  704. /* Shouldn't happen */
  705. break;
  706. }
  707. return 0;
  708. }
  709. /*
  710. * Process a message that the server has received from the client.
  711. */
  712. MSG_PROCESS_RETURN ossl_statem_server_process_message(SSL *s, PACKET *pkt)
  713. {
  714. OSSL_STATEM *st = &s->statem;
  715. switch (st->hand_state) {
  716. case TLS_ST_SR_CLNT_HELLO:
  717. return tls_process_client_hello(s, pkt);
  718. case TLS_ST_SR_CERT:
  719. #ifndef OPENSSL_NO_GMTLS
  720. if (SSL_IS_GMTLS(s))
  721. return tls_process_client_certificate(s, pkt);
  722. #endif
  723. return tls_process_client_certificate(s, pkt);
  724. case TLS_ST_SR_KEY_EXCH:
  725. #ifndef OPENSSL_NO_GMTLS
  726. if (SSL_IS_GMTLS(s))
  727. return gmtls_process_client_key_exchange(s, pkt);
  728. #endif
  729. return tls_process_client_key_exchange(s, pkt);
  730. case TLS_ST_SR_CERT_VRFY:
  731. return tls_process_cert_verify(s, pkt);
  732. #ifndef OPENSSL_NO_NEXTPROTONEG
  733. case TLS_ST_SR_NEXT_PROTO:
  734. return tls_process_next_proto(s, pkt);
  735. #endif
  736. case TLS_ST_SR_CHANGE:
  737. return tls_process_change_cipher_spec(s, pkt);
  738. case TLS_ST_SR_FINISHED:
  739. return tls_process_finished(s, pkt);
  740. default:
  741. /* Shouldn't happen */
  742. break;
  743. }
  744. return MSG_PROCESS_ERROR;
  745. }
  746. /*
  747. * Perform any further processing required following the receipt of a message
  748. * from the client
  749. */
  750. WORK_STATE ossl_statem_server_post_process_message(SSL *s, WORK_STATE wst)
  751. {
  752. OSSL_STATEM *st = &s->statem;
  753. switch (st->hand_state) {
  754. case TLS_ST_SR_CLNT_HELLO:
  755. return tls_post_process_client_hello(s, wst);
  756. case TLS_ST_SR_KEY_EXCH:
  757. return tls_post_process_client_key_exchange(s, wst);
  758. case TLS_ST_SR_CERT_VRFY:
  759. #ifndef OPENSSL_NO_SCTP
  760. if ( /* Is this SCTP? */
  761. BIO_dgram_is_sctp(SSL_get_wbio(s))
  762. /* Are we renegotiating? */
  763. && s->renegotiate && BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
  764. s->s3->in_read_app_data = 2;
  765. s->rwstate = SSL_READING;
  766. BIO_clear_retry_flags(SSL_get_rbio(s));
  767. BIO_set_retry_read(SSL_get_rbio(s));
  768. ossl_statem_set_sctp_read_sock(s, 1);
  769. return WORK_MORE_A;
  770. } else {
  771. ossl_statem_set_sctp_read_sock(s, 0);
  772. }
  773. #endif
  774. return WORK_FINISHED_CONTINUE;
  775. default:
  776. break;
  777. }
  778. /* Shouldn't happen */
  779. return WORK_ERROR;
  780. }
  781. #ifndef OPENSSL_NO_SRP
  782. static int ssl_check_srp_ext_ClientHello(SSL *s, int *al)
  783. {
  784. int ret = SSL_ERROR_NONE;
  785. *al = SSL_AD_UNRECOGNIZED_NAME;
  786. if ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) &&
  787. (s->srp_ctx.TLS_ext_srp_username_callback != NULL)) {
  788. if (s->srp_ctx.login == NULL) {
  789. /*
  790. * RFC 5054 says SHOULD reject, we do so if There is no srp
  791. * login name
  792. */
  793. ret = SSL3_AL_FATAL;
  794. *al = SSL_AD_UNKNOWN_PSK_IDENTITY;
  795. } else {
  796. ret = SSL_srp_server_param_with_username(s, al);
  797. }
  798. }
  799. return ret;
  800. }
  801. #endif
  802. int tls_construct_hello_request(SSL *s)
  803. {
  804. if (!ssl_set_handshake_header(s, SSL3_MT_HELLO_REQUEST, 0)) {
  805. SSLerr(SSL_F_TLS_CONSTRUCT_HELLO_REQUEST, ERR_R_INTERNAL_ERROR);
  806. ossl_statem_set_error(s);
  807. return 0;
  808. }
  809. return 1;
  810. }
  811. unsigned int dtls_raw_hello_verify_request(unsigned char *buf,
  812. unsigned char *cookie,
  813. unsigned char cookie_len)
  814. {
  815. unsigned int msg_len;
  816. unsigned char *p;
  817. p = buf;
  818. /* Always use DTLS 1.0 version: see RFC 6347 */
  819. *(p++) = DTLS1_VERSION >> 8;
  820. *(p++) = DTLS1_VERSION & 0xFF;
  821. *(p++) = (unsigned char)cookie_len;
  822. memcpy(p, cookie, cookie_len);
  823. p += cookie_len;
  824. msg_len = p - buf;
  825. return msg_len;
  826. }
  827. int dtls_construct_hello_verify_request(SSL *s)
  828. {
  829. unsigned int len;
  830. unsigned char *buf;
  831. buf = (unsigned char *)s->init_buf->data;
  832. if (s->ctx->app_gen_cookie_cb == NULL ||
  833. s->ctx->app_gen_cookie_cb(s, s->d1->cookie,
  834. &(s->d1->cookie_len)) == 0 ||
  835. s->d1->cookie_len > 255) {
  836. SSLerr(SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST,
  837. SSL_R_COOKIE_GEN_CALLBACK_FAILURE);
  838. ossl_statem_set_error(s);
  839. return 0;
  840. }
  841. len = dtls_raw_hello_verify_request(&buf[DTLS1_HM_HEADER_LENGTH],
  842. s->d1->cookie, s->d1->cookie_len);
  843. dtls1_set_message_header(s, DTLS1_MT_HELLO_VERIFY_REQUEST, len, 0, len);
  844. len += DTLS1_HM_HEADER_LENGTH;
  845. /* number of bytes to write */
  846. s->init_num = len;
  847. s->init_off = 0;
  848. return 1;
  849. }
  850. MSG_PROCESS_RETURN tls_process_client_hello(SSL *s, PACKET *pkt)
  851. {
  852. int i, al = SSL_AD_INTERNAL_ERROR;
  853. unsigned int j, complen = 0;
  854. unsigned long id;
  855. const SSL_CIPHER *c;
  856. #ifndef OPENSSL_NO_COMP
  857. SSL_COMP *comp = NULL;
  858. #endif
  859. STACK_OF(SSL_CIPHER) *ciphers = NULL;
  860. int protverr;
  861. /* |cookie| will only be initialized for DTLS. */
  862. PACKET session_id, cipher_suites, compression, extensions, cookie;
  863. int is_v2_record;
  864. static const unsigned char null_compression = 0;
  865. is_v2_record = RECORD_LAYER_is_sslv2_record(&s->rlayer);
  866. PACKET_null_init(&cookie);
  867. /* First lets get s->client_version set correctly */
  868. if (is_v2_record) {
  869. unsigned int version;
  870. unsigned int mt;
  871. /*-
  872. * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2
  873. * header is sent directly on the wire, not wrapped as a TLS
  874. * record. Our record layer just processes the message length and passes
  875. * the rest right through. Its format is:
  876. * Byte Content
  877. * 0-1 msg_length - decoded by the record layer
  878. * 2 msg_type - s->init_msg points here
  879. * 3-4 version
  880. * 5-6 cipher_spec_length
  881. * 7-8 session_id_length
  882. * 9-10 challenge_length
  883. * ... ...
  884. */
  885. if (!PACKET_get_1(pkt, &mt)
  886. || mt != SSL2_MT_CLIENT_HELLO) {
  887. /*
  888. * Should never happen. We should have tested this in the record
  889. * layer in order to have determined that this is a SSLv2 record
  890. * in the first place
  891. */
  892. SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
  893. goto err;
  894. }
  895. if (!PACKET_get_net_2(pkt, &version)) {
  896. /* No protocol version supplied! */
  897. SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);
  898. goto err;
  899. }
  900. if (version == 0x0002) {
  901. /* This is real SSLv2. We don't support it. */
  902. SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);
  903. goto err;
  904. } else if ((version & 0xff00) == (SSL3_VERSION_MAJOR << 8)) {
  905. /* SSLv3/TLS */
  906. s->client_version = version;
  907. #ifndef OPENSSL_NO_GMTLS
  908. } else if (version == GMTLS_VERSION) {
  909. s->client_version = version;
  910. #endif
  911. } else {
  912. /* No idea what protocol this is */
  913. SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);
  914. goto err;
  915. }
  916. } else {
  917. /*
  918. * use version from inside client hello, not from record header (may
  919. * differ: see RFC 2246, Appendix E, second paragraph)
  920. */
  921. if (!PACKET_get_net_2(pkt, (unsigned int *)&s->client_version)) {
  922. al = SSL_AD_DECODE_ERROR;
  923. SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
  924. goto f_err;
  925. }
  926. }
  927. /*
  928. * Do SSL/TLS version negotiation if applicable. For DTLS we just check
  929. * versions are potentially compatible. Version negotiation comes later.
  930. */
  931. if (!SSL_IS_DTLS(s)) {
  932. protverr = ssl_choose_server_version(s);
  933. } else if (s->method->version != DTLS_ANY_VERSION &&
  934. DTLS_VERSION_LT(s->client_version, s->version)) {
  935. protverr = SSL_R_VERSION_TOO_LOW;
  936. } else {
  937. protverr = 0;
  938. }
  939. if (protverr) {
  940. SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, protverr);
  941. if ((!s->enc_write_ctx && !s->write_hash)) {
  942. /*
  943. * similar to ssl3_get_record, send alert using remote version
  944. * number
  945. */
  946. s->version = s->client_version;
  947. }
  948. al = SSL_AD_PROTOCOL_VERSION;
  949. goto f_err;
  950. }
  951. /* Parse the message and load client random. */
  952. if (is_v2_record) {
  953. /*
  954. * Handle an SSLv2 backwards compatible ClientHello
  955. * Note, this is only for SSLv3+ using the backward compatible format.
  956. * Real SSLv2 is not supported, and is rejected above.
  957. */
  958. unsigned int cipher_len, session_id_len, challenge_len;
  959. PACKET challenge;
  960. if (!PACKET_get_net_2(pkt, &cipher_len)
  961. || !PACKET_get_net_2(pkt, &session_id_len)
  962. || !PACKET_get_net_2(pkt, &challenge_len)) {
  963. SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
  964. SSL_R_RECORD_LENGTH_MISMATCH);
  965. al = SSL_AD_DECODE_ERROR;
  966. goto f_err;
  967. }
  968. if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
  969. al = SSL_AD_DECODE_ERROR;
  970. SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
  971. goto f_err;
  972. }
  973. if (!PACKET_get_sub_packet(pkt, &cipher_suites, cipher_len)
  974. || !PACKET_get_sub_packet(pkt, &session_id, session_id_len)
  975. || !PACKET_get_sub_packet(pkt, &challenge, challenge_len)
  976. /* No extensions. */
  977. || PACKET_remaining(pkt) != 0) {
  978. SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
  979. SSL_R_RECORD_LENGTH_MISMATCH);
  980. al = SSL_AD_DECODE_ERROR;
  981. goto f_err;
  982. }
  983. /* Load the client random and compression list. */
  984. challenge_len = challenge_len > SSL3_RANDOM_SIZE ? SSL3_RANDOM_SIZE :
  985. challenge_len;
  986. memset(s->s3->client_random, 0, SSL3_RANDOM_SIZE);
  987. if (!PACKET_copy_bytes(&challenge,
  988. s->s3->client_random + SSL3_RANDOM_SIZE -
  989. challenge_len, challenge_len)
  990. /* Advertise only null compression. */
  991. || !PACKET_buf_init(&compression, &null_compression, 1)) {
  992. SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
  993. al = SSL_AD_INTERNAL_ERROR;
  994. goto f_err;
  995. }
  996. PACKET_null_init(&extensions);
  997. } else {
  998. /* Regular ClientHello. */
  999. if (!PACKET_copy_bytes(pkt, s->s3->client_random, SSL3_RANDOM_SIZE)
  1000. || !PACKET_get_length_prefixed_1(pkt, &session_id)) {
  1001. al = SSL_AD_DECODE_ERROR;
  1002. SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
  1003. goto f_err;
  1004. }
  1005. if (PACKET_remaining(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
  1006. al = SSL_AD_DECODE_ERROR;
  1007. SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
  1008. goto f_err;
  1009. }
  1010. if (SSL_IS_DTLS(s)) {
  1011. if (!PACKET_get_length_prefixed_1(pkt, &cookie)) {
  1012. al = SSL_AD_DECODE_ERROR;
  1013. SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
  1014. goto f_err;
  1015. }
  1016. /*
  1017. * If we require cookies and this ClientHello doesn't contain one,
  1018. * just return since we do not want to allocate any memory yet.
  1019. * So check cookie length...
  1020. */
  1021. if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
  1022. if (PACKET_remaining(&cookie) == 0)
  1023. return 1;
  1024. }
  1025. }
  1026. if (!PACKET_get_length_prefixed_2(pkt, &cipher_suites)
  1027. || !PACKET_get_length_prefixed_1(pkt, &compression)) {
  1028. al = SSL_AD_DECODE_ERROR;
  1029. SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
  1030. goto f_err;
  1031. }
  1032. /* Could be empty. */
  1033. extensions = *pkt;
  1034. }
  1035. if (SSL_IS_DTLS(s)) {
  1036. /* Empty cookie was already handled above by returning early. */
  1037. if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
  1038. if (s->ctx->app_verify_cookie_cb != NULL) {
  1039. if (s->ctx->app_verify_cookie_cb(s, PACKET_data(&cookie),
  1040. PACKET_remaining(&cookie)) ==
  1041. 0) {
  1042. al = SSL_AD_HANDSHAKE_FAILURE;
  1043. SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
  1044. SSL_R_COOKIE_MISMATCH);
  1045. goto f_err;
  1046. /* else cookie verification succeeded */
  1047. }
  1048. /* default verification */
  1049. } else if (!PACKET_equal(&cookie, s->d1->cookie, s->d1->cookie_len)) {
  1050. al = SSL_AD_HANDSHAKE_FAILURE;
  1051. SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
  1052. goto f_err;
  1053. }
  1054. s->d1->cookie_verified = 1;
  1055. }
  1056. if (s->method->version == DTLS_ANY_VERSION) {
  1057. protverr = ssl_choose_server_version(s);
  1058. if (protverr != 0) {
  1059. SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, protverr);
  1060. s->version = s->client_version;
  1061. al = SSL_AD_PROTOCOL_VERSION;
  1062. goto f_err;
  1063. }
  1064. }
  1065. }
  1066. s->hit = 0;
  1067. /*
  1068. * We don't allow resumption in a backwards compatible ClientHello.
  1069. * TODO(openssl-team): in TLS1.1+, session_id MUST be empty.
  1070. *
  1071. * Versions before 0.9.7 always allow clients to resume sessions in
  1072. * renegotiation. 0.9.7 and later allow this by default, but optionally
  1073. * ignore resumption requests with flag
  1074. * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
  1075. * than a change to default behavior so that applications relying on
  1076. * this for security won't even compile against older library versions).
  1077. * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
  1078. * request renegotiation but not a new session (s->new_session remains
  1079. * unset): for servers, this essentially just means that the
  1080. * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
  1081. * ignored.
  1082. */
  1083. if (is_v2_record ||
  1084. (s->new_session &&
  1085. (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) {
  1086. if (!ssl_get_new_session(s, 1))
  1087. goto err;
  1088. } else {
  1089. i = ssl_get_prev_session(s, &extensions, &session_id);
  1090. /*
  1091. * Only resume if the session's version matches the negotiated
  1092. * version.
  1093. * RFC 5246 does not provide much useful advice on resumption
  1094. * with a different protocol version. It doesn't forbid it but
  1095. * the sanity of such behaviour would be questionable.
  1096. * In practice, clients do not accept a version mismatch and
  1097. * will abort the handshake with an error.
  1098. */
  1099. if (i == 1 && s->version == s->session->ssl_version) {
  1100. /* previous session */
  1101. s->hit = 1;
  1102. } else if (i == -1) {
  1103. goto err;
  1104. } else {
  1105. /* i == 0 */
  1106. if (!ssl_get_new_session(s, 1))
  1107. goto err;
  1108. }
  1109. }
  1110. if (ssl_bytes_to_cipher_list(s, &cipher_suites, &(ciphers),
  1111. is_v2_record, &al) == NULL) {
  1112. goto f_err;
  1113. }
  1114. /* If it is a hit, check that the cipher is in the list */
  1115. if (s->hit) {
  1116. j = 0;
  1117. id = s->session->cipher->id;
  1118. #ifdef CIPHER_DEBUG
  1119. fprintf(stderr, "client sent %d ciphers\n", sk_SSL_CIPHER_num(ciphers));
  1120. #endif
  1121. for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
  1122. c = sk_SSL_CIPHER_value(ciphers, i);
  1123. #ifdef CIPHER_DEBUG
  1124. fprintf(stderr, "client [%2d of %2d]:%s\n",
  1125. i, sk_SSL_CIPHER_num(ciphers), SSL_CIPHER_get_name(c));
  1126. #endif
  1127. if (c->id == id) {
  1128. j = 1;
  1129. break;
  1130. }
  1131. }
  1132. if (j == 0) {
  1133. /*
  1134. * we need to have the cipher in the cipher list if we are asked
  1135. * to reuse it
  1136. */
  1137. al = SSL_AD_ILLEGAL_PARAMETER;
  1138. SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
  1139. SSL_R_REQUIRED_CIPHER_MISSING);
  1140. goto f_err;
  1141. }
  1142. }
  1143. complen = PACKET_remaining(&compression);
  1144. for (j = 0; j < complen; j++) {
  1145. if (PACKET_data(&compression)[j] == 0)
  1146. break;
  1147. }
  1148. if (j >= complen) {
  1149. /* no compress */
  1150. al = SSL_AD_DECODE_ERROR;
  1151. SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_NO_COMPRESSION_SPECIFIED);
  1152. goto f_err;
  1153. }
  1154. /* TLS extensions */
  1155. if (s->version >= SSL3_VERSION) {
  1156. if (!ssl_parse_clienthello_tlsext(s, &extensions)) {
  1157. SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_PARSE_TLSEXT);
  1158. goto err;
  1159. }
  1160. }
  1161. /*
  1162. * Check if we want to use external pre-shared secret for this handshake
  1163. * for not reused session only. We need to generate server_random before
  1164. * calling tls_session_secret_cb in order to allow SessionTicket
  1165. * processing to use it in key derivation.
  1166. */
  1167. {
  1168. unsigned char *pos;
  1169. pos = s->s3->server_random;
  1170. if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE) <= 0) {
  1171. goto f_err;
  1172. }
  1173. }
  1174. #ifndef OPENSSL_NO_GMTLS
  1175. if (!s->hit && (s->version == GMTLS_VERSION || s->version >= TLS1_VERSION)
  1176. && s->tls_session_secret_cb) {
  1177. #else
  1178. if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb) {
  1179. #endif
  1180. const SSL_CIPHER *pref_cipher = NULL;
  1181. s->session->master_key_length = sizeof(s->session->master_key);
  1182. if (s->tls_session_secret_cb(s, s->session->master_key,
  1183. &s->session->master_key_length, ciphers,
  1184. &pref_cipher,
  1185. s->tls_session_secret_cb_arg)) {
  1186. s->hit = 1;
  1187. s->session->ciphers = ciphers;
  1188. s->session->verify_result = X509_V_OK;
  1189. ciphers = NULL;
  1190. /* check if some cipher was preferred by call back */
  1191. pref_cipher =
  1192. pref_cipher ? pref_cipher : ssl3_choose_cipher(s,
  1193. s->
  1194. session->ciphers,
  1195. SSL_get_ciphers
  1196. (s));
  1197. if (pref_cipher == NULL) {
  1198. al = SSL_AD_HANDSHAKE_FAILURE;
  1199. SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER);
  1200. goto f_err;
  1201. }
  1202. s->session->cipher = pref_cipher;
  1203. sk_SSL_CIPHER_free(s->cipher_list);
  1204. s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
  1205. sk_SSL_CIPHER_free(s->cipher_list_by_id);
  1206. s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
  1207. }
  1208. }
  1209. /*
  1210. * Worst case, we will use the NULL compression, but if we have other
  1211. * options, we will now look for them. We have complen-1 compression
  1212. * algorithms from the client, starting at q.
  1213. */
  1214. s->s3->tmp.new_compression = NULL;
  1215. #ifndef OPENSSL_NO_COMP
  1216. /* This only happens if we have a cache hit */
  1217. if (s->session->compress_meth != 0) {
  1218. int m, comp_id = s->session->compress_meth;
  1219. unsigned int k;
  1220. /* Perform sanity checks on resumed compression algorithm */
  1221. /* Can't disable compression */
  1222. if (!ssl_allow_compression(s)) {
  1223. SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
  1224. SSL_R_INCONSISTENT_COMPRESSION);
  1225. goto f_err;
  1226. }
  1227. /* Look for resumed compression method */
  1228. for (m = 0; m < sk_SSL_COMP_num(s->ctx->comp_methods); m++) {
  1229. comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
  1230. if (comp_id == comp->id) {
  1231. s->s3->tmp.new_compression = comp;
  1232. break;
  1233. }
  1234. }
  1235. if (s->s3->tmp.new_compression == NULL) {
  1236. SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
  1237. SSL_R_INVALID_COMPRESSION_ALGORITHM);
  1238. goto f_err;
  1239. }
  1240. /* Look for resumed method in compression list */
  1241. for (k = 0; k < complen; k++) {
  1242. if (PACKET_data(&compression)[k] == comp_id)
  1243. break;
  1244. }
  1245. if (k >= complen) {
  1246. al = SSL_AD_ILLEGAL_PARAMETER;
  1247. SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO,
  1248. SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING);
  1249. goto f_err;
  1250. }
  1251. } else if (s->hit)
  1252. comp = NULL;
  1253. else if (ssl_allow_compression(s) && s->ctx->comp_methods) {
  1254. /* See if we have a match */
  1255. int m, nn, v, done = 0;
  1256. unsigned int o;
  1257. nn = sk_SSL_COMP_num(s->ctx->comp_methods);
  1258. for (m = 0; m < nn; m++) {
  1259. comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
  1260. v = comp->id;
  1261. for (o = 0; o < complen; o++) {
  1262. if (v == PACKET_data(&compression)[o]) {
  1263. done = 1;
  1264. break;
  1265. }
  1266. }
  1267. if (done)
  1268. break;
  1269. }
  1270. if (done)
  1271. s->s3->tmp.new_compression = comp;
  1272. else
  1273. comp = NULL;
  1274. }
  1275. #else
  1276. /*
  1277. * If compression is disabled we'd better not try to resume a session
  1278. * using compression.
  1279. */
  1280. if (s->session->compress_meth != 0) {
  1281. SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
  1282. goto f_err;
  1283. }
  1284. #endif
  1285. /*
  1286. * Given s->session->ciphers and SSL_get_ciphers, we must pick a cipher
  1287. */
  1288. if (!s->hit) {
  1289. #ifdef OPENSSL_NO_COMP
  1290. s->session->compress_meth = 0;
  1291. #else
  1292. s->session->compress_meth = (comp == NULL) ? 0 : comp->id;
  1293. #endif
  1294. sk_SSL_CIPHER_free(s->session->ciphers);
  1295. s->session->ciphers = ciphers;
  1296. if (ciphers == NULL) {
  1297. al = SSL_AD_INTERNAL_ERROR;
  1298. SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
  1299. goto f_err;
  1300. }
  1301. ciphers = NULL;
  1302. if (!tls1_set_server_sigalgs(s)) {
  1303. SSLerr(SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
  1304. goto err;
  1305. }
  1306. }
  1307. sk_SSL_CIPHER_free(ciphers);
  1308. return MSG_PROCESS_CONTINUE_PROCESSING;
  1309. f_err:
  1310. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  1311. err:
  1312. ossl_statem_set_error(s);
  1313. sk_SSL_CIPHER_free(ciphers);
  1314. return MSG_PROCESS_ERROR;
  1315. }
  1316. WORK_STATE tls_post_process_client_hello(SSL *s, WORK_STATE wst)
  1317. {
  1318. int al = SSL_AD_HANDSHAKE_FAILURE;
  1319. const SSL_CIPHER *cipher;
  1320. if (wst == WORK_MORE_A) {
  1321. if (!s->hit) {
  1322. /* Let cert callback update server certificates if required */
  1323. if (s->cert->cert_cb) {
  1324. int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg);
  1325. if (rv == 0) {
  1326. al = SSL_AD_INTERNAL_ERROR;
  1327. SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
  1328. SSL_R_CERT_CB_ERROR);
  1329. goto f_err;
  1330. }
  1331. if (rv < 0) {
  1332. s->rwstate = SSL_X509_LOOKUP;
  1333. return WORK_MORE_A;
  1334. }
  1335. s->rwstate = SSL_NOTHING;
  1336. }
  1337. cipher =
  1338. ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
  1339. if (cipher == NULL) {
  1340. SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
  1341. SSL_R_NO_SHARED_CIPHER);
  1342. goto f_err;
  1343. }
  1344. s->s3->tmp.new_cipher = cipher;
  1345. /* check whether we should disable session resumption */
  1346. if (s->not_resumable_session_cb != NULL)
  1347. s->session->not_resumable = s->not_resumable_session_cb(s,
  1348. ((cipher->algorithm_mkey & (SSL_kDHE | SSL_kECDHE | SSL_kSM2DHE)) != 0));
  1349. if (s->session->not_resumable)
  1350. /* do not send a session ticket */
  1351. s->tlsext_ticket_expected = 0;
  1352. } else {
  1353. /* Session-id reuse */
  1354. s->s3->tmp.new_cipher = s->session->cipher;
  1355. }
  1356. if (!(s->verify_mode & SSL_VERIFY_PEER)) {
  1357. if (!ssl3_digest_cached_records(s, 0)) {
  1358. al = SSL_AD_INTERNAL_ERROR;
  1359. goto f_err;
  1360. }
  1361. }
  1362. /*-
  1363. * we now have the following setup.
  1364. * client_random
  1365. * cipher_list - our preferred list of ciphers
  1366. * ciphers - the clients preferred list of ciphers
  1367. * compression - basically ignored right now
  1368. * ssl version is set - sslv3
  1369. * s->session - The ssl session has been setup.
  1370. * s->hit - session reuse flag
  1371. * s->s3->tmp.new_cipher- the new cipher to use.
  1372. */
  1373. /* Handles TLS extensions that we couldn't check earlier */
  1374. if (s->version >= SSL3_VERSION) {
  1375. if (!ssl_check_clienthello_tlsext_late(s, &al)) {
  1376. SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
  1377. SSL_R_CLIENTHELLO_TLSEXT);
  1378. goto f_err;
  1379. }
  1380. }
  1381. wst = WORK_MORE_B;
  1382. }
  1383. #ifndef OPENSSL_NO_SRP
  1384. if (wst == WORK_MORE_B) {
  1385. int ret;
  1386. if ((ret = ssl_check_srp_ext_ClientHello(s, &al)) < 0) {
  1387. /*
  1388. * callback indicates further work to be done
  1389. */
  1390. s->rwstate = SSL_X509_LOOKUP;
  1391. return WORK_MORE_B;
  1392. }
  1393. if (ret != SSL_ERROR_NONE) {
  1394. /*
  1395. * This is not really an error but the only means to for
  1396. * a client to detect whether srp is supported.
  1397. */
  1398. if (al != TLS1_AD_UNKNOWN_PSK_IDENTITY)
  1399. SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
  1400. SSL_R_CLIENTHELLO_TLSEXT);
  1401. else
  1402. SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
  1403. SSL_R_PSK_IDENTITY_NOT_FOUND);
  1404. goto f_err;
  1405. }
  1406. }
  1407. #endif
  1408. s->renegotiate = 2;
  1409. return WORK_FINISHED_STOP;
  1410. f_err:
  1411. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  1412. ossl_statem_set_error(s);
  1413. return WORK_ERROR;
  1414. }
  1415. int tls_construct_server_hello(SSL *s)
  1416. {
  1417. unsigned char *buf;
  1418. unsigned char *p, *d;
  1419. int i, sl;
  1420. int al = 0;
  1421. unsigned long l;
  1422. buf = (unsigned char *)s->init_buf->data;
  1423. /* Do the message type and length last */
  1424. d = p = ssl_handshake_start(s);
  1425. *(p++) = s->version >> 8;
  1426. *(p++) = s->version & 0xff;
  1427. /*
  1428. * Random stuff. Filling of the server_random takes place in
  1429. * tls_process_client_hello()
  1430. */
  1431. memcpy(p, s->s3->server_random, SSL3_RANDOM_SIZE);
  1432. p += SSL3_RANDOM_SIZE;
  1433. /*-
  1434. * There are several cases for the session ID to send
  1435. * back in the server hello:
  1436. * - For session reuse from the session cache,
  1437. * we send back the old session ID.
  1438. * - If stateless session reuse (using a session ticket)
  1439. * is successful, we send back the client's "session ID"
  1440. * (which doesn't actually identify the session).
  1441. * - If it is a new session, we send back the new
  1442. * session ID.
  1443. * - However, if we want the new session to be single-use,
  1444. * we send back a 0-length session ID.
  1445. * s->hit is non-zero in either case of session reuse,
  1446. * so the following won't overwrite an ID that we're supposed
  1447. * to send back.
  1448. */
  1449. if (s->session->not_resumable ||
  1450. (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)
  1451. && !s->hit))
  1452. s->session->session_id_length = 0;
  1453. sl = s->session->session_id_length;
  1454. if (sl > (int)sizeof(s->session->session_id)) {
  1455. SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
  1456. ossl_statem_set_error(s);
  1457. return 0;
  1458. }
  1459. *(p++) = sl;
  1460. memcpy(p, s->session->session_id, sl);
  1461. p += sl;
  1462. /* put the cipher */
  1463. i = ssl3_put_cipher_by_char(s->s3->tmp.new_cipher, p);
  1464. p += i;
  1465. /* put the compression method */
  1466. #ifdef OPENSSL_NO_COMP
  1467. *(p++) = 0;
  1468. #else
  1469. if (s->s3->tmp.new_compression == NULL)
  1470. *(p++) = 0;
  1471. else
  1472. *(p++) = s->s3->tmp.new_compression->id;
  1473. #endif
  1474. if (ssl_prepare_serverhello_tlsext(s) <= 0) {
  1475. SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, SSL_R_SERVERHELLO_TLSEXT);
  1476. ossl_statem_set_error(s);
  1477. return 0;
  1478. }
  1479. if ((s->version != GMTLS_VERSION) && (p =
  1480. ssl_add_serverhello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
  1481. &al)) == NULL) {
  1482. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  1483. SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
  1484. ossl_statem_set_error(s);
  1485. return 0;
  1486. }
  1487. /* do the header */
  1488. l = (p - d);
  1489. if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_HELLO, l)) {
  1490. SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
  1491. ossl_statem_set_error(s);
  1492. return 0;
  1493. }
  1494. return 1;
  1495. }
  1496. int tls_construct_server_done(SSL *s)
  1497. {
  1498. if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_DONE, 0)) {
  1499. SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_DONE, ERR_R_INTERNAL_ERROR);
  1500. ossl_statem_set_error(s);
  1501. return 0;
  1502. }
  1503. if (!s->s3->tmp.cert_request) {
  1504. if (!ssl3_digest_cached_records(s, 0)) {
  1505. ossl_statem_set_error(s);
  1506. }
  1507. }
  1508. return 1;
  1509. }
  1510. int tls_construct_server_key_exchange(SSL *s)
  1511. {
  1512. #ifndef OPENSSL_NO_DH
  1513. EVP_PKEY *pkdh = NULL;
  1514. int j;
  1515. #endif
  1516. #ifndef OPENSSL_NO_EC
  1517. unsigned char *encodedPoint = NULL;
  1518. int encodedlen = 0;
  1519. int curve_id = 0;
  1520. #endif
  1521. EVP_PKEY *pkey;
  1522. const EVP_MD *md = NULL;
  1523. unsigned char *p, *d;
  1524. int al, i;
  1525. unsigned long type;
  1526. int n;
  1527. const BIGNUM *r[4];
  1528. int nr[4], kn;
  1529. BUF_MEM *buf;
  1530. EVP_MD_CTX *md_ctx = NULL;
  1531. if (!(md_ctx = EVP_MD_CTX_new())) {
  1532. SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
  1533. al = SSL_AD_INTERNAL_ERROR;
  1534. goto f_err;
  1535. }
  1536. type = s->s3->tmp.new_cipher->algorithm_mkey;
  1537. buf = s->init_buf;
  1538. r[0] = r[1] = r[2] = r[3] = NULL;
  1539. n = 0;
  1540. #ifndef OPENSSL_NO_PSK
  1541. if (type & SSL_PSK) {
  1542. /*
  1543. * reserve size for record length and PSK identity hint
  1544. */
  1545. n += 2;
  1546. if (s->cert->psk_identity_hint)
  1547. n += strlen(s->cert->psk_identity_hint);
  1548. }
  1549. /* Plain PSK or RSAPSK nothing to do */
  1550. if (type & (SSL_kPSK | SSL_kRSAPSK)) {
  1551. } else
  1552. #endif /* !OPENSSL_NO_PSK */
  1553. #ifndef OPENSSL_NO_DH
  1554. if (type & (SSL_kDHE | SSL_kDHEPSK)) {
  1555. CERT *cert = s->cert;
  1556. EVP_PKEY *pkdhp = NULL;
  1557. DH *dh;
  1558. if (s->cert->dh_tmp_auto) {
  1559. DH *dhp = ssl_get_auto_dh(s);
  1560. pkdh = EVP_PKEY_new();
  1561. if (pkdh == NULL || dhp == NULL) {
  1562. DH_free(dhp);
  1563. al = SSL_AD_INTERNAL_ERROR;
  1564. SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
  1565. ERR_R_INTERNAL_ERROR);
  1566. goto f_err;
  1567. }
  1568. EVP_PKEY_assign_DH(pkdh, dhp);
  1569. pkdhp = pkdh;
  1570. } else {
  1571. pkdhp = cert->dh_tmp;
  1572. }
  1573. if ((pkdhp == NULL) && (s->cert->dh_tmp_cb != NULL)) {
  1574. DH *dhp = s->cert->dh_tmp_cb(s, 0, 1024);
  1575. pkdh = ssl_dh_to_pkey(dhp);
  1576. if (pkdh == NULL) {
  1577. al = SSL_AD_INTERNAL_ERROR;
  1578. SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
  1579. ERR_R_INTERNAL_ERROR);
  1580. goto f_err;
  1581. }
  1582. pkdhp = pkdh;
  1583. }
  1584. if (pkdhp == NULL) {
  1585. al = SSL_AD_HANDSHAKE_FAILURE;
  1586. SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
  1587. SSL_R_MISSING_TMP_DH_KEY);
  1588. goto f_err;
  1589. }
  1590. if (!ssl_security(s, SSL_SECOP_TMP_DH,
  1591. EVP_PKEY_security_bits(pkdhp), 0, pkdhp)) {
  1592. al = SSL_AD_HANDSHAKE_FAILURE;
  1593. SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
  1594. SSL_R_DH_KEY_TOO_SMALL);
  1595. goto f_err;
  1596. }
  1597. if (s->s3->tmp.pkey != NULL) {
  1598. SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
  1599. ERR_R_INTERNAL_ERROR);
  1600. goto err;
  1601. }
  1602. s->s3->tmp.pkey = ssl_generate_pkey(pkdhp);
  1603. if (s->s3->tmp.pkey == NULL) {
  1604. SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_EVP_LIB);
  1605. goto err;
  1606. }
  1607. dh = EVP_PKEY_get0_DH(s->s3->tmp.pkey);
  1608. EVP_PKEY_free(pkdh);
  1609. pkdh = NULL;
  1610. DH_get0_pqg(dh, &r[0], NULL, &r[1]);
  1611. DH_get0_key(dh, &r[2], NULL);
  1612. } else
  1613. #endif
  1614. #ifndef OPENSSL_NO_EC
  1615. if (type & (SSL_kECDHE | SSL_kECDHEPSK | SSL_kSM2DHE | SSL_kSM2PSK)) {
  1616. int nid;
  1617. if (s->s3->tmp.pkey != NULL) {
  1618. SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
  1619. ERR_R_INTERNAL_ERROR);
  1620. goto err;
  1621. }
  1622. /* Get NID of appropriate shared curve */
  1623. nid = tls1_shared_curve(s, -2);
  1624. curve_id = tls1_ec_nid2curve_id(nid);
  1625. if (curve_id == 0) {
  1626. SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
  1627. SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
  1628. goto err;
  1629. }
  1630. s->s3->tmp.pkey = ssl_generate_pkey_curve(curve_id);
  1631. /* Generate a new key for this curve */
  1632. if (s->s3->tmp.pkey == NULL) {
  1633. al = SSL_AD_INTERNAL_ERROR;
  1634. SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_EVP_LIB);
  1635. goto f_err;
  1636. }
  1637. /* Encode the public key. */
  1638. encodedlen = EVP_PKEY_get1_tls_encodedpoint(s->s3->tmp.pkey,
  1639. &encodedPoint);
  1640. if (encodedlen == 0) {
  1641. SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_EC_LIB);
  1642. goto err;
  1643. }
  1644. /*
  1645. * We only support named (not generic) curves in ECDH ephemeral key
  1646. * exchanges. In this situation, we need four additional bytes to
  1647. * encode the entire ServerECDHParams structure.
  1648. */
  1649. n += 4 + encodedlen;
  1650. /*
  1651. * We'll generate the serverKeyExchange message explicitly so we
  1652. * can set these to NULLs
  1653. */
  1654. r[0] = NULL;
  1655. r[1] = NULL;
  1656. r[2] = NULL;
  1657. r[3] = NULL;
  1658. } else
  1659. #endif /* !OPENSSL_NO_EC */
  1660. #ifndef OPENSSL_NO_SRP
  1661. if (type & SSL_kSRP) {
  1662. if ((s->srp_ctx.N == NULL) ||
  1663. (s->srp_ctx.g == NULL) ||
  1664. (s->srp_ctx.s == NULL) || (s->srp_ctx.B == NULL)) {
  1665. SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
  1666. SSL_R_MISSING_SRP_PARAM);
  1667. goto err;
  1668. }
  1669. r[0] = s->srp_ctx.N;
  1670. r[1] = s->srp_ctx.g;
  1671. r[2] = s->srp_ctx.s;
  1672. r[3] = s->srp_ctx.B;
  1673. } else
  1674. #endif
  1675. {
  1676. al = SSL_AD_HANDSHAKE_FAILURE;
  1677. SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
  1678. SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
  1679. goto f_err;
  1680. }
  1681. for (i = 0; i < 4 && r[i] != NULL; i++) {
  1682. nr[i] = BN_num_bytes(r[i]);
  1683. #ifndef OPENSSL_NO_SRP
  1684. if ((i == 2) && (type & SSL_kSRP))
  1685. n += 1 + nr[i];
  1686. else
  1687. #endif
  1688. #ifndef OPENSSL_NO_DH
  1689. /*-
  1690. * for interoperability with some versions of the Microsoft TLS
  1691. * stack, we need to zero pad the DHE pub key to the same length
  1692. * as the prime, so use the length of the prime here
  1693. */
  1694. if ((i == 2) && (type & (SSL_kDHE | SSL_kDHEPSK)))
  1695. n += 2 + nr[0];
  1696. else
  1697. #endif
  1698. n += 2 + nr[i];
  1699. }
  1700. if (!(s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
  1701. && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)) {
  1702. if ((pkey = ssl_get_sign_pkey(s, s->s3->tmp.new_cipher, &md))
  1703. == NULL) {
  1704. al = SSL_AD_DECODE_ERROR;
  1705. goto f_err;
  1706. }
  1707. kn = EVP_PKEY_size(pkey);
  1708. /* Allow space for signature algorithm */
  1709. if (SSL_USE_SIGALGS(s))
  1710. kn += 2;
  1711. /* Allow space for signature length */
  1712. kn += 2;
  1713. } else {
  1714. pkey = NULL;
  1715. kn = 0;
  1716. }
  1717. if (!BUF_MEM_grow_clean(buf, n + SSL_HM_HEADER_LENGTH(s) + kn)) {
  1718. SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_LIB_BUF);
  1719. goto err;
  1720. }
  1721. d = p = ssl_handshake_start(s);
  1722. #ifndef OPENSSL_NO_PSK
  1723. if (type & SSL_PSK) {
  1724. /* copy PSK identity hint */
  1725. if (s->cert->psk_identity_hint) {
  1726. size_t len = strlen(s->cert->psk_identity_hint);
  1727. if (len > PSK_MAX_IDENTITY_LEN) {
  1728. /*
  1729. * Should not happen - we already checked this when we set
  1730. * the identity hint
  1731. */
  1732. SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
  1733. ERR_R_INTERNAL_ERROR);
  1734. goto err;
  1735. }
  1736. s2n(len, p);
  1737. memcpy(p, s->cert->psk_identity_hint, len);
  1738. p += len;
  1739. } else {
  1740. s2n(0, p);
  1741. }
  1742. }
  1743. #endif
  1744. for (i = 0; i < 4 && r[i] != NULL; i++) {
  1745. #ifndef OPENSSL_NO_SRP
  1746. if ((i == 2) && (type & SSL_kSRP)) {
  1747. *p = nr[i];
  1748. p++;
  1749. } else
  1750. #endif
  1751. #ifndef OPENSSL_NO_DH
  1752. /*-
  1753. * for interoperability with some versions of the Microsoft TLS
  1754. * stack, we need to zero pad the DHE pub key to the same length
  1755. * as the prime
  1756. */
  1757. if ((i == 2) && (type & (SSL_kDHE | SSL_kDHEPSK))) {
  1758. s2n(nr[0], p);
  1759. for (j = 0; j < (nr[0] - nr[2]); ++j) {
  1760. *p = 0;
  1761. ++p;
  1762. }
  1763. } else
  1764. #endif
  1765. s2n(nr[i], p);
  1766. BN_bn2bin(r[i], p);
  1767. p += nr[i];
  1768. }
  1769. #ifndef OPENSSL_NO_EC
  1770. if (type & (SSL_kECDHE | SSL_kECDHEPSK | SSL_kSM2DHE | SSL_kSM2PSK)) {
  1771. /*
  1772. * XXX: For now, we only support named (not generic) curves. In
  1773. * this situation, the serverKeyExchange message has: [1 byte
  1774. * CurveType], [2 byte CurveName] [1 byte length of encoded
  1775. * point], followed by the actual encoded point itself
  1776. */
  1777. *p = NAMED_CURVE_TYPE;
  1778. p += 1;
  1779. *p = 0;
  1780. p += 1;
  1781. *p = curve_id;
  1782. p += 1;
  1783. *p = encodedlen;
  1784. p += 1;
  1785. memcpy(p, encodedPoint, encodedlen);
  1786. OPENSSL_free(encodedPoint);
  1787. encodedPoint = NULL;
  1788. p += encodedlen;
  1789. }
  1790. #endif
  1791. /* not anonymous */
  1792. if (pkey != NULL) {
  1793. /*
  1794. * n is the length of the params, they start at &(d[4]) and p
  1795. * points to the space at the end.
  1796. */
  1797. if (md) {
  1798. /* send signature algorithm */
  1799. if (SSL_USE_SIGALGS(s)) {
  1800. if (!tls12_get_sigandhash(p, pkey, md)) {
  1801. /* Should never happen */
  1802. al = SSL_AD_INTERNAL_ERROR;
  1803. SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
  1804. ERR_R_INTERNAL_ERROR);
  1805. goto f_err;
  1806. }
  1807. p += 2;
  1808. }
  1809. #ifdef SSL_DEBUG
  1810. fprintf(stderr, "Using hash %s\n", EVP_MD_name(md));
  1811. #endif
  1812. if (EVP_SignInit_ex(md_ctx, md, NULL) <= 0) {
  1813. SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_LIB_EVP);
  1814. al = SSL_AD_INTERNAL_ERROR;
  1815. goto f_err;
  1816. }
  1817. #ifndef OPENSSL_NO_SM2
  1818. if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aSM2) {
  1819. unsigned char z[EVP_MAX_MD_SIZE];
  1820. size_t zlen = sizeof(z);
  1821. char *id = SM2_DEFAULT_ID;
  1822. if (!SM2_compute_id_digest(md, id, strlen(id), z, &zlen,
  1823. EVP_PKEY_get0_EC_KEY(pkey))) {
  1824. SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_LIB_SM2);
  1825. al = SSL_AD_INTERNAL_ERROR;
  1826. goto f_err;
  1827. }
  1828. if (EVP_SignUpdate(md_ctx, z, zlen) <= 0) {
  1829. SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_LIB_SM2);
  1830. al = SSL_AD_INTERNAL_ERROR;
  1831. goto f_err;
  1832. }
  1833. }
  1834. #endif
  1835. if (EVP_SignUpdate(md_ctx, &(s->s3->client_random[0]),
  1836. SSL3_RANDOM_SIZE) <= 0
  1837. || EVP_SignUpdate(md_ctx, &(s->s3->server_random[0]),
  1838. SSL3_RANDOM_SIZE) <= 0
  1839. || EVP_SignUpdate(md_ctx, d, n) <= 0
  1840. || EVP_SignFinal(md_ctx, &(p[2]),
  1841. (unsigned int *)&i, pkey) <= 0) {
  1842. SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_LIB_EVP);
  1843. al = SSL_AD_INTERNAL_ERROR;
  1844. goto f_err;
  1845. }
  1846. s2n(i, p);
  1847. n += i + 2;
  1848. if (SSL_USE_SIGALGS(s)) {
  1849. n += 2;
  1850. }
  1851. } else {
  1852. /* Is this error check actually needed? */
  1853. al = SSL_AD_HANDSHAKE_FAILURE;
  1854. SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
  1855. SSL_R_UNKNOWN_PKEY_TYPE);
  1856. goto f_err;
  1857. }
  1858. }
  1859. if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_KEY_EXCHANGE, n)) {
  1860. al = SSL_AD_HANDSHAKE_FAILURE;
  1861. SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  1862. goto f_err;
  1863. }
  1864. EVP_MD_CTX_free(md_ctx);
  1865. return 1;
  1866. f_err:
  1867. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  1868. err:
  1869. #ifndef OPENSSL_NO_DH
  1870. EVP_PKEY_free(pkdh);
  1871. #endif
  1872. #ifndef OPENSSL_NO_EC
  1873. OPENSSL_free(encodedPoint);
  1874. #endif
  1875. EVP_MD_CTX_free(md_ctx);
  1876. ossl_statem_set_error(s);
  1877. return 0;
  1878. }
  1879. int tls_construct_certificate_request(SSL *s)
  1880. {
  1881. unsigned char *p, *d;
  1882. int i, j, nl, off, n;
  1883. STACK_OF(X509_NAME) *sk = NULL;
  1884. X509_NAME *name;
  1885. BUF_MEM *buf;
  1886. buf = s->init_buf;
  1887. d = p = ssl_handshake_start(s);
  1888. /* get the list of acceptable cert types */
  1889. p++;
  1890. n = ssl3_get_req_cert_type(s, p);
  1891. d[0] = n;
  1892. p += n;
  1893. n++;
  1894. if (SSL_USE_SIGALGS(s)) {
  1895. const unsigned char *psigs;
  1896. unsigned char *etmp = p;
  1897. nl = tls12_get_psigalgs(s, &psigs);
  1898. /* Skip over length for now */
  1899. p += 2;
  1900. nl = tls12_copy_sigalgs(s, p, psigs, nl);
  1901. /* Now fill in length */
  1902. s2n(nl, etmp);
  1903. p += nl;
  1904. n += nl + 2;
  1905. }
  1906. off = n;
  1907. p += 2;
  1908. n += 2;
  1909. sk = SSL_get_client_CA_list(s);
  1910. nl = 0;
  1911. if (sk != NULL) {
  1912. for (i = 0; i < sk_X509_NAME_num(sk); i++) {
  1913. name = sk_X509_NAME_value(sk, i);
  1914. j = i2d_X509_NAME(name, NULL);
  1915. if (!BUF_MEM_grow_clean(buf, SSL_HM_HEADER_LENGTH(s) + n + j + 2)) {
  1916. SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST, ERR_R_BUF_LIB);
  1917. goto err;
  1918. }
  1919. p = ssl_handshake_start(s) + n;
  1920. s2n(j, p);
  1921. i2d_X509_NAME(name, &p);
  1922. n += 2 + j;
  1923. nl += 2 + j;
  1924. }
  1925. }
  1926. /* else no CA names */
  1927. p = ssl_handshake_start(s) + off;
  1928. s2n(nl, p);
  1929. if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_REQUEST, n)) {
  1930. SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR);
  1931. goto err;
  1932. }
  1933. s->s3->tmp.cert_request = 1;
  1934. return 1;
  1935. err:
  1936. ossl_statem_set_error(s);
  1937. return 0;
  1938. }
  1939. static int tls_process_cke_psk_preamble(SSL *s, PACKET *pkt, int *al)
  1940. {
  1941. #ifndef OPENSSL_NO_PSK
  1942. unsigned char psk[PSK_MAX_PSK_LEN];
  1943. size_t psklen;
  1944. PACKET psk_identity;
  1945. if (!PACKET_get_length_prefixed_2(pkt, &psk_identity)) {
  1946. *al = SSL_AD_DECODE_ERROR;
  1947. SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, SSL_R_LENGTH_MISMATCH);
  1948. return 0;
  1949. }
  1950. if (PACKET_remaining(&psk_identity) > PSK_MAX_IDENTITY_LEN) {
  1951. *al = SSL_AD_DECODE_ERROR;
  1952. SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, SSL_R_DATA_LENGTH_TOO_LONG);
  1953. return 0;
  1954. }
  1955. if (s->psk_server_callback == NULL) {
  1956. *al = SSL_AD_INTERNAL_ERROR;
  1957. SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, SSL_R_PSK_NO_SERVER_CB);
  1958. return 0;
  1959. }
  1960. if (!PACKET_strndup(&psk_identity, &s->session->psk_identity)) {
  1961. *al = SSL_AD_INTERNAL_ERROR;
  1962. SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
  1963. return 0;
  1964. }
  1965. psklen = s->psk_server_callback(s, s->session->psk_identity,
  1966. psk, sizeof(psk));
  1967. if (psklen > PSK_MAX_PSK_LEN) {
  1968. *al = SSL_AD_INTERNAL_ERROR;
  1969. SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
  1970. return 0;
  1971. } else if (psklen == 0) {
  1972. /*
  1973. * PSK related to the given identity not found
  1974. */
  1975. *al = SSL_AD_UNKNOWN_PSK_IDENTITY;
  1976. SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
  1977. SSL_R_PSK_IDENTITY_NOT_FOUND);
  1978. return 0;
  1979. }
  1980. OPENSSL_free(s->s3->tmp.psk);
  1981. s->s3->tmp.psk = OPENSSL_memdup(psk, psklen);
  1982. OPENSSL_cleanse(psk, psklen);
  1983. if (s->s3->tmp.psk == NULL) {
  1984. *al = SSL_AD_INTERNAL_ERROR;
  1985. SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, ERR_R_MALLOC_FAILURE);
  1986. return 0;
  1987. }
  1988. s->s3->tmp.psklen = psklen;
  1989. return 1;
  1990. #else
  1991. /* Should never happen */
  1992. *al = SSL_AD_INTERNAL_ERROR;
  1993. SSLerr(SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, ERR_R_INTERNAL_ERROR);
  1994. return 0;
  1995. #endif
  1996. }
  1997. static int tls_process_cke_rsa(SSL *s, PACKET *pkt, int *al)
  1998. {
  1999. #ifndef OPENSSL_NO_RSA
  2000. unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
  2001. int decrypt_len;
  2002. unsigned char decrypt_good, version_good;
  2003. size_t j, padding_len;
  2004. PACKET enc_premaster;
  2005. RSA *rsa = NULL;
  2006. unsigned char *rsa_decrypt = NULL;
  2007. int ret = 0;
  2008. rsa = EVP_PKEY_get0_RSA(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey);
  2009. if (rsa == NULL) {
  2010. *al = SSL_AD_HANDSHAKE_FAILURE;
  2011. SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, SSL_R_MISSING_RSA_CERTIFICATE);
  2012. return 0;
  2013. }
  2014. /* SSLv3 and pre-standard DTLS omit the length bytes. */
  2015. if (s->version == SSL3_VERSION || s->version == DTLS1_BAD_VER) {
  2016. enc_premaster = *pkt;
  2017. } else {
  2018. if (!PACKET_get_length_prefixed_2(pkt, &enc_premaster)
  2019. || PACKET_remaining(pkt) != 0) {
  2020. *al = SSL_AD_DECODE_ERROR;
  2021. SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, SSL_R_LENGTH_MISMATCH);
  2022. return 0;
  2023. }
  2024. }
  2025. /*
  2026. * We want to be sure that the plaintext buffer size makes it safe to
  2027. * iterate over the entire size of a premaster secret
  2028. * (SSL_MAX_MASTER_KEY_LENGTH). Reject overly short RSA keys because
  2029. * their ciphertext cannot accommodate a premaster secret anyway.
  2030. */
  2031. if (RSA_size(rsa) < SSL_MAX_MASTER_KEY_LENGTH) {
  2032. *al = SSL_AD_INTERNAL_ERROR;
  2033. SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, RSA_R_KEY_SIZE_TOO_SMALL);
  2034. return 0;
  2035. }
  2036. rsa_decrypt = OPENSSL_malloc(RSA_size(rsa));
  2037. if (rsa_decrypt == NULL) {
  2038. *al = SSL_AD_INTERNAL_ERROR;
  2039. SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, ERR_R_MALLOC_FAILURE);
  2040. return 0;
  2041. }
  2042. /*
  2043. * We must not leak whether a decryption failure occurs because of
  2044. * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
  2045. * section 7.4.7.1). The code follows that advice of the TLS RFC and
  2046. * generates a random premaster secret for the case that the decrypt
  2047. * fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
  2048. */
  2049. if (RAND_bytes(rand_premaster_secret, sizeof(rand_premaster_secret)) <= 0)
  2050. goto err;
  2051. /*
  2052. * Decrypt with no padding. PKCS#1 padding will be removed as part of
  2053. * the timing-sensitive code below.
  2054. */
  2055. decrypt_len = RSA_private_decrypt(PACKET_remaining(&enc_premaster),
  2056. PACKET_data(&enc_premaster),
  2057. rsa_decrypt, rsa, RSA_NO_PADDING);
  2058. if (decrypt_len < 0)
  2059. goto err;
  2060. /* Check the padding. See RFC 3447, section 7.2.2. */
  2061. /*
  2062. * The smallest padded premaster is 11 bytes of overhead. Small keys
  2063. * are publicly invalid, so this may return immediately. This ensures
  2064. * PS is at least 8 bytes.
  2065. */
  2066. if (decrypt_len < 11 + SSL_MAX_MASTER_KEY_LENGTH) {
  2067. *al = SSL_AD_DECRYPT_ERROR;
  2068. SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, SSL_R_DECRYPTION_FAILED);
  2069. goto err;
  2070. }
  2071. padding_len = decrypt_len - SSL_MAX_MASTER_KEY_LENGTH;
  2072. decrypt_good = constant_time_eq_int_8(rsa_decrypt[0], 0) &
  2073. constant_time_eq_int_8(rsa_decrypt[1], 2);
  2074. for (j = 2; j < padding_len - 1; j++) {
  2075. decrypt_good &= ~constant_time_is_zero_8(rsa_decrypt[j]);
  2076. }
  2077. decrypt_good &= constant_time_is_zero_8(rsa_decrypt[padding_len - 1]);
  2078. /*
  2079. * If the version in the decrypted pre-master secret is correct then
  2080. * version_good will be 0xff, otherwise it'll be zero. The
  2081. * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
  2082. * (http://eprint.iacr.org/2003/052/) exploits the version number
  2083. * check as a "bad version oracle". Thus version checks are done in
  2084. * constant time and are treated like any other decryption error.
  2085. */
  2086. version_good =
  2087. constant_time_eq_8(rsa_decrypt[padding_len],
  2088. (unsigned)(s->client_version >> 8));
  2089. version_good &=
  2090. constant_time_eq_8(rsa_decrypt[padding_len + 1],
  2091. (unsigned)(s->client_version & 0xff));
  2092. /*
  2093. * The premaster secret must contain the same version number as the
  2094. * ClientHello to detect version rollback attacks (strangely, the
  2095. * protocol does not offer such protection for DH ciphersuites).
  2096. * However, buggy clients exist that send the negotiated protocol
  2097. * version instead if the server does not support the requested
  2098. * protocol version. If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such
  2099. * clients.
  2100. */
  2101. if (s->options & SSL_OP_TLS_ROLLBACK_BUG) {
  2102. unsigned char workaround_good;
  2103. workaround_good = constant_time_eq_8(rsa_decrypt[padding_len],
  2104. (unsigned)(s->version >> 8));
  2105. workaround_good &=
  2106. constant_time_eq_8(rsa_decrypt[padding_len + 1],
  2107. (unsigned)(s->version & 0xff));
  2108. version_good |= workaround_good;
  2109. }
  2110. /*
  2111. * Both decryption and version must be good for decrypt_good to
  2112. * remain non-zero (0xff).
  2113. */
  2114. decrypt_good &= version_good;
  2115. /*
  2116. * Now copy rand_premaster_secret over from p using
  2117. * decrypt_good_mask. If decryption failed, then p does not
  2118. * contain valid plaintext, however, a check above guarantees
  2119. * it is still sufficiently large to read from.
  2120. */
  2121. for (j = 0; j < sizeof(rand_premaster_secret); j++) {
  2122. rsa_decrypt[padding_len + j] =
  2123. constant_time_select_8(decrypt_good,
  2124. rsa_decrypt[padding_len + j],
  2125. rand_premaster_secret[j]);
  2126. }
  2127. if (!ssl_generate_master_secret(s, rsa_decrypt + padding_len,
  2128. sizeof(rand_premaster_secret), 0)) {
  2129. *al = SSL_AD_INTERNAL_ERROR;
  2130. SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, ERR_R_INTERNAL_ERROR);
  2131. goto err;
  2132. }
  2133. ret = 1;
  2134. err:
  2135. OPENSSL_free(rsa_decrypt);
  2136. return ret;
  2137. #else
  2138. /* Should never happen */
  2139. *al = SSL_AD_INTERNAL_ERROR;
  2140. SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, ERR_R_INTERNAL_ERROR);
  2141. return 0;
  2142. #endif
  2143. }
  2144. static int tls_process_cke_dhe(SSL *s, PACKET *pkt, int *al)
  2145. {
  2146. #ifndef OPENSSL_NO_DH
  2147. EVP_PKEY *skey = NULL;
  2148. DH *cdh;
  2149. unsigned int i;
  2150. BIGNUM *pub_key;
  2151. const unsigned char *data;
  2152. EVP_PKEY *ckey = NULL;
  2153. int ret = 0;
  2154. if (!PACKET_get_net_2(pkt, &i) || PACKET_remaining(pkt) != i) {
  2155. *al = SSL_AD_HANDSHAKE_FAILURE;
  2156. SSLerr(SSL_F_TLS_PROCESS_CKE_DHE,
  2157. SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
  2158. goto err;
  2159. }
  2160. skey = s->s3->tmp.pkey;
  2161. if (skey == NULL) {
  2162. *al = SSL_AD_HANDSHAKE_FAILURE;
  2163. SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, SSL_R_MISSING_TMP_DH_KEY);
  2164. goto err;
  2165. }
  2166. if (PACKET_remaining(pkt) == 0L) {
  2167. *al = SSL_AD_HANDSHAKE_FAILURE;
  2168. SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, SSL_R_MISSING_TMP_DH_KEY);
  2169. goto err;
  2170. }
  2171. if (!PACKET_get_bytes(pkt, &data, i)) {
  2172. /* We already checked we have enough data */
  2173. *al = SSL_AD_INTERNAL_ERROR;
  2174. SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, ERR_R_INTERNAL_ERROR);
  2175. goto err;
  2176. }
  2177. ckey = EVP_PKEY_new();
  2178. if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) == 0) {
  2179. SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, SSL_R_BN_LIB);
  2180. goto err;
  2181. }
  2182. cdh = EVP_PKEY_get0_DH(ckey);
  2183. pub_key = BN_bin2bn(data, i, NULL);
  2184. if (pub_key == NULL || !DH_set0_key(cdh, pub_key, NULL)) {
  2185. SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, ERR_R_INTERNAL_ERROR);
  2186. if (pub_key != NULL)
  2187. BN_free(pub_key);
  2188. goto err;
  2189. }
  2190. if (ssl_derive(s, skey, ckey) == 0) {
  2191. *al = SSL_AD_INTERNAL_ERROR;
  2192. SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, ERR_R_INTERNAL_ERROR);
  2193. goto err;
  2194. }
  2195. ret = 1;
  2196. EVP_PKEY_free(s->s3->tmp.pkey);
  2197. s->s3->tmp.pkey = NULL;
  2198. err:
  2199. EVP_PKEY_free(ckey);
  2200. return ret;
  2201. #else
  2202. /* Should never happen */
  2203. *al = SSL_AD_INTERNAL_ERROR;
  2204. SSLerr(SSL_F_TLS_PROCESS_CKE_DHE, ERR_R_INTERNAL_ERROR);
  2205. return 0;
  2206. #endif
  2207. }
  2208. static int tls_process_cke_ecdhe(SSL *s, PACKET *pkt, int *al)
  2209. {
  2210. #ifndef OPENSSL_NO_EC
  2211. EVP_PKEY *skey = s->s3->tmp.pkey;
  2212. EVP_PKEY *ckey = NULL;
  2213. int ret = 0;
  2214. if (PACKET_remaining(pkt) == 0L) {
  2215. /* We don't support ECDH client auth */
  2216. *al = SSL_AD_HANDSHAKE_FAILURE;
  2217. SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, SSL_R_MISSING_TMP_ECDH_KEY);
  2218. goto err;
  2219. } else {
  2220. unsigned int i;
  2221. const unsigned char *data;
  2222. /*
  2223. * Get client's public key from encoded point in the
  2224. * ClientKeyExchange message.
  2225. */
  2226. /* Get encoded point length */
  2227. if (!PACKET_get_1(pkt, &i) || !PACKET_get_bytes(pkt, &data, i)
  2228. || PACKET_remaining(pkt) != 0) {
  2229. *al = SSL_AD_DECODE_ERROR;
  2230. SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, SSL_R_LENGTH_MISMATCH);
  2231. goto err;
  2232. }
  2233. ckey = EVP_PKEY_new();
  2234. if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) <= 0) {
  2235. SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, ERR_R_EVP_LIB);
  2236. goto err;
  2237. }
  2238. if (EVP_PKEY_set1_tls_encodedpoint(ckey, data, i) == 0) {
  2239. *al = SSL_AD_HANDSHAKE_FAILURE;
  2240. SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, ERR_R_EC_LIB);
  2241. goto err;
  2242. }
  2243. }
  2244. if (ssl_derive(s, skey, ckey) == 0) {
  2245. *al = SSL_AD_INTERNAL_ERROR;
  2246. SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, ERR_R_INTERNAL_ERROR);
  2247. goto err;
  2248. }
  2249. ret = 1;
  2250. EVP_PKEY_free(s->s3->tmp.pkey);
  2251. s->s3->tmp.pkey = NULL;
  2252. err:
  2253. EVP_PKEY_free(ckey);
  2254. return ret;
  2255. #else
  2256. /* Should never happen */
  2257. *al = SSL_AD_INTERNAL_ERROR;
  2258. SSLerr(SSL_F_TLS_PROCESS_CKE_ECDHE, ERR_R_INTERNAL_ERROR);
  2259. return 0;
  2260. #endif
  2261. }
  2262. static int tls_process_cke_srp(SSL *s, PACKET *pkt, int *al)
  2263. {
  2264. #ifndef OPENSSL_NO_SRP
  2265. unsigned int i;
  2266. const unsigned char *data;
  2267. if (!PACKET_get_net_2(pkt, &i)
  2268. || !PACKET_get_bytes(pkt, &data, i)) {
  2269. *al = SSL_AD_DECODE_ERROR;
  2270. SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, SSL_R_BAD_SRP_A_LENGTH);
  2271. return 0;
  2272. }
  2273. if ((s->srp_ctx.A = BN_bin2bn(data, i, NULL)) == NULL) {
  2274. SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, ERR_R_BN_LIB);
  2275. return 0;
  2276. }
  2277. if (BN_ucmp(s->srp_ctx.A, s->srp_ctx.N) >= 0 || BN_is_zero(s->srp_ctx.A)) {
  2278. *al = SSL_AD_ILLEGAL_PARAMETER;
  2279. SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, SSL_R_BAD_SRP_PARAMETERS);
  2280. return 0;
  2281. }
  2282. OPENSSL_free(s->session->srp_username);
  2283. s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
  2284. if (s->session->srp_username == NULL) {
  2285. SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, ERR_R_MALLOC_FAILURE);
  2286. return 0;
  2287. }
  2288. if (!srp_generate_server_master_secret(s)) {
  2289. SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, ERR_R_INTERNAL_ERROR);
  2290. return 0;
  2291. }
  2292. return 1;
  2293. #else
  2294. /* Should never happen */
  2295. *al = SSL_AD_INTERNAL_ERROR;
  2296. SSLerr(SSL_F_TLS_PROCESS_CKE_SRP, ERR_R_INTERNAL_ERROR);
  2297. return 0;
  2298. #endif
  2299. }
  2300. static int tls_process_cke_gost(SSL *s, PACKET *pkt, int *al)
  2301. {
  2302. #ifndef OPENSSL_NO_GOST
  2303. EVP_PKEY_CTX *pkey_ctx;
  2304. EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;
  2305. unsigned char premaster_secret[32];
  2306. const unsigned char *start;
  2307. size_t outlen = 32, inlen;
  2308. unsigned long alg_a;
  2309. int Ttag, Tclass;
  2310. long Tlen;
  2311. long sess_key_len;
  2312. const unsigned char *data;
  2313. int ret = 0;
  2314. /* Get our certificate private key */
  2315. alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  2316. if (alg_a & SSL_aGOST12) {
  2317. /*
  2318. * New GOST ciphersuites have SSL_aGOST01 bit too
  2319. */
  2320. pk = s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey;
  2321. if (pk == NULL) {
  2322. pk = s->cert->pkeys[SSL_PKEY_GOST12_256].privatekey;
  2323. }
  2324. if (pk == NULL) {
  2325. pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
  2326. }
  2327. } else if (alg_a & SSL_aGOST01) {
  2328. pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
  2329. }
  2330. pkey_ctx = EVP_PKEY_CTX_new(pk, NULL);
  2331. if (pkey_ctx == NULL) {
  2332. *al = SSL_AD_INTERNAL_ERROR;
  2333. SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, ERR_R_MALLOC_FAILURE);
  2334. return 0;
  2335. }
  2336. if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) {
  2337. *al = SSL_AD_INTERNAL_ERROR;
  2338. SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, ERR_R_INTERNAL_ERROR);
  2339. return 0;
  2340. }
  2341. /*
  2342. * If client certificate is present and is of the same type, maybe
  2343. * use it for key exchange. Don't mind errors from
  2344. * EVP_PKEY_derive_set_peer, because it is completely valid to use a
  2345. * client certificate for authorization only.
  2346. */
  2347. client_pub_pkey = X509_get0_pubkey(s->session->peer);
  2348. if (client_pub_pkey) {
  2349. if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)
  2350. ERR_clear_error();
  2351. }
  2352. /* Decrypt session key */
  2353. sess_key_len = PACKET_remaining(pkt);
  2354. if (!PACKET_get_bytes(pkt, &data, sess_key_len)) {
  2355. *al = SSL_AD_INTERNAL_ERROR;
  2356. SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, ERR_R_INTERNAL_ERROR);
  2357. goto err;
  2358. }
  2359. if (ASN1_get_object((const unsigned char **)&data, &Tlen, &Ttag,
  2360. &Tclass, sess_key_len) != V_ASN1_CONSTRUCTED
  2361. || Ttag != V_ASN1_SEQUENCE || Tclass != V_ASN1_UNIVERSAL) {
  2362. *al = SSL_AD_DECODE_ERROR;
  2363. SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, SSL_R_DECRYPTION_FAILED);
  2364. goto err;
  2365. }
  2366. start = data;
  2367. inlen = Tlen;
  2368. if (EVP_PKEY_decrypt
  2369. (pkey_ctx, premaster_secret, &outlen, start, inlen) <= 0) {
  2370. *al = SSL_AD_DECODE_ERROR;
  2371. SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, SSL_R_DECRYPTION_FAILED);
  2372. goto err;
  2373. }
  2374. /* Generate master secret */
  2375. if (!ssl_generate_master_secret(s, premaster_secret,
  2376. sizeof(premaster_secret), 0)) {
  2377. *al = SSL_AD_INTERNAL_ERROR;
  2378. SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, ERR_R_INTERNAL_ERROR);
  2379. goto err;
  2380. }
  2381. /* Check if pubkey from client certificate was used */
  2382. if (EVP_PKEY_CTX_ctrl
  2383. (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0)
  2384. s->statem.no_cert_verify = 1;
  2385. ret = 1;
  2386. err:
  2387. EVP_PKEY_CTX_free(pkey_ctx);
  2388. return ret;
  2389. #else
  2390. /* Should never happen */
  2391. *al = SSL_AD_INTERNAL_ERROR;
  2392. SSLerr(SSL_F_TLS_PROCESS_CKE_GOST, ERR_R_INTERNAL_ERROR);
  2393. return 0;
  2394. #endif
  2395. }
  2396. MSG_PROCESS_RETURN tls_process_client_key_exchange(SSL *s, PACKET *pkt)
  2397. {
  2398. int al = -1;
  2399. unsigned long alg_k;
  2400. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  2401. /* For PSK parse and retrieve identity, obtain PSK key */
  2402. if ((alg_k & SSL_PSK) && !tls_process_cke_psk_preamble(s, pkt, &al))
  2403. goto err;
  2404. if (alg_k & SSL_kPSK) {
  2405. /* Identity extracted earlier: should be nothing left */
  2406. if (PACKET_remaining(pkt) != 0) {
  2407. al = SSL_AD_HANDSHAKE_FAILURE;
  2408. SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
  2409. SSL_R_LENGTH_MISMATCH);
  2410. goto err;
  2411. }
  2412. /* PSK handled by ssl_generate_master_secret */
  2413. if (!ssl_generate_master_secret(s, NULL, 0, 0)) {
  2414. al = SSL_AD_INTERNAL_ERROR;
  2415. SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  2416. goto err;
  2417. }
  2418. } else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
  2419. if (!tls_process_cke_rsa(s, pkt, &al))
  2420. goto err;
  2421. } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
  2422. if (!tls_process_cke_dhe(s, pkt, &al))
  2423. goto err;
  2424. } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK | SSL_kSM2DHE | SSL_kSM2PSK)) {
  2425. if (!tls_process_cke_ecdhe(s, pkt, &al))
  2426. goto err;
  2427. } else if (alg_k & SSL_kSRP) {
  2428. if (!tls_process_cke_srp(s, pkt, &al))
  2429. goto err;
  2430. } else if (alg_k & SSL_kGOST) {
  2431. if (!tls_process_cke_gost(s, pkt, &al))
  2432. goto err;
  2433. } else {
  2434. al = SSL_AD_HANDSHAKE_FAILURE;
  2435. SSLerr(SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
  2436. SSL_R_UNKNOWN_CIPHER_TYPE);
  2437. goto err;
  2438. }
  2439. return MSG_PROCESS_CONTINUE_PROCESSING;
  2440. err:
  2441. if (al != -1)
  2442. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  2443. #ifndef OPENSSL_NO_PSK
  2444. OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
  2445. s->s3->tmp.psk = NULL;
  2446. #endif
  2447. ossl_statem_set_error(s);
  2448. return MSG_PROCESS_ERROR;
  2449. }
  2450. WORK_STATE tls_post_process_client_key_exchange(SSL *s, WORK_STATE wst)
  2451. {
  2452. #ifndef OPENSSL_NO_SCTP
  2453. if (wst == WORK_MORE_A) {
  2454. if (SSL_IS_DTLS(s)) {
  2455. unsigned char sctpauthkey[64];
  2456. char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
  2457. /*
  2458. * Add new shared key for SCTP-Auth, will be ignored if no SCTP
  2459. * used.
  2460. */
  2461. memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
  2462. sizeof(DTLS1_SCTP_AUTH_LABEL));
  2463. if (SSL_export_keying_material(s, sctpauthkey,
  2464. sizeof(sctpauthkey), labelbuffer,
  2465. sizeof(labelbuffer), NULL, 0,
  2466. 0) <= 0) {
  2467. ossl_statem_set_error(s);
  2468. return WORK_ERROR;;
  2469. }
  2470. BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
  2471. sizeof(sctpauthkey), sctpauthkey);
  2472. }
  2473. wst = WORK_MORE_B;
  2474. }
  2475. if ((wst == WORK_MORE_B)
  2476. /* Is this SCTP? */
  2477. && BIO_dgram_is_sctp(SSL_get_wbio(s))
  2478. /* Are we renegotiating? */
  2479. && s->renegotiate
  2480. /* Are we going to skip the CertificateVerify? */
  2481. && (s->session->peer == NULL || s->statem.no_cert_verify)
  2482. && BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
  2483. s->s3->in_read_app_data = 2;
  2484. s->rwstate = SSL_READING;
  2485. BIO_clear_retry_flags(SSL_get_rbio(s));
  2486. BIO_set_retry_read(SSL_get_rbio(s));
  2487. ossl_statem_set_sctp_read_sock(s, 1);
  2488. return WORK_MORE_B;
  2489. } else {
  2490. ossl_statem_set_sctp_read_sock(s, 0);
  2491. }
  2492. #endif
  2493. if (s->statem.no_cert_verify || !s->session->peer) {
  2494. /*
  2495. * No certificate verify or no peer certificate so we no longer need
  2496. * the handshake_buffer
  2497. */
  2498. if (!ssl3_digest_cached_records(s, 0)) {
  2499. ossl_statem_set_error(s);
  2500. return WORK_ERROR;
  2501. }
  2502. return WORK_FINISHED_CONTINUE;
  2503. } else {
  2504. if (!s->s3->handshake_buffer) {
  2505. SSLerr(SSL_F_TLS_POST_PROCESS_CLIENT_KEY_EXCHANGE,
  2506. ERR_R_INTERNAL_ERROR);
  2507. ossl_statem_set_error(s);
  2508. return WORK_ERROR;
  2509. }
  2510. /*
  2511. * For sigalgs freeze the handshake buffer. If we support
  2512. * extms we've done this already so this is a no-op
  2513. */
  2514. if (!ssl3_digest_cached_records(s, 1)) {
  2515. ossl_statem_set_error(s);
  2516. return WORK_ERROR;
  2517. }
  2518. }
  2519. return WORK_FINISHED_CONTINUE;
  2520. }
  2521. MSG_PROCESS_RETURN tls_process_cert_verify(SSL *s, PACKET *pkt)
  2522. {
  2523. EVP_PKEY *pkey = NULL;
  2524. const unsigned char *sig, *data;
  2525. #ifndef OPENSSL_NO_GOST
  2526. unsigned char *gost_data = NULL;
  2527. #endif
  2528. int al, ret = MSG_PROCESS_ERROR;
  2529. int type = 0, j;
  2530. unsigned int len;
  2531. X509 *peer;
  2532. const EVP_MD *md = NULL;
  2533. long hdatalen = 0;
  2534. void *hdata;
  2535. EVP_MD_CTX *mctx = EVP_MD_CTX_new();
  2536. if (mctx == NULL) {
  2537. SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_MALLOC_FAILURE);
  2538. al = SSL_AD_INTERNAL_ERROR;
  2539. goto f_err;
  2540. }
  2541. peer = s->session->peer;
  2542. pkey = X509_get0_pubkey(peer);
  2543. type = X509_certificate_type(peer, pkey);
  2544. if (!(type & EVP_PKT_SIGN)) {
  2545. SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY,
  2546. SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
  2547. al = SSL_AD_ILLEGAL_PARAMETER;
  2548. goto f_err;
  2549. }
  2550. /* Check for broken implementations of GOST ciphersuites */
  2551. /*
  2552. * If key is GOST and n is exactly 64, it is bare signature without
  2553. * length field (CryptoPro implementations at least till CSP 4.0)
  2554. */
  2555. #ifndef OPENSSL_NO_GOST
  2556. if (PACKET_remaining(pkt) == 64
  2557. && EVP_PKEY_id(pkey) == NID_id_GostR3410_2001) {
  2558. len = 64;
  2559. } else
  2560. #endif
  2561. {
  2562. if (SSL_USE_SIGALGS(s)) {
  2563. int rv;
  2564. if (!PACKET_get_bytes(pkt, &sig, 2)) {
  2565. al = SSL_AD_DECODE_ERROR;
  2566. goto f_err;
  2567. }
  2568. rv = tls12_check_peer_sigalg(&md, s, sig, pkey);
  2569. if (rv == -1) {
  2570. al = SSL_AD_INTERNAL_ERROR;
  2571. goto f_err;
  2572. } else if (rv == 0) {
  2573. al = SSL_AD_DECODE_ERROR;
  2574. goto f_err;
  2575. }
  2576. #ifdef SSL_DEBUG
  2577. fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
  2578. #endif
  2579. } else {
  2580. /* Use default digest for this key type */
  2581. int idx = ssl_cert_type(NULL, pkey);
  2582. if (idx >= 0)
  2583. md = s->s3->tmp.md[idx];
  2584. if (md == NULL) {
  2585. al = SSL_AD_INTERNAL_ERROR;
  2586. goto f_err;
  2587. }
  2588. }
  2589. if (!PACKET_get_net_2(pkt, &len)) {
  2590. SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_LENGTH_MISMATCH);
  2591. al = SSL_AD_DECODE_ERROR;
  2592. goto f_err;
  2593. }
  2594. }
  2595. j = EVP_PKEY_size(pkey);
  2596. if (((int)len > j) || ((int)PACKET_remaining(pkt) > j)
  2597. || (PACKET_remaining(pkt) == 0)) {
  2598. SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_WRONG_SIGNATURE_SIZE);
  2599. al = SSL_AD_DECODE_ERROR;
  2600. goto f_err;
  2601. }
  2602. if (!PACKET_get_bytes(pkt, &data, len)) {
  2603. SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_LENGTH_MISMATCH);
  2604. al = SSL_AD_DECODE_ERROR;
  2605. goto f_err;
  2606. }
  2607. hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
  2608. if (hdatalen <= 0) {
  2609. SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
  2610. al = SSL_AD_INTERNAL_ERROR;
  2611. goto f_err;
  2612. }
  2613. //modify begin 使用gmtls 对从 client hello 消息到 client CertificateVerify 消息(不包括client CertificateVerify消息)所有内容做SM3摘要计算
  2614. #ifndef OPENSSL_NO_SM2
  2615. EVP_MD_CTX *mctx1 = NULL;
  2616. if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aSM2)
  2617. {
  2618. // from client hello to client CertificateVerify(not include CertificateVerify) make sm3
  2619. mctx1 = EVP_MD_CTX_new();
  2620. if (mctx1 == NULL) {
  2621. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_MALLOC_FAILURE);
  2622. goto f_err;
  2623. }
  2624. EVP_MD_CTX_init(mctx1);
  2625. if (!EVP_DigestInit(mctx1, md)
  2626. || EVP_DigestUpdate(mctx1, (unsigned char *)hdata,
  2627. hdatalen) <= 0
  2628. || EVP_DigestFinal_ex(mctx1, hdata, &hdatalen) <= 0)
  2629. {
  2630. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
  2631. goto f_err;
  2632. }
  2633. *(unsigned char *)(hdata + hdatalen) = 0;
  2634. }
  2635. if (mctx1 != NULL)
  2636. EVP_MD_CTX_free(mctx1);
  2637. #endif
  2638. // modify end
  2639. #ifdef SSL_DEBUG
  2640. fprintf(stderr, "Using client verify alg %s\n", EVP_MD_name(md));
  2641. #endif
  2642. // modify begin 使用gmtls 从上面得到的SM3摘要结果,还需要使用 SM2_DEFAULT_ID(1234567812345678) 做内部哈希,再签名
  2643. if (!EVP_VerifyInit_ex(mctx, md, NULL))
  2644. {
  2645. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_EVP_LIB);
  2646. goto f_err;
  2647. }
  2648. #ifndef OPENSSL_NO_SM2
  2649. if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aSM2)
  2650. {
  2651. // SM2_DEFAULT_ID to assign, when compute CertificateVerify sign
  2652. unsigned char z[EVP_MAX_MD_SIZE];
  2653. size_t zlen;
  2654. char *id = NULL;
  2655. id = SM2_DEFAULT_ID;
  2656. zlen = sizeof(z);
  2657. if (!SM2_compute_id_digest(EVP_sm3(), id, strlen(id), z, &zlen,
  2658. EVP_PKEY_get0_EC_KEY(pkey))) {
  2659. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_SM2_LIB);
  2660. goto f_err;
  2661. }
  2662. if (!EVP_VerifyUpdate(mctx, z, zlen))
  2663. {
  2664. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_EVP_LIB);
  2665. goto f_err;
  2666. }
  2667. }
  2668. #endif
  2669. if (!EVP_VerifyUpdate(mctx, hdata, hdatalen))
  2670. {
  2671. SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_EVP_LIB);
  2672. goto f_err;
  2673. }
  2674. // modify end
  2675. #ifndef OPENSSL_NO_GOST
  2676. {
  2677. int pktype = EVP_PKEY_id(pkey);
  2678. if (pktype == NID_id_GostR3410_2001
  2679. || pktype == NID_id_GostR3410_2012_256
  2680. || pktype == NID_id_GostR3410_2012_512) {
  2681. if ((gost_data = OPENSSL_malloc(len)) == NULL) {
  2682. SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_MALLOC_FAILURE);
  2683. al = SSL_AD_INTERNAL_ERROR;
  2684. goto f_err;
  2685. }
  2686. BUF_reverse(gost_data, data, len);
  2687. data = gost_data;
  2688. }
  2689. }
  2690. #endif
  2691. if (s->version == SSL3_VERSION
  2692. && !EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET,
  2693. s->session->master_key_length,
  2694. s->session->master_key)) {
  2695. SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_EVP_LIB);
  2696. al = SSL_AD_INTERNAL_ERROR;
  2697. goto f_err;
  2698. }
  2699. if (EVP_VerifyFinal(mctx, data, len, pkey) <= 0) {
  2700. al = SSL_AD_DECRYPT_ERROR;
  2701. SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY, SSL_R_BAD_SIGNATURE);
  2702. goto f_err;
  2703. }
  2704. ret = MSG_PROCESS_CONTINUE_PROCESSING;
  2705. if (0) {
  2706. f_err:
  2707. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  2708. ossl_statem_set_error(s);
  2709. }
  2710. BIO_free(s->s3->handshake_buffer);
  2711. s->s3->handshake_buffer = NULL;
  2712. EVP_MD_CTX_free(mctx);
  2713. #ifndef OPENSSL_NO_GOST
  2714. OPENSSL_free(gost_data);
  2715. #endif
  2716. return ret;
  2717. }
  2718. MSG_PROCESS_RETURN tls_process_client_certificate(SSL *s, PACKET *pkt)
  2719. {
  2720. int i, al = SSL_AD_INTERNAL_ERROR, ret = MSG_PROCESS_ERROR;
  2721. X509 *x = NULL;
  2722. unsigned long l, llen;
  2723. const unsigned char *certstart, *certbytes;
  2724. STACK_OF(X509) *sk = NULL;
  2725. PACKET spkt;
  2726. if ((sk = sk_X509_new_null()) == NULL) {
  2727. SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
  2728. goto f_err;
  2729. }
  2730. if (!PACKET_get_net_3(pkt, &llen)
  2731. || !PACKET_get_sub_packet(pkt, &spkt, llen)
  2732. || PACKET_remaining(pkt) != 0) {
  2733. al = SSL_AD_DECODE_ERROR;
  2734. SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
  2735. goto f_err;
  2736. }
  2737. while (PACKET_remaining(&spkt) > 0) {
  2738. if (!PACKET_get_net_3(&spkt, &l)
  2739. || !PACKET_get_bytes(&spkt, &certbytes, l)) {
  2740. al = SSL_AD_DECODE_ERROR;
  2741. SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
  2742. SSL_R_CERT_LENGTH_MISMATCH);
  2743. goto f_err;
  2744. }
  2745. certstart = certbytes;
  2746. x = d2i_X509(NULL, (const unsigned char **)&certbytes, l);
  2747. if (x == NULL) {
  2748. SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_ASN1_LIB);
  2749. goto f_err;
  2750. }
  2751. if (certbytes != (certstart + l)) {
  2752. al = SSL_AD_DECODE_ERROR;
  2753. SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
  2754. SSL_R_CERT_LENGTH_MISMATCH);
  2755. goto f_err;
  2756. }
  2757. if (!sk_X509_push(sk, x)) {
  2758. SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
  2759. goto f_err;
  2760. }
  2761. x = NULL;
  2762. }
  2763. if (sk_X509_num(sk) <= 0) {
  2764. /* TLS does not mind 0 certs returned */
  2765. if (s->version == SSL3_VERSION) {
  2766. al = SSL_AD_HANDSHAKE_FAILURE;
  2767. SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
  2768. SSL_R_NO_CERTIFICATES_RETURNED);
  2769. goto f_err;
  2770. }
  2771. /* Fail for TLS only if we required a certificate */
  2772. else if ((s->verify_mode & SSL_VERIFY_PEER) &&
  2773. (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
  2774. SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
  2775. SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  2776. al = SSL_AD_HANDSHAKE_FAILURE;
  2777. goto f_err;
  2778. }
  2779. /* No client certificate so digest cached records */
  2780. if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s, 0)) {
  2781. goto f_err;
  2782. }
  2783. } else {
  2784. EVP_PKEY *pkey;
  2785. i = ssl_verify_cert_chain(s, sk);
  2786. if (i <= 0) {
  2787. al = ssl_verify_alarm_type(s->verify_result);
  2788. SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
  2789. SSL_R_CERTIFICATE_VERIFY_FAILED);
  2790. goto f_err;
  2791. }
  2792. if (i > 1) {
  2793. SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, i);
  2794. al = SSL_AD_HANDSHAKE_FAILURE;
  2795. goto f_err;
  2796. }
  2797. pkey = X509_get0_pubkey(sk_X509_value(sk, 0));
  2798. if (pkey == NULL) {
  2799. al = SSL3_AD_HANDSHAKE_FAILURE;
  2800. SSLerr(SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
  2801. SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  2802. goto f_err;
  2803. }
  2804. }
  2805. X509_free(s->session->peer);
  2806. s->session->peer = sk_X509_shift(sk);
  2807. s->session->verify_result = s->verify_result;
  2808. sk_X509_pop_free(s->session->peer_chain, X509_free);
  2809. s->session->peer_chain = sk;
  2810. /*
  2811. * Inconsistency alert: cert_chain does *not* include the peer's own
  2812. * certificate, while we do include it in statem_clnt.c
  2813. */
  2814. sk = NULL;
  2815. ret = MSG_PROCESS_CONTINUE_READING;
  2816. goto done;
  2817. f_err:
  2818. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  2819. ossl_statem_set_error(s);
  2820. done:
  2821. X509_free(x);
  2822. sk_X509_pop_free(sk, X509_free);
  2823. return ret;
  2824. }
  2825. int tls_construct_server_certificate(SSL *s)
  2826. {
  2827. CERT_PKEY *cpk;
  2828. cpk = ssl_get_server_send_pkey(s);
  2829. if (cpk == NULL) {
  2830. SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
  2831. ossl_statem_set_error(s);
  2832. return 0;
  2833. }
  2834. if (!ssl3_output_cert_chain(s, cpk)) {
  2835. SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
  2836. ossl_statem_set_error(s);
  2837. return 0;
  2838. }
  2839. return 1;
  2840. }
  2841. int tls_construct_new_session_ticket(SSL *s)
  2842. {
  2843. unsigned char *senc = NULL;
  2844. EVP_CIPHER_CTX *ctx = NULL;
  2845. HMAC_CTX *hctx = NULL;
  2846. unsigned char *p, *macstart;
  2847. const unsigned char *const_p;
  2848. int len, slen_full, slen;
  2849. SSL_SESSION *sess;
  2850. unsigned int hlen;
  2851. SSL_CTX *tctx = s->initial_ctx;
  2852. unsigned char iv[EVP_MAX_IV_LENGTH];
  2853. unsigned char key_name[TLSEXT_KEYNAME_LENGTH];
  2854. int iv_len;
  2855. /* get session encoding length */
  2856. slen_full = i2d_SSL_SESSION(s->session, NULL);
  2857. /*
  2858. * Some length values are 16 bits, so forget it if session is too
  2859. * long
  2860. */
  2861. if (slen_full == 0 || slen_full > 0xFF00) {
  2862. ossl_statem_set_error(s);
  2863. return 0;
  2864. }
  2865. senc = OPENSSL_malloc(slen_full);
  2866. if (senc == NULL) {
  2867. ossl_statem_set_error(s);
  2868. return 0;
  2869. }
  2870. ctx = EVP_CIPHER_CTX_new();
  2871. hctx = HMAC_CTX_new();
  2872. if (ctx == NULL || hctx == NULL) {
  2873. SSLerr(SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
  2874. goto err;
  2875. }
  2876. p = senc;
  2877. if (!i2d_SSL_SESSION(s->session, &p))
  2878. goto err;
  2879. /*
  2880. * create a fresh copy (not shared with other threads) to clean up
  2881. */
  2882. const_p = senc;
  2883. sess = d2i_SSL_SESSION(NULL, &const_p, slen_full);
  2884. if (sess == NULL)
  2885. goto err;
  2886. sess->session_id_length = 0; /* ID is irrelevant for the ticket */
  2887. slen = i2d_SSL_SESSION(sess, NULL);
  2888. if (slen == 0 || slen > slen_full) { /* shouldn't ever happen */
  2889. SSL_SESSION_free(sess);
  2890. goto err;
  2891. }
  2892. p = senc;
  2893. if (!i2d_SSL_SESSION(sess, &p)) {
  2894. SSL_SESSION_free(sess);
  2895. goto err;
  2896. }
  2897. SSL_SESSION_free(sess);
  2898. /*-
  2899. * Grow buffer if need be: the length calculation is as
  2900. * follows handshake_header_length +
  2901. * 4 (ticket lifetime hint) + 2 (ticket length) +
  2902. * sizeof(keyname) + max_iv_len (iv length) +
  2903. * max_enc_block_size (max encrypted session * length) +
  2904. * max_md_size (HMAC) + session_length.
  2905. */
  2906. if (!BUF_MEM_grow(s->init_buf,
  2907. SSL_HM_HEADER_LENGTH(s) + 6 + sizeof(key_name) +
  2908. EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH +
  2909. EVP_MAX_MD_SIZE + slen))
  2910. goto err;
  2911. p = ssl_handshake_start(s);
  2912. /*
  2913. * Initialize HMAC and cipher contexts. If callback present it does
  2914. * all the work otherwise use generated values from parent ctx.
  2915. */
  2916. if (tctx->tlsext_ticket_key_cb) {
  2917. /* if 0 is returned, write an empty ticket */
  2918. int ret = tctx->tlsext_ticket_key_cb(s, key_name, iv, ctx,
  2919. hctx, 1);
  2920. if (ret == 0) {
  2921. l2n(0, p); /* timeout */
  2922. s2n(0, p); /* length */
  2923. if (!ssl_set_handshake_header
  2924. (s, SSL3_MT_NEWSESSION_TICKET, p - ssl_handshake_start(s)))
  2925. goto err;
  2926. OPENSSL_free(senc);
  2927. EVP_CIPHER_CTX_free(ctx);
  2928. HMAC_CTX_free(hctx);
  2929. return 1;
  2930. }
  2931. if (ret < 0)
  2932. goto err;
  2933. iv_len = EVP_CIPHER_CTX_iv_length(ctx);
  2934. } else {
  2935. const EVP_CIPHER *cipher =
  2936. #ifndef OPENSSL_NO_AES
  2937. EVP_aes_256_cbc();
  2938. #else
  2939. EVP_sms4_cbc();
  2940. #endif
  2941. iv_len = EVP_CIPHER_iv_length(cipher);
  2942. if (RAND_bytes(iv, iv_len) <= 0)
  2943. goto err;
  2944. if (!EVP_EncryptInit_ex(ctx, cipher, NULL,
  2945. tctx->tlsext_tick_aes_key, iv))
  2946. goto err;
  2947. if (!HMAC_Init_ex(hctx, tctx->tlsext_tick_hmac_key,
  2948. sizeof(tctx->tlsext_tick_hmac_key),
  2949. EVP_get_digestbynid(NID_sha256), NULL))
  2950. goto err;
  2951. memcpy(key_name, tctx->tlsext_tick_key_name,
  2952. sizeof(tctx->tlsext_tick_key_name));
  2953. }
  2954. /*
  2955. * Ticket lifetime hint (advisory only): We leave this unspecified
  2956. * for resumed session (for simplicity), and guess that tickets for
  2957. * new sessions will live as long as their sessions.
  2958. */
  2959. l2n(s->hit ? 0 : s->session->timeout, p);
  2960. /* Skip ticket length for now */
  2961. p += 2;
  2962. /* Output key name */
  2963. macstart = p;
  2964. memcpy(p, key_name, sizeof(key_name));
  2965. p += sizeof(key_name);
  2966. /* output IV */
  2967. memcpy(p, iv, iv_len);
  2968. p += iv_len;
  2969. /* Encrypt session data */
  2970. if (!EVP_EncryptUpdate(ctx, p, &len, senc, slen))
  2971. goto err;
  2972. p += len;
  2973. if (!EVP_EncryptFinal(ctx, p, &len))
  2974. goto err;
  2975. p += len;
  2976. if (!HMAC_Update(hctx, macstart, p - macstart))
  2977. goto err;
  2978. if (!HMAC_Final(hctx, p, &hlen))
  2979. goto err;
  2980. EVP_CIPHER_CTX_free(ctx);
  2981. HMAC_CTX_free(hctx);
  2982. ctx = NULL;
  2983. hctx = NULL;
  2984. p += hlen;
  2985. /* Now write out lengths: p points to end of data written */
  2986. /* Total length */
  2987. len = p - ssl_handshake_start(s);
  2988. /* Skip ticket lifetime hint */
  2989. p = ssl_handshake_start(s) + 4;
  2990. s2n(len - 6, p);
  2991. if (!ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len))
  2992. goto err;
  2993. OPENSSL_free(senc);
  2994. return 1;
  2995. err:
  2996. OPENSSL_free(senc);
  2997. EVP_CIPHER_CTX_free(ctx);
  2998. HMAC_CTX_free(hctx);
  2999. ossl_statem_set_error(s);
  3000. return 0;
  3001. }
  3002. int tls_construct_cert_status(SSL *s)
  3003. {
  3004. unsigned char *p;
  3005. size_t msglen;
  3006. /*-
  3007. * Grow buffer if need be: the length calculation is as
  3008. * follows handshake_header_length +
  3009. * 1 (ocsp response type) + 3 (ocsp response length)
  3010. * + (ocsp response)
  3011. */
  3012. msglen = 4 + s->tlsext_ocsp_resplen;
  3013. if (!BUF_MEM_grow(s->init_buf, SSL_HM_HEADER_LENGTH(s) + msglen))
  3014. goto err;
  3015. p = ssl_handshake_start(s);
  3016. /* status type */
  3017. *(p++) = s->tlsext_status_type;
  3018. /* length of OCSP response */
  3019. l2n3(s->tlsext_ocsp_resplen, p);
  3020. /* actual response */
  3021. memcpy(p, s->tlsext_ocsp_resp, s->tlsext_ocsp_resplen);
  3022. if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_STATUS, msglen))
  3023. goto err;
  3024. return 1;
  3025. err:
  3026. ossl_statem_set_error(s);
  3027. return 0;
  3028. }
  3029. #ifndef OPENSSL_NO_NEXTPROTONEG
  3030. /*
  3031. * tls_process_next_proto reads a Next Protocol Negotiation handshake message.
  3032. * It sets the next_proto member in s if found
  3033. */
  3034. MSG_PROCESS_RETURN tls_process_next_proto(SSL *s, PACKET *pkt)
  3035. {
  3036. PACKET next_proto, padding;
  3037. size_t next_proto_len;
  3038. /*-
  3039. * The payload looks like:
  3040. * uint8 proto_len;
  3041. * uint8 proto[proto_len];
  3042. * uint8 padding_len;
  3043. * uint8 padding[padding_len];
  3044. */
  3045. if (!PACKET_get_length_prefixed_1(pkt, &next_proto)
  3046. || !PACKET_get_length_prefixed_1(pkt, &padding)
  3047. || PACKET_remaining(pkt) > 0) {
  3048. SSLerr(SSL_F_TLS_PROCESS_NEXT_PROTO, SSL_R_LENGTH_MISMATCH);
  3049. goto err;
  3050. }
  3051. if (!PACKET_memdup(&next_proto, &s->next_proto_negotiated, &next_proto_len)) {
  3052. s->next_proto_negotiated_len = 0;
  3053. goto err;
  3054. }
  3055. s->next_proto_negotiated_len = (unsigned char)next_proto_len;
  3056. return MSG_PROCESS_CONTINUE_READING;
  3057. err:
  3058. ossl_statem_set_error(s);
  3059. return MSG_PROCESS_ERROR;
  3060. }
  3061. #endif
  3062. #define SSLV2_CIPHER_LEN 3
  3063. STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s,
  3064. PACKET *cipher_suites,
  3065. STACK_OF(SSL_CIPHER) **skp,
  3066. int sslv2format, int *al)
  3067. {
  3068. const SSL_CIPHER *c;
  3069. STACK_OF(SSL_CIPHER) *sk;
  3070. int n;
  3071. /* 3 = SSLV2_CIPHER_LEN > TLS_CIPHER_LEN = 2. */
  3072. unsigned char cipher[SSLV2_CIPHER_LEN];
  3073. s->s3->send_connection_binding = 0;
  3074. n = sslv2format ? SSLV2_CIPHER_LEN : TLS_CIPHER_LEN;
  3075. if (PACKET_remaining(cipher_suites) == 0) {
  3076. SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, SSL_R_NO_CIPHERS_SPECIFIED);
  3077. *al = SSL_AD_ILLEGAL_PARAMETER;
  3078. return NULL;
  3079. }
  3080. if (PACKET_remaining(cipher_suites) % n != 0) {
  3081. SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,
  3082. SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
  3083. *al = SSL_AD_DECODE_ERROR;
  3084. return NULL;
  3085. }
  3086. sk = sk_SSL_CIPHER_new_null();
  3087. if (sk == NULL) {
  3088. SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
  3089. *al = SSL_AD_INTERNAL_ERROR;
  3090. return NULL;
  3091. }
  3092. if (sslv2format) {
  3093. size_t numciphers = PACKET_remaining(cipher_suites) / n;
  3094. PACKET sslv2ciphers = *cipher_suites;
  3095. unsigned int leadbyte;
  3096. unsigned char *raw;
  3097. /*
  3098. * We store the raw ciphers list in SSLv3+ format so we need to do some
  3099. * preprocessing to convert the list first. If there are any SSLv2 only
  3100. * ciphersuites with a non-zero leading byte then we are going to
  3101. * slightly over allocate because we won't store those. But that isn't a
  3102. * problem.
  3103. */
  3104. raw = OPENSSL_malloc(numciphers * TLS_CIPHER_LEN);
  3105. s->s3->tmp.ciphers_raw = raw;
  3106. if (raw == NULL) {
  3107. *al = SSL_AD_INTERNAL_ERROR;
  3108. goto err;
  3109. }
  3110. for (s->s3->tmp.ciphers_rawlen = 0;
  3111. PACKET_remaining(&sslv2ciphers) > 0;
  3112. raw += TLS_CIPHER_LEN) {
  3113. if (!PACKET_get_1(&sslv2ciphers, &leadbyte)
  3114. || (leadbyte == 0
  3115. && !PACKET_copy_bytes(&sslv2ciphers, raw,
  3116. TLS_CIPHER_LEN))
  3117. || (leadbyte != 0
  3118. && !PACKET_forward(&sslv2ciphers, TLS_CIPHER_LEN))) {
  3119. *al = SSL_AD_INTERNAL_ERROR;
  3120. OPENSSL_free(raw);
  3121. s->s3->tmp.ciphers_raw = NULL;
  3122. s->s3->tmp.ciphers_rawlen = 0;
  3123. goto err;
  3124. }
  3125. if (leadbyte == 0)
  3126. s->s3->tmp.ciphers_rawlen += TLS_CIPHER_LEN;
  3127. }
  3128. } else if (!PACKET_memdup(cipher_suites, &s->s3->tmp.ciphers_raw,
  3129. &s->s3->tmp.ciphers_rawlen)) {
  3130. *al = SSL_AD_INTERNAL_ERROR;
  3131. goto err;
  3132. }
  3133. while (PACKET_copy_bytes(cipher_suites, cipher, n)) {
  3134. /*
  3135. * SSLv3 ciphers wrapped in an SSLv2-compatible ClientHello have the
  3136. * first byte set to zero, while true SSLv2 ciphers have a non-zero
  3137. * first byte. We don't support any true SSLv2 ciphers, so skip them.
  3138. */
  3139. if (sslv2format && cipher[0] != '\0')
  3140. continue;
  3141. /* Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV */
  3142. if ((cipher[n - 2] == ((SSL3_CK_SCSV >> 8) & 0xff)) &&
  3143. (cipher[n - 1] == (SSL3_CK_SCSV & 0xff))) {
  3144. /* SCSV fatal if renegotiating */
  3145. if (s->renegotiate) {
  3146. SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,
  3147. SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
  3148. *al = SSL_AD_HANDSHAKE_FAILURE;
  3149. goto err;
  3150. }
  3151. s->s3->send_connection_binding = 1;
  3152. continue;
  3153. }
  3154. /* Check for TLS_FALLBACK_SCSV */
  3155. if ((cipher[n - 2] == ((SSL3_CK_FALLBACK_SCSV >> 8) & 0xff)) &&
  3156. (cipher[n - 1] == (SSL3_CK_FALLBACK_SCSV & 0xff))) {
  3157. /*
  3158. * The SCSV indicates that the client previously tried a higher
  3159. * version. Fail if the current version is an unexpected
  3160. * downgrade.
  3161. */
  3162. if (!ssl_check_version_downgrade(s)) {
  3163. SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,
  3164. SSL_R_INAPPROPRIATE_FALLBACK);
  3165. *al = SSL_AD_INAPPROPRIATE_FALLBACK;
  3166. goto err;
  3167. }
  3168. continue;
  3169. }
  3170. /* For SSLv2-compat, ignore leading 0-byte. */
  3171. c = ssl_get_cipher_by_char(s, sslv2format ? &cipher[1] : cipher);
  3172. if (c != NULL) {
  3173. if (!sk_SSL_CIPHER_push(sk, c)) {
  3174. SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
  3175. *al = SSL_AD_INTERNAL_ERROR;
  3176. goto err;
  3177. }
  3178. }
  3179. }
  3180. if (PACKET_remaining(cipher_suites) > 0) {
  3181. *al = SSL_AD_INTERNAL_ERROR;
  3182. SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, ERR_R_INTERNAL_ERROR);
  3183. goto err;
  3184. }
  3185. *skp = sk;
  3186. return sk;
  3187. err:
  3188. sk_SSL_CIPHER_free(sk);
  3189. return NULL;
  3190. }

重新编译

  • sudo  ./config --prefix=/usr/local/gmssl --openssldir=/usr/local/gmssl
  • sudo make
  • sudo make install
  • 配置 /etc/profile
  • 激活 source /etc/profile
  • 验证 gmssl version
     

成果展示

吉大正元身份认证网关和gmssl客户端之间通信

  • gmssl 客户端命令
  • gmssl s_client -gmtls -connect 192.168.80.110:8889 -key /home/chy-cpabe/tmp/second/sign.key -cert /home/chy-cpabe/tmp/second/sign.pem -dkey /home/chy-cpabe/tmp/second/encrypt.key -dcert /home/chy-cpabe/tmp/second/encrypt.pem -CAfile /home/chy-cpabe/tmp/second/rootcert.pem -state -verify 3
  • -gmtls    协议类型
  • -connect 对接端口  其中192.168.80.110:8889 是身份认证网关的地址
  • -key       签名私钥
  • -cert       签名证书
  • -dkey      加密私钥
  • -dcert      加密证书
  • -CAfile    CA证书路径
  • -state     状态信息
  • -verify     开启国密双证书双向认证
  • 1             验证深度,分析源码可知,这个数只是接收并输出,并无关键性影响
  1. chy-cpabe@ubuntu:~$ gmssl s_client -gmtls -connect 192.168.80.110:8889 -key /home/chy-cpabe/tmp/second/sign.key -cert /home/chy-cpabe/tmp/second/sign.pem -dkey /home/chy-cpabe/tmp/second/encrypt.key -dcert /home/chy-cpabe/tmp/second/encrypt.pem -CAfile /home/chy-cpabe/tmp/second/rootcert.pem -state -verify 3
  2. verify depth is 3
  3. [GMTLS_DEBUG] set sm2 signing certificate
  4. [GMTLS_DEBUG] set sm2 signing private key
  5. [GMTLS_DEBUG] set sm2 encryption certificate
  6. [GMTLS_DEBUG] set sm2 decryption private key
  7. CONNECTED(00000003)
  8. SSL_connect:before SSL initialization
  9. SSL_connect:SSLv3/TLS write client hello
  10. SSL_connect:SSLv3/TLS write client hello
  11. SSL_connect:SSLv3/TLS read server hello
  12. depth=1 C = CN, O = SDT, CN = SDTCA SM2
  13. verify return:1
  14. depth=0 C = CN, O = SDT, CN = 192.168.80.110
  15. verify return:1
  16. SSL_connect:SSLv3/TLS read server certificate
  17. Z=57A18ADE9AE65C4518E01851C91150B786FEC8CD4FA1C22DCA623E2D1C4B494D
  18. C=0001CF308201CB30820171A003020102020842C7314545E1F9E0300A06082A811CCF55018375302F310B300906035504061302434E310C300A060355040A0C035344543112301006035504030C09534454434120534D32301E170D3232303930383038353031395A170D3237303930373038353031395A3034310B300906035504061302434E310C300A060355040A0C035344543117301506035504030C0E3139322E3136382E38302E3131303059301306072A8648CE3D020106082A811CCF5501822D03420004728DDC8FE396FB0FDAB3242DB3ED4E370A275E0A422215CA62BA993E7FDAB6B400EBD45CA92E31ABAD1B20C7DC868E98DB4439A56CCD6B7501E76B2C2A9D5160A3723070300B0603551D0F04040302043030210603551D1F041A30183016A014A0128610687474703A2F2F3132372E302E302E31301D0603551D0E041604141A0578239BFB5814A30439EB36C65EADB9A12487301F0603551D230418301680140817D59FAF34E291658F2BCECDF2B5AAE7B1E2BC300A06082A811CCF55018375034800304502202A93DC8D02F6E0ADD981DF18F56A6A4266A976C037263B731E08DC52BD11847A022100A5E6D842CC52D7A49B897A92DF49C91E7D52595A12D7B7100C2B70E318A80218
  19. SSL_connect:SSLv3/TLS read server key exchange
  20. SSL_connect:SSLv3/TLS read server certificate request
  21. SSL_connect:SSLv3/TLS read server done
  22. SSL_connect:SSLv3/TLS write client certificate
  23. SSL_connect:SSLv3/TLS write client key exchange
  24. ssl_get_algorithm2=b9cdb00008x
  25. SSL_connect:SSLv3/TLS write certificate verify
  26. SSL_connect:SSLv3/TLS write change cipher spec
  27. SSL_connect:SSLv3/TLS write finished
  28. SSL_connect:SSLv3/TLS write finished
  29. SSL_connect:SSLv3/TLS read change cipher spec
  30. SSL_connect:SSLv3/TLS read finished
  31. ---
  32. Certificate chain
  33. 0 s:/C=CN/O=SDT/CN=192.168.80.110
  34. i:/C=CN/O=SDT/CN=SDTCA SM2
  35. 1 s:/C=CN/O=SDT/CN=192.168.80.110
  36. i:/C=CN/O=SDT/CN=SDTCA SM2
  37. ---
  38. Server certificate
  39. -----BEGIN CERTIFICATE-----
  40. MIIByzCCAXGgAwIBAgIIMBQFazElStAwCgYIKoEcz1UBg3UwLzELMAkGA1UEBhMC
  41. Q04xDDAKBgNVBAoMA1NEVDESMBAGA1UEAwwJU0RUQ0EgU00yMB4XDTIyMDkwODA4
  42. NTAxOVoXDTI3MDkwNzA4NTAxOVowNDELMAkGA1UEBhMCQ04xDDAKBgNVBAoMA1NE
  43. VDEXMBUGA1UEAwwOMTkyLjE2OC44MC4xMTAwWTATBgcqhkjOPQIBBggqgRzPVQGC
  44. LQNCAATlDQxehfZeFn05t6UUNR+I0dA2zYjtOeUtHdB/WRCjE6YlMzUYRDmsvHXF
  45. KtXeAioY+DwazbfwkHEBJhyIgzWUo3IwcDALBgNVHQ8EBAMCBsAwIQYDVR0fBBow
  46. GDAWoBSgEoYQaHR0cDovLzEyNy4wLjAuMTAdBgNVHQ4EFgQUSd5ccizI5+TH9ODp
  47. Aq6++mew1OAwHwYDVR0jBBgwFoAUCBfVn6804pFljyvOzfK1quex4rwwCgYIKoEc
  48. z1UBg3UDSAAwRQIhAOdvLFjuQ2ZwbyR26T3PHMyW/Dfli5gpC4TX7xSWFjlbAiBE
  49. 6MtGGkPaS1I1lB2Vkiq5ifWNdTCzBzFeV6W6sHeGag==
  50. -----END CERTIFICATE-----
  51. subject=/C=CN/O=SDT/CN=192.168.80.110
  52. issuer=/C=CN/O=SDT/CN=SDTCA SM2
  53. ---
  54. Acceptable client certificate CA names
  55. /C=CN/O=SDT/CN=SDTCA SM2
  56. Client Certificate Types: RSA sign, ECDSA sign
  57. ---
  58. SSL handshake has read 1238 bytes and written 1773 bytes
  59. Verification: OK
  60. ---
  61. New, GMTLSv1.1, Cipher is SM2-WITH-SMS4-SM3
  62. Server public key is 256 bit
  63. Secure Renegotiation IS NOT supported
  64. Compression: NONE
  65. Expansion: NONE
  66. No ALPN negotiated
  67. SSL-Session:
  68. Protocol : GMTLSv1.1
  69. Cipher : SM2-WITH-SMS4-SM3
  70. Session-ID:
  71. Session-ID-ctx:
  72. Master-Key: 83AEC9F0773CE4364F070F44FCCFFAF4AA6C09090AC71009D67F03313553DABD8E36C4E0AE61CD876B2ED2095EC9A905
  73. PSK identity: None
  74. PSK identity hint: None
  75. SRP username: None
  76. Start Time: 1668134562
  77. Timeout : 7200 (sec)
  78. Verify return code: 0 (ok)
  79. Extended master secret: no
  80. ---
  81. SSL3 alert read:warning:close notify
  82. closed
  83. SSL3 alert write:warning:close notify

吉大正元USBKey和gmssl服务端之间通信

  • 吉大正元USBKey

  •  gmssl服务端

  •  抓包测试

参考链接

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/118249
推荐阅读
相关标签
  

闽ICP备14008679号