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 ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "root": true,
3
+ "extends": [
4
+ "plugin:ivs/node"
5
+ ]
6
+ }
@@ -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
@@ -0,0 +1,11 @@
1
+ language: node_js
2
+ node_js:
3
+ - 8
4
+ after_script:
5
+ - npm run coveralls
6
+ deploy:
7
+ provider: npm
8
+ email: 131.frodon@cloudyks.org
9
+ api_key: $NPM_TOKEN
10
+ on:
11
+ tags: true
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
- # ssh-agent
2
- A pure js drop-in replacement for ssh-agent.
1
+ [![Build Status](https://travis-ci.org/131/ssh-agent-js.svg?branch=master)](https://travis-ci.org/131/ssh-agent-js)
2
+ [![Coverage Status](https://coveralls.io/repos/github/131/ssh-agent-js/badge.svg?branch=master)](https://coveralls.io/github/131/ssh-agent-js?branch=master)
3
+ [![Version](https://img.shields.io/npm/v/ssh-agent-js.svg)](https://www.npmjs.com/package/ssh-agent-js)
4
+ [![License](https://img.shields.io/badge/license-MIT-blue.svg)](http://opensource.org/licenses/MIT)
5
+
6
+ [![Code style](https://img.shields.io/badge/code%2fstyle-ivs-green.svg)](https://www.npmjs.com/package/eslint-plugin-ivs)
7
+
3
8
 
4
9
 
5
10
  # Motivation
6
- This agent is a replacement for the ssh-agent program. This package includes both a "server" and "client" API.
7
- This API is used by the project nwagent (a node-webkit project for desktops).
8
- This library implements the official agent protocol and is compatible with all ssh clients (ssh & putty)
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
- * Inspirations from mcavage ssh-agent for clients binding
29
+ * [131](https://github.com/131)
18
30
  * yks ssh-agent https://github.com/131/yks/blob/master/class/apis/ssh_agent.php
19
- * http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/ssh/PROTOCOL.agent?rev=HEAD
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
- "use strict";
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/crypt/md5');
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
- class Agent {
18
-
19
- constructor(sock) {
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
- this.lnk = { path : sock || process.env["SSH_AUTH_SOCK"] };
22
- this.lnk = { port : 8001 };
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
- _request(messageType, getRequest, callback) {
26
-
27
- var client = net.connect(this.lnk, function(){
28
- client.write(getRequest());
29
- });
30
-
31
- client.once('data', function (data) {
32
- client.end();
33
-
34
- var len = read(data, "uint32");
35
- if (len !== data.length - 4)
36
- return callback(`Expected length: ${len} but got: ${data.length}`);
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
- return callback(null, data);
43
- });
54
+ class Agent {
44
55
 
45
- client.on('error', callback);
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
- add_key(keyData, comment, callback) {
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
- remove_all_keys(callback) {
73
-
74
- var removeRequest = function() {
75
- return write(PROTOCOL.SSH2_AGENTC_REMOVE_ALL_IDENTITIES, "string");
76
- }
77
-
78
- return this._request(PROTOCOL.SSH_AGENT_SUCCESS, removeRequest, callback);
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
- remove_key(pubkey, callback) {
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
- var removeRequest = function() {
84
- var packet = write(Buffer.concat([
85
- write(PROTOCOL.SSH2_AGENTC_REMOVE_IDENTITY, "uint8"),
86
- write(pubkey, "string"),
87
- ]), "string");
88
- return packet;
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
- list_keys(callback) {
96
-
97
- var requestIdentities = function() {
98
- return write(PROTOCOL.SSH2_AGENTC_REQUEST_IDENTITIES, "string");
99
- }
100
-
101
- var identitiesAnswer = function(err, response) {
102
- if(err)
103
- return callback(err);
104
-
105
- var numKeys = read(response, "uint32");
106
-
107
- var keys = {};
108
- for (var i = 0; i < numKeys; i++) {
109
- var key = read(response, "string");
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
- return this._request(PROTOCOL.SSH2_AGENT_IDENTITIES_ANSWER, requestIdentities, identitiesAnswer);
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
- sign(key_id, message, callback) {
131
- var self = this, type = 'ssh-rsa';
132
-
133
- this.list_keys(function(err, keys) {
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
- var key = keys[key_id];
148
+ async remove_key(key_id) {
149
+ const key = await this.lookupKey(key_id);
140
150
 
141
- if(!key)
142
- return callback("Invalid key");
151
+ var packet = write(Buffer.concat([
152
+ write(PROTOCOL.SSH2_AGENTC_REMOVE_IDENTITY, 'uint8'),
153
+ write(key.blob, 'string'),
154
+ ]), 'string');
143
155
 
144
- if(key.type != type)
145
- return callback("Unsupported key format");
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
- function signatureResponse(err, response) {
158
- if(err)
159
- return callback(err);
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
- var blob = read(response, "string");
162
- var type = read(blob, "string");
163
- var signature = read(blob, "string");
164
+ var numKeys = read(response, 'uint32');
164
165
 
165
- key = pemme(openssh2pem(key.ssh_key), "PUBLIC KEY");
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
- var verifier = crypto.createVerify('RSA-SHA1');
168
- verifier.update(message);
169
- var success = verifier.verify(key, signature);
170
- if(!success)
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
- return self._request(PROTOCOL.SSH2_AGENT_SIGN_RESPONSE, signRequest, signatureResponse);
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
- module.exports = Agent;
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
- "use strict";
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 == "mpint") {
8
- ret = read(data, "string");
7
+ if(size == 'mpint') {
8
+ ret = read(data, 'string');
9
9
  }
10
10
 
11
- if(size == "uint32") {
11
+ if(size == 'uint32') {
12
12
  len = 4;
13
13
  ret = data.readUInt32BE(data._offset);
14
14
  }
15
- if(size == "uint8") {
15
+ if(size == 'uint8') {
16
16
  len = 1;
17
17
  ret = data.readUInt8(data._offset);
18
18
  }
19
19
 
20
- if(size == "string") {
21
- len = read(data, "uint32");
22
- ret = new Buffer(len);
23
- data.copy(ret, 0, data._offset, data._offset + len);
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
- "use strict";
1
+ 'use strict';
2
2
 
3
3
  var write = function(data, size){
4
4
  var ret;
5
5
 
6
- if(size == "mpint") {
7
- if(data[0] >= 128) //MSB is set, but all values are positives, prepend a null byte
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 == "string") {
13
- if(typeof data == "string") data = new Buffer(data);
14
- var body = Buffer.isBuffer(data) ? data : new Buffer([data]);
15
- ret = Buffer.concat([write(body.length, "uint32"), body]);
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 == "uint32") {
24
+ if(size == 'uint32') {
19
25
  ret = new Buffer(4);
20
26
  ret.writeUInt32BE(data, 0);
21
27
  }
22
- if(size == "uint8") {
28
+ if(size == 'uint8') {
23
29
  ret = new Buffer([data]);
24
30
  }
25
31
 
package/lib/protocol.json CHANGED
@@ -11,5 +11,9 @@
11
11
  "SSH2_AGENTC_SIGN_REQUEST": 13,
12
12
  "SSH2_AGENT_SIGN_RESPONSE": 14,
13
13
  "SSH_AGENT_FAILURE": 5,
14
- "SSH_AGENT_SUCCESS": 6
14
+ "SSH_AGENT_SUCCESS": 6,
15
+
16
+
17
+ "SSH_AGENT_RSA_SHA2_256" : 2,
18
+ "SSH_AGENT_RSA_SHA2_512" : 4
15
19
  }
package/package.json CHANGED
@@ -1,29 +1,44 @@
1
1
  {
2
2
  "name": "ssh-agent-js",
3
- "version": "1.3.7",
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": "ISC",
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": "~0.4.2",
22
- "nyks": "^3.18.0"
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
- "devDependencies": {},
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('./lib/protocol.json');
10
- const read = require('./lib/_read');
11
- const write = require('./lib/_write');
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 (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 (client, callback) {
59
+ list_keys_v1(client, callback) {
60
60
 
61
61
  var respondIdentities = function() {
62
- return write(0, "uint32");
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
- var keys = self.keychain.keys,
73
- out = [write(keys.length, "uint32")];
72
+ var keys = self.keychain.keys,
73
+ out = [write(keys.length, "uint32")];
74
74
 
75
- keys.forEach( (key) => {
76
- out.push(write(key.public, "string"));
77
- out.push(write(key.comment, "string"));
78
- });
75
+ keys.forEach((key) => {
76
+ out.push(write(key.public, "string"));
77
+ out.push(write(key.comment, "string"));
78
+ });
79
79
 
80
- return Buffer.concat(out);
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 (client, body, callback) {
86
+ sign(client, body, callback) {
87
87
  var key_blob = read(body, "string"),
88
- message = read(body, "string");
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 (client, body, callback) {
109
+ add_key(client, body, callback) {
110
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');
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
- q1 = new BigInteger(q),
121
- dmp1 = new BigInteger(d),
122
- dmq1 = new BigInteger(d);
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
- //openssl private
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 (client, type, response, callback) {
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 (client, body) {
157
+ _parse(client, body) {
158
158
 
159
159
  var type = read(body, "uint8");
160
- debug("Parsing" , body, "read ", type );
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
@@ -1,4 +0,0 @@
1
- /coverage
2
- /test
3
- /disc
4
- /npm-debug.log
File without changes
File without changes