* The memory barrier is defined by the singleton */
| 55 | * The memory barrier is defined by the singleton |
| 56 | */ |
| 57 | int16_t TLSContext::initialize(bool server_method) { |
| 58 | if (ctx) { |
| 59 | return error_value; |
| 60 | } |
| 61 | |
| 62 | logger_->log_debug("initializing %X", this); |
| 63 | |
| 64 | if (nullptr == OpenSSLInitializer::getInstance()) { |
| 65 | return error_value; |
| 66 | } |
| 67 | |
| 68 | std::string clientAuthStr; |
| 69 | bool needClientCert = true; |
| 70 | if (!(configure_->get(Configure::nifi_security_need_ClientAuth, clientAuthStr) && org::apache::nifi::minifi::utils::StringUtils::StringToBool(clientAuthStr, needClientCert))) { |
| 71 | needClientCert = true; |
| 72 | } |
| 73 | const SSL_METHOD *method; |
| 74 | method = server_method ? TLSv1_2_server_method() : TLSv1_2_client_method(); |
| 75 | auto local_context = std::unique_ptr<SSL_CTX, decltype(&deleteContext)>(SSL_CTX_new(method), deleteContext); |
| 76 | if (local_context == nullptr) { |
| 77 | logger_->log_error("Could not create SSL context, error: %s.", std::strerror(errno)); |
| 78 | error_value = TLS_ERROR_CONTEXT; |
| 79 | return error_value; |
| 80 | } |
| 81 | |
| 82 | if (needClientCert) { |
| 83 | std::string certificate; |
| 84 | std::string privatekey; |
| 85 | std::string passphrase; |
| 86 | std::string caCertificate; |
| 87 | |
| 88 | if (ssl_service_ != nullptr) { |
| 89 | if (!ssl_service_->configure_ssl_context(local_context.get())) { |
| 90 | error_value = TLS_ERROR_CERT_ERROR; |
| 91 | return error_value; |
| 92 | } |
| 93 | ctx = std::move(local_context); |
| 94 | error_value = TLS_GOOD; |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | if (!(configure_->get(Configure::nifi_security_client_certificate, certificate) && configure_->get(Configure::nifi_security_client_private_key, privatekey))) { |
| 99 | logger_->log_error("Certificate and Private Key PEM file not configured, error: %s.", std::strerror(errno)); |
| 100 | error_value = TLS_ERROR_PEM_MISSING; |
| 101 | return error_value; |
| 102 | } |
| 103 | // load certificates and private key in PEM format |
| 104 | if (SSL_CTX_use_certificate_chain_file(local_context.get(), certificate.c_str()) <= 0) { |
| 105 | logger_->log_error("Could not load certificate %s, for %X and %X error : %s", certificate, this, local_context.get(), std::strerror(errno)); |
| 106 | error_value = TLS_ERROR_CERT_MISSING; |
| 107 | return error_value; |
| 108 | } |
| 109 | if (configure_->get(Configure::nifi_security_client_pass_phrase, passphrase)) { |
| 110 | std::ifstream file(passphrase.c_str(), std::ifstream::in); |
| 111 | if (file.good()) { |
| 112 | // if we have been given a file copy that, otherwise treat the passphrase as a password |
| 113 | std::string password; |
| 114 | password.assign((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>()); |
nothing calls this directly
no test coverage detected