-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnassh_agent_message.js
171 lines (157 loc) · 5.02 KB
/
nassh_agent_message.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
167
168
169
170
171
// Copyright 2017 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
/**
* @fileoverview General message handling in accordance with the SSH agent
* protocol.
*/
nassh.agent = {};
/**
* Create an SSH agent message from a raw byte array containing the message
* contents.
*
* @see https://tools.ietf.org/id/draft-miller-ssh-agent-00.html#rfc.section.4
* @param {!nassh.agent.messages.Numbers} type The type of the message as per
* Section 7.1 of the specification.
* @param {!Uint8Array=} data The raw data of the message, if any.
* @constructor
*/
nassh.agent.Message = function(type, data = new Uint8Array(0)) {
/**
* Type of the message.
*
* @see https://tools.ietf.org/id/draft-miller-ssh-agent-00.html#rfc.section.7.1
* @type {!nassh.agent.messages.Numbers}
*/
this.type = type;
/**
* The raw data of the message.
*
* @private {!Uint8Array}
*/
this.data_ = data;
/**
* The current offset into the raw message data. This is only used when
* reading raw messages (i.e. requests).
*
* @private {number}
*/
this.offset_ = 0;
/**
* The fields encoded in the message data. This is only used when reading raw
* messages (i.e. requests) that contain data.
*
* @type {!Object}
*/
this.fields = {};
};
/**
* Get the raw, length-encoded representation of the message.
*
* @see https://tools.ietf.org/id/draft-miller-ssh-agent-00.html#rfc.section.3
* @return {!Uint8Array}
*/
nassh.agent.Message.prototype.rawMessage = function() {
const buffer = new ArrayBuffer(5);
const u8 = new Uint8Array(buffer);
const dv = new DataView(buffer);
dv.setUint32(0, 1 + this.data_.length);
u8[4] = this.type;
return lib.array.concatTyped(u8, this.data_);
};
/**
* Check whether the end of the raw message data has been reached.
*
* @return {boolean} true if the end of the raw message data has been reached;
* false otherwise.
*/
nassh.agent.Message.prototype.eom = function() {
return this.offset_ === this.data_.length;
};
/**
* Read a uint32 from the raw message data.
*
* @see https://tools.ietf.org/html/rfc4251#section-5
* @throws Will throw an error if there are less than four more bytes available.
* @return {number}
*/
nassh.agent.Message.prototype.readUint32 = function() {
if (this.data_.length < this.offset_ + 4) {
throw new Error('Message.readUint32: end of data_ reached prematurely');
}
const dv = new DataView(this.data_.buffer, this.data_.byteOffset);
const uint32 = dv.getUint32(this.offset_);
this.offset_ += 4;
return uint32;
};
/**
* Write a uint32 to the raw message data.
*
* @see https://tools.ietf.org/html/rfc4251#section-5
* @param {number} uint32 An unsigned 32-bit integer.
*/
nassh.agent.Message.prototype.writeUint32 = function(uint32) {
if (!Number.isSafeInteger(uint32)) {
throw new Error(`Message.writeUint32: ${uint32} is not a (safe) integer`);
}
const buffer = new ArrayBuffer(4);
const dv = new DataView(buffer);
dv.setUint32(0, uint32);
this.data_ = lib.array.concatTyped(this.data_, new Uint8Array(buffer));
};
/**
* Read a string from the raw message data.
*
* @see https://tools.ietf.org/html/rfc4251#section-5
* @throws Will throw an error if there are less bytes available than indicated
* by the length field.
* @return {!Uint8Array}
*/
nassh.agent.Message.prototype.readString = function() {
const length = this.readUint32();
if (this.data_.length < this.offset_ + length) {
throw new Error('Message.readString: end of data_ reached prematurely');
}
const string = this.data_.slice(this.offset_, this.offset_ + length);
this.offset_ += length;
return string;
};
/**
* Write a string to the raw message data.
*
* @see https://tools.ietf.org/html/rfc4251#section-5
* @param {!Uint8Array} string
*/
nassh.agent.Message.prototype.writeString = function(string) {
if (!(string instanceof Uint8Array)) {
throw new Error('Message.writeString: string is not of type Uint8Array');
}
const length = string.length;
this.writeUint32(length);
this.data_ = lib.array.concatTyped(this.data_, string);
};
/**
* Parse a raw SSH agent message into a Message object.
*
* @see https://tools.ietf.org/id/draft-miller-ssh-agent-00.html#rfc.section.3
* @see https://tools.ietf.org/id/draft-miller-ssh-agent-00.html#rfc.section.4
* @constructs nassh.agent.Message
* @param {!Uint8Array} rawMessage
* @return {?nassh.agent.Message} A Message object created from the raw message
* data; null if the raw message data is malformed.
*/
nassh.agent.Message.fromRawMessage = function(rawMessage) {
if (rawMessage.length < 5) {
return null;
}
const dv = new DataView(rawMessage.buffer, rawMessage.byteOffset);
const length = dv.getUint32(0);
if (length + 4 !== rawMessage.length) {
return null;
}
const message = new nassh.agent.Message(
/** @type {!nassh.agent.messages.Numbers} */ (rawMessage[4]),
rawMessage.slice(5));
return nassh.agent.messages.read(message);
};