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