ssl_lib.c 74 KB

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