ssl_lib.c 75 KB

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