-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCanary.js
166 lines (154 loc) · 4.32 KB
/
Canary.js
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
var path = require('path'),
cproc = require('child_process'),
Backlog = require('./Backlog'),
StreamReader = require('./StreamReader');
function Canary(dbname, pkring) {
/**
* dbname: file argument for Backlog constructor
* pkring: keyring file where the signer's public key is stored. empty string for default
*/
this.keyring = pkring ? path.resolve(pkring) : './default_keyring';
this.messages = new Backlog(dbname);
}
Canary.prototype.close = canaryClose;
Canary.prototype.getKey = canaryGetKey;
Canary.prototype.getLatest = canaryGetLatest;
Canary.prototype.getAll = canaryGetAll;
Canary.prototype.feedString = canaryAdd;
Canary.prototype.feedStream = canaryFeed;
Canary.prototype._add = canary_add;
function canaryGetKey(callback) {
var result = '';
var gpgexp = cproc.spawn('gpg',
['--no-default-keyring',
'--keyring', path.resolve(this.keyring),
'-a', '--export']
);
gpgexp.
on('error', function(err) {
callback({
name : err.name,
message : err.message
});
}).on('exit', function(code, signal) {
if(code !== 0)
callback({
name : "GPGExportError",
message : 'Nonzero exit status. Code: ' + code
});
});
gpgexp.stdout.on('data', function(data) {
result += data;
}).on('end', function() {
callback(null, result);
});
}
function canaryGetLatest(callback) {
return this.messages.getLatest(callback);
}
function canaryGetAll(callback) {
return this.messages.getAll(callback);
}
function canaryAdd(msg, callback) {
var result = [];
var stream = new StreamReader(msg);
var self = this;
// gpg --no-default-keyring --keyring this.keyring --trust-model always --verify
var gpgval = cproc.spawn('gpg',
['--no-default-keyring',
'--keyring', path.resolve(this.keyring),
'--trust-model', 'always',
'--verify']
);
gpgval.
on('error', function(err) {
cb({
name : err.name,
message : err.message
});
}).on('exit', function(code, signal) {
if(code !== 0)
callback({
name : "GPGVerifyError",
message : 'Nonzero exit status. Code: ' + code
});
});
// read stderr for "Good signature"
gpgval.stderr.setEncoding('utf8').
on('data', function(data) {
result.push(data);
}).on('end', function() {
var res = result.join('');
if(res.indexOf('Good signature') > -1) self._add(msg, callback);
else callback({
name : "GPGVerifyError",
message : res
});
});
// write message stream to gpg's stdin and close stream
stream.on('data', function(data) {
gpgval.stdin.write(data);
}).on('end', function() {
gpgval.stdin.end();
}).resume();
}
function canaryFeed(stream, callback) {
var result = [];
var msg = '';
var self = this;
// gpg --no-default-keyring --keyring this.keyring --trust-model always --verify
var gpgval = cproc.spawn('gpg',
['--no-default-keyring',
'--keyring', path.resolve(this.keyring),
'--trust-model', 'always',
'--verify']
);
gpgval.
on('error', callback).
on('exit', function(code, signal) {
if(code !== 0)
callback({
name : "GPGVerifyError",
message : 'Nonzero exit status. Code: ' + code
});
});
// read stderr for "Good signature"
gpgval.stderr.setEncoding('utf8').
on('data', function(data) {
result.push(data);
}).on('end', function() {
var res = result.join('');
if(res.indexOf('Good signature') > -1) self._add(msg, callback);
else callback({
name : "GPGVerifyError",
message : res
});
});
// write message stream to gpg's stdin and close stream
stream.on('data', function(data) {
gpgval.stdin.write(data);
msg += data;
}).on('end', function() {
gpgval.stdin.end();
});
}
function canary_add(msg, callback) {
var self = this;
this.messages.contains(msg, function(err, contains) {
if(err)
return callback({
name : err.name,
message : err.message
});
if(contains)
return callback({
name : "CanaryReplayError",
message : "Message not added: message is a replay."
});
self.messages.add(msg, callback);
});
}
function canaryClose(cb) {
this.messages.close(cb);
}
module.exports = Canary;