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 "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. long num1;
  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->new_session=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. 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. dtls1_start_timer(s);
  402. ret=dtls1_send_finished(s,
  403. SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
  404. s->method->ssl3_enc->client_finished_label,
  405. s->method->ssl3_enc->client_finished_label_len);
  406. if (ret <= 0) goto end;
  407. s->state=SSL3_ST_CW_FLUSH;
  408. /* clear flags */
  409. s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
  410. if (s->hit)
  411. {
  412. s->s3->tmp.next_state=SSL_ST_OK;
  413. if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)
  414. {
  415. s->state=SSL_ST_OK;
  416. s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
  417. s->s3->delay_buf_pop_ret=0;
  418. }
  419. }
  420. else
  421. {
  422. #ifndef OPENSSL_NO_TLSEXT
  423. /* Allow NewSessionTicket if ticket expected */
  424. if (s->tlsext_ticket_expected)
  425. s->s3->tmp.next_state=SSL3_ST_CR_SESSION_TICKET_A;
  426. else
  427. #endif
  428. s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;
  429. }
  430. s->init_num=0;
  431. break;
  432. #ifndef OPENSSL_NO_TLSEXT
  433. case SSL3_ST_CR_SESSION_TICKET_A:
  434. case SSL3_ST_CR_SESSION_TICKET_B:
  435. ret=ssl3_get_new_session_ticket(s);
  436. if (ret <= 0) goto end;
  437. s->state=SSL3_ST_CR_FINISHED_A;
  438. s->init_num=0;
  439. break;
  440. case SSL3_ST_CR_CERT_STATUS_A:
  441. case SSL3_ST_CR_CERT_STATUS_B:
  442. ret=ssl3_get_cert_status(s);
  443. if (ret <= 0) goto end;
  444. s->state=SSL3_ST_CR_KEY_EXCH_A;
  445. s->init_num=0;
  446. break;
  447. #endif
  448. case SSL3_ST_CR_FINISHED_A:
  449. case SSL3_ST_CR_FINISHED_B:
  450. s->d1->change_cipher_spec_ok = 1;
  451. ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
  452. SSL3_ST_CR_FINISHED_B);
  453. if (ret <= 0) goto end;
  454. dtls1_stop_timer(s);
  455. if (s->hit)
  456. s->state=SSL3_ST_CW_CHANGE_A;
  457. else
  458. s->state=SSL_ST_OK;
  459. s->init_num=0;
  460. break;
  461. case SSL3_ST_CW_FLUSH:
  462. /* number of bytes to be flushed */
  463. num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
  464. if (num1 > 0)
  465. {
  466. s->rwstate=SSL_WRITING;
  467. num1=BIO_flush(s->wbio);
  468. if (num1 <= 0) { ret= -1; goto end; }
  469. s->rwstate=SSL_NOTHING;
  470. }
  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->new_session=0;
  490. ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
  491. if (s->hit) s->ctx->stats.sess_hit++;
  492. ret=1;
  493. /* s->server=0; */
  494. s->handshake_func=dtls1_connect;
  495. s->ctx->stats.sess_connect_good++;
  496. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
  497. /* done with handshaking */
  498. s->d1->handshake_read_seq = 0;
  499. s->d1->next_handshake_write_seq = 0;
  500. goto end;
  501. /* break; */
  502. default:
  503. SSLerr(SSL_F_DTLS1_CONNECT,SSL_R_UNKNOWN_STATE);
  504. ret= -1;
  505. goto end;
  506. /* break; */
  507. }
  508. /* did we do anything */
  509. if (!s->s3->tmp.reuse_message && !skip)
  510. {
  511. if (s->debug)
  512. {
  513. if ((ret=BIO_flush(s->wbio)) <= 0)
  514. goto end;
  515. }
  516. if ((cb != NULL) && (s->state != state))
  517. {
  518. new_state=s->state;
  519. s->state=state;
  520. cb(s,SSL_CB_CONNECT_LOOP,1);
  521. s->state=new_state;
  522. }
  523. }
  524. skip=0;
  525. }
  526. end:
  527. s->in_handshake--;
  528. if (buf != NULL)
  529. BUF_MEM_free(buf);
  530. if (cb != NULL)
  531. cb(s,SSL_CB_CONNECT_EXIT,ret);
  532. return(ret);
  533. }
  534. int dtls1_client_hello(SSL *s)
  535. {
  536. unsigned char *buf;
  537. unsigned char *p,*d;
  538. unsigned int i,j;
  539. unsigned long Time,l;
  540. SSL_COMP *comp;
  541. buf=(unsigned char *)s->init_buf->data;
  542. if (s->state == SSL3_ST_CW_CLNT_HELLO_A)
  543. {
  544. SSL_SESSION *sess = s->session;
  545. if ((s->session == NULL) ||
  546. (s->session->ssl_version != s->version) ||
  547. #ifdef OPENSSL_NO_TLSEXT
  548. !sess->session_id_length ||
  549. #else
  550. (!sess->session_id_length && !sess->tlsext_tick) ||
  551. #endif
  552. (s->session->not_resumable))
  553. {
  554. if (!ssl_get_new_session(s,0))
  555. goto err;
  556. }
  557. /* else use the pre-loaded session */
  558. p=s->s3->client_random;
  559. /* if client_random is initialized, reuse it, we are
  560. * required to use same upon reply to HelloVerify */
  561. for (i=0;p[i]=='\0' && i<sizeof(s->s3->client_random);i++) ;
  562. if (i==sizeof(s->s3->client_random))
  563. {
  564. Time=(unsigned long)time(NULL); /* Time */
  565. l2n(Time,p);
  566. RAND_pseudo_bytes(p,sizeof(s->s3->client_random)-4);
  567. }
  568. /* Do the message type and length last */
  569. d=p= &(buf[DTLS1_HM_HEADER_LENGTH]);
  570. *(p++)=s->version>>8;
  571. *(p++)=s->version&0xff;
  572. s->client_version=s->version;
  573. /* Random stuff */
  574. memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
  575. p+=SSL3_RANDOM_SIZE;
  576. /* Session ID */
  577. if (s->new_session)
  578. i=0;
  579. else
  580. i=s->session->session_id_length;
  581. *(p++)=i;
  582. if (i != 0)
  583. {
  584. if (i > sizeof s->session->session_id)
  585. {
  586. SSLerr(SSL_F_DTLS1_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
  587. goto err;
  588. }
  589. memcpy(p,s->session->session_id,i);
  590. p+=i;
  591. }
  592. /* cookie stuff */
  593. if ( s->d1->cookie_len > sizeof(s->d1->cookie))
  594. {
  595. SSLerr(SSL_F_DTLS1_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
  596. goto err;
  597. }
  598. *(p++) = s->d1->cookie_len;
  599. memcpy(p, s->d1->cookie, s->d1->cookie_len);
  600. p += s->d1->cookie_len;
  601. /* Ciphers supported */
  602. i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]),0);
  603. if (i == 0)
  604. {
  605. SSLerr(SSL_F_DTLS1_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
  606. goto err;
  607. }
  608. s2n(i,p);
  609. p+=i;
  610. /* COMPRESSION */
  611. if (s->ctx->comp_methods == NULL)
  612. j=0;
  613. else
  614. j=sk_SSL_COMP_num(s->ctx->comp_methods);
  615. *(p++)=1+j;
  616. for (i=0; i<j; i++)
  617. {
  618. comp=sk_SSL_COMP_value(s->ctx->comp_methods,i);
  619. *(p++)=comp->id;
  620. }
  621. *(p++)=0; /* Add the NULL method */
  622. #ifndef OPENSSL_NO_TLSEXT
  623. if ((p = ssl_add_clienthello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH)) == NULL)
  624. {
  625. SSLerr(SSL_F_DTLS1_CLIENT_HELLO,ERR_R_INTERNAL_ERROR);
  626. goto err;
  627. }
  628. #endif
  629. l=(p-d);
  630. d=buf;
  631. d = dtls1_set_message_header(s, d, SSL3_MT_CLIENT_HELLO, l, 0, l);
  632. s->state=SSL3_ST_CW_CLNT_HELLO_B;
  633. /* number of bytes to write */
  634. s->init_num=p-buf;
  635. s->init_off=0;
  636. /* buffer the message to handle re-xmits */
  637. dtls1_buffer_message(s, 0);
  638. }
  639. /* SSL3_ST_CW_CLNT_HELLO_B */
  640. return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
  641. err:
  642. return(-1);
  643. }
  644. static int dtls1_get_hello_verify(SSL *s)
  645. {
  646. int n, al, ok = 0;
  647. unsigned char *data;
  648. unsigned int cookie_len;
  649. n=s->method->ssl_get_message(s,
  650. DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A,
  651. DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B,
  652. -1,
  653. s->max_cert_list,
  654. &ok);
  655. if (!ok) return((int)n);
  656. if (s->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST)
  657. {
  658. s->d1->send_cookie = 0;
  659. s->s3->tmp.reuse_message=1;
  660. return(1);
  661. }
  662. data = (unsigned char *)s->init_msg;
  663. if ((data[0] != (s->version>>8)) || (data[1] != (s->version&0xff)))
  664. {
  665. SSLerr(SSL_F_DTLS1_GET_HELLO_VERIFY,SSL_R_WRONG_SSL_VERSION);
  666. s->version=(s->version&0xff00)|data[1];
  667. al = SSL_AD_PROTOCOL_VERSION;
  668. goto f_err;
  669. }
  670. data+=2;
  671. cookie_len = *(data++);
  672. if ( cookie_len > sizeof(s->d1->cookie))
  673. {
  674. al=SSL_AD_ILLEGAL_PARAMETER;
  675. goto f_err;
  676. }
  677. memcpy(s->d1->cookie, data, cookie_len);
  678. s->d1->cookie_len = cookie_len;
  679. s->d1->send_cookie = 1;
  680. return 1;
  681. f_err:
  682. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  683. return -1;
  684. }
  685. int dtls1_send_client_key_exchange(SSL *s)
  686. {
  687. unsigned char *p,*d;
  688. int n;
  689. unsigned long alg_k;
  690. #ifndef OPENSSL_NO_RSA
  691. unsigned char *q;
  692. EVP_PKEY *pkey=NULL;
  693. #endif
  694. #ifndef OPENSSL_NO_KRB5
  695. KSSL_ERR kssl_err;
  696. #endif /* OPENSSL_NO_KRB5 */
  697. #ifndef OPENSSL_NO_ECDH
  698. EC_KEY *clnt_ecdh = NULL;
  699. const EC_POINT *srvr_ecpoint = NULL;
  700. EVP_PKEY *srvr_pub_pkey = NULL;
  701. unsigned char *encodedPoint = NULL;
  702. int encoded_pt_len = 0;
  703. BN_CTX * bn_ctx = NULL;
  704. #endif
  705. if (s->state == SSL3_ST_CW_KEY_EXCH_A)
  706. {
  707. d=(unsigned char *)s->init_buf->data;
  708. p= &(d[DTLS1_HM_HEADER_LENGTH]);
  709. alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
  710. /* Fool emacs indentation */
  711. if (0) {}
  712. #ifndef OPENSSL_NO_RSA
  713. else if (alg_k & SSL_kRSA)
  714. {
  715. RSA *rsa;
  716. unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
  717. if (s->session->sess_cert->peer_rsa_tmp != NULL)
  718. rsa=s->session->sess_cert->peer_rsa_tmp;
  719. else
  720. {
  721. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  722. if ((pkey == NULL) ||
  723. (pkey->type != EVP_PKEY_RSA) ||
  724. (pkey->pkey.rsa == NULL))
  725. {
  726. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
  727. goto err;
  728. }
  729. rsa=pkey->pkey.rsa;
  730. EVP_PKEY_free(pkey);
  731. }
  732. tmp_buf[0]=s->client_version>>8;
  733. tmp_buf[1]=s->client_version&0xff;
  734. if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
  735. goto err;
  736. s->session->master_key_length=sizeof tmp_buf;
  737. q=p;
  738. /* Fix buf for TLS and [incidentally] DTLS */
  739. if (s->version > SSL3_VERSION)
  740. p+=2;
  741. n=RSA_public_encrypt(sizeof tmp_buf,
  742. tmp_buf,p,rsa,RSA_PKCS1_PADDING);
  743. #ifdef PKCS1_CHECK
  744. if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;
  745. if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;
  746. #endif
  747. if (n <= 0)
  748. {
  749. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT);
  750. goto err;
  751. }
  752. /* Fix buf for TLS and [incidentally] DTLS */
  753. if (s->version > SSL3_VERSION)
  754. {
  755. s2n(n,q);
  756. n+=2;
  757. }
  758. s->session->master_key_length=
  759. s->method->ssl3_enc->generate_master_secret(s,
  760. s->session->master_key,
  761. tmp_buf,sizeof tmp_buf);
  762. OPENSSL_cleanse(tmp_buf,sizeof tmp_buf);
  763. }
  764. #endif
  765. #ifndef OPENSSL_NO_KRB5
  766. else if (alg_k & SSL_kKRB5)
  767. {
  768. krb5_error_code krb5rc;
  769. KSSL_CTX *kssl_ctx = s->kssl_ctx;
  770. /* krb5_data krb5_ap_req; */
  771. krb5_data *enc_ticket;
  772. krb5_data authenticator, *authp = NULL;
  773. EVP_CIPHER_CTX ciph_ctx;
  774. const EVP_CIPHER *enc = NULL;
  775. unsigned char iv[EVP_MAX_IV_LENGTH];
  776. unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
  777. unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH
  778. + EVP_MAX_IV_LENGTH];
  779. int padl, outl = sizeof(epms);
  780. EVP_CIPHER_CTX_init(&ciph_ctx);
  781. #ifdef KSSL_DEBUG
  782. printf("ssl3_send_client_key_exchange(%lx & %lx)\n",
  783. alg_k, SSL_kKRB5);
  784. #endif /* KSSL_DEBUG */
  785. authp = NULL;
  786. #ifdef KRB5SENDAUTH
  787. if (KRB5SENDAUTH) authp = &authenticator;
  788. #endif /* KRB5SENDAUTH */
  789. krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp,
  790. &kssl_err);
  791. enc = kssl_map_enc(kssl_ctx->enctype);
  792. if (enc == NULL)
  793. goto err;
  794. #ifdef KSSL_DEBUG
  795. {
  796. printf("kssl_cget_tkt rtn %d\n", krb5rc);
  797. if (krb5rc && kssl_err.text)
  798. printf("kssl_cget_tkt kssl_err=%s\n", kssl_err.text);
  799. }
  800. #endif /* KSSL_DEBUG */
  801. if (krb5rc)
  802. {
  803. ssl3_send_alert(s,SSL3_AL_FATAL,
  804. SSL_AD_HANDSHAKE_FAILURE);
  805. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  806. kssl_err.reason);
  807. goto err;
  808. }
  809. /* 20010406 VRS - Earlier versions used KRB5 AP_REQ
  810. ** in place of RFC 2712 KerberosWrapper, as in:
  811. **
  812. ** Send ticket (copy to *p, set n = length)
  813. ** n = krb5_ap_req.length;
  814. ** memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
  815. ** if (krb5_ap_req.data)
  816. ** kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
  817. **
  818. ** Now using real RFC 2712 KerberosWrapper
  819. ** (Thanks to Simon Wilkinson <sxw@sxw.org.uk>)
  820. ** Note: 2712 "opaque" types are here replaced
  821. ** with a 2-byte length followed by the value.
  822. ** Example:
  823. ** KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
  824. ** Where "xx xx" = length bytes. Shown here with
  825. ** optional authenticator omitted.
  826. */
  827. /* KerberosWrapper.Ticket */
  828. s2n(enc_ticket->length,p);
  829. memcpy(p, enc_ticket->data, enc_ticket->length);
  830. p+= enc_ticket->length;
  831. n = enc_ticket->length + 2;
  832. /* KerberosWrapper.Authenticator */
  833. if (authp && authp->length)
  834. {
  835. s2n(authp->length,p);
  836. memcpy(p, authp->data, authp->length);
  837. p+= authp->length;
  838. n+= authp->length + 2;
  839. free(authp->data);
  840. authp->data = NULL;
  841. authp->length = 0;
  842. }
  843. else
  844. {
  845. s2n(0,p);/* null authenticator length */
  846. n+=2;
  847. }
  848. if (RAND_bytes(tmp_buf,sizeof tmp_buf) <= 0)
  849. goto err;
  850. /* 20010420 VRS. Tried it this way; failed.
  851. ** EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
  852. ** EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
  853. ** kssl_ctx->length);
  854. ** EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
  855. */
  856. memset(iv, 0, sizeof iv); /* per RFC 1510 */
  857. EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,
  858. kssl_ctx->key,iv);
  859. EVP_EncryptUpdate(&ciph_ctx,epms,&outl,tmp_buf,
  860. sizeof tmp_buf);
  861. EVP_EncryptFinal_ex(&ciph_ctx,&(epms[outl]),&padl);
  862. outl += padl;
  863. if (outl > (int)sizeof epms)
  864. {
  865. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  866. goto err;
  867. }
  868. EVP_CIPHER_CTX_cleanup(&ciph_ctx);
  869. /* KerberosWrapper.EncryptedPreMasterSecret */
  870. s2n(outl,p);
  871. memcpy(p, epms, outl);
  872. p+=outl;
  873. n+=outl + 2;
  874. s->session->master_key_length=
  875. s->method->ssl3_enc->generate_master_secret(s,
  876. s->session->master_key,
  877. tmp_buf, sizeof tmp_buf);
  878. OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
  879. OPENSSL_cleanse(epms, outl);
  880. }
  881. #endif
  882. #ifndef OPENSSL_NO_DH
  883. else if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
  884. {
  885. DH *dh_srvr,*dh_clnt;
  886. if (s->session->sess_cert->peer_dh_tmp != NULL)
  887. dh_srvr=s->session->sess_cert->peer_dh_tmp;
  888. else
  889. {
  890. /* we get them from the cert */
  891. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
  892. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
  893. goto err;
  894. }
  895. /* generate a new random key */
  896. if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
  897. {
  898. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
  899. goto err;
  900. }
  901. if (!DH_generate_key(dh_clnt))
  902. {
  903. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
  904. goto err;
  905. }
  906. /* use the 'p' output buffer for the DH key, but
  907. * make sure to clear it out afterwards */
  908. n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
  909. if (n <= 0)
  910. {
  911. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
  912. goto err;
  913. }
  914. /* generate master key from the result */
  915. s->session->master_key_length=
  916. s->method->ssl3_enc->generate_master_secret(s,
  917. s->session->master_key,p,n);
  918. /* clean up */
  919. memset(p,0,n);
  920. /* send off the data */
  921. n=BN_num_bytes(dh_clnt->pub_key);
  922. s2n(n,p);
  923. BN_bn2bin(dh_clnt->pub_key,p);
  924. n+=2;
  925. DH_free(dh_clnt);
  926. /* perhaps clean things up a bit EAY EAY EAY EAY*/
  927. }
  928. #endif
  929. #ifndef OPENSSL_NO_ECDH
  930. else if (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe))
  931. {
  932. const EC_GROUP *srvr_group = NULL;
  933. EC_KEY *tkey;
  934. int ecdh_clnt_cert = 0;
  935. int field_size = 0;
  936. /* Did we send out the client's
  937. * ECDH share for use in premaster
  938. * computation as part of client certificate?
  939. * If so, set ecdh_clnt_cert to 1.
  940. */
  941. if ((alg_k & (SSL_kECDHr|SSL_kECDHe)) && (s->cert != NULL))
  942. {
  943. /* XXX: For now, we do not support client
  944. * authentication using ECDH certificates.
  945. * To add such support, one needs to add
  946. * code that checks for appropriate
  947. * conditions and sets ecdh_clnt_cert to 1.
  948. * For example, the cert have an ECC
  949. * key on the same curve as the server's
  950. * and the key should be authorized for
  951. * key agreement.
  952. *
  953. * One also needs to add code in ssl3_connect
  954. * to skip sending the certificate verify
  955. * message.
  956. *
  957. * if ((s->cert->key->privatekey != NULL) &&
  958. * (s->cert->key->privatekey->type ==
  959. * EVP_PKEY_EC) && ...)
  960. * ecdh_clnt_cert = 1;
  961. */
  962. }
  963. if (s->session->sess_cert->peer_ecdh_tmp != NULL)
  964. {
  965. tkey = s->session->sess_cert->peer_ecdh_tmp;
  966. }
  967. else
  968. {
  969. /* Get the Server Public Key from Cert */
  970. srvr_pub_pkey = X509_get_pubkey(s->session-> \
  971. sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
  972. if ((srvr_pub_pkey == NULL) ||
  973. (srvr_pub_pkey->type != EVP_PKEY_EC) ||
  974. (srvr_pub_pkey->pkey.ec == NULL))
  975. {
  976. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  977. ERR_R_INTERNAL_ERROR);
  978. goto err;
  979. }
  980. tkey = srvr_pub_pkey->pkey.ec;
  981. }
  982. srvr_group = EC_KEY_get0_group(tkey);
  983. srvr_ecpoint = EC_KEY_get0_public_key(tkey);
  984. if ((srvr_group == NULL) || (srvr_ecpoint == NULL))
  985. {
  986. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  987. ERR_R_INTERNAL_ERROR);
  988. goto err;
  989. }
  990. if ((clnt_ecdh=EC_KEY_new()) == NULL)
  991. {
  992. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  993. goto err;
  994. }
  995. if (!EC_KEY_set_group(clnt_ecdh, srvr_group))
  996. {
  997. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB);
  998. goto err;
  999. }
  1000. if (ecdh_clnt_cert)
  1001. {
  1002. /* Reuse key info from our certificate
  1003. * We only need our private key to perform
  1004. * the ECDH computation.
  1005. */
  1006. const BIGNUM *priv_key;
  1007. tkey = s->cert->key->privatekey->pkey.ec;
  1008. priv_key = EC_KEY_get0_private_key(tkey);
  1009. if (priv_key == NULL)
  1010. {
  1011. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  1012. goto err;
  1013. }
  1014. if (!EC_KEY_set_private_key(clnt_ecdh, priv_key))
  1015. {
  1016. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB);
  1017. goto err;
  1018. }
  1019. }
  1020. else
  1021. {
  1022. /* Generate a new ECDH key pair */
  1023. if (!(EC_KEY_generate_key(clnt_ecdh)))
  1024. {
  1025. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
  1026. goto err;
  1027. }
  1028. }
  1029. /* use the 'p' output buffer for the ECDH key, but
  1030. * make sure to clear it out afterwards
  1031. */
  1032. field_size = EC_GROUP_get_degree(srvr_group);
  1033. if (field_size <= 0)
  1034. {
  1035. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  1036. ERR_R_ECDH_LIB);
  1037. goto err;
  1038. }
  1039. n=ECDH_compute_key(p, (field_size+7)/8, srvr_ecpoint, clnt_ecdh, NULL);
  1040. if (n <= 0)
  1041. {
  1042. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  1043. ERR_R_ECDH_LIB);
  1044. goto err;
  1045. }
  1046. /* generate master key from the result */
  1047. s->session->master_key_length = s->method->ssl3_enc \
  1048. -> generate_master_secret(s,
  1049. s->session->master_key,
  1050. p, n);
  1051. memset(p, 0, n); /* clean up */
  1052. if (ecdh_clnt_cert)
  1053. {
  1054. /* Send empty client key exch message */
  1055. n = 0;
  1056. }
  1057. else
  1058. {
  1059. /* First check the size of encoding and
  1060. * allocate memory accordingly.
  1061. */
  1062. encoded_pt_len =
  1063. EC_POINT_point2oct(srvr_group,
  1064. EC_KEY_get0_public_key(clnt_ecdh),
  1065. POINT_CONVERSION_UNCOMPRESSED,
  1066. NULL, 0, NULL);
  1067. encodedPoint = (unsigned char *)
  1068. OPENSSL_malloc(encoded_pt_len *
  1069. sizeof(unsigned char));
  1070. bn_ctx = BN_CTX_new();
  1071. if ((encodedPoint == NULL) ||
  1072. (bn_ctx == NULL))
  1073. {
  1074. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  1075. goto err;
  1076. }
  1077. /* Encode the public key */
  1078. n = EC_POINT_point2oct(srvr_group,
  1079. EC_KEY_get0_public_key(clnt_ecdh),
  1080. POINT_CONVERSION_UNCOMPRESSED,
  1081. encodedPoint, encoded_pt_len, bn_ctx);
  1082. *p = n; /* length of encoded point */
  1083. /* Encoded point will be copied here */
  1084. p += 1;
  1085. /* copy the point */
  1086. memcpy((unsigned char *)p, encodedPoint, n);
  1087. /* increment n to account for length field */
  1088. n += 1;
  1089. }
  1090. /* Free allocated memory */
  1091. BN_CTX_free(bn_ctx);
  1092. if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
  1093. if (clnt_ecdh != NULL)
  1094. EC_KEY_free(clnt_ecdh);
  1095. EVP_PKEY_free(srvr_pub_pkey);
  1096. }
  1097. #endif /* !OPENSSL_NO_ECDH */
  1098. #ifndef OPENSSL_NO_PSK
  1099. else if (alg_k & SSL_kPSK)
  1100. {
  1101. char identity[PSK_MAX_IDENTITY_LEN];
  1102. unsigned char *t = NULL;
  1103. unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN*2+4];
  1104. unsigned int pre_ms_len = 0, psk_len = 0;
  1105. int psk_err = 1;
  1106. n = 0;
  1107. if (s->psk_client_callback == NULL)
  1108. {
  1109. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  1110. SSL_R_PSK_NO_CLIENT_CB);
  1111. goto err;
  1112. }
  1113. psk_len = s->psk_client_callback(s, s->ctx->psk_identity_hint,
  1114. identity, PSK_MAX_IDENTITY_LEN,
  1115. psk_or_pre_ms, sizeof(psk_or_pre_ms));
  1116. if (psk_len > PSK_MAX_PSK_LEN)
  1117. {
  1118. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  1119. ERR_R_INTERNAL_ERROR);
  1120. goto psk_err;
  1121. }
  1122. else if (psk_len == 0)
  1123. {
  1124. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  1125. SSL_R_PSK_IDENTITY_NOT_FOUND);
  1126. goto psk_err;
  1127. }
  1128. /* create PSK pre_master_secret */
  1129. pre_ms_len = 2+psk_len+2+psk_len;
  1130. t = psk_or_pre_ms;
  1131. memmove(psk_or_pre_ms+psk_len+4, psk_or_pre_ms, psk_len);
  1132. s2n(psk_len, t);
  1133. memset(t, 0, psk_len);
  1134. t+=psk_len;
  1135. s2n(psk_len, t);
  1136. if (s->session->psk_identity_hint != NULL)
  1137. OPENSSL_free(s->session->psk_identity_hint);
  1138. s->session->psk_identity_hint = BUF_strdup(s->ctx->psk_identity_hint);
  1139. if (s->ctx->psk_identity_hint != NULL &&
  1140. s->session->psk_identity_hint == NULL)
  1141. {
  1142. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  1143. ERR_R_MALLOC_FAILURE);
  1144. goto psk_err;
  1145. }
  1146. if (s->session->psk_identity != NULL)
  1147. OPENSSL_free(s->session->psk_identity);
  1148. s->session->psk_identity = BUF_strdup(identity);
  1149. if (s->session->psk_identity == NULL)
  1150. {
  1151. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
  1152. ERR_R_MALLOC_FAILURE);
  1153. goto psk_err;
  1154. }
  1155. s->session->master_key_length =
  1156. s->method->ssl3_enc->generate_master_secret(s,
  1157. s->session->master_key,
  1158. psk_or_pre_ms, pre_ms_len);
  1159. n = strlen(identity);
  1160. s2n(n, p);
  1161. memcpy(p, identity, n);
  1162. n+=2;
  1163. psk_err = 0;
  1164. psk_err:
  1165. OPENSSL_cleanse(identity, PSK_MAX_IDENTITY_LEN);
  1166. OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
  1167. if (psk_err != 0)
  1168. {
  1169. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1170. goto err;
  1171. }
  1172. }
  1173. #endif
  1174. else
  1175. {
  1176. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
  1177. SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
  1178. goto err;
  1179. }
  1180. d = dtls1_set_message_header(s, d,
  1181. SSL3_MT_CLIENT_KEY_EXCHANGE, n, 0, n);
  1182. /*
  1183. *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE;
  1184. l2n3(n,d);
  1185. l2n(s->d1->handshake_write_seq,d);
  1186. s->d1->handshake_write_seq++;
  1187. */
  1188. s->state=SSL3_ST_CW_KEY_EXCH_B;
  1189. /* number of bytes to write */
  1190. s->init_num=n+DTLS1_HM_HEADER_LENGTH;
  1191. s->init_off=0;
  1192. /* buffer the message to handle re-xmits */
  1193. dtls1_buffer_message(s, 0);
  1194. }
  1195. /* SSL3_ST_CW_KEY_EXCH_B */
  1196. return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
  1197. err:
  1198. #ifndef OPENSSL_NO_ECDH
  1199. BN_CTX_free(bn_ctx);
  1200. if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
  1201. if (clnt_ecdh != NULL)
  1202. EC_KEY_free(clnt_ecdh);
  1203. EVP_PKEY_free(srvr_pub_pkey);
  1204. #endif
  1205. return(-1);
  1206. }
  1207. int dtls1_send_client_verify(SSL *s)
  1208. {
  1209. unsigned char *p,*d;
  1210. unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
  1211. EVP_PKEY *pkey;
  1212. #ifndef OPENSSL_NO_RSA
  1213. unsigned u=0;
  1214. #endif
  1215. unsigned long n;
  1216. #if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA)
  1217. int j;
  1218. #endif
  1219. if (s->state == SSL3_ST_CW_CERT_VRFY_A)
  1220. {
  1221. d=(unsigned char *)s->init_buf->data;
  1222. p= &(d[DTLS1_HM_HEADER_LENGTH]);
  1223. pkey=s->cert->key->privatekey;
  1224. s->method->ssl3_enc->cert_verify_mac(s,
  1225. NID_sha1,
  1226. &(data[MD5_DIGEST_LENGTH]));
  1227. #ifndef OPENSSL_NO_RSA
  1228. if (pkey->type == EVP_PKEY_RSA)
  1229. {
  1230. s->method->ssl3_enc->cert_verify_mac(s,
  1231. NID_md5,
  1232. &(data[0]));
  1233. if (RSA_sign(NID_md5_sha1, data,
  1234. MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
  1235. &(p[2]), &u, pkey->pkey.rsa) <= 0 )
  1236. {
  1237. SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY,ERR_R_RSA_LIB);
  1238. goto err;
  1239. }
  1240. s2n(u,p);
  1241. n=u+2;
  1242. }
  1243. else
  1244. #endif
  1245. #ifndef OPENSSL_NO_DSA
  1246. if (pkey->type == EVP_PKEY_DSA)
  1247. {
  1248. if (!DSA_sign(pkey->save_type,
  1249. &(data[MD5_DIGEST_LENGTH]),
  1250. SHA_DIGEST_LENGTH,&(p[2]),
  1251. (unsigned int *)&j,pkey->pkey.dsa))
  1252. {
  1253. SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY,ERR_R_DSA_LIB);
  1254. goto err;
  1255. }
  1256. s2n(j,p);
  1257. n=j+2;
  1258. }
  1259. else
  1260. #endif
  1261. #ifndef OPENSSL_NO_ECDSA
  1262. if (pkey->type == EVP_PKEY_EC)
  1263. {
  1264. if (!ECDSA_sign(pkey->save_type,
  1265. &(data[MD5_DIGEST_LENGTH]),
  1266. SHA_DIGEST_LENGTH,&(p[2]),
  1267. (unsigned int *)&j,pkey->pkey.ec))
  1268. {
  1269. SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY,
  1270. ERR_R_ECDSA_LIB);
  1271. goto err;
  1272. }
  1273. s2n(j,p);
  1274. n=j+2;
  1275. }
  1276. else
  1277. #endif
  1278. {
  1279. SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY,ERR_R_INTERNAL_ERROR);
  1280. goto err;
  1281. }
  1282. d = dtls1_set_message_header(s, d,
  1283. SSL3_MT_CERTIFICATE_VERIFY, n, 0, n) ;
  1284. s->init_num=(int)n+DTLS1_HM_HEADER_LENGTH;
  1285. s->init_off=0;
  1286. /* buffer the message to handle re-xmits */
  1287. dtls1_buffer_message(s, 0);
  1288. s->state = SSL3_ST_CW_CERT_VRFY_B;
  1289. }
  1290. /* s->state = SSL3_ST_CW_CERT_VRFY_B */
  1291. return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
  1292. err:
  1293. return(-1);
  1294. }
  1295. int dtls1_send_client_certificate(SSL *s)
  1296. {
  1297. X509 *x509=NULL;
  1298. EVP_PKEY *pkey=NULL;
  1299. int i;
  1300. unsigned long l;
  1301. if (s->state == SSL3_ST_CW_CERT_A)
  1302. {
  1303. if ((s->cert == NULL) ||
  1304. (s->cert->key->x509 == NULL) ||
  1305. (s->cert->key->privatekey == NULL))
  1306. s->state=SSL3_ST_CW_CERT_B;
  1307. else
  1308. s->state=SSL3_ST_CW_CERT_C;
  1309. }
  1310. /* We need to get a client cert */
  1311. if (s->state == SSL3_ST_CW_CERT_B)
  1312. {
  1313. /* If we get an error, we need to
  1314. * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
  1315. * We then get retied later */
  1316. i=0;
  1317. i = ssl_do_client_cert_cb(s, &x509, &pkey);
  1318. if (i < 0)
  1319. {
  1320. s->rwstate=SSL_X509_LOOKUP;
  1321. return(-1);
  1322. }
  1323. s->rwstate=SSL_NOTHING;
  1324. if ((i == 1) && (pkey != NULL) && (x509 != NULL))
  1325. {
  1326. s->state=SSL3_ST_CW_CERT_B;
  1327. if ( !SSL_use_certificate(s,x509) ||
  1328. !SSL_use_PrivateKey(s,pkey))
  1329. i=0;
  1330. }
  1331. else if (i == 1)
  1332. {
  1333. i=0;
  1334. SSLerr(SSL_F_DTLS1_SEND_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
  1335. }
  1336. if (x509 != NULL) X509_free(x509);
  1337. if (pkey != NULL) EVP_PKEY_free(pkey);
  1338. if (i == 0)
  1339. {
  1340. if (s->version == SSL3_VERSION)
  1341. {
  1342. s->s3->tmp.cert_req=0;
  1343. ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
  1344. return(1);
  1345. }
  1346. else
  1347. {
  1348. s->s3->tmp.cert_req=2;
  1349. }
  1350. }
  1351. /* Ok, we have a cert */
  1352. s->state=SSL3_ST_CW_CERT_C;
  1353. }
  1354. if (s->state == SSL3_ST_CW_CERT_C)
  1355. {
  1356. s->state=SSL3_ST_CW_CERT_D;
  1357. l=dtls1_output_cert_chain(s,
  1358. (s->s3->tmp.cert_req == 2)?NULL:s->cert->key->x509);
  1359. s->init_num=(int)l;
  1360. s->init_off=0;
  1361. /* set header called by dtls1_output_cert_chain() */
  1362. /* buffer the message to handle re-xmits */
  1363. dtls1_buffer_message(s, 0);
  1364. }
  1365. /* SSL3_ST_CW_CERT_D */
  1366. return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
  1367. }