blob: 2f96961eacb5049e55558b0f8e743ca4c4afd0f3 [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 {
David Benjamin05709232015-03-23 19:01:33 -040027 "-accept", kRequiredArgument,
Dave Tapuskab8a824d2014-12-10 19:09:52 -050028 "The port of the server to bind on; eg 45102",
29 },
30 {
David Benjamin05709232015-03-23 19:01:33 -040031 "-cipher", kOptionalArgument,
Dave Tapuskab8a824d2014-12-10 19:09:52 -050032 "An OpenSSL-style cipher suite string that configures the offered ciphers",
33 },
34 {
David Benjamin225e5ad2016-07-16 14:51:58 +020035 "-max-version", kOptionalArgument,
36 "The maximum acceptable protocol version",
37 },
38 {
39 "-min-version", kOptionalArgument,
40 "The minimum acceptable protocol version",
41 },
42 {
David Benjamin05709232015-03-23 19:01:33 -040043 "-key", kOptionalArgument,
Martin Kreichgauerce1f62c2016-11-29 14:04:13 -080044 "PEM-encoded file containing the private key, leaf certificate and "
45 "optional certificate chain. A self-signed certificate is generated "
46 "at runtime if this argument is not provided.",
Dave Tapuskab8a824d2014-12-10 19:09:52 -050047 },
48 {
Paul Lietaraeeff2c2015-08-12 11:47:11 +010049 "-ocsp-response", kOptionalArgument,
50 "OCSP response file to send",
51 },
52 {
David Benjamin05709232015-03-23 19:01:33 -040053 "", kOptionalArgument, "",
Dave Tapuskab8a824d2014-12-10 19:09:52 -050054 },
55};
56
Paul Lietaraeeff2c2015-08-12 11:47:11 +010057static bool LoadOCSPResponse(SSL_CTX *ctx, const char *filename) {
58 void *data = NULL;
59 bool ret = false;
David Benjamined50cee2015-08-28 15:43:26 -040060 size_t bytes_read;
Paul Lietaraeeff2c2015-08-12 11:47:11 +010061 long length;
62
63 FILE *f = fopen(filename, "rb");
64
65 if (f == NULL ||
66 fseek(f, 0, SEEK_END) != 0) {
67 goto out;
68 }
69
70 length = ftell(f);
71 if (length < 0) {
72 goto out;
73 }
74
75 data = malloc(length);
76 if (data == NULL) {
77 goto out;
78 }
79 rewind(f);
80
David Benjamined50cee2015-08-28 15:43:26 -040081 bytes_read = fread(data, 1, length, f);
Paul Lietaraeeff2c2015-08-12 11:47:11 +010082 if (ferror(f) != 0 ||
David Benjamined50cee2015-08-28 15:43:26 -040083 bytes_read != (size_t)length ||
84 !SSL_CTX_set_ocsp_response(ctx, (uint8_t*)data, bytes_read)) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +010085 goto out;
86 }
87
88 ret = true;
89out:
90 if (f != NULL) {
91 fclose(f);
92 }
93 free(data);
94 return ret;
95}
96
Martin Kreichgauerce1f62c2016-11-29 14:04:13 -080097static bssl::UniquePtr<EVP_PKEY> MakeKeyPairForSelfSignedCert() {
98 bssl::UniquePtr<EC_KEY> ec_key(EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
99 if (!ec_key || !EC_KEY_generate_key(ec_key.get())) {
100 fprintf(stderr, "Failed to generate key pair.\n");
101 return nullptr;
102 }
103 bssl::UniquePtr<EVP_PKEY> evp_pkey(EVP_PKEY_new());
104 if (!evp_pkey || !EVP_PKEY_assign_EC_KEY(evp_pkey.get(), ec_key.release())) {
105 fprintf(stderr, "Failed to assign key pair.\n");
106 return nullptr;
107 }
108 return evp_pkey;
109}
110
111static bssl::UniquePtr<X509> MakeSelfSignedCert(EVP_PKEY *evp_pkey,
112 const int valid_days) {
113 bssl::UniquePtr<X509> x509(X509_new());
114 uint32_t serial;
115 RAND_bytes(reinterpret_cast<uint8_t*>(&serial), sizeof(serial));
116 ASN1_INTEGER_set(X509_get_serialNumber(x509.get()), serial);
117 X509_gmtime_adj(X509_get_notBefore(x509.get()), 0);
118 X509_gmtime_adj(X509_get_notAfter(x509.get()), 60 * 60 * 24 * valid_days);
119
120 X509_NAME* subject = X509_get_subject_name(x509.get());
121 X509_NAME_add_entry_by_txt(subject, "C", MBSTRING_ASC,
122 reinterpret_cast<const uint8_t *>("US"), -1, -1,
123 0);
124 X509_NAME_add_entry_by_txt(subject, "O", MBSTRING_ASC,
125 reinterpret_cast<const uint8_t *>("BoringSSL"), -1,
126 -1, 0);
127 X509_set_issuer_name(x509.get(), subject);
128
129 if (!X509_set_pubkey(x509.get(), evp_pkey)) {
130 fprintf(stderr, "Failed to set public key.\n");
131 return nullptr;
132 }
133 if (!X509_sign(x509.get(), evp_pkey, EVP_sha256())) {
134 fprintf(stderr, "Failed to sign certificate.\n");
135 return nullptr;
136 }
137 return x509;
138}
139
Dave Tapuskab8a824d2014-12-10 19:09:52 -0500140bool Server(const std::vector<std::string> &args) {
Brian Smith33970e62015-01-27 22:32:08 -0800141 if (!InitSocketLibrary()) {
142 return false;
143 }
144
Dave Tapuskab8a824d2014-12-10 19:09:52 -0500145 std::map<std::string, std::string> args_map;
146
147 if (!ParseKeyValueArguments(&args_map, args, kArguments)) {
148 PrintUsage(kArguments);
149 return false;
150 }
151
David Benjamin0cce8632016-10-20 15:13:26 -0400152 bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method()));
153 SSL_CTX_set_options(ctx.get(), SSL_OP_NO_SSLv3);
Dave Tapuskab8a824d2014-12-10 19:09:52 -0500154
155 // Server authentication is required.
Dave Tapuskab8a824d2014-12-10 19:09:52 -0500156 if (args_map.count("-key") != 0) {
Martin Kreichgauerce1f62c2016-11-29 14:04:13 -0800157 std::string key_file = args_map["-key"];
158 if (!SSL_CTX_use_PrivateKey_file(ctx.get(), key_file.c_str(), SSL_FILETYPE_PEM)) {
159 fprintf(stderr, "Failed to load private key: %s\n", key_file.c_str());
160 return false;
161 }
162 if (!SSL_CTX_use_certificate_chain_file(ctx.get(), key_file.c_str())) {
163 fprintf(stderr, "Failed to load cert chain: %s\n", key_file.c_str());
164 return false;
165 }
166 } else {
167 bssl::UniquePtr<EVP_PKEY> evp_pkey = MakeKeyPairForSelfSignedCert();
168 if (!evp_pkey) {
169 return false;
170 }
171 bssl::UniquePtr<X509> cert =
172 MakeSelfSignedCert(evp_pkey.get(), 365 /* valid_days */);
173 if (!cert) {
174 return false;
175 }
176 if (!SSL_CTX_use_PrivateKey(ctx.get(), evp_pkey.get())) {
177 fprintf(stderr, "Failed to set private key.\n");
178 return false;
179 }
180 if (!SSL_CTX_use_certificate(ctx.get(), cert.get())) {
181 fprintf(stderr, "Failed to set certificate.\n");
182 return false;
183 }
Dave Tapuskab8a824d2014-12-10 19:09:52 -0500184 }
185
186 if (args_map.count("-cipher") != 0 &&
David Benjamin0cce8632016-10-20 15:13:26 -0400187 !SSL_CTX_set_cipher_list(ctx.get(), args_map["-cipher"].c_str())) {
Dave Tapuskab8a824d2014-12-10 19:09:52 -0500188 fprintf(stderr, "Failed setting cipher list\n");
189 return false;
190 }
191
David Benjamin225e5ad2016-07-16 14:51:58 +0200192 if (args_map.count("-max-version") != 0) {
193 uint16_t version;
194 if (!VersionFromString(&version, args_map["-max-version"])) {
195 fprintf(stderr, "Unknown protocol version: '%s'\n",
196 args_map["-max-version"].c_str());
197 return false;
198 }
David Benjamin0cce8632016-10-20 15:13:26 -0400199 if (!SSL_CTX_set_max_proto_version(ctx.get(), version)) {
David Benjamin2dc02042016-09-19 19:57:37 -0400200 return false;
201 }
David Benjamin225e5ad2016-07-16 14:51:58 +0200202 }
203
204 if (args_map.count("-min-version") != 0) {
205 uint16_t version;
206 if (!VersionFromString(&version, args_map["-min-version"])) {
207 fprintf(stderr, "Unknown protocol version: '%s'\n",
208 args_map["-min-version"].c_str());
209 return false;
210 }
David Benjamin0cce8632016-10-20 15:13:26 -0400211 if (!SSL_CTX_set_min_proto_version(ctx.get(), version)) {
David Benjamin2dc02042016-09-19 19:57:37 -0400212 return false;
213 }
David Benjamin225e5ad2016-07-16 14:51:58 +0200214 }
215
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100216 if (args_map.count("-ocsp-response") != 0 &&
David Benjamin0cce8632016-10-20 15:13:26 -0400217 !LoadOCSPResponse(ctx.get(), args_map["-ocsp-response"].c_str())) {
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100218 fprintf(stderr, "Failed to load OCSP response: %s\n", args_map["-ocsp-response"].c_str());
219 return false;
220 }
221
Dave Tapuskab8a824d2014-12-10 19:09:52 -0500222 int sock = -1;
223 if (!Accept(&sock, args_map["-accept"])) {
224 return false;
225 }
226
227 BIO *bio = BIO_new_socket(sock, BIO_CLOSE);
David Benjamin0cce8632016-10-20 15:13:26 -0400228 bssl::UniquePtr<SSL> ssl(SSL_new(ctx.get()));
229 SSL_set_bio(ssl.get(), bio, bio);
Dave Tapuskab8a824d2014-12-10 19:09:52 -0500230
David Benjamin0cce8632016-10-20 15:13:26 -0400231 int ret = SSL_accept(ssl.get());
Dave Tapuskab8a824d2014-12-10 19:09:52 -0500232 if (ret != 1) {
David Benjamin0cce8632016-10-20 15:13:26 -0400233 int ssl_err = SSL_get_error(ssl.get(), ret);
Dave Tapuskab8a824d2014-12-10 19:09:52 -0500234 fprintf(stderr, "Error while connecting: %d\n", ssl_err);
235 ERR_print_errors_cb(PrintErrorCallback, stderr);
236 return false;
237 }
238
239 fprintf(stderr, "Connected.\n");
David Benjamin0cce8632016-10-20 15:13:26 -0400240 PrintConnectionInfo(ssl.get());
Dave Tapuskab8a824d2014-12-10 19:09:52 -0500241
David Benjamin0cce8632016-10-20 15:13:26 -0400242 return TransferData(ssl.get(), sock);
Dave Tapuskab8a824d2014-12-10 19:09:52 -0500243}