ssltest.c 67 KB

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