node.jsencryptiononeloginpassport-saml

passport-saml lib doesn't correctly decrypt saml EncryptedAssertion with a private decryption key


I have an encrypted SAML 2.0 response/assertion that i need to decrypt. the format looks like this:

<saml:EncryptedAssertion xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">
    <xenc:EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
      <xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes128-cbc"/>
      <ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
        <xenc:EncryptedKey>
          <xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p"/>
          <xenc:CipherData>
            <xenc:CipherValue>{some ciphers}</xenc:CipherValue>
          </xenc:CipherData>
        </xenc:EncryptedKey>
      </ds:KeyInfo>
      <xenc:CipherData>
        <xenc:CipherValue>{assertion body}</xenc:CipherValue>
    </xenc:CipherData>
    </xenc:EncryptedData>
</saml:EncryptedAssertion>

I also have a private decryption key with format like this:

-----BEGIN RSA PRIVATE KEY-----
{mumbo jumbos}
-----END RSA PRIVATE KEY-----

I have tried using OneLogin's saml decryption tools here to decrypt the encrypted SAML assertion (copy+paste to that input box), and it works like a charm. https://www.samltool.com/decrypt.php

However, when I tried to use nodejs, passport-saml to import the private key file and try to decrypt the response, it gets a "Invalid PEM formated" if i omit the ("-----BEGIN----" or "---END---" banner), or it gets a "Invalid RSAES-OAEP padding" error.

This is my code snippet:

const fs = require('fs');
const Promise = require('bluebird');
const path = require('path');
const forge = require('node-forge');
let pkey = fs.readFileSync(path.join(__dirname,'./myTestKey.pem'), 'utf8');
//let testKey = new Buffer(pkey).toString('base64');
let SAML = require('passport-saml/lib/passport-saml/saml.js');
let saml = new SAML({...,decryptionPvk: pkey });
let validatePostResponseAsync = Promise.promisify(saml.validatePostResponse);

validatePostResponseAsync(myResponse, pkey)
.then(response=>{
})
.catch(error=>{
 // it always throw error of the 2 mentioned above. 
})

Any workaround would be appreciated.


Solution

  • I think i figured it out. for those who is struggling with similar issue, you must include the ---BEGIN RSA PRIVATE KEY--- and ---END RSA PRIVATE KEY---. the node-forge library which passport-saml lib is depending on will throw error if the banner is not included.