ssl_lib.c 81 KB

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