| 1 |
/* |
| 2 |
* |
| 3 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
* or more contributor license agreements. See the NOTICE file |
| 5 |
* distributed with this work for additional information |
| 6 |
* regarding copyright ownership. The ASF licenses this file |
| 7 |
* to you under the Apache License, Version 2.0 (the |
| 8 |
* "License"); you may not use this file except in compliance |
| 9 |
* with the License. You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, |
| 14 |
* software distributed under the License is distributed on an |
| 15 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
* KIND, either express or implied. See the License for the |
| 17 |
* specific language governing permissions and limitations |
| 18 |
* under the License. |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "platform/platform.h" |
| 23 |
#include "platform/platform_fmt.h" |
| 24 |
#include "core/engine-internal.h" |
| 25 |
#include "core/logger_private.h" |
| 26 |
#include "core/util.h" |
| 27 |
|
| 28 |
#include <proton/ssl.h> |
| 29 |
#include <proton/engine.h> |
| 30 |
|
| 31 |
// openssl on windows expects the user to have already included |
| 32 |
// winsock.h |
| 33 |
|
| 34 |
#ifdef _MSC_VER |
| 35 |
#ifndef _WIN32_WINNT |
| 36 |
#define _WIN32_WINNT 0x0501 |
| 37 |
#endif |
| 38 |
#if _WIN32_WINNT < 0x0501 |
| 39 |
#error "Proton requires Windows API support for XP or later." |
| 40 |
#endif |
| 41 |
#include <winsock2.h> |
| 42 |
#include <mswsock.h> |
| 43 |
#include <Ws2tcpip.h> |
| 44 |
#endif |
| 45 |
|
| 46 |
|
| 47 |
#include <openssl/ssl.h> |
| 48 |
#include <openssl/dh.h> |
| 49 |
#include <openssl/err.h> |
| 50 |
#include <openssl/x509v3.h> |
| 51 |
#include <sys/types.h> |
| 52 |
#include <sys/stat.h> |
| 53 |
#include <fcntl.h> |
| 54 |
#include <assert.h> |
| 55 |
#include <stdio.h> |
| 56 |
|
| •57 |
#pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 58 |
|
| 59 |
/** @file |
| 60 |
* SSL/TLS support API. |
| 61 |
* |
| 62 |
* This file contains an OpenSSL-based implemention of the SSL/TLS API. |
| 63 |
*/ |
| 64 |
|
| 65 |
typedef struct pn_ssl_session_t pn_ssl_session_t; |
| 66 |
|
| 67 |
static int ssl_ex_data_index; |
| 68 |
|
| 69 |
struct pn_ssl_domain_t { |
| 70 |
|
| 71 |
SSL_CTX *ctx; |
| 72 |
|
| 73 |
char *keyfile_pw; |
| 74 |
|
| 75 |
// settings used for all connections |
| 76 |
char *trusted_CAs; |
| 77 |
|
| 78 |
char *ciphers; |
| 79 |
|
| 80 |
int ref_count; |
| 81 |
#ifdef SSL_SECOP_PEER |
| 82 |
int default_seclevel; |
| 83 |
#endif |
| 84 |
pn_ssl_mode_t mode; |
| 85 |
pn_ssl_verify_mode_t verify_mode; |
| 86 |
|
| 87 |
bool has_certificate; // true when certificate configured |
| 88 |
bool allow_unsecured; |
| 89 |
}; |
| 90 |
|
| 91 |
|
| 92 |
struct pni_ssl_t { |
| 93 |
pn_ssl_mode_t mode; |
| 94 |
pn_ssl_verify_mode_t verify_mode; |
| 95 |
const char *session_id; |
| 96 |
const char *peer_hostname; |
| 97 |
SSL *ssl; |
| 98 |
|
| 99 |
BIO *bio_ssl; // i/o from/to SSL socket layer |
| 100 |
BIO *bio_ssl_io; // SSL "half" of network-facing BIO |
| 101 |
BIO *bio_net_io; // socket-side "half" of network-facing BIO |
| 102 |
// buffers for holding I/O from "applications" above SSL |
| 103 |
#define APP_BUF_SIZE (4*1024) |
| 104 |
char *outbuf; |
| 105 |
char *inbuf; |
| 106 |
|
| 107 |
ssize_t app_input_closed; // error code returned by upper layer process input |
| 108 |
ssize_t app_output_closed; // error code returned by upper layer process output |
| 109 |
|
| 110 |
size_t out_size; |
| 111 |
size_t out_count; |
| 112 |
size_t in_size; |
| 113 |
size_t in_count; |
| 114 |
|
| 115 |
bool ssl_shutdown; // BIO_ssl_shutdown() called on socket. |
| 116 |
bool ssl_closed; // shutdown complete, or SSL error |
| 117 |
bool read_blocked; // SSL blocked until more network data is read |
| 118 |
bool write_blocked; // SSL blocked until data is written to network |
| 119 |
|
| 120 |
char *subject; |
| 121 |
X509 *peer_certificate; |
| 122 |
}; |
| 123 |
|
| 124 |
static inline pn_transport_t *get_transport_internal(pn_ssl_t *ssl) |
| 125 |
{ |
| 126 |
// The external pn_sasl_t is really a pointer to the internal pni_transport_t |
| 127 |
return ((pn_transport_t *)ssl); |
| 128 |
} |
| 129 |
|
| 130 |
static inline pni_ssl_t *get_ssl_internal(pn_ssl_t *ssl) |
| 131 |
{ |
| 132 |
// The external pn_sasl_t is really a pointer to the internal pni_transport_t |
| 133 |
return ssl ? ((pn_transport_t *)ssl)->ssl : NULL; |
| 134 |
} |
| 135 |
|
| 136 |
// define two sets of allowable ciphers: those that require authentication, and those |
| 137 |
// that do not require authentication (anonymous). See ciphers(1). |
| 138 |
#define CIPHERS_AUTHENTICATE "ALL:!aNULL:!eNULL:@STRENGTH" |
| 139 |
#define CIPHERS_ANONYMOUS "ALL:aNULL:!eNULL:@STRENGTH" |
| 140 |
|
| 141 |
/* */ |
| 142 |
static int keyfile_pw_cb(char *buf, int size, int rwflag, void *userdata); |
| 143 |
static void handle_error_ssl( pn_transport_t *transport, unsigned int layer); |
| 144 |
static ssize_t process_input_ssl( pn_transport_t *transport, unsigned int layer, const char *input_data, size_t len); |
| 145 |
static ssize_t process_output_ssl( pn_transport_t *transport, unsigned int layer, char *input_data, size_t len); |
| 146 |
static ssize_t process_input_done(pn_transport_t *transport, unsigned int layer, const char *input_data, size_t len); |
| 147 |
static ssize_t process_output_done(pn_transport_t *transport, unsigned int layer, char *input_data, size_t len); |
| 148 |
static int init_ssl_socket(pn_transport_t *, pni_ssl_t *, pn_ssl_domain_t *); |
| 149 |
static void release_ssl_socket( pni_ssl_t * ); |
| 150 |
static size_t buffered_output( pn_transport_t *transport ); |
| 151 |
static X509 *get_peer_certificate(pni_ssl_t *ssl); |
| 152 |
|
| 153 |
static void ssl_vlog(pn_transport_t *transport, pn_log_level_t sev, const char *fmt, va_list ap) |
| 154 |
{ |
| 155 |
pn_logger_t *logger = transport ? &transport->logger : pn_default_logger(); |
| 156 |
if (PN_SHOULD_LOG(logger, PN_SUBSYSTEM_SSL, sev)) { |
| 157 |
pni_logger_vlogf(logger, PN_SUBSYSTEM_SSL, sev, fmt, ap); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
static void ssl_log(pn_transport_t *transport, pn_log_level_t sev, const char *fmt, ...) |
| 162 |
{ |
| 163 |
va_list ap; |
| 164 |
va_start(ap, fmt); |
| 165 |
ssl_vlog(transport, sev, fmt, ap); |
| 166 |
va_end(ap); |
| 167 |
} |
| 168 |
|
| 169 |
static void ssl_log_flush(pn_transport_t* transport, pn_log_level_t sev) |
| 170 |
{ |
| 171 |
char buf[128]; // see "man ERR_error_string_n()" |
| 172 |
unsigned long err = ERR_get_error(); |
| 173 |
while (err) { |
| 174 |
ERR_error_string_n(err, buf, sizeof(buf)); |
| 175 |
ssl_log(transport, sev, "%s", buf); |
| 176 |
err = ERR_get_error(); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
// log an error and dump the SSL error stack |
| 181 |
static void ssl_log_error(const char *fmt, ...) |
| 182 |
{ |
| 183 |
if (fmt) { |
| 184 |
va_list ap; |
| 185 |
va_start(ap, fmt); |
| 186 |
ssl_vlog(NULL, PN_LEVEL_ERROR, fmt, ap); |
| 187 |
va_end(ap); |
| 188 |
} |
| 189 |
ssl_log_flush(NULL, PN_LEVEL_ERROR); |
| 190 |
} |
| 191 |
|
| 192 |
static void ssl_log_clear_data(pn_transport_t *transport, const char *data, size_t len) |
| 193 |
{ |
| 194 |
PN_LOG_DATA(&transport->logger, PN_SUBSYSTEM_SSL, PN_LEVEL_RAW, "decrypted data", data, len ); |
| 195 |
} |
| 196 |
|
| 197 |
// unrecoverable SSL failure occurred, notify transport and generate error code. |
| 198 |
static int ssl_failed(pn_transport_t *transport) |
| 199 |
{ |
| 200 |
pni_ssl_t *ssl = transport->ssl; |
| 201 |
SSL_set_shutdown(ssl->ssl, SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN); |
| 202 |
ssl->ssl_closed = true; |
| 203 |
ssl->app_input_closed = ssl->app_output_closed = PN_EOS; |
| 204 |
// fake a shutdown so the i/o processing code will close properly |
| 205 |
SSL_set_shutdown(ssl->ssl, SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN); |
| 206 |
// try to grab the first SSL error to add to the failure log |
| 207 |
char buf[256] = "Unknown error"; |
| 208 |
unsigned long ssl_err = ERR_get_error(); |
| 209 |
if (ssl_err) { |
| 210 |
ERR_error_string_n( ssl_err, buf, sizeof(buf) ); |
| 211 |
} |
| 212 |
ssl_log_flush(transport, PN_LEVEL_ERROR); // spit out any remaining errors to the log file |
| 213 |
pn_do_error(transport, "amqp:connection:framing-error", "SSL Failure: %s", buf); |
| 214 |
return PN_EOS; |
| 215 |
} |
| 216 |
|
| 217 |
/* match the DNS name pattern from the peer certificate against our configured peer |
| 218 |
hostname */ |
| 219 |
static bool match_dns_pattern( const char *hostname, |
| 220 |
const char *pattern, int plen ) |
| 221 |
{ |
| 222 |
int slen = (int) strlen(hostname); |
| 223 |
if (memchr( pattern, '*', plen ) == NULL) |
| 224 |
return (plen == slen && |
| 225 |
pn_strncasecmp( pattern, hostname, plen ) == 0); |
| 226 |
|
| 227 |
/* dns wildcarded pattern - RFC2818 */ |
| 228 |
char plabel[64]; /* max label length < 63 - RFC1034 */ |
| 229 |
char slabel[64]; |
| 230 |
|
| 231 |
while (plen > 0 && slen > 0) { |
| 232 |
const char *cptr; |
| 233 |
int len; |
| 234 |
|
| 235 |
cptr = (const char *) memchr( pattern, '.', plen ); |
| 236 |
len = (cptr) ? cptr - pattern : plen; |
| 237 |
if (len > (int) sizeof(plabel) - 1) return false; |
| 238 |
memcpy( plabel, pattern, len ); |
| 239 |
plabel[len] = 0; |
| 240 |
if (cptr) ++len; // skip matching '.' |
| 241 |
pattern += len; |
| 242 |
plen -= len; |
| 243 |
|
| 244 |
cptr = (const char *) memchr( hostname, '.', slen ); |
| 245 |
len = (cptr) ? cptr - hostname : slen; |
| 246 |
if (len > (int) sizeof(slabel) - 1) return false; |
| 247 |
memcpy( slabel, hostname, len ); |
| 248 |
slabel[len] = 0; |
| 249 |
if (cptr) ++len; // skip matching '.' |
| 250 |
hostname += len; |
| 251 |
slen -= len; |
| 252 |
|
| 253 |
char *star = strchr( plabel, '*' ); |
| 254 |
if (!star) { |
| 255 |
if (pn_strcasecmp( plabel, slabel )) return false; |
| 256 |
} else { |
| 257 |
*star = '\0'; |
| 258 |
char *prefix = plabel; |
| 259 |
int prefix_len = strlen(prefix); |
| 260 |
char *suffix = star + 1; |
| 261 |
int suffix_len = strlen(suffix); |
| 262 |
if (prefix_len && pn_strncasecmp( prefix, slabel, prefix_len )) return false; |
| 263 |
if (suffix_len && pn_strncasecmp( suffix, |
| 264 |
slabel + (strlen(slabel) - suffix_len), |
| 265 |
suffix_len )) return false; |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
return plen == slen; |
| 270 |
} |
| 271 |
|
| 272 |
// Certificate chain verification callback: return 1 if verified, |
| 273 |
// 0 if remote cannot be verified (fail handshake). |
| 274 |
// |
| 275 |
static int verify_callback(int preverify_ok, X509_STORE_CTX *ctx) |
| 276 |
{ |
| 277 |
if (!preverify_ok || X509_STORE_CTX_get_error_depth(ctx) != 0) |
| 278 |
// already failed, or not at peer cert in chain |
| 279 |
return preverify_ok; |
| 280 |
|
| 281 |
X509 *cert = X509_STORE_CTX_get_current_cert(ctx); |
| 282 |
SSL *ssn = (SSL *) X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx()); |
| 283 |
if (!ssn) { |
| 284 |
ssl_log(NULL, PN_LEVEL_ERROR, "Error: unexpected error - SSL session info not available for peer verify!"); |
| 285 |
return 0; // fail connection |
| 286 |
} |
| 287 |
|
| 288 |
pn_transport_t *transport = (pn_transport_t *)SSL_get_ex_data(ssn, ssl_ex_data_index); |
| 289 |
if (!transport) { |
| 290 |
ssl_log(transport, PN_LEVEL_ERROR, "Error: unexpected error - SSL context info not available for peer verify!"); |
| 291 |
return 0; // fail connection |
| 292 |
} |
| 293 |
|
| 294 |
pni_ssl_t *ssl = transport->ssl; |
| 295 |
if (ssl->verify_mode != PN_SSL_VERIFY_PEER_NAME) return preverify_ok; |
| 296 |
if (!ssl->peer_hostname) { |
| 297 |
ssl_log(transport, PN_LEVEL_ERROR, "Error: configuration error: PN_SSL_VERIFY_PEER_NAME configured, but no peer hostname set!"); |
| 298 |
return 0; // fail connection |
| 299 |
} |
| 300 |
|
| 301 |
ssl_log(transport, PN_LEVEL_TRACE, "Checking identifying name in peer cert against '%s'", ssl->peer_hostname); |
| 302 |
|
| 303 |
bool matched = false; |
| 304 |
|
| 305 |
/* first check any SubjectAltName entries, as per RFC2818 */ |
| 306 |
GENERAL_NAMES *sans = (GENERAL_NAMES *) X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 307 |
if (sans) { |
| 308 |
int name_ct = sk_GENERAL_NAME_num( sans ); |
| 309 |
int i; |
| 310 |
for (i = 0; !matched && i < name_ct; ++i) { |
| 311 |
GENERAL_NAME *name = sk_GENERAL_NAME_value( sans, i ); |
| 312 |
if (name->type == GEN_DNS) { |
| 313 |
ASN1_STRING *asn1 = name->d.dNSName; |
| •314 |
if (asn1 && ASN1_STRING_get0_data(asn1) && ASN1_STRING_length(asn1) > 0){ |
| 315 |
unsigned char *str; |
| 316 |
int len = ASN1_STRING_to_UTF8( &str, asn1 ); |
| 317 |
if (len >= 0) { |
| 318 |
ssl_log(transport, PN_LEVEL_TRACE, "SubjectAltName (dns) from peer cert = '%.*s'", len, str ); |
| 319 |
matched = match_dns_pattern( ssl->peer_hostname, (const char *)str, len ); |
| 320 |
OPENSSL_free( str ); |
| 321 |
} |
| 322 |
} |
| 323 |
} |
| 324 |
} |
| 325 |
GENERAL_NAMES_free( sans ); |
| 326 |
} |
| 327 |
|
| 328 |
/* if no general names match, try the CommonName from the subject */ |
| •329 |
const X509_NAME *name = X509_get_subject_name(cert); |
| 330 |
int i = -1; |
| 331 |
while (!matched && (i = X509_NAME_get_index_by_NID(name, NID_commonName, i)) >= 0) { |
| •332 |
const X509_NAME_ENTRY *ne = X509_NAME_get_entry(name, i); |
| 333 |
const ASN1_STRING *name_asn1 = X509_NAME_ENTRY_get_data(ne); |
| 334 |
if (name_asn1) { |
| 335 |
unsigned char *str; |
| 336 |
int len = ASN1_STRING_to_UTF8( &str, name_asn1); |
| 337 |
if (len >= 0) { |
| 338 |
ssl_log(transport, PN_LEVEL_TRACE, "commonName from peer cert = '%.*s'", len, str); |
| 339 |
matched = match_dns_pattern( ssl->peer_hostname, (const char *)str, len ); |
| 340 |
OPENSSL_free(str); |
| 341 |
} |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
if (!matched) { |
| 346 |
ssl_log(transport, PN_LEVEL_ERROR, "Error: no name matching %s found in peer cert - rejecting handshake.", |
| 347 |
ssl->peer_hostname); |
| 348 |
preverify_ok = 0; |
| 349 |
#ifdef X509_V_ERR_APPLICATION_VERIFICATION |
| 350 |
X509_STORE_CTX_set_error( ctx, X509_V_ERR_APPLICATION_VERIFICATION ); |
| 351 |
#endif |
| 352 |
} else { |
| 353 |
ssl_log(transport, PN_LEVEL_TRACE, "Name from peer cert matched - peer is valid."); |
| 354 |
} |
| 355 |
return preverify_ok; |
| 356 |
} |
| 357 |
|
| 358 |
// This was introduced in v1.1 |
| 359 |
#if OPENSSL_VERSION_NUMBER < 0x10100000 |
| 360 |
int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g) |
| 361 |
{ |
| 362 |
dh->p = p; |
| 363 |
dh->q = q; |
| 364 |
dh->g = g; |
| 365 |
return 1; |
| 366 |
} |
| 367 |
#endif |
| 368 |
|
| 369 |
// this code was generated using the command: |
| 370 |
// "openssl dhparam -C -2 2048" |
| 371 |
static DH *get_dh2048(void) |
| 372 |
{ |
| 373 |
static const unsigned char dhp_2048[]={ |
| 374 |
0xAE,0xF7,0xE9,0x66,0x26,0x7A,0xAC,0x0A,0x6F,0x1E,0xCD,0x81, |
| 375 |
0xBD,0x0A,0x10,0x7E,0xFA,0x2C,0xF5,0x2D,0x98,0xD4,0xE7,0xD9, |
| 376 |
0xE4,0x04,0x8B,0x06,0x85,0xF2,0x0B,0xA3,0x90,0x15,0x56,0x0C, |
| 377 |
0x8B,0xBE,0xF8,0x48,0xBB,0x29,0x63,0x75,0x12,0x48,0x9D,0x7E, |
| 378 |
0x7C,0x24,0xB4,0x3A,0x38,0x7E,0x97,0x3C,0x77,0x95,0xB0,0xA2, |
| 379 |
0x72,0xB6,0xE9,0xD8,0xB8,0xFA,0x09,0x1B,0xDC,0xB3,0x80,0x6E, |
| 380 |
0x32,0x0A,0xDA,0xBB,0xE8,0x43,0x88,0x5B,0xAB,0xC3,0xB2,0x44, |
| 381 |
0xE1,0x95,0x85,0x0A,0x0D,0x13,0xE2,0x02,0x1E,0x96,0x44,0xCF, |
| 382 |
0xA0,0xD8,0x46,0x32,0x68,0x63,0x7F,0x68,0xB3,0x37,0x52,0xCE, |
| 383 |
0x3A,0x4E,0x48,0x08,0x7F,0xD5,0x53,0x00,0x59,0xA8,0x2C,0xCB, |
| 384 |
0x51,0x64,0x3D,0x5F,0xEF,0x0E,0x5F,0xE6,0xAF,0xD9,0x1E,0xA2, |
| 385 |
0x35,0x64,0x37,0xD7,0x4C,0xC9,0x24,0xFD,0x2F,0x75,0xBB,0x3A, |
| 386 |
0x15,0x82,0x76,0x4D,0xC2,0x8B,0x1E,0xB9,0x4B,0xA1,0x33,0xCF, |
| 387 |
0xAA,0x3B,0x7C,0xC2,0x50,0x60,0x6F,0x45,0x69,0xD3,0x6B,0x88, |
| 388 |
0x34,0x9B,0xE4,0xF8,0xC6,0xC7,0x5F,0x10,0xA1,0xBA,0x01,0x8C, |
| 389 |
0xDA,0xD1,0xA3,0x59,0x9C,0x97,0xEA,0xC3,0xF6,0x02,0x55,0x5C, |
| 390 |
0x92,0x1A,0x39,0x67,0x17,0xE2,0x9B,0x27,0x8D,0xE8,0x5C,0xE9, |
| 391 |
0xA5,0x94,0xBB,0x7E,0x16,0x6F,0x53,0x5A,0x6D,0xD8,0x03,0xC2, |
| 392 |
0xAC,0x7A,0xCD,0x22,0x98,0x8E,0x33,0x2A,0xDE,0xAB,0x12,0xC0, |
| 393 |
0x0B,0x7C,0x0C,0x20,0x70,0xD9,0x0B,0xAE,0x0B,0x2F,0x20,0x9B, |
| 394 |
0xA4,0xED,0xFD,0x49,0x0B,0xE3,0x4A,0xF6,0x28,0xB3,0x98,0xB0, |
| 395 |
0x23,0x1C,0x09,0x33, |
| 396 |
}; |
| 397 |
static const unsigned char dhg_2048[]={ |
| 398 |
0x02, |
| 399 |
}; |
| 400 |
DH *dh = DH_new(); |
| 401 |
BIGNUM *dhp_bn, *dhg_bn; |
| 402 |
|
| 403 |
if (dh == NULL) |
| 404 |
return NULL; |
| 405 |
dhp_bn = BN_bin2bn(dhp_2048, sizeof (dhp_2048), NULL); |
| 406 |
dhg_bn = BN_bin2bn(dhg_2048, sizeof (dhg_2048), NULL); |
| 407 |
if (dhp_bn == NULL || dhg_bn == NULL |
| 408 |
|| !DH_set0_pqg(dh, dhp_bn, NULL, dhg_bn)) { |
| 409 |
DH_free(dh); |
| 410 |
BN_free(dhp_bn); |
| 411 |
BN_free(dhg_bn); |
| 412 |
return NULL; |
| 413 |
} |
| 414 |
return dh; |
| 415 |
} |
| 416 |
|
| 417 |
typedef struct { |
| 418 |
char *id; |
| 419 |
SSL_SESSION *session; |
| 420 |
} ssl_cache_data; |
| 421 |
|
| 422 |
#define SSL_CACHE_SIZE 4 |
| 423 |
static int ssl_cache_ptr = 0; |
| 424 |
static ssl_cache_data ssl_cache[SSL_CACHE_SIZE]; |
| 425 |
|
| 426 |
static void ssn_init(void) { |
| 427 |
ssl_cache_data s = {NULL, NULL}; |
| 428 |
for (int i=0; i<SSL_CACHE_SIZE; i++) { |
| 429 |
ssl_cache[i] = s; |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
static void ssn_restore(pn_transport_t *transport, pni_ssl_t *ssl) { |
| 434 |
if (!ssl->session_id) return; |
| 435 |
for (int i = ssl_cache_ptr;;) { |
| 436 |
i = (i==0) ? SSL_CACHE_SIZE-1 : i-1; |
| 437 |
if (ssl_cache[i].id == NULL) return; |
| 438 |
if (strcmp(ssl_cache[i].id, ssl->session_id) == 0) { |
| 439 |
ssl_log( transport, PN_LEVEL_TRACE, "Restoring previous session id=%s", ssl->session_id ); |
| 440 |
int rc = SSL_set_session( ssl->ssl, ssl_cache[i].session ); |
| 441 |
if (rc != 1) { |
| 442 |
ssl_log( transport, PN_LEVEL_WARNING, "Session restore failed, id=%s", ssl->session_id ); |
| 443 |
} |
| 444 |
return; |
| 445 |
} |
| 446 |
if (i == ssl_cache_ptr) return; |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
static void ssn_save(pn_transport_t *transport, pni_ssl_t *ssl) { |
| 451 |
if (ssl->session_id) { |
| 452 |
// Attach the session id to the session before we close the connection |
| 453 |
// So that if we find it in the cache later we can figure out the session id |
| 454 |
SSL_SESSION *session = SSL_get1_session( ssl->ssl ); |
| 455 |
if (session) { |
| 456 |
ssl_log(transport, PN_LEVEL_TRACE, "Saving SSL session as %s", ssl->session_id ); |
| 457 |
// If we're overwriting a value, need to free it |
| 458 |
free(ssl_cache[ssl_cache_ptr].id); |
| 459 |
if (ssl_cache[ssl_cache_ptr].session) SSL_SESSION_free(ssl_cache[ssl_cache_ptr].session); |
| 460 |
|
| 461 |
char *id = pn_strdup( ssl->session_id ); |
| 462 |
ssl_cache_data s = {id, session}; |
| 463 |
ssl_cache[ssl_cache_ptr++] = s; |
| 464 |
if (ssl_cache_ptr==SSL_CACHE_SIZE) ssl_cache_ptr = 0; |
| 465 |
} |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
/** Public API - visible to application code */ |
| 470 |
|
| 471 |
bool pn_ssl_present(void) |
| 472 |
{ |
| 473 |
return true; |
| 474 |
} |
| 475 |
|
| 476 |
static bool ensure_initialized(void); |
| 477 |
|
| 478 |
static bool pni_init_ssl_domain( pn_ssl_domain_t * domain, pn_ssl_mode_t mode ) |
| 479 |
{ |
| 480 |
if (!ensure_initialized()) { |
| 481 |
ssl_log_error("Unable to initialize OpenSSL library"); |
| 482 |
return false; |
| 483 |
} |
| 484 |
|
| 485 |
domain->ref_count = 1; |
| 486 |
domain->mode = mode; |
| 487 |
|
| 488 |
// enable all supported protocol versions, then explicitly disable the |
| 489 |
// known vulnerable ones. This should allow us to use the latest version |
| 490 |
// of the TLS standard that the installed library supports. |
| 491 |
switch(mode) { |
| 492 |
case PN_SSL_MODE_CLIENT: |
| 493 |
domain->ctx = SSL_CTX_new(SSLv23_client_method()); // and TLSv1+ |
| 494 |
if (!domain->ctx) { |
| 495 |
ssl_log_error("Unable to initialize OpenSSL context."); |
| 496 |
return false; |
| 497 |
} |
| 498 |
SSL_CTX_set_session_cache_mode(domain->ctx, SSL_SESS_CACHE_CLIENT); |
| 499 |
|
| 500 |
// By default, require peer name verification - this is a safe default |
| 501 |
if (pn_ssl_domain_set_peer_authentication( domain, PN_SSL_VERIFY_PEER_NAME, NULL )) { |
| 502 |
SSL_CTX_free(domain->ctx); |
| 503 |
return false; |
| 504 |
} |
| 505 |
break; |
| 506 |
|
| 507 |
case PN_SSL_MODE_SERVER: |
| 508 |
domain->ctx = SSL_CTX_new(SSLv23_server_method()); // and TLSv1+ |
| 509 |
if (!domain->ctx) { |
| 510 |
ssl_log_error("Unable to initialize OpenSSL context."); |
| 511 |
return false; |
| 512 |
} |
| 513 |
// By default, allow anonymous ciphers and do no authentication so certificates are |
| 514 |
// not required 'out of the box'; authenticating the client can be done by SASL. |
| 515 |
if (pn_ssl_domain_set_peer_authentication( domain, PN_SSL_ANONYMOUS_PEER, NULL )) { |
| 516 |
SSL_CTX_free(domain->ctx); |
| 517 |
return false; |
| 518 |
} |
| 519 |
break; |
| 520 |
|
| 521 |
default: |
| 522 |
ssl_log(NULL, PN_LEVEL_ERROR, "Invalid value for pn_ssl_mode_t: %d", mode); |
| 523 |
return false; |
| 524 |
} |
| 525 |
|
| 526 |
// By default set up system default certificates |
| 527 |
if (!SSL_CTX_set_default_verify_paths(domain->ctx)){ |
| 528 |
ssl_log_error("Failed to set default certificate paths"); |
| 529 |
SSL_CTX_free(domain->ctx); |
| 530 |
return false; |
| 531 |
}; |
| 532 |
|
| 533 |
const long reject_insecure = |
| 534 |
SSL_OP_NO_SSLv2 |
| 535 |
| SSL_OP_NO_SSLv3 |
| 536 |
// Mitigate the CRIME vulnerability |
| 537 |
# ifdef SSL_OP_NO_COMPRESSION |
| 538 |
| SSL_OP_NO_COMPRESSION |
| 539 |
# endif |
| 540 |
; |
| 541 |
SSL_CTX_set_options(domain->ctx, reject_insecure); |
| 542 |
|
| 543 |
# ifdef SSL_SECOP_PEER |
| 544 |
domain->default_seclevel = SSL_CTX_get_security_level(domain->ctx); |
| 545 |
# endif |
| 546 |
|
| 547 |
DH *dh = get_dh2048(); |
| 548 |
if (dh) { |
| 549 |
SSL_CTX_set_tmp_dh(domain->ctx, dh); |
| 550 |
DH_free(dh); |
| 551 |
SSL_CTX_set_options(domain->ctx, SSL_OP_SINGLE_DH_USE); |
| 552 |
} |
| 553 |
|
| 554 |
return true; |
| 555 |
} |
| 556 |
|
| 557 |
pn_ssl_domain_t *pn_ssl_domain( pn_ssl_mode_t mode ) |
| 558 |
{ |
| 559 |
pn_ssl_domain_t *domain = (pn_ssl_domain_t *) calloc(1, sizeof(pn_ssl_domain_t)); |
| 560 |
if (!domain) return NULL; |
| 561 |
|
| 562 |
if (!pni_init_ssl_domain(domain, mode)) { |
| 563 |
free(domain); |
| 564 |
return NULL; |
| 565 |
} |
| 566 |
|
| 567 |
return domain; |
| 568 |
} |
| 569 |
|
| 570 |
void pn_ssl_domain_free( pn_ssl_domain_t *domain ) |
| 571 |
{ |
| 572 |
if (--domain->ref_count == 0) { |
| 573 |
|
| 574 |
SSL_CTX_free(domain->ctx); |
| 575 |
free(domain->keyfile_pw); |
| 576 |
free(domain->trusted_CAs); |
| 577 |
free(domain->ciphers); |
| 578 |
free(domain); |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
|
| 583 |
int pn_ssl_domain_set_credentials( pn_ssl_domain_t *domain, |
| 584 |
const char *certificate_file, |
| 585 |
const char *private_key_file, |
| 586 |
const char *password) |
| 587 |
{ |
| 588 |
if (!domain || !domain->ctx) return -1; |
| 589 |
|
| 590 |
if (SSL_CTX_use_certificate_chain_file(domain->ctx, certificate_file) != 1) { |
| 591 |
ssl_log_error("SSL_CTX_use_certificate_chain_file( %s ) failed", certificate_file); |
| 592 |
return -3; |
| 593 |
} |
| 594 |
|
| 595 |
if (password) { |
| 596 |
domain->keyfile_pw = pn_strdup(password); // @todo: obfuscate me!!! |
| 597 |
SSL_CTX_set_default_passwd_cb(domain->ctx, keyfile_pw_cb); |
| 598 |
SSL_CTX_set_default_passwd_cb_userdata(domain->ctx, domain->keyfile_pw); |
| 599 |
} |
| 600 |
|
| 601 |
if (SSL_CTX_use_PrivateKey_file(domain->ctx, private_key_file, SSL_FILETYPE_PEM) != 1) { |
| 602 |
ssl_log_error("SSL_CTX_use_PrivateKey_file( %s ) failed", private_key_file); |
| 603 |
return -4; |
| 604 |
} |
| 605 |
|
| 606 |
if (SSL_CTX_check_private_key(domain->ctx) != 1) { |
| 607 |
ssl_log_error("The key file %s is not consistent with the certificate %s", |
| 608 |
private_key_file, certificate_file); |
| 609 |
return -5; |
| 610 |
} |
| 611 |
|
| 612 |
domain->has_certificate = true; |
| 613 |
|
| 614 |
// bug in older versions of OpenSSL: servers may request client cert even if anonymous |
| 615 |
// cipher was negotiated. TLSv1 will reject such a request. Hack: once a cert is |
| 616 |
// configured, allow only authenticated ciphers. |
| 617 |
if (!domain->ciphers && !SSL_CTX_set_cipher_list( domain->ctx, CIPHERS_AUTHENTICATE )) { |
| 618 |
ssl_log_error("Failed to set cipher list to %s", CIPHERS_AUTHENTICATE); |
| 619 |
return -6; |
| 620 |
} |
| 621 |
|
| 622 |
return 0; |
| 623 |
} |
| 624 |
|
| 625 |
int pn_ssl_domain_set_ciphers(pn_ssl_domain_t *domain, const char *ciphers) |
| 626 |
{ |
| 627 |
if (!SSL_CTX_set_cipher_list(domain->ctx, ciphers)) { |
| 628 |
ssl_log_error("Failed to set cipher list to %s", ciphers); |
| 629 |
return -6; |
| 630 |
} |
| 631 |
if (domain->ciphers) free(domain->ciphers); |
| 632 |
domain->ciphers = pn_strdup(ciphers); |
| 633 |
return 0; |
| 634 |
} |
| 635 |
|
| 636 |
int pn_ssl_domain_set_protocols(pn_ssl_domain_t *domain, const char *protocols) |
| 637 |
{ |
| 638 |
static const struct { |
| 639 |
const char *name; |
| 640 |
const long option; |
| 641 |
} protocol_options[] = |
| 642 |
{ |
| 643 |
{"TLSv1", SSL_OP_NO_TLSv1}, |
| 644 |
{"TLSv1.1", SSL_OP_NO_TLSv1_1}, |
| 645 |
{"TLSv1.2", SSL_OP_NO_TLSv1_2}, |
| 646 |
#ifdef SSL_OP_NO_TLSv1_3 |
| 647 |
{"TLSv1.3", SSL_OP_NO_TLSv1_3}, |
| 648 |
#endif |
| 649 |
}; |
| 650 |
static const char seps[] = " ,;"; |
| 651 |
static const long all_prots = |
| 652 |
SSL_OP_NO_TLSv1 |
| 653 |
| SSL_OP_NO_TLSv1_1 |
| 654 |
| SSL_OP_NO_TLSv1_2 |
| 655 |
#ifdef SSL_OP_NO_TLSv1_3 |
| 656 |
| SSL_OP_NO_TLSv1_3 |
| 657 |
#endif |
| 658 |
; |
| 659 |
|
| 660 |
// Start with all protocols turned off |
| 661 |
long options = all_prots; |
| 662 |
|
| 663 |
// For each separate token in protocols |
| 664 |
const char *token = protocols; |
| 665 |
while (*token!=0) { |
| 666 |
// Find next separator |
| 667 |
size_t tsize = strcspn(token, seps); |
| 668 |
while (tsize==0 && *token!=0) { |
| 669 |
++token; |
| 670 |
tsize = strcspn(token, seps); |
| 671 |
} |
| 672 |
if (tsize==0) break; // No more tokens |
| 673 |
|
| 674 |
// Linear search the possibilities for the option to set |
| 675 |
for (size_t i = 0; i<sizeof(protocol_options)/sizeof(*protocol_options); ++i) { |
| 676 |
if (strncmp(token, protocol_options[i].name, tsize)==0) { |
| 677 |
options &= ~protocol_options[i].option; |
| 678 |
goto found; |
| 679 |
} |
| 680 |
} |
| 681 |
// Didn't find any match - error |
| 682 |
return PN_ARG_ERR; |
| 683 |
|
| 684 |
found: |
| 685 |
token += tsize; |
| 686 |
} |
| 687 |
|
| 688 |
// Check if we found anything |
| 689 |
if (options==all_prots) return PN_ARG_ERR; |
| 690 |
|
| 691 |
SSL_CTX_clear_options(domain->ctx, all_prots); |
| 692 |
SSL_CTX_set_options(domain->ctx, options); |
| 693 |
return 0; |
| 694 |
} |
| 695 |
|
| 696 |
int pn_ssl_domain_set_trusted_ca_db(pn_ssl_domain_t *domain, |
| 697 |
const char *certificate_db) |
| 698 |
{ |
| 699 |
if (!domain) return -1; |
| 700 |
|
| 701 |
// certificates can be either a file or a directory, which determines how it is passed |
| 702 |
// to SSL_CTX_load_verify_locations() |
| 703 |
struct stat sbuf; |
| 704 |
if (stat( certificate_db, &sbuf ) != 0) { |
| 705 |
ssl_log(NULL, PN_LEVEL_ERROR, "stat(%s) failed: %s", certificate_db, strerror(errno)); |
| 706 |
return -1; |
| 707 |
} |
| 708 |
|
| 709 |
const char *file; |
| 710 |
const char *dir; |
| 711 |
if (S_ISDIR(sbuf.st_mode)) { |
| 712 |
dir = certificate_db; |
| 713 |
file = NULL; |
| 714 |
} else { |
| 715 |
dir = NULL; |
| 716 |
file = certificate_db; |
| 717 |
} |
| 718 |
|
| 719 |
if (SSL_CTX_load_verify_locations( domain->ctx, file, dir ) != 1) { |
| 720 |
ssl_log_error("SSL_CTX_load_verify_locations( %s ) failed", certificate_db); |
| 721 |
return -1; |
| 722 |
} |
| 723 |
|
| 724 |
return 0; |
| 725 |
} |
| 726 |
|
| 727 |
|
| 728 |
int pn_ssl_domain_set_peer_authentication(pn_ssl_domain_t *domain, |
| 729 |
const pn_ssl_verify_mode_t mode, |
| 730 |
const char *trusted_CAs) |
| 731 |
{ |
| 732 |
if (!domain) return -1; |
| 733 |
|
| 734 |
switch (mode) { |
| 735 |
case PN_SSL_VERIFY_PEER: |
| 736 |
case PN_SSL_VERIFY_PEER_NAME: |
| 737 |
|
| 738 |
#ifdef SSL_SECOP_PEER |
| 739 |
SSL_CTX_set_security_level(domain->ctx, domain->default_seclevel); |
| 740 |
#endif |
| 741 |
|
| 742 |
if (domain->mode == PN_SSL_MODE_SERVER) { |
| 743 |
// openssl requires that server connections supply a list of trusted CAs which is |
| 744 |
// sent to the client |
| 745 |
if (!trusted_CAs) { |
| 746 |
ssl_log(NULL, PN_LEVEL_ERROR, "Error: a list of trusted CAs must be provided."); |
| 747 |
return -1; |
| 748 |
} |
| 749 |
if (!domain->has_certificate) { |
| 750 |
ssl_log(NULL, PN_LEVEL_ERROR, "Error: Server cannot verify peer without configuring a certificate, use pn_ssl_domain_set_credentials()"); |
| 751 |
return -1; |
| 752 |
} |
| 753 |
|
| 754 |
if (domain->trusted_CAs) free(domain->trusted_CAs); |
| 755 |
domain->trusted_CAs = pn_strdup( trusted_CAs ); |
| 756 |
STACK_OF(X509_NAME) *cert_names; |
| 757 |
cert_names = SSL_load_client_CA_file( domain->trusted_CAs ); |
| 758 |
if (cert_names != NULL) |
| 759 |
SSL_CTX_set_client_CA_list(domain->ctx, cert_names); |
| 760 |
else { |
| 761 |
ssl_log(NULL, PN_LEVEL_ERROR, "Error: Unable to process file of trusted CAs: %s", trusted_CAs); |
| 762 |
return -1; |
| 763 |
} |
| 764 |
} |
| 765 |
|
| 766 |
SSL_CTX_set_verify( domain->ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, |
| 767 |
verify_callback); |
| 768 |
#if (OPENSSL_VERSION_NUMBER < 0x00905100L) |
| 769 |
SSL_CTX_set_verify_depth(domain->ctx, 1); |
| 770 |
#endif |
| 771 |
|
| 772 |
// A bit of a hack - If we asked for peer verification then disallow anonymous ciphers |
| 773 |
// A much more robust thing would be to ensure that we actually have a peer certificate |
| 774 |
// when we've finished the SSL handshake |
| 775 |
if (!domain->ciphers && !SSL_CTX_set_cipher_list( domain->ctx, CIPHERS_AUTHENTICATE )) { |
| 776 |
ssl_log_error("Failed to set cipher list to %s", CIPHERS_AUTHENTICATE); |
| 777 |
return -1; |
| 778 |
} |
| 779 |
break; |
| 780 |
|
| 781 |
case PN_SSL_ANONYMOUS_PEER: // hippie free love mode... :) |
| 782 |
#ifdef SSL_SECOP_PEER |
| 783 |
// Must use lowest OpenSSL security level to enable anonymous ciphers. |
| 784 |
SSL_CTX_set_security_level(domain->ctx, 0); |
| 785 |
#endif |
| 786 |
SSL_CTX_set_verify( domain->ctx, SSL_VERIFY_NONE, NULL ); |
| 787 |
// Only allow anonymous ciphers if we allow anonymous peers |
| 788 |
if (!domain->ciphers && !SSL_CTX_set_cipher_list( domain->ctx, CIPHERS_ANONYMOUS )) { |
| 789 |
ssl_log_error("Failed to set cipher list to %s", CIPHERS_ANONYMOUS); |
| 790 |
return -1; |
| 791 |
} |
| 792 |
break; |
| 793 |
|
| 794 |
default: |
| 795 |
ssl_log(NULL, PN_LEVEL_ERROR, "Invalid peer authentication mode given." ); |
| 796 |
return -1; |
| 797 |
} |
| 798 |
|
| 799 |
domain->verify_mode = mode; |
| 800 |
return 0; |
| 801 |
} |
| 802 |
|
| 803 |
const pn_io_layer_t ssl_layer = { |
| 804 |
process_input_ssl, |
| 805 |
process_output_ssl, |
| 806 |
handle_error_ssl, |
| 807 |
NULL, |
| 808 |
buffered_output |
| 809 |
}; |
| 810 |
|
| 811 |
const pn_io_layer_t ssl_input_closed_layer = { |
| 812 |
process_input_done, |
| 813 |
process_output_ssl, |
| 814 |
handle_error_ssl, |
| 815 |
NULL, |
| 816 |
buffered_output |
| 817 |
}; |
| 818 |
|
| 819 |
const pn_io_layer_t ssl_output_closed_layer = { |
| 820 |
process_input_ssl, |
| 821 |
process_output_done, |
| 822 |
handle_error_ssl, |
| 823 |
NULL, |
| 824 |
buffered_output |
| 825 |
}; |
| 826 |
|
| 827 |
const pn_io_layer_t ssl_closed_layer = { |
| 828 |
process_input_done, |
| 829 |
process_output_done, |
| 830 |
handle_error_ssl, |
| 831 |
NULL, |
| 832 |
buffered_output |
| 833 |
}; |
| 834 |
|
| 835 |
static pn_ssl_domain_t default_client_domain = {0}; |
| 836 |
static pn_ssl_domain_t default_server_domain = {0}; |
| 837 |
|
| 838 |
int pn_ssl_init(pn_ssl_t *ssl0, pn_ssl_domain_t *domain, const char *session_id) |
| 839 |
{ |
| 840 |
pn_transport_t *transport = get_transport_internal(ssl0); |
| 841 |
pni_ssl_t *ssl = transport->ssl; |
| 842 |
if (!ssl) return -1; |
| 843 |
|
| 844 |
if (!domain) { |
| 845 |
if (transport->server) { |
| 846 |
domain = &default_server_domain; |
| 847 |
if (domain->ref_count==0) { |
| 848 |
pni_init_ssl_domain(domain, PN_SSL_MODE_SERVER); |
| 849 |
} |
| 850 |
} else { |
| 851 |
domain = &default_client_domain; |
| 852 |
if (domain->ref_count==0) { |
| 853 |
pni_init_ssl_domain(domain, PN_SSL_MODE_CLIENT); |
| 854 |
} |
| 855 |
} |
| 856 |
} |
| 857 |
|
| 858 |
ssl->mode = domain->mode; |
| 859 |
ssl->verify_mode = domain->verify_mode; |
| 860 |
|
| 861 |
if (session_id && domain->mode == PN_SSL_MODE_CLIENT) |
| 862 |
ssl->session_id = pn_strdup(session_id); |
| 863 |
|
| 864 |
// If SSL doesn't specifically allow skipping encryption, require SSL |
| 865 |
// TODO: This is a probably a stop-gap until allow_unsecured is removed |
| 866 |
if (!domain->allow_unsecured) transport->encryption_required = true; |
| 867 |
|
| 868 |
return init_ssl_socket(transport, ssl, domain); |
| 869 |
} |
| 870 |
|
| 871 |
|
| 872 |
int pn_ssl_domain_allow_unsecured_client(pn_ssl_domain_t *domain) |
| 873 |
{ |
| 874 |
if (!domain) return -1; |
| 875 |
if (domain->mode != PN_SSL_MODE_SERVER) { |
| 876 |
ssl_log(NULL, PN_LEVEL_ERROR, "Cannot permit unsecured clients - not a server."); |
| 877 |
return -1; |
| 878 |
} |
| 879 |
domain->allow_unsecured = true; |
| 880 |
return 0; |
| 881 |
} |
| 882 |
|
| 883 |
int pn_ssl_get_ssf(pn_ssl_t *ssl0) |
| 884 |
{ |
| 885 |
const SSL_CIPHER *c; |
| 886 |
|
| 887 |
pni_ssl_t *ssl = get_ssl_internal(ssl0); |
| 888 |
if (ssl && ssl->ssl && (c = SSL_get_current_cipher( ssl->ssl ))) { |
| 889 |
return SSL_CIPHER_get_bits(c, NULL); |
| 890 |
} |
| 891 |
return 0; |
| 892 |
} |
| 893 |
|
| 894 |
bool pn_ssl_get_cipher_name(pn_ssl_t *ssl0, char *buffer, size_t size ) |
| 895 |
{ |
| 896 |
const SSL_CIPHER *c; |
| 897 |
|
| 898 |
pni_ssl_t *ssl = get_ssl_internal(ssl0); |
| 899 |
if (buffer && size) *buffer = '\0'; |
| 900 |
if (ssl->ssl && (c = SSL_get_current_cipher( ssl->ssl ))) { |
| 901 |
const char *v = SSL_CIPHER_get_name(c); |
| 902 |
if (buffer && v) { |
| 903 |
snprintf( buffer, size, "%s", v ); |
| 904 |
return true; |
| 905 |
} |
| 906 |
} |
| 907 |
return false; |
| 908 |
} |
| 909 |
|
| 910 |
bool pn_ssl_get_protocol_name(pn_ssl_t *ssl0, char *buffer, size_t size ) |
| 911 |
{ |
| 912 |
const SSL_CIPHER *c; |
| 913 |
|
| 914 |
pni_ssl_t *ssl = get_ssl_internal(ssl0); |
| 915 |
if (buffer && size) *buffer = '\0'; |
| 916 |
if (ssl->ssl && (c = SSL_get_current_cipher( ssl->ssl ))) { |
| 917 |
const char *v = SSL_CIPHER_get_version(c); |
| 918 |
if (buffer && v) { |
| 919 |
snprintf( buffer, size, "%s", v ); |
| 920 |
return true; |
| 921 |
} |
| 922 |
} |
| 923 |
return false; |
| 924 |
} |
| 925 |
|
| 926 |
|
| 927 |
void pn_ssl_free(pn_transport_t *transport) |
| 928 |
{ |
| 929 |
pni_ssl_t *ssl = transport->ssl; |
| 930 |
if (!ssl) return; |
| 931 |
ssl_log(transport, PN_LEVEL_TRACE, "SSL socket freed." ); |
| 932 |
release_ssl_socket( ssl ); |
| 933 |
if (ssl->session_id) free((void *)ssl->session_id); |
| 934 |
if (ssl->peer_hostname) free((void *)ssl->peer_hostname); |
| 935 |
if (ssl->inbuf) free((void *)ssl->inbuf); |
| 936 |
if (ssl->outbuf) free((void *)ssl->outbuf); |
| 937 |
if (ssl->subject) free(ssl->subject); |
| 938 |
if (ssl->peer_certificate) X509_free(ssl->peer_certificate); |
| 939 |
free(ssl); |
| 940 |
} |
| 941 |
|
| 942 |
pn_ssl_t *pn_ssl(pn_transport_t *transport) |
| 943 |
{ |
| 944 |
if (!transport) return NULL; |
| 945 |
if (transport->ssl) return (pn_ssl_t *) transport; |
| 946 |
|
| 947 |
pni_ssl_t *ssl = (pni_ssl_t *) calloc(1, sizeof(pni_ssl_t)); |
| 948 |
if (!ssl) return NULL; |
| 949 |
ssl->out_size = APP_BUF_SIZE; |
| 950 |
uint32_t max_frame = pn_transport_get_max_frame(transport); |
| 951 |
ssl->in_size = max_frame ? max_frame : APP_BUF_SIZE; |
| 952 |
ssl->outbuf = (char *)malloc(ssl->out_size); |
| 953 |
if (!ssl->outbuf) { |
| 954 |
free(ssl); |
| 955 |
return NULL; |
| 956 |
} |
| 957 |
ssl->inbuf = (char *)malloc(ssl->in_size); |
| 958 |
if (!ssl->inbuf) { |
| 959 |
free(ssl->outbuf); |
| 960 |
free(ssl); |
| 961 |
return NULL; |
| 962 |
} |
| 963 |
|
| 964 |
transport->ssl = ssl; |
| 965 |
|
| 966 |
// Set up hostname from any bound connection |
| 967 |
if (transport->connection) { |
| 968 |
if (pn_string_size(transport->connection->hostname)) { |
| 969 |
pn_ssl_set_peer_hostname((pn_ssl_t *) transport, pn_string_get(transport->connection->hostname)); |
| 970 |
} |
| 971 |
} |
| 972 |
|
| 973 |
return (pn_ssl_t *) transport; |
| 974 |
} |
| 975 |
|
| 976 |
|
| 977 |
/** Private: */ |
| 978 |
|
| 979 |
static int keyfile_pw_cb(char *buf, int size, int rwflag, void *userdata) |
| 980 |
{ |
| 981 |
strncpy(buf, (char *)userdata, size); // @todo: un-obfuscate me!!! |
| 982 |
buf[size - 1] = '\0'; |
| 983 |
return(strlen(buf)); |
| 984 |
} |
| 985 |
|
| 986 |
|
| 987 |
static int start_ssl_shutdown(pn_transport_t *transport) |
| 988 |
{ |
| 989 |
pni_ssl_t *ssl = transport->ssl; |
| 990 |
if (!ssl->ssl_shutdown) { |
| 991 |
ssl_log(transport, PN_LEVEL_TRACE, "Shutting down SSL connection..."); |
| 992 |
ssn_save(transport, ssl); |
| 993 |
ssl->ssl_shutdown = true; |
| 994 |
BIO_ssl_shutdown( ssl->bio_ssl ); |
| 995 |
} |
| 996 |
return 0; |
| 997 |
} |
| 998 |
|
| 999 |
|
| 1000 |
//////// SSL Connections |
| 1001 |
|
| 1002 |
|
| 1003 |
// take data from the network, and pass it into SSL. Attempt to read decrypted data from |
| 1004 |
// SSL socket and pass it to the application. |
| 1005 |
static ssize_t process_input_ssl( pn_transport_t *transport, unsigned int layer, const char *input_data, size_t available) |
| 1006 |
{ |
| 1007 |
pni_ssl_t *ssl = transport->ssl; |
| 1008 |
if (ssl->ssl == NULL) { |
| 1009 |
if (init_ssl_socket(transport, ssl, NULL)) return PN_EOS; |
| 1010 |
transport->present_layers |= LAYER_SSL; |
| 1011 |
} |
| 1012 |
|
| 1013 |
ssl_log( transport, PN_LEVEL_TRACE, "process_input_ssl( data size=%d )",available ); |
| 1014 |
|
| 1015 |
ssize_t consumed = 0; |
| 1016 |
bool work_pending; |
| 1017 |
bool shutdown_input = (available == 0); // caller is closed |
| 1018 |
|
| 1019 |
do { |
| 1020 |
work_pending = false; |
| 1021 |
ERR_clear_error(); |
| 1022 |
|
| 1023 |
// Write to network bio as much as possible, consuming bytes/available |
| 1024 |
|
| 1025 |
if (available > 0) { |
| 1026 |
int written = BIO_write( ssl->bio_net_io, input_data, available ); |
| 1027 |
if (written > 0) { |
| 1028 |
input_data += written; |
| 1029 |
available -= written; |
| 1030 |
consumed += written; |
| 1031 |
ssl->read_blocked = false; |
| 1032 |
work_pending = (available > 0); |
| 1033 |
ssl_log( transport, PN_LEVEL_TRACE, "Wrote %d bytes to BIO Layer, %" PN_ZU " left over", written, available ); |
| 1034 |
} |
| 1035 |
} else if (shutdown_input) { |
| 1036 |
// lower layer (caller) has closed. Close the WRITE side of the BIO. This will cause |
| 1037 |
// an EOF to be passed to SSL once all pending inbound data has been consumed. |
| 1038 |
ssl_log( transport, PN_LEVEL_TRACE, "Lower layer closed - shutting down BIO write side"); |
| 1039 |
(void)BIO_shutdown_wr( ssl->bio_net_io ); |
| 1040 |
shutdown_input = false; |
| 1041 |
} |
| 1042 |
|
| 1043 |
// Read all available data from the SSL socket |
| 1044 |
|
| 1045 |
if (!ssl->ssl_closed && ssl->in_count < ssl->in_size) { |
| 1046 |
int read = BIO_read( ssl->bio_ssl, &ssl->inbuf[ssl->in_count], ssl->in_size - ssl->in_count ); |
| 1047 |
if (read > 0) { |
| 1048 |
ssl_log( transport, PN_LEVEL_TRACE, "Read %d bytes from SSL socket for app", read ); |
| 1049 |
ssl_log_clear_data(transport, &ssl->inbuf[ssl->in_count], read ); |
| 1050 |
ssl->in_count += read; |
| 1051 |
work_pending = true; |
| 1052 |
} else { |
| 1053 |
if (!BIO_should_retry(ssl->bio_ssl)) { |
| 1054 |
int reason = SSL_get_error( ssl->ssl, read ); |
| 1055 |
switch (reason) { |
| 1056 |
case SSL_ERROR_ZERO_RETURN: |
| 1057 |
// SSL closed cleanly |
| 1058 |
ssl_log(transport, PN_LEVEL_TRACE, "SSL connection has closed"); |
| 1059 |
start_ssl_shutdown(transport); // KAG: not sure - this may not be necessary |
| 1060 |
ssl->ssl_closed = true; |
| 1061 |
break; |
| 1062 |
default: |
| 1063 |
// unexpected error |
| 1064 |
return (ssize_t)ssl_failed(transport); |
| 1065 |
} |
| 1066 |
} else { |
| 1067 |
if (BIO_should_write( ssl->bio_ssl )) { |
| 1068 |
ssl->write_blocked = true; |
| 1069 |
ssl_log(transport, PN_LEVEL_TRACE, "Detected write-blocked"); |
| 1070 |
} |
| 1071 |
if (BIO_should_read( ssl->bio_ssl )) { |
| 1072 |
ssl->read_blocked = true; |
| 1073 |
ssl_log(transport, PN_LEVEL_TRACE, "Detected read-blocked"); |
| 1074 |
} |
| 1075 |
} |
| 1076 |
} |
| 1077 |
} |
| 1078 |
|
| 1079 |
// write incoming data to app layer |
| 1080 |
|
| 1081 |
if (!ssl->app_input_closed) { |
| 1082 |
if (ssl->in_count > 0 || ssl->ssl_closed) { /* if ssl_closed, send 0 count */ |
| 1083 |
ssize_t consumed = transport->io_layers[layer+1]->process_input(transport, layer+1, ssl->inbuf, ssl->in_count); |
| 1084 |
if (consumed > 0) { |
| 1085 |
ssl->in_count -= consumed; |
| 1086 |
if (ssl->in_count) |
| 1087 |
memmove( ssl->inbuf, ssl->inbuf + consumed, ssl->in_count ); |
| 1088 |
work_pending = true; |
| 1089 |
ssl_log( transport, PN_LEVEL_TRACE, "Application consumed %d bytes from peer", (int) consumed ); |
| 1090 |
} else if (consumed < 0) { |
| 1091 |
ssl_log(transport, PN_LEVEL_TRACE, "Application layer closed its input, error=%d (discarding %d bytes)", |
| 1092 |
(int) consumed, (int)ssl->in_count); |
| 1093 |
ssl->in_count = 0; // discard any pending input |
| 1094 |
ssl->app_input_closed = consumed; |
| 1095 |
if (ssl->app_output_closed && ssl->out_count == 0) { |
| 1096 |
// both sides of app closed, and no more app output pending: |
| 1097 |
start_ssl_shutdown(transport); |
| 1098 |
} |
| 1099 |
} else { |
| 1100 |
// app did not consume any bytes, must be waiting for a full frame |
| 1101 |
if (ssl->in_count == ssl->in_size) { |
| 1102 |
// but the buffer is full, not enough room for a full frame. |
| 1103 |
// can we grow the buffer? |
| 1104 |
uint32_t max_frame = pn_transport_get_max_frame(transport); |
| 1105 |
if (!max_frame) max_frame = ssl->in_size * 2; // no limit |
| 1106 |
if (ssl->in_size < max_frame) { |
| 1107 |
// no max frame limit - grow it. |
| 1108 |
size_t newsize = pn_min(max_frame, ssl->in_size * 2); |
| 1109 |
char *newbuf = (char *)realloc( ssl->inbuf, newsize ); |
| 1110 |
if (newbuf) { |
| 1111 |
ssl->in_size = newsize; |
| 1112 |
ssl->inbuf = newbuf; |
| 1113 |
work_pending = true; // can we get more input? |
| 1114 |
} |
| 1115 |
} else { |
| 1116 |
// can't gather any more input, but app needs more? |
| 1117 |
// This is a bug - since SSL can buffer up to max-frame, |
| 1118 |
// the application _must_ have enough data to process. If |
| 1119 |
// this is an oversized frame, the app _must_ handle it |
| 1120 |
// by returning an error code to SSL. |
| 1121 |
ssl_log(transport, PN_LEVEL_ERROR, "Error: application unable to consume input."); |
| 1122 |
} |
| 1123 |
} |
| 1124 |
} |
| 1125 |
} |
| 1126 |
} |
| 1127 |
|
| 1128 |
} while (work_pending); |
| 1129 |
|
| 1130 |
//_log(ssl, "ssl_closed=%d in_count=%d app_input_closed=%d app_output_closed=%d", |
| 1131 |
// ssl->ssl_closed, ssl->in_count, ssl->app_input_closed, ssl->app_output_closed ); |
| 1132 |
|
| 1133 |
// PROTON-82: Instead, close the input side as soon as we've completed enough of the SSL |
| 1134 |
// shutdown handshake to send the close_notify. We're not requiring the response, as |
| 1135 |
// some implementations never reply. |
| 1136 |
// --- |
| 1137 |
// tell transport our input side is closed if the SSL socket cannot be read from any |
| 1138 |
// longer, AND any pending input has been written up to the application (or the |
| 1139 |
// application is closed) |
| 1140 |
//if (ssl->ssl_closed && ssl->app_input_closed) { |
| 1141 |
// consumed = ssl->app_input_closed; |
| 1142 |
//} |
| 1143 |
if (ssl->app_input_closed && (SSL_get_shutdown(ssl->ssl) & SSL_SENT_SHUTDOWN) ) { |
| 1144 |
consumed = ssl->app_input_closed; |
| 1145 |
if (transport->io_layers[layer]==&ssl_output_closed_layer) { |
| 1146 |
transport->io_layers[layer] = &ssl_closed_layer; |
| 1147 |
} else { |
| 1148 |
transport->io_layers[layer] = &ssl_input_closed_layer; |
| 1149 |
} |
| 1150 |
} |
| 1151 |
ssl_log(transport, PN_LEVEL_TRACE, "process_input_ssl() returning %d", (int) consumed); |
| 1152 |
return consumed; |
| 1153 |
} |
| 1154 |
|
| 1155 |
static void handle_error_ssl(pn_transport_t *transport, unsigned int layer) |
| 1156 |
{ |
| 1157 |
transport->io_layers[layer] = &ssl_closed_layer; |
| 1158 |
} |
| 1159 |
|
| 1160 |
static ssize_t process_output_ssl( pn_transport_t *transport, unsigned int layer, char *buffer, size_t max_len) |
| 1161 |
{ |
| 1162 |
pni_ssl_t *ssl = transport->ssl; |
| 1163 |
if (!ssl) return PN_EOS; |
| 1164 |
if (ssl->ssl == NULL && init_ssl_socket(transport, ssl, NULL)) return PN_EOS; |
| 1165 |
|
| 1166 |
ssize_t written = 0; |
| 1167 |
bool work_pending; |
| 1168 |
|
| 1169 |
do { |
| 1170 |
work_pending = false; |
| 1171 |
ERR_clear_error(); |
| 1172 |
|
| 1173 |
// first, get any pending application output, if possible |
| 1174 |
|
| 1175 |
if (!ssl->app_output_closed && ssl->out_count < ssl->out_size) { |
| 1176 |
ssize_t app_bytes = transport->io_layers[layer+1]->process_output(transport, layer+1, &ssl->outbuf[ssl->out_count], ssl->out_size - ssl->out_count); |
| 1177 |
if (app_bytes > 0) { |
| 1178 |
ssl->out_count += app_bytes; |
| 1179 |
work_pending = true; |
| 1180 |
ssl_log(transport, PN_LEVEL_TRACE, "Gathered %" PN_ZI " bytes from app to send to peer", app_bytes); |
| 1181 |
} else { |
| 1182 |
if (app_bytes < 0) { |
| 1183 |
ssl_log(transport, PN_LEVEL_TRACE, "Application layer closed its output, error=%d (%d bytes pending send)", |
| 1184 |
(int) app_bytes, (int) ssl->out_count); |
| 1185 |
ssl->app_output_closed = app_bytes; |
| 1186 |
} |
| 1187 |
} |
| 1188 |
} |
| 1189 |
|
| 1190 |
// now push any pending app data into the socket |
| 1191 |
|
| 1192 |
if (!ssl->ssl_closed) { |
| 1193 |
char *data = ssl->outbuf; |
| 1194 |
if (ssl->out_count > 0) { |
| 1195 |
int wrote = BIO_write( ssl->bio_ssl, data, ssl->out_count ); |
| 1196 |
if (wrote > 0) { |
| 1197 |
data += wrote; |
| 1198 |
ssl->out_count -= wrote; |
| 1199 |
work_pending = true; |
| 1200 |
ssl_log( transport, PN_LEVEL_TRACE, "Wrote %d bytes from app to socket", wrote ); |
| 1201 |
} else { |
| 1202 |
if (!BIO_should_retry(ssl->bio_ssl)) { |
| 1203 |
int reason = SSL_get_error( ssl->ssl, wrote ); |
| 1204 |
switch (reason) { |
| 1205 |
case SSL_ERROR_ZERO_RETURN: |
| 1206 |
// SSL closed cleanly |
| 1207 |
ssl_log(transport, PN_LEVEL_TRACE, "SSL connection has closed"); |
| 1208 |
start_ssl_shutdown(transport); // KAG: not sure - this may not be necessary |
| 1209 |
ssl->out_count = 0; // can no longer write to socket, so erase app output data |
| 1210 |
ssl->ssl_closed = true; |
| 1211 |
break; |
| 1212 |
default: |
| 1213 |
// unexpected error |
| 1214 |
return (ssize_t)ssl_failed(transport); |
| 1215 |
} |
| 1216 |
} else { |
| 1217 |
if (BIO_should_read( ssl->bio_ssl )) { |
| 1218 |
ssl->read_blocked = true; |
| 1219 |
ssl_log(transport, PN_LEVEL_TRACE, "Detected read-blocked"); |
| 1220 |
} |
| 1221 |
if (BIO_should_write( ssl->bio_ssl )) { |
| 1222 |
ssl->write_blocked = true; |
| 1223 |
ssl_log(transport, PN_LEVEL_TRACE, "Detected write-blocked"); |
| 1224 |
} |
| 1225 |
} |
| 1226 |
} |
| 1227 |
} |
| 1228 |
|
| 1229 |
if (ssl->out_count == 0) { |
| 1230 |
if (ssl->app_input_closed && ssl->app_output_closed) { |
| 1231 |
// application is done sending/receiving data, and all buffered output data has |
| 1232 |
// been written to the SSL socket |
| 1233 |
start_ssl_shutdown(transport); |
| 1234 |
} |
| 1235 |
} else if (data != ssl->outbuf) { |
| 1236 |
memmove( ssl->outbuf, data, ssl->out_count ); |
| 1237 |
} |
| 1238 |
} |
| 1239 |
|
| 1240 |
// read from the network bio as much as possible, filling the buffer |
| 1241 |
if (max_len) { |
| 1242 |
int available = BIO_read( ssl->bio_net_io, buffer, max_len ); |
| 1243 |
if (available > 0) { |
| 1244 |
max_len -= available; |
| 1245 |
buffer += available; |
| 1246 |
written += available; |
| 1247 |
ssl->write_blocked = false; |
| 1248 |
work_pending = work_pending || max_len > 0; |
| 1249 |
ssl_log(transport, PN_LEVEL_TRACE, "Read %d bytes from BIO Layer", available ); |
| 1250 |
} |
| 1251 |
} |
| 1252 |
|
| 1253 |
} while (work_pending); |
| 1254 |
|
| 1255 |
//_log(ssl, "written=%d ssl_closed=%d in_count=%d app_input_closed=%d app_output_closed=%d bio_pend=%d", |
| 1256 |
// written, ssl->ssl_closed, ssl->in_count, ssl->app_input_closed, ssl->app_output_closed, BIO_pending(ssl->bio_net_io) ); |
| 1257 |
|
| 1258 |
// PROTON-82: close the output side as soon as we've sent the SSL close_notify. |
| 1259 |
// We're not requiring the response, as some implementations never reply. |
| 1260 |
// ---- |
| 1261 |
// Once no more data is available "below" the SSL socket, tell the transport we are |
| 1262 |
// done. |
| 1263 |
//if (written == 0 && ssl->ssl_closed && BIO_pending(ssl->bio_net_io) == 0) { |
| 1264 |
// written = ssl->app_output_closed ? ssl->app_output_closed : PN_EOS; |
| 1265 |
//} |
| 1266 |
if (written == 0 && (SSL_get_shutdown(ssl->ssl) & SSL_SENT_SHUTDOWN) && BIO_pending(ssl->bio_net_io) == 0) { |
| 1267 |
written = ssl->app_output_closed ? ssl->app_output_closed : PN_EOS; |
| 1268 |
if (transport->io_layers[layer]==&ssl_input_closed_layer) { |
| 1269 |
transport->io_layers[layer] = &ssl_closed_layer; |
| 1270 |
} else { |
| 1271 |
transport->io_layers[layer] = &ssl_output_closed_layer; |
| 1272 |
} |
| 1273 |
} |
| 1274 |
ssl_log(transport, PN_LEVEL_TRACE, "process_output_ssl() returning %d", (int) written); |
| 1275 |
return written; |
| 1276 |
} |
| 1277 |
|
| 1278 |
static int init_ssl_socket(pn_transport_t* transport, pni_ssl_t *ssl, pn_ssl_domain_t *domain) |
| 1279 |
{ |
| 1280 |
if (ssl->ssl) return 0; |
| 1281 |
if (!domain) return -1; |
| 1282 |
|
| 1283 |
ssl->ssl = SSL_new(domain->ctx); |
| 1284 |
if (!ssl->ssl) { |
| 1285 |
ssl_log(transport, PN_LEVEL_ERROR, "SSL socket setup failure." ); |
| 1286 |
ssl_log_flush(transport, PN_LEVEL_ERROR); |
| 1287 |
return -1; |
| 1288 |
} |
| 1289 |
|
| 1290 |
// store backpointer to pn_transport_t in SSL object: |
| 1291 |
SSL_set_ex_data(ssl->ssl, ssl_ex_data_index, transport); |
| 1292 |
|
| 1293 |
#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1294 |
if (ssl->peer_hostname && ssl->mode == PN_SSL_MODE_CLIENT) { |
| 1295 |
SSL_set_tlsext_host_name(ssl->ssl, ssl->peer_hostname); |
| 1296 |
} |
| 1297 |
#endif |
| 1298 |
|
| 1299 |
// restore session, if available |
| 1300 |
ssn_restore(transport, ssl); |
| 1301 |
|
| 1302 |
// now layer a BIO over the SSL socket |
| 1303 |
ssl->bio_ssl = BIO_new(BIO_f_ssl()); |
| 1304 |
if (!ssl->bio_ssl) { |
| 1305 |
ssl_log(transport, PN_LEVEL_ERROR, "BIO setup failure." ); |
| 1306 |
return -1; |
| 1307 |
} |
| 1308 |
(void)BIO_set_ssl(ssl->bio_ssl, ssl->ssl, BIO_NOCLOSE); |
| 1309 |
|
| 1310 |
// create the "lower" BIO "pipe", and attach it below the SSL layer |
| 1311 |
if (!BIO_new_bio_pair(&ssl->bio_ssl_io, 0, &ssl->bio_net_io, 0)) { |
| 1312 |
ssl_log(transport, PN_LEVEL_ERROR, "BIO setup failure." ); |
| 1313 |
return -1; |
| 1314 |
} |
| 1315 |
SSL_set_bio(ssl->ssl, ssl->bio_ssl_io, ssl->bio_ssl_io); |
| 1316 |
|
| 1317 |
if (ssl->mode == PN_SSL_MODE_SERVER) { |
| 1318 |
SSL_set_accept_state(ssl->ssl); |
| 1319 |
BIO_set_ssl_mode(ssl->bio_ssl, 0); // server mode |
| 1320 |
ssl_log( transport, PN_LEVEL_TRACE, "Server SSL socket created." ); |
| 1321 |
} else { // client mode |
| 1322 |
SSL_set_connect_state(ssl->ssl); |
| 1323 |
BIO_set_ssl_mode(ssl->bio_ssl, 1); // client mode |
| 1324 |
ssl_log( transport, PN_LEVEL_TRACE, "Client SSL socket created." ); |
| 1325 |
} |
| 1326 |
ssl->subject = NULL; |
| 1327 |
ssl->peer_certificate = NULL; |
| 1328 |
return 0; |
| 1329 |
} |
| 1330 |
|
| 1331 |
static void release_ssl_socket(pni_ssl_t *ssl) |
| 1332 |
{ |
| 1333 |
if (ssl->bio_ssl) BIO_free(ssl->bio_ssl); |
| 1334 |
if (ssl->ssl) { |
| 1335 |
SSL_free(ssl->ssl); // will free bio_ssl_io |
| 1336 |
} else { |
| 1337 |
if (ssl->bio_ssl_io) BIO_free(ssl->bio_ssl_io); |
| 1338 |
} |
| 1339 |
if (ssl->bio_net_io) BIO_free(ssl->bio_net_io); |
| 1340 |
ssl->bio_ssl = NULL; |
| 1341 |
ssl->bio_ssl_io = NULL; |
| 1342 |
ssl->bio_net_io = NULL; |
| 1343 |
ssl->ssl = NULL; |
| 1344 |
} |
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
pn_ssl_resume_status_t pn_ssl_resume_status(pn_ssl_t *ssl0) |
| 1349 |
{ |
| 1350 |
pni_ssl_t *ssl = get_ssl_internal(ssl0); |
| 1351 |
if (!ssl || !ssl->ssl) return PN_SSL_RESUME_UNKNOWN; |
| 1352 |
switch (SSL_session_reused( ssl->ssl )) { |
| 1353 |
case 0: return PN_SSL_RESUME_NEW; |
| 1354 |
case 1: return PN_SSL_RESUME_REUSED; |
| 1355 |
default: break; |
| 1356 |
} |
| 1357 |
return PN_SSL_RESUME_UNKNOWN; |
| 1358 |
} |
| 1359 |
|
| 1360 |
|
| 1361 |
int pn_ssl_set_peer_hostname(pn_ssl_t *ssl0, const char *hostname) |
| 1362 |
{ |
| 1363 |
pni_ssl_t *ssl = get_ssl_internal(ssl0); |
| 1364 |
if (!ssl) return -1; |
| 1365 |
|
| 1366 |
if (ssl->peer_hostname) free((void *)ssl->peer_hostname); |
| 1367 |
ssl->peer_hostname = NULL; |
| 1368 |
if (hostname) { |
| 1369 |
ssl->peer_hostname = pn_strdup(hostname); |
| 1370 |
if (!ssl->peer_hostname) return -2; |
| 1371 |
#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1372 |
if (ssl->ssl && ssl->mode == PN_SSL_MODE_CLIENT) { |
| 1373 |
SSL_set_tlsext_host_name(ssl->ssl, ssl->peer_hostname); |
| 1374 |
} |
| 1375 |
#endif |
| 1376 |
} |
| 1377 |
return 0; |
| 1378 |
} |
| 1379 |
|
| 1380 |
int pn_ssl_get_peer_hostname(pn_ssl_t *ssl0, char *hostname, size_t *bufsize) |
| 1381 |
{ |
| 1382 |
pni_ssl_t *ssl = get_ssl_internal(ssl0); |
| 1383 |
if (!ssl) return -1; |
| 1384 |
if (!ssl->peer_hostname) { |
| 1385 |
*bufsize = 0; |
| 1386 |
if (hostname) *hostname = '\0'; |
| 1387 |
return 0; |
| 1388 |
} |
| 1389 |
unsigned len = strlen(ssl->peer_hostname); |
| 1390 |
if (hostname) { |
| 1391 |
if (len >= *bufsize) return -1; |
| 1392 |
strcpy( hostname, ssl->peer_hostname ); |
| 1393 |
} |
| 1394 |
*bufsize = len; |
| 1395 |
return 0; |
| 1396 |
} |
| 1397 |
|
| 1398 |
static X509 *get_peer_certificate(pni_ssl_t *ssl) |
| 1399 |
{ |
| 1400 |
// Cache for multiple use and final X509_free |
| 1401 |
if (!ssl->peer_certificate && ssl->ssl) { |
| 1402 |
ssl->peer_certificate = SSL_get_peer_certificate(ssl->ssl); |
| 1403 |
// May still be NULL depending on timing or type of SSL connection |
| 1404 |
} |
| 1405 |
return ssl->peer_certificate; |
| 1406 |
} |
| 1407 |
|
| 1408 |
const char* pn_ssl_get_remote_subject(pn_ssl_t *ssl0) |
| 1409 |
{ |
| 1410 |
pni_ssl_t *ssl = get_ssl_internal(ssl0); |
| 1411 |
if (!ssl || !ssl->ssl) return NULL; |
| 1412 |
if (!ssl->subject) { |
| 1413 |
X509 *cert = get_peer_certificate(ssl); |
| 1414 |
if (!cert) return NULL; |
| •1415 |
const X509_NAME *subject = X509_get_subject_name(cert); |
| 1416 |
if (!subject) return NULL; |
| 1417 |
|
| 1418 |
BIO *out = BIO_new(BIO_s_mem()); |
| 1419 |
X509_NAME_print_ex(out, subject, 0, XN_FLAG_RFC2253); |
| 1420 |
int len = BIO_number_written(out); |
| 1421 |
ssl->subject = (char*) malloc(len+1); |
| 1422 |
ssl->subject[len] = 0; |
| 1423 |
BIO_read(out, ssl->subject, len); |
| 1424 |
BIO_free(out); |
| 1425 |
} |
| 1426 |
return ssl->subject; |
| 1427 |
} |
| 1428 |
|
| 1429 |
int pn_ssl_get_cert_fingerprint(pn_ssl_t *ssl0, char *fingerprint, size_t fingerprint_length, pn_ssl_hash_alg hash_alg) |
| 1430 |
{ |
| 1431 |
const char *digest_name = NULL; |
| 1432 |
size_t min_required_length; |
| 1433 |
|
| 1434 |
// old versions of python expect fingerprint to contain a valid string on |
| 1435 |
// return from this function |
| 1436 |
fingerprint[0] = 0; |
| 1437 |
|
| 1438 |
// Assign the correct digest_name value based on the enum values. |
| 1439 |
switch (hash_alg) { |
| 1440 |
case PN_SSL_SHA1 : |
| 1441 |
min_required_length = 41; // 40 hex characters + 1 '\0' character |
| 1442 |
digest_name = "sha1"; |
| 1443 |
break; |
| 1444 |
case PN_SSL_SHA256 : |
| 1445 |
min_required_length = 65; // 64 hex characters + 1 '\0' character |
| 1446 |
digest_name = "sha256"; |
| 1447 |
break; |
| 1448 |
case PN_SSL_SHA512 : |
| 1449 |
min_required_length = 129; // 128 hex characters + 1 '\0' character |
| 1450 |
digest_name = "sha512"; |
| 1451 |
break; |
| 1452 |
case PN_SSL_MD5 : |
| 1453 |
min_required_length = 33; // 32 hex characters + 1 '\0' character |
| 1454 |
digest_name = "md5"; |
| 1455 |
break; |
| 1456 |
default: |
| 1457 |
ssl_log_error("Unknown or unhandled hash algorithm %i ", hash_alg); |
| 1458 |
return PN_ERR; |
| 1459 |
|
| 1460 |
} |
| 1461 |
|
| 1462 |
if(fingerprint_length < min_required_length) { |
| 1463 |
ssl_log_error("Insufficient fingerprint_length %" PN_ZU ". fingerprint_length must be %" PN_ZU " or above for %s digest", |
| 1464 |
fingerprint_length, min_required_length, digest_name); |
| 1465 |
return PN_ERR; |
| 1466 |
} |
| 1467 |
|
| 1468 |
const EVP_MD *digest = EVP_get_digestbyname(digest_name); |
| 1469 |
|
| 1470 |
pni_ssl_t *ssl = get_ssl_internal(ssl0); |
| 1471 |
X509 *cert = get_peer_certificate(ssl); |
| 1472 |
|
| 1473 |
if(cert) { |
| 1474 |
unsigned int len; |
| 1475 |
|
| 1476 |
unsigned char bytes[64]; // sha512 uses 64 octets, we will use that as the maximum. |
| 1477 |
|
| 1478 |
if (X509_digest(cert, digest, bytes, &len) != 1) { |
| 1479 |
ssl_log_error("Failed to extract X509 digest"); |
| 1480 |
return PN_ERR; |
| 1481 |
} |
| 1482 |
|
| 1483 |
char *cursor = fingerprint; |
| 1484 |
|
| 1485 |
for (size_t i=0; i<len ; i++) { |
| 1486 |
cursor += snprintf((char *)cursor, fingerprint_length, "%02x", bytes[i]); |
| 1487 |
fingerprint_length = fingerprint_length - 2; |
| 1488 |
} |
| 1489 |
|
| 1490 |
return PN_OK; |
| 1491 |
} |
| 1492 |
else { |
| 1493 |
ssl_log_error("No certificate is available yet "); |
| 1494 |
return PN_ERR; |
| 1495 |
} |
| 1496 |
|
| 1497 |
return 0; |
| 1498 |
} |
| 1499 |
|
| 1500 |
|
| 1501 |
const char* pn_ssl_get_remote_subject_subfield(pn_ssl_t *ssl0, pn_ssl_cert_subject_subfield field) |
| 1502 |
{ |
| 1503 |
int openssl_field = 0; |
| 1504 |
|
| 1505 |
// Assign openssl internal representations of field values to openssl_field |
| 1506 |
switch (field) { |
| 1507 |
case PN_SSL_CERT_SUBJECT_COUNTRY_NAME : |
| 1508 |
openssl_field = NID_countryName; |
| 1509 |
break; |
| 1510 |
case PN_SSL_CERT_SUBJECT_STATE_OR_PROVINCE : |
| 1511 |
openssl_field = NID_stateOrProvinceName; |
| 1512 |
break; |
| 1513 |
case PN_SSL_CERT_SUBJECT_CITY_OR_LOCALITY : |
| 1514 |
openssl_field = NID_localityName; |
| 1515 |
break; |
| 1516 |
case PN_SSL_CERT_SUBJECT_ORGANIZATION_NAME : |
| 1517 |
openssl_field = NID_organizationName; |
| 1518 |
break; |
| 1519 |
case PN_SSL_CERT_SUBJECT_ORGANIZATION_UNIT : |
| 1520 |
openssl_field = NID_organizationalUnitName; |
| 1521 |
break; |
| 1522 |
case PN_SSL_CERT_SUBJECT_COMMON_NAME : |
| 1523 |
openssl_field = NID_commonName; |
| 1524 |
break; |
| 1525 |
default: |
| 1526 |
ssl_log_error("Unknown or unhandled certificate subject subfield %i", field); |
| 1527 |
return NULL; |
| 1528 |
} |
| 1529 |
|
| 1530 |
pni_ssl_t *ssl = get_ssl_internal(ssl0); |
| 1531 |
X509 *cert = get_peer_certificate(ssl); |
| 1532 |
if (!cert) return NULL; |
| 1533 |
|
| •1534 |
const X509_NAME *subject_name = X509_get_subject_name(cert); |
| 1535 |
|
| 1536 |
// TODO (gmurthy) - A server side cert subject field can have more than one common name like this - Subject: CN=www.domain1.com, CN=www.domain2.com, see https://bugzilla.mozilla.org/show_bug.cgi?id=380656 |
| 1537 |
// For now, we will only return the first common name if there is more than one common name in the cert |
| 1538 |
int index = X509_NAME_get_index_by_NID(subject_name, openssl_field, -1); |
| 1539 |
|
| 1540 |
if (index > -1) { |
| •1541 |
const X509_NAME_ENTRY *ne = X509_NAME_get_entry(subject_name, index); |
| 1542 |
if(ne) { |
| •1543 |
const ASN1_STRING *name_asn1 = X509_NAME_ENTRY_get_data(ne); |
| 1544 |
return (const char *)ASN1_STRING_get0_data(name_asn1); |
| 1545 |
} |
| 1546 |
} |
| 1547 |
|
| 1548 |
return NULL; |
| 1549 |
} |
| 1550 |
|
| 1551 |
static ssize_t process_input_done(pn_transport_t *transport, unsigned int layer, const char *input_data, size_t len) |
| 1552 |
{ |
| 1553 |
return PN_EOS; |
| 1554 |
} |
| 1555 |
static ssize_t process_output_done(pn_transport_t *transport, unsigned int layer, char *input_data, size_t len) |
| 1556 |
{ |
| 1557 |
return PN_EOS; |
| 1558 |
} |
| 1559 |
|
| 1560 |
// return # output bytes sitting in this layer |
| 1561 |
static size_t buffered_output(pn_transport_t *transport) |
| 1562 |
{ |
| 1563 |
size_t count = 0; |
| 1564 |
pni_ssl_t *ssl = transport->ssl; |
| 1565 |
if (ssl) { |
| 1566 |
count += ssl->out_count; |
| 1567 |
if (ssl->bio_net_io) { // pick up any bytes waiting for network io |
| 1568 |
count += BIO_ctrl_pending(ssl->bio_net_io); |
| 1569 |
} |
| 1570 |
} |
| 1571 |
return count; |
| 1572 |
} |
| 1573 |
|
| 1574 |
|
| 1575 |
/* Thread-safe locking and initialization for POSIX and Windows */ |
| 1576 |
|
| 1577 |
static bool init_ok = false; |
| 1578 |
|
| 1579 |
#ifdef _WIN32 |
| 1580 |
|
| 1581 |
typedef CRITICAL_SECTION pni_mutex_t; |
| 1582 |
static inline void pni_mutex_init(pni_mutex_t *m) { InitializeCriticalSection(m); } |
| 1583 |
static inline void pni_mutex_lock(pni_mutex_t *m) { EnterCriticalSection(m); } |
| 1584 |
static inline void pni_mutex_unlock(pni_mutex_t *m) { LeaveCriticalSection(m); } |
| 1585 |
static inline unsigned long id_callback(void) { return (unsigned long)GetCurrentThreadId(); } |
| 1586 |
INIT_ONCE initialize_once = INIT_ONCE_STATIC_INIT; |
| 1587 |
static inline bool ensure_initialized(void) { |
| 1588 |
void* dummy; |
| 1589 |
InitOnceExecuteOnce(&initialize_once, &initialize, NULL, &dummy); |
| 1590 |
return init_ok; |
| 1591 |
} |
| 1592 |
|
| 1593 |
#else /* POSIX */ |
| 1594 |
|
| 1595 |
#include <pthread.h> |
| 1596 |
|
| 1597 |
static void initialize(void); |
| 1598 |
|
| 1599 |
typedef pthread_mutex_t pni_mutex_t; |
| 1600 |
static inline int pni_mutex_init(pni_mutex_t *m) { return pthread_mutex_init(m, NULL); } |
| 1601 |
static inline int pni_mutex_lock(pni_mutex_t *m) { return pthread_mutex_lock(m); } |
| 1602 |
static inline int pni_mutex_unlock(pni_mutex_t *m) { return pthread_mutex_unlock(m); } |
| 1603 |
static inline unsigned long id_callback(void) { return (unsigned long)pthread_self(); } |
| 1604 |
static pthread_once_t initialize_once = PTHREAD_ONCE_INIT; |
| 1605 |
static inline bool ensure_initialized(void) { |
| 1606 |
pthread_once(&initialize_once, &initialize); |
| 1607 |
return init_ok; |
| 1608 |
} |
| 1609 |
|
| 1610 |
#endif |
| 1611 |
|
| 1612 |
static pni_mutex_t *locks = NULL; /* Lock array for openssl */ |
| 1613 |
|
| 1614 |
/* Callback function for openssl locking */ |
| 1615 |
static void locking_callback(int mode, int n, const char *file, int line) { |
| 1616 |
if(mode & CRYPTO_LOCK) |
| 1617 |
pni_mutex_lock(&locks[n]); |
| 1618 |
else |
| 1619 |
pni_mutex_unlock(&locks[n]); |
| 1620 |
} |
| 1621 |
|
| 1622 |
static void initialize(void) { |
| 1623 |
int i; |
| 1624 |
SSL_library_init(); |
| 1625 |
SSL_load_error_strings(); |
| 1626 |
OpenSSL_add_all_algorithms(); |
| 1627 |
ssl_ex_data_index = SSL_get_ex_new_index( 0, (void *) "org.apache.qpid.proton.ssl", |
| 1628 |
NULL, NULL, NULL); |
| 1629 |
ssn_init(); |
| 1630 |
locks = (pni_mutex_t*)malloc(CRYPTO_num_locks() * sizeof(pni_mutex_t)); |
| 1631 |
if (!locks) return; |
| 1632 |
for(i = 0; i < CRYPTO_num_locks(); i++) |
| 1633 |
pni_mutex_init(&locks[i]); |
| 1634 |
CRYPTO_set_id_callback(&id_callback); |
| 1635 |
CRYPTO_set_locking_callback(&locking_callback); |
| 1636 |
/* In recent versions of openssl, the set_callback functions are no-op macros, |
| 1637 |
so we need to take steps to stop the compiler complaining about unused functions. */ |
| 1638 |
(void)&id_callback; |
| 1639 |
(void)&locking_callback; |
| 1640 |
init_ok = true; |
| 1641 |
} |
| 1642 |
|
| 1643 |
/* TODO aconway 2017-10-16: There is no opportunity to clean up the locks as proton has no |
| 1644 |
final shut-down call. If it did, we should call this: */ |
| 1645 |
/* |
| 1646 |
static void shutdown(void) { |
| 1647 |
CRYPTO_set_id_callback(NULL); |
| 1648 |
CRYPTO_set_locking_callback(NULL); |
| 1649 |
if(locks) { |
| 1650 |
int i; |
| 1651 |
for(i = 0; i < CRYPTO_num_locks(); i++) |
| 1652 |
pni_mutex_destroy(&locks[i]); |
| 1653 |
free(locks); |
| 1654 |
locks = NULL; |
| 1655 |
} |
| 1656 |
} |
| 1657 |
*/ |