"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/sshpk/lib/formats/x509-pem.js" (7 Feb 2017, 1858 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 2016 Joyent, Inc.
    2 
    3 var x509 = require('./x509');
    4 
    5 module.exports = {
    6     read: read,
    7     verify: x509.verify,
    8     sign: x509.sign,
    9     write: write
   10 };
   11 
   12 var assert = require('assert-plus');
   13 var asn1 = require('asn1');
   14 var algs = require('../algs');
   15 var utils = require('../utils');
   16 var Key = require('../key');
   17 var PrivateKey = require('../private-key');
   18 var pem = require('./pem');
   19 var Identity = require('../identity');
   20 var Signature = require('../signature');
   21 var Certificate = require('../certificate');
   22 
   23 function read(buf, options) {
   24     if (typeof (buf) !== 'string') {
   25         assert.buffer(buf, 'buf');
   26         buf = buf.toString('ascii');
   27     }
   28 
   29     var lines = buf.trim().split(/[\r\n]+/g);
   30 
   31     var m = lines[0].match(/*JSSTYLED*/
   32         /[-]+[ ]*BEGIN CERTIFICATE[ ]*[-]+/);
   33     assert.ok(m, 'invalid PEM header');
   34 
   35     var m2 = lines[lines.length - 1].match(/*JSSTYLED*/
   36         /[-]+[ ]*END CERTIFICATE[ ]*[-]+/);
   37     assert.ok(m2, 'invalid PEM footer');
   38 
   39     var headers = {};
   40     while (true) {
   41         lines = lines.slice(1);
   42         m = lines[0].match(/*JSSTYLED*/
   43             /^([A-Za-z0-9-]+): (.+)$/);
   44         if (!m)
   45             break;
   46         headers[m[1].toLowerCase()] = m[2];
   47     }
   48 
   49     /* Chop off the first and last lines */
   50     lines = lines.slice(0, -1).join('');
   51     buf = new Buffer(lines, 'base64');
   52 
   53     return (x509.read(buf, options));
   54 }
   55 
   56 function write(cert, options) {
   57     var dbuf = x509.write(cert, options);
   58 
   59     var header = 'CERTIFICATE';
   60     var tmp = dbuf.toString('base64');
   61     var len = tmp.length + (tmp.length / 64) +
   62         18 + 16 + header.length*2 + 10;
   63     var buf = new Buffer(len);
   64     var o = 0;
   65     o += buf.write('-----BEGIN ' + header + '-----\n', o);
   66     for (var i = 0; i < tmp.length; ) {
   67         var limit = i + 64;
   68         if (limit > tmp.length)
   69             limit = tmp.length;
   70         o += buf.write(tmp.slice(i, limit), o);
   71         buf[o++] = 10;
   72         i = limit;
   73     }
   74     o += buf.write('-----END ' + header + '-----\n', o);
   75 
   76     return (buf.slice(0, o));
   77 }