Update POCO library.

This commit is contained in:
Sandu Liviu Catalin committed 2023-03-23 20:19:11 +02:00
1 parent 8d15f4b6e9
commit 233fc103f9
2521 files changed
+257092 -72789

No files matched your search

+4 -5
View File
@@ -93,7 +93,6 @@ public:
ENC_BINHEX = 0x02, /// BinHex-encoded output
ENC_BASE64_NO_LF = 0x81, /// Base64-encoded output, no linefeeds
ENC_BINHEX_NO_LF = 0x82 /// BinHex-encoded output, no linefeeds
};
virtual ~Cipher();
@@ -108,16 +107,16 @@ public:
virtual CryptoTransform::Ptr createDecryptor() = 0;
/// Creates a decryptor object to be used with a CryptoStream.
virtual std::string encryptString(const std::string& str, Encoding encoding = ENC_NONE);
virtual std::string encryptString(const std::string& str, Encoding encoding = ENC_NONE, bool padding = true);
/// Directly encrypt a string and encode it using the given encoding.
virtual std::string decryptString(const std::string& str, Encoding encoding = ENC_NONE);
virtual std::string decryptString(const std::string& str, Encoding encoding = ENC_NONE, bool padding = true);
/// Directly decrypt a string that is encoded with the given encoding.
virtual void encrypt(std::istream& source, std::ostream& sink, Encoding encoding = ENC_NONE);
virtual void encrypt(std::istream& source, std::ostream& sink, Encoding encoding = ENC_NONE, bool padding = true);
/// Directly encrypts an input stream and encodes it using the given encoding.
virtual void decrypt(std::istream& source, std::ostream& sink, Encoding encoding = ENC_NONE);
virtual void decrypt(std::istream& source, std::ostream& sink, Encoding encoding = ENC_NONE, bool padding = true);
/// Directly decrypt an input stream that is encoded with the given encoding.
protected:
+8 -3
View File
@@ -28,6 +28,7 @@ namespace Crypto {
class Cipher;
class CipherKey;
class RSAKey;
class EVPPKey;
class Crypto_API CipherFactory
@@ -42,8 +43,8 @@ public:
/// Destroys the CipherFactory.
Cipher* createCipher(const CipherKey& key);
/// Creates a Cipher object for the given Cipher name. Valid cipher
/// names depend on the OpenSSL version the library is linked with;
/// Creates a Cipher object for the given Cipher name. Valid cipher
/// names depend on the OpenSSL version the library is linked with;
/// see the output of
///
/// openssl enc --help
@@ -59,7 +60,11 @@ public:
Cipher* createCipher(const RSAKey& key, RSAPaddingMode paddingMode = RSA_PADDING_PKCS1);
/// Creates a RSACipher using the given RSA key and padding mode
/// for public key encryption/private key decryption.
Cipher* createCipher(const EVPPKey& key);
/// Creates an EVPCipher using the given EVP key
/// for public key encryption/private key decryption.
static CipherFactory& defaultFactory();
/// Returns the default CipherFactory.
+29
View File
@@ -24,8 +24,18 @@
#define POCO_EXTERNAL_OPENSSL_SLPRO 2
//
// Temporarily suppress deprecation warnings coming
// from OpenSSL 3.0, until we have updated our code.
//
#if !defined(POCO_DONT_SUPPRESS_OPENSSL_DEPRECATED)
#define OPENSSL_SUPPRESS_DEPRECATED
#endif
#include "Poco/Foundation.h"
#include <openssl/opensslv.h>
#include <openssl/err.h>
#ifndef OPENSSL_VERSION_PREREQ
@@ -39,6 +49,11 @@
#endif
#if OPENSSL_VERSION_NUMBER < 0x10000000L
#error "OpenSSL version too old. At least OpenSSL 1.0.0 is required."
#endif
enum RSAPaddingMode
/// The padding mode used for RSA public key encryption.
{
@@ -164,6 +179,20 @@ namespace Poco {
namespace Crypto {
inline std::string& getError(std::string& msg)
/// Appends OpenSSL error(s) to msg and
/// returns the augmented error description.
{
unsigned long err;
while ((err = ERR_get_error()))
{
if (!msg.empty()) msg.append(1, '\n');
msg.append(ERR_error_string(err, 0));
}
return msg;
}
void Crypto_API initializeCrypto();
/// Initialize the Crypto library, as well as the underlying OpenSSL
/// libraries, by calling OpenSSLInitializer::initialize().
+4 -4
View File
@@ -39,13 +39,13 @@ public:
/// See the OpenSSL documentation for a list of supported digest algorithms.
///
/// Throws a Poco::NotFoundException if no algorithm with the given name exists.
~DigestEngine();
/// Destroys the DigestEngine.
const std::string& algorithm() const;
/// Returns the name of the digest algorithm.
int nid() const;
/// Returns the NID (OpenSSL object identifier) of the digest algorithm.
@@ -56,7 +56,7 @@ public:
protected:
void updateImpl(const void* data, std::size_t length);
private:
std::string _name;
EVP_MD_CTX* _pContext;
+1
View File
@@ -32,6 +32,7 @@ class X509Certificate;
class PKCS12Container;
//@ deprecated
class Crypto_API ECKey: public KeyPair
/// This class stores an EC key pair, consisting
/// of private and public key. Storage of the private
+3 -3
View File
@@ -62,8 +62,8 @@ public:
ECKeyImpl(const std::string& publicKeyFile, const std::string& privateKeyFile, const std::string& privateKeyPassphrase);
/// Creates the ECKey, by reading public and private key from the given files and
/// using the given passphrase for the private key. Can only by used for signing if
/// a private key is available.
/// using the given passphrase for the private key. Can only by used for signing if
/// a private key is available.
ECKeyImpl(std::istream* pPublicKeyStream, std::istream* pPrivateKeyStream, const std::string& privateKeyPassphrase);
/// Creates the ECKey. Can only by used for signing if pPrivKey
@@ -91,7 +91,7 @@ public:
void save(const std::string& publicKeyFile,
const std::string& privateKeyFile = "",
const std::string& privateKeyPassphrase = "") const;
/// Exports the public and private keys to the given files.
/// Exports the public and private keys to the given files.
///
/// If an empty filename is specified, the corresponding key
/// is not exported.
+76
View File
@@ -0,0 +1,76 @@
//
// EVPCipherImpl.h
//
// Library: Crypto
// Package: EVP
// Module: EVPCipherImpl
//
// Definition of the EVPCipherImpl class.
//
// Copyright (c) 2008, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef Crypto_EVPCipherImpl_INCLUDED
#define Crypto_EVPCipherImpl_INCLUDED
#include "Poco/Crypto/Crypto.h"
#include "Poco/Crypto/Cipher.h"
#include "Poco/Crypto/EVPPKey.h"
#include "Poco/Crypto/OpenSSLInitializer.h"
#include <openssl/evp.h>
namespace Poco {
namespace Crypto {
class EVPCipherImpl: public Cipher
/// An implementation of the Cipher class for
/// asymmetric (public-private key) encryption
/// based on the OpenSSL crypto library.
///
/// Encryption is using the public key, decryption
/// requires the private key.
{
public:
EVPCipherImpl(const EVPPKey& key);
/// Creates a new EVPCipherImpl object for the given EVPPKey
/// and using the given padding mode.
virtual ~EVPCipherImpl();
/// Destroys the EVPCipherImpl.
const std::string& name() const;
/// Returns the name of the Cipher.
CryptoTransform::Ptr createEncryptor();
/// Creates an encryptor object.
CryptoTransform::Ptr createDecryptor();
/// Creates a decryptor object.
private:
EVPPKey _key;
OpenSSLInitializer _openSSLInitializer;
};
//
// Inlines
//
inline const std::string& EVPCipherImpl::name() const
{
return _key.name();
}
} } // namespace Poco::Crypto
#endif // Crypto_EVPCipherImpl_INCLUDED
+61 -22
View File
@@ -22,20 +22,25 @@
#include "Poco/Crypto/Crypto.h"
#include "Poco/Crypto/CryptoException.h"
#include "Poco/StreamCopier.h"
#include "Poco/Format.h"
#include <openssl/ec.h>
#include <openssl/rsa.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <sstream>
#include <typeinfo>
#include <map>
namespace Poco {
namespace Crypto {
//@deprecated
class ECKey;
//@deprecated
class RSAKey;
class PKCS12Container;
class X509Certificate;
class Crypto_API EVPPKey
@@ -55,10 +60,36 @@ public:
/// Only EC keys can be wrapped by an EVPPKey
/// created using this constructor.
EVPPKey(const X509Certificate& cert);
/// Constructs EVPPKey from the given certificate.
EVPPKey(const PKCS12Container& cert);
/// Constructs EVPPKey from the given container.
#if OPENSSL_VERSION_NUMBER >= 0x10000000L
EVPPKey(int type, int param);
/// Creates the EVPPKey.
/// Creates a new public/private keypair using the given parameters.
/// Can be used to sign data and verify signatures.
///
/// Suported types:
/// - EVP_PKEY_RSA
/// - EVP_PKEY_EC
///
/// Parameters:
/// - for EVP_PKEY_RSA: key length in bits
/// - for EVP_PKEY_EC: curve NID
///
/// This constructor is not available for OpenSSL version < 1.0.0
#endif // OPENSSL_VERSION_NUMBER >= 0x10000000L
explicit EVPPKey(EVP_PKEY* pEVPPKey);
/// Constructs EVPPKey from EVP_PKEY pointer.
/// The content behind the supplied pointer is internally duplicated.
//@ deprecated
template<typename K>
explicit EVPPKey(K* pKey): _pEVPPKey(EVP_PKEY_new())
/// Constructs EVPPKey from a "native" OpenSSL (RSA or EC_KEY),
@@ -124,6 +155,9 @@ public:
int type() const;
/// Retuns the EVPPKey type NID.
const std::string& name() const;
/// Retuns the EVPPKey name.
bool isSupported(int type) const;
/// Returns true if OpenSSL type is supported
@@ -135,19 +169,25 @@ public:
static EVP_PKEY* duplicate(const EVP_PKEY* pFromKey, EVP_PKEY** pToKey);
/// Duplicates pFromKey into *pToKey and returns
// the pointer to duplicated EVP_PKEY.
/// the pointer to duplicated EVP_PKEY.
private:
EVPPKey();
static int type(const EVP_PKEY* pEVPPKey);
void checkType();
void newECKey(const char* group);
void duplicate(EVP_PKEY* pEVPPKey);
//@ deprecated
void setKey(ECKey* pKey);
//@ deprecated
void setKey(RSAKey* pKey);
//@ deprecated
void setKey(EC_KEY* pKey);
//@ deprecated
void setKey(RSA* pKey);
static int passCB(char* buf, int size, int, void* pass);
typedef EVP_PKEY* (*PEM_read_FILE_Key_fn)(FILE*, EVP_PKEY**, pem_password_cb*, void*);
@@ -180,7 +220,7 @@ private:
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable:4996) // deprecation warnings
#endif
#endif
pFile = fopen(keyFile.c_str(), "r");
#if defined(_MSC_VER)
#pragma warning(pop)
@@ -211,8 +251,10 @@ private:
}
else
{
std::string msg = Poco::format("EVPPKey::loadKey('%s')\n", keyFile);
getError(msg);
if (getFunc) EVP_PKEY_free(pKey);
throw IOException("ECKeyImpl, cannot open file", keyFile);
throw IOException(msg);
}
}
else goto error;
@@ -220,8 +262,10 @@ private:
return false;
error:
std::string msg = Poco::format("EVPPKey::loadKey('%s')\n", keyFile);
getError(msg);
if (pFile) fclose(pFile);
throw OpenSSLException("EVPKey::loadKey(string)");
throw OpenSSLException(msg);
}
template <typename K, typename F>
@@ -277,13 +321,18 @@ private:
return false;
error:
std::string msg = "EVPPKey::loadKey(istream)\n";
getError(msg);
if (pBIO) BIO_free(pBIO);
throw OpenSSLException("EVPKey::loadKey(stream)");
throw OpenSSLException(msg);
}
EVP_PKEY* _pEVPPKey;
EVP_PKEY* _pEVPPKey = 0;
static const std::map<int, std::string> KNOWN_TYPES;
//@deprecated
friend class ECKeyImpl;
//@deprecated
friend class RSAKeyImpl;
};
@@ -297,7 +346,11 @@ inline bool EVPPKey::operator == (const EVPPKey& other) const
{
poco_check_ptr (other._pEVPPKey);
poco_check_ptr (_pEVPPKey);
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
return (1 == EVP_PKEY_eq(_pEVPPKey, other._pEVPPKey));
#else
return (1 == EVP_PKEY_cmp(_pEVPPKey, other._pEVPPKey));
#endif
}
@@ -311,7 +364,7 @@ inline int EVPPKey::type(const EVP_PKEY* pEVPPKey)
{
if (!pEVPPKey) return NID_undef;
return EVP_PKEY_type(EVP_PKEY_id(pEVPPKey));
return EVP_PKEY_type(EVP_PKEY_base_id(pEVPPKey));
}
@@ -339,20 +392,6 @@ inline EVPPKey::operator EVP_PKEY*()
}
inline void EVPPKey::setKey(EC_KEY* pKey)
{
if (!EVP_PKEY_set1_EC_KEY(_pEVPPKey, pKey))
throw OpenSSLException();
}
inline void EVPPKey::setKey(RSA* pKey)
{
if (!EVP_PKEY_set1_RSA(_pEVPPKey, pKey))
throw OpenSSLException();
}
} } // namespace Poco::Crypto
+174
View File
@@ -0,0 +1,174 @@
//
// Envelope.h
//
// Library: Crypto
// Package: Envelope
// Module: Envelope
//
// Definition of the Envelope class.
//
// Copyright (c) 2008, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef Crypto_Envelope_INCLUDED
#define Crypto_Envelope_INCLUDED
#include "Poco/Crypto/Crypto.h"
#include "Poco/Crypto/EVPPKey.h"
#include <vector>
#include <openssl/evp.h>
namespace Poco {
namespace Crypto {
class Crypto_API Envelope
/// Envelope encrypts/decrypts data using a symmetric key.
///
/// Encryption and decryption with asymmetric keys is computationally expensive.
/// To alleviate that, Envelope encrypts data using a symmetric session key;
/// the key is then itself asymmetrically encrypted using a public key.
/// It is also possible to encrypt the session key with multiple public keys,
/// so that the message can be sent to multiple recipients.
///
/// Each recipient decrypts the session with their private key; the session
/// key for the message decryption is the same for each recipient.
{
public:
using Byte = unsigned char;
using ByteVec = std::vector<Byte>;
using EVPPKeyVec = std::vector<EVPPKey>;
using EVP_PKEYVec = std::vector<EVP_PKEY*>;
using EncKeyVec = std::vector<ByteVec>;
Envelope() = delete;
Envelope(const EVPPKey& key, int cipherNID);
/// Creates a new Envelope object.
/// Initialization vector is automatically
/// generated.
Envelope(const EVPPKeyVec& keys, int cipherNID);
/// Creates a new Envelope object.
/// Initialization vector is automatically
/// generated.
~Envelope();
/// Destroys the Envelope.
const ByteVec& iv() const;
/// Returns the initialization vector.
void addKey(const EVPPKey& key);
/// Adds the key to the list of private keys.
const EncKeyVec& keys() const;
/// Returns encrypted symmetric keys.
int cipherNID() const;
/// Reurns the cipher NID.
const ByteVec& seal(const std::string& plainText);
/// Encrypts the given text and returns the encrypted text.
const ByteVec& seal(const ByteVec& plainData);
/// Encrypts the given data and returns the encrypted data.
const ByteVec& getContent() const;
/// Returns the encrypted content.
void setContent(const ByteVec& enc);
/// Sets the encrypted content.
ByteVec open(const EVPPKey& privKey, const ByteVec& encKeys, const ByteVec& iv = ByteVec());
/// Decrypts the stored encrypted data and returns it.
std::string openAsString(const EVPPKey& privKey, const ByteVec& encKeys, const ByteVec& iv = ByteVec());
/// Decrypts the stored encrypted data and returns it.
static std::string toString(const ByteVec& data);
/// Converts and returns string from ByteVec.
private:
Envelope(int cipherNID);
Envelope(int cipherNID, const ByteVec& iv);
int ivSize() const;
int blockSize() const;
void handleErrors(std::string&& msg);
const EVP_CIPHER* _pCipher;
EVP_CIPHER_CTX* _pCtx;
EVP_CIPHER_CTX* _pDecCtx;
ByteVec _iv;
EVP_PKEYVec _pubKeys;
EncKeyVec _encKeys;
std::vector<int> _encKeysSizes;
ByteVec _encContent;
};
inline int Envelope::ivSize() const
{
return EVP_CIPHER_iv_length(_pCipher);
}
inline const Envelope::ByteVec& Envelope::iv() const
{
return _iv;
}
inline int Envelope::blockSize() const
{
return EVP_CIPHER_block_size(_pCipher);
}
inline const Envelope::EncKeyVec& Envelope::keys() const
{
return _encKeys;
}
inline std::string Envelope::toString(const ByteVec& data)
{
return std::string(data.begin(), data.end());
}
inline std::string Envelope::openAsString(const EVPPKey& privKey, const ByteVec& encKey, const ByteVec& iv)
{
return toString(open(privKey, encKey, iv));
}
inline const Envelope::ByteVec& Envelope::getContent() const
{
return _encContent;
}
inline void Envelope::setContent(const ByteVec& enc)
{
_encContent = enc;
}
inline int Envelope::cipherNID() const
{
return EVP_CIPHER_nid(_pCipher);
}
} } // namespace Poco::Crypto
#endif // Crypto_Envelope_INCLUDED
+1 -1
View File
@@ -56,7 +56,7 @@ public:
virtual void save(const std::string& publicKeyFile,
const std::string& privateKeyFile = "",
const std::string& privateKeyPassphrase = "") const = 0;
/// Exports the public and private keys to the given files.
/// Exports the public and private keys to the given files.
///
/// If an empty filename is specified, the corresponding key
/// is not exported.
+20 -7
View File
@@ -22,7 +22,10 @@
#include "Poco/Mutex.h"
#include "Poco/AtomicCounter.h"
#include <openssl/crypto.h>
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
#include <openssl/provider.h>
#include <atomic>
#endif
#if defined(OPENSSL_FIPS) && OPENSSL_VERSION_NUMBER < 0x010001000L
#include <openssl/fips.h>
#endif
@@ -50,10 +53,10 @@ class Crypto_API OpenSSLInitializer
public:
OpenSSLInitializer();
/// Automatically initialize OpenSSL on startup.
~OpenSSLInitializer();
/// Automatically shut down OpenSSL on exit.
static void initialize();
/// Initializes the OpenSSL machinery.
@@ -61,27 +64,37 @@ public:
/// Shuts down the OpenSSL machinery.
static bool isFIPSEnabled();
// Returns true if FIPS mode is enabled, false otherwise.
/// Returns true if FIPS mode is enabled, false otherwise.
static void enableFIPSMode(bool enabled);
// Enable or disable FIPS mode. If FIPS is not available, this method doesn't do anything.
/// Enable or disable FIPS mode. If FIPS is not available, this method doesn't do anything.
protected:
enum
{
SEEDSIZE = 256
};
#if OPENSSL_VERSION_NUMBER < 0x10100000L
// OpenSSL multithreading support
static void lock(int mode, int n, const char* file, int line);
static unsigned long id();
static struct CRYPTO_dynlock_value* dynlockCreate(const char* file, int line);
static void dynlock(int mode, struct CRYPTO_dynlock_value* lock, const char* file, int line);
static void dynlockDestroy(struct CRYPTO_dynlock_value* lock, const char* file, int line);
#endif
private:
static Poco::FastMutex* _mutexes;
static Poco::AtomicCounter _rc;
#if OPENSSL_VERSION_NUMBER < 0x10100000L
static Poco::FastMutex* _mutexes;
#endif
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
static OSSL_PROVIDER* _defaultProvider;
static OSSL_PROVIDER* _legacyProvider;
#endif
};
+5 -5
View File
@@ -39,7 +39,7 @@ class Crypto_API RSADigestEngine: public Poco::DigestEngine
/// signed. Then, the hash value is encrypted, using
/// the RSA private key.
///
/// To verify a signature, pass it to the verify()
/// To verify a signature, pass it to the verify()
/// member function. It will decrypt the signature
/// using the RSA public key and compare the resulting
/// hash with the actual hash of the data.
@@ -50,7 +50,7 @@ public:
DIGEST_MD5,
DIGEST_SHA1
};
//@ deprecated
RSADigestEngine(const RSAKey& key, DigestType digestType = DIGEST_SHA1);
/// Creates the RSADigestEngine with the given RSA key,
@@ -74,11 +74,11 @@ public:
void reset();
/// Resets the engine so that a new
/// digest can be computed.
const DigestEngine::Digest& digest();
/// Finishes the computation of the digest
/// Finishes the computation of the digest
/// (the first time it's called) and
/// returns the message digest.
/// returns the message digest.
///
/// Can be called multiple times.
+2 -1
View File
@@ -31,6 +31,7 @@ class X509Certificate;
class PKCS12Container;
//@ deprecated
class Crypto_API RSAKey: public KeyPair
/// This class stores an RSA key pair, consisting
/// of private and public key. Storage of the private
@@ -56,7 +57,7 @@ public:
};
RSAKey(const EVPPKey& key);
/// Constructs ECKeyImpl by extracting the EC key.
/// Constructs RSAKey by extracting the RSA key.
RSAKey(const X509Certificate& cert);
/// Extracts the RSA public key from the given certificate.
+5 -5
View File
@@ -51,13 +51,13 @@ public:
using ByteVec = std::vector<unsigned char>;
RSAKeyImpl(const EVPPKey& key);
/// Constructs ECKeyImpl by extracting the EC key.
/// Constructs RSAKeyImpl by extracting the RSA key.
RSAKeyImpl(const X509Certificate& cert);
/// Extracts the RSA public key from the given certificate.
RSAKeyImpl(const PKCS12Container& cert);
/// Extracts the EC private key from the given certificate.
/// Extracts the RSA private key from the given certificate.
RSAKeyImpl(int keyLength, unsigned long exponent);
/// Creates the RSAKey. Creates a new public/private keypair using the given parameters.
@@ -65,8 +65,8 @@ public:
RSAKeyImpl(const std::string& publicKeyFile, const std::string& privateKeyFile, const std::string& privateKeyPassphrase);
/// Creates the RSAKey, by reading public and private key from the given files and
/// using the given passphrase for the private key. Can only by used for signing if
/// a private key is available.
/// using the given passphrase for the private key. Can only by used for signing if
/// a private key is available.
RSAKeyImpl(std::istream* pPublicKeyStream, std::istream* pPrivateKeyStream, const std::string& privateKeyPassphrase);
/// Creates the RSAKey. Can only by used for signing if pPrivKey
@@ -97,7 +97,7 @@ public:
void save(const std::string& publicKeyFile,
const std::string& privateKeyFile = "",
const std::string& privateKeyPassphrase = "") const;
/// Exports the public and private keys to the given files.
/// Exports the public and private keys to the given files.
///
/// If an empty filename is specified, the corresponding key
/// is not exported.
+1 -1
View File
@@ -84,7 +84,7 @@ public:
X509Certificate& operator = (X509Certificate&& cert) noexcept;
/// Move assignment.
void swap(X509Certificate& cert);
void swap(X509Certificate& cert) noexcept;
/// Exchanges the certificate with another one.
~X509Certificate();