我有一个加密的 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>
我还有一个私有(private)解密 key ,其格式如下:
-----BEGIN RSA PRIVATE KEY-----
{mumbo jumbos}
-----END RSA PRIVATE KEY-----
我在这里尝试使用 OneLogin 的 saml 解密工具来解密加密的 SAML 断言(复制+粘贴到该输入框),它的效果就像一个魅力。 https://www.samltool.com/decrypt.php
但是,当我尝试使用 nodejs、passport-saml 导入私钥文件并尝试解密响应时,如果我省略 ("-----BEGIN-- --”或“---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库将抛出错误。
关于node.js - Passport-saml lib 无法使用私有(private)解密 key 正确解密 saml EncryptedAssertion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43487422/