blob: 8d03d0cedcac305fd8b62f4ab3f107f1f1ab36fa [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
akmhoque7adb2772013-03-05 16:30:59 -060032 if ( nlsr->debugging )
33 printf("sign_content_with_user_defined_keystore called\n");
akmhoque7c64d802013-03-05 10:18:37 -060034
35
36 int res;
37
38
39 struct ccn_charbuf * pubid_out=ccn_charbuf_create();
40 struct ccn_charbuf * keyname;
41
42
43 struct ccn_keystore *keystore = NULL;
44 keystore=ccn_keystore_create();
45 res=ccn_keystore_init(keystore, keystore_path,keystore_passphrase );
46 if ( res < 0 ){
akmhoque7adb2772013-03-05 16:30:59 -060047 if ( nlsr->debugging )
48 printf("Error in initiating keystore :(\n");
akmhoque7c64d802013-03-05 10:18:37 -060049 ccn_keystore_destroy(&keystore);
50 return -1;
51 }
52
53
54 res=ccn_load_private_key (nlsr->ccn,
55 keystore_path,
akmhoque7adb2772013-03-05 16:30:59 -060056 keystore_passphrase,
akmhoque7c64d802013-03-05 10:18:37 -060057 pubid_out);
58
59 if(res < 0 ){
akmhoque7adb2772013-03-05 16:30:59 -060060 if ( nlsr->debugging )
61 printf("Error in loading keystore :( \n");
akmhoque7c64d802013-03-05 10:18:37 -060062 ccn_charbuf_destroy(&pubid_out);
63 return -1;
64 }
65
66 char *baseuri=(char *)calloc(strlen(key_repo_name)+strlen(site_name)+
67 strlen(router_name)+strlen("/%C1.R.N.Start")+5,sizeof(char));
68 memcpy(baseuri,key_repo_name,strlen(key_repo_name)+1);
akmhoque7adb2772013-03-05 16:30:59 -060069 if ( site_name[0] != '/')
70 memcpy(baseuri+strlen(baseuri),"/",1);
akmhoque7c64d802013-03-05 10:18:37 -060071 memcpy(baseuri+strlen(baseuri),site_name,strlen(site_name)+1);
72 memcpy(baseuri+strlen(baseuri),"/%C1.R.N.Start",strlen("/%C1.R.N.Start"));
73 memcpy(baseuri+strlen(baseuri),router_name,strlen(router_name)+1);
74 baseuri[strlen(baseuri)]='\0';
75
76
77 keyname=ccn_charbuf_create();
78 if(keyname == NULL ){
79 ccn_charbuf_destroy(&pubid_out);
80 free(baseuri);
81 return -1;
82 }
83 ccn_name_from_uri(keyname,baseuri);
84 if ( res < 0 ){
akmhoque7adb2772013-03-05 16:30:59 -060085 if ( nlsr->debugging )
86 printf("Bad URI format: %s\n",baseuri);
akmhoque7c64d802013-03-05 10:18:37 -060087 ccn_charbuf_destroy(&pubid_out);
88 ccn_charbuf_destroy(&keyname);
89 free(baseuri);
90 return -1;
91 }
akmhoque3098a312013-03-06 07:31:01 -060092
93 ccn_name_append_str(keyname,"routing");
akmhoque7c64d802013-03-05 10:18:37 -060094 ccn_name_append_str(keyname,"nlsr");
95 struct ccn_charbuf *keyid = ccn_charbuf_create();
96 ccn_charbuf_append_value(keyid, CCN_MARKER_CONTROL, 1);
97 ccn_charbuf_append_string(keyid, ".M.K");
98 ccn_charbuf_append_value(keyid, 0, 1);
99 ccn_charbuf_append_charbuf(keyid, pubid_out);
100 ccn_name_append(keyname, keyid->buf, keyid->length);
101
102
103
104 struct ccn_charbuf *uri = ccn_charbuf_create();
105 ccn_uri_append(uri, keyname->buf, keyname->length, 0);
akmhoque7adb2772013-03-05 16:30:59 -0600106 if ( nlsr->debugging )
107 printf("Key Name Included when processing content: %s\n", ccn_charbuf_as_string(uri));
akmhoque7c64d802013-03-05 10:18:37 -0600108 ccn_charbuf_destroy(&uri);
109
110 struct ccn_signing_params sp = CCN_SIGNING_PARAMS_INIT;
111 sp.type = CCN_CONTENT_DATA;
112 sp.template_ccnb = ccn_charbuf_create();
113 ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_SignedInfo, CCN_DTAG);
114 ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_KeyLocator, CCN_DTAG);
115 ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_KeyName, CCN_DTAG);
116 ccn_charbuf_append(sp.template_ccnb, keyname->buf, keyname->length);
117 ccn_charbuf_append_closer(sp.template_ccnb); // KeyName closer
118 ccn_charbuf_append_closer(sp.template_ccnb); // KeyLocator closer
119 ccn_charbuf_append_closer(sp.template_ccnb); // SignedInfo closer
120
121 sp.sp_flags |= CCN_SP_TEMPL_KEY_LOCATOR;
122 sp.sp_flags |= CCN_SP_FINAL_BLOCK;
123 sp.freshness = 60;
124
125
126 if (pubid_out->length != sizeof(sp.pubid)){
akmhoque7adb2772013-03-05 16:30:59 -0600127 if ( nlsr->debugging )
128 printf("Size of pubid and sp.pubid is not equal");
akmhoque7c64d802013-03-05 10:18:37 -0600129 ccn_charbuf_destroy(&keyname);
130 ccn_charbuf_destroy(&pubid_out);
131 free(baseuri);
132 return -1;
133 }
134
135 memcpy(sp.pubid, pubid_out->buf, pubid_out->length);
136
137
138
139 res=ccn_sign_content(nlsr->ccn,resultbuf,content_name,&sp,data,data_size);
140 if( res < 0 ){
akmhoque7adb2772013-03-05 16:30:59 -0600141 if ( nlsr->debugging )
142 printf("Content signing error \n");
akmhoque7c64d802013-03-05 10:18:37 -0600143 ccn_charbuf_destroy(&sp.template_ccnb);
144 ccn_charbuf_destroy(&keyid);
145 ccn_charbuf_destroy(&keyname);
146 ccn_charbuf_destroy(&pubid_out);
147 free(baseuri);
148 return -1;
149 }
150
151 ccn_charbuf_destroy(&sp.template_ccnb);
152 ccn_charbuf_destroy(&keyid);
153 ccn_charbuf_destroy(&keyname);
154 ccn_charbuf_destroy(&pubid_out);
155 free(baseuri);
156 return 0;
157}
158
159
160/*
161int
162process_incoming_content(struct ccn_closure* selfp,
163 struct ccn_upcall_info* info){
164
165 printf("process_incoming_content called\n");
166
167 int res=verify_key(info->content_ccnb,info->pco->offset[CCN_PCO_E],info->pco);
168
169 if ( res != 0 ){
170 printf("Error in verfiying keys !! :( \n");
171 }
172 else{
173 printf("Key verification is successful :)\n");
174 }
175 return 0;
176}
177*/
178
179int
akmhoque6e2ba842013-03-05 19:35:26 -0600180verify_key(const unsigned char *ccnb,
akmhoque7c64d802013-03-05 10:18:37 -0600181 struct ccn_parsed_ContentObject *pco){
akmhoque7adb2772013-03-05 16:30:59 -0600182 if ( nlsr->debugging )
183 printf("verify key called\n");
akmhoque7c64d802013-03-05 10:18:37 -0600184 int ret=-1;
185
186 if ( contain_key_name(ccnb, pco) == 1){
187
188 struct ccn_charbuf *key_name=get_key_name(ccnb, pco);
189 struct ccn_charbuf *key_uri = ccn_charbuf_create();
190 ccn_uri_append(key_uri, key_name->buf, key_name->length, 0);
akmhoque7adb2772013-03-05 16:30:59 -0600191 if ( nlsr->debugging )
192 printf("Key Name from Incoming Content: %s\n",ccn_charbuf_as_string(key_uri));
193 int res=get_key_type_from_key_name(key_name);
194 if ( nlsr->debugging )
195 printf("Key Type: %d \n",res);
akmhoque7c64d802013-03-05 10:18:37 -0600196
197 struct ccn_charbuf *result = ccn_charbuf_create();
198 struct ccn_parsed_ContentObject temp_pco = {0};
199 int get_flags = 0;
200 get_flags |= CCN_GET_NOKEYWAIT;
201 int counter = 0;
202 while(ccn_get(nlsr->ccn, key_name, NULL, 500, result, &temp_pco, NULL,
203 get_flags) < 0 && counter < 3) counter++;
204
205 int chk_verify=ccn_verify_content(nlsr->ccn,ccnb,pco);
206
207 if ( chk_verify == 0 ){
akmhoque7adb2772013-03-05 16:30:59 -0600208 if ( nlsr->debugging )
209 printf("Verification Successful :)\n");
akmhoque7c64d802013-03-05 10:18:37 -0600210
211 if ( counter == 3){
akmhoque7adb2772013-03-05 16:30:59 -0600212 if ( nlsr->debugging )
213 printf("Could not retrieve key by name !!!\n");
akmhoque7c64d802013-03-05 10:18:37 -0600214 }
215 else{
216 if ( res == ROOT_KEY ){
217 ret=0;
218 }
219 else{
akmhoque6e2ba842013-03-05 19:35:26 -0600220 ret=verify_key(result->buf,&temp_pco);
akmhoque7c64d802013-03-05 10:18:37 -0600221 }
222 }
223 }
224 ccn_charbuf_destroy(&result);
225 ccn_charbuf_destroy(&key_uri);
226 ccn_charbuf_destroy(&key_name);
227 return ret;
228 }
229
230 return ret;
231}
232