IBR-DTNSuite  0.8
ibrdtn/ibrdtn/security/SecurityKey.h
Go to the documentation of this file.
00001 /*
00002  * SecurityKey.h
00003  *
00004  *  Created on: 06.01.2011
00005  *      Author: morgenro
00006  */
00007 
00008 #ifndef SECURITYKEY_H_
00009 #define SECURITYKEY_H_
00010 
00011 #include "ibrdtn/data/EID.h"
00012 #include "ibrdtn/data/SDNV.h"
00013 #include "ibrdtn/data/DTNTime.h"
00014 #include "ibrdtn/data/BundleString.h"
00015 #include <ibrcommon/data/File.h>
00016 #include <openssl/rsa.h>
00017 
00018 #include <string>
00019 #include <iostream>
00020 
00021 namespace dtn
00022 {
00023         namespace security
00024         {
00025                 class SecurityKey
00026                 {
00027                 public:
00028                         enum KeyType
00029                         {
00030                                 KEY_UNSPEC = 0,
00031                                 KEY_SHARED = 1,
00032                                 KEY_PRIVATE = 2,
00033                                 KEY_PUBLIC = 3
00034                         };
00035 
00036                         SecurityKey();
00037                         virtual ~SecurityKey();
00038 
00039                         // key type
00040                         KeyType type;
00041 
00042                         // referencing EID of this key
00043                         dtn::data::EID reference;
00044 
00045                         // last update time
00046                         dtn::data::DTNTime lastupdate;
00047 
00048                         // key file
00049                         ibrcommon::File file;
00050 
00051                         virtual RSA* getRSA() const;
00052 
00053                         virtual EVP_PKEY* getEVP() const;
00054 
00055                         virtual const std::string getData() const;
00056 
00057                         static void free(RSA* key);
00058                         static void free(EVP_PKEY* key);
00059 
00060                         friend std::ostream &operator<<(std::ostream &stream, const SecurityKey &key)
00061                         {
00062                                 // key type
00063                                 stream << dtn::data::SDNV(key.type);
00064 
00065                                 // EID reference
00066                                 stream << dtn::data::BundleString(key.reference.getString());
00067 
00068                                 // timestamp of last update
00069                                 stream << key.lastupdate;
00070 
00071                                 // To support concatenation of streaming calls, we return the reference to the output stream.
00072                                 return stream;
00073                         }
00074 
00075                         friend std::istream &operator>>(std::istream &stream, SecurityKey &key)
00076                         {
00077                                 // key type
00078                                 dtn::data::SDNV sdnv_type; stream >> sdnv_type;
00079                                 key.type = KeyType(sdnv_type.getValue());
00080 
00081                                 // EID reference
00082                                 dtn::data::BundleString eid_reference; stream >> eid_reference;
00083                                 key.reference = dtn::data::EID(eid_reference);
00084 
00085                                 // timestamp of last update
00086                                 stream >> key.lastupdate;
00087 
00088                                 // To support concatenation of streaming calls, we return the reference to the input stream.
00089                                 return stream;
00090                         }
00091 
00092                 private:
00093                         RSA* getPublicRSA() const;
00094                         RSA* getPrivateRSA() const;
00095                 };
00096         }
00097 }
00098 
00099 #endif /* SECURITYKEY_H_ */