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