ssl_lib.c 46 KB

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