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