| 50 | |
| 51 | #if USE_SSL |
| 52 | void MySQLHandlerFactory::readRSAKeys() |
| 53 | { |
| 54 | const Poco::Util::LayeredConfiguration & config = Poco::Util::Application::instance().config(); |
| 55 | String certificate_file_property = "openSSL.server.certificateFile"; |
| 56 | String private_key_file_property = "openSSL.server.privateKeyFile"; |
| 57 | |
| 58 | if (!config.has(certificate_file_property)) |
| 59 | throw Exception("Certificate file is not set.", ErrorCodes::NO_ELEMENTS_IN_CONFIG); |
| 60 | |
| 61 | if (!config.has(private_key_file_property)) |
| 62 | throw Exception("Private key file is not set.", ErrorCodes::NO_ELEMENTS_IN_CONFIG); |
| 63 | |
| 64 | { |
| 65 | String certificate_file = config.getString(certificate_file_property); |
| 66 | FILE * fp = fopen(certificate_file.data(), "r"); |
| 67 | if (fp == nullptr) |
| 68 | throw Exception("Cannot open certificate file: " + certificate_file + ".", ErrorCodes::CANNOT_OPEN_FILE); |
| 69 | SCOPE_EXIT(fclose(fp)); |
| 70 | |
| 71 | X509 * x509 = PEM_read_X509(fp, nullptr, nullptr, nullptr); |
| 72 | SCOPE_EXIT(X509_free(x509)); |
| 73 | if (x509 == nullptr) |
| 74 | throw Exception("Failed to read PEM certificate from " + certificate_file + ". Error: " + getOpenSSLErrors(), ErrorCodes::OPENSSL_ERROR); |
| 75 | |
| 76 | EVP_PKEY * p = X509_get_pubkey(x509); |
| 77 | if (p == nullptr) |
| 78 | throw Exception("Failed to get RSA key from X509. Error: " + getOpenSSLErrors(), ErrorCodes::OPENSSL_ERROR); |
| 79 | SCOPE_EXIT(EVP_PKEY_free(p)); |
| 80 | |
| 81 | public_key.reset(EVP_PKEY_get1_RSA(p)); |
| 82 | if (public_key.get() == nullptr) |
| 83 | throw Exception("Failed to get RSA key from ENV_PKEY. Error: " + getOpenSSLErrors(), ErrorCodes::OPENSSL_ERROR); |
| 84 | } |
| 85 | |
| 86 | { |
| 87 | String private_key_file = config.getString(private_key_file_property); |
| 88 | |
| 89 | FILE * fp = fopen(private_key_file.data(), "r"); |
| 90 | if (fp == nullptr) |
| 91 | throw Exception ("Cannot open private key file " + private_key_file + ".", ErrorCodes::CANNOT_OPEN_FILE); |
| 92 | SCOPE_EXIT(fclose(fp)); |
| 93 | |
| 94 | private_key.reset(PEM_read_RSAPrivateKey(fp, nullptr, nullptr, nullptr)); |
| 95 | if (!private_key) |
| 96 | throw Exception("Failed to read RSA private key from " + private_key_file + ". Error: " + getOpenSSLErrors(), ErrorCodes::OPENSSL_ERROR); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | void MySQLHandlerFactory::generateRSAKeys() |
| 101 | { |