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 <openssl/err.h> |
| 18 | #include <openssl/ssl.h> |
| 19 | |
| 20 | #include "internal.h" |
| 21 | #include "transport_common.h" |
| 22 | |
| 23 | |
| 24 | static const struct argument kArguments[] = { |
| 25 | { |
David Benjamin | 0570923 | 2015-03-23 19:01:33 -0400 | [diff] [blame] | 26 | "-accept", kRequiredArgument, |
Dave Tapuska | b8a824d | 2014-12-10 19:09:52 -0500 | [diff] [blame] | 27 | "The port of the server to bind on; eg 45102", |
| 28 | }, |
| 29 | { |
David Benjamin | 0570923 | 2015-03-23 19:01:33 -0400 | [diff] [blame] | 30 | "-cipher", kOptionalArgument, |
Dave Tapuska | b8a824d | 2014-12-10 19:09:52 -0500 | [diff] [blame] | 31 | "An OpenSSL-style cipher suite string that configures the offered ciphers", |
| 32 | }, |
| 33 | { |
David Benjamin | 0570923 | 2015-03-23 19:01:33 -0400 | [diff] [blame] | 34 | "-key", kOptionalArgument, |
Dave Tapuska | b8a824d | 2014-12-10 19:09:52 -0500 | [diff] [blame] | 35 | "Private-key file to use (default is server.pem)", |
| 36 | }, |
| 37 | { |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame^] | 38 | "-ocsp-response", kOptionalArgument, |
| 39 | "OCSP response file to send", |
| 40 | }, |
| 41 | { |
David Benjamin | 0570923 | 2015-03-23 19:01:33 -0400 | [diff] [blame] | 42 | "", kOptionalArgument, "", |
Dave Tapuska | b8a824d | 2014-12-10 19:09:52 -0500 | [diff] [blame] | 43 | }, |
| 44 | }; |
| 45 | |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame^] | 46 | static bool LoadOCSPResponse(SSL_CTX *ctx, const char *filename) { |
| 47 | void *data = NULL; |
| 48 | bool ret = false; |
| 49 | long length; |
| 50 | |
| 51 | FILE *f = fopen(filename, "rb"); |
| 52 | |
| 53 | if (f == NULL || |
| 54 | fseek(f, 0, SEEK_END) != 0) { |
| 55 | goto out; |
| 56 | } |
| 57 | |
| 58 | length = ftell(f); |
| 59 | if (length < 0) { |
| 60 | goto out; |
| 61 | } |
| 62 | |
| 63 | data = malloc(length); |
| 64 | if (data == NULL) { |
| 65 | goto out; |
| 66 | } |
| 67 | rewind(f); |
| 68 | |
| 69 | fread(data, 1, length, f); |
| 70 | if (ferror(f) != 0 || |
| 71 | !SSL_CTX_set_ocsp_response(ctx, (uint8_t*)data, length)) { |
| 72 | goto out; |
| 73 | } |
| 74 | |
| 75 | ret = true; |
| 76 | out: |
| 77 | if (f != NULL) { |
| 78 | fclose(f); |
| 79 | } |
| 80 | free(data); |
| 81 | return ret; |
| 82 | } |
| 83 | |
Dave Tapuska | b8a824d | 2014-12-10 19:09:52 -0500 | [diff] [blame] | 84 | bool Server(const std::vector<std::string> &args) { |
Brian Smith | 33970e6 | 2015-01-27 22:32:08 -0800 | [diff] [blame] | 85 | if (!InitSocketLibrary()) { |
| 86 | return false; |
| 87 | } |
| 88 | |
Dave Tapuska | b8a824d | 2014-12-10 19:09:52 -0500 | [diff] [blame] | 89 | std::map<std::string, std::string> args_map; |
| 90 | |
| 91 | if (!ParseKeyValueArguments(&args_map, args, kArguments)) { |
| 92 | PrintUsage(kArguments); |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | SSL_CTX *ctx = SSL_CTX_new(SSLv23_server_method()); |
| 97 | SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv3); |
| 98 | |
| 99 | // Server authentication is required. |
| 100 | std::string key_file = "server.pem"; |
| 101 | if (args_map.count("-key") != 0) { |
| 102 | key_file = args_map["-key"]; |
| 103 | } |
| 104 | if (SSL_CTX_use_PrivateKey_file(ctx, key_file.c_str(), SSL_FILETYPE_PEM) <= 0) { |
| 105 | fprintf(stderr, "Failed to load private key: %s\n", key_file.c_str()); |
| 106 | return false; |
| 107 | } |
| 108 | if (SSL_CTX_use_certificate_chain_file(ctx, key_file.c_str()) != 1) { |
| 109 | fprintf(stderr, "Failed to load cert chain: %s\n", key_file.c_str()); |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | if (args_map.count("-cipher") != 0 && |
| 114 | !SSL_CTX_set_cipher_list(ctx, args_map["-cipher"].c_str())) { |
| 115 | fprintf(stderr, "Failed setting cipher list\n"); |
| 116 | return false; |
| 117 | } |
| 118 | |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame^] | 119 | if (args_map.count("-ocsp-response") != 0 && |
| 120 | !LoadOCSPResponse(ctx, args_map["-ocsp-response"].c_str())) { |
| 121 | fprintf(stderr, "Failed to load OCSP response: %s\n", args_map["-ocsp-response"].c_str()); |
| 122 | return false; |
| 123 | } |
| 124 | |
Dave Tapuska | b8a824d | 2014-12-10 19:09:52 -0500 | [diff] [blame] | 125 | int sock = -1; |
| 126 | if (!Accept(&sock, args_map["-accept"])) { |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | BIO *bio = BIO_new_socket(sock, BIO_CLOSE); |
| 131 | SSL *ssl = SSL_new(ctx); |
| 132 | SSL_set_bio(ssl, bio, bio); |
| 133 | |
| 134 | int ret = SSL_accept(ssl); |
| 135 | if (ret != 1) { |
| 136 | int ssl_err = SSL_get_error(ssl, ret); |
| 137 | fprintf(stderr, "Error while connecting: %d\n", ssl_err); |
| 138 | ERR_print_errors_cb(PrintErrorCallback, stderr); |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | fprintf(stderr, "Connected.\n"); |
| 143 | PrintConnectionInfo(ssl); |
| 144 | |
| 145 | bool ok = TransferData(ssl, sock); |
| 146 | |
| 147 | SSL_free(ssl); |
| 148 | SSL_CTX_free(ctx); |
| 149 | return ok; |
| 150 | } |