Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 Eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 /* global console */ | |
19 | |
18 "use strict"; | 20 "use strict"; |
19 | 21 |
20 /** | 22 /** |
21 * This is a specialized RSA library meant only to verify SHA1-based signatures. | 23 * This is a specialized RSA library meant only to verify SHA1-based signatures. |
22 */ | 24 */ |
23 | 25 |
24 let {BigInteger} = require("jsbn"); | 26 const {BigInteger} = require("jsbn"); |
25 let Rusha = require("rusha"); | 27 const Rusha = require("rusha"); |
26 | 28 |
27 let rusha = new Rusha(); | 29 let rusha = new Rusha(); |
28 | 30 |
29 // Define ASN.1 templates for the data structures used | 31 // Define ASN.1 templates for the data structures used |
30 function seq(...args) | 32 function seq(...args) |
31 { | 33 { |
32 return {type: 0x30, children: args}; | 34 return {type: 0x30, children: args}; |
33 } | 35 } |
34 function obj(id) | 36 function obj(id) |
35 { | 37 { |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
102 let len = readLength(); | 104 let len = readLength(); |
103 if ("type" in curTempl && curTempl.type != type) | 105 if ("type" in curTempl && curTempl.type != type) |
104 throw "Unexpected type"; | 106 throw "Unexpected type"; |
105 if ("content" in curTempl && curTempl.content != data.substr(pos, len)) | 107 if ("content" in curTempl && curTempl.content != data.substr(pos, len)) |
106 throw "Unexpected content"; | 108 throw "Unexpected content"; |
107 if ("out" in curTempl) | 109 if ("out" in curTempl) |
108 out[curTempl.out] = new BigInteger(data.substr(pos, len), 256); | 110 out[curTempl.out] = new BigInteger(data.substr(pos, len), 256); |
109 if ("children" in curTempl) | 111 if ("children" in curTempl) |
110 { | 112 { |
111 let i; | 113 let i; |
112 let end; | 114 let end; |
Sebastian Noack
2017/02/21 09:19:31
These variables aren't used outside of the loop, s
kzar
2017/02/21 10:37:02
They are used outside of the loop.
| |
113 for (i = 0, end = pos + len; pos < end; i++) | 115 for (i = 0, end = pos + len; pos < end; i++) |
114 { | 116 { |
115 if (i >= curTempl.children.length) | 117 if (i >= curTempl.children.length) |
116 throw "Too many children"; | 118 throw "Too many children"; |
117 readNode(curTempl.children[i]); | 119 readNode(curTempl.children[i]); |
118 } | 120 } |
119 if (i < curTempl.children.length) | 121 if (i < curTempl.children.length) |
120 throw "Too few children"; | 122 throw "Too few children"; |
121 if (pos > end) | 123 if (pos > end) |
122 throw "Children too large"; | 124 throw "Children too large"; |
(...skipping 12 matching lines...) Expand all Loading... | |
135 readNode(templ); | 137 readNode(templ); |
136 if (pos != data.length) | 138 if (pos != data.length) |
137 throw "Too much data"; | 139 throw "Too much data"; |
138 return out; | 140 return out; |
139 } | 141 } |
140 | 142 |
141 /** | 143 /** |
142 * Reads a BER-encoded RSA public key. On success returns an object with the | 144 * Reads a BER-encoded RSA public key. On success returns an object with the |
143 * properties n and e (the components of the key), otherwise null. | 145 * properties n and e (the components of the key), otherwise null. |
144 * @param {string} key | 146 * @param {string} key |
145 * @return {Object|null} | 147 * @return {?Object} |
146 */ | 148 */ |
147 function readPublicKey(key) | 149 function readPublicKey(key) |
148 { | 150 { |
149 try | 151 try |
150 { | 152 { |
151 return readASN1(atob(key), publicKeyTemplate); | 153 return readASN1(atob(key), publicKeyTemplate); |
152 } | 154 } |
153 catch (e) | 155 catch (e) |
154 { | 156 { |
155 console.warn("Invalid RSA public key: " + e); | 157 console.warn("Invalid RSA public key: " + e); |
(...skipping 22 matching lines...) Expand all Loading... | |
178 let digest = sigInt.modPowInt(keyData.e, keyData.n).toString(256); | 180 let digest = sigInt.modPowInt(keyData.e, keyData.n).toString(256); |
179 | 181 |
180 try | 182 try |
181 { | 183 { |
182 let pos = 0; | 184 let pos = 0; |
183 let next = () => digest.charCodeAt(pos++); | 185 let next = () => digest.charCodeAt(pos++); |
184 | 186 |
185 // Skip padding, see http://tools.ietf.org/html/rfc3447#section-9.2 step 5 | 187 // Skip padding, see http://tools.ietf.org/html/rfc3447#section-9.2 step 5 |
186 if (next() != 1) | 188 if (next() != 1) |
187 throw "Wrong padding in signature digest"; | 189 throw "Wrong padding in signature digest"; |
188 while (next() == 255) | 190 while (next() == 255) {} |
189 { | |
190 // Empty | |
Sebastian Noack
2017/02/21 09:19:31
I start to wonder what's the point of the no-empty
kzar
2017/02/21 10:37:01
Done. https://codereview.adblockplus.org/29376719/
| |
191 } | |
192 if (digest.charCodeAt(pos - 1) != 0) | 191 if (digest.charCodeAt(pos - 1) != 0) |
193 throw "Wrong padding in signature digest"; | 192 throw "Wrong padding in signature digest"; |
194 | 193 |
195 // Rest is an ASN.1 structure, get the SHA1 hash from it and compare to | 194 // Rest is an ASN.1 structure, get the SHA1 hash from it and compare to |
196 // the real one | 195 // the real one |
197 let {sha1} = readASN1(digest.substr(pos), signatureTemplate); | 196 let {sha1} = readASN1(digest.substr(pos), signatureTemplate); |
198 let expected = new BigInteger(rusha.digest(data), 16); | 197 let expected = new BigInteger(rusha.digest(data), 16); |
199 return (sha1.compareTo(expected) == 0); | 198 return (sha1.compareTo(expected) == 0); |
200 } | 199 } |
201 catch (e) | 200 catch (e) |
202 { | 201 { |
203 console.warn("Invalid encrypted signature: " + e); | 202 console.warn("Invalid encrypted signature: " + e); |
204 return false; | 203 return false; |
205 } | 204 } |
206 } | 205 } |
207 exports.verifySignature = verifySignature; | 206 exports.verifySignature = verifySignature; |
LEFT | RIGHT |