Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/rsa.js

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

Powered by Google App Engine
This is Rietveld