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-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 "use strict"; |
| 19 |
18 /** | 20 /** |
19 * This is a specialized RSA library meant only to verify SHA1-based signatures. | 21 * This is a specialized RSA library meant only to verify SHA1-based signatures. |
20 */ | 22 */ |
21 | 23 |
22 let {BigInteger} = require("jsbn"); | 24 const {BigInteger} = require("jsbn"); |
23 let Rusha = require("rusha"); | 25 const Rusha = require("rusha"); |
24 | 26 |
25 let rusha = new Rusha(); | 27 let rusha = new Rusha(); |
26 | 28 |
27 // Define ASN.1 templates for the data structures used | 29 // Define ASN.1 templates for the data structures used |
28 function seq() | 30 function seq(...args) |
29 { | 31 { |
30 return {type: 0x30, children: Array.prototype.slice.call(arguments)}; | 32 return {type: 0x30, children: args}; |
31 } | 33 } |
32 function obj(id) | 34 function obj(id) |
33 { | 35 { |
34 return {type: 0x06, content: id}; | 36 return {type: 0x06, content: id}; |
35 } | 37 } |
36 function bitStr(contents) | 38 function bitStr(contents) |
37 { | 39 { |
38 return {type: 0x03, encapsulates: contents}; | 40 return {type: 0x03, encapsulates: contents}; |
39 } | 41 } |
40 function intResult(id) | 42 function intResult(id) |
41 { | 43 { |
42 return {type: 0x02, out: id}; | 44 return {type: 0x02, out: id}; |
43 } | 45 } |
44 function octetResult(id) | 46 function octetResult(id) |
45 { | 47 { |
46 return {type: 0x04, out: id}; | 48 return {type: 0x04, out: id}; |
47 } | 49 } |
48 | 50 |
49 // See http://www.cryptopp.com/wiki/Keys_and_Formats#RSA_PublicKey | 51 // See http://www.cryptopp.com/wiki/Keys_and_Formats#RSA_PublicKey |
50 // 2A 86 48 86 F7 0D 01 01 01 means 1.2.840.113549.1.1.1 | 52 // 2A 86 48 86 F7 0D 01 01 01 means 1.2.840.113549.1.1.1 |
51 var publicKeyTemplate = seq(seq(obj("\x2A\x86\x48\x86\xF7\x0D\x01\x01\x01"), {})
, bitStr(seq(intResult("n"), intResult("e")))); | 53 let publicKeyTemplate = seq( |
| 54 seq(obj("\x2A\x86\x48\x86\xF7\x0D\x01\x01\x01"), {}), |
| 55 bitStr(seq(intResult("n"), intResult("e"))) |
| 56 ); |
52 | 57 |
53 // See http://tools.ietf.org/html/rfc3447#section-9.2 step 2 | 58 // See http://tools.ietf.org/html/rfc3447#section-9.2 step 2 |
54 // 2B 0E 03 02 1A means 1.3.14.3.2.26 | 59 // 2B 0E 03 02 1A means 1.3.14.3.2.26 |
55 var signatureTemplate = seq(seq(obj("\x2B\x0E\x03\x02\x1A"), {}), octetResult("s
ha1")); | 60 let signatureTemplate = seq( |
| 61 seq(obj("\x2B\x0E\x03\x02\x1A"), {}), |
| 62 octetResult("sha1") |
| 63 ); |
56 | 64 |
57 /** | 65 /** |
58 * Reads ASN.1 data matching the template passed in. This will throw an | 66 * Reads ASN.1 data matching the template passed in. This will throw an |
59 * exception if the data format doesn't match the template. On success an | 67 * exception if the data format doesn't match the template. On success an |
60 * object containing result properties is returned. | 68 * object containing result properties is returned. |
61 * | 69 * @see http://luca.ntop.org/Teaching/Appunti/asn1.html for info on the format. |
62 * See http://luca.ntop.org/Teaching/Appunti/asn1.html for info on the format. | 70 * @param {string} data |
| 71 * @param {Object} templ |
| 72 * @returns {Object} |
63 */ | 73 */ |
64 function readASN1(data, templ) | 74 function readASN1(data, templ) |
65 { | 75 { |
66 var pos = 0; | 76 let pos = 0; |
67 function next() | 77 function next() |
68 { | 78 { |
69 return data.charCodeAt(pos++); | 79 return data.charCodeAt(pos++); |
70 } | 80 } |
71 | 81 |
72 function readLength() | 82 function readLength() |
73 { | 83 { |
74 var len = next(); | 84 let len = next(); |
75 if (len & 0x80) | 85 if (len & 0x80) |
76 { | 86 { |
77 var cnt = len & 0x7F; | 87 let cnt = len & 0x7F; |
78 if (cnt > 2 || cnt == 0) | 88 if (cnt > 2 || cnt == 0) |
79 throw "Unsupported length"; | 89 throw "Unsupported length"; |
80 | 90 |
81 len = 0; | 91 len = 0; |
82 for (var i = 0; i < cnt; i++) | 92 for (let i = 0; i < cnt; i++) |
83 len += next() << (cnt - 1 - i) * 8; | 93 len += next() << (cnt - 1 - i) * 8; |
84 return len; | 94 return len; |
85 } | 95 } |
86 else | 96 return len; |
87 return len; | |
88 } | 97 } |
89 | 98 |
90 function readNode(curTempl) | 99 function readNode(curTempl) |
91 { | 100 { |
92 var type = next(); | 101 let type = next(); |
93 var len = readLength(); | 102 let len = readLength(); |
94 if ("type" in curTempl && curTempl.type != type) | 103 if ("type" in curTempl && curTempl.type != type) |
95 throw "Unexpected type"; | 104 throw "Unexpected type"; |
96 if ("content" in curTempl && curTempl.content != data.substr(pos, len)) | 105 if ("content" in curTempl && curTempl.content != data.substr(pos, len)) |
97 throw "Unexpected content"; | 106 throw "Unexpected content"; |
98 if ("out" in curTempl) | 107 if ("out" in curTempl) |
99 out[curTempl.out] = new BigInteger(data.substr(pos, len), 256); | 108 out[curTempl.out] = new BigInteger(data.substr(pos, len), 256); |
100 if ("children" in curTempl) | 109 if ("children" in curTempl) |
101 { | 110 { |
102 var i, end; | 111 let i; |
| 112 let end; |
103 for (i = 0, end = pos + len; pos < end; i++) | 113 for (i = 0, end = pos + len; pos < end; i++) |
104 { | 114 { |
105 if (i >= curTempl.children.length) | 115 if (i >= curTempl.children.length) |
106 throw "Too many children"; | 116 throw "Too many children"; |
107 readNode(curTempl.children[i]); | 117 readNode(curTempl.children[i]); |
108 } | 118 } |
109 if (i < curTempl.children.length) | 119 if (i < curTempl.children.length) |
110 throw "Too few children"; | 120 throw "Too few children"; |
111 if (pos > end) | 121 if (pos > end) |
112 throw "Children too large"; | 122 throw "Children too large"; |
113 } | 123 } |
114 else if ("encapsulates" in curTempl) | 124 else if ("encapsulates" in curTempl) |
115 { | 125 { |
116 if (next() != 0) | 126 if (next() != 0) |
117 throw "Encapsulation expected"; | 127 throw "Encapsulation expected"; |
118 readNode(curTempl.encapsulates); | 128 readNode(curTempl.encapsulates); |
119 } | 129 } |
120 else | 130 else |
121 pos += len; | 131 pos += len; |
122 } | 132 } |
123 | 133 |
124 var out = {}; | 134 let out = {}; |
125 readNode(templ); | 135 readNode(templ); |
126 if (pos != data.length) | 136 if (pos != data.length) |
127 throw "Too much data"; | 137 throw "Too much data"; |
128 return out; | 138 return out; |
129 } | 139 } |
130 | 140 |
131 /** | 141 /** |
132 * Reads a BER-encoded RSA public key. On success returns an object with the | 142 * Reads a BER-encoded RSA public key. On success returns an object with the |
133 * properties n and e (the components of the key), otherwise null. | 143 * properties n and e (the components of the key), otherwise null. |
| 144 * @param {string} key |
| 145 * @return {Object|null} |
134 */ | 146 */ |
135 function readPublicKey(key) | 147 function readPublicKey(key) |
136 { | 148 { |
137 try | 149 try |
138 { | 150 { |
139 return readASN1(atob(key), publicKeyTemplate); | 151 return readASN1(atob(key), publicKeyTemplate); |
140 } | 152 } |
141 catch (e) | 153 catch (e) |
142 { | 154 { |
143 console.log("Invalid RSA public key: " + e); | 155 console.warn("Invalid RSA public key: " + e); |
144 return null; | 156 return null; |
145 } | 157 } |
146 } | 158 } |
147 | 159 |
148 /** | 160 /** |
149 * Checks whether the signature is valid for the given public key and data. | 161 * Checks whether the signature is valid for the given public key and data. |
| 162 * @param {string} key |
| 163 * @param {string} signature |
| 164 * @param {string} data |
| 165 * @return {boolean} |
150 */ | 166 */ |
151 function verifySignature(key, signature, data) | 167 function verifySignature(key, signature, data) |
152 { | 168 { |
153 var keyData = readPublicKey(key); | 169 let keyData = readPublicKey(key); |
154 if (!keyData) | 170 if (!keyData) |
155 return false; | 171 return false; |
156 | 172 |
157 // We need the exponent as regular number | 173 // We need the exponent as regular number |
158 keyData.e = parseInt(keyData.e.toString(16), 16); | 174 keyData.e = parseInt(keyData.e.toString(16), 16); |
159 | 175 |
160 // Decrypt signature data using RSA algorithm | 176 // Decrypt signature data using RSA algorithm |
161 var sigInt = new BigInteger(atob(signature), 256); | 177 let sigInt = new BigInteger(atob(signature), 256); |
162 var digest = sigInt.modPowInt(keyData.e, keyData.n).toString(256); | 178 let digest = sigInt.modPowInt(keyData.e, keyData.n).toString(256); |
163 | 179 |
164 try | 180 try |
165 { | 181 { |
166 var pos = 0; | 182 let pos = 0; |
167 function next() | 183 let next = () => digest.charCodeAt(pos++); |
168 { | |
169 return digest.charCodeAt(pos++); | |
170 } | |
171 | 184 |
172 // Skip padding, see http://tools.ietf.org/html/rfc3447#section-9.2 step 5 | 185 // Skip padding, see http://tools.ietf.org/html/rfc3447#section-9.2 step 5 |
173 if (next() != 1) | 186 if (next() != 1) |
174 throw "Wrong padding in signature digest"; | 187 throw "Wrong padding in signature digest"; |
175 while (next() == 255) {} | 188 while (next() == 255) {} |
176 if (digest.charCodeAt(pos - 1) != 0) | 189 if (digest.charCodeAt(pos - 1) != 0) |
177 throw "Wrong padding in signature digest"; | 190 throw "Wrong padding in signature digest"; |
178 | 191 |
179 // Rest is an ASN.1 structure, get the SHA1 hash from it and compare to | 192 // Rest is an ASN.1 structure, get the SHA1 hash from it and compare to |
180 // the real one | 193 // the real one |
181 var sha1 = readASN1(digest.substr(pos), signatureTemplate).sha1; | 194 let {sha1} = readASN1(digest.substr(pos), signatureTemplate); |
182 var expected = new BigInteger(rusha.digest(data), 16); | 195 let expected = new BigInteger(rusha.digest(data), 16); |
183 return (sha1.compareTo(expected) == 0); | 196 return (sha1.compareTo(expected) == 0); |
184 } | 197 } |
185 catch (e) | 198 catch (e) |
186 { | 199 { |
187 console.log("Invalid encrypted signature: " + e); | 200 console.warn("Invalid encrypted signature: " + e); |
188 return false; | 201 return false; |
189 } | 202 } |
190 } | 203 } |
191 exports.verifySignature = verifySignature; | 204 exports.verifySignature = verifySignature; |
OLD | NEW |