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
00073 typedef enum {
00074 SYSTEM_CERTIFICATES, SYSTEM_KEYS} path_t;
00075
00076 protected:
00080 error_t error;
00081
00082 inline secure() {error = OK;};
00083
00084 public:
00089 virtual ~secure();
00090
00094 typedef secure *client_t;
00095
00096 typedef secure *server_t;
00097
00101 typedef void *session_t;
00102
00106 typedef void *bufio_t;
00107
00115 static bool init(const char *program = NULL);
00116
00123 static String path(path_t id);
00124
00130 static int oscerts(const char *path);
00131
00141 static error_t verify(session_t session, const char *peername = NULL);
00142
00152 static server_t server(const char *authority = NULL);
00153
00160 static client_t client(const char *authority = NULL);
00161
00168 static client_t user(const char *authority);
00169
00175 static void cipher(secure *context, const char *ciphers);
00176
00181 inline bool is(void)
00182 {return error == OK;};
00183
00188 inline error_t err(void)
00189 {return error;};
00190
00195 static void uuid(char *string);
00196
00197 static String uuid(void);
00198 };
00199
00207 class __EXPORT SSLBuffer : public TCPBuffer
00208 {
00209 protected:
00210 secure::session_t ssl;
00211 secure::bufio_t bio;
00212 bool server;
00213 bool verify;
00214
00215 public:
00216 SSLBuffer(secure::client_t context);
00217 SSLBuffer(const TCPServer *server, secure::server_t context, size_t size = 536);
00218 ~SSLBuffer();
00219
00227 void open(const char *host, const char *service, size_t size = 536);
00228
00229 void close(void);
00230
00231 void release(void);
00232
00233 size_t _push(const char *address, size_t size);
00234
00235 size_t _pull(char *address, size_t size);
00236
00237 bool _flush(void);
00238
00239 bool _pending(void);
00240
00241 inline bool is_secure(void)
00242 {return bio != NULL;};
00243 };
00244
00254 class __EXPORT Cipher
00255 {
00256 public:
00257 typedef enum {ENCRYPT = 1, DECRYPT = 0} mode_t;
00258
00266 class __EXPORT Key
00267 {
00268 protected:
00269 friend class Cipher;
00270
00271 union {
00272 const void *algotype;
00273 int algoid;
00274 };
00275
00276 union {
00277 const void *hashtype;
00278 int hashid;
00279 };
00280
00281 int modeid;
00282
00283
00284 unsigned char keybuf[MAX_CIPHER_KEYSIZE / 8], ivbuf[MAX_CIPHER_KEYSIZE / 8];
00285
00286
00287 size_t keysize, blksize;
00288
00289 Key(const char *cipher);
00290 Key();
00291
00292 void set(const char *cipher);
00293
00294 public:
00295 Key(const char *cipher, const char *digest, const char *text, size_t size = 0, const unsigned char *salt = NULL, unsigned rounds = 1);
00296 ~Key();
00297
00298 void clear(void);
00299
00300 inline size_t size(void)
00301 {return keysize;};
00302
00303 inline size_t iosize(void)
00304 {return blksize;};
00305
00306 inline operator bool()
00307 {return keysize > 0;};
00308
00309 inline bool operator!()
00310 {return keysize == 0;};
00311 };
00312
00313 typedef Key *key_t;
00314
00315 private:
00316 Key keys;
00317 size_t bufsize, bufpos;
00318 mode_t bufmode;
00319 unsigned char *bufaddr;
00320 void *context;
00321
00322 protected:
00323 virtual void push(unsigned char *address, size_t size);
00324
00325 void release(void);
00326
00327 public:
00328 Cipher();
00329
00330 Cipher(key_t key, mode_t mode, unsigned char *address = NULL, size_t size = 0);
00331
00332 ~Cipher();
00333
00334 void set(unsigned char *address, size_t size = 0);
00335
00336 void set(key_t key, mode_t mode, unsigned char *address, size_t size = 0);
00337
00342 size_t flush(void);
00343
00352 size_t put(const unsigned char *data, size_t size);
00353
00360 size_t puts(const char *string);
00361
00373 size_t pad(const unsigned char *address, size_t size);
00374
00383 size_t process(unsigned char *address, size_t size, bool flag = false);
00384
00385 inline size_t size(void)
00386 {return bufsize;};
00387
00388 inline size_t pos(void)
00389 {return bufpos;};
00390
00391 inline size_t align(void)
00392 {return keys.iosize();};
00393
00399 static bool is(const char *name);
00400 };
00401
00408 class __EXPORT Digest
00409 {
00410 private:
00411 void *context;
00412
00413 union {
00414 const void *hashtype;
00415 int hashid;
00416 };
00417
00418 unsigned bufsize;
00419 unsigned char buffer[MAX_DIGEST_HASHSIZE / 8];
00420 char textbuf[MAX_DIGEST_HASHSIZE / 8 + 1];
00421
00422 protected:
00423 void release(void);
00424
00425 public:
00426 Digest(const char *type);
00427
00428 Digest();
00429
00430 ~Digest();
00431
00432 inline bool puts(const char *str)
00433 {return put(str, strlen(str));};
00434
00435 bool put(const void *memory, size_t size);
00436
00437 inline unsigned size() const
00438 {return bufsize;};
00439
00440 const unsigned char *get(void);
00441
00442 const char *c_str(void);
00443
00444 inline String str(void)
00445 {return String(c_str());};
00446
00447 inline operator String()
00448 {return String(c_str());};
00449
00450 void set(const char *id);
00451
00452 inline void operator=(const char *id)
00453 {set(id);};
00454
00455 inline bool operator *=(const char *text)
00456 {return puts(text);};
00457
00458 inline bool operator +=(const char *text)
00459 {return puts(text);};
00460
00461 inline const char *operator*()
00462 {return c_str();};
00463
00464 inline bool operator!() const
00465 {return !bufsize && context == NULL;};
00466
00467 inline operator bool() const
00468 {return bufsize > 0 || context != NULL;};
00469
00475 void recycle(bool binary = false);
00476
00480 void reset(void);
00481
00487 static bool is(const char *name);
00488
00489 static void uuid(char *string, const char *name, const unsigned char *ns = NULL);
00490
00491 static String uuid(const char *name, const unsigned char *ns = NULL);
00492 };
00493
00499 class __EXPORT Random
00500 {
00501 public:
00508 static bool seed(const unsigned char *buffer, size_t size);
00509
00513 static void seed(void);
00514
00523 static size_t key(unsigned char *memory, size_t size);
00524
00533 static size_t fill(unsigned char *memory, size_t size);
00534
00539 static int get(void);
00540
00547 static int get(int min, int max);
00548
00553 static double real(void);
00554
00561 static double real(double min, double max);
00562
00568 static bool status(void);
00569
00574 static void uuid(char *string);
00575
00576 static String uuid(void);
00577 };
00578
00582 typedef SSLBuffer ssl_t;
00583
00587 typedef Digest digest_t;
00588
00592 typedef Cipher cipher_t;
00593
00597 typedef Cipher::Key skey_t;
00598
00599 inline void zerofill(void *addr, size_t size)
00600 {
00601 ::memset(addr, 0, size);
00602 }
00603
00604 #if defined(OLD_STDCPP) || defined(NEW_STDCPP)
00605
00614 class __EXPORT sstream : public tcpstream
00615 {
00616 protected:
00617 secure::session_t ssl;
00618 secure::bufio_t bio;
00619 bool server;
00620 bool verify;
00621
00622 private:
00623
00624 sstream(const sstream&);
00625
00626 public:
00627 sstream(secure::client_t context);
00628 sstream(const TCPServer *server, secure::server_t context, size_t size = 536);
00629 ~sstream();
00630
00631 void open(const char *host, const char *service, size_t size = 536);
00632
00633 void close(void);
00634
00635 int sync();
00636
00637 void release(void);
00638
00639 ssize_t _write(const char *address, size_t size);
00640
00641 ssize_t _read(char *address, size_t size);
00642
00643 bool _wait(void);
00644
00645 inline void flush(void)
00646 {sync();}
00647
00648 inline bool is_secure(void)
00649 {return bio != NULL;}
00650 };
00651
00652 #endif
00653
00654 END_NAMESPACE
00655
00656 #endif