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