00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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 private:
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
00263 unsigned char keybuf[MAX_CIPHER_KEYSIZE / 8], ivbuf[MAX_CIPHER_KEYSIZE / 8];
00264
00265
00266 size_t keysize, blksize;
00267
00268 public:
00269 Key(const char *cipher, const char *digest, const char *text, size_t size = 0, const unsigned char *salt = NULL, unsigned rounds = 1);
00270 Key();
00271 ~Key();
00272
00273 void clear(void);
00274
00275 inline size_t size(void)
00276 {return keysize;};
00277
00278 inline size_t iosize(void)
00279 {return blksize;};
00280 };
00281
00282 typedef Key *key_t;
00283
00284 private:
00285 Key keys;
00286 size_t bufsize, bufpos;
00287 mode_t bufmode;
00288 unsigned char *bufaddr;
00289 void *context;
00290
00291 protected:
00292 virtual void push(unsigned char *address, size_t size);
00293
00294 void release(void);
00295
00296 public:
00297 Cipher();
00298
00299 Cipher(key_t key, mode_t mode, unsigned char *address = NULL, size_t size = 0);
00300
00301 ~Cipher();
00302
00303 void set(unsigned char *address, size_t size = 0);
00304
00305 void set(key_t key, mode_t mode, unsigned char *address, size_t size = 0);
00306
00311 size_t flush(void);
00312
00321 size_t put(const unsigned char *data, size_t size);
00322
00329 size_t puts(const char *string);
00330
00342 size_t pad(const unsigned char *address, size_t size);
00343
00352 size_t process(unsigned char *address, size_t size, bool flag = false);
00353
00354 inline size_t size(void)
00355 {return bufsize;};
00356
00357 inline size_t pos(void)
00358 {return bufpos;};
00359
00360 inline size_t align(void)
00361 {return keys.iosize();};
00362
00368 static bool is(const char *name);
00369 };
00370
00377 class __EXPORT Digest
00378 {
00379 private:
00380 void *context;
00381
00382 union {
00383 const void *hashtype;
00384 int hashid;
00385 };
00386
00387 unsigned bufsize;
00388 unsigned char buffer[MAX_DIGEST_HASHSIZE / 8];
00389 char textbuf[MAX_DIGEST_HASHSIZE / 8 + 1];
00390
00391 protected:
00392 void release(void);
00393
00394 public:
00395 Digest(const char *type);
00396
00397 Digest();
00398
00399 ~Digest();
00400
00401 inline bool puts(const char *str)
00402 {return put(str, strlen(str));};
00403
00404 bool put(const void *memory, size_t size);
00405
00406 inline unsigned size() const
00407 {return bufsize;};
00408
00409 const unsigned char *get(void);
00410
00411 const char *c_str(void);
00412
00413 inline String str(void)
00414 {return String(c_str());};
00415
00416 inline operator String()
00417 {return String(c_str());};
00418
00419 void set(const char *id);
00420
00421 inline void operator=(const char *id)
00422 {set(id);};
00423
00424 inline bool operator *=(const char *text)
00425 {return puts(text);};
00426
00427 inline bool operator +=(const char *text)
00428 {return puts(text);};
00429
00430 inline const char *operator*()
00431 {return c_str();};
00432
00433 inline bool operator!() const
00434 {return !bufsize && context == NULL;};
00435
00436 inline operator bool() const
00437 {return bufsize > 0 || context != NULL;};
00438
00444 void recycle(bool binary = false);
00445
00449 void reset(void);
00450
00456 static bool is(const char *name);
00457
00458 static void uuid(char *string, const char *name, const unsigned char *ns = NULL);
00459
00460 static String uuid(const char *name, const unsigned char *ns = NULL);
00461 };
00462
00468 class __EXPORT Random
00469 {
00470 public:
00477 static bool seed(const unsigned char *buffer, size_t size);
00478
00482 static void seed(void);
00483
00492 static size_t key(unsigned char *memory, size_t size);
00493
00502 static size_t fill(unsigned char *memory, size_t size);
00503
00508 static int get(void);
00509
00516 static int get(int min, int max);
00517
00522 static double real(void);
00523
00530 static double real(double min, double max);
00531
00537 static bool status(void);
00538
00543 static void uuid(char *string);
00544
00545 static String uuid(void);
00546 };
00547
00551 typedef SSLBuffer ssl_t;
00552
00556 typedef Digest digest_t;
00557
00561 typedef Cipher cipher_t;
00562
00566 typedef Cipher::Key skey_t;
00567
00568 inline void zerofill(void *addr, size_t size)
00569 {
00570 ::memset(addr, 0, size);
00571 }
00572
00573 #if defined(OLD_STDCPP) || defined(NEW_STDCPP)
00574
00583 class __EXPORT sstream : public tcpstream
00584 {
00585 protected:
00586 secure::session_t ssl;
00587 secure::bufio_t bio;
00588 bool server;
00589 bool verify;
00590
00591 public:
00592 sstream(secure::client_t context);
00593 sstream(const TCPServer *server, secure::server_t context, size_t size = 536);
00594 ~sstream();
00595
00596 void open(const char *host, const char *service, size_t size = 536);
00597
00598 void close(void);
00599
00600 int sync();
00601
00602 void release(void);
00603
00604 ssize_t _write(const char *address, size_t size);
00605
00606 ssize_t _read(char *address, size_t size);
00607
00608 bool _wait(void);
00609
00610 inline void flush(void)
00611 {sync();}
00612
00613 inline bool is_secure(void)
00614 {return bio != NULL;}
00615 };
00616
00617 #endif
00618
00619 END_NAMESPACE
00620
00621 #endif