ssltest.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590
  1. /* ssl/ssltest.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  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 (tjh@cryptsoft.com).
  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 (eay@cryptsoft.com)"
  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 (tjh@cryptsoft.com)"
  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-2000 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. * openssl-core@openssl.org.
  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. * (eay@cryptsoft.com). This product includes software written by Tim
  108. * Hudson (tjh@cryptsoft.com).
  109. *
  110. */
  111. /* ====================================================================
  112. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  113. * ECC cipher suite support in OpenSSL originally developed by
  114. * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
  115. */
  116. /* ====================================================================
  117. * Copyright 2005 Nokia. All rights reserved.
  118. *
  119. * The portions of the attached software ("Contribution") is developed by
  120. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  121. * license.
  122. *
  123. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  124. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  125. * support (see RFC 4279) to OpenSSL.
  126. *
  127. * No patent licenses or other rights except those expressly stated in
  128. * the OpenSSL open source license shall be deemed granted or received
  129. * expressly, by implication, estoppel, or otherwise.
  130. *
  131. * No assurances are provided by Nokia that the Contribution does not
  132. * infringe the patent or other intellectual property rights of any third
  133. * party or that the license provides you with all the necessary rights
  134. * to make use of the Contribution.
  135. *
  136. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  137. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  138. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  139. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  140. * OTHERWISE.
  141. */
  142. #define _BSD_SOURCE 1 /* Or gethostname won't be declared properly
  143. on Linux and GNU platforms. */
  144. #include <assert.h>
  145. #include <errno.h>
  146. #include <limits.h>
  147. #include <stdio.h>
  148. #include <stdlib.h>
  149. #include <string.h>
  150. #include <time.h>
  151. #define USE_SOCKETS
  152. #include "e_os.h"
  153. #ifdef OPENSSL_SYS_VMS
  154. #define _XOPEN_SOURCE 500 /* Or isascii won't be declared properly on
  155. VMS (at least with DECompHP C). */
  156. #endif
  157. #include <ctype.h>
  158. #include <openssl/bio.h>
  159. #include <openssl/crypto.h>
  160. #include <openssl/evp.h>
  161. #include <openssl/x509.h>
  162. #include <openssl/x509v3.h>
  163. #include <openssl/ssl.h>
  164. #ifndef OPENSSL_NO_ENGINE
  165. #include <openssl/engine.h>
  166. #endif
  167. #include <openssl/err.h>
  168. #include <openssl/rand.h>
  169. #ifndef OPENSSL_NO_RSA
  170. #include <openssl/rsa.h>
  171. #endif
  172. #ifndef OPENSSL_NO_DSA
  173. #include <openssl/dsa.h>
  174. #endif
  175. #ifndef OPENSSL_NO_DH
  176. #include <openssl/dh.h>
  177. #endif
  178. #include <openssl/bn.h>
  179. #define _XOPEN_SOURCE_EXTENDED 1 /* Or gethostname won't be declared properly
  180. on Compaq platforms (at least with DEC C).
  181. Do not try to put it earlier, or IPv6 includes
  182. get screwed...
  183. */
  184. #ifdef OPENSSL_SYS_WINDOWS
  185. #include <winsock.h>
  186. #else
  187. #include OPENSSL_UNISTD
  188. #endif
  189. #ifdef OPENSSL_SYS_VMS
  190. # define TEST_SERVER_CERT "SYS$DISK:[-.APPS]SERVER.PEM"
  191. # define TEST_CLIENT_CERT "SYS$DISK:[-.APPS]CLIENT.PEM"
  192. #elif defined(OPENSSL_SYS_WINCE)
  193. # define TEST_SERVER_CERT "\\OpenSSL\\server.pem"
  194. # define TEST_CLIENT_CERT "\\OpenSSL\\client.pem"
  195. #elif defined(OPENSSL_SYS_NETWARE)
  196. # define TEST_SERVER_CERT "\\openssl\\apps\\server.pem"
  197. # define TEST_CLIENT_CERT "\\openssl\\apps\\client.pem"
  198. #else
  199. # define TEST_SERVER_CERT "../apps/server.pem"
  200. # define TEST_CLIENT_CERT "../apps/client.pem"
  201. #endif
  202. /* There is really no standard for this, so let's assign some tentative
  203. numbers. In any case, these numbers are only for this test */
  204. #define COMP_RLE 255
  205. #define COMP_ZLIB 1
  206. static int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *ctx);
  207. #ifndef OPENSSL_NO_RSA
  208. static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export,int keylength);
  209. static void free_tmp_rsa(void);
  210. #endif
  211. static int MS_CALLBACK app_verify_callback(X509_STORE_CTX *ctx, void *arg);
  212. #define APP_CALLBACK_STRING "Test Callback Argument"
  213. struct app_verify_arg
  214. {
  215. char *string;
  216. int app_verify;
  217. int allow_proxy_certs;
  218. char *proxy_auth;
  219. char *proxy_cond;
  220. };
  221. #ifndef OPENSSL_NO_DH
  222. static DH *get_dh512(void);
  223. static DH *get_dh1024(void);
  224. static DH *get_dh1024dsa(void);
  225. #endif
  226. static char *psk_key=NULL; /* by default PSK is not used */
  227. #ifndef OPENSSL_NO_PSK
  228. static unsigned int psk_client_callback(SSL *ssl, const char *hint, char *identity,
  229. unsigned int max_identity_len, unsigned char *psk,
  230. unsigned int max_psk_len);
  231. static unsigned int psk_server_callback(SSL *ssl, const char *identity, unsigned char *psk,
  232. unsigned int max_psk_len);
  233. #endif
  234. static BIO *bio_err=NULL;
  235. static BIO *bio_stdout=NULL;
  236. #ifndef OPENSSL_NO_NPN
  237. /* Note that this code assumes that this is only a one element list: */
  238. static const char NEXT_PROTO_STRING[] = "\x09testproto";
  239. int npn_client = 0;
  240. int npn_server = 0;
  241. int npn_server_reject = 0;
  242. static int cb_client_npn(SSL *s, unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg)
  243. {
  244. /* This callback only returns the protocol string, rather than a length
  245. prefixed set. We assume that NEXT_PROTO_STRING is a one element list and
  246. remove the first byte to chop off the length prefix. */
  247. *out = (unsigned char*) NEXT_PROTO_STRING + 1;
  248. *outlen = sizeof(NEXT_PROTO_STRING) - 2;
  249. return SSL_TLSEXT_ERR_OK;
  250. }
  251. static int cb_server_npn(SSL *s, const unsigned char **data, unsigned int *len, void *arg)
  252. {
  253. *data = (const unsigned char *) NEXT_PROTO_STRING;
  254. *len = sizeof(NEXT_PROTO_STRING) - 1;
  255. return SSL_TLSEXT_ERR_OK;
  256. }
  257. static int cb_server_rejects_npn(SSL *s, const unsigned char **data, unsigned int *len, void *arg)
  258. {
  259. return SSL_TLSEXT_ERR_NOACK;
  260. }
  261. static int verify_npn(SSL *client, SSL *server)
  262. {
  263. const unsigned char *client_s;
  264. unsigned client_len;
  265. const unsigned char *server_s;
  266. unsigned server_len;
  267. SSL_get0_next_proto_negotiated(client, &client_s, &client_len);
  268. SSL_get0_next_proto_negotiated(server, &server_s, &server_len);
  269. if (client_len)
  270. {
  271. BIO_printf(bio_stdout, "Client NPN: ");
  272. BIO_write(bio_stdout, client_s, client_len);
  273. BIO_printf(bio_stdout, "\n");
  274. }
  275. if (server_len)
  276. {
  277. BIO_printf(bio_stdout, "Server NPN: ");
  278. BIO_write(bio_stdout, server_s, server_len);
  279. BIO_printf(bio_stdout, "\n");
  280. }
  281. /* If an NPN string was returned, it must be the protocol that we
  282. * expected to negotiate. */
  283. if (client_len && (client_len != sizeof(NEXT_PROTO_STRING) - 2 ||
  284. memcmp(client_s, NEXT_PROTO_STRING + 1, client_len)))
  285. return -1;
  286. if (server_len && (server_len != sizeof(NEXT_PROTO_STRING) - 2 ||
  287. memcmp(server_s, NEXT_PROTO_STRING + 1, server_len)))
  288. return -1;
  289. if (!npn_client && client_len)
  290. return -1;
  291. if (!npn_server && server_len)
  292. return -1;
  293. if (npn_server_reject && server_len)
  294. return -1;
  295. if (npn_client && npn_server && (!client_len || !server_len))
  296. return -1;
  297. return 0;
  298. }
  299. #endif
  300. static char *cipher=NULL;
  301. static int verbose=0;
  302. static int debug=0;
  303. #if 0
  304. /* Not used yet. */
  305. #ifdef FIONBIO
  306. static int s_nbio=0;
  307. #endif
  308. #endif
  309. static const char rnd_seed[] = "string to make the random number generator think it has entropy";
  310. int doit_biopair(SSL *s_ssl,SSL *c_ssl,long bytes,clock_t *s_time,clock_t *c_time);
  311. int doit(SSL *s_ssl,SSL *c_ssl,long bytes);
  312. static int do_test_cipherlist(void);
  313. static void sv_usage(void)
  314. {
  315. fprintf(stderr,"usage: ssltest [args ...]\n");
  316. fprintf(stderr,"\n");
  317. fprintf(stderr," -server_auth - check server certificate\n");
  318. fprintf(stderr," -client_auth - do client authentication\n");
  319. fprintf(stderr," -proxy - allow proxy certificates\n");
  320. fprintf(stderr," -proxy_auth <val> - set proxy policy rights\n");
  321. fprintf(stderr," -proxy_cond <val> - experssion to test proxy policy rights\n");
  322. fprintf(stderr," -v - more output\n");
  323. fprintf(stderr," -d - debug output\n");
  324. fprintf(stderr," -reuse - use session-id reuse\n");
  325. fprintf(stderr," -num <val> - number of connections to perform\n");
  326. fprintf(stderr," -bytes <val> - number of bytes to swap between client/server\n");
  327. #ifndef OPENSSL_NO_DH
  328. fprintf(stderr," -dhe1024 - use 1024 bit key (safe prime) for DHE\n");
  329. fprintf(stderr," -dhe1024dsa - use 1024 bit key (with 160-bit subprime) for DHE\n");
  330. fprintf(stderr," -no_dhe - disable DHE\n");
  331. #endif
  332. #ifndef OPENSSL_NO_ECDH
  333. fprintf(stderr," -no_ecdhe - disable ECDHE\n");
  334. #endif
  335. #ifndef OPENSSL_NO_PSK
  336. fprintf(stderr," -psk arg - PSK in hex (without 0x)\n");
  337. #endif
  338. #ifndef OPENSSL_NO_SSL2
  339. fprintf(stderr," -ssl2 - use SSLv2\n");
  340. #endif
  341. #ifndef OPENSSL_NO_SSL3
  342. fprintf(stderr," -ssl3 - use SSLv3\n");
  343. #endif
  344. #ifndef OPENSSL_NO_TLS1
  345. fprintf(stderr," -tls1 - use TLSv1\n");
  346. #endif
  347. fprintf(stderr," -CApath arg - PEM format directory of CA's\n");
  348. fprintf(stderr," -CAfile arg - PEM format file of CA's\n");
  349. fprintf(stderr," -cert arg - Server certificate file\n");
  350. fprintf(stderr," -key arg - Server key file (default: same as -cert)\n");
  351. fprintf(stderr," -c_cert arg - Client certificate file\n");
  352. fprintf(stderr," -c_key arg - Client key file (default: same as -c_cert)\n");
  353. fprintf(stderr," -cipher arg - The cipher list\n");
  354. fprintf(stderr," -bio_pair - Use BIO pairs\n");
  355. fprintf(stderr," -f - Test even cases that can't work\n");
  356. fprintf(stderr," -time - measure processor time used by client and server\n");
  357. fprintf(stderr," -zlib - use zlib compression\n");
  358. fprintf(stderr," -rle - use rle compression\n");
  359. #ifndef OPENSSL_NO_ECDH
  360. fprintf(stderr," -named_curve arg - Elliptic curve name to use for ephemeral ECDH keys.\n" \
  361. " Use \"openssl ecparam -list_curves\" for all names\n" \
  362. " (default is sect163r2).\n");
  363. #endif
  364. fprintf(stderr," -test_cipherlist - verifies the order of the ssl cipher lists\n");
  365. #ifndef OPENSSL_NO_NPN
  366. fprintf(stderr," -npn_client - have client side offer NPN\n");
  367. fprintf(stderr," -npn_server - have server side offer NPN\n");
  368. fprintf(stderr," -npn_server_reject - have server reject NPN\n");
  369. #endif
  370. }
  371. static void print_details(SSL *c_ssl, const char *prefix)
  372. {
  373. const SSL_CIPHER *ciph;
  374. X509 *cert;
  375. ciph=SSL_get_current_cipher(c_ssl);
  376. BIO_printf(bio_stdout,"%s%s, cipher %s %s",
  377. prefix,
  378. SSL_get_version(c_ssl),
  379. SSL_CIPHER_get_version(ciph),
  380. SSL_CIPHER_get_name(ciph));
  381. cert=SSL_get_peer_certificate(c_ssl);
  382. if (cert != NULL)
  383. {
  384. EVP_PKEY *pkey = X509_get_pubkey(cert);
  385. if (pkey != NULL)
  386. {
  387. if (0)
  388. ;
  389. #ifndef OPENSSL_NO_RSA
  390. else if (pkey->type == EVP_PKEY_RSA && pkey->pkey.rsa != NULL
  391. && pkey->pkey.rsa->n != NULL)
  392. {
  393. BIO_printf(bio_stdout, ", %d bit RSA",
  394. BN_num_bits(pkey->pkey.rsa->n));
  395. }
  396. #endif
  397. #ifndef OPENSSL_NO_DSA
  398. else if (pkey->type == EVP_PKEY_DSA && pkey->pkey.dsa != NULL
  399. && pkey->pkey.dsa->p != NULL)
  400. {
  401. BIO_printf(bio_stdout, ", %d bit DSA",
  402. BN_num_bits(pkey->pkey.dsa->p));
  403. }
  404. #endif
  405. EVP_PKEY_free(pkey);
  406. }
  407. X509_free(cert);
  408. }
  409. /* The SSL API does not allow us to look at temporary RSA/DH keys,
  410. * otherwise we should print their lengths too */
  411. BIO_printf(bio_stdout,"\n");
  412. }
  413. static void lock_dbg_cb(int mode, int type, const char *file, int line)
  414. {
  415. static int modes[CRYPTO_NUM_LOCKS]; /* = {0, 0, ... } */
  416. const char *errstr = NULL;
  417. int rw;
  418. rw = mode & (CRYPTO_READ|CRYPTO_WRITE);
  419. if (!((rw == CRYPTO_READ) || (rw == CRYPTO_WRITE)))
  420. {
  421. errstr = "invalid mode";
  422. goto err;
  423. }
  424. if (type < 0 || type >= CRYPTO_NUM_LOCKS)
  425. {
  426. errstr = "type out of bounds";
  427. goto err;
  428. }
  429. if (mode & CRYPTO_LOCK)
  430. {
  431. if (modes[type])
  432. {
  433. errstr = "already locked";
  434. /* must not happen in a single-threaded program
  435. * (would deadlock) */
  436. goto err;
  437. }
  438. modes[type] = rw;
  439. }
  440. else if (mode & CRYPTO_UNLOCK)
  441. {
  442. if (!modes[type])
  443. {
  444. errstr = "not locked";
  445. goto err;
  446. }
  447. if (modes[type] != rw)
  448. {
  449. errstr = (rw == CRYPTO_READ) ?
  450. "CRYPTO_r_unlock on write lock" :
  451. "CRYPTO_w_unlock on read lock";
  452. }
  453. modes[type] = 0;
  454. }
  455. else
  456. {
  457. errstr = "invalid mode";
  458. goto err;
  459. }
  460. err:
  461. if (errstr)
  462. {
  463. /* we cannot use bio_err here */
  464. fprintf(stderr, "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
  465. errstr, mode, type, file, line);
  466. }
  467. }
  468. #ifdef TLSEXT_TYPE_opaque_prf_input
  469. struct cb_info_st { void *input; size_t len; int ret; };
  470. struct cb_info_st co1 = { "C", 1, 1 }; /* try to negotiate oqaque PRF input */
  471. struct cb_info_st co2 = { "C", 1, 2 }; /* insist on oqaque PRF input */
  472. struct cb_info_st so1 = { "S", 1, 1 }; /* try to negotiate oqaque PRF input */
  473. struct cb_info_st so2 = { "S", 1, 2 }; /* insist on oqaque PRF input */
  474. int opaque_prf_input_cb(SSL *ssl, void *peerinput, size_t len, void *arg_)
  475. {
  476. struct cb_info_st *arg = arg_;
  477. if (arg == NULL)
  478. return 1;
  479. if (!SSL_set_tlsext_opaque_prf_input(ssl, arg->input, arg->len))
  480. return 0;
  481. return arg->ret;
  482. }
  483. #endif
  484. int main(int argc, char *argv[])
  485. {
  486. char *CApath=NULL,*CAfile=NULL;
  487. int badop=0;
  488. int bio_pair=0;
  489. int force=0;
  490. int tls1=0,ssl2=0,ssl3=0,ret=1;
  491. int client_auth=0;
  492. int server_auth=0,i;
  493. struct app_verify_arg app_verify_arg =
  494. { APP_CALLBACK_STRING, 0, 0, NULL, NULL };
  495. char *server_cert=TEST_SERVER_CERT;
  496. char *server_key=NULL;
  497. char *client_cert=TEST_CLIENT_CERT;
  498. char *client_key=NULL;
  499. #ifndef OPENSSL_NO_ECDH
  500. char *named_curve = NULL;
  501. #endif
  502. SSL_CTX *s_ctx=NULL;
  503. SSL_CTX *c_ctx=NULL;
  504. const SSL_METHOD *meth=NULL;
  505. SSL *c_ssl,*s_ssl;
  506. int number=1,reuse=0;
  507. long bytes=256L;
  508. #ifndef OPENSSL_NO_DH
  509. DH *dh;
  510. int dhe1024 = 0, dhe1024dsa = 0;
  511. #endif
  512. #ifndef OPENSSL_NO_ECDH
  513. EC_KEY *ecdh = NULL;
  514. #endif
  515. int no_dhe = 0;
  516. int no_ecdhe = 0;
  517. int no_psk = 0;
  518. int print_time = 0;
  519. clock_t s_time = 0, c_time = 0;
  520. int comp = 0;
  521. #ifndef OPENSSL_NO_COMP
  522. COMP_METHOD *cm = NULL;
  523. STACK_OF(SSL_COMP) *ssl_comp_methods = NULL;
  524. #endif
  525. int test_cipherlist = 0;
  526. verbose = 0;
  527. debug = 0;
  528. cipher = 0;
  529. bio_err=BIO_new_fp(stderr,BIO_NOCLOSE|BIO_FP_TEXT);
  530. CRYPTO_set_locking_callback(lock_dbg_cb);
  531. /* enable memory leak checking unless explicitly disabled */
  532. if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))))
  533. {
  534. CRYPTO_malloc_debug_init();
  535. CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
  536. }
  537. else
  538. {
  539. /* OPENSSL_DEBUG_MEMORY=off */
  540. CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
  541. }
  542. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  543. RAND_seed(rnd_seed, sizeof rnd_seed);
  544. bio_stdout=BIO_new_fp(stdout,BIO_NOCLOSE|BIO_FP_TEXT);
  545. argc--;
  546. argv++;
  547. while (argc >= 1)
  548. {
  549. if (strcmp(*argv,"-server_auth") == 0)
  550. server_auth=1;
  551. else if (strcmp(*argv,"-client_auth") == 0)
  552. client_auth=1;
  553. else if (strcmp(*argv,"-proxy_auth") == 0)
  554. {
  555. if (--argc < 1) goto bad;
  556. app_verify_arg.proxy_auth= *(++argv);
  557. }
  558. else if (strcmp(*argv,"-proxy_cond") == 0)
  559. {
  560. if (--argc < 1) goto bad;
  561. app_verify_arg.proxy_cond= *(++argv);
  562. }
  563. else if (strcmp(*argv,"-v") == 0)
  564. verbose=1;
  565. else if (strcmp(*argv,"-d") == 0)
  566. debug=1;
  567. else if (strcmp(*argv,"-reuse") == 0)
  568. reuse=1;
  569. else if (strcmp(*argv,"-dhe1024") == 0)
  570. {
  571. #ifndef OPENSSL_NO_DH
  572. dhe1024=1;
  573. #else
  574. fprintf(stderr,"ignoring -dhe1024, since I'm compiled without DH\n");
  575. #endif
  576. }
  577. else if (strcmp(*argv,"-dhe1024dsa") == 0)
  578. {
  579. #ifndef OPENSSL_NO_DH
  580. dhe1024dsa=1;
  581. #else
  582. fprintf(stderr,"ignoring -dhe1024, since I'm compiled without DH\n");
  583. #endif
  584. }
  585. else if (strcmp(*argv,"-no_dhe") == 0)
  586. no_dhe=1;
  587. else if (strcmp(*argv,"-no_ecdhe") == 0)
  588. no_ecdhe=1;
  589. else if (strcmp(*argv,"-psk") == 0)
  590. {
  591. if (--argc < 1) goto bad;
  592. psk_key=*(++argv);
  593. #ifndef OPENSSL_NO_PSK
  594. if (strspn(psk_key, "abcdefABCDEF1234567890") != strlen(psk_key))
  595. {
  596. BIO_printf(bio_err,"Not a hex number '%s'\n",*argv);
  597. goto bad;
  598. }
  599. #else
  600. no_psk=1;
  601. #endif
  602. }
  603. else if (strcmp(*argv,"-ssl2") == 0)
  604. ssl2=1;
  605. else if (strcmp(*argv,"-tls1") == 0)
  606. tls1=1;
  607. else if (strcmp(*argv,"-ssl3") == 0)
  608. ssl3=1;
  609. else if (strncmp(*argv,"-num",4) == 0)
  610. {
  611. if (--argc < 1) goto bad;
  612. number= atoi(*(++argv));
  613. if (number == 0) number=1;
  614. }
  615. else if (strcmp(*argv,"-bytes") == 0)
  616. {
  617. if (--argc < 1) goto bad;
  618. bytes= atol(*(++argv));
  619. if (bytes == 0L) bytes=1L;
  620. i=strlen(argv[0]);
  621. if (argv[0][i-1] == 'k') bytes*=1024L;
  622. if (argv[0][i-1] == 'm') bytes*=1024L*1024L;
  623. }
  624. else if (strcmp(*argv,"-cert") == 0)
  625. {
  626. if (--argc < 1) goto bad;
  627. server_cert= *(++argv);
  628. }
  629. else if (strcmp(*argv,"-s_cert") == 0)
  630. {
  631. if (--argc < 1) goto bad;
  632. server_cert= *(++argv);
  633. }
  634. else if (strcmp(*argv,"-key") == 0)
  635. {
  636. if (--argc < 1) goto bad;
  637. server_key= *(++argv);
  638. }
  639. else if (strcmp(*argv,"-s_key") == 0)
  640. {
  641. if (--argc < 1) goto bad;
  642. server_key= *(++argv);
  643. }
  644. else if (strcmp(*argv,"-c_cert") == 0)
  645. {
  646. if (--argc < 1) goto bad;
  647. client_cert= *(++argv);
  648. }
  649. else if (strcmp(*argv,"-c_key") == 0)
  650. {
  651. if (--argc < 1) goto bad;
  652. client_key= *(++argv);
  653. }
  654. else if (strcmp(*argv,"-cipher") == 0)
  655. {
  656. if (--argc < 1) goto bad;
  657. cipher= *(++argv);
  658. }
  659. else if (strcmp(*argv,"-CApath") == 0)
  660. {
  661. if (--argc < 1) goto bad;
  662. CApath= *(++argv);
  663. }
  664. else if (strcmp(*argv,"-CAfile") == 0)
  665. {
  666. if (--argc < 1) goto bad;
  667. CAfile= *(++argv);
  668. }
  669. else if (strcmp(*argv,"-bio_pair") == 0)
  670. {
  671. bio_pair = 1;
  672. }
  673. else if (strcmp(*argv,"-f") == 0)
  674. {
  675. force = 1;
  676. }
  677. else if (strcmp(*argv,"-time") == 0)
  678. {
  679. print_time = 1;
  680. }
  681. else if (strcmp(*argv,"-zlib") == 0)
  682. {
  683. comp = COMP_ZLIB;
  684. }
  685. else if (strcmp(*argv,"-rle") == 0)
  686. {
  687. comp = COMP_RLE;
  688. }
  689. else if (strcmp(*argv,"-named_curve") == 0)
  690. {
  691. if (--argc < 1) goto bad;
  692. #ifndef OPENSSL_NO_ECDH
  693. named_curve = *(++argv);
  694. #else
  695. fprintf(stderr,"ignoring -named_curve, since I'm compiled without ECDH\n");
  696. ++argv;
  697. #endif
  698. }
  699. else if (strcmp(*argv,"-app_verify") == 0)
  700. {
  701. app_verify_arg.app_verify = 1;
  702. }
  703. else if (strcmp(*argv,"-proxy") == 0)
  704. {
  705. app_verify_arg.allow_proxy_certs = 1;
  706. }
  707. else if (strcmp(*argv,"-test_cipherlist") == 0)
  708. {
  709. test_cipherlist = 1;
  710. }
  711. #ifndef OPENSSL_NO_NPN
  712. else if (strcmp(*argv,"-npn_client") == 0)
  713. {
  714. npn_client = 1;
  715. }
  716. else if (strcmp(*argv,"-npn_server") == 0)
  717. {
  718. npn_server = 1;
  719. }
  720. else if (strcmp(*argv,"-npn_server_reject") == 0)
  721. {
  722. npn_server_reject = 1;
  723. }
  724. #endif
  725. else
  726. {
  727. fprintf(stderr,"unknown option %s\n",*argv);
  728. badop=1;
  729. break;
  730. }
  731. argc--;
  732. argv++;
  733. }
  734. if (badop)
  735. {
  736. bad:
  737. sv_usage();
  738. goto end;
  739. }
  740. if (test_cipherlist == 1)
  741. {
  742. /* ensure that the cipher list are correctly sorted and exit */
  743. if (do_test_cipherlist() == 0)
  744. EXIT(1);
  745. ret = 0;
  746. goto end;
  747. }
  748. if (!ssl2 && !ssl3 && !tls1 && number > 1 && !reuse && !force)
  749. {
  750. fprintf(stderr, "This case cannot work. Use -f to perform "
  751. "the test anyway (and\n-d to see what happens), "
  752. "or add one of -ssl2, -ssl3, -tls1, -reuse\n"
  753. "to avoid protocol mismatch.\n");
  754. EXIT(1);
  755. }
  756. if (print_time)
  757. {
  758. if (!bio_pair)
  759. {
  760. fprintf(stderr, "Using BIO pair (-bio_pair)\n");
  761. bio_pair = 1;
  762. }
  763. if (number < 50 && !force)
  764. fprintf(stderr, "Warning: For accurate timings, use more connections (e.g. -num 1000)\n");
  765. }
  766. /* if (cipher == NULL) cipher=getenv("SSL_CIPHER"); */
  767. SSL_library_init();
  768. SSL_load_error_strings();
  769. #ifndef OPENSSL_NO_COMP
  770. if (comp == COMP_ZLIB) cm = COMP_zlib();
  771. if (comp == COMP_RLE) cm = COMP_rle();
  772. if (cm != NULL)
  773. {
  774. if (cm->type != NID_undef)
  775. {
  776. if (SSL_COMP_add_compression_method(comp, cm) != 0)
  777. {
  778. fprintf(stderr,
  779. "Failed to add compression method\n");
  780. ERR_print_errors_fp(stderr);
  781. }
  782. }
  783. else
  784. {
  785. fprintf(stderr,
  786. "Warning: %s compression not supported\n",
  787. (comp == COMP_RLE ? "rle" :
  788. (comp == COMP_ZLIB ? "zlib" :
  789. "unknown")));
  790. ERR_print_errors_fp(stderr);
  791. }
  792. }
  793. ssl_comp_methods = SSL_COMP_get_compression_methods();
  794. fprintf(stderr, "Available compression methods:\n");
  795. {
  796. int j, n = sk_SSL_COMP_num(ssl_comp_methods);
  797. if (n == 0)
  798. fprintf(stderr, " NONE\n");
  799. else
  800. for (j = 0; j < n; j++)
  801. {
  802. SSL_COMP *c = sk_SSL_COMP_value(ssl_comp_methods, j);
  803. fprintf(stderr, " %d: %s\n", c->id, c->name);
  804. }
  805. }
  806. #endif
  807. #if !defined(OPENSSL_NO_SSL2) && !defined(OPENSSL_NO_SSL3)
  808. if (ssl2)
  809. meth=SSLv2_method();
  810. else
  811. if (tls1)
  812. meth=TLSv1_method();
  813. else
  814. if (ssl3)
  815. meth=SSLv3_method();
  816. else
  817. meth=SSLv23_method();
  818. #else
  819. #ifdef OPENSSL_NO_SSL2
  820. meth=SSLv3_method();
  821. #else
  822. meth=SSLv2_method();
  823. #endif
  824. #endif
  825. c_ctx=SSL_CTX_new(meth);
  826. s_ctx=SSL_CTX_new(meth);
  827. if ((c_ctx == NULL) || (s_ctx == NULL))
  828. {
  829. ERR_print_errors(bio_err);
  830. goto end;
  831. }
  832. if (cipher != NULL)
  833. {
  834. SSL_CTX_set_cipher_list(c_ctx,cipher);
  835. SSL_CTX_set_cipher_list(s_ctx,cipher);
  836. }
  837. #ifndef OPENSSL_NO_DH
  838. if (!no_dhe)
  839. {
  840. if (dhe1024dsa)
  841. {
  842. /* use SSL_OP_SINGLE_DH_USE to avoid small subgroup attacks */
  843. SSL_CTX_set_options(s_ctx, SSL_OP_SINGLE_DH_USE);
  844. dh=get_dh1024dsa();
  845. }
  846. else if (dhe1024)
  847. dh=get_dh1024();
  848. else
  849. dh=get_dh512();
  850. SSL_CTX_set_tmp_dh(s_ctx,dh);
  851. DH_free(dh);
  852. }
  853. #else
  854. (void)no_dhe;
  855. #endif
  856. #ifndef OPENSSL_NO_ECDH
  857. if (!no_ecdhe)
  858. {
  859. int nid;
  860. if (named_curve != NULL)
  861. {
  862. nid = OBJ_sn2nid(named_curve);
  863. if (nid == 0)
  864. {
  865. BIO_printf(bio_err, "unknown curve name (%s)\n", named_curve);
  866. goto end;
  867. }
  868. }
  869. else
  870. nid = NID_sect163r2;
  871. ecdh = EC_KEY_new_by_curve_name(nid);
  872. if (ecdh == NULL)
  873. {
  874. BIO_printf(bio_err, "unable to create curve\n");
  875. goto end;
  876. }
  877. SSL_CTX_set_tmp_ecdh(s_ctx, ecdh);
  878. SSL_CTX_set_options(s_ctx, SSL_OP_SINGLE_ECDH_USE);
  879. EC_KEY_free(ecdh);
  880. }
  881. #else
  882. (void)no_ecdhe;
  883. #endif
  884. #ifndef OPENSSL_NO_RSA
  885. SSL_CTX_set_tmp_rsa_callback(s_ctx,tmp_rsa_cb);
  886. #endif
  887. #ifdef TLSEXT_TYPE_opaque_prf_input
  888. SSL_CTX_set_tlsext_opaque_prf_input_callback(c_ctx, opaque_prf_input_cb);
  889. SSL_CTX_set_tlsext_opaque_prf_input_callback(s_ctx, opaque_prf_input_cb);
  890. SSL_CTX_set_tlsext_opaque_prf_input_callback_arg(c_ctx, &co1); /* or &co2 or NULL */
  891. SSL_CTX_set_tlsext_opaque_prf_input_callback_arg(s_ctx, &so1); /* or &so2 or NULL */
  892. #endif
  893. if (!SSL_CTX_use_certificate_file(s_ctx,server_cert,SSL_FILETYPE_PEM))
  894. {
  895. ERR_print_errors(bio_err);
  896. }
  897. else if (!SSL_CTX_use_PrivateKey_file(s_ctx,
  898. (server_key?server_key:server_cert), SSL_FILETYPE_PEM))
  899. {
  900. ERR_print_errors(bio_err);
  901. goto end;
  902. }
  903. if (client_auth)
  904. {
  905. SSL_CTX_use_certificate_file(c_ctx,client_cert,
  906. SSL_FILETYPE_PEM);
  907. SSL_CTX_use_PrivateKey_file(c_ctx,
  908. (client_key?client_key:client_cert),
  909. SSL_FILETYPE_PEM);
  910. }
  911. if ( (!SSL_CTX_load_verify_locations(s_ctx,CAfile,CApath)) ||
  912. (!SSL_CTX_set_default_verify_paths(s_ctx)) ||
  913. (!SSL_CTX_load_verify_locations(c_ctx,CAfile,CApath)) ||
  914. (!SSL_CTX_set_default_verify_paths(c_ctx)))
  915. {
  916. /* fprintf(stderr,"SSL_load_verify_locations\n"); */
  917. ERR_print_errors(bio_err);
  918. /* goto end; */
  919. }
  920. if (client_auth)
  921. {
  922. BIO_printf(bio_err,"client authentication\n");
  923. SSL_CTX_set_verify(s_ctx,
  924. SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
  925. verify_callback);
  926. SSL_CTX_set_cert_verify_callback(s_ctx, app_verify_callback, &app_verify_arg);
  927. }
  928. if (server_auth)
  929. {
  930. BIO_printf(bio_err,"server authentication\n");
  931. SSL_CTX_set_verify(c_ctx,SSL_VERIFY_PEER,
  932. verify_callback);
  933. SSL_CTX_set_cert_verify_callback(c_ctx, app_verify_callback, &app_verify_arg);
  934. }
  935. {
  936. int session_id_context = 0;
  937. SSL_CTX_set_session_id_context(s_ctx, (void *)&session_id_context, sizeof session_id_context);
  938. }
  939. /* Use PSK only if PSK key is given */
  940. if (psk_key != NULL)
  941. {
  942. /* no_psk is used to avoid putting psk command to openssl tool */
  943. if (no_psk)
  944. {
  945. /* if PSK is not compiled in and psk key is
  946. * given, do nothing and exit successfully */
  947. ret=0;
  948. goto end;
  949. }
  950. #ifndef OPENSSL_NO_PSK
  951. SSL_CTX_set_psk_client_callback(c_ctx, psk_client_callback);
  952. SSL_CTX_set_psk_server_callback(s_ctx, psk_server_callback);
  953. if (debug)
  954. BIO_printf(bio_err,"setting PSK identity hint to s_ctx\n");
  955. if (!SSL_CTX_use_psk_identity_hint(s_ctx, "ctx server identity_hint"))
  956. {
  957. BIO_printf(bio_err,"error setting PSK identity hint to s_ctx\n");
  958. ERR_print_errors(bio_err);
  959. goto end;
  960. }
  961. #endif
  962. }
  963. #ifndef OPENSSL_NO_NPN
  964. if (npn_client)
  965. {
  966. SSL_CTX_set_next_proto_select_cb(c_ctx, cb_client_npn, NULL);
  967. }
  968. if (npn_server)
  969. {
  970. if (npn_server_reject)
  971. {
  972. BIO_printf(bio_err, "Can't have both -npn_server and -npn_server_reject\n");
  973. goto end;
  974. }
  975. SSL_CTX_set_next_protos_advertised_cb(s_ctx, cb_server_npn, NULL);
  976. }
  977. if (npn_server_reject)
  978. {
  979. SSL_CTX_set_next_protos_advertised_cb(s_ctx, cb_server_rejects_npn, NULL);
  980. }
  981. #endif
  982. c_ssl=SSL_new(c_ctx);
  983. s_ssl=SSL_new(s_ctx);
  984. #ifndef OPENSSL_NO_KRB5
  985. if (c_ssl && c_ssl->kssl_ctx)
  986. {
  987. char localhost[MAXHOSTNAMELEN+2];
  988. if (gethostname(localhost, sizeof localhost-1) == 0)
  989. {
  990. localhost[sizeof localhost-1]='\0';
  991. if(strlen(localhost) == sizeof localhost-1)
  992. {
  993. BIO_printf(bio_err,"localhost name too long\n");
  994. goto end;
  995. }
  996. kssl_ctx_setstring(c_ssl->kssl_ctx, KSSL_SERVER,
  997. localhost);
  998. }
  999. }
  1000. #endif /* OPENSSL_NO_KRB5 */
  1001. for (i=0; i<number; i++)
  1002. {
  1003. if (!reuse) SSL_set_session(c_ssl,NULL);
  1004. if (bio_pair)
  1005. ret=doit_biopair(s_ssl,c_ssl,bytes,&s_time,&c_time);
  1006. else
  1007. ret=doit(s_ssl,c_ssl,bytes);
  1008. }
  1009. if (!verbose)
  1010. {
  1011. print_details(c_ssl, "");
  1012. }
  1013. if ((number > 1) || (bytes > 1L))
  1014. BIO_printf(bio_stdout, "%d handshakes of %ld bytes done\n",number,bytes);
  1015. if (print_time)
  1016. {
  1017. #ifdef CLOCKS_PER_SEC
  1018. /* "To determine the time in seconds, the value returned
  1019. * by the clock function should be divided by the value
  1020. * of the macro CLOCKS_PER_SEC."
  1021. * -- ISO/IEC 9899 */
  1022. BIO_printf(bio_stdout, "Approximate total server time: %6.2f s\n"
  1023. "Approximate total client time: %6.2f s\n",
  1024. (double)s_time/CLOCKS_PER_SEC,
  1025. (double)c_time/CLOCKS_PER_SEC);
  1026. #else
  1027. /* "`CLOCKS_PER_SEC' undeclared (first use this function)"
  1028. * -- cc on NeXTstep/OpenStep */
  1029. BIO_printf(bio_stdout,
  1030. "Approximate total server time: %6.2f units\n"
  1031. "Approximate total client time: %6.2f units\n",
  1032. (double)s_time,
  1033. (double)c_time);
  1034. #endif
  1035. }
  1036. SSL_free(s_ssl);
  1037. SSL_free(c_ssl);
  1038. end:
  1039. if (s_ctx != NULL) SSL_CTX_free(s_ctx);
  1040. if (c_ctx != NULL) SSL_CTX_free(c_ctx);
  1041. if (bio_stdout != NULL) BIO_free(bio_stdout);
  1042. #ifndef OPENSSL_NO_RSA
  1043. free_tmp_rsa();
  1044. #endif
  1045. #ifndef OPENSSL_NO_ENGINE
  1046. ENGINE_cleanup();
  1047. #endif
  1048. CRYPTO_cleanup_all_ex_data();
  1049. ERR_free_strings();
  1050. ERR_remove_thread_state(NULL);
  1051. EVP_cleanup();
  1052. CRYPTO_mem_leaks(bio_err);
  1053. if (bio_err != NULL) BIO_free(bio_err);
  1054. EXIT(ret);
  1055. return ret;
  1056. }
  1057. int doit_biopair(SSL *s_ssl, SSL *c_ssl, long count,
  1058. clock_t *s_time, clock_t *c_time)
  1059. {
  1060. long cw_num = count, cr_num = count, sw_num = count, sr_num = count;
  1061. BIO *s_ssl_bio = NULL, *c_ssl_bio = NULL;
  1062. BIO *server = NULL, *server_io = NULL, *client = NULL, *client_io = NULL;
  1063. int ret = 1;
  1064. size_t bufsiz = 256; /* small buffer for testing */
  1065. if (!BIO_new_bio_pair(&server, bufsiz, &server_io, bufsiz))
  1066. goto err;
  1067. if (!BIO_new_bio_pair(&client, bufsiz, &client_io, bufsiz))
  1068. goto err;
  1069. s_ssl_bio = BIO_new(BIO_f_ssl());
  1070. if (!s_ssl_bio)
  1071. goto err;
  1072. c_ssl_bio = BIO_new(BIO_f_ssl());
  1073. if (!c_ssl_bio)
  1074. goto err;
  1075. SSL_set_connect_state(c_ssl);
  1076. SSL_set_bio(c_ssl, client, client);
  1077. (void)BIO_set_ssl(c_ssl_bio, c_ssl, BIO_NOCLOSE);
  1078. SSL_set_accept_state(s_ssl);
  1079. SSL_set_bio(s_ssl, server, server);
  1080. (void)BIO_set_ssl(s_ssl_bio, s_ssl, BIO_NOCLOSE);
  1081. do
  1082. {
  1083. /* c_ssl_bio: SSL filter BIO
  1084. *
  1085. * client: pseudo-I/O for SSL library
  1086. *
  1087. * client_io: client's SSL communication; usually to be
  1088. * relayed over some I/O facility, but in this
  1089. * test program, we're the server, too:
  1090. *
  1091. * server_io: server's SSL communication
  1092. *
  1093. * server: pseudo-I/O for SSL library
  1094. *
  1095. * s_ssl_bio: SSL filter BIO
  1096. *
  1097. * The client and the server each employ a "BIO pair":
  1098. * client + client_io, server + server_io.
  1099. * BIO pairs are symmetric. A BIO pair behaves similar
  1100. * to a non-blocking socketpair (but both endpoints must
  1101. * be handled by the same thread).
  1102. * [Here we could connect client and server to the ends
  1103. * of a single BIO pair, but then this code would be less
  1104. * suitable as an example for BIO pairs in general.]
  1105. *
  1106. * Useful functions for querying the state of BIO pair endpoints:
  1107. *
  1108. * BIO_ctrl_pending(bio) number of bytes we can read now
  1109. * BIO_ctrl_get_read_request(bio) number of bytes needed to fulfil
  1110. * other side's read attempt
  1111. * BIO_ctrl_get_write_guarantee(bio) number of bytes we can write now
  1112. *
  1113. * ..._read_request is never more than ..._write_guarantee;
  1114. * it depends on the application which one you should use.
  1115. */
  1116. /* We have non-blocking behaviour throughout this test program, but
  1117. * can be sure that there is *some* progress in each iteration; so
  1118. * we don't have to worry about ..._SHOULD_READ or ..._SHOULD_WRITE
  1119. * -- we just try everything in each iteration
  1120. */
  1121. {
  1122. /* CLIENT */
  1123. MS_STATIC char cbuf[1024*8];
  1124. int i, r;
  1125. clock_t c_clock = clock();
  1126. memset(cbuf, 0, sizeof(cbuf));
  1127. if (debug)
  1128. if (SSL_in_init(c_ssl))
  1129. printf("client waiting in SSL_connect - %s\n",
  1130. SSL_state_string_long(c_ssl));
  1131. if (cw_num > 0)
  1132. {
  1133. /* Write to server. */
  1134. if (cw_num > (long)sizeof cbuf)
  1135. i = sizeof cbuf;
  1136. else
  1137. i = (int)cw_num;
  1138. r = BIO_write(c_ssl_bio, cbuf, i);
  1139. if (r < 0)
  1140. {
  1141. if (!BIO_should_retry(c_ssl_bio))
  1142. {
  1143. fprintf(stderr,"ERROR in CLIENT\n");
  1144. goto err;
  1145. }
  1146. /* BIO_should_retry(...) can just be ignored here.
  1147. * The library expects us to call BIO_write with
  1148. * the same arguments again, and that's what we will
  1149. * do in the next iteration. */
  1150. }
  1151. else if (r == 0)
  1152. {
  1153. fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
  1154. goto err;
  1155. }
  1156. else
  1157. {
  1158. if (debug)
  1159. printf("client wrote %d\n", r);
  1160. cw_num -= r;
  1161. }
  1162. }
  1163. if (cr_num > 0)
  1164. {
  1165. /* Read from server. */
  1166. r = BIO_read(c_ssl_bio, cbuf, sizeof(cbuf));
  1167. if (r < 0)
  1168. {
  1169. if (!BIO_should_retry(c_ssl_bio))
  1170. {
  1171. fprintf(stderr,"ERROR in CLIENT\n");
  1172. goto err;
  1173. }
  1174. /* Again, "BIO_should_retry" can be ignored. */
  1175. }
  1176. else if (r == 0)
  1177. {
  1178. fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
  1179. goto err;
  1180. }
  1181. else
  1182. {
  1183. if (debug)
  1184. printf("client read %d\n", r);
  1185. cr_num -= r;
  1186. }
  1187. }
  1188. /* c_time and s_time increments will typically be very small
  1189. * (depending on machine speed and clock tick intervals),
  1190. * but sampling over a large number of connections should
  1191. * result in fairly accurate figures. We cannot guarantee
  1192. * a lot, however -- if each connection lasts for exactly
  1193. * one clock tick, it will be counted only for the client
  1194. * or only for the server or even not at all.
  1195. */
  1196. *c_time += (clock() - c_clock);
  1197. }
  1198. {
  1199. /* SERVER */
  1200. MS_STATIC char sbuf[1024*8];
  1201. int i, r;
  1202. clock_t s_clock = clock();
  1203. memset(sbuf, 0, sizeof(sbuf));
  1204. if (debug)
  1205. if (SSL_in_init(s_ssl))
  1206. printf("server waiting in SSL_accept - %s\n",
  1207. SSL_state_string_long(s_ssl));
  1208. if (sw_num > 0)
  1209. {
  1210. /* Write to client. */
  1211. if (sw_num > (long)sizeof sbuf)
  1212. i = sizeof sbuf;
  1213. else
  1214. i = (int)sw_num;
  1215. r = BIO_write(s_ssl_bio, sbuf, i);
  1216. if (r < 0)
  1217. {
  1218. if (!BIO_should_retry(s_ssl_bio))
  1219. {
  1220. fprintf(stderr,"ERROR in SERVER\n");
  1221. goto err;
  1222. }
  1223. /* Ignore "BIO_should_retry". */
  1224. }
  1225. else if (r == 0)
  1226. {
  1227. fprintf(stderr,"SSL SERVER STARTUP FAILED\n");
  1228. goto err;
  1229. }
  1230. else
  1231. {
  1232. if (debug)
  1233. printf("server wrote %d\n", r);
  1234. sw_num -= r;
  1235. }
  1236. }
  1237. if (sr_num > 0)
  1238. {
  1239. /* Read from client. */
  1240. r = BIO_read(s_ssl_bio, sbuf, sizeof(sbuf));
  1241. if (r < 0)
  1242. {
  1243. if (!BIO_should_retry(s_ssl_bio))
  1244. {
  1245. fprintf(stderr,"ERROR in SERVER\n");
  1246. goto err;
  1247. }
  1248. /* blah, blah */
  1249. }
  1250. else if (r == 0)
  1251. {
  1252. fprintf(stderr,"SSL SERVER STARTUP FAILED\n");
  1253. goto err;
  1254. }
  1255. else
  1256. {
  1257. if (debug)
  1258. printf("server read %d\n", r);
  1259. sr_num -= r;
  1260. }
  1261. }
  1262. *s_time += (clock() - s_clock);
  1263. }
  1264. {
  1265. /* "I/O" BETWEEN CLIENT AND SERVER. */
  1266. size_t r1, r2;
  1267. BIO *io1 = server_io, *io2 = client_io;
  1268. /* we use the non-copying interface for io1
  1269. * and the standard BIO_write/BIO_read interface for io2
  1270. */
  1271. static int prev_progress = 1;
  1272. int progress = 0;
  1273. /* io1 to io2 */
  1274. do
  1275. {
  1276. size_t num;
  1277. int r;
  1278. r1 = BIO_ctrl_pending(io1);
  1279. r2 = BIO_ctrl_get_write_guarantee(io2);
  1280. num = r1;
  1281. if (r2 < num)
  1282. num = r2;
  1283. if (num)
  1284. {
  1285. char *dataptr;
  1286. if (INT_MAX < num) /* yeah, right */
  1287. num = INT_MAX;
  1288. r = BIO_nread(io1, &dataptr, (int)num);
  1289. assert(r > 0);
  1290. assert(r <= (int)num);
  1291. /* possibly r < num (non-contiguous data) */
  1292. num = r;
  1293. r = BIO_write(io2, dataptr, (int)num);
  1294. if (r != (int)num) /* can't happen */
  1295. {
  1296. fprintf(stderr, "ERROR: BIO_write could not write "
  1297. "BIO_ctrl_get_write_guarantee() bytes");
  1298. goto err;
  1299. }
  1300. progress = 1;
  1301. if (debug)
  1302. printf((io1 == client_io) ?
  1303. "C->S relaying: %d bytes\n" :
  1304. "S->C relaying: %d bytes\n",
  1305. (int)num);
  1306. }
  1307. }
  1308. while (r1 && r2);
  1309. /* io2 to io1 */
  1310. {
  1311. size_t num;
  1312. int r;
  1313. r1 = BIO_ctrl_pending(io2);
  1314. r2 = BIO_ctrl_get_read_request(io1);
  1315. /* here we could use ..._get_write_guarantee instead of
  1316. * ..._get_read_request, but by using the latter
  1317. * we test restartability of the SSL implementation
  1318. * more thoroughly */
  1319. num = r1;
  1320. if (r2 < num)
  1321. num = r2;
  1322. if (num)
  1323. {
  1324. char *dataptr;
  1325. if (INT_MAX < num)
  1326. num = INT_MAX;
  1327. if (num > 1)
  1328. --num; /* test restartability even more thoroughly */
  1329. r = BIO_nwrite0(io1, &dataptr);
  1330. assert(r > 0);
  1331. if (r < (int)num)
  1332. num = r;
  1333. r = BIO_read(io2, dataptr, (int)num);
  1334. if (r != (int)num) /* can't happen */
  1335. {
  1336. fprintf(stderr, "ERROR: BIO_read could not read "
  1337. "BIO_ctrl_pending() bytes");
  1338. goto err;
  1339. }
  1340. progress = 1;
  1341. r = BIO_nwrite(io1, &dataptr, (int)num);
  1342. if (r != (int)num) /* can't happen */
  1343. {
  1344. fprintf(stderr, "ERROR: BIO_nwrite() did not accept "
  1345. "BIO_nwrite0() bytes");
  1346. goto err;
  1347. }
  1348. if (debug)
  1349. printf((io2 == client_io) ?
  1350. "C->S relaying: %d bytes\n" :
  1351. "S->C relaying: %d bytes\n",
  1352. (int)num);
  1353. }
  1354. } /* no loop, BIO_ctrl_get_read_request now returns 0 anyway */
  1355. if (!progress && !prev_progress)
  1356. if (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0)
  1357. {
  1358. fprintf(stderr, "ERROR: got stuck\n");
  1359. if (strcmp("SSLv2", SSL_get_version(c_ssl)) == 0)
  1360. {
  1361. fprintf(stderr, "This can happen for SSL2 because "
  1362. "CLIENT-FINISHED and SERVER-VERIFY are written \n"
  1363. "concurrently ...");
  1364. if (strncmp("2SCF", SSL_state_string(c_ssl), 4) == 0
  1365. && strncmp("2SSV", SSL_state_string(s_ssl), 4) == 0)
  1366. {
  1367. fprintf(stderr, " ok.\n");
  1368. goto end;
  1369. }
  1370. }
  1371. fprintf(stderr, " ERROR.\n");
  1372. goto err;
  1373. }
  1374. prev_progress = progress;
  1375. }
  1376. }
  1377. while (cw_num > 0 || cr_num > 0 || sw_num > 0 || sr_num > 0);
  1378. if (verbose)
  1379. print_details(c_ssl, "DONE via BIO pair: ");
  1380. #ifndef OPENSSL_NO_NPN
  1381. if (verify_npn(c_ssl, s_ssl) < 0)
  1382. {
  1383. ret = 1;
  1384. goto end;
  1385. }
  1386. #endif
  1387. end:
  1388. ret = 0;
  1389. err:
  1390. ERR_print_errors(bio_err);
  1391. if (server)
  1392. BIO_free(server);
  1393. if (server_io)
  1394. BIO_free(server_io);
  1395. if (client)
  1396. BIO_free(client);
  1397. if (client_io)
  1398. BIO_free(client_io);
  1399. if (s_ssl_bio)
  1400. BIO_free(s_ssl_bio);
  1401. if (c_ssl_bio)
  1402. BIO_free(c_ssl_bio);
  1403. return ret;
  1404. }
  1405. #define W_READ 1
  1406. #define W_WRITE 2
  1407. #define C_DONE 1
  1408. #define S_DONE 2
  1409. int doit(SSL *s_ssl, SSL *c_ssl, long count)
  1410. {
  1411. MS_STATIC char cbuf[1024*8],sbuf[1024*8];
  1412. long cw_num=count,cr_num=count;
  1413. long sw_num=count,sr_num=count;
  1414. int ret=1;
  1415. BIO *c_to_s=NULL;
  1416. BIO *s_to_c=NULL;
  1417. BIO *c_bio=NULL;
  1418. BIO *s_bio=NULL;
  1419. int c_r,c_w,s_r,s_w;
  1420. int i,j;
  1421. int done=0;
  1422. int c_write,s_write;
  1423. int do_server=0,do_client=0;
  1424. memset(cbuf,0,sizeof(cbuf));
  1425. memset(sbuf,0,sizeof(sbuf));
  1426. c_to_s=BIO_new(BIO_s_mem());
  1427. s_to_c=BIO_new(BIO_s_mem());
  1428. if ((s_to_c == NULL) || (c_to_s == NULL))
  1429. {
  1430. ERR_print_errors(bio_err);
  1431. goto err;
  1432. }
  1433. c_bio=BIO_new(BIO_f_ssl());
  1434. s_bio=BIO_new(BIO_f_ssl());
  1435. if ((c_bio == NULL) || (s_bio == NULL))
  1436. {
  1437. ERR_print_errors(bio_err);
  1438. goto err;
  1439. }
  1440. SSL_set_connect_state(c_ssl);
  1441. SSL_set_bio(c_ssl,s_to_c,c_to_s);
  1442. BIO_set_ssl(c_bio,c_ssl,BIO_NOCLOSE);
  1443. SSL_set_accept_state(s_ssl);
  1444. SSL_set_bio(s_ssl,c_to_s,s_to_c);
  1445. BIO_set_ssl(s_bio,s_ssl,BIO_NOCLOSE);
  1446. c_r=0; s_r=1;
  1447. c_w=1; s_w=0;
  1448. c_write=1,s_write=0;
  1449. /* We can always do writes */
  1450. for (;;)
  1451. {
  1452. do_server=0;
  1453. do_client=0;
  1454. i=(int)BIO_pending(s_bio);
  1455. if ((i && s_r) || s_w) do_server=1;
  1456. i=(int)BIO_pending(c_bio);
  1457. if ((i && c_r) || c_w) do_client=1;
  1458. if (do_server && debug)
  1459. {
  1460. if (SSL_in_init(s_ssl))
  1461. printf("server waiting in SSL_accept - %s\n",
  1462. SSL_state_string_long(s_ssl));
  1463. /* else if (s_write)
  1464. printf("server:SSL_write()\n");
  1465. else
  1466. printf("server:SSL_read()\n"); */
  1467. }
  1468. if (do_client && debug)
  1469. {
  1470. if (SSL_in_init(c_ssl))
  1471. printf("client waiting in SSL_connect - %s\n",
  1472. SSL_state_string_long(c_ssl));
  1473. /* else if (c_write)
  1474. printf("client:SSL_write()\n");
  1475. else
  1476. printf("client:SSL_read()\n"); */
  1477. }
  1478. if (!do_client && !do_server)
  1479. {
  1480. fprintf(stdout,"ERROR IN STARTUP\n");
  1481. ERR_print_errors(bio_err);
  1482. break;
  1483. }
  1484. if (do_client && !(done & C_DONE))
  1485. {
  1486. if (c_write)
  1487. {
  1488. j = (cw_num > (long)sizeof(cbuf)) ?
  1489. (int)sizeof(cbuf) : (int)cw_num;
  1490. i=BIO_write(c_bio,cbuf,j);
  1491. if (i < 0)
  1492. {
  1493. c_r=0;
  1494. c_w=0;
  1495. if (BIO_should_retry(c_bio))
  1496. {
  1497. if (BIO_should_read(c_bio))
  1498. c_r=1;
  1499. if (BIO_should_write(c_bio))
  1500. c_w=1;
  1501. }
  1502. else
  1503. {
  1504. fprintf(stderr,"ERROR in CLIENT\n");
  1505. ERR_print_errors(bio_err);
  1506. goto err;
  1507. }
  1508. }
  1509. else if (i == 0)
  1510. {
  1511. fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
  1512. goto err;
  1513. }
  1514. else
  1515. {
  1516. if (debug)
  1517. printf("client wrote %d\n",i);
  1518. /* ok */
  1519. s_r=1;
  1520. c_write=0;
  1521. cw_num-=i;
  1522. }
  1523. }
  1524. else
  1525. {
  1526. i=BIO_read(c_bio,cbuf,sizeof(cbuf));
  1527. if (i < 0)
  1528. {
  1529. c_r=0;
  1530. c_w=0;
  1531. if (BIO_should_retry(c_bio))
  1532. {
  1533. if (BIO_should_read(c_bio))
  1534. c_r=1;
  1535. if (BIO_should_write(c_bio))
  1536. c_w=1;
  1537. }
  1538. else
  1539. {
  1540. fprintf(stderr,"ERROR in CLIENT\n");
  1541. ERR_print_errors(bio_err);
  1542. goto err;
  1543. }
  1544. }
  1545. else if (i == 0)
  1546. {
  1547. fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");
  1548. goto err;
  1549. }
  1550. else
  1551. {
  1552. if (debug)
  1553. printf("client read %d\n",i);
  1554. cr_num-=i;
  1555. if (sw_num > 0)
  1556. {
  1557. s_write=1;
  1558. s_w=1;
  1559. }
  1560. if (cr_num <= 0)
  1561. {
  1562. s_write=1;
  1563. s_w=1;
  1564. done=S_DONE|C_DONE;
  1565. }
  1566. }
  1567. }
  1568. }
  1569. if (do_server && !(done & S_DONE))
  1570. {
  1571. if (!s_write)
  1572. {
  1573. i=BIO_read(s_bio,sbuf,sizeof(cbuf));
  1574. if (i < 0)
  1575. {
  1576. s_r=0;
  1577. s_w=0;
  1578. if (BIO_should_retry(s_bio))
  1579. {
  1580. if (BIO_should_read(s_bio))
  1581. s_r=1;
  1582. if (BIO_should_write(s_bio))
  1583. s_w=1;
  1584. }
  1585. else
  1586. {
  1587. fprintf(stderr,"ERROR in SERVER\n");
  1588. ERR_print_errors(bio_err);
  1589. goto err;
  1590. }
  1591. }
  1592. else if (i == 0)
  1593. {
  1594. ERR_print_errors(bio_err);
  1595. fprintf(stderr,"SSL SERVER STARTUP FAILED in SSL_read\n");
  1596. goto err;
  1597. }
  1598. else
  1599. {
  1600. if (debug)
  1601. printf("server read %d\n",i);
  1602. sr_num-=i;
  1603. if (cw_num > 0)
  1604. {
  1605. c_write=1;
  1606. c_w=1;
  1607. }
  1608. if (sr_num <= 0)
  1609. {
  1610. s_write=1;
  1611. s_w=1;
  1612. c_write=0;
  1613. }
  1614. }
  1615. }
  1616. else
  1617. {
  1618. j = (sw_num > (long)sizeof(sbuf)) ?
  1619. (int)sizeof(sbuf) : (int)sw_num;
  1620. i=BIO_write(s_bio,sbuf,j);
  1621. if (i < 0)
  1622. {
  1623. s_r=0;
  1624. s_w=0;
  1625. if (BIO_should_retry(s_bio))
  1626. {
  1627. if (BIO_should_read(s_bio))
  1628. s_r=1;
  1629. if (BIO_should_write(s_bio))
  1630. s_w=1;
  1631. }
  1632. else
  1633. {
  1634. fprintf(stderr,"ERROR in SERVER\n");
  1635. ERR_print_errors(bio_err);
  1636. goto err;
  1637. }
  1638. }
  1639. else if (i == 0)
  1640. {
  1641. ERR_print_errors(bio_err);
  1642. fprintf(stderr,"SSL SERVER STARTUP FAILED in SSL_write\n");
  1643. goto err;
  1644. }
  1645. else
  1646. {
  1647. if (debug)
  1648. printf("server wrote %d\n",i);
  1649. sw_num-=i;
  1650. s_write=0;
  1651. c_r=1;
  1652. if (sw_num <= 0)
  1653. done|=S_DONE;
  1654. }
  1655. }
  1656. }
  1657. if ((done & S_DONE) && (done & C_DONE)) break;
  1658. }
  1659. if (verbose)
  1660. print_details(c_ssl, "DONE: ");
  1661. #ifndef OPENSSL_NO_NPN
  1662. if (verify_npn(c_ssl, s_ssl) < 0)
  1663. {
  1664. ret = 1;
  1665. goto err;
  1666. }
  1667. #endif
  1668. ret=0;
  1669. err:
  1670. /* We have to set the BIO's to NULL otherwise they will be
  1671. * OPENSSL_free()ed twice. Once when th s_ssl is SSL_free()ed and
  1672. * again when c_ssl is SSL_free()ed.
  1673. * This is a hack required because s_ssl and c_ssl are sharing the same
  1674. * BIO structure and SSL_set_bio() and SSL_free() automatically
  1675. * BIO_free non NULL entries.
  1676. * You should not normally do this or be required to do this */
  1677. if (s_ssl != NULL)
  1678. {
  1679. s_ssl->rbio=NULL;
  1680. s_ssl->wbio=NULL;
  1681. }
  1682. if (c_ssl != NULL)
  1683. {
  1684. c_ssl->rbio=NULL;
  1685. c_ssl->wbio=NULL;
  1686. }
  1687. if (c_to_s != NULL) BIO_free(c_to_s);
  1688. if (s_to_c != NULL) BIO_free(s_to_c);
  1689. if (c_bio != NULL) BIO_free_all(c_bio);
  1690. if (s_bio != NULL) BIO_free_all(s_bio);
  1691. return(ret);
  1692. }
  1693. static int get_proxy_auth_ex_data_idx(void)
  1694. {
  1695. static volatile int idx = -1;
  1696. if (idx < 0)
  1697. {
  1698. CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
  1699. if (idx < 0)
  1700. {
  1701. idx = X509_STORE_CTX_get_ex_new_index(0,
  1702. "SSLtest for verify callback", NULL,NULL,NULL);
  1703. }
  1704. CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
  1705. }
  1706. return idx;
  1707. }
  1708. static int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *ctx)
  1709. {
  1710. char *s,buf[256];
  1711. s=X509_NAME_oneline(X509_get_subject_name(ctx->current_cert),buf,
  1712. sizeof buf);
  1713. if (s != NULL)
  1714. {
  1715. if (ok)
  1716. fprintf(stderr,"depth=%d %s\n",
  1717. ctx->error_depth,buf);
  1718. else
  1719. {
  1720. fprintf(stderr,"depth=%d error=%d %s\n",
  1721. ctx->error_depth,ctx->error,buf);
  1722. }
  1723. }
  1724. if (ok == 0)
  1725. {
  1726. fprintf(stderr,"Error string: %s\n",
  1727. X509_verify_cert_error_string(ctx->error));
  1728. switch (ctx->error)
  1729. {
  1730. case X509_V_ERR_CERT_NOT_YET_VALID:
  1731. case X509_V_ERR_CERT_HAS_EXPIRED:
  1732. case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
  1733. fprintf(stderr," ... ignored.\n");
  1734. ok=1;
  1735. }
  1736. }
  1737. if (ok == 1)
  1738. {
  1739. X509 *xs = ctx->current_cert;
  1740. #if 0
  1741. X509 *xi = ctx->current_issuer;
  1742. #endif
  1743. if (xs->ex_flags & EXFLAG_PROXY)
  1744. {
  1745. unsigned int *letters =
  1746. X509_STORE_CTX_get_ex_data(ctx,
  1747. get_proxy_auth_ex_data_idx());
  1748. if (letters)
  1749. {
  1750. int found_any = 0;
  1751. int i;
  1752. PROXY_CERT_INFO_EXTENSION *pci =
  1753. X509_get_ext_d2i(xs, NID_proxyCertInfo,
  1754. NULL, NULL);
  1755. switch (OBJ_obj2nid(pci->proxyPolicy->policyLanguage))
  1756. {
  1757. case NID_Independent:
  1758. /* Completely meaningless in this
  1759. program, as there's no way to
  1760. grant explicit rights to a
  1761. specific PrC. Basically, using
  1762. id-ppl-Independent is the perfect
  1763. way to grant no rights at all. */
  1764. fprintf(stderr, " Independent proxy certificate");
  1765. for (i = 0; i < 26; i++)
  1766. letters[i] = 0;
  1767. break;
  1768. case NID_id_ppl_inheritAll:
  1769. /* This is basically a NOP, we
  1770. simply let the current rights
  1771. stand as they are. */
  1772. fprintf(stderr, " Proxy certificate inherits all");
  1773. break;
  1774. default:
  1775. s = (char *)
  1776. pci->proxyPolicy->policy->data;
  1777. i = pci->proxyPolicy->policy->length;
  1778. /* The algorithm works as follows:
  1779. it is assumed that previous
  1780. iterations or the initial granted
  1781. rights has already set some elements
  1782. of `letters'. What we need to do is
  1783. to clear those that weren't granted
  1784. by the current PrC as well. The
  1785. easiest way to do this is to add 1
  1786. to all the elements whose letters
  1787. are given with the current policy.
  1788. That way, all elements that are set
  1789. by the current policy and were
  1790. already set by earlier policies and
  1791. through the original grant of rights
  1792. will get the value 2 or higher.
  1793. The last thing to do is to sweep
  1794. through `letters' and keep the
  1795. elements having the value 2 as set,
  1796. and clear all the others. */
  1797. fprintf(stderr, " Certificate proxy rights = %*.*s", i, i, s);
  1798. while(i-- > 0)
  1799. {
  1800. int c = *s++;
  1801. if (isascii(c) && isalpha(c))
  1802. {
  1803. if (islower(c))
  1804. c = toupper(c);
  1805. letters[c - 'A']++;
  1806. }
  1807. }
  1808. for (i = 0; i < 26; i++)
  1809. if (letters[i] < 2)
  1810. letters[i] = 0;
  1811. else
  1812. letters[i] = 1;
  1813. }
  1814. found_any = 0;
  1815. fprintf(stderr,
  1816. ", resulting proxy rights = ");
  1817. for(i = 0; i < 26; i++)
  1818. if (letters[i])
  1819. {
  1820. fprintf(stderr, "%c", i + 'A');
  1821. found_any = 1;
  1822. }
  1823. if (!found_any)
  1824. fprintf(stderr, "none");
  1825. fprintf(stderr, "\n");
  1826. PROXY_CERT_INFO_EXTENSION_free(pci);
  1827. }
  1828. }
  1829. }
  1830. return(ok);
  1831. }
  1832. static void process_proxy_debug(int indent, const char *format, ...)
  1833. {
  1834. static const char indentation[] =
  1835. ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
  1836. ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"; /* That's 80 > */
  1837. char my_format[256];
  1838. va_list args;
  1839. BIO_snprintf(my_format, sizeof(my_format), "%*.*s %s",
  1840. indent, indent, indentation, format);
  1841. va_start(args, format);
  1842. vfprintf(stderr, my_format, args);
  1843. va_end(args);
  1844. }
  1845. /* Priority levels:
  1846. 0 [!]var, ()
  1847. 1 & ^
  1848. 2 |
  1849. */
  1850. static int process_proxy_cond_adders(unsigned int letters[26],
  1851. const char *cond, const char **cond_end, int *pos, int indent);
  1852. static int process_proxy_cond_val(unsigned int letters[26],
  1853. const char *cond, const char **cond_end, int *pos, int indent)
  1854. {
  1855. int c;
  1856. int ok = 1;
  1857. int negate = 0;
  1858. while(isspace((int)*cond))
  1859. {
  1860. cond++; (*pos)++;
  1861. }
  1862. c = *cond;
  1863. if (debug)
  1864. process_proxy_debug(indent,
  1865. "Start process_proxy_cond_val at position %d: %s\n",
  1866. *pos, cond);
  1867. while(c == '!')
  1868. {
  1869. negate = !negate;
  1870. cond++; (*pos)++;
  1871. while(isspace((int)*cond))
  1872. {
  1873. cond++; (*pos)++;
  1874. }
  1875. c = *cond;
  1876. }
  1877. if (c == '(')
  1878. {
  1879. cond++; (*pos)++;
  1880. ok = process_proxy_cond_adders(letters, cond, cond_end, pos,
  1881. indent + 1);
  1882. cond = *cond_end;
  1883. if (ok < 0)
  1884. goto end;
  1885. while(isspace((int)*cond))
  1886. {
  1887. cond++; (*pos)++;
  1888. }
  1889. c = *cond;
  1890. if (c != ')')
  1891. {
  1892. fprintf(stderr,
  1893. "Weird condition character in position %d: "
  1894. "%c\n", *pos, c);
  1895. ok = -1;
  1896. goto end;
  1897. }
  1898. cond++; (*pos)++;
  1899. }
  1900. else if (isascii(c) && isalpha(c))
  1901. {
  1902. if (islower(c))
  1903. c = toupper(c);
  1904. ok = letters[c - 'A'];
  1905. cond++; (*pos)++;
  1906. }
  1907. else
  1908. {
  1909. fprintf(stderr,
  1910. "Weird condition character in position %d: "
  1911. "%c\n", *pos, c);
  1912. ok = -1;
  1913. goto end;
  1914. }
  1915. end:
  1916. *cond_end = cond;
  1917. if (ok >= 0 && negate)
  1918. ok = !ok;
  1919. if (debug)
  1920. process_proxy_debug(indent,
  1921. "End process_proxy_cond_val at position %d: %s, returning %d\n",
  1922. *pos, cond, ok);
  1923. return ok;
  1924. }
  1925. static int process_proxy_cond_multipliers(unsigned int letters[26],
  1926. const char *cond, const char **cond_end, int *pos, int indent)
  1927. {
  1928. int ok;
  1929. char c;
  1930. if (debug)
  1931. process_proxy_debug(indent,
  1932. "Start process_proxy_cond_multipliers at position %d: %s\n",
  1933. *pos, cond);
  1934. ok = process_proxy_cond_val(letters, cond, cond_end, pos, indent + 1);
  1935. cond = *cond_end;
  1936. if (ok < 0)
  1937. goto end;
  1938. while(ok >= 0)
  1939. {
  1940. while(isspace((int)*cond))
  1941. {
  1942. cond++; (*pos)++;
  1943. }
  1944. c = *cond;
  1945. switch(c)
  1946. {
  1947. case '&':
  1948. case '^':
  1949. {
  1950. int save_ok = ok;
  1951. cond++; (*pos)++;
  1952. ok = process_proxy_cond_val(letters,
  1953. cond, cond_end, pos, indent + 1);
  1954. cond = *cond_end;
  1955. if (ok < 0)
  1956. break;
  1957. switch(c)
  1958. {
  1959. case '&':
  1960. ok &= save_ok;
  1961. break;
  1962. case '^':
  1963. ok ^= save_ok;
  1964. break;
  1965. default:
  1966. fprintf(stderr, "SOMETHING IS SERIOUSLY WRONG!"
  1967. " STOPPING\n");
  1968. EXIT(1);
  1969. }
  1970. }
  1971. break;
  1972. default:
  1973. goto end;
  1974. }
  1975. }
  1976. end:
  1977. if (debug)
  1978. process_proxy_debug(indent,
  1979. "End process_proxy_cond_multipliers at position %d: %s, returning %d\n",
  1980. *pos, cond, ok);
  1981. *cond_end = cond;
  1982. return ok;
  1983. }
  1984. static int process_proxy_cond_adders(unsigned int letters[26],
  1985. const char *cond, const char **cond_end, int *pos, int indent)
  1986. {
  1987. int ok;
  1988. char c;
  1989. if (debug)
  1990. process_proxy_debug(indent,
  1991. "Start process_proxy_cond_adders at position %d: %s\n",
  1992. *pos, cond);
  1993. ok = process_proxy_cond_multipliers(letters, cond, cond_end, pos,
  1994. indent + 1);
  1995. cond = *cond_end;
  1996. if (ok < 0)
  1997. goto end;
  1998. while(ok >= 0)
  1999. {
  2000. while(isspace((int)*cond))
  2001. {
  2002. cond++; (*pos)++;
  2003. }
  2004. c = *cond;
  2005. switch(c)
  2006. {
  2007. case '|':
  2008. {
  2009. int save_ok = ok;
  2010. cond++; (*pos)++;
  2011. ok = process_proxy_cond_multipliers(letters,
  2012. cond, cond_end, pos, indent + 1);
  2013. cond = *cond_end;
  2014. if (ok < 0)
  2015. break;
  2016. switch(c)
  2017. {
  2018. case '|':
  2019. ok |= save_ok;
  2020. break;
  2021. default:
  2022. fprintf(stderr, "SOMETHING IS SERIOUSLY WRONG!"
  2023. " STOPPING\n");
  2024. EXIT(1);
  2025. }
  2026. }
  2027. break;
  2028. default:
  2029. goto end;
  2030. }
  2031. }
  2032. end:
  2033. if (debug)
  2034. process_proxy_debug(indent,
  2035. "End process_proxy_cond_adders at position %d: %s, returning %d\n",
  2036. *pos, cond, ok);
  2037. *cond_end = cond;
  2038. return ok;
  2039. }
  2040. static int process_proxy_cond(unsigned int letters[26],
  2041. const char *cond, const char **cond_end)
  2042. {
  2043. int pos = 1;
  2044. return process_proxy_cond_adders(letters, cond, cond_end, &pos, 1);
  2045. }
  2046. static int MS_CALLBACK app_verify_callback(X509_STORE_CTX *ctx, void *arg)
  2047. {
  2048. int ok=1;
  2049. struct app_verify_arg *cb_arg = arg;
  2050. unsigned int letters[26]; /* only used with proxy_auth */
  2051. if (cb_arg->app_verify)
  2052. {
  2053. char *s = NULL,buf[256];
  2054. fprintf(stderr, "In app_verify_callback, allowing cert. ");
  2055. fprintf(stderr, "Arg is: %s\n", cb_arg->string);
  2056. fprintf(stderr, "Finished printing do we have a context? 0x%p a cert? 0x%p\n",
  2057. (void *)ctx, (void *)ctx->cert);
  2058. if (ctx->cert)
  2059. s=X509_NAME_oneline(X509_get_subject_name(ctx->cert),buf,256);
  2060. if (s != NULL)
  2061. {
  2062. fprintf(stderr,"cert depth=%d %s\n",ctx->error_depth,buf);
  2063. }
  2064. return(1);
  2065. }
  2066. if (cb_arg->proxy_auth)
  2067. {
  2068. int found_any = 0, i;
  2069. char *sp;
  2070. for(i = 0; i < 26; i++)
  2071. letters[i] = 0;
  2072. for(sp = cb_arg->proxy_auth; *sp; sp++)
  2073. {
  2074. int c = *sp;
  2075. if (isascii(c) && isalpha(c))
  2076. {
  2077. if (islower(c))
  2078. c = toupper(c);
  2079. letters[c - 'A'] = 1;
  2080. }
  2081. }
  2082. fprintf(stderr,
  2083. " Initial proxy rights = ");
  2084. for(i = 0; i < 26; i++)
  2085. if (letters[i])
  2086. {
  2087. fprintf(stderr, "%c", i + 'A');
  2088. found_any = 1;
  2089. }
  2090. if (!found_any)
  2091. fprintf(stderr, "none");
  2092. fprintf(stderr, "\n");
  2093. X509_STORE_CTX_set_ex_data(ctx,
  2094. get_proxy_auth_ex_data_idx(),letters);
  2095. }
  2096. if (cb_arg->allow_proxy_certs)
  2097. {
  2098. X509_STORE_CTX_set_flags(ctx, X509_V_FLAG_ALLOW_PROXY_CERTS);
  2099. }
  2100. #ifndef OPENSSL_NO_X509_VERIFY
  2101. # ifdef OPENSSL_FIPS
  2102. if(s->version == TLS1_VERSION)
  2103. FIPS_allow_md5(1);
  2104. # endif
  2105. ok = X509_verify_cert(ctx);
  2106. # ifdef OPENSSL_FIPS
  2107. if(s->version == TLS1_VERSION)
  2108. FIPS_allow_md5(0);
  2109. # endif
  2110. #endif
  2111. if (cb_arg->proxy_auth)
  2112. {
  2113. if (ok > 0)
  2114. {
  2115. const char *cond_end = NULL;
  2116. ok = process_proxy_cond(letters,
  2117. cb_arg->proxy_cond, &cond_end);
  2118. if (ok < 0)
  2119. EXIT(3);
  2120. if (*cond_end)
  2121. {
  2122. fprintf(stderr, "Stopped processing condition before it's end.\n");
  2123. ok = 0;
  2124. }
  2125. if (!ok)
  2126. fprintf(stderr, "Proxy rights check with condition '%s' proved invalid\n",
  2127. cb_arg->proxy_cond);
  2128. else
  2129. fprintf(stderr, "Proxy rights check with condition '%s' proved valid\n",
  2130. cb_arg->proxy_cond);
  2131. }
  2132. }
  2133. return(ok);
  2134. }
  2135. #ifndef OPENSSL_NO_RSA
  2136. static RSA *rsa_tmp=NULL;
  2137. static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength)
  2138. {
  2139. BIGNUM *bn = NULL;
  2140. if (rsa_tmp == NULL)
  2141. {
  2142. bn = BN_new();
  2143. rsa_tmp = RSA_new();
  2144. if(!bn || !rsa_tmp || !BN_set_word(bn, RSA_F4))
  2145. {
  2146. BIO_printf(bio_err, "Memory error...");
  2147. goto end;
  2148. }
  2149. BIO_printf(bio_err,"Generating temp (%d bit) RSA key...",keylength);
  2150. (void)BIO_flush(bio_err);
  2151. if(!RSA_generate_key_ex(rsa_tmp,keylength,bn,NULL))
  2152. {
  2153. BIO_printf(bio_err, "Error generating key.");
  2154. RSA_free(rsa_tmp);
  2155. rsa_tmp = NULL;
  2156. }
  2157. end:
  2158. BIO_printf(bio_err,"\n");
  2159. (void)BIO_flush(bio_err);
  2160. }
  2161. if(bn) BN_free(bn);
  2162. return(rsa_tmp);
  2163. }
  2164. static void free_tmp_rsa(void)
  2165. {
  2166. if (rsa_tmp != NULL)
  2167. {
  2168. RSA_free(rsa_tmp);
  2169. rsa_tmp = NULL;
  2170. }
  2171. }
  2172. #endif
  2173. #ifndef OPENSSL_NO_DH
  2174. /* These DH parameters have been generated as follows:
  2175. * $ openssl dhparam -C -noout 512
  2176. * $ openssl dhparam -C -noout 1024
  2177. * $ openssl dhparam -C -noout -dsaparam 1024
  2178. * (The third function has been renamed to avoid name conflicts.)
  2179. */
  2180. static DH *get_dh512()
  2181. {
  2182. static unsigned char dh512_p[]={
  2183. 0xCB,0xC8,0xE1,0x86,0xD0,0x1F,0x94,0x17,0xA6,0x99,0xF0,0xC6,
  2184. 0x1F,0x0D,0xAC,0xB6,0x25,0x3E,0x06,0x39,0xCA,0x72,0x04,0xB0,
  2185. 0x6E,0xDA,0xC0,0x61,0xE6,0x7A,0x77,0x25,0xE8,0x3B,0xB9,0x5F,
  2186. 0x9A,0xB6,0xB5,0xFE,0x99,0x0B,0xA1,0x93,0x4E,0x35,0x33,0xB8,
  2187. 0xE1,0xF1,0x13,0x4F,0x59,0x1A,0xD2,0x57,0xC0,0x26,0x21,0x33,
  2188. 0x02,0xC5,0xAE,0x23,
  2189. };
  2190. static unsigned char dh512_g[]={
  2191. 0x02,
  2192. };
  2193. DH *dh;
  2194. if ((dh=DH_new()) == NULL) return(NULL);
  2195. dh->p=BN_bin2bn(dh512_p,sizeof(dh512_p),NULL);
  2196. dh->g=BN_bin2bn(dh512_g,sizeof(dh512_g),NULL);
  2197. if ((dh->p == NULL) || (dh->g == NULL))
  2198. { DH_free(dh); return(NULL); }
  2199. return(dh);
  2200. }
  2201. static DH *get_dh1024()
  2202. {
  2203. static unsigned char dh1024_p[]={
  2204. 0xF8,0x81,0x89,0x7D,0x14,0x24,0xC5,0xD1,0xE6,0xF7,0xBF,0x3A,
  2205. 0xE4,0x90,0xF4,0xFC,0x73,0xFB,0x34,0xB5,0xFA,0x4C,0x56,0xA2,
  2206. 0xEA,0xA7,0xE9,0xC0,0xC0,0xCE,0x89,0xE1,0xFA,0x63,0x3F,0xB0,
  2207. 0x6B,0x32,0x66,0xF1,0xD1,0x7B,0xB0,0x00,0x8F,0xCA,0x87,0xC2,
  2208. 0xAE,0x98,0x89,0x26,0x17,0xC2,0x05,0xD2,0xEC,0x08,0xD0,0x8C,
  2209. 0xFF,0x17,0x52,0x8C,0xC5,0x07,0x93,0x03,0xB1,0xF6,0x2F,0xB8,
  2210. 0x1C,0x52,0x47,0x27,0x1B,0xDB,0xD1,0x8D,0x9D,0x69,0x1D,0x52,
  2211. 0x4B,0x32,0x81,0xAA,0x7F,0x00,0xC8,0xDC,0xE6,0xD9,0xCC,0xC1,
  2212. 0x11,0x2D,0x37,0x34,0x6C,0xEA,0x02,0x97,0x4B,0x0E,0xBB,0xB1,
  2213. 0x71,0x33,0x09,0x15,0xFD,0xDD,0x23,0x87,0x07,0x5E,0x89,0xAB,
  2214. 0x6B,0x7C,0x5F,0xEC,0xA6,0x24,0xDC,0x53,
  2215. };
  2216. static unsigned char dh1024_g[]={
  2217. 0x02,
  2218. };
  2219. DH *dh;
  2220. if ((dh=DH_new()) == NULL) return(NULL);
  2221. dh->p=BN_bin2bn(dh1024_p,sizeof(dh1024_p),NULL);
  2222. dh->g=BN_bin2bn(dh1024_g,sizeof(dh1024_g),NULL);
  2223. if ((dh->p == NULL) || (dh->g == NULL))
  2224. { DH_free(dh); return(NULL); }
  2225. return(dh);
  2226. }
  2227. static DH *get_dh1024dsa()
  2228. {
  2229. static unsigned char dh1024_p[]={
  2230. 0xC8,0x00,0xF7,0x08,0x07,0x89,0x4D,0x90,0x53,0xF3,0xD5,0x00,
  2231. 0x21,0x1B,0xF7,0x31,0xA6,0xA2,0xDA,0x23,0x9A,0xC7,0x87,0x19,
  2232. 0x3B,0x47,0xB6,0x8C,0x04,0x6F,0xFF,0xC6,0x9B,0xB8,0x65,0xD2,
  2233. 0xC2,0x5F,0x31,0x83,0x4A,0xA7,0x5F,0x2F,0x88,0x38,0xB6,0x55,
  2234. 0xCF,0xD9,0x87,0x6D,0x6F,0x9F,0xDA,0xAC,0xA6,0x48,0xAF,0xFC,
  2235. 0x33,0x84,0x37,0x5B,0x82,0x4A,0x31,0x5D,0xE7,0xBD,0x52,0x97,
  2236. 0xA1,0x77,0xBF,0x10,0x9E,0x37,0xEA,0x64,0xFA,0xCA,0x28,0x8D,
  2237. 0x9D,0x3B,0xD2,0x6E,0x09,0x5C,0x68,0xC7,0x45,0x90,0xFD,0xBB,
  2238. 0x70,0xC9,0x3A,0xBB,0xDF,0xD4,0x21,0x0F,0xC4,0x6A,0x3C,0xF6,
  2239. 0x61,0xCF,0x3F,0xD6,0x13,0xF1,0x5F,0xBC,0xCF,0xBC,0x26,0x9E,
  2240. 0xBC,0x0B,0xBD,0xAB,0x5D,0xC9,0x54,0x39,
  2241. };
  2242. static unsigned char dh1024_g[]={
  2243. 0x3B,0x40,0x86,0xE7,0xF3,0x6C,0xDE,0x67,0x1C,0xCC,0x80,0x05,
  2244. 0x5A,0xDF,0xFE,0xBD,0x20,0x27,0x74,0x6C,0x24,0xC9,0x03,0xF3,
  2245. 0xE1,0x8D,0xC3,0x7D,0x98,0x27,0x40,0x08,0xB8,0x8C,0x6A,0xE9,
  2246. 0xBB,0x1A,0x3A,0xD6,0x86,0x83,0x5E,0x72,0x41,0xCE,0x85,0x3C,
  2247. 0xD2,0xB3,0xFC,0x13,0xCE,0x37,0x81,0x9E,0x4C,0x1C,0x7B,0x65,
  2248. 0xD3,0xE6,0xA6,0x00,0xF5,0x5A,0x95,0x43,0x5E,0x81,0xCF,0x60,
  2249. 0xA2,0x23,0xFC,0x36,0xA7,0x5D,0x7A,0x4C,0x06,0x91,0x6E,0xF6,
  2250. 0x57,0xEE,0x36,0xCB,0x06,0xEA,0xF5,0x3D,0x95,0x49,0xCB,0xA7,
  2251. 0xDD,0x81,0xDF,0x80,0x09,0x4A,0x97,0x4D,0xA8,0x22,0x72,0xA1,
  2252. 0x7F,0xC4,0x70,0x56,0x70,0xE8,0x20,0x10,0x18,0x8F,0x2E,0x60,
  2253. 0x07,0xE7,0x68,0x1A,0x82,0x5D,0x32,0xA2,
  2254. };
  2255. DH *dh;
  2256. if ((dh=DH_new()) == NULL) return(NULL);
  2257. dh->p=BN_bin2bn(dh1024_p,sizeof(dh1024_p),NULL);
  2258. dh->g=BN_bin2bn(dh1024_g,sizeof(dh1024_g),NULL);
  2259. if ((dh->p == NULL) || (dh->g == NULL))
  2260. { DH_free(dh); return(NULL); }
  2261. dh->length = 160;
  2262. return(dh);
  2263. }
  2264. #endif
  2265. #ifndef OPENSSL_NO_PSK
  2266. /* convert the PSK key (psk_key) in ascii to binary (psk) */
  2267. static int psk_key2bn(const char *pskkey, unsigned char *psk,
  2268. unsigned int max_psk_len)
  2269. {
  2270. int ret;
  2271. BIGNUM *bn = NULL;
  2272. ret = BN_hex2bn(&bn, pskkey);
  2273. if (!ret)
  2274. {
  2275. BIO_printf(bio_err,"Could not convert PSK key '%s' to BIGNUM\n", pskkey);
  2276. if (bn)
  2277. BN_free(bn);
  2278. return 0;
  2279. }
  2280. if (BN_num_bytes(bn) > (int)max_psk_len)
  2281. {
  2282. BIO_printf(bio_err,"psk buffer of callback is too small (%d) for key (%d)\n",
  2283. max_psk_len, BN_num_bytes(bn));
  2284. BN_free(bn);
  2285. return 0;
  2286. }
  2287. ret = BN_bn2bin(bn, psk);
  2288. BN_free(bn);
  2289. return ret;
  2290. }
  2291. static unsigned int psk_client_callback(SSL *ssl, const char *hint, char *identity,
  2292. unsigned int max_identity_len, unsigned char *psk,
  2293. unsigned int max_psk_len)
  2294. {
  2295. int ret;
  2296. unsigned int psk_len = 0;
  2297. ret = BIO_snprintf(identity, max_identity_len, "Client_identity");
  2298. if (ret < 0)
  2299. goto out_err;
  2300. if (debug)
  2301. fprintf(stderr, "client: created identity '%s' len=%d\n", identity, ret);
  2302. ret = psk_key2bn(psk_key, psk, max_psk_len);
  2303. if (ret < 0)
  2304. goto out_err;
  2305. psk_len = ret;
  2306. out_err:
  2307. return psk_len;
  2308. }
  2309. static unsigned int psk_server_callback(SSL *ssl, const char *identity,
  2310. unsigned char *psk, unsigned int max_psk_len)
  2311. {
  2312. unsigned int psk_len=0;
  2313. if (strcmp(identity, "Client_identity") != 0)
  2314. {
  2315. BIO_printf(bio_err, "server: PSK error: client identity not found\n");
  2316. return 0;
  2317. }
  2318. psk_len=psk_key2bn(psk_key, psk, max_psk_len);
  2319. return psk_len;
  2320. }
  2321. #endif
  2322. static int do_test_cipherlist(void)
  2323. {
  2324. int i = 0;
  2325. const SSL_METHOD *meth;
  2326. const SSL_CIPHER *ci, *tci = NULL;
  2327. #ifndef OPENSSL_NO_SSL2
  2328. fprintf(stderr, "testing SSLv2 cipher list order: ");
  2329. meth = SSLv2_method();
  2330. while ((ci = meth->get_cipher(i++)) != NULL)
  2331. {
  2332. if (tci != NULL)
  2333. if (ci->id >= tci->id)
  2334. {
  2335. fprintf(stderr, "failed %lx vs. %lx\n", ci->id, tci->id);
  2336. return 0;
  2337. }
  2338. tci = ci;
  2339. }
  2340. fprintf(stderr, "ok\n");
  2341. #endif
  2342. #ifndef OPENSSL_NO_SSL3
  2343. fprintf(stderr, "testing SSLv3 cipher list order: ");
  2344. meth = SSLv3_method();
  2345. tci = NULL;
  2346. while ((ci = meth->get_cipher(i++)) != NULL)
  2347. {
  2348. if (tci != NULL)
  2349. if (ci->id >= tci->id)
  2350. {
  2351. fprintf(stderr, "failed %lx vs. %lx\n", ci->id, tci->id);
  2352. return 0;
  2353. }
  2354. tci = ci;
  2355. }
  2356. fprintf(stderr, "ok\n");
  2357. #endif
  2358. #ifndef OPENSSL_NO_TLS1
  2359. fprintf(stderr, "testing TLSv1 cipher list order: ");
  2360. meth = TLSv1_method();
  2361. tci = NULL;
  2362. while ((ci = meth->get_cipher(i++)) != NULL)
  2363. {
  2364. if (tci != NULL)
  2365. if (ci->id >= tci->id)
  2366. {
  2367. fprintf(stderr, "failed %lx vs. %lx\n", ci->id, tci->id);
  2368. return 0;
  2369. }
  2370. tci = ci;
  2371. }
  2372. fprintf(stderr, "ok\n");
  2373. #endif
  2374. return 1;
  2375. }