blob: dcd0d289b238d20ca2ddd42fbb1580d4c82861aa [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){
akmhoqueb8dbba82013-03-11 11:34:17 -0500257 printf("check_key_name_hierarchy called\n");
258 if (key_type == UNKNOWN_KEY ){
259 return 1;
260 }
akmhoque5c68a642013-03-15 00:28:16 -0500261 //int res;
akmhoqueb8dbba82013-03-11 11:34:17 -0500262 struct ccn_charbuf *key_name=get_key_name(ccnb, pco);
263
264 struct ccn_charbuf *key_uri = ccn_charbuf_create();
265 ccn_uri_append(key_uri, key_name->buf, key_name->length, 0);
266 printf("Key Name: %s\n",ccn_charbuf_as_string(key_uri));
267 ccn_charbuf_destroy(&key_uri);
268
269 struct ccn_charbuf *content_name=ccn_charbuf_create();
270 res=ccn_charbuf_append(content_name, ccnb + pco->offset[CCN_PCO_B_Name],
271 pco->offset[CCN_PCO_E_Name] - pco->offset[CCN_PCO_B_Name]);
272
273 struct ccn_charbuf *content_uri = ccn_charbuf_create();
274 ccn_uri_append(content_uri, content_name->buf, content_name->length, 0);
275 printf("Content Name: %s\n",ccn_charbuf_as_string(content_uri));
276 ccn_charbuf_destroy(&content_uri);
277
278 if ( key_type == NLSR_KEY){
279 char *orig_router_key_name=get_orig_router_from_key_name(key_name,0,0);
akmhoqueb7958182013-03-11 12:03:54 -0500280 char *orig_router_content_name;
281 if ( content_type == 1 ){
282 orig_router_content_name=get_orig_router_from_lsa_name(content_name);
283 }
284 else if ( content_type == 0 ){
285 orig_router_content_name=get_orig_router_from_info_content_name(content_name);
286 }
akmhoqueb8dbba82013-03-11 11:34:17 -0500287 printf("Orig Router (Key Name):%s\n",orig_router_key_name);
288 printf("Orig Router (Content Name):%s\n",orig_router_content_name);
289
290 if (strcmp(orig_router_key_name,orig_router_content_name) == 0 ){
291 free(orig_router_key_name);
292 free(orig_router_content_name);
293 ccn_charbuf_destroy(&key_name);
294 ccn_charbuf_destroy(&content_name);
295 return 1;
296 }
297 }
298
299 if ( key_type == ROUTING_KEY){
300 char *orig_router_key_name=get_orig_router_from_key_name(key_name,1,0);
301 char *orig_router_content_name=get_orig_router_from_key_name(content_name,1,1);
302 printf("Orig Router (Key Name):%s\n",orig_router_key_name);
303 printf("Orig Router (Content Name):%s\n",orig_router_content_name);
304
305 if (strcmp(orig_router_key_name,orig_router_content_name) == 0 ){
306 free(orig_router_key_name);
307 free(orig_router_content_name);
308 ccn_charbuf_destroy(&key_name);
309 ccn_charbuf_destroy(&content_name);
310 return 1;
311 }
312 }
313 if ( key_type == OPERATOR_KEY){
314 struct ccn_indexbuf *key_name_comps;
315 key_name_comps = ccn_indexbuf_create();
316 res = ccn_name_split(key_name, key_name_comps);
317 int last_indx=check_for_tag_component_in_name(key_name,key_name_comps,"O.N.Start");
318 char *site_key_prefix_key=get_name_segments_from_name(key_name,0,last_indx);
319 printf("Site key prefix(key Name):%s\n",site_key_prefix_key);
320 ccn_indexbuf_destroy(&key_name_comps);
321
322 struct ccn_indexbuf *content_name_comps;
323 content_name_comps = ccn_indexbuf_create();
324 res = ccn_name_split(content_name, content_name_comps);
325 int last_indx_rtr=check_for_tag_component_in_name(content_name,content_name_comps,"R.N.Start");
326 char *site_key_prefix_content=get_name_segments_from_name(key_name,0,last_indx_rtr);
327 printf("Site key prefix(Content Name):%s\n",site_key_prefix_content);
328 ccn_indexbuf_destroy(&content_name_comps);
329
330 if( strcmp(site_key_prefix_key,site_key_prefix_content) == 0 ){
331 free(site_key_prefix_key);
332 free(site_key_prefix_content);
333 ccn_charbuf_destroy(&key_name);
334 ccn_charbuf_destroy(&content_name);
335 return 1;
336 }
337
338 }
339
340 if ( key_type == SITE_KEY){
341 struct ccn_indexbuf *key_name_comps;
342 key_name_comps = ccn_indexbuf_create();
343 res = ccn_name_split(key_name, key_name_comps);
344 int last_indx=check_for_tag_component_in_name(key_name,key_name_comps,"M.K");
345 char *site_key_prefix_key=get_name_segments_from_name(key_name,0,last_indx);
346 printf("Site key prefix(key Name):%s\n",site_key_prefix_key);
347 ccn_indexbuf_destroy(&key_name_comps);
348
349 struct ccn_indexbuf *content_name_comps;
350 content_name_comps = ccn_indexbuf_create();
351 res = ccn_name_split(content_name, content_name_comps);
352 int last_indx_rtr=check_for_tag_component_in_name(content_name,content_name_comps,"O.N.Start");
353 char *site_key_prefix_content=get_name_segments_from_name(key_name,0,last_indx_rtr);
354 printf("Site key prefix(Content Name):%s\n",site_key_prefix_content);
355 ccn_indexbuf_destroy(&content_name_comps);
356
357 if( strcmp(site_key_prefix_key,site_key_prefix_content) == 0 ){
358 free(site_key_prefix_key);
359 free(site_key_prefix_content);
360 ccn_charbuf_destroy(&key_name);
361 ccn_charbuf_destroy(&content_name);
362 return 1;
363 }
364
365 }
366
367 if ( key_type == ROOT_KEY){
368 ccn_charbuf_destroy(&key_name);
369 ccn_charbuf_destroy(&content_name);
370 return 1;
371 }
372
373 ccn_charbuf_destroy(&key_name);
374 ccn_charbuf_destroy(&content_name);
375 return 0;
376}
377
akmhoque7c64d802013-03-05 10:18:37 -0600378int
akmhoque6e2ba842013-03-05 19:35:26 -0600379verify_key(const unsigned char *ccnb,
akmhoqueb7958182013-03-11 12:03:54 -0500380 struct ccn_parsed_ContentObject *pco,
381 int content_type){
akmhoque7adb2772013-03-05 16:30:59 -0600382 if ( nlsr->debugging )
383 printf("verify key called\n");
akmhoque7c64d802013-03-05 10:18:37 -0600384 int ret=-1;
akmhoqueb8dbba82013-03-11 11:34:17 -0500385 //int res;
386
akmhoque7c64d802013-03-05 10:18:37 -0600387 if ( contain_key_name(ccnb, pco) == 1){
388
389 struct ccn_charbuf *key_name=get_key_name(ccnb, pco);
390 struct ccn_charbuf *key_uri = ccn_charbuf_create();
391 ccn_uri_append(key_uri, key_name->buf, key_name->length, 0);
akmhoque7adb2772013-03-05 16:30:59 -0600392 if ( nlsr->debugging )
393 printf("Key Name from Incoming Content: %s\n",ccn_charbuf_as_string(key_uri));
akmhoqueb8dbba82013-03-11 11:34:17 -0500394 int key_type=get_key_type_from_key_name(key_name);
akmhoque7adb2772013-03-05 16:30:59 -0600395 if ( nlsr->debugging )
akmhoqueb8dbba82013-03-11 11:34:17 -0500396 printf("Key Type: %d \n",key_type);
akmhoque7c64d802013-03-05 10:18:37 -0600397
398 struct ccn_charbuf *result = ccn_charbuf_create();
399 struct ccn_parsed_ContentObject temp_pco = {0};
400 int get_flags = 0;
401 get_flags |= CCN_GET_NOKEYWAIT;
402 int counter = 0;
403 while(ccn_get(nlsr->ccn, key_name, NULL, 500, result, &temp_pco, NULL,
404 get_flags) < 0 && counter < 3) counter++;
405
406 int chk_verify=ccn_verify_content(nlsr->ccn,ccnb,pco);
407
408 if ( chk_verify == 0 ){
akmhoque7adb2772013-03-05 16:30:59 -0600409 if ( nlsr->debugging )
akmhoqueb7958182013-03-11 12:03:54 -0500410 printf("Content verification Successful :)\n");
akmhoque7c64d802013-03-05 10:18:37 -0600411
412 if ( counter == 3){
akmhoque7adb2772013-03-05 16:30:59 -0600413 if ( nlsr->debugging )
414 printf("Could not retrieve key by name !!!\n");
akmhoque7c64d802013-03-05 10:18:37 -0600415 }
416 else{
akmhoqueb8dbba82013-03-11 11:34:17 -0500417 if ( key_type == ROOT_KEY ){
akmhoque7c64d802013-03-05 10:18:37 -0600418 ret=0;
419 }
420 else{
akmhoqueb8dbba82013-03-11 11:34:17 -0500421 if ( nlsr->isStrictHierchicalKeyCheck ){
akmhoqueb7958182013-03-11 12:03:54 -0500422 int key_name_test=check_key_name_hierarchy(ccnb,
423 pco,
424 key_type,
425 content_type);
akmhoqueb8dbba82013-03-11 11:34:17 -0500426 if ( key_name_test == 1){
akmhoqueb7958182013-03-11 12:03:54 -0500427 ret=verify_key(result->buf,&temp_pco,content_type);
akmhoqueb8dbba82013-03-11 11:34:17 -0500428 }
429 }
430 else{
akmhoqueb7958182013-03-11 12:03:54 -0500431 ret=verify_key(result->buf,&temp_pco,content_type);
akmhoqueb8dbba82013-03-11 11:34:17 -0500432 }
akmhoque7c64d802013-03-05 10:18:37 -0600433 }
434 }
435 }
436 ccn_charbuf_destroy(&result);
437 ccn_charbuf_destroy(&key_uri);
438 ccn_charbuf_destroy(&key_name);
439 return ret;
440 }
441
442 return ret;
443}
444