s3_clnt.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050
  1. /* ssl/s3_clnt.c */
  2. /* Copyright (C) 1995-1998 Eric Young ([email protected])
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young ([email protected]).
  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 ([email protected]).
  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 ([email protected])"
  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 ([email protected])"
  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. * [email protected].
  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. * ([email protected]). This product includes software written by Tim
  108. * Hudson ([email protected]).
  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. /* Workaround is now obsolete */
  781. #if 0
  782. if (!(s->options &
  783. SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG))
  784. #endif
  785. {
  786. al=SSL_AD_ILLEGAL_PARAMETER;
  787. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
  788. goto f_err;
  789. }
  790. }
  791. s->s3->tmp.new_cipher=c;
  792. if (!ssl3_digest_cached_records(s))
  793. goto f_err;
  794. /* lets get the compression algorithm */
  795. /* COMPRESSION */
  796. #ifdef OPENSSL_NO_COMP
  797. if (*(p++) != 0)
  798. {
  799. al=SSL_AD_ILLEGAL_PARAMETER;
  800. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  801. goto f_err;
  802. }
  803. /* If compression is disabled we'd better not try to resume a session
  804. * using compression.
  805. */
  806. if (s->session->compress_meth != 0)
  807. {
  808. al=SSL_AD_INTERNAL_ERROR;
  809. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_INCONSISTENT_COMPRESSION);
  810. goto f_err;
  811. }
  812. #else
  813. j= *(p++);
  814. if (s->hit && j != s->session->compress_meth)
  815. {
  816. al=SSL_AD_ILLEGAL_PARAMETER;
  817. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
  818. goto f_err;
  819. }
  820. if (j == 0)
  821. comp=NULL;
  822. else if (s->options & SSL_OP_NO_COMPRESSION)
  823. {
  824. al=SSL_AD_ILLEGAL_PARAMETER;
  825. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_COMPRESSION_DISABLED);
  826. goto f_err;
  827. }
  828. else
  829. comp=ssl3_comp_find(s->ctx->comp_methods,j);
  830. if ((j != 0) && (comp == NULL))
  831. {
  832. al=SSL_AD_ILLEGAL_PARAMETER;
  833. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  834. goto f_err;
  835. }
  836. else
  837. {
  838. s->s3->tmp.new_compression=comp;
  839. }
  840. #endif
  841. #ifndef OPENSSL_NO_TLSEXT
  842. /* TLS extensions*/
  843. if (s->version >= SSL3_VERSION)
  844. {
  845. if (!ssl_parse_serverhello_tlsext(s,&p,d,n, &al))
  846. {
  847. /* 'al' set by ssl_parse_serverhello_tlsext */
  848. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_PARSE_TLSEXT);
  849. goto f_err;
  850. }
  851. if (ssl_check_serverhello_tlsext(s) <= 0)
  852. {
  853. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SERVERHELLO_TLSEXT);
  854. goto err;
  855. }
  856. }
  857. #endif
  858. if (p != (d+n))
  859. {
  860. /* wrong packet length */
  861. al=SSL_AD_DECODE_ERROR;
  862. SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_PACKET_LENGTH);
  863. goto f_err;
  864. }
  865. return(1);
  866. f_err:
  867. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  868. err:
  869. return(-1);
  870. }
  871. int ssl3_get_server_certificate(SSL *s)
  872. {
  873. int al,i,ok,ret= -1;
  874. unsigned long n,nc,llen,l;
  875. X509 *x=NULL;
  876. const unsigned char *q,*p;
  877. unsigned char *d;
  878. STACK_OF(X509) *sk=NULL;
  879. SESS_CERT *sc;
  880. EVP_PKEY *pkey=NULL;
  881. int need_cert = 1; /* VRS: 0=> will allow null cert if auth == KRB5 */
  882. n=s->method->ssl_get_message(s,
  883. SSL3_ST_CR_CERT_A,
  884. SSL3_ST_CR_CERT_B,
  885. -1,
  886. s->max_cert_list,
  887. &ok);
  888. if (!ok) return((int)n);
  889. if ((s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) ||
  890. ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5) &&
  891. (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)))
  892. {
  893. s->s3->tmp.reuse_message=1;
  894. return(1);
  895. }
  896. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
  897. {
  898. al=SSL_AD_UNEXPECTED_MESSAGE;
  899. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_BAD_MESSAGE_TYPE);
  900. goto f_err;
  901. }
  902. p=d=(unsigned char *)s->init_msg;
  903. if ((sk=sk_X509_new_null()) == NULL)
  904. {
  905. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
  906. goto err;
  907. }
  908. n2l3(p,llen);
  909. if (llen+3 != n)
  910. {
  911. al=SSL_AD_DECODE_ERROR;
  912. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
  913. goto f_err;
  914. }
  915. for (nc=0; nc<llen; )
  916. {
  917. n2l3(p,l);
  918. if ((l+nc+3) > llen)
  919. {
  920. al=SSL_AD_DECODE_ERROR;
  921. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
  922. goto f_err;
  923. }
  924. q=p;
  925. x=d2i_X509(NULL,&q,l);
  926. if (x == NULL)
  927. {
  928. al=SSL_AD_BAD_CERTIFICATE;
  929. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_ASN1_LIB);
  930. goto f_err;
  931. }
  932. if (q != (p+l))
  933. {
  934. al=SSL_AD_DECODE_ERROR;
  935. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
  936. goto f_err;
  937. }
  938. if (!sk_X509_push(sk,x))
  939. {
  940. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
  941. goto err;
  942. }
  943. x=NULL;
  944. nc+=l+3;
  945. p=q;
  946. }
  947. i=ssl_verify_cert_chain(s,sk);
  948. if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0)
  949. #ifndef OPENSSL_NO_KRB5
  950. && !((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
  951. (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
  952. #endif /* OPENSSL_NO_KRB5 */
  953. )
  954. {
  955. al=ssl_verify_alarm_type(s->verify_result);
  956. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED);
  957. goto f_err;
  958. }
  959. ERR_clear_error(); /* but we keep s->verify_result */
  960. sc=ssl_sess_cert_new();
  961. if (sc == NULL) goto err;
  962. if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert);
  963. s->session->sess_cert=sc;
  964. sc->cert_chain=sk;
  965. /* Inconsistency alert: cert_chain does include the peer's
  966. * certificate, which we don't include in s3_srvr.c */
  967. x=sk_X509_value(sk,0);
  968. sk=NULL;
  969. /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
  970. pkey=X509_get_pubkey(x);
  971. /* VRS: allow null cert if auth == KRB5 */
  972. need_cert = ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
  973. (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
  974. ? 0 : 1;
  975. #ifdef KSSL_DEBUG
  976. printf("pkey,x = %p, %p\n", pkey,x);
  977. printf("ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x,pkey));
  978. printf("cipher, alg, nc = %s, %lx, %lx, %d\n", s->s3->tmp.new_cipher->name,
  979. s->s3->tmp.new_cipher->algorithm_mkey, s->s3->tmp.new_cipher->algorithm_auth, need_cert);
  980. #endif /* KSSL_DEBUG */
  981. if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey)))
  982. {
  983. x=NULL;
  984. al=SSL3_AL_FATAL;
  985. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
  986. SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
  987. goto f_err;
  988. }
  989. i=ssl_cert_type(x,pkey);
  990. if (need_cert && i < 0)
  991. {
  992. x=NULL;
  993. al=SSL3_AL_FATAL;
  994. SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
  995. SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  996. goto f_err;
  997. }
  998. if (need_cert)
  999. {
  1000. sc->peer_cert_type=i;
  1001. CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
  1002. /* Why would the following ever happen?
  1003. * We just created sc a couple of lines ago. */
  1004. if (sc->peer_pkeys[i].x509 != NULL)
  1005. X509_free(sc->peer_pkeys[i].x509);
  1006. sc->peer_pkeys[i].x509=x;
  1007. sc->peer_key= &(sc->peer_pkeys[i]);
  1008. if (s->session->peer != NULL)
  1009. X509_free(s->session->peer);
  1010. CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
  1011. s->session->peer=x;
  1012. }
  1013. else
  1014. {
  1015. sc->peer_cert_type=i;
  1016. sc->peer_key= NULL;
  1017. if (s->session->peer != NULL)
  1018. X509_free(s->session->peer);
  1019. s->session->peer=NULL;
  1020. }
  1021. s->session->verify_result = s->verify_result;
  1022. x=NULL;
  1023. ret=1;
  1024. if (0)
  1025. {
  1026. f_err:
  1027. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1028. }
  1029. err:
  1030. EVP_PKEY_free(pkey);
  1031. X509_free(x);
  1032. sk_X509_pop_free(sk,X509_free);
  1033. return(ret);
  1034. }
  1035. int ssl3_get_key_exchange(SSL *s)
  1036. {
  1037. #ifndef OPENSSL_NO_RSA
  1038. unsigned char *q,md_buf[EVP_MAX_MD_SIZE*2];
  1039. #endif
  1040. EVP_MD_CTX md_ctx;
  1041. unsigned char *param,*p;
  1042. int al,i,j,param_len,ok;
  1043. long n,alg_k,alg_a;
  1044. EVP_PKEY *pkey=NULL;
  1045. #ifndef OPENSSL_NO_RSA
  1046. RSA *rsa=NULL;
  1047. #endif
  1048. #ifndef OPENSSL_NO_DH
  1049. DH *dh=NULL;
  1050. #endif
  1051. #ifndef OPENSSL_NO_ECDH
  1052. EC_KEY *ecdh = NULL;
  1053. BN_CTX *bn_ctx = NULL;
  1054. EC_POINT *srvr_ecpoint = NULL;
  1055. int curve_nid = 0;
  1056. int encoded_pt_len = 0;
  1057. #endif
  1058. /* use same message size as in ssl3_get_certificate_request()
  1059. * as ServerKeyExchange message may be skipped */
  1060. n=s->method->ssl_get_message(s,
  1061. SSL3_ST_CR_KEY_EXCH_A,
  1062. SSL3_ST_CR_KEY_EXCH_B,
  1063. -1,
  1064. s->max_cert_list,
  1065. &ok);
  1066. if (!ok) return((int)n);
  1067. if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE)
  1068. {
  1069. #ifndef OPENSSL_NO_PSK
  1070. /* In plain PSK ciphersuite, ServerKeyExchange can be
  1071. omitted if no identity hint is sent. Set
  1072. session->sess_cert anyway to avoid problems
  1073. later.*/
  1074. if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)
  1075. {
  1076. s->session->sess_cert=ssl_sess_cert_new();
  1077. if (s->ctx->psk_identity_hint)
  1078. OPENSSL_free(s->ctx->psk_identity_hint);
  1079. s->ctx->psk_identity_hint = NULL;
  1080. }
  1081. #endif
  1082. s->s3->tmp.reuse_message=1;
  1083. return(1);
  1084. }
  1085. param=p=(unsigned char *)s->init_msg;
  1086. if (s->session->sess_cert != NULL)
  1087. {
  1088. #ifndef OPENSSL_NO_RSA
  1089. if (s->session->sess_cert->peer_rsa_tmp != NULL)
  1090. {
  1091. RSA_free(s->session->sess_cert->peer_rsa_tmp);
  1092. s->session->sess_cert->peer_rsa_tmp=NULL;
  1093. }
  1094. #endif
  1095. #ifndef OPENSSL_NO_DH
  1096. if (s->session->sess_cert->peer_dh_tmp)
  1097. {
  1098. DH_free(s->session->sess_cert->peer_dh_tmp);
  1099. s->session->sess_cert->peer_dh_tmp=NULL;
  1100. }
  1101. #endif
  1102. #ifndef OPENSSL_NO_ECDH
  1103. if (s->session->sess_cert->peer_ecdh_tmp)
  1104. {
  1105. EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
  1106. s->session->sess_cert->peer_ecdh_tmp=NULL;
  1107. }
  1108. #endif
  1109. }
  1110. else
  1111. {
  1112. s->session->sess_cert=ssl_sess_cert_new();
  1113. }
  1114. param_len=0;
  1115. alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
  1116. alg_a=s->s3->tmp.new_cipher->algorithm_auth;
  1117. EVP_MD_CTX_init(&md_ctx);
  1118. #ifndef OPENSSL_NO_PSK
  1119. if (alg_k & SSL_kPSK)
  1120. {
  1121. char tmp_id_hint[PSK_MAX_IDENTITY_LEN+1];
  1122. al=SSL_AD_HANDSHAKE_FAILURE;
  1123. n2s(p,i);
  1124. param_len=i+2;
  1125. /* Store PSK identity hint for later use, hint is used
  1126. * in ssl3_send_client_key_exchange. Assume that the
  1127. * maximum length of a PSK identity hint can be as
  1128. * long as the maximum length of a PSK identity. */
  1129. if (i > PSK_MAX_IDENTITY_LEN)
  1130. {
  1131. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
  1132. SSL_R_DATA_LENGTH_TOO_LONG);
  1133. goto f_err;
  1134. }
  1135. if (param_len > n)
  1136. {
  1137. al=SSL_AD_DECODE_ERROR;
  1138. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
  1139. SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH);
  1140. goto f_err;
  1141. }
  1142. /* If received PSK identity hint contains NULL
  1143. * characters, the hint is truncated from the first
  1144. * NULL. p may not be ending with NULL, so create a
  1145. * NULL-terminated string. */
  1146. memcpy(tmp_id_hint, p, i);
  1147. memset(tmp_id_hint+i, 0, PSK_MAX_IDENTITY_LEN+1-i);
  1148. if (s->ctx->psk_identity_hint != NULL)
  1149. OPENSSL_free(s->ctx->psk_identity_hint);
  1150. s->ctx->psk_identity_hint = BUF_strdup(tmp_id_hint);
  1151. if (s->ctx->psk_identity_hint == NULL)
  1152. {
  1153. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
  1154. goto f_err;
  1155. }
  1156. p+=i;
  1157. n-=param_len;
  1158. }
  1159. else
  1160. #endif /* !OPENSSL_NO_PSK */
  1161. #ifndef OPENSSL_NO_RSA
  1162. if (alg_k & SSL_kRSA)
  1163. {
  1164. if ((rsa=RSA_new()) == NULL)
  1165. {
  1166. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  1167. goto err;
  1168. }
  1169. n2s(p,i);
  1170. param_len=i+2;
  1171. if (param_len > n)
  1172. {
  1173. al=SSL_AD_DECODE_ERROR;
  1174. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_MODULUS_LENGTH);
  1175. goto f_err;
  1176. }
  1177. if (!(rsa->n=BN_bin2bn(p,i,rsa->n)))
  1178. {
  1179. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  1180. goto err;
  1181. }
  1182. p+=i;
  1183. n2s(p,i);
  1184. param_len+=i+2;
  1185. if (param_len > n)
  1186. {
  1187. al=SSL_AD_DECODE_ERROR;
  1188. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_E_LENGTH);
  1189. goto f_err;
  1190. }
  1191. if (!(rsa->e=BN_bin2bn(p,i,rsa->e)))
  1192. {
  1193. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  1194. goto err;
  1195. }
  1196. p+=i;
  1197. n-=param_len;
  1198. /* this should be because we are using an export cipher */
  1199. if (alg_a & SSL_aRSA)
  1200. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1201. else
  1202. {
  1203. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
  1204. goto err;
  1205. }
  1206. s->session->sess_cert->peer_rsa_tmp=rsa;
  1207. rsa=NULL;
  1208. }
  1209. #else /* OPENSSL_NO_RSA */
  1210. if (0)
  1211. ;
  1212. #endif
  1213. #ifndef OPENSSL_NO_DH
  1214. else if (alg_k & SSL_kEDH)
  1215. {
  1216. if ((dh=DH_new()) == NULL)
  1217. {
  1218. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_DH_LIB);
  1219. goto err;
  1220. }
  1221. n2s(p,i);
  1222. param_len=i+2;
  1223. if (param_len > n)
  1224. {
  1225. al=SSL_AD_DECODE_ERROR;
  1226. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_P_LENGTH);
  1227. goto f_err;
  1228. }
  1229. if (!(dh->p=BN_bin2bn(p,i,NULL)))
  1230. {
  1231. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  1232. goto err;
  1233. }
  1234. p+=i;
  1235. n2s(p,i);
  1236. param_len+=i+2;
  1237. if (param_len > n)
  1238. {
  1239. al=SSL_AD_DECODE_ERROR;
  1240. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_G_LENGTH);
  1241. goto f_err;
  1242. }
  1243. if (!(dh->g=BN_bin2bn(p,i,NULL)))
  1244. {
  1245. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  1246. goto err;
  1247. }
  1248. p+=i;
  1249. n2s(p,i);
  1250. param_len+=i+2;
  1251. if (param_len > n)
  1252. {
  1253. al=SSL_AD_DECODE_ERROR;
  1254. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_PUB_KEY_LENGTH);
  1255. goto f_err;
  1256. }
  1257. if (!(dh->pub_key=BN_bin2bn(p,i,NULL)))
  1258. {
  1259. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
  1260. goto err;
  1261. }
  1262. p+=i;
  1263. n-=param_len;
  1264. #ifndef OPENSSL_NO_RSA
  1265. if (alg_a & SSL_aRSA)
  1266. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1267. #else
  1268. if (0)
  1269. ;
  1270. #endif
  1271. #ifndef OPENSSL_NO_DSA
  1272. else if (alg_a & SSL_aDSS)
  1273. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].x509);
  1274. #endif
  1275. /* else anonymous DH, so no certificate or pkey. */
  1276. s->session->sess_cert->peer_dh_tmp=dh;
  1277. dh=NULL;
  1278. }
  1279. else if ((alg_k & SSL_kDHr) || (alg_k & SSL_kDHd))
  1280. {
  1281. al=SSL_AD_ILLEGAL_PARAMETER;
  1282. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
  1283. goto f_err;
  1284. }
  1285. #endif /* !OPENSSL_NO_DH */
  1286. #ifndef OPENSSL_NO_ECDH
  1287. else if (alg_k & SSL_kEECDH)
  1288. {
  1289. EC_GROUP *ngroup;
  1290. const EC_GROUP *group;
  1291. if ((ecdh=EC_KEY_new()) == NULL)
  1292. {
  1293. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  1294. goto err;
  1295. }
  1296. /* Extract elliptic curve parameters and the
  1297. * server's ephemeral ECDH public key.
  1298. * Keep accumulating lengths of various components in
  1299. * param_len and make sure it never exceeds n.
  1300. */
  1301. /* XXX: For now we only support named (not generic) curves
  1302. * and the ECParameters in this case is just three bytes.
  1303. */
  1304. param_len=3;
  1305. if ((param_len > n) ||
  1306. (*p != NAMED_CURVE_TYPE) ||
  1307. ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0))
  1308. {
  1309. al=SSL_AD_INTERNAL_ERROR;
  1310. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
  1311. goto f_err;
  1312. }
  1313. ngroup = EC_GROUP_new_by_curve_name(curve_nid);
  1314. if (ngroup == NULL)
  1315. {
  1316. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_EC_LIB);
  1317. goto err;
  1318. }
  1319. if (EC_KEY_set_group(ecdh, ngroup) == 0)
  1320. {
  1321. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_EC_LIB);
  1322. goto err;
  1323. }
  1324. EC_GROUP_free(ngroup);
  1325. group = EC_KEY_get0_group(ecdh);
  1326. if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
  1327. (EC_GROUP_get_degree(group) > 163))
  1328. {
  1329. al=SSL_AD_EXPORT_RESTRICTION;
  1330. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
  1331. goto f_err;
  1332. }
  1333. p+=3;
  1334. /* Next, get the encoded ECPoint */
  1335. if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
  1336. ((bn_ctx = BN_CTX_new()) == NULL))
  1337. {
  1338. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  1339. goto err;
  1340. }
  1341. encoded_pt_len = *p; /* length of encoded point */
  1342. p+=1;
  1343. param_len += (1 + encoded_pt_len);
  1344. if ((param_len > n) ||
  1345. (EC_POINT_oct2point(group, srvr_ecpoint,
  1346. p, encoded_pt_len, bn_ctx) == 0))
  1347. {
  1348. al=SSL_AD_DECODE_ERROR;
  1349. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_ECPOINT);
  1350. goto f_err;
  1351. }
  1352. n-=param_len;
  1353. p+=encoded_pt_len;
  1354. /* The ECC/TLS specification does not mention
  1355. * the use of DSA to sign ECParameters in the server
  1356. * key exchange message. We do support RSA and ECDSA.
  1357. */
  1358. if (0) ;
  1359. #ifndef OPENSSL_NO_RSA
  1360. else if (alg_a & SSL_aRSA)
  1361. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1362. #endif
  1363. #ifndef OPENSSL_NO_ECDSA
  1364. else if (alg_a & SSL_aECDSA)
  1365. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
  1366. #endif
  1367. /* else anonymous ECDH, so no certificate or pkey. */
  1368. EC_KEY_set_public_key(ecdh, srvr_ecpoint);
  1369. s->session->sess_cert->peer_ecdh_tmp=ecdh;
  1370. ecdh=NULL;
  1371. BN_CTX_free(bn_ctx);
  1372. bn_ctx = NULL;
  1373. EC_POINT_free(srvr_ecpoint);
  1374. srvr_ecpoint = NULL;
  1375. }
  1376. else if (alg_k)
  1377. {
  1378. al=SSL_AD_UNEXPECTED_MESSAGE;
  1379. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_UNEXPECTED_MESSAGE);
  1380. goto f_err;
  1381. }
  1382. #endif /* !OPENSSL_NO_ECDH */
  1383. /* p points to the next byte, there are 'n' bytes left */
  1384. /* if it was signed, check the signature */
  1385. if (pkey != NULL)
  1386. {
  1387. n2s(p,i);
  1388. n-=2;
  1389. j=EVP_PKEY_size(pkey);
  1390. if ((i != n) || (n > j) || (n <= 0))
  1391. {
  1392. /* wrong packet length */
  1393. al=SSL_AD_DECODE_ERROR;
  1394. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_WRONG_SIGNATURE_LENGTH);
  1395. goto f_err;
  1396. }
  1397. #ifndef OPENSSL_NO_RSA
  1398. if (pkey->type == EVP_PKEY_RSA)
  1399. {
  1400. int num;
  1401. j=0;
  1402. q=md_buf;
  1403. for (num=2; num > 0; num--)
  1404. {
  1405. EVP_DigestInit_ex(&md_ctx,(num == 2)
  1406. ?s->ctx->md5:s->ctx->sha1, NULL);
  1407. EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1408. EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1409. EVP_DigestUpdate(&md_ctx,param,param_len);
  1410. EVP_DigestFinal_ex(&md_ctx,q,(unsigned int *)&i);
  1411. q+=i;
  1412. j+=i;
  1413. }
  1414. i=RSA_verify(NID_md5_sha1, md_buf, j, p, n,
  1415. pkey->pkey.rsa);
  1416. if (i < 0)
  1417. {
  1418. al=SSL_AD_DECRYPT_ERROR;
  1419. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);
  1420. goto f_err;
  1421. }
  1422. if (i == 0)
  1423. {
  1424. /* bad signature */
  1425. al=SSL_AD_DECRYPT_ERROR;
  1426. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
  1427. goto f_err;
  1428. }
  1429. }
  1430. else
  1431. #endif
  1432. #ifndef OPENSSL_NO_DSA
  1433. if (pkey->type == EVP_PKEY_DSA)
  1434. {
  1435. /* lets do DSS */
  1436. EVP_VerifyInit_ex(&md_ctx,EVP_dss1(), NULL);
  1437. EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1438. EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1439. EVP_VerifyUpdate(&md_ctx,param,param_len);
  1440. if (EVP_VerifyFinal(&md_ctx,p,(int)n,pkey) <= 0)
  1441. {
  1442. /* bad signature */
  1443. al=SSL_AD_DECRYPT_ERROR;
  1444. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
  1445. goto f_err;
  1446. }
  1447. }
  1448. else
  1449. #endif
  1450. #ifndef OPENSSL_NO_ECDSA
  1451. if (pkey->type == EVP_PKEY_EC)
  1452. {
  1453. /* let's do ECDSA */
  1454. EVP_VerifyInit_ex(&md_ctx,EVP_ecdsa(), NULL);
  1455. EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1456. EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1457. EVP_VerifyUpdate(&md_ctx,param,param_len);
  1458. if (EVP_VerifyFinal(&md_ctx,p,(int)n,pkey) <= 0)
  1459. {
  1460. /* bad signature */
  1461. al=SSL_AD_DECRYPT_ERROR;
  1462. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
  1463. goto f_err;
  1464. }
  1465. }
  1466. else
  1467. #endif
  1468. {
  1469. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
  1470. goto err;
  1471. }
  1472. }
  1473. else
  1474. {
  1475. if (!(alg_a & SSL_aNULL) && !(alg_k & SSL_kPSK))
  1476. /* aNULL or kPSK do not need public keys */
  1477. {
  1478. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
  1479. goto err;
  1480. }
  1481. /* still data left over */
  1482. if (n != 0)
  1483. {
  1484. al=SSL_AD_DECODE_ERROR;
  1485. SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_EXTRA_DATA_IN_MESSAGE);
  1486. goto f_err;
  1487. }
  1488. }
  1489. EVP_PKEY_free(pkey);
  1490. EVP_MD_CTX_cleanup(&md_ctx);
  1491. return(1);
  1492. f_err:
  1493. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1494. err:
  1495. EVP_PKEY_free(pkey);
  1496. #ifndef OPENSSL_NO_RSA
  1497. if (rsa != NULL)
  1498. RSA_free(rsa);
  1499. #endif
  1500. #ifndef OPENSSL_NO_DH
  1501. if (dh != NULL)
  1502. DH_free(dh);
  1503. #endif
  1504. #ifndef OPENSSL_NO_ECDH
  1505. BN_CTX_free(bn_ctx);
  1506. EC_POINT_free(srvr_ecpoint);
  1507. if (ecdh != NULL)
  1508. EC_KEY_free(ecdh);
  1509. #endif
  1510. EVP_MD_CTX_cleanup(&md_ctx);
  1511. return(-1);
  1512. }
  1513. int ssl3_get_certificate_request(SSL *s)
  1514. {
  1515. int ok,ret=0;
  1516. unsigned long n,nc,l;
  1517. unsigned int llen,ctype_num,i;
  1518. X509_NAME *xn=NULL;
  1519. const unsigned char *p,*q;
  1520. unsigned char *d;
  1521. STACK_OF(X509_NAME) *ca_sk=NULL;
  1522. n=s->method->ssl_get_message(s,
  1523. SSL3_ST_CR_CERT_REQ_A,
  1524. SSL3_ST_CR_CERT_REQ_B,
  1525. -1,
  1526. s->max_cert_list,
  1527. &ok);
  1528. if (!ok) return((int)n);
  1529. s->s3->tmp.cert_req=0;
  1530. if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)
  1531. {
  1532. s->s3->tmp.reuse_message=1;
  1533. return(1);
  1534. }
  1535. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST)
  1536. {
  1537. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
  1538. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_WRONG_MESSAGE_TYPE);
  1539. goto err;
  1540. }
  1541. /* TLS does not like anon-DH with client cert */
  1542. if (s->version > SSL3_VERSION)
  1543. {
  1544. if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
  1545. {
  1546. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
  1547. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
  1548. goto err;
  1549. }
  1550. }
  1551. p=d=(unsigned char *)s->init_msg;
  1552. if ((ca_sk=sk_X509_NAME_new(ca_dn_cmp)) == NULL)
  1553. {
  1554. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
  1555. goto err;
  1556. }
  1557. /* get the certificate types */
  1558. ctype_num= *(p++);
  1559. if (ctype_num > SSL3_CT_NUMBER)
  1560. ctype_num=SSL3_CT_NUMBER;
  1561. for (i=0; i<ctype_num; i++)
  1562. s->s3->tmp.ctype[i]= p[i];
  1563. p+=ctype_num;
  1564. /* get the CA RDNs */
  1565. n2s(p,llen);
  1566. #if 0
  1567. {
  1568. FILE *out;
  1569. out=fopen("/tmp/vsign.der","w");
  1570. fwrite(p,1,llen,out);
  1571. fclose(out);
  1572. }
  1573. #endif
  1574. if ((llen+ctype_num+2+1) != n)
  1575. {
  1576. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1577. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_LENGTH_MISMATCH);
  1578. goto err;
  1579. }
  1580. for (nc=0; nc<llen; )
  1581. {
  1582. n2s(p,l);
  1583. if ((l+nc+2) > llen)
  1584. {
  1585. if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
  1586. goto cont; /* netscape bugs */
  1587. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1588. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_TOO_LONG);
  1589. goto err;
  1590. }
  1591. q=p;
  1592. if ((xn=d2i_X509_NAME(NULL,&q,l)) == NULL)
  1593. {
  1594. /* If netscape tolerance is on, ignore errors */
  1595. if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG)
  1596. goto cont;
  1597. else
  1598. {
  1599. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1600. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_ASN1_LIB);
  1601. goto err;
  1602. }
  1603. }
  1604. if (q != (p+l))
  1605. {
  1606. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1607. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_LENGTH_MISMATCH);
  1608. goto err;
  1609. }
  1610. if (!sk_X509_NAME_push(ca_sk,xn))
  1611. {
  1612. SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
  1613. goto err;
  1614. }
  1615. p+=l;
  1616. nc+=l+2;
  1617. }
  1618. if (0)
  1619. {
  1620. cont:
  1621. ERR_clear_error();
  1622. }
  1623. /* we should setup a certificate to return.... */
  1624. s->s3->tmp.cert_req=1;
  1625. s->s3->tmp.ctype_num=ctype_num;
  1626. if (s->s3->tmp.ca_names != NULL)
  1627. sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
  1628. s->s3->tmp.ca_names=ca_sk;
  1629. ca_sk=NULL;
  1630. ret=1;
  1631. err:
  1632. if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free);
  1633. return(ret);
  1634. }
  1635. static int ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b)
  1636. {
  1637. return(X509_NAME_cmp(*a,*b));
  1638. }
  1639. #ifndef OPENSSL_NO_TLSEXT
  1640. int ssl3_get_new_session_ticket(SSL *s)
  1641. {
  1642. int ok,al,ret=0, ticklen;
  1643. long n;
  1644. const unsigned char *p;
  1645. unsigned char *d;
  1646. n=s->method->ssl_get_message(s,
  1647. SSL3_ST_CR_SESSION_TICKET_A,
  1648. SSL3_ST_CR_SESSION_TICKET_B,
  1649. -1,
  1650. 16384,
  1651. &ok);
  1652. if (!ok)
  1653. return((int)n);
  1654. if (s->s3->tmp.message_type == SSL3_MT_FINISHED)
  1655. {
  1656. s->s3->tmp.reuse_message=1;
  1657. return(1);
  1658. }
  1659. if (s->s3->tmp.message_type != SSL3_MT_NEWSESSION_TICKET)
  1660. {
  1661. al=SSL_AD_UNEXPECTED_MESSAGE;
  1662. SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,SSL_R_BAD_MESSAGE_TYPE);
  1663. goto f_err;
  1664. }
  1665. if (n < 6)
  1666. {
  1667. /* need at least ticket_lifetime_hint + ticket length */
  1668. al = SSL_AD_DECODE_ERROR;
  1669. SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,SSL_R_LENGTH_MISMATCH);
  1670. goto f_err;
  1671. }
  1672. p=d=(unsigned char *)s->init_msg;
  1673. n2l(p, s->session->tlsext_tick_lifetime_hint);
  1674. n2s(p, ticklen);
  1675. /* ticket_lifetime_hint + ticket_length + ticket */
  1676. if (ticklen + 6 != n)
  1677. {
  1678. al = SSL_AD_DECODE_ERROR;
  1679. SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,SSL_R_LENGTH_MISMATCH);
  1680. goto f_err;
  1681. }
  1682. if (s->session->tlsext_tick)
  1683. {
  1684. OPENSSL_free(s->session->tlsext_tick);
  1685. s->session->tlsext_ticklen = 0;
  1686. }
  1687. s->session->tlsext_tick = OPENSSL_malloc(ticklen);
  1688. if (!s->session->tlsext_tick)
  1689. {
  1690. SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,ERR_R_MALLOC_FAILURE);
  1691. goto err;
  1692. }
  1693. memcpy(s->session->tlsext_tick, p, ticklen);
  1694. s->session->tlsext_ticklen = ticklen;
  1695. /* There are two ways to detect a resumed ticket sesion.
  1696. * One is to set an appropriate session ID and then the server
  1697. * must return a match in ServerHello. This allows the normal
  1698. * client session ID matching to work and we know much
  1699. * earlier that the ticket has been accepted.
  1700. *
  1701. * The other way is to set zero length session ID when the
  1702. * ticket is presented and rely on the handshake to determine
  1703. * session resumption.
  1704. *
  1705. * We choose the former approach because this fits in with
  1706. * assumptions elsewhere in OpenSSL. The session ID is set
  1707. * to the SHA256 (or SHA1 is SHA256 is disabled) hash of the
  1708. * ticket.
  1709. */
  1710. EVP_Digest(p, ticklen,
  1711. s->session->session_id, &s->session->session_id_length,
  1712. #ifndef OPENSSL_NO_SHA256
  1713. EVP_sha256(), NULL);
  1714. #else
  1715. EVP_sha1(), NULL);
  1716. #endif
  1717. ret=1;
  1718. return(ret);
  1719. f_err:
  1720. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1721. err:
  1722. return(-1);
  1723. }
  1724. int ssl3_get_cert_status(SSL *s)
  1725. {
  1726. int ok, al;
  1727. unsigned long resplen,n;
  1728. const unsigned char *p;
  1729. n=s->method->ssl_get_message(s,
  1730. SSL3_ST_CR_CERT_STATUS_A,
  1731. SSL3_ST_CR_CERT_STATUS_B,
  1732. SSL3_MT_CERTIFICATE_STATUS,
  1733. 16384,
  1734. &ok);
  1735. if (!ok) return((int)n);
  1736. if (n < 4)
  1737. {
  1738. /* need at least status type + length */
  1739. al = SSL_AD_DECODE_ERROR;
  1740. SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_LENGTH_MISMATCH);
  1741. goto f_err;
  1742. }
  1743. p = (unsigned char *)s->init_msg;
  1744. if (*p++ != TLSEXT_STATUSTYPE_ocsp)
  1745. {
  1746. al = SSL_AD_DECODE_ERROR;
  1747. SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_UNSUPPORTED_STATUS_TYPE);
  1748. goto f_err;
  1749. }
  1750. n2l3(p, resplen);
  1751. if (resplen + 4 != n)
  1752. {
  1753. al = SSL_AD_DECODE_ERROR;
  1754. SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_LENGTH_MISMATCH);
  1755. goto f_err;
  1756. }
  1757. if (s->tlsext_ocsp_resp)
  1758. OPENSSL_free(s->tlsext_ocsp_resp);
  1759. s->tlsext_ocsp_resp = BUF_memdup(p, resplen);
  1760. if (!s->tlsext_ocsp_resp)
  1761. {
  1762. al = SSL_AD_INTERNAL_ERROR;
  1763. SSLerr(SSL_F_SSL3_GET_CERT_STATUS,ERR_R_MALLOC_FAILURE);
  1764. goto f_err;
  1765. }
  1766. s->tlsext_ocsp_resplen = resplen;
  1767. if (s->ctx->tlsext_status_cb)
  1768. {
  1769. int ret;
  1770. ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
  1771. if (ret == 0)
  1772. {
  1773. al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
  1774. SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_INVALID_STATUS_RESPONSE);
  1775. goto f_err;
  1776. }
  1777. if (ret < 0)
  1778. {
  1779. al = SSL_AD_INTERNAL_ERROR;
  1780. SSLerr(SSL_F_SSL3_GET_CERT_STATUS,ERR_R_MALLOC_FAILURE);
  1781. goto f_err;
  1782. }
  1783. }
  1784. return 1;
  1785. f_err:
  1786. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1787. return(-1);
  1788. }
  1789. #endif
  1790. int ssl3_get_server_done(SSL *s)
  1791. {
  1792. int ok,ret=0;
  1793. long n;
  1794. n=s->method->ssl_get_message(s,
  1795. SSL3_ST_CR_SRVR_DONE_A,
  1796. SSL3_ST_CR_SRVR_DONE_B,
  1797. SSL3_MT_SERVER_DONE,
  1798. 30, /* should be very small, like 0 :-) */
  1799. &ok);
  1800. if (!ok) return((int)n);
  1801. if (n > 0)
  1802. {
  1803. /* should contain no data */
  1804. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1805. SSLerr(SSL_F_SSL3_GET_SERVER_DONE,SSL_R_LENGTH_MISMATCH);
  1806. return -1;
  1807. }
  1808. ret=1;
  1809. return(ret);
  1810. }
  1811. int ssl3_send_client_key_exchange(SSL *s)
  1812. {
  1813. unsigned char *p,*d;
  1814. int n;
  1815. unsigned long alg_k;
  1816. #ifndef OPENSSL_NO_RSA
  1817. unsigned char *q;
  1818. EVP_PKEY *pkey=NULL;
  1819. #endif
  1820. #ifndef OPENSSL_NO_KRB5
  1821. KSSL_ERR kssl_err;
  1822. #endif /* OPENSSL_NO_KRB5 */
  1823. #ifndef OPENSSL_NO_ECDH
  1824. EC_KEY *clnt_ecdh = NULL;
  1825. const EC_POINT *srvr_ecpoint = NULL;
  1826. EVP_PKEY *srvr_pub_pkey = NULL;
  1827. unsigned char *encodedPoint = NULL;
  1828. int encoded_pt_len = 0;
  1829. BN_CTX * bn_ctx = NULL;
  1830. #endif
  1831. if (s->state == SSL3_ST_CW_KEY_EXCH_A)
  1832. {
  1833. d=(unsigned char *)s->init_buf->data;
  1834. p= &(d[4]);
  1835. alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
  1836. /* Fool emacs indentation */
  1837. if (0) {}
  1838. #ifndef OPENSSL_NO_RSA
  1839. else if (alg_k & SSL_kRSA)
  1840. {
  1841. RSA *rsa;
  1842. unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
  1843. if (s->session->sess_cert->peer_rsa_tmp != NULL)
  1844. rsa=s->session->sess_cert->peer_rsa_tmp;
  1845. else
  1846. {
  1847. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1848. if ((pkey == NULL) ||
  1849. (pkey->type != EVP_PKEY_RSA) ||
  1850. (pkey->pkey.rsa == NULL))
  1851. {
  1852. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
  1853. goto err;
  1854. }
  1855. rsa=pkey->pkey.rsa;
  1856. EVP_PKEY_free(pkey);
  1857. }
  1858. tmp_buf[0]=s->client_version>>8;
  1859. tmp_buf[1]=s->client_version&0xff;
  1860. if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
  1861. goto err;
  1862. s->session->master_key_length=sizeof tmp_buf;
  1863. q=p;
  1864. /* Fix buf for TLS and beyond */
  1865. if (s->version > SSL3_VERSION)
  1866. p+=2;
  1867. n=RSA_public_encrypt(sizeof tmp_buf,
  1868. tmp_buf,p,rsa,RSA_PKCS1_PADDING);
  1869. #ifdef PKCS1_CHECK
  1870. if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;
  1871. if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;
  1872. #endif
  1873. if (n <= 0)
  1874. {
  1875. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT);
  1876. goto err;
  1877. }
  1878. /* Fix buf for TLS and beyond */
  1879. if (s->version > SSL3_VERSION)
  1880. {
  1881. s2n(n,q);
  1882. n+=2;
  1883. }
  1884. s->session->master_key_length=
  1885. s->method->ssl3_enc->generate_master_secret(s,
  1886. s->session->master_key,
  1887. tmp_buf,sizeof tmp_buf);
  1888. OPENSSL_cleanse(tmp_buf,sizeof tmp_buf);
  1889. }
  1890. #endif
  1891. #ifndef OPENSSL_NO_KRB5
  1892. else if (alg_k & SSL_kKRB5)
  1893. {
  1894. krb5_error_code krb5rc;
  1895. KSSL_CTX *kssl_ctx = s->kssl_ctx;
  1896. /* krb5_data krb5_ap_req; */
  1897. krb5_data *enc_ticket;
  1898. krb5_data authenticator, *authp = NULL;
  1899. EVP_CIPHER_CTX ciph_ctx;
  1900. const EVP_CIPHER *enc = NULL;
  1901. unsigned char iv[EVP_MAX_IV_LENGTH];
  1902. unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
  1903. unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH
  1904. + EVP_MAX_IV_LENGTH];
  1905. int padl, outl = sizeof(epms);
  1906. EVP_CIPHER_CTX_init(&ciph_ctx);
  1907. #ifdef KSSL_DEBUG
  1908. printf("ssl3_send_client_key_exchange(%lx & %lx)\n",
  1909. alg_k, SSL_kKRB5);
  1910. #endif /* KSSL_DEBUG */
  1911. authp = NULL;
  1912. #ifdef KRB5SENDAUTH
  1913. if (KRB5SENDAUTH) authp = &authenticator;
  1914. #endif /* KRB5SENDAUTH */
  1915. krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp,
  1916. &kssl_err);
  1917. enc = kssl_map_enc(kssl_ctx->enctype);
  1918. if (enc == NULL)
  1919. goto err;
  1920. #ifdef KSSL_DEBUG
  1921. {
  1922. printf("kssl_cget_tkt rtn %d\n", krb5rc);
  1923. if (krb5rc && kssl_err.text)
  1924. printf("kssl_cget_tkt kssl_err=%s\n", kssl_err.text);
  1925. }
  1926. #endif /* KSSL_DEBUG */
  1927. if (krb5rc)
  1928. {
  1929. ssl3_send_alert(s,SSL3_AL_FATAL,
  1930. SSL_AD_HANDSHAKE_FAILURE);
  1931. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  1932. kssl_err.reason);
  1933. goto err;
  1934. }
  1935. /* 20010406 VRS - Earlier versions used KRB5 AP_REQ
  1936. ** in place of RFC 2712 KerberosWrapper, as in:
  1937. **
  1938. ** Send ticket (copy to *p, set n = length)
  1939. ** n = krb5_ap_req.length;
  1940. ** memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
  1941. ** if (krb5_ap_req.data)
  1942. ** kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
  1943. **
  1944. ** Now using real RFC 2712 KerberosWrapper
  1945. ** (Thanks to Simon Wilkinson <[email protected]>)
  1946. ** Note: 2712 "opaque" types are here replaced
  1947. ** with a 2-byte length followed by the value.
  1948. ** Example:
  1949. ** KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
  1950. ** Where "xx xx" = length bytes. Shown here with
  1951. ** optional authenticator omitted.
  1952. */
  1953. /* KerberosWrapper.Ticket */
  1954. s2n(enc_ticket->length,p);
  1955. memcpy(p, enc_ticket->data, enc_ticket->length);
  1956. p+= enc_ticket->length;
  1957. n = enc_ticket->length + 2;
  1958. /* KerberosWrapper.Authenticator */
  1959. if (authp && authp->length)
  1960. {
  1961. s2n(authp->length,p);
  1962. memcpy(p, authp->data, authp->length);
  1963. p+= authp->length;
  1964. n+= authp->length + 2;
  1965. free(authp->data);
  1966. authp->data = NULL;
  1967. authp->length = 0;
  1968. }
  1969. else
  1970. {
  1971. s2n(0,p);/* null authenticator length */
  1972. n+=2;
  1973. }
  1974. tmp_buf[0]=s->client_version>>8;
  1975. tmp_buf[1]=s->client_version&0xff;
  1976. if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
  1977. goto err;
  1978. /* 20010420 VRS. Tried it this way; failed.
  1979. ** EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
  1980. ** EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
  1981. ** kssl_ctx->length);
  1982. ** EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
  1983. */
  1984. memset(iv, 0, sizeof iv); /* per RFC 1510 */
  1985. EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,
  1986. kssl_ctx->key,iv);
  1987. EVP_EncryptUpdate(&ciph_ctx,epms,&outl,tmp_buf,
  1988. sizeof tmp_buf);
  1989. EVP_EncryptFinal_ex(&ciph_ctx,&(epms[outl]),&padl);
  1990. outl += padl;
  1991. if (outl > (int)sizeof epms)
  1992. {
  1993. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  1994. goto err;
  1995. }
  1996. EVP_CIPHER_CTX_cleanup(&ciph_ctx);
  1997. /* KerberosWrapper.EncryptedPreMasterSecret */
  1998. s2n(outl,p);
  1999. memcpy(p, epms, outl);
  2000. p+=outl;
  2001. n+=outl + 2;
  2002. s->session->master_key_length=
  2003. s->method->ssl3_enc->generate_master_secret(s,
  2004. s->session->master_key,
  2005. tmp_buf, sizeof tmp_buf);
  2006. OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
  2007. OPENSSL_cleanse(epms, outl);
  2008. }
  2009. #endif
  2010. #ifndef OPENSSL_NO_DH
  2011. else if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
  2012. {
  2013. DH *dh_srvr,*dh_clnt;
  2014. if (s->session->sess_cert == NULL)
  2015. {
  2016. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
  2017. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNEXPECTED_MESSAGE);
  2018. goto err;
  2019. }
  2020. if (s->session->sess_cert->peer_dh_tmp != NULL)
  2021. dh_srvr=s->session->sess_cert->peer_dh_tmp;
  2022. else
  2023. {
  2024. /* we get them from the cert */
  2025. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
  2026. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
  2027. goto err;
  2028. }
  2029. /* generate a new random key */
  2030. if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
  2031. {
  2032. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
  2033. goto err;
  2034. }
  2035. if (!DH_generate_key(dh_clnt))
  2036. {
  2037. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
  2038. DH_free(dh_clnt);
  2039. goto err;
  2040. }
  2041. /* use the 'p' output buffer for the DH key, but
  2042. * make sure to clear it out afterwards */
  2043. n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
  2044. if (n <= 0)
  2045. {
  2046. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
  2047. DH_free(dh_clnt);
  2048. goto err;
  2049. }
  2050. /* generate master key from the result */
  2051. s->session->master_key_length=
  2052. s->method->ssl3_enc->generate_master_secret(s,
  2053. s->session->master_key,p,n);
  2054. /* clean up */
  2055. memset(p,0,n);
  2056. /* send off the data */
  2057. n=BN_num_bytes(dh_clnt->pub_key);
  2058. s2n(n,p);
  2059. BN_bn2bin(dh_clnt->pub_key,p);
  2060. n+=2;
  2061. DH_free(dh_clnt);
  2062. /* perhaps clean things up a bit EAY EAY EAY EAY*/
  2063. }
  2064. #endif
  2065. #ifndef OPENSSL_NO_ECDH
  2066. else if (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe))
  2067. {
  2068. const EC_GROUP *srvr_group = NULL;
  2069. EC_KEY *tkey;
  2070. int ecdh_clnt_cert = 0;
  2071. int field_size = 0;
  2072. /* Did we send out the client's
  2073. * ECDH share for use in premaster
  2074. * computation as part of client certificate?
  2075. * If so, set ecdh_clnt_cert to 1.
  2076. */
  2077. if ((alg_k & (SSL_kECDHr|SSL_kECDHe)) && (s->cert != NULL))
  2078. {
  2079. /* XXX: For now, we do not support client
  2080. * authentication using ECDH certificates.
  2081. * To add such support, one needs to add
  2082. * code that checks for appropriate
  2083. * conditions and sets ecdh_clnt_cert to 1.
  2084. * For example, the cert have an ECC
  2085. * key on the same curve as the server's
  2086. * and the key should be authorized for
  2087. * key agreement.
  2088. *
  2089. * One also needs to add code in ssl3_connect
  2090. * to skip sending the certificate verify
  2091. * message.
  2092. *
  2093. * if ((s->cert->key->privatekey != NULL) &&
  2094. * (s->cert->key->privatekey->type ==
  2095. * EVP_PKEY_EC) && ...)
  2096. * ecdh_clnt_cert = 1;
  2097. */
  2098. }
  2099. if (s->session->sess_cert->peer_ecdh_tmp != NULL)
  2100. {
  2101. tkey = s->session->sess_cert->peer_ecdh_tmp;
  2102. }
  2103. else
  2104. {
  2105. /* Get the Server Public Key from Cert */
  2106. srvr_pub_pkey = X509_get_pubkey(s->session-> \
  2107. sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
  2108. if ((srvr_pub_pkey == NULL) ||
  2109. (srvr_pub_pkey->type != EVP_PKEY_EC) ||
  2110. (srvr_pub_pkey->pkey.ec == NULL))
  2111. {
  2112. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2113. ERR_R_INTERNAL_ERROR);
  2114. goto err;
  2115. }
  2116. tkey = srvr_pub_pkey->pkey.ec;
  2117. }
  2118. srvr_group = EC_KEY_get0_group(tkey);
  2119. srvr_ecpoint = EC_KEY_get0_public_key(tkey);
  2120. if ((srvr_group == NULL) || (srvr_ecpoint == NULL))
  2121. {
  2122. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2123. ERR_R_INTERNAL_ERROR);
  2124. goto err;
  2125. }
  2126. if ((clnt_ecdh=EC_KEY_new()) == NULL)
  2127. {
  2128. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  2129. goto err;
  2130. }
  2131. if (!EC_KEY_set_group(clnt_ecdh, srvr_group))
  2132. {
  2133. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB);
  2134. goto err;
  2135. }
  2136. if (ecdh_clnt_cert)
  2137. {
  2138. /* Reuse key info from our certificate
  2139. * We only need our private key to perform
  2140. * the ECDH computation.
  2141. */
  2142. const BIGNUM *priv_key;
  2143. tkey = s->cert->key->privatekey->pkey.ec;
  2144. priv_key = EC_KEY_get0_private_key(tkey);
  2145. if (priv_key == NULL)
  2146. {
  2147. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  2148. goto err;
  2149. }
  2150. if (!EC_KEY_set_private_key(clnt_ecdh, priv_key))
  2151. {
  2152. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB);
  2153. goto err;
  2154. }
  2155. }
  2156. else
  2157. {
  2158. /* Generate a new ECDH key pair */
  2159. if (!(EC_KEY_generate_key(clnt_ecdh)))
  2160. {
  2161. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
  2162. goto err;
  2163. }
  2164. }
  2165. /* use the 'p' output buffer for the ECDH key, but
  2166. * make sure to clear it out afterwards
  2167. */
  2168. field_size = EC_GROUP_get_degree(srvr_group);
  2169. if (field_size <= 0)
  2170. {
  2171. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2172. ERR_R_ECDH_LIB);
  2173. goto err;
  2174. }
  2175. n=ECDH_compute_key(p, (field_size+7)/8, srvr_ecpoint, clnt_ecdh, NULL);
  2176. if (n <= 0)
  2177. {
  2178. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2179. ERR_R_ECDH_LIB);
  2180. goto err;
  2181. }
  2182. /* generate master key from the result */
  2183. s->session->master_key_length = s->method->ssl3_enc \
  2184. -> generate_master_secret(s,
  2185. s->session->master_key,
  2186. p, n);
  2187. memset(p, 0, n); /* clean up */
  2188. if (ecdh_clnt_cert)
  2189. {
  2190. /* Send empty client key exch message */
  2191. n = 0;
  2192. }
  2193. else
  2194. {
  2195. /* First check the size of encoding and
  2196. * allocate memory accordingly.
  2197. */
  2198. encoded_pt_len =
  2199. EC_POINT_point2oct(srvr_group,
  2200. EC_KEY_get0_public_key(clnt_ecdh),
  2201. POINT_CONVERSION_UNCOMPRESSED,
  2202. NULL, 0, NULL);
  2203. encodedPoint = (unsigned char *)
  2204. OPENSSL_malloc(encoded_pt_len *
  2205. sizeof(unsigned char));
  2206. bn_ctx = BN_CTX_new();
  2207. if ((encodedPoint == NULL) ||
  2208. (bn_ctx == NULL))
  2209. {
  2210. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  2211. goto err;
  2212. }
  2213. /* Encode the public key */
  2214. n = EC_POINT_point2oct(srvr_group,
  2215. EC_KEY_get0_public_key(clnt_ecdh),
  2216. POINT_CONVERSION_UNCOMPRESSED,
  2217. encodedPoint, encoded_pt_len, bn_ctx);
  2218. *p = n; /* length of encoded point */
  2219. /* Encoded point will be copied here */
  2220. p += 1;
  2221. /* copy the point */
  2222. memcpy((unsigned char *)p, encodedPoint, n);
  2223. /* increment n to account for length field */
  2224. n += 1;
  2225. }
  2226. /* Free allocated memory */
  2227. BN_CTX_free(bn_ctx);
  2228. if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
  2229. if (clnt_ecdh != NULL)
  2230. EC_KEY_free(clnt_ecdh);
  2231. EVP_PKEY_free(srvr_pub_pkey);
  2232. }
  2233. #endif /* !OPENSSL_NO_ECDH */
  2234. else if (alg_k & SSL_kGOST)
  2235. {
  2236. /* GOST key exchange message creation */
  2237. EVP_PKEY_CTX *pkey_ctx;
  2238. X509 *peer_cert;
  2239. size_t msglen;
  2240. unsigned int md_len;
  2241. int keytype;
  2242. unsigned char premaster_secret[32],shared_ukm[32], tmp[256];
  2243. EVP_MD_CTX *ukm_hash;
  2244. EVP_PKEY *pub_key;
  2245. /* Get server sertificate PKEY and create ctx from it */
  2246. peer_cert=s->session->sess_cert->peer_pkeys[(keytype=SSL_PKEY_GOST01)].x509;
  2247. if (!peer_cert)
  2248. peer_cert=s->session->sess_cert->peer_pkeys[(keytype=SSL_PKEY_GOST94)].x509;
  2249. if (!peer_cert) {
  2250. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
  2251. goto err;
  2252. }
  2253. pkey_ctx=EVP_PKEY_CTX_new(pub_key=X509_get_pubkey(peer_cert),NULL);
  2254. /* If we have send a certificate, and certificate key
  2255. * parameters match those of server certificate, use
  2256. * certificate key for key exchange
  2257. */
  2258. /* Otherwise, generate ephemeral key pair */
  2259. EVP_PKEY_encrypt_init(pkey_ctx);
  2260. /* Generate session key */
  2261. RAND_bytes(premaster_secret,32);
  2262. /* If we have client certificate, use its secret as peer key */
  2263. if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
  2264. if (EVP_PKEY_derive_set_peer(pkey_ctx,s->cert->key->privatekey) <=0) {
  2265. /* If there was an error - just ignore it. Ephemeral key
  2266. * would be used
  2267. */
  2268. ERR_clear_error();
  2269. }
  2270. }
  2271. /* Compute shared IV and store it in algorithm-specific
  2272. * context data */
  2273. ukm_hash = EVP_MD_CTX_create();
  2274. EVP_DigestInit(ukm_hash,EVP_get_digestbynid(NID_id_GostR3411_94));
  2275. EVP_DigestUpdate(ukm_hash,s->s3->client_random,SSL3_RANDOM_SIZE);
  2276. EVP_DigestUpdate(ukm_hash,s->s3->server_random,SSL3_RANDOM_SIZE);
  2277. EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len);
  2278. EVP_MD_CTX_destroy(ukm_hash);
  2279. if (EVP_PKEY_CTX_ctrl(pkey_ctx,-1,EVP_PKEY_OP_ENCRYPT,EVP_PKEY_CTRL_SET_IV,
  2280. 8,shared_ukm)<0) {
  2281. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2282. SSL_R_LIBRARY_BUG);
  2283. goto err;
  2284. }
  2285. /* Make GOST keytransport blob message */
  2286. /*Encapsulate it into sequence */
  2287. *(p++)=V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
  2288. msglen=255;
  2289. if (EVP_PKEY_encrypt(pkey_ctx,tmp,&msglen,premaster_secret,32)<0) {
  2290. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2291. SSL_R_LIBRARY_BUG);
  2292. goto err;
  2293. }
  2294. if (msglen >= 0x80)
  2295. {
  2296. *(p++)=0x81;
  2297. *(p++)= msglen & 0xff;
  2298. n=msglen+3;
  2299. }
  2300. else
  2301. {
  2302. *(p++)= msglen & 0xff;
  2303. n=msglen+2;
  2304. }
  2305. memcpy(p, tmp, msglen);
  2306. /* Check if pubkey from client certificate was used */
  2307. if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0)
  2308. {
  2309. /* Set flag "skip certificate verify" */
  2310. s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
  2311. }
  2312. EVP_PKEY_CTX_free(pkey_ctx);
  2313. s->session->master_key_length=
  2314. s->method->ssl3_enc->generate_master_secret(s,
  2315. s->session->master_key,premaster_secret,32);
  2316. EVP_PKEY_free(pub_key);
  2317. }
  2318. #ifndef OPENSSL_NO_PSK
  2319. else if (alg_k & SSL_kPSK)
  2320. {
  2321. char identity[PSK_MAX_IDENTITY_LEN];
  2322. unsigned char *t = NULL;
  2323. unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN*2+4];
  2324. unsigned int pre_ms_len = 0, psk_len = 0;
  2325. int psk_err = 1;
  2326. n = 0;
  2327. if (s->psk_client_callback == NULL)
  2328. {
  2329. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2330. SSL_R_PSK_NO_CLIENT_CB);
  2331. goto err;
  2332. }
  2333. psk_len = s->psk_client_callback(s, s->ctx->psk_identity_hint,
  2334. identity, PSK_MAX_IDENTITY_LEN,
  2335. psk_or_pre_ms, sizeof(psk_or_pre_ms));
  2336. if (psk_len > PSK_MAX_PSK_LEN)
  2337. {
  2338. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2339. ERR_R_INTERNAL_ERROR);
  2340. goto psk_err;
  2341. }
  2342. else if (psk_len == 0)
  2343. {
  2344. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2345. SSL_R_PSK_IDENTITY_NOT_FOUND);
  2346. goto psk_err;
  2347. }
  2348. /* create PSK pre_master_secret */
  2349. pre_ms_len = 2+psk_len+2+psk_len;
  2350. t = psk_or_pre_ms;
  2351. memmove(psk_or_pre_ms+psk_len+4, psk_or_pre_ms, psk_len);
  2352. s2n(psk_len, t);
  2353. memset(t, 0, psk_len);
  2354. t+=psk_len;
  2355. s2n(psk_len, t);
  2356. if (s->session->psk_identity_hint != NULL)
  2357. OPENSSL_free(s->session->psk_identity_hint);
  2358. s->session->psk_identity_hint = BUF_strdup(s->ctx->psk_identity_hint);
  2359. if (s->ctx->psk_identity_hint != NULL &&
  2360. s->session->psk_identity_hint == NULL)
  2361. {
  2362. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2363. ERR_R_MALLOC_FAILURE);
  2364. goto psk_err;
  2365. }
  2366. if (s->session->psk_identity != NULL)
  2367. OPENSSL_free(s->session->psk_identity);
  2368. s->session->psk_identity = BUF_strdup(identity);
  2369. if (s->session->psk_identity == NULL)
  2370. {
  2371. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2372. ERR_R_MALLOC_FAILURE);
  2373. goto psk_err;
  2374. }
  2375. s->session->master_key_length =
  2376. s->method->ssl3_enc->generate_master_secret(s,
  2377. s->session->master_key,
  2378. psk_or_pre_ms, pre_ms_len);
  2379. n = strlen(identity);
  2380. s2n(n, p);
  2381. memcpy(p, identity, n);
  2382. n+=2;
  2383. psk_err = 0;
  2384. psk_err:
  2385. OPENSSL_cleanse(identity, PSK_MAX_IDENTITY_LEN);
  2386. OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
  2387. if (psk_err != 0)
  2388. {
  2389. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  2390. goto err;
  2391. }
  2392. }
  2393. #endif
  2394. else
  2395. {
  2396. ssl3_send_alert(s, SSL3_AL_FATAL,
  2397. SSL_AD_HANDSHAKE_FAILURE);
  2398. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  2399. ERR_R_INTERNAL_ERROR);
  2400. goto err;
  2401. }
  2402. *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE;
  2403. l2n3(n,d);
  2404. s->state=SSL3_ST_CW_KEY_EXCH_B;
  2405. /* number of bytes to write */
  2406. s->init_num=n+4;
  2407. s->init_off=0;
  2408. }
  2409. /* SSL3_ST_CW_KEY_EXCH_B */
  2410. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  2411. err:
  2412. #ifndef OPENSSL_NO_ECDH
  2413. BN_CTX_free(bn_ctx);
  2414. if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
  2415. if (clnt_ecdh != NULL)
  2416. EC_KEY_free(clnt_ecdh);
  2417. EVP_PKEY_free(srvr_pub_pkey);
  2418. #endif
  2419. return(-1);
  2420. }
  2421. int ssl3_send_client_verify(SSL *s)
  2422. {
  2423. unsigned char *p,*d;
  2424. unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
  2425. EVP_PKEY *pkey;
  2426. EVP_PKEY_CTX *pctx=NULL;
  2427. #ifndef OPENSSL_NO_RSA
  2428. unsigned u=0;
  2429. #endif
  2430. unsigned long n;
  2431. int j;
  2432. if (s->state == SSL3_ST_CW_CERT_VRFY_A)
  2433. {
  2434. d=(unsigned char *)s->init_buf->data;
  2435. p= &(d[4]);
  2436. pkey=s->cert->key->privatekey;
  2437. /* Create context from key and test if sha1 is allowed as digest */
  2438. pctx = EVP_PKEY_CTX_new(pkey,NULL);
  2439. EVP_PKEY_sign_init(pctx);
  2440. if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1())>0)
  2441. {
  2442. s->method->ssl3_enc->cert_verify_mac(s,
  2443. NID_sha1,
  2444. &(data[MD5_DIGEST_LENGTH]));
  2445. }
  2446. else
  2447. {
  2448. ERR_clear_error();
  2449. }
  2450. #ifndef OPENSSL_NO_RSA
  2451. if (pkey->type == EVP_PKEY_RSA)
  2452. {
  2453. s->method->ssl3_enc->cert_verify_mac(s,
  2454. NID_md5,
  2455. &(data[0]));
  2456. if (RSA_sign(NID_md5_sha1, data,
  2457. MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
  2458. &(p[2]), &u, pkey->pkey.rsa) <= 0 )
  2459. {
  2460. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_RSA_LIB);
  2461. goto err;
  2462. }
  2463. s2n(u,p);
  2464. n=u+2;
  2465. }
  2466. else
  2467. #endif
  2468. #ifndef OPENSSL_NO_DSA
  2469. if (pkey->type == EVP_PKEY_DSA)
  2470. {
  2471. if (!DSA_sign(pkey->save_type,
  2472. &(data[MD5_DIGEST_LENGTH]),
  2473. SHA_DIGEST_LENGTH,&(p[2]),
  2474. (unsigned int *)&j,pkey->pkey.dsa))
  2475. {
  2476. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_DSA_LIB);
  2477. goto err;
  2478. }
  2479. s2n(j,p);
  2480. n=j+2;
  2481. }
  2482. else
  2483. #endif
  2484. #ifndef OPENSSL_NO_ECDSA
  2485. if (pkey->type == EVP_PKEY_EC)
  2486. {
  2487. if (!ECDSA_sign(pkey->save_type,
  2488. &(data[MD5_DIGEST_LENGTH]),
  2489. SHA_DIGEST_LENGTH,&(p[2]),
  2490. (unsigned int *)&j,pkey->pkey.ec))
  2491. {
  2492. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
  2493. ERR_R_ECDSA_LIB);
  2494. goto err;
  2495. }
  2496. s2n(j,p);
  2497. n=j+2;
  2498. }
  2499. else
  2500. #endif
  2501. if (pkey->type == NID_id_GostR3410_94 || pkey->type == NID_id_GostR3410_2001)
  2502. {
  2503. unsigned char signbuf[64];
  2504. int i;
  2505. size_t sigsize=64;
  2506. s->method->ssl3_enc->cert_verify_mac(s,
  2507. NID_id_GostR3411_94,
  2508. data);
  2509. if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) {
  2510. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
  2511. ERR_R_INTERNAL_ERROR);
  2512. goto err;
  2513. }
  2514. for (i=63,j=0; i>=0; j++, i--) {
  2515. p[2+j]=signbuf[i];
  2516. }
  2517. s2n(j,p);
  2518. n=j+2;
  2519. }
  2520. else
  2521. {
  2522. SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_INTERNAL_ERROR);
  2523. goto err;
  2524. }
  2525. *(d++)=SSL3_MT_CERTIFICATE_VERIFY;
  2526. l2n3(n,d);
  2527. s->state=SSL3_ST_CW_CERT_VRFY_B;
  2528. s->init_num=(int)n+4;
  2529. s->init_off=0;
  2530. }
  2531. EVP_PKEY_CTX_free(pctx);
  2532. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  2533. err:
  2534. EVP_PKEY_CTX_free(pctx);
  2535. return(-1);
  2536. }
  2537. int ssl3_send_client_certificate(SSL *s)
  2538. {
  2539. X509 *x509=NULL;
  2540. EVP_PKEY *pkey=NULL;
  2541. int i;
  2542. unsigned long l;
  2543. if (s->state == SSL3_ST_CW_CERT_A)
  2544. {
  2545. if ((s->cert == NULL) ||
  2546. (s->cert->key->x509 == NULL) ||
  2547. (s->cert->key->privatekey == NULL))
  2548. s->state=SSL3_ST_CW_CERT_B;
  2549. else
  2550. s->state=SSL3_ST_CW_CERT_C;
  2551. }
  2552. /* We need to get a client cert */
  2553. if (s->state == SSL3_ST_CW_CERT_B)
  2554. {
  2555. /* If we get an error, we need to
  2556. * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
  2557. * We then get retied later */
  2558. i=0;
  2559. i = ssl_do_client_cert_cb(s, &x509, &pkey);
  2560. if (i < 0)
  2561. {
  2562. s->rwstate=SSL_X509_LOOKUP;
  2563. return(-1);
  2564. }
  2565. s->rwstate=SSL_NOTHING;
  2566. if ((i == 1) && (pkey != NULL) && (x509 != NULL))
  2567. {
  2568. s->state=SSL3_ST_CW_CERT_B;
  2569. if ( !SSL_use_certificate(s,x509) ||
  2570. !SSL_use_PrivateKey(s,pkey))
  2571. i=0;
  2572. }
  2573. else if (i == 1)
  2574. {
  2575. i=0;
  2576. SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
  2577. }
  2578. if (x509 != NULL) X509_free(x509);
  2579. if (pkey != NULL) EVP_PKEY_free(pkey);
  2580. if (i == 0)
  2581. {
  2582. if (s->version == SSL3_VERSION)
  2583. {
  2584. s->s3->tmp.cert_req=0;
  2585. ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
  2586. return(1);
  2587. }
  2588. else
  2589. {
  2590. s->s3->tmp.cert_req=2;
  2591. }
  2592. }
  2593. /* Ok, we have a cert */
  2594. s->state=SSL3_ST_CW_CERT_C;
  2595. }
  2596. if (s->state == SSL3_ST_CW_CERT_C)
  2597. {
  2598. s->state=SSL3_ST_CW_CERT_D;
  2599. l=ssl3_output_cert_chain(s,
  2600. (s->s3->tmp.cert_req == 2)?NULL:s->cert->key->x509);
  2601. s->init_num=(int)l;
  2602. s->init_off=0;
  2603. }
  2604. /* SSL3_ST_CW_CERT_D */
  2605. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  2606. }
  2607. #define has_bits(i,m) (((i)&(m)) == (m))
  2608. int ssl3_check_cert_and_algorithm(SSL *s)
  2609. {
  2610. int i,idx;
  2611. long alg_k,alg_a;
  2612. EVP_PKEY *pkey=NULL;
  2613. SESS_CERT *sc;
  2614. #ifndef OPENSSL_NO_RSA
  2615. RSA *rsa;
  2616. #endif
  2617. #ifndef OPENSSL_NO_DH
  2618. DH *dh;
  2619. #endif
  2620. alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
  2621. alg_a=s->s3->tmp.new_cipher->algorithm_auth;
  2622. /* we don't have a certificate */
  2623. if ((alg_a & (SSL_aDH|SSL_aNULL|SSL_aKRB5)) || (alg_k & SSL_kPSK))
  2624. return(1);
  2625. sc=s->session->sess_cert;
  2626. if (sc == NULL)
  2627. {
  2628. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,ERR_R_INTERNAL_ERROR);
  2629. goto err;
  2630. }
  2631. #ifndef OPENSSL_NO_RSA
  2632. rsa=s->session->sess_cert->peer_rsa_tmp;
  2633. #endif
  2634. #ifndef OPENSSL_NO_DH
  2635. dh=s->session->sess_cert->peer_dh_tmp;
  2636. #endif
  2637. /* This is the passed certificate */
  2638. idx=sc->peer_cert_type;
  2639. #ifndef OPENSSL_NO_ECDH
  2640. if (idx == SSL_PKEY_ECC)
  2641. {
  2642. if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509,
  2643. s->s3->tmp.new_cipher) == 0)
  2644. { /* check failed */
  2645. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_BAD_ECC_CERT);
  2646. goto f_err;
  2647. }
  2648. else
  2649. {
  2650. return 1;
  2651. }
  2652. }
  2653. #endif
  2654. pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509);
  2655. i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey);
  2656. EVP_PKEY_free(pkey);
  2657. /* Check that we have a certificate if we require one */
  2658. if ((alg_a & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))
  2659. {
  2660. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_SIGNING_CERT);
  2661. goto f_err;
  2662. }
  2663. #ifndef OPENSSL_NO_DSA
  2664. else if ((alg_a & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))
  2665. {
  2666. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DSA_SIGNING_CERT);
  2667. goto f_err;
  2668. }
  2669. #endif
  2670. #ifndef OPENSSL_NO_RSA
  2671. if ((alg_k & SSL_kRSA) &&
  2672. !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))
  2673. {
  2674. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_ENCRYPTING_CERT);
  2675. goto f_err;
  2676. }
  2677. #endif
  2678. #ifndef OPENSSL_NO_DH
  2679. if ((alg_k & SSL_kEDH) &&
  2680. !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))
  2681. {
  2682. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_KEY);
  2683. goto f_err;
  2684. }
  2685. else if ((alg_k & SSL_kDHr) && !has_bits(i,EVP_PK_DH|EVP_PKS_RSA))
  2686. {
  2687. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_RSA_CERT);
  2688. goto f_err;
  2689. }
  2690. #ifndef OPENSSL_NO_DSA
  2691. else if ((alg_k & SSL_kDHd) && !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))
  2692. {
  2693. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_DSA_CERT);
  2694. goto f_err;
  2695. }
  2696. #endif
  2697. #endif
  2698. if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i,EVP_PKT_EXP))
  2699. {
  2700. #ifndef OPENSSL_NO_RSA
  2701. if (alg_k & SSL_kRSA)
  2702. {
  2703. if (rsa == NULL
  2704. || RSA_size(rsa)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
  2705. {
  2706. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
  2707. goto f_err;
  2708. }
  2709. }
  2710. else
  2711. #endif
  2712. #ifndef OPENSSL_NO_DH
  2713. if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
  2714. {
  2715. if (dh == NULL
  2716. || DH_size(dh)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
  2717. {
  2718. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_DH_KEY);
  2719. goto f_err;
  2720. }
  2721. }
  2722. else
  2723. #endif
  2724. {
  2725. SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
  2726. goto f_err;
  2727. }
  2728. }
  2729. return(1);
  2730. f_err:
  2731. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
  2732. err:
  2733. return(0);
  2734. }
  2735. /* Check to see if handshake is full or resumed. Usually this is just a
  2736. * case of checking to see if a cache hit has occurred. In the case of
  2737. * session tickets we have to check the next message to be sure.
  2738. */
  2739. #ifndef OPENSSL_NO_TLSEXT
  2740. int ssl3_check_finished(SSL *s)
  2741. {
  2742. int ok;
  2743. long n;
  2744. /* If we have no ticket it cannot be a resumed session. */
  2745. if (!s->session->tlsext_tick)
  2746. return 1;
  2747. /* this function is called when we really expect a Certificate
  2748. * message, so permit appropriate message length */
  2749. n=s->method->ssl_get_message(s,
  2750. SSL3_ST_CR_CERT_A,
  2751. SSL3_ST_CR_CERT_B,
  2752. -1,
  2753. s->max_cert_list,
  2754. &ok);
  2755. if (!ok) return((int)n);
  2756. s->s3->tmp.reuse_message = 1;
  2757. if ((s->s3->tmp.message_type == SSL3_MT_FINISHED)
  2758. || (s->s3->tmp.message_type == SSL3_MT_NEWSESSION_TICKET))
  2759. return 2;
  2760. return 1;
  2761. }
  2762. #endif
  2763. int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
  2764. {
  2765. int i = 0;
  2766. #ifndef OPENSSL_NO_ENGINE
  2767. if (s->ctx->client_cert_engine)
  2768. {
  2769. i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
  2770. SSL_get_client_CA_list(s),
  2771. px509, ppkey, NULL, NULL, NULL);
  2772. if (i != 0)
  2773. return i;
  2774. }
  2775. #endif
  2776. if (s->ctx->client_cert_cb)
  2777. i = s->ctx->client_cert_cb(s,px509,ppkey);
  2778. return i;
  2779. }