blob: a650ffe48e7767ade7f84f250ae1cb235bcc060c [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
akmhoque7c64d802013-03-05 10:18:37 -060093 ccn_name_append_str(keyname,"nlsr");
94 struct ccn_charbuf *keyid = ccn_charbuf_create();
95 ccn_charbuf_append_value(keyid, CCN_MARKER_CONTROL, 1);
96 ccn_charbuf_append_string(keyid, ".M.K");
97 ccn_charbuf_append_value(keyid, 0, 1);
98 ccn_charbuf_append_charbuf(keyid, pubid_out);
99 ccn_name_append(keyname, keyid->buf, keyid->length);
100
101
102
103 struct ccn_charbuf *uri = ccn_charbuf_create();
104 ccn_uri_append(uri, keyname->buf, keyname->length, 0);
akmhoque7adb2772013-03-05 16:30:59 -0600105 if ( nlsr->debugging )
106 printf("Key Name Included when processing content: %s\n", ccn_charbuf_as_string(uri));
akmhoque7c64d802013-03-05 10:18:37 -0600107 ccn_charbuf_destroy(&uri);
108
109 struct ccn_signing_params sp = CCN_SIGNING_PARAMS_INIT;
110 sp.type = CCN_CONTENT_DATA;
111 sp.template_ccnb = ccn_charbuf_create();
112 ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_SignedInfo, CCN_DTAG);
113 ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_KeyLocator, CCN_DTAG);
114 ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_KeyName, CCN_DTAG);
115 ccn_charbuf_append(sp.template_ccnb, keyname->buf, keyname->length);
116 ccn_charbuf_append_closer(sp.template_ccnb); // KeyName closer
117 ccn_charbuf_append_closer(sp.template_ccnb); // KeyLocator closer
118 ccn_charbuf_append_closer(sp.template_ccnb); // SignedInfo closer
119
120 sp.sp_flags |= CCN_SP_TEMPL_KEY_LOCATOR;
121 sp.sp_flags |= CCN_SP_FINAL_BLOCK;
122 sp.freshness = 60;
123
124
125 if (pubid_out->length != sizeof(sp.pubid)){
akmhoque7adb2772013-03-05 16:30:59 -0600126 if ( nlsr->debugging )
127 printf("Size of pubid and sp.pubid is not equal");
akmhoque7c64d802013-03-05 10:18:37 -0600128 ccn_charbuf_destroy(&keyname);
129 ccn_charbuf_destroy(&pubid_out);
130 free(baseuri);
131 return -1;
132 }
133
134 memcpy(sp.pubid, pubid_out->buf, pubid_out->length);
135
136
137
138 res=ccn_sign_content(nlsr->ccn,resultbuf,content_name,&sp,data,data_size);
139 if( res < 0 ){
akmhoque7adb2772013-03-05 16:30:59 -0600140 if ( nlsr->debugging )
141 printf("Content signing error \n");
akmhoque7c64d802013-03-05 10:18:37 -0600142 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 -1;
148 }
149
150 ccn_charbuf_destroy(&sp.template_ccnb);
151 ccn_charbuf_destroy(&keyid);
152 ccn_charbuf_destroy(&keyname);
153 ccn_charbuf_destroy(&pubid_out);
154 free(baseuri);
155 return 0;
156}
157
158
159/*
160int
161process_incoming_content(struct ccn_closure* selfp,
162 struct ccn_upcall_info* info){
163
164 printf("process_incoming_content called\n");
165
166 int res=verify_key(info->content_ccnb,info->pco->offset[CCN_PCO_E],info->pco);
167
168 if ( res != 0 ){
169 printf("Error in verfiying keys !! :( \n");
170 }
171 else{
172 printf("Key verification is successful :)\n");
173 }
174 return 0;
175}
176*/
177
178int
akmhoque6e2ba842013-03-05 19:35:26 -0600179verify_key(const unsigned char *ccnb,
akmhoque7c64d802013-03-05 10:18:37 -0600180 struct ccn_parsed_ContentObject *pco){
akmhoque7adb2772013-03-05 16:30:59 -0600181 if ( nlsr->debugging )
182 printf("verify key called\n");
akmhoque7c64d802013-03-05 10:18:37 -0600183 int ret=-1;
184
185 if ( contain_key_name(ccnb, pco) == 1){
186
187 struct ccn_charbuf *key_name=get_key_name(ccnb, pco);
188 struct ccn_charbuf *key_uri = ccn_charbuf_create();
189 ccn_uri_append(key_uri, key_name->buf, key_name->length, 0);
akmhoque7adb2772013-03-05 16:30:59 -0600190 if ( nlsr->debugging )
191 printf("Key Name from Incoming Content: %s\n",ccn_charbuf_as_string(key_uri));
192 int res=get_key_type_from_key_name(key_name);
193 if ( nlsr->debugging )
194 printf("Key Type: %d \n",res);
akmhoque7c64d802013-03-05 10:18:37 -0600195
196 struct ccn_charbuf *result = ccn_charbuf_create();
197 struct ccn_parsed_ContentObject temp_pco = {0};
198 int get_flags = 0;
199 get_flags |= CCN_GET_NOKEYWAIT;
200 int counter = 0;
201 while(ccn_get(nlsr->ccn, key_name, NULL, 500, result, &temp_pco, NULL,
202 get_flags) < 0 && counter < 3) counter++;
203
204 int chk_verify=ccn_verify_content(nlsr->ccn,ccnb,pco);
205
206 if ( chk_verify == 0 ){
akmhoque7adb2772013-03-05 16:30:59 -0600207 if ( nlsr->debugging )
208 printf("Verification Successful :)\n");
akmhoque7c64d802013-03-05 10:18:37 -0600209
210 if ( counter == 3){
akmhoque7adb2772013-03-05 16:30:59 -0600211 if ( nlsr->debugging )
212 printf("Could not retrieve key by name !!!\n");
akmhoque7c64d802013-03-05 10:18:37 -0600213 }
214 else{
215 if ( res == ROOT_KEY ){
216 ret=0;
217 }
218 else{
akmhoque6e2ba842013-03-05 19:35:26 -0600219 ret=verify_key(result->buf,&temp_pco);
akmhoque7c64d802013-03-05 10:18:37 -0600220 }
221 }
222 }
223 ccn_charbuf_destroy(&result);
224 ccn_charbuf_destroy(&key_uri);
225 ccn_charbuf_destroy(&key_name);
226 return ret;
227 }
228
229 return ret;
230}
231