blob: 81c4a809c47bb9557ab138c1502fd26744289e2c [file] [log] [blame]
akmhoque7c64d802013-03-05 10:18:37 -06001#include<stdio.h>
2#include<string.h>
3#include<stdlib.h>
4#include <unistd.h>
5#ifdef HAVE_CONFIG_H
6#include <config.h>
7#endif
8
9
10#include <ccn/ccn.h>
11#include <ccn/uri.h>
12#include <ccn/keystore.h>
13#include <ccn/signing.h>
14#include <ccn/schedule.h>
15#include <ccn/hashtb.h>
16
17#include "nlsr.h"
18#include "nlsr_km.h"
19#include "nlsr_km_util.h"
20
21int
22sign_content_with_user_defined_keystore(struct ccn_charbuf *content_name,
23 struct ccn_charbuf *resultbuf,
24 const void *data,
25 size_t data_size,
26 char *keystore_path,
27 char *keystore_passphrase,
28 char *key_repo_name,
29 char *site_name,
30 char *router_name){
31
akmhoque7adb2772013-03-05 16:30:59 -060032 if ( nlsr->debugging )
33 printf("sign_content_with_user_defined_keystore called\n");
akmhoque7c64d802013-03-05 10:18:37 -060034
35
36 int res;
37
38
39 struct ccn_charbuf * pubid_out=ccn_charbuf_create();
40 struct ccn_charbuf * keyname;
41
42
43 struct ccn_keystore *keystore = NULL;
44 keystore=ccn_keystore_create();
45 res=ccn_keystore_init(keystore, keystore_path,keystore_passphrase );
46 if ( res < 0 ){
akmhoque7adb2772013-03-05 16:30:59 -060047 if ( nlsr->debugging )
48 printf("Error in initiating keystore :(\n");
akmhoque7c64d802013-03-05 10:18:37 -060049 ccn_keystore_destroy(&keystore);
50 return -1;
51 }
52
53
54 res=ccn_load_private_key (nlsr->ccn,
55 keystore_path,
akmhoque7adb2772013-03-05 16:30:59 -060056 keystore_passphrase,
akmhoque7c64d802013-03-05 10:18:37 -060057 pubid_out);
58
59 if(res < 0 ){
akmhoque7adb2772013-03-05 16:30:59 -060060 if ( nlsr->debugging )
61 printf("Error in loading keystore :( \n");
akmhoque7c64d802013-03-05 10:18:37 -060062 ccn_charbuf_destroy(&pubid_out);
63 return -1;
64 }
65
66 char *baseuri=(char *)calloc(strlen(key_repo_name)+strlen(site_name)+
67 strlen(router_name)+strlen("/%C1.R.N.Start")+5,sizeof(char));
68 memcpy(baseuri,key_repo_name,strlen(key_repo_name)+1);
akmhoque7adb2772013-03-05 16:30:59 -060069 if ( site_name[0] != '/')
70 memcpy(baseuri+strlen(baseuri),"/",1);
akmhoque7c64d802013-03-05 10:18:37 -060071 memcpy(baseuri+strlen(baseuri),site_name,strlen(site_name)+1);
72 memcpy(baseuri+strlen(baseuri),"/%C1.R.N.Start",strlen("/%C1.R.N.Start"));
73 memcpy(baseuri+strlen(baseuri),router_name,strlen(router_name)+1);
74 baseuri[strlen(baseuri)]='\0';
75
76
77 keyname=ccn_charbuf_create();
78 if(keyname == NULL ){
79 ccn_charbuf_destroy(&pubid_out);
80 free(baseuri);
81 return -1;
82 }
83 ccn_name_from_uri(keyname,baseuri);
84 if ( res < 0 ){
akmhoque7adb2772013-03-05 16:30:59 -060085 if ( nlsr->debugging )
86 printf("Bad URI format: %s\n",baseuri);
akmhoque7c64d802013-03-05 10:18:37 -060087 ccn_charbuf_destroy(&pubid_out);
88 ccn_charbuf_destroy(&keyname);
89 free(baseuri);
90 return -1;
91 }
akmhoque3098a312013-03-06 07:31:01 -060092
akmhoque7c64d802013-03-05 10:18:37 -060093 ccn_name_append_str(keyname,"nlsr");
94 struct ccn_charbuf *keyid = ccn_charbuf_create();
95 ccn_charbuf_append_value(keyid, CCN_MARKER_CONTROL, 1);
96 ccn_charbuf_append_string(keyid, ".M.K");
97 ccn_charbuf_append_value(keyid, 0, 1);
98 ccn_charbuf_append_charbuf(keyid, pubid_out);
99 ccn_name_append(keyname, keyid->buf, keyid->length);
100
101
102
103 struct ccn_charbuf *uri = ccn_charbuf_create();
104 ccn_uri_append(uri, keyname->buf, keyname->length, 0);
akmhoque7adb2772013-03-05 16:30:59 -0600105 if ( nlsr->debugging )
106 printf("Key Name Included when processing content: %s\n", ccn_charbuf_as_string(uri));
akmhoque7c64d802013-03-05 10:18:37 -0600107 ccn_charbuf_destroy(&uri);
108
109 struct ccn_signing_params sp = CCN_SIGNING_PARAMS_INIT;
110 sp.type = CCN_CONTENT_DATA;
111 sp.template_ccnb = ccn_charbuf_create();
112 ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_SignedInfo, CCN_DTAG);
113 ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_KeyLocator, CCN_DTAG);
114 ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_KeyName, CCN_DTAG);
115 ccn_charbuf_append(sp.template_ccnb, keyname->buf, keyname->length);
116 ccn_charbuf_append_closer(sp.template_ccnb); // KeyName closer
117 ccn_charbuf_append_closer(sp.template_ccnb); // KeyLocator closer
118 ccn_charbuf_append_closer(sp.template_ccnb); // SignedInfo closer
119
120 sp.sp_flags |= CCN_SP_TEMPL_KEY_LOCATOR;
121 sp.sp_flags |= CCN_SP_FINAL_BLOCK;
122 sp.freshness = 60;
123
124
125 if (pubid_out->length != sizeof(sp.pubid)){
akmhoque7adb2772013-03-05 16:30:59 -0600126 if ( nlsr->debugging )
127 printf("Size of pubid and sp.pubid is not equal");
akmhoque7c64d802013-03-05 10:18:37 -0600128 ccn_charbuf_destroy(&keyname);
129 ccn_charbuf_destroy(&pubid_out);
130 free(baseuri);
131 return -1;
132 }
133
134 memcpy(sp.pubid, pubid_out->buf, pubid_out->length);
135
136
137
138 res=ccn_sign_content(nlsr->ccn,resultbuf,content_name,&sp,data,data_size);
139 if( res < 0 ){
akmhoque7adb2772013-03-05 16:30:59 -0600140 if ( nlsr->debugging )
141 printf("Content signing error \n");
akmhoque7c64d802013-03-05 10:18:37 -0600142 ccn_charbuf_destroy(&sp.template_ccnb);
143 ccn_charbuf_destroy(&keyid);
144 ccn_charbuf_destroy(&keyname);
145 ccn_charbuf_destroy(&pubid_out);
146 free(baseuri);
147 return -1;
148 }
149
150 ccn_charbuf_destroy(&sp.template_ccnb);
151 ccn_charbuf_destroy(&keyid);
152 ccn_charbuf_destroy(&keyname);
153 ccn_charbuf_destroy(&pubid_out);
154 free(baseuri);
155 return 0;
156}
157
158
159/*
160int
161process_incoming_content(struct ccn_closure* selfp,
162 struct ccn_upcall_info* info){
163
164 printf("process_incoming_content called\n");
165
166 int res=verify_key(info->content_ccnb,info->pco->offset[CCN_PCO_E],info->pco);
167
168 if ( res != 0 ){
169 printf("Error in verfiying keys !! :( \n");
170 }
171 else{
172 printf("Key verification is successful :)\n");
173 }
174 return 0;
175}
176*/
177
akmhoqueb8dbba82013-03-11 11:34:17 -0500178char *
179get_orig_router_from_lsa_name(struct ccn_charbuf * content_name)
180{
181 int start=0;
182
183 size_t comp_size;
184 const unsigned char *second_last_comp;
185 char *second_comp_type;
186 char *sep=".";
187 char *rem;
188
189 struct ccn_indexbuf *components=ccn_indexbuf_create();
190 struct ccn_charbuf *name=ccn_charbuf_create();
191 ccn_name_from_uri(name,"/ndn/routing/nlsr/LSA");
192 ccn_name_split (name, components);
193 start=components->n-2;
194 ccn_charbuf_destroy(&name);
195 ccn_indexbuf_destroy(&components);
196
197 struct ccn_indexbuf *comps=ccn_indexbuf_create();
198 ccn_name_split (content_name, comps);
199 ccn_name_comp_get( content_name->buf, comps,
200 comps->n-1-2, &second_last_comp, &comp_size);
201
202 second_comp_type=strtok_r((char *)second_last_comp, sep, &rem);
203 if ( strcmp( second_comp_type, "lsId" ) == 0 ){
204 ccn_name_chop(content_name,comps,-3);
205 }
206 else{
207 ccn_name_chop(content_name,comps,-2);
208 }
209
210
211 struct ccn_charbuf *temp=ccn_charbuf_create();
212 ccn_name_init(temp);
213 ccn_name_append_components( temp, content_name->buf,
214 comps->buf[start+1],
215 comps->buf[comps->n - 1]);
216
217 struct ccn_charbuf *temp1=ccn_charbuf_create();
218 ccn_uri_append(temp1, temp->buf, temp->length, 0);
219
220 char *orig_router=(char *)calloc(strlen(ccn_charbuf_as_string(temp1))+1,
221 sizeof(char));
222 memcpy(orig_router,ccn_charbuf_as_string(temp1),
223 strlen(ccn_charbuf_as_string(temp1)));
224 orig_router[strlen(orig_router)]='\0';
225
226 ccn_charbuf_destroy(&temp);
227 ccn_charbuf_destroy(&temp1);
228 ccn_indexbuf_destroy(&comps);
229 return orig_router;
230
231
232}
233
234
235
236int
237check_key_name_hierarchy(const unsigned char *ccnb,
238 struct ccn_parsed_ContentObject *pco,
239 int key_type){
240 printf("check_key_name_hierarchy called\n");
241 if (key_type == UNKNOWN_KEY ){
242 return 1;
243 }
244 int res;
245 struct ccn_charbuf *key_name=get_key_name(ccnb, pco);
246
247 struct ccn_charbuf *key_uri = ccn_charbuf_create();
248 ccn_uri_append(key_uri, key_name->buf, key_name->length, 0);
249 printf("Key Name: %s\n",ccn_charbuf_as_string(key_uri));
250 ccn_charbuf_destroy(&key_uri);
251
252 struct ccn_charbuf *content_name=ccn_charbuf_create();
253 res=ccn_charbuf_append(content_name, ccnb + pco->offset[CCN_PCO_B_Name],
254 pco->offset[CCN_PCO_E_Name] - pco->offset[CCN_PCO_B_Name]);
255
256 struct ccn_charbuf *content_uri = ccn_charbuf_create();
257 ccn_uri_append(content_uri, content_name->buf, content_name->length, 0);
258 printf("Content Name: %s\n",ccn_charbuf_as_string(content_uri));
259 ccn_charbuf_destroy(&content_uri);
260
261 if ( key_type == NLSR_KEY){
262 char *orig_router_key_name=get_orig_router_from_key_name(key_name,0,0);
263 char *orig_router_content_name=get_orig_router_from_lsa_name(content_name);
264 printf("Orig Router (Key Name):%s\n",orig_router_key_name);
265 printf("Orig Router (Content Name):%s\n",orig_router_content_name);
266
267 if (strcmp(orig_router_key_name,orig_router_content_name) == 0 ){
268 free(orig_router_key_name);
269 free(orig_router_content_name);
270 ccn_charbuf_destroy(&key_name);
271 ccn_charbuf_destroy(&content_name);
272 return 1;
273 }
274 }
275
276 if ( key_type == ROUTING_KEY){
277 char *orig_router_key_name=get_orig_router_from_key_name(key_name,1,0);
278 char *orig_router_content_name=get_orig_router_from_key_name(content_name,1,1);
279 printf("Orig Router (Key Name):%s\n",orig_router_key_name);
280 printf("Orig Router (Content Name):%s\n",orig_router_content_name);
281
282 if (strcmp(orig_router_key_name,orig_router_content_name) == 0 ){
283 free(orig_router_key_name);
284 free(orig_router_content_name);
285 ccn_charbuf_destroy(&key_name);
286 ccn_charbuf_destroy(&content_name);
287 return 1;
288 }
289 }
290 if ( key_type == OPERATOR_KEY){
291 struct ccn_indexbuf *key_name_comps;
292 key_name_comps = ccn_indexbuf_create();
293 res = ccn_name_split(key_name, key_name_comps);
294 int last_indx=check_for_tag_component_in_name(key_name,key_name_comps,"O.N.Start");
295 char *site_key_prefix_key=get_name_segments_from_name(key_name,0,last_indx);
296 printf("Site key prefix(key Name):%s\n",site_key_prefix_key);
297 ccn_indexbuf_destroy(&key_name_comps);
298
299 struct ccn_indexbuf *content_name_comps;
300 content_name_comps = ccn_indexbuf_create();
301 res = ccn_name_split(content_name, content_name_comps);
302 int last_indx_rtr=check_for_tag_component_in_name(content_name,content_name_comps,"R.N.Start");
303 char *site_key_prefix_content=get_name_segments_from_name(key_name,0,last_indx_rtr);
304 printf("Site key prefix(Content Name):%s\n",site_key_prefix_content);
305 ccn_indexbuf_destroy(&content_name_comps);
306
307 if( strcmp(site_key_prefix_key,site_key_prefix_content) == 0 ){
308 free(site_key_prefix_key);
309 free(site_key_prefix_content);
310 ccn_charbuf_destroy(&key_name);
311 ccn_charbuf_destroy(&content_name);
312 return 1;
313 }
314
315 }
316
317 if ( key_type == SITE_KEY){
318 struct ccn_indexbuf *key_name_comps;
319 key_name_comps = ccn_indexbuf_create();
320 res = ccn_name_split(key_name, key_name_comps);
321 int last_indx=check_for_tag_component_in_name(key_name,key_name_comps,"M.K");
322 char *site_key_prefix_key=get_name_segments_from_name(key_name,0,last_indx);
323 printf("Site key prefix(key Name):%s\n",site_key_prefix_key);
324 ccn_indexbuf_destroy(&key_name_comps);
325
326 struct ccn_indexbuf *content_name_comps;
327 content_name_comps = ccn_indexbuf_create();
328 res = ccn_name_split(content_name, content_name_comps);
329 int last_indx_rtr=check_for_tag_component_in_name(content_name,content_name_comps,"O.N.Start");
330 char *site_key_prefix_content=get_name_segments_from_name(key_name,0,last_indx_rtr);
331 printf("Site key prefix(Content Name):%s\n",site_key_prefix_content);
332 ccn_indexbuf_destroy(&content_name_comps);
333
334 if( strcmp(site_key_prefix_key,site_key_prefix_content) == 0 ){
335 free(site_key_prefix_key);
336 free(site_key_prefix_content);
337 ccn_charbuf_destroy(&key_name);
338 ccn_charbuf_destroy(&content_name);
339 return 1;
340 }
341
342 }
343
344 if ( key_type == ROOT_KEY){
345 ccn_charbuf_destroy(&key_name);
346 ccn_charbuf_destroy(&content_name);
347 return 1;
348 }
349
350 ccn_charbuf_destroy(&key_name);
351 ccn_charbuf_destroy(&content_name);
352 return 0;
353}
354
akmhoque7c64d802013-03-05 10:18:37 -0600355int
akmhoque6e2ba842013-03-05 19:35:26 -0600356verify_key(const unsigned char *ccnb,
akmhoque7c64d802013-03-05 10:18:37 -0600357 struct ccn_parsed_ContentObject *pco){
akmhoque7adb2772013-03-05 16:30:59 -0600358 if ( nlsr->debugging )
359 printf("verify key called\n");
akmhoque7c64d802013-03-05 10:18:37 -0600360 int ret=-1;
akmhoqueb8dbba82013-03-11 11:34:17 -0500361 //int res;
362
akmhoque7c64d802013-03-05 10:18:37 -0600363 if ( contain_key_name(ccnb, pco) == 1){
364
365 struct ccn_charbuf *key_name=get_key_name(ccnb, pco);
366 struct ccn_charbuf *key_uri = ccn_charbuf_create();
367 ccn_uri_append(key_uri, key_name->buf, key_name->length, 0);
akmhoque7adb2772013-03-05 16:30:59 -0600368 if ( nlsr->debugging )
369 printf("Key Name from Incoming Content: %s\n",ccn_charbuf_as_string(key_uri));
akmhoqueb8dbba82013-03-11 11:34:17 -0500370 int key_type=get_key_type_from_key_name(key_name);
akmhoque7adb2772013-03-05 16:30:59 -0600371 if ( nlsr->debugging )
akmhoqueb8dbba82013-03-11 11:34:17 -0500372 printf("Key Type: %d \n",key_type);
akmhoque7c64d802013-03-05 10:18:37 -0600373
374 struct ccn_charbuf *result = ccn_charbuf_create();
375 struct ccn_parsed_ContentObject temp_pco = {0};
376 int get_flags = 0;
377 get_flags |= CCN_GET_NOKEYWAIT;
378 int counter = 0;
379 while(ccn_get(nlsr->ccn, key_name, NULL, 500, result, &temp_pco, NULL,
380 get_flags) < 0 && counter < 3) counter++;
381
382 int chk_verify=ccn_verify_content(nlsr->ccn,ccnb,pco);
383
384 if ( chk_verify == 0 ){
akmhoque7adb2772013-03-05 16:30:59 -0600385 if ( nlsr->debugging )
386 printf("Verification Successful :)\n");
akmhoque7c64d802013-03-05 10:18:37 -0600387
388 if ( counter == 3){
akmhoque7adb2772013-03-05 16:30:59 -0600389 if ( nlsr->debugging )
390 printf("Could not retrieve key by name !!!\n");
akmhoque7c64d802013-03-05 10:18:37 -0600391 }
392 else{
akmhoqueb8dbba82013-03-11 11:34:17 -0500393 if ( key_type == ROOT_KEY ){
akmhoque7c64d802013-03-05 10:18:37 -0600394 ret=0;
395 }
396 else{
akmhoqueb8dbba82013-03-11 11:34:17 -0500397 //ret=verify_key(result->buf,&temp_pco);
398 if ( nlsr->isStrictHierchicalKeyCheck ){
399 int key_name_test=check_key_name_hierarchy(ccnb,pco,key_type);
400 if ( key_name_test == 1){
401 ret=verify_key(result->buf,&temp_pco);
402 }
403 }
404 else{
405 ret=verify_key(result->buf,&temp_pco);
406 }
akmhoque7c64d802013-03-05 10:18:37 -0600407 }
408 }
409 }
410 ccn_charbuf_destroy(&result);
411 ccn_charbuf_destroy(&key_uri);
412 ccn_charbuf_destroy(&key_name);
413 return ret;
414 }
415
416 return ret;
417}
418