ssl_lib.c 104 KB

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