ssltest.c 104 KB

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