d1_srvr.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  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-2005 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 l,Time=(unsigned long)time(NULL);
  144. void (*cb)(const SSL *ssl,int type,int val)=NULL;
  145. long num1;
  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. if ( (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE))
  221. s->d1->send_cookie = 1;
  222. else
  223. s->d1->send_cookie = 0;
  224. break;
  225. case SSL3_ST_SW_HELLO_REQ_A:
  226. case SSL3_ST_SW_HELLO_REQ_B:
  227. s->shutdown=0;
  228. ret=dtls1_send_hello_request(s);
  229. if (ret <= 0) goto end;
  230. s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C;
  231. s->state=SSL3_ST_SW_FLUSH;
  232. s->init_num=0;
  233. ssl3_init_finished_mac(s);
  234. break;
  235. case SSL3_ST_SW_HELLO_REQ_C:
  236. s->state=SSL_ST_OK;
  237. break;
  238. case SSL3_ST_SR_CLNT_HELLO_A:
  239. case SSL3_ST_SR_CLNT_HELLO_B:
  240. case SSL3_ST_SR_CLNT_HELLO_C:
  241. s->shutdown=0;
  242. ret=ssl3_get_client_hello(s);
  243. if (ret <= 0) goto end;
  244. s->new_session = 2;
  245. if ( s->d1->send_cookie)
  246. s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A;
  247. else
  248. s->state = SSL3_ST_SW_SRVR_HELLO_A;
  249. s->init_num=0;
  250. break;
  251. case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A:
  252. case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B:
  253. ret = dtls1_send_hello_verify_request(s);
  254. if ( ret <= 0) goto end;
  255. s->d1->send_cookie = 0;
  256. s->state=SSL3_ST_SW_FLUSH;
  257. s->s3->tmp.next_state=SSL3_ST_SR_CLNT_HELLO_A;
  258. break;
  259. case SSL3_ST_SW_SRVR_HELLO_A:
  260. case SSL3_ST_SW_SRVR_HELLO_B:
  261. ret=dtls1_send_server_hello(s);
  262. if (ret <= 0) goto end;
  263. if (s->hit)
  264. s->state=SSL3_ST_SW_CHANGE_A;
  265. else
  266. s->state=SSL3_ST_SW_CERT_A;
  267. s->init_num=0;
  268. break;
  269. case SSL3_ST_SW_CERT_A:
  270. case SSL3_ST_SW_CERT_B:
  271. /* Check if it is anon DH */
  272. if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
  273. {
  274. ret=dtls1_send_server_certificate(s);
  275. if (ret <= 0) goto end;
  276. }
  277. else
  278. skip=1;
  279. s->state=SSL3_ST_SW_KEY_EXCH_A;
  280. s->init_num=0;
  281. break;
  282. case SSL3_ST_SW_KEY_EXCH_A:
  283. case SSL3_ST_SW_KEY_EXCH_B:
  284. l=s->s3->tmp.new_cipher->algorithms;
  285. /* clear this, it may get reset by
  286. * send_server_key_exchange */
  287. if ((s->options & SSL_OP_EPHEMERAL_RSA)
  288. #ifndef OPENSSL_NO_KRB5
  289. && !(l & SSL_KRB5)
  290. #endif /* OPENSSL_NO_KRB5 */
  291. )
  292. /* option SSL_OP_EPHEMERAL_RSA sends temporary RSA key
  293. * even when forbidden by protocol specs
  294. * (handshake may fail as clients are not required to
  295. * be able to handle this) */
  296. s->s3->tmp.use_rsa_tmp=1;
  297. else
  298. s->s3->tmp.use_rsa_tmp=0;
  299. /* only send if a DH key exchange, fortezza or
  300. * RSA but we have a sign only certificate */
  301. if (s->s3->tmp.use_rsa_tmp
  302. || (l & (SSL_DH|SSL_kFZA))
  303. || ((l & SSL_kRSA)
  304. && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
  305. || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
  306. && EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
  307. )
  308. )
  309. )
  310. )
  311. {
  312. ret=dtls1_send_server_key_exchange(s);
  313. if (ret <= 0) goto end;
  314. }
  315. else
  316. skip=1;
  317. s->state=SSL3_ST_SW_CERT_REQ_A;
  318. s->init_num=0;
  319. break;
  320. case SSL3_ST_SW_CERT_REQ_A:
  321. case SSL3_ST_SW_CERT_REQ_B:
  322. if (/* don't request cert unless asked for it: */
  323. !(s->verify_mode & SSL_VERIFY_PEER) ||
  324. /* if SSL_VERIFY_CLIENT_ONCE is set,
  325. * don't request cert during re-negotiation: */
  326. ((s->session->peer != NULL) &&
  327. (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
  328. /* never request cert in anonymous ciphersuites
  329. * (see section "Certificate request" in SSL 3 drafts
  330. * and in RFC 2246): */
  331. ((s->s3->tmp.new_cipher->algorithms & SSL_aNULL) &&
  332. /* ... except when the application insists on verification
  333. * (against the specs, but s3_clnt.c accepts this for SSL 3) */
  334. !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
  335. /* never request cert in Kerberos ciphersuites */
  336. (s->s3->tmp.new_cipher->algorithms & SSL_aKRB5))
  337. {
  338. /* no cert request */
  339. skip=1;
  340. s->s3->tmp.cert_request=0;
  341. s->state=SSL3_ST_SW_SRVR_DONE_A;
  342. }
  343. else
  344. {
  345. s->s3->tmp.cert_request=1;
  346. ret=dtls1_send_certificate_request(s);
  347. if (ret <= 0) goto end;
  348. #ifndef NETSCAPE_HANG_BUG
  349. s->state=SSL3_ST_SW_SRVR_DONE_A;
  350. #else
  351. s->state=SSL3_ST_SW_FLUSH;
  352. s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
  353. #endif
  354. s->init_num=0;
  355. }
  356. break;
  357. case SSL3_ST_SW_SRVR_DONE_A:
  358. case SSL3_ST_SW_SRVR_DONE_B:
  359. ret=dtls1_send_server_done(s);
  360. if (ret <= 0) goto end;
  361. s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
  362. s->state=SSL3_ST_SW_FLUSH;
  363. s->init_num=0;
  364. break;
  365. case SSL3_ST_SW_FLUSH:
  366. /* number of bytes to be flushed */
  367. num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
  368. if (num1 > 0)
  369. {
  370. s->rwstate=SSL_WRITING;
  371. num1=BIO_flush(s->wbio);
  372. if (num1 <= 0) { ret= -1; goto end; }
  373. s->rwstate=SSL_NOTHING;
  374. }
  375. s->state=s->s3->tmp.next_state;
  376. break;
  377. case SSL3_ST_SR_CERT_A:
  378. case SSL3_ST_SR_CERT_B:
  379. /* Check for second client hello (MS SGC) */
  380. ret = ssl3_check_client_hello(s);
  381. if (ret <= 0)
  382. goto end;
  383. if (ret == 2)
  384. s->state = SSL3_ST_SR_CLNT_HELLO_C;
  385. else {
  386. /* could be sent for a DH cert, even if we
  387. * have not asked for it :-) */
  388. ret=ssl3_get_client_certificate(s);
  389. if (ret <= 0) goto end;
  390. s->init_num=0;
  391. s->state=SSL3_ST_SR_KEY_EXCH_A;
  392. }
  393. break;
  394. case SSL3_ST_SR_KEY_EXCH_A:
  395. case SSL3_ST_SR_KEY_EXCH_B:
  396. ret=ssl3_get_client_key_exchange(s);
  397. if (ret <= 0) goto end;
  398. s->state=SSL3_ST_SR_CERT_VRFY_A;
  399. s->init_num=0;
  400. /* We need to get hashes here so if there is
  401. * a client cert, it can be verified */
  402. s->method->ssl3_enc->cert_verify_mac(s,
  403. &(s->s3->finish_dgst1),
  404. &(s->s3->tmp.cert_verify_md[0]));
  405. s->method->ssl3_enc->cert_verify_mac(s,
  406. &(s->s3->finish_dgst2),
  407. &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]));
  408. break;
  409. case SSL3_ST_SR_CERT_VRFY_A:
  410. case SSL3_ST_SR_CERT_VRFY_B:
  411. /* we should decide if we expected this one */
  412. ret=ssl3_get_cert_verify(s);
  413. if (ret <= 0) goto end;
  414. s->state=SSL3_ST_SR_FINISHED_A;
  415. s->init_num=0;
  416. break;
  417. case SSL3_ST_SR_FINISHED_A:
  418. case SSL3_ST_SR_FINISHED_B:
  419. ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A,
  420. SSL3_ST_SR_FINISHED_B);
  421. if (ret <= 0) goto end;
  422. if (s->hit)
  423. s->state=SSL_ST_OK;
  424. else
  425. s->state=SSL3_ST_SW_CHANGE_A;
  426. s->init_num=0;
  427. break;
  428. case SSL3_ST_SW_CHANGE_A:
  429. case SSL3_ST_SW_CHANGE_B:
  430. s->session->cipher=s->s3->tmp.new_cipher;
  431. if (!s->method->ssl3_enc->setup_key_block(s))
  432. { ret= -1; goto end; }
  433. ret=dtls1_send_change_cipher_spec(s,
  434. SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B);
  435. if (ret <= 0) goto end;
  436. s->state=SSL3_ST_SW_FINISHED_A;
  437. s->init_num=0;
  438. if (!s->method->ssl3_enc->change_cipher_state(s,
  439. SSL3_CHANGE_CIPHER_SERVER_WRITE))
  440. {
  441. ret= -1;
  442. goto end;
  443. }
  444. dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
  445. break;
  446. case SSL3_ST_SW_FINISHED_A:
  447. case SSL3_ST_SW_FINISHED_B:
  448. ret=dtls1_send_finished(s,
  449. SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B,
  450. s->method->ssl3_enc->server_finished_label,
  451. s->method->ssl3_enc->server_finished_label_len);
  452. if (ret <= 0) goto end;
  453. s->state=SSL3_ST_SW_FLUSH;
  454. if (s->hit)
  455. s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A;
  456. else
  457. s->s3->tmp.next_state=SSL_ST_OK;
  458. s->init_num=0;
  459. break;
  460. case SSL_ST_OK:
  461. /* clean a few things up */
  462. ssl3_cleanup_key_block(s);
  463. #if 0
  464. BUF_MEM_free(s->init_buf);
  465. s->init_buf=NULL;
  466. #endif
  467. /* remove buffering on output */
  468. ssl_free_wbio_buffer(s);
  469. s->init_num=0;
  470. if (s->new_session == 2) /* skipped if we just sent a HelloRequest */
  471. {
  472. /* actually not necessarily a 'new' session unless
  473. * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is set */
  474. s->new_session=0;
  475. ssl_update_cache(s,SSL_SESS_CACHE_SERVER);
  476. s->ctx->stats.sess_accept_good++;
  477. /* s->server=1; */
  478. s->handshake_func=dtls1_accept;
  479. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
  480. }
  481. ret = 1;
  482. /* done handshaking, next message is client hello */
  483. s->d1->handshake_read_seq = 0;
  484. /* next message is server hello */
  485. s->d1->handshake_write_seq = 0;
  486. goto end;
  487. /* break; */
  488. default:
  489. SSLerr(SSL_F_DTLS1_ACCEPT,SSL_R_UNKNOWN_STATE);
  490. ret= -1;
  491. goto end;
  492. /* break; */
  493. }
  494. if (!s->s3->tmp.reuse_message && !skip)
  495. {
  496. if (s->debug)
  497. {
  498. if ((ret=BIO_flush(s->wbio)) <= 0)
  499. goto end;
  500. }
  501. if ((cb != NULL) && (s->state != state))
  502. {
  503. new_state=s->state;
  504. s->state=state;
  505. cb(s,SSL_CB_ACCEPT_LOOP,1);
  506. s->state=new_state;
  507. }
  508. }
  509. skip=0;
  510. }
  511. end:
  512. /* BIO_flush(s->wbio); */
  513. s->in_handshake--;
  514. if (cb != NULL)
  515. cb(s,SSL_CB_ACCEPT_EXIT,ret);
  516. return(ret);
  517. }
  518. int dtls1_send_hello_request(SSL *s)
  519. {
  520. unsigned char *p;
  521. if (s->state == SSL3_ST_SW_HELLO_REQ_A)
  522. {
  523. p=(unsigned char *)s->init_buf->data;
  524. p = dtls1_set_message_header(s, p, SSL3_MT_HELLO_REQUEST, 0, 0, 0);
  525. s->state=SSL3_ST_SW_HELLO_REQ_B;
  526. /* number of bytes to write */
  527. s->init_num=DTLS1_HM_HEADER_LENGTH;
  528. s->init_off=0;
  529. /* no need to buffer this message, since there are no retransmit
  530. * requests for it */
  531. }
  532. /* SSL3_ST_SW_HELLO_REQ_B */
  533. return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
  534. }
  535. int dtls1_send_hello_verify_request(SSL *s)
  536. {
  537. unsigned int msg_len;
  538. unsigned char *msg, *buf, *p;
  539. if (s->state == DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A)
  540. {
  541. buf = (unsigned char *)s->init_buf->data;
  542. msg = p = &(buf[DTLS1_HM_HEADER_LENGTH]);
  543. *(p++) = s->version >> 8;
  544. *(p++) = s->version & 0xFF;
  545. *(p++) = (unsigned char) s->d1->cookie_len;
  546. if ( s->ctx->app_gen_cookie_cb != NULL &&
  547. s->ctx->app_gen_cookie_cb(s, s->d1->cookie,
  548. &(s->d1->cookie_len)) == 0)
  549. {
  550. SSLerr(SSL_F_DTLS1_SEND_HELLO_VERIFY_REQUEST,ERR_R_INTERNAL_ERROR);
  551. return 0;
  552. }
  553. /* else the cookie is assumed to have
  554. * been initialized by the application */
  555. memcpy(p, s->d1->cookie, s->d1->cookie_len);
  556. p += s->d1->cookie_len;
  557. msg_len = p - msg;
  558. dtls1_set_message_header(s, buf,
  559. DTLS1_MT_HELLO_VERIFY_REQUEST, msg_len, 0, msg_len);
  560. s->state=DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B;
  561. /* number of bytes to write */
  562. s->init_num=p-buf;
  563. s->init_off=0;
  564. /* buffer the message to handle re-xmits */
  565. dtls1_buffer_message(s, 0);
  566. }
  567. /* s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B */
  568. return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
  569. }
  570. int dtls1_send_server_hello(SSL *s)
  571. {
  572. unsigned char *buf;
  573. unsigned char *p,*d;
  574. int i;
  575. unsigned int sl;
  576. unsigned long l,Time;
  577. if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
  578. {
  579. buf=(unsigned char *)s->init_buf->data;
  580. p=s->s3->server_random;
  581. Time=(unsigned long)time(NULL); /* Time */
  582. l2n(Time,p);
  583. RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-sizeof(Time));
  584. /* Do the message type and length last */
  585. d=p= &(buf[DTLS1_HM_HEADER_LENGTH]);
  586. *(p++)=s->version>>8;
  587. *(p++)=s->version&0xff;
  588. /* Random stuff */
  589. memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
  590. p+=SSL3_RANDOM_SIZE;
  591. /* now in theory we have 3 options to sending back the
  592. * session id. If it is a re-use, we send back the
  593. * old session-id, if it is a new session, we send
  594. * back the new session-id or we send back a 0 length
  595. * session-id if we want it to be single use.
  596. * Currently I will not implement the '0' length session-id
  597. * 12-Jan-98 - I'll now support the '0' length stuff.
  598. */
  599. if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER))
  600. s->session->session_id_length=0;
  601. sl=s->session->session_id_length;
  602. if (sl > sizeof s->session->session_id)
  603. {
  604. SSLerr(SSL_F_DTLS1_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
  605. return -1;
  606. }
  607. *(p++)=sl;
  608. memcpy(p,s->session->session_id,sl);
  609. p+=sl;
  610. /* put the cipher */
  611. i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p);
  612. p+=i;
  613. /* put the compression method */
  614. #ifdef OPENSSL_NO_COMP
  615. *(p++)=0;
  616. #else
  617. if (s->s3->tmp.new_compression == NULL)
  618. *(p++)=0;
  619. else
  620. *(p++)=s->s3->tmp.new_compression->id;
  621. #endif
  622. /* do the header */
  623. l=(p-d);
  624. d=buf;
  625. d = dtls1_set_message_header(s, d, SSL3_MT_SERVER_HELLO, l, 0, l);
  626. s->state=SSL3_ST_CW_CLNT_HELLO_B;
  627. /* number of bytes to write */
  628. s->init_num=p-buf;
  629. s->init_off=0;
  630. /* buffer the message to handle re-xmits */
  631. dtls1_buffer_message(s, 0);
  632. }
  633. /* SSL3_ST_CW_CLNT_HELLO_B */
  634. return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
  635. }
  636. int dtls1_send_server_done(SSL *s)
  637. {
  638. unsigned char *p;
  639. if (s->state == SSL3_ST_SW_SRVR_DONE_A)
  640. {
  641. p=(unsigned char *)s->init_buf->data;
  642. /* do the header */
  643. p = dtls1_set_message_header(s, p, SSL3_MT_SERVER_DONE, 0, 0, 0);
  644. s->state=SSL3_ST_SW_SRVR_DONE_B;
  645. /* number of bytes to write */
  646. s->init_num=DTLS1_HM_HEADER_LENGTH;
  647. s->init_off=0;
  648. /* buffer the message to handle re-xmits */
  649. dtls1_buffer_message(s, 0);
  650. }
  651. /* SSL3_ST_CW_CLNT_HELLO_B */
  652. return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
  653. }
  654. int dtls1_send_server_key_exchange(SSL *s)
  655. {
  656. #ifndef OPENSSL_NO_RSA
  657. unsigned char *q;
  658. int j,num;
  659. RSA *rsa;
  660. unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
  661. unsigned int u;
  662. #endif
  663. #ifndef OPENSSL_NO_DH
  664. DH *dh=NULL,*dhp;
  665. #endif
  666. EVP_PKEY *pkey;
  667. unsigned char *p,*d;
  668. int al,i;
  669. unsigned long type;
  670. int n;
  671. CERT *cert;
  672. BIGNUM *r[4];
  673. int nr[4],kn;
  674. BUF_MEM *buf;
  675. EVP_MD_CTX md_ctx;
  676. EVP_MD_CTX_init(&md_ctx);
  677. if (s->state == SSL3_ST_SW_KEY_EXCH_A)
  678. {
  679. type=s->s3->tmp.new_cipher->algorithms & SSL_MKEY_MASK;
  680. cert=s->cert;
  681. buf=s->init_buf;
  682. r[0]=r[1]=r[2]=r[3]=NULL;
  683. n=0;
  684. #ifndef OPENSSL_NO_RSA
  685. if (type & SSL_kRSA)
  686. {
  687. rsa=cert->rsa_tmp;
  688. if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL))
  689. {
  690. rsa=s->cert->rsa_tmp_cb(s,
  691. SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
  692. SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
  693. if(rsa == NULL)
  694. {
  695. al=SSL_AD_HANDSHAKE_FAILURE;
  696. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
  697. goto f_err;
  698. }
  699. RSA_up_ref(rsa);
  700. cert->rsa_tmp=rsa;
  701. }
  702. if (rsa == NULL)
  703. {
  704. al=SSL_AD_HANDSHAKE_FAILURE;
  705. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_KEY);
  706. goto f_err;
  707. }
  708. r[0]=rsa->n;
  709. r[1]=rsa->e;
  710. s->s3->tmp.use_rsa_tmp=1;
  711. }
  712. else
  713. #endif
  714. #ifndef OPENSSL_NO_DH
  715. if (type & SSL_kEDH)
  716. {
  717. dhp=cert->dh_tmp;
  718. if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
  719. dhp=s->cert->dh_tmp_cb(s,
  720. SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
  721. SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
  722. if (dhp == NULL)
  723. {
  724. al=SSL_AD_HANDSHAKE_FAILURE;
  725. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
  726. goto f_err;
  727. }
  728. if (s->s3->tmp.dh != NULL)
  729. {
  730. DH_free(dh);
  731. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  732. goto err;
  733. }
  734. if ((dh=DHparams_dup(dhp)) == NULL)
  735. {
  736. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
  737. goto err;
  738. }
  739. s->s3->tmp.dh=dh;
  740. if ((dhp->pub_key == NULL ||
  741. dhp->priv_key == NULL ||
  742. (s->options & SSL_OP_SINGLE_DH_USE)))
  743. {
  744. if(!DH_generate_key(dh))
  745. {
  746. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
  747. ERR_R_DH_LIB);
  748. goto err;
  749. }
  750. }
  751. else
  752. {
  753. dh->pub_key=BN_dup(dhp->pub_key);
  754. dh->priv_key=BN_dup(dhp->priv_key);
  755. if ((dh->pub_key == NULL) ||
  756. (dh->priv_key == NULL))
  757. {
  758. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
  759. goto err;
  760. }
  761. }
  762. r[0]=dh->p;
  763. r[1]=dh->g;
  764. r[2]=dh->pub_key;
  765. }
  766. else
  767. #endif
  768. {
  769. al=SSL_AD_HANDSHAKE_FAILURE;
  770. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
  771. goto f_err;
  772. }
  773. for (i=0; r[i] != NULL; i++)
  774. {
  775. nr[i]=BN_num_bytes(r[i]);
  776. n+=2+nr[i];
  777. }
  778. if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
  779. {
  780. if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher))
  781. == NULL)
  782. {
  783. al=SSL_AD_DECODE_ERROR;
  784. goto f_err;
  785. }
  786. kn=EVP_PKEY_size(pkey);
  787. }
  788. else
  789. {
  790. pkey=NULL;
  791. kn=0;
  792. }
  793. if (!BUF_MEM_grow_clean(buf,n+DTLS1_HM_HEADER_LENGTH+kn))
  794. {
  795. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF);
  796. goto err;
  797. }
  798. d=(unsigned char *)s->init_buf->data;
  799. p= &(d[DTLS1_HM_HEADER_LENGTH]);
  800. for (i=0; r[i] != NULL; i++)
  801. {
  802. s2n(nr[i],p);
  803. BN_bn2bin(r[i],p);
  804. p+=nr[i];
  805. }
  806. /* not anonymous */
  807. if (pkey != NULL)
  808. {
  809. /* n is the length of the params, they start at
  810. * &(d[DTLS1_HM_HEADER_LENGTH]) and p points to the space
  811. * at the end. */
  812. #ifndef OPENSSL_NO_RSA
  813. if (pkey->type == EVP_PKEY_RSA)
  814. {
  815. q=md_buf;
  816. j=0;
  817. for (num=2; num > 0; num--)
  818. {
  819. EVP_DigestInit_ex(&md_ctx,(num == 2)
  820. ?s->ctx->md5:s->ctx->sha1, NULL);
  821. EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  822. EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  823. EVP_DigestUpdate(&md_ctx,&(d[DTLS1_HM_HEADER_LENGTH]),n);
  824. EVP_DigestFinal_ex(&md_ctx,q,
  825. (unsigned int *)&i);
  826. q+=i;
  827. j+=i;
  828. }
  829. if (RSA_sign(NID_md5_sha1, md_buf, j,
  830. &(p[2]), &u, pkey->pkey.rsa) <= 0)
  831. {
  832. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_RSA);
  833. goto err;
  834. }
  835. s2n(u,p);
  836. n+=u+2;
  837. }
  838. else
  839. #endif
  840. #if !defined(OPENSSL_NO_DSA)
  841. if (pkey->type == EVP_PKEY_DSA)
  842. {
  843. /* lets do DSS */
  844. EVP_SignInit_ex(&md_ctx,EVP_dss1(), NULL);
  845. EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  846. EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  847. EVP_SignUpdate(&md_ctx,&(d[DTLS1_HM_HEADER_LENGTH]),n);
  848. if (!EVP_SignFinal(&md_ctx,&(p[2]),
  849. (unsigned int *)&i,pkey))
  850. {
  851. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_DSA);
  852. goto err;
  853. }
  854. s2n(i,p);
  855. n+=i+2;
  856. }
  857. else
  858. #endif
  859. {
  860. /* Is this error check actually needed? */
  861. al=SSL_AD_HANDSHAKE_FAILURE;
  862. SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_PKEY_TYPE);
  863. goto f_err;
  864. }
  865. }
  866. d = dtls1_set_message_header(s, d,
  867. SSL3_MT_SERVER_KEY_EXCHANGE, n, 0, n);
  868. /* we should now have things packed up, so lets send
  869. * it off */
  870. s->init_num=n+DTLS1_HM_HEADER_LENGTH;
  871. s->init_off=0;
  872. /* buffer the message to handle re-xmits */
  873. dtls1_buffer_message(s, 0);
  874. }
  875. s->state = SSL3_ST_SW_KEY_EXCH_B;
  876. EVP_MD_CTX_cleanup(&md_ctx);
  877. return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
  878. f_err:
  879. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  880. err:
  881. EVP_MD_CTX_cleanup(&md_ctx);
  882. return(-1);
  883. }
  884. int dtls1_send_certificate_request(SSL *s)
  885. {
  886. unsigned char *p,*d;
  887. int i,j,nl,off,n;
  888. STACK_OF(X509_NAME) *sk=NULL;
  889. X509_NAME *name;
  890. BUF_MEM *buf;
  891. if (s->state == SSL3_ST_SW_CERT_REQ_A)
  892. {
  893. buf=s->init_buf;
  894. d=p=(unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH]);
  895. /* get the list of acceptable cert types */
  896. p++;
  897. n=ssl3_get_req_cert_type(s,p);
  898. d[0]=n;
  899. p+=n;
  900. n++;
  901. off=n;
  902. p+=2;
  903. n+=2;
  904. sk=SSL_get_client_CA_list(s);
  905. nl=0;
  906. if (sk != NULL)
  907. {
  908. for (i=0; i<sk_X509_NAME_num(sk); i++)
  909. {
  910. name=sk_X509_NAME_value(sk,i);
  911. j=i2d_X509_NAME(name,NULL);
  912. if (!BUF_MEM_grow_clean(buf,DTLS1_HM_HEADER_LENGTH+n+j+2))
  913. {
  914. SSLerr(SSL_F_DTLS1_SEND_CERTIFICATE_REQUEST,ERR_R_BUF_LIB);
  915. goto err;
  916. }
  917. p=(unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH+n]);
  918. if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
  919. {
  920. s2n(j,p);
  921. i2d_X509_NAME(name,&p);
  922. n+=2+j;
  923. nl+=2+j;
  924. }
  925. else
  926. {
  927. d=p;
  928. i2d_X509_NAME(name,&p);
  929. j-=2; s2n(j,d); j+=2;
  930. n+=j;
  931. nl+=j;
  932. }
  933. }
  934. }
  935. /* else no CA names */
  936. p=(unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH+off]);
  937. s2n(nl,p);
  938. d=(unsigned char *)buf->data;
  939. *(d++)=SSL3_MT_CERTIFICATE_REQUEST;
  940. l2n3(n,d);
  941. s2n(s->d1->handshake_write_seq,d);
  942. s->d1->handshake_write_seq++;
  943. /* we should now have things packed up, so lets send
  944. * it off */
  945. s->init_num=n+DTLS1_HM_HEADER_LENGTH;
  946. s->init_off=0;
  947. #ifdef NETSCAPE_HANG_BUG
  948. /* XXX: what to do about this? */
  949. p=(unsigned char *)s->init_buf->data + s->init_num;
  950. /* do the header */
  951. *(p++)=SSL3_MT_SERVER_DONE;
  952. *(p++)=0;
  953. *(p++)=0;
  954. *(p++)=0;
  955. s->init_num += 4;
  956. #endif
  957. /* XDTLS: set message header ? */
  958. /* buffer the message to handle re-xmits */
  959. dtls1_buffer_message(s, 0);
  960. s->state = SSL3_ST_SW_CERT_REQ_B;
  961. }
  962. /* SSL3_ST_SW_CERT_REQ_B */
  963. return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
  964. err:
  965. return(-1);
  966. }
  967. int dtls1_send_server_certificate(SSL *s)
  968. {
  969. unsigned long l;
  970. X509 *x;
  971. if (s->state == SSL3_ST_SW_CERT_A)
  972. {
  973. x=ssl_get_server_send_cert(s);
  974. if (x == NULL &&
  975. /* VRS: allow null cert if auth == KRB5 */
  976. (s->s3->tmp.new_cipher->algorithms
  977. & (SSL_MKEY_MASK|SSL_AUTH_MASK))
  978. != (SSL_aKRB5|SSL_kKRB5))
  979. {
  980. SSLerr(SSL_F_DTLS1_SEND_SERVER_CERTIFICATE,ERR_R_INTERNAL_ERROR);
  981. return(0);
  982. }
  983. l=dtls1_output_cert_chain(s,x);
  984. s->state=SSL3_ST_SW_CERT_B;
  985. s->init_num=(int)l;
  986. s->init_off=0;
  987. /* buffer the message to handle re-xmits */
  988. dtls1_buffer_message(s, 0);
  989. }
  990. /* SSL3_ST_SW_CERT_B */
  991. return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
  992. }