@@ -136,7 +136,8 @@ public Metadata(Saml2Settings settings) throws CertificateEncodingException {
136136 private StrSubstitutor generateSubstitutor (Saml2Settings settings ) throws CertificateEncodingException {
137137
138138 Map <String , String > valueMap = new HashMap <String , String >();
139-
139+ Boolean wantsEncrypted = settings .getWantAssertionsEncrypted () || settings .getWantNameIdEncrypted ();
140+
140141 valueMap .put ("id" , Util .generateUniqueID ());
141142 valueMap .put ("validUntilTime" , Util .formatDateTime (validUntilTime .getTimeInMillis ()));
142143 valueMap .put ("cacheDuration" , String .valueOf (cacheDuration ));
@@ -150,7 +151,7 @@ private StrSubstitutor generateSubstitutor(Saml2Settings settings) throws Certif
150151
151152 valueMap .put ("strAttributeConsumingService" , getAttributeConsumingServiceXml ());
152153
153- valueMap .put ("strKeyDescriptor" , toX509KeyDescriptorsXML (settings .getSPcert ()));
154+ valueMap .put ("strKeyDescriptor" , toX509KeyDescriptorsXML (settings .getSPcert (), wantsEncrypted ));
154155 valueMap .put ("strContacts" , toContactsXml (settings .getContacts ()));
155156 valueMap .put ("strOrganization" , toOrganizationXml (settings .getOrganization ()));
156157
@@ -291,10 +292,12 @@ private String toOrganizationXml(Organization organization) {
291292 *
292293 * @param cert
293294 * the public cert that will be used by the SP to sign and encrypt
295+ * @param wantsEncrypted
296+ * Whether to include the KeyDescriptor for encryption
294297 *
295298 * @return the KeyDescriptor section of the metadata's template
296299 */
297- private String toX509KeyDescriptorsXML (X509Certificate cert ) throws CertificateEncodingException {
300+ private String toX509KeyDescriptorsXML (X509Certificate cert , Boolean wantsEncrypted ) throws CertificateEncodingException {
298301 StringBuilder keyDescriptorXml = new StringBuilder ();
299302
300303 if (cert != null ) {
@@ -310,18 +313,32 @@ private String toX509KeyDescriptorsXML(X509Certificate cert) throws CertificateE
310313 keyDescriptorXml .append ("</ds:KeyInfo>" );
311314 keyDescriptorXml .append ("</md:KeyDescriptor>" );
312315
313- keyDescriptorXml .append ("<md:KeyDescriptor use=\" encryption\" >" );
314- keyDescriptorXml .append ("<ds:KeyInfo xmlns:ds=\" http://www.w3.org/2000/09/xmldsig#\" >" );
315- keyDescriptorXml .append ("<ds:X509Data>" );
316- keyDescriptorXml .append ("<ds:X509Certificate>" +certString +"</ds:X509Certificate>" );
317- keyDescriptorXml .append ("</ds:X509Data>" );
318- keyDescriptorXml .append ("</ds:KeyInfo>" );
319- keyDescriptorXml .append ("</md:KeyDescriptor>" );
316+ if (wantsEncrypted ) {
317+ keyDescriptorXml .append ("<md:KeyDescriptor use=\" encryption\" >" );
318+ keyDescriptorXml .append ("<ds:KeyInfo xmlns:ds=\" http://www.w3.org/2000/09/xmldsig#\" >" );
319+ keyDescriptorXml .append ("<ds:X509Data>" );
320+ keyDescriptorXml .append ("<ds:X509Certificate>" +certString +"</ds:X509Certificate>" );
321+ keyDescriptorXml .append ("</ds:X509Data>" );
322+ keyDescriptorXml .append ("</ds:KeyInfo>" );
323+ keyDescriptorXml .append ("</md:KeyDescriptor>" );
324+ }
320325 }
321326
322327 return keyDescriptorXml .toString ();
323328 }
324329
330+ /**
331+ * Generates the KeyDescriptor section of the metadata's template
332+ *
333+ * @param cert
334+ * the public cert that will be used by the SP to sign and encrypt
335+ *
336+ * @return the KeyDescriptor section of the metadata's template
337+ */
338+ private String toX509KeyDescriptorsXML (X509Certificate cert ) throws CertificateEncodingException {
339+ return toX509KeyDescriptorsXML (cert , true );
340+ }
341+
325342 /**
326343 * @return the md:SingleLogoutService section of the metadata's template
327344 */
0 commit comments