/usr/include/sipxtapi/utl/UtlCryptoKey.h is in libsipxtapi-dev 3.3.0~test17-2.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 | //
// Copyright (C) 2008 SIPfoundry Inc.
// Licensed by SIPfoundry under the LGPL license.
//
// Copyright (C) 2008 SIPez LLC.
// Licensed to SIPfoundry under a Contributor Agreement.
//
// Copyright (C) 2008 Mutualink, Inc.
// Licensed to SIPfoundry under a Contributor Agreement.
//
// $$
//////////////////////////////////////////////////////////////////////////////
#ifndef _UtlCryptoKey_h_
#define _UtlCryptoKey_h_
#include "utl/UtlString.h"
#include "utl/UtlCryptoData.h"
// EXTERNAL FUNCTIONS
// EXTERNAL VARIABLES
// CONSTANTS
// TYPEDEFS
typedef struct rsa_st RSA;
typedef struct evp_cipher_st EVP_CIPHER;
typedef struct env_md_st EVP_MD;
// DEFINES
// MACROS
// STATIC VARIABLE INITIALIZATIONS
/**
* @brief A generic cryptography key base class
*/
class UtlCryptoKey
{
/* //////////////////////////////// PUBLIC //////////////////////////////// */
public:
/// What type of Key is this?
enum KeyType
{
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
};
/* =============================== CREATORS =============================== */
///@name Creators
//@{
/// Constructor
UtlCryptoKey();
/// Destructor
virtual ~UtlCryptoKey();
// Key loading & retrieval
/// Generates a new RSA private/public key pair
virtual int generateKey() = 0;
/**<
* @return 0 on success, non-0 error code on failure
*/
/// Imports a key from the given file
virtual int importFromFile(const char* pFilename);
/**<
* @return 0 on success, non-0 error code on failure
*/
/// Loads a binary key value
virtual int loadBinaryKey(const unsigned char* pSrc,
int srcLen);
/// Retrieves a binary key value (for later use by loadBinaryKey)
virtual UtlCryptoData* getBinaryKey() const;
// Encryption & decryption
/// Returns the max encrypted size of srcLen bytes from encrypt()
virtual int getMaxEncryptedSize(int srcLen) const = 0;
/// Encrypts the given source data with the current key
virtual int encrypt(const unsigned char* pSrc,
int srcLen,
unsigned char* pDest,
int* pDestLen) const = 0;
/**<
* @param[in] pSrc - Source data
* @param[in] srcLen - Source data len
* @param[out] pDest - Destination buffer
* @param[in,out] pDestLen - Size of destination buffer and
* actual encrypted size after operation
*
* @return Length of the encrypted data (0 on error)
*/
/// Encrypts the given source data with the current key
virtual UtlCryptoData* encrypt(const unsigned char* pSrc,
int srcLen) const;
/**<
* @param[in] pSrc - Source data
* @param[in] srcLen - Source data len
*
* @return UtlCryptoData allocated object with encrypted data
* NOTE: User must free returned object
*/
/// Returns the max decrypted size of srcLen bytes from decrypt()
virtual int getMaxDecryptedSize(int srcLen) const = 0;
/// Decrypts the given source data with the current key
virtual int decrypt(const unsigned char* pSrc,
int srcLen,
unsigned char* pDest,
int* pDestLen) const = 0;
/**<
* @param[in] pSrc - Source data
* @param[in] srcLen - Source data len
* @param[out] pDest - Destination buffer
* @param[in,out] pDestLen - Size of destination buffer and
* actual decrypted size after operation
*
* @return Length of the decrypted data (0 on error)
*/
/// Decrypts the given source data with the current key
virtual UtlCryptoData* decrypt(const unsigned char* pSrc,
int srcLen) const;
/**<
* @param[in] pSrc - Source data
* @param[in] srcLen - Source data len
*
* @return UtlCryptoData allocated object with decrypted data
* NOTE: User must free returned object
*/
/// Signing & verifying
virtual int getMaxSignatureSize(int srcLen) const;
/// Signs source data
virtual int sign(const unsigned char* pSrc,
int srcLen,
unsigned char* pDest,
int* pDestLen) const;
/**<
* @param[in] pSrc - Source data
* @param[in] srcLen - Source data len
* @param[out] pDest - Destination buffer
* @param[in,out] pDestLen - Size of destination buffer and
* actual decrypted size after operation
*
* @return Length of the decrypted data (0 on error)
*/
/// Signs source data
virtual UtlCryptoData* sign(const unsigned char* pSrc,
int srcLen) const;
/**<
* @param[in] pSrc - Source data
* @param[in] srcLen - Source data len
*
* @return Valid signature
* NOTE: User must free returned object
*/
/// Verifies that the signature is valid for the source data
virtual int verify(const unsigned char* pSrc,
int srcLen,
const unsigned char* pSig,
int sigLen) const;
/**<
* @param[in] pSrc - Source data
* @param[in] srcLen - Source data len
* @param[in] pSig - Signature data
* @param[in] sigLen - Signature data len
*
* @return 0 if signature is valid, non-0 if not
*/
//@}
/* ============================== ACCESSORS =============================== */
///@name Accessors
//@{
inline bool isValid() const;
inline bool isPrivate() const;
inline bool isPublic() const;
inline bool isSymmetric() const;
inline KeyType getKeyType() const;
inline unsigned long getLastError() const;
virtual UtlString output() const;
//@}
// STATICS
/// Returns the digest algorithm type that computeDigest() will return
static int getDigestAlgType();
/// Returns the max size of a digest that computeDigest() will return
static int getMaxDigestSize(int srcLen);
/// Computes message digest (MD) of given data
static int computeDigest(const unsigned char* pSrc,
int srcLen,
unsigned char* pDest,
int* pDestLen);
/**<
* @param[in] pSrc - Source data
* @param[in] srcLen - Source data len
* @param[in] pDest - Destination buffer
* @param[out] pDestLen - Size of destination buffer and
* actual size after operation
*
* @return Valid signature
*/
/// Computes message digest (MD) of given data
static UtlCryptoData* computeDigest(const unsigned char* pSrc,
int srcLen);
/**<
* @param[in] pSrc - Source data
* @param[in] srcLen - Source data len
*
* @return Valid signature
*/
/// Returns the max base64-encoded size of srcLen bytes from base64Encode
static int getMaxBase64EncodedSize(int srcLen);
/// Encodes the given binary data in base64 format
static int base64Encode(const unsigned char* pSrc,
int srcLen,
unsigned char* pDest,
int* pDestLen);
/**<
* @param[in] pSrc - Source data
* @param[in] srcLen - Source data len
* @param[in] pDest - Destination buffer
* @param[out] pDestLen - Size of destination buffer and
* actual size after operation
*
* @return written characters
*/
/// Encodes the given binary data in base64 format
static UtlString base64Encode(const unsigned char* pSrc,
int srcLen);
/**<
* @param[in] pSrc - Source data
* @param[in] srcLen - Source data len
*
* @return Encoded data
*/
/// Returns the max decoded size of srcLen bytes from base64Decode
static int getMaxBase64DecodedSize(int srcLen);
/// Decodes the given base64 data into binary format
static int base64Decode(const unsigned char* pSrc,
int srcLen,
unsigned char* pDest,
int* pDestLen);
/**<
* @param[in] pSrc - Source data
* @param[in] srcLen - Source data len
* @param[in] pDest - Destination buffer
* @param[out] pDestLen - Size of destination buffer and
* actual size after operation
*
* @return written characters
*/
/// Decodes the given base64 data into binary format
static int base64Decode(const UtlString& pSrc,
unsigned char* pDest,
int* pDestLen);
/**<
* @param[in] pSrc - Source data
* @param[in] pDest - Destination buffer
* @param[out] pDestLen - Size of destination buffer and
* actual size after operation
*
* @return Length of the returned decoded data (0 on error)
*/
/// Decodes the given base64 data into binary format
static UtlCryptoData* base64Decode(const unsigned char* pSrc,
int srcLen);
/**<
* @param[in] pSrc - Source data
* @param[in] srcLen - length of source data
*
* @return Decoded data
*/
/// Decodes the given base64 data into binary format
static UtlCryptoData* base64Decode(const UtlString& pSrc);
/**<
* @param[in] pSrc - Source data
*
* @return Decoded data
*/
protected:
inline KeyType setKeyType(KeyType type);
unsigned long setLastError(unsigned long err) const;
virtual void clearKey();
/// Decode base64 character
static int getBase64Idx(unsigned char c);
/// Returns true if the given char is a base64 char
static bool isBase64(unsigned char c);
static const char sBase64Chars[]; ///< Set of base64 chars
private:
KeyType mKeyType; ///< What type of key are we?
mutable unsigned long mLastErr; ///< Saved error from last failing method
// STATICS
static const EVP_MD* spMdAlg; ///< Message Digest algorithm to use
};
/* ============================ INLINE METHODS ============================ */
bool UtlCryptoKey::isValid() const
{
return mKeyType != KEY_INVALID;
}
bool UtlCryptoKey::isPrivate() const
{
return mKeyType == KEY_PRIVATE;
}
bool UtlCryptoKey::isPublic() const
{
return mKeyType == KEY_PUBLIC;
}
bool UtlCryptoKey::isSymmetric() const
{
return mKeyType == KEY_SYMMETRIC;
}
UtlCryptoKey::KeyType UtlCryptoKey::getKeyType() const
{
return mKeyType;
}
unsigned long UtlCryptoKey::getLastError() const
{
return mLastErr;
}
UtlCryptoKey::KeyType UtlCryptoKey::setKeyType(KeyType type)
{
mKeyType=type;
return type;
}
#endif // Include guard
|