ssl_lib.c 103 KB

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