ssltest.c 98 KB

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