| 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 |
/* Enable POSIX features beyond c99 for modern pthread and standard strerror_r() */ |
| 23 |
#ifndef _POSIX_C_SOURCE |
| 24 |
#define _POSIX_C_SOURCE 200809L |
| 25 |
#endif |
| 26 |
/* Avoid GNU extensions, in particular the incompatible alternative strerror_r() */ |
| 27 |
#undef _GNU_SOURCE |
| 28 |
|
| 29 |
|
| 30 |
#include "platform/platform.h" |
| 31 |
#include "platform/platform_fmt.h" |
| 32 |
#include "core/util.h" |
| 33 |
#include <proton/error.h> |
| 34 |
#include <proton/tls.h> |
| 35 |
|
| 36 |
// openssl on windows expects the user to have already included |
| 37 |
// winsock.h |
| 38 |
|
| 39 |
#ifdef _MSC_VER |
| 40 |
#ifndef _WIN32_WINNT |
| 41 |
#define _WIN32_WINNT 0x0501 |
| 42 |
#endif |
| 43 |
#if _WIN32_WINNT < 0x0501 |
| 44 |
#error "Proton requires Windows API support for XP or later." |
| 45 |
#endif |
| 46 |
#include <winsock2.h> |
| 47 |
#include <mswsock.h> |
| 48 |
#include <Ws2tcpip.h> |
| 49 |
#endif |
| 50 |
|
| 51 |
|
| 52 |
#include <openssl/ssl.h> |
| 53 |
#include <openssl/dh.h> |
| 54 |
#include <openssl/err.h> |
| 55 |
#include <openssl/x509v3.h> |
| 56 |
#include <sys/types.h> |
| 57 |
#include <sys/stat.h> |
| 58 |
#include <fcntl.h> |
| 59 |
#include <assert.h> |
| 60 |
#include <stdio.h> |
| 61 |
#include <ctype.h> |
| 62 |
#include <string.h> |
| 63 |
#include <unistd.h> |
| 64 |
|
| 65 |
/** @file |
| 66 |
* Standalone raw buffer based SSL/TLS support API. |
| 67 |
* |
| 68 |
* This file is heavily based on the original Proton SSL layer for |
| 69 |
* AMQP connections and the buffer management logic in |
| 70 |
* raw_connection.c. |
| 71 |
*/ |
| 72 |
|
| 73 |
enum { |
| 74 |
default_eresult_buffer_count = 4, |
| 75 |
default_dresult_buffer_count = 4, |
| 76 |
default_decrypt_buffer_count = 4, |
| 77 |
default_encrypt_buffer_count = 4 |
| 78 |
}; |
| 79 |
|
| 80 |
typedef enum { |
| 81 |
buff_empty = 0, |
| 82 |
|
| 83 |
buff_decrypt_pending = 1, // application buffers for decryption |
| 84 |
buff_decrypt_done = 2, |
| 85 |
|
| 86 |
buff_encrypt_pending = 3, // application buffers for encryption |
| 87 |
buff_encrypt_done = 4, |
| 88 |
|
| 89 |
buff_eresult_blank = 5, |
| 90 |
buff_eresult_encrypted = 6, |
| 91 |
|
| 92 |
buff_dresult_blank = 7, |
| 93 |
buff_dresult_decrypted = 8 |
| 94 |
} buff_type; |
| 95 |
|
| 96 |
|
| 97 |
typedef uint16_t buff_ptr; // This is always the index+1 so that 0 can be special |
| 98 |
|
| 99 |
typedef struct pbuffer_t { |
| 100 |
uintptr_t context; |
| 101 |
char *bytes; |
| 102 |
uint32_t capacity; |
| 103 |
uint32_t size; |
| 104 |
uint32_t offset; |
| 105 |
buff_ptr next; |
| 106 |
uint8_t type; // For debugging |
| 107 |
} pbuffer_t; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
typedef struct pn_tls_session_t pn_tls_session_t; |
| 112 |
|
| 113 |
static int tls_ex_data_index; |
| 114 |
|
| 115 |
struct pn_tls_config_t { |
| 116 |
|
| 117 |
SSL_CTX *ctx; |
| 118 |
|
| 119 |
char *keyfile_pw; |
| 120 |
|
| 121 |
// settings used for all connections |
| 122 |
char *trusted_CAs; |
| 123 |
|
| 124 |
char *ciphers; |
| 125 |
|
| 126 |
int ref_count; |
| 127 |
#ifdef SSL_SECOP_PEER |
| 128 |
int default_seclevel; |
| 129 |
#endif |
| 130 |
pn_tls_mode_t mode; |
| 131 |
pn_tls_verify_mode_t verify_mode; |
| 132 |
|
| 133 |
bool has_certificate; // true when certificate configured |
| 134 |
unsigned char *alpn_list; |
| 135 |
unsigned int alpn_list_len; |
| 136 |
}; |
| 137 |
|
| 138 |
struct pn_tls_t { |
| 139 |
pbuffer_t *eresult_buffers; |
| 140 |
pbuffer_t *dresult_buffers; |
| 141 |
pbuffer_t *encrypt_buffers; |
| 142 |
pbuffer_t *decrypt_buffers; |
| 143 |
size_t eresult_buffer_count; |
| 144 |
size_t dresult_buffer_count; |
| 145 |
size_t encrypt_buffer_count; |
| 146 |
size_t decrypt_buffer_count; |
| 147 |
|
| 148 |
uint16_t encrypt_buffer_empty_count; |
| 149 |
uint16_t decrypt_buffer_empty_count; |
| 150 |
uint16_t eresult_encrypted_count; |
| 151 |
uint16_t eresult_empty_count; |
| 152 |
uint16_t dresult_decrypted_count; |
| 153 |
uint16_t dresult_empty_count; |
| 154 |
|
| 155 |
// Lists within encrypt_buffers |
| 156 |
buff_ptr encrypt_first_empty; // unordered: no encryp_last_empty |
| 157 |
buff_ptr encrypt_first_pending; |
| 158 |
buff_ptr encrypt_last_pending; |
| 159 |
buff_ptr encrypt_first_done; |
| 160 |
buff_ptr encrypt_last_done; |
| 161 |
|
| 162 |
// Lists within decrypt_buffers |
| 163 |
buff_ptr decrypt_first_empty; // unordered. |
| 164 |
buff_ptr decrypt_first_pending; |
| 165 |
buff_ptr decrypt_last_pending; |
| 166 |
buff_ptr decrypt_first_done; |
| 167 |
buff_ptr decrypt_last_done; |
| 168 |
|
| 169 |
// Lists within eresult_buffers |
| 170 |
buff_ptr eresult_first_empty; // unordered. |
| 171 |
buff_ptr eresult_first_blank; // unordered. |
| 172 |
buff_ptr eresult_first_encrypted; |
| 173 |
buff_ptr eresult_last_encrypted; |
| 174 |
|
| 175 |
// Lists within dresult_buffers |
| 176 |
buff_ptr dresult_first_empty; // unordered. |
| 177 |
buff_ptr dresult_first_blank; // unordered. |
| 178 |
buff_ptr dresult_first_decrypted; |
| 179 |
buff_ptr dresult_last_decrypted; |
| 180 |
|
| 181 |
uint32_t encrypt_pending_offset; // if set, points to remaining bytes to consume in working buffer. |
| 182 |
uint32_t decrypt_pending_offset; |
| 183 |
|
| 184 |
pn_tls_mode_t mode; |
| 185 |
pn_tls_verify_mode_t verify_mode; |
| 186 |
pn_tls_config_t *domain; |
| 187 |
const char *session_id; |
| 188 |
const char *peer_hostname; |
| 189 |
SSL *ssl; |
| 190 |
|
| 191 |
BIO *bio_ssl; // i/o from/to SSL socket layer |
| 192 |
BIO *bio_ssl_io; // SSL "half" of network-facing BIO |
| 193 |
BIO *bio_net_io; // socket-side "half" of network-facing BIO |
| 194 |
// buffers for holding unprocessed bytes to be en/decoded when BIO is able to process them. |
| 195 |
|
| 196 |
int pn_tls_err; // TLS errors are fatal to the session, including session tickets/psks |
| 197 |
int openssl_err_type; // From SSL_get_error() |
| 198 |
unsigned long openssl_err; // From ERR_get_error() |
| 199 |
int os_errno; // In case the OS can supply error context not available from OpenSSL |
| 200 |
|
| 201 |
bool ssl_shutdown; // BIO_ssl_shutdown() called on socket. |
| 202 |
bool ssl_closed; // shutdown complete, or SSL error |
| 203 |
bool dec_closed; // Peer's TLS closure record received. Clean EOF of inbound decrypted data. |
| 204 |
bool enc_closed; // Self TLS closure record sent or pending flush. |
| 205 |
bool read_blocked; // SSL blocked until more network data is read |
| 206 |
bool write_blocked; // SSL blocked until data is written to network |
| 207 |
bool enc_rblocked; |
| 208 |
bool enc_wblocked; |
| 209 |
bool dec_rblocked; |
| 210 |
bool dec_wblocked; |
| 211 |
bool dec_stale; |
| 212 |
bool handshake_ok; |
| 213 |
bool can_shutdown; |
| 214 |
bool started; |
| 215 |
bool stopped; |
| 216 |
bool validating; |
| 217 |
const char *validate_error; |
| 218 |
|
| 219 |
char *subject; |
| 220 |
X509 *peer_certificate; |
| 221 |
unsigned char *alpn_list; |
| 222 |
unsigned int alpn_list_len; |
| 223 |
}; |
| 224 |
|
| 225 |
static void encrypt(pn_tls_t *); |
| 226 |
static void decrypt(pn_tls_t *); |
| 227 |
static int pn_tls_alpn_cb(SSL *ssn, const unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg); |
| 228 |
|
| 229 |
|
| 230 |
static void tls_initialize_buffers(pn_tls_t *tls) { |
| 231 |
// Link together free lists |
| 232 |
for (buff_ptr i = 1; i<=tls->encrypt_buffer_count; i++) { |
| 233 |
tls->encrypt_buffers[i-1].next = i==tls->encrypt_buffer_count ? 0 : i+1; |
| 234 |
tls->encrypt_buffers[i-1].type = buff_empty; |
| 235 |
} |
| 236 |
for (buff_ptr i = 1; i<=tls->decrypt_buffer_count; i++) { |
| 237 |
tls->decrypt_buffers[i-1].next = i==tls->decrypt_buffer_count ? 0 : i+1; |
| 238 |
tls->decrypt_buffers[i-1].type = buff_empty; |
| 239 |
} |
| 240 |
for (buff_ptr i = 1; i<=tls->eresult_buffer_count; i++) { |
| 241 |
tls->eresult_buffers[i-1].next = i==tls->eresult_buffer_count ? 0 : i+1; |
| 242 |
tls->eresult_buffers[i-1].type = buff_empty; |
| 243 |
} |
| 244 |
for (buff_ptr i = 1; i<=tls->dresult_buffer_count; i++) { |
| 245 |
tls->dresult_buffers[i-1].next = i==tls->dresult_buffer_count ? 0 : i+1; |
| 246 |
tls->dresult_buffers[i-1].type = buff_empty; |
| 247 |
} |
| 248 |
|
| 249 |
tls->encrypt_buffer_empty_count = tls->encrypt_buffer_count; |
| 250 |
tls->decrypt_buffer_empty_count = tls->decrypt_buffer_count; |
| 251 |
tls->eresult_empty_count = tls->eresult_buffer_count; |
| 252 |
tls->dresult_empty_count = tls->dresult_buffer_count; |
| 253 |
|
| 254 |
tls->eresult_first_empty = 1; |
| 255 |
tls->dresult_first_empty = 1; |
| 256 |
tls->encrypt_first_empty = 1; |
| 257 |
tls->decrypt_first_empty = 1; |
| 258 |
} |
| 259 |
|
| 260 |
bool pni_tls_validate(pn_tls_t *tls) { |
| 261 |
// encrypt input buffers: empty + 2 ordered sublists |
| 262 |
tls->validate_error = "encrypt empty list"; |
| 263 |
size_t e_empty_count = 0; |
| 264 |
for (buff_ptr i = tls->encrypt_first_empty; i; i = tls->encrypt_buffers[i-1].next) { |
| 265 |
if (tls->encrypt_buffers[i-1].type != buff_empty) return false; |
| 266 |
e_empty_count++; |
| 267 |
} |
| 268 |
if (e_empty_count != tls->encrypt_buffer_empty_count) return false; |
| 269 |
tls->validate_error = "encrypt pending list"; |
| 270 |
size_t e_pending_count = 0; |
| 271 |
for (buff_ptr i = tls->encrypt_first_pending; i; i = tls->encrypt_buffers[i-1].next) { |
| 272 |
if (tls->encrypt_buffers[i-1].type != buff_encrypt_pending) return false; |
| 273 |
e_pending_count++; |
| 274 |
} |
| 275 |
tls->validate_error = "encrypt done list"; |
| 276 |
size_t e_done_count = 0; |
| 277 |
for (buff_ptr i = tls->encrypt_first_done; i; i = tls->encrypt_buffers[i-1].next) { |
| 278 |
if (tls->encrypt_buffers[i-1].type != buff_encrypt_done) return false; |
| 279 |
e_done_count++; |
| 280 |
} |
| 281 |
tls->validate_error = "encrypt pointers and counts"; |
| 282 |
if (e_empty_count+e_pending_count+e_done_count != tls->encrypt_buffer_count) return false; |
| 283 |
if (!tls->encrypt_first_pending && tls->encrypt_last_pending) return false; |
| 284 |
if (tls->encrypt_last_pending && |
| 285 |
(tls->encrypt_buffers[tls->encrypt_last_pending-1].type != buff_encrypt_pending || tls->encrypt_buffers[tls->encrypt_last_pending-1].next != 0)) return false; |
| 286 |
if (!tls->encrypt_first_done && tls->encrypt_last_done) return false; |
| 287 |
if (tls->encrypt_last_done && |
| 288 |
(tls->encrypt_buffers[tls->encrypt_last_done-1].type != buff_encrypt_done || tls->encrypt_buffers[tls->encrypt_last_done-1].next != 0)) return false; |
| 289 |
|
| 290 |
// decrypt input buffers: empty + 2 ordered sublists |
| 291 |
tls->validate_error = "decrypt empty list"; |
| 292 |
size_t d_empty_count = 0; |
| 293 |
for (buff_ptr i = tls->decrypt_first_empty; i; i = tls->decrypt_buffers[i-1].next) { |
| 294 |
if (tls->decrypt_buffers[i-1].type != buff_empty) return false; |
| 295 |
d_empty_count++; |
| 296 |
} |
| 297 |
if (d_empty_count != tls->decrypt_buffer_empty_count) return false; |
| 298 |
tls->validate_error = "decrypt pending list"; |
| 299 |
size_t d_pending_count = 0; |
| 300 |
for (buff_ptr i = tls->decrypt_first_pending; i; i = tls->decrypt_buffers[i-1].next) { |
| 301 |
if (tls->decrypt_buffers[i-1].type != buff_decrypt_pending) return false; |
| 302 |
d_pending_count++; |
| 303 |
} |
| 304 |
tls->validate_error = "decrypt done list"; |
| 305 |
size_t d_done_count = 0; |
| 306 |
for (buff_ptr i = tls->decrypt_first_done; i; i = tls->decrypt_buffers[i-1].next) { |
| 307 |
if (tls->decrypt_buffers[i-1].type != buff_decrypt_done) return false; |
| 308 |
d_done_count++; |
| 309 |
} |
| 310 |
tls->validate_error = "decrypt pointers and counts"; |
| 311 |
if (d_empty_count+d_pending_count+d_done_count != tls->decrypt_buffer_count) return false; |
| 312 |
if (!tls->decrypt_first_pending && tls->decrypt_last_pending) return false; |
| 313 |
if (tls->decrypt_last_pending && |
| 314 |
(tls->decrypt_buffers[tls->decrypt_last_pending-1].type != buff_decrypt_pending || tls->decrypt_buffers[tls->decrypt_last_pending-1].next != 0)) return false; |
| 315 |
if (!tls->decrypt_first_done && tls->decrypt_last_done) return false; |
| 316 |
if (tls->decrypt_last_done && |
| 317 |
(tls->decrypt_buffers[tls->decrypt_last_done-1].type != buff_decrypt_done || tls->decrypt_buffers[tls->decrypt_last_done-1].next != 0)) return false; |
| 318 |
|
| 319 |
// encrypt result buffers: empty + unordered blank + ordered "encrypted" |
| 320 |
tls->validate_error = "eresult empty list"; |
| 321 |
size_t e_r_empty_count = 0; |
| 322 |
for (buff_ptr i = tls->eresult_first_empty; i; i = tls->eresult_buffers[i-1].next) { |
| 323 |
if (tls->eresult_buffers[i-1].type != buff_empty) return false; |
| 324 |
e_r_empty_count++; |
| 325 |
} |
| 326 |
if (e_r_empty_count != tls->eresult_empty_count) return false; |
| 327 |
tls->validate_error = "eresult blank list"; |
| 328 |
size_t e_blank_count = 0; |
| 329 |
for (buff_ptr i = tls->eresult_first_blank; i; i = tls->eresult_buffers[i-1].next) { |
| 330 |
if (tls->eresult_buffers[i-1].type != buff_eresult_blank) return false; |
| 331 |
e_blank_count++; |
| 332 |
} |
| 333 |
tls->validate_error = "eresult encrypted list"; |
| 334 |
size_t e_encrypted_count = 0; |
| 335 |
for (buff_ptr i = tls->eresult_first_encrypted; i; i = tls->eresult_buffers[i-1].next) { |
| 336 |
if (tls->eresult_buffers[i-1].type != buff_eresult_encrypted) return false; |
| 337 |
e_encrypted_count++; |
| 338 |
} |
| 339 |
if (e_encrypted_count != tls->eresult_encrypted_count) return false; |
| 340 |
tls->validate_error = "encrypt pointers and counts"; |
| 341 |
if (e_r_empty_count+e_blank_count+e_encrypted_count != tls->eresult_buffer_count) return false; |
| 342 |
if (!tls->eresult_first_encrypted && tls->eresult_last_encrypted) return false; |
| 343 |
if (tls->eresult_last_encrypted && |
| 344 |
(tls->eresult_buffers[tls->eresult_last_encrypted-1].type != buff_eresult_encrypted || tls->eresult_buffers[tls->eresult_last_encrypted-1].next != 0)) return false; |
| 345 |
|
| 346 |
// decrypt result buffers: empty + unordered blank + ordered "decrypted" |
| 347 |
tls->validate_error = "dresult empty list"; |
| 348 |
size_t d_r_empty_count = 0; |
| 349 |
for (buff_ptr i = tls->dresult_first_empty; i; i = tls->dresult_buffers[i-1].next) { |
| 350 |
if (tls->dresult_buffers[i-1].type != buff_empty) return false; |
| 351 |
d_r_empty_count++; |
| 352 |
} |
| 353 |
if (d_r_empty_count != tls->dresult_empty_count) return false; |
| 354 |
tls->validate_error = "dresult blank list"; |
| 355 |
size_t d_blank_count = 0; |
| 356 |
for (buff_ptr i = tls->dresult_first_blank; i; i = tls->dresult_buffers[i-1].next) { |
| 357 |
if (tls->dresult_buffers[i-1].type != buff_dresult_blank) return false; |
| 358 |
d_blank_count++; |
| 359 |
} |
| 360 |
tls->validate_error = "dresult decrypted list"; |
| 361 |
size_t d_decrypted_count = 0; |
| 362 |
for (buff_ptr i = tls->dresult_first_decrypted; i; i = tls->dresult_buffers[i-1].next) { |
| 363 |
if (tls->dresult_buffers[i-1].type != buff_dresult_decrypted) return false; |
| 364 |
d_decrypted_count++; |
| 365 |
} |
| 366 |
if (d_decrypted_count != tls->dresult_decrypted_count) return false; |
| 367 |
tls->validate_error = "decrypt pointers and counts"; |
| 368 |
if (d_r_empty_count+d_blank_count+d_decrypted_count != tls->dresult_buffer_count) return false; |
| 369 |
if (!tls->dresult_first_decrypted && tls->dresult_last_decrypted) return false; |
| 370 |
if (tls->dresult_last_decrypted && |
| 371 |
(tls->dresult_buffers[tls->dresult_last_decrypted-1].type != buff_dresult_decrypted || tls->dresult_buffers[tls->dresult_last_decrypted-1].next != 0)) return false; |
| 372 |
|
| 373 |
|
| 374 |
tls->validate_error = NULL; |
| 375 |
return true; |
| 376 |
} |
| 377 |
|
| 378 |
static void validate_strict(pn_tls_t *tls) { |
| 379 |
if (!pni_tls_validate(tls)) { |
| 380 |
fprintf(stderr, "TLS internal validation error: %s", tls->validate_error); |
| 381 |
fflush(stderr); |
| 382 |
abort(); |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
static void tls_fatal(pn_tls_t *tls, int ssl_err_type, int pn_tls_err) { |
| 387 |
if (!tls->pn_tls_err) { |
| 388 |
// no previous error to overwrite |
| 389 |
tls->pn_tls_err = pn_tls_err; |
| 390 |
tls->openssl_err_type = ssl_err_type; |
| 391 |
tls->openssl_err = ERR_get_error(); |
| 392 |
tls->enc_wblocked = true; |
| 393 |
tls->dec_rblocked = true; |
| 394 |
tls->dec_wblocked = true; |
| 395 |
tls->enc_closed = true; |
| 396 |
// TODO: recocile doc which says do this: if (ssl_err_type == SSL_ERROR_SYSCALL || ssl_err_type == SSL_ERROR_SSL) { |
| 397 |
if (ssl_err_type == SSL_ERROR_SYSCALL) { |
| 398 |
// OpenSSL requires immediate halt |
| 399 |
tls->can_shutdown = false; |
| 400 |
tls->enc_rblocked = true; |
| 401 |
} else { |
| 402 |
if (tls->enc_rblocked) { |
| 403 |
// Maybe new output generated. |
| 404 |
ERR_clear_error(); // TODO: revisit need. |
| 405 |
tls->enc_rblocked = (BIO_pending(tls->bio_net_io) == 0); |
| 406 |
} |
| 407 |
} |
| 408 |
if (ssl_err_type == SSL_ERROR_SYSCALL) |
| 409 |
tls->os_errno = errno; // Save this in case it helps error reporting. |
| 410 |
} else { |
| 411 |
// TODO: handle a subsequent fatal error here. Just log it? |
| 412 |
} |
| 413 |
// TODO: use tracing here to provide additional error info from the ERR_get_error error queue. |
| 414 |
// Must be done here while in same thread as caller. |
| 415 |
ERR_clear_error(); |
| 416 |
} |
| 417 |
|
| 418 |
int pn_tls_get_session_error(pn_tls_t* tls) { |
| 419 |
return tls->pn_tls_err; |
| 420 |
} |
| 421 |
|
| 422 |
size_t pn_tls_get_session_error_string(pn_tls_t* tls, char *buf, size_t buf_len) { |
| 423 |
if (!buf || !buf_len || !tls->pn_tls_err) |
| 424 |
return 0; |
| 425 |
|
| 426 |
if (tls->openssl_err) |
| 427 |
ERR_error_string_n(tls->openssl_err, buf, buf_len); // Null terminated. |
| 428 |
else { |
| 429 |
if (tls->openssl_err_type == SSL_ERROR_SYSCALL) { |
| 430 |
// OpenSSL doesn't know the cause of the error. Memory corruption or starvation? |
| 431 |
// Byte stream in input buffers from peer not the same as it sent? |
| 432 |
// errno may help identify cause. |
| 433 |
if (tls->os_errno) { |
| 434 |
char oserr[1024]; |
| 435 |
int e = strerror_r(tls->os_errno, oserr, sizeof(oserr)); |
| 436 |
if (e) snprintf(oserr, sizeof(oserr), "unknown system error %d", tls->os_errno); |
| 437 |
snprintf(buf, buf_len, "external error: %s", oserr); |
| 438 |
} else { |
| 439 |
snprintf(buf, buf_len, "external error: bad TLS stream data"); |
| 440 |
} |
| 441 |
} else { |
| 442 |
// Presumably this never happens, always accompanied by tls->openssl_err, but if not: |
| 443 |
snprintf(buf, buf_len, "OpenSSL generic error type %d", tls->openssl_err_type); |
| 444 |
} |
| 445 |
} |
| 446 |
return strlen(buf); |
| 447 |
} |
| 448 |
|
| 449 |
size_t pn_tls_get_encrypt_input_buffer_capacity(pn_tls_t *tls) { return tls->encrypt_buffer_empty_count; } |
| 450 |
size_t pn_tls_get_decrypt_input_buffer_capacity(pn_tls_t *tls) { return tls->decrypt_buffer_empty_count; } |
| 451 |
size_t pn_tls_get_encrypt_output_buffer_capacity(pn_tls_t *tls) { return tls->eresult_empty_count; } |
| 452 |
size_t pn_tls_get_decrypt_output_buffer_capacity(pn_tls_t *tls) { return tls->dresult_empty_count; } |
| 453 |
|
| 454 |
size_t pn_tls_get_decrypt_output_buffer_count(pn_tls_t *tls) { |
| 455 |
return tls->dresult_decrypted_count; |
| 456 |
} |
| 457 |
|
| 458 |
size_t pn_tls_get_encrypt_output_buffer_count(pn_tls_t *tls) { |
| 459 |
return tls->eresult_encrypted_count; |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
uint32_t pn_tls_get_last_decrypt_output_buffer_size(pn_tls_t *tls) { |
| 464 |
return tls->dresult_last_decrypted ? tls->dresult_buffers[tls->dresult_last_decrypted-1].size : 0; |
| 465 |
} |
| 466 |
|
| 467 |
uint32_t pn_tls_get_last_encrypt_output_buffer_size(pn_tls_t *tls) { |
| 468 |
return tls->eresult_last_encrypted ? tls->eresult_buffers[tls->eresult_last_encrypted-1].size : 0; |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
// define two sets of allowable ciphers: those that require authentication, and those |
| 473 |
// that do not require authentication (anonymous). See ciphers(1). |
| 474 |
#define CIPHERS_AUTHENTICATE "ALL:!aNULL:!eNULL:@STRENGTH" |
| 475 |
#define CIPHERS_ANONYMOUS "ALL:aNULL:!eNULL:@STRENGTH" |
| 476 |
|
| 477 |
/* */ |
| 478 |
static int keyfile_pw_cb(char *buf, int size, int rwflag, void *userdata); |
| 479 |
static int init_ssl_socket(pn_tls_t *, pn_tls_config_t *); |
| 480 |
static void release_ssl_socket( pn_tls_t * ); |
| 481 |
static X509 *get_peer_certificate(pn_tls_t *ssl); |
| 482 |
|
| 483 |
|
| 484 |
//TODO: convenience for embedded existing logging. Replace ASAP. |
| 485 |
#define PN_LEVEL_NONE 0 |
| 486 |
#define PN_LEVEL_CRITICAL 1 |
| 487 |
#define PN_LEVEL_ERROR 2 |
| 488 |
#define PN_LEVEL_WARNING 4 |
| 489 |
#define PN_LEVEL_INFO 8 |
| 490 |
#define PN_LEVEL_DEBUG 16 |
| 491 |
#define PN_LEVEL_TRACE 32 |
| 492 |
#define PN_LEVEL_FRAME 64 |
| 493 |
#define PN_LEVEL_RAW 128 |
| 494 |
#define PN_LEVEL_ALL 65535 |
| 495 |
|
| 496 |
static void ssl_log(void *v, int sev, const char *fmt, ...) |
| 497 |
{ |
| 498 |
#ifdef at_least_some_logging |
| 499 |
va_list ap; |
| 500 |
va_start(ap, fmt); |
| 501 |
vfprintf(stderr, fmt, ap); |
| 502 |
va_end(ap); |
| 503 |
#endif |
| 504 |
} |
| 505 |
|
| 506 |
static void ssl_log_flush(void *v, int sev) |
| 507 |
{ |
| 508 |
char buf[128]; // see "man ERR_error_string_n()" |
| 509 |
unsigned long err = ERR_get_error(); |
| 510 |
while (err) { |
| 511 |
ERR_error_string_n(err, buf, sizeof(buf)); |
| 512 |
ssl_log(v, sev, "%s", buf); |
| 513 |
err = ERR_get_error(); |
| 514 |
} |
| 515 |
} |
| 516 |
// log an error and dump the SSL error stack |
| 517 |
static void ssl_log_error(const char *fmt, ...) |
| 518 |
{ |
| 519 |
} |
| 520 |
|
| 521 |
// Next three not exported from Proton core. Copied for now. |
| 522 |
static char *pni_strdup(const char *src) |
| 523 |
{ |
| 524 |
if (!src) return NULL; |
| 525 |
char *dest = (char *) malloc(strlen(src)+1); |
| 526 |
if (!dest) return NULL; |
| 527 |
return strcpy(dest, src); |
| 528 |
} |
| 529 |
|
| 530 |
static int pni_strcasecmp(const char *a, const char *b) |
| 531 |
{ |
| 532 |
int diff; |
| 533 |
while (*b) { |
| 534 |
char aa = *a++, bb = *b++; |
| 535 |
diff = tolower(aa)-tolower(bb); |
| 536 |
if ( diff!=0 ) return diff; |
| 537 |
} |
| 538 |
return *a; |
| 539 |
} |
| 540 |
|
| 541 |
static int pni_strncasecmp(const char* a, const char* b, size_t len) |
| 542 |
{ |
| 543 |
int diff = 0; |
| 544 |
while (*b && len > 0) { |
| 545 |
char aa = *a++, bb = *b++; |
| 546 |
diff = tolower(aa)-tolower(bb); |
| 547 |
if ( diff!=0 ) return diff; |
| 548 |
--len; |
| 549 |
}; |
| 550 |
return len==0 ? diff : *a; |
| 551 |
} |
| 552 |
|
| 553 |
|
| 554 |
/* match the DNS name pattern from the peer certificate against our configured peer |
| 555 |
hostname */ |
| 556 |
static bool match_dns_pattern( const char *hostname, |
| 557 |
const char *pattern, int plen ) |
| 558 |
{ |
| 559 |
int slen = (int) strlen(hostname); |
| 560 |
if (memchr( pattern, '*', plen ) == NULL) |
| 561 |
return (plen == slen && |
| 562 |
pni_strncasecmp( pattern, hostname, plen ) == 0); |
| 563 |
|
| 564 |
/* dns wildcarded pattern - RFC2818 */ |
| 565 |
char plabel[64]; /* max label length < 63 - RFC1034 */ |
| 566 |
char slabel[64]; |
| 567 |
|
| 568 |
while (plen > 0 && slen > 0) { |
| 569 |
const char *cptr; |
| 570 |
int len; |
| 571 |
|
| 572 |
cptr = (const char *) memchr( pattern, '.', plen ); |
| 573 |
len = (cptr) ? cptr - pattern : plen; |
| 574 |
if (len > (int) sizeof(plabel) - 1) return false; |
| 575 |
memcpy( plabel, pattern, len ); |
| 576 |
plabel[len] = 0; |
| 577 |
if (cptr) ++len; // skip matching '.' |
| 578 |
pattern += len; |
| 579 |
plen -= len; |
| 580 |
|
| 581 |
cptr = (const char *) memchr( hostname, '.', slen ); |
| 582 |
len = (cptr) ? cptr - hostname : slen; |
| 583 |
if (len > (int) sizeof(slabel) - 1) return false; |
| 584 |
memcpy( slabel, hostname, len ); |
| 585 |
slabel[len] = 0; |
| 586 |
if (cptr) ++len; // skip matching '.' |
| 587 |
hostname += len; |
| 588 |
slen -= len; |
| 589 |
|
| 590 |
char *star = strchr( plabel, '*' ); |
| 591 |
if (!star) { |
| 592 |
if (pni_strcasecmp( plabel, slabel )) return false; |
| 593 |
} else { |
| 594 |
*star = '\0'; |
| 595 |
char *prefix = plabel; |
| 596 |
int prefix_len = strlen(prefix); |
| 597 |
char *suffix = star + 1; |
| 598 |
int suffix_len = strlen(suffix); |
| 599 |
if (prefix_len && pni_strncasecmp( prefix, slabel, prefix_len )) return false; |
| 600 |
if (suffix_len && pni_strncasecmp( suffix, |
| 601 |
slabel + (strlen(slabel) - suffix_len), |
| 602 |
suffix_len )) return false; |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
return plen == slen; |
| 607 |
} |
| 608 |
|
| 609 |
// Certificate chain verification callback: return 1 if verified, |
| 610 |
// 0 if remote cannot be verified (fail handshake). |
| 611 |
// |
| 612 |
static int verify_callback(int preverify_ok, X509_STORE_CTX *ctx) |
| 613 |
{ |
| 614 |
if (!preverify_ok || X509_STORE_CTX_get_error_depth(ctx) != 0) |
| 615 |
// already failed, or not at peer cert in chain |
| 616 |
return preverify_ok; |
| 617 |
|
| 618 |
X509 *cert = X509_STORE_CTX_get_current_cert(ctx); |
| 619 |
SSL *ssn = (SSL *) X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx()); |
| 620 |
if (!ssn) { |
| 621 |
ssl_log(NULL, PN_LEVEL_ERROR, "Error: unexpected error - SSL session info not available for peer verify!"); |
| 622 |
return 0; // fail connection |
| 623 |
} |
| 624 |
|
| 625 |
pn_tls_t *ssl = (pn_tls_t *)SSL_get_ex_data(ssn, tls_ex_data_index); |
| 626 |
if (!ssl) { |
| 627 |
// TODO: replace: ssl_log(transport, PN_LEVEL_ERROR, "Error: unexpected error - SSL context info not available for peer verify!"); |
| 628 |
return 0; // fail connection |
| 629 |
} |
| 630 |
|
| 631 |
if (ssl->verify_mode != PN_TLS_VERIFY_PEER_NAME) return preverify_ok; |
| 632 |
if (!ssl->peer_hostname) { |
| 633 |
ssl_log(NULL, PN_LEVEL_ERROR, "Error: configuration error: PN_TLS_VERIFY_PEER_NAME configured, but no peer hostname set!"); |
| 634 |
return 0; // fail connection |
| 635 |
} |
| 636 |
|
| 637 |
ssl_log(NULL, PN_LEVEL_TRACE, "Checking identifying name in peer cert against '%s'", ssl->peer_hostname); |
| 638 |
|
| 639 |
bool matched = false; |
| 640 |
|
| 641 |
/* first check any SubjectAltName entries, as per RFC2818 */ |
| 642 |
GENERAL_NAMES *sans = (GENERAL_NAMES *) X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 643 |
if (sans) { |
| 644 |
int name_ct = sk_GENERAL_NAME_num( sans ); |
| 645 |
int i; |
| 646 |
for (i = 0; !matched && i < name_ct; ++i) { |
| 647 |
GENERAL_NAME *name = sk_GENERAL_NAME_value( sans, i ); |
| 648 |
if (name->type == GEN_DNS) { |
| 649 |
ASN1_STRING *asn1 = name->d.dNSName; |
| 650 |
if (asn1 && asn1->data && asn1->length) { |
| 651 |
unsigned char *str; |
| 652 |
int len = ASN1_STRING_to_UTF8( &str, asn1 ); |
| 653 |
if (len >= 0) { |
| 654 |
ssl_log(NULL, PN_LEVEL_TRACE, "SubjectAltName (dns) from peer cert = '%.*s'", len, str ); |
| 655 |
matched = match_dns_pattern( ssl->peer_hostname, (const char *)str, len ); |
| 656 |
OPENSSL_free( str ); |
| 657 |
} |
| 658 |
} |
| 659 |
} |
| 660 |
} |
| 661 |
GENERAL_NAMES_free( sans ); |
| 662 |
} |
| 663 |
|
| 664 |
/* if no general names match, try the CommonName from the subject */ |
| 665 |
X509_NAME *name = X509_get_subject_name(cert); |
| 666 |
int i = -1; |
| 667 |
while (!matched && (i = X509_NAME_get_index_by_NID(name, NID_commonName, i)) >= 0) { |
| 668 |
X509_NAME_ENTRY *ne = X509_NAME_get_entry(name, i); |
| 669 |
ASN1_STRING *name_asn1 = X509_NAME_ENTRY_get_data(ne); |
| 670 |
if (name_asn1) { |
| 671 |
unsigned char *str; |
| 672 |
int len = ASN1_STRING_to_UTF8( &str, name_asn1); |
| 673 |
if (len >= 0) { |
| 674 |
ssl_log(NULL, PN_LEVEL_TRACE, "commonName from peer cert = '%.*s'", len, str); |
| 675 |
matched = match_dns_pattern( ssl->peer_hostname, (const char *)str, len ); |
| 676 |
OPENSSL_free(str); |
| 677 |
} |
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
if (!matched) { |
| 682 |
ssl_log(NULL, PN_LEVEL_ERROR, "Error: no name matching %s found in peer cert - rejecting handshake.", |
| 683 |
ssl->peer_hostname); |
| 684 |
preverify_ok = 0; |
| 685 |
#ifdef X509_V_ERR_APPLICATION_VERIFICATION |
| 686 |
X509_STORE_CTX_set_error( ctx, X509_V_ERR_APPLICATION_VERIFICATION ); |
| 687 |
#endif |
| 688 |
} else { |
| 689 |
ssl_log(NULL, PN_LEVEL_TRACE, "Name from peer cert matched - peer is valid."); |
| 690 |
} |
| 691 |
|
| 692 |
return preverify_ok; |
| 693 |
} |
| 694 |
|
| 695 |
// This was introduced in v1.1 |
| 696 |
#if OPENSSL_VERSION_NUMBER < 0x10100000 |
| 697 |
int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g) |
| 698 |
{ |
| 699 |
dh->p = p; |
| 700 |
dh->q = q; |
| 701 |
dh->g = g; |
| 702 |
return 1; |
| 703 |
} |
| 704 |
#endif |
| 705 |
|
| 706 |
// this code was generated using the command: |
| 707 |
// "openssl dhparam -C -2 2048" |
| 708 |
static DH *get_dh2048(void) |
| 709 |
{ |
| 710 |
static const unsigned char dhp_2048[]={ |
| 711 |
0xAE,0xF7,0xE9,0x66,0x26,0x7A,0xAC,0x0A,0x6F,0x1E,0xCD,0x81, |
| 712 |
0xBD,0x0A,0x10,0x7E,0xFA,0x2C,0xF5,0x2D,0x98,0xD4,0xE7,0xD9, |
| 713 |
0xE4,0x04,0x8B,0x06,0x85,0xF2,0x0B,0xA3,0x90,0x15,0x56,0x0C, |
| 714 |
0x8B,0xBE,0xF8,0x48,0xBB,0x29,0x63,0x75,0x12,0x48,0x9D,0x7E, |
| 715 |
0x7C,0x24,0xB4,0x3A,0x38,0x7E,0x97,0x3C,0x77,0x95,0xB0,0xA2, |
| 716 |
0x72,0xB6,0xE9,0xD8,0xB8,0xFA,0x09,0x1B,0xDC,0xB3,0x80,0x6E, |
| 717 |
0x32,0x0A,0xDA,0xBB,0xE8,0x43,0x88,0x5B,0xAB,0xC3,0xB2,0x44, |
| 718 |
0xE1,0x95,0x85,0x0A,0x0D,0x13,0xE2,0x02,0x1E,0x96,0x44,0xCF, |
| 719 |
0xA0,0xD8,0x46,0x32,0x68,0x63,0x7F,0x68,0xB3,0x37,0x52,0xCE, |
| 720 |
0x3A,0x4E,0x48,0x08,0x7F,0xD5,0x53,0x00,0x59,0xA8,0x2C,0xCB, |
| 721 |
0x51,0x64,0x3D,0x5F,0xEF,0x0E,0x5F,0xE6,0xAF,0xD9,0x1E,0xA2, |
| 722 |
0x35,0x64,0x37,0xD7,0x4C,0xC9,0x24,0xFD,0x2F,0x75,0xBB,0x3A, |
| 723 |
0x15,0x82,0x76,0x4D,0xC2,0x8B,0x1E,0xB9,0x4B,0xA1,0x33,0xCF, |
| 724 |
0xAA,0x3B,0x7C,0xC2,0x50,0x60,0x6F,0x45,0x69,0xD3,0x6B,0x88, |
| 725 |
0x34,0x9B,0xE4,0xF8,0xC6,0xC7,0x5F,0x10,0xA1,0xBA,0x01,0x8C, |
| 726 |
0xDA,0xD1,0xA3,0x59,0x9C,0x97,0xEA,0xC3,0xF6,0x02,0x55,0x5C, |
| 727 |
0x92,0x1A,0x39,0x67,0x17,0xE2,0x9B,0x27,0x8D,0xE8,0x5C,0xE9, |
| 728 |
0xA5,0x94,0xBB,0x7E,0x16,0x6F,0x53,0x5A,0x6D,0xD8,0x03,0xC2, |
| 729 |
0xAC,0x7A,0xCD,0x22,0x98,0x8E,0x33,0x2A,0xDE,0xAB,0x12,0xC0, |
| 730 |
0x0B,0x7C,0x0C,0x20,0x70,0xD9,0x0B,0xAE,0x0B,0x2F,0x20,0x9B, |
| 731 |
0xA4,0xED,0xFD,0x49,0x0B,0xE3,0x4A,0xF6,0x28,0xB3,0x98,0xB0, |
| 732 |
0x23,0x1C,0x09,0x33, |
| 733 |
}; |
| 734 |
static const unsigned char dhg_2048[]={ |
| 735 |
0x02, |
| 736 |
}; |
| 737 |
DH *dh = DH_new(); |
| 738 |
BIGNUM *dhp_bn, *dhg_bn; |
| 739 |
|
| 740 |
if (dh == NULL) |
| 741 |
return NULL; |
| 742 |
dhp_bn = BN_bin2bn(dhp_2048, sizeof (dhp_2048), NULL); |
| 743 |
dhg_bn = BN_bin2bn(dhg_2048, sizeof (dhg_2048), NULL); |
| 744 |
if (dhp_bn == NULL || dhg_bn == NULL |
| 745 |
|| !DH_set0_pqg(dh, dhp_bn, NULL, dhg_bn)) { |
| 746 |
DH_free(dh); |
| 747 |
BN_free(dhp_bn); |
| 748 |
BN_free(dhg_bn); |
| 749 |
return NULL; |
| 750 |
} |
| 751 |
return dh; |
| 752 |
} |
| 753 |
|
| 754 |
typedef struct { |
| 755 |
char *id; |
| 756 |
SSL_SESSION *session; |
| 757 |
} ssl_cache_data; |
| 758 |
|
| 759 |
#define SSL_CACHE_SIZE 4 |
| 760 |
static int ssl_cache_ptr = 0; |
| 761 |
static ssl_cache_data ssl_cache[SSL_CACHE_SIZE]; |
| 762 |
|
| 763 |
static void ssn_init(void) { |
| 764 |
ssl_cache_data s = {NULL, NULL}; |
| 765 |
for (int i=0; i<SSL_CACHE_SIZE; i++) { |
| 766 |
ssl_cache[i] = s; |
| 767 |
} |
| 768 |
} |
| 769 |
|
| 770 |
static void ssn_restore(pn_tls_t *ssl) { |
| 771 |
if (!ssl->session_id) return; |
| 772 |
for (int i = ssl_cache_ptr;;) { |
| 773 |
i = (i==0) ? SSL_CACHE_SIZE-1 : i-1; |
| 774 |
if (ssl_cache[i].id == NULL) return; |
| 775 |
if (strcmp(ssl_cache[i].id, ssl->session_id) == 0) { |
| 776 |
ssl_log( NULL, PN_LEVEL_TRACE, "Restoring previous session id=%s", ssl->session_id ); |
| 777 |
int rc = SSL_set_session( ssl->ssl, ssl_cache[i].session ); |
| 778 |
if (rc != 1) { |
| 779 |
ssl_log( NULL, PN_LEVEL_WARNING, "Session restore failed, id=%s", ssl->session_id ); |
| 780 |
} |
| 781 |
return; |
| 782 |
} |
| 783 |
if (i == ssl_cache_ptr) return; |
| 784 |
} |
| 785 |
} |
| 786 |
|
| 787 |
|
| 788 |
/** Public API - visible to application code */ |
| 789 |
|
| 790 |
static bool ensure_initialized(void); |
| 791 |
|
| 792 |
static bool pni_init_ssl_domain( pn_tls_config_t * domain, pn_tls_mode_t mode ) |
| 793 |
{ |
| 794 |
if (!ensure_initialized()) { |
| 795 |
ssl_log_error("Unable to initialize OpenSSL library"); |
| 796 |
return false; |
| 797 |
} |
| 798 |
|
| 799 |
domain->ref_count = 1; |
| 800 |
domain->mode = mode; |
| 801 |
|
| 802 |
// enable all supported protocol versions, then explicitly disable the |
| 803 |
// known vulnerable ones. This should allow us to use the latest version |
| 804 |
// of the TLS standard that the installed library supports. |
| 805 |
switch(mode) { |
| 806 |
case PN_TLS_MODE_CLIENT: |
| 807 |
domain->ctx = SSL_CTX_new(SSLv23_client_method()); // and TLSv1+ |
| 808 |
if (!domain->ctx) { |
| 809 |
ssl_log_error("Unable to initialize OpenSSL context."); |
| 810 |
return false; |
| 811 |
} |
| 812 |
SSL_CTX_set_session_cache_mode(domain->ctx, SSL_SESS_CACHE_CLIENT); |
| 813 |
|
| 814 |
// By default, require peer name verification - this is a safe default |
| 815 |
if (pn_tls_config_set_peer_authentication( domain, PN_TLS_VERIFY_PEER_NAME, NULL )) { |
| 816 |
SSL_CTX_free(domain->ctx); |
| 817 |
return false; |
| 818 |
} |
| 819 |
break; |
| 820 |
|
| 821 |
case PN_TLS_MODE_SERVER: |
| 822 |
domain->ctx = SSL_CTX_new(SSLv23_server_method()); // and TLSv1+ |
| 823 |
if (!domain->ctx) { |
| 824 |
ssl_log_error("Unable to initialize OpenSSL context."); |
| 825 |
return false; |
| 826 |
} |
| 827 |
// By default, allow anonymous ciphers and do no authentication so certificates are |
| 828 |
// not required 'out of the box'; authenticating the client can be done by SASL. |
| 829 |
if (pn_tls_config_set_peer_authentication( domain, PN_TLS_ANONYMOUS_PEER, NULL )) { |
| 830 |
SSL_CTX_free(domain->ctx); |
| 831 |
return false; |
| 832 |
} |
| 833 |
break; |
| 834 |
|
| 835 |
default: |
| 836 |
ssl_log(NULL, PN_LEVEL_ERROR, "Invalid value for pn_tls_mode_t: %d", mode); |
| 837 |
return false; |
| 838 |
} |
| 839 |
|
| 840 |
// By default set up system default certificates |
| 841 |
if (!SSL_CTX_set_default_verify_paths(domain->ctx)){ |
| 842 |
ssl_log_error("Failed to set default certificate paths"); |
| 843 |
SSL_CTX_free(domain->ctx); |
| 844 |
return false; |
| 845 |
}; |
| 846 |
|
| 847 |
const long reject_insecure = |
| 848 |
SSL_OP_NO_SSLv2 |
| 849 |
| SSL_OP_NO_SSLv3 |
| 850 |
// Mitigate the CRIME vulnerability |
| 851 |
# ifdef SSL_OP_NO_COMPRESSION |
| 852 |
| SSL_OP_NO_COMPRESSION |
| 853 |
# endif |
| 854 |
; |
| 855 |
SSL_CTX_set_options(domain->ctx, reject_insecure); |
| 856 |
|
| 857 |
# ifdef SSL_SECOP_PEER |
| 858 |
domain->default_seclevel = SSL_CTX_get_security_level(domain->ctx); |
| 859 |
# endif |
| 860 |
|
| 861 |
DH *dh = get_dh2048(); |
| 862 |
if (dh) { |
| 863 |
SSL_CTX_set_tmp_dh(domain->ctx, dh); |
| 864 |
DH_free(dh); |
| 865 |
SSL_CTX_set_options(domain->ctx, SSL_OP_SINGLE_DH_USE); |
| 866 |
} |
| 867 |
|
| 868 |
return true; |
| 869 |
} |
| 870 |
|
| 871 |
pn_tls_config_t *pn_tls_config( pn_tls_mode_t mode ) |
| 872 |
{ |
| 873 |
pn_tls_config_t *domain = (pn_tls_config_t *) calloc(1, sizeof(pn_tls_config_t)); |
| 874 |
if (!domain) return NULL; |
| 875 |
|
| 876 |
if (!pni_init_ssl_domain(domain, mode)) { |
| 877 |
free(domain); |
| 878 |
return NULL; |
| 879 |
} |
| 880 |
|
| 881 |
return domain; |
| 882 |
} |
| 883 |
|
| 884 |
void pn_tls_config_free( pn_tls_config_t *domain ) |
| 885 |
{ |
| 886 |
if (--domain->ref_count == 0) { |
| 887 |
|
| 888 |
SSL_CTX_free(domain->ctx); |
| 889 |
free(domain->keyfile_pw); |
| 890 |
free(domain->trusted_CAs); |
| 891 |
free(domain->ciphers); |
| 892 |
free(domain->alpn_list); |
| 893 |
free(domain); |
| 894 |
} |
| 895 |
} |
| 896 |
|
| 897 |
|
| 898 |
int pn_tls_config_set_credentials( pn_tls_config_t *domain, |
| 899 |
const char *certificate_file, |
| 900 |
const char *private_key_file, |
| 901 |
const char *password) |
| 902 |
{ |
| 903 |
if (!domain || !domain->ctx) return -1; |
| 904 |
|
| 905 |
if (SSL_CTX_use_certificate_chain_file(domain->ctx, certificate_file) != 1) { |
| 906 |
ssl_log_error("SSL_CTX_use_certificate_chain_file( %s ) failed", certificate_file); |
| 907 |
return -3; |
| 908 |
} |
| 909 |
|
| 910 |
if (password) { |
| 911 |
domain->keyfile_pw = pni_strdup(password); // @todo: obfuscate me!!! |
| 912 |
SSL_CTX_set_default_passwd_cb(domain->ctx, keyfile_pw_cb); |
| 913 |
SSL_CTX_set_default_passwd_cb_userdata(domain->ctx, domain->keyfile_pw); |
| 914 |
} |
| 915 |
|
| 916 |
if (SSL_CTX_use_PrivateKey_file(domain->ctx, private_key_file, SSL_FILETYPE_PEM) != 1) { |
| 917 |
ssl_log_error("SSL_CTX_use_PrivateKey_file( %s ) failed", private_key_file); |
| 918 |
return -4; |
| 919 |
} |
| 920 |
|
| 921 |
if (SSL_CTX_check_private_key(domain->ctx) != 1) { |
| 922 |
ssl_log_error("The key file %s is not consistent with the certificate %s", |
| 923 |
private_key_file, certificate_file); |
| 924 |
return -5; |
| 925 |
} |
| 926 |
|
| 927 |
domain->has_certificate = true; |
| 928 |
|
| 929 |
// bug in older versions of OpenSSL: servers may request client cert even if anonymous |
| 930 |
// cipher was negotiated. TLSv1 will reject such a request. Hack: once a cert is |
| 931 |
// configured, allow only authenticated ciphers. |
| 932 |
if (!domain->ciphers && !SSL_CTX_set_cipher_list( domain->ctx, CIPHERS_AUTHENTICATE )) { |
| 933 |
ssl_log_error("Failed to set cipher list to %s", CIPHERS_AUTHENTICATE); |
| 934 |
return -6; |
| 935 |
} |
| 936 |
|
| 937 |
return 0; |
| 938 |
} |
| 939 |
|
| 940 |
int pn_tls_config_set_impl_ciphers(pn_tls_config_t *domain, const char *ciphers) |
| 941 |
{ |
| 942 |
if (!SSL_CTX_set_cipher_list(domain->ctx, ciphers)) { |
| 943 |
ssl_log_error("Failed to set cipher list to %s", ciphers); |
| 944 |
return -6; |
| 945 |
} |
| 946 |
if (domain->ciphers) free(domain->ciphers); |
| 947 |
domain->ciphers = pni_strdup(ciphers); |
| 948 |
return 0; |
| 949 |
} |
| 950 |
|
| 951 |
int pn_tls_config_set_trusted_certs(pn_tls_config_t *domain, |
| 952 |
const char *certificate_db) |
| 953 |
{ |
| 954 |
if (!domain) return -1; |
| 955 |
|
| 956 |
// certificates can be either a file or a directory, which determines how it is passed |
| 957 |
// to SSL_CTX_load_verify_locations() |
| 958 |
struct stat sbuf; |
| 959 |
if (stat( certificate_db, &sbuf ) != 0) { |
| 960 |
ssl_log(NULL, PN_LEVEL_ERROR, "stat(%s) failed: %s", certificate_db, strerror(errno)); |
| 961 |
return -1; |
| 962 |
} |
| 963 |
|
| 964 |
const char *file; |
| 965 |
const char *dir; |
| 966 |
if (S_ISDIR(sbuf.st_mode)) { |
| 967 |
dir = certificate_db; |
| 968 |
file = NULL; |
| 969 |
} else { |
| 970 |
dir = NULL; |
| 971 |
file = certificate_db; |
| 972 |
} |
| 973 |
|
| 974 |
if (SSL_CTX_load_verify_locations( domain->ctx, file, dir ) != 1) { |
| 975 |
ssl_log_error("SSL_CTX_load_verify_locations( %s ) failed", certificate_db); |
| 976 |
return -1; |
| 977 |
} |
| 978 |
|
| 979 |
return 0; |
| 980 |
} |
| 981 |
|
| 982 |
|
| 983 |
int pn_tls_config_set_peer_authentication(pn_tls_config_t *domain, |
| 984 |
const pn_tls_verify_mode_t mode, |
| 985 |
const char *trusted_CAs) |
| 986 |
{ |
| 987 |
if (!domain) return -1; |
| 988 |
|
| 989 |
switch (mode) { |
| 990 |
case PN_TLS_VERIFY_PEER: |
| 991 |
case PN_TLS_VERIFY_PEER_NAME: |
| 992 |
|
| 993 |
#ifdef SSL_SECOP_PEER |
| 994 |
SSL_CTX_set_security_level(domain->ctx, domain->default_seclevel); |
| 995 |
#endif |
| 996 |
|
| 997 |
if (domain->mode == PN_TLS_MODE_SERVER) { |
| 998 |
// openssl requires that server connections supply a list of trusted CAs which is |
| 999 |
// sent to the client |
| 1000 |
if (!trusted_CAs) { |
| 1001 |
ssl_log(NULL, PN_LEVEL_ERROR, "Error: a list of trusted CAs must be provided."); |
| 1002 |
return -1; |
| 1003 |
} |
| 1004 |
if (!domain->has_certificate) { |
| 1005 |
ssl_log(NULL, PN_LEVEL_ERROR, "Error: Server cannot verify peer without configuring a certificate, use pn_tls_config_set_credentials()"); |
| 1006 |
return -1; |
| 1007 |
} |
| 1008 |
|
| 1009 |
if (domain->trusted_CAs) free(domain->trusted_CAs); |
| 1010 |
domain->trusted_CAs = pni_strdup( trusted_CAs ); |
| 1011 |
STACK_OF(X509_NAME) *cert_names; |
| 1012 |
cert_names = SSL_load_client_CA_file( domain->trusted_CAs ); |
| 1013 |
if (cert_names != NULL) |
| 1014 |
SSL_CTX_set_client_CA_list(domain->ctx, cert_names); |
| 1015 |
else { |
| 1016 |
ssl_log(NULL, PN_LEVEL_ERROR, "Error: Unable to process file of trusted CAs: %s", trusted_CAs); |
| 1017 |
return -1; |
| 1018 |
} |
| 1019 |
} |
| 1020 |
|
| 1021 |
SSL_CTX_set_verify( domain->ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, |
| 1022 |
verify_callback); |
| 1023 |
#if (OPENSSL_VERSION_NUMBER < 0x00905100L) |
| 1024 |
SSL_CTX_set_verify_depth(domain->ctx, 1); |
| 1025 |
#endif |
| 1026 |
|
| 1027 |
// A bit of a hack - If we asked for peer verification then disallow anonymous ciphers |
| 1028 |
// A much more robust thing would be to ensure that we actually have a peer certificate |
| 1029 |
// when we've finished the SSL handshake |
| 1030 |
if (!domain->ciphers && !SSL_CTX_set_cipher_list( domain->ctx, CIPHERS_AUTHENTICATE )) { |
| 1031 |
ssl_log_error("Failed to set cipher list to %s", CIPHERS_AUTHENTICATE); |
| 1032 |
return -1; |
| 1033 |
} |
| 1034 |
break; |
| 1035 |
|
| 1036 |
case PN_TLS_ANONYMOUS_PEER: // hippie free love mode... :) |
| 1037 |
#ifdef SSL_SECOP_PEER |
| 1038 |
// Must use lowest OpenSSL security level to enable anonymous ciphers. |
| 1039 |
SSL_CTX_set_security_level(domain->ctx, 0); |
| 1040 |
#endif |
| 1041 |
SSL_CTX_set_verify( domain->ctx, SSL_VERIFY_NONE, NULL ); |
| 1042 |
// Only allow anonymous ciphers if we allow anonymous peers |
| 1043 |
if (!domain->ciphers && !SSL_CTX_set_cipher_list( domain->ctx, CIPHERS_ANONYMOUS )) { |
| 1044 |
ssl_log_error("Failed to set cipher list to %s", CIPHERS_ANONYMOUS); |
| 1045 |
return -1; |
| 1046 |
} |
| 1047 |
break; |
| 1048 |
|
| 1049 |
default: |
| 1050 |
ssl_log(NULL, PN_LEVEL_ERROR, "Invalid peer authentication mode given." ); |
| 1051 |
return -1; |
| 1052 |
} |
| 1053 |
|
| 1054 |
domain->verify_mode = mode; |
| 1055 |
return 0; |
| 1056 |
} |
| 1057 |
|
| 1058 |
void pn_tls_free(pn_tls_t *ssl) |
| 1059 |
{ |
| 1060 |
if (!ssl) return; |
| 1061 |
ssl_log(NULL, PN_LEVEL_TRACE, "SSL socket freed." ); |
| 1062 |
release_ssl_socket( ssl ); |
| 1063 |
if (ssl->session_id) free((void *)ssl->session_id); |
| 1064 |
if (ssl->peer_hostname) free((void *)ssl->peer_hostname); |
| 1065 |
if (ssl->subject) free(ssl->subject); |
| 1066 |
if (ssl->peer_certificate) X509_free(ssl->peer_certificate); |
| 1067 |
if (ssl->eresult_buffers) free(ssl->eresult_buffers); |
| 1068 |
if (ssl->alpn_list) free(ssl->alpn_list); |
| 1069 |
free(ssl); |
| 1070 |
} |
| 1071 |
|
| 1072 |
static int pni_tls_init(pn_tls_t *ssl, pn_tls_config_t *domain, const char *unused) |
| 1073 |
{ |
| 1074 |
if (!ssl) return -1; |
| 1075 |
|
| 1076 |
ssl->mode = domain->mode; |
| 1077 |
ssl->verify_mode = domain->verify_mode; |
| 1078 |
return init_ssl_socket(ssl, domain); |
| 1079 |
} |
| 1080 |
|
| 1081 |
pn_tls_t *pn_tls(pn_tls_config_t *domain) { |
| 1082 |
if (!domain) return NULL; |
| 1083 |
pn_tls_t *tls = (pn_tls_t *) calloc(1, sizeof(pn_tls_t)); |
| 1084 |
if (!tls) return NULL; |
| 1085 |
|
| 1086 |
if (domain->alpn_list_len) { |
| 1087 |
// make copy since small: avoid reference counting and thread safety management. |
| 1088 |
tls->alpn_list = (unsigned char *) malloc(domain->alpn_list_len); |
| 1089 |
if (!tls->alpn_list) { |
| 1090 |
free(tls); |
| 1091 |
return NULL; |
| 1092 |
} |
| 1093 |
memmove(tls->alpn_list, domain->alpn_list, domain->alpn_list_len); |
| 1094 |
tls->alpn_list_len = domain->alpn_list_len; |
| 1095 |
} |
| 1096 |
|
| 1097 |
tls->domain = domain; |
| 1098 |
const char *env = getenv("PN_TLS_DBG"); |
| 1099 |
if (env && *env == '1') tls->validating = true; |
| 1100 |
return tls; |
| 1101 |
} |
| 1102 |
|
| 1103 |
int pn_tls_start(pn_tls_t *tls) { |
| 1104 |
if (tls->started) |
| 1105 |
return PN_ARG_ERR; |
| 1106 |
if (!tls->eresult_buffer_count) |
| 1107 |
tls->eresult_buffer_count = default_eresult_buffer_count; |
| 1108 |
if (!tls->dresult_buffer_count) |
| 1109 |
tls->dresult_buffer_count = default_dresult_buffer_count; |
| 1110 |
if (!tls->encrypt_buffer_count) |
| 1111 |
tls->encrypt_buffer_count = default_encrypt_buffer_count; |
| 1112 |
if (!tls->decrypt_buffer_count) |
| 1113 |
tls->decrypt_buffer_count = default_decrypt_buffer_count; |
| 1114 |
// Allocate all four in one go. |
| 1115 |
size_t count = tls->eresult_buffer_count + tls->dresult_buffer_count + tls->encrypt_buffer_count + tls->decrypt_buffer_count; |
| 1116 |
tls->eresult_buffers = calloc(count, sizeof(pbuffer_t)); |
| 1117 |
if (!tls->eresult_buffers) |
| 1118 |
return PN_OUT_OF_MEMORY; |
| 1119 |
tls->dresult_buffers = tls->eresult_buffers + tls->eresult_buffer_count; |
| 1120 |
tls->encrypt_buffers = tls->dresult_buffers + tls->dresult_buffer_count; |
| 1121 |
tls->decrypt_buffers = tls->encrypt_buffers + tls->encrypt_buffer_count; |
| 1122 |
|
| 1123 |
tls_initialize_buffers(tls); |
| 1124 |
|
| 1125 |
tls->started = true; |
| 1126 |
return pni_tls_init(tls, tls->domain, tls->session_id); |
| 1127 |
} |
| 1128 |
|
| 1129 |
int pn_tls_stop(pn_tls_t *tls) { |
| 1130 |
if (tls->stopped) |
| 1131 |
return PN_ARG_ERR; |
| 1132 |
tls->stopped = true; |
| 1133 |
return 0; |
| 1134 |
} |
| 1135 |
|
| 1136 |
int pn_tls_get_ssf(pn_tls_t *ssl) |
| 1137 |
{ |
| 1138 |
const SSL_CIPHER *c; |
| 1139 |
|
| 1140 |
if (ssl && ssl->ssl && (c = SSL_get_current_cipher( ssl->ssl ))) { |
| 1141 |
return SSL_CIPHER_get_bits(c, NULL); |
| 1142 |
} |
| 1143 |
return 0; |
| 1144 |
} |
| 1145 |
|
| 1146 |
bool pn_tls_get_cipher(pn_tls_t *ssl, const char **cipher, size_t *size ) |
| 1147 |
{ |
| 1148 |
const SSL_CIPHER *c; |
| 1149 |
if (ssl->ssl && (c = SSL_get_current_cipher( ssl->ssl ))) { |
| 1150 |
const char *v = SSL_CIPHER_get_name(c); |
| 1151 |
if (v) { |
| 1152 |
*size = strlen(v); |
| 1153 |
*cipher = v; |
| 1154 |
return true; |
| 1155 |
} |
| 1156 |
} |
| 1157 |
*cipher = NULL; |
| 1158 |
*size = 0; |
| 1159 |
return false; |
| 1160 |
} |
| 1161 |
|
| 1162 |
bool pn_tls_get_protocol_version(pn_tls_t *ssl, const char **version, size_t *size ) |
| 1163 |
{ |
| 1164 |
const SSL_CIPHER *c; |
| 1165 |
if (ssl->ssl && (c = SSL_get_current_cipher( ssl->ssl ))) { |
| 1166 |
const char *v = SSL_CIPHER_get_version(c); |
| 1167 |
if (v) { |
| 1168 |
*size = strlen(v); |
| 1169 |
*version = v; |
| 1170 |
return true; |
| 1171 |
} |
| 1172 |
} |
| 1173 |
*version = NULL; |
| 1174 |
*size = 0; |
| 1175 |
return false; |
| 1176 |
} |
| 1177 |
|
| 1178 |
|
| 1179 |
/** Private: */ |
| 1180 |
|
| 1181 |
static int keyfile_pw_cb(char *buf, int size, int rwflag, void *userdata) |
| 1182 |
{ |
| 1183 |
strncpy(buf, (char *)userdata, size); // @todo: un-obfuscate me!!! |
| 1184 |
buf[size - 1] = '\0'; |
| 1185 |
return(strlen(buf)); |
| 1186 |
} |
| 1187 |
|
| 1188 |
|
| 1189 |
//////// SSL Connections |
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
static int init_ssl_socket(pn_tls_t *ssl, pn_tls_config_t *domain) |
| 1194 |
{ |
| 1195 |
if (ssl->ssl) return 0; |
| 1196 |
if (!domain) return -1; |
| 1197 |
|
| 1198 |
ssl->ssl = SSL_new(domain->ctx); |
| 1199 |
if (!ssl->ssl) { |
| 1200 |
ssl_log(NULL, PN_LEVEL_ERROR, "SSL socket setup failure." ); |
| 1201 |
ssl_log_flush(NULL, PN_LEVEL_ERROR); |
| 1202 |
return -1; |
| 1203 |
} |
| 1204 |
|
| 1205 |
// store backpointer |
| 1206 |
SSL_set_ex_data(ssl->ssl, tls_ex_data_index, ssl); |
| 1207 |
|
| 1208 |
#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1209 |
if (ssl->peer_hostname && ssl->mode == PN_TLS_MODE_CLIENT) { |
| 1210 |
SSL_set_tlsext_host_name(ssl->ssl, ssl->peer_hostname); |
| 1211 |
} |
| 1212 |
#endif |
| 1213 |
|
| 1214 |
if (ssl->alpn_list && ssl->mode == PN_TLS_MODE_CLIENT) { |
| 1215 |
if (SSL_set_alpn_protos(ssl->ssl, ssl->alpn_list, ssl->alpn_list_len) != 0) { |
| 1216 |
ssl_log(NULL, PN_LEVEL_ERROR, "Internal ALPN setup failure." ); |
| 1217 |
return -1; |
| 1218 |
} |
| 1219 |
} // server case is per domain set pn_tls_config_set_alpn |
| 1220 |
|
| 1221 |
// restore session, if available |
| 1222 |
ssn_restore(ssl); |
| 1223 |
|
| 1224 |
// now layer a BIO over the SSL socket |
| 1225 |
ssl->bio_ssl = BIO_new(BIO_f_ssl()); |
| 1226 |
if (!ssl->bio_ssl) { |
| 1227 |
ssl_log(NULL, PN_LEVEL_ERROR, "BIO setup failure." ); |
| 1228 |
return -1; |
| 1229 |
} |
| 1230 |
(void)BIO_set_ssl(ssl->bio_ssl, ssl->ssl, BIO_NOCLOSE); |
| 1231 |
|
| 1232 |
// create the "lower" BIO "pipe", and attach it below the SSL layer |
| 1233 |
if (!BIO_new_bio_pair(&ssl->bio_ssl_io, 0, &ssl->bio_net_io, 0)) { |
| 1234 |
ssl_log(NULL, PN_LEVEL_ERROR, "BIO setup failure." ); |
| 1235 |
return -1; |
| 1236 |
} |
| 1237 |
SSL_set_bio(ssl->ssl, ssl->bio_ssl_io, ssl->bio_ssl_io); |
| 1238 |
|
| 1239 |
if (ssl->mode == PN_TLS_MODE_SERVER) { |
| 1240 |
SSL_set_accept_state(ssl->ssl); |
| 1241 |
BIO_set_ssl_mode(ssl->bio_ssl, 0); // server mode |
| 1242 |
ssl_log( NULL, PN_LEVEL_TRACE, "Server SSL socket created." ); |
| 1243 |
ssl->enc_rblocked = true; |
| 1244 |
ssl->dec_rblocked = true; |
| 1245 |
} else { // client mode |
| 1246 |
SSL_set_connect_state(ssl->ssl); |
| 1247 |
BIO_set_ssl_mode(ssl->bio_ssl, 1); // client mode |
| 1248 |
ssl_log( NULL, PN_LEVEL_TRACE, "Client SSL socket created." ); |
| 1249 |
// Start the handshake process. SSL/BIO read/writes keep it going as necessary. |
| 1250 |
SSL_do_handshake(ssl->ssl); |
| 1251 |
ssl->enc_rblocked = false; // client hello starts the whole process |
| 1252 |
ssl->dec_rblocked = true; |
| 1253 |
} |
| 1254 |
ssl->subject = NULL; |
| 1255 |
ssl->peer_certificate = NULL; |
| 1256 |
return 0; |
| 1257 |
} |
| 1258 |
|
| 1259 |
static void release_ssl_socket(pn_tls_t *ssl) |
| 1260 |
{ |
| 1261 |
if (ssl->bio_ssl) BIO_free(ssl->bio_ssl); |
| 1262 |
if (ssl->ssl) { |
| 1263 |
SSL_free(ssl->ssl); // will free bio_ssl_io |
| 1264 |
} else { |
| 1265 |
if (ssl->bio_ssl_io) BIO_free(ssl->bio_ssl_io); |
| 1266 |
} |
| 1267 |
if (ssl->bio_net_io) BIO_free(ssl->bio_net_io); |
| 1268 |
ssl->bio_ssl = NULL; |
| 1269 |
ssl->bio_ssl_io = NULL; |
| 1270 |
ssl->bio_net_io = NULL; |
| 1271 |
ssl->ssl = NULL; |
| 1272 |
} |
| 1273 |
|
| 1274 |
int pn_tls_set_peer_hostname(pn_tls_t *ssl, const char *hostname) |
| 1275 |
{ |
| 1276 |
if (!ssl) return -1; |
| 1277 |
|
| 1278 |
if (ssl->peer_hostname) free((void *)ssl->peer_hostname); |
| 1279 |
ssl->peer_hostname = NULL; |
| 1280 |
if (hostname) { |
| 1281 |
ssl->peer_hostname = pni_strdup(hostname); |
| 1282 |
if (!ssl->peer_hostname) return -2; |
| 1283 |
#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1284 |
if (ssl->ssl && ssl->mode == PN_TLS_MODE_CLIENT) { |
| 1285 |
SSL_set_tlsext_host_name(ssl->ssl, ssl->peer_hostname); |
| 1286 |
} |
| 1287 |
#endif |
| 1288 |
} |
| 1289 |
return 0; |
| 1290 |
} |
| 1291 |
|
| 1292 |
int pn_tls_get_peer_hostname(pn_tls_t *ssl, char *hostname, size_t *bufsize) |
| 1293 |
{ |
| 1294 |
if (!ssl) return -1; |
| 1295 |
if (!ssl->peer_hostname) { |
| 1296 |
*bufsize = 0; |
| 1297 |
if (hostname) *hostname = '\0'; |
| 1298 |
return 0; |
| 1299 |
} |
| 1300 |
unsigned len = strlen(ssl->peer_hostname); |
| 1301 |
if (hostname) { |
| 1302 |
if (len >= *bufsize) return -1; |
| 1303 |
strcpy( hostname, ssl->peer_hostname ); |
| 1304 |
} |
| 1305 |
*bufsize = len; |
| 1306 |
return 0; |
| 1307 |
} |
| 1308 |
|
| 1309 |
static X509 *get_peer_certificate(pn_tls_t *ssl) |
| 1310 |
{ |
| 1311 |
// Cache for multiple use and final X509_free |
| 1312 |
if (!ssl->peer_certificate && ssl->ssl) { |
| 1313 |
ssl->peer_certificate = SSL_get_peer_certificate(ssl->ssl); |
| 1314 |
// May still be NULL depending on timing or type of SSL connection |
| 1315 |
} |
| 1316 |
return ssl->peer_certificate; |
| 1317 |
} |
| 1318 |
|
| 1319 |
const char* pn_tls_get_remote_subject(pn_tls_t *ssl) |
| 1320 |
{ |
| 1321 |
if (!ssl || !ssl->ssl) return NULL; |
| 1322 |
if (!ssl->subject) { |
| 1323 |
X509 *cert = get_peer_certificate(ssl); |
| 1324 |
if (!cert) return NULL; |
| 1325 |
X509_NAME *subject = X509_get_subject_name(cert); |
| 1326 |
if (!subject) return NULL; |
| 1327 |
|
| 1328 |
BIO *out = BIO_new(BIO_s_mem()); |
| 1329 |
X509_NAME_print_ex(out, subject, 0, XN_FLAG_RFC2253); |
| 1330 |
int len = BIO_number_written(out); |
| 1331 |
ssl->subject = (char*) malloc(len+1); |
| 1332 |
ssl->subject[len] = 0; |
| 1333 |
BIO_read(out, ssl->subject, len); |
| 1334 |
BIO_free(out); |
| 1335 |
} |
| 1336 |
return ssl->subject; |
| 1337 |
} |
| 1338 |
|
| 1339 |
int pn_tls_get_cert_fingerprint(pn_tls_t *ssl, char *fingerprint, size_t fingerprint_length, pn_tls_hash_alg hash_alg) |
| 1340 |
{ |
| 1341 |
const char *digest_name = NULL; |
| 1342 |
size_t min_required_length; |
| 1343 |
|
| 1344 |
// old versions of python expect fingerprint to contain a valid string on |
| 1345 |
// return from this function |
| 1346 |
fingerprint[0] = 0; |
| 1347 |
|
| 1348 |
// Assign the correct digest_name value based on the enum values. |
| 1349 |
switch (hash_alg) { |
| 1350 |
case PN_TLS_SHA1 : |
| 1351 |
min_required_length = 41; // 40 hex characters + 1 '\0' character |
| 1352 |
digest_name = "sha1"; |
| 1353 |
break; |
| 1354 |
case PN_TLS_SHA256 : |
| 1355 |
min_required_length = 65; // 64 hex characters + 1 '\0' character |
| 1356 |
digest_name = "sha256"; |
| 1357 |
break; |
| 1358 |
case PN_TLS_SHA512 : |
| 1359 |
min_required_length = 129; // 128 hex characters + 1 '\0' character |
| 1360 |
digest_name = "sha512"; |
| 1361 |
break; |
| 1362 |
case PN_TLS_MD5 : |
| 1363 |
min_required_length = 33; // 32 hex characters + 1 '\0' character |
| 1364 |
digest_name = "md5"; |
| 1365 |
break; |
| 1366 |
default: |
| 1367 |
ssl_log_error("Unknown or unhandled hash algorithm %i ", hash_alg); |
| 1368 |
return PN_ERR; |
| 1369 |
|
| 1370 |
} |
| 1371 |
|
| 1372 |
if(fingerprint_length < min_required_length) { |
| 1373 |
ssl_log_error("Insufficient fingerprint_length %" PN_ZU ". fingerprint_length must be %" PN_ZU " or above for %s digest", |
| 1374 |
fingerprint_length, min_required_length, digest_name); |
| 1375 |
return PN_ERR; |
| 1376 |
} |
| 1377 |
|
| 1378 |
const EVP_MD *digest = EVP_get_digestbyname(digest_name); |
| 1379 |
|
| 1380 |
X509 *cert = get_peer_certificate(ssl); |
| 1381 |
|
| 1382 |
if(cert) { |
| 1383 |
unsigned int len; |
| 1384 |
|
| 1385 |
unsigned char bytes[64]; // sha512 uses 64 octets, we will use that as the maximum. |
| 1386 |
|
| 1387 |
if (X509_digest(cert, digest, bytes, &len) != 1) { |
| 1388 |
ssl_log_error("Failed to extract X509 digest"); |
| 1389 |
return PN_ERR; |
| 1390 |
} |
| 1391 |
|
| 1392 |
char *cursor = fingerprint; |
| 1393 |
|
| 1394 |
for (size_t i=0; i<len ; i++) { |
| 1395 |
cursor += snprintf((char *)cursor, fingerprint_length, "%02x", bytes[i]); |
| 1396 |
fingerprint_length = fingerprint_length - 2; |
| 1397 |
} |
| 1398 |
|
| 1399 |
return PN_OK; |
| 1400 |
} |
| 1401 |
else { |
| 1402 |
ssl_log_error("No certificate is available yet "); |
| 1403 |
return PN_ERR; |
| 1404 |
} |
| 1405 |
|
| 1406 |
return 0; |
| 1407 |
} |
| 1408 |
|
| 1409 |
|
| 1410 |
const char* pn_tls_get_remote_subject_subfield(pn_tls_t *ssl, pn_tls_cert_subject_subfield field) |
| 1411 |
{ |
| 1412 |
int openssl_field = 0; |
| 1413 |
|
| 1414 |
// Assign openssl internal representations of field values to openssl_field |
| 1415 |
switch (field) { |
| 1416 |
case PN_TLS_CERT_SUBJECT_COUNTRY_NAME : |
| 1417 |
openssl_field = NID_countryName; |
| 1418 |
break; |
| 1419 |
case PN_TLS_CERT_SUBJECT_STATE_OR_PROVINCE : |
| 1420 |
openssl_field = NID_stateOrProvinceName; |
| 1421 |
break; |
| 1422 |
case PN_TLS_CERT_SUBJECT_CITY_OR_LOCALITY : |
| 1423 |
openssl_field = NID_localityName; |
| 1424 |
break; |
| 1425 |
case PN_TLS_CERT_SUBJECT_ORGANIZATION_NAME : |
| 1426 |
openssl_field = NID_organizationName; |
| 1427 |
break; |
| 1428 |
case PN_TLS_CERT_SUBJECT_ORGANIZATION_UNIT : |
| 1429 |
openssl_field = NID_organizationalUnitName; |
| 1430 |
break; |
| 1431 |
case PN_TLS_CERT_SUBJECT_COMMON_NAME : |
| 1432 |
openssl_field = NID_commonName; |
| 1433 |
break; |
| 1434 |
default: |
| 1435 |
ssl_log_error("Unknown or unhandled certificate subject subfield %i", field); |
| 1436 |
return NULL; |
| 1437 |
} |
| 1438 |
|
| 1439 |
X509 *cert = get_peer_certificate(ssl); |
| 1440 |
if (!cert) return NULL; |
| 1441 |
|
| 1442 |
X509_NAME *subject_name = X509_get_subject_name(cert); |
| 1443 |
|
| 1444 |
// 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 |
| 1445 |
// For now, we will only return the first common name if there is more than one common name in the cert |
| 1446 |
int index = X509_NAME_get_index_by_NID(subject_name, openssl_field, -1); |
| 1447 |
|
| 1448 |
if (index > -1) { |
| 1449 |
X509_NAME_ENTRY *ne = X509_NAME_get_entry(subject_name, index); |
| 1450 |
if(ne) { |
| 1451 |
ASN1_STRING *name_asn1 = X509_NAME_ENTRY_get_data(ne); |
| 1452 |
return (char *) name_asn1->data; |
| 1453 |
} |
| 1454 |
} |
| 1455 |
|
| 1456 |
return NULL; |
| 1457 |
} |
| 1458 |
|
| 1459 |
static inline uint32_t room(pbuffer_t const *b) { |
| 1460 |
if (b) |
| 1461 |
return b->capacity - (b->offset + b->size); |
| 1462 |
return 0; |
| 1463 |
} |
| 1464 |
|
| 1465 |
bool pn_tls_is_secure(pn_tls_t * tls) { |
| 1466 |
return tls->handshake_ok && !tls->pn_tls_err && !tls->stopped; |
| 1467 |
} |
| 1468 |
|
| 1469 |
|
| 1470 |
/* Thread-safe locking and initialization for POSIX and Windows */ |
| 1471 |
|
| 1472 |
static bool init_ok = false; |
| 1473 |
|
| 1474 |
#ifdef _WIN32 |
| 1475 |
|
| 1476 |
typedef CRITICAL_SECTION pni_mutex_t; |
| 1477 |
static inline void pni_mutex_init(pni_mutex_t *m) { InitializeCriticalSection(m); } |
| 1478 |
static inline void pni_mutex_lock(pni_mutex_t *m) { EnterCriticalSection(m); } |
| 1479 |
static inline void pni_mutex_unlock(pni_mutex_t *m) { LeaveCriticalSection(m); } |
| 1480 |
static inline unsigned long id_callback(void) { return (unsigned long)GetCurrentThreadId(); } |
| 1481 |
INIT_ONCE initialize_once = INIT_ONCE_STATIC_INIT; |
| 1482 |
static inline bool ensure_initialized(void) { |
| 1483 |
void* dummy; |
| 1484 |
InitOnceExecuteOnce(&initialize_once, &initialize, NULL, &dummy); |
| 1485 |
return init_ok; |
| 1486 |
} |
| 1487 |
|
| 1488 |
#else /* POSIX */ |
| 1489 |
|
| 1490 |
#include <pthread.h> |
| 1491 |
|
| 1492 |
static void initialize(void); |
| 1493 |
|
| 1494 |
typedef pthread_mutex_t pni_mutex_t; |
| 1495 |
static inline int pni_mutex_init(pni_mutex_t *m) { return pthread_mutex_init(m, NULL); } |
| 1496 |
static inline int pni_mutex_lock(pni_mutex_t *m) { return pthread_mutex_lock(m); } |
| 1497 |
static inline int pni_mutex_unlock(pni_mutex_t *m) { return pthread_mutex_unlock(m); } |
| 1498 |
static inline unsigned long id_callback(void) { return (unsigned long)pthread_self(); } |
| 1499 |
static pthread_once_t initialize_once = PTHREAD_ONCE_INIT; |
| 1500 |
static inline bool ensure_initialized(void) { |
| 1501 |
pthread_once(&initialize_once, &initialize); |
| 1502 |
return init_ok; |
| 1503 |
} |
| 1504 |
|
| 1505 |
#endif |
| 1506 |
|
| 1507 |
static pni_mutex_t *locks = NULL; /* Lock array for openssl */ |
| 1508 |
|
| 1509 |
/* Callback function for openssl locking */ |
| 1510 |
static void locking_callback(int mode, int n, const char *file, int line) { |
| 1511 |
if(mode & CRYPTO_LOCK) |
| 1512 |
pni_mutex_lock(&locks[n]); |
| 1513 |
else |
| 1514 |
pni_mutex_unlock(&locks[n]); |
| 1515 |
} |
| 1516 |
|
| 1517 |
static void initialize(void) { |
| 1518 |
int i; |
| 1519 |
SSL_library_init(); |
| 1520 |
SSL_load_error_strings(); |
| 1521 |
OpenSSL_add_all_algorithms(); |
| 1522 |
tls_ex_data_index = SSL_get_ex_new_index( 0, (void *) "org.apache.qpid.proton.tls", |
| 1523 |
NULL, NULL, NULL); |
| 1524 |
ssn_init(); |
| 1525 |
locks = (pni_mutex_t*)malloc(CRYPTO_num_locks() * sizeof(pni_mutex_t)); |
| 1526 |
if (!locks) return; |
| 1527 |
for(i = 0; i < CRYPTO_num_locks(); i++) |
| 1528 |
pni_mutex_init(&locks[i]); |
| 1529 |
CRYPTO_set_id_callback(&id_callback); |
| 1530 |
CRYPTO_set_locking_callback(&locking_callback); |
| 1531 |
/* In recent versions of openssl, the set_callback functions are no-op macros, |
| 1532 |
so we need to take steps to stop the compiler complaining about unused functions. */ |
| 1533 |
(void)&id_callback; |
| 1534 |
(void)&locking_callback; |
| 1535 |
init_ok = true; |
| 1536 |
} |
| 1537 |
|
| 1538 |
/* TODO aconway 2017-10-16: There is no opportunity to clean up the locks as proton has no |
| 1539 |
final shut-down call. If it did, we should call this: */ |
| 1540 |
/* |
| 1541 |
static void shutdown(void) { |
| 1542 |
CRYPTO_set_id_callback(NULL); |
| 1543 |
CRYPTO_set_locking_callback(NULL); |
| 1544 |
if(locks) { |
| 1545 |
int i; |
| 1546 |
for(i = 0; i < CRYPTO_num_locks(); i++) |
| 1547 |
pni_mutex_destroy(&locks[i]); |
| 1548 |
free(locks); |
| 1549 |
locks = NULL; |
| 1550 |
} |
| 1551 |
} |
| 1552 |
*/ |
| 1553 |
|
| 1554 |
static void raw_buffer_to_pbuffer(pn_raw_buffer_t const* rbuf, pbuffer_t *pbuf, buff_type type) { |
| 1555 |
pbuf->context = rbuf->context; |
| 1556 |
pbuf->bytes = rbuf->bytes; |
| 1557 |
pbuf->capacity = rbuf->capacity; |
| 1558 |
pbuf->size = rbuf->size; |
| 1559 |
pbuf->offset = rbuf->offset; |
| 1560 |
pbuf->type = type; |
| 1561 |
} |
| 1562 |
|
| 1563 |
static void pbuffer_to_raw_buffer(pbuffer_t *pbuf, pn_raw_buffer_t *rbuf) { |
| 1564 |
rbuf->context = pbuf->context; |
| 1565 |
rbuf->bytes = pbuf->bytes; |
| 1566 |
rbuf->capacity = pbuf->capacity; |
| 1567 |
rbuf->size = pbuf->size; |
| 1568 |
rbuf->offset = pbuf->offset; |
| 1569 |
} |
| 1570 |
|
| 1571 |
|
| 1572 |
size_t pn_tls_give_encrypt_input_buffers(pn_tls_t* tls, pn_raw_buffer_t const* bufs, size_t count_bufs) { |
| 1573 |
assert(tls); |
| 1574 |
|
| 1575 |
if (tls->enc_closed || tls->stopped || tls->pn_tls_err) |
| 1576 |
return 0; |
| 1577 |
size_t can_take = pn_min(count_bufs, pn_tls_get_encrypt_input_buffer_capacity(tls)); |
| 1578 |
if ( can_take==0 ) return 0; |
| 1579 |
|
| 1580 |
buff_ptr current = tls->encrypt_first_empty; |
| 1581 |
assert(current); |
| 1582 |
|
| 1583 |
buff_ptr previous; |
| 1584 |
for (size_t i = 0; i < can_take; i++) { |
| 1585 |
// Get next free |
| 1586 |
assert(tls->encrypt_buffers[current-1].type == buff_empty); |
| 1587 |
raw_buffer_to_pbuffer(bufs + i, &tls->encrypt_buffers[current-1], buff_encrypt_pending); |
| 1588 |
previous = current; |
| 1589 |
current = tls->encrypt_buffers[current-1].next; |
| 1590 |
} |
| 1591 |
|
| 1592 |
if (!tls->encrypt_first_pending) { |
| 1593 |
tls->encrypt_first_pending = tls->encrypt_first_empty; |
| 1594 |
} |
| 1595 |
if (tls->encrypt_last_pending) { |
| 1596 |
tls->encrypt_buffers[tls->encrypt_last_pending-1].next = tls->encrypt_first_empty; |
| 1597 |
} |
| 1598 |
|
| 1599 |
tls->encrypt_last_pending = previous; |
| 1600 |
tls->encrypt_buffers[previous-1].next = 0; |
| 1601 |
tls->encrypt_first_empty = current; |
| 1602 |
|
| 1603 |
tls->encrypt_buffer_empty_count -= can_take; |
| 1604 |
if (tls->validating) validate_strict(tls); |
| 1605 |
return can_take; |
| 1606 |
} |
| 1607 |
|
| 1608 |
size_t pn_tls_give_decrypt_input_buffers(pn_tls_t* tls, pn_raw_buffer_t const* bufs, size_t count_bufs) { |
| 1609 |
assert(tls); |
| 1610 |
|
| 1611 |
if (tls->dec_closed || tls->stopped || tls->pn_tls_err) |
| 1612 |
return 0; |
| 1613 |
size_t can_take = pn_min(count_bufs, pn_tls_get_decrypt_input_buffer_capacity(tls)); |
| 1614 |
if ( can_take==0 ) return 0; |
| 1615 |
|
| 1616 |
buff_ptr current = tls->decrypt_first_empty; |
| 1617 |
assert(current); |
| 1618 |
|
| 1619 |
buff_ptr previous; |
| 1620 |
for (size_t i = 0; i < can_take; i++) { |
| 1621 |
// Get next free |
| 1622 |
assert(tls->decrypt_buffers[current-1].type == buff_empty); |
| 1623 |
raw_buffer_to_pbuffer(bufs + i, &tls->decrypt_buffers[current-1], buff_decrypt_pending); |
| 1624 |
previous = current; |
| 1625 |
current = tls->decrypt_buffers[current-1].next; |
| 1626 |
} |
| 1627 |
|
| 1628 |
if (!tls->decrypt_first_pending) { |
| 1629 |
tls->decrypt_first_pending = tls->decrypt_first_empty; |
| 1630 |
} |
| 1631 |
if (tls->decrypt_last_pending) { |
| 1632 |
tls->decrypt_buffers[tls->decrypt_last_pending-1].next = tls->decrypt_first_empty; |
| 1633 |
} |
| 1634 |
|
| 1635 |
tls->decrypt_last_pending = previous; |
| 1636 |
tls->decrypt_buffers[previous-1].next = 0; |
| 1637 |
tls->decrypt_first_empty = current; |
| 1638 |
|
| 1639 |
tls->decrypt_buffer_empty_count -= can_take; |
| 1640 |
if (tls->validating) validate_strict(tls); |
| 1641 |
return can_take; |
| 1642 |
} |
| 1643 |
|
| 1644 |
size_t pn_tls_give_encrypt_output_buffers(pn_tls_t* tls, pn_raw_buffer_t const* bufs, size_t count_bufs) { |
| 1645 |
assert(tls); |
| 1646 |
|
| 1647 |
size_t can_take = pn_min(count_bufs, pn_tls_get_encrypt_output_buffer_capacity(tls)); |
| 1648 |
if ( can_take==0 ) return 0; |
| 1649 |
|
| 1650 |
buff_ptr current = tls->eresult_first_empty; |
| 1651 |
assert(current); |
| 1652 |
|
| 1653 |
buff_ptr previous; |
| 1654 |
for (size_t i = 0; i < can_take; i++) { |
| 1655 |
// Get next free |
| 1656 |
assert(tls->eresult_buffers[current-1].type == buff_empty); |
| 1657 |
raw_buffer_to_pbuffer(bufs + i, &tls->eresult_buffers[current-1], buff_eresult_blank); |
| 1658 |
previous = current; |
| 1659 |
current = tls->eresult_buffers[current-1].next; |
| 1660 |
} |
| 1661 |
|
| 1662 |
tls->eresult_buffers[previous-1].next = tls->eresult_first_blank; |
| 1663 |
tls->eresult_first_blank = tls->eresult_first_empty; |
| 1664 |
tls->eresult_first_empty = current; |
| 1665 |
|
| 1666 |
tls->eresult_empty_count -= can_take; |
| 1667 |
if (tls->validating) validate_strict(tls); |
| 1668 |
return can_take; |
| 1669 |
} |
| 1670 |
|
| 1671 |
size_t pn_tls_give_decrypt_output_buffers(pn_tls_t* tls, pn_raw_buffer_t const* bufs, size_t count_bufs) { |
| 1672 |
assert(tls); |
| 1673 |
|
| 1674 |
size_t can_take = pn_min(count_bufs, pn_tls_get_decrypt_output_buffer_capacity(tls)); |
| 1675 |
if ( can_take==0 ) return 0; |
| 1676 |
|
| 1677 |
buff_ptr current = tls->dresult_first_empty; |
| 1678 |
assert(current); |
| 1679 |
|
| 1680 |
buff_ptr previous; |
| 1681 |
for (size_t i = 0; i < can_take; i++) { |
| 1682 |
// Get next free |
| 1683 |
assert(tls->dresult_buffers[current-1].type == buff_empty); |
| 1684 |
raw_buffer_to_pbuffer(bufs + i, &tls->dresult_buffers[current-1], buff_dresult_blank); |
| 1685 |
previous = current; |
| 1686 |
current = tls->dresult_buffers[current-1].next; |
| 1687 |
} |
| 1688 |
|
| 1689 |
tls->dresult_buffers[previous-1].next = tls->dresult_first_blank; |
| 1690 |
tls->dresult_first_blank = tls->dresult_first_empty; |
| 1691 |
tls->dresult_first_empty = current; |
| 1692 |
|
| 1693 |
tls->dresult_empty_count -= can_take; |
| 1694 |
if (tls->validating) validate_strict(tls); |
| 1695 |
return can_take; |
| 1696 |
} |
| 1697 |
|
| 1698 |
size_t pn_tls_take_decrypt_input_buffers(pn_tls_t *tls, pn_raw_buffer_t *buffers, size_t num) { |
| 1699 |
assert(tls); |
| 1700 |
size_t count = 0; |
| 1701 |
|
| 1702 |
buff_ptr current = tls->decrypt_first_done; |
| 1703 |
if (!current) return 0; |
| 1704 |
|
| 1705 |
buff_ptr previous; |
| 1706 |
for (; current && count < num; count++) { |
| 1707 |
assert(tls->decrypt_buffers[current-1].type == buff_decrypt_done); |
| 1708 |
pbuffer_to_raw_buffer(&tls->decrypt_buffers[current-1], buffers + count); |
| 1709 |
tls->decrypt_buffers[current-1].type = buff_empty; |
| 1710 |
|
| 1711 |
previous = current; |
| 1712 |
current = tls->decrypt_buffers[current-1].next; |
| 1713 |
} |
| 1714 |
if (!count) return 0; |
| 1715 |
|
| 1716 |
tls->decrypt_buffers[previous-1].next = tls->decrypt_first_empty; |
| 1717 |
tls->decrypt_first_empty = tls->decrypt_first_done; |
| 1718 |
|
| 1719 |
tls->decrypt_first_done = current; |
| 1720 |
if (!current) { |
| 1721 |
tls->decrypt_last_done = 0; |
| 1722 |
} |
| 1723 |
tls->decrypt_buffer_empty_count += count; |
| 1724 |
if (tls->validating) validate_strict(tls); |
| 1725 |
return count; |
| 1726 |
} |
| 1727 |
|
| 1728 |
size_t pn_tls_take_encrypt_input_buffers(pn_tls_t *tls, pn_raw_buffer_t *buffers, size_t num) { |
| 1729 |
assert(tls); |
| 1730 |
size_t count = 0; |
| 1731 |
|
| 1732 |
buff_ptr current = tls->encrypt_first_done; |
| 1733 |
if (!current) return 0; |
| 1734 |
|
| 1735 |
buff_ptr previous; |
| 1736 |
for (; current && count < num; count++) { |
| 1737 |
assert(tls->encrypt_buffers[current-1].type == buff_encrypt_done); |
| 1738 |
pbuffer_to_raw_buffer(&tls->encrypt_buffers[current-1], buffers + count); |
| 1739 |
tls->encrypt_buffers[current-1].type = buff_empty; |
| 1740 |
|
| 1741 |
previous = current; |
| 1742 |
current = tls->encrypt_buffers[current-1].next; |
| 1743 |
} |
| 1744 |
if (!count) return 0; |
| 1745 |
|
| 1746 |
tls->encrypt_buffers[previous-1].next = tls->encrypt_first_empty; |
| 1747 |
tls->encrypt_first_empty = tls->encrypt_first_done; |
| 1748 |
|
| 1749 |
tls->encrypt_first_done = current; |
| 1750 |
if (!current) { |
| 1751 |
tls->encrypt_last_done = 0; |
| 1752 |
} |
| 1753 |
tls->encrypt_buffer_empty_count += count; |
| 1754 |
if (tls->validating) validate_strict(tls); |
| 1755 |
return count; |
| 1756 |
} |
| 1757 |
|
| 1758 |
size_t pn_tls_take_decrypt_output_buffers(pn_tls_t *tls, pn_raw_buffer_t *buffers, size_t num) { |
| 1759 |
assert(tls); |
| 1760 |
size_t count = 0; |
| 1761 |
|
| 1762 |
buff_ptr current = tls->dresult_first_decrypted; |
| 1763 |
if (!current) return 0; |
| 1764 |
|
| 1765 |
buff_ptr previous; |
| 1766 |
for (; current && count < num; count++) { |
| 1767 |
assert(tls->dresult_buffers[current-1].type == buff_dresult_decrypted); |
| 1768 |
pbuffer_to_raw_buffer(&tls->dresult_buffers[current-1], buffers + count); |
| 1769 |
tls->dresult_buffers[current-1].type = buff_empty; |
| 1770 |
|
| 1771 |
previous = current; |
| 1772 |
current = tls->dresult_buffers[current-1].next; |
| 1773 |
} |
| 1774 |
if (!count) return 0; |
| 1775 |
|
| 1776 |
tls->dresult_buffers[previous-1].next = tls->dresult_first_empty; |
| 1777 |
tls->dresult_first_empty = tls->dresult_first_decrypted; |
| 1778 |
|
| 1779 |
tls->dresult_first_decrypted = current; |
| 1780 |
if (!current) { |
| 1781 |
tls->dresult_last_decrypted = 0; |
| 1782 |
} |
| 1783 |
tls->dresult_empty_count += count; |
| 1784 |
tls->dresult_decrypted_count -= count; |
| 1785 |
if (tls->validating) validate_strict(tls); |
| 1786 |
return count; |
| 1787 |
} |
| 1788 |
|
| 1789 |
size_t pn_tls_take_encrypt_output_buffers(pn_tls_t *tls, pn_raw_buffer_t *buffers, size_t num) { |
| 1790 |
assert(tls); |
| 1791 |
size_t count = 0; |
| 1792 |
|
| 1793 |
buff_ptr current = tls->eresult_first_encrypted; |
| 1794 |
if (!current) return 0; |
| 1795 |
|
| 1796 |
buff_ptr previous; |
| 1797 |
for (; current && count < num; count++) { |
| 1798 |
assert(tls->eresult_buffers[current-1].type == buff_eresult_encrypted); |
| 1799 |
pbuffer_to_raw_buffer(&tls->eresult_buffers[current-1], buffers + count); |
| 1800 |
tls->eresult_buffers[current-1].type = buff_empty; |
| 1801 |
|
| 1802 |
previous = current; |
| 1803 |
current = tls->eresult_buffers[current-1].next; |
| 1804 |
} |
| 1805 |
if (!count) return 0; |
| 1806 |
|
| 1807 |
tls->eresult_buffers[previous-1].next = tls->eresult_first_empty; |
| 1808 |
tls->eresult_first_empty = tls->eresult_first_encrypted; |
| 1809 |
|
| 1810 |
tls->eresult_first_encrypted = current; |
| 1811 |
if (!current) { |
| 1812 |
tls->eresult_last_encrypted = 0; |
| 1813 |
} |
| 1814 |
tls->eresult_empty_count += count; |
| 1815 |
tls->eresult_encrypted_count -= count; |
| 1816 |
if (tls->validating) validate_strict(tls); |
| 1817 |
return count; |
| 1818 |
} |
| 1819 |
|
| 1820 |
static pbuffer_t *next_encrypt_pending(pn_tls_t *tls) { |
| 1821 |
if (!tls->encrypt_first_pending) return NULL; |
| 1822 |
buff_ptr p = tls->encrypt_first_pending; |
| 1823 |
pbuffer_t *current = &tls->encrypt_buffers[p-1]; |
| 1824 |
if (current->size > tls->encrypt_pending_offset) |
| 1825 |
return current; |
| 1826 |
// current is full, convert from pending to done |
| 1827 |
assert(current->type == buff_encrypt_pending); |
| 1828 |
tls->encrypt_pending_offset = 0; |
| 1829 |
if (!tls->encrypt_first_done) { |
| 1830 |
tls->encrypt_first_done = p; |
| 1831 |
} |
| 1832 |
if (tls->encrypt_last_done) { |
| 1833 |
tls->encrypt_buffers[tls->encrypt_last_done-1].next = p; |
| 1834 |
} |
| 1835 |
tls->encrypt_last_done = p; |
| 1836 |
tls->encrypt_first_pending = current->next; |
| 1837 |
if (!tls->encrypt_first_pending) |
| 1838 |
tls->encrypt_last_pending = 0; |
| 1839 |
current->next = 0; |
| 1840 |
current->type = buff_encrypt_done; |
| 1841 |
|
| 1842 |
// Advance |
| 1843 |
p = tls->encrypt_first_pending; |
| 1844 |
pbuffer_t *next = p ? &tls->encrypt_buffers[p-1] : NULL; |
| 1845 |
assert(!next || next->type == buff_encrypt_pending); |
| 1846 |
|
| 1847 |
return next; |
| 1848 |
} |
| 1849 |
|
| 1850 |
static pbuffer_t *next_decrypt_pending(pn_tls_t *tls) { |
| 1851 |
if (!tls->decrypt_first_pending) return NULL; |
| 1852 |
buff_ptr p = tls->decrypt_first_pending; |
| 1853 |
pbuffer_t *current = &tls->decrypt_buffers[p-1]; |
| 1854 |
if (current->size > tls->decrypt_pending_offset) |
| 1855 |
return current; |
| 1856 |
// current is full, convert from pending to done |
| 1857 |
assert(current->type == buff_decrypt_pending); |
| 1858 |
tls->decrypt_pending_offset = 0; |
| 1859 |
if (!tls->decrypt_first_done) { |
| 1860 |
tls->decrypt_first_done = p; |
| 1861 |
} |
| 1862 |
if (tls->decrypt_last_done) { |
| 1863 |
tls->decrypt_buffers[tls->decrypt_last_done-1].next = p; |
| 1864 |
} |
| 1865 |
tls->decrypt_last_done = p; |
| 1866 |
tls->decrypt_first_pending = current->next; |
| 1867 |
if (!tls->decrypt_first_pending) |
| 1868 |
tls->decrypt_last_pending = 0; |
| 1869 |
current->next = 0; |
| 1870 |
current->type = buff_decrypt_done; |
| 1871 |
|
| 1872 |
// Advance |
| 1873 |
p = tls->decrypt_first_pending; |
| 1874 |
pbuffer_t *next = p ? &tls->decrypt_buffers[p-1] : NULL; |
| 1875 |
assert(!next || next->type == buff_decrypt_pending); |
| 1876 |
|
| 1877 |
return next; |
| 1878 |
} |
| 1879 |
|
| 1880 |
static void blank_eresult_pop(pn_tls_t *tls, buff_ptr p) { |
| 1881 |
assert (p && p == tls->eresult_first_blank); // From front only. |
| 1882 |
tls->eresult_first_blank = tls->eresult_buffers[p-1].next; |
| 1883 |
tls->eresult_buffers[p-1].next = 0; |
| 1884 |
} |
| 1885 |
|
| 1886 |
static void blank_dresult_pop(pn_tls_t *tls, buff_ptr p) { |
| 1887 |
assert (p && p == tls->dresult_first_blank); // From front only. |
| 1888 |
tls->dresult_first_blank = tls->dresult_buffers[p-1].next; |
| 1889 |
tls->dresult_buffers[p-1].next = 0; |
| 1890 |
} |
| 1891 |
|
| 1892 |
static void encrypted_result_add(pn_tls_t *tls, buff_ptr p) { |
| 1893 |
tls->eresult_buffers[p-1].type = buff_eresult_encrypted; |
| 1894 |
if (!tls->eresult_first_encrypted) |
| 1895 |
tls->eresult_first_encrypted = p; |
| 1896 |
if (tls->eresult_last_encrypted) |
| 1897 |
tls->eresult_buffers[tls->eresult_last_encrypted-1].next = p; |
| 1898 |
tls->eresult_last_encrypted = p; |
| 1899 |
tls->eresult_encrypted_count++; |
| 1900 |
} |
| 1901 |
|
| 1902 |
static void decrypted_result_add(pn_tls_t *tls, buff_ptr p) { |
| 1903 |
tls->dresult_buffers[p-1].type = buff_dresult_decrypted; |
| 1904 |
if (!tls->dresult_first_decrypted) |
| 1905 |
tls->dresult_first_decrypted = p; |
| 1906 |
if (tls->dresult_last_decrypted) |
| 1907 |
tls->dresult_buffers[tls->dresult_last_decrypted-1].next = p; |
| 1908 |
tls->dresult_last_decrypted = p; |
| 1909 |
tls->dresult_decrypted_count++; |
| 1910 |
} |
| 1911 |
|
| 1912 |
static buff_ptr current_encrypted_result(pn_tls_t *tls) { |
| 1913 |
buff_ptr p = tls->eresult_last_encrypted; |
| 1914 |
if (p && room(&tls->eresult_buffers[p-1])) |
| 1915 |
return p; // Has room so keep filling. |
| 1916 |
// Otherwise, use a blank reult if available. |
| 1917 |
return tls->eresult_first_blank; |
| 1918 |
} |
| 1919 |
|
| 1920 |
static buff_ptr current_decrypted_result(pn_tls_t *tls) { |
| 1921 |
buff_ptr p = tls->dresult_last_decrypted; |
| 1922 |
if (p && room(&tls->dresult_buffers[p-1])) |
| 1923 |
return p; // Has room so keep filling. |
| 1924 |
// Otherwise, use a blank reult if available. |
| 1925 |
return tls->dresult_first_blank; |
| 1926 |
} |
| 1927 |
|
| 1928 |
static bool try_shutdown(pn_tls_t *tls) { |
| 1929 |
assert(tls->enc_closed && !tls->encrypt_first_pending && !tls->ssl_shutdown); |
| 1930 |
bool success = false; |
| 1931 |
if (tls->can_shutdown) { |
| 1932 |
int shutdown_ret = SSL_shutdown(tls->ssl); |
| 1933 |
if (shutdown_ret < 0) { |
| 1934 |
int reason = SSL_get_error(tls->ssl, shutdown_ret); |
| 1935 |
switch (reason) { |
| 1936 |
case SSL_ERROR_WANT_READ: |
| 1937 |
case SSL_ERROR_WANT_WRITE: |
| 1938 |
// Insufficient space. Try again later after extracting from BIO. |
| 1939 |
break; |
| 1940 |
default: |
| 1941 |
tls_fatal(tls, reason, PN_TLS_PROTOCOL_ERR); |
| 1942 |
break; |
| 1943 |
} |
| 1944 |
} else { |
| 1945 |
success = true; |
| 1946 |
tls->ssl_shutdown = true; |
| 1947 |
tls->enc_rblocked = false; |
| 1948 |
} |
| 1949 |
} |
| 1950 |
return success; |
| 1951 |
} |
| 1952 |
|
| 1953 |
static void encrypt(pn_tls_t *tls) { |
| 1954 |
assert(tls); |
| 1955 |
buff_ptr curr_result = current_encrypted_result(tls); |
| 1956 |
pbuffer_t *pending = next_encrypt_pending(tls); |
| 1957 |
bool try_shutdown_again = false; |
| 1958 |
|
| 1959 |
while (true) { |
| 1960 |
// Insert unencrypted data into BIO. |
| 1961 |
// OpenSSL maps each BIO_write to a separate TLS record. |
| 1962 |
// The BIO can take 16KB + a bit before blocking. |
| 1963 |
// TODO: consider allowing application to configure BIO buffer size on encrypt side. |
| 1964 |
while (pending && !tls->enc_wblocked && tls->can_shutdown && !tls->pn_tls_err) { |
| 1965 |
size_t n = pending->size - tls->encrypt_pending_offset; |
| 1966 |
if (n) { |
| 1967 |
char *bytes = pending->bytes + pending->offset + tls->encrypt_pending_offset; |
| 1968 |
int wcount = BIO_write(tls->bio_ssl, bytes, n); |
| 1969 |
if (wcount < (int) n) |
| 1970 |
tls->enc_wblocked = true; |
| 1971 |
if (wcount > 0) { |
| 1972 |
tls->enc_rblocked = false; |
| 1973 |
tls->encrypt_pending_offset += wcount; |
| 1974 |
} |
| 1975 |
} |
| 1976 |
pending = next_encrypt_pending(tls); |
| 1977 |
} |
| 1978 |
|
| 1979 |
// Finished writing? |
| 1980 |
if (tls->enc_closed && !pending && !tls->ssl_shutdown) { |
| 1981 |
try_shutdown_again = !try_shutdown(tls); // may need multple tries if BIO buffers need draining |
| 1982 |
} |
| 1983 |
|
| 1984 |
// Extract encrypted data from other side of BIO. |
| 1985 |
while (curr_result && !tls->enc_rblocked) { |
| 1986 |
pbuffer_t *result = &tls->eresult_buffers[curr_result-1]; |
| 1987 |
size_t n = room(result); |
| 1988 |
assert(n); |
| 1989 |
int rcount = BIO_read(tls->bio_net_io, result->bytes + result->offset + result->size, n); |
| 1990 |
if (rcount < (int) n) |
| 1991 |
tls->enc_rblocked = true; |
| 1992 |
if (rcount > 0) { |
| 1993 |
tls->write_blocked = false; |
| 1994 |
if (!tls->pn_tls_err) |
| 1995 |
tls->enc_wblocked = false; |
| 1996 |
if (result->size == 0) { |
| 1997 |
// first data inserted: convert from blank type to encrypted type |
| 1998 |
assert(result->type == buff_eresult_blank); |
| 1999 |
blank_eresult_pop(tls, curr_result); |
| 2000 |
encrypted_result_add(tls, curr_result); |
| 2001 |
} |
| 2002 |
result->size += rcount; |
| 2003 |
if (room(result) == 0) { |
| 2004 |
// Tentatively set a blank buffer for future output without popping. |
| 2005 |
curr_result = tls->eresult_first_blank; |
| 2006 |
} |
| 2007 |
if (try_shutdown_again) |
| 2008 |
try_shutdown_again = !try_shutdown(tls); |
| 2009 |
} else if (!BIO_should_retry(tls->bio_ssl)) { |
| 2010 |
int reason = SSL_get_error( tls->ssl, rcount ); |
| 2011 |
switch (reason) { |
| 2012 |
case SSL_ERROR_ZERO_RETURN: |
| 2013 |
// SSL closed cleanly |
| 2014 |
ssl_log(NULL, PN_LEVEL_TRACE, "SSL connection has closed"); |
| 2015 |
// TODO: replacement for: start_ssl_shutdown(transport); // KAG: not sure - this may not be necessary |
| 2016 |
tls->dec_closed = true; |
| 2017 |
tls->ssl_closed = true; // TODO: still true? |
| 2018 |
break; |
| 2019 |
default: |
| 2020 |
tls_fatal(tls, reason, PN_TLS_PROTOCOL_ERR); |
| 2021 |
} |
| 2022 |
} |
| 2023 |
} |
| 2024 |
|
| 2025 |
// Done if output buffers exhausted or all available encrypted bytes drained from BIO. |
| 2026 |
if (!curr_result || tls->enc_rblocked) |
| 2027 |
break; |
| 2028 |
} |
| 2029 |
} |
| 2030 |
|
| 2031 |
static void decrypt(pn_tls_t *tls) { |
| 2032 |
assert(tls); |
| 2033 |
buff_ptr curr_result = current_decrypted_result(tls); |
| 2034 |
pbuffer_t *pending = next_decrypt_pending(tls); |
| 2035 |
|
| 2036 |
while (true) { |
| 2037 |
if (tls->pn_tls_err) |
| 2038 |
return; |
| 2039 |
|
| 2040 |
// Insert encrypted data into openssl filter chain. |
| 2041 |
while (pending && !tls->dec_wblocked && !tls->dec_closed) { |
| 2042 |
size_t n = pending->size - tls->decrypt_pending_offset; |
| 2043 |
if (n) { |
| 2044 |
char *bytes = pending->bytes + pending->offset + tls->decrypt_pending_offset; |
| 2045 |
int wcount = BIO_write(tls->bio_net_io, bytes, n); |
| 2046 |
if (wcount < (int) n) |
| 2047 |
tls->dec_wblocked = true; |
| 2048 |
if (wcount > 0) { |
| 2049 |
if (!tls->dec_closed) |
| 2050 |
tls->dec_rblocked = false; |
| 2051 |
tls->enc_rblocked = false; |
| 2052 |
tls->decrypt_pending_offset += wcount; |
| 2053 |
tls->dec_stale = true; // new write side content, no read side calculation yet. |
| 2054 |
// tls_trace_callback("tls decrypt: scanned %d bytes", wcount);) |
| 2055 |
} |
| 2056 |
} |
| 2057 |
pending = next_decrypt_pending(tls); |
| 2058 |
} |
| 2059 |
|
| 2060 |
// Extract decrypted data from other side of filter chain. |
| 2061 |
while (curr_result && !tls->dec_rblocked) { |
| 2062 |
pbuffer_t *result = &tls->dresult_buffers[curr_result-1]; |
| 2063 |
size_t n = room(result); |
| 2064 |
assert(n); |
| 2065 |
int rcount = BIO_read(tls->bio_ssl, result->bytes + result->offset + result->size, n); |
| 2066 |
if (rcount > 0) { |
| 2067 |
tls->dec_wblocked = false; |
| 2068 |
tls->dec_stale = false; |
| 2069 |
if (result->size == 0) { |
| 2070 |
// first data inserted: convert from blank type to encrypted type |
| 2071 |
assert(result->type == buff_dresult_blank); |
| 2072 |
blank_dresult_pop(tls, curr_result); |
| 2073 |
decrypted_result_add(tls, curr_result); |
| 2074 |
} |
| 2075 |
result->size += rcount; |
| 2076 |
if (room(result) == 0) { |
| 2077 |
// Tentatively set a blank buffer for future output without popping. |
| 2078 |
curr_result = tls->dresult_first_blank; |
| 2079 |
} |
| 2080 |
} else { |
| 2081 |
if (!BIO_should_retry(tls->bio_ssl)) { |
| 2082 |
int reason = SSL_get_error( tls->ssl, rcount ); |
| 2083 |
switch (reason) { |
| 2084 |
case SSL_ERROR_ZERO_RETURN: |
| 2085 |
// SSL closed cleanly |
| 2086 |
ssl_log(NULL, PN_LEVEL_TRACE, "SSL connection has closed"); |
| 2087 |
tls->dec_closed = true; |
| 2088 |
tls->dec_rblocked = true; |
| 2089 |
break; |
| 2090 |
default: |
| 2091 |
tls_fatal(tls, reason, PN_TLS_PROTOCOL_ERR); |
| 2092 |
break; |
| 2093 |
} |
| 2094 |
} else { |
| 2095 |
if (rcount == -1 && BIO_should_read(tls->bio_ssl)) |
| 2096 |
tls->dec_rblocked = true; |
| 2097 |
} |
| 2098 |
} |
| 2099 |
} |
| 2100 |
|
| 2101 |
// Done if outbufs exhausted or all inbufs decrypted |
| 2102 |
if (!curr_result || tls->dec_rblocked || tls->dec_closed) |
| 2103 |
break; |
| 2104 |
} |
| 2105 |
|
| 2106 |
// SSL_do_handshake() to see if handshaking is done but also to force handshake bytes into the BIO. |
| 2107 |
if (!tls->handshake_ok && SSL_do_handshake(tls->ssl) == 1) { |
| 2108 |
tls->handshake_ok = true; |
| 2109 |
tls->can_shutdown = true; |
| 2110 |
} |
| 2111 |
|
| 2112 |
if (tls->dec_stale) { |
| 2113 |
// Force OpenSSL to process "enough" buffered content to correctly answer if BIO_pending(tls->bio_ssl) > 0. |
| 2114 |
char unused; |
| 2115 |
SSL_peek(tls->ssl, &unused, 1); |
| 2116 |
tls->dec_stale = false; |
| 2117 |
} |
| 2118 |
} |
| 2119 |
|
| 2120 |
int pn_tls_process(pn_tls_t* tls) { |
| 2121 |
if (!tls->started || tls->stopped) |
| 2122 |
return PN_TLS_STATE_ERR; |
| 2123 |
|
| 2124 |
if (!tls->pn_tls_err) { |
| 2125 |
decrypt(tls); // Do this first. May generate handshake or other on encrypt side. |
| 2126 |
if (tls->validating) validate_strict(tls); |
| 2127 |
} |
| 2128 |
// We keep sending if there is a "minor" error that may result in an error message for the peer |
| 2129 |
if (!(tls->pn_tls_err && (tls->openssl_err_type == SSL_ERROR_SYSCALL || tls->openssl_err_type == SSL_ERROR_SSL))) { |
| 2130 |
encrypt(tls); |
| 2131 |
if (tls->validating) validate_strict(tls); |
| 2132 |
} |
| 2133 |
return(tls->pn_tls_err); |
| 2134 |
} |
| 2135 |
|
| 2136 |
bool pn_tls_need_encrypt_output_buffers(pn_tls_t* tls) { |
| 2137 |
if (tls && tls->started && !tls->stopped && tls->eresult_empty_count) { |
| 2138 |
// We keep sending if there is a "minor" error that may result in an error message for the peer |
| 2139 |
if (!(tls->pn_tls_err && (tls->openssl_err_type == SSL_ERROR_SYSCALL || tls->openssl_err_type == SSL_ERROR_SSL))) { |
| 2140 |
if (!current_encrypted_result(tls)) { |
| 2141 |
// Existing result buffers all full. Check if OpenSSL has data to read. |
| 2142 |
return (BIO_pending(tls->bio_net_io) > 0); |
| 2143 |
} |
| 2144 |
} |
| 2145 |
} |
| 2146 |
return false; |
| 2147 |
} |
| 2148 |
|
| 2149 |
bool pn_tls_need_decrypt_output_buffers(pn_tls_t* tls) { |
| 2150 |
if (tls && tls->started && !tls->stopped && tls->dresult_empty_count && !tls->dec_closed && !tls->pn_tls_err) { |
| 2151 |
if (!current_decrypted_result(tls)) { |
| 2152 |
// Existing result buffers all full. Check if OpenSSL has data to read. |
| 2153 |
return (BIO_pending(tls->bio_ssl) > 0); |
| 2154 |
} |
| 2155 |
} |
| 2156 |
return false; |
| 2157 |
} |
| 2158 |
|
| 2159 |
bool pn_tls_is_encrypt_output_pending(pn_tls_t *tls) |
| 2160 |
{ |
| 2161 |
if (tls && tls->started && !tls->stopped) { |
| 2162 |
if (!(tls->pn_tls_err && (tls->openssl_err_type == SSL_ERROR_SYSCALL || tls->openssl_err_type == SSL_ERROR_SSL))) |
| 2163 |
return tls->eresult_first_encrypted || (BIO_pending(tls->bio_net_io) > 0); |
| 2164 |
} |
| 2165 |
return false; |
| 2166 |
} |
| 2167 |
|
| 2168 |
bool pn_tls_is_decrypt_output_pending(pn_tls_t *tls) |
| 2169 |
{ |
| 2170 |
if (tls && tls->started && !tls->stopped &&!tls->dec_closed && !tls->pn_tls_err) { |
| 2171 |
return tls->dresult_first_decrypted || (BIO_pending(tls->bio_ssl) > 0); |
| 2172 |
} |
| 2173 |
return false; |
| 2174 |
} |
| 2175 |
|
| 2176 |
|
| 2177 |
void pn_tls_set_encrypt_input_buffer_max_capacity(pn_tls_t *tls, size_t s) { |
| 2178 |
if (!tls->started) tls->encrypt_buffer_count = s; |
| 2179 |
} |
| 2180 |
void pn_tls_set_decrypt_input_buffer_max_capacity(pn_tls_t *tls, size_t s) { |
| 2181 |
if (!tls->started) tls->decrypt_buffer_count = s; |
| 2182 |
} |
| 2183 |
void pn_tls_set_encrypt_output_buffer_max_capacity(pn_tls_t *tls, size_t s) { |
| 2184 |
if (!tls->started) tls->eresult_buffer_count = s; |
| 2185 |
} |
| 2186 |
void pn_tls_set_decrypt_output_buffer_max_capacity(pn_tls_t *tls, size_t s) { |
| 2187 |
if (!tls->started) tls->dresult_buffer_count = s; |
| 2188 |
} |
| 2189 |
|
| 2190 |
bool pn_tls_is_input_closed(pn_tls_t* tls) { |
| 2191 |
return tls->dec_closed; |
| 2192 |
} |
| 2193 |
|
| 2194 |
void pn_tls_close_output(pn_tls_t* tls) { |
| 2195 |
if (!tls->enc_closed) { |
| 2196 |
tls->enc_closed = true; |
| 2197 |
} |
| 2198 |
} |
| 2199 |
|
| 2200 |
// ======== ALPN ======== |
| 2201 |
|
| 2202 |
// returns false if invalid input strings, wire_bytes is NULL if no memory. |
| 2203 |
static bool strings_to_wire_list(const char **protocols, size_t protocol_count, unsigned char **wire_bytes, size_t *wb_len) { |
| 2204 |
// First pass: validate and count len. |
| 2205 |
size_t total_len = 0; |
| 2206 |
size_t l; |
| 2207 |
if (protocol_count == 0) |
| 2208 |
return false; |
| 2209 |
for (size_t i = 0; i < protocol_count; i++) { |
| 2210 |
if (protocols[i] == NULL) return false; |
| 2211 |
l = strnlen(protocols[i], 255); |
| 2212 |
if (l == 0 || (l == 255 && protocols[i][255])) |
| 2213 |
return false; // 0 or > 255 in length |
| 2214 |
total_len += l; |
| 2215 |
} |
| 2216 |
total_len += protocol_count; // account for unsigned char length indicator for each string |
| 2217 |
if (total_len > 65535) return false; |
| 2218 |
|
| 2219 |
|
| 2220 |
|
| 2221 |
// validated |
| 2222 |
*wire_bytes = (unsigned char *) malloc(total_len); |
| 2223 |
if (!*wire_bytes) return true; |
| 2224 |
|
| 2225 |
*wb_len = total_len; |
| 2226 |
unsigned char *p = *wire_bytes; |
| 2227 |
|
| 2228 |
for (size_t i = 0; i < protocol_count; i++) { |
| 2229 |
l = strnlen(protocols[i], 255); |
| 2230 |
*p++ = (unsigned char) l; |
| 2231 |
memmove(p, protocols[i], l); |
| 2232 |
p += l; |
| 2233 |
} |
| 2234 |
return true; |
| 2235 |
} |
| 2236 |
|
| 2237 |
// Callback from openssl on receipt of client_hello containing ALPN TLS extension. |
| 2238 |
static int pn_tls_alpn_cb(SSL *ssn, |
| 2239 |
const unsigned char **out, |
| 2240 |
unsigned char *outlen, |
| 2241 |
const unsigned char *in, |
| 2242 |
unsigned int inlen, |
| 2243 |
void *arg) { |
| 2244 |
pn_tls_t *tls = (pn_tls_t *)SSL_get_ex_data(ssn, tls_ex_data_index); |
| 2245 |
if (!tls) { |
| 2246 |
// TODO: log it. Should never happen. |
| 2247 |
return SSL_TLSEXT_ERR_ALERT_FATAL; // fail negotiation |
| 2248 |
} |
| 2249 |
assert(tls->mode == PN_TLS_MODE_SERVER); |
| 2250 |
if (!tls->alpn_list) { |
| 2251 |
return SSL_TLSEXT_ERR_NOACK; // No ALPN configured on server, ignored. |
| 2252 |
} |
| 2253 |
|
| 2254 |
unsigned char *proto_out; |
| 2255 |
unsigned char proto_outlen; |
| 2256 |
if (SSL_select_next_proto(&proto_out, &proto_outlen, tls->alpn_list, tls->alpn_list_len, in, inlen) |
| 2257 |
== OPENSSL_NPN_NO_OVERLAP) { |
| 2258 |
return SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2259 |
} |
| 2260 |
|
| 2261 |
// proto_out points into either tls->alpn_list (the server list) or into "in" (the client |
| 2262 |
// list). The former survives the life of the tls session. The latter is allowed to be set |
| 2263 |
// to "out" according to the documentation for cb in SSL_CTX_set_alpn_select_cb(). Either |
| 2264 |
// way, proto_out needs no copying. Just some casting. |
| 2265 |
|
| 2266 |
*out = (const unsigned char *) proto_out; |
| 2267 |
*outlen = proto_outlen; |
| 2268 |
return SSL_TLSEXT_ERR_OK;; |
| 2269 |
} |
| 2270 |
|
| 2271 |
int pn_tls_config_set_alpn_protocols(pn_tls_config_t *domain, const char **protocols, size_t protocol_count) { |
| 2272 |
unsigned char *wire_bytes; |
| 2273 |
size_t wb_len; |
| 2274 |
if ((protocols == NULL && protocol_count != 0) || (protocols && protocol_count == 0)) |
| 2275 |
return PN_ARG_ERR; |
| 2276 |
if (protocols == NULL && protocol_count == 0) { |
| 2277 |
free(domain->alpn_list); |
| 2278 |
domain->alpn_list = NULL; |
| 2279 |
domain->alpn_list_len = 0; |
| 2280 |
} else { |
| 2281 |
if (!strings_to_wire_list(protocols, protocol_count, &wire_bytes, &wb_len)) |
| 2282 |
return PN_ARG_ERR; |
| 2283 |
if (!wire_bytes) |
| 2284 |
return PN_OUT_OF_MEMORY; |
| 2285 |
free(domain->alpn_list); |
| 2286 |
domain->alpn_list = wire_bytes; |
| 2287 |
domain->alpn_list_len = wb_len; |
| 2288 |
} |
| 2289 |
// Never turn off the callback in case outstanding TLS objects to be started. |
| 2290 |
if (domain->alpn_list && domain->mode == PN_TLS_MODE_SERVER) |
| 2291 |
SSL_CTX_set_alpn_select_cb(domain->ctx, pn_tls_alpn_cb, NULL); |
| 2292 |
|
| 2293 |
return 0; |
| 2294 |
// free on domain free, copy on ssl creation |
| 2295 |
} |
| 2296 |
|
| 2297 |
bool pn_tls_get_alpn_protocol(pn_tls_t *tls, const char **protocol_name, size_t *size) { |
| 2298 |
if (tls) { |
| 2299 |
const unsigned char *proto = NULL; |
| 2300 |
unsigned int proto_len = 0; |
| 2301 |
SSL_get0_alpn_selected(tls->ssl, &proto, &proto_len); |
| 2302 |
if (proto_len) { |
| 2303 |
*protocol_name = (const char *) proto; |
| 2304 |
*size = (size_t) proto_len; |
| 2305 |
return true; |
| 2306 |
} |
| 2307 |
} |
| 2308 |
*protocol_name = NULL; |
| 2309 |
*size = 0; |
| 2310 |
return false; |
| 2311 |
} |