s23_clnt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /* ssl/s23_clnt.c */
  2. /* Copyright (C) 1995-1998 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 <openssl/buffer.h>
  60. #include <openssl/rand.h>
  61. #include <openssl/objects.h>
  62. #include <openssl/evp.h>
  63. #include "ssl_locl.h"
  64. static SSL_METHOD *ssl23_get_client_method(int ver);
  65. static int ssl23_client_hello(SSL *s);
  66. static int ssl23_get_server_hello(SSL *s);
  67. static SSL_METHOD *ssl23_get_client_method(int ver)
  68. {
  69. #ifndef NO_SSL2
  70. if (ver == SSL2_VERSION)
  71. return(SSLv2_client_method());
  72. #endif
  73. if (ver == SSL3_VERSION)
  74. return(SSLv3_client_method());
  75. else if (ver == TLS1_VERSION)
  76. return(TLSv1_client_method());
  77. else
  78. return(NULL);
  79. }
  80. SSL_METHOD *SSLv23_client_method(void)
  81. {
  82. static int init=1;
  83. static SSL_METHOD SSLv23_client_data;
  84. if (init)
  85. {
  86. CRYPTO_w_lock(CRYPTO_LOCK_SSL_METHOD);
  87. if (init)
  88. {
  89. memcpy((char *)&SSLv23_client_data,
  90. (char *)sslv23_base_method(),sizeof(SSL_METHOD));
  91. SSLv23_client_data.ssl_connect=ssl23_connect;
  92. SSLv23_client_data.get_ssl_method=ssl23_get_client_method;
  93. init=0;
  94. }
  95. CRYPTO_w_unlock(CRYPTO_LOCK_SSL_METHOD);
  96. }
  97. return(&SSLv23_client_data);
  98. }
  99. int ssl23_connect(SSL *s)
  100. {
  101. BUF_MEM *buf=NULL;
  102. unsigned long Time=time(NULL);
  103. void (*cb)()=NULL;
  104. int ret= -1;
  105. int new_state,state;
  106. RAND_add(&Time,sizeof(Time),0);
  107. ERR_clear_error();
  108. clear_sys_error();
  109. if (s->info_callback != NULL)
  110. cb=s->info_callback;
  111. else if (s->ctx->info_callback != NULL)
  112. cb=s->ctx->info_callback;
  113. s->in_handshake++;
  114. if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
  115. for (;;)
  116. {
  117. state=s->state;
  118. switch(s->state)
  119. {
  120. case SSL_ST_BEFORE:
  121. case SSL_ST_CONNECT:
  122. case SSL_ST_BEFORE|SSL_ST_CONNECT:
  123. case SSL_ST_OK|SSL_ST_CONNECT:
  124. if (s->session != NULL)
  125. {
  126. SSLerr(SSL_F_SSL23_CONNECT,SSL_R_SSL23_DOING_SESSION_ID_REUSE);
  127. ret= -1;
  128. goto end;
  129. }
  130. s->server=0;
  131. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
  132. /* s->version=TLS1_VERSION; */
  133. s->type=SSL_ST_CONNECT;
  134. if (s->init_buf == NULL)
  135. {
  136. if ((buf=BUF_MEM_new()) == NULL)
  137. {
  138. ret= -1;
  139. goto end;
  140. }
  141. if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
  142. {
  143. ret= -1;
  144. goto end;
  145. }
  146. s->init_buf=buf;
  147. buf=NULL;
  148. }
  149. if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
  150. ssl3_init_finished_mac(s);
  151. s->state=SSL23_ST_CW_CLNT_HELLO_A;
  152. s->ctx->stats.sess_connect++;
  153. s->init_num=0;
  154. break;
  155. case SSL23_ST_CW_CLNT_HELLO_A:
  156. case SSL23_ST_CW_CLNT_HELLO_B:
  157. s->shutdown=0;
  158. ret=ssl23_client_hello(s);
  159. if (ret <= 0) goto end;
  160. s->state=SSL23_ST_CR_SRVR_HELLO_A;
  161. s->init_num=0;
  162. break;
  163. case SSL23_ST_CR_SRVR_HELLO_A:
  164. case SSL23_ST_CR_SRVR_HELLO_B:
  165. ret=ssl23_get_server_hello(s);
  166. if (ret >= 0) cb=NULL;
  167. goto end;
  168. /* break; */
  169. default:
  170. SSLerr(SSL_F_SSL23_CONNECT,SSL_R_UNKNOWN_STATE);
  171. ret= -1;
  172. goto end;
  173. /* break; */
  174. }
  175. if (s->debug) { (void)BIO_flush(s->wbio); }
  176. if ((cb != NULL) && (s->state != state))
  177. {
  178. new_state=s->state;
  179. s->state=state;
  180. cb(s,SSL_CB_CONNECT_LOOP,1);
  181. s->state=new_state;
  182. }
  183. }
  184. end:
  185. s->in_handshake--;
  186. if (buf != NULL)
  187. BUF_MEM_free(buf);
  188. if (cb != NULL)
  189. cb(s,SSL_CB_CONNECT_EXIT,ret);
  190. return(ret);
  191. }
  192. static int ssl23_client_hello(SSL *s)
  193. {
  194. unsigned char *buf;
  195. unsigned char *p,*d;
  196. int i,ch_len;
  197. buf=(unsigned char *)s->init_buf->data;
  198. if (s->state == SSL23_ST_CW_CLNT_HELLO_A)
  199. {
  200. #if 0
  201. /* don't reuse session-id's */
  202. if (!ssl_get_new_session(s,0))
  203. {
  204. return(-1);
  205. }
  206. #endif
  207. p=s->s3->client_random;
  208. RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE);
  209. /* Do the message type and length last */
  210. d= &(buf[2]);
  211. p=d+9;
  212. *(d++)=SSL2_MT_CLIENT_HELLO;
  213. if (!(s->options & SSL_OP_NO_TLSv1))
  214. {
  215. *(d++)=TLS1_VERSION_MAJOR;
  216. *(d++)=TLS1_VERSION_MINOR;
  217. s->client_version=TLS1_VERSION;
  218. }
  219. else if (!(s->options & SSL_OP_NO_SSLv3))
  220. {
  221. *(d++)=SSL3_VERSION_MAJOR;
  222. *(d++)=SSL3_VERSION_MINOR;
  223. s->client_version=SSL3_VERSION;
  224. }
  225. else if (!(s->options & SSL_OP_NO_SSLv2))
  226. {
  227. *(d++)=SSL2_VERSION_MAJOR;
  228. *(d++)=SSL2_VERSION_MINOR;
  229. s->client_version=SSL2_VERSION;
  230. }
  231. else
  232. {
  233. SSLerr(SSL_F_SSL23_CLIENT_HELLO,SSL_R_NO_PROTOCOLS_AVAILABLE);
  234. return(-1);
  235. }
  236. /* Ciphers supported */
  237. i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),p);
  238. if (i == 0)
  239. {
  240. /* no ciphers */
  241. SSLerr(SSL_F_SSL23_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
  242. return(-1);
  243. }
  244. s2n(i,d);
  245. p+=i;
  246. /* put in the session-id, zero since there is no
  247. * reuse. */
  248. #if 0
  249. s->session->session_id_length=0;
  250. #endif
  251. s2n(0,d);
  252. if (s->options & SSL_OP_NETSCAPE_CHALLENGE_BUG)
  253. ch_len=SSL2_CHALLENGE_LENGTH;
  254. else
  255. ch_len=SSL2_MAX_CHALLENGE_LENGTH;
  256. /* write out sslv2 challenge */
  257. if (SSL3_RANDOM_SIZE < ch_len)
  258. i=SSL3_RANDOM_SIZE;
  259. else
  260. i=ch_len;
  261. s2n(i,d);
  262. memset(&(s->s3->client_random[0]),0,SSL3_RANDOM_SIZE);
  263. RAND_pseudo_bytes(&(s->s3->client_random[SSL3_RANDOM_SIZE-i]),i);
  264. memcpy(p,&(s->s3->client_random[SSL3_RANDOM_SIZE-i]),i);
  265. p+=i;
  266. i= p- &(buf[2]);
  267. buf[0]=((i>>8)&0xff)|0x80;
  268. buf[1]=(i&0xff);
  269. s->state=SSL23_ST_CW_CLNT_HELLO_B;
  270. /* number of bytes to write */
  271. s->init_num=i+2;
  272. s->init_off=0;
  273. ssl3_finish_mac(s,&(buf[2]),i);
  274. }
  275. /* SSL3_ST_CW_CLNT_HELLO_B */
  276. return(ssl23_write_bytes(s));
  277. }
  278. static int ssl23_get_server_hello(SSL *s)
  279. {
  280. char buf[8];
  281. unsigned char *p;
  282. int i;
  283. int n;
  284. n=ssl23_read_bytes(s,7);
  285. if (n != 7) return(n);
  286. p=s->packet;
  287. memcpy(buf,p,n);
  288. if ((p[0] & 0x80) && (p[2] == SSL2_MT_SERVER_HELLO) &&
  289. (p[5] == 0x00) && (p[6] == 0x02))
  290. {
  291. #ifdef NO_SSL2
  292. SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_PROTOCOL);
  293. goto err;
  294. #else
  295. /* we are talking sslv2 */
  296. /* we need to clean up the SSLv3 setup and put in the
  297. * sslv2 stuff. */
  298. int ch_len;
  299. if (s->options & SSL_OP_NO_SSLv2)
  300. {
  301. SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_PROTOCOL);
  302. goto err;
  303. }
  304. if (s->s2 == NULL)
  305. {
  306. if (!ssl2_new(s))
  307. goto err;
  308. }
  309. else
  310. ssl2_clear(s);
  311. if (s->options & SSL_OP_NETSCAPE_CHALLENGE_BUG)
  312. ch_len=SSL2_CHALLENGE_LENGTH;
  313. else
  314. ch_len=SSL2_MAX_CHALLENGE_LENGTH;
  315. /* write out sslv2 challenge */
  316. i=(SSL3_RANDOM_SIZE < ch_len)
  317. ?SSL3_RANDOM_SIZE:ch_len;
  318. s->s2->challenge_length=i;
  319. memcpy(s->s2->challenge,
  320. &(s->s3->client_random[SSL3_RANDOM_SIZE-i]),i);
  321. if (s->s3 != NULL) ssl3_free(s);
  322. if (!BUF_MEM_grow(s->init_buf,
  323. SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER))
  324. {
  325. SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,ERR_R_BUF_LIB);
  326. goto err;
  327. }
  328. s->state=SSL2_ST_GET_SERVER_HELLO_A;
  329. if (!(s->client_version == SSL2_VERSION))
  330. /* use special padding (SSL 3.0 draft/RFC 2246, App. E.2) */
  331. s->s2->ssl2_rollback=1;
  332. /* setup the 5 bytes we have read so we get them from
  333. * the sslv2 buffer */
  334. s->rstate=SSL_ST_READ_HEADER;
  335. s->packet_length=n;
  336. s->packet= &(s->s2->rbuf[0]);
  337. memcpy(s->packet,buf,n);
  338. s->s2->rbuf_left=n;
  339. s->s2->rbuf_offs=0;
  340. /* we have already written one */
  341. s->s2->write_sequence=1;
  342. s->method=SSLv2_client_method();
  343. s->handshake_func=s->method->ssl_connect;
  344. #endif
  345. }
  346. else if ((p[0] == SSL3_RT_HANDSHAKE) &&
  347. (p[1] == SSL3_VERSION_MAJOR) &&
  348. ((p[2] == SSL3_VERSION_MINOR) ||
  349. (p[2] == TLS1_VERSION_MINOR)) &&
  350. (p[5] == SSL3_MT_SERVER_HELLO))
  351. {
  352. /* we have sslv3 or tls1 */
  353. if (!ssl_init_wbio_buffer(s,1)) goto err;
  354. /* we are in this state */
  355. s->state=SSL3_ST_CR_SRVR_HELLO_A;
  356. /* put the 5 bytes we have read into the input buffer
  357. * for SSLv3 */
  358. s->rstate=SSL_ST_READ_HEADER;
  359. s->packet_length=n;
  360. s->packet= &(s->s3->rbuf.buf[0]);
  361. memcpy(s->packet,buf,n);
  362. s->s3->rbuf.left=n;
  363. s->s3->rbuf.offset=0;
  364. if ((p[2] == SSL3_VERSION_MINOR) &&
  365. !(s->options & SSL_OP_NO_SSLv3))
  366. {
  367. s->version=SSL3_VERSION;
  368. s->method=SSLv3_client_method();
  369. }
  370. else if ((p[2] == TLS1_VERSION_MINOR) &&
  371. !(s->options & SSL_OP_NO_TLSv1))
  372. {
  373. s->version=TLS1_VERSION;
  374. s->method=TLSv1_client_method();
  375. }
  376. else
  377. {
  378. SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_PROTOCOL);
  379. goto err;
  380. }
  381. s->handshake_func=s->method->ssl_connect;
  382. }
  383. else if ((p[0] == SSL3_RT_ALERT) &&
  384. (p[1] == SSL3_VERSION_MAJOR) &&
  385. ((p[2] == SSL3_VERSION_MINOR) ||
  386. (p[2] == TLS1_VERSION_MINOR)) &&
  387. (p[3] == 0) &&
  388. (p[4] == 2))
  389. {
  390. void (*cb)()=NULL;
  391. int j;
  392. /* An alert */
  393. if (s->info_callback != NULL)
  394. cb=s->info_callback;
  395. else if (s->ctx->info_callback != NULL)
  396. cb=s->ctx->info_callback;
  397. i=p[5];
  398. if (cb != NULL)
  399. {
  400. j=(i<<8)|p[6];
  401. cb(s,SSL_CB_READ_ALERT,j);
  402. }
  403. s->rwstate=SSL_NOTHING;
  404. SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_AD_REASON_OFFSET+p[6]);
  405. goto err;
  406. }
  407. else
  408. {
  409. SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_R_UNKNOWN_PROTOCOL);
  410. goto err;
  411. }
  412. s->init_num=0;
  413. /* Since, if we are sending a ssl23 client hello, we are not
  414. * reusing a session-id */
  415. if (!ssl_get_new_session(s,0))
  416. goto err;
  417. s->first_packet=1;
  418. return(SSL_connect(s));
  419. err:
  420. return(-1);
  421. }