WvStreams
wvsslstream.h
00001 /* -*- Mode: C++ -*-
00002  * Worldvisions Weaver Software:
00003  *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
00004  *
00005  * SSL (Socket Security Layer) communications via WvStreams.
00006  */ 
00007 #ifndef __WVSSLSTREAM_H
00008 #define __WVSSLSTREAM_H
00009 
00010 #include "wvfdstream.h"
00011 #include "wvlog.h"
00012 #include "wvstreamclone.h"
00013 #include "wvtr1.h"
00014 
00015 struct ssl_st;
00016 struct ssl_ctx_st;
00017 struct ssl_method_st;
00018 
00019 typedef struct ssl_ctx_st SSL_CTX;
00020 typedef struct ssl_st SSL;
00021 typedef struct ssl_method_st SSL_METHOD;
00022 
00023 class WvX509;
00024 class WvX509Mgr;
00025 class WvSSLStream;
00026 
00027 typedef wv::function<bool(WvX509*)> WvSSLValidateCallback;
00028 typedef wv::function<bool(WvX509*, WvSSLStream *)> WvSSLGlobalValidateCallback;
00029 
00035 class WvSSLStream : public WvStreamClone
00036 {
00037 public:
00038     /* This ValidateCallback is purely more convenient to set (not passed in
00039      * via constructor) than its local cousin.  It is used when you want an
00040      * easy way to assign a validation function to any WvSSLStream you might
00041      * be using.  NOTE:  It should be assigned before you instantiate a stream,
00042      * and should never be changed while WvSSLStreams still linger.
00043      *
00044      * NOTE:  Using wv::bind can effectively bind an object with a particular
00045      * function for this callback, so you can do all sorts of interesting stuff
00046      * with it.
00047      */
00048     static WvSSLGlobalValidateCallback global_vcb;
00054     WvSSLStream(IWvStream *_slave, WvX509Mgr *_x509 = NULL, 
00055                 WvSSLValidateCallback _vcb = 0, bool _is_server = false);
00056     
00058     virtual ~WvSSLStream();
00059     
00060     virtual void pre_select(SelectInfo &si);
00061     virtual bool post_select(SelectInfo &si);
00062     
00063     virtual void close();
00064     virtual bool isok() const;
00065     virtual void noread();
00066     virtual void nowrite();
00067     
00068 protected:
00069     WvX509Mgr *x509;
00070     
00072     SSL_CTX *ctx;
00073     
00078     SSL *ssl;
00079     
00080     virtual size_t uwrite(const void *buf, size_t len);
00081     virtual size_t uread(void *buf, size_t len);
00082     
00083 private:
00088     bool sslconnected;
00089     SelectRequest connect_wants;
00090 
00092     void setconnected(bool conn);
00093     
00095     bool is_server;
00096     
00098     bool ssl_stop_read, ssl_stop_write;
00099     
00101     WvSSLValidateCallback vcb;
00102     
00104     WvLog debug;
00105 
00114     WvInPlaceBuf write_bouncebuf;
00115     size_t write_eat;
00116 
00118     WvInPlaceBuf read_bouncebuf;
00119     bool read_pending;
00120 
00122     WvDynBuf unconnected_buf;
00123 
00125     void printerr(WvStringParm func);
00126 
00127 public:
00128     const char *wstype() const { return "WvSSLStream"; }
00129 };
00130 
00131 #endif // __WVSSLSTREAM_H
00132