s_cb.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664
  1. /* apps/s_cb.c - callback functions used by s_client, s_server, and s_time */
  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-2006 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. #include <stdio.h>
  112. #include <stdlib.h>
  113. #include <string.h> /* for memcpy() and strcmp() */
  114. #define USE_SOCKETS
  115. #define NON_MAIN
  116. #include "apps.h"
  117. #undef NON_MAIN
  118. #undef USE_SOCKETS
  119. #include <openssl/err.h>
  120. #include <openssl/rand.h>
  121. #include <openssl/x509.h>
  122. #include <openssl/ssl.h>
  123. #include "s_apps.h"
  124. #define COOKIE_SECRET_LENGTH 16
  125. int verify_depth = 0;
  126. int verify_quiet = 0;
  127. int verify_error = X509_V_OK;
  128. int verify_return_error = 0;
  129. unsigned char cookie_secret[COOKIE_SECRET_LENGTH];
  130. int cookie_initialized = 0;
  131. int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *ctx)
  132. {
  133. X509 *err_cert;
  134. int err, depth;
  135. err_cert = X509_STORE_CTX_get_current_cert(ctx);
  136. err = X509_STORE_CTX_get_error(ctx);
  137. depth = X509_STORE_CTX_get_error_depth(ctx);
  138. if (!verify_quiet || !ok) {
  139. BIO_printf(bio_err, "depth=%d ", depth);
  140. if (err_cert) {
  141. X509_NAME_print_ex(bio_err,
  142. X509_get_subject_name(err_cert),
  143. 0, XN_FLAG_ONELINE);
  144. BIO_puts(bio_err, "\n");
  145. } else
  146. BIO_puts(bio_err, "<no cert>\n");
  147. }
  148. if (!ok) {
  149. BIO_printf(bio_err, "verify error:num=%d:%s\n", err,
  150. X509_verify_cert_error_string(err));
  151. if (verify_depth >= depth) {
  152. if (!verify_return_error)
  153. ok = 1;
  154. verify_error = X509_V_OK;
  155. } else {
  156. ok = 0;
  157. verify_error = X509_V_ERR_CERT_CHAIN_TOO_LONG;
  158. }
  159. }
  160. switch (err) {
  161. case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
  162. BIO_puts(bio_err, "issuer= ");
  163. X509_NAME_print_ex(bio_err, X509_get_issuer_name(err_cert),
  164. 0, XN_FLAG_ONELINE);
  165. BIO_puts(bio_err, "\n");
  166. break;
  167. case X509_V_ERR_CERT_NOT_YET_VALID:
  168. case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
  169. BIO_printf(bio_err, "notBefore=");
  170. ASN1_TIME_print(bio_err, X509_get_notBefore(err_cert));
  171. BIO_printf(bio_err, "\n");
  172. break;
  173. case X509_V_ERR_CERT_HAS_EXPIRED:
  174. case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
  175. BIO_printf(bio_err, "notAfter=");
  176. ASN1_TIME_print(bio_err, X509_get_notAfter(err_cert));
  177. BIO_printf(bio_err, "\n");
  178. break;
  179. case X509_V_ERR_NO_EXPLICIT_POLICY:
  180. if (!verify_quiet)
  181. policies_print(bio_err, ctx);
  182. break;
  183. }
  184. if (err == X509_V_OK && ok == 2 && !verify_quiet)
  185. policies_print(bio_err, ctx);
  186. if (ok && !verify_quiet)
  187. BIO_printf(bio_err, "verify return:%d\n", ok);
  188. return (ok);
  189. }
  190. int set_cert_stuff(SSL_CTX *ctx, char *cert_file, char *key_file)
  191. {
  192. if (cert_file != NULL) {
  193. /*-
  194. SSL *ssl;
  195. X509 *x509;
  196. */
  197. if (SSL_CTX_use_certificate_file(ctx, cert_file,
  198. SSL_FILETYPE_PEM) <= 0) {
  199. BIO_printf(bio_err, "unable to get certificate from '%s'\n",
  200. cert_file);
  201. ERR_print_errors(bio_err);
  202. return (0);
  203. }
  204. if (key_file == NULL)
  205. key_file = cert_file;
  206. if (SSL_CTX_use_PrivateKey_file(ctx, key_file, SSL_FILETYPE_PEM) <= 0) {
  207. BIO_printf(bio_err, "unable to get private key from '%s'\n",
  208. key_file);
  209. ERR_print_errors(bio_err);
  210. return (0);
  211. }
  212. /*-
  213. In theory this is no longer needed
  214. ssl=SSL_new(ctx);
  215. x509=SSL_get_certificate(ssl);
  216. if (x509 != NULL) {
  217. EVP_PKEY *pktmp;
  218. pktmp = X509_get_pubkey(x509);
  219. EVP_PKEY_copy_parameters(pktmp,
  220. SSL_get_privatekey(ssl));
  221. EVP_PKEY_free(pktmp);
  222. }
  223. SSL_free(ssl);
  224. */
  225. /*
  226. * If we are using DSA, we can copy the parameters from the private
  227. * key
  228. */
  229. /*
  230. * Now we know that a key and cert have been set against the SSL
  231. * context
  232. */
  233. if (!SSL_CTX_check_private_key(ctx)) {
  234. BIO_printf(bio_err,
  235. "Private key does not match the certificate public key\n");
  236. return (0);
  237. }
  238. }
  239. return (1);
  240. }
  241. int set_cert_key_stuff(SSL_CTX *ctx, X509 *cert, EVP_PKEY *key,
  242. STACK_OF(X509) *chain, int build_chain)
  243. {
  244. int chflags = chain ? SSL_BUILD_CHAIN_FLAG_CHECK : 0;
  245. if (cert == NULL)
  246. return 1;
  247. if (SSL_CTX_use_certificate(ctx, cert) <= 0) {
  248. BIO_printf(bio_err, "error setting certificate\n");
  249. ERR_print_errors(bio_err);
  250. return 0;
  251. }
  252. if (SSL_CTX_use_PrivateKey(ctx, key) <= 0) {
  253. BIO_printf(bio_err, "error setting private key\n");
  254. ERR_print_errors(bio_err);
  255. return 0;
  256. }
  257. /*
  258. * Now we know that a key and cert have been set against the SSL context
  259. */
  260. if (!SSL_CTX_check_private_key(ctx)) {
  261. BIO_printf(bio_err,
  262. "Private key does not match the certificate public key\n");
  263. return 0;
  264. }
  265. if (chain && !SSL_CTX_set1_chain(ctx, chain)) {
  266. BIO_printf(bio_err, "error setting certificate chain\n");
  267. ERR_print_errors(bio_err);
  268. return 0;
  269. }
  270. if (build_chain && !SSL_CTX_build_cert_chain(ctx, chflags)) {
  271. BIO_printf(bio_err, "error building certificate chain\n");
  272. ERR_print_errors(bio_err);
  273. return 0;
  274. }
  275. return 1;
  276. }
  277. static void ssl_print_client_cert_types(BIO *bio, SSL *s)
  278. {
  279. const unsigned char *p;
  280. int i;
  281. int cert_type_num = SSL_get0_certificate_types(s, &p);
  282. if (!cert_type_num)
  283. return;
  284. BIO_puts(bio, "Client Certificate Types: ");
  285. for (i = 0; i < cert_type_num; i++) {
  286. unsigned char cert_type = p[i];
  287. char *cname;
  288. switch (cert_type) {
  289. case TLS_CT_RSA_SIGN:
  290. cname = "RSA sign";
  291. break;
  292. case TLS_CT_DSS_SIGN:
  293. cname = "DSA sign";
  294. break;
  295. case TLS_CT_RSA_FIXED_DH:
  296. cname = "RSA fixed DH";
  297. break;
  298. case TLS_CT_DSS_FIXED_DH:
  299. cname = "DSS fixed DH";
  300. break;
  301. case TLS_CT_ECDSA_SIGN:
  302. cname = "ECDSA sign";
  303. break;
  304. case TLS_CT_RSA_FIXED_ECDH:
  305. cname = "RSA fixed ECDH";
  306. break;
  307. case TLS_CT_ECDSA_FIXED_ECDH:
  308. cname = "ECDSA fixed ECDH";
  309. break;
  310. case TLS_CT_GOST94_SIGN:
  311. cname = "GOST94 Sign";
  312. break;
  313. case TLS_CT_GOST01_SIGN:
  314. cname = "GOST01 Sign";
  315. break;
  316. default:
  317. cname = NULL;
  318. }
  319. if (i)
  320. BIO_puts(bio, ", ");
  321. if (cname)
  322. BIO_puts(bio, cname);
  323. else
  324. BIO_printf(bio, "UNKNOWN (%d),", cert_type);
  325. }
  326. BIO_puts(bio, "\n");
  327. }
  328. static int do_print_sigalgs(BIO *out, SSL *s, int shared)
  329. {
  330. int i, nsig, client;
  331. client = SSL_is_server(s) ? 0 : 1;
  332. if (shared)
  333. nsig = SSL_get_shared_sigalgs(s, -1, NULL, NULL, NULL, NULL, NULL);
  334. else
  335. nsig = SSL_get_sigalgs(s, -1, NULL, NULL, NULL, NULL, NULL);
  336. if (nsig == 0)
  337. return 1;
  338. if (shared)
  339. BIO_puts(out, "Shared ");
  340. if (client)
  341. BIO_puts(out, "Requested ");
  342. BIO_puts(out, "Signature Algorithms: ");
  343. for (i = 0; i < nsig; i++) {
  344. int hash_nid, sign_nid;
  345. unsigned char rhash, rsign;
  346. const char *sstr = NULL;
  347. if (shared)
  348. SSL_get_shared_sigalgs(s, i, &sign_nid, &hash_nid, NULL,
  349. &rsign, &rhash);
  350. else
  351. SSL_get_sigalgs(s, i, &sign_nid, &hash_nid, NULL, &rsign, &rhash);
  352. if (i)
  353. BIO_puts(out, ":");
  354. if (sign_nid == EVP_PKEY_RSA)
  355. sstr = "RSA";
  356. else if (sign_nid == EVP_PKEY_DSA)
  357. sstr = "DSA";
  358. else if (sign_nid == EVP_PKEY_EC)
  359. sstr = "ECDSA";
  360. if (sstr)
  361. BIO_printf(out, "%s+", sstr);
  362. else
  363. BIO_printf(out, "0x%02X+", (int)rsign);
  364. if (hash_nid != NID_undef)
  365. BIO_printf(out, "%s", OBJ_nid2sn(hash_nid));
  366. else
  367. BIO_printf(out, "0x%02X", (int)rhash);
  368. }
  369. BIO_puts(out, "\n");
  370. return 1;
  371. }
  372. int ssl_print_sigalgs(BIO *out, SSL *s)
  373. {
  374. int mdnid;
  375. if (!SSL_is_server(s))
  376. ssl_print_client_cert_types(out, s);
  377. do_print_sigalgs(out, s, 0);
  378. do_print_sigalgs(out, s, 1);
  379. if (SSL_get_peer_signature_nid(s, &mdnid))
  380. BIO_printf(out, "Peer signing digest: %s\n", OBJ_nid2sn(mdnid));
  381. return 1;
  382. }
  383. #ifndef OPENSSL_NO_EC
  384. int ssl_print_point_formats(BIO *out, SSL *s)
  385. {
  386. int i, nformats;
  387. const char *pformats;
  388. nformats = SSL_get0_ec_point_formats(s, &pformats);
  389. if (nformats <= 0)
  390. return 1;
  391. BIO_puts(out, "Supported Elliptic Curve Point Formats: ");
  392. for (i = 0; i < nformats; i++, pformats++) {
  393. if (i)
  394. BIO_puts(out, ":");
  395. switch (*pformats) {
  396. case TLSEXT_ECPOINTFORMAT_uncompressed:
  397. BIO_puts(out, "uncompressed");
  398. break;
  399. case TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime:
  400. BIO_puts(out, "ansiX962_compressed_prime");
  401. break;
  402. case TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2:
  403. BIO_puts(out, "ansiX962_compressed_char2");
  404. break;
  405. default:
  406. BIO_printf(out, "unknown(%d)", (int)*pformats);
  407. break;
  408. }
  409. }
  410. if (nformats <= 0)
  411. BIO_puts(out, "NONE");
  412. BIO_puts(out, "\n");
  413. return 1;
  414. }
  415. int ssl_print_curves(BIO *out, SSL *s, int noshared)
  416. {
  417. int i, ncurves, *curves, nid;
  418. const char *cname;
  419. ncurves = SSL_get1_curves(s, NULL);
  420. if (ncurves <= 0)
  421. return 1;
  422. curves = OPENSSL_malloc(ncurves * sizeof(int));
  423. if (!curves) {
  424. BIO_puts(out, "Malloc error getting supported curves\n");
  425. return 0;
  426. }
  427. SSL_get1_curves(s, curves);
  428. BIO_puts(out, "Supported Elliptic Curves: ");
  429. for (i = 0; i < ncurves; i++) {
  430. if (i)
  431. BIO_puts(out, ":");
  432. nid = curves[i];
  433. /* If unrecognised print out hex version */
  434. if (nid & TLSEXT_nid_unknown)
  435. BIO_printf(out, "0x%04X", nid & 0xFFFF);
  436. else {
  437. /* Use NIST name for curve if it exists */
  438. cname = EC_curve_nid2nist(nid);
  439. if (!cname)
  440. cname = OBJ_nid2sn(nid);
  441. BIO_printf(out, "%s", cname);
  442. }
  443. }
  444. if (ncurves == 0)
  445. BIO_puts(out, "NONE");
  446. OPENSSL_free(curves);
  447. if (noshared) {
  448. BIO_puts(out, "\n");
  449. return 1;
  450. }
  451. BIO_puts(out, "\nShared Elliptic curves: ");
  452. ncurves = SSL_get_shared_curve(s, -1);
  453. for (i = 0; i < ncurves; i++) {
  454. if (i)
  455. BIO_puts(out, ":");
  456. nid = SSL_get_shared_curve(s, i);
  457. cname = EC_curve_nid2nist(nid);
  458. if (!cname)
  459. cname = OBJ_nid2sn(nid);
  460. BIO_printf(out, "%s", cname);
  461. }
  462. if (ncurves == 0)
  463. BIO_puts(out, "NONE");
  464. BIO_puts(out, "\n");
  465. return 1;
  466. }
  467. #endif
  468. int ssl_print_tmp_key(BIO *out, SSL *s)
  469. {
  470. EVP_PKEY *key;
  471. if (!SSL_get_server_tmp_key(s, &key))
  472. return 1;
  473. BIO_puts(out, "Server Temp Key: ");
  474. switch (EVP_PKEY_id(key)) {
  475. case EVP_PKEY_RSA:
  476. BIO_printf(out, "RSA, %d bits\n", EVP_PKEY_bits(key));
  477. break;
  478. case EVP_PKEY_DH:
  479. BIO_printf(out, "DH, %d bits\n", EVP_PKEY_bits(key));
  480. break;
  481. #ifndef OPENSSL_NO_ECDH
  482. case EVP_PKEY_EC:
  483. {
  484. EC_KEY *ec = EVP_PKEY_get1_EC_KEY(key);
  485. int nid;
  486. const char *cname;
  487. nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
  488. EC_KEY_free(ec);
  489. cname = EC_curve_nid2nist(nid);
  490. if (!cname)
  491. cname = OBJ_nid2sn(nid);
  492. BIO_printf(out, "ECDH, %s, %d bits\n", cname, EVP_PKEY_bits(key));
  493. }
  494. #endif
  495. }
  496. EVP_PKEY_free(key);
  497. return 1;
  498. }
  499. long MS_CALLBACK bio_dump_callback(BIO *bio, int cmd, const char *argp,
  500. int argi, long argl, long ret)
  501. {
  502. BIO *out;
  503. out = (BIO *)BIO_get_callback_arg(bio);
  504. if (out == NULL)
  505. return (ret);
  506. if (cmd == (BIO_CB_READ | BIO_CB_RETURN)) {
  507. BIO_printf(out, "read from %p [%p] (%lu bytes => %ld (0x%lX))\n",
  508. (void *)bio, (void *)argp, (unsigned long)argi, ret, ret);
  509. BIO_dump(out, argp, (int)ret);
  510. return (ret);
  511. } else if (cmd == (BIO_CB_WRITE | BIO_CB_RETURN)) {
  512. BIO_printf(out, "write to %p [%p] (%lu bytes => %ld (0x%lX))\n",
  513. (void *)bio, (void *)argp, (unsigned long)argi, ret, ret);
  514. BIO_dump(out, argp, (int)ret);
  515. }
  516. return (ret);
  517. }
  518. void MS_CALLBACK apps_ssl_info_callback(const SSL *s, int where, int ret)
  519. {
  520. const char *str;
  521. int w;
  522. w = where & ~SSL_ST_MASK;
  523. if (w & SSL_ST_CONNECT)
  524. str = "SSL_connect";
  525. else if (w & SSL_ST_ACCEPT)
  526. str = "SSL_accept";
  527. else
  528. str = "undefined";
  529. if (where & SSL_CB_LOOP) {
  530. BIO_printf(bio_err, "%s:%s\n", str, SSL_state_string_long(s));
  531. } else if (where & SSL_CB_ALERT) {
  532. str = (where & SSL_CB_READ) ? "read" : "write";
  533. BIO_printf(bio_err, "SSL3 alert %s:%s:%s\n",
  534. str,
  535. SSL_alert_type_string_long(ret),
  536. SSL_alert_desc_string_long(ret));
  537. } else if (where & SSL_CB_EXIT) {
  538. if (ret == 0)
  539. BIO_printf(bio_err, "%s:failed in %s\n",
  540. str, SSL_state_string_long(s));
  541. else if (ret < 0) {
  542. BIO_printf(bio_err, "%s:error in %s\n",
  543. str, SSL_state_string_long(s));
  544. }
  545. }
  546. }
  547. void MS_CALLBACK msg_cb(int write_p, int version, int content_type,
  548. const void *buf, size_t len, SSL *ssl, void *arg)
  549. {
  550. BIO *bio = arg;
  551. const char *str_write_p, *str_version, *str_content_type =
  552. "", *str_details1 = "", *str_details2 = "";
  553. str_write_p = write_p ? ">>>" : "<<<";
  554. switch (version) {
  555. case SSL2_VERSION:
  556. str_version = "SSL 2.0";
  557. break;
  558. case SSL3_VERSION:
  559. str_version = "SSL 3.0 ";
  560. break;
  561. case TLS1_VERSION:
  562. str_version = "TLS 1.0 ";
  563. break;
  564. case TLS1_1_VERSION:
  565. str_version = "TLS 1.1 ";
  566. break;
  567. case TLS1_2_VERSION:
  568. str_version = "TLS 1.2 ";
  569. break;
  570. case DTLS1_VERSION:
  571. str_version = "DTLS 1.0 ";
  572. break;
  573. case DTLS1_BAD_VER:
  574. str_version = "DTLS 1.0 (bad) ";
  575. break;
  576. default:
  577. str_version = "???";
  578. }
  579. if (version == SSL2_VERSION) {
  580. str_details1 = "???";
  581. if (len > 0) {
  582. switch (((const unsigned char *)buf)[0]) {
  583. case 0:
  584. str_details1 = ", ERROR:";
  585. str_details2 = " ???";
  586. if (len >= 3) {
  587. unsigned err =
  588. (((const unsigned char *)buf)[1] << 8) +
  589. ((const unsigned char *)buf)[2];
  590. switch (err) {
  591. case 0x0001:
  592. str_details2 = " NO-CIPHER-ERROR";
  593. break;
  594. case 0x0002:
  595. str_details2 = " NO-CERTIFICATE-ERROR";
  596. break;
  597. case 0x0004:
  598. str_details2 = " BAD-CERTIFICATE-ERROR";
  599. break;
  600. case 0x0006:
  601. str_details2 = " UNSUPPORTED-CERTIFICATE-TYPE-ERROR";
  602. break;
  603. }
  604. }
  605. break;
  606. case 1:
  607. str_details1 = ", CLIENT-HELLO";
  608. break;
  609. case 2:
  610. str_details1 = ", CLIENT-MASTER-KEY";
  611. break;
  612. case 3:
  613. str_details1 = ", CLIENT-FINISHED";
  614. break;
  615. case 4:
  616. str_details1 = ", SERVER-HELLO";
  617. break;
  618. case 5:
  619. str_details1 = ", SERVER-VERIFY";
  620. break;
  621. case 6:
  622. str_details1 = ", SERVER-FINISHED";
  623. break;
  624. case 7:
  625. str_details1 = ", REQUEST-CERTIFICATE";
  626. break;
  627. case 8:
  628. str_details1 = ", CLIENT-CERTIFICATE";
  629. break;
  630. }
  631. }
  632. }
  633. if (version == SSL3_VERSION ||
  634. version == TLS1_VERSION ||
  635. version == TLS1_1_VERSION ||
  636. version == TLS1_2_VERSION ||
  637. version == DTLS1_VERSION || version == DTLS1_BAD_VER) {
  638. switch (content_type) {
  639. case 20:
  640. str_content_type = "ChangeCipherSpec";
  641. break;
  642. case 21:
  643. str_content_type = "Alert";
  644. break;
  645. case 22:
  646. str_content_type = "Handshake";
  647. break;
  648. }
  649. if (content_type == 21) { /* Alert */
  650. str_details1 = ", ???";
  651. if (len == 2) {
  652. switch (((const unsigned char *)buf)[0]) {
  653. case 1:
  654. str_details1 = ", warning";
  655. break;
  656. case 2:
  657. str_details1 = ", fatal";
  658. break;
  659. }
  660. str_details2 = " ???";
  661. switch (((const unsigned char *)buf)[1]) {
  662. case 0:
  663. str_details2 = " close_notify";
  664. break;
  665. case 10:
  666. str_details2 = " unexpected_message";
  667. break;
  668. case 20:
  669. str_details2 = " bad_record_mac";
  670. break;
  671. case 21:
  672. str_details2 = " decryption_failed";
  673. break;
  674. case 22:
  675. str_details2 = " record_overflow";
  676. break;
  677. case 30:
  678. str_details2 = " decompression_failure";
  679. break;
  680. case 40:
  681. str_details2 = " handshake_failure";
  682. break;
  683. case 42:
  684. str_details2 = " bad_certificate";
  685. break;
  686. case 43:
  687. str_details2 = " unsupported_certificate";
  688. break;
  689. case 44:
  690. str_details2 = " certificate_revoked";
  691. break;
  692. case 45:
  693. str_details2 = " certificate_expired";
  694. break;
  695. case 46:
  696. str_details2 = " certificate_unknown";
  697. break;
  698. case 47:
  699. str_details2 = " illegal_parameter";
  700. break;
  701. case 48:
  702. str_details2 = " unknown_ca";
  703. break;
  704. case 49:
  705. str_details2 = " access_denied";
  706. break;
  707. case 50:
  708. str_details2 = " decode_error";
  709. break;
  710. case 51:
  711. str_details2 = " decrypt_error";
  712. break;
  713. case 60:
  714. str_details2 = " export_restriction";
  715. break;
  716. case 70:
  717. str_details2 = " protocol_version";
  718. break;
  719. case 71:
  720. str_details2 = " insufficient_security";
  721. break;
  722. case 80:
  723. str_details2 = " internal_error";
  724. break;
  725. case 90:
  726. str_details2 = " user_canceled";
  727. break;
  728. case 100:
  729. str_details2 = " no_renegotiation";
  730. break;
  731. case 110:
  732. str_details2 = " unsupported_extension";
  733. break;
  734. case 111:
  735. str_details2 = " certificate_unobtainable";
  736. break;
  737. case 112:
  738. str_details2 = " unrecognized_name";
  739. break;
  740. case 113:
  741. str_details2 = " bad_certificate_status_response";
  742. break;
  743. case 114:
  744. str_details2 = " bad_certificate_hash_value";
  745. break;
  746. case 115:
  747. str_details2 = " unknown_psk_identity";
  748. break;
  749. }
  750. }
  751. }
  752. if (content_type == 22) { /* Handshake */
  753. str_details1 = "???";
  754. if (len > 0) {
  755. switch (((const unsigned char *)buf)[0]) {
  756. case 0:
  757. str_details1 = ", HelloRequest";
  758. break;
  759. case 1:
  760. str_details1 = ", ClientHello";
  761. break;
  762. case 2:
  763. str_details1 = ", ServerHello";
  764. break;
  765. case 3:
  766. str_details1 = ", HelloVerifyRequest";
  767. break;
  768. case 11:
  769. str_details1 = ", Certificate";
  770. break;
  771. case 12:
  772. str_details1 = ", ServerKeyExchange";
  773. break;
  774. case 13:
  775. str_details1 = ", CertificateRequest";
  776. break;
  777. case 14:
  778. str_details1 = ", ServerHelloDone";
  779. break;
  780. case 15:
  781. str_details1 = ", CertificateVerify";
  782. break;
  783. case 16:
  784. str_details1 = ", ClientKeyExchange";
  785. break;
  786. case 20:
  787. str_details1 = ", Finished";
  788. break;
  789. }
  790. }
  791. }
  792. #ifndef OPENSSL_NO_HEARTBEATS
  793. if (content_type == 24) { /* Heartbeat */
  794. str_details1 = ", Heartbeat";
  795. if (len > 0) {
  796. switch (((const unsigned char *)buf)[0]) {
  797. case 1:
  798. str_details1 = ", HeartbeatRequest";
  799. break;
  800. case 2:
  801. str_details1 = ", HeartbeatResponse";
  802. break;
  803. }
  804. }
  805. }
  806. #endif
  807. }
  808. BIO_printf(bio, "%s %s%s [length %04lx]%s%s\n", str_write_p, str_version,
  809. str_content_type, (unsigned long)len, str_details1,
  810. str_details2);
  811. if (len > 0) {
  812. size_t num, i;
  813. BIO_printf(bio, " ");
  814. num = len;
  815. #if 0
  816. if (num > 16)
  817. num = 16;
  818. #endif
  819. for (i = 0; i < num; i++) {
  820. if (i % 16 == 0 && i > 0)
  821. BIO_printf(bio, "\n ");
  822. BIO_printf(bio, " %02x", ((const unsigned char *)buf)[i]);
  823. }
  824. if (i < len)
  825. BIO_printf(bio, " ...");
  826. BIO_printf(bio, "\n");
  827. }
  828. (void)BIO_flush(bio);
  829. }
  830. void MS_CALLBACK tlsext_cb(SSL *s, int client_server, int type,
  831. unsigned char *data, int len, void *arg)
  832. {
  833. BIO *bio = arg;
  834. char *extname;
  835. switch (type) {
  836. case TLSEXT_TYPE_server_name:
  837. extname = "server name";
  838. break;
  839. case TLSEXT_TYPE_max_fragment_length:
  840. extname = "max fragment length";
  841. break;
  842. case TLSEXT_TYPE_client_certificate_url:
  843. extname = "client certificate URL";
  844. break;
  845. case TLSEXT_TYPE_trusted_ca_keys:
  846. extname = "trusted CA keys";
  847. break;
  848. case TLSEXT_TYPE_truncated_hmac:
  849. extname = "truncated HMAC";
  850. break;
  851. case TLSEXT_TYPE_status_request:
  852. extname = "status request";
  853. break;
  854. case TLSEXT_TYPE_user_mapping:
  855. extname = "user mapping";
  856. break;
  857. case TLSEXT_TYPE_client_authz:
  858. extname = "client authz";
  859. break;
  860. case TLSEXT_TYPE_server_authz:
  861. extname = "server authz";
  862. break;
  863. case TLSEXT_TYPE_cert_type:
  864. extname = "cert type";
  865. break;
  866. case TLSEXT_TYPE_elliptic_curves:
  867. extname = "elliptic curves";
  868. break;
  869. case TLSEXT_TYPE_ec_point_formats:
  870. extname = "EC point formats";
  871. break;
  872. case TLSEXT_TYPE_srp:
  873. extname = "SRP";
  874. break;
  875. case TLSEXT_TYPE_signature_algorithms:
  876. extname = "signature algorithms";
  877. break;
  878. case TLSEXT_TYPE_use_srtp:
  879. extname = "use SRTP";
  880. break;
  881. case TLSEXT_TYPE_heartbeat:
  882. extname = "heartbeat";
  883. break;
  884. case TLSEXT_TYPE_session_ticket:
  885. extname = "session ticket";
  886. break;
  887. case TLSEXT_TYPE_renegotiate:
  888. extname = "renegotiation info";
  889. break;
  890. #ifdef TLSEXT_TYPE_opaque_prf_input
  891. case TLSEXT_TYPE_opaque_prf_input:
  892. extname = "opaque PRF input";
  893. break;
  894. #endif
  895. #ifdef TLSEXT_TYPE_next_proto_neg
  896. case TLSEXT_TYPE_next_proto_neg:
  897. extname = "next protocol";
  898. break;
  899. #endif
  900. #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
  901. case TLSEXT_TYPE_application_layer_protocol_negotiation:
  902. extname = "application layer protocol negotiation";
  903. break;
  904. #endif
  905. case TLSEXT_TYPE_padding:
  906. extname = "TLS padding";
  907. break;
  908. default:
  909. extname = "unknown";
  910. break;
  911. }
  912. BIO_printf(bio, "TLS %s extension \"%s\" (id=%d), len=%d\n",
  913. client_server ? "server" : "client", extname, type, len);
  914. BIO_dump(bio, (char *)data, len);
  915. (void)BIO_flush(bio);
  916. }
  917. int MS_CALLBACK generate_cookie_callback(SSL *ssl, unsigned char *cookie,
  918. unsigned int *cookie_len)
  919. {
  920. unsigned char *buffer, result[EVP_MAX_MD_SIZE];
  921. unsigned int length, resultlength;
  922. union {
  923. struct sockaddr sa;
  924. struct sockaddr_in s4;
  925. #if OPENSSL_USE_IPV6
  926. struct sockaddr_in6 s6;
  927. #endif
  928. } peer;
  929. /* Initialize a random secret */
  930. if (!cookie_initialized) {
  931. if (RAND_bytes(cookie_secret, COOKIE_SECRET_LENGTH) <= 0) {
  932. BIO_printf(bio_err, "error setting random cookie secret\n");
  933. return 0;
  934. }
  935. cookie_initialized = 1;
  936. }
  937. /* Read peer information */
  938. (void)BIO_dgram_get_peer(SSL_get_rbio(ssl), &peer);
  939. /* Create buffer with peer's address and port */
  940. length = 0;
  941. switch (peer.sa.sa_family) {
  942. case AF_INET:
  943. length += sizeof(struct in_addr);
  944. length += sizeof(peer.s4.sin_port);
  945. break;
  946. #if OPENSSL_USE_IPV6
  947. case AF_INET6:
  948. length += sizeof(struct in6_addr);
  949. length += sizeof(peer.s6.sin6_port);
  950. break;
  951. #endif
  952. default:
  953. OPENSSL_assert(0);
  954. break;
  955. }
  956. buffer = OPENSSL_malloc(length);
  957. if (buffer == NULL) {
  958. BIO_printf(bio_err, "out of memory\n");
  959. return 0;
  960. }
  961. switch (peer.sa.sa_family) {
  962. case AF_INET:
  963. memcpy(buffer, &peer.s4.sin_port, sizeof(peer.s4.sin_port));
  964. memcpy(buffer + sizeof(peer.s4.sin_port),
  965. &peer.s4.sin_addr, sizeof(struct in_addr));
  966. break;
  967. #if OPENSSL_USE_IPV6
  968. case AF_INET6:
  969. memcpy(buffer, &peer.s6.sin6_port, sizeof(peer.s6.sin6_port));
  970. memcpy(buffer + sizeof(peer.s6.sin6_port),
  971. &peer.s6.sin6_addr, sizeof(struct in6_addr));
  972. break;
  973. #endif
  974. default:
  975. OPENSSL_assert(0);
  976. break;
  977. }
  978. /* Calculate HMAC of buffer using the secret */
  979. HMAC(EVP_sha1(), cookie_secret, COOKIE_SECRET_LENGTH,
  980. buffer, length, result, &resultlength);
  981. OPENSSL_free(buffer);
  982. memcpy(cookie, result, resultlength);
  983. *cookie_len = resultlength;
  984. return 1;
  985. }
  986. int MS_CALLBACK verify_cookie_callback(SSL *ssl, unsigned char *cookie,
  987. unsigned int cookie_len)
  988. {
  989. unsigned char *buffer, result[EVP_MAX_MD_SIZE];
  990. unsigned int length, resultlength;
  991. union {
  992. struct sockaddr sa;
  993. struct sockaddr_in s4;
  994. #if OPENSSL_USE_IPV6
  995. struct sockaddr_in6 s6;
  996. #endif
  997. } peer;
  998. /* If secret isn't initialized yet, the cookie can't be valid */
  999. if (!cookie_initialized)
  1000. return 0;
  1001. /* Read peer information */
  1002. (void)BIO_dgram_get_peer(SSL_get_rbio(ssl), &peer);
  1003. /* Create buffer with peer's address and port */
  1004. length = 0;
  1005. switch (peer.sa.sa_family) {
  1006. case AF_INET:
  1007. length += sizeof(struct in_addr);
  1008. length += sizeof(peer.s4.sin_port);
  1009. break;
  1010. #if OPENSSL_USE_IPV6
  1011. case AF_INET6:
  1012. length += sizeof(struct in6_addr);
  1013. length += sizeof(peer.s6.sin6_port);
  1014. break;
  1015. #endif
  1016. default:
  1017. OPENSSL_assert(0);
  1018. break;
  1019. }
  1020. buffer = OPENSSL_malloc(length);
  1021. if (buffer == NULL) {
  1022. BIO_printf(bio_err, "out of memory\n");
  1023. return 0;
  1024. }
  1025. switch (peer.sa.sa_family) {
  1026. case AF_INET:
  1027. memcpy(buffer, &peer.s4.sin_port, sizeof(peer.s4.sin_port));
  1028. memcpy(buffer + sizeof(peer.s4.sin_port),
  1029. &peer.s4.sin_addr, sizeof(struct in_addr));
  1030. break;
  1031. #if OPENSSL_USE_IPV6
  1032. case AF_INET6:
  1033. memcpy(buffer, &peer.s6.sin6_port, sizeof(peer.s6.sin6_port));
  1034. memcpy(buffer + sizeof(peer.s6.sin6_port),
  1035. &peer.s6.sin6_addr, sizeof(struct in6_addr));
  1036. break;
  1037. #endif
  1038. default:
  1039. OPENSSL_assert(0);
  1040. break;
  1041. }
  1042. /* Calculate HMAC of buffer using the secret */
  1043. HMAC(EVP_sha1(), cookie_secret, COOKIE_SECRET_LENGTH,
  1044. buffer, length, result, &resultlength);
  1045. OPENSSL_free(buffer);
  1046. if (cookie_len == resultlength
  1047. && memcmp(result, cookie, resultlength) == 0)
  1048. return 1;
  1049. return 0;
  1050. }
  1051. /*
  1052. * Example of extended certificate handling. Where the standard support of
  1053. * one certificate per algorithm is not sufficient an application can decide
  1054. * which certificate(s) to use at runtime based on whatever criteria it deems
  1055. * appropriate.
  1056. */
  1057. /* Linked list of certificates, keys and chains */
  1058. struct ssl_excert_st {
  1059. int certform;
  1060. const char *certfile;
  1061. int keyform;
  1062. const char *keyfile;
  1063. const char *chainfile;
  1064. X509 *cert;
  1065. EVP_PKEY *key;
  1066. STACK_OF(X509) *chain;
  1067. int build_chain;
  1068. struct ssl_excert_st *next, *prev;
  1069. };
  1070. struct chain_flags {
  1071. int flag;
  1072. const char *name;
  1073. };
  1074. struct chain_flags chain_flags_list[] = {
  1075. {CERT_PKEY_VALID, "Overall Validity"},
  1076. {CERT_PKEY_SIGN, "Sign with EE key"},
  1077. {CERT_PKEY_EE_SIGNATURE, "EE signature"},
  1078. {CERT_PKEY_CA_SIGNATURE, "CA signature"},
  1079. {CERT_PKEY_EE_PARAM, "EE key parameters"},
  1080. {CERT_PKEY_CA_PARAM, "CA key parameters"},
  1081. {CERT_PKEY_EXPLICIT_SIGN, "Explicity sign with EE key"},
  1082. {CERT_PKEY_ISSUER_NAME, "Issuer Name"},
  1083. {CERT_PKEY_CERT_TYPE, "Certificate Type"},
  1084. {0, NULL}
  1085. };
  1086. static void print_chain_flags(BIO *out, SSL *s, int flags)
  1087. {
  1088. struct chain_flags *ctmp = chain_flags_list;
  1089. while (ctmp->name) {
  1090. BIO_printf(out, "\t%s: %s\n", ctmp->name,
  1091. flags & ctmp->flag ? "OK" : "NOT OK");
  1092. ctmp++;
  1093. }
  1094. BIO_printf(out, "\tSuite B: ");
  1095. if (SSL_set_cert_flags(s, 0) & SSL_CERT_FLAG_SUITEB_128_LOS)
  1096. BIO_puts(out, flags & CERT_PKEY_SUITEB ? "OK\n" : "NOT OK\n");
  1097. else
  1098. BIO_printf(out, "not tested\n");
  1099. }
  1100. /*
  1101. * Very basic selection callback: just use any certificate chain reported as
  1102. * valid. More sophisticated could prioritise according to local policy.
  1103. */
  1104. static int set_cert_cb(SSL *ssl, void *arg)
  1105. {
  1106. int i, rv;
  1107. SSL_EXCERT *exc = arg;
  1108. #ifdef CERT_CB_TEST_RETRY
  1109. static int retry_cnt;
  1110. if (retry_cnt < 5) {
  1111. retry_cnt++;
  1112. fprintf(stderr, "Certificate callback retry test: count %d\n",
  1113. retry_cnt);
  1114. return -1;
  1115. }
  1116. #endif
  1117. SSL_certs_clear(ssl);
  1118. if (!exc)
  1119. return 1;
  1120. /*
  1121. * Go to end of list and traverse backwards since we prepend newer
  1122. * entries this retains the original order.
  1123. */
  1124. while (exc->next)
  1125. exc = exc->next;
  1126. i = 0;
  1127. while (exc) {
  1128. i++;
  1129. rv = SSL_check_chain(ssl, exc->cert, exc->key, exc->chain);
  1130. BIO_printf(bio_err, "Checking cert chain %d:\nSubject: ", i);
  1131. X509_NAME_print_ex(bio_err, X509_get_subject_name(exc->cert), 0,
  1132. XN_FLAG_ONELINE);
  1133. BIO_puts(bio_err, "\n");
  1134. print_chain_flags(bio_err, ssl, rv);
  1135. if (rv & CERT_PKEY_VALID) {
  1136. SSL_use_certificate(ssl, exc->cert);
  1137. SSL_use_PrivateKey(ssl, exc->key);
  1138. /*
  1139. * NB: we wouldn't normally do this as it is not efficient
  1140. * building chains on each connection better to cache the chain
  1141. * in advance.
  1142. */
  1143. if (exc->build_chain) {
  1144. if (!SSL_build_cert_chain(ssl, 0))
  1145. return 0;
  1146. } else if (exc->chain)
  1147. SSL_set1_chain(ssl, exc->chain);
  1148. }
  1149. exc = exc->prev;
  1150. }
  1151. return 1;
  1152. }
  1153. void ssl_ctx_set_excert(SSL_CTX *ctx, SSL_EXCERT *exc)
  1154. {
  1155. SSL_CTX_set_cert_cb(ctx, set_cert_cb, exc);
  1156. }
  1157. static int ssl_excert_prepend(SSL_EXCERT **pexc)
  1158. {
  1159. SSL_EXCERT *exc;
  1160. exc = OPENSSL_malloc(sizeof(SSL_EXCERT));
  1161. if (!exc)
  1162. return 0;
  1163. exc->certfile = NULL;
  1164. exc->keyfile = NULL;
  1165. exc->chainfile = NULL;
  1166. exc->cert = NULL;
  1167. exc->key = NULL;
  1168. exc->chain = NULL;
  1169. exc->prev = NULL;
  1170. exc->build_chain = 0;
  1171. exc->next = *pexc;
  1172. *pexc = exc;
  1173. if (exc->next) {
  1174. exc->certform = exc->next->certform;
  1175. exc->keyform = exc->next->keyform;
  1176. exc->next->prev = exc;
  1177. } else {
  1178. exc->certform = FORMAT_PEM;
  1179. exc->keyform = FORMAT_PEM;
  1180. }
  1181. return 1;
  1182. }
  1183. void ssl_excert_free(SSL_EXCERT *exc)
  1184. {
  1185. SSL_EXCERT *curr;
  1186. while (exc) {
  1187. if (exc->cert)
  1188. X509_free(exc->cert);
  1189. if (exc->key)
  1190. EVP_PKEY_free(exc->key);
  1191. if (exc->chain)
  1192. sk_X509_pop_free(exc->chain, X509_free);
  1193. curr = exc;
  1194. exc = exc->next;
  1195. OPENSSL_free(curr);
  1196. }
  1197. }
  1198. int load_excert(SSL_EXCERT **pexc, BIO *err)
  1199. {
  1200. SSL_EXCERT *exc = *pexc;
  1201. if (!exc)
  1202. return 1;
  1203. /* If nothing in list, free and set to NULL */
  1204. if (!exc->certfile && !exc->next) {
  1205. ssl_excert_free(exc);
  1206. *pexc = NULL;
  1207. return 1;
  1208. }
  1209. for (; exc; exc = exc->next) {
  1210. if (!exc->certfile) {
  1211. BIO_printf(err, "Missing filename\n");
  1212. return 0;
  1213. }
  1214. exc->cert = load_cert(err, exc->certfile, exc->certform,
  1215. NULL, NULL, "Server Certificate");
  1216. if (!exc->cert)
  1217. return 0;
  1218. if (exc->keyfile) {
  1219. exc->key = load_key(err, exc->keyfile, exc->keyform,
  1220. 0, NULL, NULL, "Server Key");
  1221. } else {
  1222. exc->key = load_key(err, exc->certfile, exc->certform,
  1223. 0, NULL, NULL, "Server Key");
  1224. }
  1225. if (!exc->key)
  1226. return 0;
  1227. if (exc->chainfile) {
  1228. exc->chain = load_certs(err,
  1229. exc->chainfile, FORMAT_PEM,
  1230. NULL, NULL, "Server Chain");
  1231. if (!exc->chain)
  1232. return 0;
  1233. }
  1234. }
  1235. return 1;
  1236. }
  1237. int args_excert(char ***pargs, int *pargc,
  1238. int *badarg, BIO *err, SSL_EXCERT **pexc)
  1239. {
  1240. char *arg = **pargs, *argn = (*pargs)[1];
  1241. SSL_EXCERT *exc = *pexc;
  1242. int narg = 2;
  1243. if (!exc) {
  1244. if (ssl_excert_prepend(&exc))
  1245. *pexc = exc;
  1246. else {
  1247. BIO_printf(err, "Error initialising xcert\n");
  1248. *badarg = 1;
  1249. goto err;
  1250. }
  1251. }
  1252. if (strcmp(arg, "-xcert") == 0) {
  1253. if (!argn) {
  1254. *badarg = 1;
  1255. return 1;
  1256. }
  1257. if (exc->certfile && !ssl_excert_prepend(&exc)) {
  1258. BIO_printf(err, "Error adding xcert\n");
  1259. *badarg = 1;
  1260. goto err;
  1261. }
  1262. exc->certfile = argn;
  1263. } else if (strcmp(arg, "-xkey") == 0) {
  1264. if (!argn) {
  1265. *badarg = 1;
  1266. return 1;
  1267. }
  1268. if (exc->keyfile) {
  1269. BIO_printf(err, "Key already specified\n");
  1270. *badarg = 1;
  1271. return 1;
  1272. }
  1273. exc->keyfile = argn;
  1274. } else if (strcmp(arg, "-xchain") == 0) {
  1275. if (!argn) {
  1276. *badarg = 1;
  1277. return 1;
  1278. }
  1279. if (exc->chainfile) {
  1280. BIO_printf(err, "Chain already specified\n");
  1281. *badarg = 1;
  1282. return 1;
  1283. }
  1284. exc->chainfile = argn;
  1285. } else if (strcmp(arg, "-xchain_build") == 0) {
  1286. narg = 1;
  1287. exc->build_chain = 1;
  1288. } else if (strcmp(arg, "-xcertform") == 0) {
  1289. if (!argn) {
  1290. *badarg = 1;
  1291. goto err;
  1292. }
  1293. exc->certform = str2fmt(argn);
  1294. } else if (strcmp(arg, "-xkeyform") == 0) {
  1295. if (!argn) {
  1296. *badarg = 1;
  1297. goto err;
  1298. }
  1299. exc->keyform = str2fmt(argn);
  1300. } else
  1301. return 0;
  1302. (*pargs) += narg;
  1303. if (pargc)
  1304. *pargc -= narg;
  1305. *pexc = exc;
  1306. return 1;
  1307. err:
  1308. ERR_print_errors(err);
  1309. ssl_excert_free(exc);
  1310. *pexc = NULL;
  1311. return 1;
  1312. }
  1313. static void print_raw_cipherlist(BIO *bio, SSL *s)
  1314. {
  1315. const unsigned char *rlist;
  1316. static const unsigned char scsv_id[] = { 0, 0, 0xFF };
  1317. size_t i, rlistlen, num;
  1318. if (!SSL_is_server(s))
  1319. return;
  1320. num = SSL_get0_raw_cipherlist(s, NULL);
  1321. rlistlen = SSL_get0_raw_cipherlist(s, &rlist);
  1322. BIO_puts(bio, "Client cipher list: ");
  1323. for (i = 0; i < rlistlen; i += num, rlist += num) {
  1324. const SSL_CIPHER *c = SSL_CIPHER_find(s, rlist);
  1325. if (i)
  1326. BIO_puts(bio, ":");
  1327. if (c)
  1328. BIO_puts(bio, SSL_CIPHER_get_name(c));
  1329. else if (!memcmp(rlist, scsv_id - num + 3, num))
  1330. BIO_puts(bio, "SCSV");
  1331. else {
  1332. size_t j;
  1333. BIO_puts(bio, "0x");
  1334. for (j = 0; j < num; j++)
  1335. BIO_printf(bio, "%02X", rlist[j]);
  1336. }
  1337. }
  1338. BIO_puts(bio, "\n");
  1339. }
  1340. void print_ssl_summary(BIO *bio, SSL *s)
  1341. {
  1342. const SSL_CIPHER *c;
  1343. X509 *peer;
  1344. /*
  1345. * const char *pnam = SSL_is_server(s) ? "client" : "server";
  1346. */
  1347. BIO_printf(bio, "Protocol version: %s\n", SSL_get_version(s));
  1348. print_raw_cipherlist(bio, s);
  1349. c = SSL_get_current_cipher(s);
  1350. BIO_printf(bio, "Ciphersuite: %s\n", SSL_CIPHER_get_name(c));
  1351. do_print_sigalgs(bio, s, 0);
  1352. peer = SSL_get_peer_certificate(s);
  1353. if (peer) {
  1354. int nid;
  1355. BIO_puts(bio, "Peer certificate: ");
  1356. X509_NAME_print_ex(bio, X509_get_subject_name(peer),
  1357. 0, XN_FLAG_ONELINE);
  1358. BIO_puts(bio, "\n");
  1359. if (SSL_get_peer_signature_nid(s, &nid))
  1360. BIO_printf(bio, "Hash used: %s\n", OBJ_nid2sn(nid));
  1361. } else
  1362. BIO_puts(bio, "No peer certificate\n");
  1363. if (peer)
  1364. X509_free(peer);
  1365. #ifndef OPENSSL_NO_EC
  1366. ssl_print_point_formats(bio, s);
  1367. if (SSL_is_server(s))
  1368. ssl_print_curves(bio, s, 1);
  1369. else
  1370. ssl_print_tmp_key(bio, s);
  1371. #else
  1372. if (!SSL_is_server(s))
  1373. ssl_print_tmp_key(bio, s);
  1374. #endif
  1375. }
  1376. int args_ssl(char ***pargs, int *pargc, SSL_CONF_CTX *cctx,
  1377. int *badarg, BIO *err, STACK_OF(OPENSSL_STRING) **pstr,
  1378. int *no_prot_opt)
  1379. {
  1380. char *arg = **pargs, *argn = (*pargs)[1];
  1381. int rv;
  1382. if (strcmp(arg, "-no_ssl2") == 0 || strcmp(arg, "-no_ssl3") == 0
  1383. || strcmp(arg, "-no_tls1") == 0 || strcmp(arg, "-no_tls1_1") == 0
  1384. || strcmp(arg, "-no_tls1_2") == 0) {
  1385. *no_prot_opt = 1;
  1386. }
  1387. /* Attempt to run SSL configuration command */
  1388. rv = SSL_CONF_cmd_argv(cctx, pargc, pargs);
  1389. /* If parameter not recognised just return */
  1390. if (rv == 0)
  1391. return 0;
  1392. /* see if missing argument error */
  1393. if (rv == -3) {
  1394. BIO_printf(err, "%s needs an argument\n", arg);
  1395. *badarg = 1;
  1396. goto end;
  1397. }
  1398. /* Check for some other error */
  1399. if (rv < 0) {
  1400. BIO_printf(err, "Error with command: \"%s %s\"\n",
  1401. arg, argn ? argn : "");
  1402. *badarg = 1;
  1403. goto end;
  1404. }
  1405. /* Store command and argument */
  1406. /* If only one argument processed store value as NULL */
  1407. if (rv == 1)
  1408. argn = NULL;
  1409. if (!*pstr)
  1410. *pstr = sk_OPENSSL_STRING_new_null();
  1411. if (!*pstr || !sk_OPENSSL_STRING_push(*pstr, arg) ||
  1412. !sk_OPENSSL_STRING_push(*pstr, argn)) {
  1413. BIO_puts(err, "Memory allocation failure\n");
  1414. goto end;
  1415. }
  1416. end:
  1417. if (*badarg)
  1418. ERR_print_errors(err);
  1419. return 1;
  1420. }
  1421. int args_ssl_call(SSL_CTX *ctx, BIO *err, SSL_CONF_CTX *cctx,
  1422. STACK_OF(OPENSSL_STRING) *str, int no_ecdhe, int no_jpake)
  1423. {
  1424. int i;
  1425. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  1426. for (i = 0; i < sk_OPENSSL_STRING_num(str); i += 2) {
  1427. const char *param = sk_OPENSSL_STRING_value(str, i);
  1428. const char *value = sk_OPENSSL_STRING_value(str, i + 1);
  1429. /*
  1430. * If no_ecdhe or named curve already specified don't need a default.
  1431. */
  1432. if (!no_ecdhe && !strcmp(param, "-named_curve"))
  1433. no_ecdhe = 1;
  1434. #ifndef OPENSSL_NO_JPAKE
  1435. if (!no_jpake && !strcmp(param, "-cipher")) {
  1436. BIO_puts(err, "JPAKE sets cipher to PSK\n");
  1437. return 0;
  1438. }
  1439. #endif
  1440. if (SSL_CONF_cmd(cctx, param, value) <= 0) {
  1441. BIO_printf(err, "Error with command: \"%s %s\"\n",
  1442. param, value ? value : "");
  1443. ERR_print_errors(err);
  1444. return 0;
  1445. }
  1446. }
  1447. /*
  1448. * This is a special case to keep existing s_server functionality: if we
  1449. * don't have any curve specified *and* we haven't disabled ECDHE then
  1450. * use P-256.
  1451. */
  1452. if (!no_ecdhe) {
  1453. if (SSL_CONF_cmd(cctx, "-named_curve", "P-256") <= 0) {
  1454. BIO_puts(err, "Error setting EC curve\n");
  1455. ERR_print_errors(err);
  1456. return 0;
  1457. }
  1458. }
  1459. #ifndef OPENSSL_NO_JPAKE
  1460. if (!no_jpake) {
  1461. if (SSL_CONF_cmd(cctx, "-cipher", "PSK") <= 0) {
  1462. BIO_puts(err, "Error setting cipher to PSK\n");
  1463. ERR_print_errors(err);
  1464. return 0;
  1465. }
  1466. }
  1467. #endif
  1468. if (!SSL_CONF_CTX_finish(cctx)) {
  1469. BIO_puts(err, "Error finishing context\n");
  1470. ERR_print_errors(err);
  1471. return 0;
  1472. }
  1473. return 1;
  1474. }
  1475. static int add_crls_store(X509_STORE *st, STACK_OF(X509_CRL) *crls)
  1476. {
  1477. X509_CRL *crl;
  1478. int i;
  1479. for (i = 0; i < sk_X509_CRL_num(crls); i++) {
  1480. crl = sk_X509_CRL_value(crls, i);
  1481. X509_STORE_add_crl(st, crl);
  1482. }
  1483. return 1;
  1484. }
  1485. int ssl_ctx_add_crls(SSL_CTX *ctx, STACK_OF(X509_CRL) *crls, int crl_download)
  1486. {
  1487. X509_STORE *st;
  1488. st = SSL_CTX_get_cert_store(ctx);
  1489. add_crls_store(st, crls);
  1490. if (crl_download)
  1491. store_setup_crl_download(st);
  1492. return 1;
  1493. }
  1494. int ssl_load_stores(SSL_CTX *ctx,
  1495. const char *vfyCApath, const char *vfyCAfile,
  1496. const char *chCApath, const char *chCAfile,
  1497. STACK_OF(X509_CRL) *crls, int crl_download)
  1498. {
  1499. X509_STORE *vfy = NULL, *ch = NULL;
  1500. int rv = 0;
  1501. if (vfyCApath || vfyCAfile) {
  1502. vfy = X509_STORE_new();
  1503. if (!X509_STORE_load_locations(vfy, vfyCAfile, vfyCApath))
  1504. goto err;
  1505. add_crls_store(vfy, crls);
  1506. SSL_CTX_set1_verify_cert_store(ctx, vfy);
  1507. if (crl_download)
  1508. store_setup_crl_download(vfy);
  1509. }
  1510. if (chCApath || chCAfile) {
  1511. ch = X509_STORE_new();
  1512. if (!X509_STORE_load_locations(ch, chCAfile, chCApath))
  1513. goto err;
  1514. SSL_CTX_set1_chain_cert_store(ctx, ch);
  1515. }
  1516. rv = 1;
  1517. err:
  1518. if (vfy)
  1519. X509_STORE_free(vfy);
  1520. if (ch)
  1521. X509_STORE_free(ch);
  1522. return rv;
  1523. }