IBR-DTN  1.0.0
SecurityFilter.cpp
Go to the documentation of this file.
1 /*
2  * SecurityFilter.cpp
3  *
4  * Copyright (C) 2014 IBR, TU Braunschweig
5  *
6  * Written-by: Johannes Morgenroth <jm@m-network.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 #include <ibrdtn/ibrdtn.h>
23 #include "SecurityFilter.h"
24 
25 #ifdef IBRDTN_SUPPORT_BSP
30 #endif
31 
32 namespace dtn
33 {
34  namespace core
35  {
37  : _mode(m), _positive_action(positive), _negative_action(negative)
38  {
39  }
40 
42  {
43  }
44 
46  {
47 #ifdef IBRDTN_SUPPORT_BSP
48  switch (_mode)
49  {
50  default:
51  break;
52 
53  case VERIFY_AUTH:
54  {
55  try {
56  // extract bundle from context
57  const dtn::data::Bundle &bundle = context.getBundle();
58 
59  // check if at least one BAB is present
60  if (std::count(bundle.begin(), bundle.end(), dtn::security::BundleAuthenticationBlock::BLOCK_TYPE) > 0)
61  {
62  if (_positive_action != BundleFilter::PASS) return _positive_action;
63  }
64  else
65  {
66  if (_negative_action != BundleFilter::PASS) return _negative_action;
67  }
68  } catch (const FilterException&) {
69  // necessary bundle object is not present - abort the chain
70  return BundleFilter::PASS;
71  }
72  break;
73  }
74 
75  case VERIFY_INTEGRITY:
76  {
77  try {
78  // extract bundle from context
79  const dtn::data::Bundle &bundle = context.getBundle();
80 
81  // check if at least one PIB is present
82  if (std::count(bundle.begin(), bundle.end(), dtn::security::PayloadIntegrityBlock::BLOCK_TYPE) > 0)
83  {
84  if (_positive_action != BundleFilter::PASS) return _positive_action;
85  }
86  else
87  {
88  if (_negative_action != BundleFilter::PASS) return _negative_action;
89  }
90  } catch (const FilterException&) {
91  // necessary bundle object is not present - abort the chain
92  return BundleFilter::PASS;
93  }
94  break;
95  }
96 
97  case VERIFY_CONFIDENTIALITY:
98  {
99  try {
100  // extract bundle from context
101  const dtn::data::Bundle &bundle = context.getBundle();
102 
103  // check if at least one PCB is present
104  if (std::count(bundle.begin(), bundle.end(), dtn::security::PayloadConfidentialBlock::BLOCK_TYPE) > 0)
105  {
106  if (_positive_action != BundleFilter::PASS) return _positive_action;
107  }
108  else
109  {
110  if (_negative_action != BundleFilter::PASS) return _negative_action;
111  }
112  } catch (const FilterException&) {
113  // necessary bundle object is not present - abort the chain
114  return BundleFilter::PASS;
115  }
116  break;
117  }
118  }
119 #endif
120 
121  // forward call to the next filter or return with the default action
122  return BundleFilter::evaluate(context);
123  }
124 
126  {
127 #ifdef IBRDTN_SUPPORT_BSP
128  switch (_mode)
129  {
130  default:
131  break;
132 
133  case VERIFY_AUTH:
134  {
135  try {
136  // do verify and strip blocks
138  if (_positive_action != BundleFilter::PASS) return _positive_action;
140  // necessary bundle object is not present - abort the chain
141  if (_negative_action != BundleFilter::PASS) return _negative_action;
142  }
143  break;
144  }
145 
146  case APPLY_AUTH:
147  {
148  try {
149  // apply authentication
151  if (_positive_action != BundleFilter::PASS) return _positive_action;
153  if (_negative_action != BundleFilter::PASS) return _negative_action;
154  }
155  break;
156  }
157 
158  case VERIFY_INTEGRITY:
159  {
160  try {
161  // do verify and strip blocks
163  if (_positive_action != BundleFilter::PASS) return _positive_action;
165  // necessary bundle object is not present - abort the chain
166  if (_negative_action != BundleFilter::PASS) return _negative_action;
167  }
168  break;
169  }
170  }
171 #endif
172 
173  // forward call to the next filter or return with the default action
174  return BundleFilter::filter(context, bundle);
175  }
176  } /* namespace core */
177 } /* namespace dtn */
virtual ACTION evaluate(const FilterContext &) const
virtual ACTION filter(const FilterContext &, dtn::data::Bundle &) const
void verifyIntegrity(dtn::data::Bundle &bundle) const
SecurityFilter(MODE mode, BundleFilter::ACTION positive=BundleFilter::PASS, BundleFilter::ACTION negative=BundleFilter::PASS)
void auth(dtn::data::Bundle &bundle) const
virtual ACTION evaluate(const FilterContext &context) const
virtual ACTION filter(const FilterContext &context, dtn::data::Bundle &bundle) const
iterator begin()
Definition: Bundle.cpp:49
static const dtn::data::block_t BLOCK_TYPE
static SecurityManager & getInstance()
void verifyAuthentication(dtn::data::Bundle &bundle) const
static const dtn::data::block_t BLOCK_TYPE
iterator end()
Definition: Bundle.cpp:54