| OLD | NEW | 
|---|
| (Empty) |  | 
|  | 1 /* | 
|  | 2  * This file is part of Adblock Plus <https://adblockplus.org/>, | 
|  | 3  * Copyright (C) 2006-2016 Eyeo GmbH | 
|  | 4  * | 
|  | 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 | 
|  | 7  * published by the Free Software Foundation. | 
|  | 8  * | 
|  | 9  * Adblock Plus is distributed in the hope that it will be useful, | 
|  | 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 12  * GNU General Public License for more details. | 
|  | 13  * | 
|  | 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/>. | 
|  | 16  */ | 
|  | 17 | 
|  | 18 /** | 
|  | 19  * This is a specialized RSA library meant only to verify SHA1-based signatures. | 
|  | 20  */ | 
|  | 21 | 
|  | 22 let {BigInteger} = require("jsbn"); | 
|  | 23 let Rusha = require("rusha"); | 
|  | 24 | 
|  | 25 let rusha = new Rusha(); | 
|  | 26 | 
|  | 27 // Define ASN.1 templates for the data structures used | 
|  | 28 function seq() | 
|  | 29 { | 
|  | 30   return {type: 0x30, children: Array.prototype.slice.call(arguments)}; | 
|  | 31 } | 
|  | 32 function obj(id) | 
|  | 33 { | 
|  | 34   return {type: 0x06, content: id}; | 
|  | 35 } | 
|  | 36 function bitStr(contents) | 
|  | 37 { | 
|  | 38   return {type: 0x03, encapsulates: contents}; | 
|  | 39 } | 
|  | 40 function intResult(id) | 
|  | 41 { | 
|  | 42   return {type: 0x02, out: id}; | 
|  | 43 } | 
|  | 44 function octetResult(id) | 
|  | 45 { | 
|  | 46   return {type: 0x04, out: id}; | 
|  | 47 } | 
|  | 48 | 
|  | 49 // 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 | 
|  | 51 var publicKeyTemplate = seq(seq(obj("\x2A\x86\x48\x86\xF7\x0D\x01\x01\x01"), {})
     , bitStr(seq(intResult("n"), intResult("e")))); | 
|  | 52 | 
|  | 53 // 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 | 
|  | 55 var signatureTemplate = seq(seq(obj("\x2B\x0E\x03\x02\x1A"), {}), octetResult("s
     ha1")); | 
|  | 56 | 
|  | 57 /** | 
|  | 58  * 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 | 
|  | 60  * object containing result properties is returned. | 
|  | 61  * | 
|  | 62  * See http://luca.ntop.org/Teaching/Appunti/asn1.html for info on the format. | 
|  | 63  */ | 
|  | 64 function readASN1(data, templ) | 
|  | 65 { | 
|  | 66   var pos = 0; | 
|  | 67   function next() | 
|  | 68   { | 
|  | 69     return data.charCodeAt(pos++); | 
|  | 70   } | 
|  | 71 | 
|  | 72   function readLength() | 
|  | 73   { | 
|  | 74     var len = next(); | 
|  | 75     if (len & 0x80) | 
|  | 76     { | 
|  | 77       var cnt = len & 0x7F; | 
|  | 78       if (cnt > 2 || cnt == 0) | 
|  | 79         throw "Unsupported length"; | 
|  | 80 | 
|  | 81       len = 0; | 
|  | 82       for (var i = 0; i < cnt; i++) | 
|  | 83         len += next() << (cnt - 1 - i) * 8; | 
|  | 84       return len; | 
|  | 85     } | 
|  | 86     else | 
|  | 87       return len; | 
|  | 88   } | 
|  | 89 | 
|  | 90   function readNode(curTempl) | 
|  | 91   { | 
|  | 92     var type = next(); | 
|  | 93     var len = readLength(); | 
|  | 94     if ("type" in curTempl && curTempl.type != type) | 
|  | 95       throw "Unexpected type"; | 
|  | 96     if ("content" in curTempl && curTempl.content != data.substr(pos, len)) | 
|  | 97       throw "Unexpected content"; | 
|  | 98     if ("out" in curTempl) | 
|  | 99       out[curTempl.out] = new BigInteger(data.substr(pos, len), 256); | 
|  | 100     if ("children" in curTempl) | 
|  | 101     { | 
|  | 102       var i, end; | 
|  | 103       for (i = 0, end = pos + len; pos < end; i++) | 
|  | 104       { | 
|  | 105         if (i >= curTempl.children.length) | 
|  | 106           throw "Too many children"; | 
|  | 107         readNode(curTempl.children[i]); | 
|  | 108       } | 
|  | 109       if (i < curTempl.children.length) | 
|  | 110         throw "Too few children"; | 
|  | 111       if (pos > end) | 
|  | 112         throw "Children too large"; | 
|  | 113     } | 
|  | 114     else if ("encapsulates" in curTempl) | 
|  | 115     { | 
|  | 116       if (next() != 0) | 
|  | 117         throw "Encapsulation expected"; | 
|  | 118       readNode(curTempl.encapsulates); | 
|  | 119     } | 
|  | 120     else | 
|  | 121       pos += len; | 
|  | 122   } | 
|  | 123 | 
|  | 124   var out = {}; | 
|  | 125   readNode(templ); | 
|  | 126   if (pos != data.length) | 
|  | 127     throw "Too much data"; | 
|  | 128   return out; | 
|  | 129 } | 
|  | 130 | 
|  | 131 /** | 
|  | 132  * 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. | 
|  | 134  */ | 
|  | 135 function readPublicKey(key) | 
|  | 136 { | 
|  | 137   try | 
|  | 138   { | 
|  | 139     return readASN1(atob(key), publicKeyTemplate); | 
|  | 140   } | 
|  | 141   catch (e) | 
|  | 142   { | 
|  | 143     console.log("Invalid RSA public key: " + e); | 
|  | 144     return null; | 
|  | 145   } | 
|  | 146 } | 
|  | 147 | 
|  | 148 /** | 
|  | 149  * Checks whether the signature is valid for the given public key and data. | 
|  | 150  */ | 
|  | 151 function verifySignature(key, signature, data) | 
|  | 152 { | 
|  | 153   var keyData = readPublicKey(key); | 
|  | 154   if (!keyData) | 
|  | 155     return false; | 
|  | 156 | 
|  | 157   // We need the exponent as regular number | 
|  | 158   keyData.e = parseInt(keyData.e.toString(16), 16); | 
|  | 159 | 
|  | 160   // Decrypt signature data using RSA algorithm | 
|  | 161   var sigInt = new BigInteger(atob(signature), 256); | 
|  | 162   var digest = sigInt.modPowInt(keyData.e, keyData.n).toString(256); | 
|  | 163 | 
|  | 164   try | 
|  | 165   { | 
|  | 166     var pos = 0; | 
|  | 167     function next() | 
|  | 168     { | 
|  | 169       return digest.charCodeAt(pos++); | 
|  | 170     } | 
|  | 171 | 
|  | 172     // Skip padding, see http://tools.ietf.org/html/rfc3447#section-9.2 step 5 | 
|  | 173     if (next() != 1) | 
|  | 174       throw "Wrong padding in signature digest"; | 
|  | 175     while (next() == 255) {} | 
|  | 176     if (digest.charCodeAt(pos - 1) != 0) | 
|  | 177       throw "Wrong padding in signature digest"; | 
|  | 178 | 
|  | 179     // Rest is an ASN.1 structure, get the SHA1 hash from it and compare to | 
|  | 180     // the real one | 
|  | 181     var sha1 = readASN1(digest.substr(pos), signatureTemplate).sha1; | 
|  | 182     var expected = new BigInteger(rusha.digest(data), 16); | 
|  | 183     return (sha1.compareTo(expected) == 0); | 
|  | 184   } | 
|  | 185   catch (e) | 
|  | 186   { | 
|  | 187     console.log("Invalid encrypted signature: " + e); | 
|  | 188     return false; | 
|  | 189   } | 
|  | 190 } | 
|  | 191 exports.verifySignature = verifySignature; | 
| OLD | NEW | 
|---|