"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/sshpk/lib/formats/ssh.js" (7 Feb 2017, 3092 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,
    5     write: write
    6 };
    7 
    8 var assert = require('assert-plus');
    9 var rfc4253 = require('./rfc4253');
   10 var utils = require('../utils');
   11 var Key = require('../key');
   12 var PrivateKey = require('../private-key');
   13 
   14 var sshpriv = require('./ssh-private');
   15 
   16 /*JSSTYLED*/
   17 var SSHKEY_RE = /^([a-z0-9-]+)[ \t]+([a-zA-Z0-9+\/]+[=]*)([\n \t]+([^\n]+))?$/;
   18 /*JSSTYLED*/
   19 var SSHKEY_RE2 = /^([a-z0-9-]+)[ \t]+([a-zA-Z0-9+\/ \t\n]+[=]*)(.*)$/;
   20 
   21 function read(buf, options) {
   22     if (typeof (buf) !== 'string') {
   23         assert.buffer(buf, 'buf');
   24         buf = buf.toString('ascii');
   25     }
   26 
   27     var trimmed = buf.trim().replace(/[\\\r]/g, '');
   28     var m = trimmed.match(SSHKEY_RE);
   29     if (!m)
   30         m = trimmed.match(SSHKEY_RE2);
   31     assert.ok(m, 'key must match regex');
   32 
   33     var type = rfc4253.algToKeyType(m[1]);
   34     var kbuf = new Buffer(m[2], 'base64');
   35 
   36     /*
   37      * This is a bit tricky. If we managed to parse the key and locate the
   38      * key comment with the regex, then do a non-partial read and assert
   39      * that we have consumed all bytes. If we couldn't locate the key
   40      * comment, though, there may be whitespace shenanigans going on that
   41      * have conjoined the comment to the rest of the key. We do a partial
   42      * read in this case to try to make the best out of a sorry situation.
   43      */
   44     var key;
   45     var ret = {};
   46     if (m[4]) {
   47         try {
   48             key = rfc4253.read(kbuf);
   49 
   50         } catch (e) {
   51             m = trimmed.match(SSHKEY_RE2);
   52             assert.ok(m, 'key must match regex');
   53             kbuf = new Buffer(m[2], 'base64');
   54             key = rfc4253.readInternal(ret, 'public', kbuf);
   55         }
   56     } else {
   57         key = rfc4253.readInternal(ret, 'public', kbuf);
   58     }
   59 
   60     assert.strictEqual(type, key.type);
   61 
   62     if (m[4] && m[4].length > 0) {
   63         key.comment = m[4];
   64 
   65     } else if (ret.consumed) {
   66         /*
   67          * Now the magic: trying to recover the key comment when it's
   68          * gotten conjoined to the key or otherwise shenanigan'd.
   69          *
   70          * Work out how much base64 we used, then drop all non-base64
   71          * chars from the beginning up to this point in the the string.
   72          * Then offset in this and try to make up for missing = chars.
   73          */
   74         var data = m[2] + m[3];
   75         var realOffset = Math.ceil(ret.consumed / 3) * 4;
   76         data = data.slice(0, realOffset - 2). /*JSSTYLED*/
   77             replace(/[^a-zA-Z0-9+\/=]/g, '') +
   78             data.slice(realOffset - 2);
   79 
   80         var padding = ret.consumed % 3;
   81         if (padding > 0 &&
   82             data.slice(realOffset - 1, realOffset) !== '=')
   83             realOffset--;
   84         while (data.slice(realOffset, realOffset + 1) === '=')
   85             realOffset++;
   86 
   87         /* Finally, grab what we think is the comment & clean it up. */
   88         var trailer = data.slice(realOffset);
   89         trailer = trailer.replace(/[\r\n]/g, ' ').
   90             replace(/^\s+/, '');
   91         if (trailer.match(/^[a-zA-Z0-9]/))
   92             key.comment = trailer;
   93     }
   94 
   95     return (key);
   96 }
   97 
   98 function write(key, options) {
   99     assert.object(key);
  100     if (!Key.isKey(key))
  101         throw (new Error('Must be a public key'));
  102 
  103     var parts = [];
  104     var alg = rfc4253.keyTypeToAlg(key);
  105     parts.push(alg);
  106 
  107     var buf = rfc4253.write(key);
  108     parts.push(buf.toString('base64'));
  109 
  110     if (key.comment)
  111         parts.push(key.comment);
  112 
  113     return (new Buffer(parts.join(' ')));
  114 }