ssl_lib.c 103 KB

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