Wednesday, July 15, 2015

Adding KeyInfo to a SAML message

When a message is signed with a private key, the receiving end will need to verify the message using the corresponding public key/certificate. But in order to do this, the receiving end must have the certificate.

The certificate is transported in encoded form in a KeyInfo element. Below is a example


There are many ways to give the receiving end the certificate. Two common methods is metadata and in the message.

When using the metadata method the KeyInfo object is embedded in the metadata inside the KeyDescriptor element. When attaching KeyInfo to the SAML message the element is embedded in the Signature object.

To create and add a KeyInfo object and add it to a SAML message signature, add this call to SecurityHelper before generating the signature.

SecurityHelper.prepareSignatureParams(signature, IDPCredentials.getCredential(),
                    Configuration.getGlobalSecurityConfiguration(), null);
This helper method does not only add a the key info but it also sets the

  • signature algorithm URI
  • canonicalization algorithm URI
  • and HMAC output length (if applicable and a value is configured)

Customising the KeyInfo

The above statement only uses the default configuration of for generating KeyInfo. To customise the KeyInfo you create your own instance of KeyInfoGeneratorFactory, set it up as preferred and use it in the statement. 
The example below shows how to use a X509KeyInfoGeneratorFactory to create a KeyInfo with properties from the X509 certificate used as credential.

X509KeyInfoGeneratorFactory x509Factory = new X509KeyInfoGeneratorFactory();

Configuration.getGlobalSecurityConfiguration().getKeyInfoGeneratorManager().registerFactory("x509emitingKeyInfoGenerator", x509Factory);

SecurityHelper.prepareSignatureParams(signature, SPCredentials.getCredential(), null,  "x509emitingKeyInfoGenerator");


  1. This comment has been removed by the author.

  2. Hi Stefan,
    I'm trying to add KeyInfo to a SAML message in Opensaml3. is that possible, any help? thank's

  3. Your articles are inventive. I am looking forward to reading the plethora of articles that you have linked here. Thumbs up! Melbourne Integriti