"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/sshpk/lib/formats/auto.js" (31 May 2018, 2871 Bytes) of package /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 utils = require('../utils');
   10 var Key = require('../key');
   11 var PrivateKey = require('../private-key');
   12 
   13 var pem = require('./pem');
   14 var ssh = require('./ssh');
   15 var rfc4253 = require('./rfc4253');
   16 var dnssec = require('./dnssec');
   17 
   18 var DNSSEC_PRIVKEY_HEADER_PREFIX = 'Private-key-format: v1';
   19 
   20 function read(buf, options) {
   21     if (typeof (buf) === 'string') {
   22         if (buf.trim().match(/^[-]+[ ]*BEGIN/))
   23             return (pem.read(buf, options));
   24         if (buf.match(/^\s*ssh-[a-z]/))
   25             return (ssh.read(buf, options));
   26         if (buf.match(/^\s*ecdsa-/))
   27             return (ssh.read(buf, options));
   28         if (findDNSSECHeader(buf))
   29             return (dnssec.read(buf, options));
   30         buf = new Buffer(buf, 'binary');
   31     } else {
   32         assert.buffer(buf);
   33         if (findPEMHeader(buf))
   34             return (pem.read(buf, options));
   35         if (findSSHHeader(buf))
   36             return (ssh.read(buf, options));
   37         if (findDNSSECHeader(buf))
   38             return (dnssec.read(buf, options));
   39     }
   40     if (buf.readUInt32BE(0) < buf.length)
   41         return (rfc4253.read(buf, options));
   42     throw (new Error('Failed to auto-detect format of key'));
   43 }
   44 
   45 function findSSHHeader(buf) {
   46     var offset = 0;
   47     while (offset < buf.length &&
   48         (buf[offset] === 32 || buf[offset] === 10 || buf[offset] === 9))
   49         ++offset;
   50     if (offset + 4 <= buf.length &&
   51         buf.slice(offset, offset + 4).toString('ascii') === 'ssh-')
   52         return (true);
   53     if (offset + 6 <= buf.length &&
   54         buf.slice(offset, offset + 6).toString('ascii') === 'ecdsa-')
   55         return (true);
   56     return (false);
   57 }
   58 
   59 function findPEMHeader(buf) {
   60     var offset = 0;
   61     while (offset < buf.length &&
   62         (buf[offset] === 32 || buf[offset] === 10))
   63         ++offset;
   64     if (buf[offset] !== 45)
   65         return (false);
   66     while (offset < buf.length &&
   67         (buf[offset] === 45))
   68         ++offset;
   69     while (offset < buf.length &&
   70         (buf[offset] === 32))
   71         ++offset;
   72     if (offset + 5 > buf.length ||
   73         buf.slice(offset, offset + 5).toString('ascii') !== 'BEGIN')
   74         return (false);
   75     return (true);
   76 }
   77 
   78 function findDNSSECHeader(buf) {
   79     // private case first
   80     if (buf.length <= DNSSEC_PRIVKEY_HEADER_PREFIX.length)
   81         return (false);
   82     var headerCheck = buf.slice(0, DNSSEC_PRIVKEY_HEADER_PREFIX.length);
   83     if (headerCheck.toString('ascii') === DNSSEC_PRIVKEY_HEADER_PREFIX)
   84         return (true);
   85 
   86     // public-key RFC3110 ?
   87     // 'domain.com. IN KEY ...' or 'domain.com. IN DNSKEY ...'
   88     // skip any comment-lines
   89     if (typeof (buf) !== 'string') {
   90         buf = buf.toString('ascii');
   91     }
   92     var lines = buf.split('\n');
   93     var line = 0;
   94     /* JSSTYLED */
   95     while (lines[line].match(/^\;/))
   96         line++;
   97     if (lines[line].toString('ascii').match(/\. IN KEY /))
   98         return (true);
   99     if (lines[line].toString('ascii').match(/\. IN DNSKEY /))
  100         return (true);
  101     return (false);
  102 }
  103 
  104 function write(key, options) {
  105     throw (new Error('"auto" format cannot be used for writing'));
  106 }