Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cKeyWrap.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cKeyWrap_H
7 #define _INC_cKeyWrap_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "cKeyBase.h"
13 #include "cKeyTypeDef.h"
14 #include "cKeyRSA.h"
15 #include "cKeyECPPair.h"
16 #include "cKeyECDSA.h"
17 
18 #include "../File/cASNWriterRev.h"
19 #include "../Hash/cHashWrap.h"
20 #include "../SSL/SSLTypes.h"
21 
22 namespace GrayLib
23 {
25  {
29 
30  public:
33 
34  static const char* k_pszPEM_RSAPK;
35  static const char* k_pszPEM_EC;
36  static const char* k_pszPEM_PK;
37  static const char* k_pszPEM_EPK;
38  static const char* k_pszPEM_Pub;
39 
40  private:
41  static HRESULT ReadAlgorithmIdentifier(cASNReader& r, SSL_Key_TYPE& eKeyType, OUT cASNBuf& params);
42 
43  HRESULT ReadPrivateKeyPkcs8Enc(const cMemBlock& key, const cMemBlock& password);
44  HRESULT ReadPrivateKeyPkcs8(const cMemBlock& key);
45  HRESULT ReadPrivateKeyPVK(const cMemBlock& key, const cMemBlock& password);
46 
47  public:
49  : m_pKeyTypeDef(nullptr)
50  {
51  }
53  {
54  SetZeroKey();
55  }
56 
57  bool isZeroKey() const
58  {
59  return get_KeyType() == SSL_Key_NONE;
60  }
61  void SetZeroKey();
62 
63  static inline bool GetHashSize(SSL_Hash_TYPE eHashType, size_t* pnHashSize)
64  {
66  if (*pnHashSize != 0)
67  return true;
68  const cHashTypeDef* pHashType = cHashTypeMgr::I().GetHashType(eHashType);
69  if (pHashType == nullptr)
70  return false;
71  *pnHashSize = pHashType->get_HashSize();
72  return true;
73  }
74 
75  SSL_SIG_TYPE get_SSLSIGType() const;
76  static SSL_Key_TYPE GetKeyTypeFromSSLSIG(SSL_SIG_TYPE sig);
77 
78  HRESULT InitKeyType(SSL_Key_TYPE eKeyType);
79 
80  cKeyECPPair* get_ECPPair() const;
81 
82  const char* get_KeyTypeName() const
83  {
86  if ( this->m_pKeyTypeDef == nullptr)
87  return("invalid PK");
88  return(this->m_pKeyTypeDef->m_pszName);
89  }
90 
91  size_t get_KeySize() const
92  {
95  if ( this->m_pKeyBase == nullptr)
96  return 0;
97  return this->m_pKeyBase->get_KeySize();
98  }
99 
100  bool HasKeyType(SSL_Key_TYPE eKeyType) const noexcept
101  {
105  // null or NONE context can't do anything
106  if (this->m_pKeyTypeDef == nullptr)
107  return false;
108  return this->m_pKeyTypeDef->HasKeyType(eKeyType);
109  }
110 
112  {
115  if ( this->m_pKeyTypeDef == nullptr)
116  return SSL_Key_NONE;
117  return this->m_pKeyTypeDef->m_eKeyType;
118  }
119 
121  {
124 
125  if ( this->m_pKeyBase == nullptr)
126  return NTE_BAD_DATA;
127  return this->m_pKeyBase->GetKeyValues(items);
128  }
129 
138  //
139  HRESULT ReadPrivateKey(const cMemBlock& key, const cMemBlock& password);
140 
144  HRESULT ReadPublicKey(cASNReader& r);
145 
154  //
155  HRESULT ReadPublicKey(const BYTE* pKey, size_t nSizeKey);
156 
165  HRESULT ReadPrivateKeyFile(const FILECHAR_t* pszFilePath, const char* pszPassword);
166 
174  HRESULT ReadPublicKeyFile(const FILECHAR_t* pszFilePath);
175 
181  HRESULT WritePrivateKey(cASNWriterRev& wr) const;
182 
188  HRESULT WritePublicKey2(cASNWriterRev& wr) const;
189 
194  HRESULT WritePublicPEM(char* pOut, StrLen_t size) const; // NOT USED
195 
200  HRESULT WritePrivatePEM(char* pOut, StrLen_t size) const; // NOT USED
201 
206  HRESULT WritePublicKey(cASNWriterRev& w) const;
207 
208  HRESULT CheckPublicPrivatePair(const cKeyWrap* pKeyPrivate) const;
209 
216  //
219  //
222  //
226  HRESULT VerifySignature(SSL_Hash_TYPE eHashType, const BYTE* pHash, size_t nSizeHash, const BYTE* pSig, size_t nSizeSig) const;
227 
228  HRESULT VerifySignatureExt(const cKeySigAlg& sigalg, const cKeyRSASignOptions* pSignOptions, const BYTE* pHash, size_t nSizeHash, const cMemBlock& signBuffer) const;
229 
236  //
238  //
241  //
244  //
246  HRESULT MakeSignature(BYTE* pSig, SSL_Hash_TYPE eHashType, const BYTE* pHash, size_t nSizeHash, IRandomNoise* pRandom) const;
247 
256 
257  HRESULT DecryptWithKey(BYTE* pOut, size_t nSizeOut, const BYTE* pInp, size_t nSizeIn, IRandomNoise* pRandom);
258 
267 
268  HRESULT EncryptWithKey(BYTE* pOut, size_t nSizeOut, const BYTE* pInp, size_t nSizeIn, IRandomNoise* pRandom);
269  };
270 }
271 
272 #endif // _INC_cKeyWrap_H
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
Definition: cASN.h:78
Definition: cASNReader.h:22
Definition: cASNWriterRev.h:22
Definition: cHashTypeDef.h:21
size_t get_HashSize() const noexcept
Definition: cHashTypeDef.h:52
Definition: cKeyECPPair.h:24
Definition: cKeyRSA.h:31
Definition: cKeyTypeDef.h:22
SSL_Key_TYPE const m_eKeyType
Public key type.
Definition: cKeyTypeDef.h:27
const char *const m_pszName
Type name.
Definition: cKeyTypeDef.h:28
virtual bool HasKeyType(SSL_Key_TYPE eKeyType) const noexcept=0
Tell if the context implements this type (e.g. ECKEY can do ECDSA)
Definition: cKeyWrap.h:25
static const char * k_pszPEM_RSAPK
"RSA PRIVATE KEY"
Definition: cKeyWrap.h:34
cKeyWrap()
Definition: cKeyWrap.h:48
const char * get_KeyTypeName() const
Definition: cKeyWrap.h:82
static const char * k_pszPEM_EPK
"ENCRYPTED PRIVATE KEY"
Definition: cKeyWrap.h:37
~cKeyWrap()
Definition: cKeyWrap.h:52
static bool GetHashSize(SSL_Hash_TYPE eHashType, size_t *pnHashSize)
Definition: cKeyWrap.h:63
HRESULT GetKeyValues(cKeyBase::cValue *items) const
Definition: cKeyWrap.h:120
static const char * k_pszPEM_PK
"PRIVATE KEY"
Definition: cKeyWrap.h:36
static const char * k_pszPEM_Pub
"PUBLIC KEY"
Definition: cKeyWrap.h:38
const cKeyTypeDef * m_pKeyTypeDef
Metadata about key type.
Definition: cKeyWrap.h:31
static const char * k_pszPEM_EC
"EC PRIVATE KEY"
Definition: cKeyWrap.h:35
size_t get_KeySize() const
Definition: cKeyWrap.h:91
bool isZeroKey() const
Definition: cKeyWrap.h:57
cRefPtr< cKeyBase > m_pKeyBase
Underlying key instance.
Definition: cKeyWrap.h:32
SSL_Key_TYPE get_KeyType() const
Definition: cKeyWrap.h:111
bool HasKeyType(SSL_Key_TYPE eKeyType) const noexcept
Definition: cKeyWrap.h:100
Definition: cMem.h:311
Definition: cRefPtr.h:225
static cHashTypeMgr &__stdcall I()
Definition: cSingleton.h:199
Definition: cMesh.h:22
SSL_SIG_TYPE
Definition: SSLTypes.h:132
SSL_Key_TYPE
Definition: cKeyBase.h:24
@ SSL_Key_NONE
Definition: cKeyBase.h:28
SSL_Hash_TYPE
Definition: cHashCode.h:23
int StrLen_t
the length of a string in chars (bytes for UTF8, wchar_t for UNICODE). or offset in characters....
Definition: StrConst.h:32
char FILECHAR_t
a UTF8 char in a file name. like TCHAR
Definition: FileName.h:22
Definition: cKeyBase.h:74
Definition: cKeyBase.h:40
Definition: cRandom.h:19