简体   繁体   English

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

[英]How to sign an saml 2.0 assertion using the openssl (.PCKS8) file

Hi i need some infomation on saml 2.0 Authn Request . 嗨我需要关于saml 2.0 Authn请求的一些信息。 I want to sign my authn request with a openssl created *.pkcs8 file. 我想用openssl创建的* .pkcs8文件签署我的authn请求。 I can do with a keystore file using java keytool . 我可以使用java keytool来处理密钥库文件。 But I want to achieve the same using *.PKCS8 file generateing using the openssl. 但是我希望使用openssl生成使用* .PKCS8文件来实现相同的功能。 I have been struggling with this for some time . 我一直在努力解决这个问题。 I am able to generate the xml with it . 我能用它生成xml。

// Authn Request ... // Authn请求......

<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>

// END // 结束

I am not able to get the keyInfo and x509data and certificate values that i was able to get using the java keytool. 我无法获得使用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>

Also tell me is my Authn Request complete . 还告诉我是我的Authn请求完成。 Also is the Authn request same for both the Artifact and POST(Assertion) saml message 对于Artifact和POST(断言)saml消息,Authn请求也是相同的

PLS HELP !!! PLS帮助!!!

How are you constructing your org.opensaml.xml.security.credential.Credential object ? 你是如何构建org.opensaml.xml.security.credential.Credential对象的? You can only load a private-key from PKCS8 file. 您只能从PKCS8文件加载私钥 You still need the public-key to fully construct the Credential object. 您仍然需要公钥来完全构造Credential对象。 If your public-key is stored in DER encoded bytes you can use the following code to create the Credential and use that to sign the request 如果您的公钥存储在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);

}

You can use this to output the keyinfo 您可以使用它来输出keyinfo

X509KeyInfoGeneratorFactory fact = new X509KeyInfoGeneratorFactory();
fact.setEmitEntityCertificate(true);
signature.setKeyInfo(fact.newInstance().generate(cred));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM