41 #ifndef _UCOMMON_SECURE_H_
42 #define _UCOMMON_SECURE_H_
44 #ifndef _UCOMMON_CONFIG_H_
48 #ifndef _UCOMMON_UCOMMON_H_
52 #define MAX_CIPHER_KEYSIZE 512
53 #define MAX_DIGEST_HASHSIZE 512
68 typedef enum {OK=0, INVALID, MISSING_CERTIFICATE, MISSING_PRIVATEKEY, INVALID_CERTIFICATE, INVALID_AUTHORITY, INVALID_PEERNAME, INVALID_CIPHER} error_t;
76 inline secure() {error = OK;}
107 static bool init(
void);
115 static bool fips(
void);
122 static int oscerts(
const char *path);
128 static const char *oscerts(
void);
139 static error_t verify(session_t session,
const char *peername = NULL);
150 static server_t server(
const char *
keyfile = NULL,
const char *authority = NULL);
158 static client_t client(
const char *authority = NULL);
166 static client_t user(
const char *authority);
173 static void cipher(
secure *context,
const char *ciphers);
180 {
return error == OK;};
193 static void uuid(
char *
string);
197 template <
typename T>
198 inline static void erase(T *
object)
199 {memset(
object, 0,
sizeof(T));
delete object;}
201 inline operator bool()
const
204 inline bool operator!()
const
205 {
return !is_valid();}
236 void open(
const char *host,
const char *service,
size_t size = 536);
242 size_t _push(
const char *
address,
size_t size);
244 size_t _pull(
char *address,
size_t size);
250 inline bool is_secure(
void)
const
251 {
return bio != NULL;}
266 typedef enum {ENCRYPT = 1, DECRYPT = 0} mode_t;
281 const void *algotype;
286 const void *hashtype;
293 unsigned char keybuf[MAX_CIPHER_KEYSIZE / 8], ivbuf[MAX_CIPHER_KEYSIZE / 8];
296 size_t keysize, blksize;
298 Key(
const char *cipher);
301 void set(
const char *cipher);
303 void set(
const char *cipher,
const char *digest);
305 void assign(
const char *key,
size_t size,
const unsigned char *salt,
unsigned rounds);
308 Key(
const char *cipher,
const char *digest,
const char *text,
size_t size = 0,
const unsigned char *salt = NULL,
unsigned rounds = 1);
310 Key(
const char *cipher,
const char *digest);
314 void assign(
const char *key,
size_t size = 0);
318 inline size_t size(
void)
const
321 inline size_t iosize(
void)
const
324 inline operator bool()
const
325 {
return keysize > 0;}
327 inline bool operator!()
const
328 {
return keysize == 0;}
330 inline Key& operator=(
const char *pass)
331 {assign(pass);
return *
this;}
333 static void options(
const unsigned char *salt = NULL,
unsigned rounds = 1);
340 size_t bufsize, bufpos;
342 unsigned char *bufaddr;
346 virtual void push(
unsigned char *address,
size_t size);
353 Cipher(key_t key, mode_t mode,
unsigned char *address = NULL,
size_t size = 0);
357 void set(
unsigned char *address,
size_t size = 0);
359 void set(key_t key, mode_t mode,
unsigned char *address,
size_t size = 0);
375 size_t put(
const unsigned char *data,
size_t size);
383 size_t puts(
const char *
string);
396 size_t pad(
const unsigned char *address,
size_t size);
406 size_t process(
unsigned char *address,
size_t size,
bool flag =
false);
408 inline size_t size(
void)
const
411 inline size_t pos(
void)
const
414 inline size_t align(
void)
const
415 {
return keys.iosize();}
422 static bool has(
const char *name);
437 const void *hashtype;
442 unsigned char buffer[MAX_DIGEST_HASHSIZE / 8];
443 char textbuf[MAX_DIGEST_HASHSIZE / 8 + 1];
455 inline bool puts(
const char *str)
456 {
return put(str, strlen(str));}
458 inline Digest &operator<<(
const char *str)
459 {puts(str);
return *
this;}
461 inline Digest &operator<<(int16_t value)
462 {int16_t v = htons(value); put(&v, 2);
return *
this;}
464 inline Digest &operator<<(int32_t value)
465 {int32_t v = htonl(value); put(&v, 4);
return *
this;}
468 {
const char *cp = p.
_print();
if(cp) puts(cp);
return *
this;}
470 bool put(
const void *memory,
size_t size);
472 inline unsigned size()
const
475 const unsigned char *
get(void);
477 const char *c_str(
void);
485 void set(
const char *
id);
487 inline void operator=(
const char *
id)
490 inline bool operator *=(
const char *text)
493 inline bool operator +=(
const char *text)
496 inline const char *operator*()
499 inline bool operator!()
const
500 {
return !bufsize && context == NULL;}
502 inline operator bool()
const
503 {
return bufsize > 0 || context != NULL;}
510 void recycle(
bool binary =
false);
522 static bool has(
const char *name);
524 static void uuid(
char *
string,
const char *name,
const unsigned char *ns = NULL);
526 static String uuid(
const char *name,
const unsigned char *ns = NULL);
541 const void *hmactype;
546 unsigned char buffer[MAX_DIGEST_HASHSIZE / 8];
547 char textbuf[MAX_DIGEST_HASHSIZE / 8 + 1];
553 HMAC(
const char *digest,
const char *key,
size_t keylen = 0);
559 inline bool puts(
const char *str)
560 {
return put(str, strlen(str));}
562 inline HMAC &operator<<(
const char *str)
563 {puts(str);
return *
this;}
565 inline HMAC &operator<<(int16_t value)
566 {int16_t v = htons(value); put(&v, 2);
return *
this;}
568 inline HMAC &operator<<(int32_t value)
569 {int32_t v = htonl(value); put(&v, 4);
return *
this;}
572 {
const char *cp = p.
_print();
if(cp) puts(cp);
return *
this;}
574 bool put(
const void *memory,
size_t size);
576 inline unsigned size()
const
579 const unsigned char *
get(void);
581 const char *c_str(
void);
589 void set(
const char *digest,
const char *key,
size_t len);
591 inline bool operator *=(
const char *text)
594 inline bool operator +=(
const char *text)
597 inline const char *operator*()
600 inline bool operator!()
const
601 {
return !bufsize && context == NULL;}
603 inline operator bool()
const
604 {
return bufsize > 0 || context != NULL;}
611 static bool has(
const char *name);
628 static bool seed(
const unsigned char *buffer,
size_t size);
633 static void seed(
void);
643 static size_t key(
unsigned char *memory,
size_t size);
653 static size_t fill(
unsigned char *memory,
size_t size);
659 static int get(void);
667 static int get(
int min,
int max);
673 static double real(
void);
681 static double real(
double min,
double max);
688 static bool status(
void);
694 static void uuid(
char *
string);
724 inline void zerofill(
void *
addr,
size_t size)
726 ::memset(addr, 0, size);
729 #if defined(OLD_STDCPP) || defined(NEW_STDCPP)
756 void open(
const char *host,
const char *service,
size_t size = 536);
764 ssize_t _write(
const char *address,
size_t size);
766 ssize_t _read(
char *address,
size_t size);
770 inline void flush(
void)
773 inline bool is_secure(
void)
const
774 {
return bio != NULL;}
815 {memset(buffer, 0, S);}
821 {memset(buffer, 0, S);}
842 inline operator bool()
const
850 {
return buffer[0] == 0;}
856 inline operator char *()
872 {
return buffer[offset];}
880 {
return buffer + offset;}
893 inline size_t len(
void)
const
894 {
return strlen(buffer);}
906 unsigned char buffer[S];
924 {memset(buffer, 0, S);}
936 {memset(buffer, 0, S);}
942 inline operator unsigned char *()
error_t error
Last error flagged for this context.
HMAC hmac_t
Convenience type for generic digests.
void clear(void)
Clear current key memory.
A cryptographic message authentication code class.
virtual const char * _print(void) const =0
Extract formatted string for object.
void * session_t
Convenience type to represent a secure socket session.
size_t len(void) const
Get current length of string.
~keystring()
Clear memory when destroyed.
A copy-on-write string class that operates by reference count.
T &() min(T &o1, T &o2)
Convenience function to return min of two objects.
Cipher key formed by hash algorithm.
char * operator*()
Get text by object pointer reference.
T * init(T *memory)
Template function to initialize memory by invoking default constructor.
A generic tcp server class.
Cipher::Key skey_t
Convenience type for generic cipher key.
void add(const char *text)
Append null terminated text to our string buffer.
Digest digest_t
Convenience type for generic digests.
Common namespace for all ucommon objects.
A template to create a random generated key of specified size.
Secure socket using std::iostream.
keystring(const char *text)
Create a character buffer with assigned text.
static size_t key(unsigned char *memory, size_t size)
Get high-entropy random data.
size_t size(void) const
Get allocated size of the object.
secure * client_t
Convenience type to represent a security context.
void release(SharedAccess &object)
Convenience function to unlock shared object through it's protocol.
void operator=(const char *text)
Assign null terminated text to the object.
A generic tcp socket class that offers i/o buffering.
~keyrandom()
Clear memory when destroyed.
Various miscellaneous platform specific headers and defines.
Cipher cipher_t
Convenience type for generic ciphers.
void operator+=(const char *text)
Concatenate text into the object.
void set(const char *text)
Set string object to text of a null terminated string.
A cryptographic digest class.
A generic data ciphering class.
Cryptographically relevant random numbers.
keystring()
Create a new character buffer with an empty string.
char & operator[](size_t offset) const
Array operator to get a character from the object.
char * operator()(size_t offset)
Get a pointer to an offset in the object by expression operator.
A generic socket address class.
error_t err(void) const
Get last error code associated with the security context.
bool operator!() const
Test if the object is empty.
Common secure socket support.
struct sockaddr * addr(Socket::address &address)
A convenience function to convert a socket address list into a socket address.
Traditional keypair config file parsing class.
T &() max(T &o1, T &o2)
Convenience function to return max of two objects.
error_t
Different error states of the security context.
void update(void)
Update with new random key.
Used for forming stream output.
bool is_valid(void) const
Determine if the current security context is valid.
size_t size(void) const
Get allocated size of the object.
keyrandom()
Create a new character buffer with an empty string.
SSLBuffer ssl_t
Convenience type for secure socket.
Streamable tcp connection between client and server.
void * bufio_t
Convenience type to represent a secure socket buf i/o stream.
A template to create a string array that automatically erases.
unsigned char * operator*()
Get text by object pointer reference.
ObjectProtocol * copy(ObjectProtocol *object)
Convenience function to access object copy.
void clear(void)
Clear current key memory.
Top level include file for the GNU uCommon C++ core library.