ssl_lib.c 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269
  1. /*! \file ssl/ssl_lib.c
  2. * \brief Version independent SSL functions.
  3. */
  4. /* Copyright (C) 1995-1998 Eric Young ([email protected])
  5. * All rights reserved.
  6. *
  7. * This package is an SSL implementation written
  8. * by Eric Young ([email protected]).
  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 ([email protected]).
  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 ([email protected])"
  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 ([email protected])"
  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. * [email protected].
  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. * ([email protected]). This product includes software written by Tim
  110. * Hudson ([email protected]).
  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 "kssl_lcl.h"
  150. #include <openssl/objects.h>
  151. #include <openssl/lhash.h>
  152. #include <openssl/x509v3.h>
  153. #include <openssl/rand.h>
  154. #include <openssl/ocsp.h>
  155. #ifndef OPENSSL_NO_DH
  156. #include <openssl/dh.h>
  157. #endif
  158. #ifndef OPENSSL_NO_ENGINE
  159. #include <openssl/engine.h>
  160. #endif
  161. const char *SSL_version_str=OPENSSL_VERSION_TEXT;
  162. SSL3_ENC_METHOD ssl3_undef_enc_method={
  163. /* evil casts, but these functions are only called if there's a library bug */
  164. (int (*)(SSL *,int))ssl_undefined_function,
  165. (int (*)(SSL *, unsigned char *, int))ssl_undefined_function,
  166. ssl_undefined_function,
  167. (int (*)(SSL *, unsigned char *, unsigned char *, int))ssl_undefined_function,
  168. (int (*)(SSL*, int))ssl_undefined_function,
  169. (int (*)(SSL *, const char*, int, unsigned char *))ssl_undefined_function,
  170. 0, /* finish_mac_length */
  171. (int (*)(SSL *, int, unsigned char *))ssl_undefined_function,
  172. NULL, /* client_finished_label */
  173. 0, /* client_finished_label_len */
  174. NULL, /* server_finished_label */
  175. 0, /* server_finished_label_len */
  176. (int (*)(int))ssl_undefined_function,
  177. (int (*)(SSL *, unsigned char *, size_t, const char *,
  178. size_t, const unsigned char *, size_t,
  179. int use_context)) ssl_undefined_function,
  180. };
  181. int SSL_clear(SSL *s)
  182. {
  183. if (s->method == NULL)
  184. {
  185. SSLerr(SSL_F_SSL_CLEAR,SSL_R_NO_METHOD_SPECIFIED);
  186. return(0);
  187. }
  188. if (ssl_clear_bad_session(s))
  189. {
  190. SSL_SESSION_free(s->session);
  191. s->session=NULL;
  192. }
  193. s->error=0;
  194. s->hit=0;
  195. s->shutdown=0;
  196. #if 0 /* Disabled since version 1.10 of this file (early return not
  197. * needed because SSL_clear is not called when doing renegotiation) */
  198. /* This is set if we are doing dynamic renegotiation so keep
  199. * the old cipher. It is sort of a SSL_clear_lite :-) */
  200. if (s->renegotiate) return(1);
  201. #else
  202. if (s->renegotiate)
  203. {
  204. SSLerr(SSL_F_SSL_CLEAR,ERR_R_INTERNAL_ERROR);
  205. return 0;
  206. }
  207. #endif
  208. s->type=0;
  209. s->state=SSL_ST_BEFORE|((s->server)?SSL_ST_ACCEPT:SSL_ST_CONNECT);
  210. s->version=s->method->version;
  211. s->client_version=s->version;
  212. s->rwstate=SSL_NOTHING;
  213. s->rstate=SSL_ST_READ_HEADER;
  214. #if 0
  215. s->read_ahead=s->ctx->read_ahead;
  216. #endif
  217. if (s->init_buf != NULL)
  218. {
  219. BUF_MEM_free(s->init_buf);
  220. s->init_buf=NULL;
  221. }
  222. ssl_clear_cipher_ctx(s);
  223. ssl_clear_hash_ctx(&s->read_hash);
  224. ssl_clear_hash_ctx(&s->write_hash);
  225. s->first_packet=0;
  226. #if 1
  227. /* Check to see if we were changed into a different method, if
  228. * so, revert back if we are not doing session-id reuse. */
  229. if (!s->in_handshake && (s->session == NULL) && (s->method != s->ctx->method))
  230. {
  231. s->method->ssl_free(s);
  232. s->method=s->ctx->method;
  233. if (!s->method->ssl_new(s))
  234. return(0);
  235. }
  236. else
  237. #endif
  238. s->method->ssl_clear(s);
  239. return(1);
  240. }
  241. /** Used to change an SSL_CTXs default SSL method type */
  242. int SSL_CTX_set_ssl_version(SSL_CTX *ctx,const SSL_METHOD *meth)
  243. {
  244. STACK_OF(SSL_CIPHER) *sk;
  245. ctx->method=meth;
  246. sk=ssl_create_cipher_list(ctx->method,&(ctx->cipher_list),
  247. &(ctx->cipher_list_by_id),
  248. meth->version == SSL2_VERSION ? "SSLv2" : SSL_DEFAULT_CIPHER_LIST);
  249. if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0))
  250. {
  251. SSLerr(SSL_F_SSL_CTX_SET_SSL_VERSION,SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
  252. return(0);
  253. }
  254. return(1);
  255. }
  256. SSL *SSL_new(SSL_CTX *ctx)
  257. {
  258. SSL *s;
  259. if (ctx == NULL)
  260. {
  261. SSLerr(SSL_F_SSL_NEW,SSL_R_NULL_SSL_CTX);
  262. return(NULL);
  263. }
  264. if (ctx->method == NULL)
  265. {
  266. SSLerr(SSL_F_SSL_NEW,SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
  267. return(NULL);
  268. }
  269. s=(SSL *)OPENSSL_malloc(sizeof(SSL));
  270. if (s == NULL) goto err;
  271. memset(s,0,sizeof(SSL));
  272. #ifndef OPENSSL_NO_KRB5
  273. s->kssl_ctx = kssl_ctx_new();
  274. #endif /* OPENSSL_NO_KRB5 */
  275. s->options=ctx->options;
  276. s->mode=ctx->mode;
  277. s->max_cert_list=ctx->max_cert_list;
  278. if (ctx->cert != NULL)
  279. {
  280. /* Earlier library versions used to copy the pointer to
  281. * the CERT, not its contents; only when setting new
  282. * parameters for the per-SSL copy, ssl_cert_new would be
  283. * called (and the direct reference to the per-SSL_CTX
  284. * settings would be lost, but those still were indirectly
  285. * accessed for various purposes, and for that reason they
  286. * used to be known as s->ctx->default_cert).
  287. * Now we don't look at the SSL_CTX's CERT after having
  288. * duplicated it once. */
  289. s->cert = ssl_cert_dup(ctx->cert);
  290. if (s->cert == NULL)
  291. goto err;
  292. }
  293. else
  294. s->cert=NULL; /* Cannot really happen (see SSL_CTX_new) */
  295. s->read_ahead=ctx->read_ahead;
  296. s->msg_callback=ctx->msg_callback;
  297. s->msg_callback_arg=ctx->msg_callback_arg;
  298. s->verify_mode=ctx->verify_mode;
  299. #if 0
  300. s->verify_depth=ctx->verify_depth;
  301. #endif
  302. s->sid_ctx_length=ctx->sid_ctx_length;
  303. OPENSSL_assert(s->sid_ctx_length <= sizeof s->sid_ctx);
  304. memcpy(&s->sid_ctx,&ctx->sid_ctx,sizeof(s->sid_ctx));
  305. s->verify_callback=ctx->default_verify_callback;
  306. s->generate_session_id=ctx->generate_session_id;
  307. s->param = X509_VERIFY_PARAM_new();
  308. if (!s->param)
  309. goto err;
  310. X509_VERIFY_PARAM_inherit(s->param, ctx->param);
  311. #if 0
  312. s->purpose = ctx->purpose;
  313. s->trust = ctx->trust;
  314. #endif
  315. s->quiet_shutdown=ctx->quiet_shutdown;
  316. s->max_send_fragment = ctx->max_send_fragment;
  317. CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
  318. s->ctx=ctx;
  319. #ifndef OPENSSL_NO_TLSEXT
  320. s->tlsext_debug_cb = 0;
  321. s->tlsext_debug_arg = NULL;
  322. s->tlsext_ticket_expected = 0;
  323. s->tlsext_status_type = -1;
  324. s->tlsext_status_expected = 0;
  325. s->tlsext_ocsp_ids = NULL;
  326. s->tlsext_ocsp_exts = NULL;
  327. s->tlsext_ocsp_resp = NULL;
  328. s->tlsext_ocsp_resplen = -1;
  329. CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
  330. s->initial_ctx=ctx;
  331. # ifndef OPENSSL_NO_NEXTPROTONEG
  332. s->next_proto_negotiated = NULL;
  333. # endif
  334. #endif
  335. s->verify_result=X509_V_OK;
  336. s->method=ctx->method;
  337. if (!s->method->ssl_new(s))
  338. goto err;
  339. s->references=1;
  340. s->server=(ctx->method->ssl_accept == ssl_undefined_function)?0:1;
  341. SSL_clear(s);
  342. CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
  343. #ifndef OPENSSL_NO_PSK
  344. s->psk_client_callback=ctx->psk_client_callback;
  345. s->psk_server_callback=ctx->psk_server_callback;
  346. #endif
  347. return(s);
  348. err:
  349. if (s != NULL)
  350. {
  351. if (s->cert != NULL)
  352. ssl_cert_free(s->cert);
  353. if (s->ctx != NULL)
  354. SSL_CTX_free(s->ctx); /* decrement reference count */
  355. OPENSSL_free(s);
  356. }
  357. SSLerr(SSL_F_SSL_NEW,ERR_R_MALLOC_FAILURE);
  358. return(NULL);
  359. }
  360. int SSL_CTX_set_session_id_context(SSL_CTX *ctx,const unsigned char *sid_ctx,
  361. unsigned int sid_ctx_len)
  362. {
  363. if(sid_ctx_len > sizeof ctx->sid_ctx)
  364. {
  365. SSLerr(SSL_F_SSL_CTX_SET_SESSION_ID_CONTEXT,SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  366. return 0;
  367. }
  368. ctx->sid_ctx_length=sid_ctx_len;
  369. memcpy(ctx->sid_ctx,sid_ctx,sid_ctx_len);
  370. return 1;
  371. }
  372. int SSL_set_session_id_context(SSL *ssl,const unsigned char *sid_ctx,
  373. unsigned int sid_ctx_len)
  374. {
  375. if(sid_ctx_len > SSL_MAX_SID_CTX_LENGTH)
  376. {
  377. SSLerr(SSL_F_SSL_SET_SESSION_ID_CONTEXT,SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  378. return 0;
  379. }
  380. ssl->sid_ctx_length=sid_ctx_len;
  381. memcpy(ssl->sid_ctx,sid_ctx,sid_ctx_len);
  382. return 1;
  383. }
  384. int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb)
  385. {
  386. CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
  387. ctx->generate_session_id = cb;
  388. CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
  389. return 1;
  390. }
  391. int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb)
  392. {
  393. CRYPTO_w_lock(CRYPTO_LOCK_SSL);
  394. ssl->generate_session_id = cb;
  395. CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
  396. return 1;
  397. }
  398. int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
  399. unsigned int id_len)
  400. {
  401. /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how
  402. * we can "construct" a session to give us the desired check - ie. to
  403. * find if there's a session in the hash table that would conflict with
  404. * any new session built out of this id/id_len and the ssl_version in
  405. * use by this SSL. */
  406. SSL_SESSION r, *p;
  407. if(id_len > sizeof r.session_id)
  408. return 0;
  409. r.ssl_version = ssl->version;
  410. r.session_id_length = id_len;
  411. memcpy(r.session_id, id, id_len);
  412. /* NB: SSLv2 always uses a fixed 16-byte session ID, so even if a
  413. * callback is calling us to check the uniqueness of a shorter ID, it
  414. * must be compared as a padded-out ID because that is what it will be
  415. * converted to when the callback has finished choosing it. */
  416. if((r.ssl_version == SSL2_VERSION) &&
  417. (id_len < SSL2_SSL_SESSION_ID_LENGTH))
  418. {
  419. memset(r.session_id + id_len, 0,
  420. SSL2_SSL_SESSION_ID_LENGTH - id_len);
  421. r.session_id_length = SSL2_SSL_SESSION_ID_LENGTH;
  422. }
  423. CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
  424. p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
  425. CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
  426. return (p != NULL);
  427. }
  428. int SSL_CTX_set_purpose(SSL_CTX *s, int purpose)
  429. {
  430. return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
  431. }
  432. int SSL_set_purpose(SSL *s, int purpose)
  433. {
  434. return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
  435. }
  436. int SSL_CTX_set_trust(SSL_CTX *s, int trust)
  437. {
  438. return X509_VERIFY_PARAM_set_trust(s->param, trust);
  439. }
  440. int SSL_set_trust(SSL *s, int trust)
  441. {
  442. return X509_VERIFY_PARAM_set_trust(s->param, trust);
  443. }
  444. int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm)
  445. {
  446. return X509_VERIFY_PARAM_set1(ctx->param, vpm);
  447. }
  448. int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm)
  449. {
  450. return X509_VERIFY_PARAM_set1(ssl->param, vpm);
  451. }
  452. void SSL_free(SSL *s)
  453. {
  454. int i;
  455. if(s == NULL)
  456. return;
  457. i=CRYPTO_add(&s->references,-1,CRYPTO_LOCK_SSL);
  458. #ifdef REF_PRINT
  459. REF_PRINT("SSL",s);
  460. #endif
  461. if (i > 0) return;
  462. #ifdef REF_CHECK
  463. if (i < 0)
  464. {
  465. fprintf(stderr,"SSL_free, bad reference count\n");
  466. abort(); /* ok */
  467. }
  468. #endif
  469. if (s->param)
  470. X509_VERIFY_PARAM_free(s->param);
  471. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
  472. if (s->bbio != NULL)
  473. {
  474. /* If the buffering BIO is in place, pop it off */
  475. if (s->bbio == s->wbio)
  476. {
  477. s->wbio=BIO_pop(s->wbio);
  478. }
  479. BIO_free(s->bbio);
  480. s->bbio=NULL;
  481. }
  482. if (s->rbio != NULL)
  483. BIO_free_all(s->rbio);
  484. if ((s->wbio != NULL) && (s->wbio != s->rbio))
  485. BIO_free_all(s->wbio);
  486. if (s->init_buf != NULL) BUF_MEM_free(s->init_buf);
  487. /* add extra stuff */
  488. if (s->cipher_list != NULL) sk_SSL_CIPHER_free(s->cipher_list);
  489. if (s->cipher_list_by_id != NULL) sk_SSL_CIPHER_free(s->cipher_list_by_id);
  490. /* Make the next call work :-) */
  491. if (s->session != NULL)
  492. {
  493. ssl_clear_bad_session(s);
  494. SSL_SESSION_free(s->session);
  495. }
  496. ssl_clear_cipher_ctx(s);
  497. ssl_clear_hash_ctx(&s->read_hash);
  498. ssl_clear_hash_ctx(&s->write_hash);
  499. if (s->cert != NULL) ssl_cert_free(s->cert);
  500. /* Free up if allocated */
  501. #ifndef OPENSSL_NO_TLSEXT
  502. if (s->tlsext_hostname)
  503. OPENSSL_free(s->tlsext_hostname);
  504. if (s->initial_ctx) SSL_CTX_free(s->initial_ctx);
  505. #ifndef OPENSSL_NO_EC
  506. if (s->tlsext_ecpointformatlist) OPENSSL_free(s->tlsext_ecpointformatlist);
  507. if (s->tlsext_ellipticcurvelist) OPENSSL_free(s->tlsext_ellipticcurvelist);
  508. #endif /* OPENSSL_NO_EC */
  509. if (s->tlsext_opaque_prf_input) OPENSSL_free(s->tlsext_opaque_prf_input);
  510. if (s->tlsext_ocsp_exts)
  511. sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
  512. X509_EXTENSION_free);
  513. if (s->tlsext_ocsp_ids)
  514. sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids, OCSP_RESPID_free);
  515. if (s->tlsext_ocsp_resp)
  516. OPENSSL_free(s->tlsext_ocsp_resp);
  517. #endif
  518. if (s->client_CA != NULL)
  519. sk_X509_NAME_pop_free(s->client_CA,X509_NAME_free);
  520. if (s->method != NULL) s->method->ssl_free(s);
  521. if (s->ctx) SSL_CTX_free(s->ctx);
  522. #ifndef OPENSSL_NO_KRB5
  523. if (s->kssl_ctx != NULL)
  524. kssl_ctx_free(s->kssl_ctx);
  525. #endif /* OPENSSL_NO_KRB5 */
  526. #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
  527. if (s->next_proto_negotiated)
  528. OPENSSL_free(s->next_proto_negotiated);
  529. #endif
  530. #ifndef OPENSSL_NO_SRTP
  531. if (s->srtp_profiles)
  532. sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
  533. #endif
  534. OPENSSL_free(s);
  535. }
  536. void SSL_set_bio(SSL *s,BIO *rbio,BIO *wbio)
  537. {
  538. /* If the output buffering BIO is still in place, remove it
  539. */
  540. if (s->bbio != NULL)
  541. {
  542. if (s->wbio == s->bbio)
  543. {
  544. s->wbio=s->wbio->next_bio;
  545. s->bbio->next_bio=NULL;
  546. }
  547. }
  548. if ((s->rbio != NULL) && (s->rbio != rbio))
  549. BIO_free_all(s->rbio);
  550. if ((s->wbio != NULL) && (s->wbio != wbio) && (s->rbio != s->wbio))
  551. BIO_free_all(s->wbio);
  552. s->rbio=rbio;
  553. s->wbio=wbio;
  554. }
  555. BIO *SSL_get_rbio(const SSL *s)
  556. { return(s->rbio); }
  557. BIO *SSL_get_wbio(const SSL *s)
  558. { return(s->wbio); }
  559. int SSL_get_fd(const SSL *s)
  560. {
  561. return(SSL_get_rfd(s));
  562. }
  563. int SSL_get_rfd(const SSL *s)
  564. {
  565. int ret= -1;
  566. BIO *b,*r;
  567. b=SSL_get_rbio(s);
  568. r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
  569. if (r != NULL)
  570. BIO_get_fd(r,&ret);
  571. return(ret);
  572. }
  573. int SSL_get_wfd(const SSL *s)
  574. {
  575. int ret= -1;
  576. BIO *b,*r;
  577. b=SSL_get_wbio(s);
  578. r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
  579. if (r != NULL)
  580. BIO_get_fd(r,&ret);
  581. return(ret);
  582. }
  583. #ifndef OPENSSL_NO_SOCK
  584. int SSL_set_fd(SSL *s,int fd)
  585. {
  586. int ret=0;
  587. BIO *bio=NULL;
  588. bio=BIO_new(BIO_s_socket());
  589. if (bio == NULL)
  590. {
  591. SSLerr(SSL_F_SSL_SET_FD,ERR_R_BUF_LIB);
  592. goto err;
  593. }
  594. BIO_set_fd(bio,fd,BIO_NOCLOSE);
  595. SSL_set_bio(s,bio,bio);
  596. ret=1;
  597. err:
  598. return(ret);
  599. }
  600. int SSL_set_wfd(SSL *s,int fd)
  601. {
  602. int ret=0;
  603. BIO *bio=NULL;
  604. if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_SOCKET)
  605. || ((int)BIO_get_fd(s->rbio,NULL) != fd))
  606. {
  607. bio=BIO_new(BIO_s_socket());
  608. if (bio == NULL)
  609. { SSLerr(SSL_F_SSL_SET_WFD,ERR_R_BUF_LIB); goto err; }
  610. BIO_set_fd(bio,fd,BIO_NOCLOSE);
  611. SSL_set_bio(s,SSL_get_rbio(s),bio);
  612. }
  613. else
  614. SSL_set_bio(s,SSL_get_rbio(s),SSL_get_rbio(s));
  615. ret=1;
  616. err:
  617. return(ret);
  618. }
  619. int SSL_set_rfd(SSL *s,int fd)
  620. {
  621. int ret=0;
  622. BIO *bio=NULL;
  623. if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_SOCKET)
  624. || ((int)BIO_get_fd(s->wbio,NULL) != fd))
  625. {
  626. bio=BIO_new(BIO_s_socket());
  627. if (bio == NULL)
  628. {
  629. SSLerr(SSL_F_SSL_SET_RFD,ERR_R_BUF_LIB);
  630. goto err;
  631. }
  632. BIO_set_fd(bio,fd,BIO_NOCLOSE);
  633. SSL_set_bio(s,bio,SSL_get_wbio(s));
  634. }
  635. else
  636. SSL_set_bio(s,SSL_get_wbio(s),SSL_get_wbio(s));
  637. ret=1;
  638. err:
  639. return(ret);
  640. }
  641. #endif
  642. /* return length of latest Finished message we sent, copy to 'buf' */
  643. size_t SSL_get_finished(const SSL *s, void *buf, size_t count)
  644. {
  645. size_t ret = 0;
  646. if (s->s3 != NULL)
  647. {
  648. ret = s->s3->tmp.finish_md_len;
  649. if (count > ret)
  650. count = ret;
  651. memcpy(buf, s->s3->tmp.finish_md, count);
  652. }
  653. return ret;
  654. }
  655. /* return length of latest Finished message we expected, copy to 'buf' */
  656. size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count)
  657. {
  658. size_t ret = 0;
  659. if (s->s3 != NULL)
  660. {
  661. ret = s->s3->tmp.peer_finish_md_len;
  662. if (count > ret)
  663. count = ret;
  664. memcpy(buf, s->s3->tmp.peer_finish_md, count);
  665. }
  666. return ret;
  667. }
  668. int SSL_get_verify_mode(const SSL *s)
  669. {
  670. return(s->verify_mode);
  671. }
  672. int SSL_get_verify_depth(const SSL *s)
  673. {
  674. return X509_VERIFY_PARAM_get_depth(s->param);
  675. }
  676. int (*SSL_get_verify_callback(const SSL *s))(int,X509_STORE_CTX *)
  677. {
  678. return(s->verify_callback);
  679. }
  680. int SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
  681. {
  682. return(ctx->verify_mode);
  683. }
  684. int SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
  685. {
  686. return X509_VERIFY_PARAM_get_depth(ctx->param);
  687. }
  688. int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int,X509_STORE_CTX *)
  689. {
  690. return(ctx->default_verify_callback);
  691. }
  692. void SSL_set_verify(SSL *s,int mode,
  693. int (*callback)(int ok,X509_STORE_CTX *ctx))
  694. {
  695. s->verify_mode=mode;
  696. if (callback != NULL)
  697. s->verify_callback=callback;
  698. }
  699. void SSL_set_verify_depth(SSL *s,int depth)
  700. {
  701. X509_VERIFY_PARAM_set_depth(s->param, depth);
  702. }
  703. void SSL_set_read_ahead(SSL *s,int yes)
  704. {
  705. s->read_ahead=yes;
  706. }
  707. int SSL_get_read_ahead(const SSL *s)
  708. {
  709. return(s->read_ahead);
  710. }
  711. int SSL_pending(const SSL *s)
  712. {
  713. /* SSL_pending cannot work properly if read-ahead is enabled
  714. * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)),
  715. * and it is impossible to fix since SSL_pending cannot report
  716. * errors that may be observed while scanning the new data.
  717. * (Note that SSL_pending() is often used as a boolean value,
  718. * so we'd better not return -1.)
  719. */
  720. return(s->method->ssl_pending(s));
  721. }
  722. X509 *SSL_get_peer_certificate(const SSL *s)
  723. {
  724. X509 *r;
  725. if ((s == NULL) || (s->session == NULL))
  726. r=NULL;
  727. else
  728. r=s->session->peer;
  729. if (r == NULL) return(r);
  730. CRYPTO_add(&r->references,1,CRYPTO_LOCK_X509);
  731. return(r);
  732. }
  733. STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s)
  734. {
  735. STACK_OF(X509) *r;
  736. if ((s == NULL) || (s->session == NULL) || (s->session->sess_cert == NULL))
  737. r=NULL;
  738. else
  739. r=s->session->sess_cert->cert_chain;
  740. /* If we are a client, cert_chain includes the peer's own
  741. * certificate; if we are a server, it does not. */
  742. return(r);
  743. }
  744. /* Now in theory, since the calling process own 't' it should be safe to
  745. * modify. We need to be able to read f without being hassled */
  746. void SSL_copy_session_id(SSL *t,const SSL *f)
  747. {
  748. CERT *tmp;
  749. /* Do we need to to SSL locking? */
  750. SSL_set_session(t,SSL_get_session(f));
  751. /* what if we are setup as SSLv2 but want to talk SSLv3 or
  752. * vice-versa */
  753. if (t->method != f->method)
  754. {
  755. t->method->ssl_free(t); /* cleanup current */
  756. t->method=f->method; /* change method */
  757. t->method->ssl_new(t); /* setup new */
  758. }
  759. tmp=t->cert;
  760. if (f->cert != NULL)
  761. {
  762. CRYPTO_add(&f->cert->references,1,CRYPTO_LOCK_SSL_CERT);
  763. t->cert=f->cert;
  764. }
  765. else
  766. t->cert=NULL;
  767. if (tmp != NULL) ssl_cert_free(tmp);
  768. SSL_set_session_id_context(t,f->sid_ctx,f->sid_ctx_length);
  769. }
  770. /* Fix this so it checks all the valid key/cert options */
  771. int SSL_CTX_check_private_key(const SSL_CTX *ctx)
  772. {
  773. if ( (ctx == NULL) ||
  774. (ctx->cert == NULL) ||
  775. (ctx->cert->key->x509 == NULL))
  776. {
  777. SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED);
  778. return(0);
  779. }
  780. if (ctx->cert->key->privatekey == NULL)
  781. {
  782. SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,SSL_R_NO_PRIVATE_KEY_ASSIGNED);
  783. return(0);
  784. }
  785. return(X509_check_private_key(ctx->cert->key->x509, ctx->cert->key->privatekey));
  786. }
  787. /* Fix this function so that it takes an optional type parameter */
  788. int SSL_check_private_key(const SSL *ssl)
  789. {
  790. if (ssl == NULL)
  791. {
  792. SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,ERR_R_PASSED_NULL_PARAMETER);
  793. return(0);
  794. }
  795. if (ssl->cert == NULL)
  796. {
  797. SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED);
  798. return 0;
  799. }
  800. if (ssl->cert->key->x509 == NULL)
  801. {
  802. SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED);
  803. return(0);
  804. }
  805. if (ssl->cert->key->privatekey == NULL)
  806. {
  807. SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_PRIVATE_KEY_ASSIGNED);
  808. return(0);
  809. }
  810. return(X509_check_private_key(ssl->cert->key->x509,
  811. ssl->cert->key->privatekey));
  812. }
  813. int SSL_accept(SSL *s)
  814. {
  815. if (s->handshake_func == 0)
  816. /* Not properly initialized yet */
  817. SSL_set_accept_state(s);
  818. return(s->method->ssl_accept(s));
  819. }
  820. int SSL_connect(SSL *s)
  821. {
  822. if (s->handshake_func == 0)
  823. /* Not properly initialized yet */
  824. SSL_set_connect_state(s);
  825. return(s->method->ssl_connect(s));
  826. }
  827. long SSL_get_default_timeout(const SSL *s)
  828. {
  829. return(s->method->get_timeout());
  830. }
  831. int SSL_read(SSL *s,void *buf,int num)
  832. {
  833. if (s->handshake_func == 0)
  834. {
  835. SSLerr(SSL_F_SSL_READ, SSL_R_UNINITIALIZED);
  836. return -1;
  837. }
  838. if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
  839. {
  840. s->rwstate=SSL_NOTHING;
  841. return(0);
  842. }
  843. return(s->method->ssl_read(s,buf,num));
  844. }
  845. int SSL_peek(SSL *s,void *buf,int num)
  846. {
  847. if (s->handshake_func == 0)
  848. {
  849. SSLerr(SSL_F_SSL_PEEK, SSL_R_UNINITIALIZED);
  850. return -1;
  851. }
  852. if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
  853. {
  854. return(0);
  855. }
  856. return(s->method->ssl_peek(s,buf,num));
  857. }
  858. int SSL_write(SSL *s,const void *buf,int num)
  859. {
  860. if (s->handshake_func == 0)
  861. {
  862. SSLerr(SSL_F_SSL_WRITE, SSL_R_UNINITIALIZED);
  863. return -1;
  864. }
  865. if (s->shutdown & SSL_SENT_SHUTDOWN)
  866. {
  867. s->rwstate=SSL_NOTHING;
  868. SSLerr(SSL_F_SSL_WRITE,SSL_R_PROTOCOL_IS_SHUTDOWN);
  869. return(-1);
  870. }
  871. return(s->method->ssl_write(s,buf,num));
  872. }
  873. int SSL_shutdown(SSL *s)
  874. {
  875. /* Note that this function behaves differently from what one might
  876. * expect. Return values are 0 for no success (yet),
  877. * 1 for success; but calling it once is usually not enough,
  878. * even if blocking I/O is used (see ssl3_shutdown).
  879. */
  880. if (s->handshake_func == 0)
  881. {
  882. SSLerr(SSL_F_SSL_SHUTDOWN, SSL_R_UNINITIALIZED);
  883. return -1;
  884. }
  885. if ((s != NULL) && !SSL_in_init(s))
  886. return(s->method->ssl_shutdown(s));
  887. else
  888. return(1);
  889. }
  890. int SSL_renegotiate(SSL *s)
  891. {
  892. if (s->renegotiate == 0)
  893. s->renegotiate=1;
  894. s->new_session=1;
  895. return(s->method->ssl_renegotiate(s));
  896. }
  897. int SSL_renegotiate_abbreviated(SSL *s)
  898. {
  899. if (s->renegotiate == 0)
  900. s->renegotiate=1;
  901. s->new_session=0;
  902. return(s->method->ssl_renegotiate(s));
  903. }
  904. int SSL_renegotiate_pending(SSL *s)
  905. {
  906. /* becomes true when negotiation is requested;
  907. * false again once a handshake has finished */
  908. return (s->renegotiate != 0);
  909. }
  910. long SSL_ctrl(SSL *s,int cmd,long larg,void *parg)
  911. {
  912. long l;
  913. switch (cmd)
  914. {
  915. case SSL_CTRL_GET_READ_AHEAD:
  916. return(s->read_ahead);
  917. case SSL_CTRL_SET_READ_AHEAD:
  918. l=s->read_ahead;
  919. s->read_ahead=larg;
  920. return(l);
  921. case SSL_CTRL_SET_MSG_CALLBACK_ARG:
  922. s->msg_callback_arg = parg;
  923. return 1;
  924. case SSL_CTRL_OPTIONS:
  925. return(s->options|=larg);
  926. case SSL_CTRL_CLEAR_OPTIONS:
  927. return(s->options&=~larg);
  928. case SSL_CTRL_MODE:
  929. return(s->mode|=larg);
  930. case SSL_CTRL_CLEAR_MODE:
  931. return(s->mode &=~larg);
  932. case SSL_CTRL_GET_MAX_CERT_LIST:
  933. return(s->max_cert_list);
  934. case SSL_CTRL_SET_MAX_CERT_LIST:
  935. l=s->max_cert_list;
  936. s->max_cert_list=larg;
  937. return(l);
  938. case SSL_CTRL_SET_MTU:
  939. #ifndef OPENSSL_NO_DTLS1
  940. if (larg < (long)dtls1_min_mtu())
  941. return 0;
  942. #endif
  943. if (SSL_version(s) == DTLS1_VERSION ||
  944. SSL_version(s) == DTLS1_BAD_VER)
  945. {
  946. s->d1->mtu = larg;
  947. return larg;
  948. }
  949. return 0;
  950. case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
  951. if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
  952. return 0;
  953. s->max_send_fragment = larg;
  954. return 1;
  955. case SSL_CTRL_GET_RI_SUPPORT:
  956. if (s->s3)
  957. return s->s3->send_connection_binding;
  958. else return 0;
  959. default:
  960. return(s->method->ssl_ctrl(s,cmd,larg,parg));
  961. }
  962. }
  963. long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void))
  964. {
  965. switch(cmd)
  966. {
  967. case SSL_CTRL_SET_MSG_CALLBACK:
  968. s->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
  969. return 1;
  970. default:
  971. return(s->method->ssl_callback_ctrl(s,cmd,fp));
  972. }
  973. }
  974. LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx)
  975. {
  976. return ctx->sessions;
  977. }
  978. long SSL_CTX_ctrl(SSL_CTX *ctx,int cmd,long larg,void *parg)
  979. {
  980. long l;
  981. switch (cmd)
  982. {
  983. case SSL_CTRL_GET_READ_AHEAD:
  984. return(ctx->read_ahead);
  985. case SSL_CTRL_SET_READ_AHEAD:
  986. l=ctx->read_ahead;
  987. ctx->read_ahead=larg;
  988. return(l);
  989. case SSL_CTRL_SET_MSG_CALLBACK_ARG:
  990. ctx->msg_callback_arg = parg;
  991. return 1;
  992. case SSL_CTRL_GET_MAX_CERT_LIST:
  993. return(ctx->max_cert_list);
  994. case SSL_CTRL_SET_MAX_CERT_LIST:
  995. l=ctx->max_cert_list;
  996. ctx->max_cert_list=larg;
  997. return(l);
  998. case SSL_CTRL_SET_SESS_CACHE_SIZE:
  999. l=ctx->session_cache_size;
  1000. ctx->session_cache_size=larg;
  1001. return(l);
  1002. case SSL_CTRL_GET_SESS_CACHE_SIZE:
  1003. return(ctx->session_cache_size);
  1004. case SSL_CTRL_SET_SESS_CACHE_MODE:
  1005. l=ctx->session_cache_mode;
  1006. ctx->session_cache_mode=larg;
  1007. return(l);
  1008. case SSL_CTRL_GET_SESS_CACHE_MODE:
  1009. return(ctx->session_cache_mode);
  1010. case SSL_CTRL_SESS_NUMBER:
  1011. return(lh_SSL_SESSION_num_items(ctx->sessions));
  1012. case SSL_CTRL_SESS_CONNECT:
  1013. return(ctx->stats.sess_connect);
  1014. case SSL_CTRL_SESS_CONNECT_GOOD:
  1015. return(ctx->stats.sess_connect_good);
  1016. case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
  1017. return(ctx->stats.sess_connect_renegotiate);
  1018. case SSL_CTRL_SESS_ACCEPT:
  1019. return(ctx->stats.sess_accept);
  1020. case SSL_CTRL_SESS_ACCEPT_GOOD:
  1021. return(ctx->stats.sess_accept_good);
  1022. case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
  1023. return(ctx->stats.sess_accept_renegotiate);
  1024. case SSL_CTRL_SESS_HIT:
  1025. return(ctx->stats.sess_hit);
  1026. case SSL_CTRL_SESS_CB_HIT:
  1027. return(ctx->stats.sess_cb_hit);
  1028. case SSL_CTRL_SESS_MISSES:
  1029. return(ctx->stats.sess_miss);
  1030. case SSL_CTRL_SESS_TIMEOUTS:
  1031. return(ctx->stats.sess_timeout);
  1032. case SSL_CTRL_SESS_CACHE_FULL:
  1033. return(ctx->stats.sess_cache_full);
  1034. case SSL_CTRL_OPTIONS:
  1035. return(ctx->options|=larg);
  1036. case SSL_CTRL_CLEAR_OPTIONS:
  1037. return(ctx->options&=~larg);
  1038. case SSL_CTRL_MODE:
  1039. return(ctx->mode|=larg);
  1040. case SSL_CTRL_CLEAR_MODE:
  1041. return(ctx->mode&=~larg);
  1042. case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
  1043. if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
  1044. return 0;
  1045. ctx->max_send_fragment = larg;
  1046. return 1;
  1047. default:
  1048. return(ctx->method->ssl_ctx_ctrl(ctx,cmd,larg,parg));
  1049. }
  1050. }
  1051. long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void))
  1052. {
  1053. switch(cmd)
  1054. {
  1055. case SSL_CTRL_SET_MSG_CALLBACK:
  1056. ctx->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
  1057. return 1;
  1058. default:
  1059. return(ctx->method->ssl_ctx_callback_ctrl(ctx,cmd,fp));
  1060. }
  1061. }
  1062. int ssl_cipher_id_cmp(const SSL_CIPHER *a, const SSL_CIPHER *b)
  1063. {
  1064. long l;
  1065. l=a->id-b->id;
  1066. if (l == 0L)
  1067. return(0);
  1068. else
  1069. return((l > 0)?1:-1);
  1070. }
  1071. int ssl_cipher_ptr_id_cmp(const SSL_CIPHER * const *ap,
  1072. const SSL_CIPHER * const *bp)
  1073. {
  1074. long l;
  1075. l=(*ap)->id-(*bp)->id;
  1076. if (l == 0L)
  1077. return(0);
  1078. else
  1079. return((l > 0)?1:-1);
  1080. }
  1081. /** return a STACK of the ciphers available for the SSL and in order of
  1082. * preference */
  1083. STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s)
  1084. {
  1085. if (s != NULL)
  1086. {
  1087. if (s->cipher_list != NULL)
  1088. {
  1089. return(s->cipher_list);
  1090. }
  1091. else if ((s->ctx != NULL) &&
  1092. (s->ctx->cipher_list != NULL))
  1093. {
  1094. return(s->ctx->cipher_list);
  1095. }
  1096. }
  1097. return(NULL);
  1098. }
  1099. /** return a STACK of the ciphers available for the SSL and in order of
  1100. * algorithm id */
  1101. STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s)
  1102. {
  1103. if (s != NULL)
  1104. {
  1105. if (s->cipher_list_by_id != NULL)
  1106. {
  1107. return(s->cipher_list_by_id);
  1108. }
  1109. else if ((s->ctx != NULL) &&
  1110. (s->ctx->cipher_list_by_id != NULL))
  1111. {
  1112. return(s->ctx->cipher_list_by_id);
  1113. }
  1114. }
  1115. return(NULL);
  1116. }
  1117. /** The old interface to get the same thing as SSL_get_ciphers() */
  1118. const char *SSL_get_cipher_list(const SSL *s,int n)
  1119. {
  1120. SSL_CIPHER *c;
  1121. STACK_OF(SSL_CIPHER) *sk;
  1122. if (s == NULL) return(NULL);
  1123. sk=SSL_get_ciphers(s);
  1124. if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= n))
  1125. return(NULL);
  1126. c=sk_SSL_CIPHER_value(sk,n);
  1127. if (c == NULL) return(NULL);
  1128. return(c->name);
  1129. }
  1130. /** specify the ciphers to be used by default by the SSL_CTX */
  1131. int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
  1132. {
  1133. STACK_OF(SSL_CIPHER) *sk;
  1134. sk=ssl_create_cipher_list(ctx->method,&ctx->cipher_list,
  1135. &ctx->cipher_list_by_id,str);
  1136. /* ssl_create_cipher_list may return an empty stack if it
  1137. * was unable to find a cipher matching the given rule string
  1138. * (for example if the rule string specifies a cipher which
  1139. * has been disabled). This is not an error as far as
  1140. * ssl_create_cipher_list is concerned, and hence
  1141. * ctx->cipher_list and ctx->cipher_list_by_id has been
  1142. * updated. */
  1143. if (sk == NULL)
  1144. return 0;
  1145. else if (sk_SSL_CIPHER_num(sk) == 0)
  1146. {
  1147. SSLerr(SSL_F_SSL_CTX_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH);
  1148. return 0;
  1149. }
  1150. return 1;
  1151. }
  1152. /** specify the ciphers to be used by the SSL */
  1153. int SSL_set_cipher_list(SSL *s,const char *str)
  1154. {
  1155. STACK_OF(SSL_CIPHER) *sk;
  1156. sk=ssl_create_cipher_list(s->ctx->method,&s->cipher_list,
  1157. &s->cipher_list_by_id,str);
  1158. /* see comment in SSL_CTX_set_cipher_list */
  1159. if (sk == NULL)
  1160. return 0;
  1161. else if (sk_SSL_CIPHER_num(sk) == 0)
  1162. {
  1163. SSLerr(SSL_F_SSL_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH);
  1164. return 0;
  1165. }
  1166. return 1;
  1167. }
  1168. /* works well for SSLv2, not so good for SSLv3 */
  1169. char *SSL_get_shared_ciphers(const SSL *s,char *buf,int len)
  1170. {
  1171. char *p;
  1172. STACK_OF(SSL_CIPHER) *sk;
  1173. SSL_CIPHER *c;
  1174. int i;
  1175. if ((s->session == NULL) || (s->session->ciphers == NULL) ||
  1176. (len < 2))
  1177. return(NULL);
  1178. p=buf;
  1179. sk=s->session->ciphers;
  1180. if (sk_SSL_CIPHER_num(sk) == 0)
  1181. return NULL;
  1182. for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
  1183. {
  1184. int n;
  1185. c=sk_SSL_CIPHER_value(sk,i);
  1186. n=strlen(c->name);
  1187. if (n+1 > len)
  1188. {
  1189. if (p != buf)
  1190. --p;
  1191. *p='\0';
  1192. return buf;
  1193. }
  1194. strcpy(p,c->name);
  1195. p+=n;
  1196. *(p++)=':';
  1197. len-=n+1;
  1198. }
  1199. p[-1]='\0';
  1200. return(buf);
  1201. }
  1202. int ssl_cipher_list_to_bytes(SSL *s,STACK_OF(SSL_CIPHER) *sk,unsigned char *p,
  1203. int (*put_cb)(const SSL_CIPHER *, unsigned char *))
  1204. {
  1205. int i,j=0;
  1206. SSL_CIPHER *c;
  1207. unsigned char *q;
  1208. #ifndef OPENSSL_NO_KRB5
  1209. int nokrb5 = !kssl_tgt_is_available(s->kssl_ctx);
  1210. #endif /* OPENSSL_NO_KRB5 */
  1211. if (sk == NULL) return(0);
  1212. q=p;
  1213. for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
  1214. {
  1215. c=sk_SSL_CIPHER_value(sk,i);
  1216. /* Skip TLS v1.2 only ciphersuites if lower than v1.2 */
  1217. if ((c->algorithm_ssl & SSL_TLSV1_2) &&
  1218. (TLS1_get_client_version(s) < TLS1_2_VERSION))
  1219. continue;
  1220. #ifndef OPENSSL_NO_KRB5
  1221. if (((c->algorithm_mkey & SSL_kKRB5) || (c->algorithm_auth & SSL_aKRB5)) &&
  1222. nokrb5)
  1223. continue;
  1224. #endif /* OPENSSL_NO_KRB5 */
  1225. #ifndef OPENSSL_NO_PSK
  1226. /* with PSK there must be client callback set */
  1227. if (((c->algorithm_mkey & SSL_kPSK) || (c->algorithm_auth & SSL_aPSK)) &&
  1228. s->psk_client_callback == NULL)
  1229. continue;
  1230. #endif /* OPENSSL_NO_PSK */
  1231. j = put_cb ? put_cb(c,p) : ssl_put_cipher_by_char(s,c,p);
  1232. p+=j;
  1233. }
  1234. /* If p == q, no ciphers and caller indicates an error. Otherwise
  1235. * add SCSV if not renegotiating.
  1236. */
  1237. if (p != q && !s->renegotiate)
  1238. {
  1239. static SSL_CIPHER scsv =
  1240. {
  1241. 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
  1242. };
  1243. j = put_cb ? put_cb(&scsv,p) : ssl_put_cipher_by_char(s,&scsv,p);
  1244. p+=j;
  1245. #ifdef OPENSSL_RI_DEBUG
  1246. fprintf(stderr, "SCSV sent by client\n");
  1247. #endif
  1248. }
  1249. return(p-q);
  1250. }
  1251. STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s,unsigned char *p,int num,
  1252. STACK_OF(SSL_CIPHER) **skp)
  1253. {
  1254. const SSL_CIPHER *c;
  1255. STACK_OF(SSL_CIPHER) *sk;
  1256. int i,n;
  1257. if (s->s3)
  1258. s->s3->send_connection_binding = 0;
  1259. n=ssl_put_cipher_by_char(s,NULL,NULL);
  1260. if ((num%n) != 0)
  1261. {
  1262. SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
  1263. return(NULL);
  1264. }
  1265. if ((skp == NULL) || (*skp == NULL))
  1266. sk=sk_SSL_CIPHER_new_null(); /* change perhaps later */
  1267. else
  1268. {
  1269. sk= *skp;
  1270. sk_SSL_CIPHER_zero(sk);
  1271. }
  1272. for (i=0; i<num; i+=n)
  1273. {
  1274. /* Check for SCSV */
  1275. if (s->s3 && (n != 3 || !p[0]) &&
  1276. (p[n-2] == ((SSL3_CK_SCSV >> 8) & 0xff)) &&
  1277. (p[n-1] == (SSL3_CK_SCSV & 0xff)))
  1278. {
  1279. /* SCSV fatal if renegotiating */
  1280. if (s->renegotiate)
  1281. {
  1282. SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
  1283. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
  1284. goto err;
  1285. }
  1286. s->s3->send_connection_binding = 1;
  1287. p += n;
  1288. #ifdef OPENSSL_RI_DEBUG
  1289. fprintf(stderr, "SCSV received by server\n");
  1290. #endif
  1291. continue;
  1292. }
  1293. c=ssl_get_cipher_by_char(s,p);
  1294. p+=n;
  1295. if (c != NULL)
  1296. {
  1297. if (!sk_SSL_CIPHER_push(sk,c))
  1298. {
  1299. SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,ERR_R_MALLOC_FAILURE);
  1300. goto err;
  1301. }
  1302. }
  1303. }
  1304. if (skp != NULL)
  1305. *skp=sk;
  1306. return(sk);
  1307. err:
  1308. if ((skp == NULL) || (*skp == NULL))
  1309. sk_SSL_CIPHER_free(sk);
  1310. return(NULL);
  1311. }
  1312. #ifndef OPENSSL_NO_TLSEXT
  1313. /** return a servername extension value if provided in Client Hello, or NULL.
  1314. * So far, only host_name types are defined (RFC 3546).
  1315. */
  1316. const char *SSL_get_servername(const SSL *s, const int type)
  1317. {
  1318. if (type != TLSEXT_NAMETYPE_host_name)
  1319. return NULL;
  1320. return s->session && !s->tlsext_hostname ?
  1321. s->session->tlsext_hostname :
  1322. s->tlsext_hostname;
  1323. }
  1324. int SSL_get_servername_type(const SSL *s)
  1325. {
  1326. if (s->session && (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname))
  1327. return TLSEXT_NAMETYPE_host_name;
  1328. return -1;
  1329. }
  1330. # ifndef OPENSSL_NO_NEXTPROTONEG
  1331. /* SSL_select_next_proto implements the standard protocol selection. It is
  1332. * expected that this function is called from the callback set by
  1333. * SSL_CTX_set_next_proto_select_cb.
  1334. *
  1335. * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
  1336. * strings. The length byte itself is not included in the length. A byte
  1337. * string of length 0 is invalid. No byte string may be truncated.
  1338. *
  1339. * The current, but experimental algorithm for selecting the protocol is:
  1340. *
  1341. * 1) If the server doesn't support NPN then this is indicated to the
  1342. * callback. In this case, the client application has to abort the connection
  1343. * or have a default application level protocol.
  1344. *
  1345. * 2) If the server supports NPN, but advertises an empty list then the
  1346. * client selects the first protcol in its list, but indicates via the
  1347. * API that this fallback case was enacted.
  1348. *
  1349. * 3) Otherwise, the client finds the first protocol in the server's list
  1350. * that it supports and selects this protocol. This is because it's
  1351. * assumed that the server has better information about which protocol
  1352. * a client should use.
  1353. *
  1354. * 4) If the client doesn't support any of the server's advertised
  1355. * protocols, then this is treated the same as case 2.
  1356. *
  1357. * It returns either
  1358. * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
  1359. * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
  1360. */
  1361. int SSL_select_next_proto(unsigned char **out, unsigned char *outlen, const unsigned char *server, unsigned int server_len, const unsigned char *client, unsigned int client_len)
  1362. {
  1363. unsigned int i, j;
  1364. const unsigned char *result;
  1365. int status = OPENSSL_NPN_UNSUPPORTED;
  1366. /* For each protocol in server preference order, see if we support it. */
  1367. for (i = 0; i < server_len; )
  1368. {
  1369. for (j = 0; j < client_len; )
  1370. {
  1371. if (server[i] == client[j] &&
  1372. memcmp(&server[i+1], &client[j+1], server[i]) == 0)
  1373. {
  1374. /* We found a match */
  1375. result = &server[i];
  1376. status = OPENSSL_NPN_NEGOTIATED;
  1377. goto found;
  1378. }
  1379. j += client[j];
  1380. j++;
  1381. }
  1382. i += server[i];
  1383. i++;
  1384. }
  1385. /* There's no overlap between our protocols and the server's list. */
  1386. result = client;
  1387. status = OPENSSL_NPN_NO_OVERLAP;
  1388. found:
  1389. *out = (unsigned char *) result + 1;
  1390. *outlen = result[0];
  1391. return status;
  1392. }
  1393. /* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
  1394. * requested protocol for this connection and returns 0. If the client didn't
  1395. * request any protocol, then *data is set to NULL.
  1396. *
  1397. * Note that the client can request any protocol it chooses. The value returned
  1398. * from this function need not be a member of the list of supported protocols
  1399. * provided by the callback.
  1400. */
  1401. void SSL_get0_next_proto_negotiated(const SSL *s, const unsigned char **data, unsigned *len)
  1402. {
  1403. *data = s->next_proto_negotiated;
  1404. if (!*data) {
  1405. *len = 0;
  1406. } else {
  1407. *len = s->next_proto_negotiated_len;
  1408. }
  1409. }
  1410. /* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
  1411. * TLS server needs a list of supported protocols for Next Protocol
  1412. * Negotiation. The returned list must be in wire format. The list is returned
  1413. * by setting |out| to point to it and |outlen| to its length. This memory will
  1414. * not be modified, but one should assume that the SSL* keeps a reference to
  1415. * it.
  1416. *
  1417. * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise. Otherwise, no
  1418. * such extension will be included in the ServerHello. */
  1419. void SSL_CTX_set_next_protos_advertised_cb(SSL_CTX *ctx, int (*cb) (SSL *ssl, const unsigned char **out, unsigned int *outlen, void *arg), void *arg)
  1420. {
  1421. ctx->next_protos_advertised_cb = cb;
  1422. ctx->next_protos_advertised_cb_arg = arg;
  1423. }
  1424. /* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
  1425. * client needs to select a protocol from the server's provided list. |out|
  1426. * must be set to point to the selected protocol (which may be within |in|).
  1427. * The length of the protocol name must be written into |outlen|. The server's
  1428. * advertised protocols are provided in |in| and |inlen|. The callback can
  1429. * assume that |in| is syntactically valid.
  1430. *
  1431. * The client must select a protocol. It is fatal to the connection if this
  1432. * callback returns a value other than SSL_TLSEXT_ERR_OK.
  1433. */
  1434. void SSL_CTX_set_next_proto_select_cb(SSL_CTX *ctx, int (*cb) (SSL *s, unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg), void *arg)
  1435. {
  1436. ctx->next_proto_select_cb = cb;
  1437. ctx->next_proto_select_cb_arg = arg;
  1438. }
  1439. # endif
  1440. #endif
  1441. int SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen,
  1442. const char *label, size_t llen, const unsigned char *p, size_t plen,
  1443. int use_context)
  1444. {
  1445. if (s->version < TLS1_VERSION)
  1446. return -1;
  1447. return s->method->ssl3_enc->export_keying_material(s, out, olen, label,
  1448. llen, p, plen,
  1449. use_context);
  1450. }
  1451. static unsigned long ssl_session_hash(const SSL_SESSION *a)
  1452. {
  1453. unsigned long l;
  1454. l=(unsigned long)
  1455. ((unsigned int) a->session_id[0] )|
  1456. ((unsigned int) a->session_id[1]<< 8L)|
  1457. ((unsigned long)a->session_id[2]<<16L)|
  1458. ((unsigned long)a->session_id[3]<<24L);
  1459. return(l);
  1460. }
  1461. /* NB: If this function (or indeed the hash function which uses a sort of
  1462. * coarser function than this one) is changed, ensure
  1463. * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
  1464. * able to construct an SSL_SESSION that will collide with any existing session
  1465. * with a matching session ID. */
  1466. static int ssl_session_cmp(const SSL_SESSION *a,const SSL_SESSION *b)
  1467. {
  1468. if (a->ssl_version != b->ssl_version)
  1469. return(1);
  1470. if (a->session_id_length != b->session_id_length)
  1471. return(1);
  1472. return(memcmp(a->session_id,b->session_id,a->session_id_length));
  1473. }
  1474. /* These wrapper functions should remain rather than redeclaring
  1475. * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each
  1476. * variable. The reason is that the functions aren't static, they're exposed via
  1477. * ssl.h. */
  1478. static IMPLEMENT_LHASH_HASH_FN(ssl_session, SSL_SESSION)
  1479. static IMPLEMENT_LHASH_COMP_FN(ssl_session, SSL_SESSION)
  1480. SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
  1481. {
  1482. SSL_CTX *ret=NULL;
  1483. if (meth == NULL)
  1484. {
  1485. SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_NULL_SSL_METHOD_PASSED);
  1486. return(NULL);
  1487. }
  1488. #ifdef OPENSSL_FIPS
  1489. if (FIPS_mode() && (meth->version < TLS1_VERSION))
  1490. {
  1491. SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
  1492. return NULL;
  1493. }
  1494. #endif
  1495. if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0)
  1496. {
  1497. SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
  1498. goto err;
  1499. }
  1500. ret=(SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
  1501. if (ret == NULL)
  1502. goto err;
  1503. memset(ret,0,sizeof(SSL_CTX));
  1504. ret->method=meth;
  1505. ret->cert_store=NULL;
  1506. ret->session_cache_mode=SSL_SESS_CACHE_SERVER;
  1507. ret->session_cache_size=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
  1508. ret->session_cache_head=NULL;
  1509. ret->session_cache_tail=NULL;
  1510. /* We take the system default */
  1511. ret->session_timeout=meth->get_timeout();
  1512. ret->new_session_cb=0;
  1513. ret->remove_session_cb=0;
  1514. ret->get_session_cb=0;
  1515. ret->generate_session_id=0;
  1516. memset((char *)&ret->stats,0,sizeof(ret->stats));
  1517. ret->references=1;
  1518. ret->quiet_shutdown=0;
  1519. /* ret->cipher=NULL;*/
  1520. /* ret->s2->challenge=NULL;
  1521. ret->master_key=NULL;
  1522. ret->key_arg=NULL;
  1523. ret->s2->conn_id=NULL; */
  1524. ret->info_callback=NULL;
  1525. ret->app_verify_callback=0;
  1526. ret->app_verify_arg=NULL;
  1527. ret->max_cert_list=SSL_MAX_CERT_LIST_DEFAULT;
  1528. ret->read_ahead=0;
  1529. ret->msg_callback=0;
  1530. ret->msg_callback_arg=NULL;
  1531. ret->verify_mode=SSL_VERIFY_NONE;
  1532. #if 0
  1533. ret->verify_depth=-1; /* Don't impose a limit (but x509_lu.c does) */
  1534. #endif
  1535. ret->sid_ctx_length=0;
  1536. ret->default_verify_callback=NULL;
  1537. if ((ret->cert=ssl_cert_new()) == NULL)
  1538. goto err;
  1539. ret->default_passwd_callback=0;
  1540. ret->default_passwd_callback_userdata=NULL;
  1541. ret->client_cert_cb=0;
  1542. ret->app_gen_cookie_cb=0;
  1543. ret->app_verify_cookie_cb=0;
  1544. ret->sessions=lh_SSL_SESSION_new();
  1545. if (ret->sessions == NULL) goto err;
  1546. ret->cert_store=X509_STORE_new();
  1547. if (ret->cert_store == NULL) goto err;
  1548. ssl_create_cipher_list(ret->method,
  1549. &ret->cipher_list,&ret->cipher_list_by_id,
  1550. meth->version == SSL2_VERSION ? "SSLv2" : SSL_DEFAULT_CIPHER_LIST);
  1551. if (ret->cipher_list == NULL
  1552. || sk_SSL_CIPHER_num(ret->cipher_list) <= 0)
  1553. {
  1554. SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_LIBRARY_HAS_NO_CIPHERS);
  1555. goto err2;
  1556. }
  1557. ret->param = X509_VERIFY_PARAM_new();
  1558. if (!ret->param)
  1559. goto err;
  1560. if ((ret->rsa_md5=EVP_get_digestbyname("ssl2-md5")) == NULL)
  1561. {
  1562. SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL2_MD5_ROUTINES);
  1563. goto err2;
  1564. }
  1565. if ((ret->md5=EVP_get_digestbyname("ssl3-md5")) == NULL)
  1566. {
  1567. SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES);
  1568. goto err2;
  1569. }
  1570. if ((ret->sha1=EVP_get_digestbyname("ssl3-sha1")) == NULL)
  1571. {
  1572. SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES);
  1573. goto err2;
  1574. }
  1575. if ((ret->client_CA=sk_X509_NAME_new_null()) == NULL)
  1576. goto err;
  1577. CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
  1578. ret->extra_certs=NULL;
  1579. /* No compression for DTLS */
  1580. if (meth->version != DTLS1_VERSION)
  1581. ret->comp_methods=SSL_COMP_get_compression_methods();
  1582. ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  1583. #ifndef OPENSSL_NO_TLSEXT
  1584. ret->tlsext_servername_callback = 0;
  1585. ret->tlsext_servername_arg = NULL;
  1586. /* Setup RFC4507 ticket keys */
  1587. if ((RAND_pseudo_bytes(ret->tlsext_tick_key_name, 16) <= 0)
  1588. || (RAND_bytes(ret->tlsext_tick_hmac_key, 16) <= 0)
  1589. || (RAND_bytes(ret->tlsext_tick_aes_key, 16) <= 0))
  1590. ret->options |= SSL_OP_NO_TICKET;
  1591. ret->tlsext_status_cb = 0;
  1592. ret->tlsext_status_arg = NULL;
  1593. # ifndef OPENSSL_NO_NEXTPROTONEG
  1594. ret->next_protos_advertised_cb = 0;
  1595. ret->next_proto_select_cb = 0;
  1596. # endif
  1597. #endif
  1598. #ifndef OPENSSL_NO_PSK
  1599. ret->psk_identity_hint=NULL;
  1600. ret->psk_client_callback=NULL;
  1601. ret->psk_server_callback=NULL;
  1602. #endif
  1603. #ifndef OPENSSL_NO_SRP
  1604. SSL_CTX_SRP_CTX_init(ret);
  1605. #endif
  1606. #ifndef OPENSSL_NO_BUF_FREELISTS
  1607. ret->freelist_max_len = SSL_MAX_BUF_FREELIST_LEN_DEFAULT;
  1608. ret->rbuf_freelist = OPENSSL_malloc(sizeof(SSL3_BUF_FREELIST));
  1609. if (!ret->rbuf_freelist)
  1610. goto err;
  1611. ret->rbuf_freelist->chunklen = 0;
  1612. ret->rbuf_freelist->len = 0;
  1613. ret->rbuf_freelist->head = NULL;
  1614. ret->wbuf_freelist = OPENSSL_malloc(sizeof(SSL3_BUF_FREELIST));
  1615. if (!ret->wbuf_freelist)
  1616. {
  1617. OPENSSL_free(ret->rbuf_freelist);
  1618. goto err;
  1619. }
  1620. ret->wbuf_freelist->chunklen = 0;
  1621. ret->wbuf_freelist->len = 0;
  1622. ret->wbuf_freelist->head = NULL;
  1623. #endif
  1624. #ifndef OPENSSL_NO_ENGINE
  1625. ret->client_cert_engine = NULL;
  1626. #ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO
  1627. #define eng_strx(x) #x
  1628. #define eng_str(x) eng_strx(x)
  1629. /* Use specific client engine automatically... ignore errors */
  1630. {
  1631. ENGINE *eng;
  1632. eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
  1633. if (!eng)
  1634. {
  1635. ERR_clear_error();
  1636. ENGINE_load_builtin_engines();
  1637. eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
  1638. }
  1639. if (!eng || !SSL_CTX_set_client_cert_engine(ret, eng))
  1640. ERR_clear_error();
  1641. }
  1642. #endif
  1643. #endif
  1644. /* Default is to connect to non-RI servers. When RI is more widely
  1645. * deployed might change this.
  1646. */
  1647. ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
  1648. return(ret);
  1649. err:
  1650. SSLerr(SSL_F_SSL_CTX_NEW,ERR_R_MALLOC_FAILURE);
  1651. err2:
  1652. if (ret != NULL) SSL_CTX_free(ret);
  1653. return(NULL);
  1654. }
  1655. #if 0
  1656. static void SSL_COMP_free(SSL_COMP *comp)
  1657. { OPENSSL_free(comp); }
  1658. #endif
  1659. #ifndef OPENSSL_NO_BUF_FREELISTS
  1660. static void
  1661. ssl_buf_freelist_free(SSL3_BUF_FREELIST *list)
  1662. {
  1663. SSL3_BUF_FREELIST_ENTRY *ent, *next;
  1664. for (ent = list->head; ent; ent = next)
  1665. {
  1666. next = ent->next;
  1667. OPENSSL_free(ent);
  1668. }
  1669. OPENSSL_free(list);
  1670. }
  1671. #endif
  1672. void SSL_CTX_free(SSL_CTX *a)
  1673. {
  1674. int i;
  1675. if (a == NULL) return;
  1676. i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX);
  1677. #ifdef REF_PRINT
  1678. REF_PRINT("SSL_CTX",a);
  1679. #endif
  1680. if (i > 0) return;
  1681. #ifdef REF_CHECK
  1682. if (i < 0)
  1683. {
  1684. fprintf(stderr,"SSL_CTX_free, bad reference count\n");
  1685. abort(); /* ok */
  1686. }
  1687. #endif
  1688. if (a->param)
  1689. X509_VERIFY_PARAM_free(a->param);
  1690. /*
  1691. * Free internal session cache. However: the remove_cb() may reference
  1692. * the ex_data of SSL_CTX, thus the ex_data store can only be removed
  1693. * after the sessions were flushed.
  1694. * As the ex_data handling routines might also touch the session cache,
  1695. * the most secure solution seems to be: empty (flush) the cache, then
  1696. * free ex_data, then finally free the cache.
  1697. * (See ticket [openssl.org #212].)
  1698. */
  1699. if (a->sessions != NULL)
  1700. SSL_CTX_flush_sessions(a,0);
  1701. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
  1702. if (a->sessions != NULL)
  1703. lh_SSL_SESSION_free(a->sessions);
  1704. if (a->cert_store != NULL)
  1705. X509_STORE_free(a->cert_store);
  1706. if (a->cipher_list != NULL)
  1707. sk_SSL_CIPHER_free(a->cipher_list);
  1708. if (a->cipher_list_by_id != NULL)
  1709. sk_SSL_CIPHER_free(a->cipher_list_by_id);
  1710. if (a->cert != NULL)
  1711. ssl_cert_free(a->cert);
  1712. if (a->client_CA != NULL)
  1713. sk_X509_NAME_pop_free(a->client_CA,X509_NAME_free);
  1714. if (a->extra_certs != NULL)
  1715. sk_X509_pop_free(a->extra_certs,X509_free);
  1716. #if 0 /* This should never be done, since it removes a global database */
  1717. if (a->comp_methods != NULL)
  1718. sk_SSL_COMP_pop_free(a->comp_methods,SSL_COMP_free);
  1719. #else
  1720. a->comp_methods = NULL;
  1721. #endif
  1722. #ifndef OPENSSL_NO_SRTP
  1723. if (a->srtp_profiles)
  1724. sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
  1725. #endif
  1726. #ifndef OPENSSL_NO_PSK
  1727. if (a->psk_identity_hint)
  1728. OPENSSL_free(a->psk_identity_hint);
  1729. #endif
  1730. #ifndef OPENSSL_NO_SRP
  1731. SSL_CTX_SRP_CTX_free(a);
  1732. #endif
  1733. #ifndef OPENSSL_NO_ENGINE
  1734. if (a->client_cert_engine)
  1735. ENGINE_finish(a->client_cert_engine);
  1736. #endif
  1737. #ifndef OPENSSL_NO_BUF_FREELISTS
  1738. if (a->wbuf_freelist)
  1739. ssl_buf_freelist_free(a->wbuf_freelist);
  1740. if (a->rbuf_freelist)
  1741. ssl_buf_freelist_free(a->rbuf_freelist);
  1742. #endif
  1743. OPENSSL_free(a);
  1744. }
  1745. void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
  1746. {
  1747. ctx->default_passwd_callback=cb;
  1748. }
  1749. void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx,void *u)
  1750. {
  1751. ctx->default_passwd_callback_userdata=u;
  1752. }
  1753. void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, int (*cb)(X509_STORE_CTX *,void *), void *arg)
  1754. {
  1755. ctx->app_verify_callback=cb;
  1756. ctx->app_verify_arg=arg;
  1757. }
  1758. void SSL_CTX_set_verify(SSL_CTX *ctx,int mode,int (*cb)(int, X509_STORE_CTX *))
  1759. {
  1760. ctx->verify_mode=mode;
  1761. ctx->default_verify_callback=cb;
  1762. }
  1763. void SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth)
  1764. {
  1765. X509_VERIFY_PARAM_set_depth(ctx->param, depth);
  1766. }
  1767. void ssl_set_cert_masks(CERT *c, const SSL_CIPHER *cipher)
  1768. {
  1769. CERT_PKEY *cpk;
  1770. int rsa_enc,rsa_tmp,rsa_sign,dh_tmp,dh_rsa,dh_dsa,dsa_sign;
  1771. int rsa_enc_export,dh_rsa_export,dh_dsa_export;
  1772. int rsa_tmp_export,dh_tmp_export,kl;
  1773. unsigned long mask_k,mask_a,emask_k,emask_a;
  1774. int have_ecc_cert, ecdh_ok, ecdsa_ok, ecc_pkey_size;
  1775. #ifndef OPENSSL_NO_ECDH
  1776. int have_ecdh_tmp;
  1777. #endif
  1778. X509 *x = NULL;
  1779. EVP_PKEY *ecc_pkey = NULL;
  1780. int signature_nid = 0, pk_nid = 0, md_nid = 0;
  1781. if (c == NULL) return;
  1782. kl=SSL_C_EXPORT_PKEYLENGTH(cipher);
  1783. #ifndef OPENSSL_NO_RSA
  1784. rsa_tmp=(c->rsa_tmp != NULL || c->rsa_tmp_cb != NULL);
  1785. rsa_tmp_export=(c->rsa_tmp_cb != NULL ||
  1786. (rsa_tmp && RSA_size(c->rsa_tmp)*8 <= kl));
  1787. #else
  1788. rsa_tmp=rsa_tmp_export=0;
  1789. #endif
  1790. #ifndef OPENSSL_NO_DH
  1791. dh_tmp=(c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
  1792. dh_tmp_export=(c->dh_tmp_cb != NULL ||
  1793. (dh_tmp && DH_size(c->dh_tmp)*8 <= kl));
  1794. #else
  1795. dh_tmp=dh_tmp_export=0;
  1796. #endif
  1797. #ifndef OPENSSL_NO_ECDH
  1798. have_ecdh_tmp=(c->ecdh_tmp != NULL || c->ecdh_tmp_cb != NULL);
  1799. #endif
  1800. cpk= &(c->pkeys[SSL_PKEY_RSA_ENC]);
  1801. rsa_enc= (cpk->x509 != NULL && cpk->privatekey != NULL);
  1802. rsa_enc_export=(rsa_enc && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
  1803. cpk= &(c->pkeys[SSL_PKEY_RSA_SIGN]);
  1804. rsa_sign=(cpk->x509 != NULL && cpk->privatekey != NULL);
  1805. cpk= &(c->pkeys[SSL_PKEY_DSA_SIGN]);
  1806. dsa_sign=(cpk->x509 != NULL && cpk->privatekey != NULL);
  1807. cpk= &(c->pkeys[SSL_PKEY_DH_RSA]);
  1808. dh_rsa= (cpk->x509 != NULL && cpk->privatekey != NULL);
  1809. dh_rsa_export=(dh_rsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
  1810. cpk= &(c->pkeys[SSL_PKEY_DH_DSA]);
  1811. /* FIX THIS EAY EAY EAY */
  1812. dh_dsa= (cpk->x509 != NULL && cpk->privatekey != NULL);
  1813. dh_dsa_export=(dh_dsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
  1814. cpk= &(c->pkeys[SSL_PKEY_ECC]);
  1815. have_ecc_cert= (cpk->x509 != NULL && cpk->privatekey != NULL);
  1816. mask_k=0;
  1817. mask_a=0;
  1818. emask_k=0;
  1819. emask_a=0;
  1820. #ifdef CIPHER_DEBUG
  1821. printf("rt=%d rte=%d dht=%d ecdht=%d re=%d ree=%d rs=%d ds=%d dhr=%d dhd=%d\n",
  1822. rsa_tmp,rsa_tmp_export,dh_tmp,have_ecdh_tmp,
  1823. rsa_enc,rsa_enc_export,rsa_sign,dsa_sign,dh_rsa,dh_dsa);
  1824. #endif
  1825. cpk = &(c->pkeys[SSL_PKEY_GOST01]);
  1826. if (cpk->x509 != NULL && cpk->privatekey !=NULL) {
  1827. mask_k |= SSL_kGOST;
  1828. mask_a |= SSL_aGOST01;
  1829. }
  1830. cpk = &(c->pkeys[SSL_PKEY_GOST94]);
  1831. if (cpk->x509 != NULL && cpk->privatekey !=NULL) {
  1832. mask_k |= SSL_kGOST;
  1833. mask_a |= SSL_aGOST94;
  1834. }
  1835. if (rsa_enc || (rsa_tmp && rsa_sign))
  1836. mask_k|=SSL_kRSA;
  1837. if (rsa_enc_export || (rsa_tmp_export && (rsa_sign || rsa_enc)))
  1838. emask_k|=SSL_kRSA;
  1839. #if 0
  1840. /* The match needs to be both kEDH and aRSA or aDSA, so don't worry */
  1841. if ( (dh_tmp || dh_rsa || dh_dsa) &&
  1842. (rsa_enc || rsa_sign || dsa_sign))
  1843. mask_k|=SSL_kEDH;
  1844. if ((dh_tmp_export || dh_rsa_export || dh_dsa_export) &&
  1845. (rsa_enc || rsa_sign || dsa_sign))
  1846. emask_k|=SSL_kEDH;
  1847. #endif
  1848. if (dh_tmp_export)
  1849. emask_k|=SSL_kEDH;
  1850. if (dh_tmp)
  1851. mask_k|=SSL_kEDH;
  1852. if (dh_rsa) mask_k|=SSL_kDHr;
  1853. if (dh_rsa_export) emask_k|=SSL_kDHr;
  1854. if (dh_dsa) mask_k|=SSL_kDHd;
  1855. if (dh_dsa_export) emask_k|=SSL_kDHd;
  1856. if (rsa_enc || rsa_sign)
  1857. {
  1858. mask_a|=SSL_aRSA;
  1859. emask_a|=SSL_aRSA;
  1860. }
  1861. if (dsa_sign)
  1862. {
  1863. mask_a|=SSL_aDSS;
  1864. emask_a|=SSL_aDSS;
  1865. }
  1866. mask_a|=SSL_aNULL;
  1867. emask_a|=SSL_aNULL;
  1868. #ifndef OPENSSL_NO_KRB5
  1869. mask_k|=SSL_kKRB5;
  1870. mask_a|=SSL_aKRB5;
  1871. emask_k|=SSL_kKRB5;
  1872. emask_a|=SSL_aKRB5;
  1873. #endif
  1874. /* An ECC certificate may be usable for ECDH and/or
  1875. * ECDSA cipher suites depending on the key usage extension.
  1876. */
  1877. if (have_ecc_cert)
  1878. {
  1879. /* This call populates extension flags (ex_flags) */
  1880. x = (c->pkeys[SSL_PKEY_ECC]).x509;
  1881. X509_check_purpose(x, -1, 0);
  1882. ecdh_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
  1883. (x->ex_kusage & X509v3_KU_KEY_AGREEMENT) : 1;
  1884. ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
  1885. (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) : 1;
  1886. ecc_pkey = X509_get_pubkey(x);
  1887. ecc_pkey_size = (ecc_pkey != NULL) ?
  1888. EVP_PKEY_bits(ecc_pkey) : 0;
  1889. EVP_PKEY_free(ecc_pkey);
  1890. if ((x->sig_alg) && (x->sig_alg->algorithm))
  1891. {
  1892. signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
  1893. OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
  1894. }
  1895. #ifndef OPENSSL_NO_ECDH
  1896. if (ecdh_ok)
  1897. {
  1898. if (pk_nid == NID_rsaEncryption || pk_nid == NID_rsa)
  1899. {
  1900. mask_k|=SSL_kECDHr;
  1901. mask_a|=SSL_aECDH;
  1902. if (ecc_pkey_size <= 163)
  1903. {
  1904. emask_k|=SSL_kECDHr;
  1905. emask_a|=SSL_aECDH;
  1906. }
  1907. }
  1908. if (pk_nid == NID_X9_62_id_ecPublicKey)
  1909. {
  1910. mask_k|=SSL_kECDHe;
  1911. mask_a|=SSL_aECDH;
  1912. if (ecc_pkey_size <= 163)
  1913. {
  1914. emask_k|=SSL_kECDHe;
  1915. emask_a|=SSL_aECDH;
  1916. }
  1917. }
  1918. }
  1919. #endif
  1920. #ifndef OPENSSL_NO_ECDSA
  1921. if (ecdsa_ok)
  1922. {
  1923. mask_a|=SSL_aECDSA;
  1924. emask_a|=SSL_aECDSA;
  1925. }
  1926. #endif
  1927. }
  1928. #ifndef OPENSSL_NO_ECDH
  1929. if (have_ecdh_tmp)
  1930. {
  1931. mask_k|=SSL_kEECDH;
  1932. emask_k|=SSL_kEECDH;
  1933. }
  1934. #endif
  1935. #ifndef OPENSSL_NO_PSK
  1936. mask_k |= SSL_kPSK;
  1937. mask_a |= SSL_aPSK;
  1938. emask_k |= SSL_kPSK;
  1939. emask_a |= SSL_aPSK;
  1940. #endif
  1941. c->mask_k=mask_k;
  1942. c->mask_a=mask_a;
  1943. c->export_mask_k=emask_k;
  1944. c->export_mask_a=emask_a;
  1945. c->valid=1;
  1946. }
  1947. /* This handy macro borrowed from crypto/x509v3/v3_purp.c */
  1948. #define ku_reject(x, usage) \
  1949. (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
  1950. #ifndef OPENSSL_NO_EC
  1951. int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s)
  1952. {
  1953. unsigned long alg_k, alg_a;
  1954. EVP_PKEY *pkey = NULL;
  1955. int keysize = 0;
  1956. int signature_nid = 0, md_nid = 0, pk_nid = 0;
  1957. const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
  1958. alg_k = cs->algorithm_mkey;
  1959. alg_a = cs->algorithm_auth;
  1960. if (SSL_C_IS_EXPORT(cs))
  1961. {
  1962. /* ECDH key length in export ciphers must be <= 163 bits */
  1963. pkey = X509_get_pubkey(x);
  1964. if (pkey == NULL) return 0;
  1965. keysize = EVP_PKEY_bits(pkey);
  1966. EVP_PKEY_free(pkey);
  1967. if (keysize > 163) return 0;
  1968. }
  1969. /* This call populates the ex_flags field correctly */
  1970. X509_check_purpose(x, -1, 0);
  1971. if ((x->sig_alg) && (x->sig_alg->algorithm))
  1972. {
  1973. signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
  1974. OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
  1975. }
  1976. if (alg_k & SSL_kECDHe || alg_k & SSL_kECDHr)
  1977. {
  1978. /* key usage, if present, must allow key agreement */
  1979. if (ku_reject(x, X509v3_KU_KEY_AGREEMENT))
  1980. {
  1981. SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_NOT_FOR_KEY_AGREEMENT);
  1982. return 0;
  1983. }
  1984. if ((alg_k & SSL_kECDHe) && TLS1_get_version(s) < TLS1_2_VERSION)
  1985. {
  1986. /* signature alg must be ECDSA */
  1987. if (pk_nid != NID_X9_62_id_ecPublicKey)
  1988. {
  1989. SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_SHOULD_HAVE_SHA1_SIGNATURE);
  1990. return 0;
  1991. }
  1992. }
  1993. if ((alg_k & SSL_kECDHr) && TLS1_get_version(s) < TLS1_2_VERSION)
  1994. {
  1995. /* signature alg must be RSA */
  1996. if (pk_nid != NID_rsaEncryption && pk_nid != NID_rsa)
  1997. {
  1998. SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_SHOULD_HAVE_RSA_SIGNATURE);
  1999. return 0;
  2000. }
  2001. }
  2002. }
  2003. if (alg_a & SSL_aECDSA)
  2004. {
  2005. /* key usage, if present, must allow signing */
  2006. if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE))
  2007. {
  2008. SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
  2009. return 0;
  2010. }
  2011. }
  2012. return 1; /* all checks are ok */
  2013. }
  2014. #endif
  2015. /* THIS NEEDS CLEANING UP */
  2016. CERT_PKEY *ssl_get_server_send_pkey(const SSL *s)
  2017. {
  2018. unsigned long alg_k,alg_a;
  2019. CERT *c;
  2020. int i;
  2021. c=s->cert;
  2022. ssl_set_cert_masks(c, s->s3->tmp.new_cipher);
  2023. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  2024. alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  2025. if (alg_k & (SSL_kECDHr|SSL_kECDHe))
  2026. {
  2027. /* we don't need to look at SSL_kEECDH
  2028. * since no certificate is needed for
  2029. * anon ECDH and for authenticated
  2030. * EECDH, the check for the auth
  2031. * algorithm will set i correctly
  2032. * NOTE: For ECDH-RSA, we need an ECC
  2033. * not an RSA cert but for EECDH-RSA
  2034. * we need an RSA cert. Placing the
  2035. * checks for SSL_kECDH before RSA
  2036. * checks ensures the correct cert is chosen.
  2037. */
  2038. i=SSL_PKEY_ECC;
  2039. }
  2040. else if (alg_a & SSL_aECDSA)
  2041. {
  2042. i=SSL_PKEY_ECC;
  2043. }
  2044. else if (alg_k & SSL_kDHr)
  2045. i=SSL_PKEY_DH_RSA;
  2046. else if (alg_k & SSL_kDHd)
  2047. i=SSL_PKEY_DH_DSA;
  2048. else if (alg_a & SSL_aDSS)
  2049. i=SSL_PKEY_DSA_SIGN;
  2050. else if (alg_a & SSL_aRSA)
  2051. {
  2052. if (c->pkeys[SSL_PKEY_RSA_ENC].x509 == NULL)
  2053. i=SSL_PKEY_RSA_SIGN;
  2054. else
  2055. i=SSL_PKEY_RSA_ENC;
  2056. }
  2057. else if (alg_a & SSL_aKRB5)
  2058. {
  2059. /* VRS something else here? */
  2060. return(NULL);
  2061. }
  2062. else if (alg_a & SSL_aGOST94)
  2063. i=SSL_PKEY_GOST94;
  2064. else if (alg_a & SSL_aGOST01)
  2065. i=SSL_PKEY_GOST01;
  2066. else /* if (alg_a & SSL_aNULL) */
  2067. {
  2068. SSLerr(SSL_F_SSL_GET_SERVER_SEND_PKEY,ERR_R_INTERNAL_ERROR);
  2069. return(NULL);
  2070. }
  2071. return c->pkeys + i;
  2072. }
  2073. X509 *ssl_get_server_send_cert(const SSL *s)
  2074. {
  2075. CERT_PKEY *cpk;
  2076. cpk = ssl_get_server_send_pkey(s);
  2077. if (!cpk)
  2078. return NULL;
  2079. return cpk->x509;
  2080. }
  2081. EVP_PKEY *ssl_get_sign_pkey(SSL *s,const SSL_CIPHER *cipher, const EVP_MD **pmd)
  2082. {
  2083. unsigned long alg_a;
  2084. CERT *c;
  2085. int idx = -1;
  2086. alg_a = cipher->algorithm_auth;
  2087. c=s->cert;
  2088. if ((alg_a & SSL_aDSS) &&
  2089. (c->pkeys[SSL_PKEY_DSA_SIGN].privatekey != NULL))
  2090. idx = SSL_PKEY_DSA_SIGN;
  2091. else if (alg_a & SSL_aRSA)
  2092. {
  2093. if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL)
  2094. idx = SSL_PKEY_RSA_SIGN;
  2095. else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL)
  2096. idx = SSL_PKEY_RSA_ENC;
  2097. }
  2098. else if ((alg_a & SSL_aECDSA) &&
  2099. (c->pkeys[SSL_PKEY_ECC].privatekey != NULL))
  2100. idx = SSL_PKEY_ECC;
  2101. if (idx == -1)
  2102. {
  2103. SSLerr(SSL_F_SSL_GET_SIGN_PKEY,ERR_R_INTERNAL_ERROR);
  2104. return(NULL);
  2105. }
  2106. if (pmd)
  2107. *pmd = c->pkeys[idx].digest;
  2108. return c->pkeys[idx].privatekey;
  2109. }
  2110. void ssl_update_cache(SSL *s,int mode)
  2111. {
  2112. int i;
  2113. /* If the session_id_length is 0, we are not supposed to cache it,
  2114. * and it would be rather hard to do anyway :-) */
  2115. if (s->session->session_id_length == 0) return;
  2116. i=s->session_ctx->session_cache_mode;
  2117. if ((i & mode) && (!s->hit)
  2118. && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE)
  2119. || SSL_CTX_add_session(s->session_ctx,s->session))
  2120. && (s->session_ctx->new_session_cb != NULL))
  2121. {
  2122. CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION);
  2123. if (!s->session_ctx->new_session_cb(s,s->session))
  2124. SSL_SESSION_free(s->session);
  2125. }
  2126. /* auto flush every 255 connections */
  2127. if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) &&
  2128. ((i & mode) == mode))
  2129. {
  2130. if ( (((mode & SSL_SESS_CACHE_CLIENT)
  2131. ?s->session_ctx->stats.sess_connect_good
  2132. :s->session_ctx->stats.sess_accept_good) & 0xff) == 0xff)
  2133. {
  2134. SSL_CTX_flush_sessions(s->session_ctx,(unsigned long)time(NULL));
  2135. }
  2136. }
  2137. }
  2138. const SSL_METHOD *SSL_get_ssl_method(SSL *s)
  2139. {
  2140. return(s->method);
  2141. }
  2142. int SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth)
  2143. {
  2144. int conn= -1;
  2145. int ret=1;
  2146. if (s->method != meth)
  2147. {
  2148. if (s->handshake_func != NULL)
  2149. conn=(s->handshake_func == s->method->ssl_connect);
  2150. if (s->method->version == meth->version)
  2151. s->method=meth;
  2152. else
  2153. {
  2154. s->method->ssl_free(s);
  2155. s->method=meth;
  2156. ret=s->method->ssl_new(s);
  2157. }
  2158. if (conn == 1)
  2159. s->handshake_func=meth->ssl_connect;
  2160. else if (conn == 0)
  2161. s->handshake_func=meth->ssl_accept;
  2162. }
  2163. return(ret);
  2164. }
  2165. int SSL_get_error(const SSL *s,int i)
  2166. {
  2167. int reason;
  2168. unsigned long l;
  2169. BIO *bio;
  2170. if (i > 0) return(SSL_ERROR_NONE);
  2171. /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
  2172. * etc, where we do encode the error */
  2173. if ((l=ERR_peek_error()) != 0)
  2174. {
  2175. if (ERR_GET_LIB(l) == ERR_LIB_SYS)
  2176. return(SSL_ERROR_SYSCALL);
  2177. else
  2178. return(SSL_ERROR_SSL);
  2179. }
  2180. if ((i < 0) && SSL_want_read(s))
  2181. {
  2182. bio=SSL_get_rbio(s);
  2183. if (BIO_should_read(bio))
  2184. return(SSL_ERROR_WANT_READ);
  2185. else if (BIO_should_write(bio))
  2186. /* This one doesn't make too much sense ... We never try
  2187. * to write to the rbio, and an application program where
  2188. * rbio and wbio are separate couldn't even know what it
  2189. * should wait for.
  2190. * However if we ever set s->rwstate incorrectly
  2191. * (so that we have SSL_want_read(s) instead of
  2192. * SSL_want_write(s)) and rbio and wbio *are* the same,
  2193. * this test works around that bug; so it might be safer
  2194. * to keep it. */
  2195. return(SSL_ERROR_WANT_WRITE);
  2196. else if (BIO_should_io_special(bio))
  2197. {
  2198. reason=BIO_get_retry_reason(bio);
  2199. if (reason == BIO_RR_CONNECT)
  2200. return(SSL_ERROR_WANT_CONNECT);
  2201. else if (reason == BIO_RR_ACCEPT)
  2202. return(SSL_ERROR_WANT_ACCEPT);
  2203. else
  2204. return(SSL_ERROR_SYSCALL); /* unknown */
  2205. }
  2206. }
  2207. if ((i < 0) && SSL_want_write(s))
  2208. {
  2209. bio=SSL_get_wbio(s);
  2210. if (BIO_should_write(bio))
  2211. return(SSL_ERROR_WANT_WRITE);
  2212. else if (BIO_should_read(bio))
  2213. /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
  2214. return(SSL_ERROR_WANT_READ);
  2215. else if (BIO_should_io_special(bio))
  2216. {
  2217. reason=BIO_get_retry_reason(bio);
  2218. if (reason == BIO_RR_CONNECT)
  2219. return(SSL_ERROR_WANT_CONNECT);
  2220. else if (reason == BIO_RR_ACCEPT)
  2221. return(SSL_ERROR_WANT_ACCEPT);
  2222. else
  2223. return(SSL_ERROR_SYSCALL);
  2224. }
  2225. }
  2226. if ((i < 0) && SSL_want_x509_lookup(s))
  2227. {
  2228. return(SSL_ERROR_WANT_X509_LOOKUP);
  2229. }
  2230. if (i == 0)
  2231. {
  2232. if (s->version == SSL2_VERSION)
  2233. {
  2234. /* assume it is the socket being closed */
  2235. return(SSL_ERROR_ZERO_RETURN);
  2236. }
  2237. else
  2238. {
  2239. if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
  2240. (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
  2241. return(SSL_ERROR_ZERO_RETURN);
  2242. }
  2243. }
  2244. return(SSL_ERROR_SYSCALL);
  2245. }
  2246. int SSL_do_handshake(SSL *s)
  2247. {
  2248. int ret=1;
  2249. if (s->handshake_func == NULL)
  2250. {
  2251. SSLerr(SSL_F_SSL_DO_HANDSHAKE,SSL_R_CONNECTION_TYPE_NOT_SET);
  2252. return(-1);
  2253. }
  2254. s->method->ssl_renegotiate_check(s);
  2255. if (SSL_in_init(s) || SSL_in_before(s))
  2256. {
  2257. ret=s->handshake_func(s);
  2258. }
  2259. return(ret);
  2260. }
  2261. /* For the next 2 functions, SSL_clear() sets shutdown and so
  2262. * one of these calls will reset it */
  2263. void SSL_set_accept_state(SSL *s)
  2264. {
  2265. s->server=1;
  2266. s->shutdown=0;
  2267. s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE;
  2268. s->handshake_func=s->method->ssl_accept;
  2269. /* clear the current cipher */
  2270. ssl_clear_cipher_ctx(s);
  2271. ssl_clear_hash_ctx(&s->read_hash);
  2272. ssl_clear_hash_ctx(&s->write_hash);
  2273. }
  2274. void SSL_set_connect_state(SSL *s)
  2275. {
  2276. s->server=0;
  2277. s->shutdown=0;
  2278. s->state=SSL_ST_CONNECT|SSL_ST_BEFORE;
  2279. s->handshake_func=s->method->ssl_connect;
  2280. /* clear the current cipher */
  2281. ssl_clear_cipher_ctx(s);
  2282. ssl_clear_hash_ctx(&s->read_hash);
  2283. ssl_clear_hash_ctx(&s->write_hash);
  2284. }
  2285. int ssl_undefined_function(SSL *s)
  2286. {
  2287. SSLerr(SSL_F_SSL_UNDEFINED_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  2288. return(0);
  2289. }
  2290. int ssl_undefined_void_function(void)
  2291. {
  2292. SSLerr(SSL_F_SSL_UNDEFINED_VOID_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  2293. return(0);
  2294. }
  2295. int ssl_undefined_const_function(const SSL *s)
  2296. {
  2297. SSLerr(SSL_F_SSL_UNDEFINED_CONST_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  2298. return(0);
  2299. }
  2300. SSL_METHOD *ssl_bad_method(int ver)
  2301. {
  2302. SSLerr(SSL_F_SSL_BAD_METHOD,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  2303. return(NULL);
  2304. }
  2305. const char *SSL_get_version(const SSL *s)
  2306. {
  2307. if (s->version == TLS1_2_VERSION)
  2308. return("TLSv1.2");
  2309. else if (s->version == TLS1_1_VERSION)
  2310. return("TLSv1.1");
  2311. else if (s->version == TLS1_VERSION)
  2312. return("TLSv1");
  2313. else if (s->version == SSL3_VERSION)
  2314. return("SSLv3");
  2315. else if (s->version == SSL2_VERSION)
  2316. return("SSLv2");
  2317. else
  2318. return("unknown");
  2319. }
  2320. SSL *SSL_dup(SSL *s)
  2321. {
  2322. STACK_OF(X509_NAME) *sk;
  2323. X509_NAME *xn;
  2324. SSL *ret;
  2325. int i;
  2326. if ((ret=SSL_new(SSL_get_SSL_CTX(s))) == NULL)
  2327. return(NULL);
  2328. ret->version = s->version;
  2329. ret->type = s->type;
  2330. ret->method = s->method;
  2331. if (s->session != NULL)
  2332. {
  2333. /* This copies session-id, SSL_METHOD, sid_ctx, and 'cert' */
  2334. SSL_copy_session_id(ret,s);
  2335. }
  2336. else
  2337. {
  2338. /* No session has been established yet, so we have to expect
  2339. * that s->cert or ret->cert will be changed later --
  2340. * they should not both point to the same object,
  2341. * and thus we can't use SSL_copy_session_id. */
  2342. ret->method->ssl_free(ret);
  2343. ret->method = s->method;
  2344. ret->method->ssl_new(ret);
  2345. if (s->cert != NULL)
  2346. {
  2347. if (ret->cert != NULL)
  2348. {
  2349. ssl_cert_free(ret->cert);
  2350. }
  2351. ret->cert = ssl_cert_dup(s->cert);
  2352. if (ret->cert == NULL)
  2353. goto err;
  2354. }
  2355. SSL_set_session_id_context(ret,
  2356. s->sid_ctx, s->sid_ctx_length);
  2357. }
  2358. ret->options=s->options;
  2359. ret->mode=s->mode;
  2360. SSL_set_max_cert_list(ret,SSL_get_max_cert_list(s));
  2361. SSL_set_read_ahead(ret,SSL_get_read_ahead(s));
  2362. ret->msg_callback = s->msg_callback;
  2363. ret->msg_callback_arg = s->msg_callback_arg;
  2364. SSL_set_verify(ret,SSL_get_verify_mode(s),
  2365. SSL_get_verify_callback(s));
  2366. SSL_set_verify_depth(ret,SSL_get_verify_depth(s));
  2367. ret->generate_session_id = s->generate_session_id;
  2368. SSL_set_info_callback(ret,SSL_get_info_callback(s));
  2369. ret->debug=s->debug;
  2370. /* copy app data, a little dangerous perhaps */
  2371. if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL, &ret->ex_data, &s->ex_data))
  2372. goto err;
  2373. /* setup rbio, and wbio */
  2374. if (s->rbio != NULL)
  2375. {
  2376. if (!BIO_dup_state(s->rbio,(char *)&ret->rbio))
  2377. goto err;
  2378. }
  2379. if (s->wbio != NULL)
  2380. {
  2381. if (s->wbio != s->rbio)
  2382. {
  2383. if (!BIO_dup_state(s->wbio,(char *)&ret->wbio))
  2384. goto err;
  2385. }
  2386. else
  2387. ret->wbio=ret->rbio;
  2388. }
  2389. ret->rwstate = s->rwstate;
  2390. ret->in_handshake = s->in_handshake;
  2391. ret->handshake_func = s->handshake_func;
  2392. ret->server = s->server;
  2393. ret->renegotiate = s->renegotiate;
  2394. ret->new_session = s->new_session;
  2395. ret->quiet_shutdown = s->quiet_shutdown;
  2396. ret->shutdown=s->shutdown;
  2397. ret->state=s->state; /* SSL_dup does not really work at any state, though */
  2398. ret->rstate=s->rstate;
  2399. ret->init_num = 0; /* would have to copy ret->init_buf, ret->init_msg, ret->init_num, ret->init_off */
  2400. ret->hit=s->hit;
  2401. X509_VERIFY_PARAM_inherit(ret->param, s->param);
  2402. /* dup the cipher_list and cipher_list_by_id stacks */
  2403. if (s->cipher_list != NULL)
  2404. {
  2405. if ((ret->cipher_list=sk_SSL_CIPHER_dup(s->cipher_list)) == NULL)
  2406. goto err;
  2407. }
  2408. if (s->cipher_list_by_id != NULL)
  2409. if ((ret->cipher_list_by_id=sk_SSL_CIPHER_dup(s->cipher_list_by_id))
  2410. == NULL)
  2411. goto err;
  2412. /* Dup the client_CA list */
  2413. if (s->client_CA != NULL)
  2414. {
  2415. if ((sk=sk_X509_NAME_dup(s->client_CA)) == NULL) goto err;
  2416. ret->client_CA=sk;
  2417. for (i=0; i<sk_X509_NAME_num(sk); i++)
  2418. {
  2419. xn=sk_X509_NAME_value(sk,i);
  2420. if (sk_X509_NAME_set(sk,i,X509_NAME_dup(xn)) == NULL)
  2421. {
  2422. X509_NAME_free(xn);
  2423. goto err;
  2424. }
  2425. }
  2426. }
  2427. if (0)
  2428. {
  2429. err:
  2430. if (ret != NULL) SSL_free(ret);
  2431. ret=NULL;
  2432. }
  2433. return(ret);
  2434. }
  2435. void ssl_clear_cipher_ctx(SSL *s)
  2436. {
  2437. if (s->enc_read_ctx != NULL)
  2438. {
  2439. EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
  2440. OPENSSL_free(s->enc_read_ctx);
  2441. s->enc_read_ctx=NULL;
  2442. }
  2443. if (s->enc_write_ctx != NULL)
  2444. {
  2445. EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
  2446. OPENSSL_free(s->enc_write_ctx);
  2447. s->enc_write_ctx=NULL;
  2448. }
  2449. #ifndef OPENSSL_NO_COMP
  2450. if (s->expand != NULL)
  2451. {
  2452. COMP_CTX_free(s->expand);
  2453. s->expand=NULL;
  2454. }
  2455. if (s->compress != NULL)
  2456. {
  2457. COMP_CTX_free(s->compress);
  2458. s->compress=NULL;
  2459. }
  2460. #endif
  2461. }
  2462. /* Fix this function so that it takes an optional type parameter */
  2463. X509 *SSL_get_certificate(const SSL *s)
  2464. {
  2465. if (s->cert != NULL)
  2466. return(s->cert->key->x509);
  2467. else
  2468. return(NULL);
  2469. }
  2470. /* Fix this function so that it takes an optional type parameter */
  2471. EVP_PKEY *SSL_get_privatekey(SSL *s)
  2472. {
  2473. if (s->cert != NULL)
  2474. return(s->cert->key->privatekey);
  2475. else
  2476. return(NULL);
  2477. }
  2478. const SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
  2479. {
  2480. if ((s->session != NULL) && (s->session->cipher != NULL))
  2481. return(s->session->cipher);
  2482. return(NULL);
  2483. }
  2484. #ifdef OPENSSL_NO_COMP
  2485. const void *SSL_get_current_compression(SSL *s)
  2486. {
  2487. return NULL;
  2488. }
  2489. const void *SSL_get_current_expansion(SSL *s)
  2490. {
  2491. return NULL;
  2492. }
  2493. #else
  2494. const COMP_METHOD *SSL_get_current_compression(SSL *s)
  2495. {
  2496. if (s->compress != NULL)
  2497. return(s->compress->meth);
  2498. return(NULL);
  2499. }
  2500. const COMP_METHOD *SSL_get_current_expansion(SSL *s)
  2501. {
  2502. if (s->expand != NULL)
  2503. return(s->expand->meth);
  2504. return(NULL);
  2505. }
  2506. #endif
  2507. int ssl_init_wbio_buffer(SSL *s,int push)
  2508. {
  2509. BIO *bbio;
  2510. if (s->bbio == NULL)
  2511. {
  2512. bbio=BIO_new(BIO_f_buffer());
  2513. if (bbio == NULL) return(0);
  2514. s->bbio=bbio;
  2515. }
  2516. else
  2517. {
  2518. bbio=s->bbio;
  2519. if (s->bbio == s->wbio)
  2520. s->wbio=BIO_pop(s->wbio);
  2521. }
  2522. (void)BIO_reset(bbio);
  2523. /* if (!BIO_set_write_buffer_size(bbio,16*1024)) */
  2524. if (!BIO_set_read_buffer_size(bbio,1))
  2525. {
  2526. SSLerr(SSL_F_SSL_INIT_WBIO_BUFFER,ERR_R_BUF_LIB);
  2527. return(0);
  2528. }
  2529. if (push)
  2530. {
  2531. if (s->wbio != bbio)
  2532. s->wbio=BIO_push(bbio,s->wbio);
  2533. }
  2534. else
  2535. {
  2536. if (s->wbio == bbio)
  2537. s->wbio=BIO_pop(bbio);
  2538. }
  2539. return(1);
  2540. }
  2541. void ssl_free_wbio_buffer(SSL *s)
  2542. {
  2543. if (s->bbio == NULL) return;
  2544. if (s->bbio == s->wbio)
  2545. {
  2546. /* remove buffering */
  2547. s->wbio=BIO_pop(s->wbio);
  2548. #ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids adding one more preprocessor symbol */
  2549. assert(s->wbio != NULL);
  2550. #endif
  2551. }
  2552. BIO_free(s->bbio);
  2553. s->bbio=NULL;
  2554. }
  2555. void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx,int mode)
  2556. {
  2557. ctx->quiet_shutdown=mode;
  2558. }
  2559. int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
  2560. {
  2561. return(ctx->quiet_shutdown);
  2562. }
  2563. void SSL_set_quiet_shutdown(SSL *s,int mode)
  2564. {
  2565. s->quiet_shutdown=mode;
  2566. }
  2567. int SSL_get_quiet_shutdown(const SSL *s)
  2568. {
  2569. return(s->quiet_shutdown);
  2570. }
  2571. void SSL_set_shutdown(SSL *s,int mode)
  2572. {
  2573. s->shutdown=mode;
  2574. }
  2575. int SSL_get_shutdown(const SSL *s)
  2576. {
  2577. return(s->shutdown);
  2578. }
  2579. int SSL_version(const SSL *s)
  2580. {
  2581. return(s->version);
  2582. }
  2583. SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
  2584. {
  2585. return(ssl->ctx);
  2586. }
  2587. SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx)
  2588. {
  2589. if (ssl->ctx == ctx)
  2590. return ssl->ctx;
  2591. #ifndef OPENSSL_NO_TLSEXT
  2592. if (ctx == NULL)
  2593. ctx = ssl->initial_ctx;
  2594. #endif
  2595. if (ssl->cert != NULL)
  2596. ssl_cert_free(ssl->cert);
  2597. ssl->cert = ssl_cert_dup(ctx->cert);
  2598. CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
  2599. if (ssl->ctx != NULL)
  2600. SSL_CTX_free(ssl->ctx); /* decrement reference count */
  2601. ssl->ctx = ctx;
  2602. return(ssl->ctx);
  2603. }
  2604. #ifndef OPENSSL_NO_STDIO
  2605. int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
  2606. {
  2607. return(X509_STORE_set_default_paths(ctx->cert_store));
  2608. }
  2609. int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
  2610. const char *CApath)
  2611. {
  2612. return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath));
  2613. }
  2614. #endif
  2615. void SSL_set_info_callback(SSL *ssl,
  2616. void (*cb)(const SSL *ssl,int type,int val))
  2617. {
  2618. ssl->info_callback=cb;
  2619. }
  2620. /* One compiler (Diab DCC) doesn't like argument names in returned
  2621. function pointer. */
  2622. void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/,int /*type*/,int /*val*/)
  2623. {
  2624. return ssl->info_callback;
  2625. }
  2626. int SSL_state(const SSL *ssl)
  2627. {
  2628. return(ssl->state);
  2629. }
  2630. void SSL_set_state(SSL *ssl, int state)
  2631. {
  2632. ssl->state = state;
  2633. }
  2634. void SSL_set_verify_result(SSL *ssl,long arg)
  2635. {
  2636. ssl->verify_result=arg;
  2637. }
  2638. long SSL_get_verify_result(const SSL *ssl)
  2639. {
  2640. return(ssl->verify_result);
  2641. }
  2642. int SSL_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
  2643. CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
  2644. {
  2645. return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp,
  2646. new_func, dup_func, free_func);
  2647. }
  2648. int SSL_set_ex_data(SSL *s,int idx,void *arg)
  2649. {
  2650. return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
  2651. }
  2652. void *SSL_get_ex_data(const SSL *s,int idx)
  2653. {
  2654. return(CRYPTO_get_ex_data(&s->ex_data,idx));
  2655. }
  2656. int SSL_CTX_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
  2657. CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
  2658. {
  2659. return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp,
  2660. new_func, dup_func, free_func);
  2661. }
  2662. int SSL_CTX_set_ex_data(SSL_CTX *s,int idx,void *arg)
  2663. {
  2664. return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
  2665. }
  2666. void *SSL_CTX_get_ex_data(const SSL_CTX *s,int idx)
  2667. {
  2668. return(CRYPTO_get_ex_data(&s->ex_data,idx));
  2669. }
  2670. int ssl_ok(SSL *s)
  2671. {
  2672. return(1);
  2673. }
  2674. X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
  2675. {
  2676. return(ctx->cert_store);
  2677. }
  2678. void SSL_CTX_set_cert_store(SSL_CTX *ctx,X509_STORE *store)
  2679. {
  2680. if (ctx->cert_store != NULL)
  2681. X509_STORE_free(ctx->cert_store);
  2682. ctx->cert_store=store;
  2683. }
  2684. int SSL_want(const SSL *s)
  2685. {
  2686. return(s->rwstate);
  2687. }
  2688. /*!
  2689. * \brief Set the callback for generating temporary RSA keys.
  2690. * \param ctx the SSL context.
  2691. * \param cb the callback
  2692. */
  2693. #ifndef OPENSSL_NO_RSA
  2694. void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl,
  2695. int is_export,
  2696. int keylength))
  2697. {
  2698. SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
  2699. }
  2700. void SSL_set_tmp_rsa_callback(SSL *ssl,RSA *(*cb)(SSL *ssl,
  2701. int is_export,
  2702. int keylength))
  2703. {
  2704. SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
  2705. }
  2706. #endif
  2707. #ifdef DOXYGEN
  2708. /*!
  2709. * \brief The RSA temporary key callback function.
  2710. * \param ssl the SSL session.
  2711. * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite.
  2712. * \param keylength if \c is_export is \c TRUE, then \c keylength is the size
  2713. * of the required key in bits.
  2714. * \return the temporary RSA key.
  2715. * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback
  2716. */
  2717. RSA *cb(SSL *ssl,int is_export,int keylength)
  2718. {}
  2719. #endif
  2720. /*!
  2721. * \brief Set the callback for generating temporary DH keys.
  2722. * \param ctx the SSL context.
  2723. * \param dh the callback
  2724. */
  2725. #ifndef OPENSSL_NO_DH
  2726. void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int is_export,
  2727. int keylength))
  2728. {
  2729. SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
  2730. }
  2731. void SSL_set_tmp_dh_callback(SSL *ssl,DH *(*dh)(SSL *ssl,int is_export,
  2732. int keylength))
  2733. {
  2734. SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
  2735. }
  2736. #endif
  2737. #ifndef OPENSSL_NO_ECDH
  2738. void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
  2739. int keylength))
  2740. {
  2741. SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
  2742. }
  2743. void SSL_set_tmp_ecdh_callback(SSL *ssl,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
  2744. int keylength))
  2745. {
  2746. SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
  2747. }
  2748. #endif
  2749. #ifndef OPENSSL_NO_PSK
  2750. int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint)
  2751. {
  2752. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
  2753. {
  2754. SSLerr(SSL_F_SSL_CTX_USE_PSK_IDENTITY_HINT, SSL_R_DATA_LENGTH_TOO_LONG);
  2755. return 0;
  2756. }
  2757. if (ctx->psk_identity_hint != NULL)
  2758. OPENSSL_free(ctx->psk_identity_hint);
  2759. if (identity_hint != NULL)
  2760. {
  2761. ctx->psk_identity_hint = BUF_strdup(identity_hint);
  2762. if (ctx->psk_identity_hint == NULL)
  2763. return 0;
  2764. }
  2765. else
  2766. ctx->psk_identity_hint = NULL;
  2767. return 1;
  2768. }
  2769. int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint)
  2770. {
  2771. if (s == NULL)
  2772. return 0;
  2773. if (s->session == NULL)
  2774. return 1; /* session not created yet, ignored */
  2775. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
  2776. {
  2777. SSLerr(SSL_F_SSL_USE_PSK_IDENTITY_HINT, SSL_R_DATA_LENGTH_TOO_LONG);
  2778. return 0;
  2779. }
  2780. if (s->session->psk_identity_hint != NULL)
  2781. OPENSSL_free(s->session->psk_identity_hint);
  2782. if (identity_hint != NULL)
  2783. {
  2784. s->session->psk_identity_hint = BUF_strdup(identity_hint);
  2785. if (s->session->psk_identity_hint == NULL)
  2786. return 0;
  2787. }
  2788. else
  2789. s->session->psk_identity_hint = NULL;
  2790. return 1;
  2791. }
  2792. const char *SSL_get_psk_identity_hint(const SSL *s)
  2793. {
  2794. if (s == NULL || s->session == NULL)
  2795. return NULL;
  2796. return(s->session->psk_identity_hint);
  2797. }
  2798. const char *SSL_get_psk_identity(const SSL *s)
  2799. {
  2800. if (s == NULL || s->session == NULL)
  2801. return NULL;
  2802. return(s->session->psk_identity);
  2803. }
  2804. void SSL_set_psk_client_callback(SSL *s,
  2805. unsigned int (*cb)(SSL *ssl, const char *hint,
  2806. char *identity, unsigned int max_identity_len, unsigned char *psk,
  2807. unsigned int max_psk_len))
  2808. {
  2809. s->psk_client_callback = cb;
  2810. }
  2811. void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx,
  2812. unsigned int (*cb)(SSL *ssl, const char *hint,
  2813. char *identity, unsigned int max_identity_len, unsigned char *psk,
  2814. unsigned int max_psk_len))
  2815. {
  2816. ctx->psk_client_callback = cb;
  2817. }
  2818. void SSL_set_psk_server_callback(SSL *s,
  2819. unsigned int (*cb)(SSL *ssl, const char *identity,
  2820. unsigned char *psk, unsigned int max_psk_len))
  2821. {
  2822. s->psk_server_callback = cb;
  2823. }
  2824. void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx,
  2825. unsigned int (*cb)(SSL *ssl, const char *identity,
  2826. unsigned char *psk, unsigned int max_psk_len))
  2827. {
  2828. ctx->psk_server_callback = cb;
  2829. }
  2830. #endif
  2831. void SSL_CTX_set_msg_callback(SSL_CTX *ctx, void (*cb)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))
  2832. {
  2833. SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
  2834. }
  2835. void SSL_set_msg_callback(SSL *ssl, void (*cb)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))
  2836. {
  2837. SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
  2838. }
  2839. /* Allocates new EVP_MD_CTX and sets pointer to it into given pointer
  2840. * vairable, freeing EVP_MD_CTX previously stored in that variable, if
  2841. * any. If EVP_MD pointer is passed, initializes ctx with this md
  2842. * Returns newly allocated ctx;
  2843. */
  2844. EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash,const EVP_MD *md)
  2845. {
  2846. ssl_clear_hash_ctx(hash);
  2847. *hash = EVP_MD_CTX_create();
  2848. if (md) EVP_DigestInit_ex(*hash,md,NULL);
  2849. return *hash;
  2850. }
  2851. void ssl_clear_hash_ctx(EVP_MD_CTX **hash)
  2852. {
  2853. if (*hash) EVP_MD_CTX_destroy(*hash);
  2854. *hash=NULL;
  2855. }
  2856. void SSL_set_debug(SSL *s, int debug)
  2857. {
  2858. s->debug = debug;
  2859. }
  2860. int SSL_cache_hit(SSL *s)
  2861. {
  2862. return s->hit;
  2863. }
  2864. #if defined(_WINDLL) && defined(OPENSSL_SYS_WIN16)
  2865. #include "../crypto/bio/bss_file.c"
  2866. #endif
  2867. IMPLEMENT_STACK_OF(SSL_CIPHER)
  2868. IMPLEMENT_STACK_OF(SSL_COMP)
  2869. IMPLEMENT_OBJ_BSEARCH_GLOBAL_CMP_FN(SSL_CIPHER, SSL_CIPHER,
  2870. ssl_cipher_id);