ssl_lib.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570
  1. /*! \file ssl/ssl_lib.c
  2. * \brief Version independent SSL functions.
  3. */
  4. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  5. * All rights reserved.
  6. *
  7. * This package is an SSL implementation written
  8. * by Eric Young (eay@cryptsoft.com).
  9. * The implementation was written so as to conform with Netscapes SSL.
  10. *
  11. * This library is free for commercial and non-commercial use as long as
  12. * the following conditions are aheared to. The following conditions
  13. * apply to all code found in this distribution, be it the RC4, RSA,
  14. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  15. * included with this distribution is covered by the same copyright terms
  16. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  17. *
  18. * Copyright remains Eric Young's, and as such any Copyright notices in
  19. * the code are not to be removed.
  20. * If this package is used in a product, Eric Young should be given attribution
  21. * as the author of the parts of the library used.
  22. * This can be in the form of a textual message at program startup or
  23. * in documentation (online or textual) provided with the package.
  24. *
  25. * Redistribution and use in source and binary forms, with or without
  26. * modification, are permitted provided that the following conditions
  27. * are met:
  28. * 1. Redistributions of source code must retain the copyright
  29. * notice, this list of conditions and the following disclaimer.
  30. * 2. Redistributions in binary form must reproduce the above copyright
  31. * notice, this list of conditions and the following disclaimer in the
  32. * documentation and/or other materials provided with the distribution.
  33. * 3. All advertising materials mentioning features or use of this software
  34. * must display the following acknowledgement:
  35. * "This product includes cryptographic software written by
  36. * Eric Young (eay@cryptsoft.com)"
  37. * The word 'cryptographic' can be left out if the rouines from the library
  38. * being used are not cryptographic related :-).
  39. * 4. If you include any Windows specific code (or a derivative thereof) from
  40. * the apps directory (application code) you must include an acknowledgement:
  41. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  42. *
  43. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  44. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  45. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  46. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  47. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  48. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  49. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  50. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  51. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  52. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  53. * SUCH DAMAGE.
  54. *
  55. * The licence and distribution terms for any publically available version or
  56. * derivative of this code cannot be changed. i.e. this code cannot simply be
  57. * copied and put under another distribution licence
  58. * [including the GNU Public Licence.]
  59. */
  60. /* ====================================================================
  61. * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
  62. *
  63. * Redistribution and use in source and binary forms, with or without
  64. * modification, are permitted provided that the following conditions
  65. * are met:
  66. *
  67. * 1. Redistributions of source code must retain the above copyright
  68. * notice, this list of conditions and the following disclaimer.
  69. *
  70. * 2. Redistributions in binary form must reproduce the above copyright
  71. * notice, this list of conditions and the following disclaimer in
  72. * the documentation and/or other materials provided with the
  73. * distribution.
  74. *
  75. * 3. All advertising materials mentioning features or use of this
  76. * software must display the following acknowledgment:
  77. * "This product includes software developed by the OpenSSL Project
  78. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  79. *
  80. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  81. * endorse or promote products derived from this software without
  82. * prior written permission. For written permission, please contact
  83. * openssl-core@openssl.org.
  84. *
  85. * 5. Products derived from this software may not be called "OpenSSL"
  86. * nor may "OpenSSL" appear in their names without prior written
  87. * permission of the OpenSSL Project.
  88. *
  89. * 6. Redistributions of any form whatsoever must retain the following
  90. * acknowledgment:
  91. * "This product includes software developed by the OpenSSL Project
  92. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  93. *
  94. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  95. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  96. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  97. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  98. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  99. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  100. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  101. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  102. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  103. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  104. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  105. * OF THE POSSIBILITY OF SUCH DAMAGE.
  106. * ====================================================================
  107. *
  108. * This product includes cryptographic software written by Eric Young
  109. * (eay@cryptsoft.com). This product includes software written by Tim
  110. * Hudson (tjh@cryptsoft.com).
  111. *
  112. */
  113. /* ====================================================================
  114. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  115. * ECC cipher suite support in OpenSSL originally developed by
  116. * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
  117. */
  118. #ifdef REF_CHECK
  119. # include <assert.h>
  120. #endif
  121. #include <stdio.h>
  122. #include "ssl_locl.h"
  123. #include "kssl_lcl.h"
  124. #include <openssl/objects.h>
  125. #include <openssl/lhash.h>
  126. #include <openssl/x509v3.h>
  127. #ifndef OPENSSL_NO_DH
  128. #include <openssl/dh.h>
  129. #endif
  130. const char *SSL_version_str=OPENSSL_VERSION_TEXT;
  131. SSL3_ENC_METHOD ssl3_undef_enc_method={
  132. /* evil casts, but these functions are only called if there's a library bug */
  133. (int (*)(SSL *,int))ssl_undefined_function,
  134. (int (*)(SSL *, unsigned char *, int))ssl_undefined_function,
  135. ssl_undefined_function,
  136. (int (*)(SSL *, unsigned char *, unsigned char *, int))ssl_undefined_function,
  137. (int (*)(SSL*, int))ssl_undefined_function,
  138. (int (*)(SSL *, EVP_MD_CTX *, EVP_MD_CTX *, const char*, int, unsigned char *))ssl_undefined_function
  139. };
  140. int SSL_clear(SSL *s)
  141. {
  142. if (s->method == NULL)
  143. {
  144. SSLerr(SSL_F_SSL_CLEAR,SSL_R_NO_METHOD_SPECIFIED);
  145. return(0);
  146. }
  147. if (ssl_clear_bad_session(s))
  148. {
  149. SSL_SESSION_free(s->session);
  150. s->session=NULL;
  151. }
  152. s->error=0;
  153. s->hit=0;
  154. s->shutdown=0;
  155. #if 0 /* Disabled since version 1.10 of this file (early return not
  156. * needed because SSL_clear is not called when doing renegotiation) */
  157. /* This is set if we are doing dynamic renegotiation so keep
  158. * the old cipher. It is sort of a SSL_clear_lite :-) */
  159. if (s->new_session) return(1);
  160. #else
  161. if (s->new_session)
  162. {
  163. SSLerr(SSL_F_SSL_CLEAR,ERR_R_INTERNAL_ERROR);
  164. return 0;
  165. }
  166. #endif
  167. s->type=0;
  168. s->state=SSL_ST_BEFORE|((s->server)?SSL_ST_ACCEPT:SSL_ST_CONNECT);
  169. s->version=s->method->version;
  170. s->client_version=s->version;
  171. s->rwstate=SSL_NOTHING;
  172. s->rstate=SSL_ST_READ_HEADER;
  173. #if 0
  174. s->read_ahead=s->ctx->read_ahead;
  175. #endif
  176. if (s->init_buf != NULL)
  177. {
  178. BUF_MEM_free(s->init_buf);
  179. s->init_buf=NULL;
  180. }
  181. ssl_clear_cipher_ctx(s);
  182. s->first_packet=0;
  183. #if 1
  184. /* Check to see if we were changed into a different method, if
  185. * so, revert back if we are not doing session-id reuse. */
  186. if (!s->in_handshake && (s->session == NULL) && (s->method != s->ctx->method))
  187. {
  188. s->method->ssl_free(s);
  189. s->method=s->ctx->method;
  190. if (!s->method->ssl_new(s))
  191. return(0);
  192. }
  193. else
  194. #endif
  195. s->method->ssl_clear(s);
  196. return(1);
  197. }
  198. /** Used to change an SSL_CTXs default SSL method type */
  199. int SSL_CTX_set_ssl_version(SSL_CTX *ctx,SSL_METHOD *meth)
  200. {
  201. STACK_OF(SSL_CIPHER) *sk;
  202. ctx->method=meth;
  203. sk=ssl_create_cipher_list(ctx->method,&(ctx->cipher_list),
  204. &(ctx->cipher_list_by_id),SSL_DEFAULT_CIPHER_LIST);
  205. if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0))
  206. {
  207. SSLerr(SSL_F_SSL_CTX_SET_SSL_VERSION,SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
  208. return(0);
  209. }
  210. return(1);
  211. }
  212. SSL *SSL_new(SSL_CTX *ctx)
  213. {
  214. SSL *s;
  215. if (ctx == NULL)
  216. {
  217. SSLerr(SSL_F_SSL_NEW,SSL_R_NULL_SSL_CTX);
  218. return(NULL);
  219. }
  220. if (ctx->method == NULL)
  221. {
  222. SSLerr(SSL_F_SSL_NEW,SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
  223. return(NULL);
  224. }
  225. s=(SSL *)OPENSSL_malloc(sizeof(SSL));
  226. if (s == NULL) goto err;
  227. memset(s,0,sizeof(SSL));
  228. #ifndef OPENSSL_NO_KRB5
  229. s->kssl_ctx = kssl_ctx_new();
  230. #endif /* OPENSSL_NO_KRB5 */
  231. s->options=ctx->options;
  232. s->mode=ctx->mode;
  233. s->max_cert_list=ctx->max_cert_list;
  234. if (ctx->cert != NULL)
  235. {
  236. /* Earlier library versions used to copy the pointer to
  237. * the CERT, not its contents; only when setting new
  238. * parameters for the per-SSL copy, ssl_cert_new would be
  239. * called (and the direct reference to the per-SSL_CTX
  240. * settings would be lost, but those still were indirectly
  241. * accessed for various purposes, and for that reason they
  242. * used to be known as s->ctx->default_cert).
  243. * Now we don't look at the SSL_CTX's CERT after having
  244. * duplicated it once. */
  245. s->cert = ssl_cert_dup(ctx->cert);
  246. if (s->cert == NULL)
  247. goto err;
  248. }
  249. else
  250. s->cert=NULL; /* Cannot really happen (see SSL_CTX_new) */
  251. s->read_ahead=ctx->read_ahead;
  252. s->msg_callback=ctx->msg_callback;
  253. s->msg_callback_arg=ctx->msg_callback_arg;
  254. s->verify_mode=ctx->verify_mode;
  255. #if 0
  256. s->verify_depth=ctx->verify_depth;
  257. #endif
  258. s->sid_ctx_length=ctx->sid_ctx_length;
  259. OPENSSL_assert(s->sid_ctx_length <= sizeof s->sid_ctx);
  260. memcpy(&s->sid_ctx,&ctx->sid_ctx,sizeof(s->sid_ctx));
  261. s->verify_callback=ctx->default_verify_callback;
  262. s->generate_session_id=ctx->generate_session_id;
  263. s->param = X509_VERIFY_PARAM_new();
  264. if (!s->param)
  265. goto err;
  266. X509_VERIFY_PARAM_inherit(s->param, ctx->param);
  267. #if 0
  268. s->purpose = ctx->purpose;
  269. s->trust = ctx->trust;
  270. #endif
  271. s->quiet_shutdown=ctx->quiet_shutdown;
  272. CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
  273. s->ctx=ctx;
  274. s->verify_result=X509_V_OK;
  275. s->method=ctx->method;
  276. if (!s->method->ssl_new(s))
  277. goto err;
  278. s->references=1;
  279. s->server=(ctx->method->ssl_accept == ssl_undefined_function)?0:1;
  280. SSL_clear(s);
  281. CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
  282. return(s);
  283. err:
  284. if (s != NULL)
  285. {
  286. if (s->cert != NULL)
  287. ssl_cert_free(s->cert);
  288. if (s->ctx != NULL)
  289. SSL_CTX_free(s->ctx); /* decrement reference count */
  290. OPENSSL_free(s);
  291. }
  292. SSLerr(SSL_F_SSL_NEW,ERR_R_MALLOC_FAILURE);
  293. return(NULL);
  294. }
  295. int SSL_CTX_set_session_id_context(SSL_CTX *ctx,const unsigned char *sid_ctx,
  296. unsigned int sid_ctx_len)
  297. {
  298. if(sid_ctx_len > sizeof ctx->sid_ctx)
  299. {
  300. SSLerr(SSL_F_SSL_CTX_SET_SESSION_ID_CONTEXT,SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  301. return 0;
  302. }
  303. ctx->sid_ctx_length=sid_ctx_len;
  304. memcpy(ctx->sid_ctx,sid_ctx,sid_ctx_len);
  305. return 1;
  306. }
  307. int SSL_set_session_id_context(SSL *ssl,const unsigned char *sid_ctx,
  308. unsigned int sid_ctx_len)
  309. {
  310. if(sid_ctx_len > SSL_MAX_SID_CTX_LENGTH)
  311. {
  312. SSLerr(SSL_F_SSL_SET_SESSION_ID_CONTEXT,SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  313. return 0;
  314. }
  315. ssl->sid_ctx_length=sid_ctx_len;
  316. memcpy(ssl->sid_ctx,sid_ctx,sid_ctx_len);
  317. return 1;
  318. }
  319. int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb)
  320. {
  321. CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
  322. ctx->generate_session_id = cb;
  323. CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
  324. return 1;
  325. }
  326. int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb)
  327. {
  328. CRYPTO_w_lock(CRYPTO_LOCK_SSL);
  329. ssl->generate_session_id = cb;
  330. CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
  331. return 1;
  332. }
  333. int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
  334. unsigned int id_len)
  335. {
  336. /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how
  337. * we can "construct" a session to give us the desired check - ie. to
  338. * find if there's a session in the hash table that would conflict with
  339. * any new session built out of this id/id_len and the ssl_version in
  340. * use by this SSL. */
  341. SSL_SESSION r, *p;
  342. if(id_len > sizeof r.session_id)
  343. return 0;
  344. r.ssl_version = ssl->version;
  345. r.session_id_length = id_len;
  346. memcpy(r.session_id, id, id_len);
  347. /* NB: SSLv2 always uses a fixed 16-byte session ID, so even if a
  348. * callback is calling us to check the uniqueness of a shorter ID, it
  349. * must be compared as a padded-out ID because that is what it will be
  350. * converted to when the callback has finished choosing it. */
  351. if((r.ssl_version == SSL2_VERSION) &&
  352. (id_len < SSL2_SSL_SESSION_ID_LENGTH))
  353. {
  354. memset(r.session_id + id_len, 0,
  355. SSL2_SSL_SESSION_ID_LENGTH - id_len);
  356. r.session_id_length = SSL2_SSL_SESSION_ID_LENGTH;
  357. }
  358. CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
  359. p = (SSL_SESSION *)lh_retrieve(ssl->ctx->sessions, &r);
  360. CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
  361. return (p != NULL);
  362. }
  363. int SSL_CTX_set_purpose(SSL_CTX *s, int purpose)
  364. {
  365. return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
  366. }
  367. int SSL_set_purpose(SSL *s, int purpose)
  368. {
  369. return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
  370. }
  371. int SSL_CTX_set_trust(SSL_CTX *s, int trust)
  372. {
  373. return X509_VERIFY_PARAM_set_trust(s->param, trust);
  374. }
  375. int SSL_set_trust(SSL *s, int trust)
  376. {
  377. return X509_VERIFY_PARAM_set_trust(s->param, trust);
  378. }
  379. void SSL_free(SSL *s)
  380. {
  381. int i;
  382. if(s == NULL)
  383. return;
  384. i=CRYPTO_add(&s->references,-1,CRYPTO_LOCK_SSL);
  385. #ifdef REF_PRINT
  386. REF_PRINT("SSL",s);
  387. #endif
  388. if (i > 0) return;
  389. #ifdef REF_CHECK
  390. if (i < 0)
  391. {
  392. fprintf(stderr,"SSL_free, bad reference count\n");
  393. abort(); /* ok */
  394. }
  395. #endif
  396. if (s->param)
  397. X509_VERIFY_PARAM_free(s->param);
  398. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
  399. if (s->bbio != NULL)
  400. {
  401. /* If the buffering BIO is in place, pop it off */
  402. if (s->bbio == s->wbio)
  403. {
  404. s->wbio=BIO_pop(s->wbio);
  405. }
  406. BIO_free(s->bbio);
  407. s->bbio=NULL;
  408. }
  409. if (s->rbio != NULL)
  410. BIO_free_all(s->rbio);
  411. if ((s->wbio != NULL) && (s->wbio != s->rbio))
  412. BIO_free_all(s->wbio);
  413. if (s->init_buf != NULL) BUF_MEM_free(s->init_buf);
  414. /* add extra stuff */
  415. if (s->cipher_list != NULL) sk_SSL_CIPHER_free(s->cipher_list);
  416. if (s->cipher_list_by_id != NULL) sk_SSL_CIPHER_free(s->cipher_list_by_id);
  417. /* Make the next call work :-) */
  418. if (s->session != NULL)
  419. {
  420. ssl_clear_bad_session(s);
  421. SSL_SESSION_free(s->session);
  422. }
  423. ssl_clear_cipher_ctx(s);
  424. if (s->cert != NULL) ssl_cert_free(s->cert);
  425. /* Free up if allocated */
  426. if (s->ctx) SSL_CTX_free(s->ctx);
  427. if (s->client_CA != NULL)
  428. sk_X509_NAME_pop_free(s->client_CA,X509_NAME_free);
  429. if (s->method != NULL) s->method->ssl_free(s);
  430. #ifndef OPENSSL_NO_KRB5
  431. if (s->kssl_ctx != NULL)
  432. kssl_ctx_free(s->kssl_ctx);
  433. #endif /* OPENSSL_NO_KRB5 */
  434. OPENSSL_free(s);
  435. }
  436. void SSL_set_bio(SSL *s,BIO *rbio,BIO *wbio)
  437. {
  438. /* If the output buffering BIO is still in place, remove it
  439. */
  440. if (s->bbio != NULL)
  441. {
  442. if (s->wbio == s->bbio)
  443. {
  444. s->wbio=s->wbio->next_bio;
  445. s->bbio->next_bio=NULL;
  446. }
  447. }
  448. if ((s->rbio != NULL) && (s->rbio != rbio))
  449. BIO_free_all(s->rbio);
  450. if ((s->wbio != NULL) && (s->wbio != wbio) && (s->rbio != s->wbio))
  451. BIO_free_all(s->wbio);
  452. s->rbio=rbio;
  453. s->wbio=wbio;
  454. }
  455. BIO *SSL_get_rbio(const SSL *s)
  456. { return(s->rbio); }
  457. BIO *SSL_get_wbio(const SSL *s)
  458. { return(s->wbio); }
  459. int SSL_get_fd(const SSL *s)
  460. {
  461. return(SSL_get_rfd(s));
  462. }
  463. int SSL_get_rfd(const SSL *s)
  464. {
  465. int ret= -1;
  466. BIO *b,*r;
  467. b=SSL_get_rbio(s);
  468. r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
  469. if (r != NULL)
  470. BIO_get_fd(r,&ret);
  471. return(ret);
  472. }
  473. int SSL_get_wfd(const SSL *s)
  474. {
  475. int ret= -1;
  476. BIO *b,*r;
  477. b=SSL_get_wbio(s);
  478. r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
  479. if (r != NULL)
  480. BIO_get_fd(r,&ret);
  481. return(ret);
  482. }
  483. #ifndef OPENSSL_NO_SOCK
  484. int SSL_set_fd(SSL *s,int fd)
  485. {
  486. int ret=0;
  487. BIO *bio=NULL;
  488. bio=BIO_new(BIO_s_socket());
  489. if (bio == NULL)
  490. {
  491. SSLerr(SSL_F_SSL_SET_FD,ERR_R_BUF_LIB);
  492. goto err;
  493. }
  494. BIO_set_fd(bio,fd,BIO_NOCLOSE);
  495. SSL_set_bio(s,bio,bio);
  496. ret=1;
  497. err:
  498. return(ret);
  499. }
  500. int SSL_set_wfd(SSL *s,int fd)
  501. {
  502. int ret=0;
  503. BIO *bio=NULL;
  504. if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_SOCKET)
  505. || ((int)BIO_get_fd(s->rbio,NULL) != fd))
  506. {
  507. bio=BIO_new(BIO_s_socket());
  508. if (bio == NULL)
  509. { SSLerr(SSL_F_SSL_SET_WFD,ERR_R_BUF_LIB); goto err; }
  510. BIO_set_fd(bio,fd,BIO_NOCLOSE);
  511. SSL_set_bio(s,SSL_get_rbio(s),bio);
  512. }
  513. else
  514. SSL_set_bio(s,SSL_get_rbio(s),SSL_get_rbio(s));
  515. ret=1;
  516. err:
  517. return(ret);
  518. }
  519. int SSL_set_rfd(SSL *s,int fd)
  520. {
  521. int ret=0;
  522. BIO *bio=NULL;
  523. if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_SOCKET)
  524. || ((int)BIO_get_fd(s->wbio,NULL) != fd))
  525. {
  526. bio=BIO_new(BIO_s_socket());
  527. if (bio == NULL)
  528. {
  529. SSLerr(SSL_F_SSL_SET_RFD,ERR_R_BUF_LIB);
  530. goto err;
  531. }
  532. BIO_set_fd(bio,fd,BIO_NOCLOSE);
  533. SSL_set_bio(s,bio,SSL_get_wbio(s));
  534. }
  535. else
  536. SSL_set_bio(s,SSL_get_wbio(s),SSL_get_wbio(s));
  537. ret=1;
  538. err:
  539. return(ret);
  540. }
  541. #endif
  542. /* return length of latest Finished message we sent, copy to 'buf' */
  543. size_t SSL_get_finished(const SSL *s, void *buf, size_t count)
  544. {
  545. size_t ret = 0;
  546. if (s->s3 != NULL)
  547. {
  548. ret = s->s3->tmp.finish_md_len;
  549. if (count > ret)
  550. count = ret;
  551. memcpy(buf, s->s3->tmp.finish_md, count);
  552. }
  553. return ret;
  554. }
  555. /* return length of latest Finished message we expected, copy to 'buf' */
  556. size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count)
  557. {
  558. size_t ret = 0;
  559. if (s->s3 != NULL)
  560. {
  561. ret = s->s3->tmp.peer_finish_md_len;
  562. if (count > ret)
  563. count = ret;
  564. memcpy(buf, s->s3->tmp.peer_finish_md, count);
  565. }
  566. return ret;
  567. }
  568. int SSL_get_verify_mode(const SSL *s)
  569. {
  570. return(s->verify_mode);
  571. }
  572. int SSL_get_verify_depth(const SSL *s)
  573. {
  574. return X509_VERIFY_PARAM_get_depth(s->param);
  575. }
  576. int (*SSL_get_verify_callback(const SSL *s))(int,X509_STORE_CTX *)
  577. {
  578. return(s->verify_callback);
  579. }
  580. int SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
  581. {
  582. return(ctx->verify_mode);
  583. }
  584. int SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
  585. {
  586. return X509_VERIFY_PARAM_get_depth(ctx->param);
  587. }
  588. int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int,X509_STORE_CTX *)
  589. {
  590. return(ctx->default_verify_callback);
  591. }
  592. void SSL_set_verify(SSL *s,int mode,
  593. int (*callback)(int ok,X509_STORE_CTX *ctx))
  594. {
  595. s->verify_mode=mode;
  596. if (callback != NULL)
  597. s->verify_callback=callback;
  598. }
  599. void SSL_set_verify_depth(SSL *s,int depth)
  600. {
  601. X509_VERIFY_PARAM_set_depth(s->param, depth);
  602. }
  603. void SSL_set_read_ahead(SSL *s,int yes)
  604. {
  605. s->read_ahead=yes;
  606. }
  607. int SSL_get_read_ahead(const SSL *s)
  608. {
  609. return(s->read_ahead);
  610. }
  611. int SSL_pending(const SSL *s)
  612. {
  613. /* SSL_pending cannot work properly if read-ahead is enabled
  614. * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)),
  615. * and it is impossible to fix since SSL_pending cannot report
  616. * errors that may be observed while scanning the new data.
  617. * (Note that SSL_pending() is often used as a boolean value,
  618. * so we'd better not return -1.)
  619. */
  620. return(s->method->ssl_pending(s));
  621. }
  622. X509 *SSL_get_peer_certificate(const SSL *s)
  623. {
  624. X509 *r;
  625. if ((s == NULL) || (s->session == NULL))
  626. r=NULL;
  627. else
  628. r=s->session->peer;
  629. if (r == NULL) return(r);
  630. CRYPTO_add(&r->references,1,CRYPTO_LOCK_X509);
  631. return(r);
  632. }
  633. STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s)
  634. {
  635. STACK_OF(X509) *r;
  636. if ((s == NULL) || (s->session == NULL) || (s->session->sess_cert == NULL))
  637. r=NULL;
  638. else
  639. r=s->session->sess_cert->cert_chain;
  640. /* If we are a client, cert_chain includes the peer's own
  641. * certificate; if we are a server, it does not. */
  642. return(r);
  643. }
  644. /* Now in theory, since the calling process own 't' it should be safe to
  645. * modify. We need to be able to read f without being hassled */
  646. void SSL_copy_session_id(SSL *t,const SSL *f)
  647. {
  648. CERT *tmp;
  649. /* Do we need to to SSL locking? */
  650. SSL_set_session(t,SSL_get_session(f));
  651. /* what if we are setup as SSLv2 but want to talk SSLv3 or
  652. * vice-versa */
  653. if (t->method != f->method)
  654. {
  655. t->method->ssl_free(t); /* cleanup current */
  656. t->method=f->method; /* change method */
  657. t->method->ssl_new(t); /* setup new */
  658. }
  659. tmp=t->cert;
  660. if (f->cert != NULL)
  661. {
  662. CRYPTO_add(&f->cert->references,1,CRYPTO_LOCK_SSL_CERT);
  663. t->cert=f->cert;
  664. }
  665. else
  666. t->cert=NULL;
  667. if (tmp != NULL) ssl_cert_free(tmp);
  668. SSL_set_session_id_context(t,f->sid_ctx,f->sid_ctx_length);
  669. }
  670. /* Fix this so it checks all the valid key/cert options */
  671. int SSL_CTX_check_private_key(const SSL_CTX *ctx)
  672. {
  673. if ( (ctx == NULL) ||
  674. (ctx->cert == NULL) ||
  675. (ctx->cert->key->x509 == NULL))
  676. {
  677. SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED);
  678. return(0);
  679. }
  680. if (ctx->cert->key->privatekey == NULL)
  681. {
  682. SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,SSL_R_NO_PRIVATE_KEY_ASSIGNED);
  683. return(0);
  684. }
  685. return(X509_check_private_key(ctx->cert->key->x509, ctx->cert->key->privatekey));
  686. }
  687. /* Fix this function so that it takes an optional type parameter */
  688. int SSL_check_private_key(const SSL *ssl)
  689. {
  690. if (ssl == NULL)
  691. {
  692. SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,ERR_R_PASSED_NULL_PARAMETER);
  693. return(0);
  694. }
  695. if (ssl->cert == NULL)
  696. {
  697. SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED);
  698. return 0;
  699. }
  700. if (ssl->cert->key->x509 == NULL)
  701. {
  702. SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED);
  703. return(0);
  704. }
  705. if (ssl->cert->key->privatekey == NULL)
  706. {
  707. SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_PRIVATE_KEY_ASSIGNED);
  708. return(0);
  709. }
  710. return(X509_check_private_key(ssl->cert->key->x509,
  711. ssl->cert->key->privatekey));
  712. }
  713. int SSL_accept(SSL *s)
  714. {
  715. if (s->handshake_func == 0)
  716. /* Not properly initialized yet */
  717. SSL_set_accept_state(s);
  718. return(s->method->ssl_accept(s));
  719. }
  720. int SSL_connect(SSL *s)
  721. {
  722. if (s->handshake_func == 0)
  723. /* Not properly initialized yet */
  724. SSL_set_connect_state(s);
  725. return(s->method->ssl_connect(s));
  726. }
  727. long SSL_get_default_timeout(const SSL *s)
  728. {
  729. return(s->method->get_timeout());
  730. }
  731. int SSL_read(SSL *s,void *buf,int num)
  732. {
  733. if (s->handshake_func == 0)
  734. {
  735. SSLerr(SSL_F_SSL_READ, SSL_R_UNINITIALIZED);
  736. return -1;
  737. }
  738. if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
  739. {
  740. s->rwstate=SSL_NOTHING;
  741. return(0);
  742. }
  743. return(s->method->ssl_read(s,buf,num));
  744. }
  745. int SSL_peek(SSL *s,void *buf,int num)
  746. {
  747. if (s->handshake_func == 0)
  748. {
  749. SSLerr(SSL_F_SSL_PEEK, SSL_R_UNINITIALIZED);
  750. return -1;
  751. }
  752. if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
  753. {
  754. return(0);
  755. }
  756. return(s->method->ssl_peek(s,buf,num));
  757. }
  758. int SSL_write(SSL *s,const void *buf,int num)
  759. {
  760. if (s->handshake_func == 0)
  761. {
  762. SSLerr(SSL_F_SSL_WRITE, SSL_R_UNINITIALIZED);
  763. return -1;
  764. }
  765. if (s->shutdown & SSL_SENT_SHUTDOWN)
  766. {
  767. s->rwstate=SSL_NOTHING;
  768. SSLerr(SSL_F_SSL_WRITE,SSL_R_PROTOCOL_IS_SHUTDOWN);
  769. return(-1);
  770. }
  771. return(s->method->ssl_write(s,buf,num));
  772. }
  773. int SSL_shutdown(SSL *s)
  774. {
  775. /* Note that this function behaves differently from what one might
  776. * expect. Return values are 0 for no success (yet),
  777. * 1 for success; but calling it once is usually not enough,
  778. * even if blocking I/O is used (see ssl3_shutdown).
  779. */
  780. if (s->handshake_func == 0)
  781. {
  782. SSLerr(SSL_F_SSL_SHUTDOWN, SSL_R_UNINITIALIZED);
  783. return -1;
  784. }
  785. if ((s != NULL) && !SSL_in_init(s))
  786. return(s->method->ssl_shutdown(s));
  787. else
  788. return(1);
  789. }
  790. int SSL_renegotiate(SSL *s)
  791. {
  792. if (s->new_session == 0)
  793. {
  794. s->new_session=1;
  795. }
  796. return(s->method->ssl_renegotiate(s));
  797. }
  798. int SSL_renegotiate_pending(SSL *s)
  799. {
  800. /* becomes true when negotiation is requested;
  801. * false again once a handshake has finished */
  802. return (s->new_session != 0);
  803. }
  804. long SSL_ctrl(SSL *s,int cmd,long larg,void *parg)
  805. {
  806. long l;
  807. switch (cmd)
  808. {
  809. case SSL_CTRL_GET_READ_AHEAD:
  810. return(s->read_ahead);
  811. case SSL_CTRL_SET_READ_AHEAD:
  812. l=s->read_ahead;
  813. s->read_ahead=larg;
  814. return(l);
  815. case SSL_CTRL_SET_MSG_CALLBACK_ARG:
  816. s->msg_callback_arg = parg;
  817. return 1;
  818. case SSL_CTRL_OPTIONS:
  819. return(s->options|=larg);
  820. case SSL_CTRL_MODE:
  821. return(s->mode|=larg);
  822. case SSL_CTRL_GET_MAX_CERT_LIST:
  823. return(s->max_cert_list);
  824. case SSL_CTRL_SET_MAX_CERT_LIST:
  825. l=s->max_cert_list;
  826. s->max_cert_list=larg;
  827. return(l);
  828. case SSL_CTRL_SET_MTU:
  829. if (SSL_version(s) == DTLS1_VERSION)
  830. {
  831. s->d1->mtu = larg;
  832. return larg;
  833. }
  834. return 0;
  835. default:
  836. return(s->method->ssl_ctrl(s,cmd,larg,parg));
  837. }
  838. }
  839. long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void))
  840. {
  841. switch(cmd)
  842. {
  843. case SSL_CTRL_SET_MSG_CALLBACK:
  844. s->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
  845. return 1;
  846. default:
  847. return(s->method->ssl_callback_ctrl(s,cmd,fp));
  848. }
  849. }
  850. struct lhash_st *SSL_CTX_sessions(SSL_CTX *ctx)
  851. {
  852. return ctx->sessions;
  853. }
  854. long SSL_CTX_ctrl(SSL_CTX *ctx,int cmd,long larg,void *parg)
  855. {
  856. long l;
  857. switch (cmd)
  858. {
  859. case SSL_CTRL_GET_READ_AHEAD:
  860. return(ctx->read_ahead);
  861. case SSL_CTRL_SET_READ_AHEAD:
  862. l=ctx->read_ahead;
  863. ctx->read_ahead=larg;
  864. return(l);
  865. case SSL_CTRL_SET_MSG_CALLBACK_ARG:
  866. ctx->msg_callback_arg = parg;
  867. return 1;
  868. case SSL_CTRL_GET_MAX_CERT_LIST:
  869. return(ctx->max_cert_list);
  870. case SSL_CTRL_SET_MAX_CERT_LIST:
  871. l=ctx->max_cert_list;
  872. ctx->max_cert_list=larg;
  873. return(l);
  874. case SSL_CTRL_SET_SESS_CACHE_SIZE:
  875. l=ctx->session_cache_size;
  876. ctx->session_cache_size=larg;
  877. return(l);
  878. case SSL_CTRL_GET_SESS_CACHE_SIZE:
  879. return(ctx->session_cache_size);
  880. case SSL_CTRL_SET_SESS_CACHE_MODE:
  881. l=ctx->session_cache_mode;
  882. ctx->session_cache_mode=larg;
  883. return(l);
  884. case SSL_CTRL_GET_SESS_CACHE_MODE:
  885. return(ctx->session_cache_mode);
  886. case SSL_CTRL_SESS_NUMBER:
  887. return(ctx->sessions->num_items);
  888. case SSL_CTRL_SESS_CONNECT:
  889. return(ctx->stats.sess_connect);
  890. case SSL_CTRL_SESS_CONNECT_GOOD:
  891. return(ctx->stats.sess_connect_good);
  892. case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
  893. return(ctx->stats.sess_connect_renegotiate);
  894. case SSL_CTRL_SESS_ACCEPT:
  895. return(ctx->stats.sess_accept);
  896. case SSL_CTRL_SESS_ACCEPT_GOOD:
  897. return(ctx->stats.sess_accept_good);
  898. case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
  899. return(ctx->stats.sess_accept_renegotiate);
  900. case SSL_CTRL_SESS_HIT:
  901. return(ctx->stats.sess_hit);
  902. case SSL_CTRL_SESS_CB_HIT:
  903. return(ctx->stats.sess_cb_hit);
  904. case SSL_CTRL_SESS_MISSES:
  905. return(ctx->stats.sess_miss);
  906. case SSL_CTRL_SESS_TIMEOUTS:
  907. return(ctx->stats.sess_timeout);
  908. case SSL_CTRL_SESS_CACHE_FULL:
  909. return(ctx->stats.sess_cache_full);
  910. case SSL_CTRL_OPTIONS:
  911. return(ctx->options|=larg);
  912. case SSL_CTRL_MODE:
  913. return(ctx->mode|=larg);
  914. default:
  915. return(ctx->method->ssl_ctx_ctrl(ctx,cmd,larg,parg));
  916. }
  917. }
  918. long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void))
  919. {
  920. switch(cmd)
  921. {
  922. case SSL_CTRL_SET_MSG_CALLBACK:
  923. ctx->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
  924. return 1;
  925. default:
  926. return(ctx->method->ssl_ctx_callback_ctrl(ctx,cmd,fp));
  927. }
  928. }
  929. int ssl_cipher_id_cmp(const SSL_CIPHER *a, const SSL_CIPHER *b)
  930. {
  931. long l;
  932. l=a->id-b->id;
  933. if (l == 0L)
  934. return(0);
  935. else
  936. return((l > 0)?1:-1);
  937. }
  938. int ssl_cipher_ptr_id_cmp(const SSL_CIPHER * const *ap,
  939. const SSL_CIPHER * const *bp)
  940. {
  941. long l;
  942. l=(*ap)->id-(*bp)->id;
  943. if (l == 0L)
  944. return(0);
  945. else
  946. return((l > 0)?1:-1);
  947. }
  948. /** return a STACK of the ciphers available for the SSL and in order of
  949. * preference */
  950. STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s)
  951. {
  952. if (s != NULL)
  953. {
  954. if (s->cipher_list != NULL)
  955. {
  956. return(s->cipher_list);
  957. }
  958. else if ((s->ctx != NULL) &&
  959. (s->ctx->cipher_list != NULL))
  960. {
  961. return(s->ctx->cipher_list);
  962. }
  963. }
  964. return(NULL);
  965. }
  966. /** return a STACK of the ciphers available for the SSL and in order of
  967. * algorithm id */
  968. STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s)
  969. {
  970. if (s != NULL)
  971. {
  972. if (s->cipher_list_by_id != NULL)
  973. {
  974. return(s->cipher_list_by_id);
  975. }
  976. else if ((s->ctx != NULL) &&
  977. (s->ctx->cipher_list_by_id != NULL))
  978. {
  979. return(s->ctx->cipher_list_by_id);
  980. }
  981. }
  982. return(NULL);
  983. }
  984. /** The old interface to get the same thing as SSL_get_ciphers() */
  985. const char *SSL_get_cipher_list(const SSL *s,int n)
  986. {
  987. SSL_CIPHER *c;
  988. STACK_OF(SSL_CIPHER) *sk;
  989. if (s == NULL) return(NULL);
  990. sk=SSL_get_ciphers(s);
  991. if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= n))
  992. return(NULL);
  993. c=sk_SSL_CIPHER_value(sk,n);
  994. if (c == NULL) return(NULL);
  995. return(c->name);
  996. }
  997. /** specify the ciphers to be used by default by the SSL_CTX */
  998. int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
  999. {
  1000. STACK_OF(SSL_CIPHER) *sk;
  1001. sk=ssl_create_cipher_list(ctx->method,&ctx->cipher_list,
  1002. &ctx->cipher_list_by_id,str);
  1003. /* ssl_create_cipher_list may return an empty stack if it
  1004. * was unable to find a cipher matching the given rule string
  1005. * (for example if the rule string specifies a cipher which
  1006. * has been disabled). This is not an error as far as
  1007. * ssl_create_cipher_list is concerned, and hence
  1008. * ctx->cipher_list and ctx->cipher_list_by_id has been
  1009. * updated. */
  1010. if (sk == NULL)
  1011. return 0;
  1012. else if (sk_SSL_CIPHER_num(sk) == 0)
  1013. {
  1014. SSLerr(SSL_F_SSL_CTX_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH);
  1015. return 0;
  1016. }
  1017. return 1;
  1018. }
  1019. /** specify the ciphers to be used by the SSL */
  1020. int SSL_set_cipher_list(SSL *s,const char *str)
  1021. {
  1022. STACK_OF(SSL_CIPHER) *sk;
  1023. sk=ssl_create_cipher_list(s->ctx->method,&s->cipher_list,
  1024. &s->cipher_list_by_id,str);
  1025. /* see comment in SSL_CTX_set_cipher_list */
  1026. if (sk == NULL)
  1027. return 0;
  1028. else if (sk_SSL_CIPHER_num(sk) == 0)
  1029. {
  1030. SSLerr(SSL_F_SSL_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH);
  1031. return 0;
  1032. }
  1033. return 1;
  1034. }
  1035. /* works well for SSLv2, not so good for SSLv3 */
  1036. char *SSL_get_shared_ciphers(const SSL *s,char *buf,int len)
  1037. {
  1038. char *p;
  1039. const char *cp;
  1040. STACK_OF(SSL_CIPHER) *sk;
  1041. SSL_CIPHER *c;
  1042. int i;
  1043. if ((s->session == NULL) || (s->session->ciphers == NULL) ||
  1044. (len < 2))
  1045. return(NULL);
  1046. p=buf;
  1047. sk=s->session->ciphers;
  1048. for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
  1049. {
  1050. /* Decrement for either the ':' or a '\0' */
  1051. len--;
  1052. c=sk_SSL_CIPHER_value(sk,i);
  1053. for (cp=c->name; *cp; )
  1054. {
  1055. if (len-- == 0)
  1056. {
  1057. *p='\0';
  1058. return(buf);
  1059. }
  1060. else
  1061. *(p++)= *(cp++);
  1062. }
  1063. *(p++)=':';
  1064. }
  1065. p[-1]='\0';
  1066. return(buf);
  1067. }
  1068. int ssl_cipher_list_to_bytes(SSL *s,STACK_OF(SSL_CIPHER) *sk,unsigned char *p,
  1069. int (*put_cb)(const SSL_CIPHER *, unsigned char *))
  1070. {
  1071. int i,j=0;
  1072. SSL_CIPHER *c;
  1073. unsigned char *q;
  1074. #ifndef OPENSSL_NO_KRB5
  1075. int nokrb5 = !kssl_tgt_is_available(s->kssl_ctx);
  1076. #endif /* OPENSSL_NO_KRB5 */
  1077. if (sk == NULL) return(0);
  1078. q=p;
  1079. for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
  1080. {
  1081. c=sk_SSL_CIPHER_value(sk,i);
  1082. #ifndef OPENSSL_NO_KRB5
  1083. if ((c->algorithms & SSL_KRB5) && nokrb5)
  1084. continue;
  1085. #endif /* OPENSSL_NO_KRB5 */
  1086. j = put_cb ? put_cb(c,p) : ssl_put_cipher_by_char(s,c,p);
  1087. p+=j;
  1088. }
  1089. return(p-q);
  1090. }
  1091. STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s,unsigned char *p,int num,
  1092. STACK_OF(SSL_CIPHER) **skp)
  1093. {
  1094. SSL_CIPHER *c;
  1095. STACK_OF(SSL_CIPHER) *sk;
  1096. int i,n;
  1097. n=ssl_put_cipher_by_char(s,NULL,NULL);
  1098. if ((num%n) != 0)
  1099. {
  1100. SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
  1101. return(NULL);
  1102. }
  1103. if ((skp == NULL) || (*skp == NULL))
  1104. sk=sk_SSL_CIPHER_new_null(); /* change perhaps later */
  1105. else
  1106. {
  1107. sk= *skp;
  1108. sk_SSL_CIPHER_zero(sk);
  1109. }
  1110. for (i=0; i<num; i+=n)
  1111. {
  1112. c=ssl_get_cipher_by_char(s,p);
  1113. p+=n;
  1114. if (c != NULL)
  1115. {
  1116. if (!sk_SSL_CIPHER_push(sk,c))
  1117. {
  1118. SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,ERR_R_MALLOC_FAILURE);
  1119. goto err;
  1120. }
  1121. }
  1122. }
  1123. if (skp != NULL)
  1124. *skp=sk;
  1125. return(sk);
  1126. err:
  1127. if ((skp == NULL) || (*skp == NULL))
  1128. sk_SSL_CIPHER_free(sk);
  1129. return(NULL);
  1130. }
  1131. unsigned long SSL_SESSION_hash(const SSL_SESSION *a)
  1132. {
  1133. unsigned long l;
  1134. l=(unsigned long)
  1135. ((unsigned int) a->session_id[0] )|
  1136. ((unsigned int) a->session_id[1]<< 8L)|
  1137. ((unsigned long)a->session_id[2]<<16L)|
  1138. ((unsigned long)a->session_id[3]<<24L);
  1139. return(l);
  1140. }
  1141. /* NB: If this function (or indeed the hash function which uses a sort of
  1142. * coarser function than this one) is changed, ensure
  1143. * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
  1144. * able to construct an SSL_SESSION that will collide with any existing session
  1145. * with a matching session ID. */
  1146. int SSL_SESSION_cmp(const SSL_SESSION *a,const SSL_SESSION *b)
  1147. {
  1148. if (a->ssl_version != b->ssl_version)
  1149. return(1);
  1150. if (a->session_id_length != b->session_id_length)
  1151. return(1);
  1152. return(memcmp(a->session_id,b->session_id,a->session_id_length));
  1153. }
  1154. /* These wrapper functions should remain rather than redeclaring
  1155. * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each
  1156. * variable. The reason is that the functions aren't static, they're exposed via
  1157. * ssl.h. */
  1158. static IMPLEMENT_LHASH_HASH_FN(SSL_SESSION_hash, SSL_SESSION *)
  1159. static IMPLEMENT_LHASH_COMP_FN(SSL_SESSION_cmp, SSL_SESSION *)
  1160. SSL_CTX *SSL_CTX_new(SSL_METHOD *meth)
  1161. {
  1162. SSL_CTX *ret=NULL;
  1163. if (meth == NULL)
  1164. {
  1165. SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_NULL_SSL_METHOD_PASSED);
  1166. return(NULL);
  1167. }
  1168. if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0)
  1169. {
  1170. SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
  1171. goto err;
  1172. }
  1173. ret=(SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
  1174. if (ret == NULL)
  1175. goto err;
  1176. memset(ret,0,sizeof(SSL_CTX));
  1177. ret->method=meth;
  1178. ret->cert_store=NULL;
  1179. ret->session_cache_mode=SSL_SESS_CACHE_SERVER;
  1180. ret->session_cache_size=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
  1181. ret->session_cache_head=NULL;
  1182. ret->session_cache_tail=NULL;
  1183. /* We take the system default */
  1184. ret->session_timeout=meth->get_timeout();
  1185. ret->new_session_cb=0;
  1186. ret->remove_session_cb=0;
  1187. ret->get_session_cb=0;
  1188. ret->generate_session_id=0;
  1189. memset((char *)&ret->stats,0,sizeof(ret->stats));
  1190. ret->references=1;
  1191. ret->quiet_shutdown=0;
  1192. /* ret->cipher=NULL;*/
  1193. /* ret->s2->challenge=NULL;
  1194. ret->master_key=NULL;
  1195. ret->key_arg=NULL;
  1196. ret->s2->conn_id=NULL; */
  1197. ret->info_callback=NULL;
  1198. ret->app_verify_callback=0;
  1199. ret->app_verify_arg=NULL;
  1200. ret->max_cert_list=SSL_MAX_CERT_LIST_DEFAULT;
  1201. ret->read_ahead=0;
  1202. ret->msg_callback=0;
  1203. ret->msg_callback_arg=NULL;
  1204. ret->verify_mode=SSL_VERIFY_NONE;
  1205. #if 0
  1206. ret->verify_depth=-1; /* Don't impose a limit (but x509_lu.c does) */
  1207. #endif
  1208. ret->sid_ctx_length=0;
  1209. ret->default_verify_callback=NULL;
  1210. if ((ret->cert=ssl_cert_new()) == NULL)
  1211. goto err;
  1212. ret->default_passwd_callback=0;
  1213. ret->default_passwd_callback_userdata=NULL;
  1214. ret->client_cert_cb=0;
  1215. ret->app_gen_cookie_cb=0;
  1216. ret->app_verify_cookie_cb=0;
  1217. ret->sessions=lh_new(LHASH_HASH_FN(SSL_SESSION_hash),
  1218. LHASH_COMP_FN(SSL_SESSION_cmp));
  1219. if (ret->sessions == NULL) goto err;
  1220. ret->cert_store=X509_STORE_new();
  1221. if (ret->cert_store == NULL) goto err;
  1222. ssl_create_cipher_list(ret->method,
  1223. &ret->cipher_list,&ret->cipher_list_by_id,
  1224. SSL_DEFAULT_CIPHER_LIST);
  1225. if (ret->cipher_list == NULL
  1226. || sk_SSL_CIPHER_num(ret->cipher_list) <= 0)
  1227. {
  1228. SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_LIBRARY_HAS_NO_CIPHERS);
  1229. goto err2;
  1230. }
  1231. ret->param = X509_VERIFY_PARAM_new();
  1232. if (!ret->param)
  1233. goto err;
  1234. if ((ret->rsa_md5=EVP_get_digestbyname("ssl2-md5")) == NULL)
  1235. {
  1236. SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL2_MD5_ROUTINES);
  1237. goto err2;
  1238. }
  1239. if ((ret->md5=EVP_get_digestbyname("ssl3-md5")) == NULL)
  1240. {
  1241. SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES);
  1242. goto err2;
  1243. }
  1244. if ((ret->sha1=EVP_get_digestbyname("ssl3-sha1")) == NULL)
  1245. {
  1246. SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES);
  1247. goto err2;
  1248. }
  1249. if ((ret->client_CA=sk_X509_NAME_new_null()) == NULL)
  1250. goto err;
  1251. CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
  1252. ret->extra_certs=NULL;
  1253. ret->comp_methods=SSL_COMP_get_compression_methods();
  1254. return(ret);
  1255. err:
  1256. SSLerr(SSL_F_SSL_CTX_NEW,ERR_R_MALLOC_FAILURE);
  1257. err2:
  1258. if (ret != NULL) SSL_CTX_free(ret);
  1259. return(NULL);
  1260. }
  1261. #if 0
  1262. static void SSL_COMP_free(SSL_COMP *comp)
  1263. { OPENSSL_free(comp); }
  1264. #endif
  1265. void SSL_CTX_free(SSL_CTX *a)
  1266. {
  1267. int i;
  1268. if (a == NULL) return;
  1269. i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX);
  1270. #ifdef REF_PRINT
  1271. REF_PRINT("SSL_CTX",a);
  1272. #endif
  1273. if (i > 0) return;
  1274. #ifdef REF_CHECK
  1275. if (i < 0)
  1276. {
  1277. fprintf(stderr,"SSL_CTX_free, bad reference count\n");
  1278. abort(); /* ok */
  1279. }
  1280. #endif
  1281. if (a->param)
  1282. X509_VERIFY_PARAM_free(a->param);
  1283. /*
  1284. * Free internal session cache. However: the remove_cb() may reference
  1285. * the ex_data of SSL_CTX, thus the ex_data store can only be removed
  1286. * after the sessions were flushed.
  1287. * As the ex_data handling routines might also touch the session cache,
  1288. * the most secure solution seems to be: empty (flush) the cache, then
  1289. * free ex_data, then finally free the cache.
  1290. * (See ticket [openssl.org #212].)
  1291. */
  1292. if (a->sessions != NULL)
  1293. SSL_CTX_flush_sessions(a,0);
  1294. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
  1295. if (a->sessions != NULL)
  1296. lh_free(a->sessions);
  1297. if (a->cert_store != NULL)
  1298. X509_STORE_free(a->cert_store);
  1299. if (a->cipher_list != NULL)
  1300. sk_SSL_CIPHER_free(a->cipher_list);
  1301. if (a->cipher_list_by_id != NULL)
  1302. sk_SSL_CIPHER_free(a->cipher_list_by_id);
  1303. if (a->cert != NULL)
  1304. ssl_cert_free(a->cert);
  1305. if (a->client_CA != NULL)
  1306. sk_X509_NAME_pop_free(a->client_CA,X509_NAME_free);
  1307. if (a->extra_certs != NULL)
  1308. sk_X509_pop_free(a->extra_certs,X509_free);
  1309. #if 0 /* This should never be done, since it removes a global database */
  1310. if (a->comp_methods != NULL)
  1311. sk_SSL_COMP_pop_free(a->comp_methods,SSL_COMP_free);
  1312. #else
  1313. a->comp_methods = NULL;
  1314. #endif
  1315. OPENSSL_free(a);
  1316. }
  1317. void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
  1318. {
  1319. ctx->default_passwd_callback=cb;
  1320. }
  1321. void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx,void *u)
  1322. {
  1323. ctx->default_passwd_callback_userdata=u;
  1324. }
  1325. void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, int (*cb)(X509_STORE_CTX *,void *), void *arg)
  1326. {
  1327. ctx->app_verify_callback=cb;
  1328. ctx->app_verify_arg=arg;
  1329. }
  1330. void SSL_CTX_set_verify(SSL_CTX *ctx,int mode,int (*cb)(int, X509_STORE_CTX *))
  1331. {
  1332. ctx->verify_mode=mode;
  1333. ctx->default_verify_callback=cb;
  1334. }
  1335. void SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth)
  1336. {
  1337. X509_VERIFY_PARAM_set_depth(ctx->param, depth);
  1338. }
  1339. void ssl_set_cert_masks(CERT *c, SSL_CIPHER *cipher)
  1340. {
  1341. CERT_PKEY *cpk;
  1342. int rsa_enc,rsa_tmp,rsa_sign,dh_tmp,dh_rsa,dh_dsa,dsa_sign;
  1343. int rsa_enc_export,dh_rsa_export,dh_dsa_export;
  1344. int rsa_tmp_export,dh_tmp_export,kl;
  1345. unsigned long mask,emask;
  1346. int have_ecc_cert, ecdh_ok, ecdsa_ok, ecc_pkey_size;
  1347. #ifndef OPENSSL_NO_ECDH
  1348. int have_ecdh_tmp;
  1349. #endif
  1350. X509 *x = NULL;
  1351. EVP_PKEY *ecc_pkey = NULL;
  1352. int signature_nid = 0;
  1353. if (c == NULL) return;
  1354. kl=SSL_C_EXPORT_PKEYLENGTH(cipher);
  1355. #ifndef OPENSSL_NO_RSA
  1356. rsa_tmp=(c->rsa_tmp != NULL || c->rsa_tmp_cb != NULL);
  1357. rsa_tmp_export=(c->rsa_tmp_cb != NULL ||
  1358. (rsa_tmp && RSA_size(c->rsa_tmp)*8 <= kl));
  1359. #else
  1360. rsa_tmp=rsa_tmp_export=0;
  1361. #endif
  1362. #ifndef OPENSSL_NO_DH
  1363. dh_tmp=(c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
  1364. dh_tmp_export=(c->dh_tmp_cb != NULL ||
  1365. (dh_tmp && DH_size(c->dh_tmp)*8 <= kl));
  1366. #else
  1367. dh_tmp=dh_tmp_export=0;
  1368. #endif
  1369. #ifndef OPENSSL_NO_ECDH
  1370. have_ecdh_tmp=(c->ecdh_tmp != NULL || c->ecdh_tmp_cb != NULL);
  1371. #endif
  1372. cpk= &(c->pkeys[SSL_PKEY_RSA_ENC]);
  1373. rsa_enc= (cpk->x509 != NULL && cpk->privatekey != NULL);
  1374. rsa_enc_export=(rsa_enc && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
  1375. cpk= &(c->pkeys[SSL_PKEY_RSA_SIGN]);
  1376. rsa_sign=(cpk->x509 != NULL && cpk->privatekey != NULL);
  1377. cpk= &(c->pkeys[SSL_PKEY_DSA_SIGN]);
  1378. dsa_sign=(cpk->x509 != NULL && cpk->privatekey != NULL);
  1379. cpk= &(c->pkeys[SSL_PKEY_DH_RSA]);
  1380. dh_rsa= (cpk->x509 != NULL && cpk->privatekey != NULL);
  1381. dh_rsa_export=(dh_rsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
  1382. cpk= &(c->pkeys[SSL_PKEY_DH_DSA]);
  1383. /* FIX THIS EAY EAY EAY */
  1384. dh_dsa= (cpk->x509 != NULL && cpk->privatekey != NULL);
  1385. dh_dsa_export=(dh_dsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
  1386. cpk= &(c->pkeys[SSL_PKEY_ECC]);
  1387. have_ecc_cert= (cpk->x509 != NULL && cpk->privatekey != NULL);
  1388. mask=0;
  1389. emask=0;
  1390. #ifdef CIPHER_DEBUG
  1391. printf("rt=%d rte=%d dht=%d re=%d ree=%d rs=%d ds=%d dhr=%d dhd=%d\n",
  1392. rsa_tmp,rsa_tmp_export,dh_tmp,
  1393. rsa_enc,rsa_enc_export,rsa_sign,dsa_sign,dh_rsa,dh_dsa);
  1394. #endif
  1395. if (rsa_enc || (rsa_tmp && rsa_sign))
  1396. mask|=SSL_kRSA;
  1397. if (rsa_enc_export || (rsa_tmp_export && (rsa_sign || rsa_enc)))
  1398. emask|=SSL_kRSA;
  1399. #if 0
  1400. /* The match needs to be both kEDH and aRSA or aDSA, so don't worry */
  1401. if ( (dh_tmp || dh_rsa || dh_dsa) &&
  1402. (rsa_enc || rsa_sign || dsa_sign))
  1403. mask|=SSL_kEDH;
  1404. if ((dh_tmp_export || dh_rsa_export || dh_dsa_export) &&
  1405. (rsa_enc || rsa_sign || dsa_sign))
  1406. emask|=SSL_kEDH;
  1407. #endif
  1408. if (dh_tmp_export)
  1409. emask|=SSL_kEDH;
  1410. if (dh_tmp)
  1411. mask|=SSL_kEDH;
  1412. if (dh_rsa) mask|=SSL_kDHr;
  1413. if (dh_rsa_export) emask|=SSL_kDHr;
  1414. if (dh_dsa) mask|=SSL_kDHd;
  1415. if (dh_dsa_export) emask|=SSL_kDHd;
  1416. if (rsa_enc || rsa_sign)
  1417. {
  1418. mask|=SSL_aRSA;
  1419. emask|=SSL_aRSA;
  1420. }
  1421. if (dsa_sign)
  1422. {
  1423. mask|=SSL_aDSS;
  1424. emask|=SSL_aDSS;
  1425. }
  1426. mask|=SSL_aNULL;
  1427. emask|=SSL_aNULL;
  1428. #ifndef OPENSSL_NO_KRB5
  1429. mask|=SSL_kKRB5|SSL_aKRB5;
  1430. emask|=SSL_kKRB5|SSL_aKRB5;
  1431. #endif
  1432. /* An ECC certificate may be usable for ECDH and/or
  1433. * ECDSA cipher suites depending on the key usage extension.
  1434. */
  1435. if (have_ecc_cert)
  1436. {
  1437. /* This call populates extension flags (ex_flags) */
  1438. x = (c->pkeys[SSL_PKEY_ECC]).x509;
  1439. X509_check_purpose(x, -1, 0);
  1440. ecdh_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
  1441. (x->ex_kusage & X509v3_KU_KEY_AGREEMENT) : 1;
  1442. ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
  1443. (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) : 1;
  1444. ecc_pkey = X509_get_pubkey(x);
  1445. ecc_pkey_size = (ecc_pkey != NULL) ?
  1446. EVP_PKEY_bits(ecc_pkey) : 0;
  1447. EVP_PKEY_free(ecc_pkey);
  1448. if ((x->sig_alg) && (x->sig_alg->algorithm))
  1449. signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
  1450. #ifndef OPENSSL_NO_ECDH
  1451. if (ecdh_ok)
  1452. {
  1453. if ((signature_nid == NID_md5WithRSAEncryption) ||
  1454. (signature_nid == NID_md4WithRSAEncryption) ||
  1455. (signature_nid == NID_md2WithRSAEncryption))
  1456. {
  1457. mask|=SSL_kECDH|SSL_aRSA;
  1458. if (ecc_pkey_size <= 163)
  1459. emask|=SSL_kECDH|SSL_aRSA;
  1460. }
  1461. if (signature_nid == NID_ecdsa_with_SHA1)
  1462. {
  1463. mask|=SSL_kECDH|SSL_aECDSA;
  1464. if (ecc_pkey_size <= 163)
  1465. emask|=SSL_kECDH|SSL_aECDSA;
  1466. }
  1467. }
  1468. #endif
  1469. #ifndef OPENSSL_NO_ECDSA
  1470. if (ecdsa_ok)
  1471. {
  1472. mask|=SSL_aECDSA;
  1473. emask|=SSL_aECDSA;
  1474. }
  1475. #endif
  1476. }
  1477. #ifndef OPENSSL_NO_ECDH
  1478. if (have_ecdh_tmp)
  1479. {
  1480. mask|=SSL_kECDHE;
  1481. emask|=SSL_kECDHE;
  1482. }
  1483. #endif
  1484. c->mask=mask;
  1485. c->export_mask=emask;
  1486. c->valid=1;
  1487. }
  1488. /* This handy macro borrowed from crypto/x509v3/v3_purp.c */
  1489. #define ku_reject(x, usage) \
  1490. (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
  1491. int check_srvr_ecc_cert_and_alg(X509 *x, SSL_CIPHER *cs)
  1492. {
  1493. unsigned long alg = cs->algorithms;
  1494. EVP_PKEY *pkey = NULL;
  1495. int keysize = 0;
  1496. int signature_nid = 0;
  1497. if (SSL_C_IS_EXPORT(cs))
  1498. {
  1499. /* ECDH key length in export ciphers must be <= 163 bits */
  1500. pkey = X509_get_pubkey(x);
  1501. if (pkey == NULL) return 0;
  1502. keysize = EVP_PKEY_bits(pkey);
  1503. EVP_PKEY_free(pkey);
  1504. if (keysize > 163) return 0;
  1505. }
  1506. /* This call populates the ex_flags field correctly */
  1507. X509_check_purpose(x, -1, 0);
  1508. if ((x->sig_alg) && (x->sig_alg->algorithm))
  1509. signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
  1510. if (alg & SSL_kECDH)
  1511. {
  1512. /* key usage, if present, must allow key agreement */
  1513. if (ku_reject(x, X509v3_KU_KEY_AGREEMENT))
  1514. {
  1515. return 0;
  1516. }
  1517. if (alg & SSL_aECDSA)
  1518. {
  1519. /* signature alg must be ECDSA */
  1520. if (signature_nid != NID_ecdsa_with_SHA1)
  1521. {
  1522. return 0;
  1523. }
  1524. }
  1525. if (alg & SSL_aRSA)
  1526. {
  1527. /* signature alg must be RSA */
  1528. if ((signature_nid != NID_md5WithRSAEncryption) &&
  1529. (signature_nid != NID_md4WithRSAEncryption) &&
  1530. (signature_nid != NID_md2WithRSAEncryption))
  1531. {
  1532. return 0;
  1533. }
  1534. }
  1535. }
  1536. else if (alg & SSL_aECDSA)
  1537. {
  1538. /* key usage, if present, must allow signing */
  1539. if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE))
  1540. {
  1541. return 0;
  1542. }
  1543. }
  1544. return 1; /* all checks are ok */
  1545. }
  1546. /* THIS NEEDS CLEANING UP */
  1547. X509 *ssl_get_server_send_cert(SSL *s)
  1548. {
  1549. unsigned long alg,mask,kalg;
  1550. CERT *c;
  1551. int i,is_export;
  1552. c=s->cert;
  1553. ssl_set_cert_masks(c, s->s3->tmp.new_cipher);
  1554. alg=s->s3->tmp.new_cipher->algorithms;
  1555. is_export=SSL_C_IS_EXPORT(s->s3->tmp.new_cipher);
  1556. mask=is_export?c->export_mask:c->mask;
  1557. kalg=alg&(SSL_MKEY_MASK|SSL_AUTH_MASK);
  1558. if (kalg & SSL_kECDH)
  1559. {
  1560. /* we don't need to look at SSL_kECDHE
  1561. * since no certificate is needed for
  1562. * anon ECDH and for authenticated
  1563. * ECDHE, the check for the auth
  1564. * algorithm will set i correctly
  1565. * NOTE: For ECDH-RSA, we need an ECC
  1566. * not an RSA cert but for ECDHE-RSA
  1567. * we need an RSA cert. Placing the
  1568. * checks for SSL_kECDH before RSA
  1569. * checks ensures the correct cert is chosen.
  1570. */
  1571. i=SSL_PKEY_ECC;
  1572. }
  1573. else if (kalg & SSL_aECDSA)
  1574. {
  1575. i=SSL_PKEY_ECC;
  1576. }
  1577. else if (kalg & SSL_kDHr)
  1578. i=SSL_PKEY_DH_RSA;
  1579. else if (kalg & SSL_kDHd)
  1580. i=SSL_PKEY_DH_DSA;
  1581. else if (kalg & SSL_aDSS)
  1582. i=SSL_PKEY_DSA_SIGN;
  1583. else if (kalg & SSL_aRSA)
  1584. {
  1585. if (c->pkeys[SSL_PKEY_RSA_ENC].x509 == NULL)
  1586. i=SSL_PKEY_RSA_SIGN;
  1587. else
  1588. i=SSL_PKEY_RSA_ENC;
  1589. }
  1590. else if (kalg & SSL_aKRB5)
  1591. {
  1592. /* VRS something else here? */
  1593. return(NULL);
  1594. }
  1595. else /* if (kalg & SSL_aNULL) */
  1596. {
  1597. SSLerr(SSL_F_SSL_GET_SERVER_SEND_CERT,ERR_R_INTERNAL_ERROR);
  1598. return(NULL);
  1599. }
  1600. if (c->pkeys[i].x509 == NULL) return(NULL);
  1601. return(c->pkeys[i].x509);
  1602. }
  1603. EVP_PKEY *ssl_get_sign_pkey(SSL *s,SSL_CIPHER *cipher)
  1604. {
  1605. unsigned long alg;
  1606. CERT *c;
  1607. alg=cipher->algorithms;
  1608. c=s->cert;
  1609. if ((alg & SSL_aDSS) &&
  1610. (c->pkeys[SSL_PKEY_DSA_SIGN].privatekey != NULL))
  1611. return(c->pkeys[SSL_PKEY_DSA_SIGN].privatekey);
  1612. else if (alg & SSL_aRSA)
  1613. {
  1614. if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL)
  1615. return(c->pkeys[SSL_PKEY_RSA_SIGN].privatekey);
  1616. else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL)
  1617. return(c->pkeys[SSL_PKEY_RSA_ENC].privatekey);
  1618. else
  1619. return(NULL);
  1620. }
  1621. else if ((alg & SSL_aECDSA) &&
  1622. (c->pkeys[SSL_PKEY_ECC].privatekey != NULL))
  1623. return(c->pkeys[SSL_PKEY_ECC].privatekey);
  1624. else /* if (alg & SSL_aNULL) */
  1625. {
  1626. SSLerr(SSL_F_SSL_GET_SIGN_PKEY,ERR_R_INTERNAL_ERROR);
  1627. return(NULL);
  1628. }
  1629. }
  1630. void ssl_update_cache(SSL *s,int mode)
  1631. {
  1632. int i;
  1633. /* If the session_id_length is 0, we are not supposed to cache it,
  1634. * and it would be rather hard to do anyway :-) */
  1635. if (s->session->session_id_length == 0) return;
  1636. i=s->ctx->session_cache_mode;
  1637. if ((i & mode) && (!s->hit)
  1638. && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE)
  1639. || SSL_CTX_add_session(s->ctx,s->session))
  1640. && (s->ctx->new_session_cb != NULL))
  1641. {
  1642. CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION);
  1643. if (!s->ctx->new_session_cb(s,s->session))
  1644. SSL_SESSION_free(s->session);
  1645. }
  1646. /* auto flush every 255 connections */
  1647. if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) &&
  1648. ((i & mode) == mode))
  1649. {
  1650. if ( (((mode & SSL_SESS_CACHE_CLIENT)
  1651. ?s->ctx->stats.sess_connect_good
  1652. :s->ctx->stats.sess_accept_good) & 0xff) == 0xff)
  1653. {
  1654. SSL_CTX_flush_sessions(s->ctx,time(NULL));
  1655. }
  1656. }
  1657. }
  1658. SSL_METHOD *SSL_get_ssl_method(SSL *s)
  1659. {
  1660. return(s->method);
  1661. }
  1662. int SSL_set_ssl_method(SSL *s,SSL_METHOD *meth)
  1663. {
  1664. int conn= -1;
  1665. int ret=1;
  1666. if (s->method != meth)
  1667. {
  1668. if (s->handshake_func != NULL)
  1669. conn=(s->handshake_func == s->method->ssl_connect);
  1670. if (s->method->version == meth->version)
  1671. s->method=meth;
  1672. else
  1673. {
  1674. s->method->ssl_free(s);
  1675. s->method=meth;
  1676. ret=s->method->ssl_new(s);
  1677. }
  1678. if (conn == 1)
  1679. s->handshake_func=meth->ssl_connect;
  1680. else if (conn == 0)
  1681. s->handshake_func=meth->ssl_accept;
  1682. }
  1683. return(ret);
  1684. }
  1685. int SSL_get_error(const SSL *s,int i)
  1686. {
  1687. int reason;
  1688. unsigned long l;
  1689. BIO *bio;
  1690. if (i > 0) return(SSL_ERROR_NONE);
  1691. /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
  1692. * etc, where we do encode the error */
  1693. if ((l=ERR_peek_error()) != 0)
  1694. {
  1695. if (ERR_GET_LIB(l) == ERR_LIB_SYS)
  1696. return(SSL_ERROR_SYSCALL);
  1697. else
  1698. return(SSL_ERROR_SSL);
  1699. }
  1700. if ((i < 0) && SSL_want_read(s))
  1701. {
  1702. bio=SSL_get_rbio(s);
  1703. if (BIO_should_read(bio))
  1704. return(SSL_ERROR_WANT_READ);
  1705. else if (BIO_should_write(bio))
  1706. /* This one doesn't make too much sense ... We never try
  1707. * to write to the rbio, and an application program where
  1708. * rbio and wbio are separate couldn't even know what it
  1709. * should wait for.
  1710. * However if we ever set s->rwstate incorrectly
  1711. * (so that we have SSL_want_read(s) instead of
  1712. * SSL_want_write(s)) and rbio and wbio *are* the same,
  1713. * this test works around that bug; so it might be safer
  1714. * to keep it. */
  1715. return(SSL_ERROR_WANT_WRITE);
  1716. else if (BIO_should_io_special(bio))
  1717. {
  1718. reason=BIO_get_retry_reason(bio);
  1719. if (reason == BIO_RR_CONNECT)
  1720. return(SSL_ERROR_WANT_CONNECT);
  1721. else if (reason == BIO_RR_ACCEPT)
  1722. return(SSL_ERROR_WANT_ACCEPT);
  1723. else
  1724. return(SSL_ERROR_SYSCALL); /* unknown */
  1725. }
  1726. }
  1727. if ((i < 0) && SSL_want_write(s))
  1728. {
  1729. bio=SSL_get_wbio(s);
  1730. if (BIO_should_write(bio))
  1731. return(SSL_ERROR_WANT_WRITE);
  1732. else if (BIO_should_read(bio))
  1733. /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
  1734. return(SSL_ERROR_WANT_READ);
  1735. else if (BIO_should_io_special(bio))
  1736. {
  1737. reason=BIO_get_retry_reason(bio);
  1738. if (reason == BIO_RR_CONNECT)
  1739. return(SSL_ERROR_WANT_CONNECT);
  1740. else if (reason == BIO_RR_ACCEPT)
  1741. return(SSL_ERROR_WANT_ACCEPT);
  1742. else
  1743. return(SSL_ERROR_SYSCALL);
  1744. }
  1745. }
  1746. if ((i < 0) && SSL_want_x509_lookup(s))
  1747. {
  1748. return(SSL_ERROR_WANT_X509_LOOKUP);
  1749. }
  1750. if (i == 0)
  1751. {
  1752. if (s->version == SSL2_VERSION)
  1753. {
  1754. /* assume it is the socket being closed */
  1755. return(SSL_ERROR_ZERO_RETURN);
  1756. }
  1757. else
  1758. {
  1759. if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
  1760. (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
  1761. return(SSL_ERROR_ZERO_RETURN);
  1762. }
  1763. }
  1764. return(SSL_ERROR_SYSCALL);
  1765. }
  1766. int SSL_do_handshake(SSL *s)
  1767. {
  1768. int ret=1;
  1769. if (s->handshake_func == NULL)
  1770. {
  1771. SSLerr(SSL_F_SSL_DO_HANDSHAKE,SSL_R_CONNECTION_TYPE_NOT_SET);
  1772. return(-1);
  1773. }
  1774. s->method->ssl_renegotiate_check(s);
  1775. if (SSL_in_init(s) || SSL_in_before(s))
  1776. {
  1777. ret=s->handshake_func(s);
  1778. }
  1779. return(ret);
  1780. }
  1781. /* For the next 2 functions, SSL_clear() sets shutdown and so
  1782. * one of these calls will reset it */
  1783. void SSL_set_accept_state(SSL *s)
  1784. {
  1785. s->server=1;
  1786. s->shutdown=0;
  1787. s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE;
  1788. s->handshake_func=s->method->ssl_accept;
  1789. /* clear the current cipher */
  1790. ssl_clear_cipher_ctx(s);
  1791. }
  1792. void SSL_set_connect_state(SSL *s)
  1793. {
  1794. s->server=0;
  1795. s->shutdown=0;
  1796. s->state=SSL_ST_CONNECT|SSL_ST_BEFORE;
  1797. s->handshake_func=s->method->ssl_connect;
  1798. /* clear the current cipher */
  1799. ssl_clear_cipher_ctx(s);
  1800. }
  1801. int ssl_undefined_function(SSL *s)
  1802. {
  1803. SSLerr(SSL_F_SSL_UNDEFINED_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1804. return(0);
  1805. }
  1806. int ssl_undefined_void_function(void)
  1807. {
  1808. SSLerr(SSL_F_SSL_UNDEFINED_VOID_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1809. return(0);
  1810. }
  1811. int ssl_undefined_const_function(const SSL *s)
  1812. {
  1813. SSLerr(SSL_F_SSL_UNDEFINED_CONST_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1814. return(0);
  1815. }
  1816. SSL_METHOD *ssl_bad_method(int ver)
  1817. {
  1818. SSLerr(SSL_F_SSL_BAD_METHOD,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1819. return(NULL);
  1820. }
  1821. const char *SSL_get_version(const SSL *s)
  1822. {
  1823. if (s->version == TLS1_VERSION)
  1824. return("TLSv1");
  1825. else if (s->version == SSL3_VERSION)
  1826. return("SSLv3");
  1827. else if (s->version == SSL2_VERSION)
  1828. return("SSLv2");
  1829. else
  1830. return("unknown");
  1831. }
  1832. SSL *SSL_dup(SSL *s)
  1833. {
  1834. STACK_OF(X509_NAME) *sk;
  1835. X509_NAME *xn;
  1836. SSL *ret;
  1837. int i;
  1838. if ((ret=SSL_new(SSL_get_SSL_CTX(s))) == NULL)
  1839. return(NULL);
  1840. ret->version = s->version;
  1841. ret->type = s->type;
  1842. ret->method = s->method;
  1843. if (s->session != NULL)
  1844. {
  1845. /* This copies session-id, SSL_METHOD, sid_ctx, and 'cert' */
  1846. SSL_copy_session_id(ret,s);
  1847. }
  1848. else
  1849. {
  1850. /* No session has been established yet, so we have to expect
  1851. * that s->cert or ret->cert will be changed later --
  1852. * they should not both point to the same object,
  1853. * and thus we can't use SSL_copy_session_id. */
  1854. ret->method->ssl_free(ret);
  1855. ret->method = s->method;
  1856. ret->method->ssl_new(ret);
  1857. if (s->cert != NULL)
  1858. {
  1859. if (ret->cert != NULL)
  1860. {
  1861. ssl_cert_free(ret->cert);
  1862. }
  1863. ret->cert = ssl_cert_dup(s->cert);
  1864. if (ret->cert == NULL)
  1865. goto err;
  1866. }
  1867. SSL_set_session_id_context(ret,
  1868. s->sid_ctx, s->sid_ctx_length);
  1869. }
  1870. ret->options=s->options;
  1871. ret->mode=s->mode;
  1872. SSL_set_max_cert_list(ret,SSL_get_max_cert_list(s));
  1873. SSL_set_read_ahead(ret,SSL_get_read_ahead(s));
  1874. ret->msg_callback = s->msg_callback;
  1875. ret->msg_callback_arg = s->msg_callback_arg;
  1876. SSL_set_verify(ret,SSL_get_verify_mode(s),
  1877. SSL_get_verify_callback(s));
  1878. SSL_set_verify_depth(ret,SSL_get_verify_depth(s));
  1879. ret->generate_session_id = s->generate_session_id;
  1880. SSL_set_info_callback(ret,SSL_get_info_callback(s));
  1881. ret->debug=s->debug;
  1882. /* copy app data, a little dangerous perhaps */
  1883. if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL, &ret->ex_data, &s->ex_data))
  1884. goto err;
  1885. /* setup rbio, and wbio */
  1886. if (s->rbio != NULL)
  1887. {
  1888. if (!BIO_dup_state(s->rbio,(char *)&ret->rbio))
  1889. goto err;
  1890. }
  1891. if (s->wbio != NULL)
  1892. {
  1893. if (s->wbio != s->rbio)
  1894. {
  1895. if (!BIO_dup_state(s->wbio,(char *)&ret->wbio))
  1896. goto err;
  1897. }
  1898. else
  1899. ret->wbio=ret->rbio;
  1900. }
  1901. ret->rwstate = s->rwstate;
  1902. ret->in_handshake = s->in_handshake;
  1903. ret->handshake_func = s->handshake_func;
  1904. ret->server = s->server;
  1905. ret->new_session = s->new_session;
  1906. ret->quiet_shutdown = s->quiet_shutdown;
  1907. ret->shutdown=s->shutdown;
  1908. ret->state=s->state; /* SSL_dup does not really work at any state, though */
  1909. ret->rstate=s->rstate;
  1910. ret->init_num = 0; /* would have to copy ret->init_buf, ret->init_msg, ret->init_num, ret->init_off */
  1911. ret->hit=s->hit;
  1912. X509_VERIFY_PARAM_inherit(ret->param, s->param);
  1913. /* dup the cipher_list and cipher_list_by_id stacks */
  1914. if (s->cipher_list != NULL)
  1915. {
  1916. if ((ret->cipher_list=sk_SSL_CIPHER_dup(s->cipher_list)) == NULL)
  1917. goto err;
  1918. }
  1919. if (s->cipher_list_by_id != NULL)
  1920. if ((ret->cipher_list_by_id=sk_SSL_CIPHER_dup(s->cipher_list_by_id))
  1921. == NULL)
  1922. goto err;
  1923. /* Dup the client_CA list */
  1924. if (s->client_CA != NULL)
  1925. {
  1926. if ((sk=sk_X509_NAME_dup(s->client_CA)) == NULL) goto err;
  1927. ret->client_CA=sk;
  1928. for (i=0; i<sk_X509_NAME_num(sk); i++)
  1929. {
  1930. xn=sk_X509_NAME_value(sk,i);
  1931. if (sk_X509_NAME_set(sk,i,X509_NAME_dup(xn)) == NULL)
  1932. {
  1933. X509_NAME_free(xn);
  1934. goto err;
  1935. }
  1936. }
  1937. }
  1938. if (0)
  1939. {
  1940. err:
  1941. if (ret != NULL) SSL_free(ret);
  1942. ret=NULL;
  1943. }
  1944. return(ret);
  1945. }
  1946. void ssl_clear_cipher_ctx(SSL *s)
  1947. {
  1948. if (s->enc_read_ctx != NULL)
  1949. {
  1950. EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
  1951. OPENSSL_free(s->enc_read_ctx);
  1952. s->enc_read_ctx=NULL;
  1953. }
  1954. if (s->enc_write_ctx != NULL)
  1955. {
  1956. EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
  1957. OPENSSL_free(s->enc_write_ctx);
  1958. s->enc_write_ctx=NULL;
  1959. }
  1960. if (s->expand != NULL)
  1961. {
  1962. COMP_CTX_free(s->expand);
  1963. s->expand=NULL;
  1964. }
  1965. if (s->compress != NULL)
  1966. {
  1967. COMP_CTX_free(s->compress);
  1968. s->compress=NULL;
  1969. }
  1970. }
  1971. /* Fix this function so that it takes an optional type parameter */
  1972. X509 *SSL_get_certificate(const SSL *s)
  1973. {
  1974. if (s->cert != NULL)
  1975. return(s->cert->key->x509);
  1976. else
  1977. return(NULL);
  1978. }
  1979. /* Fix this function so that it takes an optional type parameter */
  1980. EVP_PKEY *SSL_get_privatekey(SSL *s)
  1981. {
  1982. if (s->cert != NULL)
  1983. return(s->cert->key->privatekey);
  1984. else
  1985. return(NULL);
  1986. }
  1987. SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
  1988. {
  1989. if ((s->session != NULL) && (s->session->cipher != NULL))
  1990. return(s->session->cipher);
  1991. return(NULL);
  1992. }
  1993. const COMP_METHOD *SSL_get_current_compression(SSL *s)
  1994. {
  1995. if (s->compress != NULL)
  1996. return(s->compress->meth);
  1997. return(NULL);
  1998. }
  1999. const COMP_METHOD *SSL_get_current_expansion(SSL *s)
  2000. {
  2001. if (s->expand != NULL)
  2002. return(s->expand->meth);
  2003. return(NULL);
  2004. }
  2005. int ssl_init_wbio_buffer(SSL *s,int push)
  2006. {
  2007. BIO *bbio;
  2008. if (s->bbio == NULL)
  2009. {
  2010. bbio=BIO_new(BIO_f_buffer());
  2011. if (bbio == NULL) return(0);
  2012. s->bbio=bbio;
  2013. }
  2014. else
  2015. {
  2016. bbio=s->bbio;
  2017. if (s->bbio == s->wbio)
  2018. s->wbio=BIO_pop(s->wbio);
  2019. }
  2020. (void)BIO_reset(bbio);
  2021. /* if (!BIO_set_write_buffer_size(bbio,16*1024)) */
  2022. if (!BIO_set_read_buffer_size(bbio,1))
  2023. {
  2024. SSLerr(SSL_F_SSL_INIT_WBIO_BUFFER,ERR_R_BUF_LIB);
  2025. return(0);
  2026. }
  2027. if (push)
  2028. {
  2029. if (s->wbio != bbio)
  2030. s->wbio=BIO_push(bbio,s->wbio);
  2031. }
  2032. else
  2033. {
  2034. if (s->wbio == bbio)
  2035. s->wbio=BIO_pop(bbio);
  2036. }
  2037. return(1);
  2038. }
  2039. void ssl_free_wbio_buffer(SSL *s)
  2040. {
  2041. if (s->bbio == NULL) return;
  2042. if (s->bbio == s->wbio)
  2043. {
  2044. /* remove buffering */
  2045. s->wbio=BIO_pop(s->wbio);
  2046. #ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids adding one more preprocessor symbol */
  2047. assert(s->wbio != NULL);
  2048. #endif
  2049. }
  2050. BIO_free(s->bbio);
  2051. s->bbio=NULL;
  2052. }
  2053. void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx,int mode)
  2054. {
  2055. ctx->quiet_shutdown=mode;
  2056. }
  2057. int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
  2058. {
  2059. return(ctx->quiet_shutdown);
  2060. }
  2061. void SSL_set_quiet_shutdown(SSL *s,int mode)
  2062. {
  2063. s->quiet_shutdown=mode;
  2064. }
  2065. int SSL_get_quiet_shutdown(const SSL *s)
  2066. {
  2067. return(s->quiet_shutdown);
  2068. }
  2069. void SSL_set_shutdown(SSL *s,int mode)
  2070. {
  2071. s->shutdown=mode;
  2072. }
  2073. int SSL_get_shutdown(const SSL *s)
  2074. {
  2075. return(s->shutdown);
  2076. }
  2077. int SSL_version(const SSL *s)
  2078. {
  2079. return(s->version);
  2080. }
  2081. SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
  2082. {
  2083. return(ssl->ctx);
  2084. }
  2085. #ifndef OPENSSL_NO_STDIO
  2086. int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
  2087. {
  2088. return(X509_STORE_set_default_paths(ctx->cert_store));
  2089. }
  2090. int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
  2091. const char *CApath)
  2092. {
  2093. return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath));
  2094. }
  2095. #endif
  2096. void SSL_set_info_callback(SSL *ssl,
  2097. void (*cb)(const SSL *ssl,int type,int val))
  2098. {
  2099. ssl->info_callback=cb;
  2100. }
  2101. /* One compiler (Diab DCC) doesn't like argument names in returned
  2102. function pointer. */
  2103. void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/,int /*type*/,int /*val*/)
  2104. {
  2105. return ssl->info_callback;
  2106. }
  2107. int SSL_state(const SSL *ssl)
  2108. {
  2109. return(ssl->state);
  2110. }
  2111. void SSL_set_verify_result(SSL *ssl,long arg)
  2112. {
  2113. ssl->verify_result=arg;
  2114. }
  2115. long SSL_get_verify_result(const SSL *ssl)
  2116. {
  2117. return(ssl->verify_result);
  2118. }
  2119. int SSL_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
  2120. CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
  2121. {
  2122. return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp,
  2123. new_func, dup_func, free_func);
  2124. }
  2125. int SSL_set_ex_data(SSL *s,int idx,void *arg)
  2126. {
  2127. return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
  2128. }
  2129. void *SSL_get_ex_data(const SSL *s,int idx)
  2130. {
  2131. return(CRYPTO_get_ex_data(&s->ex_data,idx));
  2132. }
  2133. int SSL_CTX_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
  2134. CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
  2135. {
  2136. return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp,
  2137. new_func, dup_func, free_func);
  2138. }
  2139. int SSL_CTX_set_ex_data(SSL_CTX *s,int idx,void *arg)
  2140. {
  2141. return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
  2142. }
  2143. void *SSL_CTX_get_ex_data(const SSL_CTX *s,int idx)
  2144. {
  2145. return(CRYPTO_get_ex_data(&s->ex_data,idx));
  2146. }
  2147. int ssl_ok(SSL *s)
  2148. {
  2149. return(1);
  2150. }
  2151. X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
  2152. {
  2153. return(ctx->cert_store);
  2154. }
  2155. void SSL_CTX_set_cert_store(SSL_CTX *ctx,X509_STORE *store)
  2156. {
  2157. if (ctx->cert_store != NULL)
  2158. X509_STORE_free(ctx->cert_store);
  2159. ctx->cert_store=store;
  2160. }
  2161. int SSL_want(const SSL *s)
  2162. {
  2163. return(s->rwstate);
  2164. }
  2165. /*!
  2166. * \brief Set the callback for generating temporary RSA keys.
  2167. * \param ctx the SSL context.
  2168. * \param cb the callback
  2169. */
  2170. #ifndef OPENSSL_NO_RSA
  2171. void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl,
  2172. int is_export,
  2173. int keylength))
  2174. {
  2175. SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
  2176. }
  2177. void SSL_set_tmp_rsa_callback(SSL *ssl,RSA *(*cb)(SSL *ssl,
  2178. int is_export,
  2179. int keylength))
  2180. {
  2181. SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
  2182. }
  2183. #endif
  2184. #ifdef DOXYGEN
  2185. /*!
  2186. * \brief The RSA temporary key callback function.
  2187. * \param ssl the SSL session.
  2188. * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite.
  2189. * \param keylength if \c is_export is \c TRUE, then \c keylength is the size
  2190. * of the required key in bits.
  2191. * \return the temporary RSA key.
  2192. * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback
  2193. */
  2194. RSA *cb(SSL *ssl,int is_export,int keylength)
  2195. {}
  2196. #endif
  2197. /*!
  2198. * \brief Set the callback for generating temporary DH keys.
  2199. * \param ctx the SSL context.
  2200. * \param dh the callback
  2201. */
  2202. #ifndef OPENSSL_NO_DH
  2203. void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int is_export,
  2204. int keylength))
  2205. {
  2206. SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
  2207. }
  2208. void SSL_set_tmp_dh_callback(SSL *ssl,DH *(*dh)(SSL *ssl,int is_export,
  2209. int keylength))
  2210. {
  2211. SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
  2212. }
  2213. #endif
  2214. #ifndef OPENSSL_NO_ECDH
  2215. void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
  2216. int keylength))
  2217. {
  2218. SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
  2219. }
  2220. void SSL_set_tmp_ecdh_callback(SSL *ssl,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
  2221. int keylength))
  2222. {
  2223. SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
  2224. }
  2225. #endif
  2226. void SSL_CTX_set_msg_callback(SSL_CTX *ctx, void (*cb)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))
  2227. {
  2228. SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
  2229. }
  2230. void SSL_set_msg_callback(SSL *ssl, void (*cb)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))
  2231. {
  2232. SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
  2233. }
  2234. #if defined(_WINDLL) && defined(OPENSSL_SYS_WIN16)
  2235. #include "../crypto/bio/bss_file.c"
  2236. #endif
  2237. IMPLEMENT_STACK_OF(SSL_CIPHER)
  2238. IMPLEMENT_STACK_OF(SSL_COMP)