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 
70 protected:
75 
76  inline secure() {error = OK;};
77 
78 public:
83  virtual ~secure();
84 
88  typedef secure *client_t;
89 
90  typedef secure *server_t;
91 
95  typedef void *session_t;
96 
100  typedef void *bufio_t;
101 
107  static bool init(void);
108 
115  static bool fips(void);
116 
122  static int oscerts(const char *path);
123 
128  static const char *oscerts(void);
129 
139  static error_t verify(session_t session, const char *peername = NULL);
140 
150  static server_t server(const char *keyfile = NULL, const char *authority = NULL);
151 
158  static client_t client(const char *authority = NULL);
159 
166  static client_t user(const char *authority);
167 
173  static void cipher(secure *context, const char *ciphers);
174 
179  inline bool is_valid(void) const
180  {return error == OK;};
181 
186  inline error_t err(void)
187  {return error;};
188 
193  static void uuid(char *string);
194 
195  static String uuid(void);
196 
197  template <typename T>
198  inline static void erase(T *object)
199  {memset(object, 0, sizeof(object)); delete object;}
200 
201  inline operator bool()
202  {return is_valid();}
203 
204  inline bool operator!()
205  {return !is_valid();}
206 
207 };
208 
216 class __SHARED SSLBuffer : public TCPBuffer
217 {
218 protected:
219  secure::session_t ssl;
220  secure::bufio_t bio;
221  bool server;
222  bool verify;
223 
224 public:
225  SSLBuffer(secure::client_t context);
226  SSLBuffer(const TCPServer *server, secure::server_t context, size_t size = 536);
227  ~SSLBuffer();
228 
236  void open(const char *host, const char *service, size_t size = 536);
237 
238  void close(void);
239 
240  void release(void);
241 
242  size_t _push(const char *address, size_t size);
243 
244  size_t _pull(char *address, size_t size);
245 
246  bool _flush(void);
247 
248  bool _pending(void);
249 
250  inline bool is_secure(void)
251  {return bio != NULL;};
252 };
253 
263 class __SHARED Cipher
264 {
265 public:
266  typedef enum {ENCRYPT = 1, DECRYPT = 0} mode_t;
267 
275  class __SHARED Key
276  {
277  protected:
278  friend class Cipher;
279 
280  union {
281  const void *algotype;
282  int algoid;
283  };
284 
285  union {
286  const void *hashtype;
287  int hashid;
288  };
289 
290  int modeid;
291 
292  // assume 512 bit cipher keys possible...
293  unsigned char keybuf[MAX_CIPHER_KEYSIZE / 8], ivbuf[MAX_CIPHER_KEYSIZE / 8];
294 
295  // generated keysize
296  size_t keysize, blksize;
297 
298  Key(const char *cipher);
299  Key();
300 
301  void set(const char *cipher);
302 
303  void set(const char *cipher, const char *digest);
304 
305  void assign(const char *key, size_t size, const unsigned char *salt, unsigned rounds);
306 
307  public:
308  Key(const char *cipher, const char *digest, const char *text, size_t size = 0, const unsigned char *salt = NULL, unsigned rounds = 1);
309 
310  Key(const char *cipher, const char *digest);
311 
312  ~Key();
313 
314  void assign(const char *key, size_t size = 0);
315 
316  void clear(void);
317 
318  inline size_t size(void)
319  {return keysize;};
320 
321  inline size_t iosize(void)
322  {return blksize;};
323 
324  inline operator bool()
325  {return keysize > 0;};
326 
327  inline bool operator!()
328  {return keysize == 0;};
329 
330  inline Key& operator=(const char *pass)
331  {assign(pass); return *this;};
332 
333  static void options(const unsigned char *salt = NULL, unsigned rounds = 1);
334  };
335 
336  typedef Key *key_t;
337 
338 private:
339  Key keys;
340  size_t bufsize, bufpos;
341  mode_t bufmode;
342  unsigned char *bufaddr;
343  void *context;
344 
345 protected:
346  virtual void push(unsigned char *address, size_t size);
347 
348  void release(void);
349 
350 public:
351  Cipher();
352 
353  Cipher(key_t key, mode_t mode, unsigned char *address = NULL, size_t size = 0);
354 
355  virtual ~Cipher();
356 
357  void set(unsigned char *address, size_t size = 0);
358 
359  void set(key_t key, mode_t mode, unsigned char *address, size_t size = 0);
360 
365  size_t flush(void);
366 
375  size_t put(const unsigned char *data, size_t size);
376 
383  size_t puts(const char *string);
384 
396  size_t pad(const unsigned char *address, size_t size);
397 
406  size_t process(unsigned char *address, size_t size, bool flag = false);
407 
408  inline size_t size(void)
409  {return bufsize;};
410 
411  inline size_t pos(void)
412  {return bufpos;};
413 
414  inline size_t align(void)
415  {return keys.iosize();};
416 
422  static bool has(const char *name);
423 };
424 
431 class __SHARED Digest
432 {
433 private:
434  void *context;
435 
436  union {
437  const void *hashtype;
438  int hashid;
439  };
440 
441  unsigned bufsize;
442  unsigned char buffer[MAX_DIGEST_HASHSIZE / 8];
443  char textbuf[MAX_DIGEST_HASHSIZE / 8 + 1];
444 
445 protected:
446  void release(void);
447 
448 public:
449  Digest(const char *type);
450 
451  Digest();
452 
453  ~Digest();
454 
455  inline bool puts(const char *str)
456  {return put(str, strlen(str));};
457 
458  inline Digest &operator<<(const char *str)
459  {puts(str); return *this;}
460 
461  inline Digest &operator<<(int16_t value)
462  {int16_t v = htons(value); put(&v, 2); return *this;}
463 
464  inline Digest &operator<<(int32_t value)
465  {int32_t v = htonl(value); put(&v, 4); return *this;}
466 
467  inline Digest &operator<<(const PrintProtocol& p)
468  {const char *cp = p._print(); if(cp) puts(cp); return *this;}
469 
470  bool put(const void *memory, size_t size);
471 
472  inline unsigned size() const
473  {return bufsize;};
474 
475  const unsigned char *get(void);
476 
477  const char *c_str(void);
478 
479  inline String str(void)
480  {return String(c_str());};
481 
482  inline operator String()
483  {return String(c_str());};
484 
485  void set(const char *id);
486 
487  inline void operator=(const char *id)
488  {set(id);};
489 
490  inline bool operator *=(const char *text)
491  {return puts(text);};
492 
493  inline bool operator +=(const char *text)
494  {return puts(text);};
495 
496  inline const char *operator*()
497  {return c_str();};
498 
499  inline bool operator!() const
500  {return !bufsize && context == NULL;};
501 
502  inline operator bool() const
503  {return bufsize > 0 || context != NULL;};
504 
510  void recycle(bool binary = false);
511 
515  void reset(void);
516 
522  static bool has(const char *name);
523 
524  static void uuid(char *string, const char *name, const unsigned char *ns = NULL);
525 
526  static String uuid(const char *name, const unsigned char *ns = NULL);
527 };
528 
535 class __SHARED HMAC
536 {
537 private:
538  void *context;
539 
540  union {
541  const void *hmactype;
542  int hmacid;
543  };
544 
545  unsigned bufsize;
546  unsigned char buffer[MAX_DIGEST_HASHSIZE / 8];
547  char textbuf[MAX_DIGEST_HASHSIZE / 8 + 1];
548 
549 protected:
550  void release(void);
551 
552 public:
553  HMAC(const char *digest, const char *key, size_t keylen = 0);
554 
555  HMAC();
556 
557  ~HMAC();
558 
559  inline bool puts(const char *str)
560  {return put(str, strlen(str));};
561 
562  inline HMAC &operator<<(const char *str)
563  {puts(str); return *this;}
564 
565  inline HMAC &operator<<(int16_t value)
566  {int16_t v = htons(value); put(&v, 2); return *this;}
567 
568  inline HMAC &operator<<(int32_t value)
569  {int32_t v = htonl(value); put(&v, 4); return *this;}
570 
571  inline HMAC &operator<<(const PrintProtocol& p)
572  {const char *cp = p._print(); if(cp) puts(cp); return *this;}
573 
574  bool put(const void *memory, size_t size);
575 
576  inline unsigned size() const
577  {return bufsize;};
578 
579  const unsigned char *get(void);
580 
581  const char *c_str(void);
582 
583  inline String str(void)
584  {return String(c_str());};
585 
586  inline operator String()
587  {return String(c_str());};
588 
589  void set(const char *digest, const char *key, size_t len);
590 
591  inline bool operator *=(const char *text)
592  {return puts(text);};
593 
594  inline bool operator +=(const char *text)
595  {return puts(text);};
596 
597  inline const char *operator*()
598  {return c_str();};
599 
600  inline bool operator!() const
601  {return !bufsize && context == NULL;};
602 
603  inline operator bool() const
604  {return bufsize > 0 || context != NULL;};
605 
611  static bool has(const char *name);
612 };
613 
619 class __SHARED Random
620 {
621 public:
628  static bool seed(const unsigned char *buffer, size_t size);
629 
633  static void seed(void);
634 
643  static size_t key(unsigned char *memory, size_t size);
644 
653  static size_t fill(unsigned char *memory, size_t size);
654 
659  static int get(void);
660 
667  static int get(int min, int max);
668 
673  static double real(void);
674 
681  static double real(double min, double max);
682 
688  static bool status(void);
689 
694  static void uuid(char *string);
695 
696  static String uuid(void);
697 };
698 
702 typedef SSLBuffer ssl_t;
703 
707 typedef Digest digest_t;
708 
712 typedef HMAC hmac_t;
713 
717 typedef Cipher cipher_t;
718 
723 
724 inline void zerofill(void *addr, size_t size)
725 {
726  ::memset(addr, 0, size);
727 }
728 
729 #if defined(OLD_STDCPP) || defined(NEW_STDCPP)
730 
739 class __SHARED sstream : public tcpstream
740 {
741 protected:
742  secure::session_t ssl;
743  secure::bufio_t bio;
744  bool server;
745  bool verify;
746 
747 private:
748  // kill copy constructor
749  sstream(const sstream&);
750 
751 public:
752  sstream(secure::client_t context);
753  sstream(const TCPServer *server, secure::server_t context, size_t size = 536);
754  ~sstream();
755 
756  void open(const char *host, const char *service, size_t size = 536);
757 
758  void close(void);
759 
760  int sync();
761 
762  void release(void);
763 
764  ssize_t _write(const char *address, size_t size);
765 
766  ssize_t _read(char *address, size_t size);
767 
768  bool _wait(void);
769 
770  inline void flush(void)
771  {sync();}
772 
773  inline bool is_secure(void)
774  {return bio != NULL;}
775 };
776 
785 template<size_t S>
787 {
788 private:
789  char buffer[S];
790 
794  inline keystring(const keystring& copy) {};
795 
796 public:
800  inline keystring()
801  {buffer[0] = 0;}
802 
808  inline keystring(const char *text)
809  {String::set(buffer, S, text);}
810 
814  inline ~keystring()
815  {memset(buffer, 0, S);}
816 
820  inline void clear(void)
821  {memset(buffer, 0, S);}
822 
827  inline void operator=(const char *text)
828  {String::set(buffer, S, text);}
829 
835  inline void operator+=(const char *text)
836  {String::add(buffer, S, text);}
837 
842  inline operator bool() const
843  {return buffer[0];}
844 
849  inline bool operator!() const
850  {return buffer[0] == 0;}
851 
856  inline operator char *()
857  {return buffer;}
858 
863  inline char *operator*()
864  {return buffer;}
865 
871  inline char& operator[](size_t offset) const
872  {return buffer[offset];}
873 
879  inline char *operator()(size_t offset)
880  {return buffer + offset;}
881 
886  inline size_t size(void) const
887  {return S;}
888 
893  inline size_t len(void) const
894  {return strlen(buffer);}
895 };
896 
902 template<size_t S>
904 {
905 private:
906  unsigned char buffer[S];
907 
911  inline keyrandom(const keyrandom& copy) {};
912 
913 public:
917  inline keyrandom()
918  {Random::key(buffer, S);}
919 
923  inline ~keyrandom()
924  {memset(buffer, 0, S);}
925 
929  inline void update(void)
930  {Random::key(buffer, S);}
931 
935  inline void clear(void)
936  {memset(buffer, 0, S);}
937 
942  inline operator unsigned char *()
943  {return buffer;}
944 
949  inline unsigned char *operator*()
950  {return buffer;}
951 
956  inline size_t size(void) const
957  {return S;}
958 };
959 
960 
961 #endif
962 
963 END_NAMESPACE
964 
965 #endif