passport-saml lib没有使用私有解密密钥

时间:2017-04-19 05:49:38

标签: 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>

我还有一个私有解密密钥,格式如下:

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

我尝试过使用OneLogin的saml解密工具来解密加密的SAML断言(复制+粘贴到该输入框),它就像一个魅力。 https://www.samltool.com/decrypt.php

但是,当我尝试使用nodejs,passport-saml导入私钥文件并尝试解密响应时,它会得到一个&#34;无效的PEM格式化#34;如果我省略(&#34; ----- BEGIN ----&#34;或&#34; --- END ---&#34; banner),或者它得到一个&#34;无效的RSAES-OAEP填充&#34;错误。

这是我的代码段:

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

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

1 个答案:

答案 0 :(得分:2)

我想我明白了。对于那些正在努力解决类似问题的人,您必须包含---BEGIN RSA PRIVATE KEY------END RSA PRIVATE KEY---。如果不包含横幅,则passport-saml lib依赖的node-forge库将抛出错误。

相关问题