26 #include <ibrcommon/Logger.h>
32 #include <openssl/pem.h>
33 #include <openssl/rsa.h>
34 #include <openssl/err.h>
40 const std::string SecurityKeyManager::TAG =
"SecurityKeyManager";
48 SecurityKeyManager::SecurityKeyManager()
62 IBRCOMMON_LOGGER_TAG(SecurityKeyManager::TAG, info) <<
"initialized; path: " << sec.
getPath().getPath() << IBRCOMMON_LOGGER_ENDL;
80 _path = ibrcommon::File();
81 _key = ibrcommon::File();
82 _ca = ibrcommon::File();
86 const std::string SecurityKeyManager::hash(
const dtn::data::EID &eid)
91 const std::string SecurityKeyManager::hash(
const std::string &value)
94 for (std::string::const_iterator iter = value.begin(); iter != value.end(); ++iter)
96 unsigned char c = (*iter);
97 ss << std::hex << std::setw( 2 ) << std::setfill(
'0' ) << (
unsigned int)c;
104 ibrcommon::File keyfile =
getKeyFile(ref, type);
105 return keyfile.exists();
108 void SecurityKeyManager::createRSA(
const dtn::data::EID &ref,
const int bits)
112 RSA* rsa = RSA_new();
113 BIGNUM* e = BN_new();
115 BN_set_word(e, 65537);
117 RSA_generate_key_ex(rsa, bits, e, NULL);
123 int fd = ::open(privkey.getPath().c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0600);
125 FILE * rsa_privkey_file = fdopen(fd,
"w");
126 if (!rsa_privkey_file) {
127 IBRCOMMON_LOGGER_TAG(SecurityKeyManager::TAG, error) <<
"Failed to open " << privkey.getPath() << IBRCOMMON_LOGGER_ENDL;
131 PEM_write_RSAPrivateKey(rsa_privkey_file, rsa, NULL, NULL, 0, NULL, NULL);
132 fclose(rsa_privkey_file);
135 FILE * rsa_pubkey_file = fopen(pubkey.getPath().c_str(),
"w+");
136 if (!rsa_pubkey_file) {
137 IBRCOMMON_LOGGER_TAG(SecurityKeyManager::TAG, error) <<
"Failed to open " << privkey.getPath() << IBRCOMMON_LOGGER_ENDL;
141 PEM_write_RSA_PUBKEY(rsa_pubkey_file, rsa);
142 fclose(rsa_pubkey_file);
177 if (default_key.exists()) {
178 keydata.
file = default_key;
191 if (!keydata.
file.exists())
193 std::stringstream ss;
194 ss <<
"Key file for " << keydata.
reference.
getString() <<
" (" << keydata.
file.getPath() <<
") not found";
201 std::ifstream metastream(keydata.
getMetaFilename().getPath().c_str(), std::ios::in);
202 metastream >> keydata;
208 std::stringstream ss;
210 ifstream stream(key.
file.getPath().c_str(), std::iostream::in);
211 ss << stream.rdbuf();
213 store(key, ss.str());
223 std::ofstream keystream(keydata.
file.getPath().c_str(), std::ios::out | std::ios::trunc);
228 std::ofstream metastream(keydata.
getMetaFilename().getPath().c_str(), std::ios::out | std::ios::trunc);
239 std::ofstream keystream(keydata.
file.getPath().c_str(), std::ios::out | std::ios::trunc);
244 std::ofstream metastream(keydata.
getMetaFilename().getPath().c_str(), std::ios::out | std::ios::trunc);
251 ibrcommon::File keyfile = key.
file;
263 return _path.get(hash(peer) +
".mac");
266 return _path.get(hash(peer) +
".pub");
269 return _path.get(hash(peer) +
".pkey");
272 return _path.get(hash(peer) +
".key");
281 return _path.get(hash(prefix) +
"." + hash(peer) +
".mac");
284 return _path.get(hash(prefix) +
"." + hash(peer) +
".pub");
287 return _path.get(hash(prefix) +
"." + hash(peer) +
".pkey");
290 return _path.get(hash(prefix) +
"." + hash(peer) +
".key");
296 return _path.get(keyword +
"." + extension);
static Configuration & getInstance(bool reset=false)
static SecurityKeyManager & getInstance()
const ibrcommon::File & getBABDefaultKey() const
static dtn::data::EID local
const Configuration::Security & getSecurity() const
const ibrcommon::File getKeyFile(const std::string &prefix, const dtn::data::EID &peer, const dtn::security::SecurityKey::KeyType type=dtn::security::SecurityKey::KEY_UNSPEC) const
const ibrcommon::File & getKey() const
const ibrcommon::File getFilePath(const std::string &keyword, const std::string &extension) const
virtual ~SecurityKeyManager()
const ibrcommon::File & getCertificate() const
void remove(const SecurityKey &key)
dtn::security::SecurityKey get(const dtn::data::EID &ref, const dtn::security::SecurityKey::KeyType type=dtn::security::SecurityKey::KEY_UNSPEC) const
std::string getString() const
bool hasKey(const dtn::data::EID &ref, const dtn::security::SecurityKey::KeyType type=dtn::security::SecurityKey::KEY_UNSPEC) const
void store(const dtn::security::SecurityKey &key, const std::string &data)
ibrcommon::File getMetaFilename() const
const ibrcommon::File & getPath() const
virtual void onConfigurationChanged(const dtn::daemon::Configuration &conf)