UCommon
secure.h
Go to the documentation of this file.
1 // Copyright (C) 2010 David Sugar, Tycho Softworks.
2 //
3 // This file is part of GNU uCommon C++.
4 //
5 // GNU uCommon C++ is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // GNU uCommon C++ is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
17 
41 #ifndef _UCOMMON_SECURE_H_
42 #define _UCOMMON_SECURE_H_
43 
44 #ifndef _UCOMMON_CONFIG_H_
45 #include <ucommon/platform.h>
46 #endif
47 
48 #ifndef _UCOMMON_UCOMMON_H_
49 #include <ucommon/ucommon.h>
50 #endif
51 
52 #define MAX_CIPHER_KEYSIZE 512
53 #define MAX_DIGEST_HASHSIZE 512
54 
55 NAMESPACE_UCOMMON
56 
62 class __SHARED secure
63 {
64 public:
68  typedef enum {OK=0, INVALID, MISSING_CERTIFICATE, MISSING_PRIVATEKEY, INVALID_CERTIFICATE, INVALID_AUTHORITY, INVALID_PEERNAME, INVALID_CIPHER} error_t;
69 
73  typedef enum {
74  SYSTEM_CERTIFICATES, SYSTEM_KEYS} path_t;
75 
76 protected:
81 
82  inline secure() {error = OK;};
83 
84 public:
89  virtual ~secure();
90 
94  typedef secure *client_t;
95 
96  typedef secure *server_t;
97 
101  typedef void *session_t;
102 
106  typedef void *bufio_t;
107 
115  static bool init(const char *program = NULL);
116 
124  static bool fips(const char *program = NULL);
125 
132  static String path(path_t id);
133 
139  static int oscerts(const char *path);
140 
150  static error_t verify(session_t session, const char *peername = NULL);
151 
161  static server_t server(const char *authority = NULL);
162 
169  static client_t client(const char *authority = NULL);
170 
177  static client_t user(const char *authority);
178 
184  static void cipher(secure *context, const char *ciphers);
185 
190  inline bool is(void)
191  {return error == OK;};
192 
197  inline error_t err(void)
198  {return error;};
199 
204  static void uuid(char *string);
205 
206  static String uuid(void);
207 
208  template <typename T>
209  inline static void erase(T *object)
210  {memset(object, 0, sizeof(object)); delete object;}
211 };
212 
220 class __SHARED SSLBuffer : public TCPBuffer
221 {
222 protected:
223  secure::session_t ssl;
224  secure::bufio_t bio;
225  bool server;
226  bool verify;
227 
228 public:
229  SSLBuffer(secure::client_t context);
230  SSLBuffer(const TCPServer *server, secure::server_t context, size_t size = 536);
231  ~SSLBuffer();
232 
240  void open(const char *host, const char *service, size_t size = 536);
241 
242  void close(void);
243 
244  void release(void);
245 
246  size_t _push(const char *address, size_t size);
247 
248  size_t _pull(char *address, size_t size);
249 
250  bool _flush(void);
251 
252  bool _pending(void);
253 
254  inline bool is_secure(void)
255  {return bio != NULL;};
256 };
257 
267 class __SHARED Cipher
268 {
269 public:
270  typedef enum {ENCRYPT = 1, DECRYPT = 0} mode_t;
271 
279  class __SHARED Key
280  {
281  protected:
282  friend class Cipher;
283 
284  union {
285  const void *algotype;
286  int algoid;
287  };
288 
289  union {
290  const void *hashtype;
291  int hashid;
292  };
293 
294  int modeid;
295 
296  // assume 512 bit cipher keys possible...
297  unsigned char keybuf[MAX_CIPHER_KEYSIZE / 8], ivbuf[MAX_CIPHER_KEYSIZE / 8];
298 
299  // generated keysize
300  size_t keysize, blksize;
301 
302  Key(const char *cipher);
303  Key();
304 
305  void set(const char *cipher);
306 
307  void set(const char *cipher, const char *digest);
308 
309  void assign(const char *key, size_t size, const unsigned char *salt, unsigned rounds);
310 
311  public:
312  Key(const char *cipher, const char *digest, const char *text, size_t size = 0, const unsigned char *salt = NULL, unsigned rounds = 1);
313 
314  Key(const char *cipher, const char *digest);
315 
316  ~Key();
317 
318  void assign(const char *key, size_t size = 0);
319 
320  void clear(void);
321 
322  inline size_t size(void)
323  {return keysize;};
324 
325  inline size_t iosize(void)
326  {return blksize;};
327 
328  inline operator bool()
329  {return keysize > 0;};
330 
331  inline bool operator!()
332  {return keysize == 0;};
333 
334  inline Key& operator=(const char *pass)
335  {assign(pass); return *this;};
336 
337  static void options(const unsigned char *salt = NULL, unsigned rounds = 1);
338  };
339 
340  typedef Key *key_t;
341 
342 private:
343  Key keys;
344  size_t bufsize, bufpos;
345  mode_t bufmode;
346  unsigned char *bufaddr;
347  void *context;
348 
349 protected:
350  virtual void push(unsigned char *address, size_t size);
351 
352  void release(void);
353 
354 public:
355  Cipher();
356 
357  Cipher(key_t key, mode_t mode, unsigned char *address = NULL, size_t size = 0);
358 
359  virtual ~Cipher();
360 
361  void set(unsigned char *address, size_t size = 0);
362 
363  void set(key_t key, mode_t mode, unsigned char *address, size_t size = 0);
364 
369  size_t flush(void);
370 
379  size_t put(const unsigned char *data, size_t size);
380 
387  size_t puts(const char *string);
388 
400  size_t pad(const unsigned char *address, size_t size);
401 
410  size_t process(unsigned char *address, size_t size, bool flag = false);
411 
412  inline size_t size(void)
413  {return bufsize;};
414 
415  inline size_t pos(void)
416  {return bufpos;};
417 
418  inline size_t align(void)
419  {return keys.iosize();};
420 
426  static bool is(const char *name);
427 };
428 
435 class __SHARED Digest
436 {
437 private:
438  void *context;
439 
440  union {
441  const void *hashtype;
442  int hashid;
443  };
444 
445  unsigned bufsize;
446  unsigned char buffer[MAX_DIGEST_HASHSIZE / 8];
447  char textbuf[MAX_DIGEST_HASHSIZE / 8 + 1];
448 
449 protected:
450  void release(void);
451 
452 public:
453  Digest(const char *type);
454 
455  Digest();
456 
457  ~Digest();
458 
459  inline bool puts(const char *str)
460  {return put(str, strlen(str));};
461 
462  bool put(const void *memory, size_t size);
463 
464  inline unsigned size() const
465  {return bufsize;};
466 
467  const unsigned char *get(void);
468 
469  const char *c_str(void);
470 
471  inline String str(void)
472  {return String(c_str());};
473 
474  inline operator String()
475  {return String(c_str());};
476 
477  void set(const char *id);
478 
479  inline void operator=(const char *id)
480  {set(id);};
481 
482  inline bool operator *=(const char *text)
483  {return puts(text);};
484 
485  inline bool operator +=(const char *text)
486  {return puts(text);};
487 
488  inline const char *operator*()
489  {return c_str();};
490 
491  inline bool operator!() const
492  {return !bufsize && context == NULL;};
493 
494  inline operator bool() const
495  {return bufsize > 0 || context != NULL;};
496 
502  void recycle(bool binary = false);
503 
507  void reset(void);
508 
514  static bool is(const char *name);
515 
516  static void uuid(char *string, const char *name, const unsigned char *ns = NULL);
517 
518  static String uuid(const char *name, const unsigned char *ns = NULL);
519 };
520 
526 class __SHARED Random
527 {
528 public:
535  static bool seed(const unsigned char *buffer, size_t size);
536 
540  static void seed(void);
541 
550  static size_t key(unsigned char *memory, size_t size);
551 
560  static size_t fill(unsigned char *memory, size_t size);
561 
566  static int get(void);
567 
574  static int get(int min, int max);
575 
580  static double real(void);
581 
588  static double real(double min, double max);
589 
595  static bool status(void);
596 
601  static void uuid(char *string);
602 
603  static String uuid(void);
604 };
605 
609 typedef SSLBuffer ssl_t;
610 
614 typedef Digest digest_t;
615 
619 typedef Cipher cipher_t;
620 
625 
626 inline void zerofill(void *addr, size_t size)
627 {
628  ::memset(addr, 0, size);
629 }
630 
631 #if defined(OLD_STDCPP) || defined(NEW_STDCPP)
632 
641 class __SHARED sstream : public tcpstream
642 {
643 protected:
644  secure::session_t ssl;
645  secure::bufio_t bio;
646  bool server;
647  bool verify;
648 
649 private:
650  // kill copy constructor
651  sstream(const sstream&);
652 
653 public:
654  sstream(secure::client_t context);
655  sstream(const TCPServer *server, secure::server_t context, size_t size = 536);
656  ~sstream();
657 
658  void open(const char *host, const char *service, size_t size = 536);
659 
660  void close(void);
661 
662  int sync();
663 
664  void release(void);
665 
666  ssize_t _write(const char *address, size_t size);
667 
668  ssize_t _read(char *address, size_t size);
669 
670  bool _wait(void);
671 
672  inline void flush(void)
673  {sync();}
674 
675  inline bool is_secure(void)
676  {return bio != NULL;}
677 };
678 
679 #endif
680 
681 END_NAMESPACE
682 
683 #endif