akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 1 | #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 | |
| 21 | int |
| 22 | sign_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 | |
akmhoque | 7adb277 | 2013-03-05 16:30:59 -0600 | [diff] [blame] | 32 | if ( nlsr->debugging ) |
| 33 | printf("sign_content_with_user_defined_keystore called\n"); |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 34 | |
| 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 ){ |
akmhoque | 7adb277 | 2013-03-05 16:30:59 -0600 | [diff] [blame] | 47 | if ( nlsr->debugging ) |
| 48 | printf("Error in initiating keystore :(\n"); |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 49 | ccn_keystore_destroy(&keystore); |
| 50 | return -1; |
| 51 | } |
| 52 | |
| 53 | |
| 54 | res=ccn_load_private_key (nlsr->ccn, |
| 55 | keystore_path, |
akmhoque | 7adb277 | 2013-03-05 16:30:59 -0600 | [diff] [blame] | 56 | keystore_passphrase, |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 57 | pubid_out); |
| 58 | |
| 59 | if(res < 0 ){ |
akmhoque | 7adb277 | 2013-03-05 16:30:59 -0600 | [diff] [blame] | 60 | if ( nlsr->debugging ) |
| 61 | printf("Error in loading keystore :( \n"); |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 62 | 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); |
akmhoque | 7adb277 | 2013-03-05 16:30:59 -0600 | [diff] [blame] | 69 | if ( site_name[0] != '/') |
| 70 | memcpy(baseuri+strlen(baseuri),"/",1); |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 71 | 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 ){ |
akmhoque | 7adb277 | 2013-03-05 16:30:59 -0600 | [diff] [blame] | 85 | if ( nlsr->debugging ) |
| 86 | printf("Bad URI format: %s\n",baseuri); |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 87 | ccn_charbuf_destroy(&pubid_out); |
| 88 | ccn_charbuf_destroy(&keyname); |
| 89 | free(baseuri); |
| 90 | return -1; |
| 91 | } |
akmhoque | 3098a31 | 2013-03-06 07:31:01 -0600 | [diff] [blame] | 92 | |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 93 | 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); |
akmhoque | 7adb277 | 2013-03-05 16:30:59 -0600 | [diff] [blame] | 105 | if ( nlsr->debugging ) |
| 106 | printf("Key Name Included when processing content: %s\n", ccn_charbuf_as_string(uri)); |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 107 | 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)){ |
akmhoque | 7adb277 | 2013-03-05 16:30:59 -0600 | [diff] [blame] | 126 | if ( nlsr->debugging ) |
| 127 | printf("Size of pubid and sp.pubid is not equal"); |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 128 | 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 ){ |
akmhoque | 7adb277 | 2013-03-05 16:30:59 -0600 | [diff] [blame] | 140 | if ( nlsr->debugging ) |
| 141 | printf("Content signing error \n"); |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 142 | 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 | |
akmhoque | 284056b | 2013-03-15 01:20:16 -0500 | [diff] [blame] | 150 | ccn_keystore_destroy(&keystore); |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 151 | ccn_charbuf_destroy(&sp.template_ccnb); |
| 152 | ccn_charbuf_destroy(&keyid); |
| 153 | ccn_charbuf_destroy(&keyname); |
| 154 | ccn_charbuf_destroy(&pubid_out); |
akmhoque | 284056b | 2013-03-15 01:20:16 -0500 | [diff] [blame] | 155 | |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 156 | free(baseuri); |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 161 | char * |
| 162 | get_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(); |
akmhoque | b795818 | 2013-03-11 12:03:54 -0500 | [diff] [blame] | 174 | ccn_name_from_uri(name,nlsr->slice_prefix); |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 175 | 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 | |
akmhoque | b795818 | 2013-03-11 12:03:54 -0500 | [diff] [blame] | 218 | char * |
| 219 | get_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 | |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 254 | |
| 255 | int |
| 256 | check_key_name_hierarchy(const unsigned char *ccnb, |
| 257 | struct ccn_parsed_ContentObject *pco, |
akmhoque | b795818 | 2013-03-11 12:03:54 -0500 | [diff] [blame] | 258 | int key_type, int content_type){ |
akmhoque | e290122 | 2013-03-15 00:59:54 -0500 | [diff] [blame] | 259 | if ( nlsr->debugging ) |
| 260 | printf("check_key_name_hierarchy called\n"); |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 261 | if (key_type == UNKNOWN_KEY ){ |
| 262 | return 1; |
| 263 | } |
akmhoque | 5c68a64 | 2013-03-15 00:28:16 -0500 | [diff] [blame] | 264 | //int res; |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 265 | 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); |
akmhoque | e290122 | 2013-03-15 00:59:54 -0500 | [diff] [blame] | 269 | if ( nlsr->debugging ) |
| 270 | printf("Key Name: %s\n",ccn_charbuf_as_string(key_uri)); |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 271 | ccn_charbuf_destroy(&key_uri); |
| 272 | |
| 273 | struct ccn_charbuf *content_name=ccn_charbuf_create(); |
akmhoque | a33345f | 2013-03-15 00:30:03 -0500 | [diff] [blame] | 274 | ccn_charbuf_append(content_name, ccnb + pco->offset[CCN_PCO_B_Name], |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 275 | 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); |
akmhoque | e290122 | 2013-03-15 00:59:54 -0500 | [diff] [blame] | 279 | if ( nlsr->debugging ) |
| 280 | printf("Content Name: %s\n",ccn_charbuf_as_string(content_uri)); |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 281 | 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); |
akmhoque | b795818 | 2013-03-11 12:03:54 -0500 | [diff] [blame] | 285 | 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 | } |
akmhoque | e290122 | 2013-03-15 00:59:54 -0500 | [diff] [blame] | 292 | 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 | } |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 296 | |
| 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); |
akmhoque | e290122 | 2013-03-15 00:59:54 -0500 | [diff] [blame] | 309 | 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 | } |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 313 | |
| 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(); |
akmhoque | a33345f | 2013-03-15 00:30:03 -0500 | [diff] [blame] | 325 | ccn_name_split(key_name, key_name_comps); |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 326 | 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); |
akmhoque | e290122 | 2013-03-15 00:59:54 -0500 | [diff] [blame] | 328 | if ( nlsr->debugging ) |
| 329 | printf("Site key prefix(key Name):%s\n",site_key_prefix_key); |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 330 | ccn_indexbuf_destroy(&key_name_comps); |
| 331 | |
| 332 | struct ccn_indexbuf *content_name_comps; |
| 333 | content_name_comps = ccn_indexbuf_create(); |
akmhoque | a33345f | 2013-03-15 00:30:03 -0500 | [diff] [blame] | 334 | ccn_name_split(content_name, content_name_comps); |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 335 | 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); |
akmhoque | e290122 | 2013-03-15 00:59:54 -0500 | [diff] [blame] | 337 | if ( nlsr->debugging ) |
| 338 | printf("Site key prefix(Content Name):%s\n",site_key_prefix_content); |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 339 | 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(); |
akmhoque | a33345f | 2013-03-15 00:30:03 -0500 | [diff] [blame] | 354 | ccn_name_split(key_name, key_name_comps); |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 355 | 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); |
akmhoque | e290122 | 2013-03-15 00:59:54 -0500 | [diff] [blame] | 357 | if ( nlsr->debugging ) |
| 358 | printf("Site key prefix(key Name):%s\n",site_key_prefix_key); |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 359 | ccn_indexbuf_destroy(&key_name_comps); |
| 360 | |
| 361 | struct ccn_indexbuf *content_name_comps; |
| 362 | content_name_comps = ccn_indexbuf_create(); |
akmhoque | a33345f | 2013-03-15 00:30:03 -0500 | [diff] [blame] | 363 | ccn_name_split(content_name, content_name_comps); |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 364 | 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); |
akmhoque | e290122 | 2013-03-15 00:59:54 -0500 | [diff] [blame] | 366 | if ( nlsr->debugging ) |
| 367 | printf("Site key prefix(Content Name):%s\n",site_key_prefix_content); |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 368 | 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 | |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 391 | int |
akmhoque | 6e2ba84 | 2013-03-05 19:35:26 -0600 | [diff] [blame] | 392 | verify_key(const unsigned char *ccnb, |
akmhoque | b795818 | 2013-03-11 12:03:54 -0500 | [diff] [blame] | 393 | struct ccn_parsed_ContentObject *pco, |
| 394 | int content_type){ |
akmhoque | 7adb277 | 2013-03-05 16:30:59 -0600 | [diff] [blame] | 395 | if ( nlsr->debugging ) |
| 396 | printf("verify key called\n"); |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 397 | int ret=-1; |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 398 | |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 399 | 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); |
akmhoque | 7adb277 | 2013-03-05 16:30:59 -0600 | [diff] [blame] | 404 | if ( nlsr->debugging ) |
| 405 | printf("Key Name from Incoming Content: %s\n",ccn_charbuf_as_string(key_uri)); |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 406 | int key_type=get_key_type_from_key_name(key_name); |
akmhoque | 7adb277 | 2013-03-05 16:30:59 -0600 | [diff] [blame] | 407 | if ( nlsr->debugging ) |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 408 | printf("Key Type: %d \n",key_type); |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 409 | |
| 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 ){ |
akmhoque | 7adb277 | 2013-03-05 16:30:59 -0600 | [diff] [blame] | 421 | if ( nlsr->debugging ) |
akmhoque | b795818 | 2013-03-11 12:03:54 -0500 | [diff] [blame] | 422 | printf("Content verification Successful :)\n"); |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 423 | |
| 424 | if ( counter == 3){ |
akmhoque | 7adb277 | 2013-03-05 16:30:59 -0600 | [diff] [blame] | 425 | if ( nlsr->debugging ) |
| 426 | printf("Could not retrieve key by name !!!\n"); |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 427 | } |
| 428 | else{ |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 429 | if ( key_type == ROOT_KEY ){ |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 430 | ret=0; |
| 431 | } |
| 432 | else{ |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 433 | if ( nlsr->isStrictHierchicalKeyCheck ){ |
akmhoque | b795818 | 2013-03-11 12:03:54 -0500 | [diff] [blame] | 434 | int key_name_test=check_key_name_hierarchy(ccnb, |
| 435 | pco, |
| 436 | key_type, |
| 437 | content_type); |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 438 | if ( key_name_test == 1){ |
akmhoque | b795818 | 2013-03-11 12:03:54 -0500 | [diff] [blame] | 439 | ret=verify_key(result->buf,&temp_pco,content_type); |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | else{ |
akmhoque | b795818 | 2013-03-11 12:03:54 -0500 | [diff] [blame] | 443 | ret=verify_key(result->buf,&temp_pco,content_type); |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 444 | } |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 445 | } |
| 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 | |