blob: 2bfa8faa7b28d7dd955a70226bbc89c91641a97c [file] [log] [blame]
akmhoque7c64d802013-03-05 10:18:37 -06001#include<stdio.h>
2#include<string.h>
3#include<stdlib.h>
4#include <unistd.h>
5#ifdef HAVE_CONFIG_H
6#include <config.h>
7#endif
8
9
10#include <ccn/ccn.h>
11#include <ccn/uri.h>
12#include <ccn/keystore.h>
13#include <ccn/signing.h>
14#include <ccn/schedule.h>
15#include <ccn/hashtb.h>
16
17#include "nlsr.h"
18#include "nlsr_km.h"
19#include "nlsr_km_util.h"
20
21int
22sign_content_with_user_defined_keystore(struct ccn_charbuf *content_name,
23 struct ccn_charbuf *resultbuf,
24 const void *data,
25 size_t data_size,
26 char *keystore_path,
27 char *keystore_passphrase,
28 char *key_repo_name,
29 char *site_name,
30 char *router_name){
31
32 printf("sign_content_with_user_defined_keystore called\n");
33
34
35 int res;
36
37
38 struct ccn_charbuf * pubid_out=ccn_charbuf_create();
39 struct ccn_charbuf * keyname;
40
41
42 struct ccn_keystore *keystore = NULL;
43 keystore=ccn_keystore_create();
44 res=ccn_keystore_init(keystore, keystore_path,keystore_passphrase );
45 if ( res < 0 ){
46 printf("Error in initiating keystore :(\n");
47 ccn_keystore_destroy(&keystore);
48 return -1;
49 }
50
51
52 res=ccn_load_private_key (nlsr->ccn,
53 keystore_path,
54 "Th1s1sn0t8g00dp8ssw0rd.",
55 pubid_out);
56
57 if(res < 0 ){
58 printf("Error in loading keystore :( \n");
59 ccn_charbuf_destroy(&pubid_out);
60 return -1;
61 }
62
63 char *baseuri=(char *)calloc(strlen(key_repo_name)+strlen(site_name)+
64 strlen(router_name)+strlen("/%C1.R.N.Start")+5,sizeof(char));
65 memcpy(baseuri,key_repo_name,strlen(key_repo_name)+1);
66 memcpy(baseuri+strlen(baseuri),"/",1);
67 memcpy(baseuri+strlen(baseuri),site_name,strlen(site_name)+1);
68 memcpy(baseuri+strlen(baseuri),"/%C1.R.N.Start",strlen("/%C1.R.N.Start"));
69 memcpy(baseuri+strlen(baseuri),router_name,strlen(router_name)+1);
70 baseuri[strlen(baseuri)]='\0';
71
72
73 keyname=ccn_charbuf_create();
74 if(keyname == NULL ){
75 ccn_charbuf_destroy(&pubid_out);
76 free(baseuri);
77 return -1;
78 }
79 ccn_name_from_uri(keyname,baseuri);
80 if ( res < 0 ){
81 printf("Bad URI format: %s\n",baseuri);
82 ccn_charbuf_destroy(&pubid_out);
83 ccn_charbuf_destroy(&keyname);
84 free(baseuri);
85 return -1;
86 }
87
88 ccn_name_append_str(keyname,"nlsr");
89 struct ccn_charbuf *keyid = ccn_charbuf_create();
90 ccn_charbuf_append_value(keyid, CCN_MARKER_CONTROL, 1);
91 ccn_charbuf_append_string(keyid, ".M.K");
92 ccn_charbuf_append_value(keyid, 0, 1);
93 ccn_charbuf_append_charbuf(keyid, pubid_out);
94 ccn_name_append(keyname, keyid->buf, keyid->length);
95
96
97
98 struct ccn_charbuf *uri = ccn_charbuf_create();
99 ccn_uri_append(uri, keyname->buf, keyname->length, 0);
100 printf("Key Name Included when processing content: %s\n", ccn_charbuf_as_string(uri));
101 ccn_charbuf_destroy(&uri);
102
103 struct ccn_signing_params sp = CCN_SIGNING_PARAMS_INIT;
104 sp.type = CCN_CONTENT_DATA;
105 sp.template_ccnb = ccn_charbuf_create();
106 ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_SignedInfo, CCN_DTAG);
107 ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_KeyLocator, CCN_DTAG);
108 ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_KeyName, CCN_DTAG);
109 ccn_charbuf_append(sp.template_ccnb, keyname->buf, keyname->length);
110 ccn_charbuf_append_closer(sp.template_ccnb); // KeyName closer
111 ccn_charbuf_append_closer(sp.template_ccnb); // KeyLocator closer
112 ccn_charbuf_append_closer(sp.template_ccnb); // SignedInfo closer
113
114 sp.sp_flags |= CCN_SP_TEMPL_KEY_LOCATOR;
115 sp.sp_flags |= CCN_SP_FINAL_BLOCK;
116 sp.freshness = 60;
117
118
119 if (pubid_out->length != sizeof(sp.pubid)){
120 printf("Size of pubid and sp.pubid is not equal");
121 ccn_charbuf_destroy(&keyname);
122 ccn_charbuf_destroy(&pubid_out);
123 free(baseuri);
124 return -1;
125 }
126
127 memcpy(sp.pubid, pubid_out->buf, pubid_out->length);
128
129
130
131 res=ccn_sign_content(nlsr->ccn,resultbuf,content_name,&sp,data,data_size);
132 if( res < 0 ){
133 printf("Content signing error \n");
134 ccn_charbuf_destroy(&sp.template_ccnb);
135 ccn_charbuf_destroy(&keyid);
136 ccn_charbuf_destroy(&keyname);
137 ccn_charbuf_destroy(&pubid_out);
138 free(baseuri);
139 return -1;
140 }
141
142 ccn_charbuf_destroy(&sp.template_ccnb);
143 ccn_charbuf_destroy(&keyid);
144 ccn_charbuf_destroy(&keyname);
145 ccn_charbuf_destroy(&pubid_out);
146 free(baseuri);
147 return 0;
148}
149
150
151/*
152int
153process_incoming_content(struct ccn_closure* selfp,
154 struct ccn_upcall_info* info){
155
156 printf("process_incoming_content called\n");
157
158 int res=verify_key(info->content_ccnb,info->pco->offset[CCN_PCO_E],info->pco);
159
160 if ( res != 0 ){
161 printf("Error in verfiying keys !! :( \n");
162 }
163 else{
164 printf("Key verification is successful :)\n");
165 }
166 return 0;
167}
168*/
169
170int
171verify_key(const unsigned char *ccnb,size_t size,
172 struct ccn_parsed_ContentObject *pco){
173
174 printf("verify key called\n");
175 int ret=-1;
176
177 if ( contain_key_name(ccnb, pco) == 1){
178
179 struct ccn_charbuf *key_name=get_key_name(ccnb, pco);
180 struct ccn_charbuf *key_uri = ccn_charbuf_create();
181 ccn_uri_append(key_uri, key_name->buf, key_name->length, 0);
182 printf("Key Name from Incoming Content: %s\n",ccn_charbuf_as_string(key_uri));
183 int res=get_key_type_from_key_name(key_name);
184 printf("Key Type: %d \n",res);
185
186 struct ccn_charbuf *result = ccn_charbuf_create();
187 struct ccn_parsed_ContentObject temp_pco = {0};
188 int get_flags = 0;
189 get_flags |= CCN_GET_NOKEYWAIT;
190 int counter = 0;
191 while(ccn_get(nlsr->ccn, key_name, NULL, 500, result, &temp_pco, NULL,
192 get_flags) < 0 && counter < 3) counter++;
193
194 int chk_verify=ccn_verify_content(nlsr->ccn,ccnb,pco);
195
196 if ( chk_verify == 0 ){
197 printf("Verification Successful :)\n");
198
199 if ( counter == 3){
200 printf("Could not retrieve key by name !!!\n");
201 }
202 else{
203 if ( res == ROOT_KEY ){
204 ret=0;
205 }
206 else{
207 ret=verify_key(result->buf,temp_pco.offset[CCN_PCO_E],&temp_pco);
208 }
209 }
210 }
211 ccn_charbuf_destroy(&result);
212 ccn_charbuf_destroy(&key_uri);
213 ccn_charbuf_destroy(&key_name);
214 return ret;
215 }
216
217 return ret;
218}
219