s3_srvr.c 89 KB

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