Wentao Shang | bd63e46 | 2012-12-03 16:19:33 -0800 | [diff] [blame] | 1 | /** |
Jeff Thompson | 745026e | 2012-10-13 12:49:20 -0700 | [diff] [blame] | 2 | * This file contains utilities to help encode and decode NDN objects. |
Jeff Thompson | 146d7de | 2012-11-17 16:15:28 -0800 | [diff] [blame] | 3 | * author: Meki Cheraoui |
Jeff Thompson | 745026e | 2012-10-13 12:49:20 -0700 | [diff] [blame] | 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 | bc0d326 | 2013-07-26 18:15:36 -0700 | [diff] [blame] | 8 | return DataUtils.toHex(interest.encode()); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 9 | } |
| 10 | |
Jeff Thompson | bc0d326 | 2013-07-26 18:15:36 -0700 | [diff] [blame] | 11 | // Deprecated: Use interest.encode(). |
Jeff Thompson | a5a2a2b | 2012-11-15 00:09:17 -0800 | [diff] [blame] | 12 | function encodeToBinaryInterest(interest) { |
Jeff Thompson | bc0d326 | 2013-07-26 18:15:36 -0700 | [diff] [blame] | 13 | return interest.encode(); |
Jeff Thompson | 344891b | 2012-11-11 18:41:09 -0800 | [diff] [blame] | 14 | } |
| 15 | |
Jeff Thompson | bc0d326 | 2013-07-26 18:15:36 -0700 | [diff] [blame] | 16 | function encodeToHexContentObject(contentObject) { |
| 17 | return DataUtils.toHex(contentObject.encode()); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 18 | } |
| 19 | |
Jeff Thompson | bc0d326 | 2013-07-26 18:15:36 -0700 | [diff] [blame] | 20 | // Deprecated: Use contentObject.encode(). |
| 21 | function encodeToBinaryContentObject(contentObject) { |
| 22 | contentObject.encode(); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 23 | } |
| 24 | |
Jeff Thompson | bc0d326 | 2013-07-26 18:15:36 -0700 | [diff] [blame] | 25 | function encodeForwardingEntry(co) { |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 26 | var enc = new BinaryXMLEncoder(); |
| 27 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 28 | co.to_ccnb(enc); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 29 | |
| 30 | var bytes = enc.getReducedOstream(); |
| 31 | |
| 32 | return bytes; |
| 33 | |
| 34 | |
| 35 | } |
| 36 | |
| 37 | |
| 38 | |
| 39 | function decodeHexFaceInstance(result){ |
| 40 | |
| 41 | var numbers = DataUtils.toNumbers(result); |
| 42 | |
| 43 | |
Jeff Thompson | 48ff28a | 2013-02-18 22:53:29 -0800 | [diff] [blame] | 44 | var decoder = new BinaryXMLDecoder(numbers); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 45 | |
| 46 | if(LOG>3)console.log('DECODING HEX FACE INSTANCE \n'+numbers); |
| 47 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 48 | var faceInstance = new FaceInstance(); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 49 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 50 | faceInstance.from_ccnb(decoder); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 51 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 52 | return faceInstance; |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 53 | |
| 54 | } |
| 55 | |
Jeff Thompson | bc0d326 | 2013-07-26 18:15:36 -0700 | [diff] [blame] | 56 | function decodeHexInterest(input){ |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 57 | var interest = new Interest(); |
Jeff Thompson | bc0d326 | 2013-07-26 18:15:36 -0700 | [diff] [blame] | 58 | interest.decode(DataUtils.toNumbers(input)); |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 59 | return interest; |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Jeff Thompson | bc0d326 | 2013-07-26 18:15:36 -0700 | [diff] [blame] | 62 | function decodeHexContentObject(input){ |
| 63 | var contentObject = new ContentObject(); |
| 64 | contentObject.decode(DataUtils.toNumbers(input)); |
| 65 | return contentObject; |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 68 | function decodeHexForwardingEntry(result){ |
| 69 | var numbers = DataUtils.toNumbers(result); |
| 70 | |
Jeff Thompson | 48ff28a | 2013-02-18 22:53:29 -0800 | [diff] [blame] | 71 | var decoder = new BinaryXMLDecoder(numbers); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 72 | |
| 73 | if(LOG>3)console.log('DECODED HEX FORWARDING ENTRY \n'+numbers); |
| 74 | |
Jeff Thompson | 48ff28a | 2013-02-18 22:53:29 -0800 | [diff] [blame] | 75 | var forwardingEntry = new ForwardingEntry(); |
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 | forwardingEntry.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 forwardingEntry; |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 80 | |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Jeff Thompson | 68fccd6 | 2012-12-29 17:38:23 -0800 | [diff] [blame] | 83 | /* |
| 84 | * Decode the Uint8Array which holds SubjectPublicKeyInfo and return an RSAKey. |
| 85 | */ |
| 86 | function decodeSubjectPublicKeyInfo(array) { |
| 87 | var hex = DataUtils.toHex(array).toLowerCase(); |
| 88 | var a = _x509_getPublicKeyHexArrayFromCertHex(hex, _x509_getSubjectPublicKeyPosFromCertHex(hex, 0)); |
| 89 | var rsaKey = new RSAKey(); |
| 90 | rsaKey.setPublic(a[0], a[1]); |
| 91 | return rsaKey; |
| 92 | } |
| 93 | |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 94 | /* Return a user friendly HTML string with the contents of co. |
| 95 | This also outputs to console.log. |
| 96 | */ |
| 97 | function contentObjectToHtml(/* ContentObject */ co) { |
| 98 | var output =""; |
| 99 | |
| 100 | if(co==-1) |
| 101 | output+= "NO CONTENT FOUND" |
| 102 | else if (co==-2) |
| 103 | output+= "CONTENT NAME IS EMPTY" |
| 104 | else{ |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 105 | if(co.name!=null && co.name.components!=null){ |
Jeff Thompson | a5a2a2b | 2012-11-15 00:09:17 -0800 | [diff] [blame] | 106 | output+= "NAME: " + co.name.to_uri(); |
| 107 | |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 108 | output+= "<br />"; |
| 109 | output+= "<br />"; |
| 110 | } |
| 111 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 112 | if(co.content !=null){ |
| 113 | output += "CONTENT(ASCII): "+ DataUtils.toString(co.content); |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 114 | |
| 115 | output+= "<br />"; |
| 116 | output+= "<br />"; |
| 117 | } |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 118 | if(co.content !=null){ |
| 119 | output += "CONTENT(hex): "+ DataUtils.toHex(co.content); |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 120 | |
| 121 | output+= "<br />"; |
| 122 | output+= "<br />"; |
| 123 | } |
Jeff Thompson | 612f255 | 2013-07-09 19:31:44 -0700 | [diff] [blame] | 124 | if(co.signature !=null && co.signature.digestAlgorithm!=null){ |
| 125 | output += "DigestAlgorithm (hex): "+ DataUtils.toHex(co.signature.digestAlgorithm); |
| 126 | |
| 127 | output+= "<br />"; |
| 128 | output+= "<br />"; |
| 129 | } |
| 130 | if(co.signature !=null && co.signature.witness!=null){ |
| 131 | output += "Witness (hex): "+ DataUtils.toHex(co.signature.witness); |
| 132 | |
| 133 | output+= "<br />"; |
| 134 | output+= "<br />"; |
| 135 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 136 | if(co.signature !=null && co.signature.signature!=null){ |
Jeff Thompson | 612f255 | 2013-07-09 19:31:44 -0700 | [diff] [blame] | 137 | output += "Signature(hex): "+ DataUtils.toHex(co.signature.signature); |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 138 | |
| 139 | output+= "<br />"; |
| 140 | output+= "<br />"; |
| 141 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 142 | if(co.signedInfo !=null && co.signedInfo.publisher!=null && co.signedInfo.publisher.publisherPublicKeyDigest!=null){ |
| 143 | output += "Publisher Public Key Digest(hex): "+ DataUtils.toHex(co.signedInfo.publisher.publisherPublicKeyDigest); |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 144 | |
| 145 | output+= "<br />"; |
| 146 | output+= "<br />"; |
| 147 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 148 | if(co.signedInfo !=null && co.signedInfo.timestamp!=null){ |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 149 | var d = new Date(); |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 150 | d.setTime( co.signedInfo.timestamp.msec ); |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 151 | |
| 152 | var bytes = [217, 185, 12, 225, 217, 185, 12, 225]; |
| 153 | |
| 154 | output += "TimeStamp: "+d; |
| 155 | output+= "<br />"; |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 156 | output += "TimeStamp(number): "+ co.signedInfo.timestamp.msec; |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 157 | |
| 158 | output+= "<br />"; |
| 159 | } |
Jeff Thompson | 6444788 | 2012-10-14 18:11:33 -0700 | [diff] [blame] | 160 | if(co.signedInfo !=null && co.signedInfo.finalBlockID!=null){ |
| 161 | output += "FinalBlockID: "+ DataUtils.toHex(co.signedInfo.finalBlockID); |
| 162 | output+= "<br />"; |
| 163 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 164 | if(co.signedInfo!=null && co.signedInfo.locator!=null && co.signedInfo.locator.certificate!=null){ |
Jeff Thompson | 68fccd6 | 2012-12-29 17:38:23 -0800 | [diff] [blame] | 165 | var certificateHex = DataUtils.toHex(co.signedInfo.locator.certificate).toLowerCase(); |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 166 | var signature = DataUtils.toHex(co.signature.signature).toLowerCase(); |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 167 | var input = DataUtils.toString(co.rawSignatureData); |
| 168 | |
Jeff Thompson | 68fccd6 | 2012-12-29 17:38:23 -0800 | [diff] [blame] | 169 | output += "Hex Certificate: "+ certificateHex ; |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 170 | |
| 171 | output+= "<br />"; |
| 172 | output+= "<br />"; |
| 173 | |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 174 | var x509 = new X509(); |
Jeff Thompson | 68fccd6 | 2012-12-29 17:38:23 -0800 | [diff] [blame] | 175 | x509.readCertHex(certificateHex); |
Jeff Thompson | 253cab4 | 2012-12-29 17:48:40 -0800 | [diff] [blame] | 176 | output += "Public key (hex) modulus: " + x509.subjectPublicKeyRSA.n.toString(16) + "<br/>"; |
| 177 | output += "exponent: " + x509.subjectPublicKeyRSA.e.toString(16) + "<br/>"; |
| 178 | output += "<br/>"; |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 179 | |
Wentao Shang | fddf90d | 2013-01-05 17:18:49 -0800 | [diff] [blame] | 180 | var result = x509.subjectPublicKeyRSA.verifyByteArray(co.rawSignatureData, null, signature); |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 181 | if(LOG>2) console.log('result is '+result); |
| 182 | |
| 183 | var n = x509.subjectPublicKeyRSA.n; |
| 184 | var e = x509.subjectPublicKeyRSA.e; |
| 185 | |
| 186 | if(LOG>2) console.log('PUBLIC KEY n after is '); |
| 187 | if(LOG>2) console.log(n); |
| 188 | |
| 189 | if(LOG>2) console.log('EXPONENT e after is '); |
| 190 | if(LOG>2) console.log(e); |
| 191 | |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 192 | if(result) |
Jeff Thompson | 68fccd6 | 2012-12-29 17:38:23 -0800 | [diff] [blame] | 193 | output += 'SIGNATURE VALID'; |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 194 | else |
Jeff Thompson | 68fccd6 | 2012-12-29 17:38:23 -0800 | [diff] [blame] | 195 | output += 'SIGNATURE INVALID'; |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 196 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 197 | //output += "VALID: "+ toHex(co.signedInfo.locator.publicKey); |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 198 | |
| 199 | output+= "<br />"; |
| 200 | output+= "<br />"; |
| 201 | |
| 202 | //if(LOG>4) console.log('str'[1]); |
| 203 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 204 | if(co.signedInfo!=null && co.signedInfo.locator!=null && co.signedInfo.locator.publicKey!=null){ |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 205 | var publickeyHex = DataUtils.toHex(co.signedInfo.locator.publicKey).toLowerCase(); |
| 206 | var publickeyString = DataUtils.toString(co.signedInfo.locator.publicKey); |
| 207 | var signature = DataUtils.toHex(co.signature.signature).toLowerCase(); |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 208 | var input = DataUtils.toString(co.rawSignatureData); |
| 209 | |
Wentao Shang | fddf90d | 2013-01-05 17:18:49 -0800 | [diff] [blame] | 210 | var wit = null; |
| 211 | var witHex = ""; |
| 212 | if (co.signature.Witness != null) { |
| 213 | wit = new Witness(); |
| 214 | wit.decode(co.signature.Witness); |
| 215 | witHex = DataUtils.toHex(co.signature.Witness); |
| 216 | } |
| 217 | |
Jeff Thompson | 68fccd6 | 2012-12-29 17:38:23 -0800 | [diff] [blame] | 218 | output += "Public key: " + publickeyHex; |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 219 | |
| 220 | output+= "<br />"; |
| 221 | output+= "<br />"; |
| 222 | |
| 223 | if(LOG>2) console.log(" ContentName + SignedInfo + Content = "+input); |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 224 | if(LOG>2) console.log(" PublicKeyHex = "+publickeyHex ); |
| 225 | if(LOG>2) console.log(" PublicKeyString = "+publickeyString ); |
| 226 | |
| 227 | if(LOG>2) console.log(" Signature "+signature ); |
Wentao Shang | fddf90d | 2013-01-05 17:18:49 -0800 | [diff] [blame] | 228 | if(LOG>2) console.log(" Witness "+witHex ); |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 229 | |
| 230 | if(LOG>2) console.log(" Signature NOW IS" ); |
| 231 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 232 | if(LOG>2) console.log(co.signature.signature); |
Jeff Thompson | 68fccd6 | 2012-12-29 17:38:23 -0800 | [diff] [blame] | 233 | |
| 234 | var rsakey = decodeSubjectPublicKeyInfo(co.signedInfo.locator.publicKey); |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 235 | |
Jeff Thompson | 68fccd6 | 2012-12-29 17:38:23 -0800 | [diff] [blame] | 236 | output += "Public key (hex) modulus: " + rsakey.n.toString(16) + "<br/>"; |
| 237 | output += "exponent: " + rsakey.e.toString(16) + "<br/>"; |
| 238 | output += "<br/>"; |
| 239 | |
Wentao Shang | fddf90d | 2013-01-05 17:18:49 -0800 | [diff] [blame] | 240 | var result = rsakey.verifyByteArray(co.rawSignatureData, wit, signature); |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 241 | // var result = rsakey.verifyString(input, signature); |
| 242 | |
Wentao Shang | 2b740e6 | 2012-12-07 00:02:53 -0800 | [diff] [blame] | 243 | if(LOG>2) console.log('PUBLIC KEY n after is '); |
| 244 | if(LOG>2) console.log(rsakey.n); |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 245 | |
Wentao Shang | 2b740e6 | 2012-12-07 00:02:53 -0800 | [diff] [blame] | 246 | if(LOG>2) console.log('EXPONENT e after is '); |
| 247 | if(LOG>2) console.log(rsakey.e); |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 248 | |
| 249 | if(result) |
Wentao Shang | fddf90d | 2013-01-05 17:18:49 -0800 | [diff] [blame] | 250 | output += 'SIGNATURE VALID'; |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 251 | else |
Wentao Shang | fddf90d | 2013-01-05 17:18:49 -0800 | [diff] [blame] | 252 | output += 'SIGNATURE INVALID'; |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 253 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 254 | //output += "VALID: "+ toHex(co.signedInfo.locator.publicKey); |
jeff | cc8b3a9 | 2012-09-03 15:13:27 -0700 | [diff] [blame] | 255 | |
| 256 | output+= "<br />"; |
| 257 | output+= "<br />"; |
| 258 | |
| 259 | //if(LOG>4) console.log('str'[1]); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | return output; |
| 264 | } |
Jeff Thompson | 68fccd6 | 2012-12-29 17:38:23 -0800 | [diff] [blame] | 265 | |
| 266 | |