Skip to content

Commit 812bb0e

Browse files
committed
Add missing KeyInfo
Closes gh-11354
1 parent bb9c7d1 commit 812bb0e

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

saml2/saml2-service-provider/src/main/java/org/springframework/security/saml2/provider/service/web/authentication/OpenSamlSigningUtils.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
import org.opensaml.xmlsec.criterion.SignatureSigningConfigurationCriterion;
4343
import org.opensaml.xmlsec.crypto.XMLSigningUtil;
4444
import org.opensaml.xmlsec.impl.BasicSignatureSigningConfiguration;
45+
import org.opensaml.xmlsec.keyinfo.KeyInfoGeneratorManager;
46+
import org.opensaml.xmlsec.keyinfo.NamedKeyInfoGeneratorManager;
47+
import org.opensaml.xmlsec.keyinfo.impl.X509KeyInfoGeneratorFactory;
4548
import org.opensaml.xmlsec.signature.SignableXMLObject;
4649
import org.opensaml.xmlsec.signature.support.SignatureConstants;
4750
import org.opensaml.xmlsec.signature.support.SignatureSupport;
@@ -102,6 +105,7 @@ private static SignatureSigningParameters resolveSigningParameters(
102105
signingConfiguration.setSignatureAlgorithms(algorithms);
103106
signingConfiguration.setSignatureReferenceDigestMethods(digests);
104107
signingConfiguration.setSignatureCanonicalizationAlgorithm(canonicalization);
108+
signingConfiguration.setKeyInfoGeneratorManager(buildSignatureKeyInfoGeneratorManager());
105109
criteria.add(new SignatureSigningConfigurationCriterion(signingConfiguration));
106110
try {
107111
SignatureSigningParameters parameters = resolver.resolveSingle(criteria);
@@ -113,6 +117,22 @@ private static SignatureSigningParameters resolveSigningParameters(
113117
}
114118
}
115119

120+
private static NamedKeyInfoGeneratorManager buildSignatureKeyInfoGeneratorManager() {
121+
final NamedKeyInfoGeneratorManager namedManager = new NamedKeyInfoGeneratorManager();
122+
123+
namedManager.setUseDefaultManager(true);
124+
final KeyInfoGeneratorManager defaultManager = namedManager.getDefaultManager();
125+
126+
// Generator for X509Credentials
127+
final X509KeyInfoGeneratorFactory x509Factory = new X509KeyInfoGeneratorFactory();
128+
x509Factory.setEmitEntityCertificate(true);
129+
x509Factory.setEmitEntityCertificateChain(true);
130+
131+
defaultManager.registerFactory(x509Factory);
132+
133+
return namedManager;
134+
}
135+
116136
private static List<Credential> resolveSigningCredentials(RelyingPartyRegistration relyingPartyRegistration) {
117137
List<Credential> credentials = new ArrayList<>();
118138
for (Saml2X509Credential x509Credential : relyingPartyRegistration.getSigningX509Credentials()) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2002-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.saml2.provider.service.web.authentication;
18+
19+
import java.util.UUID;
20+
21+
import javax.xml.namespace.QName;
22+
23+
import org.junit.jupiter.api.BeforeEach;
24+
import org.junit.jupiter.api.Test;
25+
import org.opensaml.core.xml.XMLObject;
26+
import org.opensaml.core.xml.config.XMLObjectProviderRegistrySupport;
27+
import org.opensaml.saml.common.SAMLVersion;
28+
import org.opensaml.saml.saml2.core.Issuer;
29+
import org.opensaml.saml.saml2.core.Response;
30+
import org.opensaml.xmlsec.signature.Signature;
31+
32+
import org.springframework.security.saml2.core.OpenSamlInitializationService;
33+
import org.springframework.security.saml2.core.TestSaml2X509Credentials;
34+
import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration;
35+
36+
import static org.assertj.core.api.Assertions.assertThat;
37+
38+
/**
39+
* Test open SAML signatures
40+
*/
41+
public class OpenSamlSigningUtilsTests {
42+
43+
static {
44+
OpenSamlInitializationService.initialize();
45+
}
46+
47+
private RelyingPartyRegistration registration;
48+
49+
@BeforeEach
50+
public void setup() {
51+
this.registration = RelyingPartyRegistration.withRegistrationId("saml-idp")
52+
.entityId("https://some.idp.example.com/entity-id").signingX509Credentials((c) -> {
53+
c.add(TestSaml2X509Credentials.relyingPartySigningCredential());
54+
c.add(TestSaml2X509Credentials.assertingPartySigningCredential());
55+
}).assertingPartyDetails((c) -> c.entityId("https://some.idp.example.com/entity-id")
56+
.singleSignOnServiceLocation("https://some.idp.example.com/service-location"))
57+
.build();
58+
}
59+
60+
@Test
61+
public void whenSigningAnObjectThenKeyInfoIsPartOfTheSignature() {
62+
Response response = response("destination", "issuer");
63+
OpenSamlSigningUtils.sign(response, this.registration);
64+
Signature signature = response.getSignature();
65+
assertThat(signature).isNotNull();
66+
assertThat(signature.getKeyInfo()).isNotNull();
67+
}
68+
69+
Response response(String destination, String issuerEntityId) {
70+
Response response = build(Response.DEFAULT_ELEMENT_NAME);
71+
response.setID("R" + UUID.randomUUID());
72+
response.setVersion(SAMLVersion.VERSION_20);
73+
response.setID("_" + UUID.randomUUID());
74+
response.setDestination(destination);
75+
response.setIssuer(issuer(issuerEntityId));
76+
return response;
77+
}
78+
79+
Issuer issuer(String entityId) {
80+
Issuer issuer = build(Issuer.DEFAULT_ELEMENT_NAME);
81+
issuer.setValue(entityId);
82+
return issuer;
83+
}
84+
85+
<T extends XMLObject> T build(QName qName) {
86+
return (T) XMLObjectProviderRegistrySupport.getBuilderFactory().getBuilder(qName).buildObject(qName);
87+
}
88+
89+
}

0 commit comments

Comments
 (0)