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 | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 71 | "", kOptionalArgument, "", |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 72 | }, |
| 73 | }; |
| 74 | |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 75 | struct FileCloser { |
| 76 | void operator()(FILE *file) { |
| 77 | fclose(file); |
| 78 | } |
| 79 | }; |
| 80 | |
| 81 | using ScopedFILE = std::unique_ptr<FILE, FileCloser>; |
| 82 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 83 | static bool LoadOCSPResponse(SSL_CTX *ctx, const char *filename) { |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 84 | ScopedFILE f(fopen(filename, "rb")); |
| 85 | std::vector<uint8_t> data; |
| 86 | if (f == nullptr || |
| 87 | !ReadAll(&data, f.get())) { |
| 88 | fprintf(stderr, "Error reading %s.\n", filename); |
| 89 | return false; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 92 | if (!SSL_CTX_set_ocsp_response(ctx, data.data(), data.size())) { |
| 93 | return false; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 96 | return true; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 97 | } |
| 98 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 99 | static bssl::UniquePtr<EVP_PKEY> MakeKeyPairForSelfSignedCert() { |
| 100 | bssl::UniquePtr<EC_KEY> ec_key(EC_KEY_new_by_curve_name(NID_X9_62_prime256v1)); |
| 101 | if (!ec_key || !EC_KEY_generate_key(ec_key.get())) { |
| 102 | fprintf(stderr, "Failed to generate key pair.\n"); |
| 103 | return nullptr; |
| 104 | } |
| 105 | bssl::UniquePtr<EVP_PKEY> evp_pkey(EVP_PKEY_new()); |
| 106 | if (!evp_pkey || !EVP_PKEY_assign_EC_KEY(evp_pkey.get(), ec_key.release())) { |
| 107 | fprintf(stderr, "Failed to assign key pair.\n"); |
| 108 | return nullptr; |
| 109 | } |
| 110 | return evp_pkey; |
| 111 | } |
| 112 | |
| 113 | static bssl::UniquePtr<X509> MakeSelfSignedCert(EVP_PKEY *evp_pkey, |
| 114 | const int valid_days) { |
| 115 | bssl::UniquePtr<X509> x509(X509_new()); |
| 116 | uint32_t serial; |
| 117 | RAND_bytes(reinterpret_cast<uint8_t*>(&serial), sizeof(serial)); |
| 118 | ASN1_INTEGER_set(X509_get_serialNumber(x509.get()), serial >> 1); |
| 119 | X509_gmtime_adj(X509_get_notBefore(x509.get()), 0); |
| 120 | X509_gmtime_adj(X509_get_notAfter(x509.get()), 60 * 60 * 24 * valid_days); |
| 121 | |
| 122 | X509_NAME* subject = X509_get_subject_name(x509.get()); |
| 123 | X509_NAME_add_entry_by_txt(subject, "C", MBSTRING_ASC, |
| 124 | reinterpret_cast<const uint8_t *>("US"), -1, -1, |
| 125 | 0); |
| 126 | X509_NAME_add_entry_by_txt(subject, "O", MBSTRING_ASC, |
| 127 | reinterpret_cast<const uint8_t *>("BoringSSL"), -1, |
| 128 | -1, 0); |
| 129 | X509_set_issuer_name(x509.get(), subject); |
| 130 | |
| 131 | if (!X509_set_pubkey(x509.get(), evp_pkey)) { |
| 132 | fprintf(stderr, "Failed to set public key.\n"); |
| 133 | return nullptr; |
| 134 | } |
| 135 | if (!X509_sign(x509.get(), evp_pkey, EVP_sha256())) { |
| 136 | fprintf(stderr, "Failed to sign certificate.\n"); |
| 137 | return nullptr; |
| 138 | } |
| 139 | return x509; |
| 140 | } |
| 141 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 142 | bool Server(const std::vector<std::string> &args) { |
| 143 | if (!InitSocketLibrary()) { |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | std::map<std::string, std::string> args_map; |
| 148 | |
| 149 | if (!ParseKeyValueArguments(&args_map, args, kArguments)) { |
| 150 | PrintUsage(kArguments); |
| 151 | return false; |
| 152 | } |
| 153 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 154 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
| 155 | SSL_CTX_set_options(ctx.get(), SSL_OP_NO_SSLv3); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 156 | |
| 157 | // Server authentication is required. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 158 | if (args_map.count("-key") != 0) { |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 159 | std::string key = args_map["-key"]; |
| 160 | if (!SSL_CTX_use_PrivateKey_file(ctx.get(), key.c_str(), |
| 161 | SSL_FILETYPE_PEM)) { |
| 162 | fprintf(stderr, "Failed to load private key: %s\n", key.c_str()); |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 163 | return false; |
| 164 | } |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 165 | const std::string &cert = |
| 166 | args_map.count("-cert") != 0 ? args_map["-cert"] : key; |
| 167 | if (!SSL_CTX_use_certificate_chain_file(ctx.get(), cert.c_str())) { |
| 168 | fprintf(stderr, "Failed to load cert chain: %s\n", cert.c_str()); |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 169 | return false; |
| 170 | } |
| 171 | } else { |
| 172 | bssl::UniquePtr<EVP_PKEY> evp_pkey = MakeKeyPairForSelfSignedCert(); |
| 173 | if (!evp_pkey) { |
| 174 | return false; |
| 175 | } |
| 176 | bssl::UniquePtr<X509> cert = |
| 177 | MakeSelfSignedCert(evp_pkey.get(), 365 /* valid_days */); |
| 178 | if (!cert) { |
| 179 | return false; |
| 180 | } |
| 181 | if (!SSL_CTX_use_PrivateKey(ctx.get(), evp_pkey.get())) { |
| 182 | fprintf(stderr, "Failed to set private key.\n"); |
| 183 | return false; |
| 184 | } |
| 185 | if (!SSL_CTX_use_certificate(ctx.get(), cert.get())) { |
| 186 | fprintf(stderr, "Failed to set certificate.\n"); |
| 187 | return false; |
| 188 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | if (args_map.count("-cipher") != 0 && |
Robert Sloan | 7c50ec5 | 2017-02-27 08:17:21 -0800 | [diff] [blame] | 192 | !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] | 193 | fprintf(stderr, "Failed setting cipher list\n"); |
| 194 | return false; |
| 195 | } |
| 196 | |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 197 | if (args_map.count("-curves") != 0 && |
| 198 | !SSL_CTX_set1_curves_list(ctx.get(), args_map["-curves"].c_str())) { |
| 199 | fprintf(stderr, "Failed setting curves list\n"); |
| 200 | return false; |
| 201 | } |
| 202 | |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 203 | uint16_t max_version = TLS1_3_VERSION; |
| 204 | if (args_map.count("-max-version") != 0 && |
| 205 | !VersionFromString(&max_version, args_map["-max-version"])) { |
| 206 | fprintf(stderr, "Unknown protocol version: '%s'\n", |
| 207 | args_map["-max-version"].c_str()); |
| 208 | return false; |
| 209 | } |
| 210 | |
| 211 | if (!SSL_CTX_set_max_proto_version(ctx.get(), max_version)) { |
| 212 | return false; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | if (args_map.count("-min-version") != 0) { |
| 216 | uint16_t version; |
| 217 | if (!VersionFromString(&version, args_map["-min-version"])) { |
| 218 | fprintf(stderr, "Unknown protocol version: '%s'\n", |
| 219 | args_map["-min-version"].c_str()); |
| 220 | return false; |
| 221 | } |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 222 | if (!SSL_CTX_set_min_proto_version(ctx.get(), version)) { |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 223 | return false; |
| 224 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 225 | } |
| 226 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 227 | if (args_map.count("-ocsp-response") != 0 && |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 228 | !LoadOCSPResponse(ctx.get(), args_map["-ocsp-response"].c_str())) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 229 | fprintf(stderr, "Failed to load OCSP response: %s\n", args_map["-ocsp-response"].c_str()); |
| 230 | return false; |
| 231 | } |
| 232 | |
Robert Sloan | 6d0d00e | 2017-03-27 07:13:07 -0700 | [diff] [blame] | 233 | if (args_map.count("-early-data") != 0) { |
| 234 | SSL_CTX_set_early_data_enabled(ctx.get(), 1); |
| 235 | } |
| 236 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 237 | bool result = true; |
| 238 | do { |
| 239 | int sock = -1; |
| 240 | if (!Accept(&sock, args_map["-accept"])) { |
| 241 | return false; |
| 242 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 243 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 244 | BIO *bio = BIO_new_socket(sock, BIO_CLOSE); |
| 245 | bssl::UniquePtr<SSL> ssl(SSL_new(ctx.get())); |
| 246 | SSL_set_bio(ssl.get(), bio, bio); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 247 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 248 | int ret = SSL_accept(ssl.get()); |
| 249 | if (ret != 1) { |
| 250 | int ssl_err = SSL_get_error(ssl.get(), ret); |
| 251 | fprintf(stderr, "Error while connecting: %d\n", ssl_err); |
| 252 | ERR_print_errors_cb(PrintErrorCallback, stderr); |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 253 | result = false; |
| 254 | continue; |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 255 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 256 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 257 | fprintf(stderr, "Connected.\n"); |
| 258 | PrintConnectionInfo(ssl.get()); |
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 | result = TransferData(ssl.get(), sock); |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 261 | } while (args_map.count("-loop") != 0); |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 262 | |
| 263 | return result; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 264 | } |