libDwmAuth-0.1.0
DwmAuthPKCrypto.hh
Go to the documentation of this file.
1 //===========================================================================
2 // @(#) $DwmPath: dwm/libDwmAuth/tags/libDwmAuth-0.1.0/include/DwmAuthPKCrypto.hh 8992 $
3 // @(#) $Id: DwmAuthPKCrypto.hh 8992 2017-04-08 13:05:45Z 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 "DwmReadable.hh"
56 #include "DwmWritable.hh"
57 
58 namespace Dwm {
59 
60  namespace Auth {
61 
62  //------------------------------------------------------------------------
64  //------------------------------------------------------------------------
65  class PKCrypto
66  {
67  public:
68  //----------------------------------------------------------------------
70  //----------------------------------------------------------------------
71  class PublicKey
72  : public CryptoPP::RSA::PublicKey,
73  public Dwm::Readable, public Dwm::Writable
74  {
75  public:
76  PublicKey()
77  : CryptoPP::RSA::PublicKey()
78  {}
79 
80  PublicKey(const CryptoPP::RSA::PublicKey & key)
81  : CryptoPP::RSA::PublicKey(key)
82  {}
83 
84  bool IsValid() const;
85 
86  std::istream & Read(std::istream & is);
87  ssize_t Read(int fd);
88  size_t Read(FILE * f);
89 
90  std::ostream & Write(std::ostream & os) const;
91  ssize_t Write(int fd) const;
92  size_t Write(FILE *f) const;
93  uint32_t StreamedLength() const;
94 
95  std::string ToString() const;
96  std::string ToBase64String() const;
97  bool FromString(const std::string & s);
98  bool FromBase64String(const std::string & s);
99 
100  //--------------------------------------------------------------------
102  //--------------------------------------------------------------------
103  bool operator == (const PublicKey & pk) const;
104  };
105 
106  //----------------------------------------------------------------------
108  //----------------------------------------------------------------------
110  : public CryptoPP::RSA::PrivateKey,
111  public Dwm::Readable, public Dwm::Writable
112  {
113  public:
114  PrivateKey()
115  : CryptoPP::RSA::PrivateKey()
116  {}
117 
118  PrivateKey(const CryptoPP::RSA::PrivateKey & key)
119  : CryptoPP::RSA::PrivateKey(key)
120  {}
121 
122  bool IsValid() const;
123 
124  std::istream & Read(std::istream & is);
125  ssize_t Read(int fd);
126  size_t Read(FILE * f);
127 
128  std::ostream & Write(std::ostream & os) const;
129  ssize_t Write(int fd) const;
130  size_t Write(FILE *f) const;
131  uint32_t StreamedLength() const;
132 
133  std::string ToString() const;
134  std::string ToBase64String() const;
135  bool FromString(const std::string & s);
136 
137  //--------------------------------------------------------------------
139  //--------------------------------------------------------------------
140  bool operator == (const PrivateKey & pk) const;
141  };
142 
143  //----------------------------------------------------------------------
145  //----------------------------------------------------------------------
146  class KeyPair
147  : public std::pair<PrivateKey,PublicKey>
148  {
149  public:
150  //--------------------------------------------------------------------
152  //--------------------------------------------------------------------
154  : std::pair<PrivateKey,PublicKey>()
155  {}
156 
157  //--------------------------------------------------------------------
159  //--------------------------------------------------------------------
160  KeyPair(const PrivateKey & privKey, const PublicKey & publicKey)
161  : std::pair<PrivateKey,PublicKey>(privKey,publicKey)
162  {}
163 
164  //--------------------------------------------------------------------
166  //--------------------------------------------------------------------
167  KeyPair & operator = (const KeyPair & keys)
168  {
169  if (this != &keys) {
170  this->first = keys.first;
171  this->second = keys.second;
172  }
173  return *this;
174  }
175 
176  //--------------------------------------------------------------------
178  //--------------------------------------------------------------------
179  const PrivateKey & Private() const
180  {
181  return first;
182  }
183 
184  //--------------------------------------------------------------------
186  //--------------------------------------------------------------------
188  {
189  return first;
190  }
191 
192  //--------------------------------------------------------------------
194  //--------------------------------------------------------------------
195  const PublicKey & Public() const
196  {
197  return second;
198  }
199 
200  //--------------------------------------------------------------------
202  //--------------------------------------------------------------------
204  {
205  return second;
206  }
207 
208  bool Load(const std::string & privKeyPath, std::string & name);
209  };
210 
211  //----------------------------------------------------------------------
214  //----------------------------------------------------------------------
215  static KeyPair CreateKeyPair(unsigned int keySize);
216 
217  //----------------------------------------------------------------------
220  //----------------------------------------------------------------------
221  static std::string
222  Encrypt(const PublicKey & publicKey, const std::string & plainstr);
223 
224  //----------------------------------------------------------------------
227  //----------------------------------------------------------------------
228  static std::string
229  Decrypt(const PrivateKey & privateKey, const std::string & cipherstr);
230 
231  };
232 
233  } // namespace Auth
234 
235 } // namespace Dwm
236 
237 #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.
const PublicKey & Public() const
Returns a const reference to the public key.
Definition: DwmAuthPKCrypto.hh:195
STL namespace.
static std::string Decrypt(const PrivateKey &privateKey, const std::string &cipherstr)
Decrypts the given cipherstr using the given privateKey and returns the decrypted string...
static KeyPair CreateKeyPair(unsigned int keySize)
Generate a private/public key pair.
Encapsulates a private/public key pair.
Definition: DwmAuthPKCrypto.hh:146
Definition: DwmAuth.hh:48
KeyPair(const PrivateKey &privKey, const PublicKey &publicKey)
Construct from a private and public key.
Definition: DwmAuthPKCrypto.hh:160
const PrivateKey & Private() const
Returns a const reference to the private key.
Definition: DwmAuthPKCrypto.hh:179
Public key crypto utilities.
Definition: DwmAuthPKCrypto.hh:65
KeyPair()
Default constructor.
Definition: DwmAuthPKCrypto.hh:153
bool operator==(const PublicKey &pk) const
operator ==
PrivateKey & Private()
Returns a mutable reference to the private key.
Definition: DwmAuthPKCrypto.hh:187
PublicKey & Public()
Returns a mutable reference to the public key.
Definition: DwmAuthPKCrypto.hh:203
Encapsulates a readable and writable private key.
Definition: DwmAuthPKCrypto.hh:109
Encapsulates a readable and writable public key.
Definition: DwmAuthPKCrypto.hh:71