Project

General

Profile

Download (2.3 KB) Statistics
| Branch: | Tag: | Revision:

haketilo / common / signing.js @ 2bd35bc4

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
 * Redistribution terms are gathered in the `copyright' file.
8
 */
9

    
10
/*
11
 * IMPORTS_START
12
 * IMPORT sha256
13
 * IMPORT browser
14
 * IMPORT is_mozilla
15
 * IMPORTS_END
16
 */
17

    
18
/*
19
 * In order to make certain data synchronously accessible in certain contexts,
20
 * Haketilo smuggles it in string form in places like cookies, URLs and headers.
21
 * When using the smuggled data, we first need to make sure it isn't spoofed.
22
 * For that, we use this pseudo-signing mechanism.
23
 *
24
 * Despite what name suggests, no assymetric cryptography is involved, as it
25
 * would bring no additional benefits and would incur bigger performance
26
 * overhead. Instead, we hash the string data together with some secret value
27
 * that is supposed to be known only by this browser instance. Resulting hash
28
 * sum plays the role of the signature. In the hash we also include current
29
 * time. This way, even if signed data leaks (which shouldn't happen in the
30
 * first place), an attacker won't be able to re-use it indefinitely.
31
 *
32
 * The secret shared between execution contexts has to be available
33
 * synchronously. Under Mozilla, this is the extension's per-session id. Under
34
 * Chromium, this is a dummy web-accessible-resource name that resides in the
35
 * manifest and is supposed to be constructed by each user using a unique value
36
 * (this is done automatically by `build.sh').
37
 */
38

    
39
function get_secret()
40
{
41
    if (is_mozilla)
42
	return browser.runtime.getURL("dummy");
43

    
44
    return chrome.runtime.getManifest().web_accessible_resources
45
	.map(r => /^chromium-key-dummy-file-(.*)/.exec(r)).filter(r => r)[0][1];
46
}
47

    
48
function extract_signed(signature, signed_data)
49
{
50
    const match = /^([1-9][0-9]{12}|0)_(.*)$/.exec(signed_data);
51
    if (!match)
52
	return {fail: "bad format"};
53

    
54
    const result = {time: parseInt(match[1]), data: match[2]};
55
    if (sign_data(result.data, result.time)[0] !== signature)
56
	result.fail = "bad signature";
57

    
58
    return result;
59
}
60

    
61
/*
62
 * Sign a given string for a given time. Time should be either 0 or in the range
63
 * 10^12 <= time < 10^13.
64
 */
65
function sign_data(data, time) {
66
    return [sha256(get_secret() + time + data), `${time}_${data}`];
67
}
68

    
69
/*
70
 * EXPORTS_START
71
 * EXPORT extract_signed
72
 * EXPORT sign_data
73
 * EXPORTS_END
74
 */
(12-12/16)