| 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 |
|
| 17 |
#include <string> |
| 18 |
#include <vector> |
| 19 |
|
| 20 |
#include <errno.h> |
| 21 |
#include <stddef.h> |
| 22 |
#include <stdlib.h> |
| 23 |
#include <string.h> |
| 24 |
#include <sys/types.h> |
| 25 |
|
| 26 |
#if !defined(OPENSSL_WINDOWS) |
| 27 |
#include <arpa/inet.h> |
| 28 |
#include <fcntl.h> |
| 29 |
#include <netdb.h> |
| 30 |
#include <netinet/in.h> |
| 31 |
#include <sys/select.h> |
| 32 |
#include <sys/socket.h> |
| 33 |
#include <unistd.h> |
| 34 |
#else |
| 35 |
#include <io.h> |
| 36 |
OPENSSL_MSVC_PRAGMA(warning(push, 3)) |
| 37 |
#include <winsock2.h> |
| 38 |
#include <ws2tcpip.h> |
| 39 |
OPENSSL_MSVC_PRAGMA(warning(pop)) |
| 40 |
|
| 41 |
typedef int ssize_t; |
| 42 |
#pragma comment(lib, "Ws2_32.lib") |
| 43 |
#endif |
| 44 |
|
| 45 |
#include <openssl/err.h> |
| 46 |
#include <openssl/ssl.h> |
| 47 |
#include <openssl/x509.h> |
| 48 |
|
| 49 |
#include "internal.h" |
| 50 |
#include "transport_common.h" |
| 51 |
|
| 52 |
|
| 53 |
#if !defined(OPENSSL_WINDOWS) |
| 54 |
static int closesocket(int sock) { |
| 55 |
return close(sock); |
| 56 |
} |
| 57 |
#endif |
| 58 |
|
| 59 |
bool InitSocketLibrary() { |
| 60 |
#if defined(OPENSSL_WINDOWS) |
| 61 |
WSADATA wsaData; |
| 62 |
int err = WSAStartup(MAKEWORD(2, 2), &wsaData); |
| 63 |
if (err != 0) { |
| 64 |
fprintf(stderr, "WSAStartup failed with error %d\n", err); |
| 65 |
return false; |
| 66 |
} |
| 67 |
#endif |
| 68 |
return true; |
| 69 |
} |
| 70 |
|
| 71 |
// Connect sets |*out_sock| to be a socket connected to the destination given |
| 72 |
// in |hostname_and_port|, which should be of the form "www.example.com:123". |
| 73 |
// It returns true on success and false otherwise. |
| 74 |
bool Connect(int *out_sock, const std::string &hostname_and_port) { |
| 75 |
const size_t colon_offset = hostname_and_port.find_last_of(':'); |
| 76 |
std::string hostname, port; |
| 77 |
|
| 78 |
if (colon_offset == std::string::npos) { |
| 79 |
hostname = hostname_and_port; |
| 80 |
port = "443"; |
| 81 |
} else { |
| 82 |
hostname = hostname_and_port.substr(0, colon_offset); |
| 83 |
port = hostname_and_port.substr(colon_offset + 1); |
| 84 |
} |
| 85 |
|
| 86 |
struct addrinfo hint, *result; |
| 87 |
memset(&hint, 0, sizeof(hint)); |
| 88 |
hint.ai_family = AF_UNSPEC; |
| 89 |
hint.ai_socktype = SOCK_STREAM; |
| 90 |
|
| 91 |
int ret = getaddrinfo(hostname.c_str(), port.c_str(), &hint, &result); |
| 92 |
if (ret != 0) { |
| 93 |
fprintf(stderr, "getaddrinfo returned: %s\n", gai_strerror(ret)); |
| 94 |
return false; |
| 95 |
} |
| 96 |
|
| 97 |
bool ok = false; |
| 98 |
char buf[256]; |
| 99 |
|
| 100 |
*out_sock = |
| 101 |
socket(result->ai_family, result->ai_socktype, result->ai_protocol); |
| 102 |
if (*out_sock < 0) { |
| 103 |
perror("socket"); |
| 104 |
goto out; |
| 105 |
} |
| 106 |
|
| 107 |
switch (result->ai_family) { |
| 108 |
case AF_INET: { |
| 109 |
struct sockaddr_in *sin = |
| 110 |
reinterpret_cast<struct sockaddr_in *>(result->ai_addr); |
| 111 |
fprintf(stderr, "Connecting to %s:%d\n", |
| 112 |
inet_ntop(result->ai_family, &sin->sin_addr, buf, sizeof(buf)), |
| 113 |
ntohs(sin->sin_port)); |
| 114 |
break; |
| 115 |
} |
| 116 |
case AF_INET6: { |
| 117 |
struct sockaddr_in6 *sin6 = |
| 118 |
reinterpret_cast<struct sockaddr_in6 *>(result->ai_addr); |
| 119 |
fprintf(stderr, "Connecting to [%s]:%d\n", |
| 120 |
inet_ntop(result->ai_family, &sin6->sin6_addr, buf, sizeof(buf)), |
| 121 |
ntohs(sin6->sin6_port)); |
| 122 |
break; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
if (connect(*out_sock, result->ai_addr, result->ai_addrlen) != 0) { |
| 127 |
perror("connect"); |
| 128 |
goto out; |
| 129 |
} |
| 130 |
ok = true; |
| 131 |
|
| 132 |
out: |
| 133 |
freeaddrinfo(result); |
| 134 |
return ok; |
| 135 |
} |
| 136 |
|
| 137 |
bool Accept(int *out_sock, const std::string &port) { |
| 138 |
struct sockaddr_in6 addr, cli_addr; |
| 139 |
socklen_t cli_addr_len = sizeof(cli_addr); |
| 140 |
memset(&addr, 0, sizeof(addr)); |
| 141 |
|
| 142 |
addr.sin6_family = AF_INET6; |
| 143 |
addr.sin6_addr = in6addr_any; |
| 144 |
addr.sin6_port = htons(atoi(port.c_str())); |
| 145 |
|
| 146 |
bool ok = false; |
| 147 |
int server_sock = -1; |
| 148 |
|
| 149 |
server_sock = |
| 150 |
socket(addr.sin6_family, SOCK_STREAM, 0); |
| 151 |
if (server_sock < 0) { |
| 152 |
perror("socket"); |
| 153 |
goto out; |
| 154 |
} |
| 155 |
|
| 156 |
if (bind(server_sock, (struct sockaddr*)&addr, sizeof(addr)) != 0) { |
| 157 |
perror("connect"); |
| 158 |
goto out; |
| 159 |
} |
| 160 |
listen(server_sock, 1); |
| 161 |
*out_sock = accept(server_sock, (struct sockaddr*)&cli_addr, &cli_addr_len); |
| 162 |
|
| 163 |
ok = true; |
| 164 |
|
| 165 |
out: |
| 166 |
closesocket(server_sock); |
| 167 |
return ok; |
| 168 |
} |
| 169 |
|
| 170 |
void PrintConnectionInfo(const SSL *ssl) { |
| 171 |
const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl); |
| 172 |
|
| 173 |
fprintf(stderr, " Version: %s\n", SSL_get_version(ssl)); |
| 174 |
fprintf(stderr, " Resumed session: %s\n", |
| 175 |
SSL_session_reused(ssl) ? "yes" : "no"); |
| 176 |
fprintf(stderr, " Cipher: %s\n", SSL_CIPHER_get_name(cipher)); |
| 177 |
if (SSL_CIPHER_is_ECDHE(cipher)) { |
| 178 |
fprintf(stderr, " ECDHE curve: %s\n", |
| 179 |
SSL_get_curve_name( |
| 180 |
SSL_SESSION_get_key_exchange_info(SSL_get_session(ssl)))); |
| 181 |
} |
| 182 |
fprintf(stderr, " Secure renegotiation: %s\n", |
| 183 |
SSL_get_secure_renegotiation_support(ssl) ? "yes" : "no"); |
| 184 |
fprintf(stderr, " Extended master secret: %s\n", |
| 185 |
SSL_get_extms_support(ssl) ? "yes" : "no"); |
| 186 |
|
| 187 |
const uint8_t *next_proto; |
| 188 |
unsigned next_proto_len; |
| 189 |
SSL_get0_next_proto_negotiated(ssl, &next_proto, &next_proto_len); |
| 190 |
fprintf(stderr, " Next protocol negotiated: %.*s\n", next_proto_len, |
| 191 |
next_proto); |
| 192 |
|
| 193 |
const uint8_t *alpn; |
| 194 |
unsigned alpn_len; |
| 195 |
SSL_get0_alpn_selected(ssl, &alpn, &alpn_len); |
| 196 |
fprintf(stderr, " ALPN protocol: %.*s\n", alpn_len, alpn); |
| 197 |
|
| 198 |
// Print the server cert subject and issuer names. |
| 199 |
X509 *peer = SSL_get_peer_certificate(ssl); |
| 200 |
if (peer != NULL) { |
| 201 |
fprintf(stderr, " Cert subject: "); |
| 202 |
X509_NAME_print_ex_fp(stderr, X509_get_subject_name(peer), 0, |
| 203 |
XN_FLAG_ONELINE); |
| 204 |
fprintf(stderr, "\n Cert issuer: "); |
| 205 |
X509_NAME_print_ex_fp(stderr, X509_get_issuer_name(peer), 0, |
| 206 |
XN_FLAG_ONELINE); |
| 207 |
fprintf(stderr, "\n"); |
| 208 |
X509_free(peer); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
bool SocketSetNonBlocking(int sock, bool is_non_blocking) { |
| 213 |
bool ok; |
| 214 |
|
| 215 |
#if defined(OPENSSL_WINDOWS) |
| 216 |
u_long arg = is_non_blocking; |
| 217 |
ok = 0 == ioctlsocket(sock, FIONBIO, &arg); |
| 218 |
#else |
| 219 |
int flags = fcntl(sock, F_GETFL, 0); |
| 220 |
if (flags < 0) { |
| 221 |
return false; |
| 222 |
} |
| 223 |
if (is_non_blocking) { |
| 224 |
flags |= O_NONBLOCK; |
| 225 |
} else { |
| 226 |
flags &= ~O_NONBLOCK; |
| 227 |
} |
| 228 |
ok = 0 == fcntl(sock, F_SETFL, flags); |
| 229 |
#endif |
| 230 |
if (!ok) { |
| 231 |
fprintf(stderr, "Failed to set socket non-blocking.\n"); |
| 232 |
} |
| 233 |
return ok; |
| 234 |
} |
| 235 |
|
| 236 |
// PrintErrorCallback is a callback function from OpenSSL's |
| 237 |
// |ERR_print_errors_cb| that writes errors to a given |FILE*|. |
| 238 |
int PrintErrorCallback(const char *str, size_t len, void *ctx) { |
| 239 |
fwrite(str, len, 1, reinterpret_cast<FILE*>(ctx)); |
| 240 |
return 1; |
| 241 |
} |
| 242 |
|
| 243 |
bool TransferData(SSL *ssl, int sock) { |
| 244 |
bool stdin_open = true; |
| 245 |
|
| 246 |
fd_set read_fds; |
| 247 |
FD_ZERO(&read_fds); |
| 248 |
|
| 249 |
if (!SocketSetNonBlocking(sock, true)) { |
| 250 |
return false; |
| 251 |
} |
| 252 |
|
| 253 |
for (;;) { |
| 254 |
if (stdin_open) { |
| 255 |
FD_SET(0, &read_fds); |
| 256 |
} |
| 257 |
FD_SET(sock, &read_fds); |
| 258 |
|
| 259 |
int ret = select(sock + 1, &read_fds, NULL, NULL, NULL); |
| 260 |
if (ret <= 0) { |
| 261 |
perror("select"); |
| 262 |
return false; |
| 263 |
} |
| 264 |
|
| 265 |
if (FD_ISSET(0, &read_fds)) { |
| 266 |
uint8_t buffer[512]; |
| 267 |
ssize_t n; |
| 268 |
|
| 269 |
do { |
| 270 |
n = BORINGSSL_READ(0, buffer, sizeof(buffer)); |
| 271 |
} while (n == -1 && errno == EINTR); |
| 272 |
|
| 273 |
if (n == 0) { |
| 274 |
FD_CLR(0, &read_fds); |
| 275 |
stdin_open = false; |
| 276 |
#if !defined(OPENSSL_WINDOWS) |
| 277 |
shutdown(sock, SHUT_WR); |
| 278 |
#else |
| 279 |
shutdown(sock, SD_SEND); |
| 280 |
#endif |
| 281 |
continue; |
| 282 |
} else if (n < 0) { |
| 283 |
perror("read from stdin"); |
| 284 |
return false; |
| 285 |
} |
| 286 |
|
| 287 |
if (!SocketSetNonBlocking(sock, false)) { |
| 288 |
return false; |
| 289 |
} |
| 290 |
int ssl_ret = SSL_write(ssl, buffer, n); |
| 291 |
if (!SocketSetNonBlocking(sock, true)) { |
| 292 |
return false; |
| 293 |
} |
| 294 |
|
| 295 |
if (ssl_ret <= 0) { |
| 296 |
int ssl_err = SSL_get_error(ssl, ssl_ret); |
| 297 |
fprintf(stderr, "Error while writing: %d\n", ssl_err); |
| 298 |
ERR_print_errors_cb(PrintErrorCallback, stderr); |
| 299 |
return false; |
| 300 |
} else if (ssl_ret != n) { |
| 301 |
fprintf(stderr, "Short write from SSL_write.\n"); |
| 302 |
return false; |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
if (FD_ISSET(sock, &read_fds)) { |
| 307 |
uint8_t buffer[512]; |
| 308 |
int ssl_ret = SSL_read(ssl, buffer, sizeof(buffer)); |
| 309 |
|
| 310 |
if (ssl_ret < 0) { |
| 311 |
int ssl_err = SSL_get_error(ssl, ssl_ret); |
| 312 |
if (ssl_err == SSL_ERROR_WANT_READ) { |
| 313 |
continue; |
| 314 |
} |
| 315 |
fprintf(stderr, "Error while reading: %d\n", ssl_err); |
| 316 |
ERR_print_errors_cb(PrintErrorCallback, stderr); |
| 317 |
return false; |
| 318 |
} else if (ssl_ret == 0) { |
| 319 |
return true; |
| 320 |
} |
| 321 |
|
| 322 |
ssize_t n; |
| 323 |
do { |
| 324 |
n = BORINGSSL_WRITE(1, buffer, ssl_ret); |
| 325 |
} while (n == -1 && errno == EINTR); |
| 326 |
|
| 327 |
if (n != ssl_ret) { |
| 328 |
fprintf(stderr, "Short write to stderr.\n"); |
| 329 |
return false; |
| 330 |
} |
| 331 |
} |
| 332 |
} |
| 333 |
} |