PageRenderTime 2890ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/gulp-sass/node_modules/node-sass/node_modules/request/node_modules/http-signature/node_modules/sshpk/lib/formats/pkcs8.js

https://gitlab.com/KongMono/GuitarChord
JavaScript | 510 lines | 399 code | 86 blank | 25 comment | 32 complexity | 2d2d0b60191f8e6aa804a4b665d40ed2 MD5 | raw file
  1. // Copyright 2015 Joyent, Inc.
  2. module.exports = {
  3. read: read,
  4. readPkcs8: readPkcs8,
  5. write: write,
  6. writePkcs8: writePkcs8,
  7. readECDSACurve: readECDSACurve,
  8. writeECDSACurve: writeECDSACurve
  9. };
  10. var assert = require('assert-plus');
  11. var asn1 = require('asn1');
  12. var algs = require('../algs');
  13. var utils = require('../utils');
  14. var Key = require('../key');
  15. var PrivateKey = require('../private-key');
  16. var pem = require('./pem');
  17. function read(buf, options) {
  18. return (pem.read(buf, options, 'pkcs8'));
  19. }
  20. function write(key, options) {
  21. return (pem.write(key, options, 'pkcs8'));
  22. }
  23. /* Helper to read in a single mpint */
  24. function readMPInt(der, nm) {
  25. assert.strictEqual(der.peek(), asn1.Ber.Integer,
  26. nm + ' is not an Integer');
  27. return (utils.mpNormalize(der.readString(asn1.Ber.Integer, true)));
  28. }
  29. function readPkcs8(alg, type, der) {
  30. /* Private keys in pkcs#8 format have a weird extra int */
  31. if (der.peek() === asn1.Ber.Integer) {
  32. assert.strictEqual(type, 'private',
  33. 'unexpected Integer at start of public key');
  34. der.readString(asn1.Ber.Integer, true);
  35. }
  36. der.readSequence();
  37. var oid = der.readOID();
  38. switch (oid) {
  39. case '1.2.840.113549.1.1.1':
  40. if (type === 'public')
  41. return (readPkcs8RSAPublic(der));
  42. else
  43. return (readPkcs8RSAPrivate(der));
  44. case '1.2.840.10040.4.1':
  45. if (type === 'public')
  46. return (readPkcs8DSAPublic(der));
  47. else
  48. return (readPkcs8DSAPrivate(der));
  49. case '1.2.840.10045.2.1':
  50. if (type === 'public')
  51. return (readPkcs8ECDSAPublic(der));
  52. else
  53. return (readPkcs8ECDSAPrivate(der));
  54. default:
  55. throw (new Error('Unknown key type OID ' + oid));
  56. }
  57. }
  58. function readPkcs8RSAPublic(der) {
  59. // Null -- XXX this probably isn't good practice
  60. der.readByte();
  61. der.readByte();
  62. // bit string sequence
  63. der.readSequence(asn1.Ber.BitString);
  64. der.readByte();
  65. der.readSequence();
  66. // modulus
  67. var n = readMPInt(der, 'modulus');
  68. var e = readMPInt(der, 'exponent');
  69. // now, make the key
  70. var key = {
  71. type: 'rsa',
  72. source: der.originalInput,
  73. parts: [
  74. { name: 'e', data: e },
  75. { name: 'n', data: n }
  76. ]
  77. };
  78. return (new Key(key));
  79. }
  80. function readPkcs8RSAPrivate(der) {
  81. der.readByte();
  82. der.readByte();
  83. der.readSequence(asn1.Ber.OctetString);
  84. der.readSequence();
  85. var ver = readMPInt(der, 'version');
  86. assert.equal(ver[0], 0x0, 'unknown RSA private key version');
  87. // modulus then public exponent
  88. var n = readMPInt(der, 'modulus');
  89. var e = readMPInt(der, 'public exponent');
  90. var d = readMPInt(der, 'private exponent');
  91. var p = readMPInt(der, 'prime1');
  92. var q = readMPInt(der, 'prime2');
  93. var dmodp = readMPInt(der, 'exponent1');
  94. var dmodq = readMPInt(der, 'exponent2');
  95. var iqmp = readMPInt(der, 'iqmp');
  96. // now, make the key
  97. var key = {
  98. type: 'rsa',
  99. parts: [
  100. { name: 'n', data: n },
  101. { name: 'e', data: e },
  102. { name: 'd', data: d },
  103. { name: 'iqmp', data: iqmp },
  104. { name: 'p', data: p },
  105. { name: 'q', data: q },
  106. { name: 'dmodp', data: dmodp },
  107. { name: 'dmodq', data: dmodq }
  108. ]
  109. };
  110. return (new PrivateKey(key));
  111. }
  112. function readPkcs8DSAPublic(der) {
  113. der.readSequence();
  114. var p = readMPInt(der, 'p');
  115. var q = readMPInt(der, 'q');
  116. var g = readMPInt(der, 'g');
  117. // bit string sequence
  118. der.readSequence(asn1.Ber.BitString);
  119. der.readByte();
  120. var y = readMPInt(der, 'y');
  121. // now, make the key
  122. var key = {
  123. type: 'dsa',
  124. parts: [
  125. { name: 'p', data: p },
  126. { name: 'q', data: q },
  127. { name: 'g', data: g },
  128. { name: 'y', data: y }
  129. ]
  130. };
  131. return (new Key(key));
  132. }
  133. function readPkcs8DSAPrivate(der) {
  134. der.readSequence();
  135. var p = readMPInt(der, 'p');
  136. var q = readMPInt(der, 'q');
  137. var g = readMPInt(der, 'g');
  138. der.readSequence(asn1.Ber.OctetString);
  139. var x = readMPInt(der, 'x');
  140. /* The pkcs#8 format does not include the public key */
  141. var y = utils.calculateDSAPublic(g, p, x);
  142. var key = {
  143. type: 'dsa',
  144. parts: [
  145. { name: 'p', data: p },
  146. { name: 'q', data: q },
  147. { name: 'g', data: g },
  148. { name: 'y', data: y },
  149. { name: 'x', data: x }
  150. ]
  151. };
  152. return (new PrivateKey(key));
  153. }
  154. function readECDSACurve(der) {
  155. var curveName, curveNames;
  156. var j, c, cd;
  157. if (der.peek() === asn1.Ber.OID) {
  158. var oid = der.readOID();
  159. curveNames = Object.keys(algs.curves);
  160. for (j = 0; j < curveNames.length; ++j) {
  161. c = curveNames[j];
  162. cd = algs.curves[c];
  163. if (cd.pkcs8oid === oid) {
  164. curveName = c;
  165. break;
  166. }
  167. }
  168. } else {
  169. // ECParameters sequence
  170. der.readSequence();
  171. var version = der.readString(asn1.Ber.Integer, true);
  172. assert.strictEqual(version[0], 1, 'ECDSA key not version 1');
  173. var curve = {};
  174. // FieldID sequence
  175. der.readSequence();
  176. var fieldTypeOid = der.readOID();
  177. assert.strictEqual(fieldTypeOid, '1.2.840.10045.1.1',
  178. 'ECDSA key is not from a prime-field');
  179. var p = curve.p = utils.mpNormalize(
  180. der.readString(asn1.Ber.Integer, true));
  181. /*
  182. * p always starts with a 1 bit, so count the zeros to get its
  183. * real size.
  184. */
  185. curve.size = p.length * 8 - utils.countZeros(p);
  186. // Curve sequence
  187. der.readSequence();
  188. curve.a = utils.mpNormalize(
  189. der.readString(asn1.Ber.OctetString, true));
  190. curve.b = utils.mpNormalize(
  191. der.readString(asn1.Ber.OctetString, true));
  192. if (der.peek() === asn1.Ber.BitString)
  193. curve.s = der.readString(asn1.Ber.BitString, true);
  194. // Combined Gx and Gy
  195. curve.G = der.readString(asn1.Ber.OctetString, true);
  196. assert.strictEqual(curve.G[0], 0x4,
  197. 'uncompressed G is required');
  198. curve.n = utils.mpNormalize(
  199. der.readString(asn1.Ber.Integer, true));
  200. curve.h = utils.mpNormalize(
  201. der.readString(asn1.Ber.Integer, true));
  202. assert.strictEqual(curve.h[0], 0x1, 'a cofactor=1 curve is ' +
  203. 'required');
  204. curveNames = Object.keys(algs.curves);
  205. var ks = Object.keys(curve);
  206. for (j = 0; j < curveNames.length; ++j) {
  207. c = curveNames[j];
  208. cd = algs.curves[c];
  209. var equal = true;
  210. for (var i = 0; i < ks.length; ++i) {
  211. var k = ks[i];
  212. if (cd[k] === undefined)
  213. continue;
  214. if (typeof (cd[k]) === 'object' &&
  215. cd[k].equals !== undefined) {
  216. if (!cd[k].equals(curve[k])) {
  217. equal = false;
  218. break;
  219. }
  220. } else if (Buffer.isBuffer(cd[k])) {
  221. if (cd[k].toString('binary')
  222. !== curve[k].toString('binary')) {
  223. equal = false;
  224. break;
  225. }
  226. } else {
  227. if (cd[k] !== curve[k]) {
  228. equal = false;
  229. break;
  230. }
  231. }
  232. }
  233. if (equal) {
  234. curveName = c;
  235. break;
  236. }
  237. }
  238. }
  239. return (curveName);
  240. }
  241. function readPkcs8ECDSAPrivate(der) {
  242. var curveName = readECDSACurve(der);
  243. assert.string(curveName, 'a known elliptic curve');
  244. der.readSequence(asn1.Ber.OctetString);
  245. der.readSequence();
  246. var version = readMPInt(der, 'version');
  247. assert.equal(version[0], 1, 'unknown version of ECDSA key');
  248. var d = der.readString(asn1.Ber.OctetString, true);
  249. der.readSequence(0xa1);
  250. var Q = der.readString(asn1.Ber.BitString, true);
  251. Q = utils.ecNormalize(Q);
  252. var key = {
  253. type: 'ecdsa',
  254. parts: [
  255. { name: 'curve', data: new Buffer(curveName) },
  256. { name: 'Q', data: Q },
  257. { name: 'd', data: d }
  258. ]
  259. };
  260. return (new PrivateKey(key));
  261. }
  262. function readPkcs8ECDSAPublic(der) {
  263. var curveName = readECDSACurve(der);
  264. assert.string(curveName, 'a known elliptic curve');
  265. var Q = der.readString(asn1.Ber.BitString, true);
  266. Q = utils.ecNormalize(Q);
  267. var key = {
  268. type: 'ecdsa',
  269. parts: [
  270. { name: 'curve', data: new Buffer(curveName) },
  271. { name: 'Q', data: Q }
  272. ]
  273. };
  274. return (new Key(key));
  275. }
  276. function writePkcs8(der, key) {
  277. der.startSequence();
  278. if (PrivateKey.isPrivateKey(key)) {
  279. var sillyInt = new Buffer(1);
  280. sillyInt[0] = 0x0;
  281. der.writeBuffer(sillyInt, asn1.Ber.Integer);
  282. }
  283. der.startSequence();
  284. switch (key.type) {
  285. case 'rsa':
  286. der.writeOID('1.2.840.113549.1.1.1');
  287. if (PrivateKey.isPrivateKey(key))
  288. writePkcs8RSAPrivate(key, der);
  289. else
  290. writePkcs8RSAPublic(key, der);
  291. break;
  292. case 'dsa':
  293. der.writeOID('1.2.840.10040.4.1');
  294. if (PrivateKey.isPrivateKey(key))
  295. writePkcs8DSAPrivate(key, der);
  296. else
  297. writePkcs8DSAPublic(key, der);
  298. break;
  299. case 'ecdsa':
  300. der.writeOID('1.2.840.10045.2.1');
  301. if (PrivateKey.isPrivateKey(key))
  302. writePkcs8ECDSAPrivate(key, der);
  303. else
  304. writePkcs8ECDSAPublic(key, der);
  305. break;
  306. default:
  307. throw (new Error('Unsupported key type: ' + key.type));
  308. }
  309. der.endSequence();
  310. }
  311. function writePkcs8RSAPrivate(key, der) {
  312. der.writeNull();
  313. der.endSequence();
  314. der.startSequence(asn1.Ber.OctetString);
  315. der.startSequence();
  316. var version = new Buffer(1);
  317. version[0] = 0;
  318. der.writeBuffer(version, asn1.Ber.Integer);
  319. der.writeBuffer(key.part.n.data, asn1.Ber.Integer);
  320. der.writeBuffer(key.part.e.data, asn1.Ber.Integer);
  321. der.writeBuffer(key.part.d.data, asn1.Ber.Integer);
  322. der.writeBuffer(key.part.p.data, asn1.Ber.Integer);
  323. der.writeBuffer(key.part.q.data, asn1.Ber.Integer);
  324. if (!key.part.dmodp || !key.part.dmodq)
  325. utils.addRSAMissing(key);
  326. der.writeBuffer(key.part.dmodp.data, asn1.Ber.Integer);
  327. der.writeBuffer(key.part.dmodq.data, asn1.Ber.Integer);
  328. der.writeBuffer(key.part.iqmp.data, asn1.Ber.Integer);
  329. der.endSequence();
  330. der.endSequence();
  331. }
  332. function writePkcs8RSAPublic(key, der) {
  333. der.writeNull();
  334. der.endSequence();
  335. der.startSequence(asn1.Ber.BitString);
  336. der.writeByte(0x00);
  337. der.startSequence();
  338. der.writeBuffer(key.part.n.data, asn1.Ber.Integer);
  339. der.writeBuffer(key.part.e.data, asn1.Ber.Integer);
  340. der.endSequence();
  341. der.endSequence();
  342. }
  343. function writePkcs8DSAPrivate(key, der) {
  344. der.startSequence();
  345. der.writeBuffer(key.part.p.data, asn1.Ber.Integer);
  346. der.writeBuffer(key.part.q.data, asn1.Ber.Integer);
  347. der.writeBuffer(key.part.g.data, asn1.Ber.Integer);
  348. der.endSequence();
  349. der.endSequence();
  350. der.startSequence(asn1.Ber.OctetString);
  351. der.writeBuffer(key.part.x.data, asn1.Ber.Integer);
  352. der.endSequence();
  353. }
  354. function writePkcs8DSAPublic(key, der) {
  355. der.startSequence();
  356. der.writeBuffer(key.part.p.data, asn1.Ber.Integer);
  357. der.writeBuffer(key.part.q.data, asn1.Ber.Integer);
  358. der.writeBuffer(key.part.g.data, asn1.Ber.Integer);
  359. der.endSequence();
  360. der.endSequence();
  361. der.startSequence(asn1.Ber.BitString);
  362. der.writeByte(0x00);
  363. der.writeBuffer(key.part.y.data, asn1.Ber.Integer);
  364. der.endSequence();
  365. }
  366. function writeECDSACurve(key, der) {
  367. var curve = algs.curves[key.curve];
  368. if (curve.pkcs8oid) {
  369. /* This one has a name in pkcs#8, so just write the oid */
  370. der.writeOID(curve.pkcs8oid);
  371. } else {
  372. // ECParameters sequence
  373. der.startSequence();
  374. var version = new Buffer(1);
  375. version.writeUInt8(1, 0);
  376. der.writeBuffer(version, asn1.Ber.Integer);
  377. // FieldID sequence
  378. der.startSequence();
  379. der.writeOID('1.2.840.10045.1.1'); // prime-field
  380. der.writeBuffer(curve.p, asn1.Ber.Integer);
  381. der.endSequence();
  382. // Curve sequence
  383. der.startSequence();
  384. var a = curve.p;
  385. if (a[0] === 0x0)
  386. a = a.slice(1);
  387. der.writeBuffer(a, asn1.Ber.OctetString);
  388. der.writeBuffer(curve.b, asn1.Ber.OctetString);
  389. der.writeBuffer(curve.s, asn1.Ber.BitString);
  390. der.endSequence();
  391. der.writeBuffer(curve.G, asn1.Ber.OctetString);
  392. der.writeBuffer(curve.n, asn1.Ber.Integer);
  393. var h = curve.h;
  394. if (!h) {
  395. h = new Buffer(1);
  396. h[0] = 1;
  397. }
  398. der.writeBuffer(h, asn1.Ber.Integer);
  399. // ECParameters
  400. der.endSequence();
  401. }
  402. }
  403. function writePkcs8ECDSAPublic(key, der) {
  404. writeECDSACurve(key, der);
  405. der.endSequence();
  406. var Q = utils.ecNormalize(key.part.Q.data, true);
  407. der.writeBuffer(Q, asn1.Ber.BitString);
  408. }
  409. function writePkcs8ECDSAPrivate(key, der) {
  410. writeECDSACurve(key, der);
  411. der.endSequence();
  412. der.startSequence(asn1.Ber.OctetString);
  413. der.startSequence();
  414. var version = new Buffer(1);
  415. version[0] = 1;
  416. der.writeBuffer(version, asn1.Ber.Integer);
  417. der.writeBuffer(key.part.d.data, asn1.Ber.OctetString);
  418. der.startSequence(0xa1);
  419. var Q = utils.ecNormalize(key.part.Q.data, true);
  420. der.writeBuffer(Q, asn1.Ber.BitString);
  421. der.endSequence();
  422. der.endSequence();
  423. der.endSequence();
  424. }