Main Page | Class Hierarchy | Class List | File List | Class Members

/Users/baford/proj/netsteria/sst/lib/ident.h

00001 #ifndef SST_IDENT_H
00002 #define SST_IDENT_H
00003 
00004 #include <QByteArray>
00005 #include <QSharedData>
00006 
00007 #include <openssl/dsa.h>
00008 #include <openssl/rsa.h>
00009 
00010 #include "sign.h"
00011 
00012 class QHostAddress;
00013 class QSettings;
00014 
00015 
00016 namespace SST {
00017 
00018 class SecureHash;
00019 class Ident;
00020 
00021 
00022 class IdentData : public QSharedData
00023 {
00024         friend class Ident;
00025 
00026         QByteArray id;
00027         SignKey *k;
00028 
00029 public:
00030         inline IdentData(const QByteArray id) : id(id), k(NULL) { }
00031         IdentData(const IdentData &other);
00032         ~IdentData();
00033 
00034         bool setKey(const QByteArray &key);
00035         void clearKey();
00036 };
00037 
00041 class Ident
00042 {
00043         QSharedDataPointer<IdentData> d;
00044 
00045 public:
00050         enum Scheme {
00051                 NoScheme = 0,   
00052 
00053                 // Non-cryptographic legacy address schemes
00054                 MAC     = 1,    
00055                 IP      = 2,    
00056 
00057                 // Cryptographic identity schemes
00058                 DSA160  = 10,   
00059                 RSA160  = 11,   
00060         };
00061 
00063         Ident();
00064 
00068         Ident(const QByteArray &id);
00069 
00075         Ident(const QByteArray &id, const QByteArray &key);
00076 
00077 
00080         inline QByteArray id() const { return d->id; }
00081 
00086         void setID(const QByteArray &id);
00087 
00090         inline bool isNull() { return d->id.isEmpty(); }
00091 
00094         inline Scheme scheme() const {
00095                 return d->id.isEmpty() ? NoScheme
00096                         : (Scheme)(d->id.at(0) >> 2); }
00097 
00101         inline bool haveKey() const
00102                 { return d->k && d->k->type() != SignKey::Invalid; }
00103 
00107         inline bool havePrivateKey() const
00108                 { return d->k && d->k->type() == SignKey::Private; }
00109 
00116         inline QByteArray key(bool getPrivateKey = false) const
00117                 { return d->k->key(getPrivateKey); }
00118 
00124         bool setKey(const QByteArray &key);
00125 
00132         inline SecureHash *newHash(QObject *parent = NULL) const
00133                 { return d->k->newHash(parent); }
00134 
00142         QByteArray hash(const void *data, int len) const;
00143 
00150         inline QByteArray hash(const QByteArray &data) const
00151                 { return hash(data.constData(), data.size()); }
00152 
00158         inline QByteArray sign(const QByteArray &digest) const
00159                 { return d->k->sign(digest); }
00160 
00167         inline bool verify(const QByteArray &digest,
00168                                 const QByteArray &sig) const
00169                 { return d->k->verify(digest, sig); }
00170 
00178         static Ident generate(Scheme sch = RSA160, int bits = 0);
00179 
00180 
00186         static Ident fromMacAddress(const QByteArray &addr);
00187 
00190         QByteArray macAddress();
00191 
00192 
00198         static Ident fromIpAddress(const QHostAddress &addr, quint16 port = 0);
00199 
00203         QHostAddress ipAddress(quint16 *out_port = NULL);
00204 
00207         quint16 ipPort();
00208 };
00209 
00210 
00212 class IdentHostState
00213 {
00214         Ident hid;
00215 
00216 
00217 public:
00223         Ident hostIdent(bool create = true);
00224 
00228         void setHostIdent(const Ident &ident);
00229 
00243         void initHostIdent(QSettings *settings);
00244 };
00245 
00246 
00247 } // namespace SST
00248 
00249 #endif  // SST_IDENT_H

Generated on Wed Mar 28 11:48:05 2007 for SST by doxygen 1.3.4