1
|
/**
|
2
|
* This file is part of Haketilo.
|
3
|
*
|
4
|
* Functions: Operations related to "signing" of data.
|
5
|
*
|
6
|
* Copyright (C) 2021 Wojtek Kosior
|
7
|
*
|
8
|
* This program is free software: you can redistribute it and/or modify
|
9
|
* it under the terms of the GNU General Public License as published by
|
10
|
* the Free Software Foundation, either version 3 of the License, or
|
11
|
* (at your option) any later version.
|
12
|
*
|
13
|
* This program is distributed in the hope that it will be useful,
|
14
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16
|
* GNU General Public License for more details.
|
17
|
*
|
18
|
* As additional permission under GNU GPL version 3 section 7, you
|
19
|
* may distribute forms of that code without the copy of the GNU
|
20
|
* GPL normally required by section 4, provided you include this
|
21
|
* license notice and, in case of non-source distribution, a URL
|
22
|
* through which recipients can access the Corresponding Source.
|
23
|
* If you modify file(s) with this exception, you may extend this
|
24
|
* exception to your version of the file(s), but you are not
|
25
|
* obligated to do so. If you do not wish to do so, delete this
|
26
|
* exception statement from your version.
|
27
|
*
|
28
|
* As a special exception to the GPL, any HTML file which merely
|
29
|
* makes function calls to this code, and for that purpose
|
30
|
* includes it by reference shall be deemed a separate work for
|
31
|
* copyright law purposes. If you modify this code, you may extend
|
32
|
* this exception to your version of the code, but you are not
|
33
|
* obligated to do so. If you do not wish to do so, delete this
|
34
|
* exception statement from your version.
|
35
|
*
|
36
|
* You should have received a copy of the GNU General Public License
|
37
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
38
|
*
|
39
|
* I, Wojtek Kosior, thereby promise not to sue for violation of this file's
|
40
|
* license. Although I request that you do not make use this code in a
|
41
|
* proprietary program, I am not going to enforce this in court.
|
42
|
*/
|
43
|
|
44
|
/*
|
45
|
* IMPORTS_START
|
46
|
* IMPORT sha256
|
47
|
* IMPORT browser
|
48
|
* IMPORT is_mozilla
|
49
|
* IMPORTS_END
|
50
|
*/
|
51
|
|
52
|
/*
|
53
|
* In order to make certain data synchronously accessible in certain contexts,
|
54
|
* Haketilo smuggles it in string form in places like cookies, URLs and headers.
|
55
|
* When using the smuggled data, we first need to make sure it isn't spoofed.
|
56
|
* For that, we use this pseudo-signing mechanism.
|
57
|
*
|
58
|
* Despite what name suggests, no assymetric cryptography is involved, as it
|
59
|
* would bring no additional benefits and would incur bigger performance
|
60
|
* overhead. Instead, we hash the string data together with some secret value
|
61
|
* that is supposed to be known only by this browser instance. Resulting hash
|
62
|
* sum plays the role of the signature. In the hash we also include current
|
63
|
* time. This way, even if signed data leaks (which shouldn't happen in the
|
64
|
* first place), an attacker won't be able to re-use it indefinitely.
|
65
|
*
|
66
|
* The secret shared between execution contexts has to be available
|
67
|
* synchronously. Under Mozilla, this is the extension's per-session id. Under
|
68
|
* Chromium, this is a dummy web-accessible-resource name that resides in the
|
69
|
* manifest and is supposed to be constructed by each user using a unique value
|
70
|
* (this is done automatically by `build.sh').
|
71
|
*/
|
72
|
|
73
|
function get_secret()
|
74
|
{
|
75
|
if (is_mozilla)
|
76
|
return browser.runtime.getURL("dummy");
|
77
|
|
78
|
return chrome.runtime.getManifest().web_accessible_resources
|
79
|
.map(r => /^chromium-key-dummy-file-(.*)/.exec(r)).filter(r => r)[0][1];
|
80
|
}
|
81
|
|
82
|
function extract_signed(signature, signed_data)
|
83
|
{
|
84
|
const match = /^([1-9][0-9]{12}|0)_(.*)$/.exec(signed_data);
|
85
|
if (!match)
|
86
|
return {fail: "bad format"};
|
87
|
|
88
|
const result = {time: parseInt(match[1]), data: match[2]};
|
89
|
if (sign_data(result.data, result.time)[0] !== signature)
|
90
|
result.fail = "bad signature";
|
91
|
|
92
|
return result;
|
93
|
}
|
94
|
|
95
|
/*
|
96
|
* Sign a given string for a given time. Time should be either 0 or in the range
|
97
|
* 10^12 <= time < 10^13.
|
98
|
*/
|
99
|
function sign_data(data, time) {
|
100
|
return [sha256(get_secret() + time + data), `${time}_${data}`];
|
101
|
}
|
102
|
|
103
|
/*
|
104
|
* EXPORTS_START
|
105
|
* EXPORT extract_signed
|
106
|
* EXPORT sign_data
|
107
|
* EXPORTS_END
|
108
|
*/
|