Skip to content

Commit 46ddd59

Browse files
committed
#117,#119 Send encryption keys only when required
1 parent ecc7e08 commit 46ddd59

File tree

2 files changed

+68
-11
lines changed

2 files changed

+68
-11
lines changed

core/src/main/java/com/onelogin/saml2/settings/Metadata.java

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/

core/src/test/java/com/onelogin/saml2/test/settings/MetadataTest.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,47 @@ public void testToX509KeyDescriptorsXML() throws IOException, CertificateEncodin
249249
assertThat(metadataStr2, not(containsString(keyDescriptorSignStr)));
250250
assertThat(metadataStr2, not(containsString(keyDescriptorEncStr)));
251251
}
252-
252+
253+
/**
254+
* Tests the toX509KeyDescriptorsXML method of Metadata
255+
* Case: Check where to add or not md:KeyDescriptor encryption
256+
*
257+
* @throws IOException
258+
* @throws CertificateEncodingException
259+
* @throws Error
260+
*
261+
* @see com.onelogin.saml2.settings.Metadata#toX509KeyDescriptorsXML
262+
*/
263+
@Test
264+
public void testToX509KeyDescriptorsXMLEncryption() throws IOException, CertificateEncodingException, Error {
265+
Saml2Settings settings = new SettingsBuilder().fromFile("config/config.all.properties").build();
266+
String keyDescriptorEncStr = "<md:KeyDescriptor use=\"encryption\"><ds:KeyInfo xmlns:ds=\"http://www.w3.org/2000/09/xmldsig#\"><ds:X509Data><ds:X509Certificate>MIICeDCCAeGgAwIBAgIBADANBgkqhkiG9w0BAQ0FADBZMQswCQYDVQQGEwJ1czET";
267+
268+
settings.setWantAssertionsEncrypted(false);
269+
settings.setWantNameIdEncrypted(false);
270+
Metadata metadataObj = new Metadata(settings);
271+
String metadataStr = metadataObj.getMetadataString();
272+
assertThat(metadataStr, not(containsString(keyDescriptorEncStr)));
273+
274+
settings.setWantAssertionsEncrypted(true);
275+
settings.setWantNameIdEncrypted(false);
276+
metadataObj = new Metadata(settings);
277+
metadataStr = metadataObj.getMetadataString();
278+
assertThat(metadataStr, containsString(keyDescriptorEncStr));
279+
280+
settings.setWantAssertionsEncrypted(false);
281+
settings.setWantNameIdEncrypted(true);
282+
metadataObj = new Metadata(settings);
283+
metadataStr = metadataObj.getMetadataString();
284+
assertThat(metadataStr, containsString(keyDescriptorEncStr));
285+
286+
settings.setWantAssertionsEncrypted(true);
287+
settings.setWantNameIdEncrypted(true);
288+
metadataObj = new Metadata(settings);
289+
metadataStr = metadataObj.getMetadataString();
290+
assertThat(metadataStr, containsString(keyDescriptorEncStr));
291+
}
292+
253293
/**
254294
* Tests the getAttributeConsumingServiceXml method of Metadata
255295
*

0 commit comments

Comments
 (0)