【问题标题】:How do I make WIF use RSA15 when encrypting data using a certificate?使用证书加密数据时如何让 WIF 使用 RSA15?
【发布时间】:2010-11-17 21:26:18
【问题描述】:

我一直在开发 WCF 服务,该服务将返回一个 Base64 编码的字符串,实际上是一个完整的 SAML 响应 XML 文档。因为这些信息将被传递给供应商,所以我必须满足他们对 SAML 文档的外观和编码方式的要求。我无法获得满足他们要求的输出。

我知道 WCF 和 WIF 一起应该可以帮助我。我最初使用 WIF 构建服务来创建 SAML 断言(令牌)和其他 C# 代码以生成最终的 SAML 文档。除文档的 节点外,所有这些都有效并满足供应商的要求。本节使用 AES256 和 RSAOAEP,但供应商需要 AES128 和 RSA15。因此,我正在寻找解决方案。任何帮助将不胜感激。

这里是一个演练。

该服务接受一个用于调用数据库和返回字段的 GUID。然后像这样使用它们:

DataTable userData = GetDataForUser(userId);
List<Claim> claims = new List<Claim>()
{
    new Claim("ClientId", "NameOfClient")
};
foreach (DataRow row in userData.Rows)
{
    string memberId = row["MemberId"].ToString().Trim();
    string firstName = row["FirstName"].ToString().Trim();
    string lastName = row["LastName"].ToString().Trim();
    DateTime dob = Convert.ToDateTime(row["DateOfBirth"], CultureInfo.InvariantCulture);

    claims.Add(new Claim("MemberId", memberId));
    claims.Add(new Claim("FirstName", firstName));
    claims.Add(new Claim("LastName", lastName));
    claims.Add(new Claim("DOB", dob.ToString("MM/dd/yyyy")));
}

return claims;

然后我像这样创建一个 SecurityTokenDescriptor:

SecurityTokenDescriptor descriptor = new SecurityTokenDescriptor();

声明被添加到描述符中,如下所示:

descriptor.Subject = new ClaimsIdentity(claims);

指示描述符像这样加密令牌:

descriptor.EncryptingCredentials = GetEncryptingCredentials();

GetEncryptingCredentials() 例程如下所示:

private EncryptedKeyEncryptingCredentials GetEncryptingCredentials()
{
    // Get the Encrypting Certificate
    X509Certificate2 encryptCert = CertificateHelper.FindSingleCertificate(StoreName.TrustedPeople, StoreLocation.LocalMachine, X509FindType.FindBySubjectDistinguishedName, "<<certificate stuff here >>", true);

    EncryptedKeyEncryptingCredentials encryptingCreds = new EncryptedKeyEncryptingCredentials(encryptCert);

    return encryptingCreds;
 }

所有这些都会生成一个令牌,当写入文件时会给出这个:

  <EncryptedAssertion xmlns="urn:oasis:names:tc:SAML:2.0:assertion">
    <xenc:EncryptedData Id="_16584ace-9f3e-4352-9fc9-f6db8b2e925c" 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#aes256-cbc" />
      <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
        <e:EncryptedKey xmlns:e="http://www.w3.org/2001/04/xmlenc#">
          <e:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p">
            <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
          </e:EncryptionMethod>
          <KeyInfo>
            <o:SecurityTokenReference xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
              <X509Data>
                <X509IssuerSerial>
                  <X509IssuerName><!-- value --></X509IssuerName>
                  <X509SerialNumber><!-- value --></X509SerialNumber>
                </X509IssuerSerial>
              </X509Data>
            </o:SecurityTokenReference>
          </KeyInfo>
          <e:CipherData>
            <e:CipherValue><!-- value -->CipherValue>
          </e:CipherData>
        </e:EncryptedKey>
      </KeyInfo>
      <xenc:CipherData><xenc:CipherValue><!-- value --></xenc:CipherValue>
      </xenc:CipherData>
    </xenc:EncryptedData>
  </EncryptedAssertion>

很好,对吧?没有。供应商需要 部分具有以下子节点:

<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes128-cbc"/>

他们需要 部分来显示:

<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5"/>

我已经尝试了 GetEncryptingCredentials() 例程中我能想到的所有组合。什么都没有产生预期的结果。我收到的最有希望的错误消息如下所示:

ID4178:加密凭据 中提供的 SecurityTokenDescriptor 用于 非对称密钥。您必须使用 EncryptedKeyEncryptingCredentials 到 加密令牌。

有人有建议吗?不要害怕告诉我重新开始。没关系。我只需要让它工作。

提前致谢。

【问题讨论】:

    标签: wcf wif


    【解决方案1】:

    我找到了一个可行的解决方案。至少,它会根据我的需要生成 XML,并且供应商表示他们能够使用我发送给他们的内容。

    我稍微重写了 GetEncryptingCredentials() 例程。现在看起来像这样:

    private EncryptingCredentials GetEncryptingCredentials()
    {
        string keyWrapAlgorithm = SecurityAlgorithms.RsaV15KeyWrap; //"http://www.w3.org/2001/04/xmlenc#aes256-cbc";
        string encryptionAlgorithm = SecurityAlgorithms.Aes128Encryption; //"http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p";
        int keySize = 128;
    
        X509Certificate2 encryptCert = CertificateHelper.FindSingleCertificate(StoreName.TrustedPeople, StoreLocation.LocalMachine, X509FindType.FindBySubjectDistinguishedName, _settings.EncryptingCredentials, true);
    
        EncryptingCredentials encryptingCredentials = new EncryptedKeyEncryptingCredentials(encryptCert, keyWrapAlgorithm, keySize, encryptionAlgorithm);
    
        return encryptingCredentials;
    }
    

    只是想我会让每个人都知道并结束这个循环。

    【讨论】:

    • 你这个摇滚人,我一直在寻找有关如何正常工作的信息!
    • 这篇文章很老了,不知道你是否还记得!哈哈!但是 KeyInfo 下的 X509Data 部分不应该也包含 X509Certificate 吗?
    猜你喜欢
    • 1970-01-01
    • 2012-11-24
    • 2020-09-28
    • 2021-09-01
    • 1970-01-01
    • 2014-12-13
    • 2019-06-12
    • 2015-03-21
    • 2016-10-20
    相关资源
    最近更新 更多