blob: 6ac28177877279c05bfb99bd27c4ab9ba18d9789 [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
akmhoqueb8dbba82013-03-11 11:34:17 -0500159char *
160get_orig_router_from_lsa_name(struct ccn_charbuf * content_name)
161{
162 int start=0;
163
164 size_t comp_size;
165 const unsigned char *second_last_comp;
166 char *second_comp_type;
167 char *sep=".";
168 char *rem;
169
170 struct ccn_indexbuf *components=ccn_indexbuf_create();
171 struct ccn_charbuf *name=ccn_charbuf_create();
akmhoqueb7958182013-03-11 12:03:54 -0500172 ccn_name_from_uri(name,nlsr->slice_prefix);
akmhoqueb8dbba82013-03-11 11:34:17 -0500173 ccn_name_split (name, components);
174 start=components->n-2;
175 ccn_charbuf_destroy(&name);
176 ccn_indexbuf_destroy(&components);
177
178 struct ccn_indexbuf *comps=ccn_indexbuf_create();
179 ccn_name_split (content_name, comps);
180 ccn_name_comp_get( content_name->buf, comps,
181 comps->n-1-2, &second_last_comp, &comp_size);
182
183 second_comp_type=strtok_r((char *)second_last_comp, sep, &rem);
184 if ( strcmp( second_comp_type, "lsId" ) == 0 ){
185 ccn_name_chop(content_name,comps,-3);
186 }
187 else{
188 ccn_name_chop(content_name,comps,-2);
189 }
190
191
192 struct ccn_charbuf *temp=ccn_charbuf_create();
193 ccn_name_init(temp);
194 ccn_name_append_components( temp, content_name->buf,
195 comps->buf[start+1],
196 comps->buf[comps->n - 1]);
197
198 struct ccn_charbuf *temp1=ccn_charbuf_create();
199 ccn_uri_append(temp1, temp->buf, temp->length, 0);
200
201 char *orig_router=(char *)calloc(strlen(ccn_charbuf_as_string(temp1))+1,
202 sizeof(char));
203 memcpy(orig_router,ccn_charbuf_as_string(temp1),
204 strlen(ccn_charbuf_as_string(temp1)));
205 orig_router[strlen(orig_router)]='\0';
206
207 ccn_charbuf_destroy(&temp);
208 ccn_charbuf_destroy(&temp1);
209 ccn_indexbuf_destroy(&comps);
210 return orig_router;
211
212
213}
214
215
akmhoqueb7958182013-03-11 12:03:54 -0500216char *
217get_orig_router_from_info_content_name(struct ccn_charbuf * content_name)
218{
219 int start,end;
220
221 start=0;
222
223 struct ccn_indexbuf *comps=ccn_indexbuf_create();
224 ccn_name_split (content_name, comps);
225
226 end=check_for_name_component_in_name(content_name,comps,"nlsr");
227
228
229 struct ccn_charbuf *temp=ccn_charbuf_create();
230 ccn_name_init(temp);
231 ccn_name_append_components( temp, content_name->buf,
232 comps->buf[start],
233 comps->buf[end]);
234
235 struct ccn_charbuf *temp1=ccn_charbuf_create();
236 ccn_uri_append(temp1, temp->buf, temp->length, 0);
237
238 char *orig_router=(char *)calloc(strlen(ccn_charbuf_as_string(temp1))+1,
239 sizeof(char));
240 memcpy(orig_router,ccn_charbuf_as_string(temp1),
241 strlen(ccn_charbuf_as_string(temp1)));
242 orig_router[strlen(orig_router)]='\0';
243
244 ccn_charbuf_destroy(&temp);
245 ccn_charbuf_destroy(&temp1);
246 ccn_indexbuf_destroy(&comps);
247 return orig_router;
248
249
250}
251
akmhoqueb8dbba82013-03-11 11:34:17 -0500252
253int
254check_key_name_hierarchy(const unsigned char *ccnb,
255 struct ccn_parsed_ContentObject *pco,
akmhoqueb7958182013-03-11 12:03:54 -0500256 int key_type, int content_type){
akmhoquee2901222013-03-15 00:59:54 -0500257 if ( nlsr->debugging )
258 printf("check_key_name_hierarchy called\n");
akmhoqueb8dbba82013-03-11 11:34:17 -0500259 if (key_type == UNKNOWN_KEY ){
260 return 1;
261 }
akmhoque5c68a642013-03-15 00:28:16 -0500262 //int res;
akmhoqueb8dbba82013-03-11 11:34:17 -0500263 struct ccn_charbuf *key_name=get_key_name(ccnb, pco);
264
265 struct ccn_charbuf *key_uri = ccn_charbuf_create();
266 ccn_uri_append(key_uri, key_name->buf, key_name->length, 0);
akmhoquee2901222013-03-15 00:59:54 -0500267 if ( nlsr->debugging )
268 printf("Key Name: %s\n",ccn_charbuf_as_string(key_uri));
akmhoqueb8dbba82013-03-11 11:34:17 -0500269 ccn_charbuf_destroy(&key_uri);
270
271 struct ccn_charbuf *content_name=ccn_charbuf_create();
akmhoquea33345f2013-03-15 00:30:03 -0500272 ccn_charbuf_append(content_name, ccnb + pco->offset[CCN_PCO_B_Name],
akmhoqueb8dbba82013-03-11 11:34:17 -0500273 pco->offset[CCN_PCO_E_Name] - pco->offset[CCN_PCO_B_Name]);
274
275 struct ccn_charbuf *content_uri = ccn_charbuf_create();
276 ccn_uri_append(content_uri, content_name->buf, content_name->length, 0);
akmhoquee2901222013-03-15 00:59:54 -0500277 if ( nlsr->debugging )
278 printf("Content Name: %s\n",ccn_charbuf_as_string(content_uri));
akmhoqueb8dbba82013-03-11 11:34:17 -0500279 ccn_charbuf_destroy(&content_uri);
280
281 if ( key_type == NLSR_KEY){
282 char *orig_router_key_name=get_orig_router_from_key_name(key_name,0,0);
akmhoqueb7958182013-03-11 12:03:54 -0500283 char *orig_router_content_name;
284 if ( content_type == 1 ){
285 orig_router_content_name=get_orig_router_from_lsa_name(content_name);
286 }
287 else if ( content_type == 0 ){
288 orig_router_content_name=get_orig_router_from_info_content_name(content_name);
289 }
akmhoquee2901222013-03-15 00:59:54 -0500290 if ( nlsr->debugging ){
291 printf("Orig Router (Key Name):%s\n",orig_router_key_name);
292 printf("Orig Router (Content Name):%s\n",orig_router_content_name);
293 }
akmhoqueb8dbba82013-03-11 11:34:17 -0500294
295 if (strcmp(orig_router_key_name,orig_router_content_name) == 0 ){
296 free(orig_router_key_name);
297 free(orig_router_content_name);
298 ccn_charbuf_destroy(&key_name);
299 ccn_charbuf_destroy(&content_name);
300 return 1;
301 }
302 }
303
304 if ( key_type == ROUTING_KEY){
305 char *orig_router_key_name=get_orig_router_from_key_name(key_name,1,0);
306 char *orig_router_content_name=get_orig_router_from_key_name(content_name,1,1);
akmhoquee2901222013-03-15 00:59:54 -0500307 if ( nlsr->debugging ){
308 printf("Orig Router (Key Name):%s\n",orig_router_key_name);
309 printf("Orig Router (Content Name):%s\n",orig_router_content_name);
310 }
akmhoqueb8dbba82013-03-11 11:34:17 -0500311
312 if (strcmp(orig_router_key_name,orig_router_content_name) == 0 ){
313 free(orig_router_key_name);
314 free(orig_router_content_name);
315 ccn_charbuf_destroy(&key_name);
316 ccn_charbuf_destroy(&content_name);
317 return 1;
318 }
319 }
320 if ( key_type == OPERATOR_KEY){
321 struct ccn_indexbuf *key_name_comps;
322 key_name_comps = ccn_indexbuf_create();
akmhoquea33345f2013-03-15 00:30:03 -0500323 ccn_name_split(key_name, key_name_comps);
akmhoqueb8dbba82013-03-11 11:34:17 -0500324 int last_indx=check_for_tag_component_in_name(key_name,key_name_comps,"O.N.Start");
325 char *site_key_prefix_key=get_name_segments_from_name(key_name,0,last_indx);
akmhoquee2901222013-03-15 00:59:54 -0500326 if ( nlsr->debugging )
327 printf("Site key prefix(key Name):%s\n",site_key_prefix_key);
akmhoqueb8dbba82013-03-11 11:34:17 -0500328 ccn_indexbuf_destroy(&key_name_comps);
329
330 struct ccn_indexbuf *content_name_comps;
331 content_name_comps = ccn_indexbuf_create();
akmhoquea33345f2013-03-15 00:30:03 -0500332 ccn_name_split(content_name, content_name_comps);
akmhoqueb8dbba82013-03-11 11:34:17 -0500333 int last_indx_rtr=check_for_tag_component_in_name(content_name,content_name_comps,"R.N.Start");
334 char *site_key_prefix_content=get_name_segments_from_name(key_name,0,last_indx_rtr);
akmhoquee2901222013-03-15 00:59:54 -0500335 if ( nlsr->debugging )
336 printf("Site key prefix(Content Name):%s\n",site_key_prefix_content);
akmhoqueb8dbba82013-03-11 11:34:17 -0500337 ccn_indexbuf_destroy(&content_name_comps);
338
339 if( strcmp(site_key_prefix_key,site_key_prefix_content) == 0 ){
340 free(site_key_prefix_key);
341 free(site_key_prefix_content);
342 ccn_charbuf_destroy(&key_name);
343 ccn_charbuf_destroy(&content_name);
344 return 1;
345 }
346
347 }
348
349 if ( key_type == SITE_KEY){
350 struct ccn_indexbuf *key_name_comps;
351 key_name_comps = ccn_indexbuf_create();
akmhoquea33345f2013-03-15 00:30:03 -0500352 ccn_name_split(key_name, key_name_comps);
akmhoqueb8dbba82013-03-11 11:34:17 -0500353 int last_indx=check_for_tag_component_in_name(key_name,key_name_comps,"M.K");
354 char *site_key_prefix_key=get_name_segments_from_name(key_name,0,last_indx);
akmhoquee2901222013-03-15 00:59:54 -0500355 if ( nlsr->debugging )
356 printf("Site key prefix(key Name):%s\n",site_key_prefix_key);
akmhoqueb8dbba82013-03-11 11:34:17 -0500357 ccn_indexbuf_destroy(&key_name_comps);
358
359 struct ccn_indexbuf *content_name_comps;
360 content_name_comps = ccn_indexbuf_create();
akmhoquea33345f2013-03-15 00:30:03 -0500361 ccn_name_split(content_name, content_name_comps);
akmhoqueb8dbba82013-03-11 11:34:17 -0500362 int last_indx_rtr=check_for_tag_component_in_name(content_name,content_name_comps,"O.N.Start");
363 char *site_key_prefix_content=get_name_segments_from_name(key_name,0,last_indx_rtr);
akmhoquee2901222013-03-15 00:59:54 -0500364 if ( nlsr->debugging )
365 printf("Site key prefix(Content Name):%s\n",site_key_prefix_content);
akmhoqueb8dbba82013-03-11 11:34:17 -0500366 ccn_indexbuf_destroy(&content_name_comps);
367
368 if( strcmp(site_key_prefix_key,site_key_prefix_content) == 0 ){
369 free(site_key_prefix_key);
370 free(site_key_prefix_content);
371 ccn_charbuf_destroy(&key_name);
372 ccn_charbuf_destroy(&content_name);
373 return 1;
374 }
375
376 }
377
378 if ( key_type == ROOT_KEY){
379 ccn_charbuf_destroy(&key_name);
380 ccn_charbuf_destroy(&content_name);
381 return 1;
382 }
383
384 ccn_charbuf_destroy(&key_name);
385 ccn_charbuf_destroy(&content_name);
386 return 0;
387}
388
akmhoque7c64d802013-03-05 10:18:37 -0600389int
akmhoque6e2ba842013-03-05 19:35:26 -0600390verify_key(const unsigned char *ccnb,
akmhoqueb7958182013-03-11 12:03:54 -0500391 struct ccn_parsed_ContentObject *pco,
392 int content_type){
akmhoque7adb2772013-03-05 16:30:59 -0600393 if ( nlsr->debugging )
394 printf("verify key called\n");
akmhoque7c64d802013-03-05 10:18:37 -0600395 int ret=-1;
akmhoqueb8dbba82013-03-11 11:34:17 -0500396 //int res;
397
akmhoque7c64d802013-03-05 10:18:37 -0600398 if ( contain_key_name(ccnb, pco) == 1){
399
400 struct ccn_charbuf *key_name=get_key_name(ccnb, pco);
401 struct ccn_charbuf *key_uri = ccn_charbuf_create();
402 ccn_uri_append(key_uri, key_name->buf, key_name->length, 0);
akmhoque7adb2772013-03-05 16:30:59 -0600403 if ( nlsr->debugging )
404 printf("Key Name from Incoming Content: %s\n",ccn_charbuf_as_string(key_uri));
akmhoqueb8dbba82013-03-11 11:34:17 -0500405 int key_type=get_key_type_from_key_name(key_name);
akmhoque7adb2772013-03-05 16:30:59 -0600406 if ( nlsr->debugging )
akmhoqueb8dbba82013-03-11 11:34:17 -0500407 printf("Key Type: %d \n",key_type);
akmhoque7c64d802013-03-05 10:18:37 -0600408
409 struct ccn_charbuf *result = ccn_charbuf_create();
410 struct ccn_parsed_ContentObject temp_pco = {0};
411 int get_flags = 0;
412 get_flags |= CCN_GET_NOKEYWAIT;
413 int counter = 0;
414 while(ccn_get(nlsr->ccn, key_name, NULL, 500, result, &temp_pco, NULL,
415 get_flags) < 0 && counter < 3) counter++;
416
417 int chk_verify=ccn_verify_content(nlsr->ccn,ccnb,pco);
418
419 if ( chk_verify == 0 ){
akmhoque7adb2772013-03-05 16:30:59 -0600420 if ( nlsr->debugging )
akmhoqueb7958182013-03-11 12:03:54 -0500421 printf("Content verification Successful :)\n");
akmhoque7c64d802013-03-05 10:18:37 -0600422
423 if ( counter == 3){
akmhoque7adb2772013-03-05 16:30:59 -0600424 if ( nlsr->debugging )
425 printf("Could not retrieve key by name !!!\n");
akmhoque7c64d802013-03-05 10:18:37 -0600426 }
427 else{
akmhoqueb8dbba82013-03-11 11:34:17 -0500428 if ( key_type == ROOT_KEY ){
akmhoque7c64d802013-03-05 10:18:37 -0600429 ret=0;
430 }
431 else{
akmhoqueb8dbba82013-03-11 11:34:17 -0500432 if ( nlsr->isStrictHierchicalKeyCheck ){
akmhoqueb7958182013-03-11 12:03:54 -0500433 int key_name_test=check_key_name_hierarchy(ccnb,
434 pco,
435 key_type,
436 content_type);
akmhoqueb8dbba82013-03-11 11:34:17 -0500437 if ( key_name_test == 1){
akmhoqueb7958182013-03-11 12:03:54 -0500438 ret=verify_key(result->buf,&temp_pco,content_type);
akmhoqueb8dbba82013-03-11 11:34:17 -0500439 }
440 }
441 else{
akmhoqueb7958182013-03-11 12:03:54 -0500442 ret=verify_key(result->buf,&temp_pco,content_type);
akmhoqueb8dbba82013-03-11 11:34:17 -0500443 }
akmhoque7c64d802013-03-05 10:18:37 -0600444 }
445 }
446 }
447 ccn_charbuf_destroy(&result);
448 ccn_charbuf_destroy(&key_uri);
449 ccn_charbuf_destroy(&key_name);
450 return ret;
451 }
452
453 return ret;
454}
455