您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

594 行
17 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com). */
  108. #include <stdio.h>
  109. #include <openssl/buf.h>
  110. #include <openssl/err.h>
  111. #include <openssl/evp.h>
  112. #include <openssl/mem.h>
  113. #include <openssl/obj.h>
  114. #include <openssl/rand.h>
  115. #include "ssl_locl.h"
  116. static const SSL_METHOD *ssl23_get_server_method(int ver);
  117. int ssl23_get_client_hello(SSL *s);
  118. static const SSL_METHOD *ssl23_get_server_method(int ver)
  119. {
  120. if (ver == SSL3_VERSION)
  121. return(SSLv3_server_method());
  122. else if (ver == TLS1_VERSION)
  123. return(TLSv1_server_method());
  124. else if (ver == TLS1_1_VERSION)
  125. return(TLSv1_1_server_method());
  126. else if (ver == TLS1_2_VERSION)
  127. return(TLSv1_2_server_method());
  128. else
  129. return(NULL);
  130. }
  131. IMPLEMENT_ssl23_meth_func(SSLv23_server_method,
  132. ssl23_accept,
  133. ssl_undefined_function,
  134. ssl23_get_server_method)
  135. int ssl23_accept(SSL *s)
  136. {
  137. BUF_MEM *buf;
  138. void (*cb)(const SSL *ssl,int type,int val)=NULL;
  139. int ret= -1;
  140. int new_state,state;
  141. ERR_clear_error();
  142. ERR_clear_system_error();
  143. if (s->info_callback != NULL)
  144. cb=s->info_callback;
  145. else if (s->ctx->info_callback != NULL)
  146. cb=s->ctx->info_callback;
  147. s->in_handshake++;
  148. if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
  149. for (;;)
  150. {
  151. state=s->state;
  152. switch(s->state)
  153. {
  154. case SSL_ST_BEFORE:
  155. case SSL_ST_ACCEPT:
  156. case SSL_ST_BEFORE|SSL_ST_ACCEPT:
  157. case SSL_ST_OK|SSL_ST_ACCEPT:
  158. s->server=1;
  159. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
  160. /* s->version=SSL3_VERSION; */
  161. s->type=SSL_ST_ACCEPT;
  162. if (s->init_buf == NULL)
  163. {
  164. if ((buf=BUF_MEM_new()) == NULL)
  165. {
  166. ret= -1;
  167. goto end;
  168. }
  169. if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
  170. {
  171. ret= -1;
  172. goto end;
  173. }
  174. s->init_buf=buf;
  175. }
  176. ssl3_init_finished_mac(s);
  177. s->state=SSL23_ST_SR_CLNT_HELLO_A;
  178. s->ctx->stats.sess_accept++;
  179. s->init_num=0;
  180. break;
  181. case SSL23_ST_SR_CLNT_HELLO_A:
  182. case SSL23_ST_SR_CLNT_HELLO_B:
  183. s->shutdown=0;
  184. ret=ssl23_get_client_hello(s);
  185. if (ret >= 0) cb=NULL;
  186. goto end;
  187. /* break; */
  188. default:
  189. OPENSSL_PUT_ERROR(SSL, ssl23_accept, SSL_R_UNKNOWN_STATE);
  190. ret= -1;
  191. goto end;
  192. /* break; */
  193. }
  194. if ((cb != NULL) && (s->state != state))
  195. {
  196. new_state=s->state;
  197. s->state=state;
  198. cb(s,SSL_CB_ACCEPT_LOOP,1);
  199. s->state=new_state;
  200. }
  201. }
  202. end:
  203. s->in_handshake--;
  204. if (cb != NULL)
  205. cb(s,SSL_CB_ACCEPT_EXIT,ret);
  206. return(ret);
  207. }
  208. int ssl23_get_client_hello(SSL *s)
  209. {
  210. char buf_space[11]; /* Request this many bytes in initial read.
  211. * We can detect SSL 3.0/TLS 1.0 Client Hellos
  212. * ('type == 3') correctly only when the following
  213. * is in a single record, which is not guaranteed by
  214. * the protocol specification:
  215. * Byte Content
  216. * 0 type \
  217. * 1/2 version > record header
  218. * 3/4 length /
  219. * 5 msg_type \
  220. * 6-8 length > Client Hello message
  221. * 9/10 client_version /
  222. */
  223. char *buf= &(buf_space[0]);
  224. unsigned char *p,*d,*d_len,*dd;
  225. unsigned int i;
  226. unsigned int csl,sil,cl;
  227. int n=0,j;
  228. int type=0;
  229. int v[2];
  230. if (s->state == SSL23_ST_SR_CLNT_HELLO_A)
  231. {
  232. /* read the initial header */
  233. v[0]=v[1]=0;
  234. if (!ssl3_setup_buffers(s)) goto err;
  235. n=ssl23_read_bytes(s, sizeof buf_space);
  236. if (n != sizeof buf_space) return(n); /* n == -1 || n == 0 */
  237. p=s->packet;
  238. memcpy(buf,p,n);
  239. if ((p[0] & 0x80) && (p[2] == SSL2_MT_CLIENT_HELLO))
  240. {
  241. /*
  242. * SSLv2 header
  243. */
  244. if ((p[3] == 0x00) && (p[4] == 0x02))
  245. {
  246. v[0]=p[3]; v[1]=p[4];
  247. /* SSLv2 */
  248. if (!(s->options & SSL_OP_NO_SSLv2))
  249. type=1;
  250. }
  251. else if (p[3] == SSL3_VERSION_MAJOR)
  252. {
  253. v[0]=p[3]; v[1]=p[4];
  254. /* SSLv3/TLSv1 */
  255. if (p[4] >= TLS1_VERSION_MINOR)
  256. {
  257. if (p[4] >= TLS1_2_VERSION_MINOR &&
  258. !(s->options & SSL_OP_NO_TLSv1_2))
  259. {
  260. s->version=TLS1_2_VERSION;
  261. s->state=SSL23_ST_SR_CLNT_HELLO_B;
  262. }
  263. else if (p[4] >= TLS1_1_VERSION_MINOR &&
  264. !(s->options & SSL_OP_NO_TLSv1_1))
  265. {
  266. s->version=TLS1_1_VERSION;
  267. /* type=2; */ /* done later to survive restarts */
  268. s->state=SSL23_ST_SR_CLNT_HELLO_B;
  269. }
  270. else if (!(s->options & SSL_OP_NO_TLSv1))
  271. {
  272. s->version=TLS1_VERSION;
  273. /* type=2; */ /* done later to survive restarts */
  274. s->state=SSL23_ST_SR_CLNT_HELLO_B;
  275. }
  276. else if (!(s->options & SSL_OP_NO_SSLv3))
  277. {
  278. s->version=SSL3_VERSION;
  279. /* type=2; */
  280. s->state=SSL23_ST_SR_CLNT_HELLO_B;
  281. }
  282. else if (!(s->options & SSL_OP_NO_SSLv2))
  283. {
  284. type=1;
  285. }
  286. }
  287. else if (!(s->options & SSL_OP_NO_SSLv3))
  288. {
  289. s->version=SSL3_VERSION;
  290. /* type=2; */
  291. s->state=SSL23_ST_SR_CLNT_HELLO_B;
  292. }
  293. else if (!(s->options & SSL_OP_NO_SSLv2))
  294. type=1;
  295. }
  296. }
  297. else if ((p[0] == SSL3_RT_HANDSHAKE) &&
  298. (p[1] == SSL3_VERSION_MAJOR) &&
  299. (p[5] == SSL3_MT_CLIENT_HELLO) &&
  300. ((p[3] == 0 && p[4] < 5 /* silly record length? */)
  301. || (p[9] >= p[1])))
  302. {
  303. /*
  304. * SSLv3 or tls1 header
  305. */
  306. v[0]=p[1]; /* major version (= SSL3_VERSION_MAJOR) */
  307. /* We must look at client_version inside the Client Hello message
  308. * to get the correct minor version.
  309. * However if we have only a pathologically small fragment of the
  310. * Client Hello message, this would be difficult, and we'd have
  311. * to read more records to find out.
  312. * No known SSL 3.0 client fragments ClientHello like this,
  313. * so we simply assume TLS 1.0 to avoid protocol version downgrade
  314. * attacks. */
  315. if (p[3] == 0 && p[4] < 6)
  316. {
  317. #if 0
  318. OPENSSL_PUT_ERROR(SSL, XXX, SSL_R_RECORD_TOO_SMALL);
  319. goto err;
  320. #else
  321. v[1] = TLS1_VERSION_MINOR;
  322. #endif
  323. }
  324. /* if major version number > 3 set minor to a value
  325. * which will use the highest version 3 we support.
  326. * If TLS 2.0 ever appears we will need to revise
  327. * this....
  328. */
  329. else if (p[9] > SSL3_VERSION_MAJOR)
  330. v[1]=0xff;
  331. else
  332. v[1]=p[10]; /* minor version according to client_version */
  333. if (v[1] >= TLS1_VERSION_MINOR)
  334. {
  335. if (v[1] >= TLS1_2_VERSION_MINOR &&
  336. !(s->options & SSL_OP_NO_TLSv1_2))
  337. {
  338. s->version=TLS1_2_VERSION;
  339. type=3;
  340. }
  341. else if (v[1] >= TLS1_1_VERSION_MINOR &&
  342. !(s->options & SSL_OP_NO_TLSv1_1))
  343. {
  344. s->version=TLS1_1_VERSION;
  345. type=3;
  346. }
  347. else if (!(s->options & SSL_OP_NO_TLSv1))
  348. {
  349. s->version=TLS1_VERSION;
  350. type=3;
  351. }
  352. else if (!(s->options & SSL_OP_NO_SSLv3))
  353. {
  354. s->version=SSL3_VERSION;
  355. type=3;
  356. }
  357. }
  358. else
  359. {
  360. /* client requests SSL 3.0 */
  361. if (!(s->options & SSL_OP_NO_SSLv3))
  362. {
  363. s->version=SSL3_VERSION;
  364. type=3;
  365. }
  366. else if (!(s->options & SSL_OP_NO_TLSv1))
  367. {
  368. /* we won't be able to use TLS of course,
  369. * but this will send an appropriate alert */
  370. s->version=TLS1_VERSION;
  371. type=3;
  372. }
  373. }
  374. }
  375. else if ((strncmp("GET ", (char *)p,4) == 0) ||
  376. (strncmp("POST ",(char *)p,5) == 0) ||
  377. (strncmp("HEAD ",(char *)p,5) == 0) ||
  378. (strncmp("PUT ", (char *)p,4) == 0))
  379. {
  380. OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_HTTP_REQUEST);
  381. goto err;
  382. }
  383. else if (strncmp("CONNECT",(char *)p,7) == 0)
  384. {
  385. OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_HTTPS_PROXY_REQUEST);
  386. goto err;
  387. }
  388. }
  389. if (s->version < TLS1_2_VERSION && tls1_suiteb(s))
  390. {
  391. OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_ONLY_TLS_1_2_ALLOWED_IN_SUITEB_MODE);
  392. goto err;
  393. }
  394. #ifdef OPENSSL_FIPS
  395. if (FIPS_mode() && (s->version < TLS1_VERSION))
  396. {
  397. OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
  398. goto err;
  399. }
  400. #endif
  401. if (s->state == SSL23_ST_SR_CLNT_HELLO_B)
  402. {
  403. /* we have SSLv3/TLSv1 in an SSLv2 header
  404. * (other cases skip this state) */
  405. type=2;
  406. p=s->packet;
  407. v[0] = p[3]; /* == SSL3_VERSION_MAJOR */
  408. v[1] = p[4];
  409. n=((p[0]&0x7f)<<8)|p[1];
  410. if (n > (1024*4))
  411. {
  412. OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_RECORD_TOO_LARGE);
  413. goto err;
  414. }
  415. j=ssl23_read_bytes(s,n+2);
  416. if (j <= 0) return(j);
  417. ssl3_finish_mac(s, s->packet+2, s->packet_length-2);
  418. if (s->msg_callback)
  419. s->msg_callback(0, SSL2_VERSION, 0, s->packet+2, s->packet_length-2, s, s->msg_callback_arg); /* CLIENT-HELLO */
  420. p=s->packet;
  421. p+=5;
  422. n2s(p,csl);
  423. n2s(p,sil);
  424. n2s(p,cl);
  425. d=(unsigned char *)s->init_buf->data;
  426. if ((csl+sil+cl+11) != s->packet_length) /* We can't have TLS extensions in SSL 2.0 format
  427. * Client Hello, can we? Error condition should be
  428. * '>' otherweise */
  429. {
  430. OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_RECORD_LENGTH_MISMATCH);
  431. goto err;
  432. }
  433. /* record header: msg_type ... */
  434. *(d++) = SSL3_MT_CLIENT_HELLO;
  435. /* ... and length (actual value will be written later) */
  436. d_len = d;
  437. d += 3;
  438. /* client_version */
  439. *(d++) = SSL3_VERSION_MAJOR; /* == v[0] */
  440. *(d++) = v[1];
  441. /* lets populate the random area */
  442. /* get the challenge_length */
  443. i=(cl > SSL3_RANDOM_SIZE)?SSL3_RANDOM_SIZE:cl;
  444. memset(d,0,SSL3_RANDOM_SIZE);
  445. memcpy(&(d[SSL3_RANDOM_SIZE-i]),&(p[csl+sil]),i);
  446. d+=SSL3_RANDOM_SIZE;
  447. /* no session-id reuse */
  448. *(d++)=0;
  449. /* ciphers */
  450. j=0;
  451. dd=d;
  452. d+=2;
  453. for (i=0; i<csl; i+=3)
  454. {
  455. if (p[i] != 0) continue;
  456. *(d++)=p[i+1];
  457. *(d++)=p[i+2];
  458. j+=2;
  459. }
  460. s2n(j,dd);
  461. /* COMPRESSION */
  462. *(d++)=1;
  463. *(d++)=0;
  464. #if 0
  465. /* copy any remaining data with may be extensions */
  466. p = p+csl+sil+cl;
  467. while (p < s->packet+s->packet_length)
  468. {
  469. *(d++)=*(p++);
  470. }
  471. #endif
  472. i = (d-(unsigned char *)s->init_buf->data) - 4;
  473. l2n3((long)i, d_len);
  474. /* get the data reused from the init_buf */
  475. s->s3->tmp.reuse_message=1;
  476. s->s3->tmp.message_type=SSL3_MT_CLIENT_HELLO;
  477. s->s3->tmp.message_size=i;
  478. }
  479. /* imaginary new state (for program structure): */
  480. /* s->state = SSL23_SR_CLNT_HELLO_C */
  481. if (type == 1)
  482. {
  483. OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_UNSUPPORTED_PROTOCOL);
  484. goto err;
  485. }
  486. if ((type == 2) || (type == 3))
  487. {
  488. /* we have SSLv3/TLSv1 (type 2: SSL2 style, type 3: SSL3/TLS style) */
  489. if (!ssl_init_wbio_buffer(s,1)) goto err;
  490. /* we are in this state */
  491. s->state=SSL3_ST_SR_CLNT_HELLO_A;
  492. if (type == 3)
  493. {
  494. /* put the 'n' bytes we have read into the input buffer
  495. * for SSLv3 */
  496. s->rstate=SSL_ST_READ_HEADER;
  497. s->packet_length=n;
  498. if (s->s3->rbuf.buf == NULL)
  499. if (!ssl3_setup_read_buffer(s))
  500. goto err;
  501. s->packet= &(s->s3->rbuf.buf[0]);
  502. memcpy(s->packet,buf,n);
  503. s->s3->rbuf.left=n;
  504. s->s3->rbuf.offset=0;
  505. }
  506. else
  507. {
  508. s->packet_length=0;
  509. s->s3->rbuf.left=0;
  510. s->s3->rbuf.offset=0;
  511. }
  512. if (s->version == TLS1_2_VERSION)
  513. s->method = TLSv1_2_server_method();
  514. else if (s->version == TLS1_1_VERSION)
  515. s->method = TLSv1_1_server_method();
  516. else if (s->version == TLS1_VERSION)
  517. s->method = TLSv1_server_method();
  518. else
  519. s->method = SSLv3_server_method();
  520. #if 0 /* ssl3_get_client_hello does this */
  521. s->client_version=(v[0]<<8)|v[1];
  522. #endif
  523. s->handshake_func=s->method->ssl_accept;
  524. }
  525. if ((type < 1) || (type > 3))
  526. {
  527. /* bad, very bad */
  528. OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_UNKNOWN_PROTOCOL);
  529. goto err;
  530. }
  531. s->init_num=0;
  532. if (buf != buf_space) OPENSSL_free(buf);
  533. return(SSL_accept(s));
  534. err:
  535. if (buf != buf_space) OPENSSL_free(buf);
  536. return(-1);
  537. }