sipxportlib  Version 3.3
UtlCryptoKey.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2008 SIPfoundry Inc.
3 // Licensed by SIPfoundry under the LGPL license.
4 //
5 // Copyright (C) 2008 SIPez LLC.
6 // Licensed to SIPfoundry under a Contributor Agreement.
7 //
8 // Copyright (C) 2008 Mutualink, Inc.
9 // Licensed to SIPfoundry under a Contributor Agreement.
10 //
11 // $$
13 
14 #ifndef _UtlCryptoKey_h_
15 #define _UtlCryptoKey_h_
16 
17 #include "utl/UtlString.h"
18 #include "utl/UtlCryptoData.h"
19 
20 // EXTERNAL FUNCTIONS
21 // EXTERNAL VARIABLES
22 // CONSTANTS
23 // TYPEDEFS
24 typedef struct rsa_st RSA;
25 typedef struct evp_cipher_st EVP_CIPHER;
26 typedef struct env_md_st EVP_MD;
27 
28 // DEFINES
29 // MACROS
30 // STATIC VARIABLE INITIALIZATIONS
31 
32 
37 {
38 /* //////////////////////////////// PUBLIC //////////////////////////////// */
39 public:
40 
42  enum KeyType
43  {
49  };
50 
51 /* =============================== CREATORS =============================== */
53 
54 
56  UtlCryptoKey();
57 
59  virtual ~UtlCryptoKey();
60 
61  // Key loading & retrieval
62 
64  virtual int generateKey() = 0;
69  virtual int importFromFile(const char* pFilename);
75  virtual int loadBinaryKey(const unsigned char* pSrc,
77  int srcLen);
78 
80  virtual UtlCryptoData* getBinaryKey() const;
81 
82  // Encryption & decryption
83 
85  virtual int getMaxEncryptedSize(int srcLen) const = 0;
86 
88  virtual int encrypt(const unsigned char* pSrc,
89  int srcLen,
90  unsigned char* pDest,
91  int* pDestLen) const = 0;
102  virtual UtlCryptoData* encrypt(const unsigned char* pSrc,
104  int srcLen) const;
113  virtual int getMaxDecryptedSize(int srcLen) const = 0;
115 
116 
118  virtual int decrypt(const unsigned char* pSrc,
119  int srcLen,
120  unsigned char* pDest,
121  int* pDestLen) const = 0;
132  virtual UtlCryptoData* decrypt(const unsigned char* pSrc,
134  int srcLen) const;
143  virtual int getMaxSignatureSize(int srcLen) const;
145 
147  virtual int sign(const unsigned char* pSrc,
148  int srcLen,
149  unsigned char* pDest,
150  int* pDestLen) const;
161  virtual UtlCryptoData* sign(const unsigned char* pSrc,
163  int srcLen) const;
172  virtual int verify(const unsigned char* pSrc,
174  int srcLen,
175  const unsigned char* pSig,
176  int sigLen) const;
187 
188  /* ============================== ACCESSORS =============================== */
190 
191 
192  inline bool isValid() const;
193  inline bool isPrivate() const;
194  inline bool isPublic() const;
195  inline bool isSymmetric() const;
196  inline KeyType getKeyType() const;
197  inline unsigned long getLastError() const;
198 
199  virtual UtlString output() const;
200 
202  // STATICS
203 
205  static int getDigestAlgType();
206 
208  static int getMaxDigestSize(int srcLen);
209 
211  static int computeDigest(const unsigned char* pSrc,
212  int srcLen,
213  unsigned char* pDest,
214  int* pDestLen);
225  static UtlCryptoData* computeDigest(const unsigned char* pSrc,
227  int srcLen);
235  static int getMaxBase64EncodedSize(int srcLen);
237 
239  static int base64Encode(const unsigned char* pSrc,
240  int srcLen,
241  unsigned char* pDest,
242  int* pDestLen);
253  static UtlString base64Encode(const unsigned char* pSrc,
255  int srcLen);
263  static int getMaxBase64DecodedSize(int srcLen);
265 
267  static int base64Decode(const unsigned char* pSrc,
268  int srcLen,
269  unsigned char* pDest,
270  int* pDestLen);
281  static int base64Decode(const UtlString& pSrc,
283  unsigned char* pDest,
284  int* pDestLen);
294  static UtlCryptoData* base64Decode(const unsigned char* pSrc,
296  int srcLen);
304  static UtlCryptoData* base64Decode(const UtlString& pSrc);
312 protected:
313 
314  inline KeyType setKeyType(KeyType type);
315  unsigned long setLastError(unsigned long err) const;
316  virtual void clearKey();
317 
319  static int getBase64Idx(unsigned char c);
320 
322  static bool isBase64(unsigned char c);
323 
324  static const char sBase64Chars[];
325 
326 private:
327 
328  KeyType mKeyType;
329  mutable unsigned long mLastErr;
330 
331  // STATICS
332  static const EVP_MD* spMdAlg;
333 };
334 
335 
336 /* ============================ INLINE METHODS ============================ */
337 
339 {
340  return mKeyType != KEY_INVALID;
341 }
342 
344 {
345  return mKeyType == KEY_PRIVATE;
346 }
347 
349 {
350  return mKeyType == KEY_PUBLIC;
351 }
352 
354 {
355  return mKeyType == KEY_SYMMETRIC;
356 }
357 
359 {
360  return mKeyType;
361 }
362 
363 unsigned long UtlCryptoKey::getLastError() const
364 {
365  return mLastErr;
366 }
367 
369 {
370  mKeyType=type;
371  return type;
372 }
373 
374 
375 #endif // Include guard
376 
377 
virtual int getMaxEncryptedSize(int srcLen) const =0
Returns the max encrypted size of srcLen bytes from encrypt()
This is a symmetric key.
Definition: UtlCryptoKey.h:45
static int getMaxBase64EncodedSize(int srcLen)
Returns the max base64-encoded size of srcLen bytes from base64Encode.
struct rsa_st RSA
Definition: UtlCryptoKey.h:24
A generic cryptography key base class.
Definition: UtlCryptoKey.h:36
virtual int importFromFile(const char *pFilename)
Imports a key from the given file.
static int getMaxDigestSize(int srcLen)
Returns the max size of a digest that computeDigest() will return.
bool isValid() const
Definition: UtlCryptoKey.h:338
virtual int verify(const unsigned char *pSrc, int srcLen, const unsigned char *pSig, int sigLen) const
Verifies that the signature is valid for the source data.
UtlCryptoKey()
Constructor.
virtual int loadBinaryKey(const unsigned char *pSrc, int srcLen)
Loads a binary key value.
static int getDigestAlgType()
Returns the digest algorithm type that computeDigest() will return.
virtual int generateKey()=0
Generates a new RSA private/public key pair.
virtual void clearKey()
static int base64Encode(const unsigned char *pSrc, int srcLen, unsigned char *pDest, int *pDestLen)
Encodes the given binary data in base64 format.
struct env_md_st EVP_MD
Definition: UtlCryptoKey.h:26
virtual UtlString output() const
virtual ~UtlCryptoKey()
Destructor.
virtual UtlCryptoData * getBinaryKey() const
Retrieves a binary key value (for later use by loadBinaryKey)
Crypto data operations.
Definition: UtlCryptoData.h:37
static int getBase64Idx(unsigned char c)
Decode base64 character.
static int getMaxBase64DecodedSize(int srcLen)
Returns the max decoded size of srcLen bytes from base64Decode.
static int base64Decode(const unsigned char *pSrc, int srcLen, unsigned char *pDest, int *pDestLen)
Decodes the given base64 data into binary format.
virtual int encrypt(const unsigned char *pSrc, int srcLen, unsigned char *pDest, int *pDestLen) const =0
Encrypts the given source data with the current key.
Number of key types defined.
Definition: UtlCryptoKey.h:48
KeyType setKeyType(KeyType type)
Definition: UtlCryptoKey.h:368
Definition: UtlString.h:48
bool isPublic() const
Definition: UtlCryptoKey.h:348
virtual int decrypt(const unsigned char *pSrc, int srcLen, unsigned char *pDest, int *pDestLen) const =0
Decrypts the given source data with the current key.
This is a private key (also includes a public key)
Definition: UtlCryptoKey.h:46
virtual int getMaxDecryptedSize(int srcLen) const =0
Returns the max decrypted size of srcLen bytes from decrypt()
static const char sBase64Chars[]
Set of base64 chars.
Definition: UtlCryptoKey.h:324
unsigned long setLastError(unsigned long err) const
static int computeDigest(const unsigned char *pSrc, int srcLen, unsigned char *pDest, int *pDestLen)
Computes message digest (MD) of given data.
KeyType
What type of Key is this?
Definition: UtlCryptoKey.h:42
static bool isBase64(unsigned char c)
Returns true if the given char is a base64 char.
virtual int sign(const unsigned char *pSrc, int srcLen, unsigned char *pDest, int *pDestLen) const
Signs source data.
bool isPrivate() const
Definition: UtlCryptoKey.h:343
This is only a public key.
Definition: UtlCryptoKey.h:47
This key is not currently valid.
Definition: UtlCryptoKey.h:44
virtual int getMaxSignatureSize(int srcLen) const
Signing & verifying.
KeyType getKeyType() const
Definition: UtlCryptoKey.h:358
struct evp_cipher_st EVP_CIPHER
Definition: UtlCryptoKey.h:25
bool isSymmetric() const
Definition: UtlCryptoKey.h:353
unsigned long getLastError() const
Definition: UtlCryptoKey.h:363