summaryrefslogtreecommitdiffstats
path: root/src/PolarSSL++/CryptoKey.h
diff options
context:
space:
mode:
authorarchshift <admin@archshift.com>2014-07-29 22:04:00 +0200
committerarchshift <admin@archshift.com>2014-07-29 22:04:00 +0200
commita9b597087b56b4526a3f6447789ba141568575a1 (patch)
treea08542d77b5668a25ca5e00492577ed6f4d61a9a /src/PolarSSL++/CryptoKey.h
parentSpacing fixes and a few more BLOCK_META constants. (diff)
parentSlight cleanup after portals (diff)
downloadcuberite-a9b597087b56b4526a3f6447789ba141568575a1.tar
cuberite-a9b597087b56b4526a3f6447789ba141568575a1.tar.gz
cuberite-a9b597087b56b4526a3f6447789ba141568575a1.tar.bz2
cuberite-a9b597087b56b4526a3f6447789ba141568575a1.tar.lz
cuberite-a9b597087b56b4526a3f6447789ba141568575a1.tar.xz
cuberite-a9b597087b56b4526a3f6447789ba141568575a1.tar.zst
cuberite-a9b597087b56b4526a3f6447789ba141568575a1.zip
Diffstat (limited to 'src/PolarSSL++/CryptoKey.h')
-rw-r--r--src/PolarSSL++/CryptoKey.h76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/PolarSSL++/CryptoKey.h b/src/PolarSSL++/CryptoKey.h
new file mode 100644
index 000000000..9c298e501
--- /dev/null
+++ b/src/PolarSSL++/CryptoKey.h
@@ -0,0 +1,76 @@
+
+// CryptoKey.h
+
+// Declares the cCryptoKey class representing a RSA public key in PolarSSL
+
+
+
+
+
+#pragma once
+
+#include "CtrDrbgContext.h"
+#include "polarssl/pk.h"
+
+
+
+
+
+class cCryptoKey
+{
+ friend class cSslContext;
+
+public:
+ /** Constructs an empty key instance. Before use, it needs to be filled by ParsePublic() or ParsePrivate() */
+ cCryptoKey(void);
+
+ /** Constructs the public key out of the DER- or PEM-encoded pubkey data */
+ cCryptoKey(const AString & a_PublicKeyData);
+
+ /** Constructs the private key out of the DER- or PEM-encoded privkey data, with the specified password.
+ If a_Password is empty, no password is assumed. */
+ cCryptoKey(const AString & a_PrivateKeyData, const AString & a_Password);
+
+ ~cCryptoKey();
+
+ /** Decrypts the data using the stored public key
+ Both a_EncryptedData and a_DecryptedData must be at least <KeySizeBytes> bytes large.
+ Returns the number of bytes decrypted, or negative number for error. */
+ int Decrypt(const Byte * a_EncryptedData, size_t a_EncryptedLength, Byte * a_DecryptedData, size_t a_DecryptedMaxLength);
+
+ /** Encrypts the data using the stored public key
+ Both a_EncryptedData and a_DecryptedData must be at least <KeySizeBytes> bytes large.
+ Returns the number of bytes decrypted, or negative number for error. */
+ int Encrypt(const Byte * a_PlainData, size_t a_PlainLength, Byte * a_EncryptedData, size_t a_EncryptedMaxLength);
+
+ /** Parses the specified data into a public key representation.
+ The key can be DER- or PEM-encoded.
+ Returns 0 on success, PolarSSL error code on failure. */
+ int ParsePublic(const void * a_Data, size_t a_NumBytes);
+
+ /** Parses the specified data into a private key representation.
+ If a_Password is empty, no password is assumed.
+ The key can be DER- or PEM-encoded.
+ Returns 0 on success, PolarSSL error code on failure. */
+ int ParsePrivate(const void * a_Data, size_t a_NumBytes, const AString & a_Password);
+
+ /** Returns true if the contained key is valid. */
+ bool IsValid(void) const;
+
+protected:
+ /** The PolarSSL representation of the key data */
+ pk_context m_Pk;
+
+ /** The random generator used in encryption and decryption */
+ cCtrDrbgContext m_CtrDrbg;
+
+
+ /** Returns the internal context ptr. Only use in PolarSSL API calls. */
+ pk_context * GetInternal(void) { return &m_Pk; }
+} ;
+
+typedef SharedPtr<cCryptoKey> cCryptoKeyPtr;
+
+
+
+