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.
 
 
 
 
 
 

104 righe
2.7 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. // TODO(davidben): bssl client does not work on Windows.
  16. #if !defined(OPENSSL_WINDOWS)
  17. #include <string>
  18. #include <vector>
  19. #include <errno.h>
  20. #include <stdlib.h>
  21. #include <sys/types.h>
  22. #include <sys/socket.h>
  23. #include <openssl/err.h>
  24. #include <openssl/ssl.h>
  25. #include "internal.h"
  26. #include "transport_common.h"
  27. static const struct argument kArguments[] = {
  28. {
  29. "-connect", true,
  30. "The hostname and port of the server to connect to, e.g. foo.com:443",
  31. },
  32. {
  33. "-cipher", false,
  34. "An OpenSSL-style cipher suite string that configures the offered ciphers",
  35. },
  36. {
  37. "", false, "",
  38. },
  39. };
  40. bool Client(const std::vector<std::string> &args) {
  41. std::map<std::string, std::string> args_map;
  42. if (!ParseKeyValueArguments(&args_map, args, kArguments)) {
  43. PrintUsage(kArguments);
  44. return false;
  45. }
  46. SSL_CTX *ctx = SSL_CTX_new(SSLv23_client_method());
  47. const char *keylog_file = getenv("SSLKEYLOGFILE");
  48. if (keylog_file) {
  49. BIO *keylog_bio = BIO_new_file(keylog_file, "a");
  50. if (!keylog_bio) {
  51. ERR_print_errors_cb(PrintErrorCallback, stderr);
  52. return false;
  53. }
  54. SSL_CTX_set_keylog_bio(ctx, keylog_bio);
  55. }
  56. if (args_map.count("-cipher") != 0 &&
  57. !SSL_CTX_set_cipher_list(ctx, args_map["-cipher"].c_str())) {
  58. fprintf(stderr, "Failed setting cipher list\n");
  59. return false;
  60. }
  61. int sock = -1;
  62. if (!Connect(&sock, args_map["-connect"])) {
  63. return false;
  64. }
  65. BIO *bio = BIO_new_socket(sock, BIO_CLOSE);
  66. SSL *ssl = SSL_new(ctx);
  67. SSL_set_bio(ssl, bio, bio);
  68. int ret = SSL_connect(ssl);
  69. if (ret != 1) {
  70. int ssl_err = SSL_get_error(ssl, ret);
  71. fprintf(stderr, "Error while connecting: %d\n", ssl_err);
  72. ERR_print_errors_cb(PrintErrorCallback, stderr);
  73. return false;
  74. }
  75. fprintf(stderr, "Connected.\n");
  76. PrintConnectionInfo(ssl);
  77. bool ok = TransferData(ssl, sock);
  78. SSL_free(ssl);
  79. SSL_CTX_free(ctx);
  80. return ok;
  81. }
  82. #endif // !OPENSSL_WINDOWS