pkcs1.js 8.8 KB

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