s3_srvr.c 122 KB

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