00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifdef HAVE_CONFIG_H
00033 # include <dtn-config.h>
00034 #endif
00035
00036 #ifdef BSP_ENABLED
00037
00038 #include "Ciphersuite.h"
00039 #include "bundling/Bundle.h"
00040 #include "contacts/Link.h"
00041 #include "security/KeySteward.h"
00042
00043 namespace dtn {
00044
00045
00046
00047 int
00048 KeySteward::encrypt(const Bundle* b,
00049 KeyParameterInfo* kpi,
00050 const LinkRef& link,
00051 std::string security_dest,
00052 u_char* data,
00053 size_t data_len,
00054 DataBuffer& db)
00055 {
00056 (void) b;
00057 (void) kpi;
00058 (void) link;
00059 (void) security_dest;
00060
00061 u_char* buf;
00062 u_int16_t len;
00063 size_t size;
00064
00065 if ( data_len > USHRT_MAX )
00066 return -1;
00067
00068 len = data_len;
00069 len = htons(len);
00070 size = std::max( static_cast<unsigned long>(data_len + sizeof(len)), 512UL );
00071 db.reserve(size);
00072 db.set_len(size);
00073 buf = db.buf();
00074 memcpy(buf, &len, sizeof(len));
00075 buf += sizeof(len);
00076 memcpy(buf, data, data_len);
00077
00078 return 0;
00079 }
00080
00081
00082 int
00083 KeySteward::decrypt(const Bundle* b,
00084 std::string security_src,
00085 u_char* enc_data,
00086 size_t enc_data_len,
00087 DataBuffer& db)
00088 {
00089 (void) b;
00090 (void) security_src;
00091
00092 u_int16_t len;
00093 u_char* buf = enc_data;
00094
00095 if (enc_data_len < 2)
00096 return -1;
00097
00098 memcpy(&len, buf, sizeof(len));
00099 buf += sizeof(len);
00100
00101 len = ntohs(len);
00102 if ( enc_data_len < len + sizeof(len) )
00103 return -1;
00104
00105 db.reserve(len);
00106 db.set_len(len);
00107 memcpy(db.buf(), buf, len);
00108
00109 return 0;
00110 }
00111
00112
00113 int
00114 KeySteward::sign(const Bundle* b,
00115 KeyParameterInfo* kpi,
00116 const LinkRef& link,
00117 u_char* data,
00118 size_t data_len,
00119 DataBuffer& db)
00120 {
00121 (void) b;
00122 (void) kpi;
00123 (void) link;
00124 (void) data;
00125 (void) data_len;
00126 (void) db;
00127
00128 u_char* buf;
00129 u_int16_t len;
00130 size_t size;
00131
00132 if ( data_len > USHRT_MAX )
00133 return -1;
00134
00135 len = data_len;
00136 len = htons(len);
00137 size = std::max( static_cast<unsigned long>(data_len + sizeof(len)), 512UL );
00138 db.reserve(size);
00139 db.set_len(size);
00140 buf = db.buf();
00141 memcpy(buf, &len, sizeof(len));
00142 buf += sizeof(len);
00143 memcpy(buf, data, data_len);
00144
00145 return 0;
00146 }
00147
00148
00149 int
00150 KeySteward::signature_length(const Bundle* b,
00151 KeyParameterInfo* kpi,
00152 const LinkRef& link,
00153 size_t data_len,
00154 size_t& out_len)
00155 {
00156 (void) b;
00157 (void) kpi;
00158 (void) link;
00159 (void) data_len;
00160
00161 out_len = 512;
00162
00163 return 0;
00164 }
00165
00166
00167 int
00168 KeySteward::verify(const Bundle* b,
00169 u_char* enc_data,
00170 size_t enc_data_len,
00171 u_char* data,
00172 size_t data_len)
00173 {
00174 (void) b;
00175 (void) enc_data;
00176 (void) enc_data_len;
00177
00178 u_int16_t len;
00179 u_char* buf = enc_data;
00180
00181 if (enc_data_len < 2)
00182 return -1;
00183
00184 memcpy(&len, buf, sizeof(len));
00185 buf += sizeof(len);
00186
00187 len = ntohs(len);
00188 if ( enc_data_len < len + sizeof(len) )
00189 return -1;
00190
00191 if ( len != data_len )
00192 return -1;
00193
00194 if ( memcmp( buf, data, len ) != 0 )
00195 return -1;
00196
00197 return 0;
00198 }
00199
00200 }
00201
00202 #endif