Dave Tapuska | b8a824d | 2014-12-10 19:09:52 -0500 | [diff] [blame] | 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 | |
| 15 | #include <openssl/base.h> |
| 16 | |
Dave Tapuska | b8a824d | 2014-12-10 19:09:52 -0500 | [diff] [blame] | 17 | #include <string> |
| 18 | #include <vector> |
| 19 | |
| 20 | #include <errno.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <sys/types.h> |
| 23 | |
| 24 | #include <openssl/err.h> |
| 25 | #include <openssl/ssl.h> |
| 26 | |
| 27 | #include "internal.h" |
| 28 | #include "transport_common.h" |
| 29 | |
| 30 | |
| 31 | static const struct argument kArguments[] = { |
| 32 | { |
| 33 | "-accept", true, |
| 34 | "The port of the server to bind on; eg 45102", |
| 35 | }, |
| 36 | { |
| 37 | "-cipher", false, |
| 38 | "An OpenSSL-style cipher suite string that configures the offered ciphers", |
| 39 | }, |
| 40 | { |
| 41 | "-key", false, |
| 42 | "Private-key file to use (default is server.pem)", |
| 43 | }, |
| 44 | { |
| 45 | "", false, "", |
| 46 | }, |
| 47 | }; |
| 48 | |
| 49 | bool Server(const std::vector<std::string> &args) { |
Brian Smith | 33970e6 | 2015-01-27 22:32:08 -0800 | [diff] [blame] | 50 | if (!InitSocketLibrary()) { |
| 51 | return false; |
| 52 | } |
| 53 | |
Dave Tapuska | b8a824d | 2014-12-10 19:09:52 -0500 | [diff] [blame] | 54 | std::map<std::string, std::string> args_map; |
| 55 | |
| 56 | if (!ParseKeyValueArguments(&args_map, args, kArguments)) { |
| 57 | PrintUsage(kArguments); |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | SSL_CTX *ctx = SSL_CTX_new(SSLv23_server_method()); |
| 62 | SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv3); |
| 63 | |
| 64 | // Server authentication is required. |
| 65 | std::string key_file = "server.pem"; |
| 66 | if (args_map.count("-key") != 0) { |
| 67 | key_file = args_map["-key"]; |
| 68 | } |
| 69 | if (SSL_CTX_use_PrivateKey_file(ctx, key_file.c_str(), SSL_FILETYPE_PEM) <= 0) { |
| 70 | fprintf(stderr, "Failed to load private key: %s\n", key_file.c_str()); |
| 71 | return false; |
| 72 | } |
| 73 | if (SSL_CTX_use_certificate_chain_file(ctx, key_file.c_str()) != 1) { |
| 74 | fprintf(stderr, "Failed to load cert chain: %s\n", key_file.c_str()); |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | if (args_map.count("-cipher") != 0 && |
| 79 | !SSL_CTX_set_cipher_list(ctx, args_map["-cipher"].c_str())) { |
| 80 | fprintf(stderr, "Failed setting cipher list\n"); |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | int sock = -1; |
| 85 | if (!Accept(&sock, args_map["-accept"])) { |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | BIO *bio = BIO_new_socket(sock, BIO_CLOSE); |
| 90 | SSL *ssl = SSL_new(ctx); |
| 91 | SSL_set_bio(ssl, bio, bio); |
| 92 | |
| 93 | int ret = SSL_accept(ssl); |
| 94 | if (ret != 1) { |
| 95 | int ssl_err = SSL_get_error(ssl, ret); |
| 96 | fprintf(stderr, "Error while connecting: %d\n", ssl_err); |
| 97 | ERR_print_errors_cb(PrintErrorCallback, stderr); |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | fprintf(stderr, "Connected.\n"); |
| 102 | PrintConnectionInfo(ssl); |
| 103 | |
| 104 | bool ok = TransferData(ssl, sock); |
| 105 | |
| 106 | SSL_free(ssl); |
| 107 | SSL_CTX_free(ctx); |
| 108 | return ok; |
| 109 | } |