Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [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 | |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 17 | #include <memory> |
| 18 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 19 | #include <openssl/err.h> |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 20 | #include <openssl/rand.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 21 | #include <openssl/ssl.h> |
| 22 | |
| 23 | #include "internal.h" |
| 24 | #include "transport_common.h" |
| 25 | |
| 26 | |
| 27 | static const struct argument kArguments[] = { |
| 28 | { |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 29 | "-accept", kRequiredArgument, |
| 30 | "The port of the server to bind on; eg 45102", |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 31 | }, |
| 32 | { |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 33 | "-cipher", kOptionalArgument, |
| 34 | "An OpenSSL-style cipher suite string that configures the offered " |
| 35 | "ciphers", |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 36 | }, |
| 37 | { |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 38 | "-curves", kOptionalArgument, |
| 39 | "An OpenSSL-style ECDH curves list that configures the offered curves", |
| 40 | }, |
| 41 | { |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 42 | "-max-version", kOptionalArgument, |
| 43 | "The maximum acceptable protocol version", |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 44 | }, |
| 45 | { |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 46 | "-min-version", kOptionalArgument, |
| 47 | "The minimum acceptable protocol version", |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 48 | }, |
| 49 | { |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 50 | "-key", kOptionalArgument, |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 51 | "PEM-encoded file containing the private key. A self-signed " |
| 52 | "certificate is generated at runtime if this argument is not provided.", |
| 53 | }, |
| 54 | { |
| 55 | "-cert", kOptionalArgument, |
| 56 | "PEM-encoded file containing the leaf certificate and optional " |
| 57 | "certificate chain. This is taken from the -key argument if this " |
| 58 | "argument is not provided.", |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 59 | }, |
| 60 | { |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 61 | "-ocsp-response", kOptionalArgument, "OCSP response file to send", |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 62 | }, |
| 63 | { |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 64 | "-loop", kBooleanArgument, |
| 65 | "The server will continue accepting new sequential connections.", |
| 66 | }, |
| 67 | { |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 68 | "-early-data", kBooleanArgument, "Allow early data", |
| 69 | }, |
| 70 | { |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame^] | 71 | "-tls13-variant", kBooleanArgument, "Enable TLS 1.3 variants", |
| 72 | }, |
| 73 | { |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 74 | "", kOptionalArgument, "", |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 75 | }, |
| 76 | }; |
| 77 | |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 78 | struct FileCloser { |
| 79 | void operator()(FILE *file) { |
| 80 | fclose(file); |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | using ScopedFILE = std::unique_ptr<FILE, FileCloser>; |
| 85 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 86 | static bool LoadOCSPResponse(SSL_CTX *ctx, const char *filename) { |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 87 | ScopedFILE f(fopen(filename, "rb")); |
| 88 | std::vector<uint8_t> data; |
| 89 | if (f == nullptr || |
| 90 | !ReadAll(&data, f.get())) { |
| 91 | fprintf(stderr, "Error reading %s.\n", filename); |
| 92 | return false; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 95 | if (!SSL_CTX_set_ocsp_response(ctx, data.data(), data.size())) { |
| 96 | return false; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 99 | return true; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 100 | } |
| 101 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 102 | static bssl::UniquePtr<EVP_PKEY> MakeKeyPairForSelfSignedCert() { |
| 103 | bssl::UniquePtr<EC_KEY> ec_key(EC_KEY_new_by_curve_name(NID_X9_62_prime256v1)); |
| 104 | if (!ec_key || !EC_KEY_generate_key(ec_key.get())) { |
| 105 | fprintf(stderr, "Failed to generate key pair.\n"); |
| 106 | return nullptr; |
| 107 | } |
| 108 | bssl::UniquePtr<EVP_PKEY> evp_pkey(EVP_PKEY_new()); |
| 109 | if (!evp_pkey || !EVP_PKEY_assign_EC_KEY(evp_pkey.get(), ec_key.release())) { |
| 110 | fprintf(stderr, "Failed to assign key pair.\n"); |
| 111 | return nullptr; |
| 112 | } |
| 113 | return evp_pkey; |
| 114 | } |
| 115 | |
| 116 | static bssl::UniquePtr<X509> MakeSelfSignedCert(EVP_PKEY *evp_pkey, |
| 117 | const int valid_days) { |
| 118 | bssl::UniquePtr<X509> x509(X509_new()); |
| 119 | uint32_t serial; |
| 120 | RAND_bytes(reinterpret_cast<uint8_t*>(&serial), sizeof(serial)); |
| 121 | ASN1_INTEGER_set(X509_get_serialNumber(x509.get()), serial >> 1); |
| 122 | X509_gmtime_adj(X509_get_notBefore(x509.get()), 0); |
| 123 | X509_gmtime_adj(X509_get_notAfter(x509.get()), 60 * 60 * 24 * valid_days); |
| 124 | |
| 125 | X509_NAME* subject = X509_get_subject_name(x509.get()); |
| 126 | X509_NAME_add_entry_by_txt(subject, "C", MBSTRING_ASC, |
| 127 | reinterpret_cast<const uint8_t *>("US"), -1, -1, |
| 128 | 0); |
| 129 | X509_NAME_add_entry_by_txt(subject, "O", MBSTRING_ASC, |
| 130 | reinterpret_cast<const uint8_t *>("BoringSSL"), -1, |
| 131 | -1, 0); |
| 132 | X509_set_issuer_name(x509.get(), subject); |
| 133 | |
| 134 | if (!X509_set_pubkey(x509.get(), evp_pkey)) { |
| 135 | fprintf(stderr, "Failed to set public key.\n"); |
| 136 | return nullptr; |
| 137 | } |
| 138 | if (!X509_sign(x509.get(), evp_pkey, EVP_sha256())) { |
| 139 | fprintf(stderr, "Failed to sign certificate.\n"); |
| 140 | return nullptr; |
| 141 | } |
| 142 | return x509; |
| 143 | } |
| 144 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 145 | bool Server(const std::vector<std::string> &args) { |
| 146 | if (!InitSocketLibrary()) { |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | std::map<std::string, std::string> args_map; |
| 151 | |
| 152 | if (!ParseKeyValueArguments(&args_map, args, kArguments)) { |
| 153 | PrintUsage(kArguments); |
| 154 | return false; |
| 155 | } |
| 156 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 157 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 158 | |
| 159 | // Server authentication is required. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 160 | if (args_map.count("-key") != 0) { |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 161 | std::string key = args_map["-key"]; |
| 162 | if (!SSL_CTX_use_PrivateKey_file(ctx.get(), key.c_str(), |
| 163 | SSL_FILETYPE_PEM)) { |
| 164 | fprintf(stderr, "Failed to load private key: %s\n", key.c_str()); |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 165 | return false; |
| 166 | } |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 167 | const std::string &cert = |
| 168 | args_map.count("-cert") != 0 ? args_map["-cert"] : key; |
| 169 | if (!SSL_CTX_use_certificate_chain_file(ctx.get(), cert.c_str())) { |
| 170 | fprintf(stderr, "Failed to load cert chain: %s\n", cert.c_str()); |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 171 | return false; |
| 172 | } |
| 173 | } else { |
| 174 | bssl::UniquePtr<EVP_PKEY> evp_pkey = MakeKeyPairForSelfSignedCert(); |
| 175 | if (!evp_pkey) { |
| 176 | return false; |
| 177 | } |
| 178 | bssl::UniquePtr<X509> cert = |
| 179 | MakeSelfSignedCert(evp_pkey.get(), 365 /* valid_days */); |
| 180 | if (!cert) { |
| 181 | return false; |
| 182 | } |
| 183 | if (!SSL_CTX_use_PrivateKey(ctx.get(), evp_pkey.get())) { |
| 184 | fprintf(stderr, "Failed to set private key.\n"); |
| 185 | return false; |
| 186 | } |
| 187 | if (!SSL_CTX_use_certificate(ctx.get(), cert.get())) { |
| 188 | fprintf(stderr, "Failed to set certificate.\n"); |
| 189 | return false; |
| 190 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | if (args_map.count("-cipher") != 0 && |
Robert Sloan | 7c50ec5 | 2017-02-27 08:17:21 -0800 | [diff] [blame] | 194 | !SSL_CTX_set_strict_cipher_list(ctx.get(), args_map["-cipher"].c_str())) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 195 | fprintf(stderr, "Failed setting cipher list\n"); |
| 196 | return false; |
| 197 | } |
| 198 | |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 199 | if (args_map.count("-curves") != 0 && |
| 200 | !SSL_CTX_set1_curves_list(ctx.get(), args_map["-curves"].c_str())) { |
| 201 | fprintf(stderr, "Failed setting curves list\n"); |
| 202 | return false; |
| 203 | } |
| 204 | |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 205 | uint16_t max_version = TLS1_3_VERSION; |
| 206 | if (args_map.count("-max-version") != 0 && |
| 207 | !VersionFromString(&max_version, args_map["-max-version"])) { |
| 208 | fprintf(stderr, "Unknown protocol version: '%s'\n", |
| 209 | args_map["-max-version"].c_str()); |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | if (!SSL_CTX_set_max_proto_version(ctx.get(), max_version)) { |
| 214 | return false; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | if (args_map.count("-min-version") != 0) { |
| 218 | uint16_t version; |
| 219 | if (!VersionFromString(&version, args_map["-min-version"])) { |
| 220 | fprintf(stderr, "Unknown protocol version: '%s'\n", |
| 221 | args_map["-min-version"].c_str()); |
| 222 | return false; |
| 223 | } |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 224 | if (!SSL_CTX_set_min_proto_version(ctx.get(), version)) { |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 225 | return false; |
| 226 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 227 | } |
| 228 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 229 | if (args_map.count("-ocsp-response") != 0 && |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 230 | !LoadOCSPResponse(ctx.get(), args_map["-ocsp-response"].c_str())) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 231 | fprintf(stderr, "Failed to load OCSP response: %s\n", args_map["-ocsp-response"].c_str()); |
| 232 | return false; |
| 233 | } |
| 234 | |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 235 | if (args_map.count("-early-data") != 0) { |
| 236 | SSL_CTX_set_early_data_enabled(ctx.get(), 1); |
| 237 | } |
| 238 | |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame^] | 239 | // Enabling any TLS 1.3 variant on the server enables all of them. |
| 240 | if (args_map.count("-tls13-variant") != 0) { |
| 241 | SSL_CTX_set_tls13_variant(ctx.get(), tls13_experiment); |
| 242 | } |
| 243 | |
Robert Sloan | 927a495 | 2017-07-03 11:25:09 -0700 | [diff] [blame] | 244 | Listener listener; |
| 245 | if (!listener.Init(args_map["-accept"])) { |
| 246 | return false; |
| 247 | } |
| 248 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 249 | bool result = true; |
| 250 | do { |
| 251 | int sock = -1; |
Robert Sloan | 927a495 | 2017-07-03 11:25:09 -0700 | [diff] [blame] | 252 | if (!listener.Accept(&sock)) { |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 253 | return false; |
| 254 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 255 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 256 | BIO *bio = BIO_new_socket(sock, BIO_CLOSE); |
| 257 | bssl::UniquePtr<SSL> ssl(SSL_new(ctx.get())); |
| 258 | SSL_set_bio(ssl.get(), bio, bio); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 259 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 260 | int ret = SSL_accept(ssl.get()); |
| 261 | if (ret != 1) { |
| 262 | int ssl_err = SSL_get_error(ssl.get(), ret); |
| 263 | fprintf(stderr, "Error while connecting: %d\n", ssl_err); |
| 264 | ERR_print_errors_cb(PrintErrorCallback, stderr); |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 265 | result = false; |
| 266 | continue; |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 267 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 268 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 269 | fprintf(stderr, "Connected.\n"); |
| 270 | PrintConnectionInfo(ssl.get()); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 271 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 272 | result = TransferData(ssl.get(), sock); |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 273 | } while (args_map.count("-loop") != 0); |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 274 | |
| 275 | return result; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 276 | } |