Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

312 righe
9.2 KiB

  1. /* Copyright (c) 2014, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <openssl/base.h>
  15. #include <stdio.h>
  16. #include <openssl/err.h>
  17. #include <openssl/pem.h>
  18. #include <openssl/ssl.h>
  19. #include "../crypto/test/scoped_types.h"
  20. #include "../ssl/test/scoped_types.h"
  21. #include "internal.h"
  22. #include "transport_common.h"
  23. static const struct argument kArguments[] = {
  24. {
  25. "-connect", kRequiredArgument,
  26. "The hostname and port of the server to connect to, e.g. foo.com:443",
  27. },
  28. {
  29. "-cipher", kOptionalArgument,
  30. "An OpenSSL-style cipher suite string that configures the offered ciphers",
  31. },
  32. {
  33. "-max-version", kOptionalArgument,
  34. "The maximum acceptable protocol version",
  35. },
  36. {
  37. "-min-version", kOptionalArgument,
  38. "The minimum acceptable protocol version",
  39. },
  40. {
  41. "-server-name", kOptionalArgument,
  42. "The server name to advertise",
  43. },
  44. {
  45. "-select-next-proto", kOptionalArgument,
  46. "An NPN protocol to select if the server supports NPN",
  47. },
  48. {
  49. "-alpn-protos", kOptionalArgument,
  50. "A comma-separated list of ALPN protocols to advertise",
  51. },
  52. {
  53. "-fallback-scsv", kBooleanArgument,
  54. "Enable FALLBACK_SCSV",
  55. },
  56. {
  57. "-ocsp-stapling", kBooleanArgument,
  58. "Advertise support for OCSP stabling",
  59. },
  60. {
  61. "-signed-certificate-timestamps", kBooleanArgument,
  62. "Advertise support for signed certificate timestamps",
  63. },
  64. {
  65. "-channel-id-key", kOptionalArgument,
  66. "The key to use for signing a channel ID",
  67. },
  68. {
  69. "-false-start", kBooleanArgument,
  70. "Enable False Start",
  71. },
  72. { "-session-in", kOptionalArgument,
  73. "A file containing a session to resume.",
  74. },
  75. { "-session-out", kOptionalArgument,
  76. "A file to write the negotiated session to.",
  77. },
  78. {
  79. "-key", kOptionalArgument,
  80. "Private-key file to use (default is no client certificate)",
  81. },
  82. {
  83. "", kOptionalArgument, "",
  84. },
  85. };
  86. static ScopedEVP_PKEY LoadPrivateKey(const std::string &file) {
  87. ScopedBIO bio(BIO_new(BIO_s_file()));
  88. if (!bio || !BIO_read_filename(bio.get(), file.c_str())) {
  89. return nullptr;
  90. }
  91. ScopedEVP_PKEY pkey(PEM_read_bio_PrivateKey(bio.get(), nullptr, nullptr,
  92. nullptr));
  93. return pkey;
  94. }
  95. static bool VersionFromString(uint16_t *out_version,
  96. const std::string& version) {
  97. if (version == "ssl3") {
  98. *out_version = SSL3_VERSION;
  99. return true;
  100. } else if (version == "tls1" || version == "tls1.0") {
  101. *out_version = TLS1_VERSION;
  102. return true;
  103. } else if (version == "tls1.1") {
  104. *out_version = TLS1_1_VERSION;
  105. return true;
  106. } else if (version == "tls1.2") {
  107. *out_version = TLS1_2_VERSION;
  108. return true;
  109. }
  110. return false;
  111. }
  112. static int NextProtoSelectCallback(SSL* ssl, uint8_t** out, uint8_t* outlen,
  113. const uint8_t* in, unsigned inlen, void* arg) {
  114. *out = reinterpret_cast<uint8_t *>(arg);
  115. *outlen = strlen(reinterpret_cast<const char *>(arg));
  116. return SSL_TLSEXT_ERR_OK;
  117. }
  118. static FILE *g_keylog_file = nullptr;
  119. static void KeyLogCallback(const SSL *ssl, const char *line) {
  120. fprintf(g_keylog_file, "%s\n", line);
  121. fflush(g_keylog_file);
  122. }
  123. bool Client(const std::vector<std::string> &args) {
  124. if (!InitSocketLibrary()) {
  125. return false;
  126. }
  127. std::map<std::string, std::string> args_map;
  128. if (!ParseKeyValueArguments(&args_map, args, kArguments)) {
  129. PrintUsage(kArguments);
  130. return false;
  131. }
  132. ScopedSSL_CTX ctx(SSL_CTX_new(SSLv23_client_method()));
  133. const char *keylog_file = getenv("SSLKEYLOGFILE");
  134. if (keylog_file) {
  135. g_keylog_file = fopen(keylog_file, "a");
  136. if (g_keylog_file == nullptr) {
  137. perror("fopen");
  138. return false;
  139. }
  140. SSL_CTX_set_keylog_callback(ctx.get(), KeyLogCallback);
  141. }
  142. if (args_map.count("-cipher") != 0 &&
  143. !SSL_CTX_set_cipher_list(ctx.get(), args_map["-cipher"].c_str())) {
  144. fprintf(stderr, "Failed setting cipher list\n");
  145. return false;
  146. }
  147. if (args_map.count("-max-version") != 0) {
  148. uint16_t version;
  149. if (!VersionFromString(&version, args_map["-max-version"])) {
  150. fprintf(stderr, "Unknown protocol version: '%s'\n",
  151. args_map["-max-version"].c_str());
  152. return false;
  153. }
  154. SSL_CTX_set_max_version(ctx.get(), version);
  155. }
  156. if (args_map.count("-min-version") != 0) {
  157. uint16_t version;
  158. if (!VersionFromString(&version, args_map["-min-version"])) {
  159. fprintf(stderr, "Unknown protocol version: '%s'\n",
  160. args_map["-min-version"].c_str());
  161. return false;
  162. }
  163. SSL_CTX_set_min_version(ctx.get(), version);
  164. }
  165. if (args_map.count("-select-next-proto") != 0) {
  166. const std::string &proto = args_map["-select-next-proto"];
  167. if (proto.size() > 255) {
  168. fprintf(stderr, "Bad NPN protocol: '%s'\n", proto.c_str());
  169. return false;
  170. }
  171. // |SSL_CTX_set_next_proto_select_cb| is not const-correct.
  172. SSL_CTX_set_next_proto_select_cb(ctx.get(), NextProtoSelectCallback,
  173. const_cast<char *>(proto.c_str()));
  174. }
  175. if (args_map.count("-alpn-protos") != 0) {
  176. const std::string &alpn_protos = args_map["-alpn-protos"];
  177. std::vector<uint8_t> wire;
  178. size_t i = 0;
  179. while (i <= alpn_protos.size()) {
  180. size_t j = alpn_protos.find(',', i);
  181. if (j == std::string::npos) {
  182. j = alpn_protos.size();
  183. }
  184. size_t len = j - i;
  185. if (len > 255) {
  186. fprintf(stderr, "Invalid ALPN protocols: '%s'\n", alpn_protos.c_str());
  187. return false;
  188. }
  189. wire.push_back(static_cast<uint8_t>(len));
  190. wire.resize(wire.size() + len);
  191. memcpy(wire.data() + wire.size() - len, alpn_protos.data() + i, len);
  192. i = j + 1;
  193. }
  194. if (SSL_CTX_set_alpn_protos(ctx.get(), wire.data(), wire.size()) != 0) {
  195. return false;
  196. }
  197. }
  198. if (args_map.count("-fallback-scsv") != 0) {
  199. SSL_CTX_set_mode(ctx.get(), SSL_MODE_SEND_FALLBACK_SCSV);
  200. }
  201. if (args_map.count("-ocsp-stapling") != 0) {
  202. SSL_CTX_enable_ocsp_stapling(ctx.get());
  203. }
  204. if (args_map.count("-signed-certificate-timestamps") != 0) {
  205. SSL_CTX_enable_signed_cert_timestamps(ctx.get());
  206. }
  207. if (args_map.count("-channel-id-key") != 0) {
  208. ScopedEVP_PKEY pkey = LoadPrivateKey(args_map["-channel-id-key"]);
  209. if (!pkey || !SSL_CTX_set1_tls_channel_id(ctx.get(), pkey.get())) {
  210. return false;
  211. }
  212. }
  213. if (args_map.count("-false-start") != 0) {
  214. SSL_CTX_set_mode(ctx.get(), SSL_MODE_ENABLE_FALSE_START);
  215. }
  216. if (args_map.count("-key") != 0) {
  217. const std::string &key = args_map["-key"];
  218. if (!SSL_CTX_use_PrivateKey_file(ctx.get(), key.c_str(), SSL_FILETYPE_PEM)) {
  219. fprintf(stderr, "Failed to load private key: %s\n", key.c_str());
  220. return false;
  221. }
  222. if (!SSL_CTX_use_certificate_chain_file(ctx.get(), key.c_str())) {
  223. fprintf(stderr, "Failed to load cert chain: %s\n", key.c_str());
  224. return false;
  225. }
  226. }
  227. int sock = -1;
  228. if (!Connect(&sock, args_map["-connect"])) {
  229. return false;
  230. }
  231. ScopedBIO bio(BIO_new_socket(sock, BIO_CLOSE));
  232. ScopedSSL ssl(SSL_new(ctx.get()));
  233. if (args_map.count("-server-name") != 0) {
  234. SSL_set_tlsext_host_name(ssl.get(), args_map["-server-name"].c_str());
  235. }
  236. if (args_map.count("-session-in") != 0) {
  237. ScopedBIO in(BIO_new_file(args_map["-session-in"].c_str(), "rb"));
  238. if (!in) {
  239. fprintf(stderr, "Error reading session\n");
  240. ERR_print_errors_cb(PrintErrorCallback, stderr);
  241. return false;
  242. }
  243. ScopedSSL_SESSION session(PEM_read_bio_SSL_SESSION(in.get(), nullptr,
  244. nullptr, nullptr));
  245. if (!session) {
  246. fprintf(stderr, "Error reading session\n");
  247. ERR_print_errors_cb(PrintErrorCallback, stderr);
  248. return false;
  249. }
  250. SSL_set_session(ssl.get(), session.get());
  251. }
  252. SSL_set_bio(ssl.get(), bio.get(), bio.get());
  253. bio.release();
  254. int ret = SSL_connect(ssl.get());
  255. if (ret != 1) {
  256. int ssl_err = SSL_get_error(ssl.get(), ret);
  257. fprintf(stderr, "Error while connecting: %d\n", ssl_err);
  258. ERR_print_errors_cb(PrintErrorCallback, stderr);
  259. return false;
  260. }
  261. fprintf(stderr, "Connected.\n");
  262. PrintConnectionInfo(ssl.get());
  263. if (args_map.count("-session-out") != 0) {
  264. ScopedBIO out(BIO_new_file(args_map["-session-out"].c_str(), "wb"));
  265. if (!out ||
  266. !PEM_write_bio_SSL_SESSION(out.get(), SSL_get0_session(ssl.get()))) {
  267. fprintf(stderr, "Error while saving session:\n");
  268. ERR_print_errors_cb(PrintErrorCallback, stderr);
  269. return false;
  270. }
  271. }
  272. bool ok = TransferData(ssl.get(), sock);
  273. return ok;
  274. }