IBR-DTN  1.0.0
SecurityBlock.h
Go to the documentation of this file.
1 /*
2  * SecurityBlock.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 SECURITYBLOCK_H_
23 #define SECURITYBLOCK_H_
24 
25 #include "ibrdtn/data/Block.h"
26 #include "ibrdtn/data/EID.h"
28 #include "ibrdtn/data/Bundle.h"
29 #include <ibrcommon/Exceptions.h>
30 #include <ibrcommon/ssl/AES128Stream.h> // TODO <-- this include sucks
31 #include <list>
32 #include <sys/types.h>
33 
34 // forward deklaration needed for struct RSA
35 struct rsa_st;
36 // forward deklaration of RSA object
37 typedef rsa_st RSA;
38 
39 namespace dtn
40 {
41  namespace security
42  {
43  class SecurityException : public ibrcommon::Exception
44  {
45  public:
46  SecurityException(std::string what = "security has been violated") : ibrcommon::Exception(what)
47  {};
48 
49  virtual ~SecurityException() throw() {};
50  };
51 
53  {
54  public:
55  EncryptException(std::string what = "Encryption failed.") : SecurityException(what)
56  {};
57 
58  virtual ~EncryptException() throw() {};
59  };
60 
62  {
63  public:
64  DecryptException(std::string what = "Decryption failed.") : SecurityException(what)
65  {};
66 
67  virtual ~DecryptException() throw() {};
68  };
69 
71  {
72  public:
73  VerificationSkippedException(std::string what = "Verification skipped.") : SecurityException(what)
74  {};
75 
76  virtual ~VerificationSkippedException() throw() {};
77  };
78 
80  {
81  public:
82  VerificationFailedException(std::string what = "Verification failed.") : SecurityException(what)
83  {};
84 
85  virtual ~VerificationFailedException() throw() {};
86  };
87 
88  class ElementMissingException : public ibrcommon::Exception
89  {
90  public:
91  ElementMissingException(std::string what = "Requested element is missing.") : ibrcommon::Exception(what)
92  {};
93 
94  virtual ~ElementMissingException() throw() {};
95  };
96 
97  class MutableSerializer;
98  class StrictSerializer;
99 
114  {
115  friend class StrictSerializer;
116  friend class MutableSerializer;
117  public:
120  {
125  };
129  {
130  not_set = 0,
135  salt = 7,
139  };
142  {
148  BIT5_RESERVED = 1 << 5,
149  BIT6_RESERVED = 1 << 6
150  };
153  {
154  BAB_HMAC = 0x001,
155  PIB_RSA_SHA256 = 0x002,
158  };
159 
160  class TLV
161  {
162  public:
163  TLV() : _type(not_set) {};
164  TLV(TLV_TYPES type, std::string value)
165  : _type(type), _value(value)
166  { }
167 
168  bool operator<(const TLV &tlv) const;
169  bool operator==(const TLV &tlv) const;
170 
171  const std::string getValue() const;
172  TLV_TYPES getType() const;
174 
175  friend std::ostream& operator<<(std::ostream &stream, const TLV &tlv);
176  friend std::istream& operator>>(std::istream &stream, TLV &tlv);
177 
178  private:
179  TLV_TYPES _type;
181  };
182 
183  class TLVList : public std::set<TLV>
184  {
185  public:
186  TLVList() {};
187  virtual ~TLVList() {};
188 
189  friend std::ostream& operator<<(std::ostream &stream, const TLVList &tlvlist);
190  friend std::istream& operator>>(std::istream &stream, TLVList &tlvlist);
191 
192  const std::string get(TLV_TYPES type) const;
193  void get(TLV_TYPES type, unsigned char *value, dtn::data::Length length) const;
194  void set(TLV_TYPES type, std::string value);
195  void set(TLV_TYPES type, const unsigned char *value, dtn::data::Length length);
196  void remove(TLV_TYPES type);
197 
198  const std::string toString() const;
200 
201  private:
202  dtn::data::Length getPayloadLength() const;
203  };
204 
206  virtual ~SecurityBlock() = 0;
207 
212  virtual dtn::data::Length getLength() const;
213 
219  virtual dtn::data::Length getLength_mutable() const;
220 
226  virtual std::ostream &serialize(std::ostream &stream, dtn::data::Length &length) const;
227 
234  virtual std::ostream &serialize_strict(std::ostream &stream, dtn::data::Length &length) const;
235 
240  virtual std::istream &deserialize(std::istream &stream, const dtn::data::Length &length);
241 
245  const dtn::data::EID getSecuritySource() const;
246 
252 
257  void setSecuritySource(const dtn::data::EID &source);
258 
263  void setSecurityDestination(const dtn::data::EID &destination);
264 
271  bool isSecuritySource(const dtn::data::Bundle&, const dtn::data::EID&) const;
272 
279  bool isSecurityDestination(const dtn::data::Bundle&, const dtn::data::EID&) const;
280 
287 
294 
295  protected:
305 
309 
312 
315 
318 
321 
329 
336 
341  void setCiphersuiteId(const CIPHERSUITE_IDS id);
342 
347  void setCorrelator(const dtn::data::Number &corr);
348 
355  static bool isCorrelatorPresent(const dtn::data::Bundle &bundle, const dtn::data::Number &correlator);
356 
364 
370  virtual MutableSerializer &serialize_mutable(MutableSerializer &serializer, bool include_security_result = true) const;
371 
382 
389  static void createSaltAndKey(uint32_t& salt, unsigned char * key, dtn::data::Length key_size);
390 
401  static void addKey(TLVList& security_parameter, unsigned char const * const key, dtn::data::Length key_size, RSA * rsa);
402 
412  static bool getKey(const TLVList& security_parameter, unsigned char * key, dtn::data::Length key_size, RSA * rsa);
413 
419  static void addSalt(TLVList& security_parameters, const uint32_t &salt);
420 
425  static uint32_t getSalt(const TLVList& security_parameters);
426 
433  static void copyEID(const dtn::data::Block& from, dtn::data::Block& to, dtn::data::Length skip = 0);
434 
448  template <class T>
449  static T& encryptBlock(dtn::data::Bundle& bundle, dtn::data::Bundle::iterator &it, uint32_t salt, const unsigned char ephemeral_key[ibrcommon::AES128Stream::key_size_in_bytes]);
450 
462  static void decryptBlock(dtn::data::Bundle& bundle, dtn::data::Bundle::iterator &it, uint32_t salt, const unsigned char key[ibrcommon::AES128Stream::key_size_in_bytes]);
463 
469  static void addFragmentRange(TLVList& ciphersuite_params, const dtn::data::Number &fragmentoffset, const dtn::data::Number &payload_length);
470 
474  static void getFragmentRange(const TLVList& ciphersuite_params, dtn::data::Number &offset, dtn::data::Number &range);
475 
476  private:
480  SecurityBlock& operator=(const SecurityBlock&);
481  };
482 
483  template <class T>
484  T& SecurityBlock::encryptBlock(dtn::data::Bundle& bundle, dtn::data::Bundle::iterator &it, uint32_t salt, const unsigned char ephemeral_key[ibrcommon::AES128Stream::key_size_in_bytes])
485  {
486  const dtn::data::Block &block = (**it);
487 
488  // insert ESB, block can be removed after encryption, because bundle will destroy it
489  T& esb = bundle.insert<T>(it);
490 
491  // take eid list
492  copyEID(block, esb);
493 
494  std::stringstream ss;
495  ibrcommon::AES128Stream encrypt(ibrcommon::CipherStream::CIPHER_ENCRYPT, ss, ephemeral_key, salt);
496  dtn::data::Dictionary dict(bundle);
497  dtn::data::DefaultSerializer dser(encrypt, dict);
498  dser << block;
499  encrypt << std::flush;
500 
501  // append tag at the end of the ciphertext
502  unsigned char tag[ibrcommon::AES128Stream::tag_len]; encrypt.getTag(tag);
503  ss.write((const char*)&tag, ibrcommon::AES128Stream::tag_len);
504 
505  esb._security_result.set(SecurityBlock::encapsulated_block, ss.str());
506  esb._ciphersuite_flags |= SecurityBlock::CONTAINS_SECURITY_RESULT;
507 
508  unsigned char iv[ibrcommon::AES128Stream::iv_len]; encrypt.getIV(iv);
509  esb._ciphersuite_params.set(SecurityBlock::initialization_vector, iv, ibrcommon::AES128Stream::iv_len);
510 
511  esb._ciphersuite_flags |= SecurityBlock::CONTAINS_CIPHERSUITE_PARAMS;
512 
513  bundle.erase(it++);
514 
515  return esb;
516  }
517  }
518 }
519 
520 #endif /* SECURITYBLOCK_H_ */
dtn::data::Bitset< CIPHERSUITE_FLAGS > _ciphersuite_flags
friend std::istream & operator>>(std::istream &stream, TLV &tlv)
VerificationFailedException(std::string what="Verification failed.")
Definition: SecurityBlock.h:82
void setCorrelator(const dtn::data::Number &corr)
const dtn::data::EID getSecuritySource() const
size_t Length
Definition: Number.h:33
static void decryptBlock(dtn::data::Bundle &bundle, dtn::data::Bundle::iterator &it, uint32_t salt, const unsigned char key[ibrcommon::AES128Stream::key_size_in_bytes])
rsa_st RSA
Definition: SecurityBlock.h:35
EncryptException(std::string what="Encryption failed.")
Definition: SecurityBlock.h:55
virtual std::istream & deserialize(std::istream &stream, const dtn::data::Length &length)
void set(ProcFlags flag, const bool &value)
Definition: Block.cpp:77
bool isSecurityDestination(const dtn::data::Bundle &, const dtn::data::EID &) const
void set(TLV_TYPES type, std::string value)
TLV(TLV_TYPES type, std::string value)
void setSecurityDestination(const dtn::data::EID &destination)
friend std::ostream & operator<<(std::ostream &stream, const TLVList &tlvlist)
virtual dtn::data::Length getSecurityResultSize() const
SecurityException(std::string what="security has been violated")
Definition: SecurityBlock.h:46
VerificationSkippedException(std::string what="Verification skipped.")
Definition: SecurityBlock.h:73
void erase(iterator it)
Definition: Bundle.cpp:127
dtn::data::EID _security_destination
void setCiphersuiteId(const CIPHERSUITE_IDS id)
bool operator<(const TLV &tlv) const
static bool getKey(const TLVList &security_parameter, unsigned char *key, dtn::data::Length key_size, RSA *rsa)
T & insert(iterator before)
Definition: Bundle.h:200
dtn::data::Length getLength() const
dtn::data::Length getLength() const
const std::string toString() const
virtual MutableSerializer & serialize_mutable(MutableSerializer &serializer, bool include_security_result=true) const
static T & encryptBlock(dtn::data::Bundle &bundle, dtn::data::Bundle::iterator &it, uint32_t salt, const unsigned char ephemeral_key[ibrcommon::AES128Stream::key_size_in_bytes])
friend std::istream & operator>>(std::istream &stream, TLVList &tlvlist)
static void addFragmentRange(TLVList &ciphersuite_params, const dtn::data::Number &fragmentoffset, const dtn::data::Number &payload_length)
static void copyEID(const dtn::data::Block &from, dtn::data::Block &to, dtn::data::Length skip=0)
dtn::data::Number _correlator
virtual std::ostream & serialize_strict(std::ostream &stream, dtn::data::Length &length) const
static void getFragmentRange(const TLVList &ciphersuite_params, dtn::data::Number &offset, dtn::data::Number &range)
block_list::iterator iterator
Definition: Bundle.h:76
virtual std::ostream & serialize(std::ostream &stream, dtn::data::Length &length) const
static bool isCorrelatorPresent(const dtn::data::Bundle &bundle, const dtn::data::Number &correlator)
static void addKey(TLVList &security_parameter, unsigned char const *const key, dtn::data::Length key_size, RSA *rsa)
friend std::ostream & operator<<(std::ostream &stream, const TLV &tlv)
static void addSalt(TLVList &security_parameters, const uint32_t &salt)
virtual dtn::data::Length getLength_mutable() const
DecryptException(std::string what="Decryption failed.")
Definition: SecurityBlock.h:64
const dtn::data::EID getSecurityDestination() const
SecurityBlock(const SecurityBlock::BLOCK_TYPES type, const CIPHERSUITE_IDS id)
dtn::data::Number _ciphersuite_id
ElementMissingException(std::string what="Requested element is missing.")
Definition: SecurityBlock.h:91
static uint32_t getSalt(const TLVList &security_parameters)
bool isSecuritySource(const dtn::data::Bundle &, const dtn::data::EID &) const
static void createSaltAndKey(uint32_t &salt, unsigned char *key, dtn::data::Length key_size)
bool operator==(const TLV &tlv) const
void setSecuritySource(const dtn::data::EID &source)
const std::string getValue() const
static dtn::data::Number createCorrelatorValue(const dtn::data::Bundle &bundle)
virtual dtn::data::Length getLength() const