d1_clnt.c 39 KB

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