ucommon

ucommon/secure.h

Go to the documentation of this file.
00001 // Copyright (C) 2010 David Sugar, Tycho Softworks.
00002 //
00003 // This file is part of GNU uCommon C++.
00004 //
00005 // GNU uCommon C++ is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU Lesser General Public License as published
00007 // by the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // GNU uCommon C++ is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public License
00016 // along with GNU uCommon C++.  If not, see <http://www.gnu.org/licenses/>.
00017 
00041 #ifndef _UCOMMON_SECURE_H_
00042 #define _UCOMMON_SECURE_H_
00043 
00044 #ifndef _UCOMMON_CONFIG_H_
00045 #include <ucommon/platform.h>
00046 #endif
00047 
00048 #ifndef _UCOMMON_UCOMMON_H_
00049 #include <ucommon/ucommon.h>
00050 #endif
00051 
00052 #define MAX_CIPHER_KEYSIZE  512
00053 #define MAX_DIGEST_HASHSIZE 512
00054 
00055 NAMESPACE_UCOMMON
00056 
00062 class __EXPORT secure
00063 {
00064 public:
00068     typedef enum {OK=0, INVALID, MISSING_CERTIFICATE, MISSING_PRIVATEKEY, INVALID_CERTIFICATE, INVALID_AUTHORITY, INVALID_PEERNAME, INVALID_CIPHER} error_t;
00069 
00070 protected:
00074     error_t error;
00075 
00076     inline secure() {error = OK;};
00077 
00078 public:
00083     virtual ~secure();
00084 
00088     typedef secure *client_t;
00089 
00090     typedef secure *server_t;
00091 
00095     typedef void *session_t;
00096 
00100     typedef void *bufio_t;
00101 
00109     static bool init(const char *program = NULL);
00110 
00120     static error_t verify(session_t session, const char *peername = NULL);
00121 
00131     static server_t server(const char *authority = NULL);
00132 
00139     static client_t client(const char *authority = NULL);
00140 
00147     static client_t user(const char *authority);
00148 
00154     static void cipher(secure *context, const char *ciphers);
00155 
00160     inline bool is(void)
00161         {return error == OK;};
00162 
00167     inline error_t err(void)
00168         {return error;};
00169 
00174     static void uuid(char *string);
00175 
00176     static String uuid(void);
00177 };
00178 
00186 class __EXPORT SSLBuffer : public TCPBuffer
00187 {
00188 protected:
00189     secure::session_t ssl;
00190     secure::bufio_t bio;
00191     bool server;
00192     bool verify;
00193 
00194 public:
00195     SSLBuffer(secure::client_t context);
00196     SSLBuffer(const TCPServer *server, secure::server_t context, size_t size = 536);
00197     ~SSLBuffer();
00198 
00206     void open(const char *host, const char *service, size_t size = 536);
00207 
00208     void close(void);
00209 
00210     void release(void);
00211 
00212     size_t _push(const char *address, size_t size);
00213 
00214     size_t _pull(char *address, size_t size);
00215 
00216     bool _flush(void);
00217 
00218     bool _pending(void);
00219 
00220     inline bool is_secure(void)
00221         {return bio != NULL;};
00222 };
00223 
00233 class __EXPORT Cipher
00234 {
00235 public:
00236     typedef enum {ENCRYPT = 1, DECRYPT = 0} mode_t;
00237 
00245     class __EXPORT Key
00246     {
00247     protected:
00248         friend class Cipher;
00249 
00250         union {
00251             const void *algotype;
00252             int algoid;
00253         };
00254 
00255         union {
00256             const void *hashtype;
00257             int hashid;
00258         };
00259 
00260         int modeid;
00261 
00262         // assume 512 bit cipher keys possible...
00263         unsigned char keybuf[MAX_CIPHER_KEYSIZE / 8], ivbuf[MAX_CIPHER_KEYSIZE / 8];
00264 
00265         // generated keysize
00266         size_t keysize, blksize;
00267 
00268         Key(const char *cipher);
00269         Key();
00270 
00271         void set(const char *cipher);
00272 
00273     public:
00274         Key(const char *cipher, const char *digest, const char *text, size_t size = 0, const unsigned char *salt = NULL, unsigned rounds = 1);
00275         ~Key();
00276 
00277         void clear(void);
00278 
00279         inline size_t size(void)
00280             {return keysize;};
00281 
00282         inline size_t iosize(void)
00283             {return blksize;};
00284 
00285         inline operator bool()
00286             {return keysize > 0;};
00287 
00288         inline bool operator!()
00289             {return keysize == 0;};
00290     };
00291 
00292     typedef Key *key_t;
00293 
00294 private:
00295     Key keys;
00296     size_t bufsize, bufpos;
00297     mode_t bufmode;
00298     unsigned char *bufaddr;
00299     void *context;
00300 
00301 protected:
00302     virtual void push(unsigned char *address, size_t size);
00303 
00304     void release(void);
00305 
00306 public:
00307     Cipher();
00308 
00309     Cipher(key_t key, mode_t mode, unsigned char *address = NULL, size_t size = 0);
00310 
00311     ~Cipher();
00312 
00313     void set(unsigned char *address, size_t size = 0);
00314 
00315     void set(key_t key, mode_t mode, unsigned char *address, size_t size = 0);
00316 
00321     size_t flush(void);
00322 
00331     size_t put(const unsigned char *data, size_t size);
00332 
00339     size_t puts(const char *string);
00340 
00352     size_t pad(const unsigned char *address, size_t size);
00353 
00362     size_t process(unsigned char *address, size_t size, bool flag = false);
00363 
00364     inline size_t size(void)
00365         {return bufsize;};
00366 
00367     inline size_t pos(void)
00368         {return bufpos;};
00369 
00370     inline size_t align(void)
00371         {return keys.iosize();};
00372 
00378     static bool is(const char *name);
00379 };
00380 
00387 class __EXPORT Digest
00388 {
00389 private:
00390     void *context;
00391 
00392     union {
00393         const void *hashtype;
00394         int hashid;
00395     };
00396 
00397     unsigned bufsize;
00398     unsigned char buffer[MAX_DIGEST_HASHSIZE / 8];
00399     char textbuf[MAX_DIGEST_HASHSIZE / 8 + 1];
00400 
00401 protected:
00402     void release(void);
00403 
00404 public:
00405     Digest(const char *type);
00406 
00407     Digest();
00408 
00409     ~Digest();
00410 
00411     inline bool puts(const char *str)
00412         {return put(str, strlen(str));};
00413 
00414     bool put(const void *memory, size_t size);
00415 
00416     inline unsigned size() const
00417         {return bufsize;};
00418 
00419     const unsigned char *get(void);
00420 
00421     const char *c_str(void);
00422 
00423     inline String str(void)
00424         {return String(c_str());};
00425 
00426     inline operator String()
00427         {return String(c_str());};
00428 
00429     void set(const char *id);
00430 
00431     inline void operator=(const char *id)
00432         {set(id);};
00433 
00434     inline bool operator *=(const char *text)
00435         {return puts(text);};
00436 
00437     inline bool operator +=(const char *text)
00438         {return puts(text);};
00439 
00440     inline const char *operator*()
00441         {return c_str();};
00442 
00443     inline bool operator!() const
00444         {return !bufsize && context == NULL;};
00445 
00446     inline operator bool() const
00447         {return bufsize > 0 || context != NULL;};
00448 
00454     void recycle(bool binary = false);
00455 
00459     void reset(void);
00460 
00466     static bool is(const char *name);
00467 
00468     static void uuid(char *string, const char *name, const unsigned char *ns = NULL);
00469 
00470     static String uuid(const char *name, const unsigned char *ns = NULL);
00471 };
00472 
00478 class __EXPORT Random
00479 {
00480 public:
00487     static bool seed(const unsigned char *buffer, size_t size);
00488 
00492     static void seed(void);
00493 
00502     static size_t key(unsigned char *memory, size_t size);
00503 
00512     static size_t fill(unsigned char *memory, size_t size);
00513 
00518     static int get(void);
00519 
00526     static int get(int min, int max);
00527 
00532     static double real(void);
00533 
00540     static double real(double min, double max);
00541 
00547     static bool status(void);
00548 
00553     static void uuid(char *string);
00554 
00555     static String uuid(void);
00556 };
00557 
00561 typedef SSLBuffer ssl_t;
00562 
00566 typedef Digest digest_t;
00567 
00571 typedef Cipher cipher_t;
00572 
00576 typedef Cipher::Key skey_t;
00577 
00578 inline void zerofill(void *addr, size_t size)
00579 {
00580     ::memset(addr, 0, size);
00581 }
00582 
00583 #if defined(OLD_STDCPP) || defined(NEW_STDCPP)
00584 
00593 class __EXPORT sstream : public tcpstream
00594 {
00595 protected:
00596     secure::session_t ssl;
00597     secure::bufio_t bio;
00598     bool server;
00599     bool verify;
00600 
00601 private:
00602     // kill copy constructor
00603     sstream(const sstream&);
00604 
00605 public:
00606     sstream(secure::client_t context);
00607     sstream(const TCPServer *server, secure::server_t context, size_t size = 536);
00608     ~sstream();
00609 
00610     void open(const char *host, const char *service, size_t size = 536);
00611 
00612     void close(void);
00613 
00614     int sync();
00615 
00616     void release(void);
00617 
00618     ssize_t _write(const char *address, size_t size);
00619 
00620     ssize_t _read(char *address, size_t size);
00621 
00622     bool _wait(void);
00623 
00624     inline void flush(void)
00625         {sync();}
00626 
00627     inline bool is_secure(void)
00628         {return bio != NULL;}
00629 };
00630 
00631 #endif
00632 
00633 END_NAMESPACE
00634 
00635 #endif