ssh-agent-js 1.3.7 → 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/.eslintrc +6 -0
- package/.github/workflows/publish-npm.yml +24 -0
- package/.travis.yml +11 -0
- package/LICENSE +21 -0
- package/README.md +48 -9
- package/client/index.js +161 -142
- package/lib/_read.js +11 -10
- package/lib/_readFrame.js +51 -0
- package/lib/_write.js +17 -11
- package/lib/protocol.json +5 -1
- package/package.json +28 -13
- package/{index.js → server/index.js} +38 -38
- package/.npmignore +0 -4
- /package/{agentd.js → server/agentd.js} +0 -0
- /package/{socket.js → server/socket.js} +0 -0
package/.eslintrc
ADDED
|
@@ -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/.travis.yml
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 Francois Leurent
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,20 +1,59 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
[](https://travis-ci.org/131/ssh-agent-js)
|
|
2
|
+
[](https://coveralls.io/github/131/ssh-agent-js?branch=master)
|
|
3
|
+
[](https://www.npmjs.com/package/ssh-agent-js)
|
|
4
|
+
[](http://opensource.org/licenses/MIT)
|
|
5
|
+
|
|
6
|
+
[](https://www.npmjs.com/package/eslint-plugin-ivs)
|
|
7
|
+
|
|
3
8
|
|
|
4
9
|
|
|
5
10
|
# Motivation
|
|
6
|
-
|
|
7
|
-
This
|
|
8
|
-
|
|
11
|
+
A pure js drop-in replacement for ssh-agent.
|
|
12
|
+
This agent is a replacement for the ssh-agent program. This package includes both a **"server"** and **"client"** API. This library implements the official agent protocol and is compatible with all ssh clients (ssh & putty)
|
|
13
|
+
|
|
14
|
+
|
|
9
15
|
|
|
16
|
+
# Client API
|
|
17
|
+
|
|
18
|
+
## new Agent(socket)
|
|
19
|
+
## await agent.list_keys();
|
|
20
|
+
## await agent.add_key(<Buffer> privateKey [, <string> alias]);
|
|
21
|
+
## await agent.remove_key(<Buffer> pubkey || <string> alias || <string> fingerprint);
|
|
22
|
+
## await agent.remove_all_keys();
|
|
23
|
+
## await agent.sign(<Buffer> publicKeyBlob || <string> alias/fingerprint, <Buffer> message [, {flags}]);
|
|
10
24
|
|
|
11
|
-
# Implementations
|
|
12
|
-
ssh-agent relies on named pipes (linux only). For windows platforms, the putty/plink/pscp/.. stack relies on winAPI sendmessage/postmessage & memorymap. This is handled by the https://github.com/131/pageantbridge project (bundled-in)
|
|
13
25
|
|
|
14
26
|
|
|
15
27
|
|
|
16
28
|
# Credits
|
|
17
|
-
*
|
|
29
|
+
* [131](https://github.com/131)
|
|
18
30
|
* yks ssh-agent https://github.com/131/yks/blob/master/class/apis/ssh_agent.php
|
|
19
|
-
*
|
|
31
|
+
* https://tools.ietf.org/html/draft-miller-ssh-agent-00
|
|
20
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
|
@@ -1,186 +1,205 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
2
|
|
|
3
3
|
const crypto = require('crypto');
|
|
4
|
-
const net = require('net');
|
|
5
|
-
|
|
6
4
|
const NodeRSA = require('node-rsa');
|
|
7
5
|
|
|
8
|
-
const md5 = require('nyks/
|
|
9
|
-
const openssh2pem = require('nyks/crypt/openssh2pem');
|
|
10
|
-
const pemme = require('nyks/crypt/pemme');
|
|
6
|
+
const md5 = require('nyks/crypto/md5');
|
|
11
7
|
|
|
8
|
+
const readFrame = require('../lib/_readFrame');
|
|
12
9
|
const read = require('../lib/_read');
|
|
13
10
|
const write = require('../lib/_write');
|
|
14
11
|
const PROTOCOL = require('../lib/protocol.json');
|
|
15
12
|
|
|
13
|
+
const debug = require('debug')('agent:client');
|
|
16
14
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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
|
+
}
|
|
20
21
|
|
|
21
|
-
|
|
22
|
-
|
|
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');
|
|
23
38
|
}
|
|
39
|
+
}
|
|
24
40
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
var type = read(data, "uint8");
|
|
39
|
-
if (type !== messageType)
|
|
40
|
-
return callback(`Expected message type: ${messageType} but got: ${type}`);
|
|
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
|
+
}
|
|
41
53
|
|
|
42
|
-
|
|
43
|
-
});
|
|
54
|
+
class Agent {
|
|
44
55
|
|
|
45
|
-
|
|
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; });
|
|
46
61
|
}
|
|
47
62
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
var key = (new NodeRSA(keyData)).keyPair;
|
|
51
|
-
|
|
52
|
-
function addRequest() {
|
|
53
|
-
var algo = "ssh-rsa";
|
|
54
|
-
var packet = write(Buffer.concat([
|
|
55
|
-
write(PROTOCOL.SSH2_AGENTC_ADD_IDENTITY, "uint8"),
|
|
56
|
-
write(algo, "string"),
|
|
57
|
-
write(key['n'].toBuffer(), 'mpint'),
|
|
58
|
-
write(new Buffer([key['e']]), 'mpint'),
|
|
59
|
-
write(key['d'].toBuffer(), 'mpint'),
|
|
60
|
-
write(key['coeff'].toBuffer(), 'mpint'),
|
|
61
|
-
write(key['p'].toBuffer(), 'mpint'),
|
|
62
|
-
write(key['q'].toBuffer(), 'mpint'),
|
|
63
|
-
write(comment || "", 'string'),
|
|
64
|
-
]), "string");
|
|
65
|
-
|
|
66
|
-
return packet;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
return this._request(PROTOCOL.SSH_AGENT_SUCCESS, addRequest, callback);
|
|
63
|
+
static publicKeyType(blob) {
|
|
64
|
+
return read(blob.slice(0), 'string').toString('ascii');
|
|
70
65
|
}
|
|
71
66
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
return
|
|
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;
|
|
79
74
|
}
|
|
80
75
|
|
|
81
|
-
|
|
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');
|
|
82
80
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
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;
|
|
86
|
+
});
|
|
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;
|
|
89
93
|
}
|
|
90
|
-
|
|
91
|
-
return this._request(PROTOCOL.SSH_AGENT_SUCCESS, removeRequest, callback);
|
|
94
|
+
return response;
|
|
92
95
|
}
|
|
93
96
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
var comment = read(response, "string");
|
|
111
|
-
var type = read(key, "string");
|
|
112
|
-
var fingerprint = md5(key);
|
|
113
|
-
|
|
114
|
-
keys[fingerprint] = {
|
|
115
|
-
type: type.toString('ascii'),
|
|
116
|
-
fingerprint : fingerprint,
|
|
117
|
-
ssh_key: key.toString('base64'),
|
|
118
|
-
comment: comment.toString('utf8'),
|
|
119
|
-
blob: key
|
|
120
|
-
};
|
|
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}`);
|
|
121
113
|
}
|
|
122
|
-
|
|
123
|
-
return callback(null, keys);
|
|
124
114
|
}
|
|
125
|
-
|
|
126
|
-
|
|
115
|
+
if(!compatible(key))
|
|
116
|
+
throw new Error(`Unsupported key type: ${key.type}`);
|
|
117
|
+
return key;
|
|
127
118
|
}
|
|
128
119
|
|
|
120
|
+
async add_key(keyData, comment) {
|
|
121
|
+
let tmp = new NodeRSA(keyData);
|
|
122
|
+
var key = tmp.exportKey('components');
|
|
123
|
+
var algo = 'ssh-rsa';
|
|
124
|
+
|
|
125
|
+
var publicKey = Buffer.concat([write(algo, 'string'), write(key.e, 'mpint'), write(key.n, 'mpint')]);
|
|
126
|
+
var fingerprint = md5(publicKey); //openssl public
|
|
127
|
+
|
|
128
|
+
var packet = write(Buffer.concat([
|
|
129
|
+
write(PROTOCOL.SSH2_AGENTC_ADD_IDENTITY, 'uint8'),
|
|
130
|
+
write(algo, 'string'),
|
|
131
|
+
write(key.n, 'mpint'),
|
|
132
|
+
write(key.e, 'mpint'),
|
|
133
|
+
write(key.d, 'mpint'),
|
|
134
|
+
write(key.coeff, 'mpint'),
|
|
135
|
+
write(key.p, 'mpint'),
|
|
136
|
+
write(key.q, 'mpint'),
|
|
137
|
+
write(comment || '', 'string'),
|
|
138
|
+
]), 'string');
|
|
139
|
+
|
|
140
|
+
return this._request(`add_key ${fingerprint}`, PROTOCOL.SSH_AGENT_SUCCESS, packet);
|
|
141
|
+
}
|
|
129
142
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
//search by key id or comment
|
|
136
|
-
if(! (key_id in keys))
|
|
137
|
-
Object.keys(keys).forEach(function(k){ if (keys[k].comment == key_id) key_id = k });
|
|
143
|
+
async remove_all_keys() {
|
|
144
|
+
let packet = write(PROTOCOL.SSH2_AGENTC_REMOVE_ALL_IDENTITIES, 'string');
|
|
145
|
+
return this._request('remove_all_keys', PROTOCOL.SSH_AGENT_SUCCESS, packet);
|
|
146
|
+
}
|
|
138
147
|
|
|
139
|
-
|
|
148
|
+
async remove_key(key_id) {
|
|
149
|
+
const key = await this.lookupKey(key_id);
|
|
140
150
|
|
|
141
|
-
|
|
142
|
-
|
|
151
|
+
var packet = write(Buffer.concat([
|
|
152
|
+
write(PROTOCOL.SSH2_AGENTC_REMOVE_IDENTITY, 'uint8'),
|
|
153
|
+
write(key.blob, 'string'),
|
|
154
|
+
]), 'string');
|
|
143
155
|
|
|
144
|
-
|
|
145
|
-
|
|
156
|
+
return this._request('remove_key', PROTOCOL.SSH_AGENT_SUCCESS, packet);
|
|
157
|
+
}
|
|
146
158
|
|
|
147
|
-
function signRequest() {
|
|
148
|
-
var packet = write(Buffer.concat([
|
|
149
|
-
write(PROTOCOL.SSH2_AGENTC_SIGN_REQUEST, "uint8"),
|
|
150
|
-
write(key.blob, "string"),
|
|
151
|
-
write(message, "string"),
|
|
152
|
-
write(0, "uint8"),
|
|
153
|
-
]), "string");
|
|
154
|
-
return packet;
|
|
155
|
-
}
|
|
156
159
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
+
async list_keys() {
|
|
161
|
+
let packet = write(PROTOCOL.SSH2_AGENTC_REQUEST_IDENTITIES, 'string');
|
|
162
|
+
let response = await this._request('list_keys', PROTOCOL.SSH2_AGENT_IDENTITIES_ANSWER, packet);
|
|
160
163
|
|
|
161
|
-
|
|
162
|
-
var type = read(blob, "string");
|
|
163
|
-
var signature = read(blob, "string");
|
|
164
|
+
var numKeys = read(response, 'uint32');
|
|
164
165
|
|
|
165
|
-
|
|
166
|
+
var keys = {};
|
|
167
|
+
for(var i = 0; i < numKeys; i++) {
|
|
168
|
+
const key = keyDetails(read(response, 'string'));
|
|
169
|
+
key.comment = read(response, 'string').toString('utf8');
|
|
170
|
+
keys[key.fingerprint] = key;
|
|
171
|
+
}
|
|
166
172
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
return callback("Cannot verify signature");
|
|
173
|
+
if(response._offset !== response.length)
|
|
174
|
+
throw new Error('Invalid SSH agent identity list');
|
|
175
|
+
return keys;
|
|
176
|
+
}
|
|
172
177
|
|
|
173
|
-
return callback(null, {
|
|
174
|
-
type: type.toString('ascii'),
|
|
175
|
-
signature: signature.toString('base64'),
|
|
176
|
-
_raw: signature
|
|
177
|
-
});
|
|
178
|
-
}
|
|
179
178
|
|
|
180
|
-
|
|
181
|
-
|
|
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([
|
|
186
|
+
write(PROTOCOL.SSH2_AGENTC_SIGN_REQUEST, 'uint8'),
|
|
187
|
+
write(publicKeyBlob, 'string'),
|
|
188
|
+
write(message, 'string'),
|
|
189
|
+
write(flags, 'uint32'),
|
|
190
|
+
]), 'string');
|
|
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};
|
|
182
198
|
}
|
|
199
|
+
|
|
183
200
|
}
|
|
184
201
|
|
|
185
202
|
|
|
186
|
-
|
|
203
|
+
Agent.RSA_SHA2_512 = PROTOCOL.SSH_AGENT_RSA_SHA2_512;
|
|
204
|
+
|
|
205
|
+
module.exports = Agent;
|
package/lib/_read.js
CHANGED
|
@@ -1,26 +1,27 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
2
|
|
|
3
3
|
var read = function(data, size){
|
|
4
4
|
if(!data._offset) data._offset = 0;
|
|
5
5
|
var ret, len = 0;
|
|
6
6
|
|
|
7
|
-
if(size ==
|
|
8
|
-
ret = read(data,
|
|
7
|
+
if(size == 'mpint') {
|
|
8
|
+
ret = read(data, 'string');
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
if(size ==
|
|
11
|
+
if(size == 'uint32') {
|
|
12
12
|
len = 4;
|
|
13
13
|
ret = data.readUInt32BE(data._offset);
|
|
14
14
|
}
|
|
15
|
-
if(size ==
|
|
15
|
+
if(size == 'uint8') {
|
|
16
16
|
len = 1;
|
|
17
17
|
ret = data.readUInt8(data._offset);
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
if(size ==
|
|
21
|
-
len = read(data,
|
|
22
|
-
|
|
23
|
-
|
|
20
|
+
if(size == 'string') {
|
|
21
|
+
len = read(data, 'uint32');
|
|
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/lib/_write.js
CHANGED
|
@@ -1,25 +1,31 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
2
|
|
|
3
3
|
var write = function(data, size){
|
|
4
4
|
var ret;
|
|
5
5
|
|
|
6
|
-
if(size ==
|
|
7
|
-
|
|
8
|
-
data = Buffer.concat([new Buffer([0]), data]);
|
|
9
|
-
ret = write(data, "string");
|
|
6
|
+
if(size == 'mpint') {
|
|
7
|
+
ret = write(data, 'string');
|
|
10
8
|
}
|
|
11
9
|
|
|
12
|
-
if(size ==
|
|
13
|
-
if(typeof data ==
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
if(size == 'string') {
|
|
11
|
+
if(typeof data == 'string')
|
|
12
|
+
data = new Buffer(data);
|
|
13
|
+
|
|
14
|
+
let body = data;
|
|
15
|
+
if(!Buffer.isBuffer(body)) {
|
|
16
|
+
body = new Buffer( 4);
|
|
17
|
+
body.writeUInt32BE(data);
|
|
18
|
+
body = body.slice(-Math.ceil(Math.log1p(data) /Math.log(256)) ); //trim leading zeros
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
ret = Buffer.concat([write(body.length, 'uint32'), body]);
|
|
16
22
|
}
|
|
17
23
|
|
|
18
|
-
if(size ==
|
|
24
|
+
if(size == 'uint32') {
|
|
19
25
|
ret = new Buffer(4);
|
|
20
26
|
ret.writeUInt32BE(data, 0);
|
|
21
27
|
}
|
|
22
|
-
if(size ==
|
|
28
|
+
if(size == 'uint8') {
|
|
23
29
|
ret = new Buffer([data]);
|
|
24
30
|
}
|
|
25
31
|
|
package/lib/protocol.json
CHANGED
package/package.json
CHANGED
|
@@ -1,29 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ssh-agent-js",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "ssh-agent-js",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
-
},
|
|
5
|
+
"main": "server/index.js",
|
|
9
6
|
"keywords": [
|
|
10
7
|
"ssh",
|
|
11
8
|
"ssh-agent",
|
|
12
9
|
"rsa"
|
|
13
10
|
],
|
|
14
11
|
"author": "Francois Leurent <131.js@cloudyks.org>",
|
|
15
|
-
"license": "
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"scripts": {
|
|
14
|
+
"test": "npm run eslint && npm run cover",
|
|
15
|
+
"preversion": "npm run checkall",
|
|
16
|
+
"eslint": "eslint client test",
|
|
17
|
+
"checkall": "npm run eslint",
|
|
18
|
+
"mocha": "mocha -b",
|
|
19
|
+
"cover": "nyc --temp-directory coverage/.nyc_output --reporter text-summary --reporter html --reporter lcov mocha",
|
|
20
|
+
"coveralls": "npm install coveralls && node ./node_modules/coveralls/bin/coveralls.js < ./coverage/lcov.info"
|
|
21
|
+
},
|
|
16
22
|
"dependencies": {
|
|
17
23
|
"asn1": "^0.2.2",
|
|
18
24
|
"debug": "^2.3.2",
|
|
19
25
|
"duplexer": "^0.1.1",
|
|
20
26
|
"node-jsbn": "~0.1.1",
|
|
21
|
-
"node-rsa": "
|
|
22
|
-
"nyks": "^
|
|
27
|
+
"node-rsa": "^1.0.5",
|
|
28
|
+
"nyks": "^5.11.3"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"eslint-plugin-ivs": "^2.1.0",
|
|
32
|
+
"expect.js": "^0.3.1",
|
|
33
|
+
"mocha": "^3.1.2",
|
|
34
|
+
"nyc": "^13.0.1"
|
|
35
|
+
},
|
|
36
|
+
"repository": {
|
|
37
|
+
"type": "git",
|
|
38
|
+
"url": "git+https://github.com/131/ssh-agent-js.git"
|
|
39
|
+
},
|
|
40
|
+
"bugs": {
|
|
41
|
+
"url": "https://github.com/131/ssh-agent-js/issues"
|
|
23
42
|
},
|
|
24
|
-
"
|
|
25
|
-
"optionalDependencies": {
|
|
26
|
-
"ssh-keychain": "^1.0.3",
|
|
27
|
-
"pageantbridge": "^1.2.1"
|
|
28
|
-
}
|
|
43
|
+
"homepage": "https://github.com/131/ssh-agent-js#readme"
|
|
29
44
|
}
|
|
@@ -6,9 +6,9 @@ const debug = require('debug')('ssh-agent');
|
|
|
6
6
|
const md5 = require('nyks/crypto/md5');
|
|
7
7
|
|
|
8
8
|
|
|
9
|
-
const PROTOCOL = require('
|
|
10
|
-
const read = require('
|
|
11
|
-
const write = require('
|
|
9
|
+
const PROTOCOL = require('../lib/protocol.json');
|
|
10
|
+
const read = require('../lib/_read');
|
|
11
|
+
const write = require('../lib/_write');
|
|
12
12
|
|
|
13
13
|
|
|
14
14
|
class SSHAgentD {
|
|
@@ -17,14 +17,14 @@ class SSHAgentD {
|
|
|
17
17
|
this.keychain = keychain;
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
attach_client
|
|
20
|
+
attach_client(client) {
|
|
21
21
|
var self = this, traffic = new Buffer(0), length;
|
|
22
22
|
|
|
23
23
|
var feed = function(buffer) {
|
|
24
24
|
traffic = Buffer.concat([traffic, buffer]);
|
|
25
25
|
debug("Feeding for traffic", traffic);
|
|
26
26
|
|
|
27
|
-
if(traffic.length >= length
|
|
27
|
+
if(traffic.length >= length) {
|
|
28
28
|
self._parse(client, traffic);
|
|
29
29
|
traffic = new Buffer(0);
|
|
30
30
|
debug("Cropped traffic is", traffic);
|
|
@@ -45,22 +45,22 @@ class SSHAgentD {
|
|
|
45
45
|
|
|
46
46
|
client.once("data", init);
|
|
47
47
|
|
|
48
|
-
client.once("error",function(){
|
|
48
|
+
client.once("error", function() {
|
|
49
49
|
debug("Client disconnected");
|
|
50
50
|
});
|
|
51
51
|
|
|
52
|
-
client.once("end", function(){
|
|
52
|
+
client.once("end", function() {
|
|
53
53
|
debug("No data anymore");
|
|
54
54
|
});
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
|
|
58
58
|
|
|
59
|
-
list_keys_v1
|
|
59
|
+
list_keys_v1(client, callback) {
|
|
60
60
|
|
|
61
61
|
var respondIdentities = function() {
|
|
62
|
-
|
|
63
|
-
}
|
|
62
|
+
return write(0, "uint32");
|
|
63
|
+
};
|
|
64
64
|
|
|
65
65
|
return this._respond(client, PROTOCOL.SSH_AGENT_RSA_IDENTITIES_ANSWER, respondIdentities, callback);
|
|
66
66
|
}
|
|
@@ -69,29 +69,29 @@ class SSHAgentD {
|
|
|
69
69
|
var self = this;
|
|
70
70
|
|
|
71
71
|
var respondIdentities = function() {
|
|
72
|
-
|
|
73
|
-
|
|
72
|
+
var keys = self.keychain.keys,
|
|
73
|
+
out = [write(keys.length, "uint32")];
|
|
74
74
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
75
|
+
keys.forEach((key) => {
|
|
76
|
+
out.push(write(key.public, "string"));
|
|
77
|
+
out.push(write(key.comment, "string"));
|
|
78
|
+
});
|
|
79
79
|
|
|
80
|
-
|
|
81
|
-
}
|
|
80
|
+
return Buffer.concat(out);
|
|
81
|
+
};
|
|
82
82
|
|
|
83
83
|
return this._respond(client, PROTOCOL.SSH2_AGENT_IDENTITIES_ANSWER, respondIdentities, callback);
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
-
sign
|
|
86
|
+
sign(client, body, callback) {
|
|
87
87
|
var key_blob = read(body, "string"),
|
|
88
|
-
|
|
88
|
+
message = read(body, "string");
|
|
89
89
|
var fingerprint = md5(key_blob);
|
|
90
90
|
try {
|
|
91
91
|
|
|
92
92
|
var sign = this.keychain.sign(fingerprint, message);
|
|
93
93
|
|
|
94
|
-
var respondSigning = function(){
|
|
94
|
+
var respondSigning = function() {
|
|
95
95
|
var blob = write(Buffer.concat([
|
|
96
96
|
write("ssh-rsa", "string"),
|
|
97
97
|
write(sign, "string"),
|
|
@@ -106,27 +106,27 @@ class SSHAgentD {
|
|
|
106
106
|
}
|
|
107
107
|
}
|
|
108
108
|
|
|
109
|
-
add_key
|
|
109
|
+
add_key(client, body, callback) {
|
|
110
110
|
var algo = read(body, "string").toString('ascii'),
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
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
118
|
|
|
119
119
|
var p1 = new BigInteger(p),
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
120
|
+
q1 = new BigInteger(q),
|
|
121
|
+
dmp1 = new BigInteger(d),
|
|
122
|
+
dmq1 = new BigInteger(d);
|
|
123
123
|
|
|
124
124
|
dmp1 = dmp1.mod(p1.subtract(BigInteger.ONE)).toBuffer();
|
|
125
125
|
dmq1 = dmq1.mod(q1.subtract(BigInteger.ONE)).toBuffer();
|
|
126
126
|
|
|
127
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
|
-
|
|
128
|
+
var writer = new ber.Writer({size : length});
|
|
129
|
+
//openssl private
|
|
130
130
|
writer.startSequence();
|
|
131
131
|
writer.writeInt(0);
|
|
132
132
|
writer.writeBuffer(n, 2);
|
|
@@ -144,8 +144,8 @@ class SSHAgentD {
|
|
|
144
144
|
return this._respond(client, PROTOCOL.SSH_AGENT_SUCCESS, null, callback);
|
|
145
145
|
}
|
|
146
146
|
|
|
147
|
-
_respond
|
|
148
|
-
var msg= [write(type, "uint8")];
|
|
147
|
+
_respond(client, type, response, callback) {
|
|
148
|
+
var msg = [write(type, "uint8")];
|
|
149
149
|
if(response) msg.push(response());
|
|
150
150
|
|
|
151
151
|
var body = write(Buffer.concat(msg), "string");
|
|
@@ -154,10 +154,10 @@ class SSHAgentD {
|
|
|
154
154
|
}
|
|
155
155
|
|
|
156
156
|
|
|
157
|
-
_parse
|
|
157
|
+
_parse(client, body) {
|
|
158
158
|
|
|
159
159
|
var type = read(body, "uint8");
|
|
160
|
-
debug("Parsing"
|
|
160
|
+
debug("Parsing", body, "read ", type);
|
|
161
161
|
|
|
162
162
|
if(type == PROTOCOL.SSH_AGENTC_REQUEST_RSA_IDENTITIES)
|
|
163
163
|
this.list_keys_v1(client);
|
package/.npmignore
DELETED
|
File without changes
|
|
File without changes
|