sipxportlib  Version 3.3
Public Types | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Static Protected Attributes | List of all members
UtlCryptoKey Class Referenceabstract

A generic cryptography key base class. More...

#include <UtlCryptoKey.h>

Inheritance diagram for UtlCryptoKey:
Inheritance graph
[legend]

Public Types

enum  KeyType {
  KEY_INVALID, KEY_SYMMETRIC, KEY_PRIVATE, KEY_PUBLIC,
  NUM_KEY_TYPES
}
 What type of Key is this? More...
 

Public Member Functions

Creators
 UtlCryptoKey ()
 Constructor. More...
 
virtual ~UtlCryptoKey ()
 Destructor. More...
 
virtual int generateKey ()=0
 Generates a new RSA private/public key pair. More...
 
virtual int importFromFile (const char *pFilename)
 Imports a key from the given file. More...
 
virtual int loadBinaryKey (const unsigned char *pSrc, int srcLen)
 Loads a binary key value. More...
 
virtual UtlCryptoDatagetBinaryKey () const
 Retrieves a binary key value (for later use by loadBinaryKey) More...
 
virtual int getMaxEncryptedSize (int srcLen) const =0
 Returns the max encrypted size of srcLen bytes from encrypt() More...
 
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. More...
 
virtual UtlCryptoDataencrypt (const unsigned char *pSrc, int srcLen) const
 Encrypts the given source data with the current key. More...
 
virtual int getMaxDecryptedSize (int srcLen) const =0
 Returns the max decrypted size of srcLen bytes from decrypt() More...
 
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. More...
 
virtual UtlCryptoDatadecrypt (const unsigned char *pSrc, int srcLen) const
 Decrypts the given source data with the current key. More...
 
virtual int getMaxSignatureSize (int srcLen) const
 Signing & verifying. More...
 
virtual int sign (const unsigned char *pSrc, int srcLen, unsigned char *pDest, int *pDestLen) const
 Signs source data. More...
 
virtual UtlCryptoDatasign (const unsigned char *pSrc, int srcLen) const
 Signs source data. More...
 
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. More...
 
Accessors
bool isValid () const
 
bool isPrivate () const
 
bool isPublic () const
 
bool isSymmetric () const
 
KeyType getKeyType () const
 
unsigned long getLastError () const
 
virtual UtlString output () const
 

Static Public Member Functions

static int getDigestAlgType ()
 Returns the digest algorithm type that computeDigest() will return. More...
 
static int getMaxDigestSize (int srcLen)
 Returns the max size of a digest that computeDigest() will return. More...
 
static int computeDigest (const unsigned char *pSrc, int srcLen, unsigned char *pDest, int *pDestLen)
 Computes message digest (MD) of given data. More...
 
static UtlCryptoDatacomputeDigest (const unsigned char *pSrc, int srcLen)
 Computes message digest (MD) of given data. More...
 
static int getMaxBase64EncodedSize (int srcLen)
 Returns the max base64-encoded size of srcLen bytes from base64Encode. More...
 
static int base64Encode (const unsigned char *pSrc, int srcLen, unsigned char *pDest, int *pDestLen)
 Encodes the given binary data in base64 format. More...
 
static UtlString base64Encode (const unsigned char *pSrc, int srcLen)
 Encodes the given binary data in base64 format. More...
 
static int getMaxBase64DecodedSize (int srcLen)
 Returns the max decoded size of srcLen bytes from base64Decode. More...
 
static int base64Decode (const unsigned char *pSrc, int srcLen, unsigned char *pDest, int *pDestLen)
 Decodes the given base64 data into binary format. More...
 
static int base64Decode (const UtlString &pSrc, unsigned char *pDest, int *pDestLen)
 Decodes the given base64 data into binary format. More...
 
static UtlCryptoDatabase64Decode (const unsigned char *pSrc, int srcLen)
 Decodes the given base64 data into binary format. More...
 
static UtlCryptoDatabase64Decode (const UtlString &pSrc)
 Decodes the given base64 data into binary format. More...
 

Protected Member Functions

KeyType setKeyType (KeyType type)
 
unsigned long setLastError (unsigned long err) const
 
virtual void clearKey ()
 

Static Protected Member Functions

static int getBase64Idx (unsigned char c)
 Decode base64 character. More...
 
static bool isBase64 (unsigned char c)
 Returns true if the given char is a base64 char. More...
 

Static Protected Attributes

static const char sBase64Chars []
 Set of base64 chars. More...
 

Detailed Description

A generic cryptography key base class.

Member Enumeration Documentation

enum KeyType

What type of Key is this?

Enumerator
KEY_INVALID 

This key is not currently valid.

KEY_SYMMETRIC 

This is a symmetric key.

KEY_PRIVATE 

This is a private key (also includes a public key)

KEY_PUBLIC 

This is only a public key.

NUM_KEY_TYPES 

Number of key types defined.

Constructor & Destructor Documentation

Constructor.

virtual ~UtlCryptoKey ( )
virtual

Destructor.

Member Function Documentation

virtual int generateKey ( )
pure virtual

Generates a new RSA private/public key pair.

Returns
0 on success, non-0 error code on failure

Implemented in UtlCryptoKeySym, and UtlCryptoKeyRsa.

virtual int importFromFile ( const char *  pFilename)
virtual

Imports a key from the given file.

Returns
0 on success, non-0 error code on failure

Reimplemented in UtlCryptoKeyRsa.

virtual int loadBinaryKey ( const unsigned char *  pSrc,
int  srcLen 
)
virtual

Loads a binary key value.

Reimplemented in UtlCryptoKeySym.

virtual UtlCryptoData* getBinaryKey ( ) const
virtual

Retrieves a binary key value (for later use by loadBinaryKey)

Reimplemented in UtlCryptoKeySym.

virtual int getMaxEncryptedSize ( int  srcLen) const
pure virtual

Returns the max encrypted size of srcLen bytes from encrypt()

Implemented in UtlCryptoKeySym, and UtlCryptoKeyRsa.

virtual int encrypt ( const unsigned char *  pSrc,
int  srcLen,
unsigned char *  pDest,
int *  pDestLen 
) const
pure virtual

Encrypts the given source data with the current key.

Parameters
[in]pSrc- Source data
[in]srcLen- Source data len
[out]pDest- Destination buffer
[in,out]pDestLen- Size of destination buffer and actual encrypted size after operation
Returns
Length of the encrypted data (0 on error)

Implemented in UtlCryptoKeySym, and UtlCryptoKeyRsa.

virtual UtlCryptoData* encrypt ( const unsigned char *  pSrc,
int  srcLen 
) const
virtual

Encrypts the given source data with the current key.

Parameters
[in]pSrc- Source data
[in]srcLen- Source data len
Returns
UtlCryptoData allocated object with encrypted data NOTE: User must free returned object
virtual int getMaxDecryptedSize ( int  srcLen) const
pure virtual

Returns the max decrypted size of srcLen bytes from decrypt()

Implemented in UtlCryptoKeySym, and UtlCryptoKeyRsa.

virtual int decrypt ( const unsigned char *  pSrc,
int  srcLen,
unsigned char *  pDest,
int *  pDestLen 
) const
pure virtual

Decrypts the given source data with the current key.

Parameters
[in]pSrc- Source data
[in]srcLen- Source data len
[out]pDest- Destination buffer
[in,out]pDestLen- Size of destination buffer and actual decrypted size after operation
Returns
Length of the decrypted data (0 on error)

Implemented in UtlCryptoKeySym, and UtlCryptoKeyRsa.

virtual UtlCryptoData* decrypt ( const unsigned char *  pSrc,
int  srcLen 
) const
virtual

Decrypts the given source data with the current key.

Parameters
[in]pSrc- Source data
[in]srcLen- Source data len
Returns
UtlCryptoData allocated object with decrypted data NOTE: User must free returned object
virtual int getMaxSignatureSize ( int  srcLen) const
virtual

Signing & verifying.

Reimplemented in UtlCryptoKeyRsa.

virtual int sign ( const unsigned char *  pSrc,
int  srcLen,
unsigned char *  pDest,
int *  pDestLen 
) const
virtual

Signs source data.

Parameters
[in]pSrc- Source data
[in]srcLen- Source data len
[out]pDest- Destination buffer
[in,out]pDestLen- Size of destination buffer and actual decrypted size after operation
Returns
Length of the decrypted data (0 on error)

Reimplemented in UtlCryptoKeyRsa.

virtual UtlCryptoData* sign ( const unsigned char *  pSrc,
int  srcLen 
) const
virtual

Signs source data.

Parameters
[in]pSrc- Source data
[in]srcLen- Source data len
Returns
Valid signature NOTE: User must free returned object
virtual int verify ( const unsigned char *  pSrc,
int  srcLen,
const unsigned char *  pSig,
int  sigLen 
) const
virtual

Verifies that the signature is valid for the source data.

Parameters
[in]pSrc- Source data
[in]srcLen- Source data len
[in]pSig- Signature data
[in]sigLen- Signature data len
Returns
0 if signature is valid, non-0 if not

Reimplemented in UtlCryptoKeyRsa.

bool isValid ( ) const
inline
bool isPrivate ( ) const
inline
bool isPublic ( ) const
inline
bool isSymmetric ( ) const
inline
UtlCryptoKey::KeyType getKeyType ( ) const
inline
unsigned long getLastError ( ) const
inline
virtual UtlString output ( ) const
virtual

Reimplemented in UtlCryptoKeyRsa, and UtlCryptoKeySym.

static int getDigestAlgType ( )
static

Returns the digest algorithm type that computeDigest() will return.

static int getMaxDigestSize ( int  srcLen)
static

Returns the max size of a digest that computeDigest() will return.

static int computeDigest ( const unsigned char *  pSrc,
int  srcLen,
unsigned char *  pDest,
int *  pDestLen 
)
static

Computes message digest (MD) of given data.

Parameters
[in]pSrc- Source data
[in]srcLen- Source data len
[in]pDest- Destination buffer
[out]pDestLen- Size of destination buffer and actual size after operation
Returns
Valid signature
static UtlCryptoData* computeDigest ( const unsigned char *  pSrc,
int  srcLen 
)
static

Computes message digest (MD) of given data.

Parameters
[in]pSrc- Source data
[in]srcLen- Source data len
Returns
Valid signature
static int getMaxBase64EncodedSize ( int  srcLen)
static

Returns the max base64-encoded size of srcLen bytes from base64Encode.

static int base64Encode ( const unsigned char *  pSrc,
int  srcLen,
unsigned char *  pDest,
int *  pDestLen 
)
static

Encodes the given binary data in base64 format.

Parameters
[in]pSrc- Source data
[in]srcLen- Source data len
[in]pDest- Destination buffer
[out]pDestLen- Size of destination buffer and actual size after operation
Returns
written characters
static UtlString base64Encode ( const unsigned char *  pSrc,
int  srcLen 
)
static

Encodes the given binary data in base64 format.

Parameters
[in]pSrc- Source data
[in]srcLen- Source data len
Returns
Encoded data
static int getMaxBase64DecodedSize ( int  srcLen)
static

Returns the max decoded size of srcLen bytes from base64Decode.

static int base64Decode ( const unsigned char *  pSrc,
int  srcLen,
unsigned char *  pDest,
int *  pDestLen 
)
static

Decodes the given base64 data into binary format.

Parameters
[in]pSrc- Source data
[in]srcLen- Source data len
[in]pDest- Destination buffer
[out]pDestLen- Size of destination buffer and actual size after operation
Returns
written characters
static int base64Decode ( const UtlString pSrc,
unsigned char *  pDest,
int *  pDestLen 
)
static

Decodes the given base64 data into binary format.

Parameters
[in]pSrc- Source data
[in]pDest- Destination buffer
[out]pDestLen- Size of destination buffer and actual size after operation
Returns
Length of the returned decoded data (0 on error)
static UtlCryptoData* base64Decode ( const unsigned char *  pSrc,
int  srcLen 
)
static

Decodes the given base64 data into binary format.

Parameters
[in]pSrc- Source data
[in]srcLen- length of source data
Returns
Decoded data
static UtlCryptoData* base64Decode ( const UtlString pSrc)
static

Decodes the given base64 data into binary format.

Parameters
[in]pSrc- Source data
Returns
Decoded data
UtlCryptoKey::KeyType setKeyType ( KeyType  type)
inlineprotected
unsigned long setLastError ( unsigned long  err) const
protected
virtual void clearKey ( )
protectedvirtual
static int getBase64Idx ( unsigned char  c)
staticprotected

Decode base64 character.

static bool isBase64 ( unsigned char  c)
staticprotected

Returns true if the given char is a base64 char.

Member Data Documentation

const char sBase64Chars[]
staticprotected

Set of base64 chars.