ssl_lib.c 80 KB

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