RSAKeyImpl::RSAKeyImpl( const std::string& publicKeyFile, const std::string& privateKeyFile, const std::string& privateKeyPassphrase): _pRSA(0) { poco_assert_dbg(_pRSA == 0); _pRSA = RSA_new(); if (!publicKeyFile.empty()) { BIO* bio = BIO_new(BIO_s_file()); if (!bio) throw Poco::IOException("Cannot create BIO for reading public key", publicKeyFile); int rc = BIO_read_filename(bio, publicKeyFile.c_str()); if (rc) { RSA* pubKey = PEM_read_bio_RSAPublicKey(bio, &_pRSA, 0, 0); BIO_free(bio); if (!pubKey) { freeRSA(); throw Poco::FileException("Failed to load public key", publicKeyFile); } } else { freeRSA(); throw Poco::FileNotFoundException("Public key file", publicKeyFile); } } if (!privateKeyFile.empty()) { BIO* bio = BIO_new(BIO_s_file()); if (!bio) throw Poco::IOException("Cannot create BIO for reading private key", privateKeyFile); int rc = BIO_read_filename(bio, privateKeyFile.c_str()); if (rc) { RSA* privKey = 0; if (privateKeyPassphrase.empty()) privKey = PEM_read_bio_RSAPrivateKey(bio, &_pRSA, 0, 0); else privKey = PEM_read_bio_RSAPrivateKey(bio, &_pRSA, 0, const_cast<char*>(privateKeyPassphrase.c_str())); BIO_free(bio); if (!privKey) { freeRSA(); throw Poco::FileException("Failed to load private key", privateKeyFile); } } else { freeRSA(); throw Poco::FileNotFoundException("Private key file", privateKeyFile); } } }
RSAKeyImpl::RSAKeyImpl(std::istream* pPublicKeyStream, std::istream* pPrivateKeyStream, const std::string& privateKeyPassphrase): KeyPairImpl("rsa", KT_RSA_IMPL), _pRSA(0) { poco_assert_dbg(_pRSA == 0); _pRSA = RSA_new(); if (pPublicKeyStream) { std::string publicKeyData; Poco::StreamCopier::copyToString(*pPublicKeyStream, publicKeyData); BIO* bio = BIO_new_mem_buf(const_cast<char*>(publicKeyData.data()), static_cast<int>(publicKeyData.size())); if (!bio) throw Poco::IOException("Cannot create BIO for reading public key"); RSA* publicKey = PEM_read_bio_RSAPublicKey(bio, &_pRSA, 0, 0); if (!publicKey) { int rc = BIO_reset(bio); // BIO_reset() normally returns 1 for success and 0 or -1 for failure. // File BIOs are an exception, they return 0 for success and -1 for failure. if (rc != 1) throw Poco::FileException("Failed to load public key"); publicKey = PEM_read_bio_RSA_PUBKEY(bio, &_pRSA, 0, 0); } BIO_free(bio); if (!publicKey) { freeRSA(); throw Poco::FileException("Failed to load public key"); } } if (pPrivateKeyStream) { std::string privateKeyData; Poco::StreamCopier::copyToString(*pPrivateKeyStream, privateKeyData); BIO* bio = BIO_new_mem_buf(const_cast<char*>(privateKeyData.data()), static_cast<int>(privateKeyData.size())); if (!bio) throw Poco::IOException("Cannot create BIO for reading private key"); RSA* privateKey = 0; if (privateKeyPassphrase.empty()) privateKey = PEM_read_bio_RSAPrivateKey(bio, &_pRSA, 0, 0); else privateKey = PEM_read_bio_RSAPrivateKey(bio, &_pRSA, 0, const_cast<char*>(privateKeyPassphrase.c_str())); BIO_free(bio); if (!privateKey) { freeRSA(); throw Poco::FileException("Failed to load private key"); } } }
RSAKeyImpl::RSAKeyImpl(std::istream* pPublicKeyStream, std::istream* pPrivateKeyStream, const std::string& privateKeyPassphrase): _pRSA(0) { poco_assert_dbg(_pRSA == 0); _pRSA = RSA_new(); if (pPublicKeyStream) { std::string publicKeyData; Poco::StreamCopier::copyToString(*pPublicKeyStream, publicKeyData); BIO* bio = BIO_new_mem_buf(const_cast<char*>(publicKeyData.data()), static_cast<int>(publicKeyData.size())); if (!bio) throw Poco::IOException("Cannot create BIO for reading public key"); RSA* publicKey = PEM_read_bio_RSAPublicKey(bio, &_pRSA, 0, 0); if (!publicKey) { int rc = BIO_seek(bio, 0); if (rc != 0) throw Poco::FileException("Failed to load public key"); publicKey = PEM_read_bio_RSA_PUBKEY(bio, &_pRSA, 0, 0); } BIO_free(bio); if (!publicKey) { freeRSA(); throw Poco::FileException("Failed to load public key"); } } if (pPrivateKeyStream) { std::string privateKeyData; Poco::StreamCopier::copyToString(*pPrivateKeyStream, privateKeyData); BIO* bio = BIO_new_mem_buf(const_cast<char*>(privateKeyData.data()), static_cast<int>(privateKeyData.size())); if (!bio) throw Poco::IOException("Cannot create BIO for reading private key"); RSA* privateKey = 0; if (privateKeyPassphrase.empty()) privateKey = PEM_read_bio_RSAPrivateKey(bio, &_pRSA, 0, 0); else privateKey = PEM_read_bio_RSAPrivateKey(bio, &_pRSA, 0, const_cast<char*>(privateKeyPassphrase.c_str())); BIO_free(bio); if (!privateKey) { freeRSA(); throw Poco::FileException("Failed to load private key"); } } }
RSAKeyImpl::~RSAKeyImpl() { freeRSA(); }
RSAKeyImpl::RSAKeyImpl(const std::string& publicKeyFile, const std::string& privateKeyFile, const std::string& privateKeyPassphrase): KeyPairImpl("rsa", KT_RSA_IMPL), _pRSA(0) { poco_assert_dbg(_pRSA == 0); _pRSA = RSA_new(); if (!publicKeyFile.empty()) { BIO* bio = BIO_new(BIO_s_file()); if (!bio) throw Poco::IOException("Cannot create BIO for reading public key", publicKeyFile); int rc = BIO_read_filename(bio, publicKeyFile.c_str()); if (rc) { RSA* pubKey = PEM_read_bio_RSAPublicKey(bio, &_pRSA, 0, 0); if (!pubKey) { int rc = BIO_reset(bio); // BIO_reset() normally returns 1 for success and 0 or -1 for failure. // File BIOs are an exception, they return 0 for success and -1 for failure. if (rc != 0) throw Poco::FileException("Failed to load public key", publicKeyFile); pubKey = PEM_read_bio_RSA_PUBKEY(bio, &_pRSA, 0, 0); } BIO_free(bio); if (!pubKey) { freeRSA(); throw Poco::FileException("Failed to load public key", publicKeyFile); } } else { freeRSA(); throw Poco::FileNotFoundException("Public key file", publicKeyFile); } } if (!privateKeyFile.empty()) { BIO* bio = BIO_new(BIO_s_file()); if (!bio) throw Poco::IOException("Cannot create BIO for reading private key", privateKeyFile); int rc = BIO_read_filename(bio, privateKeyFile.c_str()); if (rc) { RSA* privKey = 0; if (privateKeyPassphrase.empty()) privKey = PEM_read_bio_RSAPrivateKey(bio, &_pRSA, 0, 0); else privKey = PEM_read_bio_RSAPrivateKey(bio, &_pRSA, 0, const_cast<char*>(privateKeyPassphrase.c_str())); BIO_free(bio); if (!privKey) { freeRSA(); throw Poco::FileException("Failed to load private key", privateKeyFile); } } else { freeRSA(); throw Poco::FileNotFoundException("Private key file", privateKeyFile); } } }