s3_srvr.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817
  1. /* ssl/s3_srvr.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. #define REUSE_CIPHER_BUG
  59. #define NETSCAPE_HANG_BUG
  60. #include <stdio.h>
  61. #include <openssl/buffer.h>
  62. #include <openssl/rand.h>
  63. #include <openssl/objects.h>
  64. #include <openssl/md5.h>
  65. #include <openssl/sha.h>
  66. #include <openssl/evp.h>
  67. #include <openssl/x509.h>
  68. #include "ssl_locl.h"
  69. #ifndef OPENSSL_NO_KRB5
  70. #include "kssl.h"
  71. #endif /* OPENSSL_NO_KRB5 */
  72. static SSL_METHOD *ssl3_get_server_method(int ver);
  73. static int ssl3_get_client_hello(SSL *s);
  74. static int ssl3_check_client_hello(SSL *s);
  75. static int ssl3_send_server_hello(SSL *s);
  76. static int ssl3_send_server_key_exchange(SSL *s);
  77. static int ssl3_send_certificate_request(SSL *s);
  78. static int ssl3_send_server_done(SSL *s);
  79. static int ssl3_get_client_key_exchange(SSL *s);
  80. static int ssl3_get_client_certificate(SSL *s);
  81. static int ssl3_get_cert_verify(SSL *s);
  82. static int ssl3_send_hello_request(SSL *s);
  83. static SSL_METHOD *ssl3_get_server_method(int ver)
  84. {
  85. if (ver == SSL3_VERSION)
  86. return(SSLv3_server_method());
  87. else
  88. return(NULL);
  89. }
  90. SSL_METHOD *SSLv3_server_method(void)
  91. {
  92. static int init=1;
  93. static SSL_METHOD SSLv3_server_data;
  94. if (init)
  95. {
  96. memcpy((char *)&SSLv3_server_data,(char *)sslv3_base_method(),
  97. sizeof(SSL_METHOD));
  98. SSLv3_server_data.ssl_accept=ssl3_accept;
  99. SSLv3_server_data.get_ssl_method=ssl3_get_server_method;
  100. init=0;
  101. }
  102. return(&SSLv3_server_data);
  103. }
  104. int ssl3_accept(SSL *s)
  105. {
  106. BUF_MEM *buf;
  107. unsigned long l,Time=time(NULL);
  108. void (*cb)()=NULL;
  109. long num1;
  110. int ret= -1;
  111. int new_state,state,skip=0;
  112. RAND_add(&Time,sizeof(Time),0);
  113. ERR_clear_error();
  114. clear_sys_error();
  115. if (s->info_callback != NULL)
  116. cb=s->info_callback;
  117. else if (s->ctx->info_callback != NULL)
  118. cb=s->ctx->info_callback;
  119. /* init things to blank */
  120. if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
  121. s->in_handshake++;
  122. if (s->cert == NULL)
  123. {
  124. SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_NO_CERTIFICATE_SET);
  125. return(-1);
  126. }
  127. for (;;)
  128. {
  129. state=s->state;
  130. switch (s->state)
  131. {
  132. case SSL_ST_RENEGOTIATE:
  133. s->new_session=1;
  134. /* s->state=SSL_ST_ACCEPT; */
  135. case SSL_ST_BEFORE:
  136. case SSL_ST_ACCEPT:
  137. case SSL_ST_BEFORE|SSL_ST_ACCEPT:
  138. case SSL_ST_OK|SSL_ST_ACCEPT:
  139. s->server=1;
  140. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
  141. if ((s->version>>8) != 3)
  142. {
  143. SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_INTERNAL_ERROR);
  144. return -1;
  145. }
  146. s->type=SSL_ST_ACCEPT;
  147. if (s->init_buf == NULL)
  148. {
  149. if ((buf=BUF_MEM_new()) == NULL)
  150. {
  151. ret= -1;
  152. goto end;
  153. }
  154. if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
  155. {
  156. ret= -1;
  157. goto end;
  158. }
  159. s->init_buf=buf;
  160. }
  161. if (!ssl3_setup_buffers(s))
  162. {
  163. ret= -1;
  164. goto end;
  165. }
  166. /* Ok, we now need to push on a buffering BIO so that
  167. * the output is sent in a way that TCP likes :-)
  168. */
  169. if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; }
  170. s->init_num=0;
  171. if (s->state != SSL_ST_RENEGOTIATE)
  172. {
  173. ssl3_init_finished_mac(s);
  174. s->state=SSL3_ST_SR_CLNT_HELLO_A;
  175. s->ctx->stats.sess_accept++;
  176. }
  177. else
  178. {
  179. s->ctx->stats.sess_accept_renegotiate++;
  180. s->state=SSL3_ST_SW_HELLO_REQ_A;
  181. }
  182. break;
  183. case SSL3_ST_SW_HELLO_REQ_A:
  184. case SSL3_ST_SW_HELLO_REQ_B:
  185. s->shutdown=0;
  186. ret=ssl3_send_hello_request(s);
  187. if (ret <= 0) goto end;
  188. s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C;
  189. s->state=SSL3_ST_SW_FLUSH;
  190. s->init_num=0;
  191. ssl3_init_finished_mac(s);
  192. break;
  193. case SSL3_ST_SW_HELLO_REQ_C:
  194. s->state=SSL_ST_OK;
  195. ret=1;
  196. goto end;
  197. /* break; */
  198. case SSL3_ST_SR_CLNT_HELLO_A:
  199. case SSL3_ST_SR_CLNT_HELLO_B:
  200. case SSL3_ST_SR_CLNT_HELLO_C:
  201. s->shutdown=0;
  202. ret=ssl3_get_client_hello(s);
  203. if (ret <= 0) goto end;
  204. s->state=SSL3_ST_SW_SRVR_HELLO_A;
  205. s->init_num=0;
  206. break;
  207. case SSL3_ST_SW_SRVR_HELLO_A:
  208. case SSL3_ST_SW_SRVR_HELLO_B:
  209. ret=ssl3_send_server_hello(s);
  210. if (ret <= 0) goto end;
  211. if (s->hit)
  212. s->state=SSL3_ST_SW_CHANGE_A;
  213. else
  214. s->state=SSL3_ST_SW_CERT_A;
  215. s->init_num=0;
  216. break;
  217. case SSL3_ST_SW_CERT_A:
  218. case SSL3_ST_SW_CERT_B:
  219. /* Check if it is anon DH */
  220. if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
  221. {
  222. ret=ssl3_send_server_certificate(s);
  223. if (ret <= 0) goto end;
  224. }
  225. else
  226. skip=1;
  227. s->state=SSL3_ST_SW_KEY_EXCH_A;
  228. s->init_num=0;
  229. break;
  230. case SSL3_ST_SW_KEY_EXCH_A:
  231. case SSL3_ST_SW_KEY_EXCH_B:
  232. l=s->s3->tmp.new_cipher->algorithms;
  233. /* clear this, it may get reset by
  234. * send_server_key_exchange */
  235. if ((s->options & SSL_OP_EPHEMERAL_RSA)
  236. #ifndef OPENSSL_NO_KRB5
  237. && !(l & SSL_KRB5)
  238. #endif /* OPENSSL_NO_KRB5 */
  239. )
  240. s->s3->tmp.use_rsa_tmp=1;
  241. else
  242. s->s3->tmp.use_rsa_tmp=0;
  243. /* only send if a DH key exchange, fortezza or
  244. * RSA but we have a sign only certificate */
  245. if (s->s3->tmp.use_rsa_tmp
  246. || (l & (SSL_DH|SSL_kFZA))
  247. || ((l & SSL_kRSA)
  248. && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
  249. || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
  250. && EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
  251. )
  252. )
  253. )
  254. )
  255. {
  256. ret=ssl3_send_server_key_exchange(s);
  257. if (ret <= 0) goto end;
  258. }
  259. else
  260. skip=1;
  261. s->state=SSL3_ST_SW_CERT_REQ_A;
  262. s->init_num=0;
  263. break;
  264. case SSL3_ST_SW_CERT_REQ_A:
  265. case SSL3_ST_SW_CERT_REQ_B:
  266. if (/* don't request cert unless asked for it: */
  267. !(s->verify_mode & SSL_VERIFY_PEER) ||
  268. /* if SSL_VERIFY_CLIENT_ONCE is set,
  269. * don't request cert during re-negotiation: */
  270. ((s->session->peer != NULL) &&
  271. (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
  272. /* never request cert in anonymous ciphersuites
  273. * (see section "Certificate request" in SSL 3 drafts
  274. * and in RFC 2246): */
  275. ((s->s3->tmp.new_cipher->algorithms & SSL_aNULL) &&
  276. /* ... except when the application insists on verification
  277. * (against the specs, but s3_clnt.c accepts this for SSL 3) */
  278. !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)))
  279. {
  280. /* no cert request */
  281. skip=1;
  282. s->s3->tmp.cert_request=0;
  283. s->state=SSL3_ST_SW_SRVR_DONE_A;
  284. }
  285. else
  286. {
  287. s->s3->tmp.cert_request=1;
  288. ret=ssl3_send_certificate_request(s);
  289. if (ret <= 0) goto end;
  290. #ifndef NETSCAPE_HANG_BUG
  291. s->state=SSL3_ST_SW_SRVR_DONE_A;
  292. #else
  293. s->state=SSL3_ST_SW_FLUSH;
  294. s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
  295. #endif
  296. s->init_num=0;
  297. }
  298. break;
  299. case SSL3_ST_SW_SRVR_DONE_A:
  300. case SSL3_ST_SW_SRVR_DONE_B:
  301. ret=ssl3_send_server_done(s);
  302. if (ret <= 0) goto end;
  303. s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
  304. s->state=SSL3_ST_SW_FLUSH;
  305. s->init_num=0;
  306. break;
  307. case SSL3_ST_SW_FLUSH:
  308. /* number of bytes to be flushed */
  309. num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
  310. if (num1 > 0)
  311. {
  312. s->rwstate=SSL_WRITING;
  313. num1=BIO_flush(s->wbio);
  314. if (num1 <= 0) { ret= -1; goto end; }
  315. s->rwstate=SSL_NOTHING;
  316. }
  317. s->state=s->s3->tmp.next_state;
  318. break;
  319. case SSL3_ST_SR_CERT_A:
  320. case SSL3_ST_SR_CERT_B:
  321. /* Check for second client hello (MS SGC) */
  322. ret = ssl3_check_client_hello(s);
  323. if (ret <= 0)
  324. goto end;
  325. if (ret == 2)
  326. s->state = SSL3_ST_SR_CLNT_HELLO_C;
  327. else {
  328. /* could be sent for a DH cert, even if we
  329. * have not asked for it :-) */
  330. ret=ssl3_get_client_certificate(s);
  331. if (ret <= 0) goto end;
  332. s->init_num=0;
  333. s->state=SSL3_ST_SR_KEY_EXCH_A;
  334. }
  335. break;
  336. case SSL3_ST_SR_KEY_EXCH_A:
  337. case SSL3_ST_SR_KEY_EXCH_B:
  338. ret=ssl3_get_client_key_exchange(s);
  339. if (ret <= 0) goto end;
  340. s->state=SSL3_ST_SR_CERT_VRFY_A;
  341. s->init_num=0;
  342. /* We need to get hashes here so if there is
  343. * a client cert, it can be verified */
  344. s->method->ssl3_enc->cert_verify_mac(s,
  345. &(s->s3->finish_dgst1),
  346. &(s->s3->tmp.cert_verify_md[0]));
  347. s->method->ssl3_enc->cert_verify_mac(s,
  348. &(s->s3->finish_dgst2),
  349. &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]));
  350. break;
  351. case SSL3_ST_SR_CERT_VRFY_A:
  352. case SSL3_ST_SR_CERT_VRFY_B:
  353. /* we should decide if we expected this one */
  354. ret=ssl3_get_cert_verify(s);
  355. if (ret <= 0) goto end;
  356. s->state=SSL3_ST_SR_FINISHED_A;
  357. s->init_num=0;
  358. break;
  359. case SSL3_ST_SR_FINISHED_A:
  360. case SSL3_ST_SR_FINISHED_B:
  361. ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A,
  362. SSL3_ST_SR_FINISHED_B);
  363. if (ret <= 0) goto end;
  364. if (s->hit)
  365. s->state=SSL_ST_OK;
  366. else
  367. s->state=SSL3_ST_SW_CHANGE_A;
  368. s->init_num=0;
  369. break;
  370. case SSL3_ST_SW_CHANGE_A:
  371. case SSL3_ST_SW_CHANGE_B:
  372. s->session->cipher=s->s3->tmp.new_cipher;
  373. if (!s->method->ssl3_enc->setup_key_block(s))
  374. { ret= -1; goto end; }
  375. ret=ssl3_send_change_cipher_spec(s,
  376. SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B);
  377. if (ret <= 0) goto end;
  378. s->state=SSL3_ST_SW_FINISHED_A;
  379. s->init_num=0;
  380. if (!s->method->ssl3_enc->change_cipher_state(s,
  381. SSL3_CHANGE_CIPHER_SERVER_WRITE))
  382. {
  383. ret= -1;
  384. goto end;
  385. }
  386. break;
  387. case SSL3_ST_SW_FINISHED_A:
  388. case SSL3_ST_SW_FINISHED_B:
  389. ret=ssl3_send_finished(s,
  390. SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B,
  391. s->method->ssl3_enc->server_finished_label,
  392. s->method->ssl3_enc->server_finished_label_len);
  393. if (ret <= 0) goto end;
  394. s->state=SSL3_ST_SW_FLUSH;
  395. if (s->hit)
  396. s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A;
  397. else
  398. s->s3->tmp.next_state=SSL_ST_OK;
  399. s->init_num=0;
  400. break;
  401. case SSL_ST_OK:
  402. /* clean a few things up */
  403. ssl3_cleanup_key_block(s);
  404. BUF_MEM_free(s->init_buf);
  405. s->init_buf=NULL;
  406. /* remove buffering on output */
  407. ssl_free_wbio_buffer(s);
  408. s->new_session=0;
  409. s->init_num=0;
  410. ssl_update_cache(s,SSL_SESS_CACHE_SERVER);
  411. s->ctx->stats.sess_accept_good++;
  412. /* s->server=1; */
  413. s->handshake_func=ssl3_accept;
  414. ret=1;
  415. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
  416. goto end;
  417. /* break; */
  418. default:
  419. SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_UNKNOWN_STATE);
  420. ret= -1;
  421. goto end;
  422. /* break; */
  423. }
  424. if (!s->s3->tmp.reuse_message && !skip)
  425. {
  426. if (s->debug)
  427. {
  428. if ((ret=BIO_flush(s->wbio)) <= 0)
  429. goto end;
  430. }
  431. if ((cb != NULL) && (s->state != state))
  432. {
  433. new_state=s->state;
  434. s->state=state;
  435. cb(s,SSL_CB_ACCEPT_LOOP,1);
  436. s->state=new_state;
  437. }
  438. }
  439. skip=0;
  440. }
  441. end:
  442. /* BIO_flush(s->wbio); */
  443. if (cb != NULL)
  444. cb(s,SSL_CB_ACCEPT_EXIT,ret);
  445. s->in_handshake--;
  446. return(ret);
  447. }
  448. static int ssl3_send_hello_request(SSL *s)
  449. {
  450. unsigned char *p;
  451. if (s->state == SSL3_ST_SW_HELLO_REQ_A)
  452. {
  453. p=(unsigned char *)s->init_buf->data;
  454. *(p++)=SSL3_MT_HELLO_REQUEST;
  455. *(p++)=0;
  456. *(p++)=0;
  457. *(p++)=0;
  458. s->state=SSL3_ST_SW_HELLO_REQ_B;
  459. /* number of bytes to write */
  460. s->init_num=4;
  461. s->init_off=0;
  462. }
  463. /* SSL3_ST_SW_HELLO_REQ_B */
  464. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  465. }
  466. static int ssl3_check_client_hello(SSL *s)
  467. {
  468. int ok;
  469. long n;
  470. n=ssl3_get_message(s,
  471. SSL3_ST_SR_CERT_A,
  472. SSL3_ST_SR_CERT_B,
  473. -1,
  474. SSL3_RT_MAX_PLAIN_LENGTH,
  475. &ok);
  476. if (!ok) return((int)n);
  477. s->s3->tmp.reuse_message = 1;
  478. if (s->s3->tmp.message_type == SSL3_MT_CLIENT_HELLO)
  479. {
  480. /* Throw away what we have done so far in the current handshake,
  481. * which will now be aborted. (A full SSL_clear would be too much.)
  482. * I hope that tmp.dh is the only thing that may need to be cleared
  483. * when a handshake is not completed ... */
  484. #ifndef OPENSSL_NO_DH
  485. if (s->s3->tmp.dh != NULL)
  486. {
  487. DH_free(s->s3->tmp.dh);
  488. s->s3->tmp.dh = NULL;
  489. }
  490. #endif
  491. return 2;
  492. }
  493. return 1;
  494. }
  495. static int ssl3_get_client_hello(SSL *s)
  496. {
  497. int i,j,ok,al,ret= -1;
  498. long n;
  499. unsigned long id;
  500. unsigned char *p,*d,*q;
  501. SSL_CIPHER *c;
  502. SSL_COMP *comp=NULL;
  503. STACK_OF(SSL_CIPHER) *ciphers=NULL;
  504. /* We do this so that we will respond with our native type.
  505. * If we are TLSv1 and we get SSLv3, we will respond with TLSv1,
  506. * This down switching should be handled by a different method.
  507. * If we are SSLv3, we will respond with SSLv3, even if prompted with
  508. * TLSv1.
  509. */
  510. if (s->state == SSL3_ST_SR_CLNT_HELLO_A)
  511. {
  512. s->first_packet=1;
  513. s->state=SSL3_ST_SR_CLNT_HELLO_B;
  514. }
  515. n=ssl3_get_message(s,
  516. SSL3_ST_SR_CLNT_HELLO_B,
  517. SSL3_ST_SR_CLNT_HELLO_C,
  518. SSL3_MT_CLIENT_HELLO,
  519. SSL3_RT_MAX_PLAIN_LENGTH,
  520. &ok);
  521. if (!ok) return((int)n);
  522. d=p=(unsigned char *)s->init_buf->data;
  523. /* use version from inside client hello, not from record header
  524. * (may differ: see RFC 2246, Appendix E, second paragraph) */
  525. s->client_version=(((int)p[0])<<8)|(int)p[1];
  526. p+=2;
  527. /* load the client random */
  528. memcpy(s->s3->client_random,p,SSL3_RANDOM_SIZE);
  529. p+=SSL3_RANDOM_SIZE;
  530. /* get the session-id */
  531. j= *(p++);
  532. s->hit=0;
  533. if (j == 0)
  534. {
  535. if (!ssl_get_new_session(s,1))
  536. goto err;
  537. }
  538. else
  539. {
  540. i=ssl_get_prev_session(s,p,j);
  541. if (i == 1)
  542. { /* previous session */
  543. s->hit=1;
  544. }
  545. else if (i == -1)
  546. goto err;
  547. else /* i == 0 */
  548. {
  549. if (!ssl_get_new_session(s,1))
  550. goto err;
  551. }
  552. }
  553. p+=j;
  554. n2s(p,i);
  555. if ((i == 0) && (j != 0))
  556. {
  557. /* we need a cipher if we are not resuming a session */
  558. al=SSL_AD_ILLEGAL_PARAMETER;
  559. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_SPECIFIED);
  560. goto f_err;
  561. }
  562. if ((i+p) > (d+n))
  563. {
  564. /* not enough data */
  565. al=SSL_AD_DECODE_ERROR;
  566. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
  567. goto f_err;
  568. }
  569. if ((i > 0) && (ssl_bytes_to_cipher_list(s,p,i,&(ciphers))
  570. == NULL))
  571. {
  572. goto err;
  573. }
  574. p+=i;
  575. /* If it is a hit, check that the cipher is in the list */
  576. if ((s->hit) && (i > 0))
  577. {
  578. j=0;
  579. id=s->session->cipher->id;
  580. #ifdef CIPHER_DEBUG
  581. printf("client sent %d ciphers\n",sk_num(ciphers));
  582. #endif
  583. for (i=0; i<sk_SSL_CIPHER_num(ciphers); i++)
  584. {
  585. c=sk_SSL_CIPHER_value(ciphers,i);
  586. #ifdef CIPHER_DEBUG
  587. printf("client [%2d of %2d]:%s\n",
  588. i,sk_num(ciphers),SSL_CIPHER_get_name(c));
  589. #endif
  590. if (c->id == id)
  591. {
  592. j=1;
  593. break;
  594. }
  595. }
  596. if (j == 0)
  597. {
  598. if ((s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG) && (sk_SSL_CIPHER_num(ciphers) == 1))
  599. {
  600. /* Very bad for multi-threading.... */
  601. s->session->cipher=sk_SSL_CIPHER_value(ciphers,
  602. 0);
  603. }
  604. else
  605. {
  606. /* we need to have the cipher in the cipher
  607. * list if we are asked to reuse it */
  608. al=SSL_AD_ILLEGAL_PARAMETER;
  609. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_REQUIRED_CIPHER_MISSING);
  610. goto f_err;
  611. }
  612. }
  613. }
  614. /* compression */
  615. i= *(p++);
  616. q=p;
  617. for (j=0; j<i; j++)
  618. {
  619. if (p[j] == 0) break;
  620. }
  621. p+=i;
  622. if (j >= i)
  623. {
  624. /* no compress */
  625. al=SSL_AD_DECODE_ERROR;
  626. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_COMPRESSION_SPECIFIED);
  627. goto f_err;
  628. }
  629. /* Worst case, we will use the NULL compression, but if we have other
  630. * options, we will now look for them. We have i-1 compression
  631. * algorithms from the client, starting at q. */
  632. s->s3->tmp.new_compression=NULL;
  633. if (s->ctx->comp_methods != NULL)
  634. { /* See if we have a match */
  635. int m,nn,o,v,done=0;
  636. nn=sk_SSL_COMP_num(s->ctx->comp_methods);
  637. for (m=0; m<nn; m++)
  638. {
  639. comp=sk_SSL_COMP_value(s->ctx->comp_methods,m);
  640. v=comp->id;
  641. for (o=0; o<i; o++)
  642. {
  643. if (v == q[o])
  644. {
  645. done=1;
  646. break;
  647. }
  648. }
  649. if (done) break;
  650. }
  651. if (done)
  652. s->s3->tmp.new_compression=comp;
  653. else
  654. comp=NULL;
  655. }
  656. /* TLS does not mind if there is extra stuff */
  657. if (s->version == SSL3_VERSION)
  658. {
  659. if (p > (d+n))
  660. {
  661. /* wrong number of bytes,
  662. * there could be more to follow */
  663. al=SSL_AD_DECODE_ERROR;
  664. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
  665. goto f_err;
  666. }
  667. }
  668. /* Given s->session->ciphers and SSL_get_ciphers, we must
  669. * pick a cipher */
  670. if (!s->hit)
  671. {
  672. s->session->compress_meth=(comp == NULL)?0:comp->id;
  673. if (s->session->ciphers != NULL)
  674. sk_SSL_CIPHER_free(s->session->ciphers);
  675. s->session->ciphers=ciphers;
  676. if (ciphers == NULL)
  677. {
  678. al=SSL_AD_ILLEGAL_PARAMETER;
  679. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_PASSED);
  680. goto f_err;
  681. }
  682. ciphers=NULL;
  683. c=ssl3_choose_cipher(s,s->session->ciphers,
  684. SSL_get_ciphers(s));
  685. if (c == NULL)
  686. {
  687. al=SSL_AD_HANDSHAKE_FAILURE;
  688. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
  689. goto f_err;
  690. }
  691. s->s3->tmp.new_cipher=c;
  692. }
  693. else
  694. {
  695. /* Session-id reuse */
  696. #ifdef REUSE_CIPHER_BUG
  697. STACK_OF(SSL_CIPHER) *sk;
  698. SSL_CIPHER *nc=NULL;
  699. SSL_CIPHER *ec=NULL;
  700. if (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG)
  701. {
  702. sk=s->session->ciphers;
  703. for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
  704. {
  705. c=sk_SSL_CIPHER_value(sk,i);
  706. if (c->algorithms & SSL_eNULL)
  707. nc=c;
  708. if (SSL_C_IS_EXPORT(c))
  709. ec=c;
  710. }
  711. if (nc != NULL)
  712. s->s3->tmp.new_cipher=nc;
  713. else if (ec != NULL)
  714. s->s3->tmp.new_cipher=ec;
  715. else
  716. s->s3->tmp.new_cipher=s->session->cipher;
  717. }
  718. else
  719. #endif
  720. s->s3->tmp.new_cipher=s->session->cipher;
  721. }
  722. /* we now have the following setup.
  723. * client_random
  724. * cipher_list - our prefered list of ciphers
  725. * ciphers - the clients prefered list of ciphers
  726. * compression - basically ignored right now
  727. * ssl version is set - sslv3
  728. * s->session - The ssl session has been setup.
  729. * s->hit - session reuse flag
  730. * s->tmp.new_cipher - the new cipher to use.
  731. */
  732. ret=1;
  733. if (0)
  734. {
  735. f_err:
  736. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  737. }
  738. err:
  739. if (ciphers != NULL) sk_SSL_CIPHER_free(ciphers);
  740. return(ret);
  741. }
  742. static int ssl3_send_server_hello(SSL *s)
  743. {
  744. unsigned char *buf;
  745. unsigned char *p,*d;
  746. int i,sl;
  747. unsigned long l,Time;
  748. if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
  749. {
  750. buf=(unsigned char *)s->init_buf->data;
  751. p=s->s3->server_random;
  752. Time=time(NULL); /* Time */
  753. l2n(Time,p);
  754. RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-sizeof(Time));
  755. /* Do the message type and length last */
  756. d=p= &(buf[4]);
  757. *(p++)=s->version>>8;
  758. *(p++)=s->version&0xff;
  759. /* Random stuff */
  760. memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
  761. p+=SSL3_RANDOM_SIZE;
  762. /* now in theory we have 3 options to sending back the
  763. * session id. If it is a re-use, we send back the
  764. * old session-id, if it is a new session, we send
  765. * back the new session-id or we send back a 0 length
  766. * session-id if we want it to be single use.
  767. * Currently I will not implement the '0' length session-id
  768. * 12-Jan-98 - I'll now support the '0' length stuff.
  769. */
  770. if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER))
  771. s->session->session_id_length=0;
  772. sl=s->session->session_id_length;
  773. *(p++)=sl;
  774. memcpy(p,s->session->session_id,sl);
  775. p+=sl;
  776. /* put the cipher */
  777. i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p);
  778. p+=i;
  779. /* put the compression method */
  780. if (s->s3->tmp.new_compression == NULL)
  781. *(p++)=0;
  782. else
  783. *(p++)=s->s3->tmp.new_compression->id;
  784. /* do the header */
  785. l=(p-d);
  786. d=buf;
  787. *(d++)=SSL3_MT_SERVER_HELLO;
  788. l2n3(l,d);
  789. s->state=SSL3_ST_CW_CLNT_HELLO_B;
  790. /* number of bytes to write */
  791. s->init_num=p-buf;
  792. s->init_off=0;
  793. }
  794. /* SSL3_ST_CW_CLNT_HELLO_B */
  795. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  796. }
  797. static int ssl3_send_server_done(SSL *s)
  798. {
  799. unsigned char *p;
  800. if (s->state == SSL3_ST_SW_SRVR_DONE_A)
  801. {
  802. p=(unsigned char *)s->init_buf->data;
  803. /* do the header */
  804. *(p++)=SSL3_MT_SERVER_DONE;
  805. *(p++)=0;
  806. *(p++)=0;
  807. *(p++)=0;
  808. s->state=SSL3_ST_SW_SRVR_DONE_B;
  809. /* number of bytes to write */
  810. s->init_num=4;
  811. s->init_off=0;
  812. }
  813. /* SSL3_ST_CW_CLNT_HELLO_B */
  814. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  815. }
  816. static int ssl3_send_server_key_exchange(SSL *s)
  817. {
  818. #ifndef OPENSSL_NO_RSA
  819. unsigned char *q;
  820. int j,num;
  821. RSA *rsa;
  822. unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
  823. unsigned int u;
  824. #endif
  825. #ifndef OPENSSL_NO_DH
  826. DH *dh=NULL,*dhp;
  827. #endif
  828. EVP_PKEY *pkey;
  829. unsigned char *p,*d;
  830. int al,i;
  831. unsigned long type;
  832. int n;
  833. CERT *cert;
  834. BIGNUM *r[4];
  835. int nr[4],kn;
  836. BUF_MEM *buf;
  837. EVP_MD_CTX md_ctx;
  838. if (s->state == SSL3_ST_SW_KEY_EXCH_A)
  839. {
  840. type=s->s3->tmp.new_cipher->algorithms & SSL_MKEY_MASK;
  841. cert=s->cert;
  842. buf=s->init_buf;
  843. r[0]=r[1]=r[2]=r[3]=NULL;
  844. n=0;
  845. #ifndef OPENSSL_NO_RSA
  846. if (type & SSL_kRSA)
  847. {
  848. rsa=cert->rsa_tmp;
  849. if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL))
  850. {
  851. rsa=s->cert->rsa_tmp_cb(s,
  852. SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
  853. SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
  854. if(rsa == NULL)
  855. {
  856. al=SSL_AD_HANDSHAKE_FAILURE;
  857. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
  858. goto f_err;
  859. }
  860. CRYPTO_add(&rsa->references,1,CRYPTO_LOCK_RSA);
  861. cert->rsa_tmp=rsa;
  862. }
  863. if (rsa == NULL)
  864. {
  865. al=SSL_AD_HANDSHAKE_FAILURE;
  866. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_KEY);
  867. goto f_err;
  868. }
  869. r[0]=rsa->n;
  870. r[1]=rsa->e;
  871. s->s3->tmp.use_rsa_tmp=1;
  872. }
  873. else
  874. #endif
  875. #ifndef OPENSSL_NO_DH
  876. if (type & SSL_kEDH)
  877. {
  878. dhp=cert->dh_tmp;
  879. if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
  880. dhp=s->cert->dh_tmp_cb(s,
  881. SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
  882. SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
  883. if (dhp == NULL)
  884. {
  885. al=SSL_AD_HANDSHAKE_FAILURE;
  886. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
  887. goto f_err;
  888. }
  889. if (s->s3->tmp.dh != NULL)
  890. {
  891. DH_free(dh);
  892. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, SSL_R_INTERNAL_ERROR);
  893. goto err;
  894. }
  895. if ((dh=DHparams_dup(dhp)) == NULL)
  896. {
  897. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
  898. goto err;
  899. }
  900. s->s3->tmp.dh=dh;
  901. if ((dhp->pub_key == NULL ||
  902. dhp->priv_key == NULL ||
  903. (s->options & SSL_OP_SINGLE_DH_USE)))
  904. {
  905. if(!DH_generate_key(dh))
  906. {
  907. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
  908. ERR_R_DH_LIB);
  909. goto err;
  910. }
  911. }
  912. else
  913. {
  914. dh->pub_key=BN_dup(dhp->pub_key);
  915. dh->priv_key=BN_dup(dhp->priv_key);
  916. if ((dh->pub_key == NULL) ||
  917. (dh->priv_key == NULL))
  918. {
  919. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
  920. goto err;
  921. }
  922. }
  923. r[0]=dh->p;
  924. r[1]=dh->g;
  925. r[2]=dh->pub_key;
  926. }
  927. else
  928. #endif
  929. {
  930. al=SSL_AD_HANDSHAKE_FAILURE;
  931. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
  932. goto f_err;
  933. }
  934. for (i=0; r[i] != NULL; i++)
  935. {
  936. nr[i]=BN_num_bytes(r[i]);
  937. n+=2+nr[i];
  938. }
  939. if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
  940. {
  941. if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher))
  942. == NULL)
  943. {
  944. al=SSL_AD_DECODE_ERROR;
  945. goto f_err;
  946. }
  947. kn=EVP_PKEY_size(pkey);
  948. }
  949. else
  950. {
  951. pkey=NULL;
  952. kn=0;
  953. }
  954. if (!BUF_MEM_grow(buf,n+4+kn))
  955. {
  956. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF);
  957. goto err;
  958. }
  959. d=(unsigned char *)s->init_buf->data;
  960. p= &(d[4]);
  961. for (i=0; r[i] != NULL; i++)
  962. {
  963. s2n(nr[i],p);
  964. BN_bn2bin(r[i],p);
  965. p+=nr[i];
  966. }
  967. /* not anonymous */
  968. if (pkey != NULL)
  969. {
  970. /* n is the length of the params, they start at &(d[4])
  971. * and p points to the space at the end. */
  972. #ifndef OPENSSL_NO_RSA
  973. if (pkey->type == EVP_PKEY_RSA)
  974. {
  975. q=md_buf;
  976. j=0;
  977. for (num=2; num > 0; num--)
  978. {
  979. EVP_DigestInit(&md_ctx,(num == 2)
  980. ?s->ctx->md5:s->ctx->sha1);
  981. EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  982. EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  983. EVP_DigestUpdate(&md_ctx,&(d[4]),n);
  984. EVP_DigestFinal(&md_ctx,q,
  985. (unsigned int *)&i);
  986. q+=i;
  987. j+=i;
  988. }
  989. if (RSA_sign(NID_md5_sha1, md_buf, j,
  990. &(p[2]), &u, pkey->pkey.rsa) <= 0)
  991. {
  992. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_RSA);
  993. goto err;
  994. }
  995. s2n(u,p);
  996. n+=u+2;
  997. }
  998. else
  999. #endif
  1000. #if !defined(OPENSSL_NO_DSA)
  1001. if (pkey->type == EVP_PKEY_DSA)
  1002. {
  1003. /* lets do DSS */
  1004. EVP_SignInit(&md_ctx,EVP_dss1());
  1005. EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1006. EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1007. EVP_SignUpdate(&md_ctx,&(d[4]),n);
  1008. if (!EVP_SignFinal(&md_ctx,&(p[2]),
  1009. (unsigned int *)&i,pkey))
  1010. {
  1011. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_DSA);
  1012. goto err;
  1013. }
  1014. s2n(i,p);
  1015. n+=i+2;
  1016. }
  1017. else
  1018. #endif
  1019. {
  1020. /* Is this error check actually needed? */
  1021. al=SSL_AD_HANDSHAKE_FAILURE;
  1022. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_PKEY_TYPE);
  1023. goto f_err;
  1024. }
  1025. }
  1026. *(d++)=SSL3_MT_SERVER_KEY_EXCHANGE;
  1027. l2n3(n,d);
  1028. /* we should now have things packed up, so lets send
  1029. * it off */
  1030. s->init_num=n+4;
  1031. s->init_off=0;
  1032. }
  1033. s->state = SSL3_ST_SW_KEY_EXCH_B;
  1034. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  1035. f_err:
  1036. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1037. err:
  1038. return(-1);
  1039. }
  1040. static int ssl3_send_certificate_request(SSL *s)
  1041. {
  1042. unsigned char *p,*d;
  1043. int i,j,nl,off,n;
  1044. STACK_OF(X509_NAME) *sk=NULL;
  1045. X509_NAME *name;
  1046. BUF_MEM *buf;
  1047. if (s->state == SSL3_ST_SW_CERT_REQ_A)
  1048. {
  1049. buf=s->init_buf;
  1050. d=p=(unsigned char *)&(buf->data[4]);
  1051. /* get the list of acceptable cert types */
  1052. p++;
  1053. n=ssl3_get_req_cert_type(s,p);
  1054. d[0]=n;
  1055. p+=n;
  1056. n++;
  1057. off=n;
  1058. p+=2;
  1059. n+=2;
  1060. sk=SSL_get_client_CA_list(s);
  1061. nl=0;
  1062. if (sk != NULL)
  1063. {
  1064. for (i=0; i<sk_X509_NAME_num(sk); i++)
  1065. {
  1066. name=sk_X509_NAME_value(sk,i);
  1067. j=i2d_X509_NAME(name,NULL);
  1068. if (!BUF_MEM_grow(buf,4+n+j+2))
  1069. {
  1070. SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,ERR_R_BUF_LIB);
  1071. goto err;
  1072. }
  1073. p=(unsigned char *)&(buf->data[4+n]);
  1074. if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
  1075. {
  1076. s2n(j,p);
  1077. i2d_X509_NAME(name,&p);
  1078. n+=2+j;
  1079. nl+=2+j;
  1080. }
  1081. else
  1082. {
  1083. d=p;
  1084. i2d_X509_NAME(name,&p);
  1085. j-=2; s2n(j,d); j+=2;
  1086. n+=j;
  1087. nl+=j;
  1088. }
  1089. }
  1090. }
  1091. /* else no CA names */
  1092. p=(unsigned char *)&(buf->data[4+off]);
  1093. s2n(nl,p);
  1094. d=(unsigned char *)buf->data;
  1095. *(d++)=SSL3_MT_CERTIFICATE_REQUEST;
  1096. l2n3(n,d);
  1097. /* we should now have things packed up, so lets send
  1098. * it off */
  1099. s->init_num=n+4;
  1100. s->init_off=0;
  1101. #ifdef NETSCAPE_HANG_BUG
  1102. p=(unsigned char *)s->init_buf->data + s->init_num;
  1103. /* do the header */
  1104. *(p++)=SSL3_MT_SERVER_DONE;
  1105. *(p++)=0;
  1106. *(p++)=0;
  1107. *(p++)=0;
  1108. s->init_num += 4;
  1109. #endif
  1110. }
  1111. /* SSL3_ST_SW_CERT_REQ_B */
  1112. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  1113. err:
  1114. return(-1);
  1115. }
  1116. static int ssl3_get_client_key_exchange(SSL *s)
  1117. {
  1118. int i,al,ok;
  1119. long n;
  1120. unsigned long l;
  1121. unsigned char *p;
  1122. #ifndef OPENSSL_NO_RSA
  1123. RSA *rsa=NULL;
  1124. EVP_PKEY *pkey=NULL;
  1125. #endif
  1126. #ifndef OPENSSL_NO_DH
  1127. BIGNUM *pub=NULL;
  1128. DH *dh_srvr;
  1129. #endif
  1130. #ifndef OPENSSL_NO_KRB5
  1131. KSSL_ERR kssl_err;
  1132. #endif /* OPENSSL_NO_KRB5 */
  1133. n=ssl3_get_message(s,
  1134. SSL3_ST_SR_KEY_EXCH_A,
  1135. SSL3_ST_SR_KEY_EXCH_B,
  1136. SSL3_MT_CLIENT_KEY_EXCHANGE,
  1137. 2048, /* ??? */
  1138. &ok);
  1139. if (!ok) return((int)n);
  1140. p=(unsigned char *)s->init_buf->data;
  1141. l=s->s3->tmp.new_cipher->algorithms;
  1142. #ifndef OPENSSL_NO_RSA
  1143. if (l & SSL_kRSA)
  1144. {
  1145. /* FIX THIS UP EAY EAY EAY EAY */
  1146. if (s->s3->tmp.use_rsa_tmp)
  1147. {
  1148. if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))
  1149. rsa=s->cert->rsa_tmp;
  1150. /* Don't do a callback because rsa_tmp should
  1151. * be sent already */
  1152. if (rsa == NULL)
  1153. {
  1154. al=SSL_AD_HANDSHAKE_FAILURE;
  1155. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_PKEY);
  1156. goto f_err;
  1157. }
  1158. }
  1159. else
  1160. {
  1161. pkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
  1162. if ( (pkey == NULL) ||
  1163. (pkey->type != EVP_PKEY_RSA) ||
  1164. (pkey->pkey.rsa == NULL))
  1165. {
  1166. al=SSL_AD_HANDSHAKE_FAILURE;
  1167. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_RSA_CERTIFICATE);
  1168. goto f_err;
  1169. }
  1170. rsa=pkey->pkey.rsa;
  1171. }
  1172. /* TLS */
  1173. if (s->version > SSL3_VERSION)
  1174. {
  1175. n2s(p,i);
  1176. if (n != i+2)
  1177. {
  1178. if (!(s->options & SSL_OP_TLS_D5_BUG))
  1179. {
  1180. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
  1181. goto err;
  1182. }
  1183. else
  1184. p-=2;
  1185. }
  1186. else
  1187. n=i;
  1188. }
  1189. i=RSA_private_decrypt((int)n,p,p,rsa,RSA_PKCS1_PADDING);
  1190. if (i != SSL_MAX_MASTER_KEY_LENGTH)
  1191. {
  1192. al=SSL_AD_DECODE_ERROR;
  1193. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);
  1194. goto f_err;
  1195. }
  1196. if (!((p[0] == (s->client_version>>8)) && (p[1] == (s->client_version & 0xff))))
  1197. {
  1198. /* The premaster secret must contain the same version number as the
  1199. * ClientHello to detect version rollback attacks (strangely, the
  1200. * protocol does not offer such protection for DH ciphersuites).
  1201. * However, buggy clients exist that send the negotiated protocol
  1202. * version instead if the server does not support the requested
  1203. * protocol version.
  1204. * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients. */
  1205. if (!((s->options & SSL_OP_TLS_ROLLBACK_BUG) &&
  1206. (p[0] == (s->version>>8)) && (p[1] == (s->version & 0xff))))
  1207. {
  1208. al=SSL_AD_DECODE_ERROR;
  1209. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_PROTOCOL_VERSION_NUMBER);
  1210. goto f_err;
  1211. }
  1212. }
  1213. s->session->master_key_length=
  1214. s->method->ssl3_enc->generate_master_secret(s,
  1215. s->session->master_key,
  1216. p,i);
  1217. memset(p,0,i);
  1218. }
  1219. else
  1220. #endif
  1221. #ifndef OPENSSL_NO_DH
  1222. if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
  1223. {
  1224. n2s(p,i);
  1225. if (n != i+2)
  1226. {
  1227. if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG))
  1228. {
  1229. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
  1230. goto err;
  1231. }
  1232. else
  1233. {
  1234. p-=2;
  1235. i=(int)n;
  1236. }
  1237. }
  1238. if (n == 0L) /* the parameters are in the cert */
  1239. {
  1240. al=SSL_AD_HANDSHAKE_FAILURE;
  1241. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_DECODE_DH_CERTS);
  1242. goto f_err;
  1243. }
  1244. else
  1245. {
  1246. if (s->s3->tmp.dh == NULL)
  1247. {
  1248. al=SSL_AD_HANDSHAKE_FAILURE;
  1249. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
  1250. goto f_err;
  1251. }
  1252. else
  1253. dh_srvr=s->s3->tmp.dh;
  1254. }
  1255. pub=BN_bin2bn(p,i,NULL);
  1256. if (pub == NULL)
  1257. {
  1258. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BN_LIB);
  1259. goto err;
  1260. }
  1261. i=DH_compute_key(p,pub,dh_srvr);
  1262. if (i <= 0)
  1263. {
  1264. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
  1265. goto err;
  1266. }
  1267. DH_free(s->s3->tmp.dh);
  1268. s->s3->tmp.dh=NULL;
  1269. BN_clear_free(pub);
  1270. pub=NULL;
  1271. s->session->master_key_length=
  1272. s->method->ssl3_enc->generate_master_secret(s,
  1273. s->session->master_key,p,i);
  1274. memset(p,0,i);
  1275. }
  1276. else
  1277. #endif
  1278. #ifndef OPENSSL_NO_KRB5
  1279. if (l & SSL_kKRB5)
  1280. {
  1281. krb5_error_code krb5rc;
  1282. KSSL_CTX *kssl_ctx = s->kssl_ctx;
  1283. if (!kssl_ctx) kssl_ctx = kssl_ctx_new();
  1284. if ((krb5rc = kssl_sget_tkt(kssl_ctx,
  1285. s->init_buf->data, s->init_buf->length,
  1286. &kssl_err)) != 0)
  1287. {
  1288. #ifdef KSSL_DEBUG
  1289. printf("kssl_sget_tkt rtn %d [%d]\n",
  1290. krb5rc, kssl_err.reason);
  1291. if (kssl_err.text)
  1292. printf("kssl_err text= %s\n", kssl_err.text);
  1293. #endif /* KSSL_DEBUG */
  1294. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  1295. kssl_err.reason);
  1296. goto err;
  1297. }
  1298. #ifdef KSSL_DEBUG
  1299. kssl_ctx_show(kssl_ctx);
  1300. #endif /* KSSL_DEBUG */
  1301. /* 19991013 VRS - 3DES is kind of bogus here,
  1302. ** at least until Kerberos supports 3DES. The only
  1303. ** real secret is the 8-byte Kerberos session key;
  1304. ** the other key material (client_random, server_random)
  1305. ** could be sniffed. Nonces may help against replays though.
  1306. **
  1307. ** Alternate code for Kerberos Purists:
  1308. **
  1309. ** memcpy(s->session->master_key, kssl_ctx->key, kssl_ctx->length);
  1310. ** s->session->master_key_length = kssl_ctx->length;
  1311. */
  1312. s->session->master_key_length=
  1313. s->method->ssl3_enc->generate_master_secret(s,
  1314. s->session->master_key, kssl_ctx->key, kssl_ctx->length);
  1315. /* Was doing kssl_ctx_free() here, but it caused problems for apache.
  1316. ** kssl_ctx = kssl_ctx_free(kssl_ctx);
  1317. ** if (s->kssl_ctx) s->kssl_ctx = NULL;
  1318. */
  1319. }
  1320. else
  1321. #endif /* OPENSSL_NO_KRB5 */
  1322. {
  1323. al=SSL_AD_HANDSHAKE_FAILURE;
  1324. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNKNOWN_CIPHER_TYPE);
  1325. goto f_err;
  1326. }
  1327. return(1);
  1328. f_err:
  1329. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1330. #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA)
  1331. err:
  1332. #endif
  1333. return(-1);
  1334. }
  1335. static int ssl3_get_cert_verify(SSL *s)
  1336. {
  1337. EVP_PKEY *pkey=NULL;
  1338. unsigned char *p;
  1339. int al,ok,ret=0;
  1340. long n;
  1341. int type=0,i,j;
  1342. X509 *peer;
  1343. n=ssl3_get_message(s,
  1344. SSL3_ST_SR_CERT_VRFY_A,
  1345. SSL3_ST_SR_CERT_VRFY_B,
  1346. -1,
  1347. 512, /* 512? */
  1348. &ok);
  1349. if (!ok) return((int)n);
  1350. if (s->session->peer != NULL)
  1351. {
  1352. peer=s->session->peer;
  1353. pkey=X509_get_pubkey(peer);
  1354. type=X509_certificate_type(peer,pkey);
  1355. }
  1356. else
  1357. {
  1358. peer=NULL;
  1359. pkey=NULL;
  1360. }
  1361. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY)
  1362. {
  1363. s->s3->tmp.reuse_message=1;
  1364. if ((peer != NULL) && (type | EVP_PKT_SIGN))
  1365. {
  1366. al=SSL_AD_UNEXPECTED_MESSAGE;
  1367. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_MISSING_VERIFY_MESSAGE);
  1368. goto f_err;
  1369. }
  1370. ret=1;
  1371. goto end;
  1372. }
  1373. if (peer == NULL)
  1374. {
  1375. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_NO_CLIENT_CERT_RECEIVED);
  1376. al=SSL_AD_UNEXPECTED_MESSAGE;
  1377. goto f_err;
  1378. }
  1379. if (!(type & EVP_PKT_SIGN))
  1380. {
  1381. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
  1382. al=SSL_AD_ILLEGAL_PARAMETER;
  1383. goto f_err;
  1384. }
  1385. if (s->s3->change_cipher_spec)
  1386. {
  1387. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_CCS_RECEIVED_EARLY);
  1388. al=SSL_AD_UNEXPECTED_MESSAGE;
  1389. goto f_err;
  1390. }
  1391. /* we now have a signature that we need to verify */
  1392. p=(unsigned char *)s->init_buf->data;
  1393. n2s(p,i);
  1394. n-=2;
  1395. if (i > n)
  1396. {
  1397. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_LENGTH_MISMATCH);
  1398. al=SSL_AD_DECODE_ERROR;
  1399. goto f_err;
  1400. }
  1401. j=EVP_PKEY_size(pkey);
  1402. if ((i > j) || (n > j) || (n <= 0))
  1403. {
  1404. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_WRONG_SIGNATURE_SIZE);
  1405. al=SSL_AD_DECODE_ERROR;
  1406. goto f_err;
  1407. }
  1408. #ifndef OPENSSL_NO_RSA
  1409. if (pkey->type == EVP_PKEY_RSA)
  1410. {
  1411. i=RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md,
  1412. MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, p, i,
  1413. pkey->pkey.rsa);
  1414. if (i < 0)
  1415. {
  1416. al=SSL_AD_DECRYPT_ERROR;
  1417. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_DECRYPT);
  1418. goto f_err;
  1419. }
  1420. if (i == 0)
  1421. {
  1422. al=SSL_AD_DECRYPT_ERROR;
  1423. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_SIGNATURE);
  1424. goto f_err;
  1425. }
  1426. }
  1427. else
  1428. #endif
  1429. #ifndef OPENSSL_NO_DSA
  1430. if (pkey->type == EVP_PKEY_DSA)
  1431. {
  1432. j=DSA_verify(pkey->save_type,
  1433. &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
  1434. SHA_DIGEST_LENGTH,p,i,pkey->pkey.dsa);
  1435. if (j <= 0)
  1436. {
  1437. /* bad signature */
  1438. al=SSL_AD_DECRYPT_ERROR;
  1439. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_DSA_SIGNATURE);
  1440. goto f_err;
  1441. }
  1442. }
  1443. else
  1444. #endif
  1445. {
  1446. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_INTERNAL_ERROR);
  1447. al=SSL_AD_UNSUPPORTED_CERTIFICATE;
  1448. goto f_err;
  1449. }
  1450. ret=1;
  1451. if (0)
  1452. {
  1453. f_err:
  1454. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1455. }
  1456. end:
  1457. EVP_PKEY_free(pkey);
  1458. return(ret);
  1459. }
  1460. static int ssl3_get_client_certificate(SSL *s)
  1461. {
  1462. int i,ok,al,ret= -1;
  1463. X509 *x=NULL;
  1464. unsigned long l,nc,llen,n;
  1465. unsigned char *p,*d,*q;
  1466. STACK_OF(X509) *sk=NULL;
  1467. n=ssl3_get_message(s,
  1468. SSL3_ST_SR_CERT_A,
  1469. SSL3_ST_SR_CERT_B,
  1470. -1,
  1471. #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN32)
  1472. 1024*30, /* 30k max cert list :-) */
  1473. #else
  1474. 1024*100, /* 100k max cert list :-) */
  1475. #endif
  1476. &ok);
  1477. if (!ok) return((int)n);
  1478. if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE)
  1479. {
  1480. if ( (s->verify_mode & SSL_VERIFY_PEER) &&
  1481. (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
  1482. {
  1483. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  1484. al=SSL_AD_HANDSHAKE_FAILURE;
  1485. goto f_err;
  1486. }
  1487. /* If tls asked for a client cert, the client must return a 0 list */
  1488. if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request)
  1489. {
  1490. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
  1491. al=SSL_AD_UNEXPECTED_MESSAGE;
  1492. goto f_err;
  1493. }
  1494. s->s3->tmp.reuse_message=1;
  1495. return(1);
  1496. }
  1497. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
  1498. {
  1499. al=SSL_AD_UNEXPECTED_MESSAGE;
  1500. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_WRONG_MESSAGE_TYPE);
  1501. goto f_err;
  1502. }
  1503. d=p=(unsigned char *)s->init_buf->data;
  1504. if ((sk=sk_X509_new_null()) == NULL)
  1505. {
  1506. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
  1507. goto err;
  1508. }
  1509. n2l3(p,llen);
  1510. if (llen+3 != n)
  1511. {
  1512. al=SSL_AD_DECODE_ERROR;
  1513. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
  1514. goto f_err;
  1515. }
  1516. for (nc=0; nc<llen; )
  1517. {
  1518. n2l3(p,l);
  1519. if ((l+nc+3) > llen)
  1520. {
  1521. al=SSL_AD_DECODE_ERROR;
  1522. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
  1523. goto f_err;
  1524. }
  1525. q=p;
  1526. x=d2i_X509(NULL,&p,l);
  1527. if (x == NULL)
  1528. {
  1529. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_ASN1_LIB);
  1530. goto err;
  1531. }
  1532. if (p != (q+l))
  1533. {
  1534. al=SSL_AD_DECODE_ERROR;
  1535. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
  1536. goto f_err;
  1537. }
  1538. if (!sk_X509_push(sk,x))
  1539. {
  1540. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
  1541. goto err;
  1542. }
  1543. x=NULL;
  1544. nc+=l+3;
  1545. }
  1546. if (sk_X509_num(sk) <= 0)
  1547. {
  1548. /* TLS does not mind 0 certs returned */
  1549. if (s->version == SSL3_VERSION)
  1550. {
  1551. al=SSL_AD_HANDSHAKE_FAILURE;
  1552. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATES_RETURNED);
  1553. goto f_err;
  1554. }
  1555. /* Fail for TLS only if we required a certificate */
  1556. else if ((s->verify_mode & SSL_VERIFY_PEER) &&
  1557. (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
  1558. {
  1559. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  1560. al=SSL_AD_HANDSHAKE_FAILURE;
  1561. goto f_err;
  1562. }
  1563. }
  1564. else
  1565. {
  1566. i=ssl_verify_cert_chain(s,sk);
  1567. if (!i)
  1568. {
  1569. al=ssl_verify_alarm_type(s->verify_result);
  1570. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATE_RETURNED);
  1571. goto f_err;
  1572. }
  1573. }
  1574. if (s->session->peer != NULL) /* This should not be needed */
  1575. X509_free(s->session->peer);
  1576. s->session->peer=sk_X509_shift(sk);
  1577. s->session->verify_result = s->verify_result;
  1578. /* With the current implementation, sess_cert will always be NULL
  1579. * when we arrive here. */
  1580. if (s->session->sess_cert == NULL)
  1581. {
  1582. s->session->sess_cert = ssl_sess_cert_new();
  1583. if (s->session->sess_cert == NULL)
  1584. {
  1585. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
  1586. goto err;
  1587. }
  1588. }
  1589. if (s->session->sess_cert->cert_chain != NULL)
  1590. sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
  1591. s->session->sess_cert->cert_chain=sk;
  1592. /* Inconsistency alert: cert_chain does *not* include the
  1593. * peer's own certificate, while we do include it in s3_clnt.c */
  1594. sk=NULL;
  1595. ret=1;
  1596. if (0)
  1597. {
  1598. f_err:
  1599. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1600. }
  1601. err:
  1602. if (x != NULL) X509_free(x);
  1603. if (sk != NULL) sk_X509_pop_free(sk,X509_free);
  1604. return(ret);
  1605. }
  1606. int ssl3_send_server_certificate(SSL *s)
  1607. {
  1608. unsigned long l;
  1609. X509 *x;
  1610. if (s->state == SSL3_ST_SW_CERT_A)
  1611. {
  1612. x=ssl_get_server_send_cert(s);
  1613. if (x == NULL &&
  1614. /* VRS: allow null cert if auth == KRB5 */
  1615. (s->s3->tmp.new_cipher->algorithms
  1616. & (SSL_MKEY_MASK|SSL_AUTH_MASK))
  1617. != (SSL_aKRB5|SSL_kKRB5))
  1618. {
  1619. SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,SSL_R_INTERNAL_ERROR);
  1620. return(0);
  1621. }
  1622. l=ssl3_output_cert_chain(s,x);
  1623. s->state=SSL3_ST_SW_CERT_B;
  1624. s->init_num=(int)l;
  1625. s->init_off=0;
  1626. }
  1627. /* SSL3_ST_SW_CERT_B */
  1628. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  1629. }