blob: 94abbbd9a8d2f68a501fec4dedc9737035134524 [file] [log] [blame]
Dave Tapuskab8a824d2014-12-10 19:09:52 -05001/* 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 Tapuskab8a824d2014-12-10 19:09:52 -050017#include <openssl/err.h>
Martin Kreichgauerce1f62c2016-11-29 14:04:13 -080018#include <openssl/rand.h>
Dave Tapuskab8a824d2014-12-10 19:09:52 -050019#include <openssl/ssl.h>
20
21#include "internal.h"
22#include "transport_common.h"
23
24
25static const struct argument kArguments[] = {
26 {
Steven Valdez87c0bb22016-12-07 10:31:16 -050027 "-accept", kRequiredArgument,
28 "The port of the server to bind on; eg 45102",
Dave Tapuskab8a824d2014-12-10 19:09:52 -050029 },
30 {
Steven Valdez87c0bb22016-12-07 10:31:16 -050031 "-cipher", kOptionalArgument,
32 "An OpenSSL-style cipher suite string that configures the offered "
33 "ciphers",
Dave Tapuskab8a824d2014-12-10 19:09:52 -050034 },
35 {
Steven Valdez87c0bb22016-12-07 10:31:16 -050036 "-max-version", kOptionalArgument,
37 "The maximum acceptable protocol version",
David Benjamin225e5ad2016-07-16 14:51:58 +020038 },
39 {
Steven Valdez87c0bb22016-12-07 10:31:16 -050040 "-min-version", kOptionalArgument,
41 "The minimum acceptable protocol version",
David Benjamin225e5ad2016-07-16 14:51:58 +020042 },
43 {
Steven Valdez87c0bb22016-12-07 10:31:16 -050044 "-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.",
Dave Tapuskab8a824d2014-12-10 19:09:52 -050048 },
49 {
Steven Valdez87c0bb22016-12-07 10:31:16 -050050 "-ocsp-response", kOptionalArgument, "OCSP response file to send",
Paul Lietaraeeff2c2015-08-12 11:47:11 +010051 },
52 {
Steven Valdez87c0bb22016-12-07 10:31:16 -050053 "-loop", kBooleanArgument,
54 "The server will continue accepting new sequential connections.",
55 },
56 {
57 "", kOptionalArgument, "",
Dave Tapuskab8a824d2014-12-10 19:09:52 -050058 },
59};
60
Paul Lietaraeeff2c2015-08-12 11:47:11 +010061static bool LoadOCSPResponse(SSL_CTX *ctx, const char *filename) {
62 void *data = NULL;
63 bool ret = false;
David Benjamined50cee2015-08-28 15:43:26 -040064 size_t bytes_read;
Paul Lietaraeeff2c2015-08-12 11:47:11 +010065 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
David Benjamined50cee2015-08-28 15:43:26 -040085 bytes_read = fread(data, 1, length, f);
Paul Lietaraeeff2c2015-08-12 11:47:11 +010086 if (ferror(f) != 0 ||
David Benjamined50cee2015-08-28 15:43:26 -040087 bytes_read != (size_t)length ||
88 !SSL_CTX_set_ocsp_response(ctx, (uint8_t*)data, bytes_read)) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +010089 goto out;
90 }
91
92 ret = true;
93out:
94 if (f != NULL) {
95 fclose(f);
96 }
97 free(data);
98 return ret;
99}
100
Martin Kreichgauerce1f62c2016-11-29 14:04:13 -0800101static 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
115static 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));
Adam Langleyf18ad082016-12-01 11:11:26 -0800120 ASN1_INTEGER_set(X509_get_serialNumber(x509.get()), serial >> 1);
Martin Kreichgauerce1f62c2016-11-29 14:04:13 -0800121 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
Dave Tapuskab8a824d2014-12-10 19:09:52 -0500144bool Server(const std::vector<std::string> &args) {
Brian Smith33970e62015-01-27 22:32:08 -0800145 if (!InitSocketLibrary()) {
146 return false;
147 }
148
Dave Tapuskab8a824d2014-12-10 19:09:52 -0500149 std::map<std::string, std::string> args_map;
150
151 if (!ParseKeyValueArguments(&args_map, args, kArguments)) {
152 PrintUsage(kArguments);
153 return false;
154 }
155
David Benjamin0cce8632016-10-20 15:13:26 -0400156 bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method()));
157 SSL_CTX_set_options(ctx.get(), SSL_OP_NO_SSLv3);
Dave Tapuskab8a824d2014-12-10 19:09:52 -0500158
159 // Server authentication is required.
Dave Tapuskab8a824d2014-12-10 19:09:52 -0500160 if (args_map.count("-key") != 0) {
Martin Kreichgauerce1f62c2016-11-29 14:04:13 -0800161 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 }
Dave Tapuskab8a824d2014-12-10 19:09:52 -0500188 }
189
190 if (args_map.count("-cipher") != 0 &&
David Benjamin0cce8632016-10-20 15:13:26 -0400191 !SSL_CTX_set_cipher_list(ctx.get(), args_map["-cipher"].c_str())) {
Dave Tapuskab8a824d2014-12-10 19:09:52 -0500192 fprintf(stderr, "Failed setting cipher list\n");
193 return false;
194 }
195
David Benjamin225e5ad2016-07-16 14:51:58 +0200196 if (args_map.count("-max-version") != 0) {
197 uint16_t version;
198 if (!VersionFromString(&version, args_map["-max-version"])) {
199 fprintf(stderr, "Unknown protocol version: '%s'\n",
200 args_map["-max-version"].c_str());
201 return false;
202 }
David Benjamin0cce8632016-10-20 15:13:26 -0400203 if (!SSL_CTX_set_max_proto_version(ctx.get(), version)) {
David Benjamin2dc02042016-09-19 19:57:37 -0400204 return false;
205 }
David Benjamin225e5ad2016-07-16 14:51:58 +0200206 }
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 }
David Benjamin0cce8632016-10-20 15:13:26 -0400215 if (!SSL_CTX_set_min_proto_version(ctx.get(), version)) {
David Benjamin2dc02042016-09-19 19:57:37 -0400216 return false;
217 }
David Benjamin225e5ad2016-07-16 14:51:58 +0200218 }
219
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100220 if (args_map.count("-ocsp-response") != 0 &&
David Benjamin0cce8632016-10-20 15:13:26 -0400221 !LoadOCSPResponse(ctx.get(), args_map["-ocsp-response"].c_str())) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100222 fprintf(stderr, "Failed to load OCSP response: %s\n", args_map["-ocsp-response"].c_str());
223 return false;
224 }
225
Steven Valdez87c0bb22016-12-07 10:31:16 -0500226 bool result = true;
227 do {
228 int sock = -1;
229 if (!Accept(&sock, args_map["-accept"])) {
230 return false;
231 }
Dave Tapuskab8a824d2014-12-10 19:09:52 -0500232
Steven Valdez87c0bb22016-12-07 10:31:16 -0500233 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);
Dave Tapuskab8a824d2014-12-10 19:09:52 -0500236
Steven Valdez87c0bb22016-12-07 10:31:16 -0500237 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 }
Dave Tapuskab8a824d2014-12-10 19:09:52 -0500244
Steven Valdez87c0bb22016-12-07 10:31:16 -0500245 fprintf(stderr, "Connected.\n");
246 PrintConnectionInfo(ssl.get());
Dave Tapuskab8a824d2014-12-10 19:09:52 -0500247
Steven Valdez87c0bb22016-12-07 10:31:16 -0500248 result = TransferData(ssl.get(), sock);
249 } while (result && args_map.count("-loop") != 0);
250
251 return result;
Dave Tapuskab8a824d2014-12-10 19:09:52 -0500252}