ssl_lib.c 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794
  1. /*
  2. * ! \file ssl/ssl_lib.c \brief Version independent SSL functions.
  3. */
  4. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  5. * All rights reserved.
  6. *
  7. * This package is an SSL implementation written
  8. * by Eric Young (eay@cryptsoft.com).
  9. * The implementation was written so as to conform with Netscapes SSL.
  10. *
  11. * This library is free for commercial and non-commercial use as long as
  12. * the following conditions are aheared to. The following conditions
  13. * apply to all code found in this distribution, be it the RC4, RSA,
  14. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  15. * included with this distribution is covered by the same copyright terms
  16. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  17. *
  18. * Copyright remains Eric Young's, and as such any Copyright notices in
  19. * the code are not to be removed.
  20. * If this package is used in a product, Eric Young should be given attribution
  21. * as the author of the parts of the library used.
  22. * This can be in the form of a textual message at program startup or
  23. * in documentation (online or textual) provided with the package.
  24. *
  25. * Redistribution and use in source and binary forms, with or without
  26. * modification, are permitted provided that the following conditions
  27. * are met:
  28. * 1. Redistributions of source code must retain the copyright
  29. * notice, this list of conditions and the following disclaimer.
  30. * 2. Redistributions in binary form must reproduce the above copyright
  31. * notice, this list of conditions and the following disclaimer in the
  32. * documentation and/or other materials provided with the distribution.
  33. * 3. All advertising materials mentioning features or use of this software
  34. * must display the following acknowledgement:
  35. * "This product includes cryptographic software written by
  36. * Eric Young (eay@cryptsoft.com)"
  37. * The word 'cryptographic' can be left out if the rouines from the library
  38. * being used are not cryptographic related :-).
  39. * 4. If you include any Windows specific code (or a derivative thereof) from
  40. * the apps directory (application code) you must include an acknowledgement:
  41. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  42. *
  43. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  44. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  45. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  46. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  47. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  48. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  49. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  50. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  51. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  52. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  53. * SUCH DAMAGE.
  54. *
  55. * The licence and distribution terms for any publically available version or
  56. * derivative of this code cannot be changed. i.e. this code cannot simply be
  57. * copied and put under another distribution licence
  58. * [including the GNU Public Licence.]
  59. */
  60. /* ====================================================================
  61. * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
  62. *
  63. * Redistribution and use in source and binary forms, with or without
  64. * modification, are permitted provided that the following conditions
  65. * are met:
  66. *
  67. * 1. Redistributions of source code must retain the above copyright
  68. * notice, this list of conditions and the following disclaimer.
  69. *
  70. * 2. Redistributions in binary form must reproduce the above copyright
  71. * notice, this list of conditions and the following disclaimer in
  72. * the documentation and/or other materials provided with the
  73. * distribution.
  74. *
  75. * 3. All advertising materials mentioning features or use of this
  76. * software must display the following acknowledgment:
  77. * "This product includes software developed by the OpenSSL Project
  78. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  79. *
  80. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  81. * endorse or promote products derived from this software without
  82. * prior written permission. For written permission, please contact
  83. * openssl-core@openssl.org.
  84. *
  85. * 5. Products derived from this software may not be called "OpenSSL"
  86. * nor may "OpenSSL" appear in their names without prior written
  87. * permission of the OpenSSL Project.
  88. *
  89. * 6. Redistributions of any form whatsoever must retain the following
  90. * acknowledgment:
  91. * "This product includes software developed by the OpenSSL Project
  92. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  93. *
  94. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  95. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  96. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  97. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  98. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  99. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  100. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  101. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  102. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  103. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  104. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  105. * OF THE POSSIBILITY OF SUCH DAMAGE.
  106. * ====================================================================
  107. *
  108. * This product includes cryptographic software written by Eric Young
  109. * (eay@cryptsoft.com). This product includes software written by Tim
  110. * Hudson (tjh@cryptsoft.com).
  111. *
  112. */
  113. /* ====================================================================
  114. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  115. * ECC cipher suite support in OpenSSL originally developed by
  116. * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
  117. */
  118. /* ====================================================================
  119. * Copyright 2005 Nokia. All rights reserved.
  120. *
  121. * The portions of the attached software ("Contribution") is developed by
  122. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  123. * license.
  124. *
  125. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  126. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  127. * support (see RFC 4279) to OpenSSL.
  128. *
  129. * No patent licenses or other rights except those expressly stated in
  130. * the OpenSSL open source license shall be deemed granted or received
  131. * expressly, by implication, estoppel, or otherwise.
  132. *
  133. * No assurances are provided by Nokia that the Contribution does not
  134. * infringe the patent or other intellectual property rights of any third
  135. * party or that the license provides you with all the necessary rights
  136. * to make use of the Contribution.
  137. *
  138. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  139. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  140. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  141. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  142. * OTHERWISE.
  143. */
  144. #ifdef REF_CHECK
  145. # include <assert.h>
  146. #endif
  147. #include <stdio.h>
  148. #include "ssl_locl.h"
  149. #include <openssl/objects.h>
  150. #include <openssl/lhash.h>
  151. #include <openssl/x509v3.h>
  152. #include <openssl/rand.h>
  153. #include <openssl/ocsp.h>
  154. #ifndef OPENSSL_NO_DH
  155. # include <openssl/dh.h>
  156. #endif
  157. #ifndef OPENSSL_NO_ENGINE
  158. # include <openssl/engine.h>
  159. #endif
  160. #include <openssl/async.h>
  161. const char SSL_version_str[] = OPENSSL_VERSION_TEXT;
  162. SSL3_ENC_METHOD ssl3_undef_enc_method = {
  163. /*
  164. * evil casts, but these functions are only called if there's a library
  165. * bug
  166. */
  167. (int (*)(SSL *, int))ssl_undefined_function,
  168. (int (*)(SSL *, unsigned char *, int))ssl_undefined_function,
  169. ssl_undefined_function,
  170. (int (*)(SSL *, unsigned char *, unsigned char *, int))
  171. ssl_undefined_function,
  172. (int (*)(SSL *, int))ssl_undefined_function,
  173. (int (*)(SSL *, const char *, int, unsigned char *))
  174. ssl_undefined_function,
  175. 0, /* finish_mac_length */
  176. NULL, /* client_finished_label */
  177. 0, /* client_finished_label_len */
  178. NULL, /* server_finished_label */
  179. 0, /* server_finished_label_len */
  180. (int (*)(int))ssl_undefined_function,
  181. (int (*)(SSL *, unsigned char *, size_t, const char *,
  182. size_t, const unsigned char *, size_t,
  183. int use_context))ssl_undefined_function,
  184. };
  185. struct ssl_async_args {
  186. SSL *s;
  187. void *buf;
  188. int num;
  189. int type;
  190. union {
  191. int (*func1)(SSL *, void *, int);
  192. int (*func2)(SSL *, const void *, int);
  193. } f;
  194. };
  195. static const struct {
  196. uint8_t mtype;
  197. uint8_t ord;
  198. int nid;
  199. } dane_mds[] = {
  200. { DANETLS_MATCHING_FULL, 0, NID_undef },
  201. { DANETLS_MATCHING_2256, 1, NID_sha256 },
  202. { DANETLS_MATCHING_2512, 2, NID_sha512 },
  203. };
  204. static int dane_ctx_enable(struct dane_ctx_st *dctx)
  205. {
  206. const EVP_MD **mdevp;
  207. uint8_t *mdord;
  208. uint8_t mdmax = DANETLS_MATCHING_LAST;
  209. int n = ((int) mdmax) + 1; /* int to handle PrivMatch(255) */
  210. size_t i;
  211. mdevp = OPENSSL_zalloc(n * sizeof(*mdevp));
  212. mdord = OPENSSL_zalloc(n * sizeof(*mdord));
  213. if (mdord == NULL || mdevp == NULL) {
  214. OPENSSL_free(mdevp);
  215. SSLerr(SSL_F_DANE_CTX_ENABLE, ERR_R_MALLOC_FAILURE);
  216. return 0;
  217. }
  218. /* Install default entries */
  219. for (i = 0; i < OSSL_NELEM(dane_mds); ++i) {
  220. const EVP_MD *md;
  221. if (dane_mds[i].nid == NID_undef ||
  222. (md = EVP_get_digestbynid(dane_mds[i].nid)) == NULL)
  223. continue;
  224. mdevp[dane_mds[i].mtype] = md;
  225. mdord[dane_mds[i].mtype] = dane_mds[i].ord;
  226. }
  227. dctx->mdevp = mdevp;
  228. dctx->mdord = mdord;
  229. dctx->mdmax = mdmax;
  230. return 1;
  231. }
  232. static void dane_ctx_final(struct dane_ctx_st *dctx)
  233. {
  234. OPENSSL_free(dctx->mdevp);
  235. dctx->mdevp = NULL;
  236. OPENSSL_free(dctx->mdord);
  237. dctx->mdord = NULL;
  238. dctx->mdmax = 0;
  239. }
  240. static void tlsa_free(danetls_record *t)
  241. {
  242. if (t == NULL)
  243. return;
  244. OPENSSL_free(t->data);
  245. EVP_PKEY_free(t->spki);
  246. OPENSSL_free(t);
  247. }
  248. static void dane_final(struct dane_st *dane)
  249. {
  250. sk_danetls_record_pop_free(dane->trecs, tlsa_free);
  251. dane->trecs = NULL;
  252. sk_X509_pop_free(dane->certs, X509_free);
  253. dane->certs = NULL;
  254. X509_free(dane->mcert);
  255. dane->mcert = NULL;
  256. dane->mtlsa = NULL;
  257. dane->mdpth = -1;
  258. dane->pdpth = -1;
  259. }
  260. /*
  261. * dane_copy - Copy dane configuration, sans verification state.
  262. */
  263. static int ssl_dane_dup(SSL *to, SSL *from)
  264. {
  265. int num;
  266. int i;
  267. if (!DANETLS_ENABLED(&from->dane))
  268. return 1;
  269. dane_final(&to->dane);
  270. num = sk_danetls_record_num(from->dane.trecs);
  271. for (i = 0; i < num; ++i) {
  272. danetls_record *t = sk_danetls_record_value(from->dane.trecs, i);
  273. if (SSL_dane_tlsa_add(to, t->usage, t->selector, t->mtype,
  274. t->data, t->dlen) <= 0)
  275. return 0;
  276. }
  277. return 1;
  278. }
  279. static int dane_mtype_set(
  280. struct dane_ctx_st *dctx,
  281. const EVP_MD *md,
  282. uint8_t mtype,
  283. uint8_t ord)
  284. {
  285. int i;
  286. if (mtype == DANETLS_MATCHING_FULL && md != NULL) {
  287. SSLerr(SSL_F_DANE_MTYPE_SET,
  288. SSL_R_DANE_CANNOT_OVERRIDE_MTYPE_FULL);
  289. return 0;
  290. }
  291. if (mtype > dctx->mdmax) {
  292. const EVP_MD **mdevp;
  293. uint8_t *mdord;
  294. int n = ((int) mtype) + 1;
  295. mdevp = OPENSSL_realloc(dctx->mdevp, n * sizeof(*mdevp));
  296. if (mdevp == NULL) {
  297. SSLerr(SSL_F_DANE_MTYPE_SET, ERR_R_MALLOC_FAILURE);
  298. return -1;
  299. }
  300. dctx->mdevp = mdevp;
  301. mdord = OPENSSL_realloc(dctx->mdord, n * sizeof(*mdord));
  302. if (mdord == NULL) {
  303. SSLerr(SSL_F_DANE_MTYPE_SET, ERR_R_MALLOC_FAILURE);
  304. return -1;
  305. }
  306. dctx->mdord = mdord;
  307. /* Zero-fill any gaps */
  308. for (i = dctx->mdmax+1; i < mtype; ++i) {
  309. mdevp[i] = NULL;
  310. mdord[i] = 0;
  311. }
  312. dctx->mdmax = mtype;
  313. }
  314. dctx->mdevp[mtype] = md;
  315. /* Coerce ordinal of disabled matching types to 0 */
  316. dctx->mdord[mtype] = (md == NULL) ? 0 : ord;
  317. return 1;
  318. }
  319. static const EVP_MD *tlsa_md_get(struct dane_st *dane, uint8_t mtype)
  320. {
  321. if (mtype > dane->dctx->mdmax)
  322. return NULL;
  323. return dane->dctx->mdevp[mtype];
  324. }
  325. static int dane_tlsa_add(
  326. struct dane_st *dane,
  327. uint8_t usage,
  328. uint8_t selector,
  329. uint8_t mtype,
  330. unsigned char *data,
  331. size_t dlen)
  332. {
  333. danetls_record *t;
  334. const EVP_MD *md = NULL;
  335. int ilen = (int)dlen;
  336. int i;
  337. if (dane->trecs == NULL) {
  338. SSLerr(SSL_F_DANE_TLSA_ADD, SSL_R_DANE_NOT_ENABLED);
  339. return -1;
  340. }
  341. if (ilen < 0 || dlen != (size_t)ilen) {
  342. SSLerr(SSL_F_DANE_TLSA_ADD, SSL_R_DANE_TLSA_BAD_DATA_LENGTH);
  343. return 0;
  344. }
  345. if (usage > DANETLS_USAGE_LAST) {
  346. SSLerr(SSL_F_DANE_TLSA_ADD, SSL_R_DANE_TLSA_BAD_CERTIFICATE_USAGE);
  347. return 0;
  348. }
  349. if (selector > DANETLS_SELECTOR_LAST) {
  350. SSLerr(SSL_F_DANE_TLSA_ADD, SSL_R_DANE_TLSA_BAD_SELECTOR);
  351. return 0;
  352. }
  353. if (mtype != DANETLS_MATCHING_FULL) {
  354. md = tlsa_md_get(dane, mtype);
  355. if (md == NULL) {
  356. SSLerr(SSL_F_DANE_TLSA_ADD, SSL_R_DANE_TLSA_BAD_MATCHING_TYPE);
  357. return 0;
  358. }
  359. }
  360. if (md != NULL && dlen != (size_t)EVP_MD_size(md)) {
  361. SSLerr(SSL_F_DANE_TLSA_ADD, SSL_R_DANE_TLSA_BAD_DIGEST_LENGTH);
  362. return 0;
  363. }
  364. if (!data) {
  365. SSLerr(SSL_F_DANE_TLSA_ADD, SSL_R_DANE_TLSA_NULL_DATA);
  366. return 0;
  367. }
  368. if ((t = OPENSSL_zalloc(sizeof(*t))) == NULL) {
  369. SSLerr(SSL_F_DANE_TLSA_ADD, ERR_R_MALLOC_FAILURE);
  370. return -1;
  371. }
  372. t->usage = usage;
  373. t->selector = selector;
  374. t->mtype = mtype;
  375. t->data = OPENSSL_malloc(ilen);
  376. if (t->data == NULL) {
  377. tlsa_free(t);
  378. SSLerr(SSL_F_DANE_TLSA_ADD, ERR_R_MALLOC_FAILURE);
  379. return -1;
  380. }
  381. memcpy(t->data, data, ilen);
  382. t->dlen = ilen;
  383. /* Validate and cache full certificate or public key */
  384. if (mtype == DANETLS_MATCHING_FULL) {
  385. const unsigned char *p = data;
  386. X509 *cert = NULL;
  387. EVP_PKEY *pkey = NULL;
  388. switch (selector) {
  389. case DANETLS_SELECTOR_CERT:
  390. if (!d2i_X509(&cert, &p, dlen) || p < data ||
  391. dlen != (size_t)(p - data)) {
  392. tlsa_free(t);
  393. SSLerr(SSL_F_DANE_TLSA_ADD, SSL_R_DANE_TLSA_BAD_CERTIFICATE);
  394. return 0;
  395. }
  396. if (X509_get0_pubkey(cert) == NULL) {
  397. tlsa_free(t);
  398. SSLerr(SSL_F_DANE_TLSA_ADD, SSL_R_DANE_TLSA_BAD_CERTIFICATE);
  399. return 0;
  400. }
  401. if ((DANETLS_USAGE_BIT(usage) & DANETLS_TA_MASK) == 0) {
  402. X509_free(cert);
  403. break;
  404. }
  405. /*
  406. * For usage DANE-TA(2), we support authentication via "2 0 0" TLSA
  407. * records that contain full certificates of trust-anchors that are
  408. * not present in the wire chain. For usage PKIX-TA(0), we augment
  409. * the chain with untrusted Full(0) certificates from DNS, in case
  410. * they are missing from the chain.
  411. */
  412. if ((dane->certs == NULL &&
  413. (dane->certs = sk_X509_new_null()) == NULL) ||
  414. !sk_X509_push(dane->certs, cert)) {
  415. SSLerr(SSL_F_DANE_TLSA_ADD, ERR_R_MALLOC_FAILURE);
  416. X509_free(cert);
  417. tlsa_free(t);
  418. return -1;
  419. }
  420. break;
  421. case DANETLS_SELECTOR_SPKI:
  422. if (!d2i_PUBKEY(&pkey, &p, dlen) || p < data ||
  423. dlen != (size_t)(p - data)) {
  424. tlsa_free(t);
  425. SSLerr(SSL_F_DANE_TLSA_ADD, SSL_R_DANE_TLSA_BAD_PUBLIC_KEY);
  426. return 0;
  427. }
  428. /*
  429. * For usage DANE-TA(2), we support authentication via "2 1 0" TLSA
  430. * records that contain full bare keys of trust-anchors that are
  431. * not present in the wire chain.
  432. */
  433. if (usage == DANETLS_USAGE_DANE_TA)
  434. t->spki = pkey;
  435. else
  436. EVP_PKEY_free(pkey);
  437. break;
  438. }
  439. }
  440. /*-
  441. * Find the right insertion point for the new record.
  442. *
  443. * See crypto/x509/x509_vfy.c. We sort DANE-EE(3) records first, so that
  444. * they can be processed first, as they require no chain building, and no
  445. * expiration or hostname checks. Because DANE-EE(3) is numerically
  446. * largest, this is accomplished via descending sort by "usage".
  447. *
  448. * We also sort in descending order by matching ordinal to simplify
  449. * the implementation of digest agility in the verification code.
  450. *
  451. * The choice of order for the selector is not significant, so we
  452. * use the same descending order for consistency.
  453. */
  454. for (i = 0; i < sk_danetls_record_num(dane->trecs); ++i) {
  455. danetls_record *rec = sk_danetls_record_value(dane->trecs, i);
  456. if (rec->usage > usage)
  457. continue;
  458. if (rec->usage < usage)
  459. break;
  460. if (rec->selector > selector)
  461. continue;
  462. if (rec->selector < selector)
  463. break;
  464. if (dane->dctx->mdord[rec->mtype] > dane->dctx->mdord[mtype])
  465. continue;
  466. break;
  467. }
  468. if (!sk_danetls_record_insert(dane->trecs, t, i)) {
  469. tlsa_free(t);
  470. SSLerr(SSL_F_DANE_TLSA_ADD, ERR_R_MALLOC_FAILURE);
  471. return -1;
  472. }
  473. dane->umask |= DANETLS_USAGE_BIT(usage);
  474. return 1;
  475. }
  476. static void clear_ciphers(SSL *s)
  477. {
  478. /* clear the current cipher */
  479. ssl_clear_cipher_ctx(s);
  480. ssl_clear_hash_ctx(&s->read_hash);
  481. ssl_clear_hash_ctx(&s->write_hash);
  482. }
  483. int SSL_clear(SSL *s)
  484. {
  485. if (s->method == NULL) {
  486. SSLerr(SSL_F_SSL_CLEAR, SSL_R_NO_METHOD_SPECIFIED);
  487. return (0);
  488. }
  489. if (ssl_clear_bad_session(s)) {
  490. SSL_SESSION_free(s->session);
  491. s->session = NULL;
  492. }
  493. s->error = 0;
  494. s->hit = 0;
  495. s->shutdown = 0;
  496. if (s->renegotiate) {
  497. SSLerr(SSL_F_SSL_CLEAR, ERR_R_INTERNAL_ERROR);
  498. return 0;
  499. }
  500. ossl_statem_clear(s);
  501. s->version = s->method->version;
  502. s->client_version = s->version;
  503. s->rwstate = SSL_NOTHING;
  504. BUF_MEM_free(s->init_buf);
  505. s->init_buf = NULL;
  506. clear_ciphers(s);
  507. s->first_packet = 0;
  508. /* Reset DANE verification result state */
  509. s->dane.mdpth = -1;
  510. s->dane.pdpth = -1;
  511. X509_free(s->dane.mcert);
  512. s->dane.mcert = NULL;
  513. s->dane.mtlsa = NULL;
  514. /* Clear the verification result peername */
  515. X509_VERIFY_PARAM_move_peername(s->param, NULL);
  516. /*
  517. * Check to see if we were changed into a different method, if so, revert
  518. * back if we are not doing session-id reuse.
  519. */
  520. if (!ossl_statem_get_in_handshake(s) && (s->session == NULL)
  521. && (s->method != s->ctx->method)) {
  522. s->method->ssl_free(s);
  523. s->method = s->ctx->method;
  524. if (!s->method->ssl_new(s))
  525. return (0);
  526. } else
  527. s->method->ssl_clear(s);
  528. RECORD_LAYER_clear(&s->rlayer);
  529. return (1);
  530. }
  531. /** Used to change an SSL_CTXs default SSL method type */
  532. int SSL_CTX_set_ssl_version(SSL_CTX *ctx, const SSL_METHOD *meth)
  533. {
  534. STACK_OF(SSL_CIPHER) *sk;
  535. ctx->method = meth;
  536. sk = ssl_create_cipher_list(ctx->method, &(ctx->cipher_list),
  537. &(ctx->cipher_list_by_id),
  538. SSL_DEFAULT_CIPHER_LIST, ctx->cert);
  539. if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0)) {
  540. SSLerr(SSL_F_SSL_CTX_SET_SSL_VERSION,
  541. SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
  542. return (0);
  543. }
  544. return (1);
  545. }
  546. SSL *SSL_new(SSL_CTX *ctx)
  547. {
  548. SSL *s;
  549. if (ctx == NULL) {
  550. SSLerr(SSL_F_SSL_NEW, SSL_R_NULL_SSL_CTX);
  551. return (NULL);
  552. }
  553. if (ctx->method == NULL) {
  554. SSLerr(SSL_F_SSL_NEW, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
  555. return (NULL);
  556. }
  557. s = OPENSSL_zalloc(sizeof(*s));
  558. if (s == NULL)
  559. goto err;
  560. RECORD_LAYER_init(&s->rlayer, s);
  561. s->options = ctx->options;
  562. s->min_proto_version = ctx->min_proto_version;
  563. s->max_proto_version = ctx->max_proto_version;
  564. s->mode = ctx->mode;
  565. s->max_cert_list = ctx->max_cert_list;
  566. s->references = 1;
  567. /*
  568. * Earlier library versions used to copy the pointer to the CERT, not
  569. * its contents; only when setting new parameters for the per-SSL
  570. * copy, ssl_cert_new would be called (and the direct reference to
  571. * the per-SSL_CTX settings would be lost, but those still were
  572. * indirectly accessed for various purposes, and for that reason they
  573. * used to be known as s->ctx->default_cert). Now we don't look at the
  574. * SSL_CTX's CERT after having duplicated it once.
  575. */
  576. s->cert = ssl_cert_dup(ctx->cert);
  577. if (s->cert == NULL)
  578. goto err;
  579. RECORD_LAYER_set_read_ahead(&s->rlayer, ctx->read_ahead);
  580. s->msg_callback = ctx->msg_callback;
  581. s->msg_callback_arg = ctx->msg_callback_arg;
  582. s->verify_mode = ctx->verify_mode;
  583. s->not_resumable_session_cb = ctx->not_resumable_session_cb;
  584. s->sid_ctx_length = ctx->sid_ctx_length;
  585. OPENSSL_assert(s->sid_ctx_length <= sizeof s->sid_ctx);
  586. memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
  587. s->verify_callback = ctx->default_verify_callback;
  588. s->generate_session_id = ctx->generate_session_id;
  589. s->param = X509_VERIFY_PARAM_new();
  590. if (s->param == NULL)
  591. goto err;
  592. X509_VERIFY_PARAM_inherit(s->param, ctx->param);
  593. s->quiet_shutdown = ctx->quiet_shutdown;
  594. s->max_send_fragment = ctx->max_send_fragment;
  595. CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
  596. s->ctx = ctx;
  597. s->tlsext_debug_cb = 0;
  598. s->tlsext_debug_arg = NULL;
  599. s->tlsext_ticket_expected = 0;
  600. s->tlsext_status_type = -1;
  601. s->tlsext_status_expected = 0;
  602. s->tlsext_ocsp_ids = NULL;
  603. s->tlsext_ocsp_exts = NULL;
  604. s->tlsext_ocsp_resp = NULL;
  605. s->tlsext_ocsp_resplen = -1;
  606. CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
  607. s->initial_ctx = ctx;
  608. # ifndef OPENSSL_NO_EC
  609. if (ctx->tlsext_ecpointformatlist) {
  610. s->tlsext_ecpointformatlist =
  611. OPENSSL_memdup(ctx->tlsext_ecpointformatlist,
  612. ctx->tlsext_ecpointformatlist_length);
  613. if (!s->tlsext_ecpointformatlist)
  614. goto err;
  615. s->tlsext_ecpointformatlist_length =
  616. ctx->tlsext_ecpointformatlist_length;
  617. }
  618. if (ctx->tlsext_ellipticcurvelist) {
  619. s->tlsext_ellipticcurvelist =
  620. OPENSSL_memdup(ctx->tlsext_ellipticcurvelist,
  621. ctx->tlsext_ellipticcurvelist_length);
  622. if (!s->tlsext_ellipticcurvelist)
  623. goto err;
  624. s->tlsext_ellipticcurvelist_length =
  625. ctx->tlsext_ellipticcurvelist_length;
  626. }
  627. # endif
  628. # ifndef OPENSSL_NO_NEXTPROTONEG
  629. s->next_proto_negotiated = NULL;
  630. # endif
  631. if (s->ctx->alpn_client_proto_list) {
  632. s->alpn_client_proto_list =
  633. OPENSSL_malloc(s->ctx->alpn_client_proto_list_len);
  634. if (s->alpn_client_proto_list == NULL)
  635. goto err;
  636. memcpy(s->alpn_client_proto_list, s->ctx->alpn_client_proto_list,
  637. s->ctx->alpn_client_proto_list_len);
  638. s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
  639. }
  640. s->verify_result = X509_V_OK;
  641. s->default_passwd_callback = ctx->default_passwd_callback;
  642. s->default_passwd_callback_userdata = ctx->default_passwd_callback_userdata;
  643. s->method = ctx->method;
  644. if (!s->method->ssl_new(s))
  645. goto err;
  646. s->server = (ctx->method->ssl_accept == ssl_undefined_function) ? 0 : 1;
  647. if (!SSL_clear(s))
  648. goto err;
  649. CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
  650. #ifndef OPENSSL_NO_PSK
  651. s->psk_client_callback = ctx->psk_client_callback;
  652. s->psk_server_callback = ctx->psk_server_callback;
  653. #endif
  654. s->job = NULL;
  655. return (s);
  656. err:
  657. SSL_free(s);
  658. SSLerr(SSL_F_SSL_NEW, ERR_R_MALLOC_FAILURE);
  659. return (NULL);
  660. }
  661. int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const unsigned char *sid_ctx,
  662. unsigned int sid_ctx_len)
  663. {
  664. if (sid_ctx_len > sizeof ctx->sid_ctx) {
  665. SSLerr(SSL_F_SSL_CTX_SET_SESSION_ID_CONTEXT,
  666. SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  667. return 0;
  668. }
  669. ctx->sid_ctx_length = sid_ctx_len;
  670. memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
  671. return 1;
  672. }
  673. int SSL_set_session_id_context(SSL *ssl, const unsigned char *sid_ctx,
  674. unsigned int sid_ctx_len)
  675. {
  676. if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
  677. SSLerr(SSL_F_SSL_SET_SESSION_ID_CONTEXT,
  678. SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  679. return 0;
  680. }
  681. ssl->sid_ctx_length = sid_ctx_len;
  682. memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
  683. return 1;
  684. }
  685. int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb)
  686. {
  687. CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
  688. ctx->generate_session_id = cb;
  689. CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
  690. return 1;
  691. }
  692. int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb)
  693. {
  694. CRYPTO_w_lock(CRYPTO_LOCK_SSL);
  695. ssl->generate_session_id = cb;
  696. CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
  697. return 1;
  698. }
  699. int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
  700. unsigned int id_len)
  701. {
  702. /*
  703. * A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how
  704. * we can "construct" a session to give us the desired check - ie. to
  705. * find if there's a session in the hash table that would conflict with
  706. * any new session built out of this id/id_len and the ssl_version in use
  707. * by this SSL.
  708. */
  709. SSL_SESSION r, *p;
  710. if (id_len > sizeof r.session_id)
  711. return 0;
  712. r.ssl_version = ssl->version;
  713. r.session_id_length = id_len;
  714. memcpy(r.session_id, id, id_len);
  715. CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
  716. p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
  717. CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
  718. return (p != NULL);
  719. }
  720. int SSL_CTX_set_purpose(SSL_CTX *s, int purpose)
  721. {
  722. return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
  723. }
  724. int SSL_set_purpose(SSL *s, int purpose)
  725. {
  726. return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
  727. }
  728. int SSL_CTX_set_trust(SSL_CTX *s, int trust)
  729. {
  730. return X509_VERIFY_PARAM_set_trust(s->param, trust);
  731. }
  732. int SSL_set_trust(SSL *s, int trust)
  733. {
  734. return X509_VERIFY_PARAM_set_trust(s->param, trust);
  735. }
  736. int SSL_set1_host(SSL *s, const char *hostname)
  737. {
  738. return X509_VERIFY_PARAM_set1_host(s->param, hostname, 0);
  739. }
  740. int SSL_add1_host(SSL *s, const char *hostname)
  741. {
  742. return X509_VERIFY_PARAM_add1_host(s->param, hostname, 0);
  743. }
  744. void SSL_set_hostflags(SSL *s, unsigned int flags)
  745. {
  746. X509_VERIFY_PARAM_set_hostflags(s->param, flags);
  747. }
  748. const char *SSL_get0_peername(SSL *s)
  749. {
  750. return X509_VERIFY_PARAM_get0_peername(s->param);
  751. }
  752. int SSL_CTX_dane_enable(SSL_CTX *ctx)
  753. {
  754. return dane_ctx_enable(&ctx->dane);
  755. }
  756. int SSL_dane_enable(SSL *s, const char *basedomain)
  757. {
  758. struct dane_st *dane = &s->dane;
  759. if (s->ctx->dane.mdmax == 0) {
  760. SSLerr(SSL_F_SSL_DANE_ENABLE, SSL_R_CONTEXT_NOT_DANE_ENABLED);
  761. return 0;
  762. }
  763. if (dane->trecs != NULL) {
  764. SSLerr(SSL_F_SSL_DANE_ENABLE, SSL_R_DANE_ALREADY_ENABLED);
  765. return 0;
  766. }
  767. /*
  768. * Default SNI name. This rejects empty names, while set1_host below
  769. * accepts them and disables host name checks. To avoid side-effects with
  770. * invalid input, set the SNI name first.
  771. */
  772. if (s->tlsext_hostname == NULL) {
  773. if (!SSL_set_tlsext_host_name(s, basedomain)) {
  774. SSLerr(SSL_F_SSL_DANE_ENABLE, SSL_R_ERROR_SETTING_TLSA_BASE_DOMAIN);
  775. return -1;
  776. }
  777. }
  778. /* Primary RFC6125 reference identifier */
  779. if (!X509_VERIFY_PARAM_set1_host(s->param, basedomain, 0)) {
  780. SSLerr(SSL_F_SSL_DANE_ENABLE, SSL_R_ERROR_SETTING_TLSA_BASE_DOMAIN);
  781. return -1;
  782. }
  783. dane->mdpth = -1;
  784. dane->pdpth = -1;
  785. dane->dctx = &s->ctx->dane;
  786. dane->trecs = sk_danetls_record_new_null();
  787. if (dane->trecs == NULL) {
  788. SSLerr(SSL_F_SSL_DANE_ENABLE, ERR_R_MALLOC_FAILURE);
  789. return -1;
  790. }
  791. return 1;
  792. }
  793. int SSL_get0_dane_authority(SSL *s, X509 **mcert, EVP_PKEY **mspki)
  794. {
  795. struct dane_st *dane = &s->dane;
  796. if (!DANETLS_ENABLED(dane))
  797. return -1;
  798. if (dane->mtlsa) {
  799. if (mcert)
  800. *mcert = dane->mcert;
  801. if (mspki)
  802. *mspki = (dane->mcert == NULL) ? dane->mtlsa->spki : NULL;
  803. }
  804. return dane->mdpth;
  805. }
  806. int SSL_get0_dane_tlsa(SSL *s, uint8_t *usage, uint8_t *selector,
  807. uint8_t *mtype, unsigned const char **data, size_t *dlen)
  808. {
  809. struct dane_st *dane = &s->dane;
  810. if (!DANETLS_ENABLED(dane))
  811. return -1;
  812. if (dane->mtlsa) {
  813. if (usage)
  814. *usage = dane->mtlsa->usage;
  815. if (selector)
  816. *selector = dane->mtlsa->selector;
  817. if (mtype)
  818. *mtype = dane->mtlsa->mtype;
  819. if (data)
  820. *data = dane->mtlsa->data;
  821. if (dlen)
  822. *dlen = dane->mtlsa->dlen;
  823. }
  824. return dane->mdpth;
  825. }
  826. struct dane_st *SSL_get0_dane(SSL *s)
  827. {
  828. return &s->dane;
  829. }
  830. int SSL_dane_tlsa_add(SSL *s, uint8_t usage, uint8_t selector,
  831. uint8_t mtype, unsigned char *data, size_t dlen)
  832. {
  833. return dane_tlsa_add(&s->dane, usage, selector, mtype, data, dlen);
  834. }
  835. int SSL_CTX_dane_mtype_set(SSL_CTX *ctx, const EVP_MD *md, uint8_t mtype, uint8_t ord)
  836. {
  837. return dane_mtype_set(&ctx->dane, md, mtype, ord);
  838. }
  839. int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm)
  840. {
  841. return X509_VERIFY_PARAM_set1(ctx->param, vpm);
  842. }
  843. int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm)
  844. {
  845. return X509_VERIFY_PARAM_set1(ssl->param, vpm);
  846. }
  847. X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx)
  848. {
  849. return ctx->param;
  850. }
  851. X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl)
  852. {
  853. return ssl->param;
  854. }
  855. void SSL_certs_clear(SSL *s)
  856. {
  857. ssl_cert_clear_certs(s->cert);
  858. }
  859. void SSL_free(SSL *s)
  860. {
  861. int i;
  862. if (s == NULL)
  863. return;
  864. i = CRYPTO_add(&s->references, -1, CRYPTO_LOCK_SSL);
  865. #ifdef REF_PRINT
  866. REF_PRINT("SSL", s);
  867. #endif
  868. if (i > 0)
  869. return;
  870. #ifdef REF_CHECK
  871. if (i < 0) {
  872. fprintf(stderr, "SSL_free, bad reference count\n");
  873. abort(); /* ok */
  874. }
  875. #endif
  876. X509_VERIFY_PARAM_free(s->param);
  877. dane_final(&s->dane);
  878. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
  879. if (s->bbio != NULL) {
  880. /* If the buffering BIO is in place, pop it off */
  881. if (s->bbio == s->wbio) {
  882. s->wbio = BIO_pop(s->wbio);
  883. }
  884. BIO_free(s->bbio);
  885. s->bbio = NULL;
  886. }
  887. BIO_free_all(s->rbio);
  888. if (s->wbio != s->rbio)
  889. BIO_free_all(s->wbio);
  890. BUF_MEM_free(s->init_buf);
  891. /* add extra stuff */
  892. sk_SSL_CIPHER_free(s->cipher_list);
  893. sk_SSL_CIPHER_free(s->cipher_list_by_id);
  894. /* Make the next call work :-) */
  895. if (s->session != NULL) {
  896. ssl_clear_bad_session(s);
  897. SSL_SESSION_free(s->session);
  898. }
  899. clear_ciphers(s);
  900. ssl_cert_free(s->cert);
  901. /* Free up if allocated */
  902. OPENSSL_free(s->tlsext_hostname);
  903. SSL_CTX_free(s->initial_ctx);
  904. #ifndef OPENSSL_NO_EC
  905. OPENSSL_free(s->tlsext_ecpointformatlist);
  906. OPENSSL_free(s->tlsext_ellipticcurvelist);
  907. #endif /* OPENSSL_NO_EC */
  908. sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts, X509_EXTENSION_free);
  909. sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids, OCSP_RESPID_free);
  910. OPENSSL_free(s->tlsext_ocsp_resp);
  911. OPENSSL_free(s->alpn_client_proto_list);
  912. sk_X509_NAME_pop_free(s->client_CA, X509_NAME_free);
  913. if (s->method != NULL)
  914. s->method->ssl_free(s);
  915. RECORD_LAYER_release(&s->rlayer);
  916. SSL_CTX_free(s->ctx);
  917. #if !defined(OPENSSL_NO_NEXTPROTONEG)
  918. OPENSSL_free(s->next_proto_negotiated);
  919. #endif
  920. #ifndef OPENSSL_NO_SRTP
  921. sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
  922. #endif
  923. OPENSSL_free(s);
  924. }
  925. void SSL_set_rbio(SSL *s, BIO *rbio)
  926. {
  927. if (s->rbio != rbio)
  928. BIO_free_all(s->rbio);
  929. s->rbio = rbio;
  930. }
  931. void SSL_set_wbio(SSL *s, BIO *wbio)
  932. {
  933. /*
  934. * If the output buffering BIO is still in place, remove it
  935. */
  936. if (s->bbio != NULL) {
  937. if (s->wbio == s->bbio) {
  938. s->wbio = s->wbio->next_bio;
  939. s->bbio->next_bio = NULL;
  940. }
  941. }
  942. if (s->wbio != wbio && s->rbio != s->wbio)
  943. BIO_free_all(s->wbio);
  944. s->wbio = wbio;
  945. }
  946. void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio)
  947. {
  948. SSL_set_wbio(s, wbio);
  949. SSL_set_rbio(s, rbio);
  950. }
  951. BIO *SSL_get_rbio(const SSL *s)
  952. {
  953. return (s->rbio);
  954. }
  955. BIO *SSL_get_wbio(const SSL *s)
  956. {
  957. return (s->wbio);
  958. }
  959. int SSL_get_fd(const SSL *s)
  960. {
  961. return (SSL_get_rfd(s));
  962. }
  963. int SSL_get_rfd(const SSL *s)
  964. {
  965. int ret = -1;
  966. BIO *b, *r;
  967. b = SSL_get_rbio(s);
  968. r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
  969. if (r != NULL)
  970. BIO_get_fd(r, &ret);
  971. return (ret);
  972. }
  973. int SSL_get_wfd(const SSL *s)
  974. {
  975. int ret = -1;
  976. BIO *b, *r;
  977. b = SSL_get_wbio(s);
  978. r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
  979. if (r != NULL)
  980. BIO_get_fd(r, &ret);
  981. return (ret);
  982. }
  983. #ifndef OPENSSL_NO_SOCK
  984. int SSL_set_fd(SSL *s, int fd)
  985. {
  986. int ret = 0;
  987. BIO *bio = NULL;
  988. bio = BIO_new(BIO_s_socket());
  989. if (bio == NULL) {
  990. SSLerr(SSL_F_SSL_SET_FD, ERR_R_BUF_LIB);
  991. goto err;
  992. }
  993. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  994. SSL_set_bio(s, bio, bio);
  995. ret = 1;
  996. err:
  997. return (ret);
  998. }
  999. int SSL_set_wfd(SSL *s, int fd)
  1000. {
  1001. int ret = 0;
  1002. BIO *bio = NULL;
  1003. if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_SOCKET)
  1004. || ((int)BIO_get_fd(s->rbio, NULL) != fd)) {
  1005. bio = BIO_new(BIO_s_socket());
  1006. if (bio == NULL) {
  1007. SSLerr(SSL_F_SSL_SET_WFD, ERR_R_BUF_LIB);
  1008. goto err;
  1009. }
  1010. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  1011. SSL_set_bio(s, SSL_get_rbio(s), bio);
  1012. } else
  1013. SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s));
  1014. ret = 1;
  1015. err:
  1016. return (ret);
  1017. }
  1018. int SSL_set_rfd(SSL *s, int fd)
  1019. {
  1020. int ret = 0;
  1021. BIO *bio = NULL;
  1022. if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_SOCKET)
  1023. || ((int)BIO_get_fd(s->wbio, NULL) != fd)) {
  1024. bio = BIO_new(BIO_s_socket());
  1025. if (bio == NULL) {
  1026. SSLerr(SSL_F_SSL_SET_RFD, ERR_R_BUF_LIB);
  1027. goto err;
  1028. }
  1029. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  1030. SSL_set_bio(s, bio, SSL_get_wbio(s));
  1031. } else
  1032. SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s));
  1033. ret = 1;
  1034. err:
  1035. return (ret);
  1036. }
  1037. #endif
  1038. /* return length of latest Finished message we sent, copy to 'buf' */
  1039. size_t SSL_get_finished(const SSL *s, void *buf, size_t count)
  1040. {
  1041. size_t ret = 0;
  1042. if (s->s3 != NULL) {
  1043. ret = s->s3->tmp.finish_md_len;
  1044. if (count > ret)
  1045. count = ret;
  1046. memcpy(buf, s->s3->tmp.finish_md, count);
  1047. }
  1048. return ret;
  1049. }
  1050. /* return length of latest Finished message we expected, copy to 'buf' */
  1051. size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count)
  1052. {
  1053. size_t ret = 0;
  1054. if (s->s3 != NULL) {
  1055. ret = s->s3->tmp.peer_finish_md_len;
  1056. if (count > ret)
  1057. count = ret;
  1058. memcpy(buf, s->s3->tmp.peer_finish_md, count);
  1059. }
  1060. return ret;
  1061. }
  1062. int SSL_get_verify_mode(const SSL *s)
  1063. {
  1064. return (s->verify_mode);
  1065. }
  1066. int SSL_get_verify_depth(const SSL *s)
  1067. {
  1068. return X509_VERIFY_PARAM_get_depth(s->param);
  1069. }
  1070. int (*SSL_get_verify_callback(const SSL *s)) (int, X509_STORE_CTX *) {
  1071. return (s->verify_callback);
  1072. }
  1073. int SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
  1074. {
  1075. return (ctx->verify_mode);
  1076. }
  1077. int SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
  1078. {
  1079. return X509_VERIFY_PARAM_get_depth(ctx->param);
  1080. }
  1081. int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx)) (int, X509_STORE_CTX *) {
  1082. return (ctx->default_verify_callback);
  1083. }
  1084. void SSL_set_verify(SSL *s, int mode,
  1085. int (*callback) (int ok, X509_STORE_CTX *ctx))
  1086. {
  1087. s->verify_mode = mode;
  1088. if (callback != NULL)
  1089. s->verify_callback = callback;
  1090. }
  1091. void SSL_set_verify_depth(SSL *s, int depth)
  1092. {
  1093. X509_VERIFY_PARAM_set_depth(s->param, depth);
  1094. }
  1095. void SSL_set_read_ahead(SSL *s, int yes)
  1096. {
  1097. RECORD_LAYER_set_read_ahead(&s->rlayer, yes);
  1098. }
  1099. int SSL_get_read_ahead(const SSL *s)
  1100. {
  1101. return RECORD_LAYER_get_read_ahead(&s->rlayer);
  1102. }
  1103. int SSL_pending(const SSL *s)
  1104. {
  1105. /*
  1106. * SSL_pending cannot work properly if read-ahead is enabled
  1107. * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), and it is
  1108. * impossible to fix since SSL_pending cannot report errors that may be
  1109. * observed while scanning the new data. (Note that SSL_pending() is
  1110. * often used as a boolean value, so we'd better not return -1.)
  1111. */
  1112. return (s->method->ssl_pending(s));
  1113. }
  1114. X509 *SSL_get_peer_certificate(const SSL *s)
  1115. {
  1116. X509 *r;
  1117. if ((s == NULL) || (s->session == NULL))
  1118. r = NULL;
  1119. else
  1120. r = s->session->peer;
  1121. if (r == NULL)
  1122. return (r);
  1123. X509_up_ref(r);
  1124. return (r);
  1125. }
  1126. STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s)
  1127. {
  1128. STACK_OF(X509) *r;
  1129. if ((s == NULL) || (s->session == NULL))
  1130. r = NULL;
  1131. else
  1132. r = s->session->peer_chain;
  1133. /*
  1134. * If we are a client, cert_chain includes the peer's own certificate; if
  1135. * we are a server, it does not.
  1136. */
  1137. return (r);
  1138. }
  1139. /*
  1140. * Now in theory, since the calling process own 't' it should be safe to
  1141. * modify. We need to be able to read f without being hassled
  1142. */
  1143. int SSL_copy_session_id(SSL *t, const SSL *f)
  1144. {
  1145. /* Do we need to to SSL locking? */
  1146. if (!SSL_set_session(t, SSL_get_session(f))) {
  1147. return 0;
  1148. }
  1149. /*
  1150. * what if we are setup for one protocol version but want to talk another
  1151. */
  1152. if (t->method != f->method) {
  1153. t->method->ssl_free(t);
  1154. t->method = f->method;
  1155. if (t->method->ssl_new(t) == 0)
  1156. return 0;
  1157. }
  1158. CRYPTO_add(&f->cert->references, 1, CRYPTO_LOCK_SSL_CERT);
  1159. ssl_cert_free(t->cert);
  1160. t->cert = f->cert;
  1161. if (!SSL_set_session_id_context(t, f->sid_ctx, f->sid_ctx_length)) {
  1162. return 0;
  1163. }
  1164. return 1;
  1165. }
  1166. /* Fix this so it checks all the valid key/cert options */
  1167. int SSL_CTX_check_private_key(const SSL_CTX *ctx)
  1168. {
  1169. if ((ctx == NULL) ||
  1170. (ctx->cert->key->x509 == NULL)) {
  1171. SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,
  1172. SSL_R_NO_CERTIFICATE_ASSIGNED);
  1173. return (0);
  1174. }
  1175. if (ctx->cert->key->privatekey == NULL) {
  1176. SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,
  1177. SSL_R_NO_PRIVATE_KEY_ASSIGNED);
  1178. return (0);
  1179. }
  1180. return (X509_check_private_key
  1181. (ctx->cert->key->x509, ctx->cert->key->privatekey));
  1182. }
  1183. /* Fix this function so that it takes an optional type parameter */
  1184. int SSL_check_private_key(const SSL *ssl)
  1185. {
  1186. if (ssl == NULL) {
  1187. SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY, ERR_R_PASSED_NULL_PARAMETER);
  1188. return (0);
  1189. }
  1190. if (ssl->cert->key->x509 == NULL) {
  1191. SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY, SSL_R_NO_CERTIFICATE_ASSIGNED);
  1192. return (0);
  1193. }
  1194. if (ssl->cert->key->privatekey == NULL) {
  1195. SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
  1196. return (0);
  1197. }
  1198. return (X509_check_private_key(ssl->cert->key->x509,
  1199. ssl->cert->key->privatekey));
  1200. }
  1201. int SSL_waiting_for_async(SSL *s)
  1202. {
  1203. if(s->job)
  1204. return 1;
  1205. return 0;
  1206. }
  1207. int SSL_get_async_wait_fd(SSL *s)
  1208. {
  1209. if (!s->job)
  1210. return -1;
  1211. return ASYNC_get_wait_fd(s->job);
  1212. }
  1213. int SSL_accept(SSL *s)
  1214. {
  1215. if (s->handshake_func == 0) {
  1216. /* Not properly initialized yet */
  1217. SSL_set_accept_state(s);
  1218. }
  1219. return SSL_do_handshake(s);
  1220. }
  1221. int SSL_connect(SSL *s)
  1222. {
  1223. if (s->handshake_func == 0) {
  1224. /* Not properly initialized yet */
  1225. SSL_set_connect_state(s);
  1226. }
  1227. return SSL_do_handshake(s);
  1228. }
  1229. long SSL_get_default_timeout(const SSL *s)
  1230. {
  1231. return (s->method->get_timeout());
  1232. }
  1233. static int ssl_start_async_job(SSL *s, struct ssl_async_args *args,
  1234. int (*func)(void *)) {
  1235. int ret;
  1236. switch(ASYNC_start_job(&s->job, &ret, func, args,
  1237. sizeof(struct ssl_async_args))) {
  1238. case ASYNC_ERR:
  1239. s->rwstate = SSL_NOTHING;
  1240. SSLerr(SSL_F_SSL_START_ASYNC_JOB, SSL_R_FAILED_TO_INIT_ASYNC);
  1241. return -1;
  1242. case ASYNC_PAUSE:
  1243. s->rwstate = SSL_ASYNC_PAUSED;
  1244. return -1;
  1245. case ASYNC_FINISH:
  1246. s->job = NULL;
  1247. return ret;
  1248. default:
  1249. s->rwstate = SSL_NOTHING;
  1250. SSLerr(SSL_F_SSL_START_ASYNC_JOB, ERR_R_INTERNAL_ERROR);
  1251. /* Shouldn't happen */
  1252. return -1;
  1253. }
  1254. }
  1255. static int ssl_io_intern(void *vargs)
  1256. {
  1257. struct ssl_async_args *args;
  1258. SSL *s;
  1259. void *buf;
  1260. int num;
  1261. args = (struct ssl_async_args *)vargs;
  1262. s = args->s;
  1263. buf = args->buf;
  1264. num = args->num;
  1265. if (args->type == 1)
  1266. return args->f.func1(s, buf, num);
  1267. else
  1268. return args->f.func2(s, buf, num);
  1269. }
  1270. int SSL_read(SSL *s, void *buf, int num)
  1271. {
  1272. if (s->handshake_func == 0) {
  1273. SSLerr(SSL_F_SSL_READ, SSL_R_UNINITIALIZED);
  1274. return -1;
  1275. }
  1276. if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
  1277. s->rwstate = SSL_NOTHING;
  1278. return (0);
  1279. }
  1280. if((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
  1281. struct ssl_async_args args;
  1282. args.s = s;
  1283. args.buf = buf;
  1284. args.num = num;
  1285. args.type = 1;
  1286. args.f.func1 = s->method->ssl_read;
  1287. return ssl_start_async_job(s, &args, ssl_io_intern);
  1288. } else {
  1289. return s->method->ssl_read(s, buf, num);
  1290. }
  1291. }
  1292. int SSL_peek(SSL *s, void *buf, int num)
  1293. {
  1294. if (s->handshake_func == 0) {
  1295. SSLerr(SSL_F_SSL_PEEK, SSL_R_UNINITIALIZED);
  1296. return -1;
  1297. }
  1298. if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
  1299. return (0);
  1300. }
  1301. if((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
  1302. struct ssl_async_args args;
  1303. args.s = s;
  1304. args.buf = buf;
  1305. args.num = num;
  1306. args.type = 1;
  1307. args.f.func1 = s->method->ssl_peek;
  1308. return ssl_start_async_job(s, &args, ssl_io_intern);
  1309. } else {
  1310. return s->method->ssl_peek(s, buf, num);
  1311. }
  1312. }
  1313. int SSL_write(SSL *s, const void *buf, int num)
  1314. {
  1315. if (s->handshake_func == 0) {
  1316. SSLerr(SSL_F_SSL_WRITE, SSL_R_UNINITIALIZED);
  1317. return -1;
  1318. }
  1319. if (s->shutdown & SSL_SENT_SHUTDOWN) {
  1320. s->rwstate = SSL_NOTHING;
  1321. SSLerr(SSL_F_SSL_WRITE, SSL_R_PROTOCOL_IS_SHUTDOWN);
  1322. return (-1);
  1323. }
  1324. if((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
  1325. struct ssl_async_args args;
  1326. args.s = s;
  1327. args.buf = (void *)buf;
  1328. args.num = num;
  1329. args.type = 2;
  1330. args.f.func2 = s->method->ssl_write;
  1331. return ssl_start_async_job(s, &args, ssl_io_intern);
  1332. } else {
  1333. return s->method->ssl_write(s, buf, num);
  1334. }
  1335. }
  1336. int SSL_shutdown(SSL *s)
  1337. {
  1338. /*
  1339. * Note that this function behaves differently from what one might
  1340. * expect. Return values are 0 for no success (yet), 1 for success; but
  1341. * calling it once is usually not enough, even if blocking I/O is used
  1342. * (see ssl3_shutdown).
  1343. */
  1344. if (s->handshake_func == 0) {
  1345. SSLerr(SSL_F_SSL_SHUTDOWN, SSL_R_UNINITIALIZED);
  1346. return -1;
  1347. }
  1348. if (!SSL_in_init(s))
  1349. return (s->method->ssl_shutdown(s));
  1350. else
  1351. return (1);
  1352. }
  1353. int SSL_renegotiate(SSL *s)
  1354. {
  1355. if (s->renegotiate == 0)
  1356. s->renegotiate = 1;
  1357. s->new_session = 1;
  1358. return (s->method->ssl_renegotiate(s));
  1359. }
  1360. int SSL_renegotiate_abbreviated(SSL *s)
  1361. {
  1362. if (s->renegotiate == 0)
  1363. s->renegotiate = 1;
  1364. s->new_session = 0;
  1365. return (s->method->ssl_renegotiate(s));
  1366. }
  1367. int SSL_renegotiate_pending(SSL *s)
  1368. {
  1369. /*
  1370. * becomes true when negotiation is requested; false again once a
  1371. * handshake has finished
  1372. */
  1373. return (s->renegotiate != 0);
  1374. }
  1375. long SSL_ctrl(SSL *s, int cmd, long larg, void *parg)
  1376. {
  1377. long l;
  1378. switch (cmd) {
  1379. case SSL_CTRL_GET_READ_AHEAD:
  1380. return (RECORD_LAYER_get_read_ahead(&s->rlayer));
  1381. case SSL_CTRL_SET_READ_AHEAD:
  1382. l = RECORD_LAYER_get_read_ahead(&s->rlayer);
  1383. RECORD_LAYER_set_read_ahead(&s->rlayer, larg);
  1384. return (l);
  1385. case SSL_CTRL_SET_MSG_CALLBACK_ARG:
  1386. s->msg_callback_arg = parg;
  1387. return 1;
  1388. case SSL_CTRL_MODE:
  1389. return (s->mode |= larg);
  1390. case SSL_CTRL_CLEAR_MODE:
  1391. return (s->mode &= ~larg);
  1392. case SSL_CTRL_GET_MAX_CERT_LIST:
  1393. return (s->max_cert_list);
  1394. case SSL_CTRL_SET_MAX_CERT_LIST:
  1395. l = s->max_cert_list;
  1396. s->max_cert_list = larg;
  1397. return (l);
  1398. case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
  1399. if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
  1400. return 0;
  1401. s->max_send_fragment = larg;
  1402. return 1;
  1403. case SSL_CTRL_GET_RI_SUPPORT:
  1404. if (s->s3)
  1405. return s->s3->send_connection_binding;
  1406. else
  1407. return 0;
  1408. case SSL_CTRL_CERT_FLAGS:
  1409. return (s->cert->cert_flags |= larg);
  1410. case SSL_CTRL_CLEAR_CERT_FLAGS:
  1411. return (s->cert->cert_flags &= ~larg);
  1412. case SSL_CTRL_GET_RAW_CIPHERLIST:
  1413. if (parg) {
  1414. if (s->s3->tmp.ciphers_raw == NULL)
  1415. return 0;
  1416. *(unsigned char **)parg = s->s3->tmp.ciphers_raw;
  1417. return (int)s->s3->tmp.ciphers_rawlen;
  1418. } else {
  1419. return TLS_CIPHER_LEN;
  1420. }
  1421. case SSL_CTRL_GET_EXTMS_SUPPORT:
  1422. if (!s->session || SSL_in_init(s) || ossl_statem_get_in_handshake(s))
  1423. return -1;
  1424. if (s->session->flags & SSL_SESS_FLAG_EXTMS)
  1425. return 1;
  1426. else
  1427. return 0;
  1428. case SSL_CTRL_SET_MIN_PROTO_VERSION:
  1429. return ssl_set_version_bound(s->ctx->method->version, (int)larg,
  1430. &s->min_proto_version);
  1431. case SSL_CTRL_SET_MAX_PROTO_VERSION:
  1432. return ssl_set_version_bound(s->ctx->method->version, (int)larg,
  1433. &s->max_proto_version);
  1434. default:
  1435. return (s->method->ssl_ctrl(s, cmd, larg, parg));
  1436. }
  1437. }
  1438. long SSL_callback_ctrl(SSL *s, int cmd, void (*fp) (void))
  1439. {
  1440. switch (cmd) {
  1441. case SSL_CTRL_SET_MSG_CALLBACK:
  1442. s->msg_callback = (void (*)
  1443. (int write_p, int version, int content_type,
  1444. const void *buf, size_t len, SSL *ssl,
  1445. void *arg))(fp);
  1446. return 1;
  1447. default:
  1448. return (s->method->ssl_callback_ctrl(s, cmd, fp));
  1449. }
  1450. }
  1451. LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx)
  1452. {
  1453. return ctx->sessions;
  1454. }
  1455. long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
  1456. {
  1457. long l;
  1458. /* For some cases with ctx == NULL perform syntax checks */
  1459. if (ctx == NULL) {
  1460. switch (cmd) {
  1461. #ifndef OPENSSL_NO_EC
  1462. case SSL_CTRL_SET_CURVES_LIST:
  1463. return tls1_set_curves_list(NULL, NULL, parg);
  1464. #endif
  1465. case SSL_CTRL_SET_SIGALGS_LIST:
  1466. case SSL_CTRL_SET_CLIENT_SIGALGS_LIST:
  1467. return tls1_set_sigalgs_list(NULL, parg, 0);
  1468. default:
  1469. return 0;
  1470. }
  1471. }
  1472. switch (cmd) {
  1473. case SSL_CTRL_GET_READ_AHEAD:
  1474. return (ctx->read_ahead);
  1475. case SSL_CTRL_SET_READ_AHEAD:
  1476. l = ctx->read_ahead;
  1477. ctx->read_ahead = larg;
  1478. return (l);
  1479. case SSL_CTRL_SET_MSG_CALLBACK_ARG:
  1480. ctx->msg_callback_arg = parg;
  1481. return 1;
  1482. case SSL_CTRL_GET_MAX_CERT_LIST:
  1483. return (ctx->max_cert_list);
  1484. case SSL_CTRL_SET_MAX_CERT_LIST:
  1485. l = ctx->max_cert_list;
  1486. ctx->max_cert_list = larg;
  1487. return (l);
  1488. case SSL_CTRL_SET_SESS_CACHE_SIZE:
  1489. l = ctx->session_cache_size;
  1490. ctx->session_cache_size = larg;
  1491. return (l);
  1492. case SSL_CTRL_GET_SESS_CACHE_SIZE:
  1493. return (ctx->session_cache_size);
  1494. case SSL_CTRL_SET_SESS_CACHE_MODE:
  1495. l = ctx->session_cache_mode;
  1496. ctx->session_cache_mode = larg;
  1497. return (l);
  1498. case SSL_CTRL_GET_SESS_CACHE_MODE:
  1499. return (ctx->session_cache_mode);
  1500. case SSL_CTRL_SESS_NUMBER:
  1501. return (lh_SSL_SESSION_num_items(ctx->sessions));
  1502. case SSL_CTRL_SESS_CONNECT:
  1503. return (ctx->stats.sess_connect);
  1504. case SSL_CTRL_SESS_CONNECT_GOOD:
  1505. return (ctx->stats.sess_connect_good);
  1506. case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
  1507. return (ctx->stats.sess_connect_renegotiate);
  1508. case SSL_CTRL_SESS_ACCEPT:
  1509. return (ctx->stats.sess_accept);
  1510. case SSL_CTRL_SESS_ACCEPT_GOOD:
  1511. return (ctx->stats.sess_accept_good);
  1512. case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
  1513. return (ctx->stats.sess_accept_renegotiate);
  1514. case SSL_CTRL_SESS_HIT:
  1515. return (ctx->stats.sess_hit);
  1516. case SSL_CTRL_SESS_CB_HIT:
  1517. return (ctx->stats.sess_cb_hit);
  1518. case SSL_CTRL_SESS_MISSES:
  1519. return (ctx->stats.sess_miss);
  1520. case SSL_CTRL_SESS_TIMEOUTS:
  1521. return (ctx->stats.sess_timeout);
  1522. case SSL_CTRL_SESS_CACHE_FULL:
  1523. return (ctx->stats.sess_cache_full);
  1524. case SSL_CTRL_MODE:
  1525. return (ctx->mode |= larg);
  1526. case SSL_CTRL_CLEAR_MODE:
  1527. return (ctx->mode &= ~larg);
  1528. case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
  1529. if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
  1530. return 0;
  1531. ctx->max_send_fragment = larg;
  1532. return 1;
  1533. case SSL_CTRL_CERT_FLAGS:
  1534. return (ctx->cert->cert_flags |= larg);
  1535. case SSL_CTRL_CLEAR_CERT_FLAGS:
  1536. return (ctx->cert->cert_flags &= ~larg);
  1537. case SSL_CTRL_SET_MIN_PROTO_VERSION:
  1538. return ssl_set_version_bound(ctx->method->version, (int)larg,
  1539. &ctx->min_proto_version);
  1540. case SSL_CTRL_SET_MAX_PROTO_VERSION:
  1541. return ssl_set_version_bound(ctx->method->version, (int)larg,
  1542. &ctx->max_proto_version);
  1543. default:
  1544. return (ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg));
  1545. }
  1546. }
  1547. long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp) (void))
  1548. {
  1549. switch (cmd) {
  1550. case SSL_CTRL_SET_MSG_CALLBACK:
  1551. ctx->msg_callback = (void (*)
  1552. (int write_p, int version, int content_type,
  1553. const void *buf, size_t len, SSL *ssl,
  1554. void *arg))(fp);
  1555. return 1;
  1556. default:
  1557. return (ctx->method->ssl_ctx_callback_ctrl(ctx, cmd, fp));
  1558. }
  1559. }
  1560. int ssl_cipher_id_cmp(const SSL_CIPHER *a, const SSL_CIPHER *b)
  1561. {
  1562. if (a->id > b->id)
  1563. return 1;
  1564. if (a->id < b->id)
  1565. return -1;
  1566. return 0;
  1567. }
  1568. int ssl_cipher_ptr_id_cmp(const SSL_CIPHER *const *ap,
  1569. const SSL_CIPHER *const *bp)
  1570. {
  1571. if ((*ap)->id > (*bp)->id)
  1572. return 1;
  1573. if ((*ap)->id < (*bp)->id)
  1574. return -1;
  1575. return 0;
  1576. }
  1577. /** return a STACK of the ciphers available for the SSL and in order of
  1578. * preference */
  1579. STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s)
  1580. {
  1581. if (s != NULL) {
  1582. if (s->cipher_list != NULL) {
  1583. return (s->cipher_list);
  1584. } else if ((s->ctx != NULL) && (s->ctx->cipher_list != NULL)) {
  1585. return (s->ctx->cipher_list);
  1586. }
  1587. }
  1588. return (NULL);
  1589. }
  1590. STACK_OF(SSL_CIPHER) *SSL_get_client_ciphers(const SSL *s)
  1591. {
  1592. if ((s == NULL) || (s->session == NULL) || !s->server)
  1593. return NULL;
  1594. return s->session->ciphers;
  1595. }
  1596. STACK_OF(SSL_CIPHER) *SSL_get1_supported_ciphers(SSL *s)
  1597. {
  1598. STACK_OF(SSL_CIPHER) *sk = NULL, *ciphers;
  1599. int i;
  1600. ciphers = SSL_get_ciphers(s);
  1601. if (!ciphers)
  1602. return NULL;
  1603. ssl_set_client_disabled(s);
  1604. for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
  1605. const SSL_CIPHER *c = sk_SSL_CIPHER_value(ciphers, i);
  1606. if (!ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED)) {
  1607. if (!sk)
  1608. sk = sk_SSL_CIPHER_new_null();
  1609. if (!sk)
  1610. return NULL;
  1611. if (!sk_SSL_CIPHER_push(sk, c)) {
  1612. sk_SSL_CIPHER_free(sk);
  1613. return NULL;
  1614. }
  1615. }
  1616. }
  1617. return sk;
  1618. }
  1619. /** return a STACK of the ciphers available for the SSL and in order of
  1620. * algorithm id */
  1621. STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s)
  1622. {
  1623. if (s != NULL) {
  1624. if (s->cipher_list_by_id != NULL) {
  1625. return (s->cipher_list_by_id);
  1626. } else if ((s->ctx != NULL) && (s->ctx->cipher_list_by_id != NULL)) {
  1627. return (s->ctx->cipher_list_by_id);
  1628. }
  1629. }
  1630. return (NULL);
  1631. }
  1632. /** The old interface to get the same thing as SSL_get_ciphers() */
  1633. const char *SSL_get_cipher_list(const SSL *s, int n)
  1634. {
  1635. const SSL_CIPHER *c;
  1636. STACK_OF(SSL_CIPHER) *sk;
  1637. if (s == NULL)
  1638. return (NULL);
  1639. sk = SSL_get_ciphers(s);
  1640. if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= n))
  1641. return (NULL);
  1642. c = sk_SSL_CIPHER_value(sk, n);
  1643. if (c == NULL)
  1644. return (NULL);
  1645. return (c->name);
  1646. }
  1647. /** specify the ciphers to be used by default by the SSL_CTX */
  1648. int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
  1649. {
  1650. STACK_OF(SSL_CIPHER) *sk;
  1651. sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
  1652. &ctx->cipher_list_by_id, str, ctx->cert);
  1653. /*
  1654. * ssl_create_cipher_list may return an empty stack if it was unable to
  1655. * find a cipher matching the given rule string (for example if the rule
  1656. * string specifies a cipher which has been disabled). This is not an
  1657. * error as far as ssl_create_cipher_list is concerned, and hence
  1658. * ctx->cipher_list and ctx->cipher_list_by_id has been updated.
  1659. */
  1660. if (sk == NULL)
  1661. return 0;
  1662. else if (sk_SSL_CIPHER_num(sk) == 0) {
  1663. SSLerr(SSL_F_SSL_CTX_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH);
  1664. return 0;
  1665. }
  1666. return 1;
  1667. }
  1668. /** specify the ciphers to be used by the SSL */
  1669. int SSL_set_cipher_list(SSL *s, const char *str)
  1670. {
  1671. STACK_OF(SSL_CIPHER) *sk;
  1672. sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
  1673. &s->cipher_list_by_id, str, s->cert);
  1674. /* see comment in SSL_CTX_set_cipher_list */
  1675. if (sk == NULL)
  1676. return 0;
  1677. else if (sk_SSL_CIPHER_num(sk) == 0) {
  1678. SSLerr(SSL_F_SSL_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH);
  1679. return 0;
  1680. }
  1681. return 1;
  1682. }
  1683. char *SSL_get_shared_ciphers(const SSL *s, char *buf, int len)
  1684. {
  1685. char *p;
  1686. STACK_OF(SSL_CIPHER) *sk;
  1687. const SSL_CIPHER *c;
  1688. int i;
  1689. if ((s->session == NULL) || (s->session->ciphers == NULL) || (len < 2))
  1690. return (NULL);
  1691. p = buf;
  1692. sk = s->session->ciphers;
  1693. if (sk_SSL_CIPHER_num(sk) == 0)
  1694. return NULL;
  1695. for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
  1696. int n;
  1697. c = sk_SSL_CIPHER_value(sk, i);
  1698. n = strlen(c->name);
  1699. if (n + 1 > len) {
  1700. if (p != buf)
  1701. --p;
  1702. *p = '\0';
  1703. return buf;
  1704. }
  1705. strcpy(p, c->name);
  1706. p += n;
  1707. *(p++) = ':';
  1708. len -= n + 1;
  1709. }
  1710. p[-1] = '\0';
  1711. return (buf);
  1712. }
  1713. /** return a servername extension value if provided in Client Hello, or NULL.
  1714. * So far, only host_name types are defined (RFC 3546).
  1715. */
  1716. const char *SSL_get_servername(const SSL *s, const int type)
  1717. {
  1718. if (type != TLSEXT_NAMETYPE_host_name)
  1719. return NULL;
  1720. return s->session && !s->tlsext_hostname ?
  1721. s->session->tlsext_hostname : s->tlsext_hostname;
  1722. }
  1723. int SSL_get_servername_type(const SSL *s)
  1724. {
  1725. if (s->session
  1726. && (!s->tlsext_hostname ? s->session->
  1727. tlsext_hostname : s->tlsext_hostname))
  1728. return TLSEXT_NAMETYPE_host_name;
  1729. return -1;
  1730. }
  1731. /*
  1732. * SSL_select_next_proto implements the standard protocol selection. It is
  1733. * expected that this function is called from the callback set by
  1734. * SSL_CTX_set_next_proto_select_cb. The protocol data is assumed to be a
  1735. * vector of 8-bit, length prefixed byte strings. The length byte itself is
  1736. * not included in the length. A byte string of length 0 is invalid. No byte
  1737. * string may be truncated. The current, but experimental algorithm for
  1738. * selecting the protocol is: 1) If the server doesn't support NPN then this
  1739. * is indicated to the callback. In this case, the client application has to
  1740. * abort the connection or have a default application level protocol. 2) If
  1741. * the server supports NPN, but advertises an empty list then the client
  1742. * selects the first protcol in its list, but indicates via the API that this
  1743. * fallback case was enacted. 3) Otherwise, the client finds the first
  1744. * protocol in the server's list that it supports and selects this protocol.
  1745. * This is because it's assumed that the server has better information about
  1746. * which protocol a client should use. 4) If the client doesn't support any
  1747. * of the server's advertised protocols, then this is treated the same as
  1748. * case 2. It returns either OPENSSL_NPN_NEGOTIATED if a common protocol was
  1749. * found, or OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
  1750. */
  1751. int SSL_select_next_proto(unsigned char **out, unsigned char *outlen,
  1752. const unsigned char *server,
  1753. unsigned int server_len,
  1754. const unsigned char *client,
  1755. unsigned int client_len)
  1756. {
  1757. unsigned int i, j;
  1758. const unsigned char *result;
  1759. int status = OPENSSL_NPN_UNSUPPORTED;
  1760. /*
  1761. * For each protocol in server preference order, see if we support it.
  1762. */
  1763. for (i = 0; i < server_len;) {
  1764. for (j = 0; j < client_len;) {
  1765. if (server[i] == client[j] &&
  1766. memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
  1767. /* We found a match */
  1768. result = &server[i];
  1769. status = OPENSSL_NPN_NEGOTIATED;
  1770. goto found;
  1771. }
  1772. j += client[j];
  1773. j++;
  1774. }
  1775. i += server[i];
  1776. i++;
  1777. }
  1778. /* There's no overlap between our protocols and the server's list. */
  1779. result = client;
  1780. status = OPENSSL_NPN_NO_OVERLAP;
  1781. found:
  1782. *out = (unsigned char *)result + 1;
  1783. *outlen = result[0];
  1784. return status;
  1785. }
  1786. #ifndef OPENSSL_NO_NEXTPROTONEG
  1787. /*
  1788. * SSL_get0_next_proto_negotiated sets *data and *len to point to the
  1789. * client's requested protocol for this connection and returns 0. If the
  1790. * client didn't request any protocol, then *data is set to NULL. Note that
  1791. * the client can request any protocol it chooses. The value returned from
  1792. * this function need not be a member of the list of supported protocols
  1793. * provided by the callback.
  1794. */
  1795. void SSL_get0_next_proto_negotiated(const SSL *s, const unsigned char **data,
  1796. unsigned *len)
  1797. {
  1798. *data = s->next_proto_negotiated;
  1799. if (!*data) {
  1800. *len = 0;
  1801. } else {
  1802. *len = s->next_proto_negotiated_len;
  1803. }
  1804. }
  1805. /*
  1806. * SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when
  1807. * a TLS server needs a list of supported protocols for Next Protocol
  1808. * Negotiation. The returned list must be in wire format. The list is
  1809. * returned by setting |out| to point to it and |outlen| to its length. This
  1810. * memory will not be modified, but one should assume that the SSL* keeps a
  1811. * reference to it. The callback should return SSL_TLSEXT_ERR_OK if it
  1812. * wishes to advertise. Otherwise, no such extension will be included in the
  1813. * ServerHello.
  1814. */
  1815. void SSL_CTX_set_next_protos_advertised_cb(SSL_CTX *ctx,
  1816. int (*cb) (SSL *ssl,
  1817. const unsigned char
  1818. **out,
  1819. unsigned int *outlen,
  1820. void *arg), void *arg)
  1821. {
  1822. ctx->next_protos_advertised_cb = cb;
  1823. ctx->next_protos_advertised_cb_arg = arg;
  1824. }
  1825. /*
  1826. * SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
  1827. * client needs to select a protocol from the server's provided list. |out|
  1828. * must be set to point to the selected protocol (which may be within |in|).
  1829. * The length of the protocol name must be written into |outlen|. The
  1830. * server's advertised protocols are provided in |in| and |inlen|. The
  1831. * callback can assume that |in| is syntactically valid. The client must
  1832. * select a protocol. It is fatal to the connection if this callback returns
  1833. * a value other than SSL_TLSEXT_ERR_OK.
  1834. */
  1835. void SSL_CTX_set_next_proto_select_cb(SSL_CTX *ctx,
  1836. int (*cb) (SSL *s, unsigned char **out,
  1837. unsigned char *outlen,
  1838. const unsigned char *in,
  1839. unsigned int inlen,
  1840. void *arg), void *arg)
  1841. {
  1842. ctx->next_proto_select_cb = cb;
  1843. ctx->next_proto_select_cb_arg = arg;
  1844. }
  1845. #endif
  1846. /*
  1847. * SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|.
  1848. * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
  1849. * length-prefixed strings). Returns 0 on success.
  1850. */
  1851. int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char *protos,
  1852. unsigned protos_len)
  1853. {
  1854. OPENSSL_free(ctx->alpn_client_proto_list);
  1855. ctx->alpn_client_proto_list = OPENSSL_malloc(protos_len);
  1856. if (ctx->alpn_client_proto_list == NULL)
  1857. return 1;
  1858. memcpy(ctx->alpn_client_proto_list, protos, protos_len);
  1859. ctx->alpn_client_proto_list_len = protos_len;
  1860. return 0;
  1861. }
  1862. /*
  1863. * SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|.
  1864. * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
  1865. * length-prefixed strings). Returns 0 on success.
  1866. */
  1867. int SSL_set_alpn_protos(SSL *ssl, const unsigned char *protos,
  1868. unsigned protos_len)
  1869. {
  1870. OPENSSL_free(ssl->alpn_client_proto_list);
  1871. ssl->alpn_client_proto_list = OPENSSL_malloc(protos_len);
  1872. if (ssl->alpn_client_proto_list == NULL)
  1873. return 1;
  1874. memcpy(ssl->alpn_client_proto_list, protos, protos_len);
  1875. ssl->alpn_client_proto_list_len = protos_len;
  1876. return 0;
  1877. }
  1878. /*
  1879. * SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is
  1880. * called during ClientHello processing in order to select an ALPN protocol
  1881. * from the client's list of offered protocols.
  1882. */
  1883. void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
  1884. int (*cb) (SSL *ssl,
  1885. const unsigned char **out,
  1886. unsigned char *outlen,
  1887. const unsigned char *in,
  1888. unsigned int inlen,
  1889. void *arg), void *arg)
  1890. {
  1891. ctx->alpn_select_cb = cb;
  1892. ctx->alpn_select_cb_arg = arg;
  1893. }
  1894. /*
  1895. * SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from
  1896. * |ssl|. On return it sets |*data| to point to |*len| bytes of protocol name
  1897. * (not including the leading length-prefix byte). If the server didn't
  1898. * respond with a negotiated protocol then |*len| will be zero.
  1899. */
  1900. void SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data,
  1901. unsigned *len)
  1902. {
  1903. *data = NULL;
  1904. if (ssl->s3)
  1905. *data = ssl->s3->alpn_selected;
  1906. if (*data == NULL)
  1907. *len = 0;
  1908. else
  1909. *len = ssl->s3->alpn_selected_len;
  1910. }
  1911. int SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen,
  1912. const char *label, size_t llen,
  1913. const unsigned char *p, size_t plen,
  1914. int use_context)
  1915. {
  1916. if (s->version < TLS1_VERSION)
  1917. return -1;
  1918. return s->method->ssl3_enc->export_keying_material(s, out, olen, label,
  1919. llen, p, plen,
  1920. use_context);
  1921. }
  1922. static unsigned long ssl_session_hash(const SSL_SESSION *a)
  1923. {
  1924. unsigned long l;
  1925. l = (unsigned long)
  1926. ((unsigned int)a->session_id[0]) |
  1927. ((unsigned int)a->session_id[1] << 8L) |
  1928. ((unsigned long)a->session_id[2] << 16L) |
  1929. ((unsigned long)a->session_id[3] << 24L);
  1930. return (l);
  1931. }
  1932. /*
  1933. * NB: If this function (or indeed the hash function which uses a sort of
  1934. * coarser function than this one) is changed, ensure
  1935. * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on
  1936. * being able to construct an SSL_SESSION that will collide with any existing
  1937. * session with a matching session ID.
  1938. */
  1939. static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b)
  1940. {
  1941. if (a->ssl_version != b->ssl_version)
  1942. return (1);
  1943. if (a->session_id_length != b->session_id_length)
  1944. return (1);
  1945. return (memcmp(a->session_id, b->session_id, a->session_id_length));
  1946. }
  1947. /*
  1948. * These wrapper functions should remain rather than redeclaring
  1949. * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each
  1950. * variable. The reason is that the functions aren't static, they're exposed
  1951. * via ssl.h.
  1952. */
  1953. SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
  1954. {
  1955. SSL_CTX *ret = NULL;
  1956. if (meth == NULL) {
  1957. SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_NULL_SSL_METHOD_PASSED);
  1958. return (NULL);
  1959. }
  1960. if (FIPS_mode() && (meth->version < TLS1_VERSION)) {
  1961. SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_AT_LEAST_TLS_1_0_NEEDED_IN_FIPS_MODE);
  1962. return NULL;
  1963. }
  1964. if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
  1965. SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
  1966. goto err;
  1967. }
  1968. ret = OPENSSL_zalloc(sizeof(*ret));
  1969. if (ret == NULL)
  1970. goto err;
  1971. ret->method = meth;
  1972. ret->min_proto_version = 0;
  1973. ret->max_proto_version = 0;
  1974. ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
  1975. ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
  1976. /* We take the system default. */
  1977. ret->session_timeout = meth->get_timeout();
  1978. ret->references = 1;
  1979. ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
  1980. ret->verify_mode = SSL_VERIFY_NONE;
  1981. if ((ret->cert = ssl_cert_new()) == NULL)
  1982. goto err;
  1983. ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
  1984. if (ret->sessions == NULL)
  1985. goto err;
  1986. ret->cert_store = X509_STORE_new();
  1987. if (ret->cert_store == NULL)
  1988. goto err;
  1989. if (!ssl_create_cipher_list(ret->method,
  1990. &ret->cipher_list, &ret->cipher_list_by_id,
  1991. SSL_DEFAULT_CIPHER_LIST, ret->cert)
  1992. || sk_SSL_CIPHER_num(ret->cipher_list) <= 0) {
  1993. SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_LIBRARY_HAS_NO_CIPHERS);
  1994. goto err2;
  1995. }
  1996. ret->param = X509_VERIFY_PARAM_new();
  1997. if (ret->param == NULL)
  1998. goto err;
  1999. if ((ret->md5 = EVP_get_digestbyname("ssl3-md5")) == NULL) {
  2000. SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES);
  2001. goto err2;
  2002. }
  2003. if ((ret->sha1 = EVP_get_digestbyname("ssl3-sha1")) == NULL) {
  2004. SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES);
  2005. goto err2;
  2006. }
  2007. if ((ret->client_CA = sk_X509_NAME_new_null()) == NULL)
  2008. goto err;
  2009. CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
  2010. /* No compression for DTLS */
  2011. if (!(meth->ssl3_enc->enc_flags & SSL_ENC_FLAG_DTLS))
  2012. ret->comp_methods = SSL_COMP_get_compression_methods();
  2013. ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  2014. /* Setup RFC4507 ticket keys */
  2015. if ((RAND_bytes(ret->tlsext_tick_key_name, 16) <= 0)
  2016. || (RAND_bytes(ret->tlsext_tick_hmac_key, 16) <= 0)
  2017. || (RAND_bytes(ret->tlsext_tick_aes_key, 16) <= 0))
  2018. ret->options |= SSL_OP_NO_TICKET;
  2019. #ifndef OPENSSL_NO_SRP
  2020. if (!SSL_CTX_SRP_CTX_init(ret))
  2021. goto err;
  2022. #endif
  2023. #ifndef OPENSSL_NO_ENGINE
  2024. # ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO
  2025. # define eng_strx(x) #x
  2026. # define eng_str(x) eng_strx(x)
  2027. /* Use specific client engine automatically... ignore errors */
  2028. {
  2029. ENGINE *eng;
  2030. eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
  2031. if (!eng) {
  2032. ERR_clear_error();
  2033. ENGINE_load_builtin_engines();
  2034. eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
  2035. }
  2036. if (!eng || !SSL_CTX_set_client_cert_engine(ret, eng))
  2037. ERR_clear_error();
  2038. }
  2039. # endif
  2040. #endif
  2041. /*
  2042. * Default is to connect to non-RI servers. When RI is more widely
  2043. * deployed might change this.
  2044. */
  2045. ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
  2046. return (ret);
  2047. err:
  2048. SSLerr(SSL_F_SSL_CTX_NEW, ERR_R_MALLOC_FAILURE);
  2049. err2:
  2050. SSL_CTX_free(ret);
  2051. return (NULL);
  2052. }
  2053. void SSL_CTX_free(SSL_CTX *a)
  2054. {
  2055. int i;
  2056. if (a == NULL)
  2057. return;
  2058. i = CRYPTO_add(&a->references, -1, CRYPTO_LOCK_SSL_CTX);
  2059. #ifdef REF_PRINT
  2060. REF_PRINT("SSL_CTX", a);
  2061. #endif
  2062. if (i > 0)
  2063. return;
  2064. #ifdef REF_CHECK
  2065. if (i < 0) {
  2066. fprintf(stderr, "SSL_CTX_free, bad reference count\n");
  2067. abort(); /* ok */
  2068. }
  2069. #endif
  2070. X509_VERIFY_PARAM_free(a->param);
  2071. dane_ctx_final(&a->dane);
  2072. /*
  2073. * Free internal session cache. However: the remove_cb() may reference
  2074. * the ex_data of SSL_CTX, thus the ex_data store can only be removed
  2075. * after the sessions were flushed.
  2076. * As the ex_data handling routines might also touch the session cache,
  2077. * the most secure solution seems to be: empty (flush) the cache, then
  2078. * free ex_data, then finally free the cache.
  2079. * (See ticket [openssl.org #212].)
  2080. */
  2081. if (a->sessions != NULL)
  2082. SSL_CTX_flush_sessions(a, 0);
  2083. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
  2084. lh_SSL_SESSION_free(a->sessions);
  2085. X509_STORE_free(a->cert_store);
  2086. sk_SSL_CIPHER_free(a->cipher_list);
  2087. sk_SSL_CIPHER_free(a->cipher_list_by_id);
  2088. ssl_cert_free(a->cert);
  2089. sk_X509_NAME_pop_free(a->client_CA, X509_NAME_free);
  2090. sk_X509_pop_free(a->extra_certs, X509_free);
  2091. a->comp_methods = NULL;
  2092. #ifndef OPENSSL_NO_SRTP
  2093. sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
  2094. #endif
  2095. #ifndef OPENSSL_NO_SRP
  2096. SSL_CTX_SRP_CTX_free(a);
  2097. #endif
  2098. #ifndef OPENSSL_NO_ENGINE
  2099. if (a->client_cert_engine)
  2100. ENGINE_finish(a->client_cert_engine);
  2101. #endif
  2102. #ifndef OPENSSL_NO_EC
  2103. OPENSSL_free(a->tlsext_ecpointformatlist);
  2104. OPENSSL_free(a->tlsext_ellipticcurvelist);
  2105. #endif
  2106. OPENSSL_free(a->alpn_client_proto_list);
  2107. OPENSSL_free(a);
  2108. }
  2109. void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
  2110. {
  2111. ctx->default_passwd_callback = cb;
  2112. }
  2113. void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u)
  2114. {
  2115. ctx->default_passwd_callback_userdata = u;
  2116. }
  2117. void SSL_set_default_passwd_cb(SSL *s, pem_password_cb *cb)
  2118. {
  2119. s->default_passwd_callback = cb;
  2120. }
  2121. void SSL_set_default_passwd_cb_userdata(SSL *s, void *u)
  2122. {
  2123. s->default_passwd_callback_userdata = u;
  2124. }
  2125. void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
  2126. int (*cb) (X509_STORE_CTX *, void *),
  2127. void *arg)
  2128. {
  2129. ctx->app_verify_callback = cb;
  2130. ctx->app_verify_arg = arg;
  2131. }
  2132. void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
  2133. int (*cb) (int, X509_STORE_CTX *))
  2134. {
  2135. ctx->verify_mode = mode;
  2136. ctx->default_verify_callback = cb;
  2137. }
  2138. void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth)
  2139. {
  2140. X509_VERIFY_PARAM_set_depth(ctx->param, depth);
  2141. }
  2142. void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb) (SSL *ssl, void *arg),
  2143. void *arg)
  2144. {
  2145. ssl_cert_set_cert_cb(c->cert, cb, arg);
  2146. }
  2147. void SSL_set_cert_cb(SSL *s, int (*cb) (SSL *ssl, void *arg), void *arg)
  2148. {
  2149. ssl_cert_set_cert_cb(s->cert, cb, arg);
  2150. }
  2151. void ssl_set_masks(SSL *s, const SSL_CIPHER *cipher)
  2152. {
  2153. #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_GOST)
  2154. CERT_PKEY *cpk;
  2155. #endif
  2156. CERT *c = s->cert;
  2157. uint32_t *pvalid = s->s3->tmp.valid_flags;
  2158. int rsa_enc, rsa_sign, dh_tmp, dsa_sign;
  2159. unsigned long mask_k, mask_a;
  2160. #ifndef OPENSSL_NO_EC
  2161. int have_ecc_cert, ecdsa_ok;
  2162. int ecdh_ok;
  2163. X509 *x = NULL;
  2164. int pk_nid = 0, md_nid = 0;
  2165. #endif
  2166. if (c == NULL)
  2167. return;
  2168. #ifndef OPENSSL_NO_DH
  2169. dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL || c->dh_tmp_auto);
  2170. #else
  2171. dh_tmp = 0;
  2172. #endif
  2173. rsa_enc = pvalid[SSL_PKEY_RSA_ENC] & CERT_PKEY_VALID;
  2174. rsa_sign = pvalid[SSL_PKEY_RSA_SIGN] & CERT_PKEY_SIGN;
  2175. dsa_sign = pvalid[SSL_PKEY_DSA_SIGN] & CERT_PKEY_SIGN;
  2176. #ifndef OPENSSL_NO_EC
  2177. have_ecc_cert = pvalid[SSL_PKEY_ECC] & CERT_PKEY_VALID;
  2178. #endif
  2179. mask_k = 0;
  2180. mask_a = 0;
  2181. #ifdef CIPHER_DEBUG
  2182. fprintf(stderr,
  2183. "dht=%d re=%d rs=%d ds=%d dhr=%d dhd=%d\n",
  2184. dh_tmp, rsa_enc, rsa_sign, dsa_sign, dh_rsa, dh_dsa);
  2185. #endif
  2186. #ifndef OPENSSL_NO_GOST
  2187. cpk = &(c->pkeys[SSL_PKEY_GOST12_512]);
  2188. if (cpk->x509 != NULL && cpk->privatekey != NULL) {
  2189. mask_k |= SSL_kGOST;
  2190. mask_a |= SSL_aGOST12;
  2191. }
  2192. cpk = &(c->pkeys[SSL_PKEY_GOST12_256]);
  2193. if (cpk->x509 != NULL && cpk->privatekey != NULL) {
  2194. mask_k |= SSL_kGOST;
  2195. mask_a |= SSL_aGOST12;
  2196. }
  2197. cpk = &(c->pkeys[SSL_PKEY_GOST01]);
  2198. if (cpk->x509 != NULL && cpk->privatekey != NULL) {
  2199. mask_k |= SSL_kGOST;
  2200. mask_a |= SSL_aGOST01;
  2201. }
  2202. #endif
  2203. if (rsa_enc)
  2204. mask_k |= SSL_kRSA;
  2205. if (dh_tmp)
  2206. mask_k |= SSL_kDHE;
  2207. if (rsa_enc || rsa_sign) {
  2208. mask_a |= SSL_aRSA;
  2209. }
  2210. if (dsa_sign) {
  2211. mask_a |= SSL_aDSS;
  2212. }
  2213. mask_a |= SSL_aNULL;
  2214. /*
  2215. * An ECC certificate may be usable for ECDH and/or ECDSA cipher suites
  2216. * depending on the key usage extension.
  2217. */
  2218. #ifndef OPENSSL_NO_EC
  2219. if (have_ecc_cert) {
  2220. uint32_t ex_kusage;
  2221. cpk = &c->pkeys[SSL_PKEY_ECC];
  2222. x = cpk->x509;
  2223. ex_kusage = X509_get_key_usage(x);
  2224. ecdh_ok = ex_kusage & X509v3_KU_KEY_AGREEMENT;
  2225. ecdsa_ok = ex_kusage & X509v3_KU_DIGITAL_SIGNATURE;
  2226. if (!(pvalid[SSL_PKEY_ECC] & CERT_PKEY_SIGN))
  2227. ecdsa_ok = 0;
  2228. OBJ_find_sigid_algs(X509_get_signature_nid(x), &md_nid, &pk_nid);
  2229. if (ecdh_ok) {
  2230. if (pk_nid == NID_rsaEncryption || pk_nid == NID_rsa) {
  2231. mask_k |= SSL_kECDHr;
  2232. mask_a |= SSL_aECDH;
  2233. }
  2234. if (pk_nid == NID_X9_62_id_ecPublicKey) {
  2235. mask_k |= SSL_kECDHe;
  2236. mask_a |= SSL_aECDH;
  2237. }
  2238. }
  2239. if (ecdsa_ok) {
  2240. mask_a |= SSL_aECDSA;
  2241. }
  2242. }
  2243. #endif
  2244. #ifndef OPENSSL_NO_EC
  2245. mask_k |= SSL_kECDHE;
  2246. #endif
  2247. #ifndef OPENSSL_NO_PSK
  2248. mask_k |= SSL_kPSK;
  2249. mask_a |= SSL_aPSK;
  2250. if (mask_k & SSL_kRSA)
  2251. mask_k |= SSL_kRSAPSK;
  2252. if (mask_k & SSL_kDHE)
  2253. mask_k |= SSL_kDHEPSK;
  2254. if (mask_k & SSL_kECDHE)
  2255. mask_k |= SSL_kECDHEPSK;
  2256. #endif
  2257. s->s3->tmp.mask_k = mask_k;
  2258. s->s3->tmp.mask_a = mask_a;
  2259. }
  2260. #ifndef OPENSSL_NO_EC
  2261. int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s)
  2262. {
  2263. unsigned long alg_k, alg_a;
  2264. int md_nid = 0, pk_nid = 0;
  2265. const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
  2266. uint32_t ex_kusage = X509_get_key_usage(x);
  2267. alg_k = cs->algorithm_mkey;
  2268. alg_a = cs->algorithm_auth;
  2269. OBJ_find_sigid_algs(X509_get_signature_nid(x), &md_nid, &pk_nid);
  2270. if (alg_k & SSL_kECDHe || alg_k & SSL_kECDHr) {
  2271. /* key usage, if present, must allow key agreement */
  2272. if (!(ex_kusage & X509v3_KU_KEY_AGREEMENT)) {
  2273. SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG,
  2274. SSL_R_ECC_CERT_NOT_FOR_KEY_AGREEMENT);
  2275. return 0;
  2276. }
  2277. if ((alg_k & SSL_kECDHe) && TLS1_get_version(s) < TLS1_2_VERSION) {
  2278. /* signature alg must be ECDSA */
  2279. if (pk_nid != NID_X9_62_id_ecPublicKey) {
  2280. SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG,
  2281. SSL_R_ECC_CERT_SHOULD_HAVE_SHA1_SIGNATURE);
  2282. return 0;
  2283. }
  2284. }
  2285. if ((alg_k & SSL_kECDHr) && TLS1_get_version(s) < TLS1_2_VERSION) {
  2286. /* signature alg must be RSA */
  2287. if (pk_nid != NID_rsaEncryption && pk_nid != NID_rsa) {
  2288. SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG,
  2289. SSL_R_ECC_CERT_SHOULD_HAVE_RSA_SIGNATURE);
  2290. return 0;
  2291. }
  2292. }
  2293. }
  2294. if (alg_a & SSL_aECDSA) {
  2295. /* key usage, if present, must allow signing */
  2296. if (!(ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)) {
  2297. SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG,
  2298. SSL_R_ECC_CERT_NOT_FOR_SIGNING);
  2299. return 0;
  2300. }
  2301. }
  2302. return 1; /* all checks are ok */
  2303. }
  2304. #endif
  2305. static int ssl_get_server_cert_index(const SSL *s)
  2306. {
  2307. int idx;
  2308. idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
  2309. if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509)
  2310. idx = SSL_PKEY_RSA_SIGN;
  2311. if (idx == SSL_PKEY_GOST_EC) {
  2312. if (s->cert->pkeys[SSL_PKEY_GOST12_512].x509)
  2313. idx = SSL_PKEY_GOST12_512;
  2314. else if (s->cert->pkeys[SSL_PKEY_GOST12_256].x509)
  2315. idx = SSL_PKEY_GOST12_256;
  2316. else if (s->cert->pkeys[SSL_PKEY_GOST01].x509)
  2317. idx = SSL_PKEY_GOST01;
  2318. else
  2319. idx = -1;
  2320. }
  2321. if (idx == -1)
  2322. SSLerr(SSL_F_SSL_GET_SERVER_CERT_INDEX, ERR_R_INTERNAL_ERROR);
  2323. return idx;
  2324. }
  2325. CERT_PKEY *ssl_get_server_send_pkey(SSL *s)
  2326. {
  2327. CERT *c;
  2328. int i;
  2329. c = s->cert;
  2330. if (!s->s3 || !s->s3->tmp.new_cipher)
  2331. return NULL;
  2332. ssl_set_masks(s, s->s3->tmp.new_cipher);
  2333. #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
  2334. /*
  2335. * Broken protocol test: return last used certificate: which may mismatch
  2336. * the one expected.
  2337. */
  2338. if (c->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
  2339. return c->key;
  2340. #endif
  2341. i = ssl_get_server_cert_index(s);
  2342. /* This may or may not be an error. */
  2343. if (i < 0)
  2344. return NULL;
  2345. /* May be NULL. */
  2346. return &c->pkeys[i];
  2347. }
  2348. EVP_PKEY *ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher,
  2349. const EVP_MD **pmd)
  2350. {
  2351. unsigned long alg_a;
  2352. CERT *c;
  2353. int idx = -1;
  2354. alg_a = cipher->algorithm_auth;
  2355. c = s->cert;
  2356. #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
  2357. /*
  2358. * Broken protocol test: use last key: which may mismatch the one
  2359. * expected.
  2360. */
  2361. if (c->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
  2362. idx = c->key - c->pkeys;
  2363. else
  2364. #endif
  2365. if ((alg_a & SSL_aDSS) &&
  2366. (c->pkeys[SSL_PKEY_DSA_SIGN].privatekey != NULL))
  2367. idx = SSL_PKEY_DSA_SIGN;
  2368. else if (alg_a & SSL_aRSA) {
  2369. if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL)
  2370. idx = SSL_PKEY_RSA_SIGN;
  2371. else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL)
  2372. idx = SSL_PKEY_RSA_ENC;
  2373. } else if ((alg_a & SSL_aECDSA) &&
  2374. (c->pkeys[SSL_PKEY_ECC].privatekey != NULL))
  2375. idx = SSL_PKEY_ECC;
  2376. if (idx == -1) {
  2377. SSLerr(SSL_F_SSL_GET_SIGN_PKEY, ERR_R_INTERNAL_ERROR);
  2378. return (NULL);
  2379. }
  2380. if (pmd)
  2381. *pmd = s->s3->tmp.md[idx];
  2382. return c->pkeys[idx].privatekey;
  2383. }
  2384. int ssl_get_server_cert_serverinfo(SSL *s, const unsigned char **serverinfo,
  2385. size_t *serverinfo_length)
  2386. {
  2387. CERT *c = NULL;
  2388. int i = 0;
  2389. *serverinfo_length = 0;
  2390. c = s->cert;
  2391. i = ssl_get_server_cert_index(s);
  2392. if (i == -1)
  2393. return 0;
  2394. if (c->pkeys[i].serverinfo == NULL)
  2395. return 0;
  2396. *serverinfo = c->pkeys[i].serverinfo;
  2397. *serverinfo_length = c->pkeys[i].serverinfo_length;
  2398. return 1;
  2399. }
  2400. void ssl_update_cache(SSL *s, int mode)
  2401. {
  2402. int i;
  2403. /*
  2404. * If the session_id_length is 0, we are not supposed to cache it, and it
  2405. * would be rather hard to do anyway :-)
  2406. */
  2407. if (s->session->session_id_length == 0)
  2408. return;
  2409. i = s->session_ctx->session_cache_mode;
  2410. if ((i & mode) && (!s->hit)
  2411. && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE)
  2412. || SSL_CTX_add_session(s->session_ctx, s->session))
  2413. && (s->session_ctx->new_session_cb != NULL)) {
  2414. CRYPTO_add(&s->session->references, 1, CRYPTO_LOCK_SSL_SESSION);
  2415. if (!s->session_ctx->new_session_cb(s, s->session))
  2416. SSL_SESSION_free(s->session);
  2417. }
  2418. /* auto flush every 255 connections */
  2419. if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) && ((i & mode) == mode)) {
  2420. if ((((mode & SSL_SESS_CACHE_CLIENT)
  2421. ? s->session_ctx->stats.sess_connect_good
  2422. : s->session_ctx->stats.sess_accept_good) & 0xff) == 0xff) {
  2423. SSL_CTX_flush_sessions(s->session_ctx, (unsigned long)time(NULL));
  2424. }
  2425. }
  2426. }
  2427. const SSL_METHOD *SSL_CTX_get_ssl_method(SSL_CTX *ctx)
  2428. {
  2429. return ctx->method;
  2430. }
  2431. const SSL_METHOD *SSL_get_ssl_method(SSL *s)
  2432. {
  2433. return (s->method);
  2434. }
  2435. int SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth)
  2436. {
  2437. int ret = 1;
  2438. if (s->method != meth) {
  2439. const SSL_METHOD *sm = s->method;
  2440. int (*hf)(SSL *) = s->handshake_func;
  2441. if (sm->version == meth->version)
  2442. s->method = meth;
  2443. else {
  2444. sm->ssl_free(s);
  2445. s->method = meth;
  2446. ret = s->method->ssl_new(s);
  2447. }
  2448. if (hf == sm->ssl_connect)
  2449. s->handshake_func = meth->ssl_connect;
  2450. else if (hf == sm->ssl_accept)
  2451. s->handshake_func = meth->ssl_accept;
  2452. }
  2453. return (ret);
  2454. }
  2455. int SSL_get_error(const SSL *s, int i)
  2456. {
  2457. int reason;
  2458. unsigned long l;
  2459. BIO *bio;
  2460. if (i > 0)
  2461. return (SSL_ERROR_NONE);
  2462. /*
  2463. * Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
  2464. * where we do encode the error
  2465. */
  2466. if ((l = ERR_peek_error()) != 0) {
  2467. if (ERR_GET_LIB(l) == ERR_LIB_SYS)
  2468. return (SSL_ERROR_SYSCALL);
  2469. else
  2470. return (SSL_ERROR_SSL);
  2471. }
  2472. if ((i < 0) && SSL_want_read(s)) {
  2473. bio = SSL_get_rbio(s);
  2474. if (BIO_should_read(bio))
  2475. return (SSL_ERROR_WANT_READ);
  2476. else if (BIO_should_write(bio))
  2477. /*
  2478. * This one doesn't make too much sense ... We never try to write
  2479. * to the rbio, and an application program where rbio and wbio
  2480. * are separate couldn't even know what it should wait for.
  2481. * However if we ever set s->rwstate incorrectly (so that we have
  2482. * SSL_want_read(s) instead of SSL_want_write(s)) and rbio and
  2483. * wbio *are* the same, this test works around that bug; so it
  2484. * might be safer to keep it.
  2485. */
  2486. return (SSL_ERROR_WANT_WRITE);
  2487. else if (BIO_should_io_special(bio)) {
  2488. reason = BIO_get_retry_reason(bio);
  2489. if (reason == BIO_RR_CONNECT)
  2490. return (SSL_ERROR_WANT_CONNECT);
  2491. else if (reason == BIO_RR_ACCEPT)
  2492. return (SSL_ERROR_WANT_ACCEPT);
  2493. else
  2494. return (SSL_ERROR_SYSCALL); /* unknown */
  2495. }
  2496. }
  2497. if ((i < 0) && SSL_want_write(s)) {
  2498. bio = SSL_get_wbio(s);
  2499. if (BIO_should_write(bio))
  2500. return (SSL_ERROR_WANT_WRITE);
  2501. else if (BIO_should_read(bio))
  2502. /*
  2503. * See above (SSL_want_read(s) with BIO_should_write(bio))
  2504. */
  2505. return (SSL_ERROR_WANT_READ);
  2506. else if (BIO_should_io_special(bio)) {
  2507. reason = BIO_get_retry_reason(bio);
  2508. if (reason == BIO_RR_CONNECT)
  2509. return (SSL_ERROR_WANT_CONNECT);
  2510. else if (reason == BIO_RR_ACCEPT)
  2511. return (SSL_ERROR_WANT_ACCEPT);
  2512. else
  2513. return (SSL_ERROR_SYSCALL);
  2514. }
  2515. }
  2516. if ((i < 0) && SSL_want_x509_lookup(s)) {
  2517. return (SSL_ERROR_WANT_X509_LOOKUP);
  2518. }
  2519. if ((i < 0) && SSL_want_async(s)) {
  2520. return SSL_ERROR_WANT_ASYNC;
  2521. }
  2522. if (i == 0) {
  2523. if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
  2524. (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
  2525. return (SSL_ERROR_ZERO_RETURN);
  2526. }
  2527. return (SSL_ERROR_SYSCALL);
  2528. }
  2529. static int ssl_do_handshake_intern(void *vargs)
  2530. {
  2531. struct ssl_async_args *args;
  2532. SSL *s;
  2533. args = (struct ssl_async_args *)vargs;
  2534. s = args->s;
  2535. return s->handshake_func(s);
  2536. }
  2537. int SSL_do_handshake(SSL *s)
  2538. {
  2539. int ret = 1;
  2540. if (s->handshake_func == NULL) {
  2541. SSLerr(SSL_F_SSL_DO_HANDSHAKE, SSL_R_CONNECTION_TYPE_NOT_SET);
  2542. return -1;
  2543. }
  2544. s->method->ssl_renegotiate_check(s);
  2545. if (SSL_in_init(s) || SSL_in_before(s)) {
  2546. if((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
  2547. struct ssl_async_args args;
  2548. args.s = s;
  2549. ret = ssl_start_async_job(s, &args, ssl_do_handshake_intern);
  2550. } else {
  2551. ret = s->handshake_func(s);
  2552. }
  2553. }
  2554. return ret;
  2555. }
  2556. void SSL_set_accept_state(SSL *s)
  2557. {
  2558. s->server = 1;
  2559. s->shutdown = 0;
  2560. ossl_statem_clear(s);
  2561. s->handshake_func = s->method->ssl_accept;
  2562. clear_ciphers(s);
  2563. }
  2564. void SSL_set_connect_state(SSL *s)
  2565. {
  2566. s->server = 0;
  2567. s->shutdown = 0;
  2568. ossl_statem_clear(s);
  2569. s->handshake_func = s->method->ssl_connect;
  2570. clear_ciphers(s);
  2571. }
  2572. int ssl_undefined_function(SSL *s)
  2573. {
  2574. SSLerr(SSL_F_SSL_UNDEFINED_FUNCTION, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  2575. return (0);
  2576. }
  2577. int ssl_undefined_void_function(void)
  2578. {
  2579. SSLerr(SSL_F_SSL_UNDEFINED_VOID_FUNCTION,
  2580. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  2581. return (0);
  2582. }
  2583. int ssl_undefined_const_function(const SSL *s)
  2584. {
  2585. return (0);
  2586. }
  2587. SSL_METHOD *ssl_bad_method(int ver)
  2588. {
  2589. SSLerr(SSL_F_SSL_BAD_METHOD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  2590. return (NULL);
  2591. }
  2592. const char *SSL_get_version(const SSL *s)
  2593. {
  2594. if (s->version == TLS1_2_VERSION)
  2595. return ("TLSv1.2");
  2596. else if (s->version == TLS1_1_VERSION)
  2597. return ("TLSv1.1");
  2598. else if (s->version == TLS1_VERSION)
  2599. return ("TLSv1");
  2600. else if (s->version == SSL3_VERSION)
  2601. return ("SSLv3");
  2602. else if (s->version == DTLS1_BAD_VER)
  2603. return ("DTLSv0.9");
  2604. else if (s->version == DTLS1_VERSION)
  2605. return ("DTLSv1");
  2606. else if (s->version == DTLS1_2_VERSION)
  2607. return ("DTLSv1.2");
  2608. else
  2609. return ("unknown");
  2610. }
  2611. SSL *SSL_dup(SSL *s)
  2612. {
  2613. STACK_OF(X509_NAME) *sk;
  2614. X509_NAME *xn;
  2615. SSL *ret;
  2616. int i;
  2617. /* If we're not quiescent, just up_ref! */
  2618. if (!SSL_in_init(s) || !SSL_in_before(s)) {
  2619. CRYPTO_add(&s->references, 1, CRYPTO_LOCK_SSL);
  2620. return s;
  2621. }
  2622. /*
  2623. * Otherwise, copy configuration state, and session if set.
  2624. */
  2625. if ((ret = SSL_new(SSL_get_SSL_CTX(s))) == NULL)
  2626. return (NULL);
  2627. if (s->session != NULL) {
  2628. /*
  2629. * Arranges to share the same session via up_ref. This "copies"
  2630. * session-id, SSL_METHOD, sid_ctx, and 'cert'
  2631. */
  2632. if (!SSL_copy_session_id(ret, s))
  2633. goto err;
  2634. } else {
  2635. /*
  2636. * No session has been established yet, so we have to expect that
  2637. * s->cert or ret->cert will be changed later -- they should not both
  2638. * point to the same object, and thus we can't use
  2639. * SSL_copy_session_id.
  2640. */
  2641. if (!SSL_set_ssl_method(ret, s->method))
  2642. goto err;
  2643. if (s->cert != NULL) {
  2644. ssl_cert_free(ret->cert);
  2645. ret->cert = ssl_cert_dup(s->cert);
  2646. if (ret->cert == NULL)
  2647. goto err;
  2648. }
  2649. if (!SSL_set_session_id_context(ret, s->sid_ctx, s->sid_ctx_length))
  2650. goto err;
  2651. }
  2652. ssl_dane_dup(ret, s);
  2653. ret->version = s->version;
  2654. ret->options = s->options;
  2655. ret->mode = s->mode;
  2656. SSL_set_max_cert_list(ret, SSL_get_max_cert_list(s));
  2657. SSL_set_read_ahead(ret, SSL_get_read_ahead(s));
  2658. ret->msg_callback = s->msg_callback;
  2659. ret->msg_callback_arg = s->msg_callback_arg;
  2660. SSL_set_verify(ret, SSL_get_verify_mode(s), SSL_get_verify_callback(s));
  2661. SSL_set_verify_depth(ret, SSL_get_verify_depth(s));
  2662. ret->generate_session_id = s->generate_session_id;
  2663. SSL_set_info_callback(ret, SSL_get_info_callback(s));
  2664. /* copy app data, a little dangerous perhaps */
  2665. if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL, &ret->ex_data, &s->ex_data))
  2666. goto err;
  2667. /* setup rbio, and wbio */
  2668. if (s->rbio != NULL) {
  2669. if (!BIO_dup_state(s->rbio, (char *)&ret->rbio))
  2670. goto err;
  2671. }
  2672. if (s->wbio != NULL) {
  2673. if (s->wbio != s->rbio) {
  2674. if (!BIO_dup_state(s->wbio, (char *)&ret->wbio))
  2675. goto err;
  2676. } else
  2677. ret->wbio = ret->rbio;
  2678. }
  2679. ret->server = s->server;
  2680. if (s->handshake_func) {
  2681. if (s->server)
  2682. SSL_set_accept_state(ret);
  2683. else
  2684. SSL_set_connect_state(ret);
  2685. }
  2686. ret->shutdown = s->shutdown;
  2687. ret->hit = s->hit;
  2688. ret->default_passwd_callback = s->default_passwd_callback;
  2689. ret->default_passwd_callback_userdata = s->default_passwd_callback_userdata;
  2690. X509_VERIFY_PARAM_inherit(ret->param, s->param);
  2691. /* dup the cipher_list and cipher_list_by_id stacks */
  2692. if (s->cipher_list != NULL) {
  2693. if ((ret->cipher_list = sk_SSL_CIPHER_dup(s->cipher_list)) == NULL)
  2694. goto err;
  2695. }
  2696. if (s->cipher_list_by_id != NULL)
  2697. if ((ret->cipher_list_by_id = sk_SSL_CIPHER_dup(s->cipher_list_by_id))
  2698. == NULL)
  2699. goto err;
  2700. /* Dup the client_CA list */
  2701. if (s->client_CA != NULL) {
  2702. if ((sk = sk_X509_NAME_dup(s->client_CA)) == NULL)
  2703. goto err;
  2704. ret->client_CA = sk;
  2705. for (i = 0; i < sk_X509_NAME_num(sk); i++) {
  2706. xn = sk_X509_NAME_value(sk, i);
  2707. if (sk_X509_NAME_set(sk, i, X509_NAME_dup(xn)) == NULL) {
  2708. X509_NAME_free(xn);
  2709. goto err;
  2710. }
  2711. }
  2712. }
  2713. return ret;
  2714. err:
  2715. SSL_free(ret);
  2716. return NULL;
  2717. }
  2718. void ssl_clear_cipher_ctx(SSL *s)
  2719. {
  2720. if (s->enc_read_ctx != NULL) {
  2721. EVP_CIPHER_CTX_free(s->enc_read_ctx);
  2722. s->enc_read_ctx = NULL;
  2723. }
  2724. if (s->enc_write_ctx != NULL) {
  2725. EVP_CIPHER_CTX_free(s->enc_write_ctx);
  2726. s->enc_write_ctx = NULL;
  2727. }
  2728. #ifndef OPENSSL_NO_COMP
  2729. COMP_CTX_free(s->expand);
  2730. s->expand = NULL;
  2731. COMP_CTX_free(s->compress);
  2732. s->compress = NULL;
  2733. #endif
  2734. }
  2735. X509 *SSL_get_certificate(const SSL *s)
  2736. {
  2737. if (s->cert != NULL)
  2738. return (s->cert->key->x509);
  2739. else
  2740. return (NULL);
  2741. }
  2742. EVP_PKEY *SSL_get_privatekey(const SSL *s)
  2743. {
  2744. if (s->cert != NULL)
  2745. return (s->cert->key->privatekey);
  2746. else
  2747. return (NULL);
  2748. }
  2749. X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx)
  2750. {
  2751. if (ctx->cert != NULL)
  2752. return ctx->cert->key->x509;
  2753. else
  2754. return NULL;
  2755. }
  2756. EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx)
  2757. {
  2758. if (ctx->cert != NULL)
  2759. return ctx->cert->key->privatekey;
  2760. else
  2761. return NULL;
  2762. }
  2763. const SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
  2764. {
  2765. if ((s->session != NULL) && (s->session->cipher != NULL))
  2766. return (s->session->cipher);
  2767. return (NULL);
  2768. }
  2769. const COMP_METHOD *SSL_get_current_compression(SSL *s)
  2770. {
  2771. #ifndef OPENSSL_NO_COMP
  2772. return s->compress ? COMP_CTX_get_method(s->compress) : NULL;
  2773. #else
  2774. return NULL;
  2775. #endif
  2776. }
  2777. const COMP_METHOD *SSL_get_current_expansion(SSL *s)
  2778. {
  2779. #ifndef OPENSSL_NO_COMP
  2780. return s->expand ? COMP_CTX_get_method(s->expand) : NULL;
  2781. #else
  2782. return NULL;
  2783. #endif
  2784. }
  2785. int ssl_init_wbio_buffer(SSL *s, int push)
  2786. {
  2787. BIO *bbio;
  2788. if (s->bbio == NULL) {
  2789. bbio = BIO_new(BIO_f_buffer());
  2790. if (bbio == NULL)
  2791. return (0);
  2792. s->bbio = bbio;
  2793. } else {
  2794. bbio = s->bbio;
  2795. if (s->bbio == s->wbio)
  2796. s->wbio = BIO_pop(s->wbio);
  2797. }
  2798. (void)BIO_reset(bbio);
  2799. /* if (!BIO_set_write_buffer_size(bbio,16*1024)) */
  2800. if (!BIO_set_read_buffer_size(bbio, 1)) {
  2801. SSLerr(SSL_F_SSL_INIT_WBIO_BUFFER, ERR_R_BUF_LIB);
  2802. return (0);
  2803. }
  2804. if (push) {
  2805. if (s->wbio != bbio)
  2806. s->wbio = BIO_push(bbio, s->wbio);
  2807. } else {
  2808. if (s->wbio == bbio)
  2809. s->wbio = BIO_pop(bbio);
  2810. }
  2811. return (1);
  2812. }
  2813. void ssl_free_wbio_buffer(SSL *s)
  2814. {
  2815. /* callers ensure s is never null */
  2816. if (s->bbio == NULL)
  2817. return;
  2818. if (s->bbio == s->wbio) {
  2819. /* remove buffering */
  2820. s->wbio = BIO_pop(s->wbio);
  2821. #ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids
  2822. * adding one more preprocessor symbol */
  2823. assert(s->wbio != NULL);
  2824. #endif
  2825. }
  2826. BIO_free(s->bbio);
  2827. s->bbio = NULL;
  2828. }
  2829. void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode)
  2830. {
  2831. ctx->quiet_shutdown = mode;
  2832. }
  2833. int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
  2834. {
  2835. return (ctx->quiet_shutdown);
  2836. }
  2837. void SSL_set_quiet_shutdown(SSL *s, int mode)
  2838. {
  2839. s->quiet_shutdown = mode;
  2840. }
  2841. int SSL_get_quiet_shutdown(const SSL *s)
  2842. {
  2843. return (s->quiet_shutdown);
  2844. }
  2845. void SSL_set_shutdown(SSL *s, int mode)
  2846. {
  2847. s->shutdown = mode;
  2848. }
  2849. int SSL_get_shutdown(const SSL *s)
  2850. {
  2851. return (s->shutdown);
  2852. }
  2853. int SSL_version(const SSL *s)
  2854. {
  2855. return (s->version);
  2856. }
  2857. SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
  2858. {
  2859. return (ssl->ctx);
  2860. }
  2861. SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx)
  2862. {
  2863. CERT *new_cert;
  2864. if (ssl->ctx == ctx)
  2865. return ssl->ctx;
  2866. if (ctx == NULL)
  2867. ctx = ssl->initial_ctx;
  2868. new_cert = ssl_cert_dup(ctx->cert);
  2869. if (new_cert == NULL) {
  2870. return NULL;
  2871. }
  2872. ssl_cert_free(ssl->cert);
  2873. ssl->cert = new_cert;
  2874. /*
  2875. * Program invariant: |sid_ctx| has fixed size (SSL_MAX_SID_CTX_LENGTH),
  2876. * so setter APIs must prevent invalid lengths from entering the system.
  2877. */
  2878. OPENSSL_assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
  2879. /*
  2880. * If the session ID context matches that of the parent SSL_CTX,
  2881. * inherit it from the new SSL_CTX as well. If however the context does
  2882. * not match (i.e., it was set per-ssl with SSL_set_session_id_context),
  2883. * leave it unchanged.
  2884. */
  2885. if ((ssl->ctx != NULL) &&
  2886. (ssl->sid_ctx_length == ssl->ctx->sid_ctx_length) &&
  2887. (memcmp(ssl->sid_ctx, ssl->ctx->sid_ctx, ssl->sid_ctx_length) == 0)) {
  2888. ssl->sid_ctx_length = ctx->sid_ctx_length;
  2889. memcpy(&ssl->sid_ctx, &ctx->sid_ctx, sizeof(ssl->sid_ctx));
  2890. }
  2891. CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
  2892. SSL_CTX_free(ssl->ctx); /* decrement reference count */
  2893. ssl->ctx = ctx;
  2894. return (ssl->ctx);
  2895. }
  2896. int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
  2897. {
  2898. return (X509_STORE_set_default_paths(ctx->cert_store));
  2899. }
  2900. int SSL_CTX_set_default_verify_dir(SSL_CTX *ctx)
  2901. {
  2902. X509_LOOKUP *lookup;
  2903. lookup = X509_STORE_add_lookup(ctx->cert_store, X509_LOOKUP_hash_dir());
  2904. if (lookup == NULL)
  2905. return 0;
  2906. X509_LOOKUP_add_dir(lookup, NULL, X509_FILETYPE_DEFAULT);
  2907. /* Clear any errors if the default directory does not exist */
  2908. ERR_clear_error();
  2909. return 1;
  2910. }
  2911. int SSL_CTX_set_default_verify_file(SSL_CTX *ctx)
  2912. {
  2913. X509_LOOKUP *lookup;
  2914. lookup = X509_STORE_add_lookup(ctx->cert_store, X509_LOOKUP_file());
  2915. if (lookup == NULL)
  2916. return 0;
  2917. X509_LOOKUP_load_file(lookup, NULL, X509_FILETYPE_DEFAULT);
  2918. /* Clear any errors if the default file does not exist */
  2919. ERR_clear_error();
  2920. return 1;
  2921. }
  2922. int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
  2923. const char *CApath)
  2924. {
  2925. return (X509_STORE_load_locations(ctx->cert_store, CAfile, CApath));
  2926. }
  2927. void SSL_set_info_callback(SSL *ssl,
  2928. void (*cb) (const SSL *ssl, int type, int val))
  2929. {
  2930. ssl->info_callback = cb;
  2931. }
  2932. /*
  2933. * One compiler (Diab DCC) doesn't like argument names in returned function
  2934. * pointer.
  2935. */
  2936. void (*SSL_get_info_callback(const SSL *ssl)) (const SSL * /* ssl */ ,
  2937. int /* type */ ,
  2938. int /* val */ ) {
  2939. return ssl->info_callback;
  2940. }
  2941. void SSL_set_verify_result(SSL *ssl, long arg)
  2942. {
  2943. ssl->verify_result = arg;
  2944. }
  2945. long SSL_get_verify_result(const SSL *ssl)
  2946. {
  2947. return (ssl->verify_result);
  2948. }
  2949. size_t SSL_get_client_random(const SSL *ssl, unsigned char *out, size_t outlen)
  2950. {
  2951. if (outlen == 0)
  2952. return sizeof(ssl->s3->client_random);
  2953. if (outlen > sizeof(ssl->s3->client_random))
  2954. outlen = sizeof(ssl->s3->client_random);
  2955. memcpy(out, ssl->s3->client_random, outlen);
  2956. return outlen;
  2957. }
  2958. size_t SSL_get_server_random(const SSL *ssl, unsigned char *out, size_t outlen)
  2959. {
  2960. if (outlen == 0)
  2961. return sizeof(ssl->s3->server_random);
  2962. if (outlen > sizeof(ssl->s3->server_random))
  2963. outlen = sizeof(ssl->s3->server_random);
  2964. memcpy(out, ssl->s3->server_random, outlen);
  2965. return outlen;
  2966. }
  2967. size_t SSL_SESSION_get_master_key(const SSL_SESSION *session,
  2968. unsigned char *out, size_t outlen)
  2969. {
  2970. if (session->master_key_length < 0) {
  2971. /* Should never happen */
  2972. return 0;
  2973. }
  2974. if (outlen == 0)
  2975. return session->master_key_length;
  2976. if (outlen > (size_t)session->master_key_length)
  2977. outlen = session->master_key_length;
  2978. memcpy(out, session->master_key, outlen);
  2979. return outlen;
  2980. }
  2981. int SSL_set_ex_data(SSL *s, int idx, void *arg)
  2982. {
  2983. return (CRYPTO_set_ex_data(&s->ex_data, idx, arg));
  2984. }
  2985. void *SSL_get_ex_data(const SSL *s, int idx)
  2986. {
  2987. return (CRYPTO_get_ex_data(&s->ex_data, idx));
  2988. }
  2989. int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg)
  2990. {
  2991. return (CRYPTO_set_ex_data(&s->ex_data, idx, arg));
  2992. }
  2993. void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx)
  2994. {
  2995. return (CRYPTO_get_ex_data(&s->ex_data, idx));
  2996. }
  2997. int ssl_ok(SSL *s)
  2998. {
  2999. return (1);
  3000. }
  3001. X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
  3002. {
  3003. return (ctx->cert_store);
  3004. }
  3005. void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store)
  3006. {
  3007. X509_STORE_free(ctx->cert_store);
  3008. ctx->cert_store = store;
  3009. }
  3010. int SSL_want(const SSL *s)
  3011. {
  3012. return (s->rwstate);
  3013. }
  3014. /**
  3015. * \brief Set the callback for generating temporary DH keys.
  3016. * \param ctx the SSL context.
  3017. * \param dh the callback
  3018. */
  3019. #ifndef OPENSSL_NO_DH
  3020. void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
  3021. DH *(*dh) (SSL *ssl, int is_export,
  3022. int keylength))
  3023. {
  3024. SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
  3025. }
  3026. void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*dh) (SSL *ssl, int is_export,
  3027. int keylength))
  3028. {
  3029. SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
  3030. }
  3031. #endif
  3032. #ifndef OPENSSL_NO_PSK
  3033. int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint)
  3034. {
  3035. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
  3036. SSLerr(SSL_F_SSL_CTX_USE_PSK_IDENTITY_HINT,
  3037. SSL_R_DATA_LENGTH_TOO_LONG);
  3038. return 0;
  3039. }
  3040. OPENSSL_free(ctx->cert->psk_identity_hint);
  3041. if (identity_hint != NULL) {
  3042. ctx->cert->psk_identity_hint = OPENSSL_strdup(identity_hint);
  3043. if (ctx->cert->psk_identity_hint == NULL)
  3044. return 0;
  3045. } else
  3046. ctx->cert->psk_identity_hint = NULL;
  3047. return 1;
  3048. }
  3049. int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint)
  3050. {
  3051. if (s == NULL)
  3052. return 0;
  3053. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
  3054. SSLerr(SSL_F_SSL_USE_PSK_IDENTITY_HINT, SSL_R_DATA_LENGTH_TOO_LONG);
  3055. return 0;
  3056. }
  3057. OPENSSL_free(s->cert->psk_identity_hint);
  3058. if (identity_hint != NULL) {
  3059. s->cert->psk_identity_hint = OPENSSL_strdup(identity_hint);
  3060. if (s->cert->psk_identity_hint == NULL)
  3061. return 0;
  3062. } else
  3063. s->cert->psk_identity_hint = NULL;
  3064. return 1;
  3065. }
  3066. const char *SSL_get_psk_identity_hint(const SSL *s)
  3067. {
  3068. if (s == NULL || s->session == NULL)
  3069. return NULL;
  3070. return (s->session->psk_identity_hint);
  3071. }
  3072. const char *SSL_get_psk_identity(const SSL *s)
  3073. {
  3074. if (s == NULL || s->session == NULL)
  3075. return NULL;
  3076. return (s->session->psk_identity);
  3077. }
  3078. void SSL_set_psk_client_callback(SSL *s,
  3079. unsigned int (*cb) (SSL *ssl,
  3080. const char *hint,
  3081. char *identity,
  3082. unsigned int
  3083. max_identity_len,
  3084. unsigned char *psk,
  3085. unsigned int
  3086. max_psk_len))
  3087. {
  3088. s->psk_client_callback = cb;
  3089. }
  3090. void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx,
  3091. unsigned int (*cb) (SSL *ssl,
  3092. const char *hint,
  3093. char *identity,
  3094. unsigned int
  3095. max_identity_len,
  3096. unsigned char *psk,
  3097. unsigned int
  3098. max_psk_len))
  3099. {
  3100. ctx->psk_client_callback = cb;
  3101. }
  3102. void SSL_set_psk_server_callback(SSL *s,
  3103. unsigned int (*cb) (SSL *ssl,
  3104. const char *identity,
  3105. unsigned char *psk,
  3106. unsigned int
  3107. max_psk_len))
  3108. {
  3109. s->psk_server_callback = cb;
  3110. }
  3111. void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx,
  3112. unsigned int (*cb) (SSL *ssl,
  3113. const char *identity,
  3114. unsigned char *psk,
  3115. unsigned int
  3116. max_psk_len))
  3117. {
  3118. ctx->psk_server_callback = cb;
  3119. }
  3120. #endif
  3121. void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
  3122. void (*cb) (int write_p, int version,
  3123. int content_type, const void *buf,
  3124. size_t len, SSL *ssl, void *arg))
  3125. {
  3126. SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
  3127. }
  3128. void SSL_set_msg_callback(SSL *ssl,
  3129. void (*cb) (int write_p, int version,
  3130. int content_type, const void *buf,
  3131. size_t len, SSL *ssl, void *arg))
  3132. {
  3133. SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
  3134. }
  3135. void SSL_CTX_set_not_resumable_session_callback(SSL_CTX *ctx,
  3136. int (*cb) (SSL *ssl,
  3137. int
  3138. is_forward_secure))
  3139. {
  3140. SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_NOT_RESUMABLE_SESS_CB,
  3141. (void (*)(void))cb);
  3142. }
  3143. void SSL_set_not_resumable_session_callback(SSL *ssl,
  3144. int (*cb) (SSL *ssl,
  3145. int is_forward_secure))
  3146. {
  3147. SSL_callback_ctrl(ssl, SSL_CTRL_SET_NOT_RESUMABLE_SESS_CB,
  3148. (void (*)(void))cb);
  3149. }
  3150. /*
  3151. * Allocates new EVP_MD_CTX and sets pointer to it into given pointer
  3152. * vairable, freeing EVP_MD_CTX previously stored in that variable, if any.
  3153. * If EVP_MD pointer is passed, initializes ctx with this md Returns newly
  3154. * allocated ctx;
  3155. */
  3156. EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash, const EVP_MD *md)
  3157. {
  3158. ssl_clear_hash_ctx(hash);
  3159. *hash = EVP_MD_CTX_new();
  3160. if (*hash == NULL || (md && EVP_DigestInit_ex(*hash, md, NULL) <= 0)) {
  3161. EVP_MD_CTX_free(*hash);
  3162. *hash = NULL;
  3163. return NULL;
  3164. }
  3165. return *hash;
  3166. }
  3167. void ssl_clear_hash_ctx(EVP_MD_CTX **hash)
  3168. {
  3169. if (*hash)
  3170. EVP_MD_CTX_free(*hash);
  3171. *hash = NULL;
  3172. }
  3173. /* Retrieve handshake hashes */
  3174. int ssl_handshake_hash(SSL *s, unsigned char *out, int outlen)
  3175. {
  3176. EVP_MD_CTX *ctx = NULL;
  3177. EVP_MD_CTX *hdgst = s->s3->handshake_dgst;
  3178. int ret = EVP_MD_CTX_size(hdgst);
  3179. if (ret < 0 || ret > outlen) {
  3180. ret = 0;
  3181. goto err;
  3182. }
  3183. ctx = EVP_MD_CTX_new();
  3184. if (ctx == NULL) {
  3185. ret = 0;
  3186. goto err;
  3187. }
  3188. if (!EVP_MD_CTX_copy_ex(ctx, hdgst)
  3189. || EVP_DigestFinal_ex(ctx, out, NULL) <= 0)
  3190. ret = 0;
  3191. err:
  3192. EVP_MD_CTX_free(ctx);
  3193. return ret;
  3194. }
  3195. int SSL_cache_hit(SSL *s)
  3196. {
  3197. return s->hit;
  3198. }
  3199. int SSL_is_server(SSL *s)
  3200. {
  3201. return s->server;
  3202. }
  3203. #if OPENSSL_API_COMPAT < 0x10100000L
  3204. void SSL_set_debug(SSL *s, int debug)
  3205. {
  3206. /* Old function was do-nothing anyway... */
  3207. (void)s;
  3208. (void)debug;
  3209. }
  3210. #endif
  3211. void SSL_set_security_level(SSL *s, int level)
  3212. {
  3213. s->cert->sec_level = level;
  3214. }
  3215. int SSL_get_security_level(const SSL *s)
  3216. {
  3217. return s->cert->sec_level;
  3218. }
  3219. void SSL_set_security_callback(SSL *s,
  3220. int (*cb) (SSL *s, SSL_CTX *ctx, int op,
  3221. int bits, int nid, void *other,
  3222. void *ex))
  3223. {
  3224. s->cert->sec_cb = cb;
  3225. }
  3226. int (*SSL_get_security_callback(const SSL *s)) (SSL *s, SSL_CTX *ctx, int op,
  3227. int bits, int nid,
  3228. void *other, void *ex) {
  3229. return s->cert->sec_cb;
  3230. }
  3231. void SSL_set0_security_ex_data(SSL *s, void *ex)
  3232. {
  3233. s->cert->sec_ex = ex;
  3234. }
  3235. void *SSL_get0_security_ex_data(const SSL *s)
  3236. {
  3237. return s->cert->sec_ex;
  3238. }
  3239. void SSL_CTX_set_security_level(SSL_CTX *ctx, int level)
  3240. {
  3241. ctx->cert->sec_level = level;
  3242. }
  3243. int SSL_CTX_get_security_level(const SSL_CTX *ctx)
  3244. {
  3245. return ctx->cert->sec_level;
  3246. }
  3247. void SSL_CTX_set_security_callback(SSL_CTX *ctx,
  3248. int (*cb) (SSL *s, SSL_CTX *ctx, int op,
  3249. int bits, int nid, void *other,
  3250. void *ex))
  3251. {
  3252. ctx->cert->sec_cb = cb;
  3253. }
  3254. int (*SSL_CTX_get_security_callback(const SSL_CTX *ctx)) (SSL *s,
  3255. SSL_CTX *ctx,
  3256. int op, int bits,
  3257. int nid,
  3258. void *other,
  3259. void *ex) {
  3260. return ctx->cert->sec_cb;
  3261. }
  3262. void SSL_CTX_set0_security_ex_data(SSL_CTX *ctx, void *ex)
  3263. {
  3264. ctx->cert->sec_ex = ex;
  3265. }
  3266. void *SSL_CTX_get0_security_ex_data(const SSL_CTX *ctx)
  3267. {
  3268. return ctx->cert->sec_ex;
  3269. }
  3270. /*
  3271. * Get/Set/Clear options in SSL_CTX or SSL, formerly macros, now functions that
  3272. * can return unsigned long, instead of the generic long return value from the
  3273. * control interface.
  3274. */
  3275. unsigned long SSL_CTX_get_options(const SSL_CTX *ctx)
  3276. {
  3277. return ctx->options;
  3278. }
  3279. unsigned long SSL_get_options(const SSL* s)
  3280. {
  3281. return s->options;
  3282. }
  3283. unsigned long SSL_CTX_set_options(SSL_CTX *ctx, unsigned long op)
  3284. {
  3285. return ctx->options |= op;
  3286. }
  3287. unsigned long SSL_set_options(SSL *s, unsigned long op)
  3288. {
  3289. return s->options |= op;
  3290. }
  3291. unsigned long SSL_CTX_clear_options(SSL_CTX *ctx, unsigned long op)
  3292. {
  3293. return ctx->options &= ~op;
  3294. }
  3295. unsigned long SSL_clear_options(SSL *s, unsigned long op)
  3296. {
  3297. return s->options &= ~op;
  3298. }
  3299. IMPLEMENT_OBJ_BSEARCH_GLOBAL_CMP_FN(SSL_CIPHER, SSL_CIPHER, ssl_cipher_id);