ssl_lib.c 81 KB

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