]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/node_modules/request/node_modules/http-signature/node_modules/sshpk/lib/formats/ssh-private.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / node_modules / request / node_modules / http-signature / node_modules / sshpk / lib / formats / ssh-private.js
1 // Copyright 2015 Joyent, Inc.
2
3 module.exports = {
4         read: read,
5         readSSHPrivate: readSSHPrivate,
6         write: write
7 };
8
9 var assert = require('assert-plus');
10 var asn1 = require('asn1');
11 var algs = require('../algs');
12 var utils = require('../utils');
13 var crypto = require('crypto');
14
15 var Key = require('../key');
16 var PrivateKey = require('../private-key');
17 var pem = require('./pem');
18 var rfc4253 = require('./rfc4253');
19 var SSHBuffer = require('../ssh-buffer');
20
21 function read(buf, options) {
22         return (pem.read(buf, options));
23 }
24
25 var MAGIC = 'openssh-key-v1';
26
27 function readSSHPrivate(type, buf) {
28         buf = new SSHBuffer({buffer: buf});
29
30         var magic = buf.readCString();
31         assert.strictEqual(magic, MAGIC, 'bad magic string');
32
33         var cipher = buf.readString();
34         var kdf = buf.readString();
35
36         /* We only support unencrypted keys. */
37         if (cipher !== 'none' || kdf !== 'none') {
38                 throw (new Error('OpenSSH-format key is encrypted ' +
39                      '(password-protected). Please use the SSH agent ' +
40                      'or decrypt the key.'));
41         }
42
43         /* Skip over kdfoptions. */
44         buf.readString();
45
46         var nkeys = buf.readInt();
47         if (nkeys !== 1) {
48                 throw (new Error('OpenSSH-format key file contains ' +
49                     'multiple keys: this is unsupported.'));
50         }
51
52         var pubKey = buf.readBuffer();
53
54         if (type === 'public') {
55                 assert.ok(buf.atEnd(), 'excess bytes left after key');
56                 return (rfc4253.read(pubKey));
57         }
58
59         var privKeyBlob = buf.readBuffer();
60         assert.ok(buf.atEnd(), 'excess bytes left after key');
61
62         buf = new SSHBuffer({buffer: privKeyBlob});
63
64         var checkInt1 = buf.readInt();
65         var checkInt2 = buf.readInt();
66         assert.strictEqual(checkInt1, checkInt2, 'checkints do not match');
67
68         var ret = {};
69         var key = rfc4253.readInternal(ret, 'private', buf.remainder());
70
71         buf.skip(ret.consumed);
72
73         var comment = buf.readString();
74         key.comment = comment;
75
76         return (key);
77 }
78
79 function write(key, options) {
80         var pubKey;
81         if (PrivateKey.isPrivateKey(key))
82                 pubKey = key.toPublic();
83         else
84                 pubKey = key;
85
86         var privBuf;
87         if (PrivateKey.isPrivateKey(key)) {
88                 privBuf = new SSHBuffer({});
89                 var checkInt = crypto.randomBytes(4).readUInt32BE(0);
90                 privBuf.writeInt(checkInt);
91                 privBuf.writeInt(checkInt);
92                 privBuf.write(key.toBuffer('rfc4253'));
93                 privBuf.writeString(key.comment || '');
94
95                 var n = 1;
96                 while (privBuf._offset % 8 !== 0)
97                         privBuf.writeChar(n++);
98         }
99
100         var buf = new SSHBuffer({});
101
102         buf.writeCString(MAGIC);
103         buf.writeString('none');        /* cipher */
104         buf.writeString('none');        /* kdf */
105         buf.writeBuffer(new Buffer(0)); /* kdfoptions */
106
107         buf.writeInt(1);                /* nkeys */
108         buf.writeBuffer(pubKey.toBuffer('rfc4253'));
109
110         if (privBuf)
111                 buf.writeBuffer(privBuf.toBuffer());
112
113         buf = buf.toBuffer();
114
115         var header;
116         if (PrivateKey.isPrivateKey(key))
117                 header = 'OPENSSH PRIVATE KEY';
118         else
119                 header = 'OPENSSH PUBLIC KEY';
120
121         var tmp = buf.toString('base64');
122         var len = tmp.length + (tmp.length / 70) +
123             18 + 16 + header.length*2 + 10;
124         buf = new Buffer(len);
125         var o = 0;
126         o += buf.write('-----BEGIN ' + header + '-----\n', o);
127         for (var i = 0; i < tmp.length; ) {
128                 var limit = i + 70;
129                 if (limit > tmp.length)
130                         limit = tmp.length;
131                 o += buf.write(tmp.slice(i, limit), o);
132                 buf[o++] = 10;
133                 i = limit;
134         }
135         o += buf.write('-----END ' + header + '-----\n', o);
136
137         return (buf.slice(0, o));
138 }