Skip to content
Navigation Menu
{{ message }}
forked from Instagram/cpp-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssl_openssl_impl.cpp
More file actions
496 lines (413 loc) · 13.5 KB
/
Copy pathssl_openssl_impl.cpp
File metadata and controls
496 lines (413 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/*
Copyright (c) 2014-2016 DataStax
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "ssl.hpp"
#include "logger.hpp"
#include "ssl/ring_buffer_bio.hpp"
#include "string_ref.hpp"
#include "utils.hpp"
#include <openssl/crypto.h>
#include <openssl/err.h>
#include <openssl/x509v3.h>
#include <string.h>
#define DEBUG_SSL 0
namespace cass {
#if DEBUG_SSL
#define SSL_PRINT_INFO(ssl, w, flag, msg) do { \
if (w & flag) { \
fprintf(stderr, "%s - %s - %s\n", \
msg, \
SSL_state_string(ssl), \
SSL_state_string_long(ssl)); \
} \
} while (0);
static void ssl_info_callback(const SSL* ssl, int where, int ret) {
if (ret == 0) {
fprintf(stderr, "ssl_info_callback, error occurred.\n");
return;
}
SSL_PRINT_INFO(ssl, where, SSL_CB_LOOP, "LOOP");
SSL_PRINT_INFO(ssl, where, SSL_CB_EXIT, "EXIT");
SSL_PRINT_INFO(ssl, where, SSL_CB_READ, "READ");
SSL_PRINT_INFO(ssl, where, SSL_CB_WRITE, "WRITE");
SSL_PRINT_INFO(ssl, where, SSL_CB_ALERT, "ALERT");
SSL_PRINT_INFO(ssl, where, SSL_CB_HANDSHAKE_DONE, "HANDSHAKE DONE");
}
#undef SSL_PRINT_INFO
#endif
static int ssl_no_verify_callback(int ok, X509_STORE_CTX* store) {
// Verification happens after in SslSession::verify()
// via SSL_get_verify_result().
return 1;
}
static void ssl_log_errors(const char* context) {
const char* data;
int flags;
int err;
while ((err = ERR_get_error_line_data(NULL, NULL, &data, &flags)) != 0) {
char buf[256];
ERR_error_string_n(err, buf, sizeof(buf));
LOG_ERROR("%s: %s:%s", context, buf, (flags & ERR_TXT_STRING) ? data : "");
}
ERR_print_errors_fp(stderr);
}
static std::string ssl_error_string() {
const char* data;
int flags;
int err;
std::string error;
while ((err = ERR_get_error_line_data(NULL, NULL, &data, &flags)) != 0) {
char buf[256];
ERR_error_string_n(err, buf, sizeof(buf));
if (!error.empty()) error.push_back(',');
error.append(buf);
if (flags & ERR_TXT_STRING) {
error.push_back(':');
error.append(data);
}
}
return error;
}
static int pem_password_callback(char* buf, int size, int rwflag, void* u) {
if (u == NULL) return 0;
int len = strlen(static_cast<const char*>(u));
if (len == 0) return 0;
int to_copy = size;
if (len < to_copy) {
to_copy = len;
}
memcpy(buf, u, to_copy);
return len;
}
static uv_rwlock_t* crypto_locks;
static void crypto_locking_callback(int mode, int n, const char* file, int line) {
if (mode & CRYPTO_LOCK) {
if (mode & CRYPTO_READ) {
uv_rwlock_rdlock(crypto_locks + n);
} else {
uv_rwlock_wrlock(crypto_locks + n);
}
} else {
if (mode & CRYPTO_READ) {
uv_rwlock_rdunlock(crypto_locks + n);
} else {
uv_rwlock_wrunlock(crypto_locks + n);
}
}
}
static unsigned long crypto_id_callback() {
#if UV_VERSION_MAJOR == 0
return uv_thread_self();
#elif defined(WIN32) || defined(_WIN32)
return static_cast<unsigned long>(GetCurrentThreadId());
#else
return copy_cast<uv_thread_t, unsigned long>(uv_thread_self());
#endif
}
// Implementation taken from OpenSSL's SSL_CTX_use_certificate_chain_file()
// (https://github.com/openssl/openssl/blob/OpenSSL_0_9_8-stable/ssl/ssl_rsa.c#L705).
// Modified to be used for in-memory certificate chains and formatting.
static int SSL_CTX_use_certificate_chain_bio(SSL_CTX* ctx, BIO* in) {
int ret = 0;
X509* x = NULL;
x = PEM_read_bio_X509_AUX(in, NULL, pem_password_callback, NULL);
if (x == NULL) {
SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE,ERR_R_PEM_LIB);
goto end;
}
ret = SSL_CTX_use_certificate(ctx, x);
if (ERR_peek_error() != 0) {
// Key/certificate mismatch doesn't imply ret==0 ...
ret = 0;
}
if (ret) {
// If we could set up our certificate, now proceed to
// the CA certificates.
X509 *ca;
int r;
unsigned long err;
if (ctx->extra_certs != NULL) {
sk_X509_pop_free(ctx->extra_certs, X509_free);
ctx->extra_certs = NULL;
}
while ((ca = PEM_read_bio_X509(in, NULL, pem_password_callback, NULL)) != NULL) {
r = SSL_CTX_add_extra_chain_cert(ctx, ca);
if (!r) {
X509_free(ca);
ret = 0;
goto end;
}
// Note that we must not free r if it was successfully
// added to the chain (while we must free the main
// certificate, since its reference count is increased
// by SSL_CTX_use_certificate).
}
// When the while loop ends, it's usually just EOF.
err = ERR_peek_last_error();
if (ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE) {
ERR_clear_error();
} else {
// Some real error
ret = 0;
}
}
end:
if (x != NULL) X509_free(x);
return ret;
}
static X509* load_cert(const char* cert, size_t cert_size) {
BIO* bio = BIO_new_mem_buf(const_cast<char*>(cert), cert_size);
if (bio == NULL) {
return NULL;
}
X509* x509 = PEM_read_bio_X509(bio, NULL, pem_password_callback, NULL);
if (x509 == NULL) {
ssl_log_errors("Unable to load certificate");
}
BIO_free_all(bio);
return x509;
}
static EVP_PKEY* load_key(const char* key,
size_t key_size,
const char* password) {
BIO* bio = BIO_new_mem_buf(const_cast<char*>(key), key_size);
if (bio == NULL) {
return NULL;
}
EVP_PKEY* pkey = PEM_read_bio_PrivateKey(bio,
NULL,
pem_password_callback,
const_cast<char*>(password));
if (pkey == NULL) {
ssl_log_errors("Unable to load private key");
}
BIO_free_all(bio);
return pkey;
}
class OpenSslVerifyIdentity {
public:
enum Result {
INVALID_CERT,
MATCH,
NO_MATCH,
NO_SAN_PRESENT
};
static Result match(X509* cert, const Address& addr) {
Result result = match_subject_alt_names(cert, addr);
if (result == NO_SAN_PRESENT) {
result = match_common_name(cert, addr);
}
return result;
}
private:
static Result match_common_name(X509* cert, const Address& addr) {
std::string addr_str = addr.to_string();
X509_NAME* name = X509_get_subject_name(cert);
if (name == NULL) {
return INVALID_CERT;
}
int i = -1;
while ((i = X509_NAME_get_index_by_NID(name, NID_commonName, i)) > 0) {
X509_NAME_ENTRY* name_entry = X509_NAME_get_entry(name, i);
if (name_entry == NULL) {
return INVALID_CERT;
}
ASN1_STRING* str = X509_NAME_ENTRY_get_data(name_entry);
StringRef common_name(reinterpret_cast<char*>(ASN1_STRING_data(str)), ASN1_STRING_length(str));
if (iequals(common_name, addr_str)) {
return MATCH;
}
}
return NO_MATCH;
}
static Result match_subject_alt_names(X509* cert, const Address& addr) {
char addr_buf[16];
size_t addr_buf_size;
if (addr.family() == AF_INET) {
addr_buf_size = 4;
memcpy(addr_buf, &addr.addr_in()->sin_addr.s_addr, addr_buf_size);
} else {
addr_buf_size = 16;
memcpy(addr_buf, &addr.addr_in6()->sin6_addr, addr_buf_size);
}
STACK_OF(GENERAL_NAME)* names
= static_cast<STACK_OF(GENERAL_NAME)*>(X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL));
if (names == NULL) {
return NO_SAN_PRESENT;
}
for (int i = 0; i < sk_GENERAL_NAME_num(names); ++i) {
GENERAL_NAME* name = sk_GENERAL_NAME_value(names, i);
if (name->type == GEN_IPADD){
ASN1_STRING* str = name->d.iPAddress;
unsigned char* ip = ASN1_STRING_data(str);
int ip_len = ASN1_STRING_length(str);
if (ip_len != 4 && ip_len != 16) {
return INVALID_CERT;
}
if (static_cast<size_t>(ip_len) == addr_buf_size &&
memcmp(ip, addr_buf, addr_buf_size) == 0) {
return MATCH;
}
}
}
sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
return NO_MATCH;
}
};
OpenSslSession::OpenSslSession(const Address& address,
int flags,
SSL_CTX* ssl_ctx)
: SslSession(address, flags)
, ssl_(SSL_new(ssl_ctx))
, incoming_bio_(rb::RingBufferBio::create(&incoming_))
, outgoing_bio_(rb::RingBufferBio::create(&outgoing_)) {
SSL_set_bio(ssl_, incoming_bio_, outgoing_bio_);
SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_NONE, ssl_no_verify_callback);
#if DEBUG_SSL
SSL_CTX_set_info_callback(ssl_ctx, ssl_info_callback);
#endif
SSL_set_connect_state(ssl_);
}
OpenSslSession::~OpenSslSession() {
SSL_free(ssl_);
}
void OpenSslSession::do_handshake() {
int rc = SSL_connect(ssl_);
if (rc <= 0) check_error(rc);
}
void OpenSslSession::verify() {
if (!verify_flags_) return;
X509* peer_cert = SSL_get_peer_certificate(ssl_);
if (peer_cert == NULL) {
error_code_ = CASS_ERROR_SSL_NO_PEER_CERT;
error_message_ = "No peer certificate found";
return;
}
if (verify_flags_ & CASS_SSL_VERIFY_PEER_CERT) {
int rc = SSL_get_verify_result(ssl_);
if (rc != X509_V_OK) {
error_code_ = CASS_ERROR_SSL_INVALID_PEER_CERT;
error_message_ = X509_verify_cert_error_string(rc);
X509_free(peer_cert);
return;
}
}
if (verify_flags_ & CASS_SSL_VERIFY_PEER_IDENTITY) {
// We can only match IP address because that's what
// Cassandra has in system local/peers tables
switch (OpenSslVerifyIdentity::match(peer_cert, addr_)) {
case OpenSslVerifyIdentity::MATCH:
// Success
break;
case OpenSslVerifyIdentity::INVALID_CERT:
error_code_ = CASS_ERROR_SSL_INVALID_PEER_CERT;
error_message_ = "Peer certificate has malformed name field(s)";
X509_free(peer_cert);
return;
default:
error_code_ = CASS_ERROR_SSL_IDENTITY_MISMATCH;
error_message_ = "Peer certificate subject name does not match";
X509_free(peer_cert);
return;
}
}
X509_free(peer_cert);
}
int OpenSslSession::encrypt(const char* buf, size_t size) {
int rc = SSL_write(ssl_, buf, size);
if (rc <= 0) check_error(rc);
return rc;
}
int OpenSslSession::decrypt(char* buf, size_t size) {
int rc = SSL_read(ssl_, buf, size);
if (rc <= 0) check_error(rc);
return rc;
}
void OpenSslSession::check_error(int rc) {
int err = SSL_get_error(ssl_, rc);
if (err != SSL_ERROR_WANT_READ && err != SSL_ERROR_NONE) {
error_code_ = CASS_ERROR_SSL_PROTOCOL_ERROR;
error_message_ = ssl_error_string();
}
}
OpenSslContext::OpenSslContext()
: ssl_ctx_(SSL_CTX_new(SSLv23_client_method()))
, trusted_store_(X509_STORE_new()) {
SSL_CTX_set_cert_store(ssl_ctx_, trusted_store_);
}
OpenSslContext::~OpenSslContext() {
SSL_CTX_free(ssl_ctx_);
}
SslSession* OpenSslContext::create_session(const Address& address ) {
return new OpenSslSession(address, verify_flags_, ssl_ctx_);
}
CassError OpenSslContext::add_trusted_cert(const char* cert,
size_t cert_length) {
X509* x509 = load_cert(cert, cert_length);
if (x509 == NULL) {
return CASS_ERROR_SSL_INVALID_CERT;
}
X509_STORE_add_cert(trusted_store_, x509);
X509_free(x509);
return CASS_OK;
}
CassError OpenSslContext::set_cert(const char* cert,
size_t cert_length) {
BIO* bio = BIO_new_mem_buf(const_cast<char*>(cert), cert_length);
if (bio == NULL) {
return CASS_ERROR_SSL_INVALID_CERT;
}
int rc = SSL_CTX_use_certificate_chain_bio(ssl_ctx_, bio);
BIO_free_all(bio);
if (!rc) {
ssl_log_errors("Unable to load certificate chain");
return CASS_ERROR_SSL_INVALID_CERT;
}
return CASS_OK;
}
CassError OpenSslContext::set_private_key(const char* key,
size_t key_length,
const char* password,
size_t password_length) {
// TODO: Password buffer
EVP_PKEY* pkey = load_key(key, key_length, password);
if (pkey == NULL) {
return CASS_ERROR_SSL_INVALID_PRIVATE_KEY;
}
SSL_CTX_use_PrivateKey(ssl_ctx_, pkey);
EVP_PKEY_free(pkey);
return CASS_OK;
}
SslContext* OpenSslContextFactory::create() {
return new OpenSslContext();
}
void OpenSslContextFactory::init() {
SSL_library_init();
SSL_load_error_strings();
OpenSSL_add_all_algorithms();
// We have to set the lock/id callbacks for use of OpenSSL thread safety.
// It's not clear what's thread-safe in OpenSSL. Writing/Reading to
// a single "SSL" object is NOT and we don't do that, but we do create multiple
// "SSL" objects from a single "SSL_CTX" in different threads. That seems to be
// okay with the following callbacks set.
int num_locks = CRYPTO_num_locks();
crypto_locks = new uv_rwlock_t[num_locks];
for (int i = 0; i < num_locks; ++i) {
if (uv_rwlock_init(crypto_locks + i)) {
fprintf(stderr, "Unable to init read/write lock");
abort();
}
}
CRYPTO_set_locking_callback(crypto_locking_callback);
CRYPTO_set_id_callback(crypto_id_callback);
}
} // namespace cass
You can’t perform that action at this time.
