ssl_lib.c 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235
  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. /* Disable TLS v1.2 by default for now */
  1615. ret->options |= SSL_OP_NO_TLSv1_2;
  1616. return(ret);
  1617. err:
  1618. SSLerr(SSL_F_SSL_CTX_NEW,ERR_R_MALLOC_FAILURE);
  1619. err2:
  1620. if (ret != NULL) SSL_CTX_free(ret);
  1621. return(NULL);
  1622. }
  1623. #if 0
  1624. static void SSL_COMP_free(SSL_COMP *comp)
  1625. { OPENSSL_free(comp); }
  1626. #endif
  1627. #ifndef OPENSSL_NO_BUF_FREELISTS
  1628. static void
  1629. ssl_buf_freelist_free(SSL3_BUF_FREELIST *list)
  1630. {
  1631. SSL3_BUF_FREELIST_ENTRY *ent, *next;
  1632. for (ent = list->head; ent; ent = next)
  1633. {
  1634. next = ent->next;
  1635. OPENSSL_free(ent);
  1636. }
  1637. OPENSSL_free(list);
  1638. }
  1639. #endif
  1640. void SSL_CTX_free(SSL_CTX *a)
  1641. {
  1642. int i;
  1643. if (a == NULL) return;
  1644. i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX);
  1645. #ifdef REF_PRINT
  1646. REF_PRINT("SSL_CTX",a);
  1647. #endif
  1648. if (i > 0) return;
  1649. #ifdef REF_CHECK
  1650. if (i < 0)
  1651. {
  1652. fprintf(stderr,"SSL_CTX_free, bad reference count\n");
  1653. abort(); /* ok */
  1654. }
  1655. #endif
  1656. if (a->param)
  1657. X509_VERIFY_PARAM_free(a->param);
  1658. /*
  1659. * Free internal session cache. However: the remove_cb() may reference
  1660. * the ex_data of SSL_CTX, thus the ex_data store can only be removed
  1661. * after the sessions were flushed.
  1662. * As the ex_data handling routines might also touch the session cache,
  1663. * the most secure solution seems to be: empty (flush) the cache, then
  1664. * free ex_data, then finally free the cache.
  1665. * (See ticket [openssl.org #212].)
  1666. */
  1667. if (a->sessions != NULL)
  1668. SSL_CTX_flush_sessions(a,0);
  1669. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
  1670. if (a->sessions != NULL)
  1671. lh_SSL_SESSION_free(a->sessions);
  1672. if (a->cert_store != NULL)
  1673. X509_STORE_free(a->cert_store);
  1674. if (a->cipher_list != NULL)
  1675. sk_SSL_CIPHER_free(a->cipher_list);
  1676. if (a->cipher_list_by_id != NULL)
  1677. sk_SSL_CIPHER_free(a->cipher_list_by_id);
  1678. if (a->cert != NULL)
  1679. ssl_cert_free(a->cert);
  1680. if (a->client_CA != NULL)
  1681. sk_X509_NAME_pop_free(a->client_CA,X509_NAME_free);
  1682. if (a->extra_certs != NULL)
  1683. sk_X509_pop_free(a->extra_certs,X509_free);
  1684. #if 0 /* This should never be done, since it removes a global database */
  1685. if (a->comp_methods != NULL)
  1686. sk_SSL_COMP_pop_free(a->comp_methods,SSL_COMP_free);
  1687. #else
  1688. a->comp_methods = NULL;
  1689. #endif
  1690. #ifndef OPENSSL_NO_PSK
  1691. if (a->psk_identity_hint)
  1692. OPENSSL_free(a->psk_identity_hint);
  1693. #endif
  1694. #ifndef OPENSSL_NO_SRP
  1695. SSL_CTX_SRP_CTX_free(a);
  1696. #endif
  1697. #ifndef OPENSSL_NO_ENGINE
  1698. if (a->client_cert_engine)
  1699. ENGINE_finish(a->client_cert_engine);
  1700. #endif
  1701. #ifndef OPENSSL_NO_BUF_FREELISTS
  1702. if (a->wbuf_freelist)
  1703. ssl_buf_freelist_free(a->wbuf_freelist);
  1704. if (a->rbuf_freelist)
  1705. ssl_buf_freelist_free(a->rbuf_freelist);
  1706. #endif
  1707. OPENSSL_free(a);
  1708. }
  1709. void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
  1710. {
  1711. ctx->default_passwd_callback=cb;
  1712. }
  1713. void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx,void *u)
  1714. {
  1715. ctx->default_passwd_callback_userdata=u;
  1716. }
  1717. void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, int (*cb)(X509_STORE_CTX *,void *), void *arg)
  1718. {
  1719. ctx->app_verify_callback=cb;
  1720. ctx->app_verify_arg=arg;
  1721. }
  1722. void SSL_CTX_set_verify(SSL_CTX *ctx,int mode,int (*cb)(int, X509_STORE_CTX *))
  1723. {
  1724. ctx->verify_mode=mode;
  1725. ctx->default_verify_callback=cb;
  1726. }
  1727. void SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth)
  1728. {
  1729. X509_VERIFY_PARAM_set_depth(ctx->param, depth);
  1730. }
  1731. void ssl_set_cert_masks(CERT *c, const SSL_CIPHER *cipher)
  1732. {
  1733. CERT_PKEY *cpk;
  1734. int rsa_enc,rsa_tmp,rsa_sign,dh_tmp,dh_rsa,dh_dsa,dsa_sign;
  1735. int rsa_enc_export,dh_rsa_export,dh_dsa_export;
  1736. int rsa_tmp_export,dh_tmp_export,kl;
  1737. unsigned long mask_k,mask_a,emask_k,emask_a;
  1738. int have_ecc_cert, ecdh_ok, ecdsa_ok, ecc_pkey_size;
  1739. #ifndef OPENSSL_NO_ECDH
  1740. int have_ecdh_tmp;
  1741. #endif
  1742. X509 *x = NULL;
  1743. EVP_PKEY *ecc_pkey = NULL;
  1744. int signature_nid = 0;
  1745. if (c == NULL) return;
  1746. kl=SSL_C_EXPORT_PKEYLENGTH(cipher);
  1747. #ifndef OPENSSL_NO_RSA
  1748. rsa_tmp=(c->rsa_tmp != NULL || c->rsa_tmp_cb != NULL);
  1749. rsa_tmp_export=(c->rsa_tmp_cb != NULL ||
  1750. (rsa_tmp && RSA_size(c->rsa_tmp)*8 <= kl));
  1751. #else
  1752. rsa_tmp=rsa_tmp_export=0;
  1753. #endif
  1754. #ifndef OPENSSL_NO_DH
  1755. dh_tmp=(c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
  1756. dh_tmp_export=(c->dh_tmp_cb != NULL ||
  1757. (dh_tmp && DH_size(c->dh_tmp)*8 <= kl));
  1758. #else
  1759. dh_tmp=dh_tmp_export=0;
  1760. #endif
  1761. #ifndef OPENSSL_NO_ECDH
  1762. have_ecdh_tmp=(c->ecdh_tmp != NULL || c->ecdh_tmp_cb != NULL);
  1763. #endif
  1764. cpk= &(c->pkeys[SSL_PKEY_RSA_ENC]);
  1765. rsa_enc= (cpk->x509 != NULL && cpk->privatekey != NULL);
  1766. rsa_enc_export=(rsa_enc && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
  1767. cpk= &(c->pkeys[SSL_PKEY_RSA_SIGN]);
  1768. rsa_sign=(cpk->x509 != NULL && cpk->privatekey != NULL);
  1769. cpk= &(c->pkeys[SSL_PKEY_DSA_SIGN]);
  1770. dsa_sign=(cpk->x509 != NULL && cpk->privatekey != NULL);
  1771. cpk= &(c->pkeys[SSL_PKEY_DH_RSA]);
  1772. dh_rsa= (cpk->x509 != NULL && cpk->privatekey != NULL);
  1773. dh_rsa_export=(dh_rsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
  1774. cpk= &(c->pkeys[SSL_PKEY_DH_DSA]);
  1775. /* FIX THIS EAY EAY EAY */
  1776. dh_dsa= (cpk->x509 != NULL && cpk->privatekey != NULL);
  1777. dh_dsa_export=(dh_dsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
  1778. cpk= &(c->pkeys[SSL_PKEY_ECC]);
  1779. have_ecc_cert= (cpk->x509 != NULL && cpk->privatekey != NULL);
  1780. mask_k=0;
  1781. mask_a=0;
  1782. emask_k=0;
  1783. emask_a=0;
  1784. #ifdef CIPHER_DEBUG
  1785. printf("rt=%d rte=%d dht=%d ecdht=%d re=%d ree=%d rs=%d ds=%d dhr=%d dhd=%d\n",
  1786. rsa_tmp,rsa_tmp_export,dh_tmp,have_ecdh_tmp,
  1787. rsa_enc,rsa_enc_export,rsa_sign,dsa_sign,dh_rsa,dh_dsa);
  1788. #endif
  1789. cpk = &(c->pkeys[SSL_PKEY_GOST01]);
  1790. if (cpk->x509 != NULL && cpk->privatekey !=NULL) {
  1791. mask_k |= SSL_kGOST;
  1792. mask_a |= SSL_aGOST01;
  1793. }
  1794. cpk = &(c->pkeys[SSL_PKEY_GOST94]);
  1795. if (cpk->x509 != NULL && cpk->privatekey !=NULL) {
  1796. mask_k |= SSL_kGOST;
  1797. mask_a |= SSL_aGOST94;
  1798. }
  1799. if (rsa_enc || (rsa_tmp && rsa_sign))
  1800. mask_k|=SSL_kRSA;
  1801. if (rsa_enc_export || (rsa_tmp_export && (rsa_sign || rsa_enc)))
  1802. emask_k|=SSL_kRSA;
  1803. #if 0
  1804. /* The match needs to be both kEDH and aRSA or aDSA, so don't worry */
  1805. if ( (dh_tmp || dh_rsa || dh_dsa) &&
  1806. (rsa_enc || rsa_sign || dsa_sign))
  1807. mask_k|=SSL_kEDH;
  1808. if ((dh_tmp_export || dh_rsa_export || dh_dsa_export) &&
  1809. (rsa_enc || rsa_sign || dsa_sign))
  1810. emask_k|=SSL_kEDH;
  1811. #endif
  1812. if (dh_tmp_export)
  1813. emask_k|=SSL_kEDH;
  1814. if (dh_tmp)
  1815. mask_k|=SSL_kEDH;
  1816. if (dh_rsa) mask_k|=SSL_kDHr;
  1817. if (dh_rsa_export) emask_k|=SSL_kDHr;
  1818. if (dh_dsa) mask_k|=SSL_kDHd;
  1819. if (dh_dsa_export) emask_k|=SSL_kDHd;
  1820. if (rsa_enc || rsa_sign)
  1821. {
  1822. mask_a|=SSL_aRSA;
  1823. emask_a|=SSL_aRSA;
  1824. }
  1825. if (dsa_sign)
  1826. {
  1827. mask_a|=SSL_aDSS;
  1828. emask_a|=SSL_aDSS;
  1829. }
  1830. mask_a|=SSL_aNULL;
  1831. emask_a|=SSL_aNULL;
  1832. #ifndef OPENSSL_NO_KRB5
  1833. mask_k|=SSL_kKRB5;
  1834. mask_a|=SSL_aKRB5;
  1835. emask_k|=SSL_kKRB5;
  1836. emask_a|=SSL_aKRB5;
  1837. #endif
  1838. /* An ECC certificate may be usable for ECDH and/or
  1839. * ECDSA cipher suites depending on the key usage extension.
  1840. */
  1841. if (have_ecc_cert)
  1842. {
  1843. /* This call populates extension flags (ex_flags) */
  1844. x = (c->pkeys[SSL_PKEY_ECC]).x509;
  1845. X509_check_purpose(x, -1, 0);
  1846. ecdh_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
  1847. (x->ex_kusage & X509v3_KU_KEY_AGREEMENT) : 1;
  1848. ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
  1849. (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) : 1;
  1850. ecc_pkey = X509_get_pubkey(x);
  1851. ecc_pkey_size = (ecc_pkey != NULL) ?
  1852. EVP_PKEY_bits(ecc_pkey) : 0;
  1853. EVP_PKEY_free(ecc_pkey);
  1854. if ((x->sig_alg) && (x->sig_alg->algorithm))
  1855. signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
  1856. #ifndef OPENSSL_NO_ECDH
  1857. if (ecdh_ok)
  1858. {
  1859. const char *sig = OBJ_nid2ln(signature_nid);
  1860. if (sig == NULL)
  1861. {
  1862. ERR_clear_error();
  1863. sig = "unknown";
  1864. }
  1865. if (strstr(sig, "WithRSA"))
  1866. {
  1867. mask_k|=SSL_kECDHr;
  1868. mask_a|=SSL_aECDH;
  1869. if (ecc_pkey_size <= 163)
  1870. {
  1871. emask_k|=SSL_kECDHr;
  1872. emask_a|=SSL_aECDH;
  1873. }
  1874. }
  1875. if (signature_nid == NID_ecdsa_with_SHA1)
  1876. {
  1877. mask_k|=SSL_kECDHe;
  1878. mask_a|=SSL_aECDH;
  1879. if (ecc_pkey_size <= 163)
  1880. {
  1881. emask_k|=SSL_kECDHe;
  1882. emask_a|=SSL_aECDH;
  1883. }
  1884. }
  1885. }
  1886. #endif
  1887. #ifndef OPENSSL_NO_ECDSA
  1888. if (ecdsa_ok)
  1889. {
  1890. mask_a|=SSL_aECDSA;
  1891. emask_a|=SSL_aECDSA;
  1892. }
  1893. #endif
  1894. }
  1895. #ifndef OPENSSL_NO_ECDH
  1896. if (have_ecdh_tmp)
  1897. {
  1898. mask_k|=SSL_kEECDH;
  1899. emask_k|=SSL_kEECDH;
  1900. }
  1901. #endif
  1902. #ifndef OPENSSL_NO_PSK
  1903. mask_k |= SSL_kPSK;
  1904. mask_a |= SSL_aPSK;
  1905. emask_k |= SSL_kPSK;
  1906. emask_a |= SSL_aPSK;
  1907. #endif
  1908. c->mask_k=mask_k;
  1909. c->mask_a=mask_a;
  1910. c->export_mask_k=emask_k;
  1911. c->export_mask_a=emask_a;
  1912. c->valid=1;
  1913. }
  1914. /* This handy macro borrowed from crypto/x509v3/v3_purp.c */
  1915. #define ku_reject(x, usage) \
  1916. (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
  1917. #ifndef OPENSSL_NO_EC
  1918. int ssl_check_srvr_ecc_cert_and_alg(X509 *x, const SSL_CIPHER *cs)
  1919. {
  1920. unsigned long alg_k, alg_a;
  1921. EVP_PKEY *pkey = NULL;
  1922. int keysize = 0;
  1923. int signature_nid = 0;
  1924. alg_k = cs->algorithm_mkey;
  1925. alg_a = cs->algorithm_auth;
  1926. if (SSL_C_IS_EXPORT(cs))
  1927. {
  1928. /* ECDH key length in export ciphers must be <= 163 bits */
  1929. pkey = X509_get_pubkey(x);
  1930. if (pkey == NULL) return 0;
  1931. keysize = EVP_PKEY_bits(pkey);
  1932. EVP_PKEY_free(pkey);
  1933. if (keysize > 163) return 0;
  1934. }
  1935. /* This call populates the ex_flags field correctly */
  1936. X509_check_purpose(x, -1, 0);
  1937. if ((x->sig_alg) && (x->sig_alg->algorithm))
  1938. signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
  1939. if (alg_k & SSL_kECDHe || alg_k & SSL_kECDHr)
  1940. {
  1941. /* key usage, if present, must allow key agreement */
  1942. if (ku_reject(x, X509v3_KU_KEY_AGREEMENT))
  1943. {
  1944. SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_NOT_FOR_KEY_AGREEMENT);
  1945. return 0;
  1946. }
  1947. if (alg_k & SSL_kECDHe)
  1948. {
  1949. /* signature alg must be ECDSA */
  1950. if (signature_nid != NID_ecdsa_with_SHA1)
  1951. {
  1952. SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_SHOULD_HAVE_SHA1_SIGNATURE);
  1953. return 0;
  1954. }
  1955. }
  1956. if (alg_k & SSL_kECDHr)
  1957. {
  1958. /* signature alg must be RSA */
  1959. const char *sig = OBJ_nid2ln(signature_nid);
  1960. if (sig == NULL)
  1961. {
  1962. ERR_clear_error();
  1963. sig = "unknown";
  1964. }
  1965. if (strstr(sig, "WithRSA") == NULL)
  1966. {
  1967. SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_SHOULD_HAVE_RSA_SIGNATURE);
  1968. return 0;
  1969. }
  1970. }
  1971. }
  1972. if (alg_a & SSL_aECDSA)
  1973. {
  1974. /* key usage, if present, must allow signing */
  1975. if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE))
  1976. {
  1977. SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
  1978. return 0;
  1979. }
  1980. }
  1981. return 1; /* all checks are ok */
  1982. }
  1983. #endif
  1984. /* THIS NEEDS CLEANING UP */
  1985. X509 *ssl_get_server_send_cert(SSL *s)
  1986. {
  1987. unsigned long alg_k,alg_a;
  1988. CERT *c;
  1989. int i;
  1990. c=s->cert;
  1991. ssl_set_cert_masks(c, s->s3->tmp.new_cipher);
  1992. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  1993. alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  1994. if (alg_k & (SSL_kECDHr|SSL_kECDHe))
  1995. {
  1996. /* we don't need to look at SSL_kEECDH
  1997. * since no certificate is needed for
  1998. * anon ECDH and for authenticated
  1999. * EECDH, the check for the auth
  2000. * algorithm will set i correctly
  2001. * NOTE: For ECDH-RSA, we need an ECC
  2002. * not an RSA cert but for EECDH-RSA
  2003. * we need an RSA cert. Placing the
  2004. * checks for SSL_kECDH before RSA
  2005. * checks ensures the correct cert is chosen.
  2006. */
  2007. i=SSL_PKEY_ECC;
  2008. }
  2009. else if (alg_a & SSL_aECDSA)
  2010. {
  2011. i=SSL_PKEY_ECC;
  2012. }
  2013. else if (alg_k & SSL_kDHr)
  2014. i=SSL_PKEY_DH_RSA;
  2015. else if (alg_k & SSL_kDHd)
  2016. i=SSL_PKEY_DH_DSA;
  2017. else if (alg_a & SSL_aDSS)
  2018. i=SSL_PKEY_DSA_SIGN;
  2019. else if (alg_a & SSL_aRSA)
  2020. {
  2021. if (c->pkeys[SSL_PKEY_RSA_ENC].x509 == NULL)
  2022. i=SSL_PKEY_RSA_SIGN;
  2023. else
  2024. i=SSL_PKEY_RSA_ENC;
  2025. }
  2026. else if (alg_a & SSL_aKRB5)
  2027. {
  2028. /* VRS something else here? */
  2029. return(NULL);
  2030. }
  2031. else if (alg_a & SSL_aGOST94)
  2032. i=SSL_PKEY_GOST94;
  2033. else if (alg_a & SSL_aGOST01)
  2034. i=SSL_PKEY_GOST01;
  2035. else /* if (alg_a & SSL_aNULL) */
  2036. {
  2037. SSLerr(SSL_F_SSL_GET_SERVER_SEND_CERT,ERR_R_INTERNAL_ERROR);
  2038. return(NULL);
  2039. }
  2040. if (c->pkeys[i].x509 == NULL) return(NULL);
  2041. return(c->pkeys[i].x509);
  2042. }
  2043. EVP_PKEY *ssl_get_sign_pkey(SSL *s,const SSL_CIPHER *cipher)
  2044. {
  2045. unsigned long alg_a;
  2046. CERT *c;
  2047. alg_a = cipher->algorithm_auth;
  2048. c=s->cert;
  2049. if ((alg_a & SSL_aDSS) &&
  2050. (c->pkeys[SSL_PKEY_DSA_SIGN].privatekey != NULL))
  2051. return(c->pkeys[SSL_PKEY_DSA_SIGN].privatekey);
  2052. else if (alg_a & SSL_aRSA)
  2053. {
  2054. if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL)
  2055. return(c->pkeys[SSL_PKEY_RSA_SIGN].privatekey);
  2056. else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL)
  2057. return(c->pkeys[SSL_PKEY_RSA_ENC].privatekey);
  2058. else
  2059. return(NULL);
  2060. }
  2061. else if ((alg_a & SSL_aECDSA) &&
  2062. (c->pkeys[SSL_PKEY_ECC].privatekey != NULL))
  2063. return(c->pkeys[SSL_PKEY_ECC].privatekey);
  2064. else /* if (alg_a & SSL_aNULL) */
  2065. {
  2066. SSLerr(SSL_F_SSL_GET_SIGN_PKEY,ERR_R_INTERNAL_ERROR);
  2067. return(NULL);
  2068. }
  2069. }
  2070. void ssl_update_cache(SSL *s,int mode)
  2071. {
  2072. int i;
  2073. /* If the session_id_length is 0, we are not supposed to cache it,
  2074. * and it would be rather hard to do anyway :-) */
  2075. if (s->session->session_id_length == 0) return;
  2076. i=s->session_ctx->session_cache_mode;
  2077. if ((i & mode) && (!s->hit)
  2078. && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE)
  2079. || SSL_CTX_add_session(s->session_ctx,s->session))
  2080. && (s->session_ctx->new_session_cb != NULL))
  2081. {
  2082. CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION);
  2083. if (!s->session_ctx->new_session_cb(s,s->session))
  2084. SSL_SESSION_free(s->session);
  2085. }
  2086. /* auto flush every 255 connections */
  2087. if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) &&
  2088. ((i & mode) == mode))
  2089. {
  2090. if ( (((mode & SSL_SESS_CACHE_CLIENT)
  2091. ?s->session_ctx->stats.sess_connect_good
  2092. :s->session_ctx->stats.sess_accept_good) & 0xff) == 0xff)
  2093. {
  2094. SSL_CTX_flush_sessions(s->session_ctx,(unsigned long)time(NULL));
  2095. }
  2096. }
  2097. }
  2098. const SSL_METHOD *SSL_get_ssl_method(SSL *s)
  2099. {
  2100. return(s->method);
  2101. }
  2102. int SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth)
  2103. {
  2104. int conn= -1;
  2105. int ret=1;
  2106. if (s->method != meth)
  2107. {
  2108. if (s->handshake_func != NULL)
  2109. conn=(s->handshake_func == s->method->ssl_connect);
  2110. if (s->method->version == meth->version)
  2111. s->method=meth;
  2112. else
  2113. {
  2114. s->method->ssl_free(s);
  2115. s->method=meth;
  2116. ret=s->method->ssl_new(s);
  2117. }
  2118. if (conn == 1)
  2119. s->handshake_func=meth->ssl_connect;
  2120. else if (conn == 0)
  2121. s->handshake_func=meth->ssl_accept;
  2122. }
  2123. return(ret);
  2124. }
  2125. int SSL_get_error(const SSL *s,int i)
  2126. {
  2127. int reason;
  2128. unsigned long l;
  2129. BIO *bio;
  2130. if (i > 0) return(SSL_ERROR_NONE);
  2131. /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
  2132. * etc, where we do encode the error */
  2133. if ((l=ERR_peek_error()) != 0)
  2134. {
  2135. if (ERR_GET_LIB(l) == ERR_LIB_SYS)
  2136. return(SSL_ERROR_SYSCALL);
  2137. else
  2138. return(SSL_ERROR_SSL);
  2139. }
  2140. if ((i < 0) && SSL_want_read(s))
  2141. {
  2142. bio=SSL_get_rbio(s);
  2143. if (BIO_should_read(bio))
  2144. return(SSL_ERROR_WANT_READ);
  2145. else if (BIO_should_write(bio))
  2146. /* This one doesn't make too much sense ... We never try
  2147. * to write to the rbio, and an application program where
  2148. * rbio and wbio are separate couldn't even know what it
  2149. * should wait for.
  2150. * However if we ever set s->rwstate incorrectly
  2151. * (so that we have SSL_want_read(s) instead of
  2152. * SSL_want_write(s)) and rbio and wbio *are* the same,
  2153. * this test works around that bug; so it might be safer
  2154. * to keep it. */
  2155. return(SSL_ERROR_WANT_WRITE);
  2156. else if (BIO_should_io_special(bio))
  2157. {
  2158. reason=BIO_get_retry_reason(bio);
  2159. if (reason == BIO_RR_CONNECT)
  2160. return(SSL_ERROR_WANT_CONNECT);
  2161. else if (reason == BIO_RR_ACCEPT)
  2162. return(SSL_ERROR_WANT_ACCEPT);
  2163. else
  2164. return(SSL_ERROR_SYSCALL); /* unknown */
  2165. }
  2166. }
  2167. if ((i < 0) && SSL_want_write(s))
  2168. {
  2169. bio=SSL_get_wbio(s);
  2170. if (BIO_should_write(bio))
  2171. return(SSL_ERROR_WANT_WRITE);
  2172. else if (BIO_should_read(bio))
  2173. /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
  2174. return(SSL_ERROR_WANT_READ);
  2175. else if (BIO_should_io_special(bio))
  2176. {
  2177. reason=BIO_get_retry_reason(bio);
  2178. if (reason == BIO_RR_CONNECT)
  2179. return(SSL_ERROR_WANT_CONNECT);
  2180. else if (reason == BIO_RR_ACCEPT)
  2181. return(SSL_ERROR_WANT_ACCEPT);
  2182. else
  2183. return(SSL_ERROR_SYSCALL);
  2184. }
  2185. }
  2186. if ((i < 0) && SSL_want_x509_lookup(s))
  2187. {
  2188. return(SSL_ERROR_WANT_X509_LOOKUP);
  2189. }
  2190. if (i == 0)
  2191. {
  2192. if (s->version == SSL2_VERSION)
  2193. {
  2194. /* assume it is the socket being closed */
  2195. return(SSL_ERROR_ZERO_RETURN);
  2196. }
  2197. else
  2198. {
  2199. if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
  2200. (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
  2201. return(SSL_ERROR_ZERO_RETURN);
  2202. }
  2203. }
  2204. return(SSL_ERROR_SYSCALL);
  2205. }
  2206. int SSL_do_handshake(SSL *s)
  2207. {
  2208. int ret=1;
  2209. if (s->handshake_func == NULL)
  2210. {
  2211. SSLerr(SSL_F_SSL_DO_HANDSHAKE,SSL_R_CONNECTION_TYPE_NOT_SET);
  2212. return(-1);
  2213. }
  2214. s->method->ssl_renegotiate_check(s);
  2215. if (SSL_in_init(s) || SSL_in_before(s))
  2216. {
  2217. ret=s->handshake_func(s);
  2218. }
  2219. return(ret);
  2220. }
  2221. /* For the next 2 functions, SSL_clear() sets shutdown and so
  2222. * one of these calls will reset it */
  2223. void SSL_set_accept_state(SSL *s)
  2224. {
  2225. s->server=1;
  2226. s->shutdown=0;
  2227. s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE;
  2228. s->handshake_func=s->method->ssl_accept;
  2229. /* clear the current cipher */
  2230. ssl_clear_cipher_ctx(s);
  2231. ssl_clear_hash_ctx(&s->read_hash);
  2232. ssl_clear_hash_ctx(&s->write_hash);
  2233. }
  2234. void SSL_set_connect_state(SSL *s)
  2235. {
  2236. s->server=0;
  2237. s->shutdown=0;
  2238. s->state=SSL_ST_CONNECT|SSL_ST_BEFORE;
  2239. s->handshake_func=s->method->ssl_connect;
  2240. /* clear the current cipher */
  2241. ssl_clear_cipher_ctx(s);
  2242. ssl_clear_hash_ctx(&s->read_hash);
  2243. ssl_clear_hash_ctx(&s->write_hash);
  2244. }
  2245. int ssl_undefined_function(SSL *s)
  2246. {
  2247. SSLerr(SSL_F_SSL_UNDEFINED_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  2248. return(0);
  2249. }
  2250. int ssl_undefined_void_function(void)
  2251. {
  2252. SSLerr(SSL_F_SSL_UNDEFINED_VOID_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  2253. return(0);
  2254. }
  2255. int ssl_undefined_const_function(const SSL *s)
  2256. {
  2257. SSLerr(SSL_F_SSL_UNDEFINED_CONST_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  2258. return(0);
  2259. }
  2260. SSL_METHOD *ssl_bad_method(int ver)
  2261. {
  2262. SSLerr(SSL_F_SSL_BAD_METHOD,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  2263. return(NULL);
  2264. }
  2265. const char *SSL_get_version(const SSL *s)
  2266. {
  2267. if (s->version == TLS1_2_VERSION)
  2268. return("TLSv1.2");
  2269. else if (s->version == TLS1_1_VERSION)
  2270. return("TLSv1.1");
  2271. else if (s->version == TLS1_VERSION)
  2272. return("TLSv1");
  2273. else if (s->version == SSL3_VERSION)
  2274. return("SSLv3");
  2275. else if (s->version == SSL2_VERSION)
  2276. return("SSLv2");
  2277. else
  2278. return("unknown");
  2279. }
  2280. SSL *SSL_dup(SSL *s)
  2281. {
  2282. STACK_OF(X509_NAME) *sk;
  2283. X509_NAME *xn;
  2284. SSL *ret;
  2285. int i;
  2286. if ((ret=SSL_new(SSL_get_SSL_CTX(s))) == NULL)
  2287. return(NULL);
  2288. ret->version = s->version;
  2289. ret->type = s->type;
  2290. ret->method = s->method;
  2291. if (s->session != NULL)
  2292. {
  2293. /* This copies session-id, SSL_METHOD, sid_ctx, and 'cert' */
  2294. SSL_copy_session_id(ret,s);
  2295. }
  2296. else
  2297. {
  2298. /* No session has been established yet, so we have to expect
  2299. * that s->cert or ret->cert will be changed later --
  2300. * they should not both point to the same object,
  2301. * and thus we can't use SSL_copy_session_id. */
  2302. ret->method->ssl_free(ret);
  2303. ret->method = s->method;
  2304. ret->method->ssl_new(ret);
  2305. if (s->cert != NULL)
  2306. {
  2307. if (ret->cert != NULL)
  2308. {
  2309. ssl_cert_free(ret->cert);
  2310. }
  2311. ret->cert = ssl_cert_dup(s->cert);
  2312. if (ret->cert == NULL)
  2313. goto err;
  2314. }
  2315. SSL_set_session_id_context(ret,
  2316. s->sid_ctx, s->sid_ctx_length);
  2317. }
  2318. ret->options=s->options;
  2319. ret->mode=s->mode;
  2320. SSL_set_max_cert_list(ret,SSL_get_max_cert_list(s));
  2321. SSL_set_read_ahead(ret,SSL_get_read_ahead(s));
  2322. ret->msg_callback = s->msg_callback;
  2323. ret->msg_callback_arg = s->msg_callback_arg;
  2324. SSL_set_verify(ret,SSL_get_verify_mode(s),
  2325. SSL_get_verify_callback(s));
  2326. SSL_set_verify_depth(ret,SSL_get_verify_depth(s));
  2327. ret->generate_session_id = s->generate_session_id;
  2328. SSL_set_info_callback(ret,SSL_get_info_callback(s));
  2329. ret->debug=s->debug;
  2330. /* copy app data, a little dangerous perhaps */
  2331. if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL, &ret->ex_data, &s->ex_data))
  2332. goto err;
  2333. /* setup rbio, and wbio */
  2334. if (s->rbio != NULL)
  2335. {
  2336. if (!BIO_dup_state(s->rbio,(char *)&ret->rbio))
  2337. goto err;
  2338. }
  2339. if (s->wbio != NULL)
  2340. {
  2341. if (s->wbio != s->rbio)
  2342. {
  2343. if (!BIO_dup_state(s->wbio,(char *)&ret->wbio))
  2344. goto err;
  2345. }
  2346. else
  2347. ret->wbio=ret->rbio;
  2348. }
  2349. ret->rwstate = s->rwstate;
  2350. ret->in_handshake = s->in_handshake;
  2351. ret->handshake_func = s->handshake_func;
  2352. ret->server = s->server;
  2353. ret->renegotiate = s->renegotiate;
  2354. ret->new_session = s->new_session;
  2355. ret->quiet_shutdown = s->quiet_shutdown;
  2356. ret->shutdown=s->shutdown;
  2357. ret->state=s->state; /* SSL_dup does not really work at any state, though */
  2358. ret->rstate=s->rstate;
  2359. ret->init_num = 0; /* would have to copy ret->init_buf, ret->init_msg, ret->init_num, ret->init_off */
  2360. ret->hit=s->hit;
  2361. X509_VERIFY_PARAM_inherit(ret->param, s->param);
  2362. /* dup the cipher_list and cipher_list_by_id stacks */
  2363. if (s->cipher_list != NULL)
  2364. {
  2365. if ((ret->cipher_list=sk_SSL_CIPHER_dup(s->cipher_list)) == NULL)
  2366. goto err;
  2367. }
  2368. if (s->cipher_list_by_id != NULL)
  2369. if ((ret->cipher_list_by_id=sk_SSL_CIPHER_dup(s->cipher_list_by_id))
  2370. == NULL)
  2371. goto err;
  2372. /* Dup the client_CA list */
  2373. if (s->client_CA != NULL)
  2374. {
  2375. if ((sk=sk_X509_NAME_dup(s->client_CA)) == NULL) goto err;
  2376. ret->client_CA=sk;
  2377. for (i=0; i<sk_X509_NAME_num(sk); i++)
  2378. {
  2379. xn=sk_X509_NAME_value(sk,i);
  2380. if (sk_X509_NAME_set(sk,i,X509_NAME_dup(xn)) == NULL)
  2381. {
  2382. X509_NAME_free(xn);
  2383. goto err;
  2384. }
  2385. }
  2386. }
  2387. if (0)
  2388. {
  2389. err:
  2390. if (ret != NULL) SSL_free(ret);
  2391. ret=NULL;
  2392. }
  2393. return(ret);
  2394. }
  2395. void ssl_clear_cipher_ctx(SSL *s)
  2396. {
  2397. if (s->enc_read_ctx != NULL)
  2398. {
  2399. EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
  2400. OPENSSL_free(s->enc_read_ctx);
  2401. s->enc_read_ctx=NULL;
  2402. }
  2403. if (s->enc_write_ctx != NULL)
  2404. {
  2405. EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
  2406. OPENSSL_free(s->enc_write_ctx);
  2407. s->enc_write_ctx=NULL;
  2408. }
  2409. #ifndef OPENSSL_NO_COMP
  2410. if (s->expand != NULL)
  2411. {
  2412. COMP_CTX_free(s->expand);
  2413. s->expand=NULL;
  2414. }
  2415. if (s->compress != NULL)
  2416. {
  2417. COMP_CTX_free(s->compress);
  2418. s->compress=NULL;
  2419. }
  2420. #endif
  2421. }
  2422. /* Fix this function so that it takes an optional type parameter */
  2423. X509 *SSL_get_certificate(const SSL *s)
  2424. {
  2425. if (s->cert != NULL)
  2426. return(s->cert->key->x509);
  2427. else
  2428. return(NULL);
  2429. }
  2430. /* Fix this function so that it takes an optional type parameter */
  2431. EVP_PKEY *SSL_get_privatekey(SSL *s)
  2432. {
  2433. if (s->cert != NULL)
  2434. return(s->cert->key->privatekey);
  2435. else
  2436. return(NULL);
  2437. }
  2438. const SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
  2439. {
  2440. if ((s->session != NULL) && (s->session->cipher != NULL))
  2441. return(s->session->cipher);
  2442. return(NULL);
  2443. }
  2444. #ifdef OPENSSL_NO_COMP
  2445. const void *SSL_get_current_compression(SSL *s)
  2446. {
  2447. return NULL;
  2448. }
  2449. const void *SSL_get_current_expansion(SSL *s)
  2450. {
  2451. return NULL;
  2452. }
  2453. #else
  2454. const COMP_METHOD *SSL_get_current_compression(SSL *s)
  2455. {
  2456. if (s->compress != NULL)
  2457. return(s->compress->meth);
  2458. return(NULL);
  2459. }
  2460. const COMP_METHOD *SSL_get_current_expansion(SSL *s)
  2461. {
  2462. if (s->expand != NULL)
  2463. return(s->expand->meth);
  2464. return(NULL);
  2465. }
  2466. #endif
  2467. int ssl_init_wbio_buffer(SSL *s,int push)
  2468. {
  2469. BIO *bbio;
  2470. if (s->bbio == NULL)
  2471. {
  2472. bbio=BIO_new(BIO_f_buffer());
  2473. if (bbio == NULL) return(0);
  2474. s->bbio=bbio;
  2475. }
  2476. else
  2477. {
  2478. bbio=s->bbio;
  2479. if (s->bbio == s->wbio)
  2480. s->wbio=BIO_pop(s->wbio);
  2481. }
  2482. (void)BIO_reset(bbio);
  2483. /* if (!BIO_set_write_buffer_size(bbio,16*1024)) */
  2484. if (!BIO_set_read_buffer_size(bbio,1))
  2485. {
  2486. SSLerr(SSL_F_SSL_INIT_WBIO_BUFFER,ERR_R_BUF_LIB);
  2487. return(0);
  2488. }
  2489. if (push)
  2490. {
  2491. if (s->wbio != bbio)
  2492. s->wbio=BIO_push(bbio,s->wbio);
  2493. }
  2494. else
  2495. {
  2496. if (s->wbio == bbio)
  2497. s->wbio=BIO_pop(bbio);
  2498. }
  2499. return(1);
  2500. }
  2501. void ssl_free_wbio_buffer(SSL *s)
  2502. {
  2503. if (s->bbio == NULL) return;
  2504. if (s->bbio == s->wbio)
  2505. {
  2506. /* remove buffering */
  2507. s->wbio=BIO_pop(s->wbio);
  2508. #ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids adding one more preprocessor symbol */
  2509. assert(s->wbio != NULL);
  2510. #endif
  2511. }
  2512. BIO_free(s->bbio);
  2513. s->bbio=NULL;
  2514. }
  2515. void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx,int mode)
  2516. {
  2517. ctx->quiet_shutdown=mode;
  2518. }
  2519. int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
  2520. {
  2521. return(ctx->quiet_shutdown);
  2522. }
  2523. void SSL_set_quiet_shutdown(SSL *s,int mode)
  2524. {
  2525. s->quiet_shutdown=mode;
  2526. }
  2527. int SSL_get_quiet_shutdown(const SSL *s)
  2528. {
  2529. return(s->quiet_shutdown);
  2530. }
  2531. void SSL_set_shutdown(SSL *s,int mode)
  2532. {
  2533. s->shutdown=mode;
  2534. }
  2535. int SSL_get_shutdown(const SSL *s)
  2536. {
  2537. return(s->shutdown);
  2538. }
  2539. int SSL_version(const SSL *s)
  2540. {
  2541. return(s->version);
  2542. }
  2543. SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
  2544. {
  2545. return(ssl->ctx);
  2546. }
  2547. SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx)
  2548. {
  2549. if (ssl->ctx == ctx)
  2550. return ssl->ctx;
  2551. #ifndef OPENSSL_NO_TLSEXT
  2552. if (ctx == NULL)
  2553. ctx = ssl->initial_ctx;
  2554. #endif
  2555. if (ssl->cert != NULL)
  2556. ssl_cert_free(ssl->cert);
  2557. ssl->cert = ssl_cert_dup(ctx->cert);
  2558. CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
  2559. if (ssl->ctx != NULL)
  2560. SSL_CTX_free(ssl->ctx); /* decrement reference count */
  2561. ssl->ctx = ctx;
  2562. return(ssl->ctx);
  2563. }
  2564. #ifndef OPENSSL_NO_STDIO
  2565. int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
  2566. {
  2567. return(X509_STORE_set_default_paths(ctx->cert_store));
  2568. }
  2569. int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
  2570. const char *CApath)
  2571. {
  2572. return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath));
  2573. }
  2574. #endif
  2575. void SSL_set_info_callback(SSL *ssl,
  2576. void (*cb)(const SSL *ssl,int type,int val))
  2577. {
  2578. ssl->info_callback=cb;
  2579. }
  2580. /* One compiler (Diab DCC) doesn't like argument names in returned
  2581. function pointer. */
  2582. void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/,int /*type*/,int /*val*/)
  2583. {
  2584. return ssl->info_callback;
  2585. }
  2586. int SSL_state(const SSL *ssl)
  2587. {
  2588. return(ssl->state);
  2589. }
  2590. void SSL_set_state(SSL *ssl, int state)
  2591. {
  2592. ssl->state = state;
  2593. }
  2594. void SSL_set_verify_result(SSL *ssl,long arg)
  2595. {
  2596. ssl->verify_result=arg;
  2597. }
  2598. long SSL_get_verify_result(const SSL *ssl)
  2599. {
  2600. return(ssl->verify_result);
  2601. }
  2602. int SSL_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
  2603. CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
  2604. {
  2605. return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp,
  2606. new_func, dup_func, free_func);
  2607. }
  2608. int SSL_set_ex_data(SSL *s,int idx,void *arg)
  2609. {
  2610. return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
  2611. }
  2612. void *SSL_get_ex_data(const SSL *s,int idx)
  2613. {
  2614. return(CRYPTO_get_ex_data(&s->ex_data,idx));
  2615. }
  2616. int SSL_CTX_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
  2617. CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
  2618. {
  2619. return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp,
  2620. new_func, dup_func, free_func);
  2621. }
  2622. int SSL_CTX_set_ex_data(SSL_CTX *s,int idx,void *arg)
  2623. {
  2624. return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
  2625. }
  2626. void *SSL_CTX_get_ex_data(const SSL_CTX *s,int idx)
  2627. {
  2628. return(CRYPTO_get_ex_data(&s->ex_data,idx));
  2629. }
  2630. int ssl_ok(SSL *s)
  2631. {
  2632. return(1);
  2633. }
  2634. X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
  2635. {
  2636. return(ctx->cert_store);
  2637. }
  2638. void SSL_CTX_set_cert_store(SSL_CTX *ctx,X509_STORE *store)
  2639. {
  2640. if (ctx->cert_store != NULL)
  2641. X509_STORE_free(ctx->cert_store);
  2642. ctx->cert_store=store;
  2643. }
  2644. int SSL_want(const SSL *s)
  2645. {
  2646. return(s->rwstate);
  2647. }
  2648. /*!
  2649. * \brief Set the callback for generating temporary RSA keys.
  2650. * \param ctx the SSL context.
  2651. * \param cb the callback
  2652. */
  2653. #ifndef OPENSSL_NO_RSA
  2654. void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl,
  2655. int is_export,
  2656. int keylength))
  2657. {
  2658. SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
  2659. }
  2660. void SSL_set_tmp_rsa_callback(SSL *ssl,RSA *(*cb)(SSL *ssl,
  2661. int is_export,
  2662. int keylength))
  2663. {
  2664. SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
  2665. }
  2666. #endif
  2667. #ifdef DOXYGEN
  2668. /*!
  2669. * \brief The RSA temporary key callback function.
  2670. * \param ssl the SSL session.
  2671. * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite.
  2672. * \param keylength if \c is_export is \c TRUE, then \c keylength is the size
  2673. * of the required key in bits.
  2674. * \return the temporary RSA key.
  2675. * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback
  2676. */
  2677. RSA *cb(SSL *ssl,int is_export,int keylength)
  2678. {}
  2679. #endif
  2680. /*!
  2681. * \brief Set the callback for generating temporary DH keys.
  2682. * \param ctx the SSL context.
  2683. * \param dh the callback
  2684. */
  2685. #ifndef OPENSSL_NO_DH
  2686. void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int is_export,
  2687. int keylength))
  2688. {
  2689. SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
  2690. }
  2691. void SSL_set_tmp_dh_callback(SSL *ssl,DH *(*dh)(SSL *ssl,int is_export,
  2692. int keylength))
  2693. {
  2694. SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
  2695. }
  2696. #endif
  2697. #ifndef OPENSSL_NO_ECDH
  2698. void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
  2699. int keylength))
  2700. {
  2701. SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
  2702. }
  2703. void SSL_set_tmp_ecdh_callback(SSL *ssl,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
  2704. int keylength))
  2705. {
  2706. SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
  2707. }
  2708. #endif
  2709. #ifndef OPENSSL_NO_PSK
  2710. int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint)
  2711. {
  2712. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
  2713. {
  2714. SSLerr(SSL_F_SSL_CTX_USE_PSK_IDENTITY_HINT, SSL_R_DATA_LENGTH_TOO_LONG);
  2715. return 0;
  2716. }
  2717. if (ctx->psk_identity_hint != NULL)
  2718. OPENSSL_free(ctx->psk_identity_hint);
  2719. if (identity_hint != NULL)
  2720. {
  2721. ctx->psk_identity_hint = BUF_strdup(identity_hint);
  2722. if (ctx->psk_identity_hint == NULL)
  2723. return 0;
  2724. }
  2725. else
  2726. ctx->psk_identity_hint = NULL;
  2727. return 1;
  2728. }
  2729. int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint)
  2730. {
  2731. if (s == NULL)
  2732. return 0;
  2733. if (s->session == NULL)
  2734. return 1; /* session not created yet, ignored */
  2735. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
  2736. {
  2737. SSLerr(SSL_F_SSL_USE_PSK_IDENTITY_HINT, SSL_R_DATA_LENGTH_TOO_LONG);
  2738. return 0;
  2739. }
  2740. if (s->session->psk_identity_hint != NULL)
  2741. OPENSSL_free(s->session->psk_identity_hint);
  2742. if (identity_hint != NULL)
  2743. {
  2744. s->session->psk_identity_hint = BUF_strdup(identity_hint);
  2745. if (s->session->psk_identity_hint == NULL)
  2746. return 0;
  2747. }
  2748. else
  2749. s->session->psk_identity_hint = NULL;
  2750. return 1;
  2751. }
  2752. const char *SSL_get_psk_identity_hint(const SSL *s)
  2753. {
  2754. if (s == NULL || s->session == NULL)
  2755. return NULL;
  2756. return(s->session->psk_identity_hint);
  2757. }
  2758. const char *SSL_get_psk_identity(const SSL *s)
  2759. {
  2760. if (s == NULL || s->session == NULL)
  2761. return NULL;
  2762. return(s->session->psk_identity);
  2763. }
  2764. void SSL_set_psk_client_callback(SSL *s,
  2765. unsigned int (*cb)(SSL *ssl, const char *hint,
  2766. char *identity, unsigned int max_identity_len, unsigned char *psk,
  2767. unsigned int max_psk_len))
  2768. {
  2769. s->psk_client_callback = cb;
  2770. }
  2771. void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx,
  2772. unsigned int (*cb)(SSL *ssl, const char *hint,
  2773. char *identity, unsigned int max_identity_len, unsigned char *psk,
  2774. unsigned int max_psk_len))
  2775. {
  2776. ctx->psk_client_callback = cb;
  2777. }
  2778. void SSL_set_psk_server_callback(SSL *s,
  2779. unsigned int (*cb)(SSL *ssl, const char *identity,
  2780. unsigned char *psk, unsigned int max_psk_len))
  2781. {
  2782. s->psk_server_callback = cb;
  2783. }
  2784. void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx,
  2785. unsigned int (*cb)(SSL *ssl, const char *identity,
  2786. unsigned char *psk, unsigned int max_psk_len))
  2787. {
  2788. ctx->psk_server_callback = cb;
  2789. }
  2790. #endif
  2791. 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))
  2792. {
  2793. SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
  2794. }
  2795. 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))
  2796. {
  2797. SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
  2798. }
  2799. void SSL_CTX_set_not_resumable_session_callback(SSL_CTX *ctx,
  2800. int (*cb)(SSL *ssl, int is_forward_secure))
  2801. {
  2802. SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_NOT_RESUMABLE_SESS_CB,
  2803. (void (*)(void))cb);
  2804. }
  2805. void SSL_set_not_resumable_session_callback(SSL *ssl,
  2806. int (*cb)(SSL *ssl, int is_forward_secure))
  2807. {
  2808. SSL_callback_ctrl(ssl, SSL_CTRL_SET_NOT_RESUMABLE_SESS_CB,
  2809. (void (*)(void))cb);
  2810. }
  2811. /* Allocates new EVP_MD_CTX and sets pointer to it into given pointer
  2812. * vairable, freeing EVP_MD_CTX previously stored in that variable, if
  2813. * any. If EVP_MD pointer is passed, initializes ctx with this md
  2814. * Returns newly allocated ctx;
  2815. */
  2816. EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash,const EVP_MD *md)
  2817. {
  2818. ssl_clear_hash_ctx(hash);
  2819. *hash = EVP_MD_CTX_create();
  2820. if (md) EVP_DigestInit_ex(*hash,md,NULL);
  2821. return *hash;
  2822. }
  2823. void ssl_clear_hash_ctx(EVP_MD_CTX **hash)
  2824. {
  2825. if (*hash) EVP_MD_CTX_destroy(*hash);
  2826. *hash=NULL;
  2827. }
  2828. void SSL_set_debug(SSL *s, int debug)
  2829. {
  2830. s->debug = debug;
  2831. }
  2832. int SSL_cache_hit(SSL *s)
  2833. {
  2834. return s->hit;
  2835. }
  2836. #if defined(_WINDLL) && defined(OPENSSL_SYS_WIN16)
  2837. #include "../crypto/bio/bss_file.c"
  2838. #endif
  2839. IMPLEMENT_STACK_OF(SSL_CIPHER)
  2840. IMPLEMENT_STACK_OF(SSL_COMP)
  2841. IMPLEMENT_OBJ_BSEARCH_GLOBAL_CMP_FN(SSL_CIPHER, SSL_CIPHER,
  2842. ssl_cipher_id);