IBR-DTN  1.0.0
SecurityKey.h
Go to the documentation of this file.
1 /*
2  * SecurityKey.h
3  *
4  * Copyright (C) 2011 IBR, TU Braunschweig
5  *
6  * Written-by: Johannes Morgenroth <morgenroth@ibr.cs.tu-bs.de>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 
22 #ifndef SECURITYKEY_H_
23 #define SECURITYKEY_H_
24 
25 #include "ibrdtn/data/EID.h"
26 #include "ibrdtn/data/Number.h"
27 #include "ibrdtn/data/DTNTime.h"
29 #include <ibrcommon/data/File.h>
30 #include <openssl/rsa.h>
31 
32 #include <string>
33 #include <iostream>
34 
35 namespace dtn
36 {
37  namespace security
38  {
40  {
41  public:
42  enum KeyType
43  {
48  };
49 
51  {
53  LOW,
56  };
57 
58  class KeyNotFoundException : public ibrcommon::Exception
59  {
60  public:
61  KeyNotFoundException(std::string what = "Requested key not found.") : ibrcommon::Exception(what)
62  {};
63 
64  virtual ~KeyNotFoundException() throw() {};
65  };
66 
67  SecurityKey();
68  virtual ~SecurityKey();
69 
70  // key type
72 
73  // referencing EID of this key
75 
76  // last update time
78 
79  // trust-level of this key
81 
82  // key file
83  ibrcommon::File file;
84 
85  // flags
87 
88  bool operator==(const SecurityKey &key);
89 
90  ibrcommon::File getMetaFilename() const;
91 
92  virtual RSA* getRSA() const;
93 
94  virtual EVP_PKEY* getEVP() const;
95 
96  virtual const std::string getData() const;
97 
98  virtual const std::string getFingerprint() const;
99 
100  static void free(RSA* key);
101  static void free(EVP_PKEY* key);
102 
103  friend std::ostream &operator<<(std::ostream &stream, const SecurityKey &key)
104  {
105  // key type
106  stream << dtn::data::Number(key.type);
107 
108  // EID reference
110 
111  // timestamp of last update
112  stream << dtn::data::DTNTime();
113 
114  // store trust-level
115  stream << dtn::data::Number(key.trustlevel);
116 
117  // store flags
118  stream << key.flags;
119 
120  // To support concatenation of streaming calls, we return the reference to the output stream.
121  return stream;
122  }
123 
124  friend std::istream &operator>>(std::istream &stream, SecurityKey &key)
125  {
126  // key type
127  dtn::data::Number sdnv_type; stream >> sdnv_type;
128  key.type = KeyType(sdnv_type.get<KeyType>());
129 
130  // EID reference
131  dtn::data::BundleString eid_reference; stream >> eid_reference;
132  key.reference = dtn::data::EID(eid_reference);
133 
134  // timestamp of last update
135  stream >> key.lastupdate;
136 
137  // load trust-level
139  stream >> tl;
140  key.trustlevel = TrustLevel(tl.get<size_t>());
141 
142  // load flags
143  stream >> key.flags;
144 
145  // To support concatenation of streaming calls, we return the reference to the input stream.
146  return stream;
147  }
148 
149  static std::string getFingerprint(const ibrcommon::File &file);
150  static std::string getFingerprint(RSA* rsa);
151 
152  private:
153  RSA* getPublicRSA() const;
154  RSA* getPrivateRSA() const;
155  };
156  }
157 }
158 
159 #endif /* SECURITYKEY_H_ */
ibrcommon::File file
Definition: SecurityKey.h:83
virtual EVP_PKEY * getEVP() const
Definition: SecurityKey.cpp:89
rsa_st RSA
Definition: SecurityBlock.h:35
static void free(RSA *key)
Definition: SecurityKey.cpp:44
dtn::data::DTNTime lastupdate
Definition: SecurityKey.h:77
KeyNotFoundException(std::string what="Requested key not found.")
Definition: SecurityKey.h:61
virtual const std::string getData() const
Definition: SecurityKey.cpp:64
friend std::ostream & operator<<(std::ostream &stream, const SecurityKey &key)
Definition: SecurityKey.h:103
virtual RSA * getRSA() const
Definition: SecurityKey.cpp:76
friend std::istream & operator>>(std::istream &stream, SecurityKey &key)
Definition: SecurityKey.h:124
bool operator==(const SecurityKey &key)
Definition: SecurityKey.cpp:54
dtn::data::EID reference
Definition: SecurityKey.h:74
dtn::data::SDNV< unsigned int > flags
Definition: SecurityKey.h:86
virtual const std::string getFingerprint() const
std::string getString() const
Definition: EID.cpp:374
dtn::data::SDNV< Size > Number
Definition: Number.h:38
ibrcommon::File getMetaFilename() const
Definition: SecurityKey.cpp:59