zz 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /* ssl/s23_srvr.c */
  2. /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #include <stdio.h>
  59. #include "buffer.h"
  60. #include "rand.h"
  61. #include "objects.h"
  62. #include "evp.h"
  63. #include "ssl_locl.h"
  64. #define BREAK break
  65. #ifndef NOPROTO
  66. int ssl23_get_client_hello(SSL *s);
  67. #else
  68. int ssl23_get_client_hello();
  69. #endif
  70. static SSL_METHOD *ssl23_get_server_method(ver)
  71. int ver;
  72. {
  73. if (ver == 2)
  74. return(SSLv2_server_method());
  75. else if (ver == 3)
  76. return(SSLv3_server_method());
  77. else
  78. return(NULL);
  79. }
  80. SSL_METHOD *SSLv23_server_method()
  81. {
  82. static int init=1;
  83. static SSL_METHOD SSLv23_server_data;
  84. if (init)
  85. {
  86. init=0;
  87. memcpy((char *)&SSLv23_server_data,
  88. (char *)sslv23_base_method(),sizeof(SSL_METHOD));
  89. SSLv23_server_data.ssl_accept=ssl23_accept;
  90. SSLv23_server_data.get_ssl_method=ssl23_get_server_method;
  91. }
  92. return(&SSLv23_server_data);
  93. }
  94. int ssl23_accept(s)
  95. SSL *s;
  96. {
  97. BUF_MEM *buf;
  98. unsigned long Time=time(NULL);
  99. void (*cb)()=NULL;
  100. int ret= -1;
  101. int new_state,state;
  102. RAND_seed((unsigned char *)&Time,sizeof(Time));
  103. ERR_clear_error();
  104. errno=0;
  105. if (s->info_callback != NULL)
  106. cb=s->info_callback;
  107. else if (s->ctx->info_callback != NULL)
  108. cb=s->ctx->info_callback;
  109. if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
  110. s->in_handshake++;
  111. for (;;)
  112. {
  113. state=s->state;
  114. switch(s->state)
  115. {
  116. case SSL_ST_BEFORE:
  117. case SSL_ST_ACCEPT:
  118. case SSL_ST_BEFORE|SSL_ST_ACCEPT:
  119. case SSL_ST_OK|SSL_ST_ACCEPT:
  120. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
  121. s->version=3;
  122. s->type=SSL_ST_ACCEPT;
  123. if (s->init_buf == NULL)
  124. {
  125. if ((buf=BUF_MEM_new()) == NULL)
  126. {
  127. ret= -1;
  128. goto end;
  129. }
  130. if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
  131. {
  132. ret= -1;
  133. goto end;
  134. }
  135. s->init_buf=buf;
  136. }
  137. ssl3_init_finished_mac(s);
  138. s->state=SSL23_ST_SR_CLNT_HELLO_A;
  139. s->ctx->sess_accept++;
  140. s->init_num=0;
  141. break;
  142. case SSL23_ST_SR_CLNT_HELLO_A:
  143. case SSL23_ST_SR_CLNT_HELLO_B:
  144. s->shutdown=0;
  145. ret=ssl23_get_client_hello(s);
  146. if (ret >= 0) cb=NULL;
  147. goto end;
  148. break;
  149. default:
  150. SSLerr(SSL_F_SSL23_ACCEPT,SSL_R_UNKNOWN_STATE);
  151. ret= -1;
  152. goto end;
  153. /* break; */
  154. }
  155. if ((cb != NULL) && (s->state != state))
  156. {
  157. new_state=s->state;
  158. s->state=state;
  159. cb(s,SSL_CB_ACCEPT_LOOP,1);
  160. s->state=new_state;
  161. }
  162. }
  163. end:
  164. if (cb != NULL)
  165. cb(s,SSL_CB_ACCEPT_EXIT,ret);
  166. s->in_handshake--;
  167. return(ret);
  168. }
  169. int ssl23_get_client_hello(s)
  170. SSL *s;
  171. {
  172. char buf[8];
  173. unsigned char *p,*d,*dd;
  174. unsigned int i;
  175. unsigned int csl,sil,cl;
  176. int n=0,j;
  177. BIO *bbio;
  178. int type=0;
  179. /* read the initial header */
  180. if (s->state == SSL23_ST_SR_CLNT_HELLO_A)
  181. {
  182. if (!ssl3_setup_buffers(s)) goto err;
  183. n=ssl23_read_bytes(s,7);
  184. if (n != 7) return(n);
  185. p=s->packet;
  186. memcpy(buf,p,n);
  187. if ((p[0] & 0x80) && (p[2] == SSL2_MT_CLIENT_HELLO))
  188. {
  189. /* SSLv2 header */
  190. if ((p[3] == 0x00) && (p[4] == 0x02))
  191. {
  192. /* SSLv2 */
  193. type=1;
  194. }
  195. else if ((p[3] == SSL3_VERSION_MAJOR) &&
  196. (p[4] == SSL3_VERSION_MINOR))
  197. {
  198. /* SSLv3 */
  199. s->state=SSL23_ST_SR_CLNT_HELLO_B;
  200. }
  201. }
  202. else if ((p[0] == SSL3_RT_HANDSHAKE) &&
  203. (p[1] == SSL3_VERSION_MAJOR) &&
  204. (p[2] == SSL3_VERSION_MINOR) &&
  205. (p[5] == SSL3_MT_CLIENT_HELLO))
  206. {
  207. /* true SSLv3 */
  208. type=3;
  209. }
  210. }
  211. if (s->state == SSL23_ST_SR_CLNT_HELLO_B)
  212. {
  213. /* we have a SSLv3 in a SSLv2 header */
  214. type=2;
  215. p=s->packet;
  216. n=((p[0]&0x7f)<<8)|p[1];
  217. if (n > (1024*4))
  218. {
  219. SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_TOO_LARGE);
  220. goto err;
  221. }
  222. j=ssl23_read_bytes(s,n+2);
  223. if (j <= 0) return(j);
  224. ssl3_finish_mac(s,&(s->packet[2]),s->packet_length-2);
  225. p=s->packet;
  226. p+=5;
  227. n2s(p,csl);
  228. n2s(p,sil);
  229. n2s(p,cl);
  230. d=(unsigned char *)s->init_buf->data;
  231. if ((csl+sil+cl+11) != s->packet_length)
  232. {
  233. SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_LENGTH_MISMATCH);
  234. goto err;
  235. }
  236. *(d++)=SSL3_VERSION_MAJOR;
  237. *(d++)=SSL3_VERSION_MINOR;
  238. /* lets populate the random area */
  239. /* get the chalenge_length */
  240. i=(cl > SSL3_RANDOM_SIZE)?SSL3_RANDOM_SIZE:cl;
  241. memset(d,0,SSL3_RANDOM_SIZE);
  242. memcpy(&(d[SSL3_RANDOM_SIZE-i]),&(p[csl+sil]),i);
  243. d+=SSL3_RANDOM_SIZE;
  244. /* no session-id reuse */
  245. *(d++)=0;
  246. /* ciphers */
  247. j=0;
  248. dd=d;
  249. d+=2;
  250. for (i=0; i<csl; i+=3)
  251. {
  252. if (p[i] != 0) continue;
  253. *(d++)=p[i+1];
  254. *(d++)=p[i+2];
  255. j+=2;
  256. }
  257. s2n(j,dd);
  258. /* compression */
  259. *(d++)=1;
  260. *(d++)=0;
  261. i=(d-(unsigned char *)s->init_buf->data);
  262. /* get the data reused from the init_buf */
  263. s->s3->tmp.reuse_message=1;
  264. s->s3->tmp.message_type=SSL3_MT_CLIENT_HELLO;
  265. s->s3->tmp.message_size=i;
  266. }
  267. if (type == 1)
  268. {
  269. /* we are talking sslv2 */
  270. /* we need to clean up the SSLv3 setup and put in the
  271. * sslv2 stuff. */
  272. if (s->s2 == NULL)
  273. {
  274. if (!ssl2_new(s))
  275. goto err;
  276. }
  277. else
  278. ssl2_clear(s);
  279. if (s->s3 != NULL) ssl3_free(s);
  280. if (!BUF_MEM_grow(s->init_buf,
  281. SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER))
  282. {
  283. goto err;
  284. }
  285. s->state=SSL2_ST_GET_CLIENT_HELLO_A;
  286. if (s->ctx->options & SSL_OP_MSIE_SSLV2_RSA_PADDING)
  287. s->s2->ssl2_rollback=0;
  288. else
  289. s->s2->ssl2_rollback=1;
  290. /* setup the 5 bytes we have read so we get them from
  291. * the sslv2 buffer */
  292. s->rstate=SSL_ST_READ_HEADER;
  293. s->packet_length=n;
  294. s->packet= &(s->s2->rbuf[0]);
  295. memcpy(s->packet,buf,n);
  296. s->s2->rbuf_left=n;
  297. s->s2->rbuf_offs=0;
  298. s->method=SSLv2_server_method();
  299. s->handshake_func=s->method->ssl_accept;
  300. }
  301. if ((type == 2) || (type == 3))
  302. {
  303. /* we have sslv3 */
  304. if (s->bbio == NULL)
  305. {
  306. bbio=BIO_new(BIO_f_buffer());
  307. if (bbio == NULL)
  308. goto err;
  309. s->bbio=bbio;
  310. }
  311. else
  312. bbio=s->bbio;
  313. BIO_reset(bbio);
  314. if (!BIO_set_write_buffer_size(bbio,16*1024))
  315. goto err;
  316. s->wbio=BIO_push(bbio,s->wbio);
  317. /* we are in this state */
  318. s->state=SSL3_ST_SR_CLNT_HELLO_A;
  319. if (type == 3)
  320. {
  321. /* put the 'n' bytes we have read into the input buffer
  322. * for SSLv3 */
  323. s->rstate=SSL_ST_READ_HEADER;
  324. s->packet_length=n;
  325. s->packet= &(s->s3->rbuf.buf[0]);
  326. memcpy(s->packet,buf,n);
  327. s->s3->rbuf.left=n;
  328. s->s3->rbuf.offset=0;
  329. }
  330. else
  331. {
  332. s->packet_length=0;
  333. s->s3->rbuf.left=0;
  334. s->s3->rbuf.offset=0;
  335. }
  336. s->method=SSLv3_server_method();
  337. s->handshake_func=s->method->ssl_accept;
  338. }
  339. if ((type < 1) || (type > 3))
  340. {
  341. /* bad, very bad */
  342. SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_UNKNOWN_PROTOCOL);
  343. goto err;
  344. }
  345. s->init_num=0;
  346. return(SSL_accept(s));
  347. err:
  348. return(-1);
  349. }