summaryrefslogtreecommitdiffstats
path: root/src/PolarSSL++/CryptoKey.h
blob: db15cefb4bd8a14e2ca8a08296eb791c42c214bf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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 std::shared_ptr<cCryptoKey> cCryptoKeyPtr;