int encode_message(struct ccn_charbuf *message, struct path * name_path, char *data, size_t len, struct ccn_charbuf *signed_info, const void *pkey, const char *digest_algorithm) { struct ccn_charbuf *path = ccn_charbuf_create(); int i; int res; if (path == NULL || ccn_name_init(path) == -1) { fprintf(stderr, "Failed to allocate or initialize content path\n"); return -1; } for (i = 0; i < name_path->count; i++) { ccn_name_append_str(path, name_path->comps[i]); } res = ccn_encode_ContentObject(message, path, signed_info, data, len, digest_algorithm, pkey); if (res != 0) { fprintf(stderr, "Failed to encode ContentObject\n"); } ccn_charbuf_destroy(&path); return(res); }
int sign_interest(struct ccn_charbuf* name_signed, struct ccn_charbuf* name, struct ccn_charbuf* signed_info, const char* digest_algorithm, struct ccn_pkey* key) { int res = 0; // Now assemble a signed Content object // Use ccn_encode_ContentObject so we can specify the key of our choice struct ccn_charbuf *tempContentObj = ccn_charbuf_create(); res = ccn_encode_ContentObject(tempContentObj, name, signed_info, NULL /* no data */, 0, digest_algorithm, key); if (res < 0) { fprintf(stderr, "Error building content object (res == %d)\n", res); return(res); } // Call replace_name to knock out the name; // it would be more efficient to assemble this with no name a modified ccn_encode_ContentObject() call // but that requires modification to the library function struct ccn_charbuf *empty_name = ccn_charbuf_create(); struct ccn_charbuf *sigContentObj = ccn_charbuf_create(); ccn_name_init(empty_name); // First prepend the namespace; (should this be done as a "name component"?) ccn_charbuf_append(sigContentObj, NS_SIGNATURE, NS_SIGNATURE_LEN); replace_name(sigContentObj, tempContentObj->buf, tempContentObj->length, empty_name); //fprintf(stderr, "replace_name == %d (%s)\n", res, (res==0)?"ok":"fail"); /* // Check that we didn't break things struct ccn_parsed_ContentObject pco = {0}; res = ccn_parse_ContentObject(&sigContentObj->buf[NS_SIGNATURE_LEN], sigContentObj->length - NS_SIGNATURE_LEN, &pco, NULL); if (res < 0) { fprintf(stderr, "Error parsing built content object (res == %d)\n", res); return(1); } */ ccn_charbuf_destroy(&empty_name); ccn_charbuf_destroy(&tempContentObj); // Build the final name for the interest <prefix>/<namespace><contentObj> ccn_charbuf_append_charbuf(name_signed, name); // Copy the name ccn_name_append(name_signed, sigContentObj->buf, sigContentObj->length); // Concatenate the new component // Dump the signature // print_hex(stderr,&(sigContentObj->buf)[NS_SIGNATURE_LEN],sigContentObj->length - NS_SIGNATURE_LEN,12); // fprintf(stderr,"\n"); // ccn_charbuf_destroy(&sigContentObj); return (res); }
PyObject * _pyccn_cmd_encode_ContentObject(PyObject *UNUSED(self), PyObject *args) { PyObject *py_content_object, *py_name, *py_content, *py_signed_info, *py_key; PyObject *py_o = NULL, *ret = NULL; struct ccn_charbuf *name, *signed_info, *content_object = NULL; struct ccn_pkey *private_key; const char *digest_alg = NULL; char *content; Py_ssize_t content_len; int r; if (!PyArg_ParseTuple(args, "OOOOO", &py_content_object, &py_name, &py_content, &py_signed_info, &py_key)) return NULL; if (strcmp(py_content_object->ob_type->tp_name, "ContentObject")) { PyErr_SetString(PyExc_TypeError, "Must pass a ContentObject as arg 1"); return NULL; } if (!CCNObject_IsValid(NAME, py_name)) { PyErr_SetString(PyExc_TypeError, "Must pass a CCN Name as arg 2"); return NULL; } else name = CCNObject_Get(NAME, py_name); if (py_content != Py_None && !PyBytes_Check(py_content)) { PyErr_SetString(PyExc_TypeError, "Must pass a Bytes as arg 3"); return NULL; } else if (py_content == Py_None) { content = NULL; content_len = 0; } else { r = PyBytes_AsStringAndSize(py_content, &content, &content_len); if (r < 0) return NULL; } if (!CCNObject_IsValid(SIGNED_INFO, py_signed_info)) { PyErr_SetString(PyExc_TypeError, "Must pass a CCN SignedInfo as arg 4"); return NULL; } else signed_info = CCNObject_Get(SIGNED_INFO, py_signed_info); if (strcmp(py_key->ob_type->tp_name, "Key")) { PyErr_SetString(PyExc_TypeError, "Must pass a Key as arg 4"); return NULL; } // DigestAlgorithm py_o = PyObject_GetAttrString(py_content_object, "digestAlgorithm"); if (py_o != Py_None) { PyErr_SetString(PyExc_NotImplementedError, "non-default digest" " algorithm not yet supported"); goto error; } Py_CLEAR(py_o); // Key private_key = Key_to_ccn_private(py_key); // Note that we don't load this key into the keystore hashtable in the library // because it makes this method require access to a ccn handle, and in fact, // ccn_sign_content just uses what's in signedinfo (after an error check by // chk_signing_params and then calls ccn_encode_ContentObject anyway // // Encode the content object // Build the ContentObject here. content_object = ccn_charbuf_create(); JUMP_IF_NULL_MEM(content_object, error); r = ccn_encode_ContentObject(content_object, name, signed_info, content, content_len, digest_alg, private_key); debug("ccn_encode_ContentObject res=%d\n", r); if (r < 0) { ccn_charbuf_destroy(&content_object); PyErr_SetString(g_PyExc_CCNError, "Unable to encode ContentObject"); goto error; } ret = CCNObject_New(CONTENT_OBJECT, content_object); error: Py_XDECREF(py_o); return ret; }
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 main(int argc, char **argv) { struct ccn_keystore *keystore = NULL; int res = 0; struct ccn_charbuf *signed_info = ccn_charbuf_create(); int i; int sec, usec; char msgbuf[PAYLOAD_SIZE]; struct timeval start, end; struct ccn_charbuf *message = ccn_charbuf_create(); struct ccn_charbuf *path = ccn_charbuf_create(); struct ccn_charbuf *seq = ccn_charbuf_create(); struct ccn_charbuf *temp = ccn_charbuf_create(); keystore = ccn_keystore_create(); 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 %s\n", ccn_charbuf_as_string(temp)); exit(1); } ccn_charbuf_destroy(&temp); 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 */ FRESHNESS, /*finalblockid*/ NULL, /* keylocator */ NULL); srandom(time(NULL)); for (i=0; i<PAYLOAD_SIZE; i++) { msgbuf[i] = random(); } printf("Generating %d signed ContentObjects (one . per 100)\n", COUNT); gettimeofday(&start, NULL); for (i=0; i<COUNT; i++) { if (i>0 && (i%100) == 0) { printf("."); fflush(stdout); } ccn_name_init(path); ccn_name_append_str(path, "rtp"); ccn_name_append_str(path, "protocol"); ccn_name_append_str(path, "13.2.117.34"); ccn_name_append_str(path, "domain"); ccn_name_append_str(path, "smetters"); ccn_name_append_str(path, "principal"); ccn_name_append_str(path, "2021915340"); ccn_name_append_str(path, "id"); ccn_charbuf_putf(seq, "%u", i); ccn_name_append(path, seq->buf, seq->length); ccn_name_append_str(path, "seq"); res = ccn_encode_ContentObject(/* out */ message, path, signed_info, msgbuf, PAYLOAD_SIZE, /* digest_algorithm */ NULL, ccn_keystore_private_key(keystore)); ccn_charbuf_reset(message); ccn_charbuf_reset(path); ccn_charbuf_reset(seq); } gettimeofday(&end, NULL); sec = end.tv_sec - start.tv_sec; usec = (int)end.tv_usec - (int)start.tv_usec; while (usec < 0) { sec--; usec += 1000000; } printf("\nComplete in %d.%06d secs\n", sec, usec); return(0); }
void GroupManager::incomingInterest(ccn_upcall_info *info) { int res; const char *data = NULL; const unsigned char *requester = NULL; const unsigned char *refresher = NULL; const unsigned char *filter = NULL; size_t filter_len = 0; ccn_charbuf *signed_info = NULL; ccn_charbuf *name = NULL; ccn_charbuf *content = NULL; RemoteUser *refreshUser = NULL; // requesterPrefix starts from index 4 to (info->interest_comps->n - 2) int nameEnd = 0; nameEnd = (info->interest_comps)->n - 2; /* construct reply data * name format: * /ndn/broadcast/conference/conference-name/speaker-list/username */ signed_info = ccn_charbuf_create(); struct ccn_charbuf *keylocator = ccn_charbuf_create(); ccn_create_keylocator(keylocator, ccn_keystore_public_key(cached_keystore)); res = ccn_signed_info_create(signed_info, /*pubkeyid*/ get_my_publisher_key_id(), /*publisher_key_id_size*/ get_my_publisher_key_id_length(), /*datetime*/ NULL, /*type*/ CCN_CONTENT_DATA, ///*freshness*/ -1, /*freshness*/ FRESHNESS, /*finalblockid*/ NULL, /*keylocator*/ keylocator); if (res < 0) { DPRINT("FAILED TO CREATE signed_info (res == %d)", res); } name = ccn_charbuf_create(); content = ccn_charbuf_create(); ccn_name_init(name); ccn_name_append_components(name, info->interest_ccnb, info->interest_comps->buf[0], info->interest_comps->buf[nameEnd + 1]); // append own username ccn_name_append_str(name, userName.toLocal8Bit().constData()); // get user list, the caller need to free the data buffer allocated int dlen = userListtoXml(&data); ccn_encode_ContentObject(content, name, signed_info, data, dlen, NULL, get_my_private_key()); // already have the lock, no need to trylock ccn_put(info->h, content->buf, content->length); ccn_charbuf_destroy(&signed_info); ccn_charbuf_destroy(&name); ccn_charbuf_destroy(&content); if (data != NULL) { free((void *)data); data = NULL; } }
int NdnMediaProcess::ndnDataSend(const void *buf, size_t len) { #define CHARBUF_DESTROY \ ccn_charbuf_destroy(&message);\ ccn_charbuf_destroy(&path); \ ccn_charbuf_destroy(&seq); UserDataBuf *userBuf = localUdb; if (userBuf == NULL) return -1; int res = 0; int seq_num = -1; struct ccn_charbuf *message = ccn_charbuf_create(); struct ccn_charbuf *path = ccn_charbuf_create(); struct ccn_charbuf *seq = ccn_charbuf_create(); unsigned char *ccn_msg = NULL; size_t ccn_msg_size = 0; ccn_name_init(path); seq_num = localSeq++; ccn_name_from_uri(path, localUdb->user_name.toLocal8Bit().constData()); ccn_name_append_str(path, "audio"); if (seq_num < 0) { res = -1; CHARBUF_DESTROY; return res; } ccn_charbuf_putf(seq, "%ld", seq_num); ccn_name_append(path, seq->buf, seq->length); struct ccn_charbuf *signed_info = ccn_charbuf_create(); if (cached_keystore == NULL) init_cached_keystore(); ccn_charbuf *keylocator = ccn_charbuf_create(); ccn_create_keylocator(keylocator, ccn_keystore_public_key(cached_keystore)); /* Create signed_info */ res = ccn_signed_info_create(signed_info, /* pubkeyid */ get_my_publisher_key_id(), /* publisher_key_id_size */ get_my_publisher_key_id_length(), /* datetime */ NULL, /* type */ CCN_CONTENT_DATA, /* freshness */ FRESHNESS, /* finalblockid */ NULL, /* keylocator */ keylocator); if (res != 0) { fprintf(stderr, "signed_info_create failed %d (line %d)\n", res, __LINE__); } if (isPrivate) { unsigned char *enc_buf = NULL; size_t enc_len = 0; res = symEncrypt(sessionKey, NULL, (const unsigned char *)buf, len, &enc_buf, &enc_len, AES_BLOCK_SIZE); if (res != 0) { fprintf(stderr, "can not decrypt audio\n"); std::exit(1); } res = ccn_encode_ContentObject( /* out */ message, path, signed_info, enc_buf, enc_len, /* keyLocator */ NULL, get_my_private_key()); if (enc_buf != NULL) { free(enc_buf); enc_buf = NULL; } } else { res = ccn_encode_ContentObject( /* out */ message, path, signed_info, buf, len, /* keyLocator */ NULL, get_my_private_key()); } if (res != 0) { fprintf(stderr, "encode_ContentObject failed %d (line %d)\n", res, __LINE__); CHARBUF_DESTROY; return res; } ccn_charbuf_destroy(&signed_info); ccn_charbuf_destroy(&keylocator); ccn_msg = (unsigned char *)calloc(1, message->length); ccn_msg_size = message->length; memcpy(ccn_msg, message->buf, message->length); /* { struct ccn_parsed_ContentObject o = {0}; res = ccn_parse_ContentObject(ccn_msg, ccn_msg_size, &o, NULL); if (res < 0) { fprintf(stderr, "created bad ContentObject, res = %d\n", res); abort(); } } */ struct buf_list *p = NULL, *b = userBuf->data_buf.buflist; while (b != NULL) { p = b; b = b->link; } b = (struct buf_list*)calloc(1, sizeof(struct buf_list)); if (b == NULL) { CHARBUF_DESTROY; return -1; } if (p != NULL) p->link = b; else userBuf->data_buf.buflist = b; b->buf = ccn_msg; b->len = ccn_msg_size; b->link = NULL; CHARBUF_DESTROY; return res; }
int NdnMediaProcess::sendNdnText(const char *text) { #define CHARBUF_DESTROY \ ccn_charbuf_destroy(&message);\ ccn_charbuf_destroy(&path); \ ccn_charbuf_destroy(&seq); UserDataBuf *userBuf = localUdb; if (userBuf == NULL) return -1; int res = 0; int seq_num = -1; struct ccn_charbuf *message = ccn_charbuf_create(); struct ccn_charbuf *path = ccn_charbuf_create(); struct ccn_charbuf *seq = ccn_charbuf_create(); ccn_name_init(path); seq_num = textSeq++; ccn_name_from_uri(path, localUdb->user_name.toLocal8Bit().constData()); ccn_name_append_str(path, "text"); if (seq_num < 0) { res = -1; CHARBUF_DESTROY; return res; } ccn_charbuf_putf(seq, "%ld", seq_num); ccn_name_append(path, seq->buf, seq->length); struct ccn_charbuf *signed_info = ccn_charbuf_create(); if (cached_keystore == NULL) init_cached_keystore(); ccn_charbuf *keylocator = ccn_charbuf_create(); ccn_create_keylocator(keylocator, ccn_keystore_public_key(cached_keystore)); /* Create signed_info */ res = ccn_signed_info_create(signed_info, /* pubkeyid */ get_my_publisher_key_id(), /* publisher_key_id_size */ get_my_publisher_key_id_length(), /* datetime */ NULL, /* type */ CCN_CONTENT_DATA, /* freshness */ FRESHNESS, /* finalblockid */ NULL, /* keylocator */ keylocator); if (res != 0) { fprintf(stderr, "signed_info_create failed %d (line %d)\n", res, __LINE__); } res = ccn_encode_ContentObject( /* out */ message, path, signed_info, text, strlen(text), /* keyLocator */ NULL, get_my_private_key()); if (res != 0) { fprintf(stderr, "encode_ContentObject failed %d (line %d)\n", res, __LINE__); CHARBUF_DESTROY; return res; } ccn_charbuf_destroy(&signed_info); ccn_charbuf_destroy(&keylocator); pthread_mutex_lock(&ccn_mutex); res = ccn_put(ndnState.ccn, message->buf, message->length); pthread_mutex_unlock(&ccn_mutex); }