blob: 32d610274ebdcc9d99e69803dcc23283ece2beb0 [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
akmhoque284056b2013-03-15 01:20:16 -0500150 ccn_keystore_destroy(&keystore);
akmhoque7c64d802013-03-05 10:18:37 -0600151 ccn_charbuf_destroy(&sp.template_ccnb);
152 ccn_charbuf_destroy(&keyid);
153 ccn_charbuf_destroy(&keyname);
154 ccn_charbuf_destroy(&pubid_out);
akmhoque284056b2013-03-15 01:20:16 -0500155
akmhoque7c64d802013-03-05 10:18:37 -0600156 free(baseuri);
157 return 0;
158}
159
160
akmhoqueb8dbba82013-03-11 11:34:17 -0500161char *
162get_orig_router_from_lsa_name(struct ccn_charbuf * content_name)
163{
164 int start=0;
165
166 size_t comp_size;
167 const unsigned char *second_last_comp;
168 char *second_comp_type;
169 char *sep=".";
170 char *rem;
171
172 struct ccn_indexbuf *components=ccn_indexbuf_create();
173 struct ccn_charbuf *name=ccn_charbuf_create();
akmhoqueb7958182013-03-11 12:03:54 -0500174 ccn_name_from_uri(name,nlsr->slice_prefix);
akmhoqueb8dbba82013-03-11 11:34:17 -0500175 ccn_name_split (name, components);
176 start=components->n-2;
177 ccn_charbuf_destroy(&name);
178 ccn_indexbuf_destroy(&components);
179
180 struct ccn_indexbuf *comps=ccn_indexbuf_create();
181 ccn_name_split (content_name, comps);
182 ccn_name_comp_get( content_name->buf, comps,
183 comps->n-1-2, &second_last_comp, &comp_size);
184
185 second_comp_type=strtok_r((char *)second_last_comp, sep, &rem);
186 if ( strcmp( second_comp_type, "lsId" ) == 0 ){
187 ccn_name_chop(content_name,comps,-3);
188 }
189 else{
190 ccn_name_chop(content_name,comps,-2);
191 }
192
193
194 struct ccn_charbuf *temp=ccn_charbuf_create();
195 ccn_name_init(temp);
196 ccn_name_append_components( temp, content_name->buf,
197 comps->buf[start+1],
198 comps->buf[comps->n - 1]);
199
200 struct ccn_charbuf *temp1=ccn_charbuf_create();
201 ccn_uri_append(temp1, temp->buf, temp->length, 0);
202
203 char *orig_router=(char *)calloc(strlen(ccn_charbuf_as_string(temp1))+1,
204 sizeof(char));
205 memcpy(orig_router,ccn_charbuf_as_string(temp1),
206 strlen(ccn_charbuf_as_string(temp1)));
207 orig_router[strlen(orig_router)]='\0';
208
209 ccn_charbuf_destroy(&temp);
210 ccn_charbuf_destroy(&temp1);
211 ccn_indexbuf_destroy(&comps);
212 return orig_router;
213
214
215}
216
217
akmhoqueb7958182013-03-11 12:03:54 -0500218char *
219get_orig_router_from_info_content_name(struct ccn_charbuf * content_name)
220{
221 int start,end;
222
223 start=0;
224
225 struct ccn_indexbuf *comps=ccn_indexbuf_create();
226 ccn_name_split (content_name, comps);
227
228 end=check_for_name_component_in_name(content_name,comps,"nlsr");
229
230
231 struct ccn_charbuf *temp=ccn_charbuf_create();
232 ccn_name_init(temp);
233 ccn_name_append_components( temp, content_name->buf,
234 comps->buf[start],
235 comps->buf[end]);
236
237 struct ccn_charbuf *temp1=ccn_charbuf_create();
238 ccn_uri_append(temp1, temp->buf, temp->length, 0);
239
240 char *orig_router=(char *)calloc(strlen(ccn_charbuf_as_string(temp1))+1,
241 sizeof(char));
242 memcpy(orig_router,ccn_charbuf_as_string(temp1),
243 strlen(ccn_charbuf_as_string(temp1)));
244 orig_router[strlen(orig_router)]='\0';
245
246 ccn_charbuf_destroy(&temp);
247 ccn_charbuf_destroy(&temp1);
248 ccn_indexbuf_destroy(&comps);
249 return orig_router;
250
251
252}
253
akmhoqueb8dbba82013-03-11 11:34:17 -0500254
255int
256check_key_name_hierarchy(const unsigned char *ccnb,
257 struct ccn_parsed_ContentObject *pco,
akmhoqueb7958182013-03-11 12:03:54 -0500258 int key_type, int content_type){
akmhoquee2901222013-03-15 00:59:54 -0500259 if ( nlsr->debugging )
260 printf("check_key_name_hierarchy called\n");
akmhoqueb8dbba82013-03-11 11:34:17 -0500261 if (key_type == UNKNOWN_KEY ){
262 return 1;
263 }
akmhoque5c68a642013-03-15 00:28:16 -0500264 //int res;
akmhoqueb8dbba82013-03-11 11:34:17 -0500265 struct ccn_charbuf *key_name=get_key_name(ccnb, pco);
266
267 struct ccn_charbuf *key_uri = ccn_charbuf_create();
268 ccn_uri_append(key_uri, key_name->buf, key_name->length, 0);
akmhoquee2901222013-03-15 00:59:54 -0500269 if ( nlsr->debugging )
270 printf("Key Name: %s\n",ccn_charbuf_as_string(key_uri));
akmhoqueb8dbba82013-03-11 11:34:17 -0500271 ccn_charbuf_destroy(&key_uri);
272
273 struct ccn_charbuf *content_name=ccn_charbuf_create();
akmhoquea33345f2013-03-15 00:30:03 -0500274 ccn_charbuf_append(content_name, ccnb + pco->offset[CCN_PCO_B_Name],
akmhoqueb8dbba82013-03-11 11:34:17 -0500275 pco->offset[CCN_PCO_E_Name] - pco->offset[CCN_PCO_B_Name]);
276
277 struct ccn_charbuf *content_uri = ccn_charbuf_create();
278 ccn_uri_append(content_uri, content_name->buf, content_name->length, 0);
akmhoquee2901222013-03-15 00:59:54 -0500279 if ( nlsr->debugging )
280 printf("Content Name: %s\n",ccn_charbuf_as_string(content_uri));
akmhoqueb8dbba82013-03-11 11:34:17 -0500281 ccn_charbuf_destroy(&content_uri);
282
283 if ( key_type == NLSR_KEY){
284 char *orig_router_key_name=get_orig_router_from_key_name(key_name,0,0);
akmhoqueb7958182013-03-11 12:03:54 -0500285 char *orig_router_content_name;
286 if ( content_type == 1 ){
287 orig_router_content_name=get_orig_router_from_lsa_name(content_name);
288 }
289 else if ( content_type == 0 ){
290 orig_router_content_name=get_orig_router_from_info_content_name(content_name);
291 }
akmhoquee2901222013-03-15 00:59:54 -0500292 if ( nlsr->debugging ){
293 printf("Orig Router (Key Name):%s\n",orig_router_key_name);
294 printf("Orig Router (Content Name):%s\n",orig_router_content_name);
295 }
akmhoqueb8dbba82013-03-11 11:34:17 -0500296
297 if (strcmp(orig_router_key_name,orig_router_content_name) == 0 ){
298 free(orig_router_key_name);
299 free(orig_router_content_name);
300 ccn_charbuf_destroy(&key_name);
301 ccn_charbuf_destroy(&content_name);
302 return 1;
303 }
304 }
305
306 if ( key_type == ROUTING_KEY){
307 char *orig_router_key_name=get_orig_router_from_key_name(key_name,1,0);
308 char *orig_router_content_name=get_orig_router_from_key_name(content_name,1,1);
akmhoquee2901222013-03-15 00:59:54 -0500309 if ( nlsr->debugging ){
310 printf("Orig Router (Key Name):%s\n",orig_router_key_name);
311 printf("Orig Router (Content Name):%s\n",orig_router_content_name);
312 }
akmhoqueb8dbba82013-03-11 11:34:17 -0500313
314 if (strcmp(orig_router_key_name,orig_router_content_name) == 0 ){
315 free(orig_router_key_name);
316 free(orig_router_content_name);
317 ccn_charbuf_destroy(&key_name);
318 ccn_charbuf_destroy(&content_name);
319 return 1;
320 }
321 }
322 if ( key_type == OPERATOR_KEY){
323 struct ccn_indexbuf *key_name_comps;
324 key_name_comps = ccn_indexbuf_create();
akmhoquea33345f2013-03-15 00:30:03 -0500325 ccn_name_split(key_name, key_name_comps);
akmhoqueb8dbba82013-03-11 11:34:17 -0500326 int last_indx=check_for_tag_component_in_name(key_name,key_name_comps,"O.N.Start");
327 char *site_key_prefix_key=get_name_segments_from_name(key_name,0,last_indx);
akmhoquee2901222013-03-15 00:59:54 -0500328 if ( nlsr->debugging )
329 printf("Site key prefix(key Name):%s\n",site_key_prefix_key);
akmhoqueb8dbba82013-03-11 11:34:17 -0500330 ccn_indexbuf_destroy(&key_name_comps);
331
332 struct ccn_indexbuf *content_name_comps;
333 content_name_comps = ccn_indexbuf_create();
akmhoquea33345f2013-03-15 00:30:03 -0500334 ccn_name_split(content_name, content_name_comps);
akmhoqueb8dbba82013-03-11 11:34:17 -0500335 int last_indx_rtr=check_for_tag_component_in_name(content_name,content_name_comps,"R.N.Start");
336 char *site_key_prefix_content=get_name_segments_from_name(key_name,0,last_indx_rtr);
akmhoquee2901222013-03-15 00:59:54 -0500337 if ( nlsr->debugging )
338 printf("Site key prefix(Content Name):%s\n",site_key_prefix_content);
akmhoqueb8dbba82013-03-11 11:34:17 -0500339 ccn_indexbuf_destroy(&content_name_comps);
340
341 if( strcmp(site_key_prefix_key,site_key_prefix_content) == 0 ){
342 free(site_key_prefix_key);
343 free(site_key_prefix_content);
344 ccn_charbuf_destroy(&key_name);
345 ccn_charbuf_destroy(&content_name);
346 return 1;
347 }
348
349 }
350
351 if ( key_type == SITE_KEY){
352 struct ccn_indexbuf *key_name_comps;
353 key_name_comps = ccn_indexbuf_create();
akmhoquea33345f2013-03-15 00:30:03 -0500354 ccn_name_split(key_name, key_name_comps);
akmhoqueb8dbba82013-03-11 11:34:17 -0500355 int last_indx=check_for_tag_component_in_name(key_name,key_name_comps,"M.K");
356 char *site_key_prefix_key=get_name_segments_from_name(key_name,0,last_indx);
akmhoquee2901222013-03-15 00:59:54 -0500357 if ( nlsr->debugging )
358 printf("Site key prefix(key Name):%s\n",site_key_prefix_key);
akmhoqueb8dbba82013-03-11 11:34:17 -0500359 ccn_indexbuf_destroy(&key_name_comps);
360
361 struct ccn_indexbuf *content_name_comps;
362 content_name_comps = ccn_indexbuf_create();
akmhoquea33345f2013-03-15 00:30:03 -0500363 ccn_name_split(content_name, content_name_comps);
akmhoqueb8dbba82013-03-11 11:34:17 -0500364 int last_indx_rtr=check_for_tag_component_in_name(content_name,content_name_comps,"O.N.Start");
365 char *site_key_prefix_content=get_name_segments_from_name(key_name,0,last_indx_rtr);
akmhoquee2901222013-03-15 00:59:54 -0500366 if ( nlsr->debugging )
367 printf("Site key prefix(Content Name):%s\n",site_key_prefix_content);
akmhoqueb8dbba82013-03-11 11:34:17 -0500368 ccn_indexbuf_destroy(&content_name_comps);
369
370 if( strcmp(site_key_prefix_key,site_key_prefix_content) == 0 ){
371 free(site_key_prefix_key);
372 free(site_key_prefix_content);
373 ccn_charbuf_destroy(&key_name);
374 ccn_charbuf_destroy(&content_name);
375 return 1;
376 }
377
378 }
379
380 if ( key_type == ROOT_KEY){
381 ccn_charbuf_destroy(&key_name);
382 ccn_charbuf_destroy(&content_name);
383 return 1;
384 }
385
386 ccn_charbuf_destroy(&key_name);
387 ccn_charbuf_destroy(&content_name);
388 return 0;
389}
390
akmhoque7c64d802013-03-05 10:18:37 -0600391int
akmhoque6e2ba842013-03-05 19:35:26 -0600392verify_key(const unsigned char *ccnb,
akmhoqueb7958182013-03-11 12:03:54 -0500393 struct ccn_parsed_ContentObject *pco,
394 int content_type){
akmhoque7adb2772013-03-05 16:30:59 -0600395 if ( nlsr->debugging )
396 printf("verify key called\n");
akmhoque7c64d802013-03-05 10:18:37 -0600397 int ret=-1;
akmhoqueb8dbba82013-03-11 11:34:17 -0500398
akmhoque7c64d802013-03-05 10:18:37 -0600399 if ( contain_key_name(ccnb, pco) == 1){
400
401 struct ccn_charbuf *key_name=get_key_name(ccnb, pco);
402 struct ccn_charbuf *key_uri = ccn_charbuf_create();
403 ccn_uri_append(key_uri, key_name->buf, key_name->length, 0);
akmhoque7adb2772013-03-05 16:30:59 -0600404 if ( nlsr->debugging )
405 printf("Key Name from Incoming Content: %s\n",ccn_charbuf_as_string(key_uri));
akmhoqueb8dbba82013-03-11 11:34:17 -0500406 int key_type=get_key_type_from_key_name(key_name);
akmhoque7adb2772013-03-05 16:30:59 -0600407 if ( nlsr->debugging )
akmhoqueb8dbba82013-03-11 11:34:17 -0500408 printf("Key Type: %d \n",key_type);
akmhoque7c64d802013-03-05 10:18:37 -0600409
410 struct ccn_charbuf *result = ccn_charbuf_create();
411 struct ccn_parsed_ContentObject temp_pco = {0};
412 int get_flags = 0;
413 get_flags |= CCN_GET_NOKEYWAIT;
414 int counter = 0;
415 while(ccn_get(nlsr->ccn, key_name, NULL, 500, result, &temp_pco, NULL,
416 get_flags) < 0 && counter < 3) counter++;
417
418 int chk_verify=ccn_verify_content(nlsr->ccn,ccnb,pco);
419
420 if ( chk_verify == 0 ){
akmhoque7adb2772013-03-05 16:30:59 -0600421 if ( nlsr->debugging )
akmhoqueb7958182013-03-11 12:03:54 -0500422 printf("Content verification Successful :)\n");
akmhoque7c64d802013-03-05 10:18:37 -0600423
424 if ( counter == 3){
akmhoque7adb2772013-03-05 16:30:59 -0600425 if ( nlsr->debugging )
426 printf("Could not retrieve key by name !!!\n");
akmhoque7c64d802013-03-05 10:18:37 -0600427 }
428 else{
akmhoqueb8dbba82013-03-11 11:34:17 -0500429 if ( key_type == ROOT_KEY ){
akmhoque7c64d802013-03-05 10:18:37 -0600430 ret=0;
431 }
432 else{
akmhoqueb8dbba82013-03-11 11:34:17 -0500433 if ( nlsr->isStrictHierchicalKeyCheck ){
akmhoqueb7958182013-03-11 12:03:54 -0500434 int key_name_test=check_key_name_hierarchy(ccnb,
435 pco,
436 key_type,
437 content_type);
akmhoqueb8dbba82013-03-11 11:34:17 -0500438 if ( key_name_test == 1){
akmhoqueb7958182013-03-11 12:03:54 -0500439 ret=verify_key(result->buf,&temp_pco,content_type);
akmhoqueb8dbba82013-03-11 11:34:17 -0500440 }
441 }
442 else{
akmhoqueb7958182013-03-11 12:03:54 -0500443 ret=verify_key(result->buf,&temp_pco,content_type);
akmhoqueb8dbba82013-03-11 11:34:17 -0500444 }
akmhoque7c64d802013-03-05 10:18:37 -0600445 }
446 }
447 }
448 ccn_charbuf_destroy(&result);
449 ccn_charbuf_destroy(&key_uri);
450 ccn_charbuf_destroy(&key_name);
451 return ret;
452 }
453
454 return ret;
455}
456