ssl_sess.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  1. /* ssl/ssl_sess.c */
  2. /* Copyright (C) 1995-1998 Eric Young ([email protected])
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young ([email protected]).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson ([email protected]).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young ([email protected])"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson ([email protected])"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. /* ====================================================================
  59. * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
  60. *
  61. * Redistribution and use in source and binary forms, with or without
  62. * modification, are permitted provided that the following conditions
  63. * are met:
  64. *
  65. * 1. Redistributions of source code must retain the above copyright
  66. * notice, this list of conditions and the following disclaimer.
  67. *
  68. * 2. Redistributions in binary form must reproduce the above copyright
  69. * notice, this list of conditions and the following disclaimer in
  70. * the documentation and/or other materials provided with the
  71. * distribution.
  72. *
  73. * 3. All advertising materials mentioning features or use of this
  74. * software must display the following acknowledgment:
  75. * "This product includes software developed by the OpenSSL Project
  76. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  77. *
  78. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  79. * endorse or promote products derived from this software without
  80. * prior written permission. For written permission, please contact
  81. * [email protected].
  82. *
  83. * 5. Products derived from this software may not be called "OpenSSL"
  84. * nor may "OpenSSL" appear in their names without prior written
  85. * permission of the OpenSSL Project.
  86. *
  87. * 6. Redistributions of any form whatsoever must retain the following
  88. * acknowledgment:
  89. * "This product includes software developed by the OpenSSL Project
  90. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  91. *
  92. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  93. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  94. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  95. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  96. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  97. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  98. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  99. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  100. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  101. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  102. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  103. * OF THE POSSIBILITY OF SUCH DAMAGE.
  104. * ====================================================================
  105. *
  106. * This product includes cryptographic software written by Eric Young
  107. * ([email protected]). This product includes software written by Tim
  108. * Hudson ([email protected]).
  109. *
  110. */
  111. /* ====================================================================
  112. * Copyright 2005 Nokia. All rights reserved.
  113. *
  114. * The portions of the attached software ("Contribution") is developed by
  115. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  116. * license.
  117. *
  118. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  119. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  120. * support (see RFC 4279) to OpenSSL.
  121. *
  122. * No patent licenses or other rights except those expressly stated in
  123. * the OpenSSL open source license shall be deemed granted or received
  124. * expressly, by implication, estoppel, or otherwise.
  125. *
  126. * No assurances are provided by Nokia that the Contribution does not
  127. * infringe the patent or other intellectual property rights of any third
  128. * party or that the license provides you with all the necessary rights
  129. * to make use of the Contribution.
  130. *
  131. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  132. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  133. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  134. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  135. * OTHERWISE.
  136. */
  137. #include <stdio.h>
  138. #include <openssl/lhash.h>
  139. #include <openssl/rand.h>
  140. #ifndef OPENSSL_NO_ENGINE
  141. #include <openssl/engine.h>
  142. #endif
  143. #include "ssl_locl.h"
  144. static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s);
  145. static void SSL_SESSION_list_add(SSL_CTX *ctx,SSL_SESSION *s);
  146. static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck);
  147. SSL_SESSION *SSL_get_session(const SSL *ssl)
  148. /* aka SSL_get0_session; gets 0 objects, just returns a copy of the pointer */
  149. {
  150. return(ssl->session);
  151. }
  152. SSL_SESSION *SSL_get1_session(SSL *ssl)
  153. /* variant of SSL_get_session: caller really gets something */
  154. {
  155. SSL_SESSION *sess;
  156. /* Need to lock this all up rather than just use CRYPTO_add so that
  157. * somebody doesn't free ssl->session between when we check it's
  158. * non-null and when we up the reference count. */
  159. CRYPTO_w_lock(CRYPTO_LOCK_SSL_SESSION);
  160. sess = ssl->session;
  161. if(sess)
  162. sess->references++;
  163. CRYPTO_w_unlock(CRYPTO_LOCK_SSL_SESSION);
  164. return(sess);
  165. }
  166. int SSL_SESSION_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
  167. CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
  168. {
  169. return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_SESSION, argl, argp,
  170. new_func, dup_func, free_func);
  171. }
  172. int SSL_SESSION_set_ex_data(SSL_SESSION *s, int idx, void *arg)
  173. {
  174. return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
  175. }
  176. void *SSL_SESSION_get_ex_data(const SSL_SESSION *s, int idx)
  177. {
  178. return(CRYPTO_get_ex_data(&s->ex_data,idx));
  179. }
  180. SSL_SESSION *SSL_SESSION_new(void)
  181. {
  182. SSL_SESSION *ss;
  183. ss=(SSL_SESSION *)OPENSSL_malloc(sizeof(SSL_SESSION));
  184. if (ss == NULL)
  185. {
  186. SSLerr(SSL_F_SSL_SESSION_NEW,ERR_R_MALLOC_FAILURE);
  187. return(0);
  188. }
  189. memset(ss,0,sizeof(SSL_SESSION));
  190. ss->verify_result = 1; /* avoid 0 (= X509_V_OK) just in case */
  191. ss->references=1;
  192. ss->timeout=60*5+4; /* 5 minute timeout by default */
  193. ss->time=(unsigned long)time(NULL);
  194. ss->prev=NULL;
  195. ss->next=NULL;
  196. ss->compress_meth=0;
  197. #ifndef OPENSSL_NO_TLSEXT
  198. ss->tlsext_hostname = NULL;
  199. #ifndef OPENSSL_NO_EC
  200. ss->tlsext_ecpointformatlist_length = 0;
  201. ss->tlsext_ecpointformatlist = NULL;
  202. ss->tlsext_ellipticcurvelist_length = 0;
  203. ss->tlsext_ellipticcurvelist = NULL;
  204. #endif
  205. #endif
  206. CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
  207. #ifndef OPENSSL_NO_PSK
  208. ss->psk_identity_hint=NULL;
  209. ss->psk_identity=NULL;
  210. #endif
  211. #ifndef OPENSSL_NO_SRP
  212. ss->srp_username=NULL;
  213. #endif
  214. return(ss);
  215. }
  216. const unsigned char *SSL_SESSION_get_id(const SSL_SESSION *s, unsigned int *len)
  217. {
  218. if(len)
  219. *len = s->session_id_length;
  220. return s->session_id;
  221. }
  222. unsigned int SSL_SESSION_get_compress_id(const SSL_SESSION *s)
  223. {
  224. return s->compress_meth;
  225. }
  226. /* Even with SSLv2, we have 16 bytes (128 bits) of session ID space. SSLv3/TLSv1
  227. * has 32 bytes (256 bits). As such, filling the ID with random gunk repeatedly
  228. * until we have no conflict is going to complete in one iteration pretty much
  229. * "most" of the time (btw: understatement). So, if it takes us 10 iterations
  230. * and we still can't avoid a conflict - well that's a reasonable point to call
  231. * it quits. Either the RAND code is broken or someone is trying to open roughly
  232. * very close to 2^128 (or 2^256) SSL sessions to our server. How you might
  233. * store that many sessions is perhaps a more interesting question ... */
  234. #define MAX_SESS_ID_ATTEMPTS 10
  235. static int def_generate_session_id(const SSL *ssl, unsigned char *id,
  236. unsigned int *id_len)
  237. {
  238. unsigned int retry = 0;
  239. do
  240. if (RAND_pseudo_bytes(id, *id_len) <= 0)
  241. return 0;
  242. while(SSL_has_matching_session_id(ssl, id, *id_len) &&
  243. (++retry < MAX_SESS_ID_ATTEMPTS));
  244. if(retry < MAX_SESS_ID_ATTEMPTS)
  245. return 1;
  246. /* else - woops a session_id match */
  247. /* XXX We should also check the external cache --
  248. * but the probability of a collision is negligible, and
  249. * we could not prevent the concurrent creation of sessions
  250. * with identical IDs since we currently don't have means
  251. * to atomically check whether a session ID already exists
  252. * and make a reservation for it if it does not
  253. * (this problem applies to the internal cache as well).
  254. */
  255. return 0;
  256. }
  257. int ssl_get_new_session(SSL *s, int session)
  258. {
  259. /* This gets used by clients and servers. */
  260. unsigned int tmp;
  261. SSL_SESSION *ss=NULL;
  262. GEN_SESSION_CB cb = def_generate_session_id;
  263. if ((ss=SSL_SESSION_new()) == NULL) return(0);
  264. /* If the context has a default timeout, use it */
  265. if (s->session_ctx->session_timeout == 0)
  266. ss->timeout=SSL_get_default_timeout(s);
  267. else
  268. ss->timeout=s->session_ctx->session_timeout;
  269. if (s->session != NULL)
  270. {
  271. SSL_SESSION_free(s->session);
  272. s->session=NULL;
  273. }
  274. if (session)
  275. {
  276. if (s->version == SSL2_VERSION)
  277. {
  278. ss->ssl_version=SSL2_VERSION;
  279. ss->session_id_length=SSL2_SSL_SESSION_ID_LENGTH;
  280. }
  281. else if (s->version == SSL3_VERSION)
  282. {
  283. ss->ssl_version=SSL3_VERSION;
  284. ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
  285. }
  286. else if (s->version == TLS1_VERSION)
  287. {
  288. ss->ssl_version=TLS1_VERSION;
  289. ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
  290. }
  291. else if (s->version == TLS1_1_VERSION)
  292. {
  293. ss->ssl_version=TLS1_1_VERSION;
  294. ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
  295. }
  296. else if (s->version == TLS1_2_VERSION)
  297. {
  298. ss->ssl_version=TLS1_2_VERSION;
  299. ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
  300. }
  301. else if (s->version == DTLS1_BAD_VER)
  302. {
  303. ss->ssl_version=DTLS1_BAD_VER;
  304. ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
  305. }
  306. else if (s->version == DTLS1_VERSION)
  307. {
  308. ss->ssl_version=DTLS1_VERSION;
  309. ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
  310. }
  311. else
  312. {
  313. SSLerr(SSL_F_SSL_GET_NEW_SESSION,SSL_R_UNSUPPORTED_SSL_VERSION);
  314. SSL_SESSION_free(ss);
  315. return(0);
  316. }
  317. #ifndef OPENSSL_NO_TLSEXT
  318. /* If RFC4507 ticket use empty session ID */
  319. if (s->tlsext_ticket_expected)
  320. {
  321. ss->session_id_length = 0;
  322. goto sess_id_done;
  323. }
  324. #endif
  325. /* Choose which callback will set the session ID */
  326. CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
  327. if(s->generate_session_id)
  328. cb = s->generate_session_id;
  329. else if(s->session_ctx->generate_session_id)
  330. cb = s->session_ctx->generate_session_id;
  331. CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
  332. /* Choose a session ID */
  333. tmp = ss->session_id_length;
  334. if(!cb(s, ss->session_id, &tmp))
  335. {
  336. /* The callback failed */
  337. SSLerr(SSL_F_SSL_GET_NEW_SESSION,
  338. SSL_R_SSL_SESSION_ID_CALLBACK_FAILED);
  339. SSL_SESSION_free(ss);
  340. return(0);
  341. }
  342. /* Don't allow the callback to set the session length to zero.
  343. * nor set it higher than it was. */
  344. if(!tmp || (tmp > ss->session_id_length))
  345. {
  346. /* The callback set an illegal length */
  347. SSLerr(SSL_F_SSL_GET_NEW_SESSION,
  348. SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH);
  349. SSL_SESSION_free(ss);
  350. return(0);
  351. }
  352. /* If the session length was shrunk and we're SSLv2, pad it */
  353. if((tmp < ss->session_id_length) && (s->version == SSL2_VERSION))
  354. memset(ss->session_id + tmp, 0, ss->session_id_length - tmp);
  355. else
  356. ss->session_id_length = tmp;
  357. /* Finally, check for a conflict */
  358. if(SSL_has_matching_session_id(s, ss->session_id,
  359. ss->session_id_length))
  360. {
  361. SSLerr(SSL_F_SSL_GET_NEW_SESSION,
  362. SSL_R_SSL_SESSION_ID_CONFLICT);
  363. SSL_SESSION_free(ss);
  364. return(0);
  365. }
  366. #ifndef OPENSSL_NO_TLSEXT
  367. sess_id_done:
  368. if (s->tlsext_hostname) {
  369. ss->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
  370. if (ss->tlsext_hostname == NULL) {
  371. SSLerr(SSL_F_SSL_GET_NEW_SESSION, ERR_R_INTERNAL_ERROR);
  372. SSL_SESSION_free(ss);
  373. return 0;
  374. }
  375. }
  376. #ifndef OPENSSL_NO_EC
  377. if (s->tlsext_ecpointformatlist)
  378. {
  379. if (ss->tlsext_ecpointformatlist != NULL) OPENSSL_free(ss->tlsext_ecpointformatlist);
  380. if ((ss->tlsext_ecpointformatlist = OPENSSL_malloc(s->tlsext_ecpointformatlist_length)) == NULL)
  381. {
  382. SSLerr(SSL_F_SSL_GET_NEW_SESSION, ERR_R_MALLOC_FAILURE);
  383. SSL_SESSION_free(ss);
  384. return 0;
  385. }
  386. ss->tlsext_ecpointformatlist_length = s->tlsext_ecpointformatlist_length;
  387. memcpy(ss->tlsext_ecpointformatlist, s->tlsext_ecpointformatlist, s->tlsext_ecpointformatlist_length);
  388. }
  389. if (s->tlsext_ellipticcurvelist)
  390. {
  391. if (ss->tlsext_ellipticcurvelist != NULL) OPENSSL_free(ss->tlsext_ellipticcurvelist);
  392. if ((ss->tlsext_ellipticcurvelist = OPENSSL_malloc(s->tlsext_ellipticcurvelist_length)) == NULL)
  393. {
  394. SSLerr(SSL_F_SSL_GET_NEW_SESSION, ERR_R_MALLOC_FAILURE);
  395. SSL_SESSION_free(ss);
  396. return 0;
  397. }
  398. ss->tlsext_ellipticcurvelist_length = s->tlsext_ellipticcurvelist_length;
  399. memcpy(ss->tlsext_ellipticcurvelist, s->tlsext_ellipticcurvelist, s->tlsext_ellipticcurvelist_length);
  400. }
  401. #endif
  402. #endif
  403. }
  404. else
  405. {
  406. ss->session_id_length=0;
  407. }
  408. if (s->sid_ctx_length > sizeof ss->sid_ctx)
  409. {
  410. SSLerr(SSL_F_SSL_GET_NEW_SESSION, ERR_R_INTERNAL_ERROR);
  411. SSL_SESSION_free(ss);
  412. return 0;
  413. }
  414. memcpy(ss->sid_ctx,s->sid_ctx,s->sid_ctx_length);
  415. ss->sid_ctx_length=s->sid_ctx_length;
  416. s->session=ss;
  417. ss->ssl_version=s->version;
  418. ss->verify_result = X509_V_OK;
  419. return(1);
  420. }
  421. /* ssl_get_prev attempts to find an SSL_SESSION to be used to resume this
  422. * connection. It is only called by servers.
  423. *
  424. * session_id: points at the session ID in the ClientHello. This code will
  425. * read past the end of this in order to parse out the session ticket
  426. * extension, if any.
  427. * len: the length of the session ID.
  428. * limit: a pointer to the first byte after the ClientHello.
  429. *
  430. * Returns:
  431. * -1: error
  432. * 0: a session may have been found.
  433. *
  434. * Side effects:
  435. * - If a session is found then s->session is pointed at it (after freeing an
  436. * existing session if need be) and s->verify_result is set from the session.
  437. * - Both for new and resumed sessions, s->tlsext_ticket_expected is set to 1
  438. * if the server should issue a new session ticket (to 0 otherwise).
  439. */
  440. int ssl_get_prev_session(SSL *s, unsigned char *session_id, int len,
  441. const unsigned char *limit)
  442. {
  443. /* This is used only by servers. */
  444. SSL_SESSION *ret=NULL;
  445. int fatal = 0;
  446. int try_session_cache = 1;
  447. #ifndef OPENSSL_NO_TLSEXT
  448. int r;
  449. #endif
  450. if (len > SSL_MAX_SSL_SESSION_ID_LENGTH)
  451. goto err;
  452. if (len == 0)
  453. try_session_cache = 0;
  454. #ifndef OPENSSL_NO_TLSEXT
  455. r = tls1_process_ticket(s, session_id, len, limit, &ret); /* sets s->tlsext_ticket_expected */
  456. switch (r)
  457. {
  458. case -1: /* Error during processing */
  459. fatal = 1;
  460. goto err;
  461. case 0: /* No ticket found */
  462. case 1: /* Zero length ticket found */
  463. break; /* Ok to carry on processing session id. */
  464. case 2: /* Ticket found but not decrypted. */
  465. case 3: /* Ticket decrypted, *ret has been set. */
  466. try_session_cache = 0;
  467. break;
  468. default:
  469. abort();
  470. }
  471. #endif
  472. if (try_session_cache &&
  473. ret == NULL &&
  474. !(s->session_ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_LOOKUP))
  475. {
  476. SSL_SESSION data;
  477. data.ssl_version=s->version;
  478. data.session_id_length=len;
  479. if (len == 0)
  480. return 0;
  481. memcpy(data.session_id,session_id,len);
  482. CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
  483. ret=lh_SSL_SESSION_retrieve(s->session_ctx->sessions,&data);
  484. if (ret != NULL)
  485. {
  486. /* don't allow other threads to steal it: */
  487. CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);
  488. }
  489. CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
  490. if (ret == NULL)
  491. s->session_ctx->stats.sess_miss++;
  492. }
  493. if (try_session_cache &&
  494. ret == NULL &&
  495. s->session_ctx->get_session_cb != NULL)
  496. {
  497. int copy=1;
  498. if ((ret=s->session_ctx->get_session_cb(s,session_id,len,&copy)))
  499. {
  500. s->session_ctx->stats.sess_cb_hit++;
  501. /* Increment reference count now if the session callback
  502. * asks us to do so (note that if the session structures
  503. * returned by the callback are shared between threads,
  504. * it must handle the reference count itself [i.e. copy == 0],
  505. * or things won't be thread-safe). */
  506. if (copy)
  507. CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);
  508. /* Add the externally cached session to the internal
  509. * cache as well if and only if we are supposed to. */
  510. if(!(s->session_ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE))
  511. /* The following should not return 1, otherwise,
  512. * things are very strange */
  513. SSL_CTX_add_session(s->session_ctx,ret);
  514. }
  515. }
  516. if (ret == NULL)
  517. goto err;
  518. /* Now ret is non-NULL and we own one of its reference counts. */
  519. if (ret->sid_ctx_length != s->sid_ctx_length
  520. || memcmp(ret->sid_ctx,s->sid_ctx,ret->sid_ctx_length))
  521. {
  522. /* We have the session requested by the client, but we don't
  523. * want to use it in this context. */
  524. goto err; /* treat like cache miss */
  525. }
  526. if((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0)
  527. {
  528. /* We can't be sure if this session is being used out of
  529. * context, which is especially important for SSL_VERIFY_PEER.
  530. * The application should have used SSL[_CTX]_set_session_id_context.
  531. *
  532. * For this error case, we generate an error instead of treating
  533. * the event like a cache miss (otherwise it would be easy for
  534. * applications to effectively disable the session cache by
  535. * accident without anyone noticing).
  536. */
  537. SSLerr(SSL_F_SSL_GET_PREV_SESSION,SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
  538. fatal = 1;
  539. goto err;
  540. }
  541. if (ret->cipher == NULL)
  542. {
  543. unsigned char buf[5],*p;
  544. unsigned long l;
  545. p=buf;
  546. l=ret->cipher_id;
  547. l2n(l,p);
  548. if ((ret->ssl_version>>8) >= SSL3_VERSION_MAJOR)
  549. ret->cipher=ssl_get_cipher_by_char(s,&(buf[2]));
  550. else
  551. ret->cipher=ssl_get_cipher_by_char(s,&(buf[1]));
  552. if (ret->cipher == NULL)
  553. goto err;
  554. }
  555. if (ret->timeout < (long)(time(NULL) - ret->time)) /* timeout */
  556. {
  557. s->session_ctx->stats.sess_timeout++;
  558. if (try_session_cache)
  559. {
  560. /* session was from the cache, so remove it */
  561. SSL_CTX_remove_session(s->session_ctx,ret);
  562. }
  563. goto err;
  564. }
  565. s->session_ctx->stats.sess_hit++;
  566. if (s->session != NULL)
  567. SSL_SESSION_free(s->session);
  568. s->session=ret;
  569. s->verify_result = s->session->verify_result;
  570. return 1;
  571. err:
  572. if (ret != NULL)
  573. {
  574. SSL_SESSION_free(ret);
  575. #ifndef OPENSSL_NO_TLSEXT
  576. if (!try_session_cache)
  577. {
  578. /* The session was from a ticket, so we should
  579. * issue a ticket for the new session */
  580. s->tlsext_ticket_expected = 1;
  581. }
  582. #endif
  583. }
  584. if (fatal)
  585. return -1;
  586. else
  587. return 0;
  588. }
  589. int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c)
  590. {
  591. int ret=0;
  592. SSL_SESSION *s;
  593. /* add just 1 reference count for the SSL_CTX's session cache
  594. * even though it has two ways of access: each session is in a
  595. * doubly linked list and an lhash */
  596. CRYPTO_add(&c->references,1,CRYPTO_LOCK_SSL_SESSION);
  597. /* if session c is in already in cache, we take back the increment later */
  598. CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
  599. s=lh_SSL_SESSION_insert(ctx->sessions,c);
  600. /* s != NULL iff we already had a session with the given PID.
  601. * In this case, s == c should hold (then we did not really modify
  602. * ctx->sessions), or we're in trouble. */
  603. if (s != NULL && s != c)
  604. {
  605. /* We *are* in trouble ... */
  606. SSL_SESSION_list_remove(ctx,s);
  607. SSL_SESSION_free(s);
  608. /* ... so pretend the other session did not exist in cache
  609. * (we cannot handle two SSL_SESSION structures with identical
  610. * session ID in the same cache, which could happen e.g. when
  611. * two threads concurrently obtain the same session from an external
  612. * cache) */
  613. s = NULL;
  614. }
  615. /* Put at the head of the queue unless it is already in the cache */
  616. if (s == NULL)
  617. SSL_SESSION_list_add(ctx,c);
  618. if (s != NULL)
  619. {
  620. /* existing cache entry -- decrement previously incremented reference
  621. * count because it already takes into account the cache */
  622. SSL_SESSION_free(s); /* s == c */
  623. ret=0;
  624. }
  625. else
  626. {
  627. /* new cache entry -- remove old ones if cache has become too large */
  628. ret=1;
  629. if (SSL_CTX_sess_get_cache_size(ctx) > 0)
  630. {
  631. while (SSL_CTX_sess_number(ctx) >
  632. SSL_CTX_sess_get_cache_size(ctx))
  633. {
  634. if (!remove_session_lock(ctx,
  635. ctx->session_cache_tail, 0))
  636. break;
  637. else
  638. ctx->stats.sess_cache_full++;
  639. }
  640. }
  641. }
  642. CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
  643. return(ret);
  644. }
  645. int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)
  646. {
  647. return remove_session_lock(ctx, c, 1);
  648. }
  649. static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)
  650. {
  651. SSL_SESSION *r;
  652. int ret=0;
  653. if ((c != NULL) && (c->session_id_length != 0))
  654. {
  655. if(lck) CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
  656. if ((r = lh_SSL_SESSION_retrieve(ctx->sessions,c)) == c)
  657. {
  658. ret=1;
  659. r=lh_SSL_SESSION_delete(ctx->sessions,c);
  660. SSL_SESSION_list_remove(ctx,c);
  661. }
  662. if(lck) CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
  663. if (ret)
  664. {
  665. r->not_resumable=1;
  666. if (ctx->remove_session_cb != NULL)
  667. ctx->remove_session_cb(ctx,r);
  668. SSL_SESSION_free(r);
  669. }
  670. }
  671. else
  672. ret=0;
  673. return(ret);
  674. }
  675. void SSL_SESSION_free(SSL_SESSION *ss)
  676. {
  677. int i;
  678. if(ss == NULL)
  679. return;
  680. i=CRYPTO_add(&ss->references,-1,CRYPTO_LOCK_SSL_SESSION);
  681. #ifdef REF_PRINT
  682. REF_PRINT("SSL_SESSION",ss);
  683. #endif
  684. if (i > 0) return;
  685. #ifdef REF_CHECK
  686. if (i < 0)
  687. {
  688. fprintf(stderr,"SSL_SESSION_free, bad reference count\n");
  689. abort(); /* ok */
  690. }
  691. #endif
  692. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
  693. OPENSSL_cleanse(ss->key_arg,sizeof ss->key_arg);
  694. OPENSSL_cleanse(ss->master_key,sizeof ss->master_key);
  695. OPENSSL_cleanse(ss->session_id,sizeof ss->session_id);
  696. if (ss->sess_cert != NULL) ssl_sess_cert_free(ss->sess_cert);
  697. if (ss->peer != NULL) X509_free(ss->peer);
  698. if (ss->ciphers != NULL) sk_SSL_CIPHER_free(ss->ciphers);
  699. #ifndef OPENSSL_NO_TLSEXT
  700. if (ss->tlsext_hostname != NULL) OPENSSL_free(ss->tlsext_hostname);
  701. if (ss->tlsext_tick != NULL) OPENSSL_free(ss->tlsext_tick);
  702. #ifndef OPENSSL_NO_EC
  703. ss->tlsext_ecpointformatlist_length = 0;
  704. if (ss->tlsext_ecpointformatlist != NULL) OPENSSL_free(ss->tlsext_ecpointformatlist);
  705. ss->tlsext_ellipticcurvelist_length = 0;
  706. if (ss->tlsext_ellipticcurvelist != NULL) OPENSSL_free(ss->tlsext_ellipticcurvelist);
  707. #endif /* OPENSSL_NO_EC */
  708. #endif
  709. #ifndef OPENSSL_NO_PSK
  710. if (ss->psk_identity_hint != NULL)
  711. OPENSSL_free(ss->psk_identity_hint);
  712. if (ss->psk_identity != NULL)
  713. OPENSSL_free(ss->psk_identity);
  714. #endif
  715. #ifndef OPENSSL_NO_SRP
  716. if (ss->srp_username != NULL)
  717. OPENSSL_free(ss->srp_username);
  718. #endif
  719. OPENSSL_cleanse(ss,sizeof(*ss));
  720. OPENSSL_free(ss);
  721. }
  722. int SSL_set_session(SSL *s, SSL_SESSION *session)
  723. {
  724. int ret=0;
  725. const SSL_METHOD *meth;
  726. if (session != NULL)
  727. {
  728. meth=s->ctx->method->get_ssl_method(session->ssl_version);
  729. if (meth == NULL)
  730. meth=s->method->get_ssl_method(session->ssl_version);
  731. if (meth == NULL)
  732. {
  733. SSLerr(SSL_F_SSL_SET_SESSION,SSL_R_UNABLE_TO_FIND_SSL_METHOD);
  734. return(0);
  735. }
  736. if (meth != s->method)
  737. {
  738. if (!SSL_set_ssl_method(s,meth))
  739. return(0);
  740. }
  741. #ifndef OPENSSL_NO_KRB5
  742. if (s->kssl_ctx && !s->kssl_ctx->client_princ &&
  743. session->krb5_client_princ_len > 0)
  744. {
  745. s->kssl_ctx->client_princ = (char *)OPENSSL_malloc(session->krb5_client_princ_len + 1);
  746. memcpy(s->kssl_ctx->client_princ,session->krb5_client_princ,
  747. session->krb5_client_princ_len);
  748. s->kssl_ctx->client_princ[session->krb5_client_princ_len] = '\0';
  749. }
  750. #endif /* OPENSSL_NO_KRB5 */
  751. /* CRYPTO_w_lock(CRYPTO_LOCK_SSL);*/
  752. CRYPTO_add(&session->references,1,CRYPTO_LOCK_SSL_SESSION);
  753. if (s->session != NULL)
  754. SSL_SESSION_free(s->session);
  755. s->session=session;
  756. s->verify_result = s->session->verify_result;
  757. /* CRYPTO_w_unlock(CRYPTO_LOCK_SSL);*/
  758. ret=1;
  759. }
  760. else
  761. {
  762. if (s->session != NULL)
  763. {
  764. SSL_SESSION_free(s->session);
  765. s->session=NULL;
  766. }
  767. meth=s->ctx->method;
  768. if (meth != s->method)
  769. {
  770. if (!SSL_set_ssl_method(s,meth))
  771. return(0);
  772. }
  773. ret=1;
  774. }
  775. return(ret);
  776. }
  777. long SSL_SESSION_set_timeout(SSL_SESSION *s, long t)
  778. {
  779. if (s == NULL) return(0);
  780. s->timeout=t;
  781. return(1);
  782. }
  783. long SSL_SESSION_get_timeout(const SSL_SESSION *s)
  784. {
  785. if (s == NULL) return(0);
  786. return(s->timeout);
  787. }
  788. long SSL_SESSION_get_time(const SSL_SESSION *s)
  789. {
  790. if (s == NULL) return(0);
  791. return(s->time);
  792. }
  793. long SSL_SESSION_set_time(SSL_SESSION *s, long t)
  794. {
  795. if (s == NULL) return(0);
  796. s->time=t;
  797. return(t);
  798. }
  799. X509 *SSL_SESSION_get0_peer(SSL_SESSION *s)
  800. {
  801. return s->peer;
  802. }
  803. int SSL_SESSION_set1_id_context(SSL_SESSION *s,const unsigned char *sid_ctx,
  804. unsigned int sid_ctx_len)
  805. {
  806. if(sid_ctx_len > SSL_MAX_SID_CTX_LENGTH)
  807. {
  808. SSLerr(SSL_F_SSL_SESSION_SET1_ID_CONTEXT,SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  809. return 0;
  810. }
  811. s->sid_ctx_length=sid_ctx_len;
  812. memcpy(s->sid_ctx,sid_ctx,sid_ctx_len);
  813. return 1;
  814. }
  815. long SSL_CTX_set_timeout(SSL_CTX *s, long t)
  816. {
  817. long l;
  818. if (s == NULL) return(0);
  819. l=s->session_timeout;
  820. s->session_timeout=t;
  821. return(l);
  822. }
  823. long SSL_CTX_get_timeout(const SSL_CTX *s)
  824. {
  825. if (s == NULL) return(0);
  826. return(s->session_timeout);
  827. }
  828. #ifndef OPENSSL_NO_TLSEXT
  829. int SSL_set_session_secret_cb(SSL *s, int (*tls_session_secret_cb)(SSL *s, void *secret, int *secret_len,
  830. STACK_OF(SSL_CIPHER) *peer_ciphers, SSL_CIPHER **cipher, void *arg), void *arg)
  831. {
  832. if (s == NULL) return(0);
  833. s->tls_session_secret_cb = tls_session_secret_cb;
  834. s->tls_session_secret_cb_arg = arg;
  835. return(1);
  836. }
  837. int SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb,
  838. void *arg)
  839. {
  840. if (s == NULL) return(0);
  841. s->tls_session_ticket_ext_cb = cb;
  842. s->tls_session_ticket_ext_cb_arg = arg;
  843. return(1);
  844. }
  845. int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len)
  846. {
  847. if (s->version >= TLS1_VERSION)
  848. {
  849. if (s->tlsext_session_ticket)
  850. {
  851. OPENSSL_free(s->tlsext_session_ticket);
  852. s->tlsext_session_ticket = NULL;
  853. }
  854. s->tlsext_session_ticket = OPENSSL_malloc(sizeof(TLS_SESSION_TICKET_EXT) + ext_len);
  855. if (!s->tlsext_session_ticket)
  856. {
  857. SSLerr(SSL_F_SSL_SET_SESSION_TICKET_EXT, ERR_R_MALLOC_FAILURE);
  858. return 0;
  859. }
  860. if (ext_data)
  861. {
  862. s->tlsext_session_ticket->length = ext_len;
  863. s->tlsext_session_ticket->data = s->tlsext_session_ticket + 1;
  864. memcpy(s->tlsext_session_ticket->data, ext_data, ext_len);
  865. }
  866. else
  867. {
  868. s->tlsext_session_ticket->length = 0;
  869. s->tlsext_session_ticket->data = NULL;
  870. }
  871. return 1;
  872. }
  873. return 0;
  874. }
  875. #endif /* OPENSSL_NO_TLSEXT */
  876. typedef struct timeout_param_st
  877. {
  878. SSL_CTX *ctx;
  879. long time;
  880. LHASH_OF(SSL_SESSION) *cache;
  881. } TIMEOUT_PARAM;
  882. static void timeout_doall_arg(SSL_SESSION *s, TIMEOUT_PARAM *p)
  883. {
  884. if ((p->time == 0) || (p->time > (s->time+s->timeout))) /* timeout */
  885. {
  886. /* The reason we don't call SSL_CTX_remove_session() is to
  887. * save on locking overhead */
  888. (void)lh_SSL_SESSION_delete(p->cache,s);
  889. SSL_SESSION_list_remove(p->ctx,s);
  890. s->not_resumable=1;
  891. if (p->ctx->remove_session_cb != NULL)
  892. p->ctx->remove_session_cb(p->ctx,s);
  893. SSL_SESSION_free(s);
  894. }
  895. }
  896. static IMPLEMENT_LHASH_DOALL_ARG_FN(timeout, SSL_SESSION, TIMEOUT_PARAM)
  897. void SSL_CTX_flush_sessions(SSL_CTX *s, long t)
  898. {
  899. unsigned long i;
  900. TIMEOUT_PARAM tp;
  901. tp.ctx=s;
  902. tp.cache=s->sessions;
  903. if (tp.cache == NULL) return;
  904. tp.time=t;
  905. CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
  906. i=CHECKED_LHASH_OF(SSL_SESSION, tp.cache)->down_load;
  907. CHECKED_LHASH_OF(SSL_SESSION, tp.cache)->down_load=0;
  908. lh_SSL_SESSION_doall_arg(tp.cache, LHASH_DOALL_ARG_FN(timeout),
  909. TIMEOUT_PARAM, &tp);
  910. CHECKED_LHASH_OF(SSL_SESSION, tp.cache)->down_load=i;
  911. CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
  912. }
  913. int ssl_clear_bad_session(SSL *s)
  914. {
  915. if ( (s->session != NULL) &&
  916. !(s->shutdown & SSL_SENT_SHUTDOWN) &&
  917. !(SSL_in_init(s) || SSL_in_before(s)))
  918. {
  919. SSL_CTX_remove_session(s->ctx,s->session);
  920. return(1);
  921. }
  922. else
  923. return(0);
  924. }
  925. /* locked by SSL_CTX in the calling function */
  926. static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s)
  927. {
  928. if ((s->next == NULL) || (s->prev == NULL)) return;
  929. if (s->next == (SSL_SESSION *)&(ctx->session_cache_tail))
  930. { /* last element in list */
  931. if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head))
  932. { /* only one element in list */
  933. ctx->session_cache_head=NULL;
  934. ctx->session_cache_tail=NULL;
  935. }
  936. else
  937. {
  938. ctx->session_cache_tail=s->prev;
  939. s->prev->next=(SSL_SESSION *)&(ctx->session_cache_tail);
  940. }
  941. }
  942. else
  943. {
  944. if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head))
  945. { /* first element in list */
  946. ctx->session_cache_head=s->next;
  947. s->next->prev=(SSL_SESSION *)&(ctx->session_cache_head);
  948. }
  949. else
  950. { /* middle of list */
  951. s->next->prev=s->prev;
  952. s->prev->next=s->next;
  953. }
  954. }
  955. s->prev=s->next=NULL;
  956. }
  957. static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s)
  958. {
  959. if ((s->next != NULL) && (s->prev != NULL))
  960. SSL_SESSION_list_remove(ctx,s);
  961. if (ctx->session_cache_head == NULL)
  962. {
  963. ctx->session_cache_head=s;
  964. ctx->session_cache_tail=s;
  965. s->prev=(SSL_SESSION *)&(ctx->session_cache_head);
  966. s->next=(SSL_SESSION *)&(ctx->session_cache_tail);
  967. }
  968. else
  969. {
  970. s->next=ctx->session_cache_head;
  971. s->next->prev=s;
  972. s->prev=(SSL_SESSION *)&(ctx->session_cache_head);
  973. ctx->session_cache_head=s;
  974. }
  975. }
  976. void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
  977. int (*cb)(struct ssl_st *ssl,SSL_SESSION *sess))
  978. {
  979. ctx->new_session_cb=cb;
  980. }
  981. int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx))(SSL *ssl, SSL_SESSION *sess)
  982. {
  983. return ctx->new_session_cb;
  984. }
  985. void SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx,
  986. void (*cb)(SSL_CTX *ctx,SSL_SESSION *sess))
  987. {
  988. ctx->remove_session_cb=cb;
  989. }
  990. void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx))(SSL_CTX * ctx,SSL_SESSION *sess)
  991. {
  992. return ctx->remove_session_cb;
  993. }
  994. void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx,
  995. SSL_SESSION *(*cb)(struct ssl_st *ssl,
  996. unsigned char *data,int len,int *copy))
  997. {
  998. ctx->get_session_cb=cb;
  999. }
  1000. SSL_SESSION * (*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx))(SSL *ssl,
  1001. unsigned char *data,int len,int *copy)
  1002. {
  1003. return ctx->get_session_cb;
  1004. }
  1005. void SSL_CTX_set_info_callback(SSL_CTX *ctx,
  1006. void (*cb)(const SSL *ssl,int type,int val))
  1007. {
  1008. ctx->info_callback=cb;
  1009. }
  1010. void (*SSL_CTX_get_info_callback(SSL_CTX *ctx))(const SSL *ssl,int type,int val)
  1011. {
  1012. return ctx->info_callback;
  1013. }
  1014. void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx,
  1015. int (*cb)(SSL *ssl, X509 **x509, EVP_PKEY **pkey))
  1016. {
  1017. ctx->client_cert_cb=cb;
  1018. }
  1019. int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx))(SSL * ssl, X509 ** x509 , EVP_PKEY **pkey)
  1020. {
  1021. return ctx->client_cert_cb;
  1022. }
  1023. #ifndef OPENSSL_NO_ENGINE
  1024. int SSL_CTX_set_client_cert_engine(SSL_CTX *ctx, ENGINE *e)
  1025. {
  1026. if (!ENGINE_init(e))
  1027. {
  1028. SSLerr(SSL_F_SSL_CTX_SET_CLIENT_CERT_ENGINE, ERR_R_ENGINE_LIB);
  1029. return 0;
  1030. }
  1031. if(!ENGINE_get_ssl_client_cert_function(e))
  1032. {
  1033. SSLerr(SSL_F_SSL_CTX_SET_CLIENT_CERT_ENGINE, SSL_R_NO_CLIENT_CERT_METHOD);
  1034. ENGINE_finish(e);
  1035. return 0;
  1036. }
  1037. ctx->client_cert_engine = e;
  1038. return 1;
  1039. }
  1040. #endif
  1041. void SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx,
  1042. int (*cb)(SSL *ssl, unsigned char *cookie, unsigned int *cookie_len))
  1043. {
  1044. ctx->app_gen_cookie_cb=cb;
  1045. }
  1046. void SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx,
  1047. int (*cb)(SSL *ssl, unsigned char *cookie, unsigned int cookie_len))
  1048. {
  1049. ctx->app_verify_cookie_cb=cb;
  1050. }
  1051. IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, SSL_SESSION)