00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifdef HAVE_CONFIG_H
00021 # include <dtn-config.h>
00022 #endif
00023
00024 #ifdef BSP_ENABLED
00025
00026 #include <cstring>
00027 #include <string>
00028 #include <oasys/compat/inttypes.h>
00029 #include <oasys/util/StringBuffer.h>
00030
00031 #include "KeyDB.h"
00032 #include "Ciphersuite.h"
00033
00034 namespace dtn {
00035
00036 template <>
00037 KeyDB* oasys::Singleton<KeyDB, false>::instance_ = NULL;
00038
00039 static const char * log = "/dtn/bundle/security";
00040
00041 KeyDB::KeyDB()
00042 {
00043 }
00044
00045 KeyDB::~KeyDB()
00046 {
00047 }
00048
00049 void
00050 KeyDB::init()
00051 {
00052 if (instance_ != NULL)
00053 {
00054 PANIC("KeyDB already initialized");
00055 }
00056
00057 instance_ = new KeyDB();
00058 log_debug_p(log, "KeyDB::init() done");
00059 }
00060
00061 void
00062 KeyDB::set_key(Entry& entry)
00063 {
00064 EntryList& keys = instance()->keys_;
00065 EntryList::iterator iter;
00066
00067 for (iter = keys.begin(); iter != keys.end(); iter++)
00068 {
00069 if (iter->match(entry)) {
00070 *iter = entry;
00071 return;
00072 }
00073 }
00074
00075
00076
00077 for (iter = keys.begin();
00078 iter != keys.end() && iter->host() != std::string("*");
00079 iter++)
00080 {
00081 }
00082 keys.insert(iter, entry);
00083 log_debug_p(log, "KeyDB::set_key() done");
00084 }
00085
00086 const KeyDB::Entry*
00087 KeyDB::find_key(const char* host, u_int16_t cs_num)
00088 {
00089 EntryList& keys = instance()->keys_;
00090 EntryList::iterator iter;
00091 log_debug_p(log, "KeyDB::find_key()");
00092
00093 for (iter = keys.begin(); iter != keys.end(); iter++)
00094 {
00095 if (iter->match_wildcard(host, cs_num))
00096 return &(*iter);
00097 }
00098
00099
00100 return NULL;
00101 }
00102
00103 void
00104 KeyDB::del_key(const char* host, u_int16_t cs_num)
00105 {
00106 EntryList& keys = instance()->keys_;
00107 EntryList::iterator iter;
00108 log_debug_p(log, "KeyDB::del_key()");
00109
00110 for (iter = keys.begin(); iter != keys.end(); iter++)
00111 {
00112 if (iter->match(host, cs_num)) {
00113 keys.erase(iter);
00114 return;
00115 }
00116 }
00117
00118
00119 }
00120
00121 void
00122 KeyDB::flush_keys()
00123 {
00124 log_debug_p(log, "KeyDB::flush_keys()");
00125 instance()->keys_.clear();
00126 }
00127
00129 void
00130 KeyDB::dump(oasys::StringBuffer* buf)
00131 {
00132 EntryList& keys = instance()->keys_;
00133 EntryList::iterator iter;
00134
00135 for (iter = keys.begin(); iter != keys.end(); iter++)
00136 iter->dump(buf);
00137 }
00138
00140 void
00141 KeyDB::dump_header(oasys::StringBuffer* buf)
00142 {
00143 KeyDB::Entry::dump_header(buf);
00144 }
00145
00148 bool
00149 KeyDB::validate_cs_num(u_int16_t cs_num)
00150 {
00151 return (Ciphersuite::find_suite(cs_num) != NULL);
00152 }
00153
00154 bool
00155 KeyDB::validate_key_len(u_int16_t cs_num, size_t* key_len)
00156 {
00157 Ciphersuite* cs =
00158 dynamic_cast<Ciphersuite*>(Ciphersuite::find_suite(cs_num));
00159 if (cs == NULL)
00160 return false;
00161 if (*key_len != cs->result_len()) {
00162 *key_len = cs->result_len();
00163 return false;
00164 }
00165
00166 return true;
00167 }
00168
00170 KeyDB::Entry::Entry(const char* host, u_int16_t cs_num, const u_char* key,
00171 size_t key_len)
00172 : host_(host),
00173 cs_num_(cs_num),
00174 key_len_(key_len)
00175 {
00176 ASSERT(key != NULL);
00177 ASSERT(key_len != 0);
00178
00179 key_ = new u_char[key_len];
00180 memcpy(key_, key, key_len);
00181 }
00182
00184 KeyDB::Entry::Entry()
00185 : host_(""),
00186 cs_num_(0),
00187 key_(NULL),
00188 key_len_(0)
00189 {
00190 }
00191
00193 KeyDB::Entry::Entry(const Entry& other)
00194 : host_(other.host_),
00195 cs_num_(other.cs_num_),
00196 key_len_(other.key_len_)
00197 {
00198 if (other.key_ == NULL)
00199 key_ = NULL;
00200 else {
00201 key_ = new u_char[other.key_len_];
00202 memcpy(key_, other.key_, other.key_len_);
00203 }
00204 }
00205
00207 KeyDB::Entry::~Entry()
00208 {
00209 if (key_ != NULL) {
00210 memset(key_, 0, key_len_);
00211 delete key_;
00212 key_len_ = 0;
00213 } else
00214 ASSERT(key_len_ == 0);
00215 }
00216
00218 void
00219 KeyDB::Entry::operator=(const Entry& other)
00220 {
00221 if (key_ != NULL) {
00222 memset(key_, 0, key_len_);
00223 delete key_;
00224 }
00225
00226 host_ = other.host_;
00227 cs_num_ = other.cs_num_;
00228
00229 if (other.key_ == NULL)
00230 key_ = NULL;
00231 else {
00232 key_ = new u_char[other.key_len_];
00233 memcpy(key_, other.key_, other.key_len_);
00234 }
00235
00236 key_len_ = other.key_len_;
00237 }
00238
00241 bool
00242 KeyDB::Entry::match(const Entry& other) const
00243 {
00244 return (host_ == other.host_ && cs_num_ == other.cs_num_);
00245 }
00246
00249 bool
00250 KeyDB::Entry::match(const char* host, u_int16_t cs_num) const
00251 {
00252 return (host_ == std::string(host) && cs_num_ == cs_num);
00253 }
00254
00257 bool
00258 KeyDB::Entry::match_wildcard(const char* host, u_int16_t cs_num) const
00259 {
00260 return ((host_ == std::string(host) || host_ == std::string("*"))
00261 && cs_num_ == cs_num);
00262 }
00263
00265 void
00266 KeyDB::Entry::dump(oasys::StringBuffer* buf) const
00267 {
00268 buf->appendf("%15s 0x%03x ", host_.c_str(), cs_num_);
00269
00270 for (int i = 0; i < (int)key_len_; i++)
00271 buf->appendf("%02x", (int)key_[i]);
00272 buf->appendf("\n");
00273 }
00274
00276 void
00277 KeyDB::Entry::dump_header(oasys::StringBuffer* buf)
00278 {
00279 buf->appendf("host cs_num key\n" );
00280 buf->appendf("--------------- ------ -------------------\n");
00281 }
00282
00283 }
00284
00285 #endif