PeculiarVentures / PKI.js

PKI.js is a pure JavaScript library implementing the formats that are used in PKI applications (signing, encryption, certificate requests, OCSP and TSP requests/responses). It is built on WebCrypto (Web Cryptography API) and requires no plug-ins.
http://pkijs.org
Other
1.3k stars 205 forks source link

SMIME p7m file decryption #253

Closed tmdoit-zz closed 4 years ago

tmdoit-zz commented 4 years ago

I did Nodejs script wich did everything like in this example (decrypt function) https://github.com/PeculiarVentures/PKI.js/blob/master/examples/SMIMEEncryptionExample/es6.js, in last step after invoking function "cmsEnvelopedSimp.decrypt" I'm getting an error: "ERROR DURING DECRYPTION PROCESS: Unable to create WebCrypto object". What I need to do to make it work? I know that problem is related to the engine, but I don't understand it. I decrypted and parsed msg via openssl, now I need to do the same using programming language.

rmhrisk commented 4 years ago

Node does not support WebCrypto, you need a polyfill.

https://www.npmjs.com/package/@peculiar/webcrypto

YuryStrozhevsky commented 4 years ago

@tmdoit You need to just check other PKI.js examples. Please notice that almost all of them work (with same code) as browser examples, Node tests (using Mocha) and browser tests (using Mocha). For example check this package.json. There you will find using of sed command. Investigate what exactly the sed script does. Also pay attention on this line - this is where magic happens.

tmdoit-zz commented 4 years ago

Thanks for your quick support! Appreciate it. I did step forward, resolved previous problem, now I'm getting "ERROR DURING DECRYPTION PROCESS: Error: Object's schema was not verified against input data for RSAESOAEPParams". I prepared private key format like for public key (PEM):

const clearEncodedKey = keyFile.toString().replace(/(-----(BEGIN|END)( NEW)? RSA PRIVATE KEY-----|\n)/g, "");
...
 recipientPrivateKey: utils.stringToArrayBuffer(atob(clearEncodedKey))

Key is without passphrase. Cert's algorithm: 1.2.840.113549.1.1.11 - sha256WithRSAEncryption. Any ideas? I'm debugging and walking...

rmhrisk commented 4 years ago

Im pretty sure without the input file there and a repoduction of the issue there is nothing we can do with this.

YuryStrozhevsky commented 4 years ago

@tmdoit Firstly you need to check your input file against live example on pkijs.org and if it works there just investigate difference between PKI.js code and yours. But if your input will not be successfully parsed by live example then go back and past here your input.

tmdoit-zz commented 4 years ago

I created sample data which I can share. Here is the code: https://repl.it/@tmdoit/PKIJS-SMIME, Github repo: https://github.com/tmdoit/PKIJS-SMIME. @YuryStrozhevsky right away.

YuryStrozhevsky commented 4 years ago

@tmdoit I missed one major point: PKI.js has only encryption example for S/MIME, but you need decryption. This is not in scope of PKI.js and belongs only to your personal development process. I provided you information about using PKI.js crypto engines, but making your own code working would not be covered in scope of any issues.

Manouchehri commented 1 year ago

I know this is an old ticket, but I discovered it after searching for similar terms.

It looks like it's possible now, example here: https://github.com/PeculiarVentures/PKI.js/blob/master/examples/SMIMEEncryptionExample/es6.ts