2
0

s3_clnt.c 42 KB

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