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