1
|
/**
|
2
|
* part of Hachette
|
3
|
* Functions related to "signing" of data, refactored to a separate file.
|
4
|
*
|
5
|
* Copyright (C) 2021 Wojtek Kosior
|
6
|
* Redistribution terms are gathered in the `copyright' file.
|
7
|
*/
|
8
|
|
9
|
/*
|
10
|
* IMPORTS_START
|
11
|
* IMPORT sha256
|
12
|
* IMPORT browser
|
13
|
* IMPORT is_chrome
|
14
|
* IMPORTS_END
|
15
|
*/
|
16
|
|
17
|
/*
|
18
|
* In order to make certain data synchronously accessible in certain contexts,
|
19
|
* hachette smuggles it in string form in places like cookies, URLs and headers.
|
20
|
* When using the smuggled data, we first need to make sure it isn't spoofed.
|
21
|
* For that, we use this pseudo-signing mechanism.
|
22
|
*
|
23
|
* Despite what name suggests, no assymetric cryptography is involved, as it
|
24
|
* would bring no additional benefits and would incur bigger performance
|
25
|
* overhead. Instead, we hash the string data together with some secret value
|
26
|
* that is supposed to be known only by this browser instance. Resulting hash
|
27
|
* sum plays the role of the signature. In the hash we also include current
|
28
|
* time. This way, even if signed data leaks (which shouldn't happen in the
|
29
|
* first place), an attacker won't be able to re-use it indefinitely.
|
30
|
*
|
31
|
* The secret shared between execution contexts has to be available
|
32
|
* synchronously. Under Mozilla, this is the extension's per-session id. Under
|
33
|
* Chromium, this is the key that resides in the manifest.
|
34
|
*
|
35
|
* An idea to (under Chromium) instead store the secret in a file fetched
|
36
|
* synchronously using XMLHttpRequest is being considered.
|
37
|
*/
|
38
|
|
39
|
function get_secret()
|
40
|
{
|
41
|
if (is_chrome)
|
42
|
return browser.runtime.getManifest().key.substring(0, 50);
|
43
|
else
|
44
|
return browser.runtime.getURL("dummy");
|
45
|
}
|
46
|
|
47
|
function extract_signed(signature, signed_data)
|
48
|
{
|
49
|
const match = /^([1-9][0-9]{12}|0)_(.*)$/.exec(signed_data);
|
50
|
if (!match)
|
51
|
return {fail: "bad format"};
|
52
|
|
53
|
const result = {time: parseInt(match[1]), data: match[2]};
|
54
|
if (sign_data(result.data, result.time)[0] !== signature)
|
55
|
result.fail = "bad signature";
|
56
|
|
57
|
return result;
|
58
|
}
|
59
|
|
60
|
/*
|
61
|
* Sign a given string for a given time. Time should be either 0 or in the range
|
62
|
* 10^12 <= time < 10^13.
|
63
|
*/
|
64
|
function sign_data(data, time) {
|
65
|
return [sha256(get_secret() + time + data), `${time}_${data}`];
|
66
|
}
|
67
|
|
68
|
/*
|
69
|
* EXPORTS_START
|
70
|
* EXPORT extract_signed
|
71
|
* EXPORT sign_data
|
72
|
* EXPORTS_END
|
73
|
*/
|