s23_clnt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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 "ssl_locl.h"
  60. #include <openssl/buffer.h>
  61. #include <openssl/rand.h>
  62. #include <openssl/objects.h>
  63. #include <openssl/evp.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 OPENSSL_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)(const SSL *ssl,int type,int val)=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. int ret;
  198. buf=(unsigned char *)s->init_buf->data;
  199. if (s->state == SSL23_ST_CW_CLNT_HELLO_A)
  200. {
  201. #if 0
  202. /* don't reuse session-id's */
  203. if (!ssl_get_new_session(s,0))
  204. {
  205. return(-1);
  206. }
  207. #endif
  208. p=s->s3->client_random;
  209. RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE);
  210. /* Do the message type and length last */
  211. d= &(buf[2]);
  212. p=d+9;
  213. *(d++)=SSL2_MT_CLIENT_HELLO;
  214. if (!(s->options & SSL_OP_NO_TLSv1))
  215. {
  216. *(d++)=TLS1_VERSION_MAJOR;
  217. *(d++)=TLS1_VERSION_MINOR;
  218. s->client_version=TLS1_VERSION;
  219. }
  220. else if (!(s->options & SSL_OP_NO_SSLv3))
  221. {
  222. *(d++)=SSL3_VERSION_MAJOR;
  223. *(d++)=SSL3_VERSION_MINOR;
  224. s->client_version=SSL3_VERSION;
  225. }
  226. else if (!(s->options & SSL_OP_NO_SSLv2))
  227. {
  228. *(d++)=SSL2_VERSION_MAJOR;
  229. *(d++)=SSL2_VERSION_MINOR;
  230. s->client_version=SSL2_VERSION;
  231. }
  232. else
  233. {
  234. SSLerr(SSL_F_SSL23_CLIENT_HELLO,SSL_R_NO_PROTOCOLS_AVAILABLE);
  235. return(-1);
  236. }
  237. /* Ciphers supported */
  238. i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),p);
  239. if (i == 0)
  240. {
  241. /* no ciphers */
  242. SSLerr(SSL_F_SSL23_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
  243. return(-1);
  244. }
  245. s2n(i,d);
  246. p+=i;
  247. /* put in the session-id, zero since there is no
  248. * reuse. */
  249. #if 0
  250. s->session->session_id_length=0;
  251. #endif
  252. s2n(0,d);
  253. if (s->options & SSL_OP_NETSCAPE_CHALLENGE_BUG)
  254. ch_len=SSL2_CHALLENGE_LENGTH;
  255. else
  256. ch_len=SSL2_MAX_CHALLENGE_LENGTH;
  257. /* write out sslv2 challenge */
  258. if (SSL3_RANDOM_SIZE < ch_len)
  259. i=SSL3_RANDOM_SIZE;
  260. else
  261. i=ch_len;
  262. s2n(i,d);
  263. memset(&(s->s3->client_random[0]),0,SSL3_RANDOM_SIZE);
  264. RAND_pseudo_bytes(&(s->s3->client_random[SSL3_RANDOM_SIZE-i]),i);
  265. memcpy(p,&(s->s3->client_random[SSL3_RANDOM_SIZE-i]),i);
  266. p+=i;
  267. i= p- &(buf[2]);
  268. buf[0]=((i>>8)&0xff)|0x80;
  269. buf[1]=(i&0xff);
  270. s->state=SSL23_ST_CW_CLNT_HELLO_B;
  271. /* number of bytes to write */
  272. s->init_num=i+2;
  273. s->init_off=0;
  274. ssl3_finish_mac(s,&(buf[2]),i);
  275. }
  276. /* SSL3_ST_CW_CLNT_HELLO_B */
  277. ret = ssl23_write_bytes(s);
  278. if (ret >= 2)
  279. if (s->msg_callback)
  280. s->msg_callback(1, SSL2_VERSION, 0, s->init_buf->data+2, ret-2, s, s->msg_callback_arg); /* CLIENT-HELLO */
  281. return ret;
  282. }
  283. static int ssl23_get_server_hello(SSL *s)
  284. {
  285. char buf[8];
  286. unsigned char *p;
  287. int i;
  288. int n;
  289. n=ssl23_read_bytes(s,7);
  290. if (n != 7) return(n);
  291. p=s->packet;
  292. memcpy(buf,p,n);
  293. if ((p[0] & 0x80) && (p[2] == SSL2_MT_SERVER_HELLO) &&
  294. (p[5] == 0x00) && (p[6] == 0x02))
  295. {
  296. #ifdef OPENSSL_NO_SSL2
  297. SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_PROTOCOL);
  298. goto err;
  299. #else
  300. /* we are talking sslv2 */
  301. /* we need to clean up the SSLv3 setup and put in the
  302. * sslv2 stuff. */
  303. int ch_len;
  304. if (s->options & SSL_OP_NO_SSLv2)
  305. {
  306. SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_PROTOCOL);
  307. goto err;
  308. }
  309. if (s->s2 == NULL)
  310. {
  311. if (!ssl2_new(s))
  312. goto err;
  313. }
  314. else
  315. ssl2_clear(s);
  316. if (s->options & SSL_OP_NETSCAPE_CHALLENGE_BUG)
  317. ch_len=SSL2_CHALLENGE_LENGTH;
  318. else
  319. ch_len=SSL2_MAX_CHALLENGE_LENGTH;
  320. /* write out sslv2 challenge */
  321. i=(SSL3_RANDOM_SIZE < ch_len)
  322. ?SSL3_RANDOM_SIZE:ch_len;
  323. s->s2->challenge_length=i;
  324. memcpy(s->s2->challenge,
  325. &(s->s3->client_random[SSL3_RANDOM_SIZE-i]),i);
  326. if (s->s3 != NULL) ssl3_free(s);
  327. if (!BUF_MEM_grow_clean(s->init_buf,
  328. SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER))
  329. {
  330. SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,ERR_R_BUF_LIB);
  331. goto err;
  332. }
  333. s->state=SSL2_ST_GET_SERVER_HELLO_A;
  334. if (!(s->client_version == SSL2_VERSION))
  335. /* use special padding (SSL 3.0 draft/RFC 2246, App. E.2) */
  336. s->s2->ssl2_rollback=1;
  337. /* setup the 5 bytes we have read so we get them from
  338. * the sslv2 buffer */
  339. s->rstate=SSL_ST_READ_HEADER;
  340. s->packet_length=n;
  341. s->packet= &(s->s2->rbuf[0]);
  342. memcpy(s->packet,buf,n);
  343. s->s2->rbuf_left=n;
  344. s->s2->rbuf_offs=0;
  345. /* we have already written one */
  346. s->s2->write_sequence=1;
  347. s->method=SSLv2_client_method();
  348. s->handshake_func=s->method->ssl_connect;
  349. #endif
  350. }
  351. else if ((p[0] == SSL3_RT_HANDSHAKE) &&
  352. (p[1] == SSL3_VERSION_MAJOR) &&
  353. ((p[2] == SSL3_VERSION_MINOR) ||
  354. (p[2] == TLS1_VERSION_MINOR)) &&
  355. (p[5] == SSL3_MT_SERVER_HELLO))
  356. {
  357. /* we have sslv3 or tls1 */
  358. if (!ssl_init_wbio_buffer(s,1)) goto err;
  359. /* we are in this state */
  360. s->state=SSL3_ST_CR_SRVR_HELLO_A;
  361. /* put the 5 bytes we have read into the input buffer
  362. * for SSLv3 */
  363. s->rstate=SSL_ST_READ_HEADER;
  364. s->packet_length=n;
  365. s->packet= &(s->s3->rbuf.buf[0]);
  366. memcpy(s->packet,buf,n);
  367. s->s3->rbuf.left=n;
  368. s->s3->rbuf.offset=0;
  369. if ((p[2] == SSL3_VERSION_MINOR) &&
  370. !(s->options & SSL_OP_NO_SSLv3))
  371. {
  372. s->version=SSL3_VERSION;
  373. s->method=SSLv3_client_method();
  374. }
  375. else if ((p[2] == TLS1_VERSION_MINOR) &&
  376. !(s->options & SSL_OP_NO_TLSv1))
  377. {
  378. s->version=TLS1_VERSION;
  379. s->method=TLSv1_client_method();
  380. }
  381. else
  382. {
  383. SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_PROTOCOL);
  384. goto err;
  385. }
  386. s->handshake_func=s->method->ssl_connect;
  387. }
  388. else if ((p[0] == SSL3_RT_ALERT) &&
  389. (p[1] == SSL3_VERSION_MAJOR) &&
  390. ((p[2] == SSL3_VERSION_MINOR) ||
  391. (p[2] == TLS1_VERSION_MINOR)) &&
  392. (p[3] == 0) &&
  393. (p[4] == 2))
  394. {
  395. void (*cb)(const SSL *ssl,int type,int val)=NULL;
  396. int j;
  397. /* An alert */
  398. if (s->info_callback != NULL)
  399. cb=s->info_callback;
  400. else if (s->ctx->info_callback != NULL)
  401. cb=s->ctx->info_callback;
  402. i=p[5];
  403. if (cb != NULL)
  404. {
  405. j=(i<<8)|p[6];
  406. cb(s,SSL_CB_READ_ALERT,j);
  407. }
  408. s->rwstate=SSL_NOTHING;
  409. SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_AD_REASON_OFFSET+p[6]);
  410. goto err;
  411. }
  412. else
  413. {
  414. SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_R_UNKNOWN_PROTOCOL);
  415. goto err;
  416. }
  417. s->init_num=0;
  418. /* Since, if we are sending a ssl23 client hello, we are not
  419. * reusing a session-id */
  420. if (!ssl_get_new_session(s,0))
  421. goto err;
  422. s->first_packet=1;
  423. return(SSL_connect(s));
  424. err:
  425. return(-1);
  426. }