s_cb.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com).
  108. *
  109. */
  110. /* callback functions used by s_client, s_server, and s_time */
  111. #include <stdio.h>
  112. #include <stdlib.h>
  113. #include <string.h> /* for memcpy() and strcmp() */
  114. #define USE_SOCKETS
  115. #include "apps.h"
  116. #undef USE_SOCKETS
  117. #include <openssl/err.h>
  118. #include <openssl/rand.h>
  119. #include <openssl/x509.h>
  120. #include <openssl/ssl.h>
  121. #include <openssl/bn.h>
  122. #ifndef OPENSSL_NO_DH
  123. # include <openssl/dh.h>
  124. #endif
  125. #include "s_apps.h"
  126. #define COOKIE_SECRET_LENGTH 16
  127. int verify_depth = 0;
  128. int verify_quiet = 0;
  129. int verify_error = X509_V_OK;
  130. int verify_return_error = 0;
  131. static unsigned char cookie_secret[COOKIE_SECRET_LENGTH];
  132. static int cookie_initialized = 0;
  133. static const char *lookup(int val, const STRINT_PAIR* list, const char* def)
  134. {
  135. for ( ; list->name; ++list)
  136. if (list->retval == val)
  137. return list->name;
  138. return def;
  139. }
  140. int verify_callback(int ok, X509_STORE_CTX *ctx)
  141. {
  142. X509 *err_cert;
  143. int err, depth;
  144. err_cert = X509_STORE_CTX_get_current_cert(ctx);
  145. err = X509_STORE_CTX_get_error(ctx);
  146. depth = X509_STORE_CTX_get_error_depth(ctx);
  147. if (!verify_quiet || !ok) {
  148. BIO_printf(bio_err, "depth=%d ", depth);
  149. if (err_cert) {
  150. X509_NAME_print_ex(bio_err,
  151. X509_get_subject_name(err_cert),
  152. 0, XN_FLAG_ONELINE);
  153. BIO_puts(bio_err, "\n");
  154. } else
  155. BIO_puts(bio_err, "<no cert>\n");
  156. }
  157. if (!ok) {
  158. BIO_printf(bio_err, "verify error:num=%d:%s\n", err,
  159. X509_verify_cert_error_string(err));
  160. if (verify_depth >= depth) {
  161. if (!verify_return_error)
  162. ok = 1;
  163. verify_error = X509_V_OK;
  164. } else {
  165. ok = 0;
  166. verify_error = X509_V_ERR_CERT_CHAIN_TOO_LONG;
  167. }
  168. }
  169. switch (err) {
  170. case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
  171. BIO_puts(bio_err, "issuer= ");
  172. X509_NAME_print_ex(bio_err, X509_get_issuer_name(err_cert),
  173. 0, XN_FLAG_ONELINE);
  174. BIO_puts(bio_err, "\n");
  175. break;
  176. case X509_V_ERR_CERT_NOT_YET_VALID:
  177. case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
  178. BIO_printf(bio_err, "notBefore=");
  179. ASN1_TIME_print(bio_err, X509_get_notBefore(err_cert));
  180. BIO_printf(bio_err, "\n");
  181. break;
  182. case X509_V_ERR_CERT_HAS_EXPIRED:
  183. case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
  184. BIO_printf(bio_err, "notAfter=");
  185. ASN1_TIME_print(bio_err, X509_get_notAfter(err_cert));
  186. BIO_printf(bio_err, "\n");
  187. break;
  188. case X509_V_ERR_NO_EXPLICIT_POLICY:
  189. if (!verify_quiet)
  190. policies_print(ctx);
  191. break;
  192. }
  193. if (err == X509_V_OK && ok == 2 && !verify_quiet)
  194. policies_print(ctx);
  195. if (ok && !verify_quiet)
  196. BIO_printf(bio_err, "verify return:%d\n", ok);
  197. return (ok);
  198. }
  199. int set_cert_stuff(SSL_CTX *ctx, char *cert_file, char *key_file)
  200. {
  201. if (cert_file != NULL) {
  202. if (SSL_CTX_use_certificate_file(ctx, cert_file,
  203. SSL_FILETYPE_PEM) <= 0) {
  204. BIO_printf(bio_err, "unable to get certificate from '%s'\n",
  205. cert_file);
  206. ERR_print_errors(bio_err);
  207. return (0);
  208. }
  209. if (key_file == NULL)
  210. key_file = cert_file;
  211. if (SSL_CTX_use_PrivateKey_file(ctx, key_file, SSL_FILETYPE_PEM) <= 0) {
  212. BIO_printf(bio_err, "unable to get private key from '%s'\n",
  213. key_file);
  214. ERR_print_errors(bio_err);
  215. return (0);
  216. }
  217. /*
  218. * If we are using DSA, we can copy the parameters from the private
  219. * key
  220. */
  221. /*
  222. * Now we know that a key and cert have been set against the SSL
  223. * context
  224. */
  225. if (!SSL_CTX_check_private_key(ctx)) {
  226. BIO_printf(bio_err,
  227. "Private key does not match the certificate public key\n");
  228. return (0);
  229. }
  230. }
  231. return (1);
  232. }
  233. int set_cert_key_stuff(SSL_CTX *ctx, X509 *cert, EVP_PKEY *key,
  234. STACK_OF(X509) *chain, int build_chain)
  235. {
  236. int chflags = chain ? SSL_BUILD_CHAIN_FLAG_CHECK : 0;
  237. if (cert == NULL)
  238. return 1;
  239. if (SSL_CTX_use_certificate(ctx, cert) <= 0) {
  240. BIO_printf(bio_err, "error setting certificate\n");
  241. ERR_print_errors(bio_err);
  242. return 0;
  243. }
  244. if (SSL_CTX_use_PrivateKey(ctx, key) <= 0) {
  245. BIO_printf(bio_err, "error setting private key\n");
  246. ERR_print_errors(bio_err);
  247. return 0;
  248. }
  249. /*
  250. * Now we know that a key and cert have been set against the SSL context
  251. */
  252. if (!SSL_CTX_check_private_key(ctx)) {
  253. BIO_printf(bio_err,
  254. "Private key does not match the certificate public key\n");
  255. return 0;
  256. }
  257. if (chain && !SSL_CTX_set1_chain(ctx, chain)) {
  258. BIO_printf(bio_err, "error setting certificate chain\n");
  259. ERR_print_errors(bio_err);
  260. return 0;
  261. }
  262. if (build_chain && !SSL_CTX_build_cert_chain(ctx, chflags)) {
  263. BIO_printf(bio_err, "error building certificate chain\n");
  264. ERR_print_errors(bio_err);
  265. return 0;
  266. }
  267. return 1;
  268. }
  269. static STRINT_PAIR cert_type_list[] = {
  270. {"RSA sign", TLS_CT_RSA_SIGN},
  271. {"DSA sign", TLS_CT_DSS_SIGN},
  272. {"RSA fixed DH", TLS_CT_RSA_FIXED_DH},
  273. {"DSS fixed DH", TLS_CT_DSS_FIXED_DH},
  274. {"ECDSA sign", TLS_CT_ECDSA_SIGN},
  275. {"RSA fixed ECDH", TLS_CT_RSA_FIXED_ECDH},
  276. {"ECDSA fixed ECDH", TLS_CT_ECDSA_FIXED_ECDH},
  277. {"GOST01 Sign", TLS_CT_GOST01_SIGN},
  278. {NULL}
  279. };
  280. static void ssl_print_client_cert_types(BIO *bio, SSL *s)
  281. {
  282. const unsigned char *p;
  283. int i;
  284. int cert_type_num = SSL_get0_certificate_types(s, &p);
  285. if (!cert_type_num)
  286. return;
  287. BIO_puts(bio, "Client Certificate Types: ");
  288. for (i = 0; i < cert_type_num; i++) {
  289. unsigned char cert_type = p[i];
  290. const char *cname = lookup((int)cert_type, cert_type_list, NULL);
  291. if (i)
  292. BIO_puts(bio, ", ");
  293. if (cname)
  294. BIO_puts(bio, cname);
  295. else
  296. BIO_printf(bio, "UNKNOWN (%d),", cert_type);
  297. }
  298. BIO_puts(bio, "\n");
  299. }
  300. static int do_print_sigalgs(BIO *out, SSL *s, int shared)
  301. {
  302. int i, nsig, client;
  303. client = SSL_is_server(s) ? 0 : 1;
  304. if (shared)
  305. nsig = SSL_get_shared_sigalgs(s, -1, NULL, NULL, NULL, NULL, NULL);
  306. else
  307. nsig = SSL_get_sigalgs(s, -1, NULL, NULL, NULL, NULL, NULL);
  308. if (nsig == 0)
  309. return 1;
  310. if (shared)
  311. BIO_puts(out, "Shared ");
  312. if (client)
  313. BIO_puts(out, "Requested ");
  314. BIO_puts(out, "Signature Algorithms: ");
  315. for (i = 0; i < nsig; i++) {
  316. int hash_nid, sign_nid;
  317. unsigned char rhash, rsign;
  318. const char *sstr = NULL;
  319. if (shared)
  320. SSL_get_shared_sigalgs(s, i, &sign_nid, &hash_nid, NULL,
  321. &rsign, &rhash);
  322. else
  323. SSL_get_sigalgs(s, i, &sign_nid, &hash_nid, NULL, &rsign, &rhash);
  324. if (i)
  325. BIO_puts(out, ":");
  326. if (sign_nid == EVP_PKEY_RSA)
  327. sstr = "RSA";
  328. else if (sign_nid == EVP_PKEY_DSA)
  329. sstr = "DSA";
  330. else if (sign_nid == EVP_PKEY_EC)
  331. sstr = "ECDSA";
  332. if (sstr)
  333. BIO_printf(out, "%s+", sstr);
  334. else
  335. BIO_printf(out, "0x%02X+", (int)rsign);
  336. if (hash_nid != NID_undef)
  337. BIO_printf(out, "%s", OBJ_nid2sn(hash_nid));
  338. else
  339. BIO_printf(out, "0x%02X", (int)rhash);
  340. }
  341. BIO_puts(out, "\n");
  342. return 1;
  343. }
  344. int ssl_print_sigalgs(BIO *out, SSL *s)
  345. {
  346. int mdnid;
  347. if (!SSL_is_server(s))
  348. ssl_print_client_cert_types(out, s);
  349. do_print_sigalgs(out, s, 0);
  350. do_print_sigalgs(out, s, 1);
  351. if (SSL_get_peer_signature_nid(s, &mdnid))
  352. BIO_printf(out, "Peer signing digest: %s\n", OBJ_nid2sn(mdnid));
  353. return 1;
  354. }
  355. #ifndef OPENSSL_NO_EC
  356. int ssl_print_point_formats(BIO *out, SSL *s)
  357. {
  358. int i, nformats;
  359. const char *pformats;
  360. nformats = SSL_get0_ec_point_formats(s, &pformats);
  361. if (nformats <= 0)
  362. return 1;
  363. BIO_puts(out, "Supported Elliptic Curve Point Formats: ");
  364. for (i = 0; i < nformats; i++, pformats++) {
  365. if (i)
  366. BIO_puts(out, ":");
  367. switch (*pformats) {
  368. case TLSEXT_ECPOINTFORMAT_uncompressed:
  369. BIO_puts(out, "uncompressed");
  370. break;
  371. case TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime:
  372. BIO_puts(out, "ansiX962_compressed_prime");
  373. break;
  374. case TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2:
  375. BIO_puts(out, "ansiX962_compressed_char2");
  376. break;
  377. default:
  378. BIO_printf(out, "unknown(%d)", (int)*pformats);
  379. break;
  380. }
  381. }
  382. if (nformats <= 0)
  383. BIO_puts(out, "NONE");
  384. BIO_puts(out, "\n");
  385. return 1;
  386. }
  387. int ssl_print_curves(BIO *out, SSL *s, int noshared)
  388. {
  389. int i, ncurves, *curves, nid;
  390. const char *cname;
  391. ncurves = SSL_get1_curves(s, NULL);
  392. if (ncurves <= 0)
  393. return 1;
  394. curves = app_malloc(ncurves * sizeof(int), "curves to print");
  395. SSL_get1_curves(s, curves);
  396. BIO_puts(out, "Supported Elliptic Curves: ");
  397. for (i = 0; i < ncurves; i++) {
  398. if (i)
  399. BIO_puts(out, ":");
  400. nid = curves[i];
  401. /* If unrecognised print out hex version */
  402. if (nid & TLSEXT_nid_unknown)
  403. BIO_printf(out, "0x%04X", nid & 0xFFFF);
  404. else {
  405. /* Use NIST name for curve if it exists */
  406. cname = EC_curve_nid2nist(nid);
  407. if (!cname)
  408. cname = OBJ_nid2sn(nid);
  409. BIO_printf(out, "%s", cname);
  410. }
  411. }
  412. if (ncurves == 0)
  413. BIO_puts(out, "NONE");
  414. OPENSSL_free(curves);
  415. if (noshared) {
  416. BIO_puts(out, "\n");
  417. return 1;
  418. }
  419. BIO_puts(out, "\nShared Elliptic curves: ");
  420. ncurves = SSL_get_shared_curve(s, -1);
  421. for (i = 0; i < ncurves; i++) {
  422. if (i)
  423. BIO_puts(out, ":");
  424. nid = SSL_get_shared_curve(s, i);
  425. cname = EC_curve_nid2nist(nid);
  426. if (!cname)
  427. cname = OBJ_nid2sn(nid);
  428. BIO_printf(out, "%s", cname);
  429. }
  430. if (ncurves == 0)
  431. BIO_puts(out, "NONE");
  432. BIO_puts(out, "\n");
  433. return 1;
  434. }
  435. #endif
  436. int ssl_print_tmp_key(BIO *out, SSL *s)
  437. {
  438. EVP_PKEY *key;
  439. if (!SSL_get_server_tmp_key(s, &key))
  440. return 1;
  441. BIO_puts(out, "Server Temp Key: ");
  442. switch (EVP_PKEY_id(key)) {
  443. case EVP_PKEY_RSA:
  444. BIO_printf(out, "RSA, %d bits\n", EVP_PKEY_bits(key));
  445. break;
  446. case EVP_PKEY_DH:
  447. BIO_printf(out, "DH, %d bits\n", EVP_PKEY_bits(key));
  448. break;
  449. #ifndef OPENSSL_NO_EC
  450. case EVP_PKEY_EC:
  451. {
  452. EC_KEY *ec = EVP_PKEY_get1_EC_KEY(key);
  453. int nid;
  454. const char *cname;
  455. nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
  456. EC_KEY_free(ec);
  457. cname = EC_curve_nid2nist(nid);
  458. if (!cname)
  459. cname = OBJ_nid2sn(nid);
  460. BIO_printf(out, "ECDH, %s, %d bits\n", cname, EVP_PKEY_bits(key));
  461. }
  462. #endif
  463. }
  464. EVP_PKEY_free(key);
  465. return 1;
  466. }
  467. long bio_dump_callback(BIO *bio, int cmd, const char *argp,
  468. int argi, long argl, long ret)
  469. {
  470. BIO *out;
  471. out = (BIO *)BIO_get_callback_arg(bio);
  472. if (out == NULL)
  473. return (ret);
  474. if (cmd == (BIO_CB_READ | BIO_CB_RETURN)) {
  475. BIO_printf(out, "read from %p [%p] (%lu bytes => %ld (0x%lX))\n",
  476. (void *)bio, argp, (unsigned long)argi, ret, ret);
  477. BIO_dump(out, argp, (int)ret);
  478. return (ret);
  479. } else if (cmd == (BIO_CB_WRITE | BIO_CB_RETURN)) {
  480. BIO_printf(out, "write to %p [%p] (%lu bytes => %ld (0x%lX))\n",
  481. (void *)bio, argp, (unsigned long)argi, ret, ret);
  482. BIO_dump(out, argp, (int)ret);
  483. }
  484. return (ret);
  485. }
  486. void apps_ssl_info_callback(const SSL *s, int where, int ret)
  487. {
  488. const char *str;
  489. int w;
  490. w = where & ~SSL_ST_MASK;
  491. if (w & SSL_ST_CONNECT)
  492. str = "SSL_connect";
  493. else if (w & SSL_ST_ACCEPT)
  494. str = "SSL_accept";
  495. else
  496. str = "undefined";
  497. if (where & SSL_CB_LOOP) {
  498. BIO_printf(bio_err, "%s:%s\n", str, SSL_state_string_long(s));
  499. } else if (where & SSL_CB_ALERT) {
  500. str = (where & SSL_CB_READ) ? "read" : "write";
  501. BIO_printf(bio_err, "SSL3 alert %s:%s:%s\n",
  502. str,
  503. SSL_alert_type_string_long(ret),
  504. SSL_alert_desc_string_long(ret));
  505. } else if (where & SSL_CB_EXIT) {
  506. if (ret == 0)
  507. BIO_printf(bio_err, "%s:failed in %s\n",
  508. str, SSL_state_string_long(s));
  509. else if (ret < 0) {
  510. BIO_printf(bio_err, "%s:error in %s\n",
  511. str, SSL_state_string_long(s));
  512. }
  513. }
  514. }
  515. static STRINT_PAIR ssl_versions[] = {
  516. {"SSL 3.0", SSL3_VERSION},
  517. {"TLS 1.0", TLS1_VERSION},
  518. {"TLS 1.1", TLS1_1_VERSION},
  519. {"TLS 1.2", TLS1_2_VERSION},
  520. {"DTLS 1.0", DTLS1_VERSION},
  521. {"DTLS 1.0 (bad)", DTLS1_BAD_VER},
  522. {NULL}
  523. };
  524. static STRINT_PAIR alert_types[] = {
  525. {" close_notify", 0},
  526. {" unexpected_message", 10},
  527. {" bad_record_mac", 20},
  528. {" decryption_failed", 21},
  529. {" record_overflow", 22},
  530. {" decompression_failure", 30},
  531. {" handshake_failure", 40},
  532. {" bad_certificate", 42},
  533. {" unsupported_certificate", 43},
  534. {" certificate_revoked", 44},
  535. {" certificate_expired", 45},
  536. {" certificate_unknown", 46},
  537. {" illegal_parameter", 47},
  538. {" unknown_ca", 48},
  539. {" access_denied", 49},
  540. {" decode_error", 50},
  541. {" decrypt_error", 51},
  542. {" export_restriction", 60},
  543. {" protocol_version", 70},
  544. {" insufficient_security", 71},
  545. {" internal_error", 80},
  546. {" user_canceled", 90},
  547. {" no_renegotiation", 100},
  548. {" unsupported_extension", 110},
  549. {" certificate_unobtainable", 111},
  550. {" unrecognized_name", 112},
  551. {" bad_certificate_status_response", 113},
  552. {" bad_certificate_hash_value", 114},
  553. {" unknown_psk_identity", 115},
  554. {NULL}
  555. };
  556. static STRINT_PAIR handshakes[] = {
  557. {", HelloRequest", 0},
  558. {", ClientHello", 1},
  559. {", ServerHello", 2},
  560. {", HelloVerifyRequest", 3},
  561. {", Certificate", 11},
  562. {", ServerKeyExchange", 12},
  563. {", CertificateRequest", 13},
  564. {", ServerHelloDone", 14},
  565. {", CertificateVerify", 15},
  566. {", ClientKeyExchange", 16},
  567. {", Finished", 20},
  568. {NULL}
  569. };
  570. void msg_cb(int write_p, int version, int content_type, const void *buf,
  571. size_t len, SSL *ssl, void *arg)
  572. {
  573. BIO *bio = arg;
  574. const char *str_write_p = write_p ? ">>>" : "<<<";
  575. const char *str_version = lookup(version, ssl_versions, "???");
  576. const char *str_content_type = "", *str_details1 = "", *str_details2 = "";
  577. const unsigned char* bp = buf;
  578. if (version == SSL3_VERSION ||
  579. version == TLS1_VERSION ||
  580. version == TLS1_1_VERSION ||
  581. version == TLS1_2_VERSION ||
  582. version == DTLS1_VERSION || version == DTLS1_BAD_VER) {
  583. switch (content_type) {
  584. case 20:
  585. str_content_type = "ChangeCipherSpec";
  586. break;
  587. case 21:
  588. str_content_type = "Alert";
  589. str_details1 = ", ???";
  590. if (len == 2) {
  591. switch (bp[0]) {
  592. case 1:
  593. str_details1 = ", warning";
  594. break;
  595. case 2:
  596. str_details1 = ", fatal";
  597. break;
  598. }
  599. str_details2 = lookup((int)bp[1], alert_types, " ???");
  600. }
  601. break;
  602. case 22:
  603. str_content_type = "Handshake";
  604. str_details1 = "???";
  605. if (len > 0)
  606. str_details1 = lookup((int)bp[0], handshakes, "???");
  607. break;
  608. #ifndef OPENSSL_NO_HEARTBEATS
  609. case 24:
  610. str_details1 = ", Heartbeat";
  611. if (len > 0) {
  612. switch (bp[0]) {
  613. case 1:
  614. str_details1 = ", HeartbeatRequest";
  615. break;
  616. case 2:
  617. str_details1 = ", HeartbeatResponse";
  618. break;
  619. }
  620. }
  621. break;
  622. #endif
  623. }
  624. }
  625. BIO_printf(bio, "%s %s%s [length %04lx]%s%s\n", str_write_p, str_version,
  626. str_content_type, (unsigned long)len, str_details1,
  627. str_details2);
  628. if (len > 0) {
  629. size_t num, i;
  630. BIO_printf(bio, " ");
  631. num = len;
  632. for (i = 0; i < num; i++) {
  633. if (i % 16 == 0 && i > 0)
  634. BIO_printf(bio, "\n ");
  635. BIO_printf(bio, " %02x", ((const unsigned char *)buf)[i]);
  636. }
  637. if (i < len)
  638. BIO_printf(bio, " ...");
  639. BIO_printf(bio, "\n");
  640. }
  641. (void)BIO_flush(bio);
  642. }
  643. static STRINT_PAIR tlsext_types[] = {
  644. {"server name", TLSEXT_TYPE_server_name},
  645. {"max fragment length", TLSEXT_TYPE_max_fragment_length},
  646. {"client certificate URL", TLSEXT_TYPE_client_certificate_url},
  647. {"trusted CA keys", TLSEXT_TYPE_trusted_ca_keys},
  648. {"truncated HMAC", TLSEXT_TYPE_truncated_hmac},
  649. {"status request", TLSEXT_TYPE_status_request},
  650. {"user mapping", TLSEXT_TYPE_user_mapping},
  651. {"client authz", TLSEXT_TYPE_client_authz},
  652. {"server authz", TLSEXT_TYPE_server_authz},
  653. {"cert type", TLSEXT_TYPE_cert_type},
  654. {"elliptic curves", TLSEXT_TYPE_elliptic_curves},
  655. {"EC point formats", TLSEXT_TYPE_ec_point_formats},
  656. {"SRP", TLSEXT_TYPE_srp},
  657. {"signature algorithms", TLSEXT_TYPE_signature_algorithms},
  658. {"use SRTP", TLSEXT_TYPE_use_srtp},
  659. {"heartbeat", TLSEXT_TYPE_heartbeat},
  660. {"session ticket", TLSEXT_TYPE_session_ticket},
  661. {"renegotiation info", TLSEXT_TYPE_renegotiate},
  662. {"TLS padding", TLSEXT_TYPE_padding},
  663. #ifdef TLSEXT_TYPE_next_proto_neg
  664. {"next protocol", TLSEXT_TYPE_next_proto_neg},
  665. #endif
  666. #ifdef TLSEXT_TYPE_encrypt_then_mac
  667. {"encrypt-then-mac", TLSEXT_TYPE_encrypt_then_mac},
  668. #endif
  669. #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
  670. {"application layer protocol negotiation",
  671. TLSEXT_TYPE_application_layer_protocol_negotiation},
  672. #endif
  673. #ifdef TLSEXT_TYPE_extended_master_secret
  674. {"extended master secret", TLSEXT_TYPE_extended_master_secret},
  675. #endif
  676. {NULL}
  677. };
  678. void tlsext_cb(SSL *s, int client_server, int type,
  679. unsigned char *data, int len, void *arg)
  680. {
  681. BIO *bio = arg;
  682. const char *extname = lookup(type, tlsext_types, "unknown");
  683. BIO_printf(bio, "TLS %s extension \"%s\" (id=%d), len=%d\n",
  684. client_server ? "server" : "client", extname, type, len);
  685. BIO_dump(bio, (char *)data, len);
  686. (void)BIO_flush(bio);
  687. }
  688. int generate_cookie_callback(SSL *ssl, unsigned char *cookie,
  689. unsigned int *cookie_len)
  690. {
  691. unsigned char *buffer;
  692. unsigned int length;
  693. union {
  694. struct sockaddr sa;
  695. struct sockaddr_in s4;
  696. #if OPENSSL_USE_IPV6
  697. struct sockaddr_in6 s6;
  698. #endif
  699. } peer;
  700. /* Initialize a random secret */
  701. if (!cookie_initialized) {
  702. if (RAND_bytes(cookie_secret, COOKIE_SECRET_LENGTH) <= 0) {
  703. BIO_printf(bio_err, "error setting random cookie secret\n");
  704. return 0;
  705. }
  706. cookie_initialized = 1;
  707. }
  708. /* Read peer information */
  709. (void)BIO_dgram_get_peer(SSL_get_rbio(ssl), &peer);
  710. /* Create buffer with peer's address and port */
  711. length = 0;
  712. switch (peer.sa.sa_family) {
  713. case AF_INET:
  714. length += sizeof(struct in_addr);
  715. length += sizeof(peer.s4.sin_port);
  716. break;
  717. #if OPENSSL_USE_IPV6
  718. case AF_INET6:
  719. length += sizeof(struct in6_addr);
  720. length += sizeof(peer.s6.sin6_port);
  721. break;
  722. #endif
  723. default:
  724. OPENSSL_assert(0);
  725. break;
  726. }
  727. buffer = app_malloc(length, "cookie generate buffer");
  728. switch (peer.sa.sa_family) {
  729. case AF_INET:
  730. memcpy(buffer, &peer.s4.sin_port, sizeof(peer.s4.sin_port));
  731. memcpy(buffer + sizeof(peer.s4.sin_port),
  732. &peer.s4.sin_addr, sizeof(struct in_addr));
  733. break;
  734. #if OPENSSL_USE_IPV6
  735. case AF_INET6:
  736. memcpy(buffer, &peer.s6.sin6_port, sizeof(peer.s6.sin6_port));
  737. memcpy(buffer + sizeof(peer.s6.sin6_port),
  738. &peer.s6.sin6_addr, sizeof(struct in6_addr));
  739. break;
  740. #endif
  741. default:
  742. OPENSSL_assert(0);
  743. break;
  744. }
  745. /* Calculate HMAC of buffer using the secret */
  746. HMAC(EVP_sha1(), cookie_secret, COOKIE_SECRET_LENGTH,
  747. buffer, length, cookie, cookie_len);
  748. OPENSSL_free(buffer);
  749. return 1;
  750. }
  751. int verify_cookie_callback(SSL *ssl, const unsigned char *cookie,
  752. unsigned int cookie_len)
  753. {
  754. unsigned char result[EVP_MAX_MD_SIZE];
  755. unsigned int resultlength;
  756. /* Note: we check cookie_initialized because if it's not,
  757. * it cannot be valid */
  758. if (cookie_initialized
  759. && generate_cookie_callback(ssl, result, &resultlength)
  760. && cookie_len == resultlength
  761. && memcmp(result, cookie, resultlength) == 0)
  762. return 1;
  763. return 0;
  764. }
  765. /*
  766. * Example of extended certificate handling. Where the standard support of
  767. * one certificate per algorithm is not sufficient an application can decide
  768. * which certificate(s) to use at runtime based on whatever criteria it deems
  769. * appropriate.
  770. */
  771. /* Linked list of certificates, keys and chains */
  772. struct ssl_excert_st {
  773. int certform;
  774. const char *certfile;
  775. int keyform;
  776. const char *keyfile;
  777. const char *chainfile;
  778. X509 *cert;
  779. EVP_PKEY *key;
  780. STACK_OF(X509) *chain;
  781. int build_chain;
  782. struct ssl_excert_st *next, *prev;
  783. };
  784. static STRINT_PAIR chain_flags[] = {
  785. {"Overall Validity", CERT_PKEY_VALID},
  786. {"Sign with EE key", CERT_PKEY_SIGN},
  787. {"EE signature", CERT_PKEY_EE_SIGNATURE},
  788. {"CA signature", CERT_PKEY_CA_SIGNATURE},
  789. {"EE key parameters", CERT_PKEY_EE_PARAM},
  790. {"CA key parameters", CERT_PKEY_CA_PARAM},
  791. {"Explicity sign with EE key", CERT_PKEY_EXPLICIT_SIGN},
  792. {"Issuer Name", CERT_PKEY_ISSUER_NAME},
  793. {"Certificate Type", CERT_PKEY_CERT_TYPE},
  794. {NULL}
  795. };
  796. static void print_chain_flags(SSL *s, int flags)
  797. {
  798. STRINT_PAIR *pp;
  799. for (pp = chain_flags; pp->name; ++pp)
  800. BIO_printf(bio_err, "\t%s: %s\n",
  801. pp->name,
  802. (flags & pp->retval) ? "OK" : "NOT OK");
  803. BIO_printf(bio_err, "\tSuite B: ");
  804. if (SSL_set_cert_flags(s, 0) & SSL_CERT_FLAG_SUITEB_128_LOS)
  805. BIO_puts(bio_err, flags & CERT_PKEY_SUITEB ? "OK\n" : "NOT OK\n");
  806. else
  807. BIO_printf(bio_err, "not tested\n");
  808. }
  809. /*
  810. * Very basic selection callback: just use any certificate chain reported as
  811. * valid. More sophisticated could prioritise according to local policy.
  812. */
  813. static int set_cert_cb(SSL *ssl, void *arg)
  814. {
  815. int i, rv;
  816. SSL_EXCERT *exc = arg;
  817. #ifdef CERT_CB_TEST_RETRY
  818. static int retry_cnt;
  819. if (retry_cnt < 5) {
  820. retry_cnt++;
  821. BIO_printf(bio_err,
  822. "Certificate callback retry test: count %d\n",
  823. retry_cnt);
  824. return -1;
  825. }
  826. #endif
  827. SSL_certs_clear(ssl);
  828. if (!exc)
  829. return 1;
  830. /*
  831. * Go to end of list and traverse backwards since we prepend newer
  832. * entries this retains the original order.
  833. */
  834. while (exc->next)
  835. exc = exc->next;
  836. i = 0;
  837. while (exc) {
  838. i++;
  839. rv = SSL_check_chain(ssl, exc->cert, exc->key, exc->chain);
  840. BIO_printf(bio_err, "Checking cert chain %d:\nSubject: ", i);
  841. X509_NAME_print_ex(bio_err, X509_get_subject_name(exc->cert), 0,
  842. XN_FLAG_ONELINE);
  843. BIO_puts(bio_err, "\n");
  844. print_chain_flags(ssl, rv);
  845. if (rv & CERT_PKEY_VALID) {
  846. if (!SSL_use_certificate(ssl, exc->cert)
  847. || !SSL_use_PrivateKey(ssl, exc->key)) {
  848. return 0;
  849. }
  850. /*
  851. * NB: we wouldn't normally do this as it is not efficient
  852. * building chains on each connection better to cache the chain
  853. * in advance.
  854. */
  855. if (exc->build_chain) {
  856. if (!SSL_build_cert_chain(ssl, 0))
  857. return 0;
  858. } else if (exc->chain)
  859. SSL_set1_chain(ssl, exc->chain);
  860. }
  861. exc = exc->prev;
  862. }
  863. return 1;
  864. }
  865. void ssl_ctx_set_excert(SSL_CTX *ctx, SSL_EXCERT *exc)
  866. {
  867. SSL_CTX_set_cert_cb(ctx, set_cert_cb, exc);
  868. }
  869. static int ssl_excert_prepend(SSL_EXCERT **pexc)
  870. {
  871. SSL_EXCERT *exc = app_malloc(sizeof(*exc), "prepend cert");
  872. memset(exc, 0, sizeof(*exc));
  873. exc->next = *pexc;
  874. *pexc = exc;
  875. if (exc->next) {
  876. exc->certform = exc->next->certform;
  877. exc->keyform = exc->next->keyform;
  878. exc->next->prev = exc;
  879. } else {
  880. exc->certform = FORMAT_PEM;
  881. exc->keyform = FORMAT_PEM;
  882. }
  883. return 1;
  884. }
  885. void ssl_excert_free(SSL_EXCERT *exc)
  886. {
  887. SSL_EXCERT *curr;
  888. if (!exc)
  889. return;
  890. while (exc) {
  891. X509_free(exc->cert);
  892. EVP_PKEY_free(exc->key);
  893. sk_X509_pop_free(exc->chain, X509_free);
  894. curr = exc;
  895. exc = exc->next;
  896. OPENSSL_free(curr);
  897. }
  898. }
  899. int load_excert(SSL_EXCERT **pexc)
  900. {
  901. SSL_EXCERT *exc = *pexc;
  902. if (!exc)
  903. return 1;
  904. /* If nothing in list, free and set to NULL */
  905. if (!exc->certfile && !exc->next) {
  906. ssl_excert_free(exc);
  907. *pexc = NULL;
  908. return 1;
  909. }
  910. for (; exc; exc = exc->next) {
  911. if (!exc->certfile) {
  912. BIO_printf(bio_err, "Missing filename\n");
  913. return 0;
  914. }
  915. exc->cert = load_cert(exc->certfile, exc->certform,
  916. NULL, NULL, "Server Certificate");
  917. if (!exc->cert)
  918. return 0;
  919. if (exc->keyfile) {
  920. exc->key = load_key(exc->keyfile, exc->keyform,
  921. 0, NULL, NULL, "Server Key");
  922. } else {
  923. exc->key = load_key(exc->certfile, exc->certform,
  924. 0, NULL, NULL, "Server Key");
  925. }
  926. if (!exc->key)
  927. return 0;
  928. if (exc->chainfile) {
  929. exc->chain = load_certs(exc->chainfile, FORMAT_PEM,
  930. NULL, NULL, "Server Chain");
  931. if (!exc->chain)
  932. return 0;
  933. }
  934. }
  935. return 1;
  936. }
  937. enum range { OPT_X_ENUM };
  938. int args_excert(int opt, SSL_EXCERT **pexc)
  939. {
  940. SSL_EXCERT *exc = *pexc;
  941. assert(opt > OPT_X__FIRST);
  942. assert(opt < OPT_X__LAST);
  943. if (exc == NULL) {
  944. if (!ssl_excert_prepend(&exc)) {
  945. BIO_printf(bio_err, " %s: Error initialising xcert\n",
  946. opt_getprog());
  947. goto err;
  948. }
  949. *pexc = exc;
  950. }
  951. switch ((enum range)opt) {
  952. case OPT_X__FIRST:
  953. case OPT_X__LAST:
  954. return 0;
  955. case OPT_X_CERT:
  956. if (exc->certfile && !ssl_excert_prepend(&exc)) {
  957. BIO_printf(bio_err, "%s: Error adding xcert\n", opt_getprog());
  958. goto err;
  959. }
  960. exc->certfile = opt_arg();
  961. break;
  962. case OPT_X_KEY:
  963. if (exc->keyfile) {
  964. BIO_printf(bio_err, "%s: Key already specified\n", opt_getprog());
  965. goto err;
  966. }
  967. exc->keyfile = opt_arg();
  968. break;
  969. case OPT_X_CHAIN:
  970. if (exc->chainfile) {
  971. BIO_printf(bio_err, "%s: Chain already specified\n",
  972. opt_getprog());
  973. goto err;
  974. }
  975. exc->chainfile = opt_arg();
  976. break;
  977. case OPT_X_CHAIN_BUILD:
  978. exc->build_chain = 1;
  979. break;
  980. case OPT_X_CERTFORM:
  981. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &exc->certform))
  982. return 0;
  983. break;
  984. case OPT_X_KEYFORM:
  985. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &exc->keyform))
  986. return 0;
  987. break;
  988. }
  989. return 1;
  990. err:
  991. ERR_print_errors(bio_err);
  992. ssl_excert_free(exc);
  993. *pexc = NULL;
  994. return 0;
  995. }
  996. static void print_raw_cipherlist(SSL *s)
  997. {
  998. const unsigned char *rlist;
  999. static const unsigned char scsv_id[] = { 0, 0, 0xFF };
  1000. size_t i, rlistlen, num;
  1001. if (!SSL_is_server(s))
  1002. return;
  1003. num = SSL_get0_raw_cipherlist(s, NULL);
  1004. rlistlen = SSL_get0_raw_cipherlist(s, &rlist);
  1005. BIO_puts(bio_err, "Client cipher list: ");
  1006. for (i = 0; i < rlistlen; i += num, rlist += num) {
  1007. const SSL_CIPHER *c = SSL_CIPHER_find(s, rlist);
  1008. if (i)
  1009. BIO_puts(bio_err, ":");
  1010. if (c)
  1011. BIO_puts(bio_err, SSL_CIPHER_get_name(c));
  1012. else if (!memcmp(rlist, scsv_id - num + 3, num))
  1013. BIO_puts(bio_err, "SCSV");
  1014. else {
  1015. size_t j;
  1016. BIO_puts(bio_err, "0x");
  1017. for (j = 0; j < num; j++)
  1018. BIO_printf(bio_err, "%02X", rlist[j]);
  1019. }
  1020. }
  1021. BIO_puts(bio_err, "\n");
  1022. }
  1023. void print_ssl_summary(SSL *s)
  1024. {
  1025. const SSL_CIPHER *c;
  1026. X509 *peer;
  1027. /* const char *pnam = SSL_is_server(s) ? "client" : "server"; */
  1028. BIO_printf(bio_err, "Protocol version: %s\n", SSL_get_version(s));
  1029. print_raw_cipherlist(s);
  1030. c = SSL_get_current_cipher(s);
  1031. BIO_printf(bio_err, "Ciphersuite: %s\n", SSL_CIPHER_get_name(c));
  1032. do_print_sigalgs(bio_err, s, 0);
  1033. peer = SSL_get_peer_certificate(s);
  1034. if (peer) {
  1035. int nid;
  1036. BIO_puts(bio_err, "Peer certificate: ");
  1037. X509_NAME_print_ex(bio_err, X509_get_subject_name(peer),
  1038. 0, XN_FLAG_ONELINE);
  1039. BIO_puts(bio_err, "\n");
  1040. if (SSL_get_peer_signature_nid(s, &nid))
  1041. BIO_printf(bio_err, "Hash used: %s\n", OBJ_nid2sn(nid));
  1042. } else
  1043. BIO_puts(bio_err, "No peer certificate\n");
  1044. X509_free(peer);
  1045. #ifndef OPENSSL_NO_EC
  1046. ssl_print_point_formats(bio_err, s);
  1047. if (SSL_is_server(s))
  1048. ssl_print_curves(bio_err, s, 1);
  1049. else
  1050. ssl_print_tmp_key(bio_err, s);
  1051. #else
  1052. if (!SSL_is_server(s))
  1053. ssl_print_tmp_key(bio_err, s);
  1054. #endif
  1055. }
  1056. int config_ctx(SSL_CONF_CTX *cctx, STACK_OF(OPENSSL_STRING) *str,
  1057. SSL_CTX *ctx, int no_jpake)
  1058. {
  1059. int i;
  1060. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  1061. for (i = 0; i < sk_OPENSSL_STRING_num(str); i += 2) {
  1062. const char *flag = sk_OPENSSL_STRING_value(str, i);
  1063. const char *arg = sk_OPENSSL_STRING_value(str, i + 1);
  1064. #ifndef OPENSSL_NO_JPAKE
  1065. if (!no_jpake && (strcmp(flag, "-cipher") == 0)) {
  1066. BIO_puts(bio_err, "JPAKE sets cipher to PSK\n");
  1067. return 0;
  1068. }
  1069. #endif
  1070. if (SSL_CONF_cmd(cctx, flag, arg) <= 0) {
  1071. if (arg)
  1072. BIO_printf(bio_err, "Error with command: \"%s %s\"\n",
  1073. flag, arg);
  1074. else
  1075. BIO_printf(bio_err, "Error with command: \"%s\"\n", flag);
  1076. ERR_print_errors(bio_err);
  1077. return 0;
  1078. }
  1079. }
  1080. #ifndef OPENSSL_NO_JPAKE
  1081. if (!no_jpake) {
  1082. if (SSL_CONF_cmd(cctx, "-cipher", "PSK") <= 0) {
  1083. BIO_puts(bio_err, "Error setting cipher to PSK\n");
  1084. ERR_print_errors(bio_err);
  1085. return 0;
  1086. }
  1087. }
  1088. #endif
  1089. if (!SSL_CONF_CTX_finish(cctx)) {
  1090. BIO_puts(bio_err, "Error finishing context\n");
  1091. ERR_print_errors(bio_err);
  1092. return 0;
  1093. }
  1094. return 1;
  1095. }
  1096. static int add_crls_store(X509_STORE *st, STACK_OF(X509_CRL) *crls)
  1097. {
  1098. X509_CRL *crl;
  1099. int i;
  1100. for (i = 0; i < sk_X509_CRL_num(crls); i++) {
  1101. crl = sk_X509_CRL_value(crls, i);
  1102. X509_STORE_add_crl(st, crl);
  1103. }
  1104. return 1;
  1105. }
  1106. int ssl_ctx_add_crls(SSL_CTX *ctx, STACK_OF(X509_CRL) *crls, int crl_download)
  1107. {
  1108. X509_STORE *st;
  1109. st = SSL_CTX_get_cert_store(ctx);
  1110. add_crls_store(st, crls);
  1111. if (crl_download)
  1112. store_setup_crl_download(st);
  1113. return 1;
  1114. }
  1115. int ssl_load_stores(SSL_CTX *ctx,
  1116. const char *vfyCApath, const char *vfyCAfile,
  1117. const char *chCApath, const char *chCAfile,
  1118. STACK_OF(X509_CRL) *crls, int crl_download)
  1119. {
  1120. X509_STORE *vfy = NULL, *ch = NULL;
  1121. int rv = 0;
  1122. if (vfyCApath != NULL || vfyCAfile != NULL) {
  1123. vfy = X509_STORE_new();
  1124. if (vfy == NULL)
  1125. goto err;
  1126. if (!X509_STORE_load_locations(vfy, vfyCAfile, vfyCApath))
  1127. goto err;
  1128. add_crls_store(vfy, crls);
  1129. SSL_CTX_set1_verify_cert_store(ctx, vfy);
  1130. if (crl_download)
  1131. store_setup_crl_download(vfy);
  1132. }
  1133. if (chCApath != NULL || chCAfile != NULL) {
  1134. ch = X509_STORE_new();
  1135. if (ch == NULL)
  1136. goto err;
  1137. if (!X509_STORE_load_locations(ch, chCAfile, chCApath))
  1138. goto err;
  1139. SSL_CTX_set1_chain_cert_store(ctx, ch);
  1140. }
  1141. rv = 1;
  1142. err:
  1143. X509_STORE_free(vfy);
  1144. X509_STORE_free(ch);
  1145. return rv;
  1146. }
  1147. /* Verbose print out of security callback */
  1148. typedef struct {
  1149. BIO *out;
  1150. int verbose;
  1151. int (*old_cb) (SSL *s, SSL_CTX *ctx, int op, int bits, int nid,
  1152. void *other, void *ex);
  1153. } security_debug_ex;
  1154. static STRINT_PAIR callback_types[] = {
  1155. {"Supported Ciphersuite", SSL_SECOP_CIPHER_SUPPORTED},
  1156. {"Shared Ciphersuite", SSL_SECOP_CIPHER_SHARED},
  1157. {"Check Ciphersuite", SSL_SECOP_CIPHER_CHECK},
  1158. #ifndef OPENSSL_NO_DH
  1159. {"Temp DH key bits", SSL_SECOP_TMP_DH},
  1160. #endif
  1161. {"Supported Curve", SSL_SECOP_CURVE_SUPPORTED},
  1162. {"Shared Curve", SSL_SECOP_CURVE_SHARED},
  1163. {"Check Curve", SSL_SECOP_CURVE_CHECK},
  1164. {"Supported Signature Algorithm digest", SSL_SECOP_SIGALG_SUPPORTED},
  1165. {"Shared Signature Algorithm digest", SSL_SECOP_SIGALG_SHARED},
  1166. {"Check Signature Algorithm digest", SSL_SECOP_SIGALG_CHECK},
  1167. {"Signature Algorithm mask", SSL_SECOP_SIGALG_MASK},
  1168. {"Certificate chain EE key", SSL_SECOP_EE_KEY},
  1169. {"Certificate chain CA key", SSL_SECOP_CA_KEY},
  1170. {"Peer Chain EE key", SSL_SECOP_PEER_EE_KEY},
  1171. {"Peer Chain CA key", SSL_SECOP_PEER_CA_KEY},
  1172. {"Certificate chain CA digest", SSL_SECOP_CA_MD},
  1173. {"Peer chain CA digest", SSL_SECOP_PEER_CA_MD},
  1174. {"SSL compression", SSL_SECOP_COMPRESSION},
  1175. {"Session ticket", SSL_SECOP_TICKET},
  1176. {NULL}
  1177. };
  1178. static int security_callback_debug(SSL *s, SSL_CTX *ctx,
  1179. int op, int bits, int nid,
  1180. void *other, void *ex)
  1181. {
  1182. security_debug_ex *sdb = ex;
  1183. int rv, show_bits = 1, cert_md = 0;
  1184. const char *nm;
  1185. rv = sdb->old_cb(s, ctx, op, bits, nid, other, ex);
  1186. if (rv == 1 && sdb->verbose < 2)
  1187. return 1;
  1188. BIO_puts(sdb->out, "Security callback: ");
  1189. nm = lookup(op, callback_types, NULL);
  1190. switch (op) {
  1191. case SSL_SECOP_TICKET:
  1192. case SSL_SECOP_COMPRESSION:
  1193. show_bits = 0;
  1194. nm = NULL;
  1195. break;
  1196. case SSL_SECOP_VERSION:
  1197. BIO_printf(sdb->out, "Version=%s", lookup(nid, ssl_versions, "???"));
  1198. show_bits = 0;
  1199. nm = NULL;
  1200. break;
  1201. case SSL_SECOP_CA_MD:
  1202. case SSL_SECOP_PEER_CA_MD:
  1203. cert_md = 1;
  1204. break;
  1205. }
  1206. if (nm)
  1207. BIO_printf(sdb->out, "%s=", nm);
  1208. switch (op & SSL_SECOP_OTHER_TYPE) {
  1209. case SSL_SECOP_OTHER_CIPHER:
  1210. BIO_puts(sdb->out, SSL_CIPHER_get_name(other));
  1211. break;
  1212. #ifndef OPENSSL_NO_EC
  1213. case SSL_SECOP_OTHER_CURVE:
  1214. {
  1215. const char *cname;
  1216. cname = EC_curve_nid2nist(nid);
  1217. if (cname == NULL)
  1218. cname = OBJ_nid2sn(nid);
  1219. BIO_puts(sdb->out, cname);
  1220. }
  1221. break;
  1222. #endif
  1223. #ifndef OPENSSL_NO_DH
  1224. case SSL_SECOP_OTHER_DH:
  1225. {
  1226. DH *dh = other;
  1227. BIO_printf(sdb->out, "%d", BN_num_bits(dh->p));
  1228. break;
  1229. }
  1230. #endif
  1231. case SSL_SECOP_OTHER_CERT:
  1232. {
  1233. if (cert_md) {
  1234. int sig_nid = X509_get_signature_nid(other);
  1235. BIO_puts(sdb->out, OBJ_nid2sn(sig_nid));
  1236. } else {
  1237. EVP_PKEY *pkey = X509_get0_pubkey(other);
  1238. const char *algname = "";
  1239. EVP_PKEY_asn1_get0_info(NULL, NULL, NULL, NULL,
  1240. &algname, EVP_PKEY_get0_asn1(pkey));
  1241. BIO_printf(sdb->out, "%s, bits=%d",
  1242. algname, EVP_PKEY_bits(pkey));
  1243. }
  1244. break;
  1245. }
  1246. case SSL_SECOP_OTHER_SIGALG:
  1247. {
  1248. const unsigned char *salg = other;
  1249. const char *sname = NULL;
  1250. switch (salg[1]) {
  1251. case TLSEXT_signature_anonymous:
  1252. sname = "anonymous";
  1253. break;
  1254. case TLSEXT_signature_rsa:
  1255. sname = "RSA";
  1256. break;
  1257. case TLSEXT_signature_dsa:
  1258. sname = "DSA";
  1259. break;
  1260. case TLSEXT_signature_ecdsa:
  1261. sname = "ECDSA";
  1262. break;
  1263. }
  1264. BIO_puts(sdb->out, OBJ_nid2sn(nid));
  1265. if (sname)
  1266. BIO_printf(sdb->out, ", algorithm=%s", sname);
  1267. else
  1268. BIO_printf(sdb->out, ", algid=%d", salg[1]);
  1269. break;
  1270. }
  1271. }
  1272. if (show_bits)
  1273. BIO_printf(sdb->out, ", security bits=%d", bits);
  1274. BIO_printf(sdb->out, ": %s\n", rv ? "yes" : "no");
  1275. return rv;
  1276. }
  1277. void ssl_ctx_security_debug(SSL_CTX *ctx, int verbose)
  1278. {
  1279. static security_debug_ex sdb;
  1280. sdb.out = bio_err;
  1281. sdb.verbose = verbose;
  1282. sdb.old_cb = SSL_CTX_get_security_callback(ctx);
  1283. SSL_CTX_set_security_callback(ctx, security_callback_debug);
  1284. SSL_CTX_set0_security_ex_data(ctx, &sdb);
  1285. }