ssl_lib.c 97 KB

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