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" |
akmhoque | 237239c | 2013-03-18 10:29:26 -0500 | [diff] [blame] | 20 | #include "utility.h" |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 21 | |
| 22 | int |
| 23 | sign_content_with_user_defined_keystore(struct ccn_charbuf *content_name, |
| 24 | struct ccn_charbuf *resultbuf, |
| 25 | const void *data, |
| 26 | size_t data_size, |
| 27 | char *keystore_path, |
| 28 | char *keystore_passphrase, |
| 29 | char *key_repo_name, |
| 30 | char *site_name, |
akmhoque | 2fafaa5 | 2013-03-22 05:10:52 -0500 | [diff] [blame] | 31 | char *router_name, |
| 32 | long int freshness){ |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 33 | |
akmhoque | 7adb277 | 2013-03-05 16:30:59 -0600 | [diff] [blame] | 34 | if ( nlsr->debugging ) |
| 35 | printf("sign_content_with_user_defined_keystore called\n"); |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 36 | |
| 37 | |
| 38 | int res; |
| 39 | |
| 40 | |
| 41 | struct ccn_charbuf * pubid_out=ccn_charbuf_create(); |
| 42 | struct ccn_charbuf * keyname; |
| 43 | |
| 44 | |
| 45 | struct ccn_keystore *keystore = NULL; |
| 46 | keystore=ccn_keystore_create(); |
| 47 | res=ccn_keystore_init(keystore, keystore_path,keystore_passphrase ); |
| 48 | if ( res < 0 ){ |
akmhoque | 7adb277 | 2013-03-05 16:30:59 -0600 | [diff] [blame] | 49 | if ( nlsr->debugging ) |
| 50 | printf("Error in initiating keystore :(\n"); |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 51 | ccn_keystore_destroy(&keystore); |
| 52 | return -1; |
| 53 | } |
| 54 | |
| 55 | |
| 56 | res=ccn_load_private_key (nlsr->ccn, |
| 57 | keystore_path, |
akmhoque | 7adb277 | 2013-03-05 16:30:59 -0600 | [diff] [blame] | 58 | keystore_passphrase, |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 59 | pubid_out); |
| 60 | |
| 61 | if(res < 0 ){ |
akmhoque | 7adb277 | 2013-03-05 16:30:59 -0600 | [diff] [blame] | 62 | if ( nlsr->debugging ) |
| 63 | printf("Error in loading keystore :( \n"); |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 64 | ccn_charbuf_destroy(&pubid_out); |
| 65 | return -1; |
| 66 | } |
| 67 | |
| 68 | char *baseuri=(char *)calloc(strlen(key_repo_name)+strlen(site_name)+ |
| 69 | strlen(router_name)+strlen("/%C1.R.N.Start")+5,sizeof(char)); |
| 70 | memcpy(baseuri,key_repo_name,strlen(key_repo_name)+1); |
akmhoque | 7adb277 | 2013-03-05 16:30:59 -0600 | [diff] [blame] | 71 | if ( site_name[0] != '/') |
| 72 | memcpy(baseuri+strlen(baseuri),"/",1); |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 73 | memcpy(baseuri+strlen(baseuri),site_name,strlen(site_name)+1); |
| 74 | memcpy(baseuri+strlen(baseuri),"/%C1.R.N.Start",strlen("/%C1.R.N.Start")); |
| 75 | memcpy(baseuri+strlen(baseuri),router_name,strlen(router_name)+1); |
| 76 | baseuri[strlen(baseuri)]='\0'; |
| 77 | |
| 78 | |
| 79 | keyname=ccn_charbuf_create(); |
| 80 | if(keyname == NULL ){ |
| 81 | ccn_charbuf_destroy(&pubid_out); |
| 82 | free(baseuri); |
| 83 | return -1; |
| 84 | } |
| 85 | ccn_name_from_uri(keyname,baseuri); |
| 86 | if ( res < 0 ){ |
akmhoque | 7adb277 | 2013-03-05 16:30:59 -0600 | [diff] [blame] | 87 | if ( nlsr->debugging ) |
| 88 | printf("Bad URI format: %s\n",baseuri); |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 89 | ccn_charbuf_destroy(&pubid_out); |
| 90 | ccn_charbuf_destroy(&keyname); |
| 91 | free(baseuri); |
| 92 | return -1; |
| 93 | } |
akmhoque | 3098a31 | 2013-03-06 07:31:01 -0600 | [diff] [blame] | 94 | |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 95 | ccn_name_append_str(keyname,"nlsr"); |
| 96 | struct ccn_charbuf *keyid = ccn_charbuf_create(); |
| 97 | ccn_charbuf_append_value(keyid, CCN_MARKER_CONTROL, 1); |
| 98 | ccn_charbuf_append_string(keyid, ".M.K"); |
| 99 | ccn_charbuf_append_value(keyid, 0, 1); |
| 100 | ccn_charbuf_append_charbuf(keyid, pubid_out); |
| 101 | ccn_name_append(keyname, keyid->buf, keyid->length); |
| 102 | |
| 103 | |
| 104 | |
| 105 | struct ccn_charbuf *uri = ccn_charbuf_create(); |
| 106 | ccn_uri_append(uri, keyname->buf, keyname->length, 0); |
akmhoque | 7adb277 | 2013-03-05 16:30:59 -0600 | [diff] [blame] | 107 | if ( nlsr->debugging ) |
| 108 | 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] | 109 | ccn_charbuf_destroy(&uri); |
| 110 | |
| 111 | struct ccn_signing_params sp = CCN_SIGNING_PARAMS_INIT; |
| 112 | sp.type = CCN_CONTENT_DATA; |
| 113 | sp.template_ccnb = ccn_charbuf_create(); |
| 114 | ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_SignedInfo, CCN_DTAG); |
| 115 | ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_KeyLocator, CCN_DTAG); |
| 116 | ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_KeyName, CCN_DTAG); |
| 117 | ccn_charbuf_append(sp.template_ccnb, keyname->buf, keyname->length); |
| 118 | ccn_charbuf_append_closer(sp.template_ccnb); // KeyName closer |
| 119 | ccn_charbuf_append_closer(sp.template_ccnb); // KeyLocator closer |
| 120 | ccn_charbuf_append_closer(sp.template_ccnb); // SignedInfo closer |
akmhoque | dbf6acc | 2013-03-22 05:36:54 -0500 | [diff] [blame] | 121 | |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 122 | |
| 123 | sp.sp_flags |= CCN_SP_TEMPL_KEY_LOCATOR; |
| 124 | sp.sp_flags |= CCN_SP_FINAL_BLOCK; |
akmhoque | ced50e9 | 2013-03-22 05:40:18 -0500 | [diff] [blame] | 125 | sp.freshness = freshness; |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 126 | |
| 127 | |
| 128 | if (pubid_out->length != sizeof(sp.pubid)){ |
akmhoque | 7adb277 | 2013-03-05 16:30:59 -0600 | [diff] [blame] | 129 | if ( nlsr->debugging ) |
| 130 | printf("Size of pubid and sp.pubid is not equal"); |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 131 | ccn_charbuf_destroy(&keyname); |
| 132 | ccn_charbuf_destroy(&pubid_out); |
| 133 | free(baseuri); |
| 134 | return -1; |
| 135 | } |
| 136 | |
| 137 | memcpy(sp.pubid, pubid_out->buf, pubid_out->length); |
| 138 | |
| 139 | |
| 140 | |
| 141 | res=ccn_sign_content(nlsr->ccn,resultbuf,content_name,&sp,data,data_size); |
| 142 | if( res < 0 ){ |
akmhoque | 7adb277 | 2013-03-05 16:30:59 -0600 | [diff] [blame] | 143 | if ( nlsr->debugging ) |
| 144 | printf("Content signing error \n"); |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 145 | ccn_charbuf_destroy(&sp.template_ccnb); |
| 146 | ccn_charbuf_destroy(&keyid); |
| 147 | ccn_charbuf_destroy(&keyname); |
| 148 | ccn_charbuf_destroy(&pubid_out); |
| 149 | free(baseuri); |
| 150 | return -1; |
| 151 | } |
| 152 | |
akmhoque | 284056b | 2013-03-15 01:20:16 -0500 | [diff] [blame] | 153 | ccn_keystore_destroy(&keystore); |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 154 | ccn_charbuf_destroy(&sp.template_ccnb); |
| 155 | ccn_charbuf_destroy(&keyid); |
| 156 | ccn_charbuf_destroy(&keyname); |
| 157 | ccn_charbuf_destroy(&pubid_out); |
akmhoque | 284056b | 2013-03-15 01:20:16 -0500 | [diff] [blame] | 158 | |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 159 | free(baseuri); |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 164 | char * |
| 165 | get_orig_router_from_lsa_name(struct ccn_charbuf * content_name) |
| 166 | { |
| 167 | int start=0; |
| 168 | |
| 169 | size_t comp_size; |
| 170 | const unsigned char *second_last_comp; |
| 171 | char *second_comp_type; |
| 172 | char *sep="."; |
| 173 | char *rem; |
| 174 | |
| 175 | struct ccn_indexbuf *components=ccn_indexbuf_create(); |
| 176 | struct ccn_charbuf *name=ccn_charbuf_create(); |
akmhoque | b795818 | 2013-03-11 12:03:54 -0500 | [diff] [blame] | 177 | ccn_name_from_uri(name,nlsr->slice_prefix); |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 178 | ccn_name_split (name, components); |
| 179 | start=components->n-2; |
| 180 | ccn_charbuf_destroy(&name); |
| 181 | ccn_indexbuf_destroy(&components); |
| 182 | |
| 183 | struct ccn_indexbuf *comps=ccn_indexbuf_create(); |
| 184 | ccn_name_split (content_name, comps); |
| 185 | ccn_name_comp_get( content_name->buf, comps, |
| 186 | comps->n-1-2, &second_last_comp, &comp_size); |
| 187 | |
| 188 | second_comp_type=strtok_r((char *)second_last_comp, sep, &rem); |
| 189 | if ( strcmp( second_comp_type, "lsId" ) == 0 ){ |
| 190 | ccn_name_chop(content_name,comps,-3); |
| 191 | } |
| 192 | else{ |
| 193 | ccn_name_chop(content_name,comps,-2); |
| 194 | } |
| 195 | |
| 196 | |
| 197 | struct ccn_charbuf *temp=ccn_charbuf_create(); |
| 198 | ccn_name_init(temp); |
| 199 | ccn_name_append_components( temp, content_name->buf, |
| 200 | comps->buf[start+1], |
| 201 | comps->buf[comps->n - 1]); |
| 202 | |
| 203 | struct ccn_charbuf *temp1=ccn_charbuf_create(); |
| 204 | ccn_uri_append(temp1, temp->buf, temp->length, 0); |
| 205 | |
| 206 | char *orig_router=(char *)calloc(strlen(ccn_charbuf_as_string(temp1))+1, |
| 207 | sizeof(char)); |
| 208 | memcpy(orig_router,ccn_charbuf_as_string(temp1), |
| 209 | strlen(ccn_charbuf_as_string(temp1))); |
| 210 | orig_router[strlen(orig_router)]='\0'; |
| 211 | |
| 212 | ccn_charbuf_destroy(&temp); |
| 213 | ccn_charbuf_destroy(&temp1); |
| 214 | ccn_indexbuf_destroy(&comps); |
| 215 | return orig_router; |
| 216 | |
| 217 | |
| 218 | } |
| 219 | |
| 220 | |
akmhoque | b795818 | 2013-03-11 12:03:54 -0500 | [diff] [blame] | 221 | char * |
| 222 | get_orig_router_from_info_content_name(struct ccn_charbuf * content_name) |
| 223 | { |
| 224 | int start,end; |
| 225 | |
| 226 | start=0; |
| 227 | |
| 228 | struct ccn_indexbuf *comps=ccn_indexbuf_create(); |
| 229 | ccn_name_split (content_name, comps); |
| 230 | |
| 231 | end=check_for_name_component_in_name(content_name,comps,"nlsr"); |
| 232 | |
| 233 | |
| 234 | struct ccn_charbuf *temp=ccn_charbuf_create(); |
| 235 | ccn_name_init(temp); |
| 236 | ccn_name_append_components( temp, content_name->buf, |
| 237 | comps->buf[start], |
| 238 | comps->buf[end]); |
| 239 | |
| 240 | struct ccn_charbuf *temp1=ccn_charbuf_create(); |
| 241 | ccn_uri_append(temp1, temp->buf, temp->length, 0); |
| 242 | |
| 243 | char *orig_router=(char *)calloc(strlen(ccn_charbuf_as_string(temp1))+1, |
| 244 | sizeof(char)); |
| 245 | memcpy(orig_router,ccn_charbuf_as_string(temp1), |
| 246 | strlen(ccn_charbuf_as_string(temp1))); |
| 247 | orig_router[strlen(orig_router)]='\0'; |
| 248 | |
| 249 | ccn_charbuf_destroy(&temp); |
| 250 | ccn_charbuf_destroy(&temp1); |
| 251 | ccn_indexbuf_destroy(&comps); |
| 252 | return orig_router; |
| 253 | |
| 254 | |
| 255 | } |
| 256 | |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 257 | |
| 258 | int |
| 259 | check_key_name_hierarchy(const unsigned char *ccnb, |
| 260 | struct ccn_parsed_ContentObject *pco, |
akmhoque | b795818 | 2013-03-11 12:03:54 -0500 | [diff] [blame] | 261 | int key_type, int content_type){ |
akmhoque | e290122 | 2013-03-15 00:59:54 -0500 | [diff] [blame] | 262 | if ( nlsr->debugging ) |
| 263 | printf("check_key_name_hierarchy called\n"); |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 264 | if (key_type == UNKNOWN_KEY ){ |
| 265 | return 1; |
| 266 | } |
akmhoque | 5c68a64 | 2013-03-15 00:28:16 -0500 | [diff] [blame] | 267 | //int res; |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 268 | struct ccn_charbuf *key_name=get_key_name(ccnb, pco); |
| 269 | |
| 270 | struct ccn_charbuf *key_uri = ccn_charbuf_create(); |
| 271 | ccn_uri_append(key_uri, key_name->buf, key_name->length, 0); |
akmhoque | e290122 | 2013-03-15 00:59:54 -0500 | [diff] [blame] | 272 | if ( nlsr->debugging ) |
| 273 | printf("Key Name: %s\n",ccn_charbuf_as_string(key_uri)); |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 274 | ccn_charbuf_destroy(&key_uri); |
| 275 | |
| 276 | struct ccn_charbuf *content_name=ccn_charbuf_create(); |
akmhoque | a33345f | 2013-03-15 00:30:03 -0500 | [diff] [blame] | 277 | ccn_charbuf_append(content_name, ccnb + pco->offset[CCN_PCO_B_Name], |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 278 | pco->offset[CCN_PCO_E_Name] - pco->offset[CCN_PCO_B_Name]); |
| 279 | |
| 280 | struct ccn_charbuf *content_uri = ccn_charbuf_create(); |
| 281 | ccn_uri_append(content_uri, content_name->buf, content_name->length, 0); |
akmhoque | e290122 | 2013-03-15 00:59:54 -0500 | [diff] [blame] | 282 | if ( nlsr->debugging ) |
| 283 | printf("Content Name: %s\n",ccn_charbuf_as_string(content_uri)); |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 284 | ccn_charbuf_destroy(&content_uri); |
| 285 | |
| 286 | if ( key_type == NLSR_KEY){ |
| 287 | 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] | 288 | char *orig_router_content_name; |
| 289 | if ( content_type == 1 ){ |
| 290 | orig_router_content_name=get_orig_router_from_lsa_name(content_name); |
| 291 | } |
| 292 | else if ( content_type == 0 ){ |
| 293 | orig_router_content_name=get_orig_router_from_info_content_name(content_name); |
| 294 | } |
akmhoque | e290122 | 2013-03-15 00:59:54 -0500 | [diff] [blame] | 295 | if ( nlsr->debugging ){ |
| 296 | printf("Orig Router (Key Name):%s\n",orig_router_key_name); |
| 297 | printf("Orig Router (Content Name):%s\n",orig_router_content_name); |
| 298 | } |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 299 | |
| 300 | if (strcmp(orig_router_key_name,orig_router_content_name) == 0 ){ |
| 301 | free(orig_router_key_name); |
| 302 | free(orig_router_content_name); |
| 303 | ccn_charbuf_destroy(&key_name); |
| 304 | ccn_charbuf_destroy(&content_name); |
| 305 | return 1; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | if ( key_type == ROUTING_KEY){ |
| 310 | char *orig_router_key_name=get_orig_router_from_key_name(key_name,1,0); |
| 311 | 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] | 312 | if ( nlsr->debugging ){ |
| 313 | printf("Orig Router (Key Name):%s\n",orig_router_key_name); |
| 314 | printf("Orig Router (Content Name):%s\n",orig_router_content_name); |
| 315 | } |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 316 | |
| 317 | if (strcmp(orig_router_key_name,orig_router_content_name) == 0 ){ |
| 318 | free(orig_router_key_name); |
| 319 | free(orig_router_content_name); |
| 320 | ccn_charbuf_destroy(&key_name); |
| 321 | ccn_charbuf_destroy(&content_name); |
| 322 | return 1; |
| 323 | } |
| 324 | } |
| 325 | if ( key_type == OPERATOR_KEY){ |
| 326 | struct ccn_indexbuf *key_name_comps; |
| 327 | key_name_comps = ccn_indexbuf_create(); |
akmhoque | a33345f | 2013-03-15 00:30:03 -0500 | [diff] [blame] | 328 | ccn_name_split(key_name, key_name_comps); |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 329 | int last_indx=check_for_tag_component_in_name(key_name,key_name_comps,"O.N.Start"); |
| 330 | 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] | 331 | if ( nlsr->debugging ) |
| 332 | printf("Site key prefix(key Name):%s\n",site_key_prefix_key); |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 333 | ccn_indexbuf_destroy(&key_name_comps); |
| 334 | |
| 335 | struct ccn_indexbuf *content_name_comps; |
| 336 | content_name_comps = ccn_indexbuf_create(); |
akmhoque | a33345f | 2013-03-15 00:30:03 -0500 | [diff] [blame] | 337 | ccn_name_split(content_name, content_name_comps); |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 338 | int last_indx_rtr=check_for_tag_component_in_name(content_name,content_name_comps,"R.N.Start"); |
| 339 | 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] | 340 | if ( nlsr->debugging ) |
| 341 | printf("Site key prefix(Content Name):%s\n",site_key_prefix_content); |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 342 | ccn_indexbuf_destroy(&content_name_comps); |
| 343 | |
| 344 | if( strcmp(site_key_prefix_key,site_key_prefix_content) == 0 ){ |
| 345 | free(site_key_prefix_key); |
| 346 | free(site_key_prefix_content); |
| 347 | ccn_charbuf_destroy(&key_name); |
| 348 | ccn_charbuf_destroy(&content_name); |
| 349 | return 1; |
| 350 | } |
| 351 | |
| 352 | } |
| 353 | |
| 354 | if ( key_type == SITE_KEY){ |
| 355 | struct ccn_indexbuf *key_name_comps; |
| 356 | key_name_comps = ccn_indexbuf_create(); |
akmhoque | a33345f | 2013-03-15 00:30:03 -0500 | [diff] [blame] | 357 | ccn_name_split(key_name, key_name_comps); |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 358 | int last_indx=check_for_tag_component_in_name(key_name,key_name_comps,"M.K"); |
| 359 | 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] | 360 | if ( nlsr->debugging ) |
| 361 | printf("Site key prefix(key Name):%s\n",site_key_prefix_key); |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 362 | ccn_indexbuf_destroy(&key_name_comps); |
| 363 | |
| 364 | struct ccn_indexbuf *content_name_comps; |
| 365 | content_name_comps = ccn_indexbuf_create(); |
akmhoque | a33345f | 2013-03-15 00:30:03 -0500 | [diff] [blame] | 366 | ccn_name_split(content_name, content_name_comps); |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 367 | int last_indx_rtr=check_for_tag_component_in_name(content_name,content_name_comps,"O.N.Start"); |
| 368 | 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] | 369 | if ( nlsr->debugging ) |
| 370 | printf("Site key prefix(Content Name):%s\n",site_key_prefix_content); |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 371 | ccn_indexbuf_destroy(&content_name_comps); |
| 372 | |
| 373 | if( strcmp(site_key_prefix_key,site_key_prefix_content) == 0 ){ |
| 374 | free(site_key_prefix_key); |
| 375 | free(site_key_prefix_content); |
| 376 | ccn_charbuf_destroy(&key_name); |
| 377 | ccn_charbuf_destroy(&content_name); |
| 378 | return 1; |
| 379 | } |
| 380 | |
| 381 | } |
| 382 | |
| 383 | if ( key_type == ROOT_KEY){ |
| 384 | ccn_charbuf_destroy(&key_name); |
| 385 | ccn_charbuf_destroy(&content_name); |
| 386 | return 1; |
| 387 | } |
| 388 | |
| 389 | ccn_charbuf_destroy(&key_name); |
| 390 | ccn_charbuf_destroy(&content_name); |
| 391 | return 0; |
| 392 | } |
| 393 | |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 394 | int |
akmhoque | 6e2ba84 | 2013-03-05 19:35:26 -0600 | [diff] [blame] | 395 | verify_key(const unsigned char *ccnb, |
akmhoque | b795818 | 2013-03-11 12:03:54 -0500 | [diff] [blame] | 396 | struct ccn_parsed_ContentObject *pco, |
| 397 | int content_type){ |
akmhoque | 7adb277 | 2013-03-05 16:30:59 -0600 | [diff] [blame] | 398 | if ( nlsr->debugging ) |
| 399 | printf("verify key called\n"); |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 400 | int ret=-1; |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 401 | |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 402 | if ( contain_key_name(ccnb, pco) == 1){ |
| 403 | |
| 404 | struct ccn_charbuf *key_name=get_key_name(ccnb, pco); |
| 405 | struct ccn_charbuf *key_uri = ccn_charbuf_create(); |
| 406 | ccn_uri_append(key_uri, key_name->buf, key_name->length, 0); |
akmhoque | 7adb277 | 2013-03-05 16:30:59 -0600 | [diff] [blame] | 407 | if ( nlsr->debugging ) |
| 408 | 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] | 409 | int key_type=get_key_type_from_key_name(key_name); |
akmhoque | 7adb277 | 2013-03-05 16:30:59 -0600 | [diff] [blame] | 410 | if ( nlsr->debugging ) |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 411 | printf("Key Type: %d \n",key_type); |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 412 | |
| 413 | struct ccn_charbuf *result = ccn_charbuf_create(); |
| 414 | struct ccn_parsed_ContentObject temp_pco = {0}; |
| 415 | int get_flags = 0; |
| 416 | get_flags |= CCN_GET_NOKEYWAIT; |
| 417 | int counter = 0; |
| 418 | while(ccn_get(nlsr->ccn, key_name, NULL, 500, result, &temp_pco, NULL, |
| 419 | get_flags) < 0 && counter < 3) counter++; |
| 420 | |
| 421 | int chk_verify=ccn_verify_content(nlsr->ccn,ccnb,pco); |
| 422 | |
| 423 | if ( chk_verify == 0 ){ |
akmhoque | 7adb277 | 2013-03-05 16:30:59 -0600 | [diff] [blame] | 424 | if ( nlsr->debugging ) |
akmhoque | b795818 | 2013-03-11 12:03:54 -0500 | [diff] [blame] | 425 | printf("Content verification Successful :)\n"); |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 426 | |
| 427 | if ( counter == 3){ |
akmhoque | 7adb277 | 2013-03-05 16:30:59 -0600 | [diff] [blame] | 428 | if ( nlsr->debugging ) |
| 429 | printf("Could not retrieve key by name !!!\n"); |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 430 | } |
| 431 | else{ |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 432 | if ( key_type == ROOT_KEY ){ |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 433 | ret=0; |
| 434 | } |
| 435 | else{ |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 436 | if ( nlsr->isStrictHierchicalKeyCheck ){ |
akmhoque | b795818 | 2013-03-11 12:03:54 -0500 | [diff] [blame] | 437 | int key_name_test=check_key_name_hierarchy(ccnb, |
| 438 | pco, |
| 439 | key_type, |
| 440 | content_type); |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 441 | if ( key_name_test == 1){ |
akmhoque | b795818 | 2013-03-11 12:03:54 -0500 | [diff] [blame] | 442 | ret=verify_key(result->buf,&temp_pco,content_type); |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | else{ |
akmhoque | b795818 | 2013-03-11 12:03:54 -0500 | [diff] [blame] | 446 | ret=verify_key(result->buf,&temp_pco,content_type); |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 447 | } |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | } |
| 451 | ccn_charbuf_destroy(&result); |
| 452 | ccn_charbuf_destroy(&key_uri); |
| 453 | ccn_charbuf_destroy(&key_name); |
| 454 | return ret; |
| 455 | } |
| 456 | |
| 457 | return ret; |
| 458 | } |
| 459 | |
akmhoque | 237239c | 2013-03-18 10:29:26 -0500 | [diff] [blame] | 460 | void |
| 461 | destroy_keys(void) |
| 462 | { |
| 463 | int i, key_element; |
| 464 | struct nlsr_key *key; |
| 465 | |
| 466 | struct hashtb_enumerator ee; |
| 467 | struct hashtb_enumerator *e = ⅇ |
| 468 | |
| 469 | hashtb_start(nlsr->keys, e); |
| 470 | key_element=hashtb_n(nlsr->keys); |
| 471 | |
| 472 | for(i=0;i<key_element;i++) |
| 473 | { |
| 474 | key=e->data; |
| 475 | free(key->key_name); |
| 476 | hashtb_next(e); |
| 477 | } |
| 478 | |
| 479 | hashtb_end(e); |
| 480 | |
| 481 | if( nlsr->keys ) |
| 482 | hashtb_destroy(&nlsr->keys); |
| 483 | |
| 484 | } |
| 485 | |
| 486 | void |
| 487 | print_keys(void){ |
| 488 | |
| 489 | if ( nlsr->debugging ) |
| 490 | printf("print_keys called \n"); |
| 491 | if ( nlsr->detailed_logging ) |
| 492 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"print_keys called \n"); |
| 493 | |
| 494 | int i, key_element; |
| 495 | struct nlsr_key *key; |
| 496 | |
| 497 | struct hashtb_enumerator ee; |
| 498 | struct hashtb_enumerator *e = ⅇ |
| 499 | |
| 500 | hashtb_start(nlsr->keys, e); |
| 501 | key_element=hashtb_n(nlsr->keys); |
| 502 | |
| 503 | for(i=0;i<key_element;i++) |
| 504 | { |
| 505 | key=e->data; |
| 506 | |
| 507 | if ( nlsr->debugging ) |
| 508 | printf("Key : %s \n",key->key_name); |
| 509 | |
| 510 | hashtb_next(e); |
| 511 | } |
| 512 | |
| 513 | hashtb_end(e); |
| 514 | |
| 515 | } |
| 516 | |
| 517 | int |
| 518 | does_key_exist(char *keyname){ |
| 519 | if (nlsr->debugging) |
| 520 | { |
| 521 | printf("does_key_exist called\n"); |
| 522 | printf("Keyname : %s \n",keyname); |
| 523 | } |
| 524 | |
| 525 | int ret=0; |
| 526 | |
| 527 | unsigned *v; |
| 528 | v = hashtb_lookup(nlsr->keys, keyname, strlen(keyname)); |
| 529 | if (v != NULL){ |
| 530 | ret = 1; |
| 531 | if (nlsr->debugging) |
| 532 | printf("Key Found\n"); |
| 533 | } |
| 534 | |
| 535 | return ret; |
| 536 | } |
| 537 | |
| 538 | void |
| 539 | add_key(char *keyname){ |
| 540 | if (nlsr->debugging) |
| 541 | { |
| 542 | printf("add_key called\n"); |
| 543 | printf("Keyname : %s \n",keyname); |
| 544 | } |
| 545 | |
| 546 | struct nlsr_key *key; |
| 547 | |
| 548 | struct hashtb_enumerator ee; |
| 549 | struct hashtb_enumerator *e = ⅇ |
| 550 | int res; |
| 551 | |
| 552 | hashtb_start(nlsr->keys, e); |
| 553 | res = hashtb_seek(e, keyname, strlen(keyname), 0); |
| 554 | |
| 555 | if(res == HT_NEW_ENTRY ) |
| 556 | { |
| 557 | key=e->data; |
| 558 | key->key_name=(char *)calloc(strlen(keyname)+1,sizeof(char)); |
| 559 | memcpy(key->key_name,keyname,strlen(keyname)+1); |
| 560 | } |
| 561 | |
| 562 | if (nlsr->debugging) |
| 563 | print_keys(); |
| 564 | } |