ssl_lib.c 103 KB

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