libDwmAuth-0.3.6
DwmAuthPKCrypto.hh
Go to the documentation of this file.
1 //===========================================================================
2 // @(#) $DwmPath: dwm/libDwmAuth/tags/libDwmAuth-0.3.6/include/DwmAuthPKCrypto.hh 11185 $
3 // @(#) $Id: DwmAuthPKCrypto.hh 11185 2020-09-19 03:32:25Z dwm $
4 //===========================================================================
5 // Copyright (c) Daniel W. McRobb 2016
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions
10 // are met:
11 //
12 // 1. Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 // 2. Redistributions in binary form must reproduce the above copyright
15 // notice, this list of conditions and the following disclaimer in the
16 // documentation and/or other materials provided with the distribution.
17 // 3. The names of the authors and copyright holders may not be used to
18 // endorse or promote products derived from this software without
19 // specific prior written permission.
20 //
21 // IN NO EVENT SHALL DANIEL W. MCROBB BE LIABLE TO ANY PARTY FOR
22 // DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
23 // INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE,
24 // EVEN IF DANIEL W. MCROBB HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
25 // DAMAGE.
26 //
27 // THE SOFTWARE PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND
28 // DANIEL W. MCROBB HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
29 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. DANIEL W. MCROBB MAKES NO
30 // REPRESENTATIONS AND EXTENDS NO WARRANTIES OF ANY KIND, EITHER
31 // IMPLIED OR EXPRESS, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 // WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE,
33 // OR THAT THE USE OF THIS SOFTWARE WILL NOT INFRINGE ANY PATENT,
34 // TRADEMARK OR OTHER RIGHTS.
35 //===========================================================================
36 
37 //---------------------------------------------------------------------------
40 //---------------------------------------------------------------------------
41 
42 #ifndef _DWMAUTHPKCRYPTO_HH_
43 #define _DWMAUTHPKCRYPTO_HH_
44 
45 #include <string>
46 #include <utility>
47 
48 #include <cryptopp/cryptlib.h>
49 #include <cryptopp/aes.h>
50 #include <cryptopp/filters.h>
51 #include <cryptopp/gcm.h>
52 #include <cryptopp/osrng.h>
53 #include <cryptopp/rsa.h>
54 
55 #include "DwmDescriptorIOCapable.hh"
56 #include "DwmFileIOCapable.hh"
57 #include "DwmStreamIOCapable.hh"
58 #include "DwmStreamedLengthCapable.hh"
59 
60 namespace Dwm {
61 
62  namespace Auth {
63 
64  //------------------------------------------------------------------------
66  //------------------------------------------------------------------------
67  class PKCrypto
68  {
69  public:
70  //----------------------------------------------------------------------
72  //----------------------------------------------------------------------
73  class PublicKey
74  : public CryptoPP::RSA::PublicKey,
75  public DescriptorIOCapable, public FileIOCapable,
76  public StreamIOCapable, public StreamedLengthCapable
77  {
78  public:
79  PublicKey()
80  : CryptoPP::RSA::PublicKey()
81  {}
82 
83  PublicKey(const CryptoPP::RSA::PublicKey & key)
84  : CryptoPP::RSA::PublicKey(key)
85  {}
86 
87  bool IsValid() const;
88 
89  std::istream & Read(std::istream & is) override;
90  ssize_t Read(int fd) override;
91  size_t Read(FILE * f) override;
92 
93  std::ostream & Write(std::ostream & os) const override;
94  ssize_t Write(int fd) const override;
95  size_t Write(FILE *f) const override;
96  uint32_t StreamedLength() const override;
97 
98  std::string ToString() const;
99  std::string ToBase64String() const;
100  bool FromString(const std::string & s);
101  bool FromBase64String(const std::string & s);
102 
103  //--------------------------------------------------------------------
105  //--------------------------------------------------------------------
106  bool operator == (const PublicKey & pk) const;
107  };
108 
109  //----------------------------------------------------------------------
111  //----------------------------------------------------------------------
113  : public CryptoPP::RSA::PrivateKey,
114  public DescriptorIOCapable, public FileIOCapable,
115  public StreamIOCapable, public StreamedLengthCapable
116  {
117  public:
118  PrivateKey()
119  : CryptoPP::RSA::PrivateKey()
120  {}
121 
122  PrivateKey(const CryptoPP::RSA::PrivateKey & key)
123  : CryptoPP::RSA::PrivateKey(key)
124  {}
125 
126  bool IsValid() const;
127 
128  std::istream & Read(std::istream & is) override;
129  ssize_t Read(int fd) override;
130  size_t Read(FILE * f) override;
131 
132  std::ostream & Write(std::ostream & os) const override;
133  ssize_t Write(int fd) const override;
134  size_t Write(FILE *f) const override;
135  uint32_t StreamedLength() const override;
136 
137  std::string ToString() const;
138  std::string ToBase64String() const;
139  bool FromString(const std::string & s);
140  bool FromBase64String(const std::string & s);
141 
142  //--------------------------------------------------------------------
144  //--------------------------------------------------------------------
145  bool operator == (const PrivateKey & pk) const;
146  };
147 
148  //----------------------------------------------------------------------
150  //----------------------------------------------------------------------
151  class KeyPair
152  : public std::pair<PrivateKey,PublicKey>
153  {
154  public:
155  //--------------------------------------------------------------------
157  //--------------------------------------------------------------------
159  : std::pair<PrivateKey,PublicKey>()
160  {}
161 
162  //--------------------------------------------------------------------
164  //--------------------------------------------------------------------
165  KeyPair(const PrivateKey & privKey, const PublicKey & publicKey)
166  : std::pair<PrivateKey,PublicKey>(privKey,publicKey)
167  {}
168 
169  //--------------------------------------------------------------------
171  //--------------------------------------------------------------------
172  KeyPair & operator = (const KeyPair & keys)
173  {
174  if (this != &keys) {
175  this->first = keys.first;
176  this->second = keys.second;
177  }
178  return *this;
179  }
180 
181  //--------------------------------------------------------------------
183  //--------------------------------------------------------------------
184  const PrivateKey & Private() const
185  {
186  return first;
187  }
188 
189  //--------------------------------------------------------------------
191  //--------------------------------------------------------------------
193  {
194  return first;
195  }
196 
197  //--------------------------------------------------------------------
199  //--------------------------------------------------------------------
200  const PublicKey & Public() const
201  {
202  return second;
203  }
204 
205  //--------------------------------------------------------------------
207  //--------------------------------------------------------------------
209  {
210  return second;
211  }
212 
213  bool Load(const std::string & privKeyPath, std::string & name);
214  };
215 
216  //----------------------------------------------------------------------
219  //----------------------------------------------------------------------
220  static KeyPair CreateKeyPair(unsigned int keySize);
221 
222  //----------------------------------------------------------------------
225  //----------------------------------------------------------------------
226  static std::string
227  Encrypt(const PublicKey & publicKey, const std::string & plainstr);
228 
229  //----------------------------------------------------------------------
232  //----------------------------------------------------------------------
233  static std::string
234  Decrypt(const PrivateKey & privateKey, const std::string & cipherstr);
235 
236  };
237 
238  } // namespace Auth
239 
240 } // namespace Dwm
241 
242 #endif // _DWMAUTHPKCRYPTO_HH_
static std::string Encrypt(const PublicKey &publicKey, const std::string &plainstr)
Encrypts the given plainstr with the given publicKey and returns the encrypted string.
static std::string Decrypt(const PrivateKey &privateKey, const std::string &cipherstr)
Decrypts the given cipherstr using the given privateKey and returns the decrypted string.
KeyPair & operator=(const KeyPair &keys)
operator =
Definition: DwmAuthPKCrypto.hh:172
static KeyPair CreateKeyPair(unsigned int keySize)
Generate a private/public key pair.
bool operator==(const PublicKey &pk) const
operator ==
Encapsulates a private/public key pair.
Definition: DwmAuthPKCrypto.hh:151
KeyPair(const PrivateKey &privKey, const PublicKey &publicKey)
Construct from a private and public key.
Definition: DwmAuthPKCrypto.hh:165
Public key crypto utilities.
Definition: DwmAuthPKCrypto.hh:67
const PrivateKey & Private() const
Returns a const reference to the private key.
Definition: DwmAuthPKCrypto.hh:184
const PublicKey & Public() const
Returns a const reference to the public key.
Definition: DwmAuthPKCrypto.hh:200
KeyPair()
Default constructor.
Definition: DwmAuthPKCrypto.hh:158
PrivateKey & Private()
Returns a mutable reference to the private key.
Definition: DwmAuthPKCrypto.hh:192
bool operator==(const PrivateKey &pk) const
operator ==
PublicKey & Public()
Returns a mutable reference to the public key.
Definition: DwmAuthPKCrypto.hh:208
Encapsulates a readable and writable private key.
Definition: DwmAuthPKCrypto.hh:112
Encapsulates a readable and writable public key.
Definition: DwmAuthPKCrypto.hh:73