UNPKG

node-opcua-crypto

Version:
290 lines (242 loc) 11.6 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var _chunkFRVYTI7Scjs = require('./chunk-FRVYTI7S.cjs'); // source_nodejs/generate_private_key_filename.ts var _crypto = require('crypto'); var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs); async function generatePrivateKeyFile(privateKeyFilename, modulusLength) { const keys = await _chunkFRVYTI7Scjs.generateKeyPair.call(void 0, modulusLength); const privateKeyPem = await _chunkFRVYTI7Scjs.privateKeyToPEM.call(void 0, keys.privateKey); await _fs2.default.promises.writeFile(privateKeyFilename, privateKeyPem.privPem, "utf-8"); privateKeyPem.privPem = ""; privateKeyPem.privDer = new ArrayBuffer(0); } async function generatePrivateKeyFileAlternate(privateKeyFilename, modulusLength) { const { privateKey } = _crypto.generateKeyPairSync.call(void 0, "rsa", { modulusLength, privateKeyEncoding: { type: "pkcs8", format: "pem" }, publicKeyEncoding: { type: "spki", format: "pem" } }); await _fs2.default.promises.writeFile(privateKeyFilename, privateKey, "utf-8"); } // source_nodejs/read.ts var _assert = require('assert'); var _assert2 = _interopRequireDefault(_assert); var _path = require('path'); var _path2 = _interopRequireDefault(_path); var _sshpk = require('sshpk'); var _sshpk2 = _interopRequireDefault(_sshpk); function _readPemFile(filename) { _assert2.default.call(void 0, typeof filename === "string"); return _chunkFRVYTI7Scjs.removeTrailingLF.call(void 0, _fs2.default.readFileSync(filename, "utf-8")); } function _countPemCertBlocks(pem) { const matches = pem.match(/-----BEGIN CERTIFICATE-----/g); return matches ? matches.length : 0; } function readCertificate(filename) { if (filename.match(/.*\.der/)) { return _fs2.default.readFileSync(filename); } const pem = _readPemFile(filename); const count = _countPemCertBlocks(pem); if (count > 1) { console.warn( `[node-opcua-crypto] readCertificate: "${_path2.default.basename(filename)}" contains ${count} PEM certificate block(s) but only the first will be used. Use readCertificateChain() to read all certificates.` ); } return _chunkFRVYTI7Scjs.convertPEMtoDER.call(void 0, pem); } function readCertificateChain(filename) { if (filename.match(/.*\.der/)) { return _chunkFRVYTI7Scjs.split_der.call(void 0, _fs2.default.readFileSync(filename)); } const pem = _readPemFile(filename); return _extractAllPemDerCertificates(pem); } async function readCertificateChainAsync(filename) { const buf = await _fs2.default.promises.readFile(filename); if (filename.match(/.*\.der/)) { return _chunkFRVYTI7Scjs.split_der.call(void 0, buf); } const pem = _chunkFRVYTI7Scjs.removeTrailingLF.call(void 0, buf.toString("utf-8")); return _extractAllPemDerCertificates(pem); } function _extractAllPemDerCertificates(pem) { const certs = []; const regex = /-----BEGIN CERTIFICATE-----\r?\n([/+=a-zA-Z0-9\r\n]*)\r?\n-----END CERTIFICATE-----/g; let match; match = regex.exec(pem); while (match !== null) { const base64 = match[1].replace(/\r?\n/g, ""); const derBuffer = Buffer.from(base64, "base64"); try { const separatedCerts = _chunkFRVYTI7Scjs.split_der.call(void 0, derBuffer); for (const c of separatedCerts) { certs.push(c); } } catch (_err) { certs.push(derBuffer); } match = regex.exec(pem); } return certs; } async function readCertificateAsync(filename) { const buf = await _fs2.default.promises.readFile(filename); if (filename.match(/.*\.der/)) { return buf; } const raw_key = _chunkFRVYTI7Scjs.removeTrailingLF.call(void 0, buf.toString("utf-8")); const count = _countPemCertBlocks(raw_key); if (count > 1) { console.warn( `[node-opcua-crypto] readCertificateAsync: "${_path2.default.basename(filename)}" contains ${count} PEM certificate block(s) but only the first will be used. Use readCertificateChainAsync() to read all certificates.` ); } return _chunkFRVYTI7Scjs.convertPEMtoDER.call(void 0, raw_key); } function readPublicKey(filename) { if (filename.match(/.*\.der/)) { const der = _fs2.default.readFileSync(filename); return _crypto.createPublicKey.call(void 0, der); } else { const raw_key = _readPemFile(filename); return _crypto.createPublicKey.call(void 0, raw_key); } } async function readPublicKeyAsync(filename) { const buf = await _fs2.default.promises.readFile(filename); if (filename.match(/.*\.der/)) { return _crypto.createPublicKey.call(void 0, buf); } return _crypto.createPublicKey.call(void 0, _chunkFRVYTI7Scjs.removeTrailingLF.call(void 0, buf.toString("utf-8"))); } function myCreatePrivateKey(rawKey) { if (!_crypto.createPrivateKey || process.env.NO_CREATE_PRIVATEKEY) { if (Buffer.isBuffer(rawKey)) { const pemKey = _chunkFRVYTI7Scjs.toPem.call(void 0, rawKey, "PRIVATE KEY"); _assert2.default.call(void 0, ["RSA PRIVATE KEY", "PRIVATE KEY"].indexOf(_chunkFRVYTI7Scjs.identifyPemType.call(void 0, pemKey)) >= 0); return { hidden: pemKey }; } return { hidden: ensureTrailingLF(rawKey) }; } const backup = process.env.OPENSSL_CONF; process.env.OPENSSL_CONF = "/dev/null"; const retValue = _crypto.createPrivateKey.call(void 0, rawKey); process.env.OPENSSL_CONF = backup; return { hidden: retValue }; } function ensureTrailingLF(str) { return str.match(/\n$/) ? str : `${str} `; } function readPrivateKey(filename) { if (filename.match(/.*\.der/)) { const der = _fs2.default.readFileSync(filename); return myCreatePrivateKey(der); } else { const raw_key = _readPemFile(filename); return myCreatePrivateKey(raw_key); } } async function readPrivateKeyAsync(filename) { const buf = await _fs2.default.promises.readFile(filename); if (filename.match(/.*\.der/)) { return myCreatePrivateKey(buf); } return myCreatePrivateKey(_chunkFRVYTI7Scjs.removeTrailingLF.call(void 0, buf.toString("utf-8"))); } function readCertificatePEM(filename) { return _readPemFile(filename); } async function readCertificatePEMAsync(filename) { const buf = await _fs2.default.promises.readFile(filename, "utf-8"); return _chunkFRVYTI7Scjs.removeTrailingLF.call(void 0, buf); } function readPublicKeyPEM(filename) { return _readPemFile(filename); } async function readPublicKeyPEMAsync(filename) { const buf = await _fs2.default.promises.readFile(filename, "utf-8"); return _chunkFRVYTI7Scjs.removeTrailingLF.call(void 0, buf); } function readPrivateKeyPEM(filename) { return _readPemFile(filename); } async function readPrivateKeyPEMAsync(filename) { const buf = await _fs2.default.promises.readFile(filename, "utf-8"); return _chunkFRVYTI7Scjs.removeTrailingLF.call(void 0, buf); } var _g_certificate_store = ""; function setCertificateStore(store) { const old_store = _g_certificate_store; _g_certificate_store = store; return old_store; } function getCertificateStore() { if (!_g_certificate_store) { _g_certificate_store = _path2.default.join(__dirname, "../../certificates/"); } return _g_certificate_store; } function readPrivateRsaKey(filename) { if (!_crypto.createPrivateKey) { throw new Error("createPrivateKey is not supported in this environment"); } if (filename.substring(0, 1) !== "." && !_fs2.default.existsSync(filename)) { filename = _path2.default.join(getCertificateStore(), filename); } const content = _fs2.default.readFileSync(filename, "utf8"); const sshKey = _sshpk2.default.parsePrivateKey(content, "auto"); const key = sshKey.toString("pkcs1"); const hidden = _crypto.createPrivateKey.call(void 0, { format: "pem", type: "pkcs1", key }); return { hidden }; } function readPublicRsaKey(filename) { if (filename.substring(0, 1) !== "." && !_fs2.default.existsSync(filename)) { filename = _path2.default.join(getCertificateStore(), filename); } const content = _fs2.default.readFileSync(filename, "utf-8"); const sshKey = _sshpk2.default.parseKey(content, "ssh"); const key = sshKey.toString("pkcs1"); return _crypto.createPublicKey.call(void 0, { format: "pem", type: "pkcs1", key }); } // source_nodejs/read_certificate_revocation_list.ts async function readCertificateRevocationList(filename) { const crl = await _fs2.default.promises.readFile(filename); if (crl[0] === 48 && crl[1] === 130) { return crl; } const raw_crl = crl.toString(); return _chunkFRVYTI7Scjs.convertPEMtoDER.call(void 0, raw_crl); } // source_nodejs/read_certificate_signing_request.ts async function readCertificateSigningRequest(filename) { const csr = await _fs2.default.promises.readFile(filename); if (csr[0] === 48 && csr[1] === 130) { return csr; } const raw_crl = csr.toString(); return _chunkFRVYTI7Scjs.convertPEMtoDER.call(void 0, raw_crl); } // source_nodejs/write.ts function certificatesToPem(certificates) { const certs = Array.isArray(certificates) ? certificates : [certificates]; return `${certs.map((der) => _chunkFRVYTI7Scjs.toPem.call(void 0, der, "CERTIFICATE")).join("\n")} `; } function writeCertificateChain(filename, certificates) { _fs2.default.writeFileSync(filename, certificatesToPem(certificates), "utf-8"); } async function writeCertificateChainAsync(filename, certificates) { await _fs2.default.promises.writeFile(filename, certificatesToPem(certificates), "utf-8"); } function certificatesToDer(certificates) { const certs = Array.isArray(certificates) ? certificates : [certificates]; return _chunkFRVYTI7Scjs.combine_der.call(void 0, certs); } function writeCertificateChainDer(filename, certificates) { _fs2.default.writeFileSync(filename, certificatesToDer(certificates)); } async function writeCertificateChainDerAsync(filename, certificates) { await _fs2.default.promises.writeFile(filename, certificatesToDer(certificates)); } exports.generatePrivateKeyFile = generatePrivateKeyFile; exports.generatePrivateKeyFileAlternate = generatePrivateKeyFileAlternate; exports.readCertificate = readCertificate; exports.readCertificateChain = readCertificateChain; exports.readCertificateChainAsync = readCertificateChainAsync; exports.readCertificateAsync = readCertificateAsync; exports.readPublicKey = readPublicKey; exports.readPublicKeyAsync = readPublicKeyAsync; exports.readPrivateKey = readPrivateKey; exports.readPrivateKeyAsync = readPrivateKeyAsync; exports.readCertificatePEM = readCertificatePEM; exports.readCertificatePEMAsync = readCertificatePEMAsync; exports.readPublicKeyPEM = readPublicKeyPEM; exports.readPublicKeyPEMAsync = readPublicKeyPEMAsync; exports.readPrivateKeyPEM = readPrivateKeyPEM; exports.readPrivateKeyPEMAsync = readPrivateKeyPEMAsync; exports.setCertificateStore = setCertificateStore; exports.getCertificateStore = getCertificateStore; exports.readPrivateRsaKey = readPrivateRsaKey; exports.readPublicRsaKey = readPublicRsaKey; exports.readCertificateRevocationList = readCertificateRevocationList; exports.readCertificateSigningRequest = readCertificateSigningRequest; exports.certificatesToPem = certificatesToPem; exports.writeCertificateChain = writeCertificateChain; exports.writeCertificateChainAsync = writeCertificateChainAsync; exports.certificatesToDer = certificatesToDer; exports.writeCertificateChainDer = writeCertificateChainDer; exports.writeCertificateChainDerAsync = writeCertificateChainDerAsync; //# sourceMappingURL=chunk-2DISC7JP.cjs.map