"use strict"; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"); var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.toJwkFrom = exports.fromJwkTo = void 0; var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof")); var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); var asn1enc = _interopRequireWildcard(require("./asn1enc.js")); var octenc = _interopRequireWildcard(require("./octenc.js")); /** * converter.js */ // ASN.1 in RFC5280 (SPKI) and RFC5208 (PKCS8) -> RSA and EC, encode='asn', format='pem' or 'der' // -> SPKI (in X.509): RFC5280 for public key, PKCS8: RFC5208 for private key // Octet Form in ANSI X9.63 -> EC, encode='oct', format='string' or 'binary', compact=true or false // -> Standards for Efficient Cryptography Group (SECG), "SEC1: Elliptic Curve Cryptography", Version 1.0, September 2000. /** * Convert JWK to ASN.1 (for RSA and EC) and Octet (for EC) encoded keys. * @param {String} output - 'pem', 'der', or 'oct' (only EC JWK), output format. * @param {JsonWebKey} jwkey - A JWK to be encoded. * @param {KeyExportOptions} options - Options to export key including encryption options. * For EC JWK : options.compact = true or false * For EC JWK with output = 'oct' : options.format = 'binary' or 'string' * For both: outputPublic (optional) : boolean. derived key type. from private key, public key can be derived when true. * @return {PEM|DER|OctetEC} - Output key object. */ var fromJwkTo = /*#__PURE__*/ function () { var _ref = (0, _asyncToGenerator2.default)( /*#__PURE__*/ _regenerator.default.mark(function _callee() { var output, jwkey, options, _args = arguments; return _regenerator.default.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: output = _args.length > 0 && _args[0] !== undefined ? _args[0] : 'pem'; jwkey = _args.length > 1 ? _args[1] : undefined; options = _args.length > 2 && _args[2] !== undefined ? _args[2] : {}; if (!(['pem', 'der', 'oct'].indexOf(output) < 0)) { _context.next = 5; break; } throw new Error('InvalidOutputForm'); case 5: if (!((0, _typeof2.default)(jwkey) !== 'object')) { _context.next = 7; break; } throw new Error('InvalidJWKAsObject'); case 7: if (!(jwkey.kty !== 'EC' && jwkey.kty !== 'RSA')) { _context.next = 9; break; } throw new Error('UnsupportedKeyType'); case 9: if (!(typeof options.outputPublic !== 'undefined' && typeof options.outputPublic !== 'boolean')) { _context.next = 11; break; } throw new Error('outputPublicMustBeBoolean'); case 11: // default values if (jwkey.key === 'EC' && typeof options.compact !== 'boolean') options.compact = false; if (output === 'oct' && options.output !== 'string') options.output = 'binary'; if (typeof options.encryptParams === 'undefined') options.encryptParams = {}; if ((output === 'der' || output === 'pem') && typeof options.encryptParams.passphrase === 'undefined') options.encryptParams.passphrase = ''; // In the case of PEM/DER if (!(output === 'der' || output === 'pem')) { _context.next = 21; break; } _context.next = 18; return asn1enc.fromJwk(jwkey, output, { outputPublic: options.outputPublic, compact: options.compact, encOptions: options.encryptParams }); case 18: return _context.abrupt("return", _context.sent); case 21: if (!(output === 'oct' && jwkey.kty === 'EC')) { _context.next = 25; break; } return _context.abrupt("return", octenc.fromJwk(jwkey, { outputPublic: options.outputPublic, outputFormat: options.output, compact: options.compact })); case 25: throw new Error('UnsupportedConversion'); case 26: case "end": return _context.stop(); } } }, _callee); })); return function fromJwkTo() { return _ref.apply(this, arguments); }; }(); /** * Convert ASN.1 encoded (for RSA and EC) or octet formed (for EC) keys to JWK. * @param {String} input - 'pem', 'der' or 'oct', input key format. * @param {PEM|DER|OctetEC} key - A key object to be encoded. * @param {JwkExportOptionsInternal} [options={}] - options to export JWK keys. * @return {JsonWebKey} - Obtained key object in JWK format. * @throws {Error} - Throws if InvalidInputForm, InappropriateOptions, outputPublicMustBeBoolean or UnsupportedConversion */ exports.fromJwkTo = fromJwkTo; var toJwkFrom = /*#__PURE__*/ function () { var _ref2 = (0, _asyncToGenerator2.default)( /*#__PURE__*/ _regenerator.default.mark(function _callee2(input, key) { var options, _args2 = arguments; return _regenerator.default.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: options = _args2.length > 2 && _args2[2] !== undefined ? _args2[2] : {}; if (!(['pem', 'der', 'oct'].indexOf(input) < 0)) { _context2.next = 3; break; } throw new Error('InvalidInputForm'); case 3: if (!(input === 'oct' && !options.namedCurve)) { _context2.next = 5; break; } throw new Error('InappropriateOptions'); case 5: if (!(typeof options.outputPublic !== 'undefined' && typeof options.outputPublic !== 'boolean')) { _context2.next = 7; break; } throw new Error('outputPublicMustBeBoolean'); case 7: // default values if ((input === 'der' || input === 'pem') && typeof options.passphrase === 'undefined') options.passphrase = ''; // In the case of PEM if (!(input === 'der' || input === 'pem')) { _context2.next = 14; break; } _context2.next = 11; return asn1enc.toJwk(key, input, { outputPublic: options.outputPublic, passphrase: options.passphrase }); case 11: return _context2.abrupt("return", _context2.sent); case 14: if (!(input === 'oct')) { _context2.next = 18; break; } return _context2.abrupt("return", octenc.toJwk(key, options.namedCurve, { outputPublic: options.outputPublic })); case 18: throw new Error('UnsupportedConversion'); case 19: case "end": return _context2.stop(); } } }, _callee2); })); return function toJwkFrom(_x, _x2) { return _ref2.apply(this, arguments); }; }(); exports.toJwkFrom = toJwkFrom;