s_cb.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /* callback functions used by s_client, s_server, and s_time */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h> /* for memcpy() and strcmp() */
  13. #include "apps.h"
  14. #include <openssl/err.h>
  15. #include <openssl/rand.h>
  16. #include <openssl/x509.h>
  17. #include <openssl/ssl.h>
  18. #include <openssl/bn.h>
  19. #ifndef OPENSSL_NO_DH
  20. # include <openssl/dh.h>
  21. #endif
  22. #include "s_apps.h"
  23. #define COOKIE_SECRET_LENGTH 16
  24. VERIFY_CB_ARGS verify_args = { 0, 0, X509_V_OK, 0 };
  25. #ifndef OPENSSL_NO_SOCK
  26. static unsigned char cookie_secret[COOKIE_SECRET_LENGTH];
  27. static int cookie_initialized = 0;
  28. #endif
  29. static BIO *bio_keylog = NULL;
  30. static const char *lookup(int val, const STRINT_PAIR* list, const char* def)
  31. {
  32. for ( ; list->name; ++list)
  33. if (list->retval == val)
  34. return list->name;
  35. return def;
  36. }
  37. int verify_callback(int ok, X509_STORE_CTX *ctx)
  38. {
  39. X509 *err_cert;
  40. int err, depth;
  41. err_cert = X509_STORE_CTX_get_current_cert(ctx);
  42. err = X509_STORE_CTX_get_error(ctx);
  43. depth = X509_STORE_CTX_get_error_depth(ctx);
  44. if (!verify_args.quiet || !ok) {
  45. BIO_printf(bio_err, "depth=%d ", depth);
  46. if (err_cert != NULL) {
  47. X509_NAME_print_ex(bio_err,
  48. X509_get_subject_name(err_cert),
  49. 0, get_nameopt());
  50. BIO_puts(bio_err, "\n");
  51. } else {
  52. BIO_puts(bio_err, "<no cert>\n");
  53. }
  54. }
  55. if (!ok) {
  56. BIO_printf(bio_err, "verify error:num=%d:%s\n", err,
  57. X509_verify_cert_error_string(err));
  58. if (verify_args.depth >= depth) {
  59. if (!verify_args.return_error)
  60. ok = 1;
  61. verify_args.error = err;
  62. } else {
  63. ok = 0;
  64. verify_args.error = X509_V_ERR_CERT_CHAIN_TOO_LONG;
  65. }
  66. }
  67. switch (err) {
  68. case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
  69. BIO_puts(bio_err, "issuer= ");
  70. X509_NAME_print_ex(bio_err, X509_get_issuer_name(err_cert),
  71. 0, get_nameopt());
  72. BIO_puts(bio_err, "\n");
  73. break;
  74. case X509_V_ERR_CERT_NOT_YET_VALID:
  75. case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
  76. BIO_printf(bio_err, "notBefore=");
  77. ASN1_TIME_print(bio_err, X509_get0_notBefore(err_cert));
  78. BIO_printf(bio_err, "\n");
  79. break;
  80. case X509_V_ERR_CERT_HAS_EXPIRED:
  81. case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
  82. BIO_printf(bio_err, "notAfter=");
  83. ASN1_TIME_print(bio_err, X509_get0_notAfter(err_cert));
  84. BIO_printf(bio_err, "\n");
  85. break;
  86. case X509_V_ERR_NO_EXPLICIT_POLICY:
  87. if (!verify_args.quiet)
  88. policies_print(ctx);
  89. break;
  90. }
  91. if (err == X509_V_OK && ok == 2 && !verify_args.quiet)
  92. policies_print(ctx);
  93. if (ok && !verify_args.quiet)
  94. BIO_printf(bio_err, "verify return:%d\n", ok);
  95. return ok;
  96. }
  97. int set_cert_stuff(SSL_CTX *ctx, char *cert_file, char *key_file)
  98. {
  99. if (cert_file != NULL) {
  100. if (SSL_CTX_use_certificate_file(ctx, cert_file,
  101. SSL_FILETYPE_PEM) <= 0) {
  102. BIO_printf(bio_err, "unable to get certificate from '%s'\n",
  103. cert_file);
  104. ERR_print_errors(bio_err);
  105. return 0;
  106. }
  107. if (key_file == NULL)
  108. key_file = cert_file;
  109. if (SSL_CTX_use_PrivateKey_file(ctx, key_file, SSL_FILETYPE_PEM) <= 0) {
  110. BIO_printf(bio_err, "unable to get private key from '%s'\n",
  111. key_file);
  112. ERR_print_errors(bio_err);
  113. return 0;
  114. }
  115. /*
  116. * If we are using DSA, we can copy the parameters from the private
  117. * key
  118. */
  119. /*
  120. * Now we know that a key and cert have been set against the SSL
  121. * context
  122. */
  123. if (!SSL_CTX_check_private_key(ctx)) {
  124. BIO_printf(bio_err,
  125. "Private key does not match the certificate public key\n");
  126. return 0;
  127. }
  128. }
  129. return 1;
  130. }
  131. int set_cert_key_stuff(SSL_CTX *ctx, X509 *cert, EVP_PKEY *key,
  132. STACK_OF(X509) *chain, int build_chain)
  133. {
  134. int chflags = chain ? SSL_BUILD_CHAIN_FLAG_CHECK : 0;
  135. if (cert == NULL)
  136. return 1;
  137. if (SSL_CTX_use_certificate(ctx, cert) <= 0) {
  138. BIO_printf(bio_err, "error setting certificate\n");
  139. ERR_print_errors(bio_err);
  140. return 0;
  141. }
  142. if (SSL_CTX_use_PrivateKey(ctx, key) <= 0) {
  143. BIO_printf(bio_err, "error setting private key\n");
  144. ERR_print_errors(bio_err);
  145. return 0;
  146. }
  147. /*
  148. * Now we know that a key and cert have been set against the SSL context
  149. */
  150. if (!SSL_CTX_check_private_key(ctx)) {
  151. BIO_printf(bio_err,
  152. "Private key does not match the certificate public key\n");
  153. return 0;
  154. }
  155. if (chain && !SSL_CTX_set1_chain(ctx, chain)) {
  156. BIO_printf(bio_err, "error setting certificate chain\n");
  157. ERR_print_errors(bio_err);
  158. return 0;
  159. }
  160. if (build_chain && !SSL_CTX_build_cert_chain(ctx, chflags)) {
  161. BIO_printf(bio_err, "error building certificate chain\n");
  162. ERR_print_errors(bio_err);
  163. return 0;
  164. }
  165. return 1;
  166. }
  167. static STRINT_PAIR cert_type_list[] = {
  168. {"RSA sign", TLS_CT_RSA_SIGN},
  169. {"DSA sign", TLS_CT_DSS_SIGN},
  170. {"RSA fixed DH", TLS_CT_RSA_FIXED_DH},
  171. {"DSS fixed DH", TLS_CT_DSS_FIXED_DH},
  172. {"ECDSA sign", TLS_CT_ECDSA_SIGN},
  173. {"RSA fixed ECDH", TLS_CT_RSA_FIXED_ECDH},
  174. {"ECDSA fixed ECDH", TLS_CT_ECDSA_FIXED_ECDH},
  175. {"GOST01 Sign", TLS_CT_GOST01_SIGN},
  176. {NULL}
  177. };
  178. static void ssl_print_client_cert_types(BIO *bio, SSL *s)
  179. {
  180. const unsigned char *p;
  181. int i;
  182. int cert_type_num = SSL_get0_certificate_types(s, &p);
  183. if (!cert_type_num)
  184. return;
  185. BIO_puts(bio, "Client Certificate Types: ");
  186. for (i = 0; i < cert_type_num; i++) {
  187. unsigned char cert_type = p[i];
  188. const char *cname = lookup((int)cert_type, cert_type_list, NULL);
  189. if (i)
  190. BIO_puts(bio, ", ");
  191. if (cname != NULL)
  192. BIO_puts(bio, cname);
  193. else
  194. BIO_printf(bio, "UNKNOWN (%d),", cert_type);
  195. }
  196. BIO_puts(bio, "\n");
  197. }
  198. static const char *get_sigtype(int nid)
  199. {
  200. switch (nid) {
  201. case EVP_PKEY_RSA:
  202. return "RSA";
  203. case EVP_PKEY_RSA_PSS:
  204. return "RSA-PSS";
  205. case EVP_PKEY_DSA:
  206. return "DSA";
  207. case EVP_PKEY_EC:
  208. return "ECDSA";
  209. case NID_ED25519:
  210. return "Ed25519";
  211. case NID_ED448:
  212. return "Ed448";
  213. default:
  214. return NULL;
  215. }
  216. }
  217. static int do_print_sigalgs(BIO *out, SSL *s, int shared)
  218. {
  219. int i, nsig, client;
  220. client = SSL_is_server(s) ? 0 : 1;
  221. if (shared)
  222. nsig = SSL_get_shared_sigalgs(s, 0, NULL, NULL, NULL, NULL, NULL);
  223. else
  224. nsig = SSL_get_sigalgs(s, -1, NULL, NULL, NULL, NULL, NULL);
  225. if (nsig == 0)
  226. return 1;
  227. if (shared)
  228. BIO_puts(out, "Shared ");
  229. if (client)
  230. BIO_puts(out, "Requested ");
  231. BIO_puts(out, "Signature Algorithms: ");
  232. for (i = 0; i < nsig; i++) {
  233. int hash_nid, sign_nid;
  234. unsigned char rhash, rsign;
  235. const char *sstr = NULL;
  236. if (shared)
  237. SSL_get_shared_sigalgs(s, i, &sign_nid, &hash_nid, NULL,
  238. &rsign, &rhash);
  239. else
  240. SSL_get_sigalgs(s, i, &sign_nid, &hash_nid, NULL, &rsign, &rhash);
  241. if (i)
  242. BIO_puts(out, ":");
  243. sstr = get_sigtype(sign_nid);
  244. if (sstr)
  245. BIO_printf(out, "%s", sstr);
  246. else
  247. BIO_printf(out, "0x%02X", (int)rsign);
  248. if (hash_nid != NID_undef)
  249. BIO_printf(out, "+%s", OBJ_nid2sn(hash_nid));
  250. else if (sstr == NULL)
  251. BIO_printf(out, "+0x%02X", (int)rhash);
  252. }
  253. BIO_puts(out, "\n");
  254. return 1;
  255. }
  256. int ssl_print_sigalgs(BIO *out, SSL *s)
  257. {
  258. int nid;
  259. if (!SSL_is_server(s))
  260. ssl_print_client_cert_types(out, s);
  261. do_print_sigalgs(out, s, 0);
  262. do_print_sigalgs(out, s, 1);
  263. if (SSL_get_peer_signature_nid(s, &nid) && nid != NID_undef)
  264. BIO_printf(out, "Peer signing digest: %s\n", OBJ_nid2sn(nid));
  265. if (SSL_get_peer_signature_type_nid(s, &nid))
  266. BIO_printf(out, "Peer signature type: %s\n", get_sigtype(nid));
  267. return 1;
  268. }
  269. #ifndef OPENSSL_NO_EC
  270. int ssl_print_point_formats(BIO *out, SSL *s)
  271. {
  272. int i, nformats;
  273. const char *pformats;
  274. nformats = SSL_get0_ec_point_formats(s, &pformats);
  275. if (nformats <= 0)
  276. return 1;
  277. BIO_puts(out, "Supported Elliptic Curve Point Formats: ");
  278. for (i = 0; i < nformats; i++, pformats++) {
  279. if (i)
  280. BIO_puts(out, ":");
  281. switch (*pformats) {
  282. case TLSEXT_ECPOINTFORMAT_uncompressed:
  283. BIO_puts(out, "uncompressed");
  284. break;
  285. case TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime:
  286. BIO_puts(out, "ansiX962_compressed_prime");
  287. break;
  288. case TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2:
  289. BIO_puts(out, "ansiX962_compressed_char2");
  290. break;
  291. default:
  292. BIO_printf(out, "unknown(%d)", (int)*pformats);
  293. break;
  294. }
  295. }
  296. BIO_puts(out, "\n");
  297. return 1;
  298. }
  299. int ssl_print_groups(BIO *out, SSL *s, int noshared)
  300. {
  301. int i, ngroups, *groups, nid;
  302. const char *gname;
  303. ngroups = SSL_get1_groups(s, NULL);
  304. if (ngroups <= 0)
  305. return 1;
  306. groups = app_malloc(ngroups * sizeof(int), "groups to print");
  307. SSL_get1_groups(s, groups);
  308. BIO_puts(out, "Supported Elliptic Groups: ");
  309. for (i = 0; i < ngroups; i++) {
  310. if (i)
  311. BIO_puts(out, ":");
  312. nid = groups[i];
  313. /* If unrecognised print out hex version */
  314. if (nid & TLSEXT_nid_unknown) {
  315. BIO_printf(out, "0x%04X", nid & 0xFFFF);
  316. } else {
  317. /* TODO(TLS1.3): Get group name here */
  318. /* Use NIST name for curve if it exists */
  319. gname = EC_curve_nid2nist(nid);
  320. if (gname == NULL)
  321. gname = OBJ_nid2sn(nid);
  322. BIO_printf(out, "%s", gname);
  323. }
  324. }
  325. OPENSSL_free(groups);
  326. if (noshared) {
  327. BIO_puts(out, "\n");
  328. return 1;
  329. }
  330. BIO_puts(out, "\nShared Elliptic groups: ");
  331. ngroups = SSL_get_shared_group(s, -1);
  332. for (i = 0; i < ngroups; i++) {
  333. if (i)
  334. BIO_puts(out, ":");
  335. nid = SSL_get_shared_group(s, i);
  336. /* TODO(TLS1.3): Convert for DH groups */
  337. gname = EC_curve_nid2nist(nid);
  338. if (gname == NULL)
  339. gname = OBJ_nid2sn(nid);
  340. BIO_printf(out, "%s", gname);
  341. }
  342. if (ngroups == 0)
  343. BIO_puts(out, "NONE");
  344. BIO_puts(out, "\n");
  345. return 1;
  346. }
  347. #endif
  348. int ssl_print_tmp_key(BIO *out, SSL *s)
  349. {
  350. EVP_PKEY *key;
  351. if (!SSL_get_server_tmp_key(s, &key))
  352. return 1;
  353. BIO_puts(out, "Server Temp Key: ");
  354. switch (EVP_PKEY_id(key)) {
  355. case EVP_PKEY_RSA:
  356. BIO_printf(out, "RSA, %d bits\n", EVP_PKEY_bits(key));
  357. break;
  358. case EVP_PKEY_DH:
  359. BIO_printf(out, "DH, %d bits\n", EVP_PKEY_bits(key));
  360. break;
  361. #ifndef OPENSSL_NO_EC
  362. case EVP_PKEY_EC:
  363. {
  364. EC_KEY *ec = EVP_PKEY_get1_EC_KEY(key);
  365. int nid;
  366. const char *cname;
  367. nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
  368. EC_KEY_free(ec);
  369. cname = EC_curve_nid2nist(nid);
  370. if (cname == NULL)
  371. cname = OBJ_nid2sn(nid);
  372. BIO_printf(out, "ECDH, %s, %d bits\n", cname, EVP_PKEY_bits(key));
  373. }
  374. break;
  375. #endif
  376. default:
  377. BIO_printf(out, "%s, %d bits\n", OBJ_nid2sn(EVP_PKEY_id(key)),
  378. EVP_PKEY_bits(key));
  379. }
  380. EVP_PKEY_free(key);
  381. return 1;
  382. }
  383. long bio_dump_callback(BIO *bio, int cmd, const char *argp,
  384. int argi, long argl, long ret)
  385. {
  386. BIO *out;
  387. out = (BIO *)BIO_get_callback_arg(bio);
  388. if (out == NULL)
  389. return ret;
  390. if (cmd == (BIO_CB_READ | BIO_CB_RETURN)) {
  391. BIO_printf(out, "read from %p [%p] (%lu bytes => %ld (0x%lX))\n",
  392. (void *)bio, (void *)argp, (unsigned long)argi, ret, ret);
  393. BIO_dump(out, argp, (int)ret);
  394. return ret;
  395. } else if (cmd == (BIO_CB_WRITE | BIO_CB_RETURN)) {
  396. BIO_printf(out, "write to %p [%p] (%lu bytes => %ld (0x%lX))\n",
  397. (void *)bio, (void *)argp, (unsigned long)argi, ret, ret);
  398. BIO_dump(out, argp, (int)ret);
  399. }
  400. return ret;
  401. }
  402. void apps_ssl_info_callback(const SSL *s, int where, int ret)
  403. {
  404. const char *str;
  405. int w;
  406. w = where & ~SSL_ST_MASK;
  407. if (w & SSL_ST_CONNECT)
  408. str = "SSL_connect";
  409. else if (w & SSL_ST_ACCEPT)
  410. str = "SSL_accept";
  411. else
  412. str = "undefined";
  413. if (where & SSL_CB_LOOP) {
  414. BIO_printf(bio_err, "%s:%s\n", str, SSL_state_string_long(s));
  415. } else if (where & SSL_CB_ALERT) {
  416. str = (where & SSL_CB_READ) ? "read" : "write";
  417. BIO_printf(bio_err, "SSL3 alert %s:%s:%s\n",
  418. str,
  419. SSL_alert_type_string_long(ret),
  420. SSL_alert_desc_string_long(ret));
  421. } else if (where & SSL_CB_EXIT) {
  422. if (ret == 0)
  423. BIO_printf(bio_err, "%s:failed in %s\n",
  424. str, SSL_state_string_long(s));
  425. else if (ret < 0)
  426. BIO_printf(bio_err, "%s:error in %s\n",
  427. str, SSL_state_string_long(s));
  428. }
  429. }
  430. static STRINT_PAIR ssl_versions[] = {
  431. {"SSL 3.0", SSL3_VERSION},
  432. {"TLS 1.0", TLS1_VERSION},
  433. {"TLS 1.1", TLS1_1_VERSION},
  434. {"TLS 1.2", TLS1_2_VERSION},
  435. {"TLS 1.3", TLS1_3_VERSION},
  436. {"DTLS 1.0", DTLS1_VERSION},
  437. {"DTLS 1.0 (bad)", DTLS1_BAD_VER},
  438. {NULL}
  439. };
  440. static STRINT_PAIR alert_types[] = {
  441. {" close_notify", 0},
  442. {" end_of_early_data", 1},
  443. {" unexpected_message", 10},
  444. {" bad_record_mac", 20},
  445. {" decryption_failed", 21},
  446. {" record_overflow", 22},
  447. {" decompression_failure", 30},
  448. {" handshake_failure", 40},
  449. {" bad_certificate", 42},
  450. {" unsupported_certificate", 43},
  451. {" certificate_revoked", 44},
  452. {" certificate_expired", 45},
  453. {" certificate_unknown", 46},
  454. {" illegal_parameter", 47},
  455. {" unknown_ca", 48},
  456. {" access_denied", 49},
  457. {" decode_error", 50},
  458. {" decrypt_error", 51},
  459. {" export_restriction", 60},
  460. {" protocol_version", 70},
  461. {" insufficient_security", 71},
  462. {" internal_error", 80},
  463. {" inappropriate_fallback", 86},
  464. {" user_canceled", 90},
  465. {" no_renegotiation", 100},
  466. {" missing_extension", 109},
  467. {" unsupported_extension", 110},
  468. {" certificate_unobtainable", 111},
  469. {" unrecognized_name", 112},
  470. {" bad_certificate_status_response", 113},
  471. {" bad_certificate_hash_value", 114},
  472. {" unknown_psk_identity", 115},
  473. {" certificate_required", 116},
  474. {NULL}
  475. };
  476. static STRINT_PAIR handshakes[] = {
  477. {", HelloRequest", SSL3_MT_HELLO_REQUEST},
  478. {", ClientHello", SSL3_MT_CLIENT_HELLO},
  479. {", ServerHello", SSL3_MT_SERVER_HELLO},
  480. {", HelloVerifyRequest", DTLS1_MT_HELLO_VERIFY_REQUEST},
  481. {", NewSessionTicket", SSL3_MT_NEWSESSION_TICKET},
  482. {", EndOfEarlyData", SSL3_MT_END_OF_EARLY_DATA},
  483. {", EncryptedExtensions", SSL3_MT_ENCRYPTED_EXTENSIONS},
  484. {", Certificate", SSL3_MT_CERTIFICATE},
  485. {", ServerKeyExchange", SSL3_MT_SERVER_KEY_EXCHANGE},
  486. {", CertificateRequest", SSL3_MT_CERTIFICATE_REQUEST},
  487. {", ServerHelloDone", SSL3_MT_SERVER_DONE},
  488. {", CertificateVerify", SSL3_MT_CERTIFICATE_VERIFY},
  489. {", ClientKeyExchange", SSL3_MT_CLIENT_KEY_EXCHANGE},
  490. {", Finished", SSL3_MT_FINISHED},
  491. {", CertificateUrl", SSL3_MT_CERTIFICATE_URL},
  492. {", CertificateStatus", SSL3_MT_CERTIFICATE_STATUS},
  493. {", SupplementalData", SSL3_MT_SUPPLEMENTAL_DATA},
  494. {", KeyUpdate", SSL3_MT_KEY_UPDATE},
  495. #ifndef OPENSSL_NO_NEXTPROTONEG
  496. {", NextProto", SSL3_MT_NEXT_PROTO},
  497. #endif
  498. {", MessageHash", SSL3_MT_MESSAGE_HASH},
  499. {NULL}
  500. };
  501. void msg_cb(int write_p, int version, int content_type, const void *buf,
  502. size_t len, SSL *ssl, void *arg)
  503. {
  504. BIO *bio = arg;
  505. const char *str_write_p = write_p ? ">>>" : "<<<";
  506. const char *str_version = lookup(version, ssl_versions, "???");
  507. const char *str_content_type = "", *str_details1 = "", *str_details2 = "";
  508. const unsigned char* bp = buf;
  509. if (version == SSL3_VERSION ||
  510. version == TLS1_VERSION ||
  511. version == TLS1_1_VERSION ||
  512. version == TLS1_2_VERSION ||
  513. version == TLS1_3_VERSION ||
  514. version == DTLS1_VERSION || version == DTLS1_BAD_VER) {
  515. switch (content_type) {
  516. case 20:
  517. str_content_type = ", ChangeCipherSpec";
  518. break;
  519. case 21:
  520. str_content_type = ", Alert";
  521. str_details1 = ", ???";
  522. if (len == 2) {
  523. switch (bp[0]) {
  524. case 1:
  525. str_details1 = ", warning";
  526. break;
  527. case 2:
  528. str_details1 = ", fatal";
  529. break;
  530. }
  531. str_details2 = lookup((int)bp[1], alert_types, " ???");
  532. }
  533. break;
  534. case 22:
  535. str_content_type = ", Handshake";
  536. str_details1 = "???";
  537. if (len > 0)
  538. str_details1 = lookup((int)bp[0], handshakes, "???");
  539. break;
  540. case 23:
  541. str_content_type = ", ApplicationData";
  542. break;
  543. #ifndef OPENSSL_NO_HEARTBEATS
  544. case 24:
  545. str_details1 = ", Heartbeat";
  546. if (len > 0) {
  547. switch (bp[0]) {
  548. case 1:
  549. str_details1 = ", HeartbeatRequest";
  550. break;
  551. case 2:
  552. str_details1 = ", HeartbeatResponse";
  553. break;
  554. }
  555. }
  556. break;
  557. #endif
  558. }
  559. }
  560. BIO_printf(bio, "%s %s%s [length %04lx]%s%s\n", str_write_p, str_version,
  561. str_content_type, (unsigned long)len, str_details1,
  562. str_details2);
  563. if (len > 0) {
  564. size_t num, i;
  565. BIO_printf(bio, " ");
  566. num = len;
  567. for (i = 0; i < num; i++) {
  568. if (i % 16 == 0 && i > 0)
  569. BIO_printf(bio, "\n ");
  570. BIO_printf(bio, " %02x", ((const unsigned char *)buf)[i]);
  571. }
  572. if (i < len)
  573. BIO_printf(bio, " ...");
  574. BIO_printf(bio, "\n");
  575. }
  576. (void)BIO_flush(bio);
  577. }
  578. static STRINT_PAIR tlsext_types[] = {
  579. {"server name", TLSEXT_TYPE_server_name},
  580. {"max fragment length", TLSEXT_TYPE_max_fragment_length},
  581. {"client certificate URL", TLSEXT_TYPE_client_certificate_url},
  582. {"trusted CA keys", TLSEXT_TYPE_trusted_ca_keys},
  583. {"truncated HMAC", TLSEXT_TYPE_truncated_hmac},
  584. {"status request", TLSEXT_TYPE_status_request},
  585. {"user mapping", TLSEXT_TYPE_user_mapping},
  586. {"client authz", TLSEXT_TYPE_client_authz},
  587. {"server authz", TLSEXT_TYPE_server_authz},
  588. {"cert type", TLSEXT_TYPE_cert_type},
  589. {"supported_groups", TLSEXT_TYPE_supported_groups},
  590. {"EC point formats", TLSEXT_TYPE_ec_point_formats},
  591. {"SRP", TLSEXT_TYPE_srp},
  592. {"signature algorithms", TLSEXT_TYPE_signature_algorithms},
  593. {"use SRTP", TLSEXT_TYPE_use_srtp},
  594. {"heartbeat", TLSEXT_TYPE_heartbeat},
  595. {"session ticket", TLSEXT_TYPE_session_ticket},
  596. {"renegotiation info", TLSEXT_TYPE_renegotiate},
  597. {"signed certificate timestamps", TLSEXT_TYPE_signed_certificate_timestamp},
  598. {"TLS padding", TLSEXT_TYPE_padding},
  599. #ifdef TLSEXT_TYPE_next_proto_neg
  600. {"next protocol", TLSEXT_TYPE_next_proto_neg},
  601. #endif
  602. #ifdef TLSEXT_TYPE_encrypt_then_mac
  603. {"encrypt-then-mac", TLSEXT_TYPE_encrypt_then_mac},
  604. #endif
  605. #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
  606. {"application layer protocol negotiation",
  607. TLSEXT_TYPE_application_layer_protocol_negotiation},
  608. #endif
  609. #ifdef TLSEXT_TYPE_extended_master_secret
  610. {"extended master secret", TLSEXT_TYPE_extended_master_secret},
  611. #endif
  612. {"key share", TLSEXT_TYPE_key_share},
  613. {"supported versions", TLSEXT_TYPE_supported_versions},
  614. {"psk", TLSEXT_TYPE_psk},
  615. {"psk kex modes", TLSEXT_TYPE_psk_kex_modes},
  616. {"certificate authorities", TLSEXT_TYPE_certificate_authorities},
  617. {"post handshake auth", TLSEXT_TYPE_post_handshake_auth},
  618. {NULL}
  619. };
  620. void tlsext_cb(SSL *s, int client_server, int type,
  621. const unsigned char *data, int len, void *arg)
  622. {
  623. BIO *bio = arg;
  624. const char *extname = lookup(type, tlsext_types, "unknown");
  625. BIO_printf(bio, "TLS %s extension \"%s\" (id=%d), len=%d\n",
  626. client_server ? "server" : "client", extname, type, len);
  627. BIO_dump(bio, (const char *)data, len);
  628. (void)BIO_flush(bio);
  629. }
  630. #ifndef OPENSSL_NO_SOCK
  631. int generate_cookie_callback(SSL *ssl, unsigned char *cookie,
  632. unsigned int *cookie_len)
  633. {
  634. unsigned char *buffer;
  635. size_t length = 0;
  636. unsigned short port;
  637. BIO_ADDR *lpeer = NULL, *peer = NULL;
  638. /* Initialize a random secret */
  639. if (!cookie_initialized) {
  640. if (RAND_bytes(cookie_secret, COOKIE_SECRET_LENGTH) <= 0) {
  641. BIO_printf(bio_err, "error setting random cookie secret\n");
  642. return 0;
  643. }
  644. cookie_initialized = 1;
  645. }
  646. if (SSL_is_dtls(ssl)) {
  647. lpeer = peer = BIO_ADDR_new();
  648. if (peer == NULL) {
  649. BIO_printf(bio_err, "memory full\n");
  650. return 0;
  651. }
  652. /* Read peer information */
  653. (void)BIO_dgram_get_peer(SSL_get_rbio(ssl), peer);
  654. } else {
  655. peer = ourpeer;
  656. }
  657. /* Create buffer with peer's address and port */
  658. if (!BIO_ADDR_rawaddress(peer, NULL, &length)) {
  659. BIO_printf(bio_err, "Failed getting peer address\n");
  660. return 0;
  661. }
  662. OPENSSL_assert(length != 0);
  663. port = BIO_ADDR_rawport(peer);
  664. length += sizeof(port);
  665. buffer = app_malloc(length, "cookie generate buffer");
  666. memcpy(buffer, &port, sizeof(port));
  667. BIO_ADDR_rawaddress(peer, buffer + sizeof(port), NULL);
  668. /* Calculate HMAC of buffer using the secret */
  669. HMAC(EVP_sha1(), cookie_secret, COOKIE_SECRET_LENGTH,
  670. buffer, length, cookie, cookie_len);
  671. OPENSSL_free(buffer);
  672. BIO_ADDR_free(lpeer);
  673. return 1;
  674. }
  675. int verify_cookie_callback(SSL *ssl, const unsigned char *cookie,
  676. unsigned int cookie_len)
  677. {
  678. unsigned char result[EVP_MAX_MD_SIZE];
  679. unsigned int resultlength;
  680. /* Note: we check cookie_initialized because if it's not,
  681. * it cannot be valid */
  682. if (cookie_initialized
  683. && generate_cookie_callback(ssl, result, &resultlength)
  684. && cookie_len == resultlength
  685. && memcmp(result, cookie, resultlength) == 0)
  686. return 1;
  687. return 0;
  688. }
  689. int generate_stateless_cookie_callback(SSL *ssl, unsigned char *cookie,
  690. size_t *cookie_len)
  691. {
  692. unsigned int temp;
  693. int res = generate_cookie_callback(ssl, cookie, &temp);
  694. *cookie_len = temp;
  695. return res;
  696. }
  697. int verify_stateless_cookie_callback(SSL *ssl, const unsigned char *cookie,
  698. size_t cookie_len)
  699. {
  700. return verify_cookie_callback(ssl, cookie, cookie_len);
  701. }
  702. #endif
  703. /*
  704. * Example of extended certificate handling. Where the standard support of
  705. * one certificate per algorithm is not sufficient an application can decide
  706. * which certificate(s) to use at runtime based on whatever criteria it deems
  707. * appropriate.
  708. */
  709. /* Linked list of certificates, keys and chains */
  710. struct ssl_excert_st {
  711. int certform;
  712. const char *certfile;
  713. int keyform;
  714. const char *keyfile;
  715. const char *chainfile;
  716. X509 *cert;
  717. EVP_PKEY *key;
  718. STACK_OF(X509) *chain;
  719. int build_chain;
  720. struct ssl_excert_st *next, *prev;
  721. };
  722. static STRINT_PAIR chain_flags[] = {
  723. {"Overall Validity", CERT_PKEY_VALID},
  724. {"Sign with EE key", CERT_PKEY_SIGN},
  725. {"EE signature", CERT_PKEY_EE_SIGNATURE},
  726. {"CA signature", CERT_PKEY_CA_SIGNATURE},
  727. {"EE key parameters", CERT_PKEY_EE_PARAM},
  728. {"CA key parameters", CERT_PKEY_CA_PARAM},
  729. {"Explicitly sign with EE key", CERT_PKEY_EXPLICIT_SIGN},
  730. {"Issuer Name", CERT_PKEY_ISSUER_NAME},
  731. {"Certificate Type", CERT_PKEY_CERT_TYPE},
  732. {NULL}
  733. };
  734. static void print_chain_flags(SSL *s, int flags)
  735. {
  736. STRINT_PAIR *pp;
  737. for (pp = chain_flags; pp->name; ++pp)
  738. BIO_printf(bio_err, "\t%s: %s\n",
  739. pp->name,
  740. (flags & pp->retval) ? "OK" : "NOT OK");
  741. BIO_printf(bio_err, "\tSuite B: ");
  742. if (SSL_set_cert_flags(s, 0) & SSL_CERT_FLAG_SUITEB_128_LOS)
  743. BIO_puts(bio_err, flags & CERT_PKEY_SUITEB ? "OK\n" : "NOT OK\n");
  744. else
  745. BIO_printf(bio_err, "not tested\n");
  746. }
  747. /*
  748. * Very basic selection callback: just use any certificate chain reported as
  749. * valid. More sophisticated could prioritise according to local policy.
  750. */
  751. static int set_cert_cb(SSL *ssl, void *arg)
  752. {
  753. int i, rv;
  754. SSL_EXCERT *exc = arg;
  755. #ifdef CERT_CB_TEST_RETRY
  756. static int retry_cnt;
  757. if (retry_cnt < 5) {
  758. retry_cnt++;
  759. BIO_printf(bio_err,
  760. "Certificate callback retry test: count %d\n",
  761. retry_cnt);
  762. return -1;
  763. }
  764. #endif
  765. SSL_certs_clear(ssl);
  766. if (exc == NULL)
  767. return 1;
  768. /*
  769. * Go to end of list and traverse backwards since we prepend newer
  770. * entries this retains the original order.
  771. */
  772. while (exc->next != NULL)
  773. exc = exc->next;
  774. i = 0;
  775. while (exc != NULL) {
  776. i++;
  777. rv = SSL_check_chain(ssl, exc->cert, exc->key, exc->chain);
  778. BIO_printf(bio_err, "Checking cert chain %d:\nSubject: ", i);
  779. X509_NAME_print_ex(bio_err, X509_get_subject_name(exc->cert), 0,
  780. get_nameopt());
  781. BIO_puts(bio_err, "\n");
  782. print_chain_flags(ssl, rv);
  783. if (rv & CERT_PKEY_VALID) {
  784. if (!SSL_use_certificate(ssl, exc->cert)
  785. || !SSL_use_PrivateKey(ssl, exc->key)) {
  786. return 0;
  787. }
  788. /*
  789. * NB: we wouldn't normally do this as it is not efficient
  790. * building chains on each connection better to cache the chain
  791. * in advance.
  792. */
  793. if (exc->build_chain) {
  794. if (!SSL_build_cert_chain(ssl, 0))
  795. return 0;
  796. } else if (exc->chain != NULL) {
  797. SSL_set1_chain(ssl, exc->chain);
  798. }
  799. }
  800. exc = exc->prev;
  801. }
  802. return 1;
  803. }
  804. void ssl_ctx_set_excert(SSL_CTX *ctx, SSL_EXCERT *exc)
  805. {
  806. SSL_CTX_set_cert_cb(ctx, set_cert_cb, exc);
  807. }
  808. static int ssl_excert_prepend(SSL_EXCERT **pexc)
  809. {
  810. SSL_EXCERT *exc = app_malloc(sizeof(*exc), "prepend cert");
  811. memset(exc, 0, sizeof(*exc));
  812. exc->next = *pexc;
  813. *pexc = exc;
  814. if (exc->next) {
  815. exc->certform = exc->next->certform;
  816. exc->keyform = exc->next->keyform;
  817. exc->next->prev = exc;
  818. } else {
  819. exc->certform = FORMAT_PEM;
  820. exc->keyform = FORMAT_PEM;
  821. }
  822. return 1;
  823. }
  824. void ssl_excert_free(SSL_EXCERT *exc)
  825. {
  826. SSL_EXCERT *curr;
  827. if (exc == NULL)
  828. return;
  829. while (exc) {
  830. X509_free(exc->cert);
  831. EVP_PKEY_free(exc->key);
  832. sk_X509_pop_free(exc->chain, X509_free);
  833. curr = exc;
  834. exc = exc->next;
  835. OPENSSL_free(curr);
  836. }
  837. }
  838. int load_excert(SSL_EXCERT **pexc)
  839. {
  840. SSL_EXCERT *exc = *pexc;
  841. if (exc == NULL)
  842. return 1;
  843. /* If nothing in list, free and set to NULL */
  844. if (exc->certfile == NULL && exc->next == NULL) {
  845. ssl_excert_free(exc);
  846. *pexc = NULL;
  847. return 1;
  848. }
  849. for (; exc; exc = exc->next) {
  850. if (exc->certfile == NULL) {
  851. BIO_printf(bio_err, "Missing filename\n");
  852. return 0;
  853. }
  854. exc->cert = load_cert(exc->certfile, exc->certform,
  855. "Server Certificate");
  856. if (exc->cert == NULL)
  857. return 0;
  858. if (exc->keyfile != NULL) {
  859. exc->key = load_key(exc->keyfile, exc->keyform,
  860. 0, NULL, NULL, "Server Key");
  861. } else {
  862. exc->key = load_key(exc->certfile, exc->certform,
  863. 0, NULL, NULL, "Server Key");
  864. }
  865. if (exc->key == NULL)
  866. return 0;
  867. if (exc->chainfile != NULL) {
  868. if (!load_certs(exc->chainfile, &exc->chain, FORMAT_PEM, NULL,
  869. "Server Chain"))
  870. return 0;
  871. }
  872. }
  873. return 1;
  874. }
  875. enum range { OPT_X_ENUM };
  876. int args_excert(int opt, SSL_EXCERT **pexc)
  877. {
  878. SSL_EXCERT *exc = *pexc;
  879. assert(opt > OPT_X__FIRST);
  880. assert(opt < OPT_X__LAST);
  881. if (exc == NULL) {
  882. if (!ssl_excert_prepend(&exc)) {
  883. BIO_printf(bio_err, " %s: Error initialising xcert\n",
  884. opt_getprog());
  885. goto err;
  886. }
  887. *pexc = exc;
  888. }
  889. switch ((enum range)opt) {
  890. case OPT_X__FIRST:
  891. case OPT_X__LAST:
  892. return 0;
  893. case OPT_X_CERT:
  894. if (exc->certfile != NULL && !ssl_excert_prepend(&exc)) {
  895. BIO_printf(bio_err, "%s: Error adding xcert\n", opt_getprog());
  896. goto err;
  897. }
  898. *pexc = exc;
  899. exc->certfile = opt_arg();
  900. break;
  901. case OPT_X_KEY:
  902. if (exc->keyfile != NULL) {
  903. BIO_printf(bio_err, "%s: Key already specified\n", opt_getprog());
  904. goto err;
  905. }
  906. exc->keyfile = opt_arg();
  907. break;
  908. case OPT_X_CHAIN:
  909. if (exc->chainfile != NULL) {
  910. BIO_printf(bio_err, "%s: Chain already specified\n",
  911. opt_getprog());
  912. goto err;
  913. }
  914. exc->chainfile = opt_arg();
  915. break;
  916. case OPT_X_CHAIN_BUILD:
  917. exc->build_chain = 1;
  918. break;
  919. case OPT_X_CERTFORM:
  920. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &exc->certform))
  921. return 0;
  922. break;
  923. case OPT_X_KEYFORM:
  924. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &exc->keyform))
  925. return 0;
  926. break;
  927. }
  928. return 1;
  929. err:
  930. ERR_print_errors(bio_err);
  931. ssl_excert_free(exc);
  932. *pexc = NULL;
  933. return 0;
  934. }
  935. static void print_raw_cipherlist(SSL *s)
  936. {
  937. const unsigned char *rlist;
  938. static const unsigned char scsv_id[] = { 0, 0xFF };
  939. size_t i, rlistlen, num;
  940. if (!SSL_is_server(s))
  941. return;
  942. num = SSL_get0_raw_cipherlist(s, NULL);
  943. OPENSSL_assert(num == 2);
  944. rlistlen = SSL_get0_raw_cipherlist(s, &rlist);
  945. BIO_puts(bio_err, "Client cipher list: ");
  946. for (i = 0; i < rlistlen; i += num, rlist += num) {
  947. const SSL_CIPHER *c = SSL_CIPHER_find(s, rlist);
  948. if (i)
  949. BIO_puts(bio_err, ":");
  950. if (c != NULL) {
  951. BIO_puts(bio_err, SSL_CIPHER_get_name(c));
  952. } else if (memcmp(rlist, scsv_id, num) == 0) {
  953. BIO_puts(bio_err, "SCSV");
  954. } else {
  955. size_t j;
  956. BIO_puts(bio_err, "0x");
  957. for (j = 0; j < num; j++)
  958. BIO_printf(bio_err, "%02X", rlist[j]);
  959. }
  960. }
  961. BIO_puts(bio_err, "\n");
  962. }
  963. /*
  964. * Hex encoder for TLSA RRdata, not ':' delimited.
  965. */
  966. static char *hexencode(const unsigned char *data, size_t len)
  967. {
  968. static const char *hex = "0123456789abcdef";
  969. char *out;
  970. char *cp;
  971. size_t outlen = 2 * len + 1;
  972. int ilen = (int) outlen;
  973. if (outlen < len || ilen < 0 || outlen != (size_t)ilen) {
  974. BIO_printf(bio_err, "%s: %zu-byte buffer too large to hexencode\n",
  975. opt_getprog(), len);
  976. exit(1);
  977. }
  978. cp = out = app_malloc(ilen, "TLSA hex data buffer");
  979. while (len-- > 0) {
  980. *cp++ = hex[(*data >> 4) & 0x0f];
  981. *cp++ = hex[*data++ & 0x0f];
  982. }
  983. *cp = '\0';
  984. return out;
  985. }
  986. void print_verify_detail(SSL *s, BIO *bio)
  987. {
  988. int mdpth;
  989. EVP_PKEY *mspki;
  990. long verify_err = SSL_get_verify_result(s);
  991. if (verify_err == X509_V_OK) {
  992. const char *peername = SSL_get0_peername(s);
  993. BIO_printf(bio, "Verification: OK\n");
  994. if (peername != NULL)
  995. BIO_printf(bio, "Verified peername: %s\n", peername);
  996. } else {
  997. const char *reason = X509_verify_cert_error_string(verify_err);
  998. BIO_printf(bio, "Verification error: %s\n", reason);
  999. }
  1000. if ((mdpth = SSL_get0_dane_authority(s, NULL, &mspki)) >= 0) {
  1001. uint8_t usage, selector, mtype;
  1002. const unsigned char *data = NULL;
  1003. size_t dlen = 0;
  1004. char *hexdata;
  1005. mdpth = SSL_get0_dane_tlsa(s, &usage, &selector, &mtype, &data, &dlen);
  1006. /*
  1007. * The TLSA data field can be quite long when it is a certificate,
  1008. * public key or even a SHA2-512 digest. Because the initial octets of
  1009. * ASN.1 certificates and public keys contain mostly boilerplate OIDs
  1010. * and lengths, we show the last 12 bytes of the data instead, as these
  1011. * are more likely to distinguish distinct TLSA records.
  1012. */
  1013. #define TLSA_TAIL_SIZE 12
  1014. if (dlen > TLSA_TAIL_SIZE)
  1015. hexdata = hexencode(data + dlen - TLSA_TAIL_SIZE, TLSA_TAIL_SIZE);
  1016. else
  1017. hexdata = hexencode(data, dlen);
  1018. BIO_printf(bio, "DANE TLSA %d %d %d %s%s %s at depth %d\n",
  1019. usage, selector, mtype,
  1020. (dlen > TLSA_TAIL_SIZE) ? "..." : "", hexdata,
  1021. (mspki != NULL) ? "signed the certificate" :
  1022. mdpth ? "matched TA certificate" : "matched EE certificate",
  1023. mdpth);
  1024. OPENSSL_free(hexdata);
  1025. }
  1026. }
  1027. void print_ssl_summary(SSL *s)
  1028. {
  1029. const SSL_CIPHER *c;
  1030. X509 *peer;
  1031. BIO_printf(bio_err, "Protocol version: %s\n", SSL_get_version(s));
  1032. print_raw_cipherlist(s);
  1033. c = SSL_get_current_cipher(s);
  1034. BIO_printf(bio_err, "Ciphersuite: %s\n", SSL_CIPHER_get_name(c));
  1035. do_print_sigalgs(bio_err, s, 0);
  1036. peer = SSL_get_peer_certificate(s);
  1037. if (peer != NULL) {
  1038. int nid;
  1039. BIO_puts(bio_err, "Peer certificate: ");
  1040. X509_NAME_print_ex(bio_err, X509_get_subject_name(peer),
  1041. 0, get_nameopt());
  1042. BIO_puts(bio_err, "\n");
  1043. if (SSL_get_peer_signature_nid(s, &nid))
  1044. BIO_printf(bio_err, "Hash used: %s\n", OBJ_nid2sn(nid));
  1045. if (SSL_get_peer_signature_type_nid(s, &nid))
  1046. BIO_printf(bio_err, "Signature type: %s\n", get_sigtype(nid));
  1047. print_verify_detail(s, bio_err);
  1048. } else {
  1049. BIO_puts(bio_err, "No peer certificate\n");
  1050. }
  1051. X509_free(peer);
  1052. #ifndef OPENSSL_NO_EC
  1053. ssl_print_point_formats(bio_err, s);
  1054. if (SSL_is_server(s))
  1055. ssl_print_groups(bio_err, s, 1);
  1056. else
  1057. ssl_print_tmp_key(bio_err, s);
  1058. #else
  1059. if (!SSL_is_server(s))
  1060. ssl_print_tmp_key(bio_err, s);
  1061. #endif
  1062. }
  1063. int config_ctx(SSL_CONF_CTX *cctx, STACK_OF(OPENSSL_STRING) *str,
  1064. SSL_CTX *ctx)
  1065. {
  1066. int i;
  1067. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  1068. for (i = 0; i < sk_OPENSSL_STRING_num(str); i += 2) {
  1069. const char *flag = sk_OPENSSL_STRING_value(str, i);
  1070. const char *arg = sk_OPENSSL_STRING_value(str, i + 1);
  1071. if (SSL_CONF_cmd(cctx, flag, arg) <= 0) {
  1072. if (arg != NULL)
  1073. BIO_printf(bio_err, "Error with command: \"%s %s\"\n",
  1074. flag, arg);
  1075. else
  1076. BIO_printf(bio_err, "Error with command: \"%s\"\n", flag);
  1077. ERR_print_errors(bio_err);
  1078. return 0;
  1079. }
  1080. }
  1081. if (!SSL_CONF_CTX_finish(cctx)) {
  1082. BIO_puts(bio_err, "Error finishing context\n");
  1083. ERR_print_errors(bio_err);
  1084. return 0;
  1085. }
  1086. return 1;
  1087. }
  1088. static int add_crls_store(X509_STORE *st, STACK_OF(X509_CRL) *crls)
  1089. {
  1090. X509_CRL *crl;
  1091. int i;
  1092. for (i = 0; i < sk_X509_CRL_num(crls); i++) {
  1093. crl = sk_X509_CRL_value(crls, i);
  1094. X509_STORE_add_crl(st, crl);
  1095. }
  1096. return 1;
  1097. }
  1098. int ssl_ctx_add_crls(SSL_CTX *ctx, STACK_OF(X509_CRL) *crls, int crl_download)
  1099. {
  1100. X509_STORE *st;
  1101. st = SSL_CTX_get_cert_store(ctx);
  1102. add_crls_store(st, crls);
  1103. if (crl_download)
  1104. store_setup_crl_download(st);
  1105. return 1;
  1106. }
  1107. int ssl_load_stores(SSL_CTX *ctx,
  1108. const char *vfyCApath, const char *vfyCAfile,
  1109. const char *chCApath, const char *chCAfile,
  1110. STACK_OF(X509_CRL) *crls, int crl_download)
  1111. {
  1112. X509_STORE *vfy = NULL, *ch = NULL;
  1113. int rv = 0;
  1114. if (vfyCApath != NULL || vfyCAfile != NULL) {
  1115. vfy = X509_STORE_new();
  1116. if (vfy == NULL)
  1117. goto err;
  1118. if (!X509_STORE_load_locations(vfy, vfyCAfile, vfyCApath))
  1119. goto err;
  1120. add_crls_store(vfy, crls);
  1121. SSL_CTX_set1_verify_cert_store(ctx, vfy);
  1122. if (crl_download)
  1123. store_setup_crl_download(vfy);
  1124. }
  1125. if (chCApath != NULL || chCAfile != NULL) {
  1126. ch = X509_STORE_new();
  1127. if (ch == NULL)
  1128. goto err;
  1129. if (!X509_STORE_load_locations(ch, chCAfile, chCApath))
  1130. goto err;
  1131. SSL_CTX_set1_chain_cert_store(ctx, ch);
  1132. }
  1133. rv = 1;
  1134. err:
  1135. X509_STORE_free(vfy);
  1136. X509_STORE_free(ch);
  1137. return rv;
  1138. }
  1139. /* Verbose print out of security callback */
  1140. typedef struct {
  1141. BIO *out;
  1142. int verbose;
  1143. int (*old_cb) (const SSL *s, const SSL_CTX *ctx, int op, int bits, int nid,
  1144. void *other, void *ex);
  1145. } security_debug_ex;
  1146. static STRINT_PAIR callback_types[] = {
  1147. {"Supported Ciphersuite", SSL_SECOP_CIPHER_SUPPORTED},
  1148. {"Shared Ciphersuite", SSL_SECOP_CIPHER_SHARED},
  1149. {"Check Ciphersuite", SSL_SECOP_CIPHER_CHECK},
  1150. #ifndef OPENSSL_NO_DH
  1151. {"Temp DH key bits", SSL_SECOP_TMP_DH},
  1152. #endif
  1153. {"Supported Curve", SSL_SECOP_CURVE_SUPPORTED},
  1154. {"Shared Curve", SSL_SECOP_CURVE_SHARED},
  1155. {"Check Curve", SSL_SECOP_CURVE_CHECK},
  1156. {"Supported Signature Algorithm digest", SSL_SECOP_SIGALG_SUPPORTED},
  1157. {"Shared Signature Algorithm digest", SSL_SECOP_SIGALG_SHARED},
  1158. {"Check Signature Algorithm digest", SSL_SECOP_SIGALG_CHECK},
  1159. {"Signature Algorithm mask", SSL_SECOP_SIGALG_MASK},
  1160. {"Certificate chain EE key", SSL_SECOP_EE_KEY},
  1161. {"Certificate chain CA key", SSL_SECOP_CA_KEY},
  1162. {"Peer Chain EE key", SSL_SECOP_PEER_EE_KEY},
  1163. {"Peer Chain CA key", SSL_SECOP_PEER_CA_KEY},
  1164. {"Certificate chain CA digest", SSL_SECOP_CA_MD},
  1165. {"Peer chain CA digest", SSL_SECOP_PEER_CA_MD},
  1166. {"SSL compression", SSL_SECOP_COMPRESSION},
  1167. {"Session ticket", SSL_SECOP_TICKET},
  1168. {NULL}
  1169. };
  1170. static int security_callback_debug(const SSL *s, const SSL_CTX *ctx,
  1171. int op, int bits, int nid,
  1172. void *other, void *ex)
  1173. {
  1174. security_debug_ex *sdb = ex;
  1175. int rv, show_bits = 1, cert_md = 0;
  1176. const char *nm;
  1177. rv = sdb->old_cb(s, ctx, op, bits, nid, other, ex);
  1178. if (rv == 1 && sdb->verbose < 2)
  1179. return 1;
  1180. BIO_puts(sdb->out, "Security callback: ");
  1181. nm = lookup(op, callback_types, NULL);
  1182. switch (op) {
  1183. case SSL_SECOP_TICKET:
  1184. case SSL_SECOP_COMPRESSION:
  1185. show_bits = 0;
  1186. nm = NULL;
  1187. break;
  1188. case SSL_SECOP_VERSION:
  1189. BIO_printf(sdb->out, "Version=%s", lookup(nid, ssl_versions, "???"));
  1190. show_bits = 0;
  1191. nm = NULL;
  1192. break;
  1193. case SSL_SECOP_CA_MD:
  1194. case SSL_SECOP_PEER_CA_MD:
  1195. cert_md = 1;
  1196. break;
  1197. }
  1198. if (nm != NULL)
  1199. BIO_printf(sdb->out, "%s=", nm);
  1200. switch (op & SSL_SECOP_OTHER_TYPE) {
  1201. case SSL_SECOP_OTHER_CIPHER:
  1202. BIO_puts(sdb->out, SSL_CIPHER_get_name(other));
  1203. break;
  1204. #ifndef OPENSSL_NO_EC
  1205. case SSL_SECOP_OTHER_CURVE:
  1206. {
  1207. const char *cname;
  1208. cname = EC_curve_nid2nist(nid);
  1209. if (cname == NULL)
  1210. cname = OBJ_nid2sn(nid);
  1211. BIO_puts(sdb->out, cname);
  1212. }
  1213. break;
  1214. #endif
  1215. #ifndef OPENSSL_NO_DH
  1216. case SSL_SECOP_OTHER_DH:
  1217. {
  1218. DH *dh = other;
  1219. BIO_printf(sdb->out, "%d", DH_bits(dh));
  1220. break;
  1221. }
  1222. #endif
  1223. case SSL_SECOP_OTHER_CERT:
  1224. {
  1225. if (cert_md) {
  1226. int sig_nid = X509_get_signature_nid(other);
  1227. BIO_puts(sdb->out, OBJ_nid2sn(sig_nid));
  1228. } else {
  1229. EVP_PKEY *pkey = X509_get0_pubkey(other);
  1230. const char *algname = "";
  1231. EVP_PKEY_asn1_get0_info(NULL, NULL, NULL, NULL,
  1232. &algname, EVP_PKEY_get0_asn1(pkey));
  1233. BIO_printf(sdb->out, "%s, bits=%d",
  1234. algname, EVP_PKEY_bits(pkey));
  1235. }
  1236. break;
  1237. }
  1238. case SSL_SECOP_OTHER_SIGALG:
  1239. {
  1240. const unsigned char *salg = other;
  1241. const char *sname = NULL;
  1242. switch (salg[1]) {
  1243. case TLSEXT_signature_anonymous:
  1244. sname = "anonymous";
  1245. break;
  1246. case TLSEXT_signature_rsa:
  1247. sname = "RSA";
  1248. break;
  1249. case TLSEXT_signature_dsa:
  1250. sname = "DSA";
  1251. break;
  1252. case TLSEXT_signature_ecdsa:
  1253. sname = "ECDSA";
  1254. break;
  1255. }
  1256. BIO_puts(sdb->out, OBJ_nid2sn(nid));
  1257. if (sname)
  1258. BIO_printf(sdb->out, ", algorithm=%s", sname);
  1259. else
  1260. BIO_printf(sdb->out, ", algid=%d", salg[1]);
  1261. break;
  1262. }
  1263. }
  1264. if (show_bits)
  1265. BIO_printf(sdb->out, ", security bits=%d", bits);
  1266. BIO_printf(sdb->out, ": %s\n", rv ? "yes" : "no");
  1267. return rv;
  1268. }
  1269. void ssl_ctx_security_debug(SSL_CTX *ctx, int verbose)
  1270. {
  1271. static security_debug_ex sdb;
  1272. sdb.out = bio_err;
  1273. sdb.verbose = verbose;
  1274. sdb.old_cb = SSL_CTX_get_security_callback(ctx);
  1275. SSL_CTX_set_security_callback(ctx, security_callback_debug);
  1276. SSL_CTX_set0_security_ex_data(ctx, &sdb);
  1277. }
  1278. static void keylog_callback(const SSL *ssl, const char *line)
  1279. {
  1280. if (bio_keylog == NULL) {
  1281. BIO_printf(bio_err, "Keylog callback is invoked without valid file!\n");
  1282. return;
  1283. }
  1284. /*
  1285. * There might be concurrent writers to the keylog file, so we must ensure
  1286. * that the given line is written at once.
  1287. */
  1288. BIO_printf(bio_keylog, "%s\n", line);
  1289. (void)BIO_flush(bio_keylog);
  1290. }
  1291. int set_keylog_file(SSL_CTX *ctx, const char *keylog_file)
  1292. {
  1293. /* Close any open files */
  1294. BIO_free_all(bio_keylog);
  1295. bio_keylog = NULL;
  1296. if (ctx == NULL || keylog_file == NULL) {
  1297. /* Keylogging is disabled, OK. */
  1298. return 0;
  1299. }
  1300. /*
  1301. * Append rather than write in order to allow concurrent modification.
  1302. * Furthermore, this preserves existing keylog files which is useful when
  1303. * the tool is run multiple times.
  1304. */
  1305. bio_keylog = BIO_new_file(keylog_file, "a");
  1306. if (bio_keylog == NULL) {
  1307. BIO_printf(bio_err, "Error writing keylog file %s\n", keylog_file);
  1308. return 1;
  1309. }
  1310. /* Write a header for seekable, empty files (this excludes pipes). */
  1311. if (BIO_tell(bio_keylog) == 0) {
  1312. BIO_puts(bio_keylog,
  1313. "# SSL/TLS secrets log file, generated by OpenSSL\n");
  1314. (void)BIO_flush(bio_keylog);
  1315. }
  1316. SSL_CTX_set_keylog_callback(ctx, keylog_callback);
  1317. return 0;
  1318. }
  1319. void print_ca_names(BIO *bio, SSL *s)
  1320. {
  1321. const char *cs = SSL_is_server(s) ? "server" : "client";
  1322. const STACK_OF(X509_NAME) *sk = SSL_get0_peer_CA_list(s);
  1323. int i;
  1324. if (sk == NULL || sk_X509_NAME_num(sk) == 0) {
  1325. BIO_printf(bio, "---\nNo %s certificate CA names sent\n", cs);
  1326. return;
  1327. }
  1328. BIO_printf(bio, "---\nAcceptable %s certificate CA names\n",cs);
  1329. for (i = 0; i < sk_X509_NAME_num(sk); i++) {
  1330. X509_NAME_print_ex(bio, sk_X509_NAME_value(sk, i), 0, get_nameopt());
  1331. BIO_write(bio, "\n", 1);
  1332. }
  1333. }