IBR-DTNSuite  0.10
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 
50  SecurityKey();
51  virtual ~SecurityKey();
52 
53  // key type
55 
56  // referencing EID of this key
58 
59  // last update time
61 
62  // key file
64 
65  virtual RSA* getRSA() const;
66 
67  virtual EVP_PKEY* getEVP() const;
68 
69  virtual const std::string getData() const;
70 
71  static void free(RSA* key);
72  static void free(EVP_PKEY* key);
73 
74  friend std::ostream &operator<<(std::ostream &stream, const SecurityKey &key)
75  {
76  // key type
77  stream << dtn::data::Number(key.type);
78 
79  // EID reference
81 
82  // timestamp of last update
83  stream << key.lastupdate;
84 
85  // To support concatenation of streaming calls, we return the reference to the output stream.
86  return stream;
87  }
88 
89  friend std::istream &operator>>(std::istream &stream, SecurityKey &key)
90  {
91  // key type
92  dtn::data::Number sdnv_type; stream >> sdnv_type;
93  key.type = KeyType(sdnv_type.get<KeyType>());
94 
95  // EID reference
96  dtn::data::BundleString eid_reference; stream >> eid_reference;
97  key.reference = dtn::data::EID(eid_reference);
98 
99  // timestamp of last update
100  stream >> key.lastupdate;
101 
102  // To support concatenation of streaming calls, we return the reference to the input stream.
103  return stream;
104  }
105 
106  private:
107  RSA* getPublicRSA() const;
108  RSA* getPrivateRSA() const;
109  };
110  }
111 }
112 
113 #endif /* SECURITYKEY_H_ */