"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/sshpk/lib/formats/pkcs1.js" (8 Mar 2017, 7696 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     readPkcs1: readPkcs1,
    6     write: write,
    7     writePkcs1: writePkcs1
    8 };
    9 
   10 var assert = require('assert-plus');
   11 var asn1 = require('asn1');
   12 var algs = require('../algs');
   13 var utils = require('../utils');
   14 
   15 var Key = require('../key');
   16 var PrivateKey = require('../private-key');
   17 var pem = require('./pem');
   18 
   19 var pkcs8 = require('./pkcs8');
   20 var readECDSACurve = pkcs8.readECDSACurve;
   21 
   22 function read(buf, options) {
   23     return (pem.read(buf, options, 'pkcs1'));
   24 }
   25 
   26 function write(key, options) {
   27     return (pem.write(key, options, 'pkcs1'));
   28 }
   29 
   30 /* Helper to read in a single mpint */
   31 function readMPInt(der, nm) {
   32     assert.strictEqual(der.peek(), asn1.Ber.Integer,
   33         nm + ' is not an Integer');
   34     return (utils.mpNormalize(der.readString(asn1.Ber.Integer, true)));
   35 }
   36 
   37 function readPkcs1(alg, type, der) {
   38     switch (alg) {
   39     case 'RSA':
   40         if (type === 'public')
   41             return (readPkcs1RSAPublic(der));
   42         else if (type === 'private')
   43             return (readPkcs1RSAPrivate(der));
   44         throw (new Error('Unknown key type: ' + type));
   45     case 'DSA':
   46         if (type === 'public')
   47             return (readPkcs1DSAPublic(der));
   48         else if (type === 'private')
   49             return (readPkcs1DSAPrivate(der));
   50         throw (new Error('Unknown key type: ' + type));
   51     case 'EC':
   52     case 'ECDSA':
   53         if (type === 'private')
   54             return (readPkcs1ECDSAPrivate(der));
   55         else if (type === 'public')
   56             return (readPkcs1ECDSAPublic(der));
   57         throw (new Error('Unknown key type: ' + type));
   58     default:
   59         throw (new Error('Unknown key algo: ' + alg));
   60     }
   61 }
   62 
   63 function readPkcs1RSAPublic(der) {
   64     // modulus and exponent
   65     var n = readMPInt(der, 'modulus');
   66     var e = readMPInt(der, 'exponent');
   67 
   68     // now, make the key
   69     var key = {
   70         type: 'rsa',
   71         parts: [
   72             { name: 'e', data: e },
   73             { name: 'n', data: n }
   74         ]
   75     };
   76 
   77     return (new Key(key));
   78 }
   79 
   80 function readPkcs1RSAPrivate(der) {
   81     var version = readMPInt(der, 'version');
   82     assert.strictEqual(version[0], 0);
   83 
   84     // modulus then public exponent
   85     var n = readMPInt(der, 'modulus');
   86     var e = readMPInt(der, 'public exponent');
   87     var d = readMPInt(der, 'private exponent');
   88     var p = readMPInt(der, 'prime1');
   89     var q = readMPInt(der, 'prime2');
   90     var dmodp = readMPInt(der, 'exponent1');
   91     var dmodq = readMPInt(der, 'exponent2');
   92     var iqmp = readMPInt(der, 'iqmp');
   93 
   94     // now, make the key
   95     var key = {
   96         type: 'rsa',
   97         parts: [
   98             { name: 'n', data: n },
   99             { name: 'e', data: e },
  100             { name: 'd', data: d },
  101             { name: 'iqmp', data: iqmp },
  102             { name: 'p', data: p },
  103             { name: 'q', data: q },
  104             { name: 'dmodp', data: dmodp },
  105             { name: 'dmodq', data: dmodq }
  106         ]
  107     };
  108 
  109     return (new PrivateKey(key));
  110 }
  111 
  112 function readPkcs1DSAPrivate(der) {
  113     var version = readMPInt(der, 'version');
  114     assert.strictEqual(version.readUInt8(0), 0);
  115 
  116     var p = readMPInt(der, 'p');
  117     var q = readMPInt(der, 'q');
  118     var g = readMPInt(der, 'g');
  119     var y = readMPInt(der, 'y');
  120     var x = readMPInt(der, 'x');
  121 
  122     // now, make the key
  123     var key = {
  124         type: 'dsa',
  125         parts: [
  126             { name: 'p', data: p },
  127             { name: 'q', data: q },
  128             { name: 'g', data: g },
  129             { name: 'y', data: y },
  130             { name: 'x', data: x }
  131         ]
  132     };
  133 
  134     return (new PrivateKey(key));
  135 }
  136 
  137 function readPkcs1DSAPublic(der) {
  138     var y = readMPInt(der, 'y');
  139     var p = readMPInt(der, 'p');
  140     var q = readMPInt(der, 'q');
  141     var g = readMPInt(der, 'g');
  142 
  143     var key = {
  144         type: 'dsa',
  145         parts: [
  146             { name: 'y', data: y },
  147             { name: 'p', data: p },
  148             { name: 'q', data: q },
  149             { name: 'g', data: g }
  150         ]
  151     };
  152 
  153     return (new Key(key));
  154 }
  155 
  156 function readPkcs1ECDSAPublic(der) {
  157     der.readSequence();
  158 
  159     var oid = der.readOID();
  160     assert.strictEqual(oid, '1.2.840.10045.2.1', 'must be ecPublicKey');
  161 
  162     var curveOid = der.readOID();
  163 
  164     var curve;
  165     var curves = Object.keys(algs.curves);
  166     for (var j = 0; j < curves.length; ++j) {
  167         var c = curves[j];
  168         var cd = algs.curves[c];
  169         if (cd.pkcs8oid === curveOid) {
  170             curve = c;
  171             break;
  172         }
  173     }
  174     assert.string(curve, 'a known ECDSA named curve');
  175 
  176     var Q = der.readString(asn1.Ber.BitString, true);
  177     Q = utils.ecNormalize(Q);
  178 
  179     var key = {
  180         type: 'ecdsa',
  181         parts: [
  182             { name: 'curve', data: new Buffer(curve) },
  183             { name: 'Q', data: Q }
  184         ]
  185     };
  186 
  187     return (new Key(key));
  188 }
  189 
  190 function readPkcs1ECDSAPrivate(der) {
  191     var version = readMPInt(der, 'version');
  192     assert.strictEqual(version.readUInt8(0), 1);
  193 
  194     // private key
  195     var d = der.readString(asn1.Ber.OctetString, true);
  196 
  197     der.readSequence(0xa0);
  198     var curve = readECDSACurve(der);
  199     assert.string(curve, 'a known elliptic curve');
  200 
  201     der.readSequence(0xa1);
  202     var Q = der.readString(asn1.Ber.BitString, true);
  203     Q = utils.ecNormalize(Q);
  204 
  205     var key = {
  206         type: 'ecdsa',
  207         parts: [
  208             { name: 'curve', data: new Buffer(curve) },
  209             { name: 'Q', data: Q },
  210             { name: 'd', data: d }
  211         ]
  212     };
  213 
  214     return (new PrivateKey(key));
  215 }
  216 
  217 function writePkcs1(der, key) {
  218     der.startSequence();
  219 
  220     switch (key.type) {
  221     case 'rsa':
  222         if (PrivateKey.isPrivateKey(key))
  223             writePkcs1RSAPrivate(der, key);
  224         else
  225             writePkcs1RSAPublic(der, key);
  226         break;
  227     case 'dsa':
  228         if (PrivateKey.isPrivateKey(key))
  229             writePkcs1DSAPrivate(der, key);
  230         else
  231             writePkcs1DSAPublic(der, key);
  232         break;
  233     case 'ecdsa':
  234         if (PrivateKey.isPrivateKey(key))
  235             writePkcs1ECDSAPrivate(der, key);
  236         else
  237             writePkcs1ECDSAPublic(der, key);
  238         break;
  239     default:
  240         throw (new Error('Unknown key algo: ' + key.type));
  241     }
  242 
  243     der.endSequence();
  244 }
  245 
  246 function writePkcs1RSAPublic(der, key) {
  247     der.writeBuffer(key.part.n.data, asn1.Ber.Integer);
  248     der.writeBuffer(key.part.e.data, asn1.Ber.Integer);
  249 }
  250 
  251 function writePkcs1RSAPrivate(der, key) {
  252     var ver = new Buffer(1);
  253     ver[0] = 0;
  254     der.writeBuffer(ver, asn1.Ber.Integer);
  255 
  256     der.writeBuffer(key.part.n.data, asn1.Ber.Integer);
  257     der.writeBuffer(key.part.e.data, asn1.Ber.Integer);
  258     der.writeBuffer(key.part.d.data, asn1.Ber.Integer);
  259     der.writeBuffer(key.part.p.data, asn1.Ber.Integer);
  260     der.writeBuffer(key.part.q.data, asn1.Ber.Integer);
  261     if (!key.part.dmodp || !key.part.dmodq)
  262         utils.addRSAMissing(key);
  263     der.writeBuffer(key.part.dmodp.data, asn1.Ber.Integer);
  264     der.writeBuffer(key.part.dmodq.data, asn1.Ber.Integer);
  265     der.writeBuffer(key.part.iqmp.data, asn1.Ber.Integer);
  266 }
  267 
  268 function writePkcs1DSAPrivate(der, key) {
  269     var ver = new Buffer(1);
  270     ver[0] = 0;
  271     der.writeBuffer(ver, asn1.Ber.Integer);
  272 
  273     der.writeBuffer(key.part.p.data, asn1.Ber.Integer);
  274     der.writeBuffer(key.part.q.data, asn1.Ber.Integer);
  275     der.writeBuffer(key.part.g.data, asn1.Ber.Integer);
  276     der.writeBuffer(key.part.y.data, asn1.Ber.Integer);
  277     der.writeBuffer(key.part.x.data, asn1.Ber.Integer);
  278 }
  279 
  280 function writePkcs1DSAPublic(der, key) {
  281     der.writeBuffer(key.part.y.data, asn1.Ber.Integer);
  282     der.writeBuffer(key.part.p.data, asn1.Ber.Integer);
  283     der.writeBuffer(key.part.q.data, asn1.Ber.Integer);
  284     der.writeBuffer(key.part.g.data, asn1.Ber.Integer);
  285 }
  286 
  287 function writePkcs1ECDSAPublic(der, key) {
  288     der.startSequence();
  289 
  290     der.writeOID('1.2.840.10045.2.1'); /* ecPublicKey */
  291     var curve = key.part.curve.data.toString();
  292     var curveOid = algs.curves[curve].pkcs8oid;
  293     assert.string(curveOid, 'a known ECDSA named curve');
  294     der.writeOID(curveOid);
  295 
  296     der.endSequence();
  297 
  298     var Q = utils.ecNormalize(key.part.Q.data, true);
  299     der.writeBuffer(Q, asn1.Ber.BitString);
  300 }
  301 
  302 function writePkcs1ECDSAPrivate(der, key) {
  303     var ver = new Buffer(1);
  304     ver[0] = 1;
  305     der.writeBuffer(ver, asn1.Ber.Integer);
  306 
  307     der.writeBuffer(key.part.d.data, asn1.Ber.OctetString);
  308 
  309     der.startSequence(0xa0);
  310     var curve = key.part.curve.data.toString();
  311     var curveOid = algs.curves[curve].pkcs8oid;
  312     assert.string(curveOid, 'a known ECDSA named curve');
  313     der.writeOID(curveOid);
  314     der.endSequence();
  315 
  316     der.startSequence(0xa1);
  317     var Q = utils.ecNormalize(key.part.Q.data, true);
  318     der.writeBuffer(Q, asn1.Ber.BitString);
  319     der.endSequence();
  320 }