gpt4 book ai didi

java - 如何使用 openssl (.PCKS8) 文件签署 saml 2.0 断言

转载 作者:行者123 更新时间:2023-12-02 07:03:09 25 4
gpt4 key购买 nike

您好,我需要一些有关 saml 2.0 Authn 请求的信息。我想使用 openssl 创建的 *.pkcs8 文件签署我的身份验证请求。我可以使用 java keytool 处理 keystore 文件。但我想使用 openssl 生成的 *.PKCS8 文件来实现相同的目的。我已经为此苦苦挣扎了一段时间。我可以用它生成 xml。

//授权请求...

<samlp:AuthnRequest xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" AssertionConsumerServiceURL="http://localhost:8080/sp/AssertionConsumerService" Destination="http://localhost:8080/idp/SingleSignOnService" ID="95cc3943-67dd-43ef-809b-2ccd8bd3e4e9" IssueInstant="2013-04-26T12:18:48.799Z" Version="2.0">
<saml:Issuer xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" Format="urn:oasis:names:tc:SAML:2.0:nameid-format:entity">sp</saml:Issuer>
<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<ds:SignedInfo>
<ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
<ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
<ds:Reference URI="#95cc3943-67dd-43ef-809b-2ccd8bd3e4e9">
<ds:Transforms>
<ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
<ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#">
<ec:InclusiveNamespaces xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#"
PrefixList="ds saml samlp"/>
</ds:Transform>
</ds:Transforms>
<ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<ds:DigestValue>2HkVe/KnVzcMgneRUItjq2V/FEA=</ds:DigestValue>
</ds:Reference>
</ds:SignedInfo>
<ds:SignatureValue>
NjCxy8R3NjkN8B932FJolGTqtYTBBTLboHUo7ZqEXxICUW/ZhOV2Pwe+c4R0/TrPqBPVZBItlXyv
at3edIMrr7RlEFGy3rt7pPVRXUcmF6jtDZajCpwwaEKKD--REMOVED SOME CODE------------
egb8dua65WhY1KkugNPG4FWTVhtzul/CBo9a8vN/ZuXRbZQ6sUWbq1BFgC6Zmw8kr1aUNBwqRi7r
ZNPXcGVhXuFQTTV4Kuc1eiI1lgANKLTrkCBRSw==
</ds:SignatureValue>
</ds:Signature>
</samlp:AuthnRequest>

//结束

我无法获取使用 java keytool 获取的 keyInfo 和 x509data 以及证书值。

    <ds:KeyInfo>
<ds:X509Data>
<ds:X509Certificate>hZB2kOYypWs33Bs2BTaKZOKGig0CAwEAATANBgkqhkiG9w0BAQUFAAOB
gQB3Cfe0iTfrXY9E22TFy5b87kwpDKjLopNLtX3kqSUlfjnbN5tYN4zr91H5dZUkuFF83z7ztzKi
zkcxiMgVVQkU2X1bn5SdErvmS7aEcG8+5TdlO5bf+8as04u5qug+oQun5s1t9mSvaF7Ol5CX/gkp
EUTjXx28kldbY7ETgDUrSw==</ds:X509Certificate>
</ds:X509Data>
</ds:KeyInfo>
</ds:Signature>

还告诉我我的身份验证请求是否已完成。Artifact 和 POST(Assertion) saml 消息的 Authn 请求是否相同

请帮忙!!!

最佳答案

您如何构建 org.opensaml.xml.security.credential.Credential 对象?您只能加载private-key来自 PKCS8 文件。您仍然需要公钥来完全构造 Credential 对象。如果您的公钥存储在 DER编码字节,您可以使用以下代码来创建Credential并使用它来签署请求

/**
* Load privateKeyDerBytes from PKCS8 file and publicKeyDerBytes from .cer, .crt, .der files
*/
private static Credential getCredential(byte[] privateKeyDerBytes , byte[] publicKeyDerBytes) throws IOException
{
PrivateKey privateKey = PKCS8Key.parse(new DerValue( privateKeyDerBytes ));
PublicKey publicKey = X509Key.parse(new DerValue(publicKeyDerBytes));
BasicCredential basicCredential = new BasicCredential();
basicCredential.setUsageType(UsageType.SIGNING);
basicCredential.setPrivateKey(privateKey);
basicCredential.setPublicKey(publicKey);
return basicCredential;
}

public static void signAssertion(Assertion assertion , byte[] privateKeyDerBytes , byte[] publicKeyDerBytes) throws IOException, SecurityException
{
// get Credential
Credential credential = getCredential(privateKeyDerBytes, publicKeyDerBytes);
// create Signature
Signature signature = (Signature) Configuration.getBuilderFactory().getBuilder(
Signature.DEFAULT_ELEMENT_NAME).buildObject(
Signature.DEFAULT_ELEMENT_NAME);

signature.setSigningCredential(credential);
signature
.setSignatureAlgorithm(SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA1);
signature
.setCanonicalizationAlgorithm(SignatureConstants.ALGO_ID_C14N_EXCL_OMIT_COMMENTS);
signature.setKeyInfo(getKeyInfo(credential));

assertion.setSignature(signature);

}

public static KeyInfo getKeyInfo(Credential credential)
throws SecurityException {
SecurityConfiguration secConfiguration = Configuration
.getGlobalSecurityConfiguration();
NamedKeyInfoGeneratorManager namedKeyInfoGeneratorManager = secConfiguration
.getKeyInfoGeneratorManager();
KeyInfoGeneratorManager keyInfoGeneratorManager = namedKeyInfoGeneratorManager
.getDefaultManager();
KeyInfoGeneratorFactory factory = keyInfoGeneratorManager
.getFactory(credential);
KeyInfoGenerator generator = factory.newInstance();
return generator.generate(credential);

}

关于java - 如何使用 openssl (.PCKS8) 文件签署 saml 2.0 断言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16399945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com