int GroupManager::ccn_free() { if (ccn != NULL) { ccn_disconnect(ccn); ccn_destroy(&ccn); } if (cached_keystore != NULL) ccn_keystore_destroy(&cached_keystore); if (join_closure != NULL) { free(join_closure); join_closure = NULL; } if (req_closure != NULL) { free(req_closure); req_closure = NULL; } if (leave_closure != NULL) { free(leave_closure); leave_closure = NULL; } return 0; }
int main(int argc, char **argv) { const char *progname = argv[0]; struct ccn *ccn = NULL; struct ccn_charbuf *root = NULL; struct ccn_charbuf *name = NULL; struct ccn_charbuf *temp = NULL; struct ccn_charbuf *templ = NULL; struct ccn_charbuf *signed_info = NULL; struct ccn_charbuf *keylocator = NULL; struct ccn_charbuf *finalblockid = NULL; struct ccn_keystore *keystore = NULL; long expire = -1; long blocksize = 1024; int i; int status = 0; int res; ssize_t read_res; unsigned char *buf = NULL; struct mydata mydata = { 0 }; struct ccn_closure in_content = {.p=&incoming_content, .data=&mydata}; struct ccn_closure in_interest = {.p=&incoming_interest, .data=&mydata}; while ((res = getopt(argc, argv, "hx:b:")) != -1) { switch (res) { case 'x': expire = atol(optarg); if (expire <= 0) usage(progname); break; case 'b': blocksize = atol(optarg); break; default: case 'h': usage(progname); break; } } argc -= optind; argv += optind; if (argv[0] == NULL) usage(progname); name = ccn_charbuf_create(); res = ccn_name_from_uri(name, argv[0]); if (res < 0) { fprintf(stderr, "%s: bad ccn URI: %s\n", progname, argv[0]); exit(1); } if (argv[1] != NULL) fprintf(stderr, "%s warning: extra arguments ignored\n", progname); ccn = ccn_create(); if (ccn_connect(ccn, NULL) == -1) { perror("Could not connect to ccnd"); exit(1); } buf = calloc(1, blocksize); root = name; name = ccn_charbuf_create(); temp = ccn_charbuf_create(); templ = ccn_charbuf_create(); signed_info = ccn_charbuf_create(); keystore = ccn_keystore_create(); temp->length = 0; ccn_charbuf_putf(temp, "%s/.ccnx/.ccnx_keystore", getenv("HOME")); res = ccn_keystore_init(keystore, ccn_charbuf_as_string(temp), "Th1s1sn0t8g00dp8ssw0rd."); if (res != 0) { printf("Failed to initialize keystore\n"); exit(1); } name->length = 0; ccn_charbuf_append(name, root->buf, root->length); /* Set up a handler for interests */ ccn_set_interest_filter(ccn, name, &in_interest); /* Initiate check to see whether there is already something there. */ temp->length = 0; ccn_charbuf_putf(temp, "%d", 0); ccn_name_append(name, temp->buf, temp->length); templ->length = 0; ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG); ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG); ccn_charbuf_append_closer(templ); /* </Name> */ ccn_charbuf_append_tt(templ, CCN_DTAG_MaxSuffixComponents, CCN_DTAG); ccn_charbuf_append_tt(templ, 1, CCN_UDATA); ccn_charbuf_append(templ, "1", 1); ccn_charbuf_append_closer(templ); /* </MaxSuffixComponents> */ // XXX - use pubid ccn_charbuf_append_closer(templ); /* </Interest> */ res = ccn_express_interest(ccn, name, &in_content, templ); if (res < 0) abort(); /* Construct a key locator contining the key itself */ keylocator = ccn_charbuf_create(); ccn_charbuf_append_tt(keylocator, CCN_DTAG_KeyLocator, CCN_DTAG); ccn_charbuf_append_tt(keylocator, CCN_DTAG_Key, CCN_DTAG); res = ccn_append_pubkey_blob(keylocator, ccn_keystore_public_key(keystore)); if (res < 0) ccn_charbuf_destroy(&keylocator); else { ccn_charbuf_append_closer(keylocator); /* </Key> */ ccn_charbuf_append_closer(keylocator); /* </KeyLocator> */ } for (i = 0;; i++) { read_res = read_full(0, buf, blocksize); if (read_res < 0) { perror("read"); read_res = 0; status = 1; } signed_info->length = 0; if (read_res < blocksize) { temp->length = 0; ccn_charbuf_putf(temp, "%d", i); ccn_name_append(name, temp->buf, temp->length); finalblockid = ccn_charbuf_create(); ccn_charbuf_append_tt(finalblockid, temp->length, CCN_BLOB); ccn_charbuf_append(finalblockid, temp->buf, temp->length); } res = ccn_signed_info_create(signed_info, /*pubkeyid*/ccn_keystore_public_key_digest(keystore), /*publisher_key_id_size*/ccn_keystore_public_key_digest_length(keystore), /*datetime*/NULL, /*type*/CCN_CONTENT_DATA, /*freshness*/ expire, finalblockid, keylocator); /* Put the keylocator in the first block only. */ ccn_charbuf_destroy(&keylocator); if (res < 0) { fprintf(stderr, "Failed to create signed_info (res == %d)\n", res); exit(1); } name->length = 0; ccn_charbuf_append(name, root->buf, root->length); temp->length = 0; ccn_charbuf_putf(temp, "%d", i); ccn_name_append(name, temp->buf, temp->length); temp->length = 0; ccn_charbuf_append(temp, buf, read_res); temp->length = 0; res = ccn_encode_ContentObject(temp, name, signed_info, buf, read_res, NULL, ccn_keystore_private_key(keystore)); if (res != 0) { fprintf(stderr, "Failed to encode ContentObject (res == %d)\n", res); exit(1); } if (i == 0) { /* Finish check for old content */ if (mydata.content_received == 0) ccn_run(ccn, 100); if (mydata.content_received > 0) { fprintf(stderr, "%s: name is in use: %s\n", progname, argv[0]); exit(1); } mydata.outstanding++; /* the first one is free... */ } res = ccn_put(ccn, temp->buf, temp->length); if (res < 0) { fprintf(stderr, "ccn_put failed (res == %d)\n", res); exit(1); } if (read_res < blocksize) break; if (mydata.outstanding > 0) mydata.outstanding--; else res = 10; res = ccn_run(ccn, res * 100); if (res < 0) { status = 1; break; } } free(buf); buf = NULL; ccn_charbuf_destroy(&root); ccn_charbuf_destroy(&name); ccn_charbuf_destroy(&temp); ccn_charbuf_destroy(&signed_info); ccn_charbuf_destroy(&finalblockid); ccn_keystore_destroy(&keystore); ccn_destroy(&ccn); exit(status); }
int sign_content_with_user_defined_keystore(struct ccn_charbuf *content_name, struct ccn_charbuf *resultbuf, const void *data, size_t data_size, char *keystore_path, char *keystore_passphrase, char *key_repo_name, char *site_name, char *router_name){ if ( nlsr->debugging ) printf("sign_content_with_user_defined_keystore called\n"); int res; struct ccn_charbuf * pubid_out=ccn_charbuf_create(); struct ccn_charbuf * keyname; struct ccn_keystore *keystore = NULL; keystore=ccn_keystore_create(); res=ccn_keystore_init(keystore, keystore_path,keystore_passphrase ); if ( res < 0 ){ if ( nlsr->debugging ) printf("Error in initiating keystore :(\n"); ccn_keystore_destroy(&keystore); return -1; } res=ccn_load_private_key (nlsr->ccn, keystore_path, keystore_passphrase, pubid_out); if(res < 0 ){ if ( nlsr->debugging ) printf("Error in loading keystore :( \n"); ccn_charbuf_destroy(&pubid_out); return -1; } char *baseuri=(char *)calloc(strlen(key_repo_name)+strlen(site_name)+ strlen(router_name)+strlen("/%C1.R.N.Start")+5,sizeof(char)); memcpy(baseuri,key_repo_name,strlen(key_repo_name)+1); if ( site_name[0] != '/') memcpy(baseuri+strlen(baseuri),"/",1); memcpy(baseuri+strlen(baseuri),site_name,strlen(site_name)+1); memcpy(baseuri+strlen(baseuri),"/%C1.R.N.Start",strlen("/%C1.R.N.Start")); memcpy(baseuri+strlen(baseuri),router_name,strlen(router_name)+1); baseuri[strlen(baseuri)]='\0'; keyname=ccn_charbuf_create(); if(keyname == NULL ){ ccn_charbuf_destroy(&pubid_out); free(baseuri); return -1; } ccn_name_from_uri(keyname,baseuri); if ( res < 0 ){ if ( nlsr->debugging ) printf("Bad URI format: %s\n",baseuri); ccn_charbuf_destroy(&pubid_out); ccn_charbuf_destroy(&keyname); free(baseuri); return -1; } ccn_name_append_str(keyname,"nlsr"); struct ccn_charbuf *keyid = ccn_charbuf_create(); ccn_charbuf_append_value(keyid, CCN_MARKER_CONTROL, 1); ccn_charbuf_append_string(keyid, ".M.K"); ccn_charbuf_append_value(keyid, 0, 1); ccn_charbuf_append_charbuf(keyid, pubid_out); ccn_name_append(keyname, keyid->buf, keyid->length); struct ccn_charbuf *uri = ccn_charbuf_create(); ccn_uri_append(uri, keyname->buf, keyname->length, 0); if ( nlsr->debugging ) printf("Key Name Included when processing content: %s\n", ccn_charbuf_as_string(uri)); ccn_charbuf_destroy(&uri); struct ccn_signing_params sp = CCN_SIGNING_PARAMS_INIT; sp.type = CCN_CONTENT_DATA; sp.template_ccnb = ccn_charbuf_create(); ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_SignedInfo, CCN_DTAG); ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_KeyLocator, CCN_DTAG); ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_KeyName, CCN_DTAG); ccn_charbuf_append(sp.template_ccnb, keyname->buf, keyname->length); ccn_charbuf_append_closer(sp.template_ccnb); // KeyName closer ccn_charbuf_append_closer(sp.template_ccnb); // KeyLocator closer ccn_charbuf_append_closer(sp.template_ccnb); // SignedInfo closer sp.sp_flags |= CCN_SP_TEMPL_KEY_LOCATOR; sp.sp_flags |= CCN_SP_FINAL_BLOCK; sp.freshness = 60; if (pubid_out->length != sizeof(sp.pubid)){ if ( nlsr->debugging ) printf("Size of pubid and sp.pubid is not equal"); ccn_charbuf_destroy(&keyname); ccn_charbuf_destroy(&pubid_out); free(baseuri); return -1; } memcpy(sp.pubid, pubid_out->buf, pubid_out->length); res=ccn_sign_content(nlsr->ccn,resultbuf,content_name,&sp,data,data_size); if( res < 0 ){ if ( nlsr->debugging ) printf("Content signing error \n"); ccn_charbuf_destroy(&sp.template_ccnb); ccn_charbuf_destroy(&keyid); ccn_charbuf_destroy(&keyname); ccn_charbuf_destroy(&pubid_out); free(baseuri); return -1; } ccn_charbuf_destroy(&sp.template_ccnb); ccn_charbuf_destroy(&keyid); ccn_charbuf_destroy(&keyname); ccn_charbuf_destroy(&pubid_out); free(baseuri); return 0; }