|
1 | 1 | package com.onelogin.saml2.settings; |
2 | 2 |
|
3 | 3 | import java.net.URL; |
| 4 | +import java.util.Arrays; |
4 | 5 | import java.util.Calendar; |
5 | 6 | import java.util.HashMap; |
6 | 7 | import java.util.List; |
@@ -152,7 +153,7 @@ private StrSubstitutor generateSubstitutor(Saml2Settings settings) throws Certif |
152 | 153 |
|
153 | 154 | valueMap.put("strAttributeConsumingService", getAttributeConsumingServiceXml()); |
154 | 155 |
|
155 | | - valueMap.put("strKeyDescriptor", toX509KeyDescriptorsXML(settings.getSPcert(), wantsEncrypted)); |
| 156 | + valueMap.put("strKeyDescriptor", toX509KeyDescriptorsXML(settings.getSPcert(), settings.getSPcertNew(), wantsEncrypted)); |
156 | 157 | valueMap.put("strContacts", toContactsXml(settings.getContacts())); |
157 | 158 | valueMap.put("strOrganization", toOrganizationXml(settings.getOrganization())); |
158 | 159 |
|
@@ -292,52 +293,59 @@ private String toOrganizationXml(Organization organization) { |
292 | 293 | * Generates the KeyDescriptor section of the metadata's template |
293 | 294 | * |
294 | 295 | * @param cert |
295 | | - * the public cert that will be used by the SP to sign and encrypt |
| 296 | + * the public cert that will be used by the SP to sign and encrypt |
296 | 297 | * @param wantsEncrypted |
297 | | - * Whether to include the KeyDescriptor for encryption |
| 298 | + * Whether to include the KeyDescriptor for encryption |
298 | 299 | * |
299 | 300 | * @return the KeyDescriptor section of the metadata's template |
300 | 301 | */ |
301 | 302 | private String toX509KeyDescriptorsXML(X509Certificate cert, Boolean wantsEncrypted) throws CertificateEncodingException { |
302 | | - StringBuilder keyDescriptorXml = new StringBuilder(); |
303 | | - |
304 | | - if (cert != null) { |
305 | | - Base64 encoder = new Base64(64); |
306 | | - byte[] encodedCert = cert.getEncoded(); |
307 | | - String certString = new String(encoder.encode(encodedCert)); |
308 | | - |
309 | | - keyDescriptorXml.append("<md:KeyDescriptor use=\"signing\">"); |
310 | | - keyDescriptorXml.append("<ds:KeyInfo xmlns:ds=\"http://www.w3.org/2000/09/xmldsig#\">"); |
311 | | - keyDescriptorXml.append("<ds:X509Data>"); |
312 | | - keyDescriptorXml.append("<ds:X509Certificate>"+certString+"</ds:X509Certificate>"); |
313 | | - keyDescriptorXml.append("</ds:X509Data>"); |
314 | | - keyDescriptorXml.append("</ds:KeyInfo>"); |
315 | | - keyDescriptorXml.append("</md:KeyDescriptor>"); |
316 | | - |
317 | | - if (wantsEncrypted) { |
318 | | - keyDescriptorXml.append("<md:KeyDescriptor use=\"encryption\">"); |
319 | | - keyDescriptorXml.append("<ds:KeyInfo xmlns:ds=\"http://www.w3.org/2000/09/xmldsig#\">"); |
320 | | - keyDescriptorXml.append("<ds:X509Data>"); |
321 | | - keyDescriptorXml.append("<ds:X509Certificate>"+certString+"</ds:X509Certificate>"); |
322 | | - keyDescriptorXml.append("</ds:X509Data>"); |
323 | | - keyDescriptorXml.append("</ds:KeyInfo>"); |
324 | | - keyDescriptorXml.append("</md:KeyDescriptor>"); |
325 | | - } |
326 | | - } |
327 | | - |
328 | | - return keyDescriptorXml.toString(); |
| 303 | + return this.toX509KeyDescriptorsXML(cert, null, wantsEncrypted); |
329 | 304 | } |
330 | 305 |
|
331 | 306 | /** |
332 | 307 | * Generates the KeyDescriptor section of the metadata's template |
333 | 308 | * |
334 | | - * @param cert |
| 309 | + * @param certCurrent |
335 | 310 | * the public cert that will be used by the SP to sign and encrypt |
| 311 | + * @param certNew |
| 312 | + * the public cert that will be used by the SP to sign and encrypt in future |
| 313 | + * @param wantsEncrypted |
| 314 | + * Whether to include the KeyDescriptor for encryption |
336 | 315 | * |
337 | 316 | * @return the KeyDescriptor section of the metadata's template |
338 | 317 | */ |
339 | | - private String toX509KeyDescriptorsXML(X509Certificate cert) throws CertificateEncodingException { |
340 | | - return toX509KeyDescriptorsXML(cert, true); |
| 318 | + private String toX509KeyDescriptorsXML(X509Certificate certCurrent, X509Certificate certNew, Boolean wantsEncrypted) throws CertificateEncodingException { |
| 319 | + StringBuilder keyDescriptorXml = new StringBuilder(); |
| 320 | + |
| 321 | + List<X509Certificate> certs = Arrays.asList(certCurrent, certNew); |
| 322 | + for(X509Certificate cert : certs) { |
| 323 | + if (cert != null) { |
| 324 | + Base64 encoder = new Base64(64); |
| 325 | + byte[] encodedCert = cert.getEncoded(); |
| 326 | + String certString = new String(encoder.encode(encodedCert)); |
| 327 | + |
| 328 | + keyDescriptorXml.append("<md:KeyDescriptor use=\"signing\">"); |
| 329 | + keyDescriptorXml.append("<ds:KeyInfo xmlns:ds=\"http://www.w3.org/2000/09/xmldsig#\">"); |
| 330 | + keyDescriptorXml.append("<ds:X509Data>"); |
| 331 | + keyDescriptorXml.append("<ds:X509Certificate>"+certString+"</ds:X509Certificate>"); |
| 332 | + keyDescriptorXml.append("</ds:X509Data>"); |
| 333 | + keyDescriptorXml.append("</ds:KeyInfo>"); |
| 334 | + keyDescriptorXml.append("</md:KeyDescriptor>"); |
| 335 | + |
| 336 | + if (wantsEncrypted) { |
| 337 | + keyDescriptorXml.append("<md:KeyDescriptor use=\"encryption\">"); |
| 338 | + keyDescriptorXml.append("<ds:KeyInfo xmlns:ds=\"http://www.w3.org/2000/09/xmldsig#\">"); |
| 339 | + keyDescriptorXml.append("<ds:X509Data>"); |
| 340 | + keyDescriptorXml.append("<ds:X509Certificate>"+certString+"</ds:X509Certificate>"); |
| 341 | + keyDescriptorXml.append("</ds:X509Data>"); |
| 342 | + keyDescriptorXml.append("</ds:KeyInfo>"); |
| 343 | + keyDescriptorXml.append("</md:KeyDescriptor>"); |
| 344 | + } |
| 345 | + } |
| 346 | + } |
| 347 | + |
| 348 | + return keyDescriptorXml.toString(); |
341 | 349 | } |
342 | 350 |
|
343 | 351 | /** |
|
0 commit comments