ssl_lib.c 80 KB

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