Current Dev State

This commit is contained in:
Tim Lorsbach
2025-06-23 20:13:54 +02:00
parent b4f9bb277d
commit ded50edaa2
22617 changed files with 4345095 additions and 174 deletions

278
static/js/ketcher2/node_modules/pem/lib/convert.js generated vendored Normal file
View File

@ -0,0 +1,278 @@
'use strict'
var openssl = require('./openssl.js')
var helper = require('./helper.js')
// PEM format: .pem, .crt, .cer (!bin), .key
// base64 encoded; the cert file might also include the private key; so key file is optional
// DER format: .der, .cer (bin)
// binary encoded format; cannot include key file
// PKCS#7 / P7B format: .p7b, .p7c
// contains cert and ca chain cert files, but not the key file
// A PKCS7 certificate is serialized using either PEM or DER format.
// PKCS#12 / PFX format: .pfx, .p12
// contains all files: key file, cert and ca chain cert files
/**
* pem convert module
*
* @module convert
*/
/**
* conversion from PEM to DER format
* if private key is included in PEM encoded file, it won't be included in DER file
* use this method with type 'rsa' to export private key in that case
* @param {String} pathIN path of the PEM encoded certificate file
* @param {String} pathOUT path of the DER encoded certificate file to generate
* @param {String} [type] type of file, use 'rsa' for key file, 'x509' otherwise or leave this parameter out
* @param {Function} callback callback method called with error, boolean result
*/
module.exports.PEM2DER = function (pathIN, pathOUT, type, callback) {
if (!callback && typeof type === 'function') {
callback = type
type = 'x509'
}
var params = [
type,
'-outform',
'der',
'-in',
pathIN,
'-out',
pathOUT
]
openssl.spawnWrapper(params, false, function (error, code) {
if (error) {
callback(error)
} else {
callback(null, code === 0)
}
})
}
/**
* conversion from DER to PEM format
* @param {String} pathIN path of the DER encoded certificate file
* @param {String} pathOUT path of the PEM encoded certificate file to generate
* @param {String} [type] type of file, use 'rsa' for key file, 'x509' otherwise or leave this parameter out
* @param {Function} callback callback method called with error, boolean result
*/
module.exports.DER2PEM = function (pathIN, pathOUT, type, callback) {
if (!callback && typeof type === 'function') {
callback = type
type = 'x509'
}
var params = [
type,
'-inform',
'der',
'-in',
pathIN,
'-out',
pathOUT
]
openssl.spawnWrapper(params, false, function (error, code) {
if (error) {
callback(error)
} else {
callback(null, code === 0)
}
})
}
/**
* conversion from PEM to P7B format
* @param {Object} pathBundleIN paths of the PEM encoded certificate files ({cert: '...', ca: '...' or ['...', ...]})
* @param {String} pathOUT path of the P7B encoded certificate file to generate
* @param {Function} callback callback method called with error, boolean result
*/
module.exports.PEM2P7B = function (pathBundleIN, pathOUT, callback) {
var params = [
'crl2pkcs7',
'-nocrl',
'-certfile',
pathBundleIN.cert,
'-out',
pathOUT
]
if (pathBundleIN.ca) {
if (!Array.isArray(pathBundleIN.ca)) {
pathBundleIN.ca = [ pathBundleIN.ca ]
}
pathBundleIN.ca.forEach(function (ca) {
params.push('-certfile')
params.push(ca)
})
}
openssl.spawnWrapper(params, false, function (error, code) {
if (error) {
callback(error)
} else {
callback(null, code === 0)
}
})
}
/**
* conversion from P7B to PEM format
* @param {String} pathIN path of the P7B encoded certificate file
* @param {String} pathOUT path of the PEM encoded certificate file to generate
* @param {Function} callback callback method called with error, boolean result
*/
module.exports.P7B2PEM = function (pathIN, pathOUT, callback) {
var params = [
'pkcs7',
'-print_certs',
'-in',
pathIN,
'-out',
pathOUT
]
openssl.spawnWrapper(params, false, function (error, code) {
if (error) {
callback(error)
} else {
callback(null, code === 0)
}
})
}// TODO: CA also included?
/**
* conversion from PEM to PFX
* @param {Object} pathBundleIN paths of the PEM encoded certificate files ({cert: '...', key: '...', ca: '...' or ['...', ...]})
* @param {String} pathOUT path of the PFX encoded certificate file to generate
* @param {String} password password to set for accessing the PFX file
* @param {Function} callback callback method called with error, boolean result
*/
module.exports.PEM2PFX = function (pathBundleIN, pathOUT, password, callback) {
var params = [
'pkcs12',
'-export',
'-out',
pathOUT,
'-inkey',
pathBundleIN.key,
'-in',
pathBundleIN.cert
]
if (pathBundleIN.ca) {
if (!Array.isArray(pathBundleIN.ca)) {
pathBundleIN.ca = [ pathBundleIN.ca ]
}
pathBundleIN.ca.forEach(function (ca) {
params.push('-certfile')
params.push(ca)
})
}
var delTempPWFiles = []
helper.createPasswordFile({'cipher': '', 'password': password, 'passType': 'in'}, params, delTempPWFiles[delTempPWFiles.length])
helper.createPasswordFile({'cipher': '', 'password': password, 'passType': 'out'}, params, delTempPWFiles[delTempPWFiles.length])
openssl.spawnWrapper(params, false, function (error, code) {
function done (error) {
if (error) {
callback(error)
} else {
callback(null, code === 0)
}
}
helper.deleteTempFiles(delTempPWFiles, function (fsErr) {
done(error || fsErr)
})
})
}
/**
* conversion from PFX to PEM
* @param {Object} pathIN path of the PFX encoded certificate file
* @param {String} pathOUT path of the PEM encoded certificate file to generate
* @param {String} password password to set for accessing the PFX file
* @param {Function} callback callback method called with error, boolean result
*/
module.exports.PFX2PEM = function (pathIN, pathOUT, password, callback) {
var params = [
'pkcs12',
'-in',
pathIN,
'-out',
pathOUT,
'-nodes'
]
var delTempPWFiles = []
helper.createPasswordFile({'cipher': '', 'password': password, 'passType': 'in'}, params, delTempPWFiles[delTempPWFiles.length])
helper.createPasswordFile({'cipher': '', 'password': password, 'passType': 'out'}, params, delTempPWFiles[delTempPWFiles.length])
openssl.spawnWrapper(params, false, function (error, code) {
function done (error) {
if (error) {
callback(error)
} else {
callback(null, code === 0)
}
}
helper.deleteTempFiles(delTempPWFiles, function (fsErr) {
done(error || fsErr)
})
})
}
/**
* conversion from P7B to PFX/PKCS#12
* @param {Object} pathBundleIN paths of the PEM encoded certificate files ({cert: '...', key: '...', ca: '...' or ['...', ...]})
* @param {String} pathOUT path of the PFX certificate file to generate
* @param {String} password password to be set for the PFX file and to be used to access the key file
* @param {Function} callback callback method called with error, boolean result
*/
module.exports.P7B2PFX = function (pathBundleIN, pathOUT, password, callback) {
var tmpfile = pathBundleIN.cert.replace(/\.[^.]+$/, '.cer')
var params = [
'pkcs7',
'-print_certs',
'-in',
pathBundleIN.cert,
'-out',
tmpfile
]
openssl.spawnWrapper(params, false, function (error, code) {
if (error) {
callback(error)
} else {
var params = [
'pkcs12',
'-export',
'-in',
tmpfile,
'-inkey',
pathBundleIN.key,
'-out',
pathOUT
]
if (pathBundleIN.ca) {
if (!Array.isArray(pathBundleIN.ca)) {
pathBundleIN.ca = [ pathBundleIN.ca ]
}
pathBundleIN.ca.forEach(function (ca) {
params.push('-certfile')
params.push(ca)
})
}
var delTempPWFiles = [tmpfile]
helper.createPasswordFile({'cipher': '', 'password': password, 'passType': 'in'}, params, delTempPWFiles[delTempPWFiles.length])
helper.createPasswordFile({'cipher': '', 'password': password, 'passType': 'out'}, params, delTempPWFiles[delTempPWFiles.length])
openssl.spawnWrapper(params, false, function (error, code) {
function done (error) {
if (error) {
callback(error)
} else {
callback(null, code === 0)
}
}
helper.deleteTempFiles(delTempPWFiles, function (fsErr) {
done(error || fsErr)
})
})
}
})
}

137
static/js/ketcher2/node_modules/pem/lib/helper.js generated vendored Normal file
View File

@ -0,0 +1,137 @@
'use strict'
var pathlib = require('path')
var fs = require('fs')
var crypto = require('crypto')
var osTmpdir = require('os-tmpdir')
var tempDir = process.env.PEMJS_TMPDIR || osTmpdir()
/**
* pem helper module
*
* @module helper
*/
/**
* helper function to check is the string a number or not
* @param {String} str String that should be checked to be a number
*/
module.exports.isNumber = function (str) {
if (Array.isArray(str)) {
return false
}
/*
var bstr = str && str.toString()
str = str + ''
return bstr - parseFloat(bstr) + 1 >= 0 &&
!/^\s+|\s+$/g.test(str) && /^\d+$/g.test(str) &&
!isNaN(str) && !isNaN(parseFloat(str))
*/
return /^\d+$/g.test(str)
}
/**
* helper function to check is the string a hexaceximal value
* @param {String} hex String that should be checked to be a hexaceximal
*/
module.exports.isHex = function isHex (hex) {
return /^(0x){0,1}([0-9A-F]{1,40}|[0-9A-F]{1,40})$/gi.test(hex)
}
/**
* helper function to convert a string to a hexaceximal value
* @param {String} str String that should be converted to a hexaceximal
*/
module.exports.toHex = function toHex (str) {
var hex = ''
for (var i = 0; i < str.length; i++) {
hex += '' + str.charCodeAt(i).toString(16)
}
return hex
}
// cipherPassword returns an array of supported ciphers.
/**
* list of supported ciphers
* @type {Array}
*/
module.exports.ciphers = ['aes128', 'aes192', 'aes256', 'camellia128', 'camellia192', 'camellia256', 'des', 'des3', 'idea']
var ciphers = module.exports.ciphers
/**
* Creates a PasswordFile to hide the password form process infos via `ps auxf` etc.
* @param {Object} options object of cipher, password and passType, mustPass, {cipher:'aes128', password:'xxxx', passType:"in/out/word"}, if the object empty we do nothing
* @param {String} options.cipher cipher like 'aes128', 'aes192', 'aes256', 'camellia128', 'camellia192', 'camellia256', 'des', 'des3', 'idea'
* @param {String} options.password password can be empty or at last 4 to 1023 chars
* @param {String} options.passType passType: can be in/out/word for passIN/passOUT/passWORD
* @param {Boolean} options.mustPass mustPass is used when you need to set the pass like as "-password pass:" most needed when empty password
* @param {Object} params params will be extended with the data that need for the openssl command. IS USED AS POINTER!
* @param {String} PasswordFile PasswordFile is the filePath that later need to deleted, after the openssl command. IS USED AS POINTER!
* @return {Boolean} result
*/
module.exports.createPasswordFile = function (options, params, PasswordFile) {
if (!options || !options.hasOwnProperty('password') || !options.hasOwnProperty('passType') || !/^(word|in|out)$/.test(options.passType)) {
return false
}
PasswordFile = pathlib.join(tempDir, crypto.randomBytes(20).toString('hex'))
options.password = options.password.trim()
if (options.password === '') {
options.mustPass = true
}
if (options.cipher && (ciphers.indexOf(options.cipher) !== -1)) {
params.push('-' + options.cipher)
}
params.push('-pass' + options.passType)
if (options.mustPass) {
params.push('pass:' + options.password)
} else {
fs.writeFileSync(PasswordFile, options.password)
params.push('file:' + PasswordFile)
}
return true
}
/**
* Deletes a file or an array of files
* @param {Array} files array of files that shoudld be deleted
* @param {errorCallback} callback Callback function with an error object
*/
module.exports.deleteTempFiles = function (files, callback) {
var rmFiles = []
if (typeof files === 'string') {
rmFiles.push(files)
} else if (Array.isArray(files)) {
rmFiles = files
} else {
return callback(new Error('Unexcepted files parameter type; only string or array supported'))
}
var deleteSeries = function (list, finalCallback) {
if (list.length) {
var file = list.shift()
var myCallback = function (err) {
if (err && err.code === 'ENOENT') {
// file doens't exist
return deleteSeries(list, finalCallback)
} else if (err) {
// other errors, e.g. maybe we don't have enough permission
return finalCallback(err)
} else {
return deleteSeries(list, finalCallback)
}
}
if (file && typeof file === 'string') {
fs.unlink(file, myCallback)
} else {
return deleteSeries(list, finalCallback)
}
} else {
return finalCallback(null) // no errors
}
}
deleteSeries(rmFiles, callback)
}
/**
* Callback for return an error object.
* @callback errorCallback
* @param {Error} err - An Error Object or null
*/

257
static/js/ketcher2/node_modules/pem/lib/openssl.js generated vendored Normal file
View File

@ -0,0 +1,257 @@
var Buffer = require('safe-buffer').Buffer
var helper = require('./helper.js')
var cpspawn = require('child_process').spawn
var pathlib = require('path')
var fs = require('fs')
var osTmpdir = require('os-tmpdir')
var crypto = require('crypto')
var which = require('which')
var settings = {}
var tempDir = process.env.PEMJS_TMPDIR || osTmpdir()
/**
* pem openssl module
*
* @module openssl
*/
/**
* configue this openssl module
*
* @static
* @param {String} option name e.g. pathOpenSSL, openSslVersion; TODO rethink nomenclature
* @param {*} value value
*/
function set (option, value) {
settings[option] = value
}
/**
* get configuration setting value
*
* @static
* @param {String} option name
*/
function get (option) {
return settings[option] || null
}
/**
* Spawn an openssl command
*
* @static
* @param {Array} params Array of openssl command line parameters
* @param {String} searchStr String to use to find data
* @param {Array} [tmpfiles] list of temporary files
* @param {Function} callback Called with (error, stdout-substring)
*/
function exec (params, searchStr, tmpfiles, callback) {
if (!callback && typeof tmpfiles === 'function') {
callback = tmpfiles
tmpfiles = false
}
spawnWrapper(params, tmpfiles, function (err, code, stdout, stderr) {
var start, end
if (err) {
return callback(err)
}
if ((start = stdout.match(new RegExp('\\-+BEGIN ' + searchStr + '\\-+$', 'm')))) {
start = start.index
} else {
start = -1
}
// To get the full EC key with parameters and private key
if (searchStr === 'EC PARAMETERS') {
searchStr = 'EC PRIVATE KEY'
}
if ((end = stdout.match(new RegExp('^\\-+END ' + searchStr + '\\-+', 'm')))) {
end = end.index + end[0].length
} else {
end = -1
}
if (start >= 0 && end >= 0) {
return callback(null, stdout.substring(start, end))
} else {
return callback(new Error(searchStr + ' not found from openssl output:\n---stdout---\n' + stdout + '\n---stderr---\n' + stderr + '\ncode: ' + code))
}
})
}
/**
* Spawn an openssl command and get binary output
*
* @static
* @param {Array} params Array of openssl command line parameters
* @param {Array} [tmpfiles] list of temporary files
* @param {Function} callback Called with (error, stdout)
*/
function execBinary (params, tmpfiles, callback) {
if (!callback && typeof tmpfiles === 'function') {
callback = tmpfiles
tmpfiles = false
}
spawnWrapper(params, tmpfiles, true, function (err, code, stdout, stderr) {
if (err) {
return callback(err)
}
return callback(null, stdout)
})
}
/**
* Generically spawn openSSL, without processing the result
*
* @static
* @param {Array} params The parameters to pass to openssl
* @param {Boolean} binary Output of openssl is binary or text
* @param {Function} callback Called with (error, exitCode, stdout, stderr)
*/
function spawn (params, binary, callback) {
var pathBin = get('pathOpenSSL') || process.env.OPENSSL_BIN || 'openssl'
testOpenSSLPath(pathBin, function (err) {
if (err) {
return callback(err)
}
var openssl = cpspawn(pathBin, params)
var stderr = ''
var stdout = (binary ? new Buffer(0) : '')
openssl.stdout.on('data', function (data) {
if (!binary) {
stdout += data.toString('binary')
} else {
stdout = Buffer.concat([stdout, data])
}
})
openssl.stderr.on('data', function (data) {
stderr += data.toString('binary')
})
// We need both the return code and access to all of stdout. Stdout isn't
// *really* available until the close event fires; the timing nuance was
// making this fail periodically.
var needed = 2 // wait for both exit and close.
var code = -1
var finished = false
var done = function (err) {
if (finished) {
return
}
if (err) {
finished = true
return callback(err)
}
if (--needed < 1) {
finished = true
if (code) {
if (code === 2 && (stderr === '' || /depth lookup: unable to/.test(stderr))) {
return callback(null, code, stdout, stderr)
}
return callback(new Error('Invalid openssl exit code: ' + code + '\n% openssl ' + params.join(' ') + '\n' + stderr), code)
} else {
return callback(null, code, stdout, stderr)
}
}
}
openssl.on('error', done)
openssl.on('exit', function (ret) {
code = ret
done()
})
openssl.on('close', function () {
stdout = (binary ? stdout : new Buffer(stdout, 'binary').toString('utf-8'))
stderr = new Buffer(stderr, 'binary').toString('utf-8')
done()
})
})
}
/**
* Wrapper for spawn method
*
* @static
* @param {Array} params The parameters to pass to openssl
* @param {Array} [tmpfiles] list of temporary files
* @param {Boolean} [binary] Output of openssl is binary or text
* @param {Function} callback Called with (error, exitCode, stdout, stderr)
*/
function spawnWrapper (params, tmpfiles, binary, callback) {
if (!callback && typeof binary === 'function') {
callback = binary
binary = false
}
var files = []
var delTempPWFiles = []
if (tmpfiles) {
tmpfiles = [].concat(tmpfiles)
params.forEach(function (value, i) {
var fpath
if (value === '--TMPFILE--') {
fpath = pathlib.join(tempDir, crypto.randomBytes(20).toString('hex'))
files.push({
path: fpath,
contents: tmpfiles.shift()
})
params[i] = fpath
delTempPWFiles[delTempPWFiles.length] = fpath
}
})
}
// TODO: need to refactored
files.forEach(function (file) {
fs.writeFileSync(file.path, file.contents)
})
spawn(params, binary, function (err, code, stdout, stderr) {
helper.deleteTempFiles(delTempPWFiles, function (fsErr) {
callback(err || fsErr, code, stdout, stderr)
})
})
}
/**
* Validates the pathBin for the openssl command
*
* @private
* @param {String} pathBin The path to OpenSSL Bin
* @param {Function} callback Callback function with an error object
*/
function testOpenSSLPath (pathBin, callback) {
which(pathBin, function (error) {
if (error) {
return callback(new Error('Could not find openssl on your system on this path: ' + pathBin))
}
callback()
})
}
/* Once PEM is imported, the openSslVersion is set with this function. */
spawn(['version'], false, function (err, code, stdout, stderr) {
var text = String(stdout) + '\n' + String(stderr) + '\n' + String(err)
var tmp = text.match(/^LibreSSL/i)
set('openSslVersion', (tmp && tmp[0] ? 'LibreSSL' : 'openssl').toUpperCase())
})
module.exports = {
exec: exec,
execBinary: execBinary,
spawn: spawn,
spawnWrapper: spawnWrapper,
set: set,
get: get
}

1206
static/js/ketcher2/node_modules/pem/lib/pem.js generated vendored Normal file

File diff suppressed because it is too large Load Diff