s3_clnt.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841
  1. /* ssl/s3_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/md5.h>
  63. #include <openssl/sha.h>
  64. #include <openssl/evp.h>
  65. #include "ssl_locl.h"
  66. #ifndef OPENSSL_NO_KRB5
  67. #include "kssl.h"
  68. #endif
  69. static SSL_METHOD *ssl3_get_client_method(int ver);
  70. static int ssl3_client_hello(SSL *s);
  71. static int ssl3_get_server_hello(SSL *s);
  72. static int ssl3_get_certificate_request(SSL *s);
  73. static int ca_dn_cmp(const X509_NAME * const *a,const X509_NAME * const *b);
  74. static int ssl3_get_server_done(SSL *s);
  75. static int ssl3_send_client_verify(SSL *s);
  76. static int ssl3_send_client_certificate(SSL *s);
  77. static int ssl3_send_client_key_exchange(SSL *s);
  78. static int ssl3_get_key_exchange(SSL *s);
  79. static int ssl3_get_server_certificate(SSL *s);
  80. static int ssl3_check_cert_and_algorithm(SSL *s);
  81. static SSL_METHOD *ssl3_get_client_method(int ver)
  82. {
  83. if (ver == SSL3_VERSION)
  84. return(SSLv3_client_method());
  85. else
  86. return(NULL);
  87. }
  88. SSL_METHOD *SSLv3_client_method(void)
  89. {
  90. static int init=1;
  91. static SSL_METHOD SSLv3_client_data;
  92. if (init)
  93. {
  94. init=0;
  95. memcpy((char *)&SSLv3_client_data,(char *)sslv3_base_method(),
  96. sizeof(SSL_METHOD));
  97. SSLv3_client_data.ssl_connect=ssl3_connect;
  98. SSLv3_client_data.get_ssl_method=ssl3_get_client_method;
  99. }
  100. return(&SSLv3_client_data);
  101. }
  102. int ssl3_connect(SSL *s)
  103. {
  104. BUF_MEM *buf;
  105. unsigned long Time=time(NULL),l;
  106. long num1;
  107. void (*cb)()=NULL;
  108. int ret= -1;
  109. int new_state,state,skip=0;;
  110. RAND_add(&Time,sizeof(Time),0);
  111. ERR_clear_error();
  112. clear_sys_error();
  113. if (s->info_callback != NULL)
  114. cb=s->info_callback;
  115. else if (s->ctx->info_callback != NULL)
  116. cb=s->ctx->info_callback;
  117. if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
  118. s->in_handshake++;
  119. for (;;)
  120. {
  121. state=s->state;
  122. switch(s->state)
  123. {
  124. case SSL_ST_RENEGOTIATE:
  125. s->new_session=1;
  126. s->state=SSL_ST_CONNECT;
  127. s->ctx->stats.sess_connect_renegotiate++;
  128. /* break */
  129. case SSL_ST_BEFORE:
  130. case SSL_ST_CONNECT:
  131. case SSL_ST_BEFORE|SSL_ST_CONNECT:
  132. case SSL_ST_OK|SSL_ST_CONNECT:
  133. s->server=0;
  134. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
  135. if ((s->version & 0xff00 ) != 0x0300)
  136. {
  137. SSLerr(SSL_F_SSL3_CONNECT, SSL_R_INTERNAL_ERROR);
  138. ret = -1;
  139. goto end;
  140. }
  141. /* s->version=SSL3_VERSION; */
  142. s->type=SSL_ST_CONNECT;
  143. if (s->init_buf == NULL)
  144. {
  145. if ((buf=BUF_MEM_new()) == NULL)
  146. {
  147. ret= -1;
  148. goto end;
  149. }
  150. if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
  151. {
  152. ret= -1;
  153. goto end;
  154. }
  155. s->init_buf=buf;
  156. }
  157. if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
  158. /* setup buffing BIO */
  159. if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; }
  160. /* don't push the buffering BIO quite yet */
  161. ssl3_init_finished_mac(s);
  162. s->state=SSL3_ST_CW_CLNT_HELLO_A;
  163. s->ctx->stats.sess_connect++;
  164. s->init_num=0;
  165. break;
  166. case SSL3_ST_CW_CLNT_HELLO_A:
  167. case SSL3_ST_CW_CLNT_HELLO_B:
  168. s->shutdown=0;
  169. ret=ssl3_client_hello(s);
  170. if (ret <= 0) goto end;
  171. s->state=SSL3_ST_CR_SRVR_HELLO_A;
  172. s->init_num=0;
  173. /* turn on buffering for the next lot of output */
  174. if (s->bbio != s->wbio)
  175. s->wbio=BIO_push(s->bbio,s->wbio);
  176. break;
  177. case SSL3_ST_CR_SRVR_HELLO_A:
  178. case SSL3_ST_CR_SRVR_HELLO_B:
  179. ret=ssl3_get_server_hello(s);
  180. if (ret <= 0) goto end;
  181. if (s->hit)
  182. s->state=SSL3_ST_CR_FINISHED_A;
  183. else
  184. s->state=SSL3_ST_CR_CERT_A;
  185. s->init_num=0;
  186. break;
  187. case SSL3_ST_CR_CERT_A:
  188. case SSL3_ST_CR_CERT_B:
  189. /* Check if it is anon DH */
  190. if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
  191. {
  192. ret=ssl3_get_server_certificate(s);
  193. if (ret <= 0) goto end;
  194. }
  195. else
  196. skip=1;
  197. s->state=SSL3_ST_CR_KEY_EXCH_A;
  198. s->init_num=0;
  199. break;
  200. case SSL3_ST_CR_KEY_EXCH_A:
  201. case SSL3_ST_CR_KEY_EXCH_B:
  202. ret=ssl3_get_key_exchange(s);
  203. if (ret <= 0) goto end;
  204. s->state=SSL3_ST_CR_CERT_REQ_A;
  205. s->init_num=0;
  206. /* at this point we check that we have the
  207. * required stuff from the server */
  208. if (!ssl3_check_cert_and_algorithm(s))
  209. {
  210. ret= -1;
  211. goto end;
  212. }
  213. break;
  214. case SSL3_ST_CR_CERT_REQ_A:
  215. case SSL3_ST_CR_CERT_REQ_B:
  216. ret=ssl3_get_certificate_request(s);
  217. if (ret <= 0) goto end;
  218. s->state=SSL3_ST_CR_SRVR_DONE_A;
  219. s->init_num=0;
  220. break;
  221. case SSL3_ST_CR_SRVR_DONE_A:
  222. case SSL3_ST_CR_SRVR_DONE_B:
  223. ret=ssl3_get_server_done(s);
  224. if (ret <= 0) goto end;
  225. if (s->s3->tmp.cert_req)
  226. s->state=SSL3_ST_CW_CERT_A;
  227. else
  228. s->state=SSL3_ST_CW_KEY_EXCH_A;
  229. s->init_num=0;
  230. break;
  231. case SSL3_ST_CW_CERT_A:
  232. case SSL3_ST_CW_CERT_B:
  233. case SSL3_ST_CW_CERT_C:
  234. case SSL3_ST_CW_CERT_D:
  235. ret=ssl3_send_client_certificate(s);
  236. if (ret <= 0) goto end;
  237. s->state=SSL3_ST_CW_KEY_EXCH_A;
  238. s->init_num=0;
  239. break;
  240. case SSL3_ST_CW_KEY_EXCH_A:
  241. case SSL3_ST_CW_KEY_EXCH_B:
  242. ret=ssl3_send_client_key_exchange(s);
  243. if (ret <= 0) goto end;
  244. l=s->s3->tmp.new_cipher->algorithms;
  245. /* EAY EAY EAY need to check for DH fix cert
  246. * sent back */
  247. /* For TLS, cert_req is set to 2, so a cert chain
  248. * of nothing is sent, but no verify packet is sent */
  249. if (s->s3->tmp.cert_req == 1)
  250. {
  251. s->state=SSL3_ST_CW_CERT_VRFY_A;
  252. }
  253. else
  254. {
  255. s->state=SSL3_ST_CW_CHANGE_A;
  256. s->s3->change_cipher_spec=0;
  257. }
  258. s->init_num=0;
  259. break;
  260. case SSL3_ST_CW_CERT_VRFY_A:
  261. case SSL3_ST_CW_CERT_VRFY_B:
  262. ret=ssl3_send_client_verify(s);
  263. if (ret <= 0) goto end;
  264. s->state=SSL3_ST_CW_CHANGE_A;
  265. s->init_num=0;
  266. s->s3->change_cipher_spec=0;
  267. break;
  268. case SSL3_ST_CW_CHANGE_A:
  269. case SSL3_ST_CW_CHANGE_B:
  270. ret=ssl3_send_change_cipher_spec(s,
  271. SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);
  272. if (ret <= 0) goto end;
  273. s->state=SSL3_ST_CW_FINISHED_A;
  274. s->init_num=0;
  275. s->session->cipher=s->s3->tmp.new_cipher;
  276. if (s->s3->tmp.new_compression == NULL)
  277. s->session->compress_meth=0;
  278. else
  279. s->session->compress_meth=
  280. s->s3->tmp.new_compression->id;
  281. if (!s->method->ssl3_enc->setup_key_block(s))
  282. {
  283. ret= -1;
  284. goto end;
  285. }
  286. if (!s->method->ssl3_enc->change_cipher_state(s,
  287. SSL3_CHANGE_CIPHER_CLIENT_WRITE))
  288. {
  289. ret= -1;
  290. goto end;
  291. }
  292. break;
  293. case SSL3_ST_CW_FINISHED_A:
  294. case SSL3_ST_CW_FINISHED_B:
  295. ret=ssl3_send_finished(s,
  296. SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
  297. s->method->ssl3_enc->client_finished_label,
  298. s->method->ssl3_enc->client_finished_label_len);
  299. if (ret <= 0) goto end;
  300. s->state=SSL3_ST_CW_FLUSH;
  301. /* clear flags */
  302. s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
  303. if (s->hit)
  304. {
  305. s->s3->tmp.next_state=SSL_ST_OK;
  306. if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)
  307. {
  308. s->state=SSL_ST_OK;
  309. s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
  310. s->s3->delay_buf_pop_ret=0;
  311. }
  312. }
  313. else
  314. {
  315. s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;
  316. }
  317. s->init_num=0;
  318. break;
  319. case SSL3_ST_CR_FINISHED_A:
  320. case SSL3_ST_CR_FINISHED_B:
  321. ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
  322. SSL3_ST_CR_FINISHED_B);
  323. if (ret <= 0) goto end;
  324. if (s->hit)
  325. s->state=SSL3_ST_CW_CHANGE_A;
  326. else
  327. s->state=SSL_ST_OK;
  328. s->init_num=0;
  329. break;
  330. case SSL3_ST_CW_FLUSH:
  331. /* number of bytes to be flushed */
  332. num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
  333. if (num1 > 0)
  334. {
  335. s->rwstate=SSL_WRITING;
  336. num1=BIO_flush(s->wbio);
  337. if (num1 <= 0) { ret= -1; goto end; }
  338. s->rwstate=SSL_NOTHING;
  339. }
  340. s->state=s->s3->tmp.next_state;
  341. break;
  342. case SSL_ST_OK:
  343. /* clean a few things up */
  344. ssl3_cleanup_key_block(s);
  345. if (s->init_buf != NULL)
  346. {
  347. BUF_MEM_free(s->init_buf);
  348. s->init_buf=NULL;
  349. }
  350. /* If we are not 'joining' the last two packets,
  351. * remove the buffering now */
  352. if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
  353. ssl_free_wbio_buffer(s);
  354. /* else do it later in ssl3_write */
  355. s->init_num=0;
  356. s->new_session=0;
  357. ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
  358. if (s->hit) s->ctx->stats.sess_hit++;
  359. ret=1;
  360. /* s->server=0; */
  361. s->handshake_func=ssl3_connect;
  362. s->ctx->stats.sess_connect_good++;
  363. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
  364. goto end;
  365. /* break; */
  366. default:
  367. SSLerr(SSL_F_SSL3_CONNECT,SSL_R_UNKNOWN_STATE);
  368. ret= -1;
  369. goto end;
  370. /* break; */
  371. }
  372. /* did we do anything */
  373. if (!s->s3->tmp.reuse_message && !skip)
  374. {
  375. if (s->debug)
  376. {
  377. if ((ret=BIO_flush(s->wbio)) <= 0)
  378. goto end;
  379. }
  380. if ((cb != NULL) && (s->state != state))
  381. {
  382. new_state=s->state;
  383. s->state=state;
  384. cb(s,SSL_CB_CONNECT_LOOP,1);
  385. s->state=new_state;
  386. }
  387. }
  388. skip=0;
  389. }
  390. end:
  391. if (cb != NULL)
  392. cb(s,SSL_CB_CONNECT_EXIT,ret);
  393. s->in_handshake--;
  394. return(ret);
  395. }
  396. static int ssl3_client_hello(SSL *s)
  397. {
  398. unsigned char *buf;
  399. unsigned char *p,*d;
  400. int i,j;
  401. unsigned long Time,l;
  402. SSL_COMP *comp;
  403. buf=(unsigned char *)s->init_buf->data;
  404. if (s->state == SSL3_ST_CW_CLNT_HELLO_A)
  405. {
  406. if ((s->session == NULL) ||
  407. (s->session->ssl_version != s->version) ||
  408. (s->session->not_resumable))
  409. {
  410. if (!ssl_get_new_session(s,0))
  411. goto err;
  412. }
  413. /* else use the pre-loaded session */
  414. p=s->s3->client_random;
  415. Time=time(NULL); /* Time */
  416. l2n(Time,p);
  417. RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-sizeof(Time));
  418. /* Do the message type and length last */
  419. d=p= &(buf[4]);
  420. *(p++)=s->version>>8;
  421. *(p++)=s->version&0xff;
  422. s->client_version=s->version;
  423. /* Random stuff */
  424. memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
  425. p+=SSL3_RANDOM_SIZE;
  426. /* Session ID */
  427. if (s->new_session)
  428. i=0;
  429. else
  430. i=s->session->session_id_length;
  431. *(p++)=i;
  432. if (i != 0)
  433. {
  434. memcpy(p,s->session->session_id,i);
  435. p+=i;
  436. }
  437. /* Ciphers supported */
  438. i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]));
  439. if (i == 0)
  440. {
  441. SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
  442. goto err;
  443. }
  444. s2n(i,p);
  445. p+=i;
  446. /* COMPRESSION */
  447. if (s->ctx->comp_methods == NULL)
  448. j=0;
  449. else
  450. j=sk_SSL_COMP_num(s->ctx->comp_methods);
  451. *(p++)=1+j;
  452. for (i=0; i<j; i++)
  453. {
  454. comp=sk_SSL_COMP_value(s->ctx->comp_methods,i);
  455. *(p++)=comp->id;
  456. }
  457. *(p++)=0; /* Add the NULL method */
  458. l=(p-d);
  459. d=buf;
  460. *(d++)=SSL3_MT_CLIENT_HELLO;
  461. l2n3(l,d);
  462. s->state=SSL3_ST_CW_CLNT_HELLO_B;
  463. /* number of bytes to write */
  464. s->init_num=p-buf;
  465. s->init_off=0;
  466. }
  467. /* SSL3_ST_CW_CLNT_HELLO_B */
  468. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  469. err:
  470. return(-1);
  471. }
  472. static int ssl3_get_server_hello(SSL *s)
  473. {
  474. STACK_OF(SSL_CIPHER) *sk;
  475. SSL_CIPHER *c;
  476. unsigned char *p,*d;
  477. int i,al,ok;
  478. unsigned int j;
  479. long n;
  480. SSL_COMP *comp;
  481. n=ssl3_get_message(s,
  482. SSL3_ST_CR_SRVR_HELLO_A,
  483. SSL3_ST_CR_SRVR_HELLO_B,
  484. SSL3_MT_SERVER_HELLO,
  485. 300, /* ?? */
  486. &ok);
  487. if (!ok) return((int)n);
  488. d=p=(unsigned char *)s->init_buf->data;
  489. if ((p[0] != (s->version>>8)) || (p[1] != (s->version&0xff)))
  490. {
  491. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_SSL_VERSION);
  492. s->version=(s->version&0xff00)|p[1];
  493. al=SSL_AD_PROTOCOL_VERSION;
  494. goto f_err;
  495. }
  496. p+=2;
  497. /* load the server hello data */
  498. /* load the server random */
  499. memcpy(s->s3->server_random,p,SSL3_RANDOM_SIZE);
  500. p+=SSL3_RANDOM_SIZE;
  501. /* get the session-id */
  502. j= *(p++);
  503. if ((j != 0) && (j != SSL3_SESSION_ID_SIZE))
  504. {
  505. /* SSLref returns 16 :-( */
  506. if (j < SSL2_SSL_SESSION_ID_LENGTH)
  507. {
  508. al=SSL_AD_ILLEGAL_PARAMETER;
  509. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SSL3_SESSION_ID_TOO_SHORT);
  510. goto f_err;
  511. }
  512. }
  513. if (j != 0 && j == s->session->session_id_length
  514. && memcmp(p,s->session->session_id,j) == 0)
  515. {
  516. if(s->sid_ctx_length != s->session->sid_ctx_length
  517. || memcmp(s->session->sid_ctx,s->sid_ctx,s->sid_ctx_length))
  518. {
  519. al=SSL_AD_ILLEGAL_PARAMETER;
  520. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
  521. goto f_err;
  522. }
  523. s->hit=1;
  524. }
  525. else /* a miss or crap from the other end */
  526. {
  527. /* If we were trying for session-id reuse, make a new
  528. * SSL_SESSION so we don't stuff up other people */
  529. s->hit=0;
  530. if (s->session->session_id_length > 0)
  531. {
  532. if (!ssl_get_new_session(s,0))
  533. {
  534. al=SSL_AD_INTERNAL_ERROR;
  535. goto f_err;
  536. }
  537. }
  538. s->session->session_id_length=j;
  539. memcpy(s->session->session_id,p,j); /* j could be 0 */
  540. }
  541. p+=j;
  542. c=ssl_get_cipher_by_char(s,p);
  543. if (c == NULL)
  544. {
  545. /* unknown cipher */
  546. al=SSL_AD_ILLEGAL_PARAMETER;
  547. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNKNOWN_CIPHER_RETURNED);
  548. goto f_err;
  549. }
  550. p+=ssl_put_cipher_by_char(s,NULL,NULL);
  551. sk=ssl_get_ciphers_by_id(s);
  552. i=sk_SSL_CIPHER_find(sk,c);
  553. if (i < 0)
  554. {
  555. /* we did not say we would use this cipher */
  556. al=SSL_AD_ILLEGAL_PARAMETER;
  557. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_CIPHER_RETURNED);
  558. goto f_err;
  559. }
  560. if (s->hit && (s->session->cipher != c))
  561. {
  562. if (!(s->options &
  563. SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG))
  564. {
  565. al=SSL_AD_ILLEGAL_PARAMETER;
  566. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
  567. goto f_err;
  568. }
  569. }
  570. s->s3->tmp.new_cipher=c;
  571. /* lets get the compression algorithm */
  572. /* COMPRESSION */
  573. j= *(p++);
  574. if (j == 0)
  575. comp=NULL;
  576. else
  577. comp=ssl3_comp_find(s->ctx->comp_methods,j);
  578. if ((j != 0) && (comp == NULL))
  579. {
  580. al=SSL_AD_ILLEGAL_PARAMETER;
  581. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  582. goto f_err;
  583. }
  584. else
  585. {
  586. s->s3->tmp.new_compression=comp;
  587. }
  588. if (p != (d+n))
  589. {
  590. /* wrong packet length */
  591. al=SSL_AD_DECODE_ERROR;
  592. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_PACKET_LENGTH);
  593. goto err;
  594. }
  595. return(1);
  596. f_err:
  597. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  598. err:
  599. return(-1);
  600. }
  601. static int ssl3_get_server_certificate(SSL *s)
  602. {
  603. int al,i,ok,ret= -1;
  604. unsigned long n,nc,llen,l;
  605. X509 *x=NULL;
  606. unsigned char *p,*d,*q;
  607. STACK_OF(X509) *sk=NULL;
  608. SESS_CERT *sc;
  609. EVP_PKEY *pkey=NULL;
  610. int need_cert = 1; /* VRS: 0=> will allow null cert if auth == KRB5 */
  611. n=ssl3_get_message(s,
  612. SSL3_ST_CR_CERT_A,
  613. SSL3_ST_CR_CERT_B,
  614. -1,
  615. #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN32)
  616. 1024*30, /* 30k max cert list :-) */
  617. #else
  618. 1024*100, /* 100k max cert list :-) */
  619. #endif
  620. &ok);
  621. if (!ok) return((int)n);
  622. if (s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE)
  623. {
  624. s->s3->tmp.reuse_message=1;
  625. return(1);
  626. }
  627. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
  628. {
  629. al=SSL_AD_UNEXPECTED_MESSAGE;
  630. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_BAD_MESSAGE_TYPE);
  631. goto f_err;
  632. }
  633. d=p=(unsigned char *)s->init_buf->data;
  634. if ((sk=sk_X509_new_null()) == NULL)
  635. {
  636. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
  637. goto err;
  638. }
  639. n2l3(p,llen);
  640. if (llen+3 != n)
  641. {
  642. al=SSL_AD_DECODE_ERROR;
  643. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
  644. goto f_err;
  645. }
  646. for (nc=0; nc<llen; )
  647. {
  648. n2l3(p,l);
  649. if ((l+nc+3) > llen)
  650. {
  651. al=SSL_AD_DECODE_ERROR;
  652. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
  653. goto f_err;
  654. }
  655. q=p;
  656. x=d2i_X509(NULL,&q,l);
  657. if (x == NULL)
  658. {
  659. al=SSL_AD_BAD_CERTIFICATE;
  660. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_ASN1_LIB);
  661. goto f_err;
  662. }
  663. if (q != (p+l))
  664. {
  665. al=SSL_AD_DECODE_ERROR;
  666. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
  667. goto f_err;
  668. }
  669. if (!sk_X509_push(sk,x))
  670. {
  671. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
  672. goto err;
  673. }
  674. x=NULL;
  675. nc+=l+3;
  676. p=q;
  677. }
  678. i=ssl_verify_cert_chain(s,sk);
  679. if ((s->verify_mode != SSL_VERIFY_NONE) && (!i))
  680. {
  681. al=ssl_verify_alarm_type(s->verify_result);
  682. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED);
  683. goto f_err;
  684. }
  685. ERR_clear_error(); /* but we keep s->verify_result */
  686. sc=ssl_sess_cert_new();
  687. if (sc == NULL) goto err;
  688. if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert);
  689. s->session->sess_cert=sc;
  690. sc->cert_chain=sk;
  691. /* Inconsistency alert: cert_chain does include the peer's
  692. * certificate, which we don't include in s3_srvr.c */
  693. x=sk_X509_value(sk,0);
  694. sk=NULL;
  695. /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end */
  696. pkey=X509_get_pubkey(x);
  697. /* VRS: allow null cert if auth == KRB5 */
  698. need_cert =
  699. ((s->s3->tmp.new_cipher->algorithms & (SSL_MKEY_MASK|SSL_AUTH_MASK))
  700. == (SSL_aKRB5|SSL_kKRB5))? 0: 1;
  701. #ifdef KSSL_DEBUG
  702. printf("pkey,x = %p, %p\n", pkey,x);
  703. printf("ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x,pkey));
  704. printf("cipher, alg, nc = %s, %lx, %d\n", s->s3->tmp.new_cipher->name,
  705. s->s3->tmp.new_cipher->algorithms, need_cert);
  706. #endif /* KSSL_DEBUG */
  707. if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey)))
  708. {
  709. x=NULL;
  710. al=SSL3_AL_FATAL;
  711. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
  712. goto f_err;
  713. }
  714. i=ssl_cert_type(x,pkey);
  715. if (need_cert && i < 0)
  716. {
  717. x=NULL;
  718. al=SSL3_AL_FATAL;
  719. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  720. goto f_err;
  721. }
  722. if (need_cert)
  723. {
  724. sc->peer_cert_type=i;
  725. CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
  726. /* Why would the following ever happen?
  727. * We just created sc a couple of lines ago. */
  728. if (sc->peer_pkeys[i].x509 != NULL)
  729. X509_free(sc->peer_pkeys[i].x509);
  730. sc->peer_pkeys[i].x509=x;
  731. sc->peer_key= &(sc->peer_pkeys[i]);
  732. if (s->session->peer != NULL)
  733. X509_free(s->session->peer);
  734. CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
  735. s->session->peer=x;
  736. }
  737. else
  738. {
  739. sc->peer_cert_type=i;
  740. sc->peer_key= NULL;
  741. if (s->session->peer != NULL)
  742. X509_free(s->session->peer);
  743. s->session->peer=NULL;
  744. }
  745. s->session->verify_result = s->verify_result;
  746. x=NULL;
  747. ret=1;
  748. if (0)
  749. {
  750. f_err:
  751. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  752. }
  753. err:
  754. EVP_PKEY_free(pkey);
  755. X509_free(x);
  756. sk_X509_pop_free(sk,X509_free);
  757. return(ret);
  758. }
  759. static int ssl3_get_key_exchange(SSL *s)
  760. {
  761. #ifndef OPENSSL_NO_RSA
  762. unsigned char *q,md_buf[EVP_MAX_MD_SIZE*2];
  763. #endif
  764. EVP_MD_CTX md_ctx;
  765. unsigned char *param,*p;
  766. int al,i,j,param_len,ok;
  767. long n,alg;
  768. EVP_PKEY *pkey=NULL;
  769. #ifndef OPENSSL_NO_RSA
  770. RSA *rsa=NULL;
  771. #endif
  772. #ifndef OPENSSL_NO_DH
  773. DH *dh=NULL;
  774. #endif
  775. n=ssl3_get_message(s,
  776. SSL3_ST_CR_KEY_EXCH_A,
  777. SSL3_ST_CR_KEY_EXCH_B,
  778. -1,
  779. 1024*8, /* ?? */
  780. &ok);
  781. if (!ok) return((int)n);
  782. if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE)
  783. {
  784. s->s3->tmp.reuse_message=1;
  785. return(1);
  786. }
  787. param=p=(unsigned char *)s->init_buf->data;
  788. if (s->session->sess_cert != NULL)
  789. {
  790. #ifndef OPENSSL_NO_RSA
  791. if (s->session->sess_cert->peer_rsa_tmp != NULL)
  792. {
  793. RSA_free(s->session->sess_cert->peer_rsa_tmp);
  794. s->session->sess_cert->peer_rsa_tmp=NULL;
  795. }
  796. #endif
  797. #ifndef OPENSSL_NO_DH
  798. if (s->session->sess_cert->peer_dh_tmp)
  799. {
  800. DH_free(s->session->sess_cert->peer_dh_tmp);
  801. s->session->sess_cert->peer_dh_tmp=NULL;
  802. }
  803. #endif
  804. }
  805. else
  806. {
  807. s->session->sess_cert=ssl_sess_cert_new();
  808. }
  809. param_len=0;
  810. alg=s->s3->tmp.new_cipher->algorithms;
  811. #ifndef OPENSSL_NO_RSA
  812. if (alg & SSL_kRSA)
  813. {
  814. if ((rsa=RSA_new()) == NULL)
  815. {
  816. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  817. goto err;
  818. }
  819. n2s(p,i);
  820. param_len=i+2;
  821. if (param_len > n)
  822. {
  823. al=SSL_AD_DECODE_ERROR;
  824. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_MODULUS_LENGTH);
  825. goto f_err;
  826. }
  827. if (!(rsa->n=BN_bin2bn(p,i,rsa->n)))
  828. {
  829. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  830. goto err;
  831. }
  832. p+=i;
  833. n2s(p,i);
  834. param_len+=i+2;
  835. if (param_len > n)
  836. {
  837. al=SSL_AD_DECODE_ERROR;
  838. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_E_LENGTH);
  839. goto f_err;
  840. }
  841. if (!(rsa->e=BN_bin2bn(p,i,rsa->e)))
  842. {
  843. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  844. goto err;
  845. }
  846. p+=i;
  847. n-=param_len;
  848. /* this should be because we are using an export cipher */
  849. if (alg & SSL_aRSA)
  850. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  851. else
  852. {
  853. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);
  854. goto err;
  855. }
  856. s->session->sess_cert->peer_rsa_tmp=rsa;
  857. rsa=NULL;
  858. }
  859. #else /* OPENSSL_NO_RSA */
  860. if (0)
  861. ;
  862. #endif
  863. #ifndef OPENSSL_NO_DH
  864. else if (alg & SSL_kEDH)
  865. {
  866. if ((dh=DH_new()) == NULL)
  867. {
  868. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_DH_LIB);
  869. goto err;
  870. }
  871. n2s(p,i);
  872. param_len=i+2;
  873. if (param_len > n)
  874. {
  875. al=SSL_AD_DECODE_ERROR;
  876. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_P_LENGTH);
  877. goto f_err;
  878. }
  879. if (!(dh->p=BN_bin2bn(p,i,NULL)))
  880. {
  881. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  882. goto err;
  883. }
  884. p+=i;
  885. n2s(p,i);
  886. param_len+=i+2;
  887. if (param_len > n)
  888. {
  889. al=SSL_AD_DECODE_ERROR;
  890. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_G_LENGTH);
  891. goto f_err;
  892. }
  893. if (!(dh->g=BN_bin2bn(p,i,NULL)))
  894. {
  895. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  896. goto err;
  897. }
  898. p+=i;
  899. n2s(p,i);
  900. param_len+=i+2;
  901. if (param_len > n)
  902. {
  903. al=SSL_AD_DECODE_ERROR;
  904. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_PUB_KEY_LENGTH);
  905. goto f_err;
  906. }
  907. if (!(dh->pub_key=BN_bin2bn(p,i,NULL)))
  908. {
  909. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  910. goto err;
  911. }
  912. p+=i;
  913. n-=param_len;
  914. #ifndef OPENSSL_NO_RSA
  915. if (alg & SSL_aRSA)
  916. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  917. #else
  918. if (0)
  919. ;
  920. #endif
  921. #ifndef OPENSSL_NO_DSA
  922. else if (alg & SSL_aDSS)
  923. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].x509);
  924. #endif
  925. /* else anonymous DH, so no certificate or pkey. */
  926. s->session->sess_cert->peer_dh_tmp=dh;
  927. dh=NULL;
  928. }
  929. else if ((alg & SSL_kDHr) || (alg & SSL_kDHd))
  930. {
  931. al=SSL_AD_ILLEGAL_PARAMETER;
  932. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
  933. goto f_err;
  934. }
  935. #endif /* !OPENSSL_NO_DH */
  936. if (alg & SSL_aFZA)
  937. {
  938. al=SSL_AD_HANDSHAKE_FAILURE;
  939. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
  940. goto f_err;
  941. }
  942. /* p points to the next byte, there are 'n' bytes left */
  943. /* if it was signed, check the signature */
  944. if (pkey != NULL)
  945. {
  946. n2s(p,i);
  947. n-=2;
  948. j=EVP_PKEY_size(pkey);
  949. if ((i != n) || (n > j) || (n <= 0))
  950. {
  951. /* wrong packet length */
  952. al=SSL_AD_DECODE_ERROR;
  953. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_WRONG_SIGNATURE_LENGTH);
  954. goto f_err;
  955. }
  956. #ifndef OPENSSL_NO_RSA
  957. if (pkey->type == EVP_PKEY_RSA)
  958. {
  959. int num;
  960. j=0;
  961. q=md_buf;
  962. for (num=2; num > 0; num--)
  963. {
  964. EVP_DigestInit(&md_ctx,(num == 2)
  965. ?s->ctx->md5:s->ctx->sha1);
  966. EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  967. EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  968. EVP_DigestUpdate(&md_ctx,param,param_len);
  969. EVP_DigestFinal(&md_ctx,q,(unsigned int *)&i);
  970. q+=i;
  971. j+=i;
  972. }
  973. i=RSA_verify(NID_md5_sha1, md_buf, j, p, n,
  974. pkey->pkey.rsa);
  975. if (i < 0)
  976. {
  977. al=SSL_AD_DECRYPT_ERROR;
  978. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);
  979. goto f_err;
  980. }
  981. if (i == 0)
  982. {
  983. /* bad signature */
  984. al=SSL_AD_DECRYPT_ERROR;
  985. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
  986. goto f_err;
  987. }
  988. }
  989. else
  990. #endif
  991. #ifndef OPENSSL_NO_DSA
  992. if (pkey->type == EVP_PKEY_DSA)
  993. {
  994. /* lets do DSS */
  995. EVP_VerifyInit(&md_ctx,EVP_dss1());
  996. EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  997. EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  998. EVP_VerifyUpdate(&md_ctx,param,param_len);
  999. if (!EVP_VerifyFinal(&md_ctx,p,(int)n,pkey))
  1000. {
  1001. /* bad signature */
  1002. al=SSL_AD_DECRYPT_ERROR;
  1003. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
  1004. goto f_err;
  1005. }
  1006. }
  1007. else
  1008. #endif
  1009. {
  1010. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);
  1011. goto err;
  1012. }
  1013. }
  1014. else
  1015. {
  1016. /* still data left over */
  1017. if (!(alg & SSL_aNULL))
  1018. {
  1019. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);
  1020. goto err;
  1021. }
  1022. if (n != 0)
  1023. {
  1024. al=SSL_AD_DECODE_ERROR;
  1025. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_EXTRA_DATA_IN_MESSAGE);
  1026. goto f_err;
  1027. }
  1028. }
  1029. EVP_PKEY_free(pkey);
  1030. return(1);
  1031. f_err:
  1032. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1033. err:
  1034. EVP_PKEY_free(pkey);
  1035. #ifndef OPENSSL_NO_RSA
  1036. if (rsa != NULL)
  1037. RSA_free(rsa);
  1038. #endif
  1039. #ifndef OPENSSL_NO_DH
  1040. if (dh != NULL)
  1041. DH_free(dh);
  1042. #endif
  1043. return(-1);
  1044. }
  1045. static int ssl3_get_certificate_request(SSL *s)
  1046. {
  1047. int ok,ret=0;
  1048. unsigned long n,nc,l;
  1049. unsigned int llen,ctype_num,i;
  1050. X509_NAME *xn=NULL;
  1051. unsigned char *p,*d,*q;
  1052. STACK_OF(X509_NAME) *ca_sk=NULL;
  1053. n=ssl3_get_message(s,
  1054. SSL3_ST_CR_CERT_REQ_A,
  1055. SSL3_ST_CR_CERT_REQ_B,
  1056. -1,
  1057. #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN32)
  1058. 1024*30, /* 30k max cert list :-) */
  1059. #else
  1060. 1024*100, /* 100k max cert list :-) */
  1061. #endif
  1062. &ok);
  1063. if (!ok) return((int)n);
  1064. s->s3->tmp.cert_req=0;
  1065. if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)
  1066. {
  1067. s->s3->tmp.reuse_message=1;
  1068. return(1);
  1069. }
  1070. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST)
  1071. {
  1072. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
  1073. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_WRONG_MESSAGE_TYPE);
  1074. goto err;
  1075. }
  1076. /* TLS does not like anon-DH with client cert */
  1077. if (s->version > SSL3_VERSION)
  1078. {
  1079. l=s->s3->tmp.new_cipher->algorithms;
  1080. if (l & SSL_aNULL)
  1081. {
  1082. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
  1083. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
  1084. goto err;
  1085. }
  1086. }
  1087. d=p=(unsigned char *)s->init_buf->data;
  1088. if ((ca_sk=sk_X509_NAME_new(ca_dn_cmp)) == NULL)
  1089. {
  1090. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
  1091. goto err;
  1092. }
  1093. /* get the certificate types */
  1094. ctype_num= *(p++);
  1095. if (ctype_num > SSL3_CT_NUMBER)
  1096. ctype_num=SSL3_CT_NUMBER;
  1097. for (i=0; i<ctype_num; i++)
  1098. s->s3->tmp.ctype[i]= p[i];
  1099. p+=ctype_num;
  1100. /* get the CA RDNs */
  1101. n2s(p,llen);
  1102. #if 0
  1103. {
  1104. FILE *out;
  1105. out=fopen("/tmp/vsign.der","w");
  1106. fwrite(p,1,llen,out);
  1107. fclose(out);
  1108. }
  1109. #endif
  1110. if ((llen+ctype_num+2+1) != n)
  1111. {
  1112. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1113. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_LENGTH_MISMATCH);
  1114. goto err;
  1115. }
  1116. for (nc=0; nc<llen; )
  1117. {
  1118. n2s(p,l);
  1119. if ((l+nc+2) > llen)
  1120. {
  1121. if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
  1122. goto cont; /* netscape bugs */
  1123. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1124. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_TOO_LONG);
  1125. goto err;
  1126. }
  1127. q=p;
  1128. if ((xn=d2i_X509_NAME(NULL,&q,l)) == NULL)
  1129. {
  1130. /* If netscape tolerance is on, ignore errors */
  1131. if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG)
  1132. goto cont;
  1133. else
  1134. {
  1135. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1136. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_ASN1_LIB);
  1137. goto err;
  1138. }
  1139. }
  1140. if (q != (p+l))
  1141. {
  1142. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1143. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_LENGTH_MISMATCH);
  1144. goto err;
  1145. }
  1146. if (!sk_X509_NAME_push(ca_sk,xn))
  1147. {
  1148. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
  1149. goto err;
  1150. }
  1151. p+=l;
  1152. nc+=l+2;
  1153. }
  1154. if (0)
  1155. {
  1156. cont:
  1157. ERR_clear_error();
  1158. }
  1159. /* we should setup a certificate to return.... */
  1160. s->s3->tmp.cert_req=1;
  1161. s->s3->tmp.ctype_num=ctype_num;
  1162. if (s->s3->tmp.ca_names != NULL)
  1163. sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
  1164. s->s3->tmp.ca_names=ca_sk;
  1165. ca_sk=NULL;
  1166. ret=1;
  1167. err:
  1168. if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free);
  1169. return(ret);
  1170. }
  1171. static int ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b)
  1172. {
  1173. return(X509_NAME_cmp(*a,*b));
  1174. }
  1175. static int ssl3_get_server_done(SSL *s)
  1176. {
  1177. int ok,ret=0;
  1178. long n;
  1179. n=ssl3_get_message(s,
  1180. SSL3_ST_CR_SRVR_DONE_A,
  1181. SSL3_ST_CR_SRVR_DONE_B,
  1182. SSL3_MT_SERVER_DONE,
  1183. 30, /* should be very small, like 0 :-) */
  1184. &ok);
  1185. if (!ok) return((int)n);
  1186. if (n > 0)
  1187. {
  1188. /* should contain no data */
  1189. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1190. SSLerr(SSL_F_SSL3_GET_SERVER_DONE,SSL_R_LENGTH_MISMATCH);
  1191. }
  1192. ret=1;
  1193. return(ret);
  1194. }
  1195. static int ssl3_send_client_key_exchange(SSL *s)
  1196. {
  1197. unsigned char *p,*d;
  1198. int n;
  1199. unsigned long l;
  1200. #ifndef OPENSSL_NO_RSA
  1201. unsigned char *q;
  1202. EVP_PKEY *pkey=NULL;
  1203. #endif
  1204. #ifndef OPENSSL_NO_KRB5
  1205. KSSL_ERR kssl_err;
  1206. #endif /* OPENSSL_NO_KRB5 */
  1207. if (s->state == SSL3_ST_CW_KEY_EXCH_A)
  1208. {
  1209. d=(unsigned char *)s->init_buf->data;
  1210. p= &(d[4]);
  1211. l=s->s3->tmp.new_cipher->algorithms;
  1212. /* Fool emacs indentation */
  1213. if (0) {}
  1214. #ifndef OPENSSL_NO_RSA
  1215. else if (l & SSL_kRSA)
  1216. {
  1217. RSA *rsa;
  1218. unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
  1219. if (s->session->sess_cert->peer_rsa_tmp != NULL)
  1220. rsa=s->session->sess_cert->peer_rsa_tmp;
  1221. else
  1222. {
  1223. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1224. if ((pkey == NULL) ||
  1225. (pkey->type != EVP_PKEY_RSA) ||
  1226. (pkey->pkey.rsa == NULL))
  1227. {
  1228. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);
  1229. goto err;
  1230. }
  1231. rsa=pkey->pkey.rsa;
  1232. EVP_PKEY_free(pkey);
  1233. }
  1234. tmp_buf[0]=s->client_version>>8;
  1235. tmp_buf[1]=s->client_version&0xff;
  1236. if (RAND_bytes(&(tmp_buf[2]),SSL_MAX_MASTER_KEY_LENGTH-2) <= 0)
  1237. goto err;
  1238. s->session->master_key_length=SSL_MAX_MASTER_KEY_LENGTH;
  1239. q=p;
  1240. /* Fix buf for TLS and beyond */
  1241. if (s->version > SSL3_VERSION)
  1242. p+=2;
  1243. n=RSA_public_encrypt(SSL_MAX_MASTER_KEY_LENGTH,
  1244. tmp_buf,p,rsa,RSA_PKCS1_PADDING);
  1245. #ifdef PKCS1_CHECK
  1246. if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;
  1247. if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;
  1248. #endif
  1249. if (n <= 0)
  1250. {
  1251. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT);
  1252. goto err;
  1253. }
  1254. /* Fix buf for TLS and beyond */
  1255. if (s->version > SSL3_VERSION)
  1256. {
  1257. s2n(n,q);
  1258. n+=2;
  1259. }
  1260. s->session->master_key_length=
  1261. s->method->ssl3_enc->generate_master_secret(s,
  1262. s->session->master_key,
  1263. tmp_buf,SSL_MAX_MASTER_KEY_LENGTH);
  1264. memset(tmp_buf,0,SSL_MAX_MASTER_KEY_LENGTH);
  1265. }
  1266. #endif
  1267. #ifndef OPENSSL_NO_KRB5
  1268. else if (l & SSL_kKRB5)
  1269. {
  1270. krb5_error_code krb5rc;
  1271. KSSL_CTX *kssl_ctx = s->kssl_ctx;
  1272. krb5_data krb5_ap_req;
  1273. #ifdef KSSL_DEBUG
  1274. printf("ssl3_send_client_key_exchange(%lx & %lx)\n",
  1275. l, SSL_kKRB5);
  1276. #endif /* KSSL_DEBUG */
  1277. /*
  1278. ** Tried to send random tmp_buf[] as PMS in Kerberos ticket
  1279. ** by passing krb5_mk_req_extended(ctx,authctx,opts, tmp_buf, ...)
  1280. ** but: I can't retrieve the PMS on the other side! There is
  1281. ** some indication in the krb5 source that this is only used
  1282. ** to generate a checksum. OTOH, the Tung book shows data
  1283. ** ("GET widget01.txt") being passed in krb5_mk_req_extended()
  1284. ** by way of krb5_sendauth(). I don't get it.
  1285. ** Until Kerberos goes 3DES, the big PMS secret would only be
  1286. ** encrypted in 1-DES anyway. So losing the PMS shouldn't be
  1287. ** a big deal.
  1288. */
  1289. krb5rc = kssl_cget_tkt(kssl_ctx, &krb5_ap_req,
  1290. &kssl_err);
  1291. #ifdef KSSL_DEBUG
  1292. {
  1293. printf("kssl_cget_tkt rtn %d\n", krb5rc);
  1294. kssl_ctx_show(kssl_ctx);
  1295. if (krb5rc && kssl_err.text)
  1296. printf("kssl_cget_tkt kssl_err=%s\n", kssl_err.text);
  1297. }
  1298. #endif /* KSSL_DEBUG */
  1299. if (krb5rc)
  1300. {
  1301. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
  1302. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, kssl_err.reason);
  1303. goto err;
  1304. }
  1305. /* Send ticket (copy to *p, set n = length)
  1306. */
  1307. n = krb5_ap_req.length;
  1308. memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
  1309. if (krb5_ap_req.data)
  1310. kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
  1311. /* 19991013 VRS - 3DES is kind of bogus here,
  1312. ** at least until Kerberos supports 3DES. The only
  1313. ** real secret is the 8-byte Kerberos session key;
  1314. ** the other key material ((s->) client_random, server_random)
  1315. ** could be sniffed. Mixing in these nonces should help
  1316. ** protect against replay attacks, however.
  1317. **
  1318. ** Alternate code for Kerberos Purists:
  1319. **
  1320. ** memcpy(s->session->master_key, kssl_ctx->key, kssl_ctx->length);
  1321. ** s->session->master_key_length = kssl_ctx->length;
  1322. */
  1323. s->session->master_key_length=
  1324. s->method->ssl3_enc->generate_master_secret(s,
  1325. s->session->master_key, kssl_ctx->key,kssl_ctx->length);
  1326. }
  1327. #endif
  1328. #ifndef OPENSSL_NO_DH
  1329. else if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
  1330. {
  1331. DH *dh_srvr,*dh_clnt;
  1332. if (s->session->sess_cert->peer_dh_tmp != NULL)
  1333. dh_srvr=s->session->sess_cert->peer_dh_tmp;
  1334. else
  1335. {
  1336. /* we get them from the cert */
  1337. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
  1338. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
  1339. goto err;
  1340. }
  1341. /* generate a new random key */
  1342. if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
  1343. {
  1344. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
  1345. goto err;
  1346. }
  1347. if (!DH_generate_key(dh_clnt))
  1348. {
  1349. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
  1350. goto err;
  1351. }
  1352. /* use the 'p' output buffer for the DH key, but
  1353. * make sure to clear it out afterwards */
  1354. n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
  1355. if (n <= 0)
  1356. {
  1357. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
  1358. goto err;
  1359. }
  1360. /* generate master key from the result */
  1361. s->session->master_key_length=
  1362. s->method->ssl3_enc->generate_master_secret(s,
  1363. s->session->master_key,p,n);
  1364. /* clean up */
  1365. memset(p,0,n);
  1366. /* send off the data */
  1367. n=BN_num_bytes(dh_clnt->pub_key);
  1368. s2n(n,p);
  1369. BN_bn2bin(dh_clnt->pub_key,p);
  1370. n+=2;
  1371. DH_free(dh_clnt);
  1372. /* perhaps clean things up a bit EAY EAY EAY EAY*/
  1373. }
  1374. #endif
  1375. else
  1376. {
  1377. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
  1378. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);
  1379. goto err;
  1380. }
  1381. *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE;
  1382. l2n3(n,d);
  1383. s->state=SSL3_ST_CW_KEY_EXCH_B;
  1384. /* number of bytes to write */
  1385. s->init_num=n+4;
  1386. s->init_off=0;
  1387. }
  1388. /* SSL3_ST_CW_KEY_EXCH_B */
  1389. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  1390. err:
  1391. return(-1);
  1392. }
  1393. static int ssl3_send_client_verify(SSL *s)
  1394. {
  1395. unsigned char *p,*d;
  1396. unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
  1397. EVP_PKEY *pkey;
  1398. #ifndef OPENSSL_NO_RSA
  1399. unsigned u=0;
  1400. #endif
  1401. unsigned long n;
  1402. #ifndef OPENSSL_NO_DSA
  1403. int j;
  1404. #endif
  1405. if (s->state == SSL3_ST_CW_CERT_VRFY_A)
  1406. {
  1407. d=(unsigned char *)s->init_buf->data;
  1408. p= &(d[4]);
  1409. pkey=s->cert->key->privatekey;
  1410. s->method->ssl3_enc->cert_verify_mac(s,&(s->s3->finish_dgst2),
  1411. &(data[MD5_DIGEST_LENGTH]));
  1412. #ifndef OPENSSL_NO_RSA
  1413. if (pkey->type == EVP_PKEY_RSA)
  1414. {
  1415. s->method->ssl3_enc->cert_verify_mac(s,
  1416. &(s->s3->finish_dgst1),&(data[0]));
  1417. if (RSA_sign(NID_md5_sha1, data,
  1418. MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
  1419. &(p[2]), &u, pkey->pkey.rsa) <= 0 )
  1420. {
  1421. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_RSA_LIB);
  1422. goto err;
  1423. }
  1424. s2n(u,p);
  1425. n=u+2;
  1426. }
  1427. else
  1428. #endif
  1429. #ifndef OPENSSL_NO_DSA
  1430. if (pkey->type == EVP_PKEY_DSA)
  1431. {
  1432. if (!DSA_sign(pkey->save_type,
  1433. &(data[MD5_DIGEST_LENGTH]),
  1434. SHA_DIGEST_LENGTH,&(p[2]),
  1435. (unsigned int *)&j,pkey->pkey.dsa))
  1436. {
  1437. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_DSA_LIB);
  1438. goto err;
  1439. }
  1440. s2n(j,p);
  1441. n=j+2;
  1442. }
  1443. else
  1444. #endif
  1445. {
  1446. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,SSL_R_INTERNAL_ERROR);
  1447. goto err;
  1448. }
  1449. *(d++)=SSL3_MT_CERTIFICATE_VERIFY;
  1450. l2n3(n,d);
  1451. s->init_num=(int)n+4;
  1452. s->init_off=0;
  1453. }
  1454. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  1455. err:
  1456. return(-1);
  1457. }
  1458. static int ssl3_send_client_certificate(SSL *s)
  1459. {
  1460. X509 *x509=NULL;
  1461. EVP_PKEY *pkey=NULL;
  1462. int i;
  1463. unsigned long l;
  1464. if (s->state == SSL3_ST_CW_CERT_A)
  1465. {
  1466. if ((s->cert == NULL) ||
  1467. (s->cert->key->x509 == NULL) ||
  1468. (s->cert->key->privatekey == NULL))
  1469. s->state=SSL3_ST_CW_CERT_B;
  1470. else
  1471. s->state=SSL3_ST_CW_CERT_C;
  1472. }
  1473. /* We need to get a client cert */
  1474. if (s->state == SSL3_ST_CW_CERT_B)
  1475. {
  1476. /* If we get an error, we need to
  1477. * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
  1478. * We then get retied later */
  1479. i=0;
  1480. if (s->ctx->client_cert_cb != NULL)
  1481. i=s->ctx->client_cert_cb(s,&(x509),&(pkey));
  1482. if (i < 0)
  1483. {
  1484. s->rwstate=SSL_X509_LOOKUP;
  1485. return(-1);
  1486. }
  1487. s->rwstate=SSL_NOTHING;
  1488. if ((i == 1) && (pkey != NULL) && (x509 != NULL))
  1489. {
  1490. s->state=SSL3_ST_CW_CERT_B;
  1491. if ( !SSL_use_certificate(s,x509) ||
  1492. !SSL_use_PrivateKey(s,pkey))
  1493. i=0;
  1494. }
  1495. else if (i == 1)
  1496. {
  1497. i=0;
  1498. SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
  1499. }
  1500. if (x509 != NULL) X509_free(x509);
  1501. if (pkey != NULL) EVP_PKEY_free(pkey);
  1502. if (i == 0)
  1503. {
  1504. if (s->version == SSL3_VERSION)
  1505. {
  1506. s->s3->tmp.cert_req=0;
  1507. ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
  1508. return(1);
  1509. }
  1510. else
  1511. {
  1512. s->s3->tmp.cert_req=2;
  1513. }
  1514. }
  1515. /* Ok, we have a cert */
  1516. s->state=SSL3_ST_CW_CERT_C;
  1517. }
  1518. if (s->state == SSL3_ST_CW_CERT_C)
  1519. {
  1520. s->state=SSL3_ST_CW_CERT_D;
  1521. l=ssl3_output_cert_chain(s,
  1522. (s->s3->tmp.cert_req == 2)?NULL:s->cert->key->x509);
  1523. s->init_num=(int)l;
  1524. s->init_off=0;
  1525. }
  1526. /* SSL3_ST_CW_CERT_D */
  1527. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  1528. }
  1529. #define has_bits(i,m) (((i)&(m)) == (m))
  1530. static int ssl3_check_cert_and_algorithm(SSL *s)
  1531. {
  1532. int i,idx;
  1533. long algs;
  1534. EVP_PKEY *pkey=NULL;
  1535. SESS_CERT *sc;
  1536. #ifndef OPENSSL_NO_RSA
  1537. RSA *rsa;
  1538. #endif
  1539. #ifndef OPENSSL_NO_DH
  1540. DH *dh;
  1541. #endif
  1542. sc=s->session->sess_cert;
  1543. if (sc == NULL)
  1544. {
  1545. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_INTERNAL_ERROR);
  1546. goto err;
  1547. }
  1548. algs=s->s3->tmp.new_cipher->algorithms;
  1549. /* we don't have a certificate */
  1550. if (algs & (SSL_aDH|SSL_aNULL|SSL_aKRB5))
  1551. return(1);
  1552. #ifndef OPENSSL_NO_RSA
  1553. rsa=s->session->sess_cert->peer_rsa_tmp;
  1554. #endif
  1555. #ifndef OPENSSL_NO_DH
  1556. dh=s->session->sess_cert->peer_dh_tmp;
  1557. #endif
  1558. /* This is the passed certificate */
  1559. idx=sc->peer_cert_type;
  1560. pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509);
  1561. i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey);
  1562. EVP_PKEY_free(pkey);
  1563. /* Check that we have a certificate if we require one */
  1564. if ((algs & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))
  1565. {
  1566. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_SIGNING_CERT);
  1567. goto f_err;
  1568. }
  1569. #ifndef OPENSSL_NO_DSA
  1570. else if ((algs & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))
  1571. {
  1572. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DSA_SIGNING_CERT);
  1573. goto f_err;
  1574. }
  1575. #endif
  1576. #ifndef OPENSSL_NO_RSA
  1577. if ((algs & SSL_kRSA) &&
  1578. !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))
  1579. {
  1580. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_ENCRYPTING_CERT);
  1581. goto f_err;
  1582. }
  1583. #endif
  1584. #ifndef OPENSSL_NO_DH
  1585. if ((algs & SSL_kEDH) &&
  1586. !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))
  1587. {
  1588. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_KEY);
  1589. goto f_err;
  1590. }
  1591. else if ((algs & SSL_kDHr) && !has_bits(i,EVP_PK_DH|EVP_PKS_RSA))
  1592. {
  1593. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_RSA_CERT);
  1594. goto f_err;
  1595. }
  1596. #ifndef OPENSSL_NO_DSA
  1597. else if ((algs & SSL_kDHd) && !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))
  1598. {
  1599. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_DSA_CERT);
  1600. goto f_err;
  1601. }
  1602. #endif
  1603. #endif
  1604. if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i,EVP_PKT_EXP))
  1605. {
  1606. #ifndef OPENSSL_NO_RSA
  1607. if (algs & SSL_kRSA)
  1608. {
  1609. if (rsa == NULL
  1610. || RSA_size(rsa) > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
  1611. {
  1612. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
  1613. goto f_err;
  1614. }
  1615. }
  1616. else
  1617. #endif
  1618. #ifndef OPENSSL_NO_DH
  1619. if (algs & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
  1620. {
  1621. if (dh == NULL
  1622. || DH_size(dh) > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
  1623. {
  1624. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_DH_KEY);
  1625. goto f_err;
  1626. }
  1627. }
  1628. else
  1629. #endif
  1630. {
  1631. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
  1632. goto f_err;
  1633. }
  1634. }
  1635. return(1);
  1636. f_err:
  1637. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
  1638. err:
  1639. return(0);
  1640. }