mirror of
https://github.com/savoirfairelinux/opendht.git
synced 2026-11-08 12:28:42 +00:00
crypto: add DecryptError
This commit is contained in:
1 parent
593ca81dbd
commit
ac13795db0
2 files changed
+14
-3
No files matched your search
@@ -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
|
||||
|
||||
+6
-3
@@ -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);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user