ssl_lib.c 55 KB

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