Git Product home page Git Product logo

codevibess / openssl-nodejs Goto Github PK

View Code? Open in Web Editor NEW
52.0 2.0 27.0 43 KB

is a package which gives you a possibility to run every OpenSSL command πŸ”’ in Node.js in a handy way. Moreover, parameters like -in, -keyin, -config and etc can be replaced by a raw data (Buffor).

Home Page: https://www.npmjs.com/package/openssl-nodejs

License: MIT License

JavaScript 100.00%
openssl nodejs certificate x509 rsa rsa-key-pair public-key-cryptography private-key-encryption ssl-certificate tls-certificate

openssl-nodejs's Introduction

openssl-nodejs

is a package which gives you a possibility to run every OpenSSL command in Node.js in a handy way. Moreover, parameters like -in, -keyin, -config and etc can be replaced by a raw data (Buffor).

Installation & Usage

npm install openssl-nodejs

Import openssl module:

const openssl = require('openssl-nodejs')

Next, invoke openssl function and put command with parameters inside a function like presented in the example below.

openssl('openssl req -config csr.cnf -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout key.key -out certificate.crt')

To get access to the result of execution specify callback function as the last parameter of openssl function (with arguments err and buffer).

openssl function can be invoked with a single parameter like OpenSSL command (see example above) or within an array with command name and parameters itself.

openssl(['req', '-config', 'csr.conf', '-out', 'CSR.csr', '-new', '-newkey', 'rsa:2048', '-nodes', '-keyout', 'privateKey.key', function (err, buffer) {
console.log(err.toString(), buffer.toString());
});

If you want to specify Buffer text instead of the file as an input/output or whatever you need, use the version with an array as a function parameter. And put an object with keys: name: (specify a name of file which will be created to handle this command), and buffer: (your buffer variable) Example of object:

{ name:'csr.conf', buffer: BufferVariable }

Command example:

openssl(['req', '-config', { name:'csr.conf', buffer: BufferVariable }, '-out', 'CSR.csr', '-new', '-newkey', 'rsa:2048', '-nodes', '-keyout', 'privateKey.key'], function (err, buffer) {
console.log(err.toString(), buffer.toString());
});

When you used a command which generates additional output in file format this package will create a folder openssl/ in the directory where the command was invoked. All output files will appear in this folder (openssl).

Note:

If u want to use a command which needs additional interaction use parameter -config and specify pass to file with configuration.


That's all that you need to start using it.

For any information, improvements or bug fixes please contact me. If it's package was useful for you please give a star in GitHub. (really inspiring me to new ones)

openssl-nodejs's People

Contributors

codevibess avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

openssl-nodejs's Issues

Error: spawn openssl ENOENT

The module do not work on my end, not sure what does it mean but i get this error.

events.js:174
throw er; // Unhandled 'error' event
^

Error: spawn openssl ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:240:19)
    at onErrorNT (internal/child_process.js:415:16)
    at process._tickCallback (internal/process/next_tick.js:63:19)
    at Function.Module.runMain (internal/modules/cjs/loader.js:757:11)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
Emitted 'error' event at:
    at Process.ChildProcess._handle.onexit (internal/child_process.js:246:12)
    at onErrorNT (internal/child_process.js:415:16)
    [... lines matching original stack trace ...]
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

Error: spawn openssl ENOENT

I followed the directions in the readme for setting up.

  1. const openssl = require('openssl-nodejs');
openssl('openssl s_client -showcerts -verify 5 -connect RemovedMyDomainOnPurpose.com:443 < /dev/null', (data) => {
        console.log(data);
    });

This returns the following error,

Error: spawn openssl ENOENT
at Process.ChildProcess._handle.onexit (node:internal/child_process:283:19)
at onErrorNT (node:internal/child_process:478:16)
at processTicksAndRejections (node:internal/process/task_queues:83:21)

The certs are there and they work, I just want to get them.

Here is my OpenSSL version:
image

EDIT:
I also tried installing the below version of OpenSSL and received the same problem.
OpenSSL 1.1.1t 7 Feb 2023

"-in" used in a parameter throws an error

I tried to use the -subj parameter and used "test-intern" as Common Name.
The module interprets the "-in" as new parameter and not as string.

Example throwing an error:

global.openssl(['req', '-config', {name: "csr.conf", buffer: csrConfigBuffer}, '-new', '-key', {name: "private.pem", buffer: privateKeyBuffer}, "-subj", "/C=Test/ST=Test/L=Test/O=Test/OU=Test/CN=test-intern"], function(errorOutput, bufferOuptut) {

Resolved by changing:

const checkCommandForIO = element => element.includes('-in') || element.includes('-out')
    || element.includes('-keyout') || element.includes('-signkey') || element.includes('-key')

to:

const checkCommandForIO = element => element == '-in' || element == '-out'
    || element == '-keyout' || element == '-signkey' || element == '-key'  || element == '-config'

Cannot define output directory

I'd like to be able to define the output directory to facilitate more predictable cleanup and keep my folder tree a bit tidier.

running lib in FaaS

If you will try to run lib inside Firebase functions, or other function as a service platform it will fail.
It happens because usually you can't write to app folder, only to tmp dirrectory.
Correct approach presented here: #7

Bad output format for CMS file

I am running this command:
openssl cms -sign -nodetach -binary -in receipt.json -text -outform der -out receipt.p7b -nocerts -signer MyCRT.crt -inkey MyKey.key

But got error: Bad output format for CMS file
However running this command manualy in command line works perfectly well.

Please help me with this issue! Thanks in advance!

CanΒ΄t convert DER Certificate to PEM

I try to convert a DER formatted certificate to PEM format. i used the command for that from sslshopper.
locally over the Console it worked, but when i try it in nodejs i get the following callback:
OpenSSL process ends with code 1
x509: Invalid format "openssl/der" for -inform
x509: Use -help for summary.

At first i thought its just a problem with the in cert, so i tried to give the command the local Dir of the cert. i also tried to give it the cert as object.

Am i too stupid or is it a real issue?

OpenSSL process ends with code 1

I tried to run the following openssl command:
openssl pkcs12 -in .p12 -clcerts -nokeys -out signerCert.pem -passin pass:

in Nodejs looks like this:
openssl(['pkcs12', '-config', { name:'cer.p12', buffer: buffervar }, '-clcerts', '-nokeys', '-out', 'Cer.pem', '-passin', 'pass:password'], function (err, buffer) {
console.log(err.toString(), buffer.toString());
});

I get always the errror
OpenSSL process ends with code 1
[πŸ’»] Usage: pkcs12 [options]
[πŸ’»] where options are
[πŸ’»] -export output PKCS12 file
[πŸ’»] -chain add certificate chain
[πŸ’»] -inkey file private key if not infile
[πŸ’»] -certfile f add all certs in f....

If I do it without password, then I get the same error. Where is my issue?
At the terminal it runs.
Thanks in advance.

fs.<method>Sync methods should be replaced

The implementation here using fs.writeFileSync etc block the event loop & impact performance (any amount of parallelism with say, serving requests) would cause a significant issue.

Issue in creating cert from a csr file

openssl('openssl ca -config intermediate/openssl.cnf -extensions usr_cert -days 375 -notext -md sha256 -in intermediate/csr/Pakaya.csr.pem -out intermediate/certs/Pakaya.cert.pem', function (err, buffer) {
console.log(err.toString(), buffer.toString());
});

When I run it, it asks the passphrase and then it does not complete the process unless I press CTRL+C. It only creates a zero byte file in the specified location.
When I run the terminal ideally it should ask for the passphrase and then confirmation to sign the certificate.

Set password requeired by openssl script

I tried to run the following script

openssl(openssl genrsa -des3 -out private.pem 2048), which requires a passphrase, but I can't send it. Can you provide an array of inputs parameter for cases like this?

TypeError: element.includes is not a function

No matter what I do, I am facing this issue while using this library.

Example of coding:

 openssl(['x509', '-noout', '-text', '-in', ca.pem, function (err, buffer) {
    console.log(err.toString(), buffer.toString());
  }]);

or

openssl(['x509', '-noout', '-text', '-in', { name:'ca.pem', buffer: BufferVariable }], (output) => console.log(output.toString()));

Error:

{
    "errorType": "Runtime.UnhandledPromiseRejection",
    "errorMessage": "TypeError: element.includes is not a function",
    "reason": {
        "errorType": "TypeError",
        "errorMessage": "element.includes is not a function",
        "stack": [
            "TypeError: element.includes is not a function",
            "    at checkCommandForIO (/var/task/node_modules/@amzn/interceptor-core/node_modules/openssl-nodejs/src/index.js:17:46)",
            "    at openssl (/var/task/node_modules/@amzn/interceptor-core/node_modules/openssl-nodejs/src/index.js:69:13)",
            "    at getKeyFromURL (/var/task/node_modules/@amzn/interceptor-core/src/token-validator.js:53:3)",
            "    at processTicksAndRejections (internal/process/task_queues.js:97:5)"
        ]
    },
    "promise": {},
    "stack": [
        "Runtime.UnhandledPromiseRejection: TypeError: element.includes is not a function",
        "    at process.<anonymous> (/var/runtime/index.js:35:15)",
        "    at process.emit (events.js:314:20)",
        "    at processPromiseRejections (internal/process/promises.js:209:33)",
        "    at processTicksAndRejections (internal/process/task_queues.js:98:32)"
    ]
}

Error of dashes in path

Hello, I get an error if I try to create a certificate in a path containing dashes (and I presume spaces too).

Is there a way to fix this or do I have to work only without spaces and dashes? Thank you 8)

Need for more documentation

I was unable to find documentation on how to actually encrypt and decrypt data. I'm sorry if I missed it.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.