summaryrefslogtreecommitdiff
path: root/src/trust.js
blob: 027139605852ff8f08cd0ecb1738c304fdf6a2d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*!!obdef!!*/_ob=false;/*!!obdef!!*/
const fs = require('fs');
const md = require('./tpm/spark-md5');
const chalk = require('./tpm/chalk');

const encrypt = (text) => {

    return new Buffer.from(text).toString("hex");
};

const decrypt = (hash) => {
    
    return new Buffer.from(hash, "hex").toString("utf-8");
};

module.exports = {
    check: (file, luf) => {
        try {
            if (fs.existsSync(file)) {
                content = fs.readFileSync(file);
                hash = md.hash(content.toString());
                if (fs.existsSync("./keys.bin")) {
                    keys = JSON.parse(new Buffer.from(decrypt(fs.readFileSync("./keys.bin").toString()), "base64").toString("utf-8"));
                    if (hash === keys[file].key) {
                        console.log(chalk.green("File " + file + " has valid certificate: \n    Certificate ID: " + keys[file].id + "\n    Certificate Issuer: " + keys[file].issuer));
                    } else {
                        if (typeof keys[file] == "undefined") {
                            console.log(chalk.red("File " + file + " not in certificate database"));
                            process.exit(2);
                        } else {
                            console.log(chalk.red("File " + file + " has invalid signature: " + hash));
                            process.exit(2);
                        }
                    }
                } else {
                    console.log(chalk.yellow("No certificate database found, will skip check"));
                }
            } else {
                if (!luf) {
                    throw new Error("File not found");
                } else {
                    console.log(chalk.red("File not found: " + file));
                }
            }
        } catch (e) {
            console.log(chalk.redBright("Unable to check certificate for file " + file + ": error -1. Developers: did you added this file to the build tree instead of the source tree?"));
            if (!_ob) { console.error(e) };
            process.exit(2);
        }
    },
    add: (file, name, displayname) => {
        try {
            if (typeof displayname != "undefined") {
                dn = displayname;
            } else {
                dn = file;
            }
            if (fs.existsSync(file)) {
                content = fs.readFileSync(file);
                hash = md.hash(content.toString());
                if (fs.existsSync("./keys.bin")) {
                    keys = JSON.parse(new Buffer.from(decrypt(fs.readFileSync("./keys.bin").toString()), "base64").toString("utf-8"));
                } else {
                    keys = {};
                }
                if (typeof keys[dn] != "undefined") {
                    if (keys[dn].issuer !== name) {
                        console.log(chalk.red("File " + file + " was signed using a different issuer name (" + keys[file].issuer + "), cannot overwrite"));
                        process.exit(2);
                    }
                }
                keys[dn] = {};
                keys[dn].key = hash;
                keys[dn].issuer = name;
                keys[dn].id = Math.round(Math.random() * 100000);
                fs.writeFileSync("./keys.bin", encrypt(new Buffer.from(JSON.stringify(keys)).toString("base64")));
                if (typeof displayname != "undefined") {
                    console.log(chalk.green("Added " + file + " (shown as " + displayname + ") to certificates database, signed using " + name));
                } else {
                    console.log(chalk.green("Added " + file + " to certificates database, signed using " + name));
                }
            } else {
                throw new Error("File not found");
            }
        } catch (e) {
            console.log(chalk.redBright("Unable to add certificate to database: error -1"));
            if (!_ob) { console.error(e) };
            process.exit(2);
        }
    }
}