IBR-DTNSuite  0.8
daemon/src/security/SecurityKeyManager.cpp
Go to the documentation of this file.
00001 /*
00002  * SecurityKeyManager.cpp
00003  *
00004  *  Created on: 02.12.2010
00005  *      Author: morgenro
00006  */
00007 
00008 #include "Configuration.h"
00009 #include "security/SecurityKeyManager.h"
00010 #include <ibrcommon/Logger.h>
00011 #include <sstream>
00012 #include <iomanip>
00013 #include <fstream>
00014 
00015 #include <openssl/pem.h>
00016 #include <openssl/err.h>
00017 
00018 namespace dtn
00019 {
00020         namespace security
00021         {
00022                 SecurityKeyManager& SecurityKeyManager::getInstance()
00023                 {
00024                         static SecurityKeyManager instance;
00025                         return instance;
00026                 }
00027 
00028                 SecurityKeyManager::SecurityKeyManager()
00029                 {
00030                 }
00031 
00032                 SecurityKeyManager::~SecurityKeyManager()
00033                 {
00034                 }
00035 
00036                 void SecurityKeyManager::initialize(const ibrcommon::File &path, const ibrcommon::File &ca, const ibrcommon::File &key)
00037                 {
00038                         IBRCOMMON_LOGGER(info) << "security key manager initialized; path: " << path.getPath() << IBRCOMMON_LOGGER_ENDL;
00039 
00040                         // store all paths locally
00041                         _path = path; _key = key; _ca = ca;
00042                 }
00043 
00044                 const std::string SecurityKeyManager::hash(const dtn::data::EID &eid)
00045                 {
00046                         std::string value = eid.getNode().getString();
00047                         std::stringstream ss;
00048                         for (std::string::const_iterator iter = value.begin(); iter != value.end(); iter++)
00049                         {
00050                                 ss << std::hex << std::setw( 2 ) << std::setfill( '0' ) << (int)(*iter);
00051                         }
00052                         return ss.str();
00053                 }
00054 
00055                 void SecurityKeyManager::prefetchKey(const dtn::data::EID &ref, const dtn::security::SecurityKey::KeyType type)
00056                 {
00057                 }
00058 
00059                 bool SecurityKeyManager::hasKey(const dtn::data::EID &ref, const dtn::security::SecurityKey::KeyType type) const
00060                 {
00061                         const ibrcommon::File keyfile = _path.get(hash(ref.getNode()) + ".pem");
00062                         return keyfile.exists();
00063                 }
00064 
00065                 dtn::security::SecurityKey SecurityKeyManager::get(const dtn::data::EID &ref, const dtn::security::SecurityKey::KeyType type) const throw (SecurityKeyManager::KeyNotFoundException)
00066                 {
00067                         dtn::security::SecurityKey keydata;
00068                         keydata.reference = ref.getNode();
00069                         keydata.type = type;
00070 
00071                         switch (type)
00072                         {
00073                                 case SecurityKey::KEY_SHARED:
00074                                 {
00075                                         // read a symmetric key required for BAB signing
00076                                         const ibrcommon::File keyfile = _path.get(hash(ref.getNode()) + ".mac");
00077 
00078                                         if (!keyfile.exists())
00079                                         {
00080                                                 // get the default shared key
00081                                                 const ibrcommon::File default_key = dtn::daemon::Configuration::getInstance().getSecurity().getBABDefaultKey();
00082 
00083                                                 if (default_key.exists())
00084                                                 {
00085                                                         keydata.file = default_key;
00086                                                         keydata.lastupdate = default_key.lastmodify();
00087                                                         break;
00088                                                 }
00089 
00090                                                 IBRCOMMON_LOGGER(warning) << "Key file for " << ref.getString() << " (" << keyfile.getPath() << ") not found" << IBRCOMMON_LOGGER_ENDL;
00091                                                 throw SecurityKeyManager::KeyNotFoundException();
00092                                         }
00093 
00094                                         keydata.file = keyfile;
00095                                         keydata.lastupdate = keyfile.lastmodify();
00096                                         break;
00097                                 }
00098 
00099                                 case SecurityKey::KEY_UNSPEC:
00100                                 case SecurityKey::KEY_PUBLIC:
00101                                 case SecurityKey::KEY_PRIVATE:
00102                                 {
00103                                         const ibrcommon::File keyfile = _path.get(hash(ref.getNode()) + ".pem");
00104 
00105                                         if (!keyfile.exists())
00106                                         {
00107                                                 IBRCOMMON_LOGGER(warning) << "Key file for " << ref.getString() << " (" << keyfile.getPath() << ") not found" << IBRCOMMON_LOGGER_ENDL;
00108                                                 throw SecurityKeyManager::KeyNotFoundException();
00109                                         }
00110 
00111 
00112                                         keydata.file = keyfile;
00113                                         keydata.lastupdate = keyfile.lastmodify();
00114                                         break;
00115                                 }
00116                         }
00117 
00118                         return keydata;
00119                 }
00120 
00121                 void SecurityKeyManager::store(const dtn::data::EID &ref, const std::string &data, const dtn::security::SecurityKey::KeyType type)
00122                 {
00123                         ibrcommon::File keyfile = _path.get(hash(ref.getNode()) + ".pem");
00124 
00125                         // delete if already exists
00126                         if (keyfile.exists()) keyfile.remove();
00127 
00128                         std::ofstream keystream(keyfile.getPath().c_str());
00129                         keystream << data;
00130                         keystream.close();
00131                 }
00132         }
00133 }