node.js - Passport-saml lib 无法使用私有(private)解密 key 正确解密 saml EncryptedAssertion

标签 node.js encryption onelogin passport-saml

我有一个加密的 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/

相关文章:

javascript - setTimeout 在 try/catch 中不起作用

ruby - Rails - Onelogin ruby​​-saml 集成问题 +

php - 是什么导致 SAML 响应中的 Responder 状态

java - 插件org.jacoco :jacoco-maven-plugin: one of its dependencies could not be resolved:

javascript - 在套接字 io 函数中重用变量

javascript - 通过 knex 迁移 promise 所有竞争条件

node.js - Google Sheets API 导致 Node 出现 "GoogleAuth is not a constructor"错误

asp.net-mvc - MVC 数据安全

php - C 中的河豚 php?

json - 使用客户管理的 key 创建存储服务加密 ARM 模板