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