s2_clnt.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  1. /* ssl/s2_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 "ssl_locl.h"
  59. #ifndef OPENSSL_NO_SSL2
  60. #include <stdio.h>
  61. #include <openssl/rand.h>
  62. #include <openssl/buffer.h>
  63. #include <openssl/objects.h>
  64. #include <openssl/evp.h>
  65. static SSL_METHOD *ssl2_get_client_method(int ver);
  66. static int get_server_finished(SSL *s);
  67. static int get_server_verify(SSL *s);
  68. static int get_server_hello(SSL *s);
  69. static int client_hello(SSL *s);
  70. static int client_master_key(SSL *s);
  71. static int client_finished(SSL *s);
  72. static int client_certificate(SSL *s);
  73. static int ssl_rsa_public_encrypt(SESS_CERT *sc, int len, unsigned char *from,
  74. unsigned char *to,int padding);
  75. #define BREAK break
  76. static SSL_METHOD *ssl2_get_client_method(int ver)
  77. {
  78. if (ver == SSL2_VERSION)
  79. return(SSLv2_client_method());
  80. else
  81. return(NULL);
  82. }
  83. SSL_METHOD *SSLv2_client_method(void)
  84. {
  85. static int init=1;
  86. static SSL_METHOD SSLv2_client_data;
  87. if (init)
  88. {
  89. memcpy((char *)&SSLv2_client_data,(char *)sslv2_base_method(),
  90. sizeof(SSL_METHOD));
  91. SSLv2_client_data.ssl_connect=ssl2_connect;
  92. SSLv2_client_data.get_ssl_method=ssl2_get_client_method;
  93. init=0;
  94. }
  95. return(&SSLv2_client_data);
  96. }
  97. int ssl2_connect(SSL *s)
  98. {
  99. unsigned long l=time(NULL);
  100. BUF_MEM *buf=NULL;
  101. int ret= -1;
  102. void (*cb)()=NULL;
  103. int new_state,state;
  104. RAND_add(&l,sizeof(l),0);
  105. ERR_clear_error();
  106. clear_sys_error();
  107. if (s->info_callback != NULL)
  108. cb=s->info_callback;
  109. else if (s->ctx->info_callback != NULL)
  110. cb=s->ctx->info_callback;
  111. /* init things to blank */
  112. if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
  113. s->in_handshake++;
  114. for (;;)
  115. {
  116. state=s->state;
  117. switch (s->state)
  118. {
  119. case SSL_ST_BEFORE:
  120. case SSL_ST_CONNECT:
  121. case SSL_ST_BEFORE|SSL_ST_CONNECT:
  122. case SSL_ST_OK|SSL_ST_CONNECT:
  123. s->server=0;
  124. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
  125. s->version=SSL2_VERSION;
  126. s->type=SSL_ST_CONNECT;
  127. buf=s->init_buf;
  128. if ((buf == NULL) && ((buf=BUF_MEM_new()) == NULL))
  129. {
  130. ret= -1;
  131. goto end;
  132. }
  133. if (!BUF_MEM_grow(buf,
  134. SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER))
  135. {
  136. ret= -1;
  137. goto end;
  138. }
  139. s->init_buf=buf;
  140. s->init_num=0;
  141. s->state=SSL2_ST_SEND_CLIENT_HELLO_A;
  142. s->ctx->stats.sess_connect++;
  143. s->handshake_func=ssl2_connect;
  144. BREAK;
  145. case SSL2_ST_SEND_CLIENT_HELLO_A:
  146. case SSL2_ST_SEND_CLIENT_HELLO_B:
  147. s->shutdown=0;
  148. ret=client_hello(s);
  149. if (ret <= 0) goto end;
  150. s->init_num=0;
  151. s->state=SSL2_ST_GET_SERVER_HELLO_A;
  152. BREAK;
  153. case SSL2_ST_GET_SERVER_HELLO_A:
  154. case SSL2_ST_GET_SERVER_HELLO_B:
  155. ret=get_server_hello(s);
  156. if (ret <= 0) goto end;
  157. s->init_num=0;
  158. if (!s->hit) /* new session */
  159. {
  160. s->state=SSL2_ST_SEND_CLIENT_MASTER_KEY_A;
  161. BREAK;
  162. }
  163. else
  164. {
  165. s->state=SSL2_ST_CLIENT_START_ENCRYPTION;
  166. break;
  167. }
  168. case SSL2_ST_SEND_CLIENT_MASTER_KEY_A:
  169. case SSL2_ST_SEND_CLIENT_MASTER_KEY_B:
  170. ret=client_master_key(s);
  171. if (ret <= 0) goto end;
  172. s->init_num=0;
  173. s->state=SSL2_ST_CLIENT_START_ENCRYPTION;
  174. break;
  175. case SSL2_ST_CLIENT_START_ENCRYPTION:
  176. /* Ok, we now have all the stuff needed to
  177. * start encrypting, so lets fire it up :-) */
  178. if (!ssl2_enc_init(s,1))
  179. {
  180. ret= -1;
  181. goto end;
  182. }
  183. s->s2->clear_text=0;
  184. s->state=SSL2_ST_SEND_CLIENT_FINISHED_A;
  185. break;
  186. case SSL2_ST_SEND_CLIENT_FINISHED_A:
  187. case SSL2_ST_SEND_CLIENT_FINISHED_B:
  188. ret=client_finished(s);
  189. if (ret <= 0) goto end;
  190. s->init_num=0;
  191. s->state=SSL2_ST_GET_SERVER_VERIFY_A;
  192. break;
  193. case SSL2_ST_GET_SERVER_VERIFY_A:
  194. case SSL2_ST_GET_SERVER_VERIFY_B:
  195. ret=get_server_verify(s);
  196. if (ret <= 0) goto end;
  197. s->init_num=0;
  198. s->state=SSL2_ST_GET_SERVER_FINISHED_A;
  199. break;
  200. case SSL2_ST_GET_SERVER_FINISHED_A:
  201. case SSL2_ST_GET_SERVER_FINISHED_B:
  202. ret=get_server_finished(s);
  203. if (ret <= 0) goto end;
  204. break;
  205. case SSL2_ST_SEND_CLIENT_CERTIFICATE_A:
  206. case SSL2_ST_SEND_CLIENT_CERTIFICATE_B:
  207. case SSL2_ST_SEND_CLIENT_CERTIFICATE_C:
  208. case SSL2_ST_SEND_CLIENT_CERTIFICATE_D:
  209. case SSL2_ST_X509_GET_CLIENT_CERTIFICATE:
  210. ret=client_certificate(s);
  211. if (ret <= 0) goto end;
  212. s->init_num=0;
  213. s->state=SSL2_ST_GET_SERVER_FINISHED_A;
  214. break;
  215. case SSL_ST_OK:
  216. if (s->init_buf != NULL)
  217. {
  218. BUF_MEM_free(s->init_buf);
  219. s->init_buf=NULL;
  220. }
  221. s->init_num=0;
  222. /* ERR_clear_error();*/
  223. /* If we want to cache session-ids in the client
  224. * and we successfully add the session-id to the
  225. * cache, and there is a callback, then pass it out.
  226. * 26/11/96 - eay - only add if not a re-used session.
  227. */
  228. ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
  229. if (s->hit) s->ctx->stats.sess_hit++;
  230. ret=1;
  231. /* s->server=0; */
  232. s->ctx->stats.sess_connect_good++;
  233. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
  234. goto end;
  235. /* break; */
  236. default:
  237. SSLerr(SSL_F_SSL2_CONNECT,SSL_R_UNKNOWN_STATE);
  238. return(-1);
  239. /* break; */
  240. }
  241. if ((cb != NULL) && (s->state != state))
  242. {
  243. new_state=s->state;
  244. s->state=state;
  245. cb(s,SSL_CB_CONNECT_LOOP,1);
  246. s->state=new_state;
  247. }
  248. }
  249. end:
  250. s->in_handshake--;
  251. if (cb != NULL)
  252. cb(s,SSL_CB_CONNECT_EXIT,ret);
  253. return(ret);
  254. }
  255. static int get_server_hello(SSL *s)
  256. {
  257. unsigned char *buf;
  258. unsigned char *p;
  259. int i,j;
  260. STACK_OF(SSL_CIPHER) *sk=NULL,*cl, *prio, *allow;
  261. buf=(unsigned char *)s->init_buf->data;
  262. p=buf;
  263. if (s->state == SSL2_ST_GET_SERVER_HELLO_A)
  264. {
  265. i=ssl2_read(s,(char *)&(buf[s->init_num]),11-s->init_num);
  266. if (i < (11-s->init_num))
  267. return(ssl2_part_read(s,SSL_F_GET_SERVER_HELLO,i));
  268. if (*(p++) != SSL2_MT_SERVER_HELLO)
  269. {
  270. if (p[-1] != SSL2_MT_ERROR)
  271. {
  272. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  273. SSLerr(SSL_F_GET_SERVER_HELLO,
  274. SSL_R_READ_WRONG_PACKET_TYPE);
  275. }
  276. else
  277. SSLerr(SSL_F_GET_SERVER_HELLO,
  278. SSL_R_PEER_ERROR);
  279. return(-1);
  280. }
  281. #ifdef __APPLE_CC__
  282. /* The Rhapsody 5.5 (a.k.a. MacOS X) compiler bug
  283. * workaround. <appro@fy.chalmers.se> */
  284. s->hit=(i=*(p++))?1:0;
  285. #else
  286. s->hit=(*(p++))?1:0;
  287. #endif
  288. s->s2->tmp.cert_type= *(p++);
  289. n2s(p,i);
  290. if (i < s->version) s->version=i;
  291. n2s(p,i); s->s2->tmp.cert_length=i;
  292. n2s(p,i); s->s2->tmp.csl=i;
  293. n2s(p,i); s->s2->tmp.conn_id_length=i;
  294. s->state=SSL2_ST_GET_SERVER_HELLO_B;
  295. s->init_num=0;
  296. }
  297. /* SSL2_ST_GET_SERVER_HELLO_B */
  298. j=s->s2->tmp.cert_length+s->s2->tmp.csl+s->s2->tmp.conn_id_length
  299. - s->init_num;
  300. i=ssl2_read(s,(char *)&(buf[s->init_num]),j);
  301. if (i != j) return(ssl2_part_read(s,SSL_F_GET_SERVER_HELLO,i));
  302. /* things are looking good */
  303. p=buf;
  304. if (s->hit)
  305. {
  306. if (s->s2->tmp.cert_length != 0)
  307. {
  308. SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_REUSE_CERT_LENGTH_NOT_ZERO);
  309. return(-1);
  310. }
  311. if (s->s2->tmp.cert_type != 0)
  312. {
  313. if (!(s->options &
  314. SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG))
  315. {
  316. SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_REUSE_CERT_TYPE_NOT_ZERO);
  317. return(-1);
  318. }
  319. }
  320. if (s->s2->tmp.csl != 0)
  321. {
  322. SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_REUSE_CIPHER_LIST_NOT_ZERO);
  323. return(-1);
  324. }
  325. }
  326. else
  327. {
  328. #ifdef undef
  329. /* very bad */
  330. memset(s->session->session_id,0,
  331. SSL_MAX_SSL_SESSION_ID_LENGTH_IN_BYTES);
  332. s->session->session_id_length=0;
  333. */
  334. #endif
  335. /* we need to do this in case we were trying to reuse a
  336. * client session but others are already reusing it.
  337. * If this was a new 'blank' session ID, the session-id
  338. * length will still be 0 */
  339. if (s->session->session_id_length > 0)
  340. {
  341. if (!ssl_get_new_session(s,0))
  342. {
  343. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  344. return(-1);
  345. }
  346. }
  347. if (ssl2_set_certificate(s,s->s2->tmp.cert_type,
  348. s->s2->tmp.cert_length,p) <= 0)
  349. {
  350. ssl2_return_error(s,SSL2_PE_BAD_CERTIFICATE);
  351. return(-1);
  352. }
  353. p+=s->s2->tmp.cert_length;
  354. if (s->s2->tmp.csl == 0)
  355. {
  356. ssl2_return_error(s,SSL2_PE_NO_CIPHER);
  357. SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_NO_CIPHER_LIST);
  358. return(-1);
  359. }
  360. /* We have just received a list of ciphers back from the
  361. * server. We need to get the ones that match, then select
  362. * the one we want the most :-). */
  363. /* load the ciphers */
  364. sk=ssl_bytes_to_cipher_list(s,p,s->s2->tmp.csl,
  365. &s->session->ciphers);
  366. p+=s->s2->tmp.csl;
  367. if (sk == NULL)
  368. {
  369. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  370. SSLerr(SSL_F_GET_SERVER_HELLO,ERR_R_MALLOC_FAILURE);
  371. return(-1);
  372. }
  373. sk_SSL_CIPHER_set_cmp_func(sk,ssl_cipher_ptr_id_cmp);
  374. /* get the array of ciphers we will accept */
  375. cl=SSL_get_ciphers(s);
  376. sk_SSL_CIPHER_set_cmp_func(cl,ssl_cipher_ptr_id_cmp);
  377. /*
  378. * If server preference flag set, choose the first
  379. * (highest priority) cipher the server sends, otherwise
  380. * client preference has priority.
  381. */
  382. if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE)
  383. {
  384. prio = sk;
  385. allow = cl;
  386. }
  387. else
  388. {
  389. prio = cl;
  390. allow = sk;
  391. }
  392. /* In theory we could have ciphers sent back that we
  393. * don't want to use but that does not matter since we
  394. * will check against the list we originally sent and
  395. * for performance reasons we should not bother to match
  396. * the two lists up just to check. */
  397. for (i=0; i<sk_SSL_CIPHER_num(prio); i++)
  398. {
  399. if (sk_SSL_CIPHER_find(allow,
  400. sk_SSL_CIPHER_value(prio,i)) >= 0)
  401. break;
  402. }
  403. if (i >= sk_SSL_CIPHER_num(prio))
  404. {
  405. ssl2_return_error(s,SSL2_PE_NO_CIPHER);
  406. SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_NO_CIPHER_MATCH);
  407. return(-1);
  408. }
  409. s->session->cipher=sk_SSL_CIPHER_value(prio,i);
  410. if (s->session->peer != NULL) /* can't happen*/
  411. {
  412. ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
  413. SSLerr(SSL_F_GET_SERVER_HELLO, SSL_R_INTERNAL_ERROR);
  414. return(-1);
  415. }
  416. s->session->peer = s->session->sess_cert->peer_key->x509;
  417. /* peer_key->x509 has been set by ssl2_set_certificate. */
  418. CRYPTO_add(&s->session->peer->references, 1, CRYPTO_LOCK_X509);
  419. }
  420. if (s->session->peer != s->session->sess_cert->peer_key->x509)
  421. /* can't happen */
  422. {
  423. ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
  424. SSLerr(SSL_F_GET_SERVER_HELLO, SSL_R_INTERNAL_ERROR);
  425. return(-1);
  426. }
  427. s->s2->conn_id_length=s->s2->tmp.conn_id_length;
  428. memcpy(s->s2->conn_id,p,s->s2->tmp.conn_id_length);
  429. return(1);
  430. }
  431. static int client_hello(SSL *s)
  432. {
  433. unsigned char *buf;
  434. unsigned char *p,*d;
  435. /* CIPHER **cipher;*/
  436. int i,n,j;
  437. buf=(unsigned char *)s->init_buf->data;
  438. if (s->state == SSL2_ST_SEND_CLIENT_HELLO_A)
  439. {
  440. if ((s->session == NULL) ||
  441. (s->session->ssl_version != s->version))
  442. {
  443. if (!ssl_get_new_session(s,0))
  444. {
  445. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  446. return(-1);
  447. }
  448. }
  449. /* else use the pre-loaded session */
  450. p=buf; /* header */
  451. d=p+9; /* data section */
  452. *(p++)=SSL2_MT_CLIENT_HELLO; /* type */
  453. s2n(SSL2_VERSION,p); /* version */
  454. n=j=0;
  455. n=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),d);
  456. d+=n;
  457. if (n == 0)
  458. {
  459. SSLerr(SSL_F_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
  460. return(-1);
  461. }
  462. s2n(n,p); /* cipher spec num bytes */
  463. if ((s->session->session_id_length > 0) &&
  464. (s->session->session_id_length <=
  465. SSL2_MAX_SSL_SESSION_ID_LENGTH))
  466. {
  467. i=s->session->session_id_length;
  468. s2n(i,p); /* session id length */
  469. memcpy(d,s->session->session_id,(unsigned int)i);
  470. d+=i;
  471. }
  472. else
  473. {
  474. s2n(0,p);
  475. }
  476. s->s2->challenge_length=SSL2_CHALLENGE_LENGTH;
  477. s2n(SSL2_CHALLENGE_LENGTH,p); /* challenge length */
  478. /*challenge id data*/
  479. RAND_pseudo_bytes(s->s2->challenge,SSL2_CHALLENGE_LENGTH);
  480. memcpy(d,s->s2->challenge,SSL2_CHALLENGE_LENGTH);
  481. d+=SSL2_CHALLENGE_LENGTH;
  482. s->state=SSL2_ST_SEND_CLIENT_HELLO_B;
  483. s->init_num=d-buf;
  484. s->init_off=0;
  485. }
  486. /* SSL2_ST_SEND_CLIENT_HELLO_B */
  487. return(ssl2_do_write(s));
  488. }
  489. static int client_master_key(SSL *s)
  490. {
  491. unsigned char *buf;
  492. unsigned char *p,*d;
  493. int clear,enc,karg,i;
  494. SSL_SESSION *sess;
  495. const EVP_CIPHER *c;
  496. const EVP_MD *md;
  497. buf=(unsigned char *)s->init_buf->data;
  498. if (s->state == SSL2_ST_SEND_CLIENT_MASTER_KEY_A)
  499. {
  500. if (!ssl_cipher_get_evp(s->session,&c,&md,NULL))
  501. {
  502. ssl2_return_error(s,SSL2_PE_NO_CIPHER);
  503. SSLerr(SSL_F_CLIENT_MASTER_KEY,SSL_R_PROBLEMS_MAPPING_CIPHER_FUNCTIONS);
  504. return(-1);
  505. }
  506. sess=s->session;
  507. p=buf;
  508. d=p+10;
  509. *(p++)=SSL2_MT_CLIENT_MASTER_KEY;/* type */
  510. i=ssl_put_cipher_by_char(s,sess->cipher,p);
  511. p+=i;
  512. /* make key_arg data */
  513. i=EVP_CIPHER_iv_length(c);
  514. sess->key_arg_length=i;
  515. if (i > 0) RAND_pseudo_bytes(sess->key_arg,i);
  516. /* make a master key */
  517. i=EVP_CIPHER_key_length(c);
  518. sess->master_key_length=i;
  519. if (i > 0)
  520. {
  521. if (RAND_bytes(sess->master_key,i) <= 0)
  522. {
  523. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  524. return(-1);
  525. }
  526. }
  527. if (sess->cipher->algorithm2 & SSL2_CF_8_BYTE_ENC)
  528. enc=8;
  529. else if (SSL_C_IS_EXPORT(sess->cipher))
  530. enc=5;
  531. else
  532. enc=i;
  533. if (i < enc)
  534. {
  535. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  536. SSLerr(SSL_F_CLIENT_MASTER_KEY,SSL_R_CIPHER_TABLE_SRC_ERROR);
  537. return(-1);
  538. }
  539. clear=i-enc;
  540. s2n(clear,p);
  541. memcpy(d,sess->master_key,(unsigned int)clear);
  542. d+=clear;
  543. enc=ssl_rsa_public_encrypt(sess->sess_cert,enc,
  544. &(sess->master_key[clear]),d,
  545. (s->s2->ssl2_rollback)?RSA_SSLV23_PADDING:RSA_PKCS1_PADDING);
  546. if (enc <= 0)
  547. {
  548. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  549. SSLerr(SSL_F_CLIENT_MASTER_KEY,SSL_R_PUBLIC_KEY_ENCRYPT_ERROR);
  550. return(-1);
  551. }
  552. #ifdef PKCS1_CHECK
  553. if (s->options & SSL_OP_PKCS1_CHECK_1) d[1]++;
  554. if (s->options & SSL_OP_PKCS1_CHECK_2)
  555. sess->master_key[clear]++;
  556. #endif
  557. s2n(enc,p);
  558. d+=enc;
  559. karg=sess->key_arg_length;
  560. s2n(karg,p); /* key arg size */
  561. memcpy(d,sess->key_arg,(unsigned int)karg);
  562. d+=karg;
  563. s->state=SSL2_ST_SEND_CLIENT_MASTER_KEY_B;
  564. s->init_num=d-buf;
  565. s->init_off=0;
  566. }
  567. /* SSL2_ST_SEND_CLIENT_MASTER_KEY_B */
  568. return(ssl2_do_write(s));
  569. }
  570. static int client_finished(SSL *s)
  571. {
  572. unsigned char *p;
  573. if (s->state == SSL2_ST_SEND_CLIENT_FINISHED_A)
  574. {
  575. p=(unsigned char *)s->init_buf->data;
  576. *(p++)=SSL2_MT_CLIENT_FINISHED;
  577. memcpy(p,s->s2->conn_id,(unsigned int)s->s2->conn_id_length);
  578. s->state=SSL2_ST_SEND_CLIENT_FINISHED_B;
  579. s->init_num=s->s2->conn_id_length+1;
  580. s->init_off=0;
  581. }
  582. return(ssl2_do_write(s));
  583. }
  584. /* read the data and then respond */
  585. static int client_certificate(SSL *s)
  586. {
  587. unsigned char *buf;
  588. unsigned char *p,*d;
  589. int i;
  590. unsigned int n;
  591. int cert_ch_len=0;
  592. unsigned char *cert_ch;
  593. buf=(unsigned char *)s->init_buf->data;
  594. cert_ch= &(buf[2]);
  595. /* We have a cert associated with the SSL, so attach it to
  596. * the session if it does not have one */
  597. if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_A)
  598. {
  599. i=ssl2_read(s,(char *)&(buf[s->init_num]),
  600. SSL2_MAX_CERT_CHALLENGE_LENGTH+1-s->init_num);
  601. if (i<(SSL2_MIN_CERT_CHALLENGE_LENGTH+1-s->init_num))
  602. return(ssl2_part_read(s,SSL_F_CLIENT_CERTIFICATE,i));
  603. /* type=buf[0]; */
  604. /* type eq x509 */
  605. if (buf[1] != SSL2_AT_MD5_WITH_RSA_ENCRYPTION)
  606. {
  607. ssl2_return_error(s,SSL2_PE_UNSUPPORTED_CERTIFICATE_TYPE);
  608. SSLerr(SSL_F_CLIENT_CERTIFICATE,SSL_R_BAD_AUTHENTICATION_TYPE);
  609. return(-1);
  610. }
  611. cert_ch_len=i-1;
  612. if ((s->cert == NULL) ||
  613. (s->cert->key->x509 == NULL) ||
  614. (s->cert->key->privatekey == NULL))
  615. {
  616. s->state=SSL2_ST_X509_GET_CLIENT_CERTIFICATE;
  617. }
  618. else
  619. s->state=SSL2_ST_SEND_CLIENT_CERTIFICATE_C;
  620. }
  621. if (s->state == SSL2_ST_X509_GET_CLIENT_CERTIFICATE)
  622. {
  623. X509 *x509=NULL;
  624. EVP_PKEY *pkey=NULL;
  625. /* If we get an error we need to
  626. * ssl->rwstate=SSL_X509_LOOKUP;
  627. * return(error);
  628. * We should then be retried when things are ok and we
  629. * can get a cert or not */
  630. i=0;
  631. if (s->ctx->client_cert_cb != NULL)
  632. {
  633. i=s->ctx->client_cert_cb(s,&(x509),&(pkey));
  634. }
  635. if (i < 0)
  636. {
  637. s->rwstate=SSL_X509_LOOKUP;
  638. return(-1);
  639. }
  640. s->rwstate=SSL_NOTHING;
  641. if ((i == 1) && (pkey != NULL) && (x509 != NULL))
  642. {
  643. s->state=SSL2_ST_SEND_CLIENT_CERTIFICATE_C;
  644. if ( !SSL_use_certificate(s,x509) ||
  645. !SSL_use_PrivateKey(s,pkey))
  646. {
  647. i=0;
  648. }
  649. X509_free(x509);
  650. EVP_PKEY_free(pkey);
  651. }
  652. else if (i == 1)
  653. {
  654. if (x509 != NULL) X509_free(x509);
  655. if (pkey != NULL) EVP_PKEY_free(pkey);
  656. SSLerr(SSL_F_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
  657. i=0;
  658. }
  659. if (i == 0)
  660. {
  661. /* We have no client certificate to respond with
  662. * so send the correct error message back */
  663. s->state=SSL2_ST_SEND_CLIENT_CERTIFICATE_B;
  664. p=buf;
  665. *(p++)=SSL2_MT_ERROR;
  666. s2n(SSL2_PE_NO_CERTIFICATE,p);
  667. s->init_off=0;
  668. s->init_num=3;
  669. /* Write is done at the end */
  670. }
  671. }
  672. if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_B)
  673. {
  674. return(ssl2_do_write(s));
  675. }
  676. if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_C)
  677. {
  678. EVP_MD_CTX ctx;
  679. /* ok, now we calculate the checksum
  680. * do it first so we can reuse buf :-) */
  681. p=buf;
  682. EVP_SignInit(&ctx,s->ctx->rsa_md5);
  683. EVP_SignUpdate(&ctx,s->s2->key_material,
  684. (unsigned int)s->s2->key_material_length);
  685. EVP_SignUpdate(&ctx,cert_ch,(unsigned int)cert_ch_len);
  686. n=i2d_X509(s->session->sess_cert->peer_key->x509,&p);
  687. EVP_SignUpdate(&ctx,buf,(unsigned int)n);
  688. p=buf;
  689. d=p+6;
  690. *(p++)=SSL2_MT_CLIENT_CERTIFICATE;
  691. *(p++)=SSL2_CT_X509_CERTIFICATE;
  692. n=i2d_X509(s->cert->key->x509,&d);
  693. s2n(n,p);
  694. if (!EVP_SignFinal(&ctx,d,&n,s->cert->key->privatekey))
  695. {
  696. /* this is not good. If things have failed it
  697. * means there so something wrong with the key.
  698. * We will continue with a 0 length signature
  699. */
  700. }
  701. memset(&ctx,0,sizeof(ctx));
  702. s2n(n,p);
  703. d+=n;
  704. s->state=SSL2_ST_SEND_CLIENT_CERTIFICATE_D;
  705. s->init_num=d-buf;
  706. s->init_off=0;
  707. }
  708. /* if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_D) */
  709. return(ssl2_do_write(s));
  710. }
  711. static int get_server_verify(SSL *s)
  712. {
  713. unsigned char *p;
  714. int i;
  715. p=(unsigned char *)s->init_buf->data;
  716. if (s->state == SSL2_ST_GET_SERVER_VERIFY_A)
  717. {
  718. i=ssl2_read(s,(char *)&(p[s->init_num]),1-s->init_num);
  719. if (i < (1-s->init_num))
  720. return(ssl2_part_read(s,SSL_F_GET_SERVER_VERIFY,i));
  721. s->state= SSL2_ST_GET_SERVER_VERIFY_B;
  722. s->init_num=0;
  723. if (*p != SSL2_MT_SERVER_VERIFY)
  724. {
  725. if (p[0] != SSL2_MT_ERROR)
  726. {
  727. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  728. SSLerr(SSL_F_GET_SERVER_VERIFY,
  729. SSL_R_READ_WRONG_PACKET_TYPE);
  730. }
  731. else
  732. SSLerr(SSL_F_GET_SERVER_VERIFY,
  733. SSL_R_PEER_ERROR);
  734. return(-1);
  735. }
  736. }
  737. p=(unsigned char *)s->init_buf->data;
  738. i=ssl2_read(s,(char *)&(p[s->init_num]),
  739. (unsigned int)s->s2->challenge_length-s->init_num);
  740. if (i < ((int)s->s2->challenge_length-s->init_num))
  741. return(ssl2_part_read(s,SSL_F_GET_SERVER_VERIFY,i));
  742. if (memcmp(p,s->s2->challenge,(unsigned int)s->s2->challenge_length) != 0)
  743. {
  744. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  745. SSLerr(SSL_F_GET_SERVER_VERIFY,SSL_R_CHALLENGE_IS_DIFFERENT);
  746. return(-1);
  747. }
  748. return(1);
  749. }
  750. static int get_server_finished(SSL *s)
  751. {
  752. unsigned char *buf;
  753. unsigned char *p;
  754. int i;
  755. buf=(unsigned char *)s->init_buf->data;
  756. p=buf;
  757. if (s->state == SSL2_ST_GET_SERVER_FINISHED_A)
  758. {
  759. i=ssl2_read(s,(char *)&(buf[s->init_num]),1-s->init_num);
  760. if (i < (1-s->init_num))
  761. return(ssl2_part_read(s,SSL_F_GET_SERVER_FINISHED,i));
  762. s->init_num=i;
  763. if (*p == SSL2_MT_REQUEST_CERTIFICATE)
  764. {
  765. s->state=SSL2_ST_SEND_CLIENT_CERTIFICATE_A;
  766. return(1);
  767. }
  768. else if (*p != SSL2_MT_SERVER_FINISHED)
  769. {
  770. if (p[0] != SSL2_MT_ERROR)
  771. {
  772. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  773. SSLerr(SSL_F_GET_SERVER_FINISHED,SSL_R_READ_WRONG_PACKET_TYPE);
  774. }
  775. else
  776. SSLerr(SSL_F_GET_SERVER_FINISHED,SSL_R_PEER_ERROR);
  777. return(-1);
  778. }
  779. s->state=SSL_ST_OK;
  780. s->init_num=0;
  781. }
  782. i=ssl2_read(s,(char *)&(buf[s->init_num]),
  783. SSL2_SSL_SESSION_ID_LENGTH-s->init_num);
  784. if (i < (SSL2_SSL_SESSION_ID_LENGTH-s->init_num))
  785. return(ssl2_part_read(s,SSL_F_GET_SERVER_FINISHED,i));
  786. if (!s->hit) /* new session */
  787. {
  788. /* new session-id */
  789. /* Make sure we were not trying to re-use an old SSL_SESSION
  790. * or bad things can happen */
  791. /* ZZZZZZZZZZZZZ */
  792. s->session->session_id_length=SSL2_SSL_SESSION_ID_LENGTH;
  793. memcpy(s->session->session_id,p,SSL2_SSL_SESSION_ID_LENGTH);
  794. }
  795. else
  796. {
  797. if (!(s->options & SSL_OP_MICROSOFT_SESS_ID_BUG))
  798. {
  799. if (memcmp(buf,s->session->session_id,
  800. (unsigned int)s->session->session_id_length) != 0)
  801. {
  802. ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  803. SSLerr(SSL_F_GET_SERVER_FINISHED,SSL_R_SSL_SESSION_ID_IS_DIFFERENT);
  804. return(-1);
  805. }
  806. }
  807. }
  808. return(1);
  809. }
  810. /* loads in the certificate from the server */
  811. int ssl2_set_certificate(SSL *s, int type, int len, unsigned char *data)
  812. {
  813. STACK_OF(X509) *sk=NULL;
  814. EVP_PKEY *pkey=NULL;
  815. SESS_CERT *sc=NULL;
  816. int i;
  817. X509 *x509=NULL;
  818. int ret=0;
  819. x509=d2i_X509(NULL,&data,(long)len);
  820. if (x509 == NULL)
  821. {
  822. SSLerr(SSL_F_SSL2_SET_CERTIFICATE,ERR_R_X509_LIB);
  823. goto err;
  824. }
  825. if ((sk=sk_X509_new_null()) == NULL || !sk_X509_push(sk,x509))
  826. {
  827. SSLerr(SSL_F_SSL2_SET_CERTIFICATE,ERR_R_MALLOC_FAILURE);
  828. goto err;
  829. }
  830. i=ssl_verify_cert_chain(s,sk);
  831. if ((s->verify_mode != SSL_VERIFY_NONE) && (!i))
  832. {
  833. SSLerr(SSL_F_SSL2_SET_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED);
  834. goto err;
  835. }
  836. ERR_clear_error(); /* but we keep s->verify_result */
  837. s->session->verify_result = s->verify_result;
  838. /* server's cert for this session */
  839. sc=ssl_sess_cert_new();
  840. if (sc == NULL)
  841. {
  842. ret= -1;
  843. goto err;
  844. }
  845. if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert);
  846. s->session->sess_cert=sc;
  847. sc->peer_pkeys[SSL_PKEY_RSA_ENC].x509=x509;
  848. sc->peer_key= &(sc->peer_pkeys[SSL_PKEY_RSA_ENC]);
  849. pkey=X509_get_pubkey(x509);
  850. x509=NULL;
  851. if (pkey == NULL)
  852. {
  853. SSLerr(SSL_F_SSL2_SET_CERTIFICATE,SSL_R_UNABLE_TO_EXTRACT_PUBLIC_KEY);
  854. goto err;
  855. }
  856. if (pkey->type != EVP_PKEY_RSA)
  857. {
  858. SSLerr(SSL_F_SSL2_SET_CERTIFICATE,SSL_R_PUBLIC_KEY_NOT_RSA);
  859. goto err;
  860. }
  861. if (!ssl_set_peer_cert_type(sc,SSL2_CT_X509_CERTIFICATE))
  862. goto err;
  863. ret=1;
  864. err:
  865. sk_X509_free(sk);
  866. X509_free(x509);
  867. EVP_PKEY_free(pkey);
  868. return(ret);
  869. }
  870. static int ssl_rsa_public_encrypt(SESS_CERT *sc, int len, unsigned char *from,
  871. unsigned char *to, int padding)
  872. {
  873. EVP_PKEY *pkey=NULL;
  874. int i= -1;
  875. if ((sc == NULL) || (sc->peer_key->x509 == NULL) ||
  876. ((pkey=X509_get_pubkey(sc->peer_key->x509)) == NULL))
  877. {
  878. SSLerr(SSL_F_SSL_RSA_PUBLIC_ENCRYPT,SSL_R_NO_PUBLICKEY);
  879. return(-1);
  880. }
  881. if (pkey->type != EVP_PKEY_RSA)
  882. {
  883. SSLerr(SSL_F_SSL_RSA_PUBLIC_ENCRYPT,SSL_R_PUBLIC_KEY_IS_NOT_RSA);
  884. goto end;
  885. }
  886. /* we have the public key */
  887. i=RSA_public_encrypt(len,from,to,pkey->pkey.rsa,padding);
  888. if (i < 0)
  889. SSLerr(SSL_F_SSL_RSA_PUBLIC_ENCRYPT,ERR_R_RSA_LIB);
  890. end:
  891. EVP_PKEY_free(pkey);
  892. return(i);
  893. }
  894. #else /* !OPENSSL_NO_SSL2 */
  895. # if PEDANTIC
  896. static void *dummy=&dummy;
  897. # endif
  898. #endif