foundationdb/FDBLibTLS/FDBLibTLSPolicy.cpp

320 lines
8.8 KiB
C++
Raw Normal View History

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.
*/
#include "FDBLibTLS/FDBLibTLSPolicy.h"
#include "FDBLibTLS/FDBLibTLSSession.h"
#include "flow/Trace.h"
#include <openssl/bio.h>
#include <openssl/err.h>
2018-05-09 07:27:21 +08:00
#include <openssl/evp.h>
#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>
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) {
if ((tls_cfg = tls_config_new()) == NULL) {
TraceEvent(SevError, "FDBLibTLSConfigError");
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);
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) {
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) {
TraceEvent(SevError, "FDBLibTLSNoServerName");
2018-05-09 07:27:21 +08:00
return NULL;
}
}
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);
} 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-05-09 07:27:21 +08:00
if (size < 0)
return 0;
if (u == NULL)
return 0;
2018-05-09 07:27:21 +08:00
plen = strlen(password);
if (plen > size)
return 0;
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-05-09 07:27:21 +08:00
return plen;
}
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) {
struct stack_st_X509 *certs = NULL;
X509 *cert = NULL;
BIO *bio = NULL;
2018-05-09 07:27:21 +08:00
int errnum;
2018-05-09 07:27:21 +08:00
if (cert_pem_len > INT_MAX)
goto err;
2018-05-09 07:27:21 +08:00
if ((bio = BIO_new_mem_buf((void *)cert_pem, cert_pem_len)) == NULL) {
TraceEvent(SevError, "FDBLibTLSOutOfMemory");
goto err;
}
2018-05-09 07:27:21 +08:00
if ((certs = sk_X509_new_null()) == NULL) {
TraceEvent(SevError, "FDBLibTLSOutOfMemory");
goto err;
}
ERR_clear_error();
while ((cert = PEM_read_bio_X509(bio, NULL, password_cb, NULL)) != NULL) {
if (!sk_X509_push(certs, cert)) {
TraceEvent(SevError, "FDBLibTLSOutOfMemory");
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));
TraceEvent(SevError, "FDBLibTLSCertDataError").detail("LibcryptoErrorMessage", errbuf);
goto err;
}
if (sk_X509_num(certs) < 1) {
TraceEvent(SevError, "FDBLibTLSNoCerts");
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) {
TraceEvent(SevError, "FDBLibTLSCAAlreadySet");
2018-05-09 07:27:21 +08:00
return false;
}
2018-05-09 07:27:21 +08:00
if (session_created) {
TraceEvent(SevError, "FDBLibTLSPolicyAlreadyActive");
2018-05-09 07:27:21 +08:00
return false;
}
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-05-09 07:27:21 +08:00
if (tls_config_set_ca_mem(tls_cfg, ca_data, ca_len) == -1) {
TraceEvent(SevError, "FDBLibTLSCAError").detail("LibTLSErrorMessage", tls_config_error(tls_cfg));
2018-05-09 07:27:21 +08:00
return false;
}
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) {
TraceEvent(SevError, "FDBLibTLSCertAlreadySet");
2018-05-09 07:27:21 +08:00
return false;
}
if (session_created) {
TraceEvent(SevError, "FDBLibTLSPolicyAlreadyActive");
2018-05-09 07:27:21 +08:00
return false;
}
2018-05-09 07:27:21 +08:00
if (tls_config_set_cert_mem(tls_cfg, cert_data, cert_len) == -1) {
TraceEvent(SevError, "FDBLibTLSCertError").detail("LibTLSErrorMessage", tls_config_error(tls_cfg));
2018-05-09 07:27:21 +08:00
return false;
}
2018-05-09 07:27:21 +08:00
cert_data_set = true;
2018-05-09 07:27:21 +08:00
return true;
}
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;
if (key_data_set) {
TraceEvent(SevError, "FDBLibTLSKeyAlreadySet");
2018-05-09 07:27:21 +08:00
goto err;
}
if (session_created) {
TraceEvent(SevError, "FDBLibTLSPolicyAlreadyActive");
2018-05-09 07:27:21 +08:00
goto err;
}
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) {
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)) {
TraceEvent(SevError, "FDBLibTLSIncorrectPassword");
2018-05-09 07:27:21 +08:00
} else {
ERR_error_string_n(errnum, errbuf, sizeof(errbuf));
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) {
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)) {
TraceEvent(SevError, "FDBLibTLSOutOfMemory");
2018-05-09 07:27:21 +08:00
goto err;
}
if ((len = BIO_get_mem_data(bio, &data)) <= 0) {
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) {
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) {
TraceEvent(SevError, "FDBLibTLSKeyError").detail("LibTLSErrorMessage", tls_config_error(tls_cfg));
2018-05-09 07:27:21 +08:00
goto err;
}
}
key_data_set = true;
2018-05-09 07:27:21 +08:00
rc = true;
2018-05-09 07:27:21 +08:00
err:
BIO_free(bio);
EVP_PKEY_free(key);
return rc;
}
2018-05-09 07:27:21 +08:00
bool FDBLibTLSPolicy::set_verify_peers(int count, const uint8_t* verify_peers[], int verify_peers_len[]) {
if (verify_peers_set) {
TraceEvent(SevError, "FDBLibTLSVerifyPeersAlreadySet");
return false;
}
if (session_created) {
TraceEvent(SevError, "FDBLibTLSPolicyAlreadyActive");
return false;
}
2018-05-09 07:27:21 +08:00
if (count < 1) {
TraceEvent(SevError, "FDBLibTLSNoVerifyPeers");
return false;
}
2018-05-09 07:27:21 +08:00
for (int i = 0; i < count; i++) {
try {
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] != '\\') {
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();
std::string verifyString((const char*)verify_peers[i], verify_peers_len[i]);
TraceEvent(SevError, "FDBLibTLSVerifyPeersParseError").detail("Config", verifyString);
2018-05-09 07:27:21 +08:00
return false;
}
}
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);
verify_peers_set = true;
return true;
}