Jeff Thompson | 745026e | 2012-10-13 12:49:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file contains utilities to help encode and decode NDN objects. |
| 3 | * author: ucla-cs |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 6 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 7 | function encodeToHexInterest(interest){ |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 8 | |
| 9 | var enc = new BinaryXMLEncoder(); |
| 10 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 11 | interest.to_ccnb(enc); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 12 | |
| 13 | var hex = DataUtils.toHex(enc.getReducedOstream()); |
| 14 | |
| 15 | return hex; |
| 16 | |
| 17 | |
| 18 | } |
| 19 | |
| 20 | |
| 21 | function encodeToHexContentObject(co){ |
| 22 | var enc = new BinaryXMLEncoder(); |
| 23 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 24 | co.to_ccnb(enc); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 25 | |
| 26 | var hex = DataUtils.toHex(enc.getReducedOstream()); |
| 27 | |
| 28 | return hex; |
| 29 | |
| 30 | |
| 31 | } |
| 32 | |
| 33 | function encodeToBinaryContentObject(co){ |
| 34 | var enc = new BinaryXMLEncoder(); |
| 35 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 36 | co.to_ccnb(enc); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 37 | |
| 38 | var hex = enc.getReducedOstream(); |
| 39 | |
| 40 | return hex; |
| 41 | |
| 42 | |
| 43 | } |
| 44 | |
| 45 | function encodeForwardingEntry(co){ |
| 46 | var enc = new BinaryXMLEncoder(); |
| 47 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 48 | co.to_ccnb(enc); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 49 | |
| 50 | var bytes = enc.getReducedOstream(); |
| 51 | |
| 52 | return bytes; |
| 53 | |
| 54 | |
| 55 | } |
| 56 | |
| 57 | |
| 58 | |
| 59 | function decodeHexFaceInstance(result){ |
| 60 | |
| 61 | var numbers = DataUtils.toNumbers(result); |
| 62 | |
| 63 | |
| 64 | decoder = new BinaryXMLDecoder(numbers); |
| 65 | |
| 66 | if(LOG>3)console.log('DECODING HEX FACE INSTANCE \n'+numbers); |
| 67 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 68 | var faceInstance = new FaceInstance(); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 69 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 70 | faceInstance.from_ccnb(decoder); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 71 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 72 | return faceInstance; |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 73 | |
| 74 | } |
| 75 | |
| 76 | function decodeHexInterest(result){ |
| 77 | var numbers = DataUtils.toNumbers(result); |
| 78 | |
| 79 | |
| 80 | decoder = new BinaryXMLDecoder(numbers); |
| 81 | if(LOG>3)console.log('DECODING HEX INTERST \n'+numbers); |
| 82 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 83 | var interest = new Interest(); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 84 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 85 | interest.from_ccnb(decoder); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 86 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 87 | return interest; |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 88 | |
| 89 | } |
| 90 | |
| 91 | |
| 92 | |
| 93 | function decodeHexContentObject(result){ |
| 94 | var numbers = DataUtils.toNumbers(result); |
| 95 | |
| 96 | decoder = new BinaryXMLDecoder(numbers); |
| 97 | if(LOG>3)console.log('DECODED HEX CONTENT OBJECT \n'+numbers); |
| 98 | |
| 99 | co = new ContentObject(); |
| 100 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 101 | co.from_ccnb(decoder); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 102 | |
| 103 | return co; |
| 104 | |
| 105 | } |
| 106 | |
| 107 | |
| 108 | |
| 109 | function decodeHexForwardingEntry(result){ |
| 110 | var numbers = DataUtils.toNumbers(result); |
| 111 | |
| 112 | decoder = new BinaryXMLDecoder(numbers); |
| 113 | |
| 114 | if(LOG>3)console.log('DECODED HEX FORWARDING ENTRY \n'+numbers); |
| 115 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 116 | forwardingEntry = new ForwardingEntry(); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 117 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 118 | forwardingEntry.from_ccnb(decoder); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 119 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 120 | return forwardingEntry; |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 121 | |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /* Return a user friendly HTML string with the contents of co. |
| 125 | This also outputs to console.log. |
| 126 | */ |
| 127 | function contentObjectToHtml(/* ContentObject */ co) { |
| 128 | var output =""; |
| 129 | |
| 130 | if(co==-1) |
| 131 | output+= "NO CONTENT FOUND" |
| 132 | else if (co==-2) |
| 133 | output+= "CONTENT NAME IS EMPTY" |
| 134 | else{ |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 135 | if(co.name!=null && co.name.components!=null){ |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 136 | output+= "NAME: "; |
| 137 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 138 | for(var i=0;i<co.name.components.length;i++){ |
| 139 | output+= "/"+ DataUtils.toString(co.name.components[i]); |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 140 | } |
| 141 | output+= "<br />"; |
| 142 | output+= "<br />"; |
| 143 | } |
| 144 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 145 | if(co.content !=null){ |
| 146 | output += "CONTENT(ASCII): "+ DataUtils.toString(co.content); |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 147 | |
| 148 | output+= "<br />"; |
| 149 | output+= "<br />"; |
| 150 | } |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 151 | if(co.content !=null){ |
| 152 | output += "CONTENT(hex): "+ DataUtils.toHex(co.content); |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 153 | |
| 154 | output+= "<br />"; |
| 155 | output+= "<br />"; |
| 156 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 157 | if(co.signature !=null && co.signature.signature!=null){ |
| 158 | output += "SIGNATURE(hex): "+ DataUtils.toHex(co.signature.signature); |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 159 | |
| 160 | output+= "<br />"; |
| 161 | output+= "<br />"; |
| 162 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 163 | if(co.signedInfo !=null && co.signedInfo.publisher!=null && co.signedInfo.publisher.publisherPublicKeyDigest!=null){ |
| 164 | output += "Publisher Public Key Digest(hex): "+ DataUtils.toHex(co.signedInfo.publisher.publisherPublicKeyDigest); |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 165 | |
| 166 | output+= "<br />"; |
| 167 | output+= "<br />"; |
| 168 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 169 | if(co.signedInfo !=null && co.signedInfo.timestamp!=null){ |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 170 | var d = new Date(); |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 171 | d.setTime( co.signedInfo.timestamp.msec ); |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 172 | |
| 173 | var bytes = [217, 185, 12, 225, 217, 185, 12, 225]; |
| 174 | |
| 175 | output += "TimeStamp: "+d; |
| 176 | output+= "<br />"; |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 177 | output += "TimeStamp(number): "+ co.signedInfo.timestamp.msec; |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 178 | |
| 179 | output+= "<br />"; |
| 180 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 181 | if(co.signedInfo!=null && co.signedInfo.locator!=null && co.signedInfo.locator.certificate!=null){ |
| 182 | var tmp = DataUtils.toString(co.signedInfo.locator.certificate); |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 183 | var publickey = rstr2b64(tmp); |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 184 | var publickeyHex = DataUtils.toHex(co.signedInfo.locator.certificate).toLowerCase(); |
| 185 | var publickeyString = DataUtils.toString(co.signedInfo.locator.certificate); |
| 186 | var signature = DataUtils.toHex(co.signature.signature).toLowerCase(); |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 187 | var input = DataUtils.toString(co.rawSignatureData); |
| 188 | |
| 189 | output += "DER Certificate: "+publickey ; |
| 190 | |
| 191 | output+= "<br />"; |
| 192 | output+= "<br />"; |
| 193 | |
| 194 | if(LOG>2) console.log(" ContentName + SignedInfo + Content = "+input); |
| 195 | |
| 196 | if(LOG>2) console.log("HEX OF ContentName + SignedInfo + Content = "); |
| 197 | if(LOG>2) console.log(DataUtils.stringtoBase64(input)); |
| 198 | |
| 199 | if(LOG>2) console.log(" PublicKey = "+publickey ); |
| 200 | if(LOG>2) console.log(" PublicKeyHex = "+publickeyHex ); |
| 201 | if(LOG>2) console.log(" PublicKeyString = "+publickeyString ); |
| 202 | |
| 203 | if(LOG>2) console.log(" Signature is"); |
| 204 | if(LOG>2) console.log( signature ); |
| 205 | //if(LOG>2) console.log(" Signature NOW IS" ); |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 206 | //if(LOG>2) console.log(co.signature.signature); |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 207 | |
| 208 | var x509 = new X509(); |
| 209 | x509.readCertPEM(publickey); |
| 210 | |
| 211 | //x509.readCertPEMWithoutRSAInit(publickey); |
| 212 | |
| 213 | var result = x509.subjectPublicKeyRSA.verifyByteArray(co.rawSignatureData, signature); |
| 214 | if(LOG>2) console.log('result is '+result); |
| 215 | |
| 216 | var n = x509.subjectPublicKeyRSA.n; |
| 217 | var e = x509.subjectPublicKeyRSA.e; |
| 218 | |
| 219 | if(LOG>2) console.log('PUBLIC KEY n after is '); |
| 220 | if(LOG>2) console.log(n); |
| 221 | |
| 222 | if(LOG>2) console.log('EXPONENT e after is '); |
| 223 | if(LOG>2) console.log(e); |
| 224 | |
| 225 | /*var rsakey = new RSAKey(); |
| 226 | |
| 227 | var kp = publickeyHex.slice(56,314); |
| 228 | |
| 229 | output += "PUBLISHER KEY(hex): "+kp ; |
| 230 | |
| 231 | output+= "<br />"; |
| 232 | output+= "<br />"; |
| 233 | |
| 234 | console.log('kp is '+kp); |
| 235 | |
| 236 | var exp = publickeyHex.slice(318,324); |
| 237 | |
| 238 | console.log('kp size is '+kp.length ); |
| 239 | output += "exponent: "+exp ; |
| 240 | |
| 241 | output+= "<br />"; |
| 242 | output+= "<br />"; |
| 243 | |
| 244 | console.log('exp is '+exp); |
| 245 | |
| 246 | rsakey.setPublic(kp,exp); |
| 247 | |
| 248 | var result = rsakey.verifyString(input, signature);*/ |
| 249 | |
| 250 | if(result) |
| 251 | output += 'SIGNATURE VALID'; |
| 252 | else |
| 253 | output += 'SIGNATURE INVALID'; |
| 254 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 255 | //output += "VALID: "+ toHex(co.signedInfo.locator.publicKey); |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 256 | |
| 257 | output+= "<br />"; |
| 258 | output+= "<br />"; |
| 259 | |
| 260 | //if(LOG>4) console.log('str'[1]); |
| 261 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 262 | if(co.signedInfo!=null && co.signedInfo.locator!=null && co.signedInfo.locator.publicKey!=null){ |
| 263 | var publickey = rstr2b64(DataUtils.toString(co.signedInfo.locator.publicKey)); |
| 264 | var publickeyHex = DataUtils.toHex(co.signedInfo.locator.publicKey).toLowerCase(); |
| 265 | var publickeyString = DataUtils.toString(co.signedInfo.locator.publicKey); |
| 266 | var signature = DataUtils.toHex(co.signature.signature).toLowerCase(); |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 267 | var input = DataUtils.toString(co.rawSignatureData); |
| 268 | |
| 269 | output += "DER Certificate: "+publickey ; |
| 270 | |
| 271 | output+= "<br />"; |
| 272 | output+= "<br />"; |
| 273 | |
| 274 | if(LOG>2) console.log(" ContentName + SignedInfo + Content = "+input); |
| 275 | if(LOG>2) console.log(" PublicKey = "+publickey ); |
| 276 | if(LOG>2) console.log(" PublicKeyHex = "+publickeyHex ); |
| 277 | if(LOG>2) console.log(" PublicKeyString = "+publickeyString ); |
| 278 | |
| 279 | if(LOG>2) console.log(" Signature "+signature ); |
| 280 | |
| 281 | if(LOG>2) console.log(" Signature NOW IS" ); |
| 282 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 283 | if(LOG>2) console.log(co.signature.signature); |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 284 | |
| 285 | /*var x509 = new X509(); |
| 286 | |
| 287 | x509.readCertPEM(publickey); |
| 288 | |
| 289 | |
| 290 | //x509.readCertPEMWithoutRSAInit(publickey); |
| 291 | |
| 292 | var result = x509.subjectPublicKeyRSA.verifyString(input, signature);*/ |
| 293 | //console.log('result is '+result); |
| 294 | |
| 295 | var kp = publickeyHex.slice(56,314); |
| 296 | |
| 297 | output += "PUBLISHER KEY(hex): "+kp ; |
| 298 | |
| 299 | output+= "<br />"; |
| 300 | output+= "<br />"; |
| 301 | |
| 302 | console.log('PUBLIC KEY IN HEX is '); |
| 303 | console.log(kp); |
| 304 | |
| 305 | var exp = publickeyHex.slice(318,324); |
| 306 | |
| 307 | console.log('kp size is '+kp.length ); |
| 308 | output += "exponent: "+exp ; |
| 309 | |
| 310 | output+= "<br />"; |
| 311 | output+= "<br />"; |
| 312 | |
| 313 | console.log('EXPONENT is '); |
| 314 | console.log(exp); |
| 315 | |
| 316 | /*var c1 = hex_sha256(input); |
| 317 | var c2 = signature; |
| 318 | |
| 319 | if(LOG>4)console.log('input is '); |
| 320 | if(LOG>4)console.log(input); |
| 321 | if(LOG>4)console.log('C1 is '); |
| 322 | if(LOG>4)console.log(c1); |
| 323 | if(LOG>4)console.log('C2 is '); |
| 324 | if(LOG>4)console.log(c2); |
| 325 | var result = c1 == c2;*/ |
| 326 | |
| 327 | var rsakey = new RSAKey(); |
| 328 | |
| 329 | rsakey.setPublic(kp,exp); |
| 330 | |
| 331 | var result = rsakey.verifyByteArray(co.rawSignatureData,signature); |
| 332 | // var result = rsakey.verifyString(input, signature); |
| 333 | |
| 334 | console.log('PUBLIC KEY n after is '); |
| 335 | console.log(rsakey.n); |
| 336 | |
| 337 | console.log('EXPONENT e after is '); |
| 338 | console.log(rsakey.e); |
| 339 | |
| 340 | if(result) |
| 341 | output += 'SIGNATURE VALID'; |
| 342 | else |
| 343 | output += 'SIGNATURE INVALID'; |
| 344 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 345 | //output += "VALID: "+ toHex(co.signedInfo.locator.publicKey); |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 346 | |
| 347 | output+= "<br />"; |
| 348 | output+= "<br />"; |
| 349 | |
| 350 | //if(LOG>4) console.log('str'[1]); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | return output; |
| 355 | } |