Sample code for 30+ languages & platforms
Java

Create AuthNRequest with embedded signature (HTTP-POST binding)

See more XML Digital Signatures Examples

Demonstrates how to create a SAML AuthNRequest with embedded signature (HTTP-POST binding).

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.Cert;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.StringBuilder;
import com.chilkatsoft.chilkat2.Xml;
import com.chilkatsoft.chilkat2.XmlDSig;
import com.chilkatsoft.chilkat2.XmlDSigGen;

public class ChilkatExample {

    public static void main(String[] args) {
        boolean success = false;

        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        // This example will sign the following SAML AuthNRequest:

        // <samlp:AuthnRequest
        //      xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
        //      xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"
        //      ID="pfx41d8ef22-e612-8c50-9960-1b16f15741b3"
        //      Version="2.0" ProviderName="SP test" IssueInstant="2014-07-16T23:52:45Z"
        //      Destination="http://idp.example.com/SSOService.php"
        //      ProtocolBinding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"
        //      AssertionConsumerServiceURL="http://sp.example.com/demo1/index.php?acs">
        //   <saml:Issuer>http://sp.example.com/demo1/metadata.php</saml:Issuer>
        //   <samlp:NameIDPolicy Format="urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress" AllowCreate="true"/>
        //   <samlp:RequestedAuthnContext Comparison="exact">
        //     <saml:AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport</saml:AuthnContextClassRef>
        //   </samlp:RequestedAuthnContext>
        // </samlp:AuthnRequest>

        // First we build the XML to be signed.
        //
        // Use this online tool to generate the code from sample XML:
        // Generate Code to Create XML

        success = true;
        Xml xmlToSign = new Xml();
        xmlToSign.setTag("samlp:AuthnRequest");
        xmlToSign.addAttribute("xmlns:samlp", "urn:oasis:names:tc:SAML:2.0:protocol");
        xmlToSign.addAttribute("xmlns:saml", "urn:oasis:names:tc:SAML:2.0:assertion");
        xmlToSign.addAttribute("ID", "pfx41d8ef22-e612-8c50-9960-1b16f15741b3");
        xmlToSign.addAttribute("Version", "2.0");
        xmlToSign.addAttribute("ProviderName", "SP test");
        xmlToSign.addAttribute("IssueInstant", "2014-07-16T23:52:45Z");
        xmlToSign.addAttribute("Destination", "http://idp.example.com/SSOService.php");
        xmlToSign.addAttribute("ProtocolBinding", "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST");
        xmlToSign.addAttribute("AssertionConsumerServiceURL", "http://sp.example.com/demo1/index.php?acs");
        xmlToSign.updateChildContent("saml:Issuer", "http://sp.example.com/demo1/metadata.php");
        xmlToSign.updateAttrAt("samlp:NameIDPolicy", true, "Format", "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress");
        xmlToSign.updateAttrAt("samlp:NameIDPolicy", true, "AllowCreate", "true");
        xmlToSign.updateAttrAt("samlp:RequestedAuthnContext", true, "Comparison", "exact");
        xmlToSign.updateChildContent("samlp:RequestedAuthnContext|saml:AuthnContextClassRef", "urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport");

        // Also see the online tool to generate the code from sample already-signed XML:
        // Generate XML Signature Creation Code from an Already-Signed XML Sample

        XmlDSigGen gen = new XmlDSigGen();

        gen.setSigLocation("samlp:AuthnRequest");
        gen.setSigNamespacePrefix("ds");
        gen.setSigNamespaceUri("http://www.w3.org/2000/09/xmldsig#");
        gen.setSignedInfoCanonAlg("EXCL_C14N");
        gen.setSignedInfoDigestMethod("sha1");

        // -------- Reference 1 --------
        gen.addSameDocRef("pfx41d8ef22-e612-8c50-9960-1b16f15741b3", "sha1", "EXCL_C14N", "", "");

        // Provide a certificate + private key. (PFX password is test123)
        Cert cert = new Cert();
        try {
            cert.loadPfxFile("qa_data/pfx/cert_test123.pfx", "test123");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        gen.setX509Cert(cert, true);

        gen.setKeyInfoType("X509Data");
        gen.setX509Type("Certificate");

        // Load XML to be signed...
        StringBuilder sbXml = new StringBuilder();
        xmlToSign.getXmlSb(sbXml);

        gen.setBehaviors("IndentedSignature,ForceAddEnvelopedSignatureTransform");

        // Sign the XML...
        try {
            gen.createXmlDSigSb(sbXml);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Save the signed XMl to a file.
        sbXml.writeFile("qa_output/signedXml.xml", "utf-8", false);

        // A sample of the signed XML is shown below..
        System.out.println(sbXml.getAsString());

        // ----------------------------------------
        // Verify the signature we just produced...
        XmlDSig verifier = new XmlDSig();
        try {
            verifier.loadSignatureSb(sbXml);

            verifier.verifySignature(true);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("This signature was successfully verified.");

        // -----------------------------------------
        // Sample output of AuthNRequest signed XML:
        // (Line-breaks and some indenting added for readability..)

        // <?xml version="1.0" encoding="utf-8"?>
        // <samlp:AuthnRequest
        //   xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
        //   xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"
        //   ID="pfx41d8ef22-e612-8c50-9960-1b16f15741b3"
        //   Version="2.0" ProviderName="SP test"
        //   IssueInstant="2014-07-16T23:52:45Z"
        //   Destination="http://idp.example.com/SSOService.php"
        //   ProtocolBinding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"
        //   AssertionConsumerServiceURL="http://sp.example.com/demo1/index.php?acs">
        //     <saml:Issuer>http://sp.example.com/demo1/metadata.php</saml:Issuer>
        //     <samlp:NameIDPolicy Format="urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress" AllowCreate="true"/>
        //     <samlp:RequestedAuthnContext Comparison="exact">
        //         <saml:AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport</saml:AuthnContextClassRef>
        //     </samlp:RequestedAuthnContext>
        //     <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="#pfx41d8ef22-e612-8c50-9960-1b16f15741b3">
        //           <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#"/>
        //           </ds:Transforms>
        //           <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
        //           <ds:DigestValue>5d+/YNShy4qnvZcvik8fHHg2SWQ=</ds:DigestValue>
        //         </ds:Reference>
        //       </ds:SignedInfo>
        //       <ds:SignatureValue>QS16H5...U5LQ==</ds:SignatureValue>
        //       <ds:KeyInfo>
        //         <ds:X509Data>
        //           <ds:X509Certificate>MIIF...tjlF4=</ds:X509Certificate>
        //         </ds:X509Data>
        //       </ds:KeyInfo>
        //     </ds:Signature>
        // </samlp:AuthnRequest>
        //
    }
}