cleanup and modernization

This commit is contained in:
Günter Obiltschnig 2020-01-10 12:20:30 +01:00
parent 3a1f246ecc
commit e1f09a602b
20 changed files with 145 additions and 73 deletions

View File

@ -84,8 +84,8 @@ class Crypto_API Cipher: public Poco::RefCountedObject
/// sink.close(); /// sink.close();
{ {
public: public:
typedef Poco::AutoPtr<Cipher> Ptr; using Ptr = Poco::AutoPtr<Cipher>;
typedef std::vector<unsigned char> ByteVec; using ByteVec = std::vector<unsigned char>;
enum Encoding enum Encoding
/// Transport encoding to use for encryptString() and decryptString(). /// Transport encoding to use for encryptString() and decryptString().

View File

@ -57,8 +57,8 @@ class Crypto_API CipherKey
/// ///
{ {
public: public:
typedef CipherKeyImpl::Mode Mode; using Mode = CipherKeyImpl::Mode;
typedef CipherKeyImpl::ByteVec ByteVec; using ByteVec = CipherKeyImpl::ByteVec;
enum enum
{ {

View File

@ -37,8 +37,8 @@ class CipherKeyImpl: public RefCountedObject
/// An implementation of the CipherKey class for OpenSSL's crypto library. /// An implementation of the CipherKey class for OpenSSL's crypto library.
{ {
public: public:
typedef std::vector<unsigned char> ByteVec; using Ptr = Poco::AutoPtr<CipherKeyImpl>;
typedef Poco::AutoPtr<CipherKeyImpl> Ptr; using ByteVec = std::vector<unsigned char>;
enum Mode enum Mode
/// Cipher mode of operation. This mode determines how multiple blocks /// Cipher mode of operation. This mode determines how multiple blocks

View File

@ -38,10 +38,10 @@ public:
OpenSSLException(const std::string& msg, const std::string& arg, int code = 0); OpenSSLException(const std::string& msg, const std::string& arg, int code = 0);
OpenSSLException(const std::string& msg, const Poco::Exception& exc, int code = 0); OpenSSLException(const std::string& msg, const Poco::Exception& exc, int code = 0);
OpenSSLException(const OpenSSLException& exc); OpenSSLException(const OpenSSLException& exc);
~OpenSSLException() throw(); ~OpenSSLException() noexcept;
OpenSSLException& operator = (const OpenSSLException& exc); OpenSSLException& operator = (const OpenSSLException& exc);
const char* name() const throw(); const char* name() const noexcept;
const char* className() const throw(); const char* className() const noexcept;
Poco::Exception* clone() const; Poco::Exception* clone() const;
void rethrow() const; void rethrow() const;

View File

@ -99,7 +99,7 @@ class Crypto_API ECDSASignature
/// A helper class for dealing with ECDSA signatures. /// A helper class for dealing with ECDSA signatures.
{ {
public: public:
typedef std::vector<unsigned char> ByteVec; using ByteVec = std::vector<unsigned char>;
explicit ECDSASignature(const ByteVec& derSignature); explicit ECDSASignature(const ByteVec& derSignature);
/// Creates the ECDSASignature from a DER-encoded signature. /// Creates the ECDSASignature from a DER-encoded signature.

View File

@ -44,8 +44,8 @@ class ECKeyImpl: public KeyPairImpl
/// Elliptic Curve key clas implementation. /// Elliptic Curve key clas implementation.
{ {
public: public:
typedef Poco::AutoPtr<ECKeyImpl> Ptr; using Ptr = Poco::AutoPtr<ECKeyImpl>;
typedef std::vector<unsigned char> ByteVec; using ByteVec = std::vector<unsigned char>;
ECKeyImpl(const EVPPKey& key); ECKeyImpl(const EVPPKey& key);
/// Constructs ECKeyImpl by extracting the EC key. /// Constructs ECKeyImpl by extracting the EC key.

View File

@ -81,18 +81,14 @@ public:
EVPPKey(const EVPPKey& other); EVPPKey(const EVPPKey& other);
/// Copy constructor. /// Copy constructor.
EVPPKey& operator=(const EVPPKey& other); EVPPKey(EVPPKey&& other) noexcept;
/// Assignment operator.
#ifdef POCO_ENABLE_CPP11
EVPPKey(EVPPKey&& other);
/// Move constructor. /// Move constructor.
EVPPKey& operator=(EVPPKey&& other); EVPPKey& operator = (const EVPPKey& other);
/// Assignment move operator. /// Assignment operator.
#endif // POCO_ENABLE_CPP11 EVPPKey& operator = (EVPPKey&& other) noexcept;
/// Assignment move operator.
~EVPPKey(); ~EVPPKey();
/// Destroys the EVPPKey. /// Destroys the EVPPKey.

View File

@ -41,8 +41,8 @@ public:
KT_EC_IMPL KT_EC_IMPL
}; };
typedef Poco::AutoPtr<KeyPairImpl> Ptr; using Ptr = Poco::AutoPtr<KeyPairImpl>;
typedef std::vector<unsigned char> ByteVec; using ByteVec = std::vector<unsigned char>;
KeyPairImpl(const std::string& name, Type type); KeyPairImpl(const std::string& name, Type type);
/// Create KeyPairImpl with specified type and name. /// Create KeyPairImpl with specified type and name.

View File

@ -36,8 +36,8 @@ class Crypto_API PKCS12Container
/// This class implements PKCS#12 container functionality. /// This class implements PKCS#12 container functionality.
{ {
public: public:
typedef X509Certificate::List CAList; using CAList = X509Certificate::List;
typedef std::vector<std::string> CANameList; using CANameList = std::vector<std::string>;
explicit PKCS12Container(std::istream& istr, const std::string& password = ""); explicit PKCS12Container(std::istream& istr, const std::string& password = "");
/// Creates the PKCS12Container object from a stream. /// Creates the PKCS12Container object from a stream.
@ -48,19 +48,15 @@ public:
PKCS12Container(const PKCS12Container& cont); PKCS12Container(const PKCS12Container& cont);
/// Copy constructor. /// Copy constructor.
PKCS12Container(PKCS12Container&& cont) noexcept;
/// Move constructor.
PKCS12Container& operator = (const PKCS12Container& cont); PKCS12Container& operator = (const PKCS12Container& cont);
/// Assignment operator. /// Assignment operator.
#ifdef POCO_ENABLE_CPP11 PKCS12Container& operator = (PKCS12Container&& cont) noexcept;
PKCS12Container(PKCS12Container&& cont);
/// Move constructor.
PKCS12Container& operator = (PKCS12Container&& cont);
/// Move assignment operator. /// Move assignment operator.
#endif // POCO_ENABLE_CPP11
~PKCS12Container(); ~PKCS12Container();
/// Destroys the PKCS12Container. /// Destroys the PKCS12Container.
@ -90,7 +86,7 @@ private:
void load(PKCS12* pPKCS12, const std::string& password = ""); void load(PKCS12* pPKCS12, const std::string& password = "");
std::string extractFriendlyName(X509* pCert); std::string extractFriendlyName(X509* pCert);
typedef std::unique_ptr<X509Certificate> CertPtr; using CertPtr = std::unique_ptr<X509Certificate>;
OpenSSLInitializer _openSSLInitializer; OpenSSLInitializer _openSSLInitializer;
EVP_PKEY* _pKey; EVP_PKEY* _pKey;

View File

@ -47,8 +47,8 @@ class RSAKeyImpl: public KeyPairImpl
/// class RSAKeyImpl /// class RSAKeyImpl
{ {
public: public:
typedef Poco::AutoPtr<RSAKeyImpl> Ptr; using Ptr = Poco::AutoPtr<RSAKeyImpl>;
typedef std::vector<unsigned char> ByteVec; using ByteVec = std::vector<unsigned char>;
RSAKeyImpl(const EVPPKey& key); RSAKeyImpl(const EVPPKey& key);
/// Constructs ECKeyImpl by extracting the EC key. /// Constructs ECKeyImpl by extracting the EC key.

View File

@ -36,7 +36,7 @@ class Crypto_API X509Certificate
/// This class represents a X509 Certificate. /// This class represents a X509 Certificate.
{ {
public: public:
typedef std::vector<X509Certificate> List; using List = std::vector<X509Certificate>;
enum NID enum NID
/// Name identifier for extracting information from /// Name identifier for extracting information from
@ -74,9 +74,15 @@ public:
X509Certificate(const X509Certificate& cert); X509Certificate(const X509Certificate& cert);
/// Creates the certificate by copying another one. /// Creates the certificate by copying another one.
X509Certificate(X509Certificate&& cert) noexcept;
/// Creates the certificate by moving another one.
X509Certificate& operator = (const X509Certificate& cert); X509Certificate& operator = (const X509Certificate& cert);
/// Assigns a certificate. /// Assigns a certificate.
X509Certificate& operator = (X509Certificate&& cert) noexcept;
/// Move assignment.
void swap(X509Certificate& cert); void swap(X509Certificate& cert);
/// Exchanges the certificate with another one. /// Exchanges the certificate with another one.

View File

@ -44,7 +44,7 @@ namespace
class CryptoTransformImpl: public CryptoTransform class CryptoTransformImpl: public CryptoTransform
{ {
public: public:
typedef Cipher::ByteVec ByteVec; using ByteVec = Cipher::ByteVec;
enum Direction enum Direction
{ {

View File

@ -56,7 +56,7 @@ OpenSSLException::OpenSSLException(const OpenSSLException& exc): CryptoException
} }
OpenSSLException::~OpenSSLException() throw() OpenSSLException::~OpenSSLException() noexcept
{ {
} }
@ -68,13 +68,13 @@ OpenSSLException& OpenSSLException::operator = (const OpenSSLException& exc)
} }
const char* OpenSSLException::name() const throw() const char* OpenSSLException::name() const noexcept
{ {
return "OpenSSLException"; return "OpenSSLException";
} }
const char* OpenSSLException::className() const throw() const char* OpenSSLException::className() const noexcept
{ {
return typeid(*this).name(); return typeid(*this).name();
} }

View File

@ -89,7 +89,15 @@ EVPPKey::EVPPKey(const EVPPKey& other)
} }
EVPPKey& EVPPKey::operator=(const EVPPKey& other) EVPPKey::EVPPKey(EVPPKey&& other) noexcept:
_pEVPPKey(other._pEVPPKey)
{
other._pEVPPKey = nullptr;
poco_check_ptr(_pEVPPKey);
}
EVPPKey& EVPPKey::operator = (const EVPPKey& other)
{ {
duplicate(other._pEVPPKey, &_pEVPPKey); duplicate(other._pEVPPKey, &_pEVPPKey);
poco_check_ptr(_pEVPPKey); poco_check_ptr(_pEVPPKey);
@ -97,16 +105,7 @@ EVPPKey& EVPPKey::operator=(const EVPPKey& other)
} }
#ifdef POCO_ENABLE_CPP11 EVPPKey& EVPPKey::operator = (EVPPKey&& other) noexcept
EVPPKey::EVPPKey(EVPPKey&& other): _pEVPPKey(other._pEVPPKey)
{
other._pEVPPKey = nullptr;
poco_check_ptr(_pEVPPKey);
}
EVPPKey& EVPPKey::operator=(EVPPKey&& other)
{ {
_pEVPPKey = other._pEVPPKey; _pEVPPKey = other._pEVPPKey;
other._pEVPPKey = nullptr; other._pEVPPKey = nullptr;
@ -114,7 +113,6 @@ EVPPKey& EVPPKey::operator=(EVPPKey&& other)
return *this; return *this;
} }
#endif // POCO_ENABLE_CPP11
EVPPKey::~EVPPKey() EVPPKey::~EVPPKey()
{ {

View File

@ -88,38 +88,30 @@ PKCS12Container& PKCS12Container::operator = (const PKCS12Container& other)
} }
#ifdef POCO_ENABLE_CPP11 PKCS12Container::PKCS12Container(PKCS12Container&& other) noexcept:
PKCS12Container::PKCS12Container(PKCS12Container&& other):
_pKey(other._pKey), _pKey(other._pKey),
_pX509Cert(std::move(other._pX509Cert)), _pX509Cert(std::move(other._pX509Cert)),
_caCertList(std::move(other._caCertList)), _caCertList(std::move(other._caCertList)),
_caCertNames(std::move(other._caCertNames)), _caCertNames(std::move(other._caCertNames)),
_pkcsFriendlyName(std::move(other._pkcsFriendlyName)) _pkcsFriendlyName(std::move(other._pkcsFriendlyName))
{ {
other._pKey = 0; other._pKey = nullptr;
} }
PKCS12Container& PKCS12Container::operator = (PKCS12Container&& other) PKCS12Container& PKCS12Container::operator = (PKCS12Container&& other) noexcept
{ {
if (&other != this) if (_pKey) EVP_PKEY_free(_pKey);
{ _pKey = other._pKey; other._pKey = nullptr;
if (_pKey) EVP_PKEY_free(_pKey); _pX509Cert = std::move(other._pX509Cert);
_pKey = other._pKey; other._pKey = 0; _caCertList = std::move(other._caCertList);
_pX509Cert = std::move(other._pX509Cert); _caCertNames = std::move(other._caCertNames);
_caCertList = std::move(other._caCertList); _pkcsFriendlyName = std::move(other._pkcsFriendlyName);
_caCertNames = std::move(other._caCertNames);
_pkcsFriendlyName = std::move(other._pkcsFriendlyName);
}
return *this; return *this;
} }
#endif // POCO_ENABLE_CPP11
PKCS12Container::~PKCS12Container() PKCS12Container::~PKCS12Container()
{ {
if (_pKey) EVP_PKEY_free(_pKey); if (_pKey) EVP_PKEY_free(_pKey);

View File

@ -90,6 +90,16 @@ X509Certificate::X509Certificate(const X509Certificate& cert):
} }
X509Certificate::X509Certificate(X509Certificate&& cert) noexcept:
_issuerName(std::move(cert._issuerName)),
_subjectName(std::move(cert._subjectName)),
_serialNumber(std::move(cert._serialNumber)),
_pCert(cert._pCert)
{
cert._pCert = nullptr;
}
X509Certificate& X509Certificate::operator = (const X509Certificate& cert) X509Certificate& X509Certificate::operator = (const X509Certificate& cert)
{ {
X509Certificate tmp(cert); X509Certificate tmp(cert);
@ -98,6 +108,16 @@ X509Certificate& X509Certificate::operator = (const X509Certificate& cert)
} }
X509Certificate& X509Certificate::operator = (X509Certificate&& cert) noexcept
{
_issuerName = std::move(cert._issuerName);
_subjectName = std::move(cert._subjectName);
_serialNumber = std::move(cert._serialNumber);
_pCert = cert._pCert; cert._pCert = nullptr;
return *this;
}
void X509Certificate::swap(X509Certificate& cert) void X509Certificate::swap(X509Certificate& cert)
{ {
using std::swap; using std::swap;
@ -110,7 +130,7 @@ void X509Certificate::swap(X509Certificate& cert)
X509Certificate::~X509Certificate() X509Certificate::~X509Certificate()
{ {
X509_free(_pCert); if (_pCert) X509_free(_pCert);
} }

View File

@ -60,9 +60,21 @@ public:
X509Certificate(const Poco::Crypto::X509Certificate& cert); X509Certificate(const Poco::Crypto::X509Certificate& cert);
/// Creates the certificate by copying another one. /// Creates the certificate by copying another one.
X509Certificate(const X509Certificate& cert);
/// Creates the certificate by copying another one.
X509Certificate(X509Certificate&& cert) noexcept;
/// Creates the certificate by moving another one.
X509Certificate& operator = (const Poco::Crypto::X509Certificate& cert); X509Certificate& operator = (const Poco::Crypto::X509Certificate& cert);
/// Assigns a certificate. /// Assigns a certificate.
X509Certificate& operator = (const X509Certificate& cert);
/// Assigns a certificate.
X509Certificate& operator = (X509Certificate&& cert) noexcept;
/// Moves a certificate.
~X509Certificate(); ~X509Certificate();
/// Destroys the X509Certificate. /// Destroys the X509Certificate.

View File

@ -61,6 +61,18 @@ X509Certificate::X509Certificate(const Poco::Crypto::X509Certificate& cert):
} }
X509Certificate::X509Certificate(const X509Certificate& cert):
Poco::Crypto::X509Certificate(cert)
{
}
X509Certificate::X509Certificate(X509Certificate&& cert) noexcept:
Poco::Crypto::X509Certificate(std::move(cert))
{
}
X509Certificate& X509Certificate::operator = (const Poco::Crypto::X509Certificate& cert) X509Certificate& X509Certificate::operator = (const Poco::Crypto::X509Certificate& cert)
{ {
X509Certificate tmp(cert); X509Certificate tmp(cert);
@ -69,6 +81,21 @@ X509Certificate& X509Certificate::operator = (const Poco::Crypto::X509Certificat
} }
X509Certificate& X509Certificate::operator = (const X509Certificate& cert)
{
X509Certificate tmp(cert);
swap(tmp);
return *this;
}
X509Certificate& X509Certificate::operator = (X509Certificate&& cert) noexcept
{
Poco::Crypto::X509Certificate::operator = (cert);
return *this;
}
X509Certificate::~X509Certificate() X509Certificate::~X509Certificate()
{ {
} }

View File

@ -76,8 +76,14 @@ public:
X509Certificate(const X509Certificate& cert); X509Certificate(const X509Certificate& cert);
/// Creates the certificate by copying another one. /// Creates the certificate by copying another one.
X509Certificate(X509Certificate&& cert) noexcept;
/// Creates the certificate by moving another one.
X509Certificate& operator = (const X509Certificate& cert); X509Certificate& operator = (const X509Certificate& cert);
/// Assigns a certificate. /// Assigns a certificate.
X509Certificate& operator = (X509Certificate&& cert) noexcept;
/// Move-assigns a certificate.
void swap(X509Certificate& cert); void swap(X509Certificate& cert);
/// Exchanges the certificate with another one. /// Exchanges the certificate with another one.

View File

@ -75,6 +75,15 @@ X509Certificate::X509Certificate(const X509Certificate& cert):
} }
X509Certificate::X509Certificate(X509Certificate&& cert) noexcept:
_issuerName(std::move(cert._issuerName)),
_subjectName(std::move(cert._subjectName)),
_pCert(cert._pCert)
{
cert._pCert = nullptr;
}
X509Certificate::X509Certificate(PCCERT_CONTEXT pCert, bool shared): X509Certificate::X509Certificate(PCCERT_CONTEXT pCert, bool shared):
_pCert(pCert) _pCert(pCert)
{ {
@ -97,6 +106,16 @@ X509Certificate& X509Certificate::operator = (const X509Certificate& cert)
} }
X509Certificate& X509Certificate::operator = (X509Certificate&& cert) noexcept
{
_issuerName = std::move(cert._issuerName);
_subjectName = std::move(cert._subjectName);
_pCert = cert._pCert; cert._pCert = nullptr;
return *this;
}
void X509Certificate::swap(X509Certificate& cert) void X509Certificate::swap(X509Certificate& cert)
{ {
using std::swap; using std::swap;
@ -108,7 +127,7 @@ void X509Certificate::swap(X509Certificate& cert)
X509Certificate::~X509Certificate() X509Certificate::~X509Certificate()
{ {
CertFreeCertificateContext(_pCert); if (_pCert) CertFreeCertificateContext(_pCert);
} }