Skip to content

Commit 694af28

Browse files
committed
update readme & version bump
1 parent 516430a commit 694af28

File tree

3 files changed

+63
-60
lines changed

3 files changed

+63
-60
lines changed

Diff for: README.md

+3
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,9 @@ Questions, comments, bug reports, and pull requests are all welcome.
245245

246246
## Changelog
247247

248+
### 1.1.0
249+
* Added OpenSSH key format support.
250+
248251
### 1.0.2
249252
* Importing keys from PEM now is less dependent on non-key data in files.
250253

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-rsa",
3-
"version": "1.0.8",
3+
"version": "1.1.0",
44
"description": "Node.js RSA library",
55
"main": "src/NodeRSA.js",
66
"scripts": {

Diff for: src/formats/openssh.js

+59-59
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
var _ = require('../utils')._;
2-
var utils = require('../utils');
3-
var BigInteger = require('../libs/jsbn');
1+
var _ = require("../utils")._;
2+
var utils = require("../utils");
3+
var BigInteger = require("../libs/jsbn");
44

5-
const PRIVATE_OPENING_BOUNDARY = '-----BEGIN OPENSSH PRIVATE KEY-----';
6-
const PRIVATE_CLOSING_BOUNDARY = '-----END OPENSSH PRIVATE KEY-----';
5+
const PRIVATE_OPENING_BOUNDARY = "-----BEGIN OPENSSH PRIVATE KEY-----";
6+
const PRIVATE_CLOSING_BOUNDARY = "-----END OPENSSH PRIVATE KEY-----";
77

88
module.exports = {
99
privateExport: function (key, options) {
@@ -12,14 +12,14 @@ module.exports = {
1212
let ebuf = Buffer.alloc(4)
1313
ebuf.writeUInt32BE(key.e, 0);
1414
//Slice leading zeroes
15-
while(ebuf[0] === 0) ebuf = ebuf.slice(1);
15+
while (ebuf[0] === 0) ebuf = ebuf.slice(1);
1616

1717
const dbuf = key.d.toBuffer();
1818
const coeffbuf = key.coeff.toBuffer();
1919
const pbuf = key.p.toBuffer();
2020
const qbuf = key.q.toBuffer();
2121
let commentbuf;
22-
if(typeof key.sshcomment !== 'undefined'){
22+
if (typeof key.sshcomment !== "undefined") {
2323
commentbuf = Buffer.from(key.sshcomment);
2424
} else {
2525
commentbuf = Buffer.from([]);
@@ -51,23 +51,23 @@ module.exports = {
5151
4 + //32bit private+checksum+comment+padding length
5252
privateKeyLength;
5353

54-
const paddingLength = Math.ceil(privateKeyLength / 8)*8 - privateKeyLength;
54+
const paddingLength = Math.ceil(privateKeyLength / 8) * 8 - privateKeyLength;
5555
length += paddingLength;
5656

5757
const buf = Buffer.alloc(length);
58-
const writer = {buf:buf, off: 0};
59-
buf.write('openssh-key-v1', 'utf8');
58+
const writer = {buf: buf, off: 0};
59+
buf.write("openssh-key-v1", "utf8");
6060
buf.writeUInt8(0, 14);
6161
writer.off += 15;
6262

63-
writeOpenSSHKeyString(writer, Buffer.from('none'));
64-
writeOpenSSHKeyString(writer, Buffer.from('none'));
65-
writeOpenSSHKeyString(writer, Buffer.from(''));
63+
writeOpenSSHKeyString(writer, Buffer.from("none"));
64+
writeOpenSSHKeyString(writer, Buffer.from("none"));
65+
writeOpenSSHKeyString(writer, Buffer.from(""));
6666

6767
writer.off = writer.buf.writeUInt32BE(1, writer.off);
6868
writer.off = writer.buf.writeUInt32BE(pubkeyLength, writer.off);
6969

70-
writeOpenSSHKeyString(writer, Buffer.from('ssh-rsa'));
70+
writeOpenSSHKeyString(writer, Buffer.from("ssh-rsa"));
7171
writeOpenSSHKeyString(writer, ebuf);
7272
writeOpenSSHKeyString(writer, nbuf);
7373

@@ -77,7 +77,7 @@ module.exports = {
7777
);
7878
writer.off += 8;
7979

80-
writeOpenSSHKeyString(writer, Buffer.from('ssh-rsa'));
80+
writeOpenSSHKeyString(writer, Buffer.from("ssh-rsa"));
8181
writeOpenSSHKeyString(writer, nbuf);
8282
writeOpenSSHKeyString(writer, ebuf);
8383
writeOpenSSHKeyString(writer, dbuf);
@@ -87,70 +87,70 @@ module.exports = {
8787
writeOpenSSHKeyString(writer, commentbuf);
8888

8989
let pad = 0x01;
90-
while(writer.off < length){
90+
while (writer.off < length) {
9191
writer.off = writer.buf.writeUInt8(pad++, writer.off);
9292
}
9393

94-
if(options.type === 'der'){
94+
if (options.type === "der") {
9595
return writer.buf
9696
} else {
97-
return PRIVATE_OPENING_BOUNDARY + '\n' + utils.linebrk(buf.toString('base64'), 70) + '\n' + PRIVATE_CLOSING_BOUNDARY + '\n';
97+
return PRIVATE_OPENING_BOUNDARY + "\n" + utils.linebrk(buf.toString("base64"), 70) + "\n" + PRIVATE_CLOSING_BOUNDARY + "\n";
9898
}
9999
},
100100

101101
privateImport: function (key, data, options) {
102102
options = options || {};
103103
var buffer;
104104

105-
if (options.type !== 'der') {
105+
if (options.type !== "der") {
106106
if (Buffer.isBuffer(data)) {
107-
data = data.toString('utf8');
107+
data = data.toString("utf8");
108108
}
109109

110110
if (_.isString(data)) {
111111
var pem = utils.trimSurroundingText(data, PRIVATE_OPENING_BOUNDARY, PRIVATE_CLOSING_BOUNDARY)
112-
.replace(/\s+|\n\r|\n|\r$/gm, '');
113-
buffer = Buffer.from(pem, 'base64');
112+
.replace(/\s+|\n\r|\n|\r$/gm, "");
113+
buffer = Buffer.from(pem, "base64");
114114
} else {
115-
throw Error('Unsupported key format');
115+
throw Error("Unsupported key format");
116116
}
117117
} else if (Buffer.isBuffer(data)) {
118118
buffer = data;
119119
} else {
120-
throw Error('Unsupported key format');
120+
throw Error("Unsupported key format");
121121
}
122122

123-
const reader = {buf:buffer, off:0};
123+
const reader = {buf: buffer, off: 0};
124124

125-
if(buffer.slice(0,14).toString('ascii') !== 'openssh-key-v1')
126-
throw 'Invalid file format.';
125+
if (buffer.slice(0, 14).toString("ascii") !== "openssh-key-v1")
126+
throw "Invalid file format.";
127127

128128
reader.off += 15;
129129

130130
//ciphername
131-
if(readOpenSSHKeyString(reader).toString('ascii') !== 'none')
132-
throw Error('Unsupported key type');
131+
if (readOpenSSHKeyString(reader).toString("ascii") !== "none")
132+
throw Error("Unsupported key type");
133133
//kdfname
134-
if(readOpenSSHKeyString(reader).toString('ascii') !== 'none')
135-
throw Error('Unsupported key type');
134+
if (readOpenSSHKeyString(reader).toString("ascii") !== "none")
135+
throw Error("Unsupported key type");
136136
//kdf
137-
if(readOpenSSHKeyString(reader).toString('ascii') !== '')
138-
throw Error('Unsupported key type');
137+
if (readOpenSSHKeyString(reader).toString("ascii") !== "")
138+
throw Error("Unsupported key type");
139139
//keynum
140140
reader.off += 4;
141141

142142
//sshpublength
143143
reader.off += 4;
144144

145145
//keytype
146-
if(readOpenSSHKeyString(reader).toString('ascii') !== 'ssh-rsa')
147-
throw Error('Unsupported key type');
146+
if (readOpenSSHKeyString(reader).toString("ascii") !== "ssh-rsa")
147+
throw Error("Unsupported key type");
148148
readOpenSSHKeyString(reader);
149149
readOpenSSHKeyString(reader);
150150

151151
reader.off += 12;
152-
if(readOpenSSHKeyString(reader).toString('ascii') !== 'ssh-rsa')
153-
throw Error('Unsupported key type');
152+
if (readOpenSSHKeyString(reader).toString("ascii") !== "ssh-rsa")
153+
throw Error("Unsupported key type");
154154

155155
const n = readOpenSSHKeyString(reader);
156156
const e = readOpenSSHKeyString(reader);
@@ -177,75 +177,75 @@ module.exports = {
177177
coeff // coefficient -- (inverse of q) mod p
178178
);
179179

180-
key.sshcomment = readOpenSSHKeyString(reader).toString('ascii');
180+
key.sshcomment = readOpenSSHKeyString(reader).toString("ascii");
181181
},
182182

183183
publicExport: function (key, options) {
184184
let ebuf = Buffer.alloc(4)
185185
ebuf.writeUInt32BE(key.e, 0);
186186
//Slice leading zeroes
187-
while(ebuf[0] === 0) ebuf = ebuf.slice(1);
187+
while (ebuf[0] === 0) ebuf = ebuf.slice(1);
188188
const nbuf = key.n.toBuffer();
189189
const buf = Buffer.alloc(
190190
ebuf.byteLength + 4 +
191191
nbuf.byteLength + 4 +
192-
'ssh-rsa'.length + 4
192+
"ssh-rsa".length + 4
193193
);
194194

195195
const writer = {buf: buf, off: 0};
196-
writeOpenSSHKeyString(writer, Buffer.from('ssh-rsa'));
196+
writeOpenSSHKeyString(writer, Buffer.from("ssh-rsa"));
197197
writeOpenSSHKeyString(writer, ebuf);
198198
writeOpenSSHKeyString(writer, nbuf);
199199

200-
let comment = key.sshcomment || '';
200+
let comment = key.sshcomment || "";
201201

202-
if(options.type === 'der'){
202+
if (options.type === "der") {
203203
return writer.buf
204204
} else {
205-
return 'ssh-rsa ' + buf.toString('base64') + ' ' + comment + '\n';
205+
return "ssh-rsa " + buf.toString("base64") + " " + comment + "\n";
206206
}
207207
},
208208

209209
publicImport: function (key, data, options) {
210210
options = options || {};
211211
var buffer;
212212

213-
if (options.type !== 'der') {
213+
if (options.type !== "der") {
214214
if (Buffer.isBuffer(data)) {
215-
data = data.toString('utf8');
215+
data = data.toString("utf8");
216216
}
217217

218218
if (_.isString(data)) {
219-
if(data.substring(0, 8) !== 'ssh-rsa ')
220-
throw Error('Unsupported key format');
221-
let pemEnd = data.indexOf(' ', 8);
219+
if (data.substring(0, 8) !== "ssh-rsa ")
220+
throw Error("Unsupported key format");
221+
let pemEnd = data.indexOf(" ", 8);
222222

223223
//Handle keys with no comment
224-
if(pemEnd === -1){
224+
if (pemEnd === -1) {
225225
pemEnd = data.length;
226226
} else {
227227
key.sshcomment = data.substring(pemEnd + 1)
228-
.replace(/\s+|\n\r|\n|\r$/gm, '');
228+
.replace(/\s+|\n\r|\n|\r$/gm, "");
229229
}
230230

231231
const pem = data.substring(8, pemEnd)
232-
.replace(/\s+|\n\r|\n|\r$/gm, '');
233-
buffer = Buffer.from(pem, 'base64');
232+
.replace(/\s+|\n\r|\n|\r$/gm, "");
233+
buffer = Buffer.from(pem, "base64");
234234
} else {
235-
throw Error('Unsupported key format');
235+
throw Error("Unsupported key format");
236236
}
237237
} else if (Buffer.isBuffer(data)) {
238238
buffer = data;
239239
} else {
240-
throw Error('Unsupported key format');
240+
throw Error("Unsupported key format");
241241
}
242242

243-
const reader = {buf:buffer, off:0};
243+
const reader = {buf: buffer, off: 0};
244244

245-
const type = readOpenSSHKeyString(reader).toString('ascii');
245+
const type = readOpenSSHKeyString(reader).toString("ascii");
246246

247-
if(type !== 'ssh-rsa')
248-
throw Error('Invalid key type: '+ type);
247+
if (type !== "ssh-rsa")
248+
throw Error("Invalid key type: " + type);
249249

250250
const e = readOpenSSHKeyString(reader);
251251
const n = readOpenSSHKeyString(reader);

0 commit comments

Comments
 (0)