Sleds/libeye/eyeutils.h

213 lines
8.5 KiB
C
Raw Normal View History

2025-03-13 21:28:38 +00:00
// Copyright (c) 2013-2015 IONU Security, Inc. All rights reserved.
//
// Utility functions and classes
#ifndef eyeutils_h
#define eyeutils_h
#include <string>
#include <openssl/evp.h>
#include "eyeconstants.h"
#include "eyering.h"
#ifndef LIBEYE_DLL
#ifdef WIN32
# ifdef DLLEXPORT
# define LIBEYE_DLL __declspec(dllexport)
# else
# define LIBEYE_DLL __declspec(dllimport)
# endif
#else
#define LIBEYE_DLL
#endif
#endif
#ifdef WIN32
#include <Windows.h>
#endif
namespace sequencelogic {
static const char MESSAGE_DIGEST_MD5[] = "md5";
static const char MESSAGE_DIGEST_SHA1[] = "sha1";
static const char MESSAGE_DIGEST_SHA224[] = "sha224";
static const char MESSAGE_DIGEST_SHA256[] = "sha256";
static const char MESSAGE_DIGEST_SHA384[] = "sha384";
static const char MESSAGE_DIGEST_SHA512[] = "sha512";
static const char CIPHER_AES128CBC[] = "aes128cbc";
static const char CIPHER_AES256CBC[] = "aes256cbc";
static const char CIPHER_AES256GCM[] = "aes256gcm";
// Globals, all access is protected by ionu_policy_mutex
class EyeJSONObject;
extern EyeJSONObject* GlobalPolicy;
extern long GlobalPolicyTGIExpiration; // Default to one day
typedef std::string UTF8String;
// Utility functions
LIBEYE_DLL bool IntToOctal (size_t value, char* buffer, size_t digits);
LIBEYE_DLL size_t OctalToInt (const char* value, size_t len);
LIBEYE_DLL bool BinaryToHex (const unsigned char *data, size_t len, char* hex);
LIBEYE_DLL bool HexToBinary (const char* hex, unsigned char *data);
LIBEYE_DLL bool HexToBinary (const char* hex, size_t bytes, unsigned char *data);
LIBEYE_DLL unsigned char HexDigitToValue (const char digit);
LIBEYE_DLL bool Base32Encode (const unsigned char *input, size_t len, char *output);
LIBEYE_DLL size_t Base32Decode (const char* base32, unsigned char *data);
LIBEYE_DLL bool Base64Encode (const unsigned char *input, size_t len, char *output);
LIBEYE_DLL size_t Base64Decode (const char *input, unsigned char *output);
// Format a time_t struct as ISO 8601 formatted string (20 characters)
LIBEYE_DLL std::string GetISO8601Time (time_t t);
// Get the current time as a long in milliseconds for timing and performance work
LIBEYE_DLL long GetSystemTime ();
LIBEYE_DLL bool XorKeys (const unsigned char* k1, const unsigned char* k2, unsigned char* ok, size_t len);
// Safer versions of standard functions
LIBEYE_DLL unsigned char* New (size_t bytes);
LIBEYE_DLL char* StrDup (const char* str);
LIBEYE_DLL int StrCmp (const char* str1, const char* str2);
LIBEYE_DLL int StrnCmp (const char* str1, const char* str2, size_t len);
LIBEYE_DLL size_t StrLen (const char* str, size_t max);
LIBEYE_DLL void StrCpy (char* dst, const char* src);
LIBEYE_DLL void StrCat (char* dst, const char* src);
LIBEYE_DLL int MemCmp (const void* mem1, const void* mem2, size_t len);
LIBEYE_DLL void MemCpy (void* dst, const void* src, size_t len);
LIBEYE_DLL void MemSet (volatile void* mem, unsigned char val, size_t len);
// Check string data to see if it contains any invalid coding characters
LIBEYE_DLL bool IsStrAscii (const char *s, size_t len);
LIBEYE_DLL bool IsStrUTF8 (const char* s, size_t len);
// Internal convenience routines for performance
size_t Decryptor (EVP_CIPHER_CTX* ctx, size_t len, const unsigned char* ciphertext,
unsigned char* plaintext, const unsigned char* key,
const unsigned char* iv);
size_t Encryptor (EVP_CIPHER_CTX* ctx, size_t len, const unsigned char* plaintext,
unsigned char* ciphertext, const unsigned char* key,
const unsigned char* iv);
// Message digest and cipher name to function pointer
LIBEYE_DLL const EVP_MD* GetMessageDigester (const char *digestName);
LIBEYE_DLL const EVP_CIPHER* GetCipher (const char *cipherName);
// Message digest functions that return hex encoded signatures
LIBEYE_DLL std::string DigestMessage (const char* digest, const unsigned char* msg, size_t len);
LIBEYE_DLL std::string DigestFile (const char* digest, const std::string& filename);
LIBEYE_DLL std::string HMACMessage (const unsigned char* key, size_t klen, const unsigned char* msg, size_t mlen);
// Check a filename to help ensure that it is valid
LIBEYE_DLL bool IsValidFilename (const char* filename);
LIBEYE_DLL bool IsValidFilename (const std::string& filename);
// Path utility functions
LIBEYE_DLL bool IsLockFile (const std::string& filename);
LIBEYE_DLL std::string Canonicalise (const std::string& path, bool lowercase = true);
LIBEYE_DLL int CompareFilenames (const std::string& f1, const std::string& f2);
LIBEYE_DLL std::string StripPath (const std::string& filename);
LIBEYE_DLL std::string TempFilename (const std::string& filename, const std::string& suffix);
// Check a filename to ensure that it is valid and can be opened for read
LIBEYE_DLL bool CanReadFile (const char* filename);
LIBEYE_DLL bool CanReadFile (const std::string& filename);
LIBEYE_DLL std::string EncryptFilename (const std::string& filename, const Key& key);
LIBEYE_DLL std::string DecryptFilename (const std::string& filename, const Key& key);
LIBEYE_DLL bool CopyFiles (const std::string& src, const std::string& dst);
// Rename and remove a file, clean up locks on old file
LIBEYE_DLL bool RenameFile (const std::string& src, const std::string& dst);
LIBEYE_DLL bool RemoveFile (const std::string& src);
// Split a string into tokens
LIBEYE_DLL std::vector<std::string> SplitString (const std::string& value, const char separator);
// Replace one or more occurances of search with target substring
LIBEYE_DLL void ReplaceStringInSitu (std::string& target, const std::string& search, const std::string& replace);
// Check to ensure the urn is valid - urn:sl:VAN:PMO:device:document
LIBEYE_DLL bool IsValidURN (const char* urn);
// Guess at the mimetype, using extension and file contents
LIBEYE_DLL const std::string GuessMimeType (const std::string& filename);
// Functions for password based operations
// return 0 (null/empty) to 1.0 (highest strength)
LIBEYE_DLL int GetRounds (const char* password);
LIBEYE_DLL double PassWordStrength (const char* clearPassword);
LIBEYE_DLL std::string SlowHash (const std::string& password, const std::string& key);
LIBEYE_DLL bool DeriveKey (const char* password, unsigned char* key);
LIBEYE_DLL bool GenerateCGKey (const char* password, unsigned char* key, unsigned char* halfkey);
LIBEYE_DLL bool ValidateCGKey (const char* password, const unsigned char* key, const unsigned char* halfkey);
LIBEYE_DLL bool DeriveUserKey (const char* password, unsigned char* userkey, const unsigned char* halfkey);
// Random data and id/password generation utilities
LIBEYE_DLL bool RandomBytes (size_t bytes, unsigned char* buffer);
LIBEYE_DLL std::string RandomBase32 (size_t length);
LIBEYE_DLL std::string RandomBase64 (size_t length);
LIBEYE_DLL std::string RandomPassWord (size_t len, const char* charset = nullptr);
// Compression functions
LIBEYE_DLL bool IsGzipFile (const std::string& filename);
LIBEYE_DLL bool Compress (unsigned char* dest, size_t* dest_len, const unsigned char* src, size_t src_len);
LIBEYE_DLL bool Uncompress (unsigned char* dest, size_t* dest_len, const unsigned char* src, size_t src_len);
// Debug utilities
LIBEYE_DLL void DumpAsBinary (unsigned char* data, size_t bytes);
class EyeURN
{
public:
enum URN_FIELD {
URN = 0,
IONU = 1,
VAN = 2,
PMO = 3,
DEV = 4,
DOC = 5
};
LIBEYE_DLL EyeURN (const char* urn);
~EyeURN() {}
LIBEYE_DLL char* GetURN();
bool IsValid() {return _isValid;}
void RemoveDocument() {_doc[0] = '\0';}
void RemoveDevice() {_dev[0] = '\0';}
void RemovePMO() {_pmo[0] = '\0';}
void RemoveVAN() {_van[0] = '\0';}
private:
bool _isValid;
char _van[SL_URN_VAN_LEN + 1];
char _pmo[SL_URN_PMO_LEN + 1];
char _dev[SL_URN_DEV_LEN + 1];
char _doc[SL_URN_DOC_LEN + 1];
};
#ifdef WIN32
LIBEYE_DLL bool GetLogonFromToken (HANDLE hToken, LPSTR lpName, LPSTR lpDomain);
LIBEYE_DLL std::string GetProcessOwner (DWORD processId);
LIBEYE_DLL std::string GetProcessName (DWORD processId);
#else
std::string GetProcessOwner (pid_t processId);
std::string GetProcessName (pid_t processId);
#endif
/**
* Some more then common functions used in a number of places, in the Explorer integration code.
*/
/**
* Convert from std::string to std::wstring
*/
LIBEYE_DLL std::wstring toWString(const std::string &str, const std::locale &loc = std::locale());
/**
* Convert from std::wstring to std::string
*/
LIBEYE_DLL std::string toAString(const std::wstring &wstr, const std::locale &loc = std::locale());
LIBEYE_DLL std::string GetLastErrorMessage();
LIBEYE_DLL std::string GetErrorMessage(int nErrorCode);
} //namespace sequencelogic
#endif