2018-04-20 01:11:55 +08:00
|
|
|
/*
|
|
|
|
* FDBLibTLSPolicy.cpp
|
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
|
|
|
* Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2018-04-13 03:28:52 +08:00
|
|
|
|
2018-10-20 09:53:09 +08:00
|
|
|
#include "FDBLibTLS/FDBLibTLSPolicy.h"
|
|
|
|
#include "FDBLibTLS/FDBLibTLSSession.h"
|
|
|
|
#include "flow/Trace.h"
|
2018-04-13 03:28:52 +08:00
|
|
|
|
|
|
|
#include <openssl/bio.h>
|
|
|
|
#include <openssl/err.h>
|
2018-05-09 07:27:21 +08:00
|
|
|
#include <openssl/evp.h>
|
2018-04-13 03:28:52 +08:00
|
|
|
#include <openssl/pem.h>
|
|
|
|
#include <openssl/x509.h>
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <exception>
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2018-05-09 07:27:21 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <limits.h>
|
2018-04-13 03:28:52 +08:00
|
|
|
|
2018-07-12 09:41:46 +08:00
|
|
|
FDBLibTLSPolicy::FDBLibTLSPolicy(Reference<FDBLibTLSPlugin> plugin):
|
|
|
|
plugin(plugin), tls_cfg(NULL), roots(NULL), session_created(false), ca_data_set(false),
|
2018-05-09 07:27:21 +08:00
|
|
|
cert_data_set(false), key_data_set(false), verify_peers_set(false) {
|
2018-04-13 03:28:52 +08:00
|
|
|
|
|
|
|
if ((tls_cfg = tls_config_new()) == NULL) {
|
2018-07-13 04:24:40 +08:00
|
|
|
TraceEvent(SevError, "FDBLibTLSConfigError");
|
2018-04-13 03:28:52 +08:00
|
|
|
throw std::runtime_error("FDBLibTLSConfigError");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Require client certificates for authentication.
|
|
|
|
tls_config_verify_client(tls_cfg);
|
|
|
|
}
|
|
|
|
|
|
|
|
FDBLibTLSPolicy::~FDBLibTLSPolicy() {
|
2018-05-09 07:27:21 +08:00
|
|
|
sk_X509_pop_free(roots, X509_free);
|
2018-04-13 03:28:52 +08:00
|
|
|
tls_config_free(tls_cfg);
|
|
|
|
}
|
|
|
|
|
2018-05-09 07:27:21 +08:00
|
|
|
ITLSSession* FDBLibTLSPolicy::create_session(bool is_client, const char* servername, TLSSendCallbackFunc send_func, void* send_ctx, TLSRecvCallbackFunc recv_func, void* recv_ctx, void* uid) {
|
|
|
|
if (is_client) {
|
|
|
|
// If verify peers has been set then there is no point specifying a
|
|
|
|
// servername, since this will be ignored - the servername should be
|
|
|
|
// matched by the verify criteria instead.
|
|
|
|
if (verify_peers_set && servername != NULL) {
|
2018-07-13 04:24:40 +08:00
|
|
|
TraceEvent(SevError, "FDBLibTLSVerifyPeersWithServerName");
|
2018-05-09 07:27:21 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If verify peers has not been set, then require a server name to
|
|
|
|
// avoid an accidental lack of name validation.
|
|
|
|
if (!verify_peers_set && servername == NULL) {
|
2018-07-13 04:24:40 +08:00
|
|
|
TraceEvent(SevError, "FDBLibTLSNoServerName");
|
2018-05-09 07:27:21 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-13 03:28:52 +08:00
|
|
|
session_created = true;
|
|
|
|
try {
|
2018-05-09 07:27:21 +08:00
|
|
|
return new FDBLibTLSSession(Reference<FDBLibTLSPolicy>::addRef(this), is_client, servername, send_func, send_ctx, recv_func, recv_ctx, uid);
|
2018-04-13 03:28:52 +08:00
|
|
|
} catch ( ... ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-09 07:27:21 +08:00
|
|
|
static int password_cb(char *buf, int size, int rwflag, void *u) {
|
|
|
|
const char *password = (const char *)u;
|
|
|
|
int plen;
|
2018-04-13 03:28:52 +08:00
|
|
|
|
2018-05-09 07:27:21 +08:00
|
|
|
if (size < 0)
|
|
|
|
return 0;
|
|
|
|
if (u == NULL)
|
|
|
|
return 0;
|
2018-04-13 03:28:52 +08:00
|
|
|
|
2018-05-09 07:27:21 +08:00
|
|
|
plen = strlen(password);
|
|
|
|
if (plen > size)
|
|
|
|
return 0;
|
2018-04-13 03:28:52 +08:00
|
|
|
|
2018-05-09 07:27:21 +08:00
|
|
|
// Note: buf does not need to be NUL-terminated since
|
|
|
|
// we return an explicit length.
|
|
|
|
strncpy(buf, password, size);
|
2018-04-13 03:28:52 +08:00
|
|
|
|
2018-05-09 07:27:21 +08:00
|
|
|
return plen;
|
2018-04-13 03:28:52 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 07:27:21 +08:00
|
|
|
struct stack_st_X509* FDBLibTLSPolicy::parse_cert_pem(const uint8_t* cert_pem, size_t cert_pem_len) {
|
2018-04-13 03:28:52 +08:00
|
|
|
struct stack_st_X509 *certs = NULL;
|
|
|
|
X509 *cert = NULL;
|
|
|
|
BIO *bio = NULL;
|
2018-05-09 07:27:21 +08:00
|
|
|
int errnum;
|
2018-04-13 03:28:52 +08:00
|
|
|
|
2018-05-09 07:27:21 +08:00
|
|
|
if (cert_pem_len > INT_MAX)
|
2018-04-13 03:28:52 +08:00
|
|
|
goto err;
|
2018-05-09 07:27:21 +08:00
|
|
|
if ((bio = BIO_new_mem_buf((void *)cert_pem, cert_pem_len)) == NULL) {
|
2018-07-13 04:24:40 +08:00
|
|
|
TraceEvent(SevError, "FDBLibTLSOutOfMemory");
|
2018-04-13 03:28:52 +08:00
|
|
|
goto err;
|
|
|
|
}
|
2018-05-09 07:27:21 +08:00
|
|
|
if ((certs = sk_X509_new_null()) == NULL) {
|
2018-07-13 04:24:40 +08:00
|
|
|
TraceEvent(SevError, "FDBLibTLSOutOfMemory");
|
2018-04-13 03:28:52 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
ERR_clear_error();
|
|
|
|
while ((cert = PEM_read_bio_X509(bio, NULL, password_cb, NULL)) != NULL) {
|
|
|
|
if (!sk_X509_push(certs, cert)) {
|
2018-07-13 04:24:40 +08:00
|
|
|
TraceEvent(SevError, "FDBLibTLSOutOfMemory");
|
2018-04-13 03:28:52 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that the NULL cert was caused by EOF and not some other failure.
|
|
|
|
errnum = ERR_peek_last_error();
|
|
|
|
if (ERR_GET_LIB(errnum) != ERR_LIB_PEM || ERR_GET_REASON(errnum) != PEM_R_NO_START_LINE) {
|
|
|
|
char errbuf[256];
|
|
|
|
|
|
|
|
ERR_error_string_n(errnum, errbuf, sizeof(errbuf));
|
2018-07-13 04:24:40 +08:00
|
|
|
TraceEvent(SevError, "FDBLibTLSCertDataError").detail("LibcryptoErrorMessage", errbuf);
|
2018-04-13 03:28:52 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sk_X509_num(certs) < 1) {
|
2018-07-13 04:24:40 +08:00
|
|
|
TraceEvent(SevError, "FDBLibTLSNoCerts");
|
2018-04-13 03:28:52 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2018-05-09 07:27:21 +08:00
|
|
|
BIO_free(bio);
|
|
|
|
|
|
|
|
return certs;
|
|
|
|
|
|
|
|
err:
|
|
|
|
sk_X509_pop_free(certs, X509_free);
|
|
|
|
X509_free(cert);
|
|
|
|
BIO_free(bio);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FDBLibTLSPolicy::set_ca_data(const uint8_t* ca_data, int ca_len) {
|
|
|
|
if (ca_data_set) {
|
2018-07-13 04:24:40 +08:00
|
|
|
TraceEvent(SevError, "FDBLibTLSCAAlreadySet");
|
2018-05-09 07:27:21 +08:00
|
|
|
return false;
|
2018-04-13 03:28:52 +08:00
|
|
|
}
|
2018-05-09 07:27:21 +08:00
|
|
|
if (session_created) {
|
2018-07-13 04:24:40 +08:00
|
|
|
TraceEvent(SevError, "FDBLibTLSPolicyAlreadyActive");
|
2018-05-09 07:27:21 +08:00
|
|
|
return false;
|
2018-04-13 03:28:52 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 07:27:21 +08:00
|
|
|
if (ca_len < 0)
|
|
|
|
return false;
|
|
|
|
sk_X509_pop_free(roots, X509_free);
|
|
|
|
if ((roots = parse_cert_pem(ca_data, ca_len)) == NULL)
|
|
|
|
return false;
|
2018-04-13 03:28:52 +08:00
|
|
|
|
2018-05-09 07:27:21 +08:00
|
|
|
if (tls_config_set_ca_mem(tls_cfg, ca_data, ca_len) == -1) {
|
2018-07-13 04:24:40 +08:00
|
|
|
TraceEvent(SevError, "FDBLibTLSCAError").detail("LibTLSErrorMessage", tls_config_error(tls_cfg));
|
2018-05-09 07:27:21 +08:00
|
|
|
return false;
|
2018-04-13 03:28:52 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 07:27:21 +08:00
|
|
|
ca_data_set = true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FDBLibTLSPolicy::set_cert_data(const uint8_t* cert_data, int cert_len) {
|
|
|
|
if (cert_data_set) {
|
2018-07-13 04:24:40 +08:00
|
|
|
TraceEvent(SevError, "FDBLibTLSCertAlreadySet");
|
2018-05-09 07:27:21 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (session_created) {
|
2018-07-13 04:24:40 +08:00
|
|
|
TraceEvent(SevError, "FDBLibTLSPolicyAlreadyActive");
|
2018-05-09 07:27:21 +08:00
|
|
|
return false;
|
2018-04-13 03:28:52 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 07:27:21 +08:00
|
|
|
if (tls_config_set_cert_mem(tls_cfg, cert_data, cert_len) == -1) {
|
2018-07-13 04:24:40 +08:00
|
|
|
TraceEvent(SevError, "FDBLibTLSCertError").detail("LibTLSErrorMessage", tls_config_error(tls_cfg));
|
2018-05-09 07:27:21 +08:00
|
|
|
return false;
|
|
|
|
}
|
2018-04-13 03:28:52 +08:00
|
|
|
|
2018-05-09 07:27:21 +08:00
|
|
|
cert_data_set = true;
|
2018-04-13 03:28:52 +08:00
|
|
|
|
2018-05-09 07:27:21 +08:00
|
|
|
return true;
|
2018-04-13 03:28:52 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 07:27:21 +08:00
|
|
|
bool FDBLibTLSPolicy::set_key_data(const uint8_t* key_data, int key_len, const char* password) {
|
|
|
|
EVP_PKEY *key = NULL;
|
|
|
|
BIO *bio = NULL;
|
|
|
|
bool rc = false;
|
|
|
|
|
2018-04-13 03:28:52 +08:00
|
|
|
if (key_data_set) {
|
2018-07-13 04:24:40 +08:00
|
|
|
TraceEvent(SevError, "FDBLibTLSKeyAlreadySet");
|
2018-05-09 07:27:21 +08:00
|
|
|
goto err;
|
2018-04-13 03:28:52 +08:00
|
|
|
}
|
|
|
|
if (session_created) {
|
2018-07-13 04:24:40 +08:00
|
|
|
TraceEvent(SevError, "FDBLibTLSPolicyAlreadyActive");
|
2018-05-09 07:27:21 +08:00
|
|
|
goto err;
|
2018-04-13 03:28:52 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 07:27:21 +08:00
|
|
|
if (password != NULL) {
|
|
|
|
char *data;
|
|
|
|
long len;
|
|
|
|
|
|
|
|
if ((bio = BIO_new_mem_buf((void *)key_data, key_len)) == NULL) {
|
2018-07-13 04:24:40 +08:00
|
|
|
TraceEvent(SevError, "FDBLibTLSOutOfMemory");
|
2018-05-09 07:27:21 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
ERR_clear_error();
|
|
|
|
if ((key = PEM_read_bio_PrivateKey(bio, NULL, password_cb, (void *)password)) == NULL) {
|
|
|
|
int errnum = ERR_peek_error();
|
|
|
|
char errbuf[256];
|
|
|
|
|
|
|
|
if ((ERR_GET_LIB(errnum) == ERR_LIB_PEM && ERR_GET_REASON(errnum) == PEM_R_BAD_DECRYPT) ||
|
|
|
|
(ERR_GET_LIB(errnum) == ERR_LIB_EVP && ERR_GET_REASON(errnum) == EVP_R_BAD_DECRYPT)) {
|
2018-07-13 04:24:40 +08:00
|
|
|
TraceEvent(SevError, "FDBLibTLSIncorrectPassword");
|
2018-05-09 07:27:21 +08:00
|
|
|
} else {
|
|
|
|
ERR_error_string_n(errnum, errbuf, sizeof(errbuf));
|
2018-07-13 04:24:40 +08:00
|
|
|
TraceEvent(SevError, "FDBLibTLSPrivateKeyError").detail("LibcryptoErrorMessage", errbuf);
|
2018-05-09 07:27:21 +08:00
|
|
|
}
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
BIO_free(bio);
|
|
|
|
if ((bio = BIO_new(BIO_s_mem())) == NULL) {
|
2018-07-13 04:24:40 +08:00
|
|
|
TraceEvent(SevError, "FDBLibTLSOutOfMemory");
|
2018-05-09 07:27:21 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (!PEM_write_bio_PrivateKey(bio, key, NULL, NULL, 0, NULL, NULL)) {
|
2018-07-13 04:24:40 +08:00
|
|
|
TraceEvent(SevError, "FDBLibTLSOutOfMemory");
|
2018-05-09 07:27:21 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if ((len = BIO_get_mem_data(bio, &data)) <= 0) {
|
2018-07-13 04:24:40 +08:00
|
|
|
TraceEvent(SevError, "FDBLibTLSOutOfMemory");
|
2018-05-09 07:27:21 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (tls_config_set_key_mem(tls_cfg, (const uint8_t *)data, len) == -1) {
|
2018-07-13 04:24:40 +08:00
|
|
|
TraceEvent(SevError, "FDBLibTLSKeyError").detail("LibTLSErrorMessage", tls_config_error(tls_cfg));
|
2018-05-09 07:27:21 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (tls_config_set_key_mem(tls_cfg, key_data, key_len) == -1) {
|
2018-07-13 04:24:40 +08:00
|
|
|
TraceEvent(SevError, "FDBLibTLSKeyError").detail("LibTLSErrorMessage", tls_config_error(tls_cfg));
|
2018-05-09 07:27:21 +08:00
|
|
|
goto err;
|
|
|
|
}
|
2018-04-13 03:28:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
key_data_set = true;
|
2018-05-09 07:27:21 +08:00
|
|
|
rc = true;
|
2018-04-13 03:28:52 +08:00
|
|
|
|
2018-05-09 07:27:21 +08:00
|
|
|
err:
|
|
|
|
BIO_free(bio);
|
|
|
|
EVP_PKEY_free(key);
|
|
|
|
return rc;
|
2018-04-13 03:28:52 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 07:27:21 +08:00
|
|
|
bool FDBLibTLSPolicy::set_verify_peers(int count, const uint8_t* verify_peers[], int verify_peers_len[]) {
|
2018-04-13 03:28:52 +08:00
|
|
|
if (verify_peers_set) {
|
2018-07-13 04:24:40 +08:00
|
|
|
TraceEvent(SevError, "FDBLibTLSVerifyPeersAlreadySet");
|
2018-04-13 03:28:52 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (session_created) {
|
2018-07-13 04:24:40 +08:00
|
|
|
TraceEvent(SevError, "FDBLibTLSPolicyAlreadyActive");
|
2018-04-13 03:28:52 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-09 07:27:21 +08:00
|
|
|
if (count < 1) {
|
2018-07-13 04:24:40 +08:00
|
|
|
TraceEvent(SevError, "FDBLibTLSNoVerifyPeers");
|
2018-04-13 03:28:52 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-09 07:27:21 +08:00
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
try {
|
2018-06-12 06:52:04 +08:00
|
|
|
std::string verifyString((const char*)verify_peers[i], verify_peers_len[i]);
|
|
|
|
int start = 0;
|
|
|
|
while(start < verifyString.size()) {
|
|
|
|
int split = verifyString.find('|', start);
|
|
|
|
if(split == std::string::npos) {
|
|
|
|
break;
|
|
|
|
}
|
2018-06-12 06:56:22 +08:00
|
|
|
if(split == start || verifyString[split-1] != '\\') {
|
2018-06-12 06:52:04 +08:00
|
|
|
Reference<FDBLibTLSVerify> verify = Reference<FDBLibTLSVerify>(new FDBLibTLSVerify(verifyString.substr(start,split-start)));
|
|
|
|
verify_rules.push_back(verify);
|
|
|
|
start = split+1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Reference<FDBLibTLSVerify> verify = Reference<FDBLibTLSVerify>(new FDBLibTLSVerify(verifyString.substr(start)));
|
2018-05-09 07:27:21 +08:00
|
|
|
verify_rules.push_back(verify);
|
|
|
|
} catch ( const std::runtime_error& e ) {
|
|
|
|
verify_rules.clear();
|
2018-07-12 09:41:46 +08:00
|
|
|
std::string verifyString((const char*)verify_peers[i], verify_peers_len[i]);
|
2018-07-13 04:24:40 +08:00
|
|
|
TraceEvent(SevError, "FDBLibTLSVerifyPeersParseError").detail("Config", verifyString);
|
2018-05-09 07:27:21 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-04-13 03:28:52 +08:00
|
|
|
|
2018-05-09 07:27:21 +08:00
|
|
|
// All verification is manually handled (as requested via configuration).
|
|
|
|
tls_config_insecure_noverifycert(tls_cfg);
|
|
|
|
tls_config_insecure_noverifyname(tls_cfg);
|
|
|
|
tls_config_insecure_noverifytime(tls_cfg);
|
2018-04-13 03:28:52 +08:00
|
|
|
|
|
|
|
verify_peers_set = true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|