Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

wvrsa.h

Go to the documentation of this file.
00001 /* -*- Mode: C++ -*-
00002  * Worldvisions Tunnel Vision Software:
00003  *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
00004  *
00005  * RSA cryptography abstractions.
00006  */
00007 #ifndef __WVRSA_H
00008 #define __WVRSA_H
00009 
00010 #include "wverror.h"
00011 #include "wvencoder.h"
00012 #include "wvencoderstream.h"
00013 
00014 struct rsa_st;
00015 
00016 /**
00017  * An RSA public key or public/private key pair that can be used for
00018  * encryption.
00019  * 
00020  * Knows how to encode/decode itself into a string of hex digits
00021  * for easy transport.
00022  * 
00023  * @see WvRSAEncoder
00024  */
00025 class WvRSAKey : public WvError
00026 {
00027     int errnum;
00028     WvString pub, prv;
00029 
00030     void init(WvStringParm keystr, bool priv);
00031     static WvString hexifypub(struct rsa_st *rsa);
00032     static WvString hexifyprv(struct rsa_st *rsa);
00033 
00034 public:
00035     struct rsa_st *rsa;
00036 
00037     WvRSAKey(const WvRSAKey &k);
00038     WvRSAKey(struct rsa_st *_rsa, bool priv); // note: takes ownership
00039     WvRSAKey(WvStringParm keystr, bool priv);
00040     WvRSAKey(int bits);
00041     
00042     ~WvRSAKey();
00043     
00044     WvString private_str() const
00045         { return prv; }
00046     WvString public_str() const
00047         { return pub; }
00048 };
00049 
00050 
00051 /**
00052  * An encoder implementing the RSA public key encryption method.
00053  * 
00054  * This encoder really slow, particularly for decryption, so should
00055  * only be used to negotiate session initiation information.  For
00056  * more intensive work, consider exchanging a key for use with a
00057  * faster symmetric cipher like Blowfish.
00058  * 
00059  * Supports reset().
00060  * 
00061  */
00062 class WvRSAEncoder : public WvEncoder
00063 {
00064 public:
00065     enum Mode {
00066         Encrypt,     /*!< Encrypt with public key */
00067         Decrypt,     /*!< Decrypt with private key */
00068         SignEncrypt, /*!< Encrypt digital signature with private key */
00069         SignDecrypt  /*!< Decrypt digital signature with public key */
00070     };
00071 
00072     /**
00073      * Creates a new RSA cipher encoder.
00074      * 
00075      * "mode" is the encryption mode
00076      * "key" is the public key if mode is Encrypt or SignDecrypt,
00077      *            otherwise the private key
00078      */
00079     WvRSAEncoder(Mode mode, const WvRSAKey &key);
00080     virtual ~WvRSAEncoder();
00081 
00082 protected:
00083     virtual bool _encode(WvBuf &in, WvBuf &out, bool flush);
00084     virtual bool _reset(); // supported
00085 
00086 private:
00087     Mode mode;
00088     WvRSAKey key;
00089     size_t rsasize;
00090 };
00091 
00092 
00093 /**
00094  * A crypto stream implementing RSA public key encryption.
00095  * 
00096  * By default, written data is encrypted using WvRSAEncoder::Encrypt,
00097  * read data is decrypted using WvRSAEncoder::Decrypt.
00098  * 
00099  * @see WvRSAEncoder
00100  */
00101 class WvRSAStream : public WvEncoderStream
00102 {
00103 public:
00104     WvRSAStream(WvStream *_cloned,
00105         const WvRSAKey &_my_key, const WvRSAKey &_their_key, 
00106         WvRSAEncoder::Mode readmode = WvRSAEncoder::Decrypt,
00107         WvRSAEncoder::Mode writemode = WvRSAEncoder::Encrypt);
00108     virtual ~WvRSAStream() { }
00109 };
00110 
00111 
00112 #endif // __WVRSA_H

Generated on Wed Dec 15 15:08:11 2004 for WvStreams by  doxygen 1.3.9.1