xml-crypto icon indicating copy to clipboard operation
xml-crypto copied to clipboard

allow to use pem with passphrase

Open yaronn opened this issue 10 years ago • 5 comments

currently if a pem requires a passphrase than the user will be prompt to enter it. effectively this means a passphrase is not supported.

solution is to change signer.sign (also verify?) to accept an object as first parameter: https://nodejs.org/api/crypto.html#crypto_sign_sign_private_key_output_format

this is not available yet in node 0.10.33 (not sure starting which version it is). when used on non supported node version the object parameter to sign will throw:

crypto.js:429
  var ret = this._binding.sign(toBuf(key));
                      ^
TypeError: Not a buffer
    at Sign.sign (crypto.js:429:27)

yaronn avatar Mar 29 '15 15:03 yaronn

Just for sharing, now I use this one digitalbazaar/forge to get the pem protected by a passphrase. They have an API pki.decryptRsaPrivateKey( file, passphrase ) to decrypt the pem.

tngan avatar Oct 04 '15 13:10 tngan

@tngan cool! i use forge in some of my apps to parse certificates.. we'll have forge in mind when find a solution, thnks

bjrmatos avatar Oct 04 '15 13:10 bjrmatos

I stumbled on the same problem. Full example for the solution using forge:

var signedXml = new SignedXml();
signedXml.signingKey = readPrivateKeyFromProtectedPem('myPrivateKey.pem', 'myPassword');

function readPrivateKeyFromProtectedPem(path, passphrase){
    var pem = fs.readFileSync(path).toString();
    var privateKey = pki.decryptRsaPrivateKey(pem, passphrase);
    return pki.privateKeyToPem(privateKey);
}

marklagendijk avatar Feb 02 '16 11:02 marklagendijk

has this changed in recent node versions? I'm getting the following error which looks like passphrase could be passed in? But I'm not familiar with how this module works.

crypto.js:283 var ret = this._handle.sign(toBuf(key), null, passphrase);

I'm on Node version 6.9.1

chrisgh1 avatar May 31 '17 21:05 chrisgh1

@yaronn , @tngan , would you be interested in creating a PR with a test suite to add that functionality to xml-crypto?

cjbarth avatar May 29 '23 21:05 cjbarth