ssh-agent-js 2.0.4 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/publish-npm.yml +24 -0
- package/README.md +28 -1
- package/client/index.js +117 -77
- package/lib/_read.js +4 -3
- package/lib/_readFrame.js +51 -0
- package/package.json +1 -1
- package/server/agentd.js +36 -0
- package/server/index.js +178 -0
- package/server/socket.js +24 -0
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
name: publish-npm
|
|
2
|
+
on:
|
|
3
|
+
push:
|
|
4
|
+
tags: ['v*']
|
|
5
|
+
|
|
6
|
+
permissions:
|
|
7
|
+
contents: read
|
|
8
|
+
id-token: write
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
publish:
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
steps:
|
|
14
|
+
- uses: actions/checkout@v4
|
|
15
|
+
- uses: actions/setup-node@v4
|
|
16
|
+
with:
|
|
17
|
+
node-version: '24.x'
|
|
18
|
+
registry-url: 'https://registry.npmjs.org'
|
|
19
|
+
- run: npm install
|
|
20
|
+
- run: npm test
|
|
21
|
+
- name: Publish the tagged version
|
|
22
|
+
run: |
|
|
23
|
+
test "$GITHUB_REF_NAME" = "v$(node -p 'require("./package.json").version')"
|
|
24
|
+
npm publish
|
package/README.md
CHANGED
|
@@ -20,7 +20,7 @@ This agent is a replacement for the ssh-agent program. This package includes bot
|
|
|
20
20
|
## await agent.add_key(<Buffer> privateKey [, <string> alias]);
|
|
21
21
|
## await agent.remove_key(<Buffer> pubkey || <string> alias || <string> fingerprint);
|
|
22
22
|
## await agent.remove_all_keys();
|
|
23
|
-
## await agent.sign(<
|
|
23
|
+
## await agent.sign(<Buffer> publicKeyBlob || <string> alias/fingerprint, <Buffer> message [, {flags}]);
|
|
24
24
|
|
|
25
25
|
|
|
26
26
|
|
|
@@ -30,3 +30,30 @@ This agent is a replacement for the ssh-agent program. This package includes bot
|
|
|
30
30
|
* yks ssh-agent https://github.com/131/yks/blob/master/class/apis/ssh_agent.php
|
|
31
31
|
* https://tools.ietf.org/html/draft-miller-ssh-agent-00
|
|
32
32
|
* https://github.com/131/pageantbridge
|
|
33
|
+
|
|
34
|
+
### OpenSSH agent sockets and FIDO signatures
|
|
35
|
+
|
|
36
|
+
Pass an existing socket or duplex stream to `new Agent(socket)` (from
|
|
37
|
+
`require('ssh-agent-js/client')`). The caller opens and closes the transport.
|
|
38
|
+
For a direct connection, use `net.connect(process.env.SSH_AUTH_SOCK)` with
|
|
39
|
+
a Unix socket or Windows named pipe. After a framing error or timeout,
|
|
40
|
+
close the transport and open a new one before making another request.
|
|
41
|
+
|
|
42
|
+
`list_keys()` includes `ed25519-sk` and `ecdsa-sk` identities. `sign(key, message,
|
|
43
|
+
{flags = 0} = {})` accepts a listed identity's `blob`, an alias, or an MD5 or
|
|
44
|
+
SHA256 fingerprint. It returns `{type, signature, wire}`: `type` is the
|
|
45
|
+
algorithm reported by the agent, `signature` is the signature bytes alone, and
|
|
46
|
+
`wire` is the complete SSH signature (including FIDO flags and counter). Both
|
|
47
|
+
are Buffers. Use `{flags: Agent.RSA_SHA2_512}` for RSA SHA-512.
|
|
48
|
+
The client checks the expected RSA or Ed25519 signature format, and keeps
|
|
49
|
+
the FIDO flags and counter intact when signing with a security key.
|
|
50
|
+
|
|
51
|
+
`lookupKey(selector, {types})` returns the public `blob`, `type`, and fingerprints
|
|
52
|
+
(`fingerprint` for MD5, `fingerprintSHA256` for SHA-256). A Buffer is decoded
|
|
53
|
+
locally; it does not check whether the key is loaded. A comment or fingerprint
|
|
54
|
+
is matched against the agent's identities. With no selector, it returns the
|
|
55
|
+
first key allowed by `types`, or the first key if no filter is supplied. An
|
|
56
|
+
explicit selector never falls back to a different key.
|
|
57
|
+
|
|
58
|
+
`remove_key()` also supports FIDO identities. It unloads the selected key
|
|
59
|
+
from the agent; it does not delete the credential on the authenticator.
|
package/client/index.js
CHANGED
|
@@ -4,58 +4,116 @@ const crypto = require('crypto');
|
|
|
4
4
|
const NodeRSA = require('node-rsa');
|
|
5
5
|
|
|
6
6
|
const md5 = require('nyks/crypto/md5');
|
|
7
|
-
const openssh2pem = require('nyks/crypto/openssh2pem');
|
|
8
|
-
const pemme = require('nyks/crypto/pemme');
|
|
9
|
-
const sread = require('nyks/stream/read');
|
|
10
7
|
|
|
8
|
+
const readFrame = require('../lib/_readFrame');
|
|
11
9
|
const read = require('../lib/_read');
|
|
12
10
|
const write = require('../lib/_write');
|
|
13
11
|
const PROTOCOL = require('../lib/protocol.json');
|
|
14
12
|
|
|
15
13
|
const debug = require('debug')('agent:client');
|
|
16
14
|
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
function parseSignature(wire) {
|
|
16
|
+
const data = wire.slice(0);
|
|
17
|
+
const type = read(data, 'string').toString('ascii');
|
|
18
|
+
const signature = read(data, 'string');
|
|
19
|
+
return {type, signature, rest : data.slice(data._offset)};
|
|
20
|
+
}
|
|
19
21
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
+
function checkSignature(keyType, flags, {type, signature, rest}) {
|
|
23
|
+
if(!type || !signature.length)
|
|
24
|
+
throw new Error('Invalid SSH signature');
|
|
25
|
+
if(keyType === 'ssh-rsa' || keyType === 'ssh-ed25519') {
|
|
26
|
+
let expected = keyType;
|
|
27
|
+
if(keyType === 'ssh-rsa') {
|
|
28
|
+
if(flags & PROTOCOL.SSH_AGENT_RSA_SHA2_256)
|
|
29
|
+
expected = 'rsa-sha2-256';
|
|
30
|
+
else if(flags & PROTOCOL.SSH_AGENT_RSA_SHA2_512)
|
|
31
|
+
expected = 'rsa-sha2-512';
|
|
32
|
+
}
|
|
33
|
+
if(type !== expected || rest.length)
|
|
34
|
+
throw new Error('Unexpected SSH signature algorithm or format');
|
|
35
|
+
} else if(['sk-ssh-ed25519@openssh.com', 'sk-ecdsa-sha2-nistp256@openssh.com'].includes(keyType)) {
|
|
36
|
+
if(type !== keyType || rest.length !== 5)
|
|
37
|
+
throw new Error('Unexpected FIDO signature algorithm or format');
|
|
22
38
|
}
|
|
39
|
+
}
|
|
23
40
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
41
|
+
function keyDetails(blob) {
|
|
42
|
+
const type = Agent.publicKeyType(blob);
|
|
43
|
+
if(!type)
|
|
44
|
+
throw new Error('Invalid SSH public key');
|
|
45
|
+
return {
|
|
46
|
+
type,
|
|
47
|
+
fingerprint : md5(blob),
|
|
48
|
+
fingerprintSHA256 : 'SHA256:' + crypto.createHash('sha256').update(blob).digest('base64').replace(/=+$/, ''),
|
|
49
|
+
ssh_key : blob.toString('base64'),
|
|
50
|
+
blob,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
35
53
|
|
|
36
|
-
|
|
37
|
-
if(type !== messageType)
|
|
38
|
-
throw `Expected message type: ${messageType} but got: ${type}`;
|
|
54
|
+
class Agent {
|
|
39
55
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
56
|
+
constructor(sock, {timeout = 60000} = {}) {
|
|
57
|
+
this.lnk = sock;
|
|
58
|
+
this.timeout = timeout;
|
|
59
|
+
this.pending = Promise.resolve();
|
|
60
|
+
sock.on('error', err => { this.failure = err; });
|
|
61
|
+
}
|
|
45
62
|
|
|
63
|
+
static publicKeyType(blob) {
|
|
64
|
+
return read(blob.slice(0), 'string').toString('ascii');
|
|
46
65
|
}
|
|
47
66
|
|
|
67
|
+
_request(requestType, messageType, payload) {
|
|
68
|
+
const request = this.pending.then(() => this._exchange(messageType, payload)).catch(err => {
|
|
69
|
+
debug('%s: %s', requestType, err.message);
|
|
70
|
+
throw err;
|
|
71
|
+
});
|
|
72
|
+
this.pending = request.catch(() => {});
|
|
73
|
+
return request;
|
|
74
|
+
}
|
|
48
75
|
|
|
49
|
-
async
|
|
50
|
-
|
|
76
|
+
async _exchange(messageType, payload) {
|
|
77
|
+
const client = this.lnk;
|
|
78
|
+
if(this.failure || client.destroyed)
|
|
79
|
+
throw this.failure || new Error('SSH agent socket is closed');
|
|
51
80
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
81
|
+
const frame = readFrame(client, this.timeout);
|
|
82
|
+
client.write(payload);
|
|
83
|
+
const response = await frame.catch(err => {
|
|
84
|
+
this.failure = err;
|
|
85
|
+
throw err;
|
|
56
86
|
});
|
|
57
|
-
|
|
58
|
-
|
|
87
|
+
response._offset = 5;
|
|
88
|
+
if(response[4] === PROTOCOL.SSH_AGENT_FAILURE)
|
|
89
|
+
throw new Error('SSH agent refused the request');
|
|
90
|
+
if(response[4] !== messageType) {
|
|
91
|
+
this.failure = new Error('Unexpected SSH agent response type: ' + response[4]);
|
|
92
|
+
throw this.failure;
|
|
93
|
+
}
|
|
94
|
+
return response;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async lookupKey(selector, {types} = {}) {
|
|
98
|
+
const compatible = key => !types || types.includes(key.type);
|
|
99
|
+
let key;
|
|
100
|
+
if(Buffer.isBuffer(selector)) {
|
|
101
|
+
key = keyDetails(selector);
|
|
102
|
+
} else {
|
|
103
|
+
const keys = Object.values(await this.list_keys());
|
|
104
|
+
if(!keys.length)
|
|
105
|
+
throw new Error('No keys in ssh-agent');
|
|
106
|
+
if(!selector) {
|
|
107
|
+
key = keys.find(compatible) || keys[0];
|
|
108
|
+
} else {
|
|
109
|
+
const fingerprint = selector.replace(/^MD5:/, '').replace(/:/g, '');
|
|
110
|
+
key = keys.find(key => key.comment === selector || key.fingerprint === fingerprint || key.fingerprintSHA256 === selector);
|
|
111
|
+
if(!key)
|
|
112
|
+
throw new Error(`Key not found: ${selector}`);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
if(!compatible(key))
|
|
116
|
+
throw new Error(`Unsupported key type: ${key.type}`);
|
|
59
117
|
return key;
|
|
60
118
|
}
|
|
61
119
|
|
|
@@ -88,7 +146,7 @@ class Agent {
|
|
|
88
146
|
}
|
|
89
147
|
|
|
90
148
|
async remove_key(key_id) {
|
|
91
|
-
|
|
149
|
+
const key = await this.lookupKey(key_id);
|
|
92
150
|
|
|
93
151
|
var packet = write(Buffer.concat([
|
|
94
152
|
write(PROTOCOL.SSH2_AGENTC_REMOVE_IDENTITY, 'uint8'),
|
|
@@ -107,59 +165,41 @@ class Agent {
|
|
|
107
165
|
|
|
108
166
|
var keys = {};
|
|
109
167
|
for(var i = 0; i < numKeys; i++) {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
let comment = read(response, 'string');
|
|
114
|
-
let type = read(key, 'string');
|
|
115
|
-
|
|
116
|
-
keys[fp] = {
|
|
117
|
-
type : type.toString('ascii'),
|
|
118
|
-
fingerprint : fp,
|
|
119
|
-
ssh_key : key.toString('base64'),
|
|
120
|
-
comment : comment.toString('utf8'),
|
|
121
|
-
blob : key
|
|
122
|
-
};
|
|
168
|
+
const key = keyDetails(read(response, 'string'));
|
|
169
|
+
key.comment = read(response, 'string').toString('utf8');
|
|
170
|
+
keys[key.fingerprint] = key;
|
|
123
171
|
}
|
|
124
172
|
|
|
173
|
+
if(response._offset !== response.length)
|
|
174
|
+
throw new Error('Invalid SSH agent identity list');
|
|
125
175
|
return keys;
|
|
126
176
|
}
|
|
127
177
|
|
|
128
178
|
|
|
129
|
-
async sign(
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
179
|
+
async sign(key, message, {flags = 0} = {}) {
|
|
180
|
+
if(!(Buffer.isBuffer(key) || (typeof key === 'string' && key)) || !Buffer.isBuffer(message))
|
|
181
|
+
throw new TypeError('Expected an SSH key blob or selector and a message Buffer');
|
|
182
|
+
if(!Number.isInteger(flags) || flags < 0 || flags > 0xffffffff)
|
|
183
|
+
throw new TypeError('SSH signature flags must be an unsigned 32-bit integer');
|
|
184
|
+
const publicKeyBlob = Buffer.isBuffer(key) ? key : (await this.lookupKey(key)).blob;
|
|
185
|
+
const packet = write(Buffer.concat([
|
|
134
186
|
write(PROTOCOL.SSH2_AGENTC_SIGN_REQUEST, 'uint8'),
|
|
135
|
-
write(
|
|
187
|
+
write(publicKeyBlob, 'string'),
|
|
136
188
|
write(message, 'string'),
|
|
137
|
-
write(
|
|
189
|
+
write(flags, 'uint32'),
|
|
138
190
|
]), 'string');
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
let signature = read(blob, 'string');
|
|
147
|
-
|
|
148
|
-
key = pemme(openssh2pem(key.ssh_key), 'PUBLIC KEY');
|
|
149
|
-
|
|
150
|
-
var verifier = crypto.createVerify('RSA-SHA1');
|
|
151
|
-
verifier.update(message);
|
|
152
|
-
var success = verifier.verify(key, signature);
|
|
153
|
-
if(!success)
|
|
154
|
-
throw `Cannot verify signature`;
|
|
155
|
-
|
|
156
|
-
return {
|
|
157
|
-
type : type.toString('ascii'),
|
|
158
|
-
signature : signature.toString('base64'),
|
|
159
|
-
_raw : signature
|
|
160
|
-
};
|
|
191
|
+
const response = await this._request('sign', PROTOCOL.SSH2_AGENT_SIGN_RESPONSE, packet);
|
|
192
|
+
const wire = read(response, 'string');
|
|
193
|
+
if(!wire.length || response._offset !== response.length)
|
|
194
|
+
throw new Error('Invalid SSH agent signature response');
|
|
195
|
+
const parsed = parseSignature(wire);
|
|
196
|
+
checkSignature(Agent.publicKeyType(publicKeyBlob), flags, parsed);
|
|
197
|
+
return {type : parsed.type, signature : parsed.signature, wire};
|
|
161
198
|
}
|
|
199
|
+
|
|
162
200
|
}
|
|
163
201
|
|
|
164
202
|
|
|
203
|
+
Agent.RSA_SHA2_512 = PROTOCOL.SSH_AGENT_RSA_SHA2_512;
|
|
204
|
+
|
|
165
205
|
module.exports = Agent;
|
package/lib/_read.js
CHANGED
|
@@ -19,8 +19,9 @@ var read = function(data, size){
|
|
|
19
19
|
|
|
20
20
|
if(size == 'string') {
|
|
21
21
|
len = read(data, 'uint32');
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
if(len > data.length - data._offset)
|
|
23
|
+
throw new Error('Truncated SSH string');
|
|
24
|
+
ret = Buffer.from(data.slice(data._offset, data._offset + len));
|
|
24
25
|
}
|
|
25
26
|
|
|
26
27
|
data._offset+= len;
|
|
@@ -28,4 +29,4 @@ var read = function(data, size){
|
|
|
28
29
|
}
|
|
29
30
|
|
|
30
31
|
|
|
31
|
-
module.exports = read;
|
|
32
|
+
module.exports = read;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// The client sends one request at a time. Bytes past its response mean the socket is out of sync.
|
|
4
|
+
module.exports = function readFrame(socket, timeout) {
|
|
5
|
+
return new Promise((resolve, reject) => {
|
|
6
|
+
const chunks = [];
|
|
7
|
+
const header = Buffer.alloc(4);
|
|
8
|
+
let length = 0;
|
|
9
|
+
let size;
|
|
10
|
+
|
|
11
|
+
function cleanup() {
|
|
12
|
+
clearTimeout(timer);
|
|
13
|
+
socket.removeListener('data', receive);
|
|
14
|
+
socket.removeListener('error', fail);
|
|
15
|
+
socket.removeListener('end', ended);
|
|
16
|
+
socket.removeListener('close', ended);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function fail(err) {
|
|
20
|
+
cleanup();
|
|
21
|
+
reject(err);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function ended() {
|
|
25
|
+
fail(new Error('SSH agent closed before completing its response'));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function receive(chunk) {
|
|
29
|
+
if(length < 4)
|
|
30
|
+
chunk.copy(header, length, 0, Math.min(4 - length, chunk.length));
|
|
31
|
+
length += chunk.length;
|
|
32
|
+
chunks.push(chunk);
|
|
33
|
+
if(length < 4)
|
|
34
|
+
return;
|
|
35
|
+
if(size === undefined)
|
|
36
|
+
size = header.readUInt32BE(0);
|
|
37
|
+
if(size < 1 || size > 1024 * 1024 || length > size + 4)
|
|
38
|
+
return fail(new Error('Invalid SSH agent frame length'));
|
|
39
|
+
if(length === size + 4) {
|
|
40
|
+
cleanup();
|
|
41
|
+
resolve(Buffer.concat(chunks, length));
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const timer = setTimeout(() => fail(new Error('SSH agent response timed out')), timeout);
|
|
46
|
+
socket.on('data', receive);
|
|
47
|
+
socket.once('error', fail);
|
|
48
|
+
socket.once('end', ended);
|
|
49
|
+
socket.once('close', ended);
|
|
50
|
+
});
|
|
51
|
+
};
|
package/package.json
CHANGED
package/server/agentd.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const os = require('os');
|
|
4
|
+
|
|
5
|
+
const tmppath = require('nyks/fs/tmppath');
|
|
6
|
+
const KeyChain = require('ssh-keychain')
|
|
7
|
+
|
|
8
|
+
const SSHAgentD = require('./')
|
|
9
|
+
const SocketTransport = require('./socket');
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class AgentD {
|
|
13
|
+
|
|
14
|
+
constructor() {
|
|
15
|
+
|
|
16
|
+
//work with an empty vault
|
|
17
|
+
this.vault = new KeyChain();
|
|
18
|
+
var agent = new SSHAgentD(this.vault);
|
|
19
|
+
|
|
20
|
+
var Factory = SocketTransport;
|
|
21
|
+
|
|
22
|
+
if(os.platform() == 'win32')
|
|
23
|
+
Factory = require('pageantbridge');
|
|
24
|
+
|
|
25
|
+
this.server = new Factory( (client) => {
|
|
26
|
+
agent.attach_client(client);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
start () {
|
|
31
|
+
var port = tmppath();
|
|
32
|
+
this.server.start(port);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
module.exports = AgentD;
|
package/server/index.js
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const BigInteger = require('node-jsbn');
|
|
4
|
+
const ber = require('asn1').Ber;
|
|
5
|
+
const debug = require('debug')('ssh-agent');
|
|
6
|
+
const md5 = require('nyks/crypto/md5');
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
const PROTOCOL = require('../lib/protocol.json');
|
|
10
|
+
const read = require('../lib/_read');
|
|
11
|
+
const write = require('../lib/_write');
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class SSHAgentD {
|
|
15
|
+
|
|
16
|
+
constructor(keychain) {
|
|
17
|
+
this.keychain = keychain;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
attach_client(client) {
|
|
21
|
+
var self = this, traffic = new Buffer(0), length;
|
|
22
|
+
|
|
23
|
+
var feed = function(buffer) {
|
|
24
|
+
traffic = Buffer.concat([traffic, buffer]);
|
|
25
|
+
debug("Feeding for traffic", traffic);
|
|
26
|
+
|
|
27
|
+
if(traffic.length >= length) {
|
|
28
|
+
self._parse(client, traffic);
|
|
29
|
+
traffic = new Buffer(0);
|
|
30
|
+
debug("Cropped traffic is", traffic);
|
|
31
|
+
client.removeListener("data", feed);
|
|
32
|
+
client.once("data", init);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
var init = function(buffer) {
|
|
37
|
+
debug("Init buffer", buffer);
|
|
38
|
+
if(buffer.length < 4)
|
|
39
|
+
throw "Invalid buffer";
|
|
40
|
+
length = read(buffer, "uint32");
|
|
41
|
+
debug("New packet, lengh is", length);
|
|
42
|
+
client.on("data", feed);
|
|
43
|
+
feed(buffer.slice(4));
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
client.once("data", init);
|
|
47
|
+
|
|
48
|
+
client.once("error", function() {
|
|
49
|
+
debug("Client disconnected");
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
client.once("end", function() {
|
|
53
|
+
debug("No data anymore");
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
list_keys_v1(client, callback) {
|
|
60
|
+
|
|
61
|
+
var respondIdentities = function() {
|
|
62
|
+
return write(0, "uint32");
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
return this._respond(client, PROTOCOL.SSH_AGENT_RSA_IDENTITIES_ANSWER, respondIdentities, callback);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
list_keys_v2(client, callback) {
|
|
69
|
+
var self = this;
|
|
70
|
+
|
|
71
|
+
var respondIdentities = function() {
|
|
72
|
+
var keys = self.keychain.keys,
|
|
73
|
+
out = [write(keys.length, "uint32")];
|
|
74
|
+
|
|
75
|
+
keys.forEach((key) => {
|
|
76
|
+
out.push(write(key.public, "string"));
|
|
77
|
+
out.push(write(key.comment, "string"));
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
return Buffer.concat(out);
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
return this._respond(client, PROTOCOL.SSH2_AGENT_IDENTITIES_ANSWER, respondIdentities, callback);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
sign(client, body, callback) {
|
|
87
|
+
var key_blob = read(body, "string"),
|
|
88
|
+
message = read(body, "string");
|
|
89
|
+
var fingerprint = md5(key_blob);
|
|
90
|
+
try {
|
|
91
|
+
|
|
92
|
+
var sign = this.keychain.sign(fingerprint, message);
|
|
93
|
+
|
|
94
|
+
var respondSigning = function() {
|
|
95
|
+
var blob = write(Buffer.concat([
|
|
96
|
+
write("ssh-rsa", "string"),
|
|
97
|
+
write(sign, "string"),
|
|
98
|
+
]), "string");
|
|
99
|
+
return blob;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
return this._respond(client, PROTOCOL.SSH2_AGENT_SIGN_RESPONSE, respondSigning, callback);
|
|
104
|
+
} catch(err) {
|
|
105
|
+
return this._respond(client, PROTOCOL.SSH_AGENT_FAILURE, null, callback);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
add_key(client, body, callback) {
|
|
110
|
+
var algo = read(body, "string").toString('ascii'),
|
|
111
|
+
n = read(body, 'mpint'),
|
|
112
|
+
e = read(body, 'mpint'),
|
|
113
|
+
d = read(body, 'mpint'),
|
|
114
|
+
coeff = read(body, 'mpint'),
|
|
115
|
+
p = read(body, 'mpint'),
|
|
116
|
+
q = read(body, 'mpint'),
|
|
117
|
+
comment = read(body, 'string');
|
|
118
|
+
|
|
119
|
+
var p1 = new BigInteger(p),
|
|
120
|
+
q1 = new BigInteger(q),
|
|
121
|
+
dmp1 = new BigInteger(d),
|
|
122
|
+
dmq1 = new BigInteger(d);
|
|
123
|
+
|
|
124
|
+
dmp1 = dmp1.mod(p1.subtract(BigInteger.ONE)).toBuffer();
|
|
125
|
+
dmq1 = dmq1.mod(q1.subtract(BigInteger.ONE)).toBuffer();
|
|
126
|
+
|
|
127
|
+
var length = n.length + d.length + p.length + q.length + dmp1.length + dmq1.length + coeff.length + 512; // magic
|
|
128
|
+
var writer = new ber.Writer({size : length});
|
|
129
|
+
//openssl private
|
|
130
|
+
writer.startSequence();
|
|
131
|
+
writer.writeInt(0);
|
|
132
|
+
writer.writeBuffer(n, 2);
|
|
133
|
+
writer.writeBuffer(e, 2);
|
|
134
|
+
writer.writeBuffer(d, 2);
|
|
135
|
+
writer.writeBuffer(p, 2);
|
|
136
|
+
writer.writeBuffer(q, 2);
|
|
137
|
+
writer.writeBuffer(dmp1, 2);
|
|
138
|
+
writer.writeBuffer(dmq1, 2);
|
|
139
|
+
writer.writeBuffer(coeff, 2);
|
|
140
|
+
writer.endSequence();
|
|
141
|
+
|
|
142
|
+
this.keychain.add_key(writer.buffer);
|
|
143
|
+
|
|
144
|
+
return this._respond(client, PROTOCOL.SSH_AGENT_SUCCESS, null, callback);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
_respond(client, type, response, callback) {
|
|
148
|
+
var msg = [write(type, "uint8")];
|
|
149
|
+
if(response) msg.push(response());
|
|
150
|
+
|
|
151
|
+
var body = write(Buffer.concat(msg), "string");
|
|
152
|
+
debug("responding with", body);
|
|
153
|
+
client.write(body);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
_parse(client, body) {
|
|
158
|
+
|
|
159
|
+
var type = read(body, "uint8");
|
|
160
|
+
debug("Parsing", body, "read ", type);
|
|
161
|
+
|
|
162
|
+
if(type == PROTOCOL.SSH_AGENTC_REQUEST_RSA_IDENTITIES)
|
|
163
|
+
this.list_keys_v1(client);
|
|
164
|
+
|
|
165
|
+
if(type == PROTOCOL.SSH2_AGENTC_REQUEST_IDENTITIES)
|
|
166
|
+
this.list_keys_v2(client);
|
|
167
|
+
|
|
168
|
+
if(type == PROTOCOL.SSH2_AGENTC_ADD_IDENTITY)
|
|
169
|
+
this.add_key(client, body);
|
|
170
|
+
|
|
171
|
+
if(type == PROTOCOL.SSH2_AGENTC_SIGN_REQUEST)
|
|
172
|
+
this.sign(client, body);
|
|
173
|
+
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
module.exports = SSHAgentD;
|
package/server/socket.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const net = require('net');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class SocketTransport {
|
|
7
|
+
constructor(attach) {
|
|
8
|
+
this.attach = attach;
|
|
9
|
+
this.lnk = net.createServer(this.attach);
|
|
10
|
+
|
|
11
|
+
process.on('cnyksEnd', () => {
|
|
12
|
+
console.log("Shutting down agent");
|
|
13
|
+
this.lnk.close();
|
|
14
|
+
})
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
start(socket) {
|
|
18
|
+
this.lnk.listen(socket, function() {
|
|
19
|
+
console.log("export SSH_AUTH_SOCK=%s", socket);
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
module.exports = SocketTransport;
|