"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/sshpk/lib/formats/rfc4253.js" (7 Feb 2017, 3606 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 // Copyright 2015 Joyent, Inc.
    2 
    3 module.exports = {
    4     read: read.bind(undefined, false, undefined),
    5     readType: read.bind(undefined, false),
    6     write: write,
    7     /* semi-private api, used by sshpk-agent */
    8     readPartial: read.bind(undefined, true),
    9 
   10     /* shared with ssh format */
   11     readInternal: read,
   12     keyTypeToAlg: keyTypeToAlg,
   13     algToKeyType: algToKeyType
   14 };
   15 
   16 var assert = require('assert-plus');
   17 var algs = require('../algs');
   18 var utils = require('../utils');
   19 var Key = require('../key');
   20 var PrivateKey = require('../private-key');
   21 var SSHBuffer = require('../ssh-buffer');
   22 
   23 function algToKeyType(alg) {
   24     assert.string(alg);
   25     if (alg === 'ssh-dss')
   26         return ('dsa');
   27     else if (alg === 'ssh-rsa')
   28         return ('rsa');
   29     else if (alg === 'ssh-ed25519')
   30         return ('ed25519');
   31     else if (alg === 'ssh-curve25519')
   32         return ('curve25519');
   33     else if (alg.match(/^ecdsa-sha2-/))
   34         return ('ecdsa');
   35     else
   36         throw (new Error('Unknown algorithm ' + alg));
   37 }
   38 
   39 function keyTypeToAlg(key) {
   40     assert.object(key);
   41     if (key.type === 'dsa')
   42         return ('ssh-dss');
   43     else if (key.type === 'rsa')
   44         return ('ssh-rsa');
   45     else if (key.type === 'ed25519')
   46         return ('ssh-ed25519');
   47     else if (key.type === 'curve25519')
   48         return ('ssh-curve25519');
   49     else if (key.type === 'ecdsa')
   50         return ('ecdsa-sha2-' + key.part.curve.data.toString());
   51     else
   52         throw (new Error('Unknown key type ' + key.type));
   53 }
   54 
   55 function read(partial, type, buf, options) {
   56     if (typeof (buf) === 'string')
   57         buf = new Buffer(buf);
   58     assert.buffer(buf, 'buf');
   59 
   60     var key = {};
   61 
   62     var parts = key.parts = [];
   63     var sshbuf = new SSHBuffer({buffer: buf});
   64 
   65     var alg = sshbuf.readString();
   66     assert.ok(!sshbuf.atEnd(), 'key must have at least one part');
   67 
   68     key.type = algToKeyType(alg);
   69 
   70     var partCount = algs.info[key.type].parts.length;
   71     if (type && type === 'private')
   72         partCount = algs.privInfo[key.type].parts.length;
   73 
   74     while (!sshbuf.atEnd() && parts.length < partCount)
   75         parts.push(sshbuf.readPart());
   76     while (!partial && !sshbuf.atEnd())
   77         parts.push(sshbuf.readPart());
   78 
   79     assert.ok(parts.length >= 1,
   80         'key must have at least one part');
   81     assert.ok(partial || sshbuf.atEnd(),
   82         'leftover bytes at end of key');
   83 
   84     var Constructor = Key;
   85     var algInfo = algs.info[key.type];
   86     if (type === 'private' || algInfo.parts.length !== parts.length) {
   87         algInfo = algs.privInfo[key.type];
   88         Constructor = PrivateKey;
   89     }
   90     assert.strictEqual(algInfo.parts.length, parts.length);
   91 
   92     if (key.type === 'ecdsa') {
   93         var res = /^ecdsa-sha2-(.+)$/.exec(alg);
   94         assert.ok(res !== null);
   95         assert.strictEqual(res[1], parts[0].data.toString());
   96     }
   97 
   98     var normalized = true;
   99     for (var i = 0; i < algInfo.parts.length; ++i) {
  100         parts[i].name = algInfo.parts[i];
  101         if (parts[i].name !== 'curve' &&
  102             algInfo.normalize !== false) {
  103             var p = parts[i];
  104             var nd = utils.mpNormalize(p.data);
  105             if (nd !== p.data) {
  106                 p.data = nd;
  107                 normalized = false;
  108             }
  109         }
  110     }
  111 
  112     if (normalized)
  113         key._rfc4253Cache = sshbuf.toBuffer();
  114 
  115     if (partial && typeof (partial) === 'object') {
  116         partial.remainder = sshbuf.remainder();
  117         partial.consumed = sshbuf._offset;
  118     }
  119 
  120     return (new Constructor(key));
  121 }
  122 
  123 function write(key, options) {
  124     assert.object(key);
  125 
  126     var alg = keyTypeToAlg(key);
  127     var i;
  128 
  129     var algInfo = algs.info[key.type];
  130     if (PrivateKey.isPrivateKey(key))
  131         algInfo = algs.privInfo[key.type];
  132     var parts = algInfo.parts;
  133 
  134     var buf = new SSHBuffer({});
  135 
  136     buf.writeString(alg);
  137 
  138     for (i = 0; i < parts.length; ++i) {
  139         var data = key.part[parts[i]].data;
  140         if (algInfo.normalize !== false)
  141             data = utils.mpNormalize(data);
  142         buf.writeBuffer(data);
  143     }
  144 
  145     return (buf.toBuffer());
  146 }