d1_srvr.c 29 KB

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