Packet Signing & Key Verification Stage: Start
diff --git a/nlsr_km.c b/nlsr_km.c
new file mode 100644
index 0000000..2bfa8fa
--- /dev/null
+++ b/nlsr_km.c
@@ -0,0 +1,219 @@
+#include<stdio.h>
+#include<string.h>
+#include<stdlib.h>
+#include <unistd.h>
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+
+#include <ccn/ccn.h>
+#include <ccn/uri.h>
+#include <ccn/keystore.h>
+#include <ccn/signing.h>
+#include <ccn/schedule.h>
+#include <ccn/hashtb.h>
+
+#include "nlsr.h"
+#include "nlsr_km.h"
+#include "nlsr_km_util.h"
+
+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){
+	
+	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 ){
+		printf("Error in initiating keystore :(\n");
+		ccn_keystore_destroy(&keystore);
+		return -1;
+	}
+	
+
+	res=ccn_load_private_key	(nlsr->ccn,
+							keystore_path,
+							"Th1s1sn0t8g00dp8ssw0rd.",
+							pubid_out);
+
+	if(res < 0 ){
+		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);
+	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 ){
+		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);
+	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)){
+		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 ){
+		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
+process_incoming_content(struct ccn_closure* selfp, 
+												struct ccn_upcall_info* info){
+
+	printf("process_incoming_content called\n");
+
+	int res=verify_key(info->content_ccnb,info->pco->offset[CCN_PCO_E],info->pco);
+
+	if ( res != 0 ){
+		printf("Error in verfiying keys !! :( \n");
+	}
+	else{
+		printf("Key verification is successful :)\n");
+	}
+	return 0;
+}
+*/
+
+int 
+verify_key(const unsigned char *ccnb,size_t size, 
+										struct ccn_parsed_ContentObject *pco){
+
+	printf("verify key called\n");
+	int ret=-1;
+
+	if ( contain_key_name(ccnb, pco) == 1){
+		
+		struct ccn_charbuf *key_name=get_key_name(ccnb, pco);
+		struct ccn_charbuf *key_uri = ccn_charbuf_create();
+		ccn_uri_append(key_uri, key_name->buf, key_name->length, 0);
+		printf("Key Name from Incoming Content: %s\n",ccn_charbuf_as_string(key_uri));
+		int res=get_key_type_from_key_name(key_name);		
+		printf("Key Type: %d \n",res);
+
+		struct ccn_charbuf *result = ccn_charbuf_create();
+		struct ccn_parsed_ContentObject temp_pco = {0};
+		int get_flags = 0;
+		get_flags |= CCN_GET_NOKEYWAIT;
+		int counter = 0;
+		while(ccn_get(nlsr->ccn, key_name, NULL, 500, result, &temp_pco, NULL, 
+										get_flags) < 0 && counter < 3) counter++;
+
+		int chk_verify=ccn_verify_content(nlsr->ccn,ccnb,pco);		
+
+		if ( chk_verify == 0 ){
+			printf("Verification Successful :)\n");
+
+			if ( counter == 3){
+				printf("Could not retrieve key by name !!!\n");
+			}
+			else{
+				if ( res == ROOT_KEY ){
+					ret=0;
+				}
+				else{
+					ret=verify_key(result->buf,temp_pco.offset[CCN_PCO_E],&temp_pco);
+				}
+			}
+		}
+		ccn_charbuf_destroy(&result);
+		ccn_charbuf_destroy(&key_uri);
+		ccn_charbuf_destroy(&key_name);
+		return ret;
+	}
+
+	return ret;
+}
+
diff --git a/nlsr_km.h b/nlsr_km.h
new file mode 100644
index 0000000..f509bc8
--- /dev/null
+++ b/nlsr_km.h
@@ -0,0 +1,35 @@
+#ifndef _NLSR_KM_H_
+#define _NLSR_KM_H_
+
+enum key_type{
+	ROOT_KEY, //0
+	SITE_KEY, //1
+	OPERATOR_KEY,//2
+	ROUTING_KEY,//3
+	NLSR_KEY,//4
+	UNKNOWN_KEY//5
+};
+
+
+
+
+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);
+
+int	contain_key_name(const unsigned char *ccnb, 
+					struct ccn_parsed_ContentObject *pco);
+struct ccn_charbuf * get_key_name(const unsigned char *ccnb, 
+					struct ccn_parsed_ContentObject *pco);
+
+int verify_key(const unsigned char *ccnb, size_t size, 
+					struct ccn_parsed_ContentObject *pco);
+
+#endif
diff --git a/nlsr_km_util.c b/nlsr_km_util.c
new file mode 100644
index 0000000..ee55252
--- /dev/null
+++ b/nlsr_km_util.c
@@ -0,0 +1,169 @@
+#include<stdio.h>
+#include<string.h>
+#include<stdlib.h>
+#include <unistd.h>
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+
+#include <ccn/ccn.h>
+#include <ccn/uri.h>
+#include <ccn/keystore.h>
+#include <ccn/signing.h>
+#include <ccn/schedule.h>
+#include <ccn/hashtb.h>
+
+
+#include "nlsr.h"
+#include "nlsr_km_util.h"
+#include "nlsr_km.h"
+
+int
+appendLifetime(struct ccn_charbuf *cb, int lifetime) 
+{
+	unsigned char buf[sizeof(int32_t)];
+	int32_t dreck = lifetime << 12;
+	int pos = sizeof(int32_t);
+	int res = 0;
+	while (dreck > 0 && pos > 0) 
+	{
+		pos--;
+		buf[pos] = dreck & 255;
+		dreck = dreck >> 8;
+	}
+	res |= ccnb_append_tagged_blob(cb, CCN_DTAG_InterestLifetime, buf+pos, 
+															sizeof(buf)-pos);
+	return res;
+}
+
+int
+contain_key_name(const unsigned char *ccnb, struct ccn_parsed_ContentObject *pco) 
+{
+	if (pco->offset[CCN_PCO_B_KeyLocator] == pco->offset[CCN_PCO_E_KeyLocator])
+		return -1;
+
+	struct ccn_buf_decoder decoder;
+	struct ccn_buf_decoder *d;
+	d = ccn_buf_decoder_start(&decoder, ccnb + 
+		pco->offset[CCN_PCO_B_Key_Certificate_KeyName], 
+		pco->offset[CCN_PCO_E_Key_Certificate_KeyName] - 
+		pco->offset[CCN_PCO_B_Key_Certificate_KeyName]);
+	if (ccn_buf_match_dtag(d, CCN_DTAG_KeyName))
+		return 1;
+
+	return -1;
+}
+
+struct ccn_charbuf *
+get_key_name(const unsigned char *ccnb, struct ccn_parsed_ContentObject *pco) 
+{
+	struct ccn_charbuf *key_name = ccn_charbuf_create();
+	ccn_charbuf_append(key_name, ccnb + pco->offset[CCN_PCO_B_KeyName_Name], 
+	pco->offset[CCN_PCO_E_KeyName_Name] - pco->offset[CCN_PCO_B_KeyName_Name]);
+
+	return key_name;
+}
+
+int
+check_for_name_component_in_name(const struct ccn_charbuf *name, 
+								const struct ccn_indexbuf *indx,
+								const char *component){
+	
+	int res,i;
+	int result_position=0;
+	int name_comps=(int)indx->n;
+
+	for(i=0;i<name_comps;i++){
+		res=ccn_name_comp_strcmp(name->buf,indx,i,component);
+		if( res == 0){
+			
+				result_position=i;
+				break;
+		}	
+	}
+
+	return result_position;
+}
+
+
+int
+check_for_tag_component_in_name(const struct ccn_charbuf *name, 
+								const struct ccn_indexbuf *indx,
+								const char *component){
+	
+	int res,i;
+	int result_position=0;
+	int name_comps=(int)indx->n;
+
+	for(i=0;i<name_comps;i++){
+		const unsigned char *comp_ptr;
+		size_t comp_size;
+		res=ccn_name_comp_get(name->buf, indx,i,&comp_ptr, &comp_size);
+		if( res == 0){
+			if ( strstr((char *)comp_ptr,component) != NULL ){
+				result_position=i;
+				break;
+			}
+		}	
+	}
+
+	return result_position;
+}
+
+enum key_type
+get_key_type_from_key_name(struct ccn_charbuf *keyname)
+{
+	printf("get_key_type_from_key_name called\n");
+
+	int res;
+	int return_key=UNKNOWN_KEY;	
+
+	struct ccn_indexbuf *indx=ccn_indexbuf_create();
+	if ( indx == NULL ){
+		printf("Error in creating index for key name \n");
+		return UNKNOWN_KEY;
+	}
+
+	res=ccn_name_split(keyname,indx);
+	if ( res < 0 ){
+		printf("Error in parsing key name \n");
+		ccn_indexbuf_destroy(&indx);
+		return UNKNOWN_KEY;
+	}
+	else if ( res == 3){
+		int chk_ndn=check_for_name_component_in_name(keyname,indx,"ndn");
+		int chk_key=check_for_name_component_in_name(keyname,indx,"keys");
+		if ( chk_ndn == 0 && chk_key == 1)
+			return_key=ROOT_KEY;
+	}
+	else{
+		int check_op,check_rt;
+		check_op=check_for_tag_component_in_name(keyname,indx,
+													 "O.N.Start");
+		check_rt=check_for_tag_component_in_name(keyname,indx,
+													 "R.N.Start");
+		if ( check_op > 0){
+			return_key=OPERATOR_KEY;
+		}
+		else if(check_rt >0){
+			int check_nlsr;
+			check_nlsr=check_for_name_component_in_name(keyname,indx,
+													 "nlsr");
+			if ( check_rt > 0 ){
+				if ( check_nlsr > 0){
+					return_key=NLSR_KEY;
+				}
+				else{
+					return_key=ROUTING_KEY;
+				}
+			}
+		}
+		else if ( check_rt == 0 && check_op == 0 && res > 3){
+			return_key=SITE_KEY;
+		}
+	}
+
+	ccn_indexbuf_destroy(&indx);
+	return return_key;
+}
diff --git a/nlsr_km_util.h b/nlsr_km_util.h
new file mode 100644
index 0000000..8ad001f
--- /dev/null
+++ b/nlsr_km_util.h
@@ -0,0 +1,27 @@
+#ifndef _NLSR_KM_UTIL_H_
+#define _NLSR_KM_UTIL_H_
+
+int
+contain_key_name(const unsigned char *ccnb, 
+				struct ccn_parsed_ContentObject *pco);
+
+struct ccn_charbuf *
+get_key_name(const unsigned char *ccnb, 
+				struct ccn_parsed_ContentObject *pco);
+
+int
+check_for_name_component_in_name(const struct ccn_charbuf *name, 
+								const struct ccn_indexbuf *indx,
+								const char *component);
+int
+check_for_tag_component_in_name(const struct ccn_charbuf *name, 
+								const struct ccn_indexbuf *indx,
+								const char *component);
+enum key_type
+get_key_type_from_key_name(struct ccn_charbuf *keyname); 
+
+
+int
+appendLifetime(struct ccn_charbuf *cb, int lifetime);
+
+#endif