OLD | NEW |
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-present eyeo GmbH | 3 * Copyright (C) 2006-present 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 */ | 18 /* global console */ |
19 | 19 |
20 "use strict"; | 20 "use strict"; |
21 | 21 |
22 /** | 22 /** |
23 * 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. |
24 */ | 24 */ |
25 | 25 |
26 const {BigInteger} = require("jsbn"); | 26 import {BigInteger} from "jsbn"; |
27 const Rusha = require("rusha"); | 27 import Rusha from "rusha"; |
28 | 28 |
29 let rusha = new Rusha(); | 29 let rusha = new Rusha(); |
30 | 30 |
31 // Define ASN.1 templates for the data structures used | 31 // Define ASN.1 templates for the data structures used |
32 function seq(...args) | 32 function seq(...args) |
33 { | 33 { |
34 return {type: 0x30, children: args}; | 34 return {type: 0x30, children: args}; |
35 } | 35 } |
36 function obj(id) | 36 function obj(id) |
37 { | 37 { |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 } | 159 } |
160 } | 160 } |
161 | 161 |
162 /** | 162 /** |
163 * Checks whether the signature is valid for the given public key and data. | 163 * Checks whether the signature is valid for the given public key and data. |
164 * @param {string} key | 164 * @param {string} key |
165 * @param {string} signature | 165 * @param {string} signature |
166 * @param {string} data | 166 * @param {string} data |
167 * @return {boolean} | 167 * @return {boolean} |
168 */ | 168 */ |
169 function verifySignature(key, signature, data) | 169 export function verifySignature(key, signature, data) |
170 { | 170 { |
171 let keyData = readPublicKey(key); | 171 let keyData = readPublicKey(key); |
172 if (!keyData) | 172 if (!keyData) |
173 return false; | 173 return false; |
174 | 174 |
175 // We need the exponent as regular number | 175 // We need the exponent as regular number |
176 keyData.e = parseInt(keyData.e.toString(16), 16); | 176 keyData.e = parseInt(keyData.e.toString(16), 16); |
177 | 177 |
178 // Decrypt signature data using RSA algorithm | 178 // Decrypt signature data using RSA algorithm |
179 let sigInt = new BigInteger(atob(signature), 256); | 179 let sigInt = new BigInteger(atob(signature), 256); |
(...skipping 16 matching lines...) Expand all Loading... |
196 let {sha1} = readASN1(digest.substr(pos), signatureTemplate); | 196 let {sha1} = readASN1(digest.substr(pos), signatureTemplate); |
197 let expected = new BigInteger(rusha.digest(data), 16); | 197 let expected = new BigInteger(rusha.digest(data), 16); |
198 return (sha1.compareTo(expected) == 0); | 198 return (sha1.compareTo(expected) == 0); |
199 } | 199 } |
200 catch (e) | 200 catch (e) |
201 { | 201 { |
202 console.warn("Invalid encrypted signature: " + e); | 202 console.warn("Invalid encrypted signature: " + e); |
203 return false; | 203 return false; |
204 } | 204 } |
205 } | 205 } |
206 exports.verifySignature = verifySignature; | |
OLD | NEW |