ssl_lib.c 71 KB

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