s3_srvr.c 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281
  1. /* ssl/s3_srvr.c -*- mode:C; c-file-style: "eay" -*- */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. /* ====================================================================
  59. * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
  60. *
  61. * Redistribution and use in source and binary forms, with or without
  62. * modification, are permitted provided that the following conditions
  63. * are met:
  64. *
  65. * 1. Redistributions of source code must retain the above copyright
  66. * notice, this list of conditions and the following disclaimer.
  67. *
  68. * 2. Redistributions in binary form must reproduce the above copyright
  69. * notice, this list of conditions and the following disclaimer in
  70. * the documentation and/or other materials provided with the
  71. * distribution.
  72. *
  73. * 3. All advertising materials mentioning features or use of this
  74. * software must display the following acknowledgment:
  75. * "This product includes software developed by the OpenSSL Project
  76. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  77. *
  78. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  79. * endorse or promote products derived from this software without
  80. * prior written permission. For written permission, please contact
  81. * openssl-core@openssl.org.
  82. *
  83. * 5. Products derived from this software may not be called "OpenSSL"
  84. * nor may "OpenSSL" appear in their names without prior written
  85. * permission of the OpenSSL Project.
  86. *
  87. * 6. Redistributions of any form whatsoever must retain the following
  88. * acknowledgment:
  89. * "This product includes software developed by the OpenSSL Project
  90. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  91. *
  92. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  93. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  94. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  95. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  96. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  97. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  98. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  99. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  100. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  101. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  102. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  103. * OF THE POSSIBILITY OF SUCH DAMAGE.
  104. * ====================================================================
  105. *
  106. * This product includes cryptographic software written by Eric Young
  107. * (eay@cryptsoft.com). This product includes software written by Tim
  108. * Hudson (tjh@cryptsoft.com).
  109. *
  110. */
  111. /* ====================================================================
  112. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  113. *
  114. * Portions of the attached software ("Contribution") are developed by
  115. * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
  116. *
  117. * The Contribution is licensed pursuant to the OpenSSL open source
  118. * license provided above.
  119. *
  120. * ECC cipher suite support in OpenSSL originally written by
  121. * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
  122. *
  123. */
  124. /* ====================================================================
  125. * Copyright 2005 Nokia. All rights reserved.
  126. *
  127. * The portions of the attached software ("Contribution") is developed by
  128. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  129. * license.
  130. *
  131. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  132. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  133. * support (see RFC 4279) to OpenSSL.
  134. *
  135. * No patent licenses or other rights except those expressly stated in
  136. * the OpenSSL open source license shall be deemed granted or received
  137. * expressly, by implication, estoppel, or otherwise.
  138. *
  139. * No assurances are provided by Nokia that the Contribution does not
  140. * infringe the patent or other intellectual property rights of any third
  141. * party or that the license provides you with all the necessary rights
  142. * to make use of the Contribution.
  143. *
  144. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  145. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  146. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  147. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  148. * OTHERWISE.
  149. */
  150. #define REUSE_CIPHER_BUG
  151. #define NETSCAPE_HANG_BUG
  152. #include <stdio.h>
  153. #include "ssl_locl.h"
  154. #include "kssl_lcl.h"
  155. #include <openssl/buffer.h>
  156. #include <openssl/rand.h>
  157. #include <openssl/objects.h>
  158. #include <openssl/evp.h>
  159. #include <openssl/hmac.h>
  160. #include <openssl/x509.h>
  161. #ifndef OPENSSL_NO_DH
  162. #include <openssl/dh.h>
  163. #endif
  164. #include <openssl/bn.h>
  165. #ifndef OPENSSL_NO_KRB5
  166. #include <openssl/krb5_asn.h>
  167. #endif
  168. #include <openssl/md5.h>
  169. static const SSL_METHOD *ssl3_get_server_method(int ver);
  170. static const SSL_METHOD *ssl3_get_server_method(int ver)
  171. {
  172. if (ver == SSL3_VERSION)
  173. return(SSLv3_server_method());
  174. else
  175. return(NULL);
  176. }
  177. IMPLEMENT_ssl3_meth_func(SSLv3_server_method,
  178. ssl3_accept,
  179. ssl_undefined_function,
  180. ssl3_get_server_method)
  181. int ssl3_accept(SSL *s)
  182. {
  183. BUF_MEM *buf;
  184. unsigned long alg_k,Time=(unsigned long)time(NULL);
  185. void (*cb)(const SSL *ssl,int type,int val)=NULL;
  186. int ret= -1;
  187. int new_state,state,skip=0;
  188. RAND_add(&Time,sizeof(Time),0);
  189. ERR_clear_error();
  190. clear_sys_error();
  191. if (s->info_callback != NULL)
  192. cb=s->info_callback;
  193. else if (s->ctx->info_callback != NULL)
  194. cb=s->ctx->info_callback;
  195. /* init things to blank */
  196. s->in_handshake++;
  197. if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
  198. if (s->cert == NULL)
  199. {
  200. SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_NO_CERTIFICATE_SET);
  201. return(-1);
  202. }
  203. for (;;)
  204. {
  205. state=s->state;
  206. switch (s->state)
  207. {
  208. case SSL_ST_RENEGOTIATE:
  209. s->new_session=1;
  210. /* s->state=SSL_ST_ACCEPT; */
  211. case SSL_ST_BEFORE:
  212. case SSL_ST_ACCEPT:
  213. case SSL_ST_BEFORE|SSL_ST_ACCEPT:
  214. case SSL_ST_OK|SSL_ST_ACCEPT:
  215. s->server=1;
  216. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
  217. if ((s->version>>8) != 3)
  218. {
  219. SSLerr(SSL_F_SSL3_ACCEPT, ERR_R_INTERNAL_ERROR);
  220. return -1;
  221. }
  222. s->type=SSL_ST_ACCEPT;
  223. if (s->init_buf == NULL)
  224. {
  225. if ((buf=BUF_MEM_new()) == NULL)
  226. {
  227. ret= -1;
  228. goto end;
  229. }
  230. if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
  231. {
  232. ret= -1;
  233. goto end;
  234. }
  235. s->init_buf=buf;
  236. }
  237. if (!ssl3_setup_buffers(s))
  238. {
  239. ret= -1;
  240. goto end;
  241. }
  242. s->init_num=0;
  243. if (s->state != SSL_ST_RENEGOTIATE)
  244. {
  245. /* Ok, we now need to push on a buffering BIO so that
  246. * the output is sent in a way that TCP likes :-)
  247. */
  248. if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; }
  249. ssl3_init_finished_mac(s);
  250. s->state=SSL3_ST_SR_CLNT_HELLO_A;
  251. s->ctx->stats.sess_accept++;
  252. }
  253. else if (!s->s3->send_connection_binding &&
  254. !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
  255. {
  256. /* Server attempting to renegotiate with
  257. * client that doesn't support secure
  258. * renegotiation.
  259. */
  260. SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
  261. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
  262. ret = -1;
  263. goto end;
  264. }
  265. else
  266. {
  267. /* s->state == SSL_ST_RENEGOTIATE,
  268. * we will just send a HelloRequest */
  269. s->ctx->stats.sess_accept_renegotiate++;
  270. s->state=SSL3_ST_SW_HELLO_REQ_A;
  271. }
  272. break;
  273. case SSL3_ST_SW_HELLO_REQ_A:
  274. case SSL3_ST_SW_HELLO_REQ_B:
  275. s->shutdown=0;
  276. ret=ssl3_send_hello_request(s);
  277. if (ret <= 0) goto end;
  278. s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C;
  279. s->state=SSL3_ST_SW_FLUSH;
  280. s->init_num=0;
  281. ssl3_init_finished_mac(s);
  282. break;
  283. case SSL3_ST_SW_HELLO_REQ_C:
  284. s->state=SSL_ST_OK;
  285. break;
  286. case SSL3_ST_SR_CLNT_HELLO_A:
  287. case SSL3_ST_SR_CLNT_HELLO_B:
  288. case SSL3_ST_SR_CLNT_HELLO_C:
  289. s->shutdown=0;
  290. ret=ssl3_get_client_hello(s);
  291. if (ret <= 0) goto end;
  292. s->new_session = 2;
  293. s->state=SSL3_ST_SW_SRVR_HELLO_A;
  294. s->init_num=0;
  295. break;
  296. case SSL3_ST_SW_SRVR_HELLO_A:
  297. case SSL3_ST_SW_SRVR_HELLO_B:
  298. ret=ssl3_send_server_hello(s);
  299. if (ret <= 0) goto end;
  300. #ifndef OPENSSL_NO_TLSEXT
  301. if (s->hit)
  302. {
  303. if (s->tlsext_ticket_expected)
  304. s->state=SSL3_ST_SW_SESSION_TICKET_A;
  305. else
  306. s->state=SSL3_ST_SW_CHANGE_A;
  307. }
  308. #else
  309. if (s->hit)
  310. s->state=SSL3_ST_SW_CHANGE_A;
  311. #endif
  312. else
  313. s->state=SSL3_ST_SW_CERT_A;
  314. s->init_num=0;
  315. break;
  316. case SSL3_ST_SW_CERT_A:
  317. case SSL3_ST_SW_CERT_B:
  318. /* Check if it is anon DH or anon ECDH, */
  319. /* normal PSK or KRB5 */
  320. if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
  321. && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)
  322. && !(s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
  323. {
  324. ret=ssl3_send_server_certificate(s);
  325. if (ret <= 0) goto end;
  326. #ifndef OPENSSL_NO_TLSEXT
  327. if (s->tlsext_status_expected)
  328. s->state=SSL3_ST_SW_CERT_STATUS_A;
  329. else
  330. s->state=SSL3_ST_SW_KEY_EXCH_A;
  331. }
  332. else
  333. {
  334. skip = 1;
  335. s->state=SSL3_ST_SW_KEY_EXCH_A;
  336. }
  337. #else
  338. }
  339. else
  340. skip=1;
  341. s->state=SSL3_ST_SW_KEY_EXCH_A;
  342. #endif
  343. s->init_num=0;
  344. break;
  345. case SSL3_ST_SW_KEY_EXCH_A:
  346. case SSL3_ST_SW_KEY_EXCH_B:
  347. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  348. /* clear this, it may get reset by
  349. * send_server_key_exchange */
  350. if ((s->options & SSL_OP_EPHEMERAL_RSA)
  351. #ifndef OPENSSL_NO_KRB5
  352. && !(alg_k & SSL_kKRB5)
  353. #endif /* OPENSSL_NO_KRB5 */
  354. )
  355. /* option SSL_OP_EPHEMERAL_RSA sends temporary RSA key
  356. * even when forbidden by protocol specs
  357. * (handshake may fail as clients are not required to
  358. * be able to handle this) */
  359. s->s3->tmp.use_rsa_tmp=1;
  360. else
  361. s->s3->tmp.use_rsa_tmp=0;
  362. /* only send if a DH key exchange, fortezza or
  363. * RSA but we have a sign only certificate
  364. *
  365. * PSK: may send PSK identity hints
  366. *
  367. * For ECC ciphersuites, we send a serverKeyExchange
  368. * message only if the cipher suite is either
  369. * ECDH-anon or ECDHE. In other cases, the
  370. * server certificate contains the server's
  371. * public key for key exchange.
  372. */
  373. if (s->s3->tmp.use_rsa_tmp
  374. /* PSK: send ServerKeyExchange if PSK identity
  375. * hint if provided */
  376. #ifndef OPENSSL_NO_PSK
  377. || ((alg_k & SSL_kPSK) && s->ctx->psk_identity_hint)
  378. #endif
  379. || (alg_k & (SSL_kDHr|SSL_kDHd|SSL_kEDH))
  380. || (alg_k & SSL_kEECDH)
  381. || ((alg_k & SSL_kRSA)
  382. && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
  383. || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
  384. && EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
  385. )
  386. )
  387. )
  388. )
  389. {
  390. ret=ssl3_send_server_key_exchange(s);
  391. if (ret <= 0) goto end;
  392. }
  393. else
  394. skip=1;
  395. s->state=SSL3_ST_SW_CERT_REQ_A;
  396. s->init_num=0;
  397. break;
  398. case SSL3_ST_SW_CERT_REQ_A:
  399. case SSL3_ST_SW_CERT_REQ_B:
  400. if (/* don't request cert unless asked for it: */
  401. !(s->verify_mode & SSL_VERIFY_PEER) ||
  402. /* if SSL_VERIFY_CLIENT_ONCE is set,
  403. * don't request cert during re-negotiation: */
  404. ((s->session->peer != NULL) &&
  405. (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
  406. /* never request cert in anonymous ciphersuites
  407. * (see section "Certificate request" in SSL 3 drafts
  408. * and in RFC 2246): */
  409. ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
  410. /* ... except when the application insists on verification
  411. * (against the specs, but s3_clnt.c accepts this for SSL 3) */
  412. !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
  413. /* never request cert in Kerberos ciphersuites */
  414. (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5)
  415. /* With normal PSK Certificates and
  416. * Certificate Requests are omitted */
  417. || (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
  418. {
  419. /* no cert request */
  420. skip=1;
  421. s->s3->tmp.cert_request=0;
  422. s->state=SSL3_ST_SW_SRVR_DONE_A;
  423. }
  424. else
  425. {
  426. s->s3->tmp.cert_request=1;
  427. ret=ssl3_send_certificate_request(s);
  428. if (ret <= 0) goto end;
  429. #ifndef NETSCAPE_HANG_BUG
  430. s->state=SSL3_ST_SW_SRVR_DONE_A;
  431. #else
  432. s->state=SSL3_ST_SW_FLUSH;
  433. s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
  434. #endif
  435. s->init_num=0;
  436. }
  437. break;
  438. case SSL3_ST_SW_SRVR_DONE_A:
  439. case SSL3_ST_SW_SRVR_DONE_B:
  440. ret=ssl3_send_server_done(s);
  441. if (ret <= 0) goto end;
  442. s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
  443. s->state=SSL3_ST_SW_FLUSH;
  444. s->init_num=0;
  445. break;
  446. case SSL3_ST_SW_FLUSH:
  447. /* This code originally checked to see if
  448. * any data was pending using BIO_CTRL_INFO
  449. * and then flushed. This caused problems
  450. * as documented in PR#1939. The proposed
  451. * fix doesn't completely resolve this issue
  452. * as buggy implementations of BIO_CTRL_PENDING
  453. * still exist. So instead we just flush
  454. * unconditionally.
  455. */
  456. s->rwstate=SSL_WRITING;
  457. if (BIO_flush(s->wbio) <= 0)
  458. {
  459. ret= -1;
  460. goto end;
  461. }
  462. s->rwstate=SSL_NOTHING;
  463. s->state=s->s3->tmp.next_state;
  464. break;
  465. case SSL3_ST_SR_CERT_A:
  466. case SSL3_ST_SR_CERT_B:
  467. /* Check for second client hello (MS SGC) */
  468. ret = ssl3_check_client_hello(s);
  469. if (ret <= 0)
  470. goto end;
  471. if (ret == 2)
  472. s->state = SSL3_ST_SR_CLNT_HELLO_C;
  473. else {
  474. if (s->s3->tmp.cert_request)
  475. {
  476. ret=ssl3_get_client_certificate(s);
  477. if (ret <= 0) goto end;
  478. }
  479. s->init_num=0;
  480. s->state=SSL3_ST_SR_KEY_EXCH_A;
  481. }
  482. break;
  483. case SSL3_ST_SR_KEY_EXCH_A:
  484. case SSL3_ST_SR_KEY_EXCH_B:
  485. ret=ssl3_get_client_key_exchange(s);
  486. if (ret <= 0)
  487. goto end;
  488. if (ret == 2)
  489. {
  490. /* For the ECDH ciphersuites when
  491. * the client sends its ECDH pub key in
  492. * a certificate, the CertificateVerify
  493. * message is not sent.
  494. * Also for GOST ciphersuites when
  495. * the client uses its key from the certificate
  496. * for key exchange.
  497. */
  498. #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NPN)
  499. s->state=SSL3_ST_SR_FINISHED_A;
  500. #else
  501. if (s->s3->next_proto_neg_seen)
  502. s->state=SSL3_ST_SR_NEXT_PROTO_A;
  503. else
  504. s->state=SSL3_ST_SR_FINISHED_A;
  505. #endif
  506. s->init_num = 0;
  507. }
  508. else
  509. {
  510. int offset=0;
  511. int dgst_num;
  512. s->state=SSL3_ST_SR_CERT_VRFY_A;
  513. s->init_num=0;
  514. /* We need to get hashes here so if there is
  515. * a client cert, it can be verified
  516. * FIXME - digest processing for CertificateVerify
  517. * should be generalized. But it is next step
  518. */
  519. if (s->s3->handshake_buffer)
  520. if (!ssl3_digest_cached_records(s))
  521. return -1;
  522. for (dgst_num=0; dgst_num<SSL_MAX_DIGEST;dgst_num++)
  523. if (s->s3->handshake_dgst[dgst_num])
  524. {
  525. int dgst_size;
  526. s->method->ssl3_enc->cert_verify_mac(s,EVP_MD_CTX_type(s->s3->handshake_dgst[dgst_num]),&(s->s3->tmp.cert_verify_md[offset]));
  527. dgst_size=EVP_MD_CTX_size(s->s3->handshake_dgst[dgst_num]);
  528. if (dgst_size < 0)
  529. {
  530. ret = -1;
  531. goto end;
  532. }
  533. offset+=dgst_size;
  534. }
  535. }
  536. break;
  537. case SSL3_ST_SR_CERT_VRFY_A:
  538. case SSL3_ST_SR_CERT_VRFY_B:
  539. /* we should decide if we expected this one */
  540. ret=ssl3_get_cert_verify(s);
  541. if (ret <= 0) goto end;
  542. #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NPN)
  543. s->state=SSL3_ST_SR_FINISHED_A;
  544. #else
  545. if (s->s3->next_proto_neg_seen)
  546. s->state=SSL3_ST_SR_NEXT_PROTO_A;
  547. else
  548. s->state=SSL3_ST_SR_FINISHED_A;
  549. #endif
  550. s->init_num=0;
  551. break;
  552. #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NPN)
  553. case SSL3_ST_SR_NEXT_PROTO_A:
  554. case SSL3_ST_SR_NEXT_PROTO_B:
  555. ret=ssl3_get_next_proto(s);
  556. if (ret <= 0) goto end;
  557. s->init_num = 0;
  558. s->state=SSL3_ST_SR_FINISHED_A;
  559. break;
  560. #endif
  561. case SSL3_ST_SR_FINISHED_A:
  562. case SSL3_ST_SR_FINISHED_B:
  563. ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A,
  564. SSL3_ST_SR_FINISHED_B);
  565. if (ret <= 0) goto end;
  566. #ifndef OPENSSL_NO_TLSEXT
  567. if (s->tlsext_ticket_expected)
  568. s->state=SSL3_ST_SW_SESSION_TICKET_A;
  569. else if (s->hit)
  570. s->state=SSL_ST_OK;
  571. #else
  572. if (s->hit)
  573. s->state=SSL_ST_OK;
  574. #endif
  575. else
  576. s->state=SSL3_ST_SW_CHANGE_A;
  577. s->init_num=0;
  578. break;
  579. #ifndef OPENSSL_NO_TLSEXT
  580. case SSL3_ST_SW_SESSION_TICKET_A:
  581. case SSL3_ST_SW_SESSION_TICKET_B:
  582. ret=ssl3_send_newsession_ticket(s);
  583. if (ret <= 0) goto end;
  584. s->state=SSL3_ST_SW_CHANGE_A;
  585. s->init_num=0;
  586. break;
  587. case SSL3_ST_SW_CERT_STATUS_A:
  588. case SSL3_ST_SW_CERT_STATUS_B:
  589. ret=ssl3_send_cert_status(s);
  590. if (ret <= 0) goto end;
  591. s->state=SSL3_ST_SW_KEY_EXCH_A;
  592. s->init_num=0;
  593. break;
  594. #endif
  595. case SSL3_ST_SW_CHANGE_A:
  596. case SSL3_ST_SW_CHANGE_B:
  597. s->session->cipher=s->s3->tmp.new_cipher;
  598. if (!s->method->ssl3_enc->setup_key_block(s))
  599. { ret= -1; goto end; }
  600. ret=ssl3_send_change_cipher_spec(s,
  601. SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B);
  602. if (ret <= 0) goto end;
  603. s->state=SSL3_ST_SW_FINISHED_A;
  604. s->init_num=0;
  605. if (!s->method->ssl3_enc->change_cipher_state(s,
  606. SSL3_CHANGE_CIPHER_SERVER_WRITE))
  607. {
  608. ret= -1;
  609. goto end;
  610. }
  611. break;
  612. case SSL3_ST_SW_FINISHED_A:
  613. case SSL3_ST_SW_FINISHED_B:
  614. ret=ssl3_send_finished(s,
  615. SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B,
  616. s->method->ssl3_enc->server_finished_label,
  617. s->method->ssl3_enc->server_finished_label_len);
  618. if (ret <= 0) goto end;
  619. s->state=SSL3_ST_SW_FLUSH;
  620. if (s->hit)
  621. {
  622. #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NPN)
  623. s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A;
  624. #else
  625. if (s->s3->next_proto_neg_seen)
  626. s->s3->tmp.next_state=SSL3_ST_SR_NEXT_PROTO_A;
  627. else
  628. s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A;
  629. #endif
  630. }
  631. else
  632. s->s3->tmp.next_state=SSL_ST_OK;
  633. s->init_num=0;
  634. break;
  635. case SSL_ST_OK:
  636. /* clean a few things up */
  637. ssl3_cleanup_key_block(s);
  638. BUF_MEM_free(s->init_buf);
  639. s->init_buf=NULL;
  640. /* remove buffering on output */
  641. ssl_free_wbio_buffer(s);
  642. s->init_num=0;
  643. if (s->new_session == 2) /* skipped if we just sent a HelloRequest */
  644. {
  645. /* actually not necessarily a 'new' session unless
  646. * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is set */
  647. s->new_session=0;
  648. ssl_update_cache(s,SSL_SESS_CACHE_SERVER);
  649. s->ctx->stats.sess_accept_good++;
  650. /* s->server=1; */
  651. s->handshake_func=ssl3_accept;
  652. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
  653. }
  654. ret = 1;
  655. goto end;
  656. /* break; */
  657. default:
  658. SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_UNKNOWN_STATE);
  659. ret= -1;
  660. goto end;
  661. /* break; */
  662. }
  663. if (!s->s3->tmp.reuse_message && !skip)
  664. {
  665. if (s->debug)
  666. {
  667. if ((ret=BIO_flush(s->wbio)) <= 0)
  668. goto end;
  669. }
  670. if ((cb != NULL) && (s->state != state))
  671. {
  672. new_state=s->state;
  673. s->state=state;
  674. cb(s,SSL_CB_ACCEPT_LOOP,1);
  675. s->state=new_state;
  676. }
  677. }
  678. skip=0;
  679. }
  680. end:
  681. /* BIO_flush(s->wbio); */
  682. s->in_handshake--;
  683. if (cb != NULL)
  684. cb(s,SSL_CB_ACCEPT_EXIT,ret);
  685. return(ret);
  686. }
  687. int ssl3_send_hello_request(SSL *s)
  688. {
  689. unsigned char *p;
  690. if (s->state == SSL3_ST_SW_HELLO_REQ_A)
  691. {
  692. p=(unsigned char *)s->init_buf->data;
  693. *(p++)=SSL3_MT_HELLO_REQUEST;
  694. *(p++)=0;
  695. *(p++)=0;
  696. *(p++)=0;
  697. s->state=SSL3_ST_SW_HELLO_REQ_B;
  698. /* number of bytes to write */
  699. s->init_num=4;
  700. s->init_off=0;
  701. }
  702. /* SSL3_ST_SW_HELLO_REQ_B */
  703. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  704. }
  705. int ssl3_check_client_hello(SSL *s)
  706. {
  707. int ok;
  708. long n;
  709. /* this function is called when we really expect a Certificate message,
  710. * so permit appropriate message length */
  711. n=s->method->ssl_get_message(s,
  712. SSL3_ST_SR_CERT_A,
  713. SSL3_ST_SR_CERT_B,
  714. -1,
  715. s->max_cert_list,
  716. &ok);
  717. if (!ok) return((int)n);
  718. s->s3->tmp.reuse_message = 1;
  719. if (s->s3->tmp.message_type == SSL3_MT_CLIENT_HELLO)
  720. {
  721. /* Throw away what we have done so far in the current handshake,
  722. * which will now be aborted. (A full SSL_clear would be too much.)
  723. * I hope that tmp.dh is the only thing that may need to be cleared
  724. * when a handshake is not completed ... */
  725. #ifndef OPENSSL_NO_DH
  726. if (s->s3->tmp.dh != NULL)
  727. {
  728. DH_free(s->s3->tmp.dh);
  729. s->s3->tmp.dh = NULL;
  730. }
  731. #endif
  732. return 2;
  733. }
  734. return 1;
  735. }
  736. int ssl3_get_client_hello(SSL *s)
  737. {
  738. int i,j,ok,al,ret= -1;
  739. unsigned int cookie_len;
  740. long n;
  741. unsigned long id;
  742. unsigned char *p,*d,*q;
  743. SSL_CIPHER *c;
  744. #ifndef OPENSSL_NO_COMP
  745. SSL_COMP *comp=NULL;
  746. #endif
  747. STACK_OF(SSL_CIPHER) *ciphers=NULL;
  748. /* We do this so that we will respond with our native type.
  749. * If we are TLSv1 and we get SSLv3, we will respond with TLSv1,
  750. * This down switching should be handled by a different method.
  751. * If we are SSLv3, we will respond with SSLv3, even if prompted with
  752. * TLSv1.
  753. */
  754. if (s->state == SSL3_ST_SR_CLNT_HELLO_A)
  755. {
  756. s->state=SSL3_ST_SR_CLNT_HELLO_B;
  757. }
  758. s->first_packet=1;
  759. n=s->method->ssl_get_message(s,
  760. SSL3_ST_SR_CLNT_HELLO_B,
  761. SSL3_ST_SR_CLNT_HELLO_C,
  762. SSL3_MT_CLIENT_HELLO,
  763. SSL3_RT_MAX_PLAIN_LENGTH,
  764. &ok);
  765. if (!ok) return((int)n);
  766. s->first_packet=0;
  767. d=p=(unsigned char *)s->init_msg;
  768. /* use version from inside client hello, not from record header
  769. * (may differ: see RFC 2246, Appendix E, second paragraph) */
  770. s->client_version=(((int)p[0])<<8)|(int)p[1];
  771. p+=2;
  772. if ((s->version == DTLS1_VERSION && s->client_version > s->version) ||
  773. (s->version != DTLS1_VERSION && s->client_version < s->version))
  774. {
  775. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_WRONG_VERSION_NUMBER);
  776. if ((s->client_version>>8) == SSL3_VERSION_MAJOR)
  777. {
  778. /* similar to ssl3_get_record, send alert using remote version number */
  779. s->version = s->client_version;
  780. }
  781. al = SSL_AD_PROTOCOL_VERSION;
  782. goto f_err;
  783. }
  784. /* If we require cookies and this ClientHello doesn't
  785. * contain one, just return since we do not want to
  786. * allocate any memory yet. So check cookie length...
  787. */
  788. if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE)
  789. {
  790. unsigned int session_length, cookie_length;
  791. session_length = *(p + SSL3_RANDOM_SIZE);
  792. cookie_length = *(p + SSL3_RANDOM_SIZE + session_length + 1);
  793. if (cookie_length == 0)
  794. return 1;
  795. }
  796. /* load the client random */
  797. memcpy(s->s3->client_random,p,SSL3_RANDOM_SIZE);
  798. p+=SSL3_RANDOM_SIZE;
  799. /* get the session-id */
  800. j= *(p++);
  801. s->hit=0;
  802. /* Versions before 0.9.7 always allow session reuse during renegotiation
  803. * (i.e. when s->new_session is true), option
  804. * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is new with 0.9.7.
  805. * Maybe this optional behaviour should always have been the default,
  806. * but we cannot safely change the default behaviour (or new applications
  807. * might be written that become totally unsecure when compiled with
  808. * an earlier library version)
  809. */
  810. if ((s->new_session && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION)))
  811. {
  812. if (!ssl_get_new_session(s,1))
  813. goto err;
  814. }
  815. else
  816. {
  817. i=ssl_get_prev_session(s, p, j, d + n);
  818. if (i == 1)
  819. { /* previous session */
  820. s->hit=1;
  821. }
  822. else if (i == -1)
  823. goto err;
  824. else /* i == 0 */
  825. {
  826. if (!ssl_get_new_session(s,1))
  827. goto err;
  828. }
  829. }
  830. p+=j;
  831. if (s->version == DTLS1_VERSION || s->version == DTLS1_BAD_VER)
  832. {
  833. /* cookie stuff */
  834. cookie_len = *(p++);
  835. /*
  836. * The ClientHello may contain a cookie even if the
  837. * HelloVerify message has not been sent--make sure that it
  838. * does not cause an overflow.
  839. */
  840. if ( cookie_len > sizeof(s->d1->rcvd_cookie))
  841. {
  842. /* too much data */
  843. al = SSL_AD_DECODE_ERROR;
  844. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
  845. goto f_err;
  846. }
  847. /* verify the cookie if appropriate option is set. */
  848. if ((SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) &&
  849. cookie_len > 0)
  850. {
  851. memcpy(s->d1->rcvd_cookie, p, cookie_len);
  852. if ( s->ctx->app_verify_cookie_cb != NULL)
  853. {
  854. if ( s->ctx->app_verify_cookie_cb(s, s->d1->rcvd_cookie,
  855. cookie_len) == 0)
  856. {
  857. al=SSL_AD_HANDSHAKE_FAILURE;
  858. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
  859. SSL_R_COOKIE_MISMATCH);
  860. goto f_err;
  861. }
  862. /* else cookie verification succeeded */
  863. }
  864. else if ( memcmp(s->d1->rcvd_cookie, s->d1->cookie,
  865. s->d1->cookie_len) != 0) /* default verification */
  866. {
  867. al=SSL_AD_HANDSHAKE_FAILURE;
  868. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
  869. SSL_R_COOKIE_MISMATCH);
  870. goto f_err;
  871. }
  872. ret = 2;
  873. }
  874. p += cookie_len;
  875. }
  876. n2s(p,i);
  877. if ((i == 0) && (j != 0))
  878. {
  879. /* we need a cipher if we are not resuming a session */
  880. al=SSL_AD_ILLEGAL_PARAMETER;
  881. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_SPECIFIED);
  882. goto f_err;
  883. }
  884. if ((p+i) >= (d+n))
  885. {
  886. /* not enough data */
  887. al=SSL_AD_DECODE_ERROR;
  888. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
  889. goto f_err;
  890. }
  891. if ((i > 0) && (ssl_bytes_to_cipher_list(s,p,i,&(ciphers))
  892. == NULL))
  893. {
  894. goto err;
  895. }
  896. p+=i;
  897. /* If it is a hit, check that the cipher is in the list */
  898. if ((s->hit) && (i > 0))
  899. {
  900. j=0;
  901. id=s->session->cipher->id;
  902. #ifdef CIPHER_DEBUG
  903. printf("client sent %d ciphers\n",sk_num(ciphers));
  904. #endif
  905. for (i=0; i<sk_SSL_CIPHER_num(ciphers); i++)
  906. {
  907. c=sk_SSL_CIPHER_value(ciphers,i);
  908. #ifdef CIPHER_DEBUG
  909. printf("client [%2d of %2d]:%s\n",
  910. i,sk_num(ciphers),SSL_CIPHER_get_name(c));
  911. #endif
  912. if (c->id == id)
  913. {
  914. j=1;
  915. break;
  916. }
  917. }
  918. if (j == 0 && (s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG) && (sk_SSL_CIPHER_num(ciphers) == 1))
  919. {
  920. /* Special case as client bug workaround: the previously used cipher may
  921. * not be in the current list, the client instead might be trying to
  922. * continue using a cipher that before wasn't chosen due to server
  923. * preferences. We'll have to reject the connection if the cipher is not
  924. * enabled, though. */
  925. c = sk_SSL_CIPHER_value(ciphers, 0);
  926. if (sk_SSL_CIPHER_find(SSL_get_ciphers(s), c) >= 0)
  927. {
  928. s->session->cipher = c;
  929. j = 1;
  930. }
  931. }
  932. if (j == 0)
  933. {
  934. /* we need to have the cipher in the cipher
  935. * list if we are asked to reuse it */
  936. al=SSL_AD_ILLEGAL_PARAMETER;
  937. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_REQUIRED_CIPHER_MISSING);
  938. goto f_err;
  939. }
  940. }
  941. /* compression */
  942. i= *(p++);
  943. if ((p+i) > (d+n))
  944. {
  945. /* not enough data */
  946. al=SSL_AD_DECODE_ERROR;
  947. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
  948. goto f_err;
  949. }
  950. q=p;
  951. for (j=0; j<i; j++)
  952. {
  953. if (p[j] == 0) break;
  954. }
  955. p+=i;
  956. if (j >= i)
  957. {
  958. /* no compress */
  959. al=SSL_AD_DECODE_ERROR;
  960. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_COMPRESSION_SPECIFIED);
  961. goto f_err;
  962. }
  963. #ifndef OPENSSL_NO_TLSEXT
  964. /* TLS extensions*/
  965. if (s->version >= SSL3_VERSION)
  966. {
  967. if (!ssl_parse_clienthello_tlsext(s,&p,d,n, &al))
  968. {
  969. /* 'al' set by ssl_parse_clienthello_tlsext */
  970. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_PARSE_TLSEXT);
  971. goto f_err;
  972. }
  973. }
  974. if (ssl_check_clienthello_tlsext(s) <= 0) {
  975. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_CLIENTHELLO_TLSEXT);
  976. goto err;
  977. }
  978. /* Check if we want to use external pre-shared secret for this
  979. * handshake for not reused session only. We need to generate
  980. * server_random before calling tls_session_secret_cb in order to allow
  981. * SessionTicket processing to use it in key derivation. */
  982. {
  983. unsigned long Time;
  984. unsigned char *pos;
  985. Time=(unsigned long)time(NULL); /* Time */
  986. pos=s->s3->server_random;
  987. l2n(Time,pos);
  988. if (RAND_pseudo_bytes(pos,SSL3_RANDOM_SIZE-4) <= 0)
  989. {
  990. al=SSL_AD_INTERNAL_ERROR;
  991. goto f_err;
  992. }
  993. }
  994. if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb)
  995. {
  996. SSL_CIPHER *pref_cipher=NULL;
  997. s->session->master_key_length=sizeof(s->session->master_key);
  998. if(s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length,
  999. ciphers, &pref_cipher, s->tls_session_secret_cb_arg))
  1000. {
  1001. s->hit=1;
  1002. s->session->ciphers=ciphers;
  1003. s->session->verify_result=X509_V_OK;
  1004. ciphers=NULL;
  1005. /* check if some cipher was preferred by call back */
  1006. pref_cipher=pref_cipher ? pref_cipher : ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
  1007. if (pref_cipher == NULL)
  1008. {
  1009. al=SSL_AD_HANDSHAKE_FAILURE;
  1010. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
  1011. goto f_err;
  1012. }
  1013. s->session->cipher=pref_cipher;
  1014. if (s->cipher_list)
  1015. sk_SSL_CIPHER_free(s->cipher_list);
  1016. if (s->cipher_list_by_id)
  1017. sk_SSL_CIPHER_free(s->cipher_list_by_id);
  1018. s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
  1019. s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
  1020. }
  1021. }
  1022. #endif
  1023. /* Worst case, we will use the NULL compression, but if we have other
  1024. * options, we will now look for them. We have i-1 compression
  1025. * algorithms from the client, starting at q. */
  1026. s->s3->tmp.new_compression=NULL;
  1027. #ifndef OPENSSL_NO_COMP
  1028. /* This only happens if we have a cache hit */
  1029. if (s->session->compress_meth != 0)
  1030. {
  1031. int m, comp_id = s->session->compress_meth;
  1032. /* Perform sanity checks on resumed compression algorithm */
  1033. /* Can't disable compression */
  1034. if (s->options & SSL_OP_NO_COMPRESSION)
  1035. {
  1036. al=SSL_AD_INTERNAL_ERROR;
  1037. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_INCONSISTENT_COMPRESSION);
  1038. goto f_err;
  1039. }
  1040. /* Look for resumed compression method */
  1041. for (m = 0; m < sk_SSL_COMP_num(s->ctx->comp_methods); m++)
  1042. {
  1043. comp=sk_SSL_COMP_value(s->ctx->comp_methods,m);
  1044. if (comp_id == comp->id)
  1045. {
  1046. s->s3->tmp.new_compression=comp;
  1047. break;
  1048. }
  1049. }
  1050. if (s->s3->tmp.new_compression == NULL)
  1051. {
  1052. al=SSL_AD_INTERNAL_ERROR;
  1053. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_INVALID_COMPRESSION_ALGORITHM);
  1054. goto f_err;
  1055. }
  1056. /* Look for resumed method in compression list */
  1057. for (m = 0; m < i; m++)
  1058. {
  1059. if (q[m] == comp_id)
  1060. break;
  1061. }
  1062. if (m >= i)
  1063. {
  1064. al=SSL_AD_ILLEGAL_PARAMETER;
  1065. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_REQUIRED_COMPRESSSION_ALGORITHM_MISSING);
  1066. goto f_err;
  1067. }
  1068. }
  1069. else if (s->hit)
  1070. comp = NULL;
  1071. else if (!(s->options & SSL_OP_NO_COMPRESSION) && s->ctx->comp_methods)
  1072. { /* See if we have a match */
  1073. int m,nn,o,v,done=0;
  1074. nn=sk_SSL_COMP_num(s->ctx->comp_methods);
  1075. for (m=0; m<nn; m++)
  1076. {
  1077. comp=sk_SSL_COMP_value(s->ctx->comp_methods,m);
  1078. v=comp->id;
  1079. for (o=0; o<i; o++)
  1080. {
  1081. if (v == q[o])
  1082. {
  1083. done=1;
  1084. break;
  1085. }
  1086. }
  1087. if (done) break;
  1088. }
  1089. if (done)
  1090. s->s3->tmp.new_compression=comp;
  1091. else
  1092. comp=NULL;
  1093. }
  1094. #else
  1095. /* If compression is disabled we'd better not try to resume a session
  1096. * using compression.
  1097. */
  1098. if (s->session->compress_meth != 0)
  1099. {
  1100. al=SSL_AD_INTERNAL_ERROR;
  1101. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_INCONSISTENT_COMPRESSION);
  1102. goto f_err;
  1103. }
  1104. #endif
  1105. /* Given s->session->ciphers and SSL_get_ciphers, we must
  1106. * pick a cipher */
  1107. if (!s->hit)
  1108. {
  1109. #ifdef OPENSSL_NO_COMP
  1110. s->session->compress_meth=0;
  1111. #else
  1112. s->session->compress_meth=(comp == NULL)?0:comp->id;
  1113. #endif
  1114. if (s->session->ciphers != NULL)
  1115. sk_SSL_CIPHER_free(s->session->ciphers);
  1116. s->session->ciphers=ciphers;
  1117. if (ciphers == NULL)
  1118. {
  1119. al=SSL_AD_ILLEGAL_PARAMETER;
  1120. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_PASSED);
  1121. goto f_err;
  1122. }
  1123. ciphers=NULL;
  1124. c=ssl3_choose_cipher(s,s->session->ciphers,
  1125. SSL_get_ciphers(s));
  1126. if (c == NULL)
  1127. {
  1128. al=SSL_AD_HANDSHAKE_FAILURE;
  1129. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
  1130. goto f_err;
  1131. }
  1132. s->s3->tmp.new_cipher=c;
  1133. }
  1134. else
  1135. {
  1136. /* Session-id reuse */
  1137. #ifdef REUSE_CIPHER_BUG
  1138. STACK_OF(SSL_CIPHER) *sk;
  1139. SSL_CIPHER *nc=NULL;
  1140. SSL_CIPHER *ec=NULL;
  1141. if (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG)
  1142. {
  1143. sk=s->session->ciphers;
  1144. for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
  1145. {
  1146. c=sk_SSL_CIPHER_value(sk,i);
  1147. if (c->algorithm_enc & SSL_eNULL)
  1148. nc=c;
  1149. if (SSL_C_IS_EXPORT(c))
  1150. ec=c;
  1151. }
  1152. if (nc != NULL)
  1153. s->s3->tmp.new_cipher=nc;
  1154. else if (ec != NULL)
  1155. s->s3->tmp.new_cipher=ec;
  1156. else
  1157. s->s3->tmp.new_cipher=s->session->cipher;
  1158. }
  1159. else
  1160. #endif
  1161. s->s3->tmp.new_cipher=s->session->cipher;
  1162. }
  1163. if (!ssl3_digest_cached_records(s))
  1164. goto f_err;
  1165. /* we now have the following setup.
  1166. * client_random
  1167. * cipher_list - our prefered list of ciphers
  1168. * ciphers - the clients prefered list of ciphers
  1169. * compression - basically ignored right now
  1170. * ssl version is set - sslv3
  1171. * s->session - The ssl session has been setup.
  1172. * s->hit - session reuse flag
  1173. * s->tmp.new_cipher - the new cipher to use.
  1174. */
  1175. if (ret < 0) ret=1;
  1176. if (0)
  1177. {
  1178. f_err:
  1179. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1180. }
  1181. err:
  1182. if (ciphers != NULL) sk_SSL_CIPHER_free(ciphers);
  1183. return(ret);
  1184. }
  1185. int ssl3_send_server_hello(SSL *s)
  1186. {
  1187. unsigned char *buf;
  1188. unsigned char *p,*d;
  1189. int i,sl;
  1190. unsigned long l;
  1191. #ifdef OPENSSL_NO_TLSEXT
  1192. unsigned long Time;
  1193. #endif
  1194. if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
  1195. {
  1196. buf=(unsigned char *)s->init_buf->data;
  1197. #ifdef OPENSSL_NO_TLSEXT
  1198. p=s->s3->server_random;
  1199. /* Generate server_random if it was not needed previously */
  1200. Time=(unsigned long)time(NULL); /* Time */
  1201. l2n(Time,p);
  1202. if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0)
  1203. return -1;
  1204. #endif
  1205. /* Do the message type and length last */
  1206. d=p= &(buf[4]);
  1207. *(p++)=s->version>>8;
  1208. *(p++)=s->version&0xff;
  1209. /* Random stuff */
  1210. memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
  1211. p+=SSL3_RANDOM_SIZE;
  1212. /* now in theory we have 3 options to sending back the
  1213. * session id. If it is a re-use, we send back the
  1214. * old session-id, if it is a new session, we send
  1215. * back the new session-id or we send back a 0 length
  1216. * session-id if we want it to be single use.
  1217. * Currently I will not implement the '0' length session-id
  1218. * 12-Jan-98 - I'll now support the '0' length stuff.
  1219. *
  1220. * We also have an additional case where stateless session
  1221. * resumption is successful: we always send back the old
  1222. * session id. In this case s->hit is non zero: this can
  1223. * only happen if stateless session resumption is succesful
  1224. * if session caching is disabled so existing functionality
  1225. * is unaffected.
  1226. */
  1227. if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)
  1228. && !s->hit)
  1229. s->session->session_id_length=0;
  1230. sl=s->session->session_id_length;
  1231. if (sl > (int)sizeof(s->session->session_id))
  1232. {
  1233. SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
  1234. return -1;
  1235. }
  1236. *(p++)=sl;
  1237. memcpy(p,s->session->session_id,sl);
  1238. p+=sl;
  1239. /* put the cipher */
  1240. i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p);
  1241. p+=i;
  1242. /* put the compression method */
  1243. #ifdef OPENSSL_NO_COMP
  1244. *(p++)=0;
  1245. #else
  1246. if (s->s3->tmp.new_compression == NULL)
  1247. *(p++)=0;
  1248. else
  1249. *(p++)=s->s3->tmp.new_compression->id;
  1250. #endif
  1251. #ifndef OPENSSL_NO_TLSEXT
  1252. if (ssl_prepare_serverhello_tlsext(s) <= 0)
  1253. {
  1254. SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO,SSL_R_SERVERHELLO_TLSEXT);
  1255. return -1;
  1256. }
  1257. if ((p = ssl_add_serverhello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH)) == NULL)
  1258. {
  1259. SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO,ERR_R_INTERNAL_ERROR);
  1260. return -1;
  1261. }
  1262. #endif
  1263. /* do the header */
  1264. l=(p-d);
  1265. d=buf;
  1266. *(d++)=SSL3_MT_SERVER_HELLO;
  1267. l2n3(l,d);
  1268. s->state=SSL3_ST_SW_SRVR_HELLO_B;
  1269. /* number of bytes to write */
  1270. s->init_num=p-buf;
  1271. s->init_off=0;
  1272. }
  1273. /* SSL3_ST_SW_SRVR_HELLO_B */
  1274. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  1275. }
  1276. int ssl3_send_server_done(SSL *s)
  1277. {
  1278. unsigned char *p;
  1279. if (s->state == SSL3_ST_SW_SRVR_DONE_A)
  1280. {
  1281. p=(unsigned char *)s->init_buf->data;
  1282. /* do the header */
  1283. *(p++)=SSL3_MT_SERVER_DONE;
  1284. *(p++)=0;
  1285. *(p++)=0;
  1286. *(p++)=0;
  1287. s->state=SSL3_ST_SW_SRVR_DONE_B;
  1288. /* number of bytes to write */
  1289. s->init_num=4;
  1290. s->init_off=0;
  1291. }
  1292. /* SSL3_ST_SW_SRVR_DONE_B */
  1293. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  1294. }
  1295. int ssl3_send_server_key_exchange(SSL *s)
  1296. {
  1297. #ifndef OPENSSL_NO_RSA
  1298. unsigned char *q;
  1299. int j,num;
  1300. RSA *rsa;
  1301. unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
  1302. unsigned int u;
  1303. #endif
  1304. #ifndef OPENSSL_NO_DH
  1305. DH *dh=NULL,*dhp;
  1306. #endif
  1307. #ifndef OPENSSL_NO_ECDH
  1308. EC_KEY *ecdh=NULL, *ecdhp;
  1309. unsigned char *encodedPoint = NULL;
  1310. int encodedlen = 0;
  1311. int curve_id = 0;
  1312. BN_CTX *bn_ctx = NULL;
  1313. #endif
  1314. EVP_PKEY *pkey;
  1315. unsigned char *p,*d;
  1316. int al,i;
  1317. unsigned long type;
  1318. int n;
  1319. CERT *cert;
  1320. BIGNUM *r[4];
  1321. int nr[4],kn;
  1322. BUF_MEM *buf;
  1323. EVP_MD_CTX md_ctx;
  1324. EVP_MD_CTX_init(&md_ctx);
  1325. if (s->state == SSL3_ST_SW_KEY_EXCH_A)
  1326. {
  1327. type=s->s3->tmp.new_cipher->algorithm_mkey;
  1328. cert=s->cert;
  1329. buf=s->init_buf;
  1330. r[0]=r[1]=r[2]=r[3]=NULL;
  1331. n=0;
  1332. #ifndef OPENSSL_NO_RSA
  1333. if (type & SSL_kRSA)
  1334. {
  1335. rsa=cert->rsa_tmp;
  1336. if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL))
  1337. {
  1338. rsa=s->cert->rsa_tmp_cb(s,
  1339. SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
  1340. SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
  1341. if(rsa == NULL)
  1342. {
  1343. al=SSL_AD_HANDSHAKE_FAILURE;
  1344. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
  1345. goto f_err;
  1346. }
  1347. RSA_up_ref(rsa);
  1348. cert->rsa_tmp=rsa;
  1349. }
  1350. if (rsa == NULL)
  1351. {
  1352. al=SSL_AD_HANDSHAKE_FAILURE;
  1353. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_KEY);
  1354. goto f_err;
  1355. }
  1356. r[0]=rsa->n;
  1357. r[1]=rsa->e;
  1358. s->s3->tmp.use_rsa_tmp=1;
  1359. }
  1360. else
  1361. #endif
  1362. #ifndef OPENSSL_NO_DH
  1363. if (type & SSL_kEDH)
  1364. {
  1365. dhp=cert->dh_tmp;
  1366. if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
  1367. dhp=s->cert->dh_tmp_cb(s,
  1368. SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
  1369. SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
  1370. if (dhp == NULL)
  1371. {
  1372. al=SSL_AD_HANDSHAKE_FAILURE;
  1373. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
  1374. goto f_err;
  1375. }
  1376. if (s->s3->tmp.dh != NULL)
  1377. {
  1378. DH_free(dh);
  1379. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  1380. goto err;
  1381. }
  1382. if ((dh=DHparams_dup(dhp)) == NULL)
  1383. {
  1384. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
  1385. goto err;
  1386. }
  1387. s->s3->tmp.dh=dh;
  1388. if ((dhp->pub_key == NULL ||
  1389. dhp->priv_key == NULL ||
  1390. (s->options & SSL_OP_SINGLE_DH_USE)))
  1391. {
  1392. if(!DH_generate_key(dh))
  1393. {
  1394. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
  1395. ERR_R_DH_LIB);
  1396. goto err;
  1397. }
  1398. }
  1399. else
  1400. {
  1401. dh->pub_key=BN_dup(dhp->pub_key);
  1402. dh->priv_key=BN_dup(dhp->priv_key);
  1403. if ((dh->pub_key == NULL) ||
  1404. (dh->priv_key == NULL))
  1405. {
  1406. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
  1407. goto err;
  1408. }
  1409. }
  1410. r[0]=dh->p;
  1411. r[1]=dh->g;
  1412. r[2]=dh->pub_key;
  1413. }
  1414. else
  1415. #endif
  1416. #ifndef OPENSSL_NO_ECDH
  1417. if (type & SSL_kEECDH)
  1418. {
  1419. const EC_GROUP *group;
  1420. ecdhp=cert->ecdh_tmp;
  1421. if ((ecdhp == NULL) && (s->cert->ecdh_tmp_cb != NULL))
  1422. {
  1423. ecdhp=s->cert->ecdh_tmp_cb(s,
  1424. SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
  1425. SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
  1426. }
  1427. if (ecdhp == NULL)
  1428. {
  1429. al=SSL_AD_HANDSHAKE_FAILURE;
  1430. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_ECDH_KEY);
  1431. goto f_err;
  1432. }
  1433. if (s->s3->tmp.ecdh != NULL)
  1434. {
  1435. EC_KEY_free(s->s3->tmp.ecdh);
  1436. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  1437. goto err;
  1438. }
  1439. /* Duplicate the ECDH structure. */
  1440. if (ecdhp == NULL)
  1441. {
  1442. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
  1443. goto err;
  1444. }
  1445. if (!EC_KEY_up_ref(ecdhp))
  1446. {
  1447. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
  1448. goto err;
  1449. }
  1450. ecdh = ecdhp;
  1451. s->s3->tmp.ecdh=ecdh;
  1452. if ((EC_KEY_get0_public_key(ecdh) == NULL) ||
  1453. (EC_KEY_get0_private_key(ecdh) == NULL) ||
  1454. (s->options & SSL_OP_SINGLE_ECDH_USE))
  1455. {
  1456. if(!EC_KEY_generate_key(ecdh))
  1457. {
  1458. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
  1459. goto err;
  1460. }
  1461. }
  1462. if (((group = EC_KEY_get0_group(ecdh)) == NULL) ||
  1463. (EC_KEY_get0_public_key(ecdh) == NULL) ||
  1464. (EC_KEY_get0_private_key(ecdh) == NULL))
  1465. {
  1466. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
  1467. goto err;
  1468. }
  1469. if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
  1470. (EC_GROUP_get_degree(group) > 163))
  1471. {
  1472. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
  1473. goto err;
  1474. }
  1475. /* XXX: For now, we only support ephemeral ECDH
  1476. * keys over named (not generic) curves. For
  1477. * supported named curves, curve_id is non-zero.
  1478. */
  1479. if ((curve_id =
  1480. tls1_ec_nid2curve_id(EC_GROUP_get_curve_name(group)))
  1481. == 0)
  1482. {
  1483. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
  1484. goto err;
  1485. }
  1486. /* Encode the public key.
  1487. * First check the size of encoding and
  1488. * allocate memory accordingly.
  1489. */
  1490. encodedlen = EC_POINT_point2oct(group,
  1491. EC_KEY_get0_public_key(ecdh),
  1492. POINT_CONVERSION_UNCOMPRESSED,
  1493. NULL, 0, NULL);
  1494. encodedPoint = (unsigned char *)
  1495. OPENSSL_malloc(encodedlen*sizeof(unsigned char));
  1496. bn_ctx = BN_CTX_new();
  1497. if ((encodedPoint == NULL) || (bn_ctx == NULL))
  1498. {
  1499. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  1500. goto err;
  1501. }
  1502. encodedlen = EC_POINT_point2oct(group,
  1503. EC_KEY_get0_public_key(ecdh),
  1504. POINT_CONVERSION_UNCOMPRESSED,
  1505. encodedPoint, encodedlen, bn_ctx);
  1506. if (encodedlen == 0)
  1507. {
  1508. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
  1509. goto err;
  1510. }
  1511. BN_CTX_free(bn_ctx); bn_ctx=NULL;
  1512. /* XXX: For now, we only support named (not
  1513. * generic) curves in ECDH ephemeral key exchanges.
  1514. * In this situation, we need four additional bytes
  1515. * to encode the entire ServerECDHParams
  1516. * structure.
  1517. */
  1518. n = 4 + encodedlen;
  1519. /* We'll generate the serverKeyExchange message
  1520. * explicitly so we can set these to NULLs
  1521. */
  1522. r[0]=NULL;
  1523. r[1]=NULL;
  1524. r[2]=NULL;
  1525. r[3]=NULL;
  1526. }
  1527. else
  1528. #endif /* !OPENSSL_NO_ECDH */
  1529. #ifndef OPENSSL_NO_PSK
  1530. if (type & SSL_kPSK)
  1531. {
  1532. /* reserve size for record length and PSK identity hint*/
  1533. n+=2+strlen(s->ctx->psk_identity_hint);
  1534. }
  1535. else
  1536. #endif /* !OPENSSL_NO_PSK */
  1537. {
  1538. al=SSL_AD_HANDSHAKE_FAILURE;
  1539. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
  1540. goto f_err;
  1541. }
  1542. for (i=0; r[i] != NULL; i++)
  1543. {
  1544. nr[i]=BN_num_bytes(r[i]);
  1545. n+=2+nr[i];
  1546. }
  1547. if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
  1548. && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
  1549. {
  1550. if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher))
  1551. == NULL)
  1552. {
  1553. al=SSL_AD_DECODE_ERROR;
  1554. goto f_err;
  1555. }
  1556. kn=EVP_PKEY_size(pkey);
  1557. }
  1558. else
  1559. {
  1560. pkey=NULL;
  1561. kn=0;
  1562. }
  1563. if (!BUF_MEM_grow_clean(buf,n+4+kn))
  1564. {
  1565. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF);
  1566. goto err;
  1567. }
  1568. d=(unsigned char *)s->init_buf->data;
  1569. p= &(d[4]);
  1570. for (i=0; r[i] != NULL; i++)
  1571. {
  1572. s2n(nr[i],p);
  1573. BN_bn2bin(r[i],p);
  1574. p+=nr[i];
  1575. }
  1576. #ifndef OPENSSL_NO_ECDH
  1577. if (type & SSL_kEECDH)
  1578. {
  1579. /* XXX: For now, we only support named (not generic) curves.
  1580. * In this situation, the serverKeyExchange message has:
  1581. * [1 byte CurveType], [2 byte CurveName]
  1582. * [1 byte length of encoded point], followed by
  1583. * the actual encoded point itself
  1584. */
  1585. *p = NAMED_CURVE_TYPE;
  1586. p += 1;
  1587. *p = 0;
  1588. p += 1;
  1589. *p = curve_id;
  1590. p += 1;
  1591. *p = encodedlen;
  1592. p += 1;
  1593. memcpy((unsigned char*)p,
  1594. (unsigned char *)encodedPoint,
  1595. encodedlen);
  1596. OPENSSL_free(encodedPoint);
  1597. p += encodedlen;
  1598. }
  1599. #endif
  1600. #ifndef OPENSSL_NO_PSK
  1601. if (type & SSL_kPSK)
  1602. {
  1603. /* copy PSK identity hint */
  1604. s2n(strlen(s->ctx->psk_identity_hint), p);
  1605. strncpy((char *)p, s->ctx->psk_identity_hint, strlen(s->ctx->psk_identity_hint));
  1606. p+=strlen(s->ctx->psk_identity_hint);
  1607. }
  1608. #endif
  1609. /* not anonymous */
  1610. if (pkey != NULL)
  1611. {
  1612. /* n is the length of the params, they start at &(d[4])
  1613. * and p points to the space at the end. */
  1614. #ifndef OPENSSL_NO_RSA
  1615. if (pkey->type == EVP_PKEY_RSA)
  1616. {
  1617. q=md_buf;
  1618. j=0;
  1619. for (num=2; num > 0; num--)
  1620. {
  1621. EVP_DigestInit_ex(&md_ctx,(num == 2)
  1622. ?s->ctx->md5:s->ctx->sha1, NULL);
  1623. EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1624. EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1625. EVP_DigestUpdate(&md_ctx,&(d[4]),n);
  1626. EVP_DigestFinal_ex(&md_ctx,q,
  1627. (unsigned int *)&i);
  1628. q+=i;
  1629. j+=i;
  1630. }
  1631. if (RSA_sign(NID_md5_sha1, md_buf, j,
  1632. &(p[2]), &u, pkey->pkey.rsa) <= 0)
  1633. {
  1634. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_RSA);
  1635. goto err;
  1636. }
  1637. s2n(u,p);
  1638. n+=u+2;
  1639. }
  1640. else
  1641. #endif
  1642. #if !defined(OPENSSL_NO_DSA)
  1643. if (pkey->type == EVP_PKEY_DSA)
  1644. {
  1645. /* lets do DSS */
  1646. EVP_SignInit_ex(&md_ctx,EVP_dss1(), NULL);
  1647. EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1648. EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1649. EVP_SignUpdate(&md_ctx,&(d[4]),n);
  1650. if (!EVP_SignFinal(&md_ctx,&(p[2]),
  1651. (unsigned int *)&i,pkey))
  1652. {
  1653. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_DSA);
  1654. goto err;
  1655. }
  1656. s2n(i,p);
  1657. n+=i+2;
  1658. }
  1659. else
  1660. #endif
  1661. #if !defined(OPENSSL_NO_ECDSA)
  1662. if (pkey->type == EVP_PKEY_EC)
  1663. {
  1664. /* let's do ECDSA */
  1665. EVP_SignInit_ex(&md_ctx,EVP_ecdsa(), NULL);
  1666. EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1667. EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1668. EVP_SignUpdate(&md_ctx,&(d[4]),n);
  1669. if (!EVP_SignFinal(&md_ctx,&(p[2]),
  1670. (unsigned int *)&i,pkey))
  1671. {
  1672. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_ECDSA);
  1673. goto err;
  1674. }
  1675. s2n(i,p);
  1676. n+=i+2;
  1677. }
  1678. else
  1679. #endif
  1680. {
  1681. /* Is this error check actually needed? */
  1682. al=SSL_AD_HANDSHAKE_FAILURE;
  1683. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_PKEY_TYPE);
  1684. goto f_err;
  1685. }
  1686. }
  1687. *(d++)=SSL3_MT_SERVER_KEY_EXCHANGE;
  1688. l2n3(n,d);
  1689. /* we should now have things packed up, so lets send
  1690. * it off */
  1691. s->init_num=n+4;
  1692. s->init_off=0;
  1693. }
  1694. s->state = SSL3_ST_SW_KEY_EXCH_B;
  1695. EVP_MD_CTX_cleanup(&md_ctx);
  1696. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  1697. f_err:
  1698. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1699. err:
  1700. #ifndef OPENSSL_NO_ECDH
  1701. if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
  1702. BN_CTX_free(bn_ctx);
  1703. #endif
  1704. EVP_MD_CTX_cleanup(&md_ctx);
  1705. return(-1);
  1706. }
  1707. int ssl3_send_certificate_request(SSL *s)
  1708. {
  1709. unsigned char *p,*d;
  1710. int i,j,nl,off,n;
  1711. STACK_OF(X509_NAME) *sk=NULL;
  1712. X509_NAME *name;
  1713. BUF_MEM *buf;
  1714. if (s->state == SSL3_ST_SW_CERT_REQ_A)
  1715. {
  1716. buf=s->init_buf;
  1717. d=p=(unsigned char *)&(buf->data[4]);
  1718. /* get the list of acceptable cert types */
  1719. p++;
  1720. n=ssl3_get_req_cert_type(s,p);
  1721. d[0]=n;
  1722. p+=n;
  1723. n++;
  1724. off=n;
  1725. p+=2;
  1726. n+=2;
  1727. sk=SSL_get_client_CA_list(s);
  1728. nl=0;
  1729. if (sk != NULL)
  1730. {
  1731. for (i=0; i<sk_X509_NAME_num(sk); i++)
  1732. {
  1733. name=sk_X509_NAME_value(sk,i);
  1734. j=i2d_X509_NAME(name,NULL);
  1735. if (!BUF_MEM_grow_clean(buf,4+n+j+2))
  1736. {
  1737. SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,ERR_R_BUF_LIB);
  1738. goto err;
  1739. }
  1740. p=(unsigned char *)&(buf->data[4+n]);
  1741. if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
  1742. {
  1743. s2n(j,p);
  1744. i2d_X509_NAME(name,&p);
  1745. n+=2+j;
  1746. nl+=2+j;
  1747. }
  1748. else
  1749. {
  1750. d=p;
  1751. i2d_X509_NAME(name,&p);
  1752. j-=2; s2n(j,d); j+=2;
  1753. n+=j;
  1754. nl+=j;
  1755. }
  1756. }
  1757. }
  1758. /* else no CA names */
  1759. p=(unsigned char *)&(buf->data[4+off]);
  1760. s2n(nl,p);
  1761. d=(unsigned char *)buf->data;
  1762. *(d++)=SSL3_MT_CERTIFICATE_REQUEST;
  1763. l2n3(n,d);
  1764. /* we should now have things packed up, so lets send
  1765. * it off */
  1766. s->init_num=n+4;
  1767. s->init_off=0;
  1768. #ifdef NETSCAPE_HANG_BUG
  1769. p=(unsigned char *)s->init_buf->data + s->init_num;
  1770. /* do the header */
  1771. *(p++)=SSL3_MT_SERVER_DONE;
  1772. *(p++)=0;
  1773. *(p++)=0;
  1774. *(p++)=0;
  1775. s->init_num += 4;
  1776. #endif
  1777. s->state = SSL3_ST_SW_CERT_REQ_B;
  1778. }
  1779. /* SSL3_ST_SW_CERT_REQ_B */
  1780. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  1781. err:
  1782. return(-1);
  1783. }
  1784. int ssl3_get_client_key_exchange(SSL *s)
  1785. {
  1786. int i,al,ok;
  1787. long n;
  1788. unsigned long alg_k;
  1789. unsigned char *p;
  1790. #ifndef OPENSSL_NO_RSA
  1791. RSA *rsa=NULL;
  1792. EVP_PKEY *pkey=NULL;
  1793. #endif
  1794. #ifndef OPENSSL_NO_DH
  1795. BIGNUM *pub=NULL;
  1796. DH *dh_srvr;
  1797. #endif
  1798. #ifndef OPENSSL_NO_KRB5
  1799. KSSL_ERR kssl_err;
  1800. #endif /* OPENSSL_NO_KRB5 */
  1801. #ifndef OPENSSL_NO_ECDH
  1802. EC_KEY *srvr_ecdh = NULL;
  1803. EVP_PKEY *clnt_pub_pkey = NULL;
  1804. EC_POINT *clnt_ecpoint = NULL;
  1805. BN_CTX *bn_ctx = NULL;
  1806. #endif
  1807. n=s->method->ssl_get_message(s,
  1808. SSL3_ST_SR_KEY_EXCH_A,
  1809. SSL3_ST_SR_KEY_EXCH_B,
  1810. SSL3_MT_CLIENT_KEY_EXCHANGE,
  1811. 2048, /* ??? */
  1812. &ok);
  1813. if (!ok) return((int)n);
  1814. p=(unsigned char *)s->init_msg;
  1815. alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
  1816. #ifndef OPENSSL_NO_RSA
  1817. if (alg_k & SSL_kRSA)
  1818. {
  1819. /* FIX THIS UP EAY EAY EAY EAY */
  1820. if (s->s3->tmp.use_rsa_tmp)
  1821. {
  1822. if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))
  1823. rsa=s->cert->rsa_tmp;
  1824. /* Don't do a callback because rsa_tmp should
  1825. * be sent already */
  1826. if (rsa == NULL)
  1827. {
  1828. al=SSL_AD_HANDSHAKE_FAILURE;
  1829. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_PKEY);
  1830. goto f_err;
  1831. }
  1832. }
  1833. else
  1834. {
  1835. pkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
  1836. if ( (pkey == NULL) ||
  1837. (pkey->type != EVP_PKEY_RSA) ||
  1838. (pkey->pkey.rsa == NULL))
  1839. {
  1840. al=SSL_AD_HANDSHAKE_FAILURE;
  1841. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_RSA_CERTIFICATE);
  1842. goto f_err;
  1843. }
  1844. rsa=pkey->pkey.rsa;
  1845. }
  1846. /* TLS and [incidentally] DTLS{0xFEFF} */
  1847. if (s->version > SSL3_VERSION && s->version != DTLS1_BAD_VER)
  1848. {
  1849. n2s(p,i);
  1850. if (n != i+2)
  1851. {
  1852. if (!(s->options & SSL_OP_TLS_D5_BUG))
  1853. {
  1854. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
  1855. goto err;
  1856. }
  1857. else
  1858. p-=2;
  1859. }
  1860. else
  1861. n=i;
  1862. }
  1863. i=RSA_private_decrypt((int)n,p,p,rsa,RSA_PKCS1_PADDING);
  1864. al = -1;
  1865. if (i != SSL_MAX_MASTER_KEY_LENGTH)
  1866. {
  1867. al=SSL_AD_DECODE_ERROR;
  1868. /* SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT); */
  1869. }
  1870. if ((al == -1) && !((p[0] == (s->client_version>>8)) && (p[1] == (s->client_version & 0xff))))
  1871. {
  1872. /* The premaster secret must contain the same version number as the
  1873. * ClientHello to detect version rollback attacks (strangely, the
  1874. * protocol does not offer such protection for DH ciphersuites).
  1875. * However, buggy clients exist that send the negotiated protocol
  1876. * version instead if the server does not support the requested
  1877. * protocol version.
  1878. * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients. */
  1879. if (!((s->options & SSL_OP_TLS_ROLLBACK_BUG) &&
  1880. (p[0] == (s->version>>8)) && (p[1] == (s->version & 0xff))))
  1881. {
  1882. al=SSL_AD_DECODE_ERROR;
  1883. /* SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_PROTOCOL_VERSION_NUMBER); */
  1884. /* The Klima-Pokorny-Rosa extension of Bleichenbacher's attack
  1885. * (http://eprint.iacr.org/2003/052/) exploits the version
  1886. * number check as a "bad version oracle" -- an alert would
  1887. * reveal that the plaintext corresponding to some ciphertext
  1888. * made up by the adversary is properly formatted except
  1889. * that the version number is wrong. To avoid such attacks,
  1890. * we should treat this just like any other decryption error. */
  1891. }
  1892. }
  1893. if (al != -1)
  1894. {
  1895. /* Some decryption failure -- use random value instead as countermeasure
  1896. * against Bleichenbacher's attack on PKCS #1 v1.5 RSA padding
  1897. * (see RFC 2246, section 7.4.7.1). */
  1898. ERR_clear_error();
  1899. i = SSL_MAX_MASTER_KEY_LENGTH;
  1900. p[0] = s->client_version >> 8;
  1901. p[1] = s->client_version & 0xff;
  1902. if (RAND_pseudo_bytes(p+2, i-2) <= 0) /* should be RAND_bytes, but we cannot work around a failure */
  1903. goto err;
  1904. }
  1905. s->session->master_key_length=
  1906. s->method->ssl3_enc->generate_master_secret(s,
  1907. s->session->master_key,
  1908. p,i);
  1909. OPENSSL_cleanse(p,i);
  1910. }
  1911. else
  1912. #endif
  1913. #ifndef OPENSSL_NO_DH
  1914. if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
  1915. {
  1916. n2s(p,i);
  1917. if (n != i+2)
  1918. {
  1919. if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG))
  1920. {
  1921. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
  1922. goto err;
  1923. }
  1924. else
  1925. {
  1926. p-=2;
  1927. i=(int)n;
  1928. }
  1929. }
  1930. if (n == 0L) /* the parameters are in the cert */
  1931. {
  1932. al=SSL_AD_HANDSHAKE_FAILURE;
  1933. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_DECODE_DH_CERTS);
  1934. goto f_err;
  1935. }
  1936. else
  1937. {
  1938. if (s->s3->tmp.dh == NULL)
  1939. {
  1940. al=SSL_AD_HANDSHAKE_FAILURE;
  1941. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
  1942. goto f_err;
  1943. }
  1944. else
  1945. dh_srvr=s->s3->tmp.dh;
  1946. }
  1947. pub=BN_bin2bn(p,i,NULL);
  1948. if (pub == NULL)
  1949. {
  1950. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BN_LIB);
  1951. goto err;
  1952. }
  1953. i=DH_compute_key(p,pub,dh_srvr);
  1954. if (i <= 0)
  1955. {
  1956. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
  1957. goto err;
  1958. }
  1959. DH_free(s->s3->tmp.dh);
  1960. s->s3->tmp.dh=NULL;
  1961. BN_clear_free(pub);
  1962. pub=NULL;
  1963. s->session->master_key_length=
  1964. s->method->ssl3_enc->generate_master_secret(s,
  1965. s->session->master_key,p,i);
  1966. OPENSSL_cleanse(p,i);
  1967. }
  1968. else
  1969. #endif
  1970. #ifndef OPENSSL_NO_KRB5
  1971. if (alg_k & SSL_kKRB5)
  1972. {
  1973. krb5_error_code krb5rc;
  1974. krb5_data enc_ticket;
  1975. krb5_data authenticator;
  1976. krb5_data enc_pms;
  1977. KSSL_CTX *kssl_ctx = s->kssl_ctx;
  1978. EVP_CIPHER_CTX ciph_ctx;
  1979. const EVP_CIPHER *enc = NULL;
  1980. unsigned char iv[EVP_MAX_IV_LENGTH];
  1981. unsigned char pms[SSL_MAX_MASTER_KEY_LENGTH
  1982. + EVP_MAX_BLOCK_LENGTH];
  1983. int padl, outl;
  1984. krb5_timestamp authtime = 0;
  1985. krb5_ticket_times ttimes;
  1986. EVP_CIPHER_CTX_init(&ciph_ctx);
  1987. if (!kssl_ctx) kssl_ctx = kssl_ctx_new();
  1988. n2s(p,i);
  1989. enc_ticket.length = i;
  1990. if (n < (long)(enc_ticket.length + 6))
  1991. {
  1992. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1993. SSL_R_DATA_LENGTH_TOO_LONG);
  1994. goto err;
  1995. }
  1996. enc_ticket.data = (char *)p;
  1997. p+=enc_ticket.length;
  1998. n2s(p,i);
  1999. authenticator.length = i;
  2000. if (n < (long)(enc_ticket.length + authenticator.length + 6))
  2001. {
  2002. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2003. SSL_R_DATA_LENGTH_TOO_LONG);
  2004. goto err;
  2005. }
  2006. authenticator.data = (char *)p;
  2007. p+=authenticator.length;
  2008. n2s(p,i);
  2009. enc_pms.length = i;
  2010. enc_pms.data = (char *)p;
  2011. p+=enc_pms.length;
  2012. /* Note that the length is checked again below,
  2013. ** after decryption
  2014. */
  2015. if(enc_pms.length > sizeof pms)
  2016. {
  2017. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2018. SSL_R_DATA_LENGTH_TOO_LONG);
  2019. goto err;
  2020. }
  2021. if (n != (long)(enc_ticket.length + authenticator.length +
  2022. enc_pms.length + 6))
  2023. {
  2024. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2025. SSL_R_DATA_LENGTH_TOO_LONG);
  2026. goto err;
  2027. }
  2028. if ((krb5rc = kssl_sget_tkt(kssl_ctx, &enc_ticket, &ttimes,
  2029. &kssl_err)) != 0)
  2030. {
  2031. #ifdef KSSL_DEBUG
  2032. printf("kssl_sget_tkt rtn %d [%d]\n",
  2033. krb5rc, kssl_err.reason);
  2034. if (kssl_err.text)
  2035. printf("kssl_err text= %s\n", kssl_err.text);
  2036. #endif /* KSSL_DEBUG */
  2037. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2038. kssl_err.reason);
  2039. goto err;
  2040. }
  2041. /* Note: no authenticator is not considered an error,
  2042. ** but will return authtime == 0.
  2043. */
  2044. if ((krb5rc = kssl_check_authent(kssl_ctx, &authenticator,
  2045. &authtime, &kssl_err)) != 0)
  2046. {
  2047. #ifdef KSSL_DEBUG
  2048. printf("kssl_check_authent rtn %d [%d]\n",
  2049. krb5rc, kssl_err.reason);
  2050. if (kssl_err.text)
  2051. printf("kssl_err text= %s\n", kssl_err.text);
  2052. #endif /* KSSL_DEBUG */
  2053. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2054. kssl_err.reason);
  2055. goto err;
  2056. }
  2057. if ((krb5rc = kssl_validate_times(authtime, &ttimes)) != 0)
  2058. {
  2059. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, krb5rc);
  2060. goto err;
  2061. }
  2062. #ifdef KSSL_DEBUG
  2063. kssl_ctx_show(kssl_ctx);
  2064. #endif /* KSSL_DEBUG */
  2065. enc = kssl_map_enc(kssl_ctx->enctype);
  2066. if (enc == NULL)
  2067. goto err;
  2068. memset(iv, 0, sizeof iv); /* per RFC 1510 */
  2069. if (!EVP_DecryptInit_ex(&ciph_ctx,enc,NULL,kssl_ctx->key,iv))
  2070. {
  2071. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2072. SSL_R_DECRYPTION_FAILED);
  2073. goto err;
  2074. }
  2075. if (!EVP_DecryptUpdate(&ciph_ctx, pms,&outl,
  2076. (unsigned char *)enc_pms.data, enc_pms.length))
  2077. {
  2078. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2079. SSL_R_DECRYPTION_FAILED);
  2080. goto err;
  2081. }
  2082. if (outl > SSL_MAX_MASTER_KEY_LENGTH)
  2083. {
  2084. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2085. SSL_R_DATA_LENGTH_TOO_LONG);
  2086. goto err;
  2087. }
  2088. if (!EVP_DecryptFinal_ex(&ciph_ctx,&(pms[outl]),&padl))
  2089. {
  2090. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2091. SSL_R_DECRYPTION_FAILED);
  2092. goto err;
  2093. }
  2094. outl += padl;
  2095. if (outl > SSL_MAX_MASTER_KEY_LENGTH)
  2096. {
  2097. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2098. SSL_R_DATA_LENGTH_TOO_LONG);
  2099. goto err;
  2100. }
  2101. if (!((pms[0] == (s->client_version>>8)) && (pms[1] == (s->client_version & 0xff))))
  2102. {
  2103. /* The premaster secret must contain the same version number as the
  2104. * ClientHello to detect version rollback attacks (strangely, the
  2105. * protocol does not offer such protection for DH ciphersuites).
  2106. * However, buggy clients exist that send random bytes instead of
  2107. * the protocol version.
  2108. * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients.
  2109. * (Perhaps we should have a separate BUG value for the Kerberos cipher)
  2110. */
  2111. if (!(s->options & SSL_OP_TLS_ROLLBACK_BUG))
  2112. {
  2113. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2114. SSL_AD_DECODE_ERROR);
  2115. goto err;
  2116. }
  2117. }
  2118. EVP_CIPHER_CTX_cleanup(&ciph_ctx);
  2119. s->session->master_key_length=
  2120. s->method->ssl3_enc->generate_master_secret(s,
  2121. s->session->master_key, pms, outl);
  2122. if (kssl_ctx->client_princ)
  2123. {
  2124. size_t len = strlen(kssl_ctx->client_princ);
  2125. if ( len < SSL_MAX_KRB5_PRINCIPAL_LENGTH )
  2126. {
  2127. s->session->krb5_client_princ_len = len;
  2128. memcpy(s->session->krb5_client_princ,kssl_ctx->client_princ,len);
  2129. }
  2130. }
  2131. /* Was doing kssl_ctx_free() here,
  2132. ** but it caused problems for apache.
  2133. ** kssl_ctx = kssl_ctx_free(kssl_ctx);
  2134. ** if (s->kssl_ctx) s->kssl_ctx = NULL;
  2135. */
  2136. }
  2137. else
  2138. #endif /* OPENSSL_NO_KRB5 */
  2139. #ifndef OPENSSL_NO_ECDH
  2140. if (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe))
  2141. {
  2142. int ret = 1;
  2143. int field_size = 0;
  2144. const EC_KEY *tkey;
  2145. const EC_GROUP *group;
  2146. const BIGNUM *priv_key;
  2147. /* initialize structures for server's ECDH key pair */
  2148. if ((srvr_ecdh = EC_KEY_new()) == NULL)
  2149. {
  2150. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2151. ERR_R_MALLOC_FAILURE);
  2152. goto err;
  2153. }
  2154. /* Let's get server private key and group information */
  2155. if (alg_k & (SSL_kECDHr|SSL_kECDHe))
  2156. {
  2157. /* use the certificate */
  2158. tkey = s->cert->pkeys[SSL_PKEY_ECC].privatekey->pkey.ec;
  2159. }
  2160. else
  2161. {
  2162. /* use the ephermeral values we saved when
  2163. * generating the ServerKeyExchange msg.
  2164. */
  2165. tkey = s->s3->tmp.ecdh;
  2166. }
  2167. group = EC_KEY_get0_group(tkey);
  2168. priv_key = EC_KEY_get0_private_key(tkey);
  2169. if (!EC_KEY_set_group(srvr_ecdh, group) ||
  2170. !EC_KEY_set_private_key(srvr_ecdh, priv_key))
  2171. {
  2172. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2173. ERR_R_EC_LIB);
  2174. goto err;
  2175. }
  2176. /* Let's get client's public key */
  2177. if ((clnt_ecpoint = EC_POINT_new(group)) == NULL)
  2178. {
  2179. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2180. ERR_R_MALLOC_FAILURE);
  2181. goto err;
  2182. }
  2183. if (n == 0L)
  2184. {
  2185. /* Client Publickey was in Client Certificate */
  2186. if (alg_k & SSL_kEECDH)
  2187. {
  2188. al=SSL_AD_HANDSHAKE_FAILURE;
  2189. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_ECDH_KEY);
  2190. goto f_err;
  2191. }
  2192. if (((clnt_pub_pkey=X509_get_pubkey(s->session->peer))
  2193. == NULL) ||
  2194. (clnt_pub_pkey->type != EVP_PKEY_EC))
  2195. {
  2196. /* XXX: For now, we do not support client
  2197. * authentication using ECDH certificates
  2198. * so this branch (n == 0L) of the code is
  2199. * never executed. When that support is
  2200. * added, we ought to ensure the key
  2201. * received in the certificate is
  2202. * authorized for key agreement.
  2203. * ECDH_compute_key implicitly checks that
  2204. * the two ECDH shares are for the same
  2205. * group.
  2206. */
  2207. al=SSL_AD_HANDSHAKE_FAILURE;
  2208. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2209. SSL_R_UNABLE_TO_DECODE_ECDH_CERTS);
  2210. goto f_err;
  2211. }
  2212. if (EC_POINT_copy(clnt_ecpoint,
  2213. EC_KEY_get0_public_key(clnt_pub_pkey->pkey.ec)) == 0)
  2214. {
  2215. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2216. ERR_R_EC_LIB);
  2217. goto err;
  2218. }
  2219. ret = 2; /* Skip certificate verify processing */
  2220. }
  2221. else
  2222. {
  2223. /* Get client's public key from encoded point
  2224. * in the ClientKeyExchange message.
  2225. */
  2226. if ((bn_ctx = BN_CTX_new()) == NULL)
  2227. {
  2228. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2229. ERR_R_MALLOC_FAILURE);
  2230. goto err;
  2231. }
  2232. /* Get encoded point length */
  2233. i = *p;
  2234. p += 1;
  2235. if (EC_POINT_oct2point(group,
  2236. clnt_ecpoint, p, i, bn_ctx) == 0)
  2237. {
  2238. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2239. ERR_R_EC_LIB);
  2240. goto err;
  2241. }
  2242. /* p is pointing to somewhere in the buffer
  2243. * currently, so set it to the start
  2244. */
  2245. p=(unsigned char *)s->init_buf->data;
  2246. }
  2247. /* Compute the shared pre-master secret */
  2248. field_size = EC_GROUP_get_degree(group);
  2249. if (field_size <= 0)
  2250. {
  2251. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2252. ERR_R_ECDH_LIB);
  2253. goto err;
  2254. }
  2255. i = ECDH_compute_key(p, (field_size+7)/8, clnt_ecpoint, srvr_ecdh, NULL);
  2256. if (i <= 0)
  2257. {
  2258. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2259. ERR_R_ECDH_LIB);
  2260. goto err;
  2261. }
  2262. EVP_PKEY_free(clnt_pub_pkey);
  2263. EC_POINT_free(clnt_ecpoint);
  2264. EC_KEY_free(srvr_ecdh);
  2265. BN_CTX_free(bn_ctx);
  2266. EC_KEY_free(s->s3->tmp.ecdh);
  2267. s->s3->tmp.ecdh = NULL;
  2268. /* Compute the master secret */
  2269. s->session->master_key_length = s->method->ssl3_enc-> \
  2270. generate_master_secret(s, s->session->master_key, p, i);
  2271. OPENSSL_cleanse(p, i);
  2272. return (ret);
  2273. }
  2274. else
  2275. #endif
  2276. #ifndef OPENSSL_NO_PSK
  2277. if (alg_k & SSL_kPSK)
  2278. {
  2279. unsigned char *t = NULL;
  2280. unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN*2+4];
  2281. unsigned int pre_ms_len = 0, psk_len = 0;
  2282. int psk_err = 1;
  2283. char tmp_id[PSK_MAX_IDENTITY_LEN+1];
  2284. al=SSL_AD_HANDSHAKE_FAILURE;
  2285. n2s(p,i);
  2286. if (n != i+2)
  2287. {
  2288. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2289. SSL_R_LENGTH_MISMATCH);
  2290. goto psk_err;
  2291. }
  2292. if (i > PSK_MAX_IDENTITY_LEN)
  2293. {
  2294. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2295. SSL_R_DATA_LENGTH_TOO_LONG);
  2296. goto psk_err;
  2297. }
  2298. if (s->psk_server_callback == NULL)
  2299. {
  2300. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2301. SSL_R_PSK_NO_SERVER_CB);
  2302. goto psk_err;
  2303. }
  2304. /* Create guaranteed NULL-terminated identity
  2305. * string for the callback */
  2306. memcpy(tmp_id, p, i);
  2307. memset(tmp_id+i, 0, PSK_MAX_IDENTITY_LEN+1-i);
  2308. psk_len = s->psk_server_callback(s, tmp_id,
  2309. psk_or_pre_ms, sizeof(psk_or_pre_ms));
  2310. OPENSSL_cleanse(tmp_id, PSK_MAX_IDENTITY_LEN+1);
  2311. if (psk_len > PSK_MAX_PSK_LEN)
  2312. {
  2313. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2314. ERR_R_INTERNAL_ERROR);
  2315. goto psk_err;
  2316. }
  2317. else if (psk_len == 0)
  2318. {
  2319. /* PSK related to the given identity not found */
  2320. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2321. SSL_R_PSK_IDENTITY_NOT_FOUND);
  2322. al=SSL_AD_UNKNOWN_PSK_IDENTITY;
  2323. goto psk_err;
  2324. }
  2325. /* create PSK pre_master_secret */
  2326. pre_ms_len=2+psk_len+2+psk_len;
  2327. t = psk_or_pre_ms;
  2328. memmove(psk_or_pre_ms+psk_len+4, psk_or_pre_ms, psk_len);
  2329. s2n(psk_len, t);
  2330. memset(t, 0, psk_len);
  2331. t+=psk_len;
  2332. s2n(psk_len, t);
  2333. if (s->session->psk_identity != NULL)
  2334. OPENSSL_free(s->session->psk_identity);
  2335. s->session->psk_identity = BUF_strdup((char *)p);
  2336. if (s->session->psk_identity == NULL)
  2337. {
  2338. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2339. ERR_R_MALLOC_FAILURE);
  2340. goto psk_err;
  2341. }
  2342. if (s->session->psk_identity_hint != NULL)
  2343. OPENSSL_free(s->session->psk_identity_hint);
  2344. s->session->psk_identity_hint = BUF_strdup(s->ctx->psk_identity_hint);
  2345. if (s->ctx->psk_identity_hint != NULL &&
  2346. s->session->psk_identity_hint == NULL)
  2347. {
  2348. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2349. ERR_R_MALLOC_FAILURE);
  2350. goto psk_err;
  2351. }
  2352. s->session->master_key_length=
  2353. s->method->ssl3_enc->generate_master_secret(s,
  2354. s->session->master_key, psk_or_pre_ms, pre_ms_len);
  2355. psk_err = 0;
  2356. psk_err:
  2357. OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
  2358. if (psk_err != 0)
  2359. goto f_err;
  2360. }
  2361. else
  2362. #endif
  2363. if (alg_k & SSL_kGOST)
  2364. {
  2365. int ret = 0;
  2366. EVP_PKEY_CTX *pkey_ctx;
  2367. EVP_PKEY *client_pub_pkey = NULL;
  2368. unsigned char premaster_secret[32], *start;
  2369. size_t outlen=32, inlen;
  2370. /* Get our certificate private key*/
  2371. pkey_ctx = EVP_PKEY_CTX_new(s->cert->key->privatekey,NULL);
  2372. EVP_PKEY_decrypt_init(pkey_ctx);
  2373. /* If client certificate is present and is of the same type, maybe
  2374. * use it for key exchange. Don't mind errors from
  2375. * EVP_PKEY_derive_set_peer, because it is completely valid to use
  2376. * a client certificate for authorization only. */
  2377. client_pub_pkey = X509_get_pubkey(s->session->peer);
  2378. if (client_pub_pkey)
  2379. {
  2380. if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)
  2381. ERR_clear_error();
  2382. }
  2383. /* Decrypt session key */
  2384. if ((*p!=( V_ASN1_SEQUENCE| V_ASN1_CONSTRUCTED)))
  2385. {
  2386. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DECRYPTION_FAILED);
  2387. goto gerr;
  2388. }
  2389. if (p[1] == 0x81)
  2390. {
  2391. start = p+3;
  2392. inlen = p[2];
  2393. }
  2394. else if (p[1] < 0x80)
  2395. {
  2396. start = p+2;
  2397. inlen = p[1];
  2398. }
  2399. else
  2400. {
  2401. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DECRYPTION_FAILED);
  2402. goto gerr;
  2403. }
  2404. if (EVP_PKEY_decrypt(pkey_ctx,premaster_secret,&outlen,start,inlen) <=0)
  2405. {
  2406. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DECRYPTION_FAILED);
  2407. goto gerr;
  2408. }
  2409. /* Generate master secret */
  2410. s->session->master_key_length=
  2411. s->method->ssl3_enc->generate_master_secret(s,
  2412. s->session->master_key,premaster_secret,32);
  2413. /* Check if pubkey from client certificate was used */
  2414. if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0)
  2415. ret = 2;
  2416. else
  2417. ret = 1;
  2418. gerr:
  2419. EVP_PKEY_free(client_pub_pkey);
  2420. EVP_PKEY_CTX_free(pkey_ctx);
  2421. if (ret)
  2422. return ret;
  2423. else
  2424. goto err;
  2425. }
  2426. else
  2427. {
  2428. al=SSL_AD_HANDSHAKE_FAILURE;
  2429. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  2430. SSL_R_UNKNOWN_CIPHER_TYPE);
  2431. goto f_err;
  2432. }
  2433. return(1);
  2434. f_err:
  2435. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  2436. #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_ECDH)
  2437. err:
  2438. #endif
  2439. #ifndef OPENSSL_NO_ECDH
  2440. EVP_PKEY_free(clnt_pub_pkey);
  2441. EC_POINT_free(clnt_ecpoint);
  2442. if (srvr_ecdh != NULL)
  2443. EC_KEY_free(srvr_ecdh);
  2444. BN_CTX_free(bn_ctx);
  2445. #endif
  2446. return(-1);
  2447. }
  2448. int ssl3_get_cert_verify(SSL *s)
  2449. {
  2450. EVP_PKEY *pkey=NULL;
  2451. unsigned char *p;
  2452. int al,ok,ret=0;
  2453. long n;
  2454. int type=0,i,j;
  2455. X509 *peer;
  2456. n=s->method->ssl_get_message(s,
  2457. SSL3_ST_SR_CERT_VRFY_A,
  2458. SSL3_ST_SR_CERT_VRFY_B,
  2459. -1,
  2460. 514, /* 514? */
  2461. &ok);
  2462. if (!ok) return((int)n);
  2463. if (s->session->peer != NULL)
  2464. {
  2465. peer=s->session->peer;
  2466. pkey=X509_get_pubkey(peer);
  2467. type=X509_certificate_type(peer,pkey);
  2468. }
  2469. else
  2470. {
  2471. peer=NULL;
  2472. pkey=NULL;
  2473. }
  2474. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY)
  2475. {
  2476. s->s3->tmp.reuse_message=1;
  2477. if ((peer != NULL) && (type | EVP_PKT_SIGN))
  2478. {
  2479. al=SSL_AD_UNEXPECTED_MESSAGE;
  2480. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_MISSING_VERIFY_MESSAGE);
  2481. goto f_err;
  2482. }
  2483. ret=1;
  2484. goto end;
  2485. }
  2486. if (peer == NULL)
  2487. {
  2488. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_NO_CLIENT_CERT_RECEIVED);
  2489. al=SSL_AD_UNEXPECTED_MESSAGE;
  2490. goto f_err;
  2491. }
  2492. if (!(type & EVP_PKT_SIGN))
  2493. {
  2494. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
  2495. al=SSL_AD_ILLEGAL_PARAMETER;
  2496. goto f_err;
  2497. }
  2498. if (s->s3->change_cipher_spec)
  2499. {
  2500. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_CCS_RECEIVED_EARLY);
  2501. al=SSL_AD_UNEXPECTED_MESSAGE;
  2502. goto f_err;
  2503. }
  2504. /* we now have a signature that we need to verify */
  2505. p=(unsigned char *)s->init_msg;
  2506. /* Check for broken implementations of GOST ciphersuites */
  2507. /* If key is GOST and n is exactly 64, it is bare
  2508. * signature without length field */
  2509. if (n==64 && (pkey->type==NID_id_GostR3410_94 ||
  2510. pkey->type == NID_id_GostR3410_2001) )
  2511. {
  2512. i=64;
  2513. }
  2514. else
  2515. {
  2516. n2s(p,i);
  2517. n-=2;
  2518. if (i > n)
  2519. {
  2520. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_LENGTH_MISMATCH);
  2521. al=SSL_AD_DECODE_ERROR;
  2522. goto f_err;
  2523. }
  2524. }
  2525. j=EVP_PKEY_size(pkey);
  2526. if ((i > j) || (n > j) || (n <= 0))
  2527. {
  2528. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_WRONG_SIGNATURE_SIZE);
  2529. al=SSL_AD_DECODE_ERROR;
  2530. goto f_err;
  2531. }
  2532. #ifndef OPENSSL_NO_RSA
  2533. if (pkey->type == EVP_PKEY_RSA)
  2534. {
  2535. i=RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md,
  2536. MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, p, i,
  2537. pkey->pkey.rsa);
  2538. if (i < 0)
  2539. {
  2540. al=SSL_AD_DECRYPT_ERROR;
  2541. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_DECRYPT);
  2542. goto f_err;
  2543. }
  2544. if (i == 0)
  2545. {
  2546. al=SSL_AD_DECRYPT_ERROR;
  2547. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_SIGNATURE);
  2548. goto f_err;
  2549. }
  2550. }
  2551. else
  2552. #endif
  2553. #ifndef OPENSSL_NO_DSA
  2554. if (pkey->type == EVP_PKEY_DSA)
  2555. {
  2556. j=DSA_verify(pkey->save_type,
  2557. &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
  2558. SHA_DIGEST_LENGTH,p,i,pkey->pkey.dsa);
  2559. if (j <= 0)
  2560. {
  2561. /* bad signature */
  2562. al=SSL_AD_DECRYPT_ERROR;
  2563. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_DSA_SIGNATURE);
  2564. goto f_err;
  2565. }
  2566. }
  2567. else
  2568. #endif
  2569. #ifndef OPENSSL_NO_ECDSA
  2570. if (pkey->type == EVP_PKEY_EC)
  2571. {
  2572. j=ECDSA_verify(pkey->save_type,
  2573. &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
  2574. SHA_DIGEST_LENGTH,p,i,pkey->pkey.ec);
  2575. if (j <= 0)
  2576. {
  2577. /* bad signature */
  2578. al=SSL_AD_DECRYPT_ERROR;
  2579. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
  2580. SSL_R_BAD_ECDSA_SIGNATURE);
  2581. goto f_err;
  2582. }
  2583. }
  2584. else
  2585. #endif
  2586. if (pkey->type == NID_id_GostR3410_94 || pkey->type == NID_id_GostR3410_2001)
  2587. { unsigned char signature[64];
  2588. int idx;
  2589. EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(pkey,NULL);
  2590. EVP_PKEY_verify_init(pctx);
  2591. if (i!=64) {
  2592. fprintf(stderr,"GOST signature length is %d",i);
  2593. }
  2594. for (idx=0;idx<64;idx++) {
  2595. signature[63-idx]=p[idx];
  2596. }
  2597. j=EVP_PKEY_verify(pctx,signature,64,s->s3->tmp.cert_verify_md,32);
  2598. EVP_PKEY_CTX_free(pctx);
  2599. if (j<=0)
  2600. {
  2601. al=SSL_AD_DECRYPT_ERROR;
  2602. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
  2603. SSL_R_BAD_ECDSA_SIGNATURE);
  2604. goto f_err;
  2605. }
  2606. }
  2607. else
  2608. {
  2609. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,ERR_R_INTERNAL_ERROR);
  2610. al=SSL_AD_UNSUPPORTED_CERTIFICATE;
  2611. goto f_err;
  2612. }
  2613. ret=1;
  2614. if (0)
  2615. {
  2616. f_err:
  2617. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  2618. }
  2619. end:
  2620. EVP_PKEY_free(pkey);
  2621. return(ret);
  2622. }
  2623. int ssl3_get_client_certificate(SSL *s)
  2624. {
  2625. int i,ok,al,ret= -1;
  2626. X509 *x=NULL;
  2627. unsigned long l,nc,llen,n;
  2628. const unsigned char *p,*q;
  2629. unsigned char *d;
  2630. STACK_OF(X509) *sk=NULL;
  2631. n=s->method->ssl_get_message(s,
  2632. SSL3_ST_SR_CERT_A,
  2633. SSL3_ST_SR_CERT_B,
  2634. -1,
  2635. s->max_cert_list,
  2636. &ok);
  2637. if (!ok) return((int)n);
  2638. if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE)
  2639. {
  2640. if ( (s->verify_mode & SSL_VERIFY_PEER) &&
  2641. (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
  2642. {
  2643. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  2644. al=SSL_AD_HANDSHAKE_FAILURE;
  2645. goto f_err;
  2646. }
  2647. /* If tls asked for a client cert, the client must return a 0 list */
  2648. if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request)
  2649. {
  2650. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
  2651. al=SSL_AD_UNEXPECTED_MESSAGE;
  2652. goto f_err;
  2653. }
  2654. s->s3->tmp.reuse_message=1;
  2655. return(1);
  2656. }
  2657. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
  2658. {
  2659. al=SSL_AD_UNEXPECTED_MESSAGE;
  2660. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_WRONG_MESSAGE_TYPE);
  2661. goto f_err;
  2662. }
  2663. p=d=(unsigned char *)s->init_msg;
  2664. if ((sk=sk_X509_new_null()) == NULL)
  2665. {
  2666. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
  2667. goto err;
  2668. }
  2669. n2l3(p,llen);
  2670. if (llen+3 != n)
  2671. {
  2672. al=SSL_AD_DECODE_ERROR;
  2673. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
  2674. goto f_err;
  2675. }
  2676. for (nc=0; nc<llen; )
  2677. {
  2678. n2l3(p,l);
  2679. if ((l+nc+3) > llen)
  2680. {
  2681. al=SSL_AD_DECODE_ERROR;
  2682. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
  2683. goto f_err;
  2684. }
  2685. q=p;
  2686. x=d2i_X509(NULL,&p,l);
  2687. if (x == NULL)
  2688. {
  2689. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_ASN1_LIB);
  2690. goto err;
  2691. }
  2692. if (p != (q+l))
  2693. {
  2694. al=SSL_AD_DECODE_ERROR;
  2695. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
  2696. goto f_err;
  2697. }
  2698. if (!sk_X509_push(sk,x))
  2699. {
  2700. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
  2701. goto err;
  2702. }
  2703. x=NULL;
  2704. nc+=l+3;
  2705. }
  2706. if (sk_X509_num(sk) <= 0)
  2707. {
  2708. /* TLS does not mind 0 certs returned */
  2709. if (s->version == SSL3_VERSION)
  2710. {
  2711. al=SSL_AD_HANDSHAKE_FAILURE;
  2712. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATES_RETURNED);
  2713. goto f_err;
  2714. }
  2715. /* Fail for TLS only if we required a certificate */
  2716. else if ((s->verify_mode & SSL_VERIFY_PEER) &&
  2717. (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
  2718. {
  2719. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  2720. al=SSL_AD_HANDSHAKE_FAILURE;
  2721. goto f_err;
  2722. }
  2723. }
  2724. else
  2725. {
  2726. i=ssl_verify_cert_chain(s,sk);
  2727. if (i <= 0)
  2728. {
  2729. al=ssl_verify_alarm_type(s->verify_result);
  2730. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATE_RETURNED);
  2731. goto f_err;
  2732. }
  2733. }
  2734. if (s->session->peer != NULL) /* This should not be needed */
  2735. X509_free(s->session->peer);
  2736. s->session->peer=sk_X509_shift(sk);
  2737. s->session->verify_result = s->verify_result;
  2738. /* With the current implementation, sess_cert will always be NULL
  2739. * when we arrive here. */
  2740. if (s->session->sess_cert == NULL)
  2741. {
  2742. s->session->sess_cert = ssl_sess_cert_new();
  2743. if (s->session->sess_cert == NULL)
  2744. {
  2745. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
  2746. goto err;
  2747. }
  2748. }
  2749. if (s->session->sess_cert->cert_chain != NULL)
  2750. sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
  2751. s->session->sess_cert->cert_chain=sk;
  2752. /* Inconsistency alert: cert_chain does *not* include the
  2753. * peer's own certificate, while we do include it in s3_clnt.c */
  2754. sk=NULL;
  2755. ret=1;
  2756. if (0)
  2757. {
  2758. f_err:
  2759. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  2760. }
  2761. err:
  2762. if (x != NULL) X509_free(x);
  2763. if (sk != NULL) sk_X509_pop_free(sk,X509_free);
  2764. return(ret);
  2765. }
  2766. int ssl3_send_server_certificate(SSL *s)
  2767. {
  2768. unsigned long l;
  2769. X509 *x;
  2770. if (s->state == SSL3_ST_SW_CERT_A)
  2771. {
  2772. x=ssl_get_server_send_cert(s);
  2773. if (x == NULL)
  2774. {
  2775. /* VRS: allow null cert if auth == KRB5 */
  2776. if ((s->s3->tmp.new_cipher->algorithm_auth != SSL_aKRB5) ||
  2777. (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5))
  2778. {
  2779. SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,ERR_R_INTERNAL_ERROR);
  2780. return(0);
  2781. }
  2782. }
  2783. l=ssl3_output_cert_chain(s,x);
  2784. s->state=SSL3_ST_SW_CERT_B;
  2785. s->init_num=(int)l;
  2786. s->init_off=0;
  2787. }
  2788. /* SSL3_ST_SW_CERT_B */
  2789. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  2790. }
  2791. #ifndef OPENSSL_NO_TLSEXT
  2792. int ssl3_send_newsession_ticket(SSL *s)
  2793. {
  2794. if (s->state == SSL3_ST_SW_SESSION_TICKET_A)
  2795. {
  2796. unsigned char *p, *senc, *macstart;
  2797. int len, slen;
  2798. unsigned int hlen;
  2799. EVP_CIPHER_CTX ctx;
  2800. HMAC_CTX hctx;
  2801. SSL_CTX *tctx = s->initial_ctx;
  2802. unsigned char iv[EVP_MAX_IV_LENGTH];
  2803. unsigned char key_name[16];
  2804. /* get session encoding length */
  2805. slen = i2d_SSL_SESSION(s->session, NULL);
  2806. /* Some length values are 16 bits, so forget it if session is
  2807. * too long
  2808. */
  2809. if (slen > 0xFF00)
  2810. return -1;
  2811. /* Grow buffer if need be: the length calculation is as
  2812. * follows 1 (size of message name) + 3 (message length
  2813. * bytes) + 4 (ticket lifetime hint) + 2 (ticket length) +
  2814. * 16 (key name) + max_iv_len (iv length) +
  2815. * session_length + max_enc_block_size (max encrypted session
  2816. * length) + max_md_size (HMAC).
  2817. */
  2818. if (!BUF_MEM_grow(s->init_buf,
  2819. 26 + EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH +
  2820. EVP_MAX_MD_SIZE + slen))
  2821. return -1;
  2822. senc = OPENSSL_malloc(slen);
  2823. if (!senc)
  2824. return -1;
  2825. p = senc;
  2826. i2d_SSL_SESSION(s->session, &p);
  2827. p=(unsigned char *)s->init_buf->data;
  2828. /* do the header */
  2829. *(p++)=SSL3_MT_NEWSESSION_TICKET;
  2830. /* Skip message length for now */
  2831. p += 3;
  2832. EVP_CIPHER_CTX_init(&ctx);
  2833. HMAC_CTX_init(&hctx);
  2834. /* Initialize HMAC and cipher contexts. If callback present
  2835. * it does all the work otherwise use generated values
  2836. * from parent ctx.
  2837. */
  2838. if (tctx->tlsext_ticket_key_cb)
  2839. {
  2840. if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx,
  2841. &hctx, 1) < 0)
  2842. {
  2843. OPENSSL_free(senc);
  2844. return -1;
  2845. }
  2846. }
  2847. else
  2848. {
  2849. RAND_pseudo_bytes(iv, 16);
  2850. EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
  2851. tctx->tlsext_tick_aes_key, iv);
  2852. HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
  2853. tlsext_tick_md(), NULL);
  2854. memcpy(key_name, tctx->tlsext_tick_key_name, 16);
  2855. }
  2856. l2n(s->session->tlsext_tick_lifetime_hint, p);
  2857. /* Skip ticket length for now */
  2858. p += 2;
  2859. /* Output key name */
  2860. macstart = p;
  2861. memcpy(p, key_name, 16);
  2862. p += 16;
  2863. /* output IV */
  2864. memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
  2865. p += EVP_CIPHER_CTX_iv_length(&ctx);
  2866. /* Encrypt session data */
  2867. EVP_EncryptUpdate(&ctx, p, &len, senc, slen);
  2868. p += len;
  2869. EVP_EncryptFinal(&ctx, p, &len);
  2870. p += len;
  2871. EVP_CIPHER_CTX_cleanup(&ctx);
  2872. HMAC_Update(&hctx, macstart, p - macstart);
  2873. HMAC_Final(&hctx, p, &hlen);
  2874. HMAC_CTX_cleanup(&hctx);
  2875. p += hlen;
  2876. /* Now write out lengths: p points to end of data written */
  2877. /* Total length */
  2878. len = p - (unsigned char *)s->init_buf->data;
  2879. p=(unsigned char *)s->init_buf->data + 1;
  2880. l2n3(len - 4, p); /* Message length */
  2881. p += 4;
  2882. s2n(len - 10, p); /* Ticket length */
  2883. /* number of bytes to write */
  2884. s->init_num= len;
  2885. s->state=SSL3_ST_SW_SESSION_TICKET_B;
  2886. s->init_off=0;
  2887. OPENSSL_free(senc);
  2888. }
  2889. /* SSL3_ST_SW_SESSION_TICKET_B */
  2890. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  2891. }
  2892. int ssl3_send_cert_status(SSL *s)
  2893. {
  2894. if (s->state == SSL3_ST_SW_CERT_STATUS_A)
  2895. {
  2896. unsigned char *p;
  2897. /* Grow buffer if need be: the length calculation is as
  2898. * follows 1 (message type) + 3 (message length) +
  2899. * 1 (ocsp response type) + 3 (ocsp response length)
  2900. * + (ocsp response)
  2901. */
  2902. if (!BUF_MEM_grow(s->init_buf, 8 + s->tlsext_ocsp_resplen))
  2903. return -1;
  2904. p=(unsigned char *)s->init_buf->data;
  2905. /* do the header */
  2906. *(p++)=SSL3_MT_CERTIFICATE_STATUS;
  2907. /* message length */
  2908. l2n3(s->tlsext_ocsp_resplen + 4, p);
  2909. /* status type */
  2910. *(p++)= s->tlsext_status_type;
  2911. /* length of OCSP response */
  2912. l2n3(s->tlsext_ocsp_resplen, p);
  2913. /* actual response */
  2914. memcpy(p, s->tlsext_ocsp_resp, s->tlsext_ocsp_resplen);
  2915. /* number of bytes to write */
  2916. s->init_num = 8 + s->tlsext_ocsp_resplen;
  2917. s->state=SSL3_ST_SW_CERT_STATUS_B;
  2918. s->init_off = 0;
  2919. }
  2920. /* SSL3_ST_SW_CERT_STATUS_B */
  2921. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  2922. }
  2923. # ifndef OPENSSL_NO_NPN
  2924. /* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It
  2925. * sets the next_proto member in s if found */
  2926. int ssl3_get_next_proto(SSL *s)
  2927. {
  2928. int ok;
  2929. unsigned proto_len, padding_len;
  2930. long n;
  2931. const unsigned char *p;
  2932. /* Clients cannot send a NextProtocol message if we didn't see the
  2933. * extension in their ClientHello */
  2934. if (!s->s3->next_proto_neg_seen)
  2935. {
  2936. SSLerr(SSL_F_SSL3_GET_NEXT_PROTO,SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION);
  2937. return -1;
  2938. }
  2939. n=s->method->ssl_get_message(s,
  2940. SSL3_ST_SR_NEXT_PROTO_A,
  2941. SSL3_ST_SR_NEXT_PROTO_B,
  2942. SSL3_MT_NEXT_PROTO,
  2943. 129,
  2944. &ok);
  2945. if (!ok)
  2946. return((int)n);
  2947. /* s->state doesn't reflect whether ChangeCipherSpec has been received
  2948. * in this handshake, but s->s3->change_cipher_spec does (will be reset
  2949. * by ssl3_get_finished). */
  2950. if (!s->s3->change_cipher_spec)
  2951. {
  2952. SSLerr(SSL_F_SSL3_GET_NEXT_PROTO,SSL_R_GOT_NEXT_PROTO_BEFORE_A_CCS);
  2953. return -1;
  2954. }
  2955. if (n < 2)
  2956. return 0; /* The body must be > 1 bytes long */
  2957. p=(unsigned char *)s->init_msg;
  2958. /* The payload looks like:
  2959. * uint8 proto_len;
  2960. * uint8 proto[proto_len];
  2961. * uint8 padding_len;
  2962. * uint8 padding[padding_len];
  2963. */
  2964. proto_len = p[0];
  2965. if (proto_len + 2 > s->init_num)
  2966. return 0;
  2967. padding_len = p[proto_len + 1];
  2968. if (proto_len + padding_len + 2 != s->init_num)
  2969. return 0;
  2970. s->next_proto_negotiated = OPENSSL_malloc(proto_len);
  2971. if (!s->next_proto_negotiated)
  2972. {
  2973. SSLerr(SSL_F_SSL3_GET_NEXT_PROTO,ERR_R_MALLOC_FAILURE);
  2974. return 0;
  2975. }
  2976. memcpy(s->next_proto_negotiated, p + 1, proto_len);
  2977. s->next_proto_negotiated_len = proto_len;
  2978. return 1;
  2979. }
  2980. # endif
  2981. #endif