|
1 /* |
|
2 * Copyright 2007 BBN Technologies Corporation |
|
3 * |
|
4 * Licensed under the Apache License, Version 2.0 (the "License"); you |
|
5 * may not use this file except in compliance with the License. You |
|
6 * may obtain a copy of the License at |
|
7 * |
|
8 * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 * |
|
10 * Unless required by applicable law or agreed to in writing, software |
|
11 * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
|
13 * implied. |
|
14 */ |
|
15 |
|
16 /* |
|
17 * $Id$ |
|
18 */ |
|
19 |
|
20 #ifdef HAVE_CONFIG_H |
|
21 # include <dtn-config.h> |
|
22 #endif |
|
23 |
|
24 #ifdef BSP_ENABLED |
|
25 |
|
26 #include "SPD.h" |
|
27 #include "Ciphersuite.h" |
|
28 #include "Ciphersuite_BA1.h" |
|
29 #include "Ciphersuite_PI2.h" |
|
30 #include "Ciphersuite_PC3.h" |
|
31 |
|
32 namespace dtn { |
|
33 |
|
34 template <> |
|
35 SPD* oasys::Singleton<SPD, false>::instance_ = NULL; |
|
36 |
|
37 static const char * log = "/dtn/bundle/security"; |
|
38 |
|
39 SPD::SPD() |
|
40 : global_policy_inbound_(SPD_USE_NONE), |
|
41 global_policy_outbound_(SPD_USE_NONE) |
|
42 { |
|
43 } |
|
44 |
|
45 SPD::~SPD() |
|
46 { |
|
47 } |
|
48 |
|
49 void |
|
50 SPD::init() |
|
51 { |
|
52 if (instance_ != NULL) |
|
53 { |
|
54 PANIC("SPD already initialized"); |
|
55 } |
|
56 |
|
57 instance_ = new SPD(); |
|
58 log_debug_p(log, "SPD::init() done"); |
|
59 } |
|
60 |
|
61 void |
|
62 SPD::set_global_policy(spd_direction_t direction, spd_policy_t policy) |
|
63 { |
|
64 ASSERT(direction == SPD_DIR_IN || direction == SPD_DIR_OUT); |
|
65 ASSERT((policy & ~(SPD_USE_BAB | SPD_USE_PCB | SPD_USE_PIB)) == 0); |
|
66 if (direction == SPD_DIR_IN) |
|
67 instance()->global_policy_inbound_ = policy; |
|
68 else |
|
69 instance()->global_policy_outbound_ = policy; |
|
70 log_debug_p(log, "SPD::set_global_policy() done"); |
|
71 } |
|
72 |
|
73 void |
|
74 SPD::prepare_out_blocks(const Bundle* bundle, const LinkRef& link, |
|
75 BlockInfoVec* xmit_blocks) |
|
76 { |
|
77 spd_policy_t policy = find_policy(SPD_DIR_OUT, bundle); |
|
78 |
|
79 if (policy & SPD_USE_PIB) { |
|
80 Ciphersuite* bp = |
|
81 Ciphersuite::find_suite(Ciphersuite_PI2::CSNUM_PI2); |
|
82 ASSERT(bp != NULL); |
|
83 bp->prepare(bundle, xmit_blocks, NULL, link, |
|
84 BlockInfo::LIST_NONE); |
|
85 } |
|
86 |
|
87 if (policy & SPD_USE_PCB) { |
|
88 Ciphersuite* bp = |
|
89 Ciphersuite::find_suite(Ciphersuite_PC3::CSNUM_PC3); |
|
90 ASSERT(bp != NULL); |
|
91 bp->prepare(bundle, xmit_blocks, NULL, link, |
|
92 BlockInfo::LIST_NONE); |
|
93 } |
|
94 |
|
95 if (policy & SPD_USE_BAB) { |
|
96 Ciphersuite* bp = |
|
97 Ciphersuite::find_suite(Ciphersuite_BA1::CSNUM_BA1); |
|
98 ASSERT(bp != NULL); |
|
99 bp->prepare(bundle, xmit_blocks, NULL, link, |
|
100 BlockInfo::LIST_NONE); |
|
101 } |
|
102 log_debug_p(log, "SPD::prepare_out_blocks() done"); |
|
103 } |
|
104 |
|
105 bool |
|
106 SPD::verify_in_policy(const Bundle* bundle) |
|
107 { |
|
108 spd_policy_t policy = find_policy(SPD_DIR_IN, bundle); |
|
109 const BlockInfoVec* recv_blocks = &bundle->recv_blocks(); |
|
110 |
|
111 log_debug_p(log, "SPD::verify_in_policy() 0x%x", policy); |
|
112 |
|
113 if (policy & SPD_USE_BAB) { |
|
114 if ( !Ciphersuite::check_validation(bundle, recv_blocks, Ciphersuite_BA1::CSNUM_BA1 )) { |
|
115 log_debug_p(log, "SPD::verify_in_policy() no BP_TAG_BAB_IN_DONE"); |
|
116 return false; |
|
117 } |
|
118 } |
|
119 |
|
120 if (policy & SPD_USE_PCB) { |
|
121 if ( !Ciphersuite::check_validation(bundle, recv_blocks, Ciphersuite_PC3::CSNUM_PC3 )) { |
|
122 log_debug_p(log, "SPD::verify_in_policy() no BP_TAG_PCB_IN_DONE"); |
|
123 return false; |
|
124 } |
|
125 } |
|
126 |
|
127 if (policy & SPD_USE_PIB) { |
|
128 if ( !Ciphersuite::check_validation(bundle, recv_blocks, Ciphersuite_PI2::CSNUM_PI2 )) { |
|
129 log_debug_p(log, "SPD::verify_in_policy() no BP_TAG_PIB_IN_DONE"); |
|
130 return false; |
|
131 } |
|
132 } |
|
133 |
|
134 return true; |
|
135 } |
|
136 |
|
137 SPD::spd_policy_t |
|
138 SPD::find_policy(spd_direction_t direction, const Bundle* bundle) |
|
139 { |
|
140 ASSERT(direction == SPD_DIR_IN || direction == SPD_DIR_OUT); |
|
141 |
|
142 (void)bundle; |
|
143 log_debug_p(log, "SPD::find_policy()"); |
|
144 |
|
145 return (direction == SPD_DIR_IN ? instance()->global_policy_inbound_ |
|
146 : instance()->global_policy_outbound_); |
|
147 } |
|
148 |
|
149 } // namespace dtn |
|
150 |
|
151 #endif /* BSP_ENABLED */ |