struct ccn_proxy * ccn_proxy_init(const char *filter_uri, const char *prefix_uri) { struct ccn_proxy *proxy = calloc(1, sizeof(struct ccn_proxy)); struct ccn_buf_decoder decoder; struct ccn_buf_decoder *d = &decoder; int res; DEBUG_PRINT("IN %d %s\n", __LINE__, __func__); /* Convert URI to name this proxy is responsible for */ proxy->prefix = ccn_charbuf_create(); res = ccn_name_from_uri(proxy->prefix, prefix_uri); if (res < 0) { DEBUG_PRINT("ABORT %d %s bad ccn URI: %s\n", __LINE__, __func__, prefix_uri); abort(); } d = ccn_buf_decoder_start(d, proxy->prefix->buf, proxy->prefix->length); proxy->prefix_comps = ccn_indexbuf_create(); proxy->prefix_ncomps = ccn_parse_Name(d, proxy->prefix_comps); proxy->filter = ccn_charbuf_create(); res = ccn_name_from_uri(proxy->filter, filter_uri); if (res < 0) { DEBUG_PRINT("ABORT %d %s bad ccn URI: %s\n", __LINE__, __func__, filter_uri); abort(); } /* Initialization should be done by ccn_proxy_connect() */ proxy->handle_name = ccn_charbuf_create(); ccn_charbuf_append_string(proxy->handle_name, "in/outb"); DEBUG_PRINT("OUT %d %s\n", __LINE__, __func__); return(proxy); }
/** * Append a non-negative integer as a UDATA. * @param c is the buffer to append to. * @param nni is a non-negative value. * @returns 0 for success or -1 for error. */ int ccnb_append_number(struct ccn_charbuf *c, int nni) { char nnistring[40]; int nnistringlen; int res; if (nni < 0) return(-1); nnistringlen = snprintf(nnistring, sizeof(nnistring), "%d", nni); if (nnistringlen >= sizeof(nnistring)) return(-1); res = ccn_charbuf_append_tt(c, nnistringlen, CCN_UDATA); res |= ccn_charbuf_append_string(c, nnistring); return(res); }
/** * should probably return a new cob, rather than reusing one. * should publish link as: * CCNRID_POLICY_URI("ccnx:/%C1.M.S.localhost/%C1.M.SRV/repository/POLICY)/%C1.M.K--pubid--/--version--/%00 * should have key locator which is the key name of the repository */ PUBLIC struct ccn_charbuf * ccnr_init_policy_link_cob(struct ccnr_handle *ccnr, struct ccn *h, struct ccn_charbuf *targetname) { struct ccn_signing_params sp = CCN_SIGNING_PARAMS_INIT; struct ccn_charbuf *name = ccn_charbuf_create(); struct ccn_charbuf *pubid = ccn_charbuf_create(); struct ccn_charbuf *pubkey = ccn_charbuf_create(); struct ccn_charbuf *keyid = ccn_charbuf_create(); struct ccn_charbuf *content = ccn_charbuf_create(); struct ccn_charbuf *cob = ccn_charbuf_create(); struct ccn_charbuf *answer = NULL; int res; res = ccn_get_public_key(h, NULL, pubid, pubkey); if (res < 0) goto Bail; if (ccn_name_from_uri(name, CCNRID_POLICY_URI) < 0) goto Bail; res |= ccn_charbuf_append_value(keyid, CCN_MARKER_CONTROL, 1); res |= ccn_charbuf_append_string(keyid, ".M.K"); res |= ccn_charbuf_append_value(keyid, 0, 1); res |= ccn_charbuf_append_charbuf(keyid, pubid); res |= ccn_name_append(name, keyid->buf, keyid->length); res |= ccn_create_version(h, name, CCN_V_NOW, 0, 0); if (ccn_name_from_uri(name, "%00") < 0) goto Bail; sp.sp_flags |= CCN_SP_FINAL_BLOCK; sp.type = CCN_CONTENT_LINK; res |= ccnb_append_Link(content, targetname, "Repository Policy", NULL); if (res != 0) goto Bail; res |= ccn_sign_content(h, cob, name, &sp, content->buf, content->length); if (res != 0) goto Bail; answer = cob; cob = NULL; Bail: ccn_charbuf_destroy(&name); ccn_charbuf_destroy(&pubid); ccn_charbuf_destroy(&pubkey); ccn_charbuf_destroy(&keyid); ccn_charbuf_destroy(&content); ccn_charbuf_destroy(&cob); return (answer); }
static struct ccn_charbuf * ccnr_init_service_ccnb(struct ccnr_handle *ccnr, struct ccn *h, const char *baseuri, int freshness) { struct ccn_signing_params sp = CCN_SIGNING_PARAMS_INIT; struct ccn_charbuf *name = ccn_charbuf_create(); struct ccn_charbuf *pubid = ccn_charbuf_create(); struct ccn_charbuf *pubkey = ccn_charbuf_create(); struct ccn_charbuf *keyid = ccn_charbuf_create(); struct ccn_charbuf *cob = ccn_charbuf_create(); int res; res = ccn_get_public_key(h, NULL, pubid, pubkey); if (res < 0) abort(); ccn_name_from_uri(name, baseuri); 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); ccn_name_append(name, keyid->buf, keyid->length); ccn_create_version(h, name, 0, ccnr->starttime, ccnr->starttime_usec * 1000); sp.template_ccnb = ccn_charbuf_create(); ccnb_element_begin(sp.template_ccnb, CCN_DTAG_SignedInfo); ccnb_element_begin(sp.template_ccnb, CCN_DTAG_KeyLocator); ccnb_element_begin(sp.template_ccnb, CCN_DTAG_KeyName); ccn_charbuf_append_charbuf(sp.template_ccnb, name); ccnb_element_end(sp.template_ccnb); // ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_PublisherPublicKeyDigest, // CCN_DTAG); // ccn_charbuf_append_charbuf(sp.template_ccnb, pubid); // ccnb_element_end(sp.template_ccnb); ccnb_element_end(sp.template_ccnb); ccnb_element_end(sp.template_ccnb); sp.sp_flags |= CCN_SP_TEMPL_KEY_LOCATOR; ccn_name_from_uri(name, "%00"); sp.sp_flags |= CCN_SP_FINAL_BLOCK; sp.type = CCN_CONTENT_KEY; sp.freshness = freshness; res = ccn_sign_content(h, cob, name, &sp, pubkey->buf, pubkey->length); if (res != 0) abort(); ccn_charbuf_destroy(&name); ccn_charbuf_destroy(&pubid); ccn_charbuf_destroy(&pubkey); ccn_charbuf_destroy(&keyid); ccn_charbuf_destroy(&sp.template_ccnb); return(cob); }
int ccn_uri_append(struct ccn_charbuf *c, const unsigned char *ccnb, size_t size, int includescheme) { int ncomp = 0; const unsigned char *comp = NULL; size_t compsize = 0; struct ccn_buf_decoder decoder; struct ccn_buf_decoder *d = ccn_buf_decoder_start(&decoder, ccnb, size); if (ccn_buf_match_dtag(d, CCN_DTAG_Interest) || ccn_buf_match_dtag(d, CCN_DTAG_ContentObject)) { ccn_buf_advance(d); if (ccn_buf_match_dtag(d, CCN_DTAG_Signature)) ccn_buf_advance_past_element(d); } if (!ccn_buf_match_dtag(d, CCN_DTAG_Name)) return(-1); if (includescheme) ccn_charbuf_append_string(c, "ccnx:"); ccn_buf_advance(d); while (ccn_buf_match_dtag(d, CCN_DTAG_Component)) { ccn_buf_advance(d); compsize = 0; if (ccn_buf_match_blob(d, &comp, &compsize)) ccn_buf_advance(d); ccn_buf_check_close(d); if (d->decoder.state < 0) return(d->decoder.state); ncomp += 1; ccn_charbuf_append(c, "/", 1); ccn_uri_append_percentescaped(c, comp, compsize); } ccn_buf_check_close(d); if (d->decoder.state < 0) return (d->decoder.state); if (ncomp == 0) ccn_charbuf_append(c, "/", 1); return(ncomp); }
static int ccn_encode_Signature(struct ccn_charbuf *buf, const char *digest_algorithm, const void *witness, size_t witness_size, const struct ccn_signature *signature, size_t signature_size) { int res = 0; if (signature == NULL) return(-1); res |= ccn_charbuf_append_tt(buf, CCN_DTAG_Signature, CCN_DTAG); if (digest_algorithm != NULL) { res |= ccn_charbuf_append_tt(buf, CCN_DTAG_DigestAlgorithm, CCN_DTAG); res |= ccn_charbuf_append_tt(buf, strlen(digest_algorithm), CCN_UDATA); res |= ccn_charbuf_append_string(buf, digest_algorithm); res |= ccn_charbuf_append_closer(buf); } if (witness != NULL) { res |= ccn_charbuf_append_tt(buf, CCN_DTAG_Witness, CCN_DTAG); res |= ccn_charbuf_append_tt(buf, witness_size, CCN_BLOB); res |= ccn_charbuf_append(buf, witness, witness_size); res |= ccn_charbuf_append_closer(buf); } res |= ccn_charbuf_append_tt(buf, CCN_DTAG_SignatureBits, CCN_DTAG); res |= ccn_charbuf_append_tt(buf, signature_size, CCN_BLOB); res |= ccn_charbuf_append(buf, signature, signature_size); res |= ccn_charbuf_append_closer(buf); res |= ccn_charbuf_append_closer(buf); return(res == 0 ? 0 : -1); }
/** * Append a representation of a Link to a charbuf. * @param buf is the output buffer where encoded link is written. * @param name is the ccnb-encoded name from ccn_name_init and friends. * @param label is a UTF-8 string in a ccn_charbuf. * @param linkAuthenticator is the ccnb-encoded LinkAuthenticator. * @returns 0 for success or -1 for error. */ int ccnb_append_Link(struct ccn_charbuf *buf, const struct ccn_charbuf *name, const char *label, const struct ccn_charbuf *linkAuthenticator ) { int res = 0; res |= ccn_charbuf_append_tt(buf, CCN_DTAG_Link, CCN_DTAG); res |= ccn_charbuf_append_charbuf(buf, name); if (label != NULL) { res |= ccn_charbuf_append_tt(buf, CCN_DTAG_Label, CCN_DTAG); res |= ccn_charbuf_append_tt(buf, strlen(label), CCN_UDATA); res |= ccn_charbuf_append_string(buf, label); res |= ccn_charbuf_append_closer(buf); } if (linkAuthenticator != NULL) { res |= ccn_charbuf_append_charbuf(buf, linkAuthenticator); } res |= ccn_charbuf_append_closer(buf); return(res == 0 ? 0 : -1); }
int ccndc_srv(struct ccndc_data *self, const unsigned char *domain, size_t domain_size) { char *proto = NULL; char *host = NULL; int port = 0; char port_str[10]; struct ccn_charbuf *uri; struct ccn_charbuf *uri_auto; struct ccn_face_instance *face; struct ccn_face_instance *newface; struct ccn_forwarding_entry *prefix; struct ccn_forwarding_entry *prefix_auto; int res; res = ccndc_query_srv(domain, domain_size, &host, &port, &proto); if (res < 0) { return -1; } uri = ccn_charbuf_create(); ccn_charbuf_append_string(uri, "ccnx:/"); if (domain_size != 0) { ccn_uri_append_percentescaped(uri, domain, domain_size); } snprintf (port_str, sizeof(port_str), "%d", port); /* now process the results */ /* pflhead, lineno=0, "add" "ccnx:/asdfasdf.com/" "tcp|udp", host, portstring, NULL NULL NULL */ ccndc_note(__LINE__, " >>> trying: add %s %s %s %s <<<\n", ccn_charbuf_as_string(uri), proto, host, port_str); face = parse_ccn_face_instance(self, proto, host, port_str, NULL, NULL, (~0U) >> 1); prefix = parse_ccn_forwarding_entry(self, ccn_charbuf_as_string(uri), NULL, self->lifetime); if (face == NULL || prefix == NULL) { res = -1; goto Cleanup; } // crazy operation // First. "Create" face, which will do nothing if face already exists // Second. Destroy the face // Third. Create face for real newface = ccndc_do_face_action(self, "newface", face); if (newface == NULL) { ccndc_warn(__LINE__, "Cannot create/lookup face"); res = -1; goto Cleanup; } face->faceid = newface->faceid; ccn_face_instance_destroy(&newface); newface = ccndc_do_face_action(self, "destroyface", face); if (newface == NULL) { ccndc_warn(__LINE__, "Cannot destroy face"); } else { ccn_face_instance_destroy(&newface); } newface = ccndc_do_face_action(self, "newface", face); if (newface == NULL) { ccndc_warn(__LINE__, "Cannot create/lookup face"); res = -1; goto Cleanup; } prefix->faceid = newface->faceid; ccn_face_instance_destroy(&newface); res = ccndc_do_prefix_action(self, "prefixreg", prefix); if (res < 0) { ccndc_warn(__LINE__, "Cannot register prefix [%s]\n", ccn_charbuf_as_string(uri)); } uri_auto = ccn_charbuf_create(); ccn_charbuf_append_string(uri_auto, "ccnx:/autoconf-route"); prefix_auto = parse_ccn_forwarding_entry(self, ccn_charbuf_as_string(uri_auto), NULL, self->lifetime); if (prefix_auto == NULL) { res = -1; goto Cleanup; } prefix_auto->faceid = prefix->faceid; res = ccndc_do_prefix_action(self, "prefixreg", prefix_auto); if (res < 0) { ccndc_warn(__LINE__, "Cannot register prefix_auto [%s]\n", ccn_charbuf_as_string(uri_auto)); } Cleanup: free(host); ccn_charbuf_destroy(&uri); ccn_charbuf_destroy(&uri_auto); ccn_face_instance_destroy(&face); ccn_forwarding_entry_destroy(&prefix); ccn_forwarding_entry_destroy(&prefix_auto); return res; }
int ccnr_init_repo_keystore(struct ccnr_handle *ccnr, struct ccn *h) { struct ccn_charbuf *temp = NULL; struct ccn_charbuf *culprit = NULL; struct stat statbuf; int res = -1; char *keystore_path = NULL; struct ccn_signing_params sp = CCN_SIGNING_PARAMS_INIT; temp = ccn_charbuf_create(); culprit = temp; ccn_charbuf_putf(temp, "%s/", ccnr->directory); res = stat(ccn_charbuf_as_string(temp), &statbuf); if (res == -1) goto Finish; if ((statbuf.st_mode & S_IFDIR) == 0) { res = -1; errno = ENOTDIR; goto Finish; } ccn_charbuf_putf(temp, "ccnx_repository_keystore"); keystore_path = strdup(ccn_charbuf_as_string(temp)); res = stat(keystore_path, &statbuf); if (res == 0 && h != NULL) res = ccn_load_default_key(h, keystore_path, CCNR_KEYSTORE_PASS); if (res >= 0) { culprit = NULL; goto Finish; } /* No stored keystore that we can access. Create one if we can.*/ res = ccn_keystore_file_init(keystore_path, CCNR_KEYSTORE_PASS, "Repository", 0, 0); if (res != 0) { res = -1; goto Finish; } if (CCNSHOULDLOG(ccnr, keystore, CCNL_WARNING)) ccnr_msg(ccnr, "New repository private key saved in %s", keystore_path); if (h != NULL) res = ccn_load_default_key(h, keystore_path, CCNR_KEYSTORE_PASS); Finish: if (res >= 0 && h != NULL) res = ccn_chk_signing_params(h, NULL, &sp, NULL, NULL, NULL, NULL); if (res >= 0 && h != NULL) { memcpy(ccnr->ccnr_id, sp.pubid, sizeof(ccnr->ccnr_id)); if (ccnr->ccnr_keyid == NULL) ccnr->ccnr_keyid = ccn_charbuf_create(); else ccnr->ccnr_keyid->length = 0; ccn_charbuf_append_value(ccnr->ccnr_keyid, CCN_MARKER_CONTROL, 1); ccn_charbuf_append_string(ccnr->ccnr_keyid, ".M.K"); ccn_charbuf_append_value(ccnr->ccnr_keyid, 0, 1); ccn_charbuf_append(ccnr->ccnr_keyid, ccnr->ccnr_id, sizeof(ccnr->ccnr_id)); } if (res < 0) { ccnr->running = -1; /* Make note of init failure */ if (culprit != NULL) ccnr_msg(ccnr, "Error accessing keystore - %s: %s\n", strerror(errno), ccn_charbuf_as_string(temp)); } ccn_charbuf_destroy(&temp); if (keystore_path != NULL) free(keystore_path); return(res); }
static int dissect_ccn_interest(const unsigned char *ccnb, size_t ccnb_size, tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) { proto_tree *name_tree; proto_tree *exclude_tree; proto_item *titem; struct ccn_parsed_interest interest; struct ccn_parsed_interest *pi = &interest; struct ccn_buf_decoder decoder; struct ccn_buf_decoder *d; const unsigned char *bloom; size_t bloom_size = 0; struct ccn_charbuf *c; struct ccn_indexbuf *comps; const unsigned char *comp; size_t comp_size; const unsigned char *blob; size_t blob_size; ssize_t l; unsigned int i; double lifetime; int res; comps = ccn_indexbuf_create(); res = ccn_parse_interest(ccnb, ccnb_size, pi, comps); if (res < 0) return (res); /* Name */ l = pi->offset[CCN_PI_E_Name] - pi->offset[CCN_PI_B_Name]; c = ccn_charbuf_create(); ccn_uri_append(c, ccnb, ccnb_size, 1); titem = proto_tree_add_string(tree, hf_ccn_name, tvb, pi->offset[CCN_PI_B_Name], l, ccn_charbuf_as_string(c)); name_tree = proto_item_add_subtree(titem, ett_name); ccn_charbuf_destroy(&c); for (i = 0; i < comps->n - 1; i++) { res = ccn_name_comp_get(ccnb, comps, i, &comp, &comp_size); titem = proto_tree_add_item(name_tree, hf_ccn_name_components, tvb, comp - ccnb, comp_size, FALSE); } /* MinSuffixComponents */ l = pi->offset[CCN_PI_E_MinSuffixComponents] - pi->offset[CCN_PI_B_MinSuffixComponents]; if (l > 0) { i = pi->min_suffix_comps; titem = proto_tree_add_uint(tree, hf_ccn_minsuffixcomponents, tvb, pi->offset[CCN_PI_B_MinSuffixComponents], l, i); } /* MaxSuffixComponents */ l = pi->offset[CCN_PI_E_MaxSuffixComponents] - pi->offset[CCN_PI_B_MaxSuffixComponents]; if (l > 0) { i = pi->max_suffix_comps; titem = proto_tree_add_uint(tree, hf_ccn_maxsuffixcomponents, tvb, pi->offset[CCN_PI_B_MaxSuffixComponents], l, i); } /* PublisherPublicKeyDigest */ /* Exclude */ l = pi->offset[CCN_PI_E_Exclude] - pi->offset[CCN_PI_B_Exclude]; if (l > 0) { c = ccn_charbuf_create(); d = ccn_buf_decoder_start(&decoder, ccnb + pi->offset[CCN_PI_B_Exclude], l); if (!ccn_buf_match_dtag(d, CCN_DTAG_Exclude)) { ccn_charbuf_destroy(&c); return(-1); } ccn_charbuf_append_string(c, "Exclude: "); ccn_buf_advance(d); if (ccn_buf_match_dtag(d, CCN_DTAG_Any)) { ccn_buf_advance(d); ccn_charbuf_append_string(c, "* "); ccn_buf_check_close(d); } else if (ccn_buf_match_dtag(d, CCN_DTAG_Bloom)) { ccn_buf_advance(d); if (ccn_buf_match_blob(d, &bloom, &bloom_size)) ccn_buf_advance(d); ccn_charbuf_append_string(c, "? "); ccn_buf_check_close(d); } while (ccn_buf_match_dtag(d, CCN_DTAG_Component)) { ccn_buf_advance(d); comp_size = 0; if (ccn_buf_match_blob(d, &comp, &comp_size)) ccn_buf_advance(d); ccn_uri_append_percentescaped(c, comp, comp_size); ccn_charbuf_append_string(c, " "); ccn_buf_check_close(d); if (ccn_buf_match_dtag(d, CCN_DTAG_Any)) { ccn_buf_advance(d); ccn_charbuf_append_string(c, "* "); ccn_buf_check_close(d); } else if (ccn_buf_match_dtag(d, CCN_DTAG_Bloom)) { ccn_buf_advance(d); if (ccn_buf_match_blob(d, &bloom, &bloom_size)) ccn_buf_advance(d); ccn_charbuf_append_string(c, "? "); ccn_buf_check_close(d); } } titem = proto_tree_add_text(tree, tvb, pi->offset[CCN_PI_B_Exclude], l, "%s", ccn_charbuf_as_string(c)); exclude_tree = proto_item_add_subtree(titem, ett_exclude); ccn_charbuf_destroy(&c); } /* ChildSelector */ l = pi->offset[CCN_PI_E_ChildSelector] - pi->offset[CCN_PI_B_ChildSelector]; if (l > 0) { i = pi->orderpref; titem = proto_tree_add_uint(tree, hf_ccn_childselector, tvb, pi->offset[CCN_PI_B_ChildSelector], l, i); proto_item_append_text(titem, ", %s", val_to_str(i & 1, VALS(childselectordirection_vals), "")); } /* AnswerOriginKind */ l = pi->offset[CCN_PI_E_AnswerOriginKind] - pi->offset[CCN_PI_B_AnswerOriginKind]; if (l > 0) { i = pi->answerfrom; titem = proto_tree_add_uint(tree, hf_ccn_answeroriginkind, tvb, pi->offset[CCN_PI_B_AnswerOriginKind], l, i); } /* Scope */ l = pi->offset[CCN_PI_E_Scope] - pi->offset[CCN_PI_B_Scope]; if (l > 0) { i = pi->scope; titem = proto_tree_add_uint(tree, hf_ccn_scope, tvb, pi->offset[CCN_PI_B_Scope], l, i); } /* InterestLifetime */ l = pi->offset[CCN_PI_E_InterestLifetime] - pi->offset[CCN_PI_B_InterestLifetime]; if (l > 0) { i = ccn_ref_tagged_BLOB(CCN_DTAG_InterestLifetime, ccnb, pi->offset[CCN_PI_B_InterestLifetime], pi->offset[CCN_PI_E_InterestLifetime], &blob, &blob_size); lifetime = 0.0; for (i = 0; i < blob_size; i++) lifetime = lifetime * 256.0 + (double)blob[i]; lifetime /= 4096.0; titem = proto_tree_add_double(tree, hf_ccn_interestlifetime, tvb, blob - ccnb, blob_size, lifetime); } /* Nonce */ l = pi->offset[CCN_PI_E_Nonce] - pi->offset[CCN_PI_B_Nonce]; if (l > 0) { i = ccn_ref_tagged_BLOB(CCN_DTAG_Nonce, ccnb, pi->offset[CCN_PI_B_Nonce], pi->offset[CCN_PI_E_Nonce], &blob, &blob_size); if (check_col(pinfo->cinfo, COL_INFO)) { col_append_str(pinfo->cinfo, COL_INFO, ", <"); for (i = 0; i < blob_size; i++) col_append_fstr(pinfo->cinfo, COL_INFO, "%02x", blob[i]); col_append_str(pinfo->cinfo, COL_INFO, ">"); } titem = proto_tree_add_item(tree, hf_ccn_nonce, tvb, blob - ccnb, blob_size, FALSE); } return (1); }
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; }
int main(int argc, char **argv) { int res; int opt; char *dir = NULL; struct ccn_charbuf *filename = NULL; int force = 0; const char *password = NULL; char *name = ".ccnx_keystore"; char *username; int fullname = 0; char *key = NULL; unsigned char keybuf[CCN_SECRET_KEY_LENGTH/8]; int copylen = CCN_SECRET_KEY_LENGTH/8; struct stat statbuf; char *digest = NULL; int dirset = 0; int read_mode = 0; struct ccn_keystore *keystore = ccn_aes_keystore_create(); EVP_PKEY *sk; while ((opt = getopt(argc, argv, "hfk:p:d:ro:")) != -1) { switch (opt) { case 'f': force = 1; break; case 'k': key = optarg; break; case 'p': password = optarg; break; case 'o': dir = optarg; dirset = 1; break; case 'd': digest = optarg; break; case 'r': read_mode = 1; break; case 'h': default: usage(argv[0]); exit(1); } } username = argv[optind]; if (username != NULL) { name = username; fullname = 1; } if (dir == NULL){ dir = getenv("HOME"); } res = stat(dir, &statbuf); if (res == -1) { perror(dir); exit(1); } else if ((statbuf.st_mode & S_IFMT) != S_IFDIR) { errno = ENOTDIR; perror(dir); exit(1); } filename = ccn_charbuf_create(); if (!dirset) { ccn_charbuf_putf(filename, "%s/.ccnx", dir); res = stat(ccn_charbuf_as_string(filename), &statbuf); if (res == -1) { res = mkdir(ccn_charbuf_as_string(filename), 0700); if (res != 0) { perror(ccn_charbuf_as_string(filename)); exit(1); } } } else ccn_charbuf_append_string(filename, dir); if (password == NULL) password = ccn_get_password(); ccn_charbuf_append_string(filename, "/"); ccn_charbuf_append_string(filename, name); if (key == NULL) { ccn_generate_symmetric_key(keybuf, CCN_SECRET_KEY_LENGTH); } if (!fullname) { if (read_mode) { ccn_charbuf_append_string(filename, "-"); ccn_charbuf_append_string(filename, digest); } else { if (key != NULL) { memset(keybuf, 0, sizeof(keybuf)); if (strlen(key) < sizeof(keybuf)) copylen = strlen(key); memcpy(keybuf, key, copylen); } ccn_create_aes_filename_from_key(filename, keybuf, CCN_SECRET_KEY_LENGTH); } } if (read_mode) { res = ccn_aes_keystore_init(keystore, ccn_charbuf_as_string(filename), password); } else { if (!force) { res = stat(ccn_charbuf_as_string(filename), &statbuf); if (res != -1) { errno=EEXIST; perror(ccn_charbuf_as_string(filename)); exit(1); } } res = ccn_aes_keystore_file_init(ccn_charbuf_as_string(filename), password, keybuf, CCN_SECRET_KEY_LENGTH); } if (res != 0) { if (errno != 0) perror(ccn_charbuf_as_string(filename)); else fprintf(stderr, "ccn_keystore_file_init: invalid argument\n"); exit(1); } if (read_mode) { unsigned char *key_data; int i; sk = (EVP_PKEY *) ccn_keystore_key(keystore); key_data = ASN1_STRING_data(EVP_PKEY_get0(sk)); printf("Retrieved key: 0x"); for (i = 0; i < ccn_keystore_key_digest_length(keystore); i++) printf("%x", key_data[i]); printf("\n"); } else { printf("Created keystore: %s\n", ccn_charbuf_as_string(filename)); } return(0); }