d1_srvr.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551
  1. /* ssl/d1_srvr.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. #include <openssl/buffer.h>
  118. #include <openssl/rand.h>
  119. #include <openssl/objects.h>
  120. #include <openssl/evp.h>
  121. #include <openssl/x509.h>
  122. #include <openssl/md5.h>
  123. #include <openssl/bn.h>
  124. #ifndef OPENSSL_NO_DH
  125. #include <openssl/dh.h>
  126. #endif
  127. static const SSL_METHOD *dtls1_get_server_method(int ver);
  128. static int dtls1_send_hello_verify_request(SSL *s);
  129. static const SSL_METHOD *dtls1_get_server_method(int ver)
  130. {
  131. if (ver == DTLS1_VERSION)
  132. return(DTLSv1_server_method());
  133. else
  134. return(NULL);
  135. }
  136. IMPLEMENT_dtls1_meth_func(DTLSv1_server_method,
  137. dtls1_accept,
  138. ssl_undefined_function,
  139. dtls1_get_server_method)
  140. int dtls1_accept(SSL *s)
  141. {
  142. BUF_MEM *buf;
  143. unsigned long Time=(unsigned long)time(NULL);
  144. void (*cb)(const SSL *ssl,int type,int val)=NULL;
  145. unsigned long alg_k;
  146. int ret= -1;
  147. int new_state,state,skip=0;
  148. RAND_add(&Time,sizeof(Time),0);
  149. ERR_clear_error();
  150. clear_sys_error();
  151. if (s->info_callback != NULL)
  152. cb=s->info_callback;
  153. else if (s->ctx->info_callback != NULL)
  154. cb=s->ctx->info_callback;
  155. /* init things to blank */
  156. s->in_handshake++;
  157. if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
  158. if (s->cert == NULL)
  159. {
  160. SSLerr(SSL_F_DTLS1_ACCEPT,SSL_R_NO_CERTIFICATE_SET);
  161. return(-1);
  162. }
  163. for (;;)
  164. {
  165. state=s->state;
  166. switch (s->state)
  167. {
  168. case SSL_ST_RENEGOTIATE:
  169. s->new_session=1;
  170. /* s->state=SSL_ST_ACCEPT; */
  171. case SSL_ST_BEFORE:
  172. case SSL_ST_ACCEPT:
  173. case SSL_ST_BEFORE|SSL_ST_ACCEPT:
  174. case SSL_ST_OK|SSL_ST_ACCEPT:
  175. s->server=1;
  176. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
  177. if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00))
  178. {
  179. SSLerr(SSL_F_DTLS1_ACCEPT, ERR_R_INTERNAL_ERROR);
  180. return -1;
  181. }
  182. s->type=SSL_ST_ACCEPT;
  183. if (s->init_buf == NULL)
  184. {
  185. if ((buf=BUF_MEM_new()) == NULL)
  186. {
  187. ret= -1;
  188. goto end;
  189. }
  190. if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
  191. {
  192. ret= -1;
  193. goto end;
  194. }
  195. s->init_buf=buf;
  196. }
  197. if (!ssl3_setup_buffers(s))
  198. {
  199. ret= -1;
  200. goto end;
  201. }
  202. s->init_num=0;
  203. if (s->state != SSL_ST_RENEGOTIATE)
  204. {
  205. /* Ok, we now need to push on a buffering BIO so that
  206. * the output is sent in a way that TCP likes :-)
  207. */
  208. if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; }
  209. ssl3_init_finished_mac(s);
  210. s->state=SSL3_ST_SR_CLNT_HELLO_A;
  211. s->ctx->stats.sess_accept++;
  212. }
  213. else
  214. {
  215. /* s->state == SSL_ST_RENEGOTIATE,
  216. * we will just send a HelloRequest */
  217. s->ctx->stats.sess_accept_renegotiate++;
  218. s->state=SSL3_ST_SW_HELLO_REQ_A;
  219. }
  220. break;
  221. case SSL3_ST_SW_HELLO_REQ_A:
  222. case SSL3_ST_SW_HELLO_REQ_B:
  223. s->shutdown=0;
  224. dtls1_start_timer(s);
  225. ret=dtls1_send_hello_request(s);
  226. if (ret <= 0) goto end;
  227. s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C;
  228. s->state=SSL3_ST_SW_FLUSH;
  229. s->init_num=0;
  230. ssl3_init_finished_mac(s);
  231. break;
  232. case SSL3_ST_SW_HELLO_REQ_C:
  233. s->state=SSL_ST_OK;
  234. break;
  235. case SSL3_ST_SR_CLNT_HELLO_A:
  236. case SSL3_ST_SR_CLNT_HELLO_B:
  237. case SSL3_ST_SR_CLNT_HELLO_C:
  238. s->shutdown=0;
  239. ret=ssl3_get_client_hello(s);
  240. if (ret <= 0) goto end;
  241. dtls1_stop_timer(s);
  242. if (ret == 1 && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE))
  243. s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A;
  244. else
  245. s->state = SSL3_ST_SW_SRVR_HELLO_A;
  246. s->init_num=0;
  247. /* If we're just listening, stop here */
  248. if (s->d1->listen && s->state == SSL3_ST_SW_SRVR_HELLO_A)
  249. {
  250. ret = 2;
  251. s->d1->listen = 0;
  252. goto end;
  253. }
  254. break;
  255. case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A:
  256. case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B:
  257. dtls1_start_timer(s);
  258. ret = dtls1_send_hello_verify_request(s);
  259. if ( ret <= 0) goto end;
  260. s->state=SSL3_ST_SW_FLUSH;
  261. s->s3->tmp.next_state=SSL3_ST_SR_CLNT_HELLO_A;
  262. /* HelloVerifyRequest resets Finished MAC */
  263. if (s->version != DTLS1_BAD_VER)
  264. ssl3_init_finished_mac(s);
  265. break;
  266. case SSL3_ST_SW_SRVR_HELLO_A:
  267. case SSL3_ST_SW_SRVR_HELLO_B:
  268. s->new_session = 2;
  269. dtls1_start_timer(s);
  270. ret=dtls1_send_server_hello(s);
  271. if (ret <= 0) goto end;
  272. #ifndef OPENSSL_NO_TLSEXT
  273. if (s->hit)
  274. {
  275. if (s->tlsext_ticket_expected)
  276. s->state=SSL3_ST_SW_SESSION_TICKET_A;
  277. else
  278. s->state=SSL3_ST_SW_CHANGE_A;
  279. }
  280. #else
  281. if (s->hit)
  282. s->state=SSL3_ST_SW_CHANGE_A;
  283. #endif
  284. else
  285. s->state=SSL3_ST_SW_CERT_A;
  286. s->init_num=0;
  287. break;
  288. case SSL3_ST_SW_CERT_A:
  289. case SSL3_ST_SW_CERT_B:
  290. /* Check if it is anon DH or normal PSK */
  291. if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
  292. && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
  293. {
  294. dtls1_start_timer(s);
  295. ret=dtls1_send_server_certificate(s);
  296. if (ret <= 0) goto end;
  297. #ifndef OPENSSL_NO_TLSEXT
  298. if (s->tlsext_status_expected)
  299. s->state=SSL3_ST_SW_CERT_STATUS_A;
  300. else
  301. s->state=SSL3_ST_SW_KEY_EXCH_A;
  302. }
  303. else
  304. {
  305. skip = 1;
  306. s->state=SSL3_ST_SW_KEY_EXCH_A;
  307. }
  308. #else
  309. }
  310. else
  311. skip=1;
  312. s->state=SSL3_ST_SW_KEY_EXCH_A;
  313. #endif
  314. s->init_num=0;
  315. break;
  316. case SSL3_ST_SW_KEY_EXCH_A:
  317. case SSL3_ST_SW_KEY_EXCH_B:
  318. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  319. /* clear this, it may get reset by
  320. * send_server_key_exchange */
  321. if ((s->options & SSL_OP_EPHEMERAL_RSA)
  322. #ifndef OPENSSL_NO_KRB5
  323. && !(alg_k & SSL_kKRB5)
  324. #endif /* OPENSSL_NO_KRB5 */
  325. )
  326. /* option SSL_OP_EPHEMERAL_RSA sends temporary RSA key
  327. * even when forbidden by protocol specs
  328. * (handshake may fail as clients are not required to
  329. * be able to handle this) */
  330. s->s3->tmp.use_rsa_tmp=1;
  331. else
  332. s->s3->tmp.use_rsa_tmp=0;
  333. /* only send if a DH key exchange or
  334. * RSA but we have a sign only certificate */
  335. if (s->s3->tmp.use_rsa_tmp
  336. /* PSK: send ServerKeyExchange if PSK identity
  337. * hint if provided */
  338. #ifndef OPENSSL_NO_PSK
  339. || ((alg_k & SSL_kPSK) && s->ctx->psk_identity_hint)
  340. #endif
  341. || (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
  342. || (alg_k & SSL_kEECDH)
  343. || ((alg_k & SSL_kRSA)
  344. && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
  345. || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
  346. && EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
  347. )
  348. )
  349. )
  350. )
  351. {
  352. dtls1_start_timer(s);
  353. ret=dtls1_send_server_key_exchange(s);
  354. if (ret <= 0) goto end;
  355. }
  356. else
  357. skip=1;
  358. s->state=SSL3_ST_SW_CERT_REQ_A;
  359. s->init_num=0;
  360. break;
  361. case SSL3_ST_SW_CERT_REQ_A:
  362. case SSL3_ST_SW_CERT_REQ_B:
  363. if (/* don't request cert unless asked for it: */
  364. !(s->verify_mode & SSL_VERIFY_PEER) ||
  365. /* if SSL_VERIFY_CLIENT_ONCE is set,
  366. * don't request cert during re-negotiation: */
  367. ((s->session->peer != NULL) &&
  368. (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
  369. /* never request cert in anonymous ciphersuites
  370. * (see section "Certificate request" in SSL 3 drafts
  371. * and in RFC 2246): */
  372. ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
  373. /* ... except when the application insists on verification
  374. * (against the specs, but s3_clnt.c accepts this for SSL 3) */
  375. !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
  376. /* never request cert in Kerberos ciphersuites */
  377. (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5)
  378. /* With normal PSK Certificates and
  379. * Certificate Requests are omitted */
  380. || (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
  381. {
  382. /* no cert request */
  383. skip=1;
  384. s->s3->tmp.cert_request=0;
  385. s->state=SSL3_ST_SW_SRVR_DONE_A;
  386. }
  387. else
  388. {
  389. s->s3->tmp.cert_request=1;
  390. dtls1_start_timer(s);
  391. ret=dtls1_send_certificate_request(s);
  392. if (ret <= 0) goto end;
  393. #ifndef NETSCAPE_HANG_BUG
  394. s->state=SSL3_ST_SW_SRVR_DONE_A;
  395. #else
  396. s->state=SSL3_ST_SW_FLUSH;
  397. s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
  398. #endif
  399. s->init_num=0;
  400. }
  401. break;
  402. case SSL3_ST_SW_SRVR_DONE_A:
  403. case SSL3_ST_SW_SRVR_DONE_B:
  404. dtls1_start_timer(s);
  405. ret=dtls1_send_server_done(s);
  406. if (ret <= 0) goto end;
  407. s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
  408. s->state=SSL3_ST_SW_FLUSH;
  409. s->init_num=0;
  410. break;
  411. case SSL3_ST_SW_FLUSH:
  412. s->rwstate=SSL_WRITING;
  413. if (BIO_flush(s->wbio) <= 0)
  414. {
  415. ret= -1;
  416. goto end;
  417. }
  418. s->rwstate=SSL_NOTHING;
  419. s->state=s->s3->tmp.next_state;
  420. break;
  421. case SSL3_ST_SR_CERT_A:
  422. case SSL3_ST_SR_CERT_B:
  423. /* Check for second client hello (MS SGC) */
  424. ret = ssl3_check_client_hello(s);
  425. if (ret <= 0)
  426. goto end;
  427. dtls1_stop_timer(s);
  428. if (ret == 2)
  429. s->state = SSL3_ST_SR_CLNT_HELLO_C;
  430. else {
  431. /* could be sent for a DH cert, even if we
  432. * have not asked for it :-) */
  433. ret=ssl3_get_client_certificate(s);
  434. if (ret <= 0) goto end;
  435. dtls1_stop_timer(s);
  436. s->init_num=0;
  437. s->state=SSL3_ST_SR_KEY_EXCH_A;
  438. }
  439. break;
  440. case SSL3_ST_SR_KEY_EXCH_A:
  441. case SSL3_ST_SR_KEY_EXCH_B:
  442. ret=ssl3_get_client_key_exchange(s);
  443. if (ret <= 0) goto end;
  444. dtls1_stop_timer(s);
  445. s->state=SSL3_ST_SR_CERT_VRFY_A;
  446. s->init_num=0;
  447. if (ret == 2)
  448. {
  449. /* For the ECDH ciphersuites when
  450. * the client sends its ECDH pub key in
  451. * a certificate, the CertificateVerify
  452. * message is not sent.
  453. */
  454. s->state=SSL3_ST_SR_FINISHED_A;
  455. s->init_num = 0;
  456. }
  457. else
  458. {
  459. s->state=SSL3_ST_SR_CERT_VRFY_A;
  460. s->init_num=0;
  461. /* We need to get hashes here so if there is
  462. * a client cert, it can be verified */
  463. s->method->ssl3_enc->cert_verify_mac(s,
  464. NID_md5,
  465. &(s->s3->tmp.cert_verify_md[0]));
  466. s->method->ssl3_enc->cert_verify_mac(s,
  467. NID_sha1,
  468. &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]));
  469. }
  470. break;
  471. case SSL3_ST_SR_CERT_VRFY_A:
  472. case SSL3_ST_SR_CERT_VRFY_B:
  473. s->d1->change_cipher_spec_ok = 1;
  474. /* we should decide if we expected this one */
  475. ret=ssl3_get_cert_verify(s);
  476. if (ret <= 0) goto end;
  477. dtls1_stop_timer(s);
  478. s->state=SSL3_ST_SR_FINISHED_A;
  479. s->init_num=0;
  480. break;
  481. case SSL3_ST_SR_FINISHED_A:
  482. case SSL3_ST_SR_FINISHED_B:
  483. s->d1->change_cipher_spec_ok = 1;
  484. ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A,
  485. SSL3_ST_SR_FINISHED_B);
  486. if (ret <= 0) goto end;
  487. dtls1_stop_timer(s);
  488. if (s->hit)
  489. s->state=SSL_ST_OK;
  490. #ifndef OPENSSL_NO_TLSEXT
  491. else if (s->tlsext_ticket_expected)
  492. s->state=SSL3_ST_SW_SESSION_TICKET_A;
  493. #endif
  494. else
  495. s->state=SSL3_ST_SW_CHANGE_A;
  496. s->init_num=0;
  497. break;
  498. #ifndef OPENSSL_NO_TLSEXT
  499. case SSL3_ST_SW_SESSION_TICKET_A:
  500. case SSL3_ST_SW_SESSION_TICKET_B:
  501. ret=dtls1_send_newsession_ticket(s);
  502. if (ret <= 0) goto end;
  503. s->state=SSL3_ST_SW_CHANGE_A;
  504. s->init_num=0;
  505. break;
  506. case SSL3_ST_SW_CERT_STATUS_A:
  507. case SSL3_ST_SW_CERT_STATUS_B:
  508. ret=ssl3_send_cert_status(s);
  509. if (ret <= 0) goto end;
  510. s->state=SSL3_ST_SW_KEY_EXCH_A;
  511. s->init_num=0;
  512. break;
  513. #endif
  514. case SSL3_ST_SW_CHANGE_A:
  515. case SSL3_ST_SW_CHANGE_B:
  516. s->session->cipher=s->s3->tmp.new_cipher;
  517. if (!s->method->ssl3_enc->setup_key_block(s))
  518. { ret= -1; goto end; }
  519. ret=dtls1_send_change_cipher_spec(s,
  520. SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B);
  521. if (ret <= 0) goto end;
  522. s->state=SSL3_ST_SW_FINISHED_A;
  523. s->init_num=0;
  524. if (!s->method->ssl3_enc->change_cipher_state(s,
  525. SSL3_CHANGE_CIPHER_SERVER_WRITE))
  526. {
  527. ret= -1;
  528. goto end;
  529. }
  530. dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
  531. break;
  532. case SSL3_ST_SW_FINISHED_A:
  533. case SSL3_ST_SW_FINISHED_B:
  534. ret=dtls1_send_finished(s,
  535. SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B,
  536. s->method->ssl3_enc->server_finished_label,
  537. s->method->ssl3_enc->server_finished_label_len);
  538. if (ret <= 0) goto end;
  539. s->state=SSL3_ST_SW_FLUSH;
  540. if (s->hit)
  541. s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A;
  542. else
  543. s->s3->tmp.next_state=SSL_ST_OK;
  544. s->init_num=0;
  545. break;
  546. case SSL_ST_OK:
  547. /* clean a few things up */
  548. ssl3_cleanup_key_block(s);
  549. #if 0
  550. BUF_MEM_free(s->init_buf);
  551. s->init_buf=NULL;
  552. #endif
  553. /* remove buffering on output */
  554. ssl_free_wbio_buffer(s);
  555. s->init_num=0;
  556. if (s->new_session == 2) /* skipped if we just sent a HelloRequest */
  557. {
  558. /* actually not necessarily a 'new' session unless
  559. * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is set */
  560. s->new_session=0;
  561. ssl_update_cache(s,SSL_SESS_CACHE_SERVER);
  562. s->ctx->stats.sess_accept_good++;
  563. /* s->server=1; */
  564. s->handshake_func=dtls1_accept;
  565. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
  566. }
  567. ret = 1;
  568. /* done handshaking, next message is client hello */
  569. s->d1->handshake_read_seq = 0;
  570. /* next message is server hello */
  571. s->d1->handshake_write_seq = 0;
  572. s->d1->next_handshake_write_seq = 0;
  573. goto end;
  574. /* break; */
  575. default:
  576. SSLerr(SSL_F_DTLS1_ACCEPT,SSL_R_UNKNOWN_STATE);
  577. ret= -1;
  578. goto end;
  579. /* break; */
  580. }
  581. if (!s->s3->tmp.reuse_message && !skip)
  582. {
  583. if (s->debug)
  584. {
  585. if ((ret=BIO_flush(s->wbio)) <= 0)
  586. goto end;
  587. }
  588. if ((cb != NULL) && (s->state != state))
  589. {
  590. new_state=s->state;
  591. s->state=state;
  592. cb(s,SSL_CB_ACCEPT_LOOP,1);
  593. s->state=new_state;
  594. }
  595. }
  596. skip=0;
  597. }
  598. end:
  599. /* BIO_flush(s->wbio); */
  600. s->in_handshake--;
  601. if (cb != NULL)
  602. cb(s,SSL_CB_ACCEPT_EXIT,ret);
  603. return(ret);
  604. }
  605. int dtls1_send_hello_request(SSL *s)
  606. {
  607. unsigned char *p;
  608. if (s->state == SSL3_ST_SW_HELLO_REQ_A)
  609. {
  610. p=(unsigned char *)s->init_buf->data;
  611. p = dtls1_set_message_header(s, p, SSL3_MT_HELLO_REQUEST, 0, 0, 0);
  612. s->state=SSL3_ST_SW_HELLO_REQ_B;
  613. /* number of bytes to write */
  614. s->init_num=DTLS1_HM_HEADER_LENGTH;
  615. s->init_off=0;
  616. /* no need to buffer this message, since there are no retransmit
  617. * requests for it */
  618. }
  619. /* SSL3_ST_SW_HELLO_REQ_B */
  620. return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
  621. }
  622. int dtls1_send_hello_verify_request(SSL *s)
  623. {
  624. unsigned int msg_len;
  625. unsigned char *msg, *buf, *p;
  626. if (s->state == DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A)
  627. {
  628. buf = (unsigned char *)s->init_buf->data;
  629. msg = p = &(buf[DTLS1_HM_HEADER_LENGTH]);
  630. *(p++) = s->version >> 8;
  631. *(p++) = s->version & 0xFF;
  632. if (s->ctx->app_gen_cookie_cb == NULL ||
  633. s->ctx->app_gen_cookie_cb(s, s->d1->cookie,
  634. &(s->d1->cookie_len)) == 0)
  635. {
  636. SSLerr(SSL_F_DTLS1_SEND_HELLO_VERIFY_REQUEST,ERR_R_INTERNAL_ERROR);
  637. return 0;
  638. }
  639. *(p++) = (unsigned char) s->d1->cookie_len;
  640. memcpy(p, s->d1->cookie, s->d1->cookie_len);
  641. p += s->d1->cookie_len;
  642. msg_len = p - msg;
  643. dtls1_set_message_header(s, buf,
  644. DTLS1_MT_HELLO_VERIFY_REQUEST, msg_len, 0, msg_len);
  645. s->state=DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B;
  646. /* number of bytes to write */
  647. s->init_num=p-buf;
  648. s->init_off=0;
  649. /* buffer the message to handle re-xmits */
  650. dtls1_buffer_message(s, 0);
  651. }
  652. /* s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B */
  653. return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
  654. }
  655. int dtls1_send_server_hello(SSL *s)
  656. {
  657. unsigned char *buf;
  658. unsigned char *p,*d;
  659. int i;
  660. unsigned int sl;
  661. unsigned long l,Time;
  662. if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
  663. {
  664. buf=(unsigned char *)s->init_buf->data;
  665. p=s->s3->server_random;
  666. Time=(unsigned long)time(NULL); /* Time */
  667. l2n(Time,p);
  668. RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-sizeof(Time));
  669. /* Do the message type and length last */
  670. d=p= &(buf[DTLS1_HM_HEADER_LENGTH]);
  671. *(p++)=s->version>>8;
  672. *(p++)=s->version&0xff;
  673. /* Random stuff */
  674. memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
  675. p+=SSL3_RANDOM_SIZE;
  676. /* now in theory we have 3 options to sending back the
  677. * session id. If it is a re-use, we send back the
  678. * old session-id, if it is a new session, we send
  679. * back the new session-id or we send back a 0 length
  680. * session-id if we want it to be single use.
  681. * Currently I will not implement the '0' length session-id
  682. * 12-Jan-98 - I'll now support the '0' length stuff.
  683. */
  684. if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER))
  685. s->session->session_id_length=0;
  686. sl=s->session->session_id_length;
  687. if (sl > sizeof s->session->session_id)
  688. {
  689. SSLerr(SSL_F_DTLS1_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
  690. return -1;
  691. }
  692. *(p++)=sl;
  693. memcpy(p,s->session->session_id,sl);
  694. p+=sl;
  695. /* put the cipher */
  696. if (s->s3->tmp.new_cipher == NULL)
  697. return -1;
  698. i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p);
  699. p+=i;
  700. /* put the compression method */
  701. #ifdef OPENSSL_NO_COMP
  702. *(p++)=0;
  703. #else
  704. if (s->s3->tmp.new_compression == NULL)
  705. *(p++)=0;
  706. else
  707. *(p++)=s->s3->tmp.new_compression->id;
  708. #endif
  709. #ifndef OPENSSL_NO_TLSEXT
  710. if ((p = ssl_add_serverhello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH)) == NULL)
  711. {
  712. SSLerr(SSL_F_DTLS1_SEND_SERVER_HELLO,ERR_R_INTERNAL_ERROR);
  713. return -1;
  714. }
  715. #endif
  716. /* do the header */
  717. l=(p-d);
  718. d=buf;
  719. d = dtls1_set_message_header(s, d, SSL3_MT_SERVER_HELLO, l, 0, l);
  720. s->state=SSL3_ST_SW_SRVR_HELLO_B;
  721. /* number of bytes to write */
  722. s->init_num=p-buf;
  723. s->init_off=0;
  724. /* buffer the message to handle re-xmits */
  725. dtls1_buffer_message(s, 0);
  726. }
  727. /* SSL3_ST_SW_SRVR_HELLO_B */
  728. return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
  729. }
  730. int dtls1_send_server_done(SSL *s)
  731. {
  732. unsigned char *p;
  733. if (s->state == SSL3_ST_SW_SRVR_DONE_A)
  734. {
  735. p=(unsigned char *)s->init_buf->data;
  736. /* do the header */
  737. p = dtls1_set_message_header(s, p, SSL3_MT_SERVER_DONE, 0, 0, 0);
  738. s->state=SSL3_ST_SW_SRVR_DONE_B;
  739. /* number of bytes to write */
  740. s->init_num=DTLS1_HM_HEADER_LENGTH;
  741. s->init_off=0;
  742. /* buffer the message to handle re-xmits */
  743. dtls1_buffer_message(s, 0);
  744. }
  745. /* SSL3_ST_SW_SRVR_DONE_B */
  746. return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
  747. }
  748. int dtls1_send_server_key_exchange(SSL *s)
  749. {
  750. #ifndef OPENSSL_NO_RSA
  751. unsigned char *q;
  752. int j,num;
  753. RSA *rsa;
  754. unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
  755. unsigned int u;
  756. #endif
  757. #ifndef OPENSSL_NO_DH
  758. DH *dh=NULL,*dhp;
  759. #endif
  760. #ifndef OPENSSL_NO_ECDH
  761. EC_KEY *ecdh=NULL, *ecdhp;
  762. unsigned char *encodedPoint = NULL;
  763. int encodedlen = 0;
  764. int curve_id = 0;
  765. BN_CTX *bn_ctx = NULL;
  766. #endif
  767. EVP_PKEY *pkey;
  768. unsigned char *p,*d;
  769. int al,i;
  770. unsigned long type;
  771. int n;
  772. CERT *cert;
  773. BIGNUM *r[4];
  774. int nr[4],kn;
  775. BUF_MEM *buf;
  776. EVP_MD_CTX md_ctx;
  777. EVP_MD_CTX_init(&md_ctx);
  778. if (s->state == SSL3_ST_SW_KEY_EXCH_A)
  779. {
  780. type=s->s3->tmp.new_cipher->algorithm_mkey;
  781. cert=s->cert;
  782. buf=s->init_buf;
  783. r[0]=r[1]=r[2]=r[3]=NULL;
  784. n=0;
  785. #ifndef OPENSSL_NO_RSA
  786. if (type & SSL_kRSA)
  787. {
  788. rsa=cert->rsa_tmp;
  789. if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL))
  790. {
  791. rsa=s->cert->rsa_tmp_cb(s,
  792. SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
  793. SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
  794. if(rsa == NULL)
  795. {
  796. al=SSL_AD_HANDSHAKE_FAILURE;
  797. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
  798. goto f_err;
  799. }
  800. RSA_up_ref(rsa);
  801. cert->rsa_tmp=rsa;
  802. }
  803. if (rsa == NULL)
  804. {
  805. al=SSL_AD_HANDSHAKE_FAILURE;
  806. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_KEY);
  807. goto f_err;
  808. }
  809. r[0]=rsa->n;
  810. r[1]=rsa->e;
  811. s->s3->tmp.use_rsa_tmp=1;
  812. }
  813. else
  814. #endif
  815. #ifndef OPENSSL_NO_DH
  816. if (type & SSL_kEDH)
  817. {
  818. dhp=cert->dh_tmp;
  819. if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
  820. dhp=s->cert->dh_tmp_cb(s,
  821. SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
  822. SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
  823. if (dhp == NULL)
  824. {
  825. al=SSL_AD_HANDSHAKE_FAILURE;
  826. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
  827. goto f_err;
  828. }
  829. if (s->s3->tmp.dh != NULL)
  830. {
  831. DH_free(dh);
  832. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  833. goto err;
  834. }
  835. if ((dh=DHparams_dup(dhp)) == NULL)
  836. {
  837. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
  838. goto err;
  839. }
  840. s->s3->tmp.dh=dh;
  841. if ((dhp->pub_key == NULL ||
  842. dhp->priv_key == NULL ||
  843. (s->options & SSL_OP_SINGLE_DH_USE)))
  844. {
  845. if(!DH_generate_key(dh))
  846. {
  847. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
  848. ERR_R_DH_LIB);
  849. goto err;
  850. }
  851. }
  852. else
  853. {
  854. dh->pub_key=BN_dup(dhp->pub_key);
  855. dh->priv_key=BN_dup(dhp->priv_key);
  856. if ((dh->pub_key == NULL) ||
  857. (dh->priv_key == NULL))
  858. {
  859. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
  860. goto err;
  861. }
  862. }
  863. r[0]=dh->p;
  864. r[1]=dh->g;
  865. r[2]=dh->pub_key;
  866. }
  867. else
  868. #endif
  869. #ifndef OPENSSL_NO_ECDH
  870. if (type & SSL_kEECDH)
  871. {
  872. const EC_GROUP *group;
  873. ecdhp=cert->ecdh_tmp;
  874. if ((ecdhp == NULL) && (s->cert->ecdh_tmp_cb != NULL))
  875. {
  876. ecdhp=s->cert->ecdh_tmp_cb(s,
  877. SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
  878. SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
  879. }
  880. if (ecdhp == NULL)
  881. {
  882. al=SSL_AD_HANDSHAKE_FAILURE;
  883. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_ECDH_KEY);
  884. goto f_err;
  885. }
  886. if (s->s3->tmp.ecdh != NULL)
  887. {
  888. EC_KEY_free(s->s3->tmp.ecdh);
  889. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  890. goto err;
  891. }
  892. /* Duplicate the ECDH structure. */
  893. if (ecdhp == NULL)
  894. {
  895. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
  896. goto err;
  897. }
  898. if (!EC_KEY_up_ref(ecdhp))
  899. {
  900. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
  901. goto err;
  902. }
  903. ecdh = ecdhp;
  904. s->s3->tmp.ecdh=ecdh;
  905. if ((EC_KEY_get0_public_key(ecdh) == NULL) ||
  906. (EC_KEY_get0_private_key(ecdh) == NULL) ||
  907. (s->options & SSL_OP_SINGLE_ECDH_USE))
  908. {
  909. if(!EC_KEY_generate_key(ecdh))
  910. {
  911. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
  912. goto err;
  913. }
  914. }
  915. if (((group = EC_KEY_get0_group(ecdh)) == NULL) ||
  916. (EC_KEY_get0_public_key(ecdh) == NULL) ||
  917. (EC_KEY_get0_private_key(ecdh) == NULL))
  918. {
  919. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
  920. goto err;
  921. }
  922. if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
  923. (EC_GROUP_get_degree(group) > 163))
  924. {
  925. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
  926. goto err;
  927. }
  928. /* XXX: For now, we only support ephemeral ECDH
  929. * keys over named (not generic) curves. For
  930. * supported named curves, curve_id is non-zero.
  931. */
  932. if ((curve_id =
  933. tls1_ec_nid2curve_id(EC_GROUP_get_curve_name(group)))
  934. == 0)
  935. {
  936. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
  937. goto err;
  938. }
  939. /* Encode the public key.
  940. * First check the size of encoding and
  941. * allocate memory accordingly.
  942. */
  943. encodedlen = EC_POINT_point2oct(group,
  944. EC_KEY_get0_public_key(ecdh),
  945. POINT_CONVERSION_UNCOMPRESSED,
  946. NULL, 0, NULL);
  947. encodedPoint = (unsigned char *)
  948. OPENSSL_malloc(encodedlen*sizeof(unsigned char));
  949. bn_ctx = BN_CTX_new();
  950. if ((encodedPoint == NULL) || (bn_ctx == NULL))
  951. {
  952. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  953. goto err;
  954. }
  955. encodedlen = EC_POINT_point2oct(group,
  956. EC_KEY_get0_public_key(ecdh),
  957. POINT_CONVERSION_UNCOMPRESSED,
  958. encodedPoint, encodedlen, bn_ctx);
  959. if (encodedlen == 0)
  960. {
  961. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
  962. goto err;
  963. }
  964. BN_CTX_free(bn_ctx); bn_ctx=NULL;
  965. /* XXX: For now, we only support named (not
  966. * generic) curves in ECDH ephemeral key exchanges.
  967. * In this situation, we need four additional bytes
  968. * to encode the entire ServerECDHParams
  969. * structure.
  970. */
  971. n = 4 + encodedlen;
  972. /* We'll generate the serverKeyExchange message
  973. * explicitly so we can set these to NULLs
  974. */
  975. r[0]=NULL;
  976. r[1]=NULL;
  977. r[2]=NULL;
  978. r[3]=NULL;
  979. }
  980. else
  981. #endif /* !OPENSSL_NO_ECDH */
  982. #ifndef OPENSSL_NO_PSK
  983. if (type & SSL_kPSK)
  984. {
  985. /* reserve size for record length and PSK identity hint*/
  986. n+=2+strlen(s->ctx->psk_identity_hint);
  987. }
  988. else
  989. #endif /* !OPENSSL_NO_PSK */
  990. {
  991. al=SSL_AD_HANDSHAKE_FAILURE;
  992. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
  993. goto f_err;
  994. }
  995. for (i=0; r[i] != NULL; i++)
  996. {
  997. nr[i]=BN_num_bytes(r[i]);
  998. n+=2+nr[i];
  999. }
  1000. if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
  1001. && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
  1002. {
  1003. if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher))
  1004. == NULL)
  1005. {
  1006. al=SSL_AD_DECODE_ERROR;
  1007. goto f_err;
  1008. }
  1009. kn=EVP_PKEY_size(pkey);
  1010. }
  1011. else
  1012. {
  1013. pkey=NULL;
  1014. kn=0;
  1015. }
  1016. if (!BUF_MEM_grow_clean(buf,n+DTLS1_HM_HEADER_LENGTH+kn))
  1017. {
  1018. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF);
  1019. goto err;
  1020. }
  1021. d=(unsigned char *)s->init_buf->data;
  1022. p= &(d[DTLS1_HM_HEADER_LENGTH]);
  1023. for (i=0; r[i] != NULL; i++)
  1024. {
  1025. s2n(nr[i],p);
  1026. BN_bn2bin(r[i],p);
  1027. p+=nr[i];
  1028. }
  1029. #ifndef OPENSSL_NO_ECDH
  1030. if (type & SSL_kEECDH)
  1031. {
  1032. /* XXX: For now, we only support named (not generic) curves.
  1033. * In this situation, the serverKeyExchange message has:
  1034. * [1 byte CurveType], [2 byte CurveName]
  1035. * [1 byte length of encoded point], followed by
  1036. * the actual encoded point itself
  1037. */
  1038. *p = NAMED_CURVE_TYPE;
  1039. p += 1;
  1040. *p = 0;
  1041. p += 1;
  1042. *p = curve_id;
  1043. p += 1;
  1044. *p = encodedlen;
  1045. p += 1;
  1046. memcpy((unsigned char*)p,
  1047. (unsigned char *)encodedPoint,
  1048. encodedlen);
  1049. OPENSSL_free(encodedPoint);
  1050. p += encodedlen;
  1051. }
  1052. #endif
  1053. #ifndef OPENSSL_NO_PSK
  1054. if (type & SSL_kPSK)
  1055. {
  1056. /* copy PSK identity hint */
  1057. s2n(strlen(s->ctx->psk_identity_hint), p);
  1058. strncpy((char *)p, s->ctx->psk_identity_hint, strlen(s->ctx->psk_identity_hint));
  1059. p+=strlen(s->ctx->psk_identity_hint);
  1060. }
  1061. #endif
  1062. /* not anonymous */
  1063. if (pkey != NULL)
  1064. {
  1065. /* n is the length of the params, they start at
  1066. * &(d[DTLS1_HM_HEADER_LENGTH]) and p points to the space
  1067. * at the end. */
  1068. #ifndef OPENSSL_NO_RSA
  1069. if (pkey->type == EVP_PKEY_RSA)
  1070. {
  1071. q=md_buf;
  1072. j=0;
  1073. for (num=2; num > 0; num--)
  1074. {
  1075. EVP_DigestInit_ex(&md_ctx,(num == 2)
  1076. ?s->ctx->md5:s->ctx->sha1, NULL);
  1077. EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1078. EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1079. EVP_DigestUpdate(&md_ctx,&(d[DTLS1_HM_HEADER_LENGTH]),n);
  1080. EVP_DigestFinal_ex(&md_ctx,q,
  1081. (unsigned int *)&i);
  1082. q+=i;
  1083. j+=i;
  1084. }
  1085. if (RSA_sign(NID_md5_sha1, md_buf, j,
  1086. &(p[2]), &u, pkey->pkey.rsa) <= 0)
  1087. {
  1088. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_RSA);
  1089. goto err;
  1090. }
  1091. s2n(u,p);
  1092. n+=u+2;
  1093. }
  1094. else
  1095. #endif
  1096. #if !defined(OPENSSL_NO_DSA)
  1097. if (pkey->type == EVP_PKEY_DSA)
  1098. {
  1099. /* lets do DSS */
  1100. EVP_SignInit_ex(&md_ctx,EVP_dss1(), NULL);
  1101. EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1102. EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1103. EVP_SignUpdate(&md_ctx,&(d[DTLS1_HM_HEADER_LENGTH]),n);
  1104. if (!EVP_SignFinal(&md_ctx,&(p[2]),
  1105. (unsigned int *)&i,pkey))
  1106. {
  1107. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_DSA);
  1108. goto err;
  1109. }
  1110. s2n(i,p);
  1111. n+=i+2;
  1112. }
  1113. else
  1114. #endif
  1115. #if !defined(OPENSSL_NO_ECDSA)
  1116. if (pkey->type == EVP_PKEY_EC)
  1117. {
  1118. /* let's do ECDSA */
  1119. EVP_SignInit_ex(&md_ctx,EVP_ecdsa(), NULL);
  1120. EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1121. EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1122. EVP_SignUpdate(&md_ctx,&(d[4]),n);
  1123. if (!EVP_SignFinal(&md_ctx,&(p[2]),
  1124. (unsigned int *)&i,pkey))
  1125. {
  1126. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_ECDSA);
  1127. goto err;
  1128. }
  1129. s2n(i,p);
  1130. n+=i+2;
  1131. }
  1132. else
  1133. #endif
  1134. {
  1135. /* Is this error check actually needed? */
  1136. al=SSL_AD_HANDSHAKE_FAILURE;
  1137. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_PKEY_TYPE);
  1138. goto f_err;
  1139. }
  1140. }
  1141. d = dtls1_set_message_header(s, d,
  1142. SSL3_MT_SERVER_KEY_EXCHANGE, n, 0, n);
  1143. /* we should now have things packed up, so lets send
  1144. * it off */
  1145. s->init_num=n+DTLS1_HM_HEADER_LENGTH;
  1146. s->init_off=0;
  1147. /* buffer the message to handle re-xmits */
  1148. dtls1_buffer_message(s, 0);
  1149. }
  1150. s->state = SSL3_ST_SW_KEY_EXCH_B;
  1151. EVP_MD_CTX_cleanup(&md_ctx);
  1152. return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
  1153. f_err:
  1154. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1155. err:
  1156. #ifndef OPENSSL_NO_ECDH
  1157. if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
  1158. BN_CTX_free(bn_ctx);
  1159. #endif
  1160. EVP_MD_CTX_cleanup(&md_ctx);
  1161. return(-1);
  1162. }
  1163. int dtls1_send_certificate_request(SSL *s)
  1164. {
  1165. unsigned char *p,*d;
  1166. int i,j,nl,off,n;
  1167. STACK_OF(X509_NAME) *sk=NULL;
  1168. X509_NAME *name;
  1169. BUF_MEM *buf;
  1170. unsigned int msg_len;
  1171. if (s->state == SSL3_ST_SW_CERT_REQ_A)
  1172. {
  1173. buf=s->init_buf;
  1174. d=p=(unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH]);
  1175. /* get the list of acceptable cert types */
  1176. p++;
  1177. n=ssl3_get_req_cert_type(s,p);
  1178. d[0]=n;
  1179. p+=n;
  1180. n++;
  1181. off=n;
  1182. p+=2;
  1183. n+=2;
  1184. sk=SSL_get_client_CA_list(s);
  1185. nl=0;
  1186. if (sk != NULL)
  1187. {
  1188. for (i=0; i<sk_X509_NAME_num(sk); i++)
  1189. {
  1190. name=sk_X509_NAME_value(sk,i);
  1191. j=i2d_X509_NAME(name,NULL);
  1192. if (!BUF_MEM_grow_clean(buf,DTLS1_HM_HEADER_LENGTH+n+j+2))
  1193. {
  1194. SSLerr(SSL_F_DTLS1_SEND_CERTIFICATE_REQUEST,ERR_R_BUF_LIB);
  1195. goto err;
  1196. }
  1197. p=(unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH+n]);
  1198. if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
  1199. {
  1200. s2n(j,p);
  1201. i2d_X509_NAME(name,&p);
  1202. n+=2+j;
  1203. nl+=2+j;
  1204. }
  1205. else
  1206. {
  1207. d=p;
  1208. i2d_X509_NAME(name,&p);
  1209. j-=2; s2n(j,d); j+=2;
  1210. n+=j;
  1211. nl+=j;
  1212. }
  1213. }
  1214. }
  1215. /* else no CA names */
  1216. p=(unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH+off]);
  1217. s2n(nl,p);
  1218. d=(unsigned char *)buf->data;
  1219. *(d++)=SSL3_MT_CERTIFICATE_REQUEST;
  1220. l2n3(n,d);
  1221. s2n(s->d1->handshake_write_seq,d);
  1222. s->d1->handshake_write_seq++;
  1223. /* we should now have things packed up, so lets send
  1224. * it off */
  1225. s->init_num=n+DTLS1_HM_HEADER_LENGTH;
  1226. s->init_off=0;
  1227. #ifdef NETSCAPE_HANG_BUG
  1228. /* XXX: what to do about this? */
  1229. p=(unsigned char *)s->init_buf->data + s->init_num;
  1230. /* do the header */
  1231. *(p++)=SSL3_MT_SERVER_DONE;
  1232. *(p++)=0;
  1233. *(p++)=0;
  1234. *(p++)=0;
  1235. s->init_num += 4;
  1236. #endif
  1237. /* XDTLS: set message header ? */
  1238. msg_len = s->init_num - DTLS1_HM_HEADER_LENGTH;
  1239. dtls1_set_message_header(s, (void *)s->init_buf->data,
  1240. SSL3_MT_CERTIFICATE_REQUEST, msg_len, 0, msg_len);
  1241. /* buffer the message to handle re-xmits */
  1242. dtls1_buffer_message(s, 0);
  1243. s->state = SSL3_ST_SW_CERT_REQ_B;
  1244. }
  1245. /* SSL3_ST_SW_CERT_REQ_B */
  1246. return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
  1247. err:
  1248. return(-1);
  1249. }
  1250. int dtls1_send_server_certificate(SSL *s)
  1251. {
  1252. unsigned long l;
  1253. X509 *x;
  1254. if (s->state == SSL3_ST_SW_CERT_A)
  1255. {
  1256. x=ssl_get_server_send_cert(s);
  1257. if (x == NULL)
  1258. {
  1259. /* VRS: allow null cert if auth == KRB5 */
  1260. if ((s->s3->tmp.new_cipher->algorithm_mkey != SSL_kKRB5) ||
  1261. (s->s3->tmp.new_cipher->algorithm_auth != SSL_aKRB5))
  1262. {
  1263. SSLerr(SSL_F_DTLS1_SEND_SERVER_CERTIFICATE,ERR_R_INTERNAL_ERROR);
  1264. return(0);
  1265. }
  1266. }
  1267. l=dtls1_output_cert_chain(s,x);
  1268. s->state=SSL3_ST_SW_CERT_B;
  1269. s->init_num=(int)l;
  1270. s->init_off=0;
  1271. /* buffer the message to handle re-xmits */
  1272. dtls1_buffer_message(s, 0);
  1273. }
  1274. /* SSL3_ST_SW_CERT_B */
  1275. return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
  1276. }
  1277. #ifndef OPENSSL_NO_TLSEXT
  1278. int dtls1_send_newsession_ticket(SSL *s)
  1279. {
  1280. if (s->state == SSL3_ST_SW_SESSION_TICKET_A)
  1281. {
  1282. unsigned char *p, *senc, *macstart;
  1283. int len, slen;
  1284. unsigned int hlen, msg_len;
  1285. EVP_CIPHER_CTX ctx;
  1286. HMAC_CTX hctx;
  1287. SSL_CTX *tctx = s->initial_ctx;
  1288. unsigned char iv[EVP_MAX_IV_LENGTH];
  1289. unsigned char key_name[16];
  1290. /* get session encoding length */
  1291. slen = i2d_SSL_SESSION(s->session, NULL);
  1292. /* Some length values are 16 bits, so forget it if session is
  1293. * too long
  1294. */
  1295. if (slen > 0xFF00)
  1296. return -1;
  1297. /* Grow buffer if need be: the length calculation is as
  1298. * follows 12 (DTLS handshake message header) +
  1299. * 4 (ticket lifetime hint) + 2 (ticket length) +
  1300. * 16 (key name) + max_iv_len (iv length) +
  1301. * session_length + max_enc_block_size (max encrypted session
  1302. * length) + max_md_size (HMAC).
  1303. */
  1304. if (!BUF_MEM_grow(s->init_buf,
  1305. DTLS1_HM_HEADER_LENGTH + 22 + EVP_MAX_IV_LENGTH +
  1306. EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE + slen))
  1307. return -1;
  1308. senc = OPENSSL_malloc(slen);
  1309. if (!senc)
  1310. return -1;
  1311. p = senc;
  1312. i2d_SSL_SESSION(s->session, &p);
  1313. p=(unsigned char *)&(s->init_buf->data[DTLS1_HM_HEADER_LENGTH]);
  1314. EVP_CIPHER_CTX_init(&ctx);
  1315. HMAC_CTX_init(&hctx);
  1316. /* Initialize HMAC and cipher contexts. If callback present
  1317. * it does all the work otherwise use generated values
  1318. * from parent ctx.
  1319. */
  1320. if (tctx->tlsext_ticket_key_cb)
  1321. {
  1322. if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx,
  1323. &hctx, 1) < 0)
  1324. {
  1325. OPENSSL_free(senc);
  1326. return -1;
  1327. }
  1328. }
  1329. else
  1330. {
  1331. RAND_pseudo_bytes(iv, 16);
  1332. EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
  1333. tctx->tlsext_tick_aes_key, iv);
  1334. HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
  1335. tlsext_tick_md(), NULL);
  1336. memcpy(key_name, tctx->tlsext_tick_key_name, 16);
  1337. }
  1338. l2n(s->session->tlsext_tick_lifetime_hint, p);
  1339. /* Skip ticket length for now */
  1340. p += 2;
  1341. /* Output key name */
  1342. macstart = p;
  1343. memcpy(p, key_name, 16);
  1344. p += 16;
  1345. /* output IV */
  1346. memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
  1347. p += EVP_CIPHER_CTX_iv_length(&ctx);
  1348. /* Encrypt session data */
  1349. EVP_EncryptUpdate(&ctx, p, &len, senc, slen);
  1350. p += len;
  1351. EVP_EncryptFinal(&ctx, p, &len);
  1352. p += len;
  1353. EVP_CIPHER_CTX_cleanup(&ctx);
  1354. HMAC_Update(&hctx, macstart, p - macstart);
  1355. HMAC_Final(&hctx, p, &hlen);
  1356. HMAC_CTX_cleanup(&hctx);
  1357. p += hlen;
  1358. /* Now write out lengths: p points to end of data written */
  1359. /* Total length */
  1360. len = p - (unsigned char *)(s->init_buf->data);
  1361. /* Ticket length */
  1362. p=(unsigned char *)&(s->init_buf->data[DTLS1_HM_HEADER_LENGTH]) + 4;
  1363. s2n(len - DTLS1_HM_HEADER_LENGTH - 6, p);
  1364. /* number of bytes to write */
  1365. s->init_num= len;
  1366. s->state=SSL3_ST_SW_SESSION_TICKET_B;
  1367. s->init_off=0;
  1368. OPENSSL_free(senc);
  1369. /* XDTLS: set message header ? */
  1370. msg_len = s->init_num - DTLS1_HM_HEADER_LENGTH;
  1371. dtls1_set_message_header(s, (void *)s->init_buf->data,
  1372. SSL3_MT_NEWSESSION_TICKET, msg_len, 0, msg_len);
  1373. /* buffer the message to handle re-xmits */
  1374. dtls1_buffer_message(s, 0);
  1375. }
  1376. /* SSL3_ST_SW_SESSION_TICKET_B */
  1377. return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
  1378. }
  1379. #endif