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