42#ifndef _UCOMMON_SECURE_H_
43#define _UCOMMON_SECURE_H_
45#ifndef _UCOMMON_CONFIG_H_
49#ifndef _UCOMMON_UCOMMON_H_
53#define MAX_CIPHER_KEYSIZE 512
54#define MAX_DIGEST_HASHSIZE 512
83 return *(
static_cast<T*
>(
pointer));
87 return *(
static_cast<T*
>(
pointer));
91 return static_cast<T*
>(
pointer);
134 typedef enum {OK=0, INVALID, MISSING_CERTIFICATE, MISSING_PRIVATEKEY, INVALID_CERTIFICATE, INVALID_AUTHORITY, INVALID_PEERNAME, INVALID_CIPHER} error_t;
136 typedef enum {
NONE, SIGNED, VERIFIED} verify_t;
187 static bool init(
void);
195 static bool fips(
void);
202 static int oscerts(
const char *path);
208 static const char *oscerts(
void);
219 static server_t server(
const char *
keyfile = NULL,
const char *authority = NULL);
228 static client_t client(
const char *authority = NULL,
const char *paths = NULL);
243 static void cipher(
secure *context,
const char *ciphers);
265 static void uuid(
char *
string);
271 inline operator bool()
const {
292 typedef enum {ENCRYPT = 1, DECRYPT = 0} mode_t;
324 Key(
const char *ciper);
326 void set(
const char *
cipher);
331 Key(
const char *
cipher,
const char *
digest,
const char *text,
size_t size = 0,
const uint8_t *salt = NULL,
unsigned rounds = 1);
333 Key(
const char *
cipher,
const uint8_t *iv,
size_t ivsize);
341 void set(
const uint8_t *key,
size_t size);
355 void set(
const char *
cipher,
const uint8_t *iv,
size_t ivsize);
357 void assign(
const char *key,
size_t size,
const uint8_t *salt,
unsigned rounds);
361 void assign(
const char *key,
size_t size = 0);
367 void b64(
const char *
string);
369 size_t get(uint8_t *key, uint8_t *ivout = NULL);
371 inline size_t size(
void)
const {
379 inline operator bool()
const {
392 bool operator==(
const Key& other)
const;
395 return !operator==(other);
398 static void options(
const uint8_t *salt = NULL,
unsigned rounds = 1);
413 virtual void push(uint8_t *address,
size_t size);
424 void set(uint8_t *address,
size_t size = 0);
426 void set(
const key_t key,
mode_t mode, uint8_t *address,
size_t size = 0);
450 size_t put(
const uint8_t *data,
size_t size);
458 size_t puts(
const char *
string);
471 size_t pad(
const uint8_t *address,
size_t size);
481 size_t process(uint8_t *address,
size_t size,
bool flag =
false);
483 inline size_t size(
void)
const {
487 inline size_t pos(
void)
const {
500 static bool has(
const char *name);
528 const uint8_t *get(
void);
537 inline bool puts(
const char *str) {
538 return put(
str, strlen(
str));
547 int16_t v = htons(value);
553 int32_t v = htonl(value);
559 const char *cp = p.
_print();
565 bool put(
const void *memory,
size_t size);
579 void set(
const char *
id);
586 inline bool operator *=(
const char *text) {
590 inline bool operator +=(
const char *text) {
599 return !bufsize && context == NULL;
602 inline operator bool()
const {
603 return bufsize > 0 || context != NULL;
611 void recycle(
bool binary =
false);
623 static bool has(
const char *name);
625 static secure::string uuid(
const char *name,
const uint8_t *ns = NULL);
675 const uint8_t *get(
void);
684 inline bool puts(
const char *str) {
685 return put(
str, strlen(
str));
694 int16_t v = htons(value);
700 int32_t v = htonl(value);
706 const char *cp = p.
_print();
712 bool put(
const void *memory,
size_t size);
726 inline bool operator *=(
const char *text) {
732 inline bool operator +=(
const char *text) {
741 return !bufsize && context == NULL;
744 inline operator bool()
const {
745 return bufsize > 0 || context != NULL;
753 static bool has(
const char *name);
777 static bool seed(
const uint8_t *
buffer,
size_t size);
782 static void seed(
void);
792 static size_t key(uint8_t *memory,
size_t size);
802 static size_t fill(uint8_t *memory,
size_t size);
808 static int get(
void);
816 static int get(
int min,
int max);
822 static double real(
void);
830 static double real(
double min,
double max);
837 static bool status(
void);
843 static void uuid(
char *
string);
850 Random::key(
reinterpret_cast<uint8_t *
>(&tmp),
sizeof(tmp));
860 slice = 0xffffffff /
max;
861 while(value >=
max) {
862 value = Random::value<T>() / slice;
870 return min + Random::value<T>(
max -
min);
897 ::memset(
addr, 0, size);
900#ifndef UCOMMON_SYSRUNTIME
920 ssize_t _write(
const char *address,
size_t size)
__OVERRIDE;
922 ssize_t _read(
char *address,
size_t size)
__OVERRIDE;
954 void open(
const char *host,
const char *service,
size_t size = 536);
968 inline
void flush(
void) {
1001 return verified == secure::VERIFIED;
1019 memset(&var, 0,
sizeof(var));
static void process(void)
__DELETE_DEFAULTS(AutoClear)
Cipher key formed by hash algorithm.
bool operator!=(const Key &other) const
Key & operator=(const char *pass)
size_t iosize(void) const
A generic data ciphering class.
A cryptographic digest class.
Digest & operator<<(int32_t value)
Digest & operator<<(const PrintProtocol &p)
secure::string operator*()
Digest & operator=(const char *id)
Digest & operator<<(int16_t value)
Digest & operator<<(const char *str)
bool puts(const char *str)
A cryptographic message authentication code class.
HMAC & operator<<(int32_t value)
bool puts(const char *str)
HMAC & operator<<(int16_t value)
secure::string operator*()
HMAC & operator<<(const char *str)
HMAC & operator<<(const PrintProtocol &p)
Used for forming stream output.
virtual const char * _print(void) const =0
Extract formatted string for object.
Cryptographically relevant random numbers.
__DELETE_DEFAULTS(Random)
static T value(T min, T max)
A generic tcp server class.
Traditional keypair config file parsing class.
Generic smart pointer class.
Common secure socket support.
error_t error
Last error flagged for this context.
void * session_t
Convenience type to represent a secure socket session.
static secure::string pass(const char *prompt, size_t size)
bool is_valid(void) const
Determine if the current security context is valid.
static client_t user(const char *authority)
Create a peer user client context.
error_t
Different error states of the security context.
void * bufio_t
Convenience type to represent a secure socket buf i/o stream.
void * cert_t
Convenience type to represent a ssl certificate object.
byteref< secure_release > keybytes
secure * client_t
Convenience type to represent a security context.
stringref< secure_release > string
error_t err(void) const
Get last error code associated with the security context.
Secure socket using std::iostream.
bool is_certificate(void) const
Check if a peer certificate is present.
secure::cert_t certificate(void) const
Get peer (x509) certificate for current stream if present.
secure::verify_t verified
bool is_secure(void) const
Check if ssl session active, otherwise pure tcp.
bool is_signed(void) const
Check if peer certificate is present and at least self-signed.
bool is_verified(void) const
Check if peer certificate is verified through an authority.
Streamable tcp connection between client and server.
static void digest(const char *path=NULL)
Common namespace for all ucommon objects.
T &() min(T &o1, T &o2)
Convenience function to return min of two objects.
Cipher cipher_t
Convenience type for generic ciphers.
T &() max(T &o1, T &o2)
Convenience function to return max of two objects.
Digest digest_t
Convenience type for generic digests.
secure::string keystring_t
HMAC hmac_t
Convenience type for generic digests.
Cipher::Key skey_t
Convenience type for generic cipher key.
const struct sockaddr * addr(Socket::address &address)
A convenience function to convert a socket address list into a socket address.
String str(Socket &so, size_t size)
static shell::stringopt error('e', "--error", _TEXT("stderr path to use"), "filename")
#define MAX_CIPHER_KEYSIZE
#define MAX_DIGEST_HASHSIZE
void sha256(unsigned char hval[], const unsigned char data[], unsigned long len)
void sha384(unsigned char hval[], const unsigned char data[], unsigned long len)
Top level include file for the GNU uCommon C++ core library.
static shell::flagopt verified('V', NULL, _TEXT("requires verification"))
static uint8_t buffer[65536]
static void zerofill(void)