From ac13795db0a606c8d03b609956fd4578411cf7f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20B=C3=A9raud?= Date: Tue, 14 Jul 2015 12:46:44 -0400 Subject: [PATCH] crypto: add DecryptError --- include/opendht/crypto.h | 8 ++++++++ src/crypto.cpp | 9 ++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/include/opendht/crypto.h b/include/opendht/crypto.h index 42294b20..3df04c9b 100644 --- a/include/opendht/crypto.h +++ b/include/opendht/crypto.h @@ -55,6 +55,14 @@ class CryptoException : public std::runtime_error { CryptoException(const std::string& str) : std::runtime_error(str) {}; }; +/** + * Exception thrown when an expected decryption failed. + */ +class DecryptError : public CryptoException { + public: + DecryptError(const std::string& str = "") : CryptoException(str) {}; +}; + /** * Generate an RSA key pair (2048 bits) and a certificate. * @param name the name used in the generated certificate diff --git a/src/crypto.cpp b/src/crypto.cpp index 127d6547..11646339 100644 --- a/src/crypto.cpp +++ b/src/crypto.cpp @@ -124,8 +124,11 @@ PrivateKey::PrivateKey(const Blob& import, const std::string& password) int err_der = gnutls_x509_privkey_import2(x509_key, &dt, GNUTLS_X509_FMT_DER, password_ptr, flags); if (err_der != GNUTLS_E_SUCCESS) { gnutls_x509_privkey_deinit(x509_key); - throw CryptoException(std::string("Can't load private key: PEM: ") + gnutls_strerror(err) - + " DER: " + gnutls_strerror(err_der)); + if (err == GNUTLS_E_DECRYPTION_FAILED or err_der == GNUTLS_E_DECRYPTION_FAILED) + throw DecryptError("Can't decrypt private key"); + else + throw CryptoException(std::string("Can't load private key: PEM: ") + gnutls_strerror(err) + + " DER: " + gnutls_strerror(err_der)); } } @@ -214,7 +217,7 @@ PrivateKey::decrypt(const Blob& cipher) const gnutls_datum_t out; int err = gnutls_privkey_decrypt_data(key, 0, &dat, &out); if (err != GNUTLS_E_SUCCESS) - throw CryptoException(std::string("Can't decrypt data: ") + gnutls_strerror(err)); + throw DecryptError(std::string("Can't decrypt data: ") + gnutls_strerror(err)); ret.insert(ret.end(), out.data, out.data+out.size); gnutls_free(out.data); }