d1_clnt.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536
  1. /* ssl/d1_clnt.c */
  2. /*
  3. * DTLS implementation written by Nagendra Modadugu
  4. * ([email protected]) for the OpenSSL project 2005.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 1999-2007 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * [email protected].
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * ([email protected]). This product includes software written by Tim
  56. * Hudson ([email protected]).
  57. *
  58. */
  59. /* Copyright (C) 1995-1998 Eric Young ([email protected])
  60. * All rights reserved.
  61. *
  62. * This package is an SSL implementation written
  63. * by Eric Young ([email protected]).
  64. * The implementation was written so as to conform with Netscapes SSL.
  65. *
  66. * This library is free for commercial and non-commercial use as long as
  67. * the following conditions are aheared to. The following conditions
  68. * apply to all code found in this distribution, be it the RC4, RSA,
  69. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  70. * included with this distribution is covered by the same copyright terms
  71. * except that the holder is Tim Hudson ([email protected]).
  72. *
  73. * Copyright remains Eric Young's, and as such any Copyright notices in
  74. * the code are not to be removed.
  75. * If this package is used in a product, Eric Young should be given attribution
  76. * as the author of the parts of the library used.
  77. * This can be in the form of a textual message at program startup or
  78. * in documentation (online or textual) provided with the package.
  79. *
  80. * Redistribution and use in source and binary forms, with or without
  81. * modification, are permitted provided that the following conditions
  82. * are met:
  83. * 1. Redistributions of source code must retain the copyright
  84. * notice, this list of conditions and the following disclaimer.
  85. * 2. Redistributions in binary form must reproduce the above copyright
  86. * notice, this list of conditions and the following disclaimer in the
  87. * documentation and/or other materials provided with the distribution.
  88. * 3. All advertising materials mentioning features or use of this software
  89. * must display the following acknowledgement:
  90. * "This product includes cryptographic software written by
  91. * Eric Young ([email protected])"
  92. * The word 'cryptographic' can be left out if the rouines from the library
  93. * being used are not cryptographic related :-).
  94. * 4. If you include any Windows specific code (or a derivative thereof) from
  95. * the apps directory (application code) you must include an acknowledgement:
  96. * "This product includes software written by Tim Hudson ([email protected])"
  97. *
  98. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  99. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  100. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  101. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  102. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  103. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  104. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  105. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  106. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  107. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  108. * SUCH DAMAGE.
  109. *
  110. * The licence and distribution terms for any publically available version or
  111. * derivative of this code cannot be changed. i.e. this code cannot simply be
  112. * copied and put under another distribution licence
  113. * [including the GNU Public Licence.]
  114. */
  115. #include <stdio.h>
  116. #include "ssl_locl.h"
  117. #ifndef OPENSSL_NO_KRB5
  118. #include "kssl_lcl.h"
  119. #endif
  120. #include <openssl/buffer.h>
  121. #include <openssl/rand.h>
  122. #include <openssl/objects.h>
  123. #include <openssl/evp.h>
  124. #include <openssl/md5.h>
  125. #include <openssl/bn.h>
  126. #ifndef OPENSSL_NO_DH
  127. #include <openssl/dh.h>
  128. #endif
  129. static const SSL_METHOD *dtls1_get_client_method(int ver);
  130. static int dtls1_get_hello_verify(SSL *s);
  131. static const SSL_METHOD *dtls1_get_client_method(int ver)
  132. {
  133. if (ver == DTLS1_VERSION || ver == DTLS1_BAD_VER)
  134. return(DTLSv1_client_method());
  135. else
  136. return(NULL);
  137. }
  138. IMPLEMENT_dtls1_meth_func(DTLSv1_client_method,
  139. ssl_undefined_function,
  140. dtls1_connect,
  141. dtls1_get_client_method)
  142. int dtls1_connect(SSL *s)
  143. {
  144. BUF_MEM *buf=NULL;
  145. unsigned long Time=(unsigned long)time(NULL);
  146. void (*cb)(const SSL *ssl,int type,int val)=NULL;
  147. int ret= -1;
  148. int new_state,state,skip=0;;
  149. RAND_add(&Time,sizeof(Time),0);
  150. ERR_clear_error();
  151. clear_sys_error();
  152. if (s->info_callback != NULL)
  153. cb=s->info_callback;
  154. else if (s->ctx->info_callback != NULL)
  155. cb=s->ctx->info_callback;
  156. s->in_handshake++;
  157. if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
  158. for (;;)
  159. {
  160. state=s->state;
  161. switch(s->state)
  162. {
  163. case SSL_ST_RENEGOTIATE:
  164. s->new_session=1;
  165. s->state=SSL_ST_CONNECT;
  166. s->ctx->stats.sess_connect_renegotiate++;
  167. /* break */
  168. case SSL_ST_BEFORE:
  169. case SSL_ST_CONNECT:
  170. case SSL_ST_BEFORE|SSL_ST_CONNECT:
  171. case SSL_ST_OK|SSL_ST_CONNECT:
  172. s->server=0;
  173. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
  174. if ((s->version & 0xff00 ) != (DTLS1_VERSION & 0xff00) &&
  175. (s->version & 0xff00 ) != (DTLS1_BAD_VER & 0xff00))
  176. {
  177. SSLerr(SSL_F_DTLS1_CONNECT, ERR_R_INTERNAL_ERROR);
  178. ret = -1;
  179. goto end;
  180. }
  181. /* s->version=SSL3_VERSION; */
  182. s->type=SSL_ST_CONNECT;
  183. if (s->init_buf == NULL)
  184. {
  185. if ((buf=BUF_MEM_new()) == NULL)
  186. {
  187. ret= -1;
  188. goto end;
  189. }
  190. if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
  191. {
  192. ret= -1;
  193. goto end;
  194. }
  195. s->init_buf=buf;
  196. buf=NULL;
  197. }
  198. if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
  199. /* setup buffing BIO */
  200. if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; }
  201. /* don't push the buffering BIO quite yet */
  202. s->state=SSL3_ST_CW_CLNT_HELLO_A;
  203. s->ctx->stats.sess_connect++;
  204. s->init_num=0;
  205. /* mark client_random uninitialized */
  206. memset(s->s3->client_random,0,sizeof(s->s3->client_random));
  207. s->d1->send_cookie = 0;
  208. s->hit = 0;
  209. break;
  210. case SSL3_ST_CW_CLNT_HELLO_A:
  211. case SSL3_ST_CW_CLNT_HELLO_B:
  212. s->shutdown=0;
  213. /* every DTLS ClientHello resets Finished MAC */
  214. ssl3_init_finished_mac(s);
  215. dtls1_start_timer(s);
  216. ret=dtls1_client_hello(s);
  217. if (ret <= 0) goto end;
  218. if ( s->d1->send_cookie)
  219. {
  220. s->state=SSL3_ST_CW_FLUSH;
  221. s->s3->tmp.next_state=SSL3_ST_CR_SRVR_HELLO_A;
  222. }
  223. else
  224. s->state=SSL3_ST_CR_SRVR_HELLO_A;
  225. s->init_num=0;
  226. /* turn on buffering for the next lot of output */
  227. if (s->bbio != s->wbio)
  228. s->wbio=BIO_push(s->bbio,s->wbio);
  229. break;
  230. case SSL3_ST_CR_SRVR_HELLO_A:
  231. case SSL3_ST_CR_SRVR_HELLO_B:
  232. ret=ssl3_get_server_hello(s);
  233. if (ret <= 0) goto end;
  234. else
  235. {
  236. dtls1_stop_timer(s);
  237. if (s->hit)
  238. s->state=SSL3_ST_CR_FINISHED_A;
  239. else
  240. s->state=DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A;
  241. }
  242. s->init_num=0;
  243. break;
  244. case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
  245. case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B:
  246. ret = dtls1_get_hello_verify(s);
  247. if ( ret <= 0)
  248. goto end;
  249. dtls1_stop_timer(s);
  250. if ( s->d1->send_cookie) /* start again, with a cookie */
  251. s->state=SSL3_ST_CW_CLNT_HELLO_A;
  252. else
  253. s->state = SSL3_ST_CR_CERT_A;
  254. s->init_num = 0;
  255. break;
  256. case SSL3_ST_CR_CERT_A:
  257. case SSL3_ST_CR_CERT_B:
  258. #ifndef OPENSSL_NO_TLSEXT
  259. ret=ssl3_check_finished(s);
  260. if (ret <= 0) goto end;
  261. if (ret == 2)
  262. {
  263. s->hit = 1;
  264. if (s->tlsext_ticket_expected)
  265. s->state=SSL3_ST_CR_SESSION_TICKET_A;
  266. else
  267. s->state=SSL3_ST_CR_FINISHED_A;
  268. s->init_num=0;
  269. break;
  270. }
  271. #endif
  272. /* Check if it is anon DH or PSK */
  273. if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
  274. !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
  275. {
  276. ret=ssl3_get_server_certificate(s);
  277. if (ret <= 0) goto end;
  278. #ifndef OPENSSL_NO_TLSEXT
  279. if (s->tlsext_status_expected)
  280. s->state=SSL3_ST_CR_CERT_STATUS_A;
  281. else
  282. s->state=SSL3_ST_CR_KEY_EXCH_A;
  283. }
  284. else
  285. {
  286. skip = 1;
  287. s->state=SSL3_ST_CR_KEY_EXCH_A;
  288. }
  289. #else
  290. }
  291. else
  292. skip=1;
  293. s->state=SSL3_ST_CR_KEY_EXCH_A;
  294. #endif
  295. s->init_num=0;
  296. break;
  297. case SSL3_ST_CR_KEY_EXCH_A:
  298. case SSL3_ST_CR_KEY_EXCH_B:
  299. ret=ssl3_get_key_exchange(s);
  300. if (ret <= 0) goto end;
  301. s->state=SSL3_ST_CR_CERT_REQ_A;
  302. s->init_num=0;
  303. /* at this point we check that we have the
  304. * required stuff from the server */
  305. if (!ssl3_check_cert_and_algorithm(s))
  306. {
  307. ret= -1;
  308. goto end;
  309. }
  310. break;
  311. case SSL3_ST_CR_CERT_REQ_A:
  312. case SSL3_ST_CR_CERT_REQ_B:
  313. ret=ssl3_get_certificate_request(s);
  314. if (ret <= 0) goto end;
  315. s->state=SSL3_ST_CR_SRVR_DONE_A;
  316. s->init_num=0;
  317. break;
  318. case SSL3_ST_CR_SRVR_DONE_A:
  319. case SSL3_ST_CR_SRVR_DONE_B:
  320. ret=ssl3_get_server_done(s);
  321. if (ret <= 0) goto end;
  322. if (s->s3->tmp.cert_req)
  323. s->state=SSL3_ST_CW_CERT_A;
  324. else
  325. s->state=SSL3_ST_CW_KEY_EXCH_A;
  326. s->init_num=0;
  327. break;
  328. case SSL3_ST_CW_CERT_A:
  329. case SSL3_ST_CW_CERT_B:
  330. case SSL3_ST_CW_CERT_C:
  331. case SSL3_ST_CW_CERT_D:
  332. dtls1_start_timer(s);
  333. ret=dtls1_send_client_certificate(s);
  334. if (ret <= 0) goto end;
  335. s->state=SSL3_ST_CW_KEY_EXCH_A;
  336. s->init_num=0;
  337. break;
  338. case SSL3_ST_CW_KEY_EXCH_A:
  339. case SSL3_ST_CW_KEY_EXCH_B:
  340. dtls1_start_timer(s);
  341. ret=dtls1_send_client_key_exchange(s);
  342. if (ret <= 0) goto end;
  343. /* EAY EAY EAY need to check for DH fix cert
  344. * sent back */
  345. /* For TLS, cert_req is set to 2, so a cert chain
  346. * of nothing is sent, but no verify packet is sent */
  347. if (s->s3->tmp.cert_req == 1)
  348. {
  349. s->state=SSL3_ST_CW_CERT_VRFY_A;
  350. }
  351. else
  352. {
  353. s->state=SSL3_ST_CW_CHANGE_A;
  354. s->s3->change_cipher_spec=0;
  355. }
  356. s->init_num=0;
  357. break;
  358. case SSL3_ST_CW_CERT_VRFY_A:
  359. case SSL3_ST_CW_CERT_VRFY_B:
  360. dtls1_start_timer(s);
  361. ret=dtls1_send_client_verify(s);
  362. if (ret <= 0) goto end;
  363. s->state=SSL3_ST_CW_CHANGE_A;
  364. s->init_num=0;
  365. s->s3->change_cipher_spec=0;
  366. break;
  367. case SSL3_ST_CW_CHANGE_A:
  368. case SSL3_ST_CW_CHANGE_B:
  369. if (!s->hit)
  370. dtls1_start_timer(s);
  371. ret=dtls1_send_change_cipher_spec(s,
  372. SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);
  373. if (ret <= 0) goto end;
  374. s->state=SSL3_ST_CW_FINISHED_A;
  375. s->init_num=0;
  376. s->session->cipher=s->s3->tmp.new_cipher;
  377. #ifdef OPENSSL_NO_COMP
  378. s->session->compress_meth=0;
  379. #else
  380. if (s->s3->tmp.new_compression == NULL)
  381. s->session->compress_meth=0;
  382. else
  383. s->session->compress_meth=
  384. s->s3->tmp.new_compression->id;
  385. #endif
  386. if (!s->method->ssl3_enc->setup_key_block(s))
  387. {
  388. ret= -1;
  389. goto end;
  390. }
  391. if (!s->method->ssl3_enc->change_cipher_state(s,
  392. SSL3_CHANGE_CIPHER_CLIENT_WRITE))
  393. {
  394. ret= -1;
  395. goto end;
  396. }
  397. dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
  398. break;
  399. case SSL3_ST_CW_FINISHED_A:
  400. case SSL3_ST_CW_FINISHED_B:
  401. if (!s->hit)
  402. dtls1_start_timer(s);
  403. ret=dtls1_send_finished(s,
  404. SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
  405. s->method->ssl3_enc->client_finished_label,
  406. s->method->ssl3_enc->client_finished_label_len);
  407. if (ret <= 0) goto end;
  408. s->state=SSL3_ST_CW_FLUSH;
  409. /* clear flags */
  410. s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
  411. if (s->hit)
  412. {
  413. s->s3->tmp.next_state=SSL_ST_OK;
  414. if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)
  415. {
  416. s->state=SSL_ST_OK;
  417. s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
  418. s->s3->delay_buf_pop_ret=0;
  419. }
  420. }
  421. else
  422. {
  423. #ifndef OPENSSL_NO_TLSEXT
  424. /* Allow NewSessionTicket if ticket expected */
  425. if (s->tlsext_ticket_expected)
  426. s->s3->tmp.next_state=SSL3_ST_CR_SESSION_TICKET_A;
  427. else
  428. #endif
  429. s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;
  430. }
  431. s->init_num=0;
  432. break;
  433. #ifndef OPENSSL_NO_TLSEXT
  434. case SSL3_ST_CR_SESSION_TICKET_A:
  435. case SSL3_ST_CR_SESSION_TICKET_B:
  436. ret=ssl3_get_new_session_ticket(s);
  437. if (ret <= 0) goto end;
  438. s->state=SSL3_ST_CR_FINISHED_A;
  439. s->init_num=0;
  440. break;
  441. case SSL3_ST_CR_CERT_STATUS_A:
  442. case SSL3_ST_CR_CERT_STATUS_B:
  443. ret=ssl3_get_cert_status(s);
  444. if (ret <= 0) goto end;
  445. s->state=SSL3_ST_CR_KEY_EXCH_A;
  446. s->init_num=0;
  447. break;
  448. #endif
  449. case SSL3_ST_CR_FINISHED_A:
  450. case SSL3_ST_CR_FINISHED_B:
  451. s->d1->change_cipher_spec_ok = 1;
  452. ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
  453. SSL3_ST_CR_FINISHED_B);
  454. if (ret <= 0) goto end;
  455. dtls1_stop_timer(s);
  456. if (s->hit)
  457. s->state=SSL3_ST_CW_CHANGE_A;
  458. else
  459. s->state=SSL_ST_OK;
  460. s->init_num=0;
  461. break;
  462. case SSL3_ST_CW_FLUSH:
  463. s->rwstate=SSL_WRITING;
  464. if (BIO_flush(s->wbio) <= 0)
  465. {
  466. ret= -1;
  467. goto end;
  468. }
  469. s->rwstate=SSL_NOTHING;
  470. s->state=s->s3->tmp.next_state;
  471. break;
  472. case SSL_ST_OK:
  473. /* clean a few things up */
  474. ssl3_cleanup_key_block(s);
  475. #if 0
  476. if (s->init_buf != NULL)
  477. {
  478. BUF_MEM_free(s->init_buf);
  479. s->init_buf=NULL;
  480. }
  481. #endif
  482. /* If we are not 'joining' the last two packets,
  483. * remove the buffering now */
  484. if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
  485. ssl_free_wbio_buffer(s);
  486. /* else do it later in ssl3_write */
  487. s->init_num=0;
  488. s->new_session=0;
  489. ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
  490. if (s->hit) s->ctx->stats.sess_hit++;
  491. ret=1;
  492. /* s->server=0; */
  493. s->handshake_func=dtls1_connect;
  494. s->ctx->stats.sess_connect_good++;
  495. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
  496. /* done with handshaking */
  497. s->d1->handshake_read_seq = 0;
  498. s->d1->next_handshake_write_seq = 0;
  499. goto end;
  500. /* break; */
  501. default:
  502. SSLerr(SSL_F_DTLS1_CONNECT,SSL_R_UNKNOWN_STATE);
  503. ret= -1;
  504. goto end;
  505. /* break; */
  506. }
  507. /* did we do anything */
  508. if (!s->s3->tmp.reuse_message && !skip)
  509. {
  510. if (s->debug)
  511. {
  512. if ((ret=BIO_flush(s->wbio)) <= 0)
  513. goto end;
  514. }
  515. if ((cb != NULL) && (s->state != state))
  516. {
  517. new_state=s->state;
  518. s->state=state;
  519. cb(s,SSL_CB_CONNECT_LOOP,1);
  520. s->state=new_state;
  521. }
  522. }
  523. skip=0;
  524. }
  525. end:
  526. s->in_handshake--;
  527. if (buf != NULL)
  528. BUF_MEM_free(buf);
  529. if (cb != NULL)
  530. cb(s,SSL_CB_CONNECT_EXIT,ret);
  531. return(ret);
  532. }
  533. int dtls1_client_hello(SSL *s)
  534. {
  535. unsigned char *buf;
  536. unsigned char *p,*d;
  537. unsigned int i,j;
  538. unsigned long Time,l;
  539. SSL_COMP *comp;
  540. buf=(unsigned char *)s->init_buf->data;
  541. if (s->state == SSL3_ST_CW_CLNT_HELLO_A)
  542. {
  543. SSL_SESSION *sess = s->session;
  544. if ((s->session == NULL) ||
  545. (s->session->ssl_version != s->version) ||
  546. #ifdef OPENSSL_NO_TLSEXT
  547. !sess->session_id_length ||
  548. #else
  549. (!sess->session_id_length && !sess->tlsext_tick) ||
  550. #endif
  551. (s->session->not_resumable))
  552. {
  553. if (!ssl_get_new_session(s,0))
  554. goto err;
  555. }
  556. /* else use the pre-loaded session */
  557. p=s->s3->client_random;
  558. /* if client_random is initialized, reuse it, we are
  559. * required to use same upon reply to HelloVerify */
  560. for (i=0;p[i]=='\0' && i<sizeof(s->s3->client_random);i++) ;
  561. if (i==sizeof(s->s3->client_random))
  562. {
  563. Time=(unsigned long)time(NULL); /* Time */
  564. l2n(Time,p);
  565. RAND_pseudo_bytes(p,sizeof(s->s3->client_random)-4);
  566. }
  567. /* Do the message type and length last */
  568. d=p= &(buf[DTLS1_HM_HEADER_LENGTH]);
  569. *(p++)=s->version>>8;
  570. *(p++)=s->version&0xff;
  571. s->client_version=s->version;
  572. /* Random stuff */
  573. memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
  574. p+=SSL3_RANDOM_SIZE;
  575. /* Session ID */
  576. if (s->new_session)
  577. i=0;
  578. else
  579. i=s->session->session_id_length;
  580. *(p++)=i;
  581. if (i != 0)
  582. {
  583. if (i > sizeof s->session->session_id)
  584. {
  585. SSLerr(SSL_F_DTLS1_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
  586. goto err;
  587. }
  588. memcpy(p,s->session->session_id,i);
  589. p+=i;
  590. }
  591. /* cookie stuff */
  592. if ( s->d1->cookie_len > sizeof(s->d1->cookie))
  593. {
  594. SSLerr(SSL_F_DTLS1_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
  595. goto err;
  596. }
  597. *(p++) = s->d1->cookie_len;
  598. memcpy(p, s->d1->cookie, s->d1->cookie_len);
  599. p += s->d1->cookie_len;
  600. /* Ciphers supported */
  601. i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]),0);
  602. if (i == 0)
  603. {
  604. SSLerr(SSL_F_DTLS1_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
  605. goto err;
  606. }
  607. s2n(i,p);
  608. p+=i;
  609. /* COMPRESSION */
  610. if (s->ctx->comp_methods == NULL)
  611. j=0;
  612. else
  613. j=sk_SSL_COMP_num(s->ctx->comp_methods);
  614. *(p++)=1+j;
  615. for (i=0; i<j; i++)
  616. {
  617. comp=sk_SSL_COMP_value(s->ctx->comp_methods,i);
  618. *(p++)=comp->id;
  619. }
  620. *(p++)=0; /* Add the NULL method */
  621. #ifndef OPENSSL_NO_TLSEXT
  622. if ((p = ssl_add_clienthello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH)) == NULL)
  623. {
  624. SSLerr(SSL_F_DTLS1_CLIENT_HELLO,ERR_R_INTERNAL_ERROR);
  625. goto err;
  626. }
  627. #endif
  628. l=(p-d);
  629. d=buf;
  630. d = dtls1_set_message_header(s, d, SSL3_MT_CLIENT_HELLO, l, 0, l);
  631. s->state=SSL3_ST_CW_CLNT_HELLO_B;
  632. /* number of bytes to write */
  633. s->init_num=p-buf;
  634. s->init_off=0;
  635. /* buffer the message to handle re-xmits */
  636. dtls1_buffer_message(s, 0);
  637. }
  638. /* SSL3_ST_CW_CLNT_HELLO_B */
  639. return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
  640. err:
  641. return(-1);
  642. }
  643. static int dtls1_get_hello_verify(SSL *s)
  644. {
  645. int n, al, ok = 0;
  646. unsigned char *data;
  647. unsigned int cookie_len;
  648. n=s->method->ssl_get_message(s,
  649. DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A,
  650. DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B,
  651. -1,
  652. s->max_cert_list,
  653. &ok);
  654. if (!ok) return((int)n);
  655. if (s->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST)
  656. {
  657. s->d1->send_cookie = 0;
  658. s->s3->tmp.reuse_message=1;
  659. return(1);
  660. }
  661. data = (unsigned char *)s->init_msg;
  662. if ((data[0] != (s->version>>8)) || (data[1] != (s->version&0xff)))
  663. {
  664. SSLerr(SSL_F_DTLS1_GET_HELLO_VERIFY,SSL_R_WRONG_SSL_VERSION);
  665. s->version=(s->version&0xff00)|data[1];
  666. al = SSL_AD_PROTOCOL_VERSION;
  667. goto f_err;
  668. }
  669. data+=2;
  670. cookie_len = *(data++);
  671. if ( cookie_len > sizeof(s->d1->cookie))
  672. {
  673. al=SSL_AD_ILLEGAL_PARAMETER;
  674. goto f_err;
  675. }
  676. memcpy(s->d1->cookie, data, cookie_len);
  677. s->d1->cookie_len = cookie_len;
  678. s->d1->send_cookie = 1;
  679. return 1;
  680. f_err:
  681. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  682. return -1;
  683. }
  684. int dtls1_send_client_key_exchange(SSL *s)
  685. {
  686. unsigned char *p,*d;
  687. int n;
  688. unsigned long alg_k;
  689. #ifndef OPENSSL_NO_RSA
  690. unsigned char *q;
  691. EVP_PKEY *pkey=NULL;
  692. #endif
  693. #ifndef OPENSSL_NO_KRB5
  694. KSSL_ERR kssl_err;
  695. #endif /* OPENSSL_NO_KRB5 */
  696. #ifndef OPENSSL_NO_ECDH
  697. EC_KEY *clnt_ecdh = NULL;
  698. const EC_POINT *srvr_ecpoint = NULL;
  699. EVP_PKEY *srvr_pub_pkey = NULL;
  700. unsigned char *encodedPoint = NULL;
  701. int encoded_pt_len = 0;
  702. BN_CTX * bn_ctx = NULL;
  703. #endif
  704. if (s->state == SSL3_ST_CW_KEY_EXCH_A)
  705. {
  706. d=(unsigned char *)s->init_buf->data;
  707. p= &(d[DTLS1_HM_HEADER_LENGTH]);
  708. alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
  709. /* Fool emacs indentation */
  710. if (0) {}
  711. #ifndef OPENSSL_NO_RSA
  712. else if (alg_k & SSL_kRSA)
  713. {
  714. RSA *rsa;
  715. unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
  716. if (s->session->sess_cert->peer_rsa_tmp != NULL)
  717. rsa=s->session->sess_cert->peer_rsa_tmp;
  718. else
  719. {
  720. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  721. if ((pkey == NULL) ||
  722. (pkey->type != EVP_PKEY_RSA) ||
  723. (pkey->pkey.rsa == NULL))
  724. {
  725. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
  726. goto err;
  727. }
  728. rsa=pkey->pkey.rsa;
  729. EVP_PKEY_free(pkey);
  730. }
  731. tmp_buf[0]=s->client_version>>8;
  732. tmp_buf[1]=s->client_version&0xff;
  733. if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
  734. goto err;
  735. s->session->master_key_length=sizeof tmp_buf;
  736. q=p;
  737. /* Fix buf for TLS and [incidentally] DTLS */
  738. if (s->version > SSL3_VERSION)
  739. p+=2;
  740. n=RSA_public_encrypt(sizeof tmp_buf,
  741. tmp_buf,p,rsa,RSA_PKCS1_PADDING);
  742. #ifdef PKCS1_CHECK
  743. if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;
  744. if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;
  745. #endif
  746. if (n <= 0)
  747. {
  748. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT);
  749. goto err;
  750. }
  751. /* Fix buf for TLS and [incidentally] DTLS */
  752. if (s->version > SSL3_VERSION)
  753. {
  754. s2n(n,q);
  755. n+=2;
  756. }
  757. s->session->master_key_length=
  758. s->method->ssl3_enc->generate_master_secret(s,
  759. s->session->master_key,
  760. tmp_buf,sizeof tmp_buf);
  761. OPENSSL_cleanse(tmp_buf,sizeof tmp_buf);
  762. }
  763. #endif
  764. #ifndef OPENSSL_NO_KRB5
  765. else if (alg_k & SSL_kKRB5)
  766. {
  767. krb5_error_code krb5rc;
  768. KSSL_CTX *kssl_ctx = s->kssl_ctx;
  769. /* krb5_data krb5_ap_req; */
  770. krb5_data *enc_ticket;
  771. krb5_data authenticator, *authp = NULL;
  772. EVP_CIPHER_CTX ciph_ctx;
  773. const EVP_CIPHER *enc = NULL;
  774. unsigned char iv[EVP_MAX_IV_LENGTH];
  775. unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
  776. unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH
  777. + EVP_MAX_IV_LENGTH];
  778. int padl, outl = sizeof(epms);
  779. EVP_CIPHER_CTX_init(&ciph_ctx);
  780. #ifdef KSSL_DEBUG
  781. printf("ssl3_send_client_key_exchange(%lx & %lx)\n",
  782. alg_k, SSL_kKRB5);
  783. #endif /* KSSL_DEBUG */
  784. authp = NULL;
  785. #ifdef KRB5SENDAUTH
  786. if (KRB5SENDAUTH) authp = &authenticator;
  787. #endif /* KRB5SENDAUTH */
  788. krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp,
  789. &kssl_err);
  790. enc = kssl_map_enc(kssl_ctx->enctype);
  791. if (enc == NULL)
  792. goto err;
  793. #ifdef KSSL_DEBUG
  794. {
  795. printf("kssl_cget_tkt rtn %d\n", krb5rc);
  796. if (krb5rc && kssl_err.text)
  797. printf("kssl_cget_tkt kssl_err=%s\n", kssl_err.text);
  798. }
  799. #endif /* KSSL_DEBUG */
  800. if (krb5rc)
  801. {
  802. ssl3_send_alert(s,SSL3_AL_FATAL,
  803. SSL_AD_HANDSHAKE_FAILURE);
  804. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  805. kssl_err.reason);
  806. goto err;
  807. }
  808. /* 20010406 VRS - Earlier versions used KRB5 AP_REQ
  809. ** in place of RFC 2712 KerberosWrapper, as in:
  810. **
  811. ** Send ticket (copy to *p, set n = length)
  812. ** n = krb5_ap_req.length;
  813. ** memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
  814. ** if (krb5_ap_req.data)
  815. ** kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
  816. **
  817. ** Now using real RFC 2712 KerberosWrapper
  818. ** (Thanks to Simon Wilkinson <[email protected]>)
  819. ** Note: 2712 "opaque" types are here replaced
  820. ** with a 2-byte length followed by the value.
  821. ** Example:
  822. ** KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
  823. ** Where "xx xx" = length bytes. Shown here with
  824. ** optional authenticator omitted.
  825. */
  826. /* KerberosWrapper.Ticket */
  827. s2n(enc_ticket->length,p);
  828. memcpy(p, enc_ticket->data, enc_ticket->length);
  829. p+= enc_ticket->length;
  830. n = enc_ticket->length + 2;
  831. /* KerberosWrapper.Authenticator */
  832. if (authp && authp->length)
  833. {
  834. s2n(authp->length,p);
  835. memcpy(p, authp->data, authp->length);
  836. p+= authp->length;
  837. n+= authp->length + 2;
  838. free(authp->data);
  839. authp->data = NULL;
  840. authp->length = 0;
  841. }
  842. else
  843. {
  844. s2n(0,p);/* null authenticator length */
  845. n+=2;
  846. }
  847. if (RAND_bytes(tmp_buf,sizeof tmp_buf) <= 0)
  848. goto err;
  849. /* 20010420 VRS. Tried it this way; failed.
  850. ** EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
  851. ** EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
  852. ** kssl_ctx->length);
  853. ** EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
  854. */
  855. memset(iv, 0, sizeof iv); /* per RFC 1510 */
  856. EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,
  857. kssl_ctx->key,iv);
  858. EVP_EncryptUpdate(&ciph_ctx,epms,&outl,tmp_buf,
  859. sizeof tmp_buf);
  860. EVP_EncryptFinal_ex(&ciph_ctx,&(epms[outl]),&padl);
  861. outl += padl;
  862. if (outl > (int)sizeof epms)
  863. {
  864. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  865. goto err;
  866. }
  867. EVP_CIPHER_CTX_cleanup(&ciph_ctx);
  868. /* KerberosWrapper.EncryptedPreMasterSecret */
  869. s2n(outl,p);
  870. memcpy(p, epms, outl);
  871. p+=outl;
  872. n+=outl + 2;
  873. s->session->master_key_length=
  874. s->method->ssl3_enc->generate_master_secret(s,
  875. s->session->master_key,
  876. tmp_buf, sizeof tmp_buf);
  877. OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
  878. OPENSSL_cleanse(epms, outl);
  879. }
  880. #endif
  881. #ifndef OPENSSL_NO_DH
  882. else if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
  883. {
  884. DH *dh_srvr,*dh_clnt;
  885. if (s->session->sess_cert->peer_dh_tmp != NULL)
  886. dh_srvr=s->session->sess_cert->peer_dh_tmp;
  887. else
  888. {
  889. /* we get them from the cert */
  890. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
  891. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
  892. goto err;
  893. }
  894. /* generate a new random key */
  895. if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
  896. {
  897. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
  898. goto err;
  899. }
  900. if (!DH_generate_key(dh_clnt))
  901. {
  902. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
  903. goto err;
  904. }
  905. /* use the 'p' output buffer for the DH key, but
  906. * make sure to clear it out afterwards */
  907. n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
  908. if (n <= 0)
  909. {
  910. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
  911. goto err;
  912. }
  913. /* generate master key from the result */
  914. s->session->master_key_length=
  915. s->method->ssl3_enc->generate_master_secret(s,
  916. s->session->master_key,p,n);
  917. /* clean up */
  918. memset(p,0,n);
  919. /* send off the data */
  920. n=BN_num_bytes(dh_clnt->pub_key);
  921. s2n(n,p);
  922. BN_bn2bin(dh_clnt->pub_key,p);
  923. n+=2;
  924. DH_free(dh_clnt);
  925. /* perhaps clean things up a bit EAY EAY EAY EAY*/
  926. }
  927. #endif
  928. #ifndef OPENSSL_NO_ECDH
  929. else if (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe))
  930. {
  931. const EC_GROUP *srvr_group = NULL;
  932. EC_KEY *tkey;
  933. int ecdh_clnt_cert = 0;
  934. int field_size = 0;
  935. /* Did we send out the client's
  936. * ECDH share for use in premaster
  937. * computation as part of client certificate?
  938. * If so, set ecdh_clnt_cert to 1.
  939. */
  940. if ((alg_k & (SSL_kECDHr|SSL_kECDHe)) && (s->cert != NULL))
  941. {
  942. /* XXX: For now, we do not support client
  943. * authentication using ECDH certificates.
  944. * To add such support, one needs to add
  945. * code that checks for appropriate
  946. * conditions and sets ecdh_clnt_cert to 1.
  947. * For example, the cert have an ECC
  948. * key on the same curve as the server's
  949. * and the key should be authorized for
  950. * key agreement.
  951. *
  952. * One also needs to add code in ssl3_connect
  953. * to skip sending the certificate verify
  954. * message.
  955. *
  956. * if ((s->cert->key->privatekey != NULL) &&
  957. * (s->cert->key->privatekey->type ==
  958. * EVP_PKEY_EC) && ...)
  959. * ecdh_clnt_cert = 1;
  960. */
  961. }
  962. if (s->session->sess_cert->peer_ecdh_tmp != NULL)
  963. {
  964. tkey = s->session->sess_cert->peer_ecdh_tmp;
  965. }
  966. else
  967. {
  968. /* Get the Server Public Key from Cert */
  969. srvr_pub_pkey = X509_get_pubkey(s->session-> \
  970. sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
  971. if ((srvr_pub_pkey == NULL) ||
  972. (srvr_pub_pkey->type != EVP_PKEY_EC) ||
  973. (srvr_pub_pkey->pkey.ec == NULL))
  974. {
  975. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  976. ERR_R_INTERNAL_ERROR);
  977. goto err;
  978. }
  979. tkey = srvr_pub_pkey->pkey.ec;
  980. }
  981. srvr_group = EC_KEY_get0_group(tkey);
  982. srvr_ecpoint = EC_KEY_get0_public_key(tkey);
  983. if ((srvr_group == NULL) || (srvr_ecpoint == NULL))
  984. {
  985. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  986. ERR_R_INTERNAL_ERROR);
  987. goto err;
  988. }
  989. if ((clnt_ecdh=EC_KEY_new()) == NULL)
  990. {
  991. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  992. goto err;
  993. }
  994. if (!EC_KEY_set_group(clnt_ecdh, srvr_group))
  995. {
  996. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB);
  997. goto err;
  998. }
  999. if (ecdh_clnt_cert)
  1000. {
  1001. /* Reuse key info from our certificate
  1002. * We only need our private key to perform
  1003. * the ECDH computation.
  1004. */
  1005. const BIGNUM *priv_key;
  1006. tkey = s->cert->key->privatekey->pkey.ec;
  1007. priv_key = EC_KEY_get0_private_key(tkey);
  1008. if (priv_key == NULL)
  1009. {
  1010. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  1011. goto err;
  1012. }
  1013. if (!EC_KEY_set_private_key(clnt_ecdh, priv_key))
  1014. {
  1015. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB);
  1016. goto err;
  1017. }
  1018. }
  1019. else
  1020. {
  1021. /* Generate a new ECDH key pair */
  1022. if (!(EC_KEY_generate_key(clnt_ecdh)))
  1023. {
  1024. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
  1025. goto err;
  1026. }
  1027. }
  1028. /* use the 'p' output buffer for the ECDH key, but
  1029. * make sure to clear it out afterwards
  1030. */
  1031. field_size = EC_GROUP_get_degree(srvr_group);
  1032. if (field_size <= 0)
  1033. {
  1034. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  1035. ERR_R_ECDH_LIB);
  1036. goto err;
  1037. }
  1038. n=ECDH_compute_key(p, (field_size+7)/8, srvr_ecpoint, clnt_ecdh, NULL);
  1039. if (n <= 0)
  1040. {
  1041. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  1042. ERR_R_ECDH_LIB);
  1043. goto err;
  1044. }
  1045. /* generate master key from the result */
  1046. s->session->master_key_length = s->method->ssl3_enc \
  1047. -> generate_master_secret(s,
  1048. s->session->master_key,
  1049. p, n);
  1050. memset(p, 0, n); /* clean up */
  1051. if (ecdh_clnt_cert)
  1052. {
  1053. /* Send empty client key exch message */
  1054. n = 0;
  1055. }
  1056. else
  1057. {
  1058. /* First check the size of encoding and
  1059. * allocate memory accordingly.
  1060. */
  1061. encoded_pt_len =
  1062. EC_POINT_point2oct(srvr_group,
  1063. EC_KEY_get0_public_key(clnt_ecdh),
  1064. POINT_CONVERSION_UNCOMPRESSED,
  1065. NULL, 0, NULL);
  1066. encodedPoint = (unsigned char *)
  1067. OPENSSL_malloc(encoded_pt_len *
  1068. sizeof(unsigned char));
  1069. bn_ctx = BN_CTX_new();
  1070. if ((encodedPoint == NULL) ||
  1071. (bn_ctx == NULL))
  1072. {
  1073. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  1074. goto err;
  1075. }
  1076. /* Encode the public key */
  1077. n = EC_POINT_point2oct(srvr_group,
  1078. EC_KEY_get0_public_key(clnt_ecdh),
  1079. POINT_CONVERSION_UNCOMPRESSED,
  1080. encodedPoint, encoded_pt_len, bn_ctx);
  1081. *p = n; /* length of encoded point */
  1082. /* Encoded point will be copied here */
  1083. p += 1;
  1084. /* copy the point */
  1085. memcpy((unsigned char *)p, encodedPoint, n);
  1086. /* increment n to account for length field */
  1087. n += 1;
  1088. }
  1089. /* Free allocated memory */
  1090. BN_CTX_free(bn_ctx);
  1091. if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
  1092. if (clnt_ecdh != NULL)
  1093. EC_KEY_free(clnt_ecdh);
  1094. EVP_PKEY_free(srvr_pub_pkey);
  1095. }
  1096. #endif /* !OPENSSL_NO_ECDH */
  1097. #ifndef OPENSSL_NO_PSK
  1098. else if (alg_k & SSL_kPSK)
  1099. {
  1100. char identity[PSK_MAX_IDENTITY_LEN];
  1101. unsigned char *t = NULL;
  1102. unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN*2+4];
  1103. unsigned int pre_ms_len = 0, psk_len = 0;
  1104. int psk_err = 1;
  1105. n = 0;
  1106. if (s->psk_client_callback == NULL)
  1107. {
  1108. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  1109. SSL_R_PSK_NO_CLIENT_CB);
  1110. goto err;
  1111. }
  1112. psk_len = s->psk_client_callback(s, s->ctx->psk_identity_hint,
  1113. identity, PSK_MAX_IDENTITY_LEN,
  1114. psk_or_pre_ms, sizeof(psk_or_pre_ms));
  1115. if (psk_len > PSK_MAX_PSK_LEN)
  1116. {
  1117. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  1118. ERR_R_INTERNAL_ERROR);
  1119. goto psk_err;
  1120. }
  1121. else if (psk_len == 0)
  1122. {
  1123. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  1124. SSL_R_PSK_IDENTITY_NOT_FOUND);
  1125. goto psk_err;
  1126. }
  1127. /* create PSK pre_master_secret */
  1128. pre_ms_len = 2+psk_len+2+psk_len;
  1129. t = psk_or_pre_ms;
  1130. memmove(psk_or_pre_ms+psk_len+4, psk_or_pre_ms, psk_len);
  1131. s2n(psk_len, t);
  1132. memset(t, 0, psk_len);
  1133. t+=psk_len;
  1134. s2n(psk_len, t);
  1135. if (s->session->psk_identity_hint != NULL)
  1136. OPENSSL_free(s->session->psk_identity_hint);
  1137. s->session->psk_identity_hint = BUF_strdup(s->ctx->psk_identity_hint);
  1138. if (s->ctx->psk_identity_hint != NULL &&
  1139. s->session->psk_identity_hint == NULL)
  1140. {
  1141. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  1142. ERR_R_MALLOC_FAILURE);
  1143. goto psk_err;
  1144. }
  1145. if (s->session->psk_identity != NULL)
  1146. OPENSSL_free(s->session->psk_identity);
  1147. s->session->psk_identity = BUF_strdup(identity);
  1148. if (s->session->psk_identity == NULL)
  1149. {
  1150. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  1151. ERR_R_MALLOC_FAILURE);
  1152. goto psk_err;
  1153. }
  1154. s->session->master_key_length =
  1155. s->method->ssl3_enc->generate_master_secret(s,
  1156. s->session->master_key,
  1157. psk_or_pre_ms, pre_ms_len);
  1158. n = strlen(identity);
  1159. s2n(n, p);
  1160. memcpy(p, identity, n);
  1161. n+=2;
  1162. psk_err = 0;
  1163. psk_err:
  1164. OPENSSL_cleanse(identity, PSK_MAX_IDENTITY_LEN);
  1165. OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
  1166. if (psk_err != 0)
  1167. {
  1168. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1169. goto err;
  1170. }
  1171. }
  1172. #endif
  1173. else
  1174. {
  1175. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
  1176. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
  1177. goto err;
  1178. }
  1179. d = dtls1_set_message_header(s, d,
  1180. SSL3_MT_CLIENT_KEY_EXCHANGE, n, 0, n);
  1181. /*
  1182. *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE;
  1183. l2n3(n,d);
  1184. l2n(s->d1->handshake_write_seq,d);
  1185. s->d1->handshake_write_seq++;
  1186. */
  1187. s->state=SSL3_ST_CW_KEY_EXCH_B;
  1188. /* number of bytes to write */
  1189. s->init_num=n+DTLS1_HM_HEADER_LENGTH;
  1190. s->init_off=0;
  1191. /* buffer the message to handle re-xmits */
  1192. dtls1_buffer_message(s, 0);
  1193. }
  1194. /* SSL3_ST_CW_KEY_EXCH_B */
  1195. return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
  1196. err:
  1197. #ifndef OPENSSL_NO_ECDH
  1198. BN_CTX_free(bn_ctx);
  1199. if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
  1200. if (clnt_ecdh != NULL)
  1201. EC_KEY_free(clnt_ecdh);
  1202. EVP_PKEY_free(srvr_pub_pkey);
  1203. #endif
  1204. return(-1);
  1205. }
  1206. int dtls1_send_client_verify(SSL *s)
  1207. {
  1208. unsigned char *p,*d;
  1209. unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
  1210. EVP_PKEY *pkey;
  1211. #ifndef OPENSSL_NO_RSA
  1212. unsigned u=0;
  1213. #endif
  1214. unsigned long n;
  1215. #if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA)
  1216. int j;
  1217. #endif
  1218. if (s->state == SSL3_ST_CW_CERT_VRFY_A)
  1219. {
  1220. d=(unsigned char *)s->init_buf->data;
  1221. p= &(d[DTLS1_HM_HEADER_LENGTH]);
  1222. pkey=s->cert->key->privatekey;
  1223. s->method->ssl3_enc->cert_verify_mac(s,
  1224. NID_sha1,
  1225. &(data[MD5_DIGEST_LENGTH]));
  1226. #ifndef OPENSSL_NO_RSA
  1227. if (pkey->type == EVP_PKEY_RSA)
  1228. {
  1229. s->method->ssl3_enc->cert_verify_mac(s,
  1230. NID_md5,
  1231. &(data[0]));
  1232. if (RSA_sign(NID_md5_sha1, data,
  1233. MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
  1234. &(p[2]), &u, pkey->pkey.rsa) <= 0 )
  1235. {
  1236. SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY,ERR_R_RSA_LIB);
  1237. goto err;
  1238. }
  1239. s2n(u,p);
  1240. n=u+2;
  1241. }
  1242. else
  1243. #endif
  1244. #ifndef OPENSSL_NO_DSA
  1245. if (pkey->type == EVP_PKEY_DSA)
  1246. {
  1247. if (!DSA_sign(pkey->save_type,
  1248. &(data[MD5_DIGEST_LENGTH]),
  1249. SHA_DIGEST_LENGTH,&(p[2]),
  1250. (unsigned int *)&j,pkey->pkey.dsa))
  1251. {
  1252. SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY,ERR_R_DSA_LIB);
  1253. goto err;
  1254. }
  1255. s2n(j,p);
  1256. n=j+2;
  1257. }
  1258. else
  1259. #endif
  1260. #ifndef OPENSSL_NO_ECDSA
  1261. if (pkey->type == EVP_PKEY_EC)
  1262. {
  1263. if (!ECDSA_sign(pkey->save_type,
  1264. &(data[MD5_DIGEST_LENGTH]),
  1265. SHA_DIGEST_LENGTH,&(p[2]),
  1266. (unsigned int *)&j,pkey->pkey.ec))
  1267. {
  1268. SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY,
  1269. ERR_R_ECDSA_LIB);
  1270. goto err;
  1271. }
  1272. s2n(j,p);
  1273. n=j+2;
  1274. }
  1275. else
  1276. #endif
  1277. {
  1278. SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY,ERR_R_INTERNAL_ERROR);
  1279. goto err;
  1280. }
  1281. d = dtls1_set_message_header(s, d,
  1282. SSL3_MT_CERTIFICATE_VERIFY, n, 0, n) ;
  1283. s->init_num=(int)n+DTLS1_HM_HEADER_LENGTH;
  1284. s->init_off=0;
  1285. /* buffer the message to handle re-xmits */
  1286. dtls1_buffer_message(s, 0);
  1287. s->state = SSL3_ST_CW_CERT_VRFY_B;
  1288. }
  1289. /* s->state = SSL3_ST_CW_CERT_VRFY_B */
  1290. return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
  1291. err:
  1292. return(-1);
  1293. }
  1294. int dtls1_send_client_certificate(SSL *s)
  1295. {
  1296. X509 *x509=NULL;
  1297. EVP_PKEY *pkey=NULL;
  1298. int i;
  1299. unsigned long l;
  1300. if (s->state == SSL3_ST_CW_CERT_A)
  1301. {
  1302. if ((s->cert == NULL) ||
  1303. (s->cert->key->x509 == NULL) ||
  1304. (s->cert->key->privatekey == NULL))
  1305. s->state=SSL3_ST_CW_CERT_B;
  1306. else
  1307. s->state=SSL3_ST_CW_CERT_C;
  1308. }
  1309. /* We need to get a client cert */
  1310. if (s->state == SSL3_ST_CW_CERT_B)
  1311. {
  1312. /* If we get an error, we need to
  1313. * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
  1314. * We then get retied later */
  1315. i=0;
  1316. i = ssl_do_client_cert_cb(s, &x509, &pkey);
  1317. if (i < 0)
  1318. {
  1319. s->rwstate=SSL_X509_LOOKUP;
  1320. return(-1);
  1321. }
  1322. s->rwstate=SSL_NOTHING;
  1323. if ((i == 1) && (pkey != NULL) && (x509 != NULL))
  1324. {
  1325. s->state=SSL3_ST_CW_CERT_B;
  1326. if ( !SSL_use_certificate(s,x509) ||
  1327. !SSL_use_PrivateKey(s,pkey))
  1328. i=0;
  1329. }
  1330. else if (i == 1)
  1331. {
  1332. i=0;
  1333. SSLerr(SSL_F_DTLS1_SEND_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
  1334. }
  1335. if (x509 != NULL) X509_free(x509);
  1336. if (pkey != NULL) EVP_PKEY_free(pkey);
  1337. if (i == 0)
  1338. {
  1339. if (s->version == SSL3_VERSION)
  1340. {
  1341. s->s3->tmp.cert_req=0;
  1342. ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
  1343. return(1);
  1344. }
  1345. else
  1346. {
  1347. s->s3->tmp.cert_req=2;
  1348. }
  1349. }
  1350. /* Ok, we have a cert */
  1351. s->state=SSL3_ST_CW_CERT_C;
  1352. }
  1353. if (s->state == SSL3_ST_CW_CERT_C)
  1354. {
  1355. s->state=SSL3_ST_CW_CERT_D;
  1356. l=dtls1_output_cert_chain(s,
  1357. (s->s3->tmp.cert_req == 2)?NULL:s->cert->key->x509);
  1358. s->init_num=(int)l;
  1359. s->init_off=0;
  1360. /* set header called by dtls1_output_cert_chain() */
  1361. /* buffer the message to handle re-xmits */
  1362. dtls1_buffer_message(s, 0);
  1363. }
  1364. /* SSL3_ST_CW_CERT_D */
  1365. return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
  1366. }