s3_clnt.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044
  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-2007 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. /* ====================================================================
  112. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  113. *
  114. * Portions of the attached software ("Contribution") are developed by
  115. * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
  116. *
  117. * The Contribution is licensed pursuant to the OpenSSL open source
  118. * license provided above.
  119. *
  120. * ECC cipher suite support in OpenSSL originally written by
  121. * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
  122. *
  123. */
  124. /* ====================================================================
  125. * Copyright 2005 Nokia. All rights reserved.
  126. *
  127. * The portions of the attached software ("Contribution") is developed by
  128. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  129. * license.
  130. *
  131. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  132. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  133. * support (see RFC 4279) to OpenSSL.
  134. *
  135. * No patent licenses or other rights except those expressly stated in
  136. * the OpenSSL open source license shall be deemed granted or received
  137. * expressly, by implication, estoppel, or otherwise.
  138. *
  139. * No assurances are provided by Nokia that the Contribution does not
  140. * infringe the patent or other intellectual property rights of any third
  141. * party or that the license provides you with all the necessary rights
  142. * to make use of the Contribution.
  143. *
  144. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  145. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  146. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  147. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  148. * OTHERWISE.
  149. */
  150. #include <stdio.h>
  151. #include "ssl_locl.h"
  152. #include "kssl_lcl.h"
  153. #include <openssl/buffer.h>
  154. #include <openssl/rand.h>
  155. #include <openssl/objects.h>
  156. #include <openssl/evp.h>
  157. #include <openssl/md5.h>
  158. #ifndef OPENSSL_NO_DH
  159. #include <openssl/dh.h>
  160. #endif
  161. #include <openssl/bn.h>
  162. #ifndef OPENSSL_NO_ENGINE
  163. #include <openssl/engine.h>
  164. #endif
  165. static const SSL_METHOD *ssl3_get_client_method(int ver);
  166. static int ca_dn_cmp(const X509_NAME * const *a,const X509_NAME * const *b);
  167. static const SSL_METHOD *ssl3_get_client_method(int ver)
  168. {
  169. if (ver == SSL3_VERSION)
  170. return(SSLv3_client_method());
  171. else
  172. return(NULL);
  173. }
  174. IMPLEMENT_ssl3_meth_func(SSLv3_client_method,
  175. ssl_undefined_function,
  176. ssl3_connect,
  177. ssl3_get_client_method)
  178. int ssl3_connect(SSL *s)
  179. {
  180. BUF_MEM *buf=NULL;
  181. unsigned long Time=(unsigned long)time(NULL);
  182. void (*cb)(const SSL *ssl,int type,int val)=NULL;
  183. int ret= -1;
  184. int new_state,state,skip=0;
  185. RAND_add(&Time,sizeof(Time),0);
  186. ERR_clear_error();
  187. clear_sys_error();
  188. if (s->info_callback != NULL)
  189. cb=s->info_callback;
  190. else if (s->ctx->info_callback != NULL)
  191. cb=s->ctx->info_callback;
  192. s->in_handshake++;
  193. if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
  194. for (;;)
  195. {
  196. state=s->state;
  197. switch(s->state)
  198. {
  199. case SSL_ST_RENEGOTIATE:
  200. s->new_session=1;
  201. s->state=SSL_ST_CONNECT;
  202. s->ctx->stats.sess_connect_renegotiate++;
  203. /* break */
  204. case SSL_ST_BEFORE:
  205. case SSL_ST_CONNECT:
  206. case SSL_ST_BEFORE|SSL_ST_CONNECT:
  207. case SSL_ST_OK|SSL_ST_CONNECT:
  208. s->server=0;
  209. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
  210. if ((s->version & 0xff00 ) != 0x0300)
  211. {
  212. SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR);
  213. ret = -1;
  214. goto end;
  215. }
  216. /* s->version=SSL3_VERSION; */
  217. s->type=SSL_ST_CONNECT;
  218. if (s->init_buf == NULL)
  219. {
  220. if ((buf=BUF_MEM_new()) == NULL)
  221. {
  222. ret= -1;
  223. goto end;
  224. }
  225. if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
  226. {
  227. ret= -1;
  228. goto end;
  229. }
  230. s->init_buf=buf;
  231. buf=NULL;
  232. }
  233. if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
  234. /* setup buffing BIO */
  235. if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; }
  236. /* don't push the buffering BIO quite yet */
  237. ssl3_init_finished_mac(s);
  238. s->state=SSL3_ST_CW_CLNT_HELLO_A;
  239. s->ctx->stats.sess_connect++;
  240. s->init_num=0;
  241. break;
  242. case SSL3_ST_CW_CLNT_HELLO_A:
  243. case SSL3_ST_CW_CLNT_HELLO_B:
  244. s->shutdown=0;
  245. ret=ssl3_client_hello(s);
  246. if (ret <= 0) goto end;
  247. s->state=SSL3_ST_CR_SRVR_HELLO_A;
  248. s->init_num=0;
  249. /* turn on buffering for the next lot of output */
  250. if (s->bbio != s->wbio)
  251. s->wbio=BIO_push(s->bbio,s->wbio);
  252. break;
  253. case SSL3_ST_CR_SRVR_HELLO_A:
  254. case SSL3_ST_CR_SRVR_HELLO_B:
  255. ret=ssl3_get_server_hello(s);
  256. if (ret <= 0) goto end;
  257. if (s->hit)
  258. s->state=SSL3_ST_CR_FINISHED_A;
  259. else
  260. s->state=SSL3_ST_CR_CERT_A;
  261. s->init_num=0;
  262. break;
  263. case SSL3_ST_CR_CERT_A:
  264. case SSL3_ST_CR_CERT_B:
  265. #ifndef OPENSSL_NO_TLSEXT
  266. ret=ssl3_check_finished(s);
  267. if (ret <= 0) goto end;
  268. if (ret == 2)
  269. {
  270. s->hit = 1;
  271. if (s->tlsext_ticket_expected)
  272. s->state=SSL3_ST_CR_SESSION_TICKET_A;
  273. else
  274. s->state=SSL3_ST_CR_FINISHED_A;
  275. s->init_num=0;
  276. break;
  277. }
  278. #endif
  279. /* Check if it is anon DH/ECDH */
  280. /* or PSK */
  281. if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
  282. !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
  283. {
  284. ret=ssl3_get_server_certificate(s);
  285. if (ret <= 0) goto end;
  286. #ifndef OPENSSL_NO_TLSEXT
  287. if (s->tlsext_status_expected)
  288. s->state=SSL3_ST_CR_CERT_STATUS_A;
  289. else
  290. s->state=SSL3_ST_CR_KEY_EXCH_A;
  291. }
  292. else
  293. {
  294. skip = 1;
  295. s->state=SSL3_ST_CR_KEY_EXCH_A;
  296. }
  297. #else
  298. }
  299. else
  300. skip=1;
  301. s->state=SSL3_ST_CR_KEY_EXCH_A;
  302. #endif
  303. s->init_num=0;
  304. break;
  305. case SSL3_ST_CR_KEY_EXCH_A:
  306. case SSL3_ST_CR_KEY_EXCH_B:
  307. ret=ssl3_get_key_exchange(s);
  308. if (ret <= 0) goto end;
  309. s->state=SSL3_ST_CR_CERT_REQ_A;
  310. s->init_num=0;
  311. /* at this point we check that we have the
  312. * required stuff from the server */
  313. if (!ssl3_check_cert_and_algorithm(s))
  314. {
  315. ret= -1;
  316. goto end;
  317. }
  318. break;
  319. case SSL3_ST_CR_CERT_REQ_A:
  320. case SSL3_ST_CR_CERT_REQ_B:
  321. ret=ssl3_get_certificate_request(s);
  322. if (ret <= 0) goto end;
  323. s->state=SSL3_ST_CR_SRVR_DONE_A;
  324. s->init_num=0;
  325. break;
  326. case SSL3_ST_CR_SRVR_DONE_A:
  327. case SSL3_ST_CR_SRVR_DONE_B:
  328. ret=ssl3_get_server_done(s);
  329. if (ret <= 0) goto end;
  330. if (s->s3->tmp.cert_req)
  331. s->state=SSL3_ST_CW_CERT_A;
  332. else
  333. s->state=SSL3_ST_CW_KEY_EXCH_A;
  334. s->init_num=0;
  335. break;
  336. case SSL3_ST_CW_CERT_A:
  337. case SSL3_ST_CW_CERT_B:
  338. case SSL3_ST_CW_CERT_C:
  339. case SSL3_ST_CW_CERT_D:
  340. ret=ssl3_send_client_certificate(s);
  341. if (ret <= 0) goto end;
  342. s->state=SSL3_ST_CW_KEY_EXCH_A;
  343. s->init_num=0;
  344. break;
  345. case SSL3_ST_CW_KEY_EXCH_A:
  346. case SSL3_ST_CW_KEY_EXCH_B:
  347. ret=ssl3_send_client_key_exchange(s);
  348. if (ret <= 0) goto end;
  349. /* EAY EAY EAY need to check for DH fix cert
  350. * sent back */
  351. /* For TLS, cert_req is set to 2, so a cert chain
  352. * of nothing is sent, but no verify packet is sent */
  353. /* XXX: For now, we do not support client
  354. * authentication in ECDH cipher suites with
  355. * ECDH (rather than ECDSA) certificates.
  356. * We need to skip the certificate verify
  357. * message when client's ECDH public key is sent
  358. * inside the client certificate.
  359. */
  360. if (s->s3->tmp.cert_req == 1)
  361. {
  362. s->state=SSL3_ST_CW_CERT_VRFY_A;
  363. }
  364. else
  365. {
  366. s->state=SSL3_ST_CW_CHANGE_A;
  367. s->s3->change_cipher_spec=0;
  368. }
  369. if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
  370. {
  371. s->state=SSL3_ST_CW_CHANGE_A;
  372. s->s3->change_cipher_spec=0;
  373. }
  374. s->init_num=0;
  375. break;
  376. case SSL3_ST_CW_CERT_VRFY_A:
  377. case SSL3_ST_CW_CERT_VRFY_B:
  378. ret=ssl3_send_client_verify(s);
  379. if (ret <= 0) goto end;
  380. s->state=SSL3_ST_CW_CHANGE_A;
  381. s->init_num=0;
  382. s->s3->change_cipher_spec=0;
  383. break;
  384. case SSL3_ST_CW_CHANGE_A:
  385. case SSL3_ST_CW_CHANGE_B:
  386. ret=ssl3_send_change_cipher_spec(s,
  387. SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);
  388. if (ret <= 0) goto end;
  389. s->state=SSL3_ST_CW_FINISHED_A;
  390. s->init_num=0;
  391. s->session->cipher=s->s3->tmp.new_cipher;
  392. #ifdef OPENSSL_NO_COMP
  393. s->session->compress_meth=0;
  394. #else
  395. if (s->s3->tmp.new_compression == NULL)
  396. s->session->compress_meth=0;
  397. else
  398. s->session->compress_meth=
  399. s->s3->tmp.new_compression->id;
  400. #endif
  401. if (!s->method->ssl3_enc->setup_key_block(s))
  402. {
  403. ret= -1;
  404. goto end;
  405. }
  406. if (!s->method->ssl3_enc->change_cipher_state(s,
  407. SSL3_CHANGE_CIPHER_CLIENT_WRITE))
  408. {
  409. ret= -1;
  410. goto end;
  411. }
  412. break;
  413. case SSL3_ST_CW_FINISHED_A:
  414. case SSL3_ST_CW_FINISHED_B:
  415. ret=ssl3_send_finished(s,
  416. SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
  417. s->method->ssl3_enc->client_finished_label,
  418. s->method->ssl3_enc->client_finished_label_len);
  419. if (ret <= 0) goto end;
  420. s->state=SSL3_ST_CW_FLUSH;
  421. /* clear flags */
  422. s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
  423. if (s->hit)
  424. {
  425. s->s3->tmp.next_state=SSL_ST_OK;
  426. if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)
  427. {
  428. s->state=SSL_ST_OK;
  429. s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
  430. s->s3->delay_buf_pop_ret=0;
  431. }
  432. }
  433. else
  434. {
  435. #ifndef OPENSSL_NO_TLSEXT
  436. /* Allow NewSessionTicket if ticket expected */
  437. if (s->tlsext_ticket_expected)
  438. s->s3->tmp.next_state=SSL3_ST_CR_SESSION_TICKET_A;
  439. else
  440. #endif
  441. s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;
  442. }
  443. s->init_num=0;
  444. break;
  445. #ifndef OPENSSL_NO_TLSEXT
  446. case SSL3_ST_CR_SESSION_TICKET_A:
  447. case SSL3_ST_CR_SESSION_TICKET_B:
  448. ret=ssl3_get_new_session_ticket(s);
  449. if (ret <= 0) goto end;
  450. s->state=SSL3_ST_CR_FINISHED_A;
  451. s->init_num=0;
  452. break;
  453. case SSL3_ST_CR_CERT_STATUS_A:
  454. case SSL3_ST_CR_CERT_STATUS_B:
  455. ret=ssl3_get_cert_status(s);
  456. if (ret <= 0) goto end;
  457. s->state=SSL3_ST_CR_KEY_EXCH_A;
  458. s->init_num=0;
  459. break;
  460. #endif
  461. case SSL3_ST_CR_FINISHED_A:
  462. case SSL3_ST_CR_FINISHED_B:
  463. ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
  464. SSL3_ST_CR_FINISHED_B);
  465. if (ret <= 0) goto end;
  466. if (s->hit)
  467. s->state=SSL3_ST_CW_CHANGE_A;
  468. else
  469. s->state=SSL_ST_OK;
  470. s->init_num=0;
  471. break;
  472. case SSL3_ST_CW_FLUSH:
  473. s->rwstate=SSL_WRITING;
  474. if (BIO_flush(s->wbio) <= 0)
  475. {
  476. ret= -1;
  477. goto end;
  478. }
  479. s->rwstate=SSL_NOTHING;
  480. s->state=s->s3->tmp.next_state;
  481. break;
  482. case SSL_ST_OK:
  483. /* clean a few things up */
  484. ssl3_cleanup_key_block(s);
  485. if (s->init_buf != NULL)
  486. {
  487. BUF_MEM_free(s->init_buf);
  488. s->init_buf=NULL;
  489. }
  490. /* If we are not 'joining' the last two packets,
  491. * remove the buffering now */
  492. if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
  493. ssl_free_wbio_buffer(s);
  494. /* else do it later in ssl3_write */
  495. s->init_num=0;
  496. s->new_session=0;
  497. ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
  498. if (s->hit) s->ctx->stats.sess_hit++;
  499. ret=1;
  500. /* s->server=0; */
  501. s->handshake_func=ssl3_connect;
  502. s->ctx->stats.sess_connect_good++;
  503. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
  504. goto end;
  505. /* break; */
  506. default:
  507. SSLerr(SSL_F_SSL3_CONNECT,SSL_R_UNKNOWN_STATE);
  508. ret= -1;
  509. goto end;
  510. /* break; */
  511. }
  512. /* did we do anything */
  513. if (!s->s3->tmp.reuse_message && !skip)
  514. {
  515. if (s->debug)
  516. {
  517. if ((ret=BIO_flush(s->wbio)) <= 0)
  518. goto end;
  519. }
  520. if ((cb != NULL) && (s->state != state))
  521. {
  522. new_state=s->state;
  523. s->state=state;
  524. cb(s,SSL_CB_CONNECT_LOOP,1);
  525. s->state=new_state;
  526. }
  527. }
  528. skip=0;
  529. }
  530. end:
  531. s->in_handshake--;
  532. if (buf != NULL)
  533. BUF_MEM_free(buf);
  534. if (cb != NULL)
  535. cb(s,SSL_CB_CONNECT_EXIT,ret);
  536. return(ret);
  537. }
  538. int ssl3_client_hello(SSL *s)
  539. {
  540. unsigned char *buf;
  541. unsigned char *p,*d;
  542. int i;
  543. unsigned long Time,l;
  544. #ifndef OPENSSL_NO_COMP
  545. int j;
  546. SSL_COMP *comp;
  547. #endif
  548. buf=(unsigned char *)s->init_buf->data;
  549. if (s->state == SSL3_ST_CW_CLNT_HELLO_A)
  550. {
  551. SSL_SESSION *sess = s->session;
  552. if ((sess == NULL) ||
  553. (sess->ssl_version != s->version) ||
  554. #ifdef OPENSSL_NO_TLSEXT
  555. !sess->session_id_length ||
  556. #else
  557. (!sess->session_id_length && !sess->tlsext_tick) ||
  558. #endif
  559. (sess->not_resumable))
  560. {
  561. if (!ssl_get_new_session(s,0))
  562. goto err;
  563. }
  564. /* else use the pre-loaded session */
  565. p=s->s3->client_random;
  566. Time=(unsigned long)time(NULL); /* Time */
  567. l2n(Time,p);
  568. if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0)
  569. goto err;
  570. /* Do the message type and length last */
  571. d=p= &(buf[4]);
  572. *(p++)=s->version>>8;
  573. *(p++)=s->version&0xff;
  574. s->client_version=s->version;
  575. /* Random stuff */
  576. memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
  577. p+=SSL3_RANDOM_SIZE;
  578. /* Session ID */
  579. if (s->new_session)
  580. i=0;
  581. else
  582. i=s->session->session_id_length;
  583. *(p++)=i;
  584. if (i != 0)
  585. {
  586. if (i > (int)sizeof(s->session->session_id))
  587. {
  588. SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
  589. goto err;
  590. }
  591. memcpy(p,s->session->session_id,i);
  592. p+=i;
  593. }
  594. /* Ciphers supported */
  595. i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]),0);
  596. if (i == 0)
  597. {
  598. SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
  599. goto err;
  600. }
  601. s2n(i,p);
  602. p+=i;
  603. /* COMPRESSION */
  604. #ifdef OPENSSL_NO_COMP
  605. *(p++)=1;
  606. #else
  607. if ((s->options & SSL_OP_NO_COMPRESSION)
  608. || !s->ctx->comp_methods)
  609. j=0;
  610. else
  611. j=sk_SSL_COMP_num(s->ctx->comp_methods);
  612. *(p++)=1+j;
  613. for (i=0; i<j; i++)
  614. {
  615. comp=sk_SSL_COMP_value(s->ctx->comp_methods,i);
  616. *(p++)=comp->id;
  617. }
  618. #endif
  619. *(p++)=0; /* Add the NULL method */
  620. #ifndef OPENSSL_NO_TLSEXT
  621. /* TLS extensions*/
  622. if (ssl_prepare_clienthello_tlsext(s) <= 0)
  623. {
  624. SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_CLIENTHELLO_TLSEXT);
  625. goto err;
  626. }
  627. if ((p = ssl_add_clienthello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH)) == NULL)
  628. {
  629. SSLerr(SSL_F_SSL3_CLIENT_HELLO,ERR_R_INTERNAL_ERROR);
  630. goto err;
  631. }
  632. #endif
  633. l=(p-d);
  634. d=buf;
  635. *(d++)=SSL3_MT_CLIENT_HELLO;
  636. l2n3(l,d);
  637. s->state=SSL3_ST_CW_CLNT_HELLO_B;
  638. /* number of bytes to write */
  639. s->init_num=p-buf;
  640. s->init_off=0;
  641. }
  642. /* SSL3_ST_CW_CLNT_HELLO_B */
  643. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  644. err:
  645. return(-1);
  646. }
  647. int ssl3_get_server_hello(SSL *s)
  648. {
  649. STACK_OF(SSL_CIPHER) *sk;
  650. const SSL_CIPHER *c;
  651. unsigned char *p,*d;
  652. int i,al,ok;
  653. unsigned int j;
  654. long n;
  655. #ifndef OPENSSL_NO_COMP
  656. SSL_COMP *comp;
  657. #endif
  658. n=s->method->ssl_get_message(s,
  659. SSL3_ST_CR_SRVR_HELLO_A,
  660. SSL3_ST_CR_SRVR_HELLO_B,
  661. -1,
  662. 20000, /* ?? */
  663. &ok);
  664. if (!ok) return((int)n);
  665. if ( SSL_version(s) == DTLS1_VERSION || SSL_version(s) == DTLS1_BAD_VER)
  666. {
  667. if ( s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST)
  668. {
  669. if ( s->d1->send_cookie == 0)
  670. {
  671. s->s3->tmp.reuse_message = 1;
  672. return 1;
  673. }
  674. else /* already sent a cookie */
  675. {
  676. al=SSL_AD_UNEXPECTED_MESSAGE;
  677. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_MESSAGE_TYPE);
  678. goto f_err;
  679. }
  680. }
  681. }
  682. if ( s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO)
  683. {
  684. al=SSL_AD_UNEXPECTED_MESSAGE;
  685. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_MESSAGE_TYPE);
  686. goto f_err;
  687. }
  688. d=p=(unsigned char *)s->init_msg;
  689. if ((p[0] != (s->version>>8)) || (p[1] != (s->version&0xff)))
  690. {
  691. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_SSL_VERSION);
  692. s->version=(s->version&0xff00)|p[1];
  693. al=SSL_AD_PROTOCOL_VERSION;
  694. goto f_err;
  695. }
  696. p+=2;
  697. /* load the server hello data */
  698. /* load the server random */
  699. memcpy(s->s3->server_random,p,SSL3_RANDOM_SIZE);
  700. p+=SSL3_RANDOM_SIZE;
  701. /* get the session-id */
  702. j= *(p++);
  703. if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE))
  704. {
  705. al=SSL_AD_ILLEGAL_PARAMETER;
  706. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SSL3_SESSION_ID_TOO_LONG);
  707. goto f_err;
  708. }
  709. #ifndef OPENSSL_NO_TLSEXT
  710. /* check if we want to resume the session based on external pre-shared secret */
  711. if (s->version >= TLS1_VERSION && s->tls_session_secret_cb)
  712. {
  713. SSL_CIPHER *pref_cipher=NULL;
  714. s->session->master_key_length=sizeof(s->session->master_key);
  715. if (s->tls_session_secret_cb(s, s->session->master_key,
  716. &s->session->master_key_length,
  717. NULL, &pref_cipher,
  718. s->tls_session_secret_cb_arg))
  719. {
  720. s->session->cipher = pref_cipher ?
  721. pref_cipher : ssl_get_cipher_by_char(s, p+j);
  722. }
  723. }
  724. #endif /* OPENSSL_NO_TLSEXT */
  725. if (j != 0 && j == s->session->session_id_length
  726. && memcmp(p,s->session->session_id,j) == 0)
  727. {
  728. if(s->sid_ctx_length != s->session->sid_ctx_length
  729. || memcmp(s->session->sid_ctx,s->sid_ctx,s->sid_ctx_length))
  730. {
  731. /* actually a client application bug */
  732. al=SSL_AD_ILLEGAL_PARAMETER;
  733. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
  734. goto f_err;
  735. }
  736. s->hit=1;
  737. }
  738. else /* a miss or crap from the other end */
  739. {
  740. /* If we were trying for session-id reuse, make a new
  741. * SSL_SESSION so we don't stuff up other people */
  742. s->hit=0;
  743. if (s->session->session_id_length > 0)
  744. {
  745. if (!ssl_get_new_session(s,0))
  746. {
  747. al=SSL_AD_INTERNAL_ERROR;
  748. goto f_err;
  749. }
  750. }
  751. s->session->session_id_length=j;
  752. memcpy(s->session->session_id,p,j); /* j could be 0 */
  753. }
  754. p+=j;
  755. c=ssl_get_cipher_by_char(s,p);
  756. if (c == NULL)
  757. {
  758. /* unknown cipher */
  759. al=SSL_AD_ILLEGAL_PARAMETER;
  760. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNKNOWN_CIPHER_RETURNED);
  761. goto f_err;
  762. }
  763. p+=ssl_put_cipher_by_char(s,NULL,NULL);
  764. sk=ssl_get_ciphers_by_id(s);
  765. i=sk_SSL_CIPHER_find(sk,c);
  766. if (i < 0)
  767. {
  768. /* we did not say we would use this cipher */
  769. al=SSL_AD_ILLEGAL_PARAMETER;
  770. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_CIPHER_RETURNED);
  771. goto f_err;
  772. }
  773. /* Depending on the session caching (internal/external), the cipher
  774. and/or cipher_id values may not be set. Make sure that
  775. cipher_id is set and use it for comparison. */
  776. if (s->session->cipher)
  777. s->session->cipher_id = s->session->cipher->id;
  778. if (s->hit && (s->session->cipher_id != c->id))
  779. {
  780. if (!(s->options &
  781. SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG))
  782. {
  783. al=SSL_AD_ILLEGAL_PARAMETER;
  784. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
  785. goto f_err;
  786. }
  787. }
  788. s->s3->tmp.new_cipher=c;
  789. if (!ssl3_digest_cached_records(s))
  790. goto f_err;
  791. /* lets get the compression algorithm */
  792. /* COMPRESSION */
  793. #ifdef OPENSSL_NO_COMP
  794. if (*(p++) != 0)
  795. {
  796. al=SSL_AD_ILLEGAL_PARAMETER;
  797. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  798. goto f_err;
  799. }
  800. /* If compression is disabled we'd better not try to resume a session
  801. * using compression.
  802. */
  803. if (s->session->compress_meth != 0)
  804. {
  805. al=SSL_AD_INTERNAL_ERROR;
  806. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_INCONSISTENT_COMPRESSION);
  807. goto f_err;
  808. }
  809. #else
  810. j= *(p++);
  811. if (s->hit && j != s->session->compress_meth)
  812. {
  813. al=SSL_AD_ILLEGAL_PARAMETER;
  814. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
  815. goto f_err;
  816. }
  817. if (j == 0)
  818. comp=NULL;
  819. else if (s->options & SSL_OP_NO_COMPRESSION)
  820. {
  821. al=SSL_AD_ILLEGAL_PARAMETER;
  822. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_COMPRESSION_DISABLED);
  823. goto f_err;
  824. }
  825. else
  826. comp=ssl3_comp_find(s->ctx->comp_methods,j);
  827. if ((j != 0) && (comp == NULL))
  828. {
  829. al=SSL_AD_ILLEGAL_PARAMETER;
  830. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  831. goto f_err;
  832. }
  833. else
  834. {
  835. s->s3->tmp.new_compression=comp;
  836. }
  837. #endif
  838. #ifndef OPENSSL_NO_TLSEXT
  839. /* TLS extensions*/
  840. if (s->version >= SSL3_VERSION)
  841. {
  842. if (!ssl_parse_serverhello_tlsext(s,&p,d,n, &al))
  843. {
  844. /* 'al' set by ssl_parse_serverhello_tlsext */
  845. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_PARSE_TLSEXT);
  846. goto f_err;
  847. }
  848. if (ssl_check_serverhello_tlsext(s) <= 0)
  849. {
  850. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SERVERHELLO_TLSEXT);
  851. goto err;
  852. }
  853. }
  854. #endif
  855. if (p != (d+n))
  856. {
  857. /* wrong packet length */
  858. al=SSL_AD_DECODE_ERROR;
  859. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_PACKET_LENGTH);
  860. goto err;
  861. }
  862. return(1);
  863. f_err:
  864. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  865. err:
  866. return(-1);
  867. }
  868. int ssl3_get_server_certificate(SSL *s)
  869. {
  870. int al,i,ok,ret= -1;
  871. unsigned long n,nc,llen,l;
  872. X509 *x=NULL;
  873. const unsigned char *q,*p;
  874. unsigned char *d;
  875. STACK_OF(X509) *sk=NULL;
  876. SESS_CERT *sc;
  877. EVP_PKEY *pkey=NULL;
  878. int need_cert = 1; /* VRS: 0=> will allow null cert if auth == KRB5 */
  879. n=s->method->ssl_get_message(s,
  880. SSL3_ST_CR_CERT_A,
  881. SSL3_ST_CR_CERT_B,
  882. -1,
  883. s->max_cert_list,
  884. &ok);
  885. if (!ok) return((int)n);
  886. if ((s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) ||
  887. ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5) &&
  888. (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)))
  889. {
  890. s->s3->tmp.reuse_message=1;
  891. return(1);
  892. }
  893. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
  894. {
  895. al=SSL_AD_UNEXPECTED_MESSAGE;
  896. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_BAD_MESSAGE_TYPE);
  897. goto f_err;
  898. }
  899. p=d=(unsigned char *)s->init_msg;
  900. if ((sk=sk_X509_new_null()) == NULL)
  901. {
  902. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
  903. goto err;
  904. }
  905. n2l3(p,llen);
  906. if (llen+3 != n)
  907. {
  908. al=SSL_AD_DECODE_ERROR;
  909. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
  910. goto f_err;
  911. }
  912. for (nc=0; nc<llen; )
  913. {
  914. n2l3(p,l);
  915. if ((l+nc+3) > llen)
  916. {
  917. al=SSL_AD_DECODE_ERROR;
  918. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
  919. goto f_err;
  920. }
  921. q=p;
  922. x=d2i_X509(NULL,&q,l);
  923. if (x == NULL)
  924. {
  925. al=SSL_AD_BAD_CERTIFICATE;
  926. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_ASN1_LIB);
  927. goto f_err;
  928. }
  929. if (q != (p+l))
  930. {
  931. al=SSL_AD_DECODE_ERROR;
  932. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
  933. goto f_err;
  934. }
  935. if (!sk_X509_push(sk,x))
  936. {
  937. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
  938. goto err;
  939. }
  940. x=NULL;
  941. nc+=l+3;
  942. p=q;
  943. }
  944. i=ssl_verify_cert_chain(s,sk);
  945. if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0)
  946. #ifndef OPENSSL_NO_KRB5
  947. && !((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
  948. (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
  949. #endif /* OPENSSL_NO_KRB5 */
  950. )
  951. {
  952. al=ssl_verify_alarm_type(s->verify_result);
  953. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED);
  954. goto f_err;
  955. }
  956. ERR_clear_error(); /* but we keep s->verify_result */
  957. sc=ssl_sess_cert_new();
  958. if (sc == NULL) goto err;
  959. if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert);
  960. s->session->sess_cert=sc;
  961. sc->cert_chain=sk;
  962. /* Inconsistency alert: cert_chain does include the peer's
  963. * certificate, which we don't include in s3_srvr.c */
  964. x=sk_X509_value(sk,0);
  965. sk=NULL;
  966. /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
  967. pkey=X509_get_pubkey(x);
  968. /* VRS: allow null cert if auth == KRB5 */
  969. need_cert = ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
  970. (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
  971. ? 0 : 1;
  972. #ifdef KSSL_DEBUG
  973. printf("pkey,x = %p, %p\n", pkey,x);
  974. printf("ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x,pkey));
  975. printf("cipher, alg, nc = %s, %lx, %lx, %d\n", s->s3->tmp.new_cipher->name,
  976. s->s3->tmp.new_cipher->algorithm_mkey, s->s3->tmp.new_cipher->algorithm_auth, need_cert);
  977. #endif /* KSSL_DEBUG */
  978. if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey)))
  979. {
  980. x=NULL;
  981. al=SSL3_AL_FATAL;
  982. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
  983. SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
  984. goto f_err;
  985. }
  986. i=ssl_cert_type(x,pkey);
  987. if (need_cert && i < 0)
  988. {
  989. x=NULL;
  990. al=SSL3_AL_FATAL;
  991. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
  992. SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  993. goto f_err;
  994. }
  995. if (need_cert)
  996. {
  997. sc->peer_cert_type=i;
  998. CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
  999. /* Why would the following ever happen?
  1000. * We just created sc a couple of lines ago. */
  1001. if (sc->peer_pkeys[i].x509 != NULL)
  1002. X509_free(sc->peer_pkeys[i].x509);
  1003. sc->peer_pkeys[i].x509=x;
  1004. sc->peer_key= &(sc->peer_pkeys[i]);
  1005. if (s->session->peer != NULL)
  1006. X509_free(s->session->peer);
  1007. CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
  1008. s->session->peer=x;
  1009. }
  1010. else
  1011. {
  1012. sc->peer_cert_type=i;
  1013. sc->peer_key= NULL;
  1014. if (s->session->peer != NULL)
  1015. X509_free(s->session->peer);
  1016. s->session->peer=NULL;
  1017. }
  1018. s->session->verify_result = s->verify_result;
  1019. x=NULL;
  1020. ret=1;
  1021. if (0)
  1022. {
  1023. f_err:
  1024. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1025. }
  1026. err:
  1027. EVP_PKEY_free(pkey);
  1028. X509_free(x);
  1029. sk_X509_pop_free(sk,X509_free);
  1030. return(ret);
  1031. }
  1032. int ssl3_get_key_exchange(SSL *s)
  1033. {
  1034. #ifndef OPENSSL_NO_RSA
  1035. unsigned char *q,md_buf[EVP_MAX_MD_SIZE*2];
  1036. #endif
  1037. EVP_MD_CTX md_ctx;
  1038. unsigned char *param,*p;
  1039. int al,i,j,param_len,ok;
  1040. long n,alg_k,alg_a;
  1041. EVP_PKEY *pkey=NULL;
  1042. #ifndef OPENSSL_NO_RSA
  1043. RSA *rsa=NULL;
  1044. #endif
  1045. #ifndef OPENSSL_NO_DH
  1046. DH *dh=NULL;
  1047. #endif
  1048. #ifndef OPENSSL_NO_ECDH
  1049. EC_KEY *ecdh = NULL;
  1050. BN_CTX *bn_ctx = NULL;
  1051. EC_POINT *srvr_ecpoint = NULL;
  1052. int curve_nid = 0;
  1053. int encoded_pt_len = 0;
  1054. #endif
  1055. /* use same message size as in ssl3_get_certificate_request()
  1056. * as ServerKeyExchange message may be skipped */
  1057. n=s->method->ssl_get_message(s,
  1058. SSL3_ST_CR_KEY_EXCH_A,
  1059. SSL3_ST_CR_KEY_EXCH_B,
  1060. -1,
  1061. s->max_cert_list,
  1062. &ok);
  1063. if (!ok) return((int)n);
  1064. if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE)
  1065. {
  1066. #ifndef OPENSSL_NO_PSK
  1067. /* In plain PSK ciphersuite, ServerKeyExchange can be
  1068. omitted if no identity hint is sent. Set
  1069. session->sess_cert anyway to avoid problems
  1070. later.*/
  1071. if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)
  1072. {
  1073. s->session->sess_cert=ssl_sess_cert_new();
  1074. if (s->ctx->psk_identity_hint)
  1075. OPENSSL_free(s->ctx->psk_identity_hint);
  1076. s->ctx->psk_identity_hint = NULL;
  1077. }
  1078. #endif
  1079. s->s3->tmp.reuse_message=1;
  1080. return(1);
  1081. }
  1082. param=p=(unsigned char *)s->init_msg;
  1083. if (s->session->sess_cert != NULL)
  1084. {
  1085. #ifndef OPENSSL_NO_RSA
  1086. if (s->session->sess_cert->peer_rsa_tmp != NULL)
  1087. {
  1088. RSA_free(s->session->sess_cert->peer_rsa_tmp);
  1089. s->session->sess_cert->peer_rsa_tmp=NULL;
  1090. }
  1091. #endif
  1092. #ifndef OPENSSL_NO_DH
  1093. if (s->session->sess_cert->peer_dh_tmp)
  1094. {
  1095. DH_free(s->session->sess_cert->peer_dh_tmp);
  1096. s->session->sess_cert->peer_dh_tmp=NULL;
  1097. }
  1098. #endif
  1099. #ifndef OPENSSL_NO_ECDH
  1100. if (s->session->sess_cert->peer_ecdh_tmp)
  1101. {
  1102. EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
  1103. s->session->sess_cert->peer_ecdh_tmp=NULL;
  1104. }
  1105. #endif
  1106. }
  1107. else
  1108. {
  1109. s->session->sess_cert=ssl_sess_cert_new();
  1110. }
  1111. param_len=0;
  1112. alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
  1113. alg_a=s->s3->tmp.new_cipher->algorithm_auth;
  1114. EVP_MD_CTX_init(&md_ctx);
  1115. #ifndef OPENSSL_NO_PSK
  1116. if (alg_k & SSL_kPSK)
  1117. {
  1118. char tmp_id_hint[PSK_MAX_IDENTITY_LEN+1];
  1119. al=SSL_AD_HANDSHAKE_FAILURE;
  1120. n2s(p,i);
  1121. param_len=i+2;
  1122. /* Store PSK identity hint for later use, hint is used
  1123. * in ssl3_send_client_key_exchange. Assume that the
  1124. * maximum length of a PSK identity hint can be as
  1125. * long as the maximum length of a PSK identity. */
  1126. if (i > PSK_MAX_IDENTITY_LEN)
  1127. {
  1128. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
  1129. SSL_R_DATA_LENGTH_TOO_LONG);
  1130. goto f_err;
  1131. }
  1132. if (param_len > n)
  1133. {
  1134. al=SSL_AD_DECODE_ERROR;
  1135. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
  1136. SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH);
  1137. goto f_err;
  1138. }
  1139. /* If received PSK identity hint contains NULL
  1140. * characters, the hint is truncated from the first
  1141. * NULL. p may not be ending with NULL, so create a
  1142. * NULL-terminated string. */
  1143. memcpy(tmp_id_hint, p, i);
  1144. memset(tmp_id_hint+i, 0, PSK_MAX_IDENTITY_LEN+1-i);
  1145. if (s->ctx->psk_identity_hint != NULL)
  1146. OPENSSL_free(s->ctx->psk_identity_hint);
  1147. s->ctx->psk_identity_hint = BUF_strdup(tmp_id_hint);
  1148. if (s->ctx->psk_identity_hint == NULL)
  1149. {
  1150. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
  1151. goto f_err;
  1152. }
  1153. p+=i;
  1154. n-=param_len;
  1155. }
  1156. else
  1157. #endif /* !OPENSSL_NO_PSK */
  1158. #ifndef OPENSSL_NO_RSA
  1159. if (alg_k & SSL_kRSA)
  1160. {
  1161. if ((rsa=RSA_new()) == NULL)
  1162. {
  1163. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  1164. goto err;
  1165. }
  1166. n2s(p,i);
  1167. param_len=i+2;
  1168. if (param_len > n)
  1169. {
  1170. al=SSL_AD_DECODE_ERROR;
  1171. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_MODULUS_LENGTH);
  1172. goto f_err;
  1173. }
  1174. if (!(rsa->n=BN_bin2bn(p,i,rsa->n)))
  1175. {
  1176. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  1177. goto err;
  1178. }
  1179. p+=i;
  1180. n2s(p,i);
  1181. param_len+=i+2;
  1182. if (param_len > n)
  1183. {
  1184. al=SSL_AD_DECODE_ERROR;
  1185. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_E_LENGTH);
  1186. goto f_err;
  1187. }
  1188. if (!(rsa->e=BN_bin2bn(p,i,rsa->e)))
  1189. {
  1190. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  1191. goto err;
  1192. }
  1193. p+=i;
  1194. n-=param_len;
  1195. /* this should be because we are using an export cipher */
  1196. if (alg_a & SSL_aRSA)
  1197. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1198. else
  1199. {
  1200. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
  1201. goto err;
  1202. }
  1203. s->session->sess_cert->peer_rsa_tmp=rsa;
  1204. rsa=NULL;
  1205. }
  1206. #else /* OPENSSL_NO_RSA */
  1207. if (0)
  1208. ;
  1209. #endif
  1210. #ifndef OPENSSL_NO_DH
  1211. else if (alg_k & SSL_kEDH)
  1212. {
  1213. if ((dh=DH_new()) == NULL)
  1214. {
  1215. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_DH_LIB);
  1216. goto err;
  1217. }
  1218. n2s(p,i);
  1219. param_len=i+2;
  1220. if (param_len > n)
  1221. {
  1222. al=SSL_AD_DECODE_ERROR;
  1223. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_P_LENGTH);
  1224. goto f_err;
  1225. }
  1226. if (!(dh->p=BN_bin2bn(p,i,NULL)))
  1227. {
  1228. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  1229. goto err;
  1230. }
  1231. p+=i;
  1232. n2s(p,i);
  1233. param_len+=i+2;
  1234. if (param_len > n)
  1235. {
  1236. al=SSL_AD_DECODE_ERROR;
  1237. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_G_LENGTH);
  1238. goto f_err;
  1239. }
  1240. if (!(dh->g=BN_bin2bn(p,i,NULL)))
  1241. {
  1242. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  1243. goto err;
  1244. }
  1245. p+=i;
  1246. n2s(p,i);
  1247. param_len+=i+2;
  1248. if (param_len > n)
  1249. {
  1250. al=SSL_AD_DECODE_ERROR;
  1251. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_PUB_KEY_LENGTH);
  1252. goto f_err;
  1253. }
  1254. if (!(dh->pub_key=BN_bin2bn(p,i,NULL)))
  1255. {
  1256. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  1257. goto err;
  1258. }
  1259. p+=i;
  1260. n-=param_len;
  1261. #ifndef OPENSSL_NO_RSA
  1262. if (alg_a & SSL_aRSA)
  1263. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1264. #else
  1265. if (0)
  1266. ;
  1267. #endif
  1268. #ifndef OPENSSL_NO_DSA
  1269. else if (alg_a & SSL_aDSS)
  1270. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].x509);
  1271. #endif
  1272. /* else anonymous DH, so no certificate or pkey. */
  1273. s->session->sess_cert->peer_dh_tmp=dh;
  1274. dh=NULL;
  1275. }
  1276. else if ((alg_k & SSL_kDHr) || (alg_k & SSL_kDHd))
  1277. {
  1278. al=SSL_AD_ILLEGAL_PARAMETER;
  1279. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
  1280. goto f_err;
  1281. }
  1282. #endif /* !OPENSSL_NO_DH */
  1283. #ifndef OPENSSL_NO_ECDH
  1284. else if (alg_k & SSL_kEECDH)
  1285. {
  1286. EC_GROUP *ngroup;
  1287. const EC_GROUP *group;
  1288. if ((ecdh=EC_KEY_new()) == NULL)
  1289. {
  1290. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  1291. goto err;
  1292. }
  1293. /* Extract elliptic curve parameters and the
  1294. * server's ephemeral ECDH public key.
  1295. * Keep accumulating lengths of various components in
  1296. * param_len and make sure it never exceeds n.
  1297. */
  1298. /* XXX: For now we only support named (not generic) curves
  1299. * and the ECParameters in this case is just three bytes.
  1300. */
  1301. param_len=3;
  1302. if ((param_len > n) ||
  1303. (*p != NAMED_CURVE_TYPE) ||
  1304. ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0))
  1305. {
  1306. al=SSL_AD_INTERNAL_ERROR;
  1307. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
  1308. goto f_err;
  1309. }
  1310. ngroup = EC_GROUP_new_by_curve_name(curve_nid);
  1311. if (ngroup == NULL)
  1312. {
  1313. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_EC_LIB);
  1314. goto err;
  1315. }
  1316. if (EC_KEY_set_group(ecdh, ngroup) == 0)
  1317. {
  1318. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_EC_LIB);
  1319. goto err;
  1320. }
  1321. EC_GROUP_free(ngroup);
  1322. group = EC_KEY_get0_group(ecdh);
  1323. if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
  1324. (EC_GROUP_get_degree(group) > 163))
  1325. {
  1326. al=SSL_AD_EXPORT_RESTRICTION;
  1327. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
  1328. goto f_err;
  1329. }
  1330. p+=3;
  1331. /* Next, get the encoded ECPoint */
  1332. if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
  1333. ((bn_ctx = BN_CTX_new()) == NULL))
  1334. {
  1335. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  1336. goto err;
  1337. }
  1338. encoded_pt_len = *p; /* length of encoded point */
  1339. p+=1;
  1340. param_len += (1 + encoded_pt_len);
  1341. if ((param_len > n) ||
  1342. (EC_POINT_oct2point(group, srvr_ecpoint,
  1343. p, encoded_pt_len, bn_ctx) == 0))
  1344. {
  1345. al=SSL_AD_DECODE_ERROR;
  1346. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_ECPOINT);
  1347. goto f_err;
  1348. }
  1349. n-=param_len;
  1350. p+=encoded_pt_len;
  1351. /* The ECC/TLS specification does not mention
  1352. * the use of DSA to sign ECParameters in the server
  1353. * key exchange message. We do support RSA and ECDSA.
  1354. */
  1355. if (0) ;
  1356. #ifndef OPENSSL_NO_RSA
  1357. else if (alg_a & SSL_aRSA)
  1358. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1359. #endif
  1360. #ifndef OPENSSL_NO_ECDSA
  1361. else if (alg_a & SSL_aECDSA)
  1362. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
  1363. #endif
  1364. /* else anonymous ECDH, so no certificate or pkey. */
  1365. EC_KEY_set_public_key(ecdh, srvr_ecpoint);
  1366. s->session->sess_cert->peer_ecdh_tmp=ecdh;
  1367. ecdh=NULL;
  1368. BN_CTX_free(bn_ctx);
  1369. EC_POINT_free(srvr_ecpoint);
  1370. srvr_ecpoint = NULL;
  1371. }
  1372. else if (alg_k)
  1373. {
  1374. al=SSL_AD_UNEXPECTED_MESSAGE;
  1375. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_UNEXPECTED_MESSAGE);
  1376. goto f_err;
  1377. }
  1378. #endif /* !OPENSSL_NO_ECDH */
  1379. /* p points to the next byte, there are 'n' bytes left */
  1380. /* if it was signed, check the signature */
  1381. if (pkey != NULL)
  1382. {
  1383. n2s(p,i);
  1384. n-=2;
  1385. j=EVP_PKEY_size(pkey);
  1386. if ((i != n) || (n > j) || (n <= 0))
  1387. {
  1388. /* wrong packet length */
  1389. al=SSL_AD_DECODE_ERROR;
  1390. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_WRONG_SIGNATURE_LENGTH);
  1391. goto f_err;
  1392. }
  1393. #ifndef OPENSSL_NO_RSA
  1394. if (pkey->type == EVP_PKEY_RSA)
  1395. {
  1396. int num;
  1397. j=0;
  1398. q=md_buf;
  1399. for (num=2; num > 0; num--)
  1400. {
  1401. EVP_DigestInit_ex(&md_ctx,(num == 2)
  1402. ?s->ctx->md5:s->ctx->sha1, NULL);
  1403. EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1404. EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1405. EVP_DigestUpdate(&md_ctx,param,param_len);
  1406. EVP_DigestFinal_ex(&md_ctx,q,(unsigned int *)&i);
  1407. q+=i;
  1408. j+=i;
  1409. }
  1410. i=RSA_verify(NID_md5_sha1, md_buf, j, p, n,
  1411. pkey->pkey.rsa);
  1412. if (i < 0)
  1413. {
  1414. al=SSL_AD_DECRYPT_ERROR;
  1415. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);
  1416. goto f_err;
  1417. }
  1418. if (i == 0)
  1419. {
  1420. /* bad signature */
  1421. al=SSL_AD_DECRYPT_ERROR;
  1422. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
  1423. goto f_err;
  1424. }
  1425. }
  1426. else
  1427. #endif
  1428. #ifndef OPENSSL_NO_DSA
  1429. if (pkey->type == EVP_PKEY_DSA)
  1430. {
  1431. /* lets do DSS */
  1432. EVP_VerifyInit_ex(&md_ctx,EVP_dss1(), NULL);
  1433. EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1434. EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1435. EVP_VerifyUpdate(&md_ctx,param,param_len);
  1436. if (EVP_VerifyFinal(&md_ctx,p,(int)n,pkey) <= 0)
  1437. {
  1438. /* bad signature */
  1439. al=SSL_AD_DECRYPT_ERROR;
  1440. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
  1441. goto f_err;
  1442. }
  1443. }
  1444. else
  1445. #endif
  1446. #ifndef OPENSSL_NO_ECDSA
  1447. if (pkey->type == EVP_PKEY_EC)
  1448. {
  1449. /* let's do ECDSA */
  1450. EVP_VerifyInit_ex(&md_ctx,EVP_ecdsa(), NULL);
  1451. EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1452. EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1453. EVP_VerifyUpdate(&md_ctx,param,param_len);
  1454. if (EVP_VerifyFinal(&md_ctx,p,(int)n,pkey) <= 0)
  1455. {
  1456. /* bad signature */
  1457. al=SSL_AD_DECRYPT_ERROR;
  1458. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
  1459. goto f_err;
  1460. }
  1461. }
  1462. else
  1463. #endif
  1464. {
  1465. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
  1466. goto err;
  1467. }
  1468. }
  1469. else
  1470. {
  1471. if (!(alg_a & SSL_aNULL) && !(alg_k & SSL_kPSK))
  1472. /* aNULL or kPSK do not need public keys */
  1473. {
  1474. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
  1475. goto err;
  1476. }
  1477. /* still data left over */
  1478. if (n != 0)
  1479. {
  1480. al=SSL_AD_DECODE_ERROR;
  1481. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_EXTRA_DATA_IN_MESSAGE);
  1482. goto f_err;
  1483. }
  1484. }
  1485. EVP_PKEY_free(pkey);
  1486. EVP_MD_CTX_cleanup(&md_ctx);
  1487. return(1);
  1488. f_err:
  1489. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1490. err:
  1491. EVP_PKEY_free(pkey);
  1492. #ifndef OPENSSL_NO_RSA
  1493. if (rsa != NULL)
  1494. RSA_free(rsa);
  1495. #endif
  1496. #ifndef OPENSSL_NO_DH
  1497. if (dh != NULL)
  1498. DH_free(dh);
  1499. #endif
  1500. #ifndef OPENSSL_NO_ECDH
  1501. BN_CTX_free(bn_ctx);
  1502. EC_POINT_free(srvr_ecpoint);
  1503. if (ecdh != NULL)
  1504. EC_KEY_free(ecdh);
  1505. #endif
  1506. EVP_MD_CTX_cleanup(&md_ctx);
  1507. return(-1);
  1508. }
  1509. int ssl3_get_certificate_request(SSL *s)
  1510. {
  1511. int ok,ret=0;
  1512. unsigned long n,nc,l;
  1513. unsigned int llen,ctype_num,i;
  1514. X509_NAME *xn=NULL;
  1515. const unsigned char *p,*q;
  1516. unsigned char *d;
  1517. STACK_OF(X509_NAME) *ca_sk=NULL;
  1518. n=s->method->ssl_get_message(s,
  1519. SSL3_ST_CR_CERT_REQ_A,
  1520. SSL3_ST_CR_CERT_REQ_B,
  1521. -1,
  1522. s->max_cert_list,
  1523. &ok);
  1524. if (!ok) return((int)n);
  1525. s->s3->tmp.cert_req=0;
  1526. if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)
  1527. {
  1528. s->s3->tmp.reuse_message=1;
  1529. return(1);
  1530. }
  1531. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST)
  1532. {
  1533. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
  1534. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_WRONG_MESSAGE_TYPE);
  1535. goto err;
  1536. }
  1537. /* TLS does not like anon-DH with client cert */
  1538. if (s->version > SSL3_VERSION)
  1539. {
  1540. if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
  1541. {
  1542. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
  1543. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
  1544. goto err;
  1545. }
  1546. }
  1547. p=d=(unsigned char *)s->init_msg;
  1548. if ((ca_sk=sk_X509_NAME_new(ca_dn_cmp)) == NULL)
  1549. {
  1550. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
  1551. goto err;
  1552. }
  1553. /* get the certificate types */
  1554. ctype_num= *(p++);
  1555. if (ctype_num > SSL3_CT_NUMBER)
  1556. ctype_num=SSL3_CT_NUMBER;
  1557. for (i=0; i<ctype_num; i++)
  1558. s->s3->tmp.ctype[i]= p[i];
  1559. p+=ctype_num;
  1560. /* get the CA RDNs */
  1561. n2s(p,llen);
  1562. #if 0
  1563. {
  1564. FILE *out;
  1565. out=fopen("/tmp/vsign.der","w");
  1566. fwrite(p,1,llen,out);
  1567. fclose(out);
  1568. }
  1569. #endif
  1570. if ((llen+ctype_num+2+1) != n)
  1571. {
  1572. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1573. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_LENGTH_MISMATCH);
  1574. goto err;
  1575. }
  1576. for (nc=0; nc<llen; )
  1577. {
  1578. n2s(p,l);
  1579. if ((l+nc+2) > llen)
  1580. {
  1581. if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
  1582. goto cont; /* netscape bugs */
  1583. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1584. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_TOO_LONG);
  1585. goto err;
  1586. }
  1587. q=p;
  1588. if ((xn=d2i_X509_NAME(NULL,&q,l)) == NULL)
  1589. {
  1590. /* If netscape tolerance is on, ignore errors */
  1591. if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG)
  1592. goto cont;
  1593. else
  1594. {
  1595. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1596. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_ASN1_LIB);
  1597. goto err;
  1598. }
  1599. }
  1600. if (q != (p+l))
  1601. {
  1602. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1603. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_LENGTH_MISMATCH);
  1604. goto err;
  1605. }
  1606. if (!sk_X509_NAME_push(ca_sk,xn))
  1607. {
  1608. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
  1609. goto err;
  1610. }
  1611. p+=l;
  1612. nc+=l+2;
  1613. }
  1614. if (0)
  1615. {
  1616. cont:
  1617. ERR_clear_error();
  1618. }
  1619. /* we should setup a certificate to return.... */
  1620. s->s3->tmp.cert_req=1;
  1621. s->s3->tmp.ctype_num=ctype_num;
  1622. if (s->s3->tmp.ca_names != NULL)
  1623. sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
  1624. s->s3->tmp.ca_names=ca_sk;
  1625. ca_sk=NULL;
  1626. ret=1;
  1627. err:
  1628. if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free);
  1629. return(ret);
  1630. }
  1631. static int ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b)
  1632. {
  1633. return(X509_NAME_cmp(*a,*b));
  1634. }
  1635. #ifndef OPENSSL_NO_TLSEXT
  1636. int ssl3_get_new_session_ticket(SSL *s)
  1637. {
  1638. int ok,al,ret=0, ticklen;
  1639. long n;
  1640. const unsigned char *p;
  1641. unsigned char *d;
  1642. n=s->method->ssl_get_message(s,
  1643. SSL3_ST_CR_SESSION_TICKET_A,
  1644. SSL3_ST_CR_SESSION_TICKET_B,
  1645. -1,
  1646. 16384,
  1647. &ok);
  1648. if (!ok)
  1649. return((int)n);
  1650. if (s->s3->tmp.message_type == SSL3_MT_FINISHED)
  1651. {
  1652. s->s3->tmp.reuse_message=1;
  1653. return(1);
  1654. }
  1655. if (s->s3->tmp.message_type != SSL3_MT_NEWSESSION_TICKET)
  1656. {
  1657. al=SSL_AD_UNEXPECTED_MESSAGE;
  1658. SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,SSL_R_BAD_MESSAGE_TYPE);
  1659. goto f_err;
  1660. }
  1661. if (n < 6)
  1662. {
  1663. /* need at least ticket_lifetime_hint + ticket length */
  1664. al = SSL3_AL_FATAL,SSL_AD_DECODE_ERROR;
  1665. SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,SSL_R_LENGTH_MISMATCH);
  1666. goto f_err;
  1667. }
  1668. p=d=(unsigned char *)s->init_msg;
  1669. n2l(p, s->session->tlsext_tick_lifetime_hint);
  1670. n2s(p, ticklen);
  1671. /* ticket_lifetime_hint + ticket_length + ticket */
  1672. if (ticklen + 6 != n)
  1673. {
  1674. al = SSL3_AL_FATAL,SSL_AD_DECODE_ERROR;
  1675. SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,SSL_R_LENGTH_MISMATCH);
  1676. goto f_err;
  1677. }
  1678. if (s->session->tlsext_tick)
  1679. {
  1680. OPENSSL_free(s->session->tlsext_tick);
  1681. s->session->tlsext_ticklen = 0;
  1682. }
  1683. s->session->tlsext_tick = OPENSSL_malloc(ticklen);
  1684. if (!s->session->tlsext_tick)
  1685. {
  1686. SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,ERR_R_MALLOC_FAILURE);
  1687. goto err;
  1688. }
  1689. memcpy(s->session->tlsext_tick, p, ticklen);
  1690. s->session->tlsext_ticklen = ticklen;
  1691. /* There are two ways to detect a resumed ticket sesion.
  1692. * One is to set an appropriate session ID and then the server
  1693. * must return a match in ServerHello. This allows the normal
  1694. * client session ID matching to work and we know much
  1695. * earlier that the ticket has been accepted.
  1696. *
  1697. * The other way is to set zero length session ID when the
  1698. * ticket is presented and rely on the handshake to determine
  1699. * session resumption.
  1700. *
  1701. * We choose the former approach because this fits in with
  1702. * assumptions elsewhere in OpenSSL. The session ID is set
  1703. * to the SHA256 (or SHA1 is SHA256 is disabled) hash of the
  1704. * ticket.
  1705. */
  1706. EVP_Digest(p, ticklen,
  1707. s->session->session_id, &s->session->session_id_length,
  1708. #ifndef OPENSSL_NO_SHA256
  1709. EVP_sha256(), NULL);
  1710. #else
  1711. EVP_sha1(), NULL);
  1712. #endif
  1713. ret=1;
  1714. return(ret);
  1715. f_err:
  1716. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1717. err:
  1718. return(-1);
  1719. }
  1720. int ssl3_get_cert_status(SSL *s)
  1721. {
  1722. int ok, al;
  1723. unsigned long resplen,n;
  1724. const unsigned char *p;
  1725. n=s->method->ssl_get_message(s,
  1726. SSL3_ST_CR_CERT_STATUS_A,
  1727. SSL3_ST_CR_CERT_STATUS_B,
  1728. SSL3_MT_CERTIFICATE_STATUS,
  1729. 16384,
  1730. &ok);
  1731. if (!ok) return((int)n);
  1732. if (n < 4)
  1733. {
  1734. /* need at least status type + length */
  1735. al = SSL_AD_DECODE_ERROR;
  1736. SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_LENGTH_MISMATCH);
  1737. goto f_err;
  1738. }
  1739. p = (unsigned char *)s->init_msg;
  1740. if (*p++ != TLSEXT_STATUSTYPE_ocsp)
  1741. {
  1742. al = SSL_AD_DECODE_ERROR;
  1743. SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_UNSUPPORTED_STATUS_TYPE);
  1744. goto f_err;
  1745. }
  1746. n2l3(p, resplen);
  1747. if (resplen + 4 != n)
  1748. {
  1749. al = SSL_AD_DECODE_ERROR;
  1750. SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_LENGTH_MISMATCH);
  1751. goto f_err;
  1752. }
  1753. if (s->tlsext_ocsp_resp)
  1754. OPENSSL_free(s->tlsext_ocsp_resp);
  1755. s->tlsext_ocsp_resp = BUF_memdup(p, resplen);
  1756. if (!s->tlsext_ocsp_resp)
  1757. {
  1758. al = SSL_AD_INTERNAL_ERROR;
  1759. SSLerr(SSL_F_SSL3_GET_CERT_STATUS,ERR_R_MALLOC_FAILURE);
  1760. goto f_err;
  1761. }
  1762. s->tlsext_ocsp_resplen = resplen;
  1763. if (s->ctx->tlsext_status_cb)
  1764. {
  1765. int ret;
  1766. ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
  1767. if (ret == 0)
  1768. {
  1769. al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
  1770. SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_INVALID_STATUS_RESPONSE);
  1771. goto f_err;
  1772. }
  1773. if (ret < 0)
  1774. {
  1775. al = SSL_AD_INTERNAL_ERROR;
  1776. SSLerr(SSL_F_SSL3_GET_CERT_STATUS,ERR_R_MALLOC_FAILURE);
  1777. goto f_err;
  1778. }
  1779. }
  1780. return 1;
  1781. f_err:
  1782. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1783. return(-1);
  1784. }
  1785. #endif
  1786. int ssl3_get_server_done(SSL *s)
  1787. {
  1788. int ok,ret=0;
  1789. long n;
  1790. n=s->method->ssl_get_message(s,
  1791. SSL3_ST_CR_SRVR_DONE_A,
  1792. SSL3_ST_CR_SRVR_DONE_B,
  1793. SSL3_MT_SERVER_DONE,
  1794. 30, /* should be very small, like 0 :-) */
  1795. &ok);
  1796. if (!ok) return((int)n);
  1797. if (n > 0)
  1798. {
  1799. /* should contain no data */
  1800. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1801. SSLerr(SSL_F_SSL3_GET_SERVER_DONE,SSL_R_LENGTH_MISMATCH);
  1802. return -1;
  1803. }
  1804. ret=1;
  1805. return(ret);
  1806. }
  1807. int ssl3_send_client_key_exchange(SSL *s)
  1808. {
  1809. unsigned char *p,*d;
  1810. int n;
  1811. unsigned long alg_k;
  1812. #ifndef OPENSSL_NO_RSA
  1813. unsigned char *q;
  1814. EVP_PKEY *pkey=NULL;
  1815. #endif
  1816. #ifndef OPENSSL_NO_KRB5
  1817. KSSL_ERR kssl_err;
  1818. #endif /* OPENSSL_NO_KRB5 */
  1819. #ifndef OPENSSL_NO_ECDH
  1820. EC_KEY *clnt_ecdh = NULL;
  1821. const EC_POINT *srvr_ecpoint = NULL;
  1822. EVP_PKEY *srvr_pub_pkey = NULL;
  1823. unsigned char *encodedPoint = NULL;
  1824. int encoded_pt_len = 0;
  1825. BN_CTX * bn_ctx = NULL;
  1826. #endif
  1827. if (s->state == SSL3_ST_CW_KEY_EXCH_A)
  1828. {
  1829. d=(unsigned char *)s->init_buf->data;
  1830. p= &(d[4]);
  1831. alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
  1832. /* Fool emacs indentation */
  1833. if (0) {}
  1834. #ifndef OPENSSL_NO_RSA
  1835. else if (alg_k & SSL_kRSA)
  1836. {
  1837. RSA *rsa;
  1838. unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
  1839. if (s->session->sess_cert->peer_rsa_tmp != NULL)
  1840. rsa=s->session->sess_cert->peer_rsa_tmp;
  1841. else
  1842. {
  1843. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1844. if ((pkey == NULL) ||
  1845. (pkey->type != EVP_PKEY_RSA) ||
  1846. (pkey->pkey.rsa == NULL))
  1847. {
  1848. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
  1849. goto err;
  1850. }
  1851. rsa=pkey->pkey.rsa;
  1852. EVP_PKEY_free(pkey);
  1853. }
  1854. tmp_buf[0]=s->client_version>>8;
  1855. tmp_buf[1]=s->client_version&0xff;
  1856. if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
  1857. goto err;
  1858. s->session->master_key_length=sizeof tmp_buf;
  1859. q=p;
  1860. /* Fix buf for TLS and beyond */
  1861. if (s->version > SSL3_VERSION)
  1862. p+=2;
  1863. n=RSA_public_encrypt(sizeof tmp_buf,
  1864. tmp_buf,p,rsa,RSA_PKCS1_PADDING);
  1865. #ifdef PKCS1_CHECK
  1866. if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;
  1867. if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;
  1868. #endif
  1869. if (n <= 0)
  1870. {
  1871. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT);
  1872. goto err;
  1873. }
  1874. /* Fix buf for TLS and beyond */
  1875. if (s->version > SSL3_VERSION)
  1876. {
  1877. s2n(n,q);
  1878. n+=2;
  1879. }
  1880. s->session->master_key_length=
  1881. s->method->ssl3_enc->generate_master_secret(s,
  1882. s->session->master_key,
  1883. tmp_buf,sizeof tmp_buf);
  1884. OPENSSL_cleanse(tmp_buf,sizeof tmp_buf);
  1885. }
  1886. #endif
  1887. #ifndef OPENSSL_NO_KRB5
  1888. else if (alg_k & SSL_kKRB5)
  1889. {
  1890. krb5_error_code krb5rc;
  1891. KSSL_CTX *kssl_ctx = s->kssl_ctx;
  1892. /* krb5_data krb5_ap_req; */
  1893. krb5_data *enc_ticket;
  1894. krb5_data authenticator, *authp = NULL;
  1895. EVP_CIPHER_CTX ciph_ctx;
  1896. const EVP_CIPHER *enc = NULL;
  1897. unsigned char iv[EVP_MAX_IV_LENGTH];
  1898. unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
  1899. unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH
  1900. + EVP_MAX_IV_LENGTH];
  1901. int padl, outl = sizeof(epms);
  1902. EVP_CIPHER_CTX_init(&ciph_ctx);
  1903. #ifdef KSSL_DEBUG
  1904. printf("ssl3_send_client_key_exchange(%lx & %lx)\n",
  1905. alg_k, SSL_kKRB5);
  1906. #endif /* KSSL_DEBUG */
  1907. authp = NULL;
  1908. #ifdef KRB5SENDAUTH
  1909. if (KRB5SENDAUTH) authp = &authenticator;
  1910. #endif /* KRB5SENDAUTH */
  1911. krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp,
  1912. &kssl_err);
  1913. enc = kssl_map_enc(kssl_ctx->enctype);
  1914. if (enc == NULL)
  1915. goto err;
  1916. #ifdef KSSL_DEBUG
  1917. {
  1918. printf("kssl_cget_tkt rtn %d\n", krb5rc);
  1919. if (krb5rc && kssl_err.text)
  1920. printf("kssl_cget_tkt kssl_err=%s\n", kssl_err.text);
  1921. }
  1922. #endif /* KSSL_DEBUG */
  1923. if (krb5rc)
  1924. {
  1925. ssl3_send_alert(s,SSL3_AL_FATAL,
  1926. SSL_AD_HANDSHAKE_FAILURE);
  1927. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  1928. kssl_err.reason);
  1929. goto err;
  1930. }
  1931. /* 20010406 VRS - Earlier versions used KRB5 AP_REQ
  1932. ** in place of RFC 2712 KerberosWrapper, as in:
  1933. **
  1934. ** Send ticket (copy to *p, set n = length)
  1935. ** n = krb5_ap_req.length;
  1936. ** memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
  1937. ** if (krb5_ap_req.data)
  1938. ** kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
  1939. **
  1940. ** Now using real RFC 2712 KerberosWrapper
  1941. ** (Thanks to Simon Wilkinson <sxw@sxw.org.uk>)
  1942. ** Note: 2712 "opaque" types are here replaced
  1943. ** with a 2-byte length followed by the value.
  1944. ** Example:
  1945. ** KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
  1946. ** Where "xx xx" = length bytes. Shown here with
  1947. ** optional authenticator omitted.
  1948. */
  1949. /* KerberosWrapper.Ticket */
  1950. s2n(enc_ticket->length,p);
  1951. memcpy(p, enc_ticket->data, enc_ticket->length);
  1952. p+= enc_ticket->length;
  1953. n = enc_ticket->length + 2;
  1954. /* KerberosWrapper.Authenticator */
  1955. if (authp && authp->length)
  1956. {
  1957. s2n(authp->length,p);
  1958. memcpy(p, authp->data, authp->length);
  1959. p+= authp->length;
  1960. n+= authp->length + 2;
  1961. free(authp->data);
  1962. authp->data = NULL;
  1963. authp->length = 0;
  1964. }
  1965. else
  1966. {
  1967. s2n(0,p);/* null authenticator length */
  1968. n+=2;
  1969. }
  1970. tmp_buf[0]=s->client_version>>8;
  1971. tmp_buf[1]=s->client_version&0xff;
  1972. if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
  1973. goto err;
  1974. /* 20010420 VRS. Tried it this way; failed.
  1975. ** EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
  1976. ** EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
  1977. ** kssl_ctx->length);
  1978. ** EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
  1979. */
  1980. memset(iv, 0, sizeof iv); /* per RFC 1510 */
  1981. EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,
  1982. kssl_ctx->key,iv);
  1983. EVP_EncryptUpdate(&ciph_ctx,epms,&outl,tmp_buf,
  1984. sizeof tmp_buf);
  1985. EVP_EncryptFinal_ex(&ciph_ctx,&(epms[outl]),&padl);
  1986. outl += padl;
  1987. if (outl > (int)sizeof epms)
  1988. {
  1989. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  1990. goto err;
  1991. }
  1992. EVP_CIPHER_CTX_cleanup(&ciph_ctx);
  1993. /* KerberosWrapper.EncryptedPreMasterSecret */
  1994. s2n(outl,p);
  1995. memcpy(p, epms, outl);
  1996. p+=outl;
  1997. n+=outl + 2;
  1998. s->session->master_key_length=
  1999. s->method->ssl3_enc->generate_master_secret(s,
  2000. s->session->master_key,
  2001. tmp_buf, sizeof tmp_buf);
  2002. OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
  2003. OPENSSL_cleanse(epms, outl);
  2004. }
  2005. #endif
  2006. #ifndef OPENSSL_NO_DH
  2007. else if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
  2008. {
  2009. DH *dh_srvr,*dh_clnt;
  2010. if (s->session->sess_cert == NULL)
  2011. {
  2012. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
  2013. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNEXPECTED_MESSAGE);
  2014. goto err;
  2015. }
  2016. if (s->session->sess_cert->peer_dh_tmp != NULL)
  2017. dh_srvr=s->session->sess_cert->peer_dh_tmp;
  2018. else
  2019. {
  2020. /* we get them from the cert */
  2021. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
  2022. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
  2023. goto err;
  2024. }
  2025. /* generate a new random key */
  2026. if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
  2027. {
  2028. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
  2029. goto err;
  2030. }
  2031. if (!DH_generate_key(dh_clnt))
  2032. {
  2033. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
  2034. goto err;
  2035. }
  2036. /* use the 'p' output buffer for the DH key, but
  2037. * make sure to clear it out afterwards */
  2038. n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
  2039. if (n <= 0)
  2040. {
  2041. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
  2042. goto err;
  2043. }
  2044. /* generate master key from the result */
  2045. s->session->master_key_length=
  2046. s->method->ssl3_enc->generate_master_secret(s,
  2047. s->session->master_key,p,n);
  2048. /* clean up */
  2049. memset(p,0,n);
  2050. /* send off the data */
  2051. n=BN_num_bytes(dh_clnt->pub_key);
  2052. s2n(n,p);
  2053. BN_bn2bin(dh_clnt->pub_key,p);
  2054. n+=2;
  2055. DH_free(dh_clnt);
  2056. /* perhaps clean things up a bit EAY EAY EAY EAY*/
  2057. }
  2058. #endif
  2059. #ifndef OPENSSL_NO_ECDH
  2060. else if (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe))
  2061. {
  2062. const EC_GROUP *srvr_group = NULL;
  2063. EC_KEY *tkey;
  2064. int ecdh_clnt_cert = 0;
  2065. int field_size = 0;
  2066. /* Did we send out the client's
  2067. * ECDH share for use in premaster
  2068. * computation as part of client certificate?
  2069. * If so, set ecdh_clnt_cert to 1.
  2070. */
  2071. if ((alg_k & (SSL_kECDHr|SSL_kECDHe)) && (s->cert != NULL))
  2072. {
  2073. /* XXX: For now, we do not support client
  2074. * authentication using ECDH certificates.
  2075. * To add such support, one needs to add
  2076. * code that checks for appropriate
  2077. * conditions and sets ecdh_clnt_cert to 1.
  2078. * For example, the cert have an ECC
  2079. * key on the same curve as the server's
  2080. * and the key should be authorized for
  2081. * key agreement.
  2082. *
  2083. * One also needs to add code in ssl3_connect
  2084. * to skip sending the certificate verify
  2085. * message.
  2086. *
  2087. * if ((s->cert->key->privatekey != NULL) &&
  2088. * (s->cert->key->privatekey->type ==
  2089. * EVP_PKEY_EC) && ...)
  2090. * ecdh_clnt_cert = 1;
  2091. */
  2092. }
  2093. if (s->session->sess_cert->peer_ecdh_tmp != NULL)
  2094. {
  2095. tkey = s->session->sess_cert->peer_ecdh_tmp;
  2096. }
  2097. else
  2098. {
  2099. /* Get the Server Public Key from Cert */
  2100. srvr_pub_pkey = X509_get_pubkey(s->session-> \
  2101. sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
  2102. if ((srvr_pub_pkey == NULL) ||
  2103. (srvr_pub_pkey->type != EVP_PKEY_EC) ||
  2104. (srvr_pub_pkey->pkey.ec == NULL))
  2105. {
  2106. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2107. ERR_R_INTERNAL_ERROR);
  2108. goto err;
  2109. }
  2110. tkey = srvr_pub_pkey->pkey.ec;
  2111. }
  2112. srvr_group = EC_KEY_get0_group(tkey);
  2113. srvr_ecpoint = EC_KEY_get0_public_key(tkey);
  2114. if ((srvr_group == NULL) || (srvr_ecpoint == NULL))
  2115. {
  2116. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2117. ERR_R_INTERNAL_ERROR);
  2118. goto err;
  2119. }
  2120. if ((clnt_ecdh=EC_KEY_new()) == NULL)
  2121. {
  2122. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  2123. goto err;
  2124. }
  2125. if (!EC_KEY_set_group(clnt_ecdh, srvr_group))
  2126. {
  2127. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB);
  2128. goto err;
  2129. }
  2130. if (ecdh_clnt_cert)
  2131. {
  2132. /* Reuse key info from our certificate
  2133. * We only need our private key to perform
  2134. * the ECDH computation.
  2135. */
  2136. const BIGNUM *priv_key;
  2137. tkey = s->cert->key->privatekey->pkey.ec;
  2138. priv_key = EC_KEY_get0_private_key(tkey);
  2139. if (priv_key == NULL)
  2140. {
  2141. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  2142. goto err;
  2143. }
  2144. if (!EC_KEY_set_private_key(clnt_ecdh, priv_key))
  2145. {
  2146. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB);
  2147. goto err;
  2148. }
  2149. }
  2150. else
  2151. {
  2152. /* Generate a new ECDH key pair */
  2153. if (!(EC_KEY_generate_key(clnt_ecdh)))
  2154. {
  2155. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
  2156. goto err;
  2157. }
  2158. }
  2159. /* use the 'p' output buffer for the ECDH key, but
  2160. * make sure to clear it out afterwards
  2161. */
  2162. field_size = EC_GROUP_get_degree(srvr_group);
  2163. if (field_size <= 0)
  2164. {
  2165. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2166. ERR_R_ECDH_LIB);
  2167. goto err;
  2168. }
  2169. n=ECDH_compute_key(p, (field_size+7)/8, srvr_ecpoint, clnt_ecdh, NULL);
  2170. if (n <= 0)
  2171. {
  2172. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2173. ERR_R_ECDH_LIB);
  2174. goto err;
  2175. }
  2176. /* generate master key from the result */
  2177. s->session->master_key_length = s->method->ssl3_enc \
  2178. -> generate_master_secret(s,
  2179. s->session->master_key,
  2180. p, n);
  2181. memset(p, 0, n); /* clean up */
  2182. if (ecdh_clnt_cert)
  2183. {
  2184. /* Send empty client key exch message */
  2185. n = 0;
  2186. }
  2187. else
  2188. {
  2189. /* First check the size of encoding and
  2190. * allocate memory accordingly.
  2191. */
  2192. encoded_pt_len =
  2193. EC_POINT_point2oct(srvr_group,
  2194. EC_KEY_get0_public_key(clnt_ecdh),
  2195. POINT_CONVERSION_UNCOMPRESSED,
  2196. NULL, 0, NULL);
  2197. encodedPoint = (unsigned char *)
  2198. OPENSSL_malloc(encoded_pt_len *
  2199. sizeof(unsigned char));
  2200. bn_ctx = BN_CTX_new();
  2201. if ((encodedPoint == NULL) ||
  2202. (bn_ctx == NULL))
  2203. {
  2204. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  2205. goto err;
  2206. }
  2207. /* Encode the public key */
  2208. n = EC_POINT_point2oct(srvr_group,
  2209. EC_KEY_get0_public_key(clnt_ecdh),
  2210. POINT_CONVERSION_UNCOMPRESSED,
  2211. encodedPoint, encoded_pt_len, bn_ctx);
  2212. *p = n; /* length of encoded point */
  2213. /* Encoded point will be copied here */
  2214. p += 1;
  2215. /* copy the point */
  2216. memcpy((unsigned char *)p, encodedPoint, n);
  2217. /* increment n to account for length field */
  2218. n += 1;
  2219. }
  2220. /* Free allocated memory */
  2221. BN_CTX_free(bn_ctx);
  2222. if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
  2223. if (clnt_ecdh != NULL)
  2224. EC_KEY_free(clnt_ecdh);
  2225. EVP_PKEY_free(srvr_pub_pkey);
  2226. }
  2227. #endif /* !OPENSSL_NO_ECDH */
  2228. else if (alg_k & SSL_kGOST)
  2229. {
  2230. /* GOST key exchange message creation */
  2231. EVP_PKEY_CTX *pkey_ctx;
  2232. X509 *peer_cert;
  2233. size_t msglen;
  2234. unsigned int md_len;
  2235. int keytype;
  2236. unsigned char premaster_secret[32],shared_ukm[32], tmp[256];
  2237. EVP_MD_CTX *ukm_hash;
  2238. EVP_PKEY *pub_key;
  2239. /* Get server sertificate PKEY and create ctx from it */
  2240. peer_cert=s->session->sess_cert->peer_pkeys[(keytype=SSL_PKEY_GOST01)].x509;
  2241. if (!peer_cert)
  2242. peer_cert=s->session->sess_cert->peer_pkeys[(keytype=SSL_PKEY_GOST94)].x509;
  2243. if (!peer_cert) {
  2244. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
  2245. goto err;
  2246. }
  2247. pkey_ctx=EVP_PKEY_CTX_new(pub_key=X509_get_pubkey(peer_cert),NULL);
  2248. /* If we have send a certificate, and certificate key
  2249. * parameters match those of server certificate, use
  2250. * certificate key for key exchange
  2251. */
  2252. /* Otherwise, generate ephemeral key pair */
  2253. EVP_PKEY_encrypt_init(pkey_ctx);
  2254. /* Generate session key */
  2255. RAND_bytes(premaster_secret,32);
  2256. /* If we have client certificate, use its secret as peer key */
  2257. if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
  2258. if (EVP_PKEY_derive_set_peer(pkey_ctx,s->cert->key->privatekey) <=0) {
  2259. /* If there was an error - just ignore it. Ephemeral key
  2260. * would be used
  2261. */
  2262. ERR_clear_error();
  2263. }
  2264. }
  2265. /* Compute shared IV and store it in algorithm-specific
  2266. * context data */
  2267. ukm_hash = EVP_MD_CTX_create();
  2268. EVP_DigestInit(ukm_hash,EVP_get_digestbynid(NID_id_GostR3411_94));
  2269. EVP_DigestUpdate(ukm_hash,s->s3->client_random,SSL3_RANDOM_SIZE);
  2270. EVP_DigestUpdate(ukm_hash,s->s3->server_random,SSL3_RANDOM_SIZE);
  2271. EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len);
  2272. EVP_MD_CTX_destroy(ukm_hash);
  2273. if (EVP_PKEY_CTX_ctrl(pkey_ctx,-1,EVP_PKEY_OP_ENCRYPT,EVP_PKEY_CTRL_SET_IV,
  2274. 8,shared_ukm)<0) {
  2275. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2276. SSL_R_LIBRARY_BUG);
  2277. goto err;
  2278. }
  2279. /* Make GOST keytransport blob message */
  2280. /*Encapsulate it into sequence */
  2281. *(p++)=V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
  2282. msglen=255;
  2283. if (EVP_PKEY_encrypt(pkey_ctx,tmp,&msglen,premaster_secret,32)<0) {
  2284. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2285. SSL_R_LIBRARY_BUG);
  2286. goto err;
  2287. }
  2288. if (msglen >= 0x80)
  2289. {
  2290. *(p++)=0x81;
  2291. *(p++)= msglen & 0xff;
  2292. n=msglen+3;
  2293. }
  2294. else
  2295. {
  2296. *(p++)= msglen & 0xff;
  2297. n=msglen+2;
  2298. }
  2299. memcpy(p, tmp, msglen);
  2300. /* Check if pubkey from client certificate was used */
  2301. if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0)
  2302. {
  2303. /* Set flag "skip certificate verify" */
  2304. s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
  2305. }
  2306. EVP_PKEY_CTX_free(pkey_ctx);
  2307. s->session->master_key_length=
  2308. s->method->ssl3_enc->generate_master_secret(s,
  2309. s->session->master_key,premaster_secret,32);
  2310. EVP_PKEY_free(pub_key);
  2311. }
  2312. #ifndef OPENSSL_NO_PSK
  2313. else if (alg_k & SSL_kPSK)
  2314. {
  2315. char identity[PSK_MAX_IDENTITY_LEN];
  2316. unsigned char *t = NULL;
  2317. unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN*2+4];
  2318. unsigned int pre_ms_len = 0, psk_len = 0;
  2319. int psk_err = 1;
  2320. n = 0;
  2321. if (s->psk_client_callback == NULL)
  2322. {
  2323. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2324. SSL_R_PSK_NO_CLIENT_CB);
  2325. goto err;
  2326. }
  2327. psk_len = s->psk_client_callback(s, s->ctx->psk_identity_hint,
  2328. identity, PSK_MAX_IDENTITY_LEN,
  2329. psk_or_pre_ms, sizeof(psk_or_pre_ms));
  2330. if (psk_len > PSK_MAX_PSK_LEN)
  2331. {
  2332. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2333. ERR_R_INTERNAL_ERROR);
  2334. goto psk_err;
  2335. }
  2336. else if (psk_len == 0)
  2337. {
  2338. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2339. SSL_R_PSK_IDENTITY_NOT_FOUND);
  2340. goto psk_err;
  2341. }
  2342. /* create PSK pre_master_secret */
  2343. pre_ms_len = 2+psk_len+2+psk_len;
  2344. t = psk_or_pre_ms;
  2345. memmove(psk_or_pre_ms+psk_len+4, psk_or_pre_ms, psk_len);
  2346. s2n(psk_len, t);
  2347. memset(t, 0, psk_len);
  2348. t+=psk_len;
  2349. s2n(psk_len, t);
  2350. if (s->session->psk_identity_hint != NULL)
  2351. OPENSSL_free(s->session->psk_identity_hint);
  2352. s->session->psk_identity_hint = BUF_strdup(s->ctx->psk_identity_hint);
  2353. if (s->ctx->psk_identity_hint != NULL &&
  2354. s->session->psk_identity_hint == NULL)
  2355. {
  2356. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2357. ERR_R_MALLOC_FAILURE);
  2358. goto psk_err;
  2359. }
  2360. if (s->session->psk_identity != NULL)
  2361. OPENSSL_free(s->session->psk_identity);
  2362. s->session->psk_identity = BUF_strdup(identity);
  2363. if (s->session->psk_identity == NULL)
  2364. {
  2365. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2366. ERR_R_MALLOC_FAILURE);
  2367. goto psk_err;
  2368. }
  2369. s->session->master_key_length =
  2370. s->method->ssl3_enc->generate_master_secret(s,
  2371. s->session->master_key,
  2372. psk_or_pre_ms, pre_ms_len);
  2373. n = strlen(identity);
  2374. s2n(n, p);
  2375. memcpy(p, identity, n);
  2376. n+=2;
  2377. psk_err = 0;
  2378. psk_err:
  2379. OPENSSL_cleanse(identity, PSK_MAX_IDENTITY_LEN);
  2380. OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
  2381. if (psk_err != 0)
  2382. {
  2383. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  2384. goto err;
  2385. }
  2386. }
  2387. #endif
  2388. else
  2389. {
  2390. ssl3_send_alert(s, SSL3_AL_FATAL,
  2391. SSL_AD_HANDSHAKE_FAILURE);
  2392. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2393. ERR_R_INTERNAL_ERROR);
  2394. goto err;
  2395. }
  2396. *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE;
  2397. l2n3(n,d);
  2398. s->state=SSL3_ST_CW_KEY_EXCH_B;
  2399. /* number of bytes to write */
  2400. s->init_num=n+4;
  2401. s->init_off=0;
  2402. }
  2403. /* SSL3_ST_CW_KEY_EXCH_B */
  2404. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  2405. err:
  2406. #ifndef OPENSSL_NO_ECDH
  2407. BN_CTX_free(bn_ctx);
  2408. if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
  2409. if (clnt_ecdh != NULL)
  2410. EC_KEY_free(clnt_ecdh);
  2411. EVP_PKEY_free(srvr_pub_pkey);
  2412. #endif
  2413. return(-1);
  2414. }
  2415. int ssl3_send_client_verify(SSL *s)
  2416. {
  2417. unsigned char *p,*d;
  2418. unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
  2419. EVP_PKEY *pkey;
  2420. EVP_PKEY_CTX *pctx=NULL;
  2421. #ifndef OPENSSL_NO_RSA
  2422. unsigned u=0;
  2423. #endif
  2424. unsigned long n;
  2425. int j;
  2426. if (s->state == SSL3_ST_CW_CERT_VRFY_A)
  2427. {
  2428. d=(unsigned char *)s->init_buf->data;
  2429. p= &(d[4]);
  2430. pkey=s->cert->key->privatekey;
  2431. /* Create context from key and test if sha1 is allowed as digest */
  2432. pctx = EVP_PKEY_CTX_new(pkey,NULL);
  2433. EVP_PKEY_sign_init(pctx);
  2434. if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1())>0)
  2435. {
  2436. s->method->ssl3_enc->cert_verify_mac(s,
  2437. NID_sha1,
  2438. &(data[MD5_DIGEST_LENGTH]));
  2439. }
  2440. else
  2441. {
  2442. ERR_clear_error();
  2443. }
  2444. #ifndef OPENSSL_NO_RSA
  2445. if (pkey->type == EVP_PKEY_RSA)
  2446. {
  2447. s->method->ssl3_enc->cert_verify_mac(s,
  2448. NID_md5,
  2449. &(data[0]));
  2450. if (RSA_sign(NID_md5_sha1, data,
  2451. MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
  2452. &(p[2]), &u, pkey->pkey.rsa) <= 0 )
  2453. {
  2454. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_RSA_LIB);
  2455. goto err;
  2456. }
  2457. s2n(u,p);
  2458. n=u+2;
  2459. }
  2460. else
  2461. #endif
  2462. #ifndef OPENSSL_NO_DSA
  2463. if (pkey->type == EVP_PKEY_DSA)
  2464. {
  2465. if (!DSA_sign(pkey->save_type,
  2466. &(data[MD5_DIGEST_LENGTH]),
  2467. SHA_DIGEST_LENGTH,&(p[2]),
  2468. (unsigned int *)&j,pkey->pkey.dsa))
  2469. {
  2470. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_DSA_LIB);
  2471. goto err;
  2472. }
  2473. s2n(j,p);
  2474. n=j+2;
  2475. }
  2476. else
  2477. #endif
  2478. #ifndef OPENSSL_NO_ECDSA
  2479. if (pkey->type == EVP_PKEY_EC)
  2480. {
  2481. if (!ECDSA_sign(pkey->save_type,
  2482. &(data[MD5_DIGEST_LENGTH]),
  2483. SHA_DIGEST_LENGTH,&(p[2]),
  2484. (unsigned int *)&j,pkey->pkey.ec))
  2485. {
  2486. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
  2487. ERR_R_ECDSA_LIB);
  2488. goto err;
  2489. }
  2490. s2n(j,p);
  2491. n=j+2;
  2492. }
  2493. else
  2494. #endif
  2495. if (pkey->type == NID_id_GostR3410_94 || pkey->type == NID_id_GostR3410_2001)
  2496. {
  2497. unsigned char signbuf[64];
  2498. int i;
  2499. size_t sigsize=64;
  2500. s->method->ssl3_enc->cert_verify_mac(s,
  2501. NID_id_GostR3411_94,
  2502. data);
  2503. if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) {
  2504. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
  2505. ERR_R_INTERNAL_ERROR);
  2506. goto err;
  2507. }
  2508. for (i=63,j=0; i>=0; j++, i--) {
  2509. p[2+j]=signbuf[i];
  2510. }
  2511. s2n(j,p);
  2512. n=j+2;
  2513. }
  2514. else
  2515. {
  2516. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_INTERNAL_ERROR);
  2517. goto err;
  2518. }
  2519. *(d++)=SSL3_MT_CERTIFICATE_VERIFY;
  2520. l2n3(n,d);
  2521. s->state=SSL3_ST_CW_CERT_VRFY_B;
  2522. s->init_num=(int)n+4;
  2523. s->init_off=0;
  2524. }
  2525. EVP_PKEY_CTX_free(pctx);
  2526. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  2527. err:
  2528. EVP_PKEY_CTX_free(pctx);
  2529. return(-1);
  2530. }
  2531. int ssl3_send_client_certificate(SSL *s)
  2532. {
  2533. X509 *x509=NULL;
  2534. EVP_PKEY *pkey=NULL;
  2535. int i;
  2536. unsigned long l;
  2537. if (s->state == SSL3_ST_CW_CERT_A)
  2538. {
  2539. if ((s->cert == NULL) ||
  2540. (s->cert->key->x509 == NULL) ||
  2541. (s->cert->key->privatekey == NULL))
  2542. s->state=SSL3_ST_CW_CERT_B;
  2543. else
  2544. s->state=SSL3_ST_CW_CERT_C;
  2545. }
  2546. /* We need to get a client cert */
  2547. if (s->state == SSL3_ST_CW_CERT_B)
  2548. {
  2549. /* If we get an error, we need to
  2550. * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
  2551. * We then get retied later */
  2552. i=0;
  2553. i = ssl_do_client_cert_cb(s, &x509, &pkey);
  2554. if (i < 0)
  2555. {
  2556. s->rwstate=SSL_X509_LOOKUP;
  2557. return(-1);
  2558. }
  2559. s->rwstate=SSL_NOTHING;
  2560. if ((i == 1) && (pkey != NULL) && (x509 != NULL))
  2561. {
  2562. s->state=SSL3_ST_CW_CERT_B;
  2563. if ( !SSL_use_certificate(s,x509) ||
  2564. !SSL_use_PrivateKey(s,pkey))
  2565. i=0;
  2566. }
  2567. else if (i == 1)
  2568. {
  2569. i=0;
  2570. SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
  2571. }
  2572. if (x509 != NULL) X509_free(x509);
  2573. if (pkey != NULL) EVP_PKEY_free(pkey);
  2574. if (i == 0)
  2575. {
  2576. if (s->version == SSL3_VERSION)
  2577. {
  2578. s->s3->tmp.cert_req=0;
  2579. ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
  2580. return(1);
  2581. }
  2582. else
  2583. {
  2584. s->s3->tmp.cert_req=2;
  2585. }
  2586. }
  2587. /* Ok, we have a cert */
  2588. s->state=SSL3_ST_CW_CERT_C;
  2589. }
  2590. if (s->state == SSL3_ST_CW_CERT_C)
  2591. {
  2592. s->state=SSL3_ST_CW_CERT_D;
  2593. l=ssl3_output_cert_chain(s,
  2594. (s->s3->tmp.cert_req == 2)?NULL:s->cert->key->x509);
  2595. s->init_num=(int)l;
  2596. s->init_off=0;
  2597. }
  2598. /* SSL3_ST_CW_CERT_D */
  2599. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  2600. }
  2601. #define has_bits(i,m) (((i)&(m)) == (m))
  2602. int ssl3_check_cert_and_algorithm(SSL *s)
  2603. {
  2604. int i,idx;
  2605. long alg_k,alg_a;
  2606. EVP_PKEY *pkey=NULL;
  2607. SESS_CERT *sc;
  2608. #ifndef OPENSSL_NO_RSA
  2609. RSA *rsa;
  2610. #endif
  2611. #ifndef OPENSSL_NO_DH
  2612. DH *dh;
  2613. #endif
  2614. alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
  2615. alg_a=s->s3->tmp.new_cipher->algorithm_auth;
  2616. /* we don't have a certificate */
  2617. if ((alg_a & (SSL_aDH|SSL_aNULL|SSL_aKRB5)) || (alg_k & SSL_kPSK))
  2618. return(1);
  2619. sc=s->session->sess_cert;
  2620. if (sc == NULL)
  2621. {
  2622. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,ERR_R_INTERNAL_ERROR);
  2623. goto err;
  2624. }
  2625. #ifndef OPENSSL_NO_RSA
  2626. rsa=s->session->sess_cert->peer_rsa_tmp;
  2627. #endif
  2628. #ifndef OPENSSL_NO_DH
  2629. dh=s->session->sess_cert->peer_dh_tmp;
  2630. #endif
  2631. /* This is the passed certificate */
  2632. idx=sc->peer_cert_type;
  2633. #ifndef OPENSSL_NO_ECDH
  2634. if (idx == SSL_PKEY_ECC)
  2635. {
  2636. if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509,
  2637. s->s3->tmp.new_cipher) == 0)
  2638. { /* check failed */
  2639. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_BAD_ECC_CERT);
  2640. goto f_err;
  2641. }
  2642. else
  2643. {
  2644. return 1;
  2645. }
  2646. }
  2647. #endif
  2648. pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509);
  2649. i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey);
  2650. EVP_PKEY_free(pkey);
  2651. /* Check that we have a certificate if we require one */
  2652. if ((alg_a & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))
  2653. {
  2654. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_SIGNING_CERT);
  2655. goto f_err;
  2656. }
  2657. #ifndef OPENSSL_NO_DSA
  2658. else if ((alg_a & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))
  2659. {
  2660. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DSA_SIGNING_CERT);
  2661. goto f_err;
  2662. }
  2663. #endif
  2664. #ifndef OPENSSL_NO_RSA
  2665. if ((alg_k & SSL_kRSA) &&
  2666. !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))
  2667. {
  2668. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_ENCRYPTING_CERT);
  2669. goto f_err;
  2670. }
  2671. #endif
  2672. #ifndef OPENSSL_NO_DH
  2673. if ((alg_k & SSL_kEDH) &&
  2674. !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))
  2675. {
  2676. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_KEY);
  2677. goto f_err;
  2678. }
  2679. else if ((alg_k & SSL_kDHr) && !has_bits(i,EVP_PK_DH|EVP_PKS_RSA))
  2680. {
  2681. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_RSA_CERT);
  2682. goto f_err;
  2683. }
  2684. #ifndef OPENSSL_NO_DSA
  2685. else if ((alg_k & SSL_kDHd) && !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))
  2686. {
  2687. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_DSA_CERT);
  2688. goto f_err;
  2689. }
  2690. #endif
  2691. #endif
  2692. if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i,EVP_PKT_EXP))
  2693. {
  2694. #ifndef OPENSSL_NO_RSA
  2695. if (alg_k & SSL_kRSA)
  2696. {
  2697. if (rsa == NULL
  2698. || RSA_size(rsa)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
  2699. {
  2700. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
  2701. goto f_err;
  2702. }
  2703. }
  2704. else
  2705. #endif
  2706. #ifndef OPENSSL_NO_DH
  2707. if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
  2708. {
  2709. if (dh == NULL
  2710. || DH_size(dh)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
  2711. {
  2712. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_DH_KEY);
  2713. goto f_err;
  2714. }
  2715. }
  2716. else
  2717. #endif
  2718. {
  2719. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
  2720. goto f_err;
  2721. }
  2722. }
  2723. return(1);
  2724. f_err:
  2725. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
  2726. err:
  2727. return(0);
  2728. }
  2729. /* Check to see if handshake is full or resumed. Usually this is just a
  2730. * case of checking to see if a cache hit has occurred. In the case of
  2731. * session tickets we have to check the next message to be sure.
  2732. */
  2733. #ifndef OPENSSL_NO_TLSEXT
  2734. int ssl3_check_finished(SSL *s)
  2735. {
  2736. int ok;
  2737. long n;
  2738. /* If we have no ticket it cannot be a resumed session. */
  2739. if (!s->session->tlsext_tick)
  2740. return 1;
  2741. /* this function is called when we really expect a Certificate
  2742. * message, so permit appropriate message length */
  2743. n=s->method->ssl_get_message(s,
  2744. SSL3_ST_CR_CERT_A,
  2745. SSL3_ST_CR_CERT_B,
  2746. -1,
  2747. s->max_cert_list,
  2748. &ok);
  2749. if (!ok) return((int)n);
  2750. s->s3->tmp.reuse_message = 1;
  2751. if ((s->s3->tmp.message_type == SSL3_MT_FINISHED)
  2752. || (s->s3->tmp.message_type == SSL3_MT_NEWSESSION_TICKET))
  2753. return 2;
  2754. return 1;
  2755. }
  2756. #endif
  2757. int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
  2758. {
  2759. int i = 0;
  2760. #ifndef OPENSSL_NO_ENGINE
  2761. if (s->ctx->client_cert_engine)
  2762. {
  2763. i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
  2764. SSL_get_client_CA_list(s),
  2765. px509, ppkey, NULL, NULL, NULL);
  2766. if (i != 0)
  2767. return i;
  2768. }
  2769. #endif
  2770. if (s->ctx->client_cert_cb)
  2771. i = s->ctx->client_cert_cb(s,px509,ppkey);
  2772. return i;
  2773. }