00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef _CIPHERSUITE_H_
00018 #define _CIPHERSUITE_H_
00019
00020 #ifdef BSP_ENABLED
00021
00022 #include "bundling/BlockProcessor.h"
00023
00024 namespace dtn {
00025
00026 class BP_Local_CS;
00027
00033 class Ciphersuite {
00034 public:
00037 typedef oasys::ScratchBuffer<u_char*, 16> LocalBuffer;
00038
00040 typedef BlockInfo::list_owner_t list_owner_t;
00041 typedef BundleProtocol::status_report_reason_t status_report_reason_t;
00043
00048 typedef enum {
00049 CS_BLOCK_HAS_SOURCE = 0x10,
00050 CS_BLOCK_HAS_DEST = 0x08,
00051 CS_BLOCK_HAS_PARAMS = 0x04,
00052 CS_BLOCK_HAS_CORRELATOR = 0x02,
00053 CS_BLOCK_HAS_RESULT = 0x01
00054 } ciphersuite_flags_t;
00055
00060 typedef enum {
00061 CS_BLOCK_RESERVED0 = 0x01,
00062 CS_BLOCK_PROCESSED = 0x02,
00063 CS_BLOCK_DID_NOT_FAIL = 0x04,
00064 CS_BLOCK_FAILED_VALIDATION = 0x08,
00065 CS_BLOCK_PASSED_VALIDATION = 0x10,
00066 CS_BLOCK_COMPLETED_DO_NOT_FORWARD = 0x20,
00067 CS_BLOCK_PROCESSING_FAILED_DO_NOT_SEND = 0x40
00068 } proc_flags_t;
00069
00074 typedef enum {
00075 CS_reserved0 = 0,
00076 CS_IV_field = 1,
00077 CS_key_ID_field = 2,
00078 CS_encoded_key_field = 3,
00079 CS_fragment_offset_and_length_field = 4,
00080 CS_signature_field = 5,
00081 CS_reserved6 = 6,
00082 CS_C_block_salt = 7,
00083 CS_C_block_ICV_field = 8,
00084 CS_reserved9 = 9,
00085 CS_encap_block_field = 10,
00086 CS_reserved11 = 11
00087 } ciphersuite_fields_t;
00088
00090 Ciphersuite();
00091
00095 virtual ~Ciphersuite();
00096
00097 static void register_ciphersuite(Ciphersuite* cs);
00098
00099 static Ciphersuite* find_suite(u_int16_t num);
00100
00101 static void init_default_ciphersuites(void);
00102
00103 virtual u_int16_t cs_num();
00104
00105 virtual size_t result_len() { return 0; }
00106
00107 static void parse(BlockInfo* block);
00108
00125 virtual int consume(Bundle* bundle, BlockInfo* block,
00126 u_char* buf, size_t len) = 0;
00127
00128 virtual int reload_post_process(Bundle* bundle,
00129 BlockInfoVec* block_list,
00130 BlockInfo* block);
00131
00138 virtual bool validate(const Bundle* bundle,
00139 BlockInfoVec* block_list,
00140 BlockInfo* block,
00141 status_report_reason_t* reception_reason,
00142 status_report_reason_t* deletion_reason) = 0;
00143
00152 virtual int prepare(const Bundle* bundle,
00153 BlockInfoVec* xmit_blocks,
00154 const BlockInfo* source,
00155 const LinkRef& link,
00156 list_owner_t list) = 0;
00157
00166 virtual int generate(const Bundle* bundle,
00167 BlockInfoVec* xmit_blocks,
00168 BlockInfo* block,
00169 const LinkRef& link,
00170 bool last) = 0;
00171
00184 virtual int finalize(const Bundle* bundle,
00185 BlockInfoVec* xmit_blocks,
00186 BlockInfo* block,
00187 const LinkRef& link) = 0;
00188
00193 static bool check_validation(const Bundle* bundle,
00194 const BlockInfoVec* block_list,
00195 u_int16_t num);
00196
00201 static u_int64_t create_correlator(const Bundle* bundle,
00202 const BlockInfoVec* block_list);
00203
00208 static bool source_is_local_node(const Bundle* bundle,
00209 const BlockInfo* block);
00210
00211 static bool destination_is_local_node(const Bundle* bundle,
00212 const BlockInfo* block);
00213
00214
00215 virtual void init_locals(BlockInfo* block);
00216
00217 protected:
00218
00223 void generate_preamble(BlockInfoVec* xmit_blocks,
00224 BlockInfo* block,
00225 u_int8_t type,
00226 u_int64_t flags,
00227 u_int64_t data_length);
00228
00229
00230 private:
00231
00236 static Ciphersuite* ciphersuites_[1024];
00237
00238 static bool inited;
00239 };
00240
00241 class BP_Local_CS : public BP_Local {
00242 public:
00244 typedef Ciphersuite::LocalBuffer LocalBuffer;
00245
00249 BP_Local_CS();
00250
00254 BP_Local_CS(const BP_Local_CS&);
00255
00259 virtual ~BP_Local_CS();
00260
00262
00263
00264
00265 u_int16_t cs_flags() const { return cs_flags_; }
00266 u_int16_t owner_cs_num() const { return owner_cs_num_; }
00267 u_int32_t security_result_offset() const { return security_result_offset_; }
00268 u_int64_t correlator() const { return correlator_; }
00269 u_int16_t correlator_sequence() const { return correlator_sequence_; }
00270 const LocalBuffer& key() const { return key_; }
00271 const LocalBuffer& salt() const { return salt_; }
00272 const LocalBuffer& iv() const { return iv_; }
00273 const LocalBuffer& security_params() const { return security_params_; }
00274 std::string security_src() const { return security_src_; }
00275 std::string security_dest() const { return security_dest_; }
00276 const LocalBuffer& security_result() const { return security_result_; }
00277 BlockInfo::list_owner_t list_owner() const { return list_owner_; }
00278 u_int16_t proc_flags() const { return proc_flags_; }
00279 bool proc_flag(u_int16_t f) const { return (proc_flags_ & f) != 0; }
00281
00282
00284 void set_cs_flags(u_int16_t f) { cs_flags_ = f; }
00285 void set_owner_cs_num(u_int16_t n) { owner_cs_num_ = n; }
00286 void set_security_result_offset(u_int64_t o){ security_result_offset_ = o; }
00287 void set_key(u_char* k, size_t len);
00288 void set_salt(u_char* s, size_t len);
00289 void set_iv(u_char* iv, size_t len);
00290 void set_correlator(u_int64_t c) { correlator_ = c; }
00291 void set_correlator_sequence(u_int16_t c) { correlator_sequence_ = c; }
00292 LocalBuffer* writable_security_params() { return &security_params_; }
00293 void set_security_src(std::string s) { security_src_ = s; }
00294 void set_security_dest(std::string d) { security_dest_ = d; }
00295 LocalBuffer* writable_security_result() { return &security_result_; }
00296 void set_list_owner(BlockInfo::list_owner_t o) { list_owner_ = o; }
00297 void set_proc_flags(u_int16_t f) { proc_flags_ = f; }
00298 void set_proc_flag(u_int16_t f) { proc_flags_ |= f; }
00300
00301
00302 protected:
00303
00304 u_int16_t cs_flags_;
00305 u_int16_t correlator_sequence_;
00306 u_int32_t security_result_offset_;
00307 u_int64_t correlator_;
00308 LocalBuffer key_;
00309 LocalBuffer iv_;
00310 LocalBuffer salt_;
00311 LocalBuffer security_params_;
00312 std::string security_src_;
00313 std::string security_dest_;
00314 LocalBuffer security_result_;
00315 BlockInfo::list_owner_t list_owner_;
00316 u_int16_t owner_cs_num_;
00317 u_int16_t proc_flags_;
00318
00319 };
00320
00321 }
00322
00323 #define CS_FAIL_IF(x) \
00324 do { if ( (x) ) { \
00325 log_err_p(log, "TEST FAILED (%s) at %s:%d\n", \
00326 (#x), __FILE__, __LINE__); \
00327 goto fail; \
00328 } } while(0);
00329
00330 #define CS_FAIL_IF_NULL(x) \
00331 do { if ( (x) == NULL) { \
00332 log_err_p(log, "TEST FAILED (%s == NULL) at %s:%d\n", \
00333 (#x), __FILE__, __LINE__); \
00334 goto fail; \
00335 } } while(0);
00336
00337 #endif
00338
00339 #endif