int main(int argc, char** argv) { int res = 0; struct ccn* ccn_pub; struct ccn* ccn_rec; // Will hold the public/private key used for signing struct ccn_pkey* public_key = NULL; struct ccn_pkey* private_key = NULL; int complete=0; int outstanding_interests=0; // We need two ccn handles because the same handle cannot be used // to answer interests it issues. // ccn_pub = ccn_create(); if (ccn_connect(ccn_pub, NULL) == -1) { fprintf(stderr, "Could not connect to ccnd"); return(1); } ccn_rec = ccn_create(); if (ccn_connect(ccn_rec, NULL) == -1) { fprintf(stderr, "Could not connect to ccnd"); return(1); } // Closure to handle upcalls struct ccn_closure *cl = NULL; cl = (struct ccn_closure*) calloc(1, sizeof(struct ccn_closure)); cl->p = &packet_handler; handler_data h_data = { &complete, &outstanding_interests, &public_key, &ccn_pub, &ccn_rec}; cl->data = &h_data; // Setup our one test name without signature // The nonce here is just a random string, to avoid content store caching struct ccn_charbuf* name; name = ccn_charbuf_create(); ccn_name_from_uri(name, TEST_URI); ccn_name_append_nonce(name); fprintf(stderr, "Our name: %s/<nonce>\n", TEST_URI); // Set up a filter for interests in that name res = ccn_set_interest_filter(ccn_pub, name, cl); if (res < 0) { fprintf(stderr, "Failed to register interest (res == %d)\n", res); return(1); } // Get our default keys -- Why do we have to do all this work?? // Borrowed from ccn_client.c struct ccn_signing_params name_sp = CCN_SIGNING_PARAMS_INIT; struct ccn_signing_params p = CCN_SIGNING_PARAMS_INIT; struct ccn_keystore *keystore = NULL; struct ccn_charbuf *timestamp = NULL; struct ccn_charbuf *finalblockid = NULL; struct ccn_charbuf *keylocator = NULL; unsigned char* public_key_digest = NULL; size_t public_key_digest_length = 0; res = ccn_chk_signing_params(ccn_pub, &name_sp, &p, ×tamp, &finalblockid, &keylocator); if (res < 0) return(res); // For this test, use our default signing keys get_default_keys(ccn_pub, &p, &keystore, &public_key, &public_key_digest, &public_key_digest_length, &private_key); // We'll need a KeyLocator for our ContentObject // So continue borrowed code /* Construct a key locator containing the key itself */ build_keylocator_from_key(&keylocator, public_key); // And a SignedInfo too struct ccn_charbuf *signed_info = ccn_charbuf_create(); res = ccn_signed_info_create(signed_info, public_key_digest, public_key_digest_length, timestamp, p.type, p.freshness, 0, /* FinalBlockID is optional */ keylocator); // * Test using interests for a name that has one more component than our registered prefix // This is more representative of real apps... // 20-May-2011 ccn_name_append_str(name, "some stuff in a name component"); ccn_name_append_nonce(name); ccn_name_append_nonce(name); // Sign the interest struct ccn_charbuf *name_signed = ccn_charbuf_create(); sign_interest(name_signed, name, signed_info, NULL /* default digest alg */, private_key); // Express the signed interest from a different ccn handle so we get the packet res = ccn_express_interest(ccn_rec, name_signed, cl, NULL); // TODO: AnswerOriginKind could limit to signed interest? outstanding_interests++; // Express an interest with an incorrect namespace struct ccn_charbuf *name_signed_copy = ccn_charbuf_create(); ccn_charbuf_append_charbuf(name_signed_copy, name_signed); size_t k = name->length + 10; // Seek into the namespace part of the buffer name_signed_copy->buf[k] = name_signed_copy->buf[k] + 1; res = ccn_express_interest(ccn_rec, name_signed_copy, cl, NULL); // TODO: AnswerOriginKind could limit to signed interest? outstanding_interests++; // Express an interest with bogus signature name_signed_copy = ccn_charbuf_create(); ccn_charbuf_append_charbuf(name_signed_copy, name_signed); k = name->length + 30; // Seek into the signature part of the buffer name_signed_copy->buf[k] = name_signed_copy->buf[k] + 1; res = ccn_express_interest(ccn_rec, name_signed_copy, cl, NULL); // TODO: AnswerOriginKind could limit to signed interest? outstanding_interests++; if (res < 0) { fprintf(stderr, "Error expressing interest (res == %d)\n", res); } cl = NULL; // freed by ccn? while(!complete && outstanding_interests>0) { // Not sure how to handle two ccn_runs? ccn_run(ccn_rec, 100); /* stop if we run dry for .1 sec */ ccn_run(ccn_pub, 100); /* stop if we run dry for .1 sec */ fflush(stdout); } ccn_charbuf_destroy(×tamp); ccn_charbuf_destroy(&keylocator); ccn_charbuf_destroy(&finalblockid); ccn_charbuf_destroy(&signed_info); ccn_charbuf_destroy(&name); ccn_charbuf_destroy(&name_signed); ccn_charbuf_destroy(&name_signed_copy); ccn_destroy(&ccn_pub); ccn_destroy(&ccn_rec); fflush(stderr); return(0); }
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); }
int main(void) { int res =0; struct ccn *ccn = NULL; struct ccn_charbuf *name = NULL; struct ccn_closure *incoming; //char URI[1024] = ""; name = ccn_charbuf_create(); //char *URI; //URI = (char *) malloc(1024*sizeof(char)); /* char URI[1024] = ""; printf("Please enter your light control interest command:"); scanf("%s", URI); printf("The light control interest command is: %s\n", URI); */ res = ccn_name_from_uri(name, URI); if (res < 0) { fprintf(stderr, "bad ccn URI: %s\n", URI); exit(1); } else { fprintf(stdout, "ccn URI ok: %s\n", URI); } //from test_signed_interest.c struct ccn* ccn_pub; struct ccn* ccn_rec; // Will hold the public/private key used for signing struct ccn_pkey* public_key = NULL; struct ccn_pkey* private_key = NULL; int complete=0; int outstanding_interests=0; // We need two ccn handles because the same handle cannot be used // to answer interests it issues. // ccn_pub = ccn_create(); if (ccn_connect(ccn_pub, NULL) == -1) { fprintf(stderr, "Could not connect to ccnd"); return(1); } ccn_rec = ccn_create(); if (ccn_connect(ccn_rec, NULL) == -1) { fprintf(stderr, "Could not connect to ccnd"); return(1); } // using original callback instead of struct in test_signed_interest (for now) incoming = calloc(1, sizeof(*incoming)); incoming->p = incoming_handler; // Setup our one test name without signature //struct ccn_charbuf* name; //name = ccn_charbuf_create(); //ccn_name_from_uri(name, TEST_URI); //ccn_name_append_nonce(name); //fprintf(stderr, "Our name: %s/<nonce>\n", URI); /* res = ccn_set_interest_filter(ccn_pub, name, incoming); if (res < 0) { fprintf(stderr, "Failed to register interest (res == %d)\n", res); return(1); } */ // Get our default keys -- Why do we have to do all this work?? // Borrowed from ccn_client.c struct ccn_signing_params name_sp = CCN_SIGNING_PARAMS_INIT; struct ccn_signing_params p = CCN_SIGNING_PARAMS_INIT; struct ccn_keystore *keystore = NULL; struct ccn_charbuf *timestamp = NULL; struct ccn_charbuf *finalblockid = NULL; struct ccn_charbuf *keylocator = NULL; unsigned char* public_key_digest = NULL; size_t public_key_digest_length = 0; res = ccn_chk_signing_params(ccn_pub, &name_sp, &p, ×tamp, &finalblockid, &keylocator); if (res < 0) return(res); // For this test, use our default signing keys get_default_keys(ccn_pub, &p, &keystore, &public_key, &public_key_digest, &public_key_digest_length, &private_key); // We'll need a KeyLocator for our ContentObject // So continue borrowed code /* Construct a key locator containing the key itself */ build_keylocator_from_key(&keylocator, public_key); // And a SignedInfo too struct ccn_charbuf *signed_info = ccn_charbuf_create(); res = ccn_signed_info_create(signed_info, public_key_digest, public_key_digest_length, timestamp, p.type, p.freshness, 0, /* FinalBlockID is optional */ keylocator); // Sign the interest struct ccn_charbuf *name_signed = ccn_charbuf_create(); sign_interest(name_signed, name, signed_info, NULL /* default digest alg */, private_key); // Express the signed interest from a different ccn handle so we get the packet res = ccn_express_interest(ccn_rec, name_signed, incoming, NULL); // TODO: AnswerOriginKind could limit to signed interest? outstanding_interests++; fprintf(stderr,"Signed interested sent.\n"); /* // chenni's original // create & express non-signed interest ccn = ccn_create(); res = ccn_connect(ccn, NULL); if (res < 0) { fprintf(stderr, "can't connect: %d\n", res); ccn_perror(ccn, "ccn_connect"); exit(1); } incoming = calloc(1, sizeof(*incoming)); incoming->p = incoming_handler; res = ccn_express_interest(ccn, name, incoming, NULL); printf("Waiting for response to interest packet\n"); while (res >= 0) { printf("running ccn_run...\n"); res = ccn_run(ccn, -1); } if (res < 0) { ccn_perror(ccn, "ccn_run"); exit(1); } ccn_charbuf_destroy(&name); ccn_destroy(&ccn); */ return 0; }