passport-saml lib不能正确解密带有私钥解密密钥的saml EncryptedAssertion

我有一个encryption的SAML 2.0响应/断言,我需要解密。 格式如下所示:

<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> 

我也有这样的格式的私人解密密钥:

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

我已经尝试在这里使用OneLogin的saml解密工具来解密encryption的SAML断言(复制并粘贴到该input框),它就像一个魅力。 https://www.samltool.com/decrypt.php

但是,当我试图使用nodejs,passport-saml导入私钥文件并尝试解密响应时,如果我省略(“—– BEGIN —-”),则会得到“Invalid PEM formated”或“— END —”标题),或者得到“无效的RSAES-OAEP填充”错误。

这是我的代码片段:

 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. }) 

任何解决方法,将不胜感激。

我想我明白了。 对于那些正在为类似问题而苦恼的人,您必须包含---BEGIN RSA PRIVATE KEY------END RSA PRIVATE KEY--- 。 passport-saml lib依赖的node-forge库将会抛出错误(如果不包括横幅)。