2
0

s_cb.c 47 KB

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