|
| 1 | +/* |
| 2 | + * Copyright 2002-2024 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.metadata; |
| 18 | + |
| 19 | +import java.nio.charset.StandardCharsets; |
| 20 | +import java.security.PrivateKey; |
| 21 | +import java.security.cert.X509Certificate; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.LinkedHashMap; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | + |
| 28 | +import net.shibboleth.utilities.java.support.resolver.CriteriaSet; |
| 29 | +import net.shibboleth.utilities.java.support.xml.SerializeSupport; |
| 30 | +import org.opensaml.core.xml.XMLObject; |
| 31 | +import org.opensaml.core.xml.config.XMLObjectProviderRegistrySupport; |
| 32 | +import org.opensaml.core.xml.io.Marshaller; |
| 33 | +import org.opensaml.core.xml.io.MarshallingException; |
| 34 | +import org.opensaml.saml.security.impl.SAMLMetadataSignatureSigningParametersResolver; |
| 35 | +import org.opensaml.security.SecurityException; |
| 36 | +import org.opensaml.security.credential.BasicCredential; |
| 37 | +import org.opensaml.security.credential.Credential; |
| 38 | +import org.opensaml.security.credential.CredentialSupport; |
| 39 | +import org.opensaml.security.credential.UsageType; |
| 40 | +import org.opensaml.xmlsec.SignatureSigningParameters; |
| 41 | +import org.opensaml.xmlsec.SignatureSigningParametersResolver; |
| 42 | +import org.opensaml.xmlsec.criterion.SignatureSigningConfigurationCriterion; |
| 43 | +import org.opensaml.xmlsec.crypto.XMLSigningUtil; |
| 44 | +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; |
| 48 | +import org.opensaml.xmlsec.signature.SignableXMLObject; |
| 49 | +import org.opensaml.xmlsec.signature.support.SignatureConstants; |
| 50 | +import org.opensaml.xmlsec.signature.support.SignatureSupport; |
| 51 | +import org.w3c.dom.Element; |
| 52 | + |
| 53 | +import org.springframework.security.saml2.Saml2Exception; |
| 54 | +import org.springframework.security.saml2.core.Saml2ParameterNames; |
| 55 | +import org.springframework.security.saml2.core.Saml2X509Credential; |
| 56 | +import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration; |
| 57 | +import org.springframework.util.Assert; |
| 58 | +import org.springframework.web.util.UriComponentsBuilder; |
| 59 | +import org.springframework.web.util.UriUtils; |
| 60 | + |
| 61 | +/** |
| 62 | + * Utility methods for signing SAML components with OpenSAML |
| 63 | + * |
| 64 | + * For internal use only. |
| 65 | + * |
| 66 | + * @author Josh Cummings |
| 67 | + * @since 6.3 |
| 68 | + */ |
| 69 | +final class OpenSamlSigningUtils { |
| 70 | + |
| 71 | + static String serialize(XMLObject object) { |
| 72 | + try { |
| 73 | + Marshaller marshaller = XMLObjectProviderRegistrySupport.getMarshallerFactory().getMarshaller(object); |
| 74 | + Element element = marshaller.marshall(object); |
| 75 | + return SerializeSupport.nodeToString(element); |
| 76 | + } |
| 77 | + catch (MarshallingException ex) { |
| 78 | + throw new Saml2Exception(ex); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + static <O extends SignableXMLObject> O sign(O object, RelyingPartyRegistration relyingPartyRegistration) { |
| 83 | + SignatureSigningParameters parameters = resolveSigningParameters(relyingPartyRegistration); |
| 84 | + try { |
| 85 | + SignatureSupport.signObject(object, parameters); |
| 86 | + return object; |
| 87 | + } |
| 88 | + catch (Exception ex) { |
| 89 | + throw new Saml2Exception(ex); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + static QueryParametersPartial sign(RelyingPartyRegistration registration) { |
| 94 | + return new QueryParametersPartial(registration); |
| 95 | + } |
| 96 | + |
| 97 | + private static SignatureSigningParameters resolveSigningParameters( |
| 98 | + RelyingPartyRegistration relyingPartyRegistration) { |
| 99 | + List<Credential> credentials = resolveSigningCredentials(relyingPartyRegistration); |
| 100 | + List<String> algorithms = relyingPartyRegistration.getAssertingPartyDetails().getSigningAlgorithms(); |
| 101 | + List<String> digests = Collections.singletonList(SignatureConstants.ALGO_ID_DIGEST_SHA256); |
| 102 | + String canonicalization = SignatureConstants.ALGO_ID_C14N_EXCL_OMIT_COMMENTS; |
| 103 | + SignatureSigningParametersResolver resolver = new SAMLMetadataSignatureSigningParametersResolver(); |
| 104 | + CriteriaSet criteria = new CriteriaSet(); |
| 105 | + BasicSignatureSigningConfiguration signingConfiguration = new BasicSignatureSigningConfiguration(); |
| 106 | + signingConfiguration.setSigningCredentials(credentials); |
| 107 | + signingConfiguration.setSignatureAlgorithms(algorithms); |
| 108 | + signingConfiguration.setSignatureReferenceDigestMethods(digests); |
| 109 | + signingConfiguration.setSignatureCanonicalizationAlgorithm(canonicalization); |
| 110 | + signingConfiguration.setKeyInfoGeneratorManager(buildSignatureKeyInfoGeneratorManager()); |
| 111 | + criteria.add(new SignatureSigningConfigurationCriterion(signingConfiguration)); |
| 112 | + try { |
| 113 | + SignatureSigningParameters parameters = resolver.resolveSingle(criteria); |
| 114 | + Assert.notNull(parameters, "Failed to resolve any signing credential"); |
| 115 | + return parameters; |
| 116 | + } |
| 117 | + catch (Exception ex) { |
| 118 | + throw new Saml2Exception(ex); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private static NamedKeyInfoGeneratorManager buildSignatureKeyInfoGeneratorManager() { |
| 123 | + final NamedKeyInfoGeneratorManager namedManager = new NamedKeyInfoGeneratorManager(); |
| 124 | + |
| 125 | + namedManager.setUseDefaultManager(true); |
| 126 | + final KeyInfoGeneratorManager defaultManager = namedManager.getDefaultManager(); |
| 127 | + |
| 128 | + // Generator for X509Credentials |
| 129 | + final X509KeyInfoGeneratorFactory x509Factory = new X509KeyInfoGeneratorFactory(); |
| 130 | + x509Factory.setEmitEntityCertificate(true); |
| 131 | + x509Factory.setEmitEntityCertificateChain(true); |
| 132 | + |
| 133 | + defaultManager.registerFactory(x509Factory); |
| 134 | + |
| 135 | + return namedManager; |
| 136 | + } |
| 137 | + |
| 138 | + private static List<Credential> resolveSigningCredentials(RelyingPartyRegistration relyingPartyRegistration) { |
| 139 | + List<Credential> credentials = new ArrayList<>(); |
| 140 | + for (Saml2X509Credential x509Credential : relyingPartyRegistration.getSigningX509Credentials()) { |
| 141 | + X509Certificate certificate = x509Credential.getCertificate(); |
| 142 | + PrivateKey privateKey = x509Credential.getPrivateKey(); |
| 143 | + BasicCredential credential = CredentialSupport.getSimpleCredential(certificate, privateKey); |
| 144 | + credential.setEntityId(relyingPartyRegistration.getEntityId()); |
| 145 | + credential.setUsageType(UsageType.SIGNING); |
| 146 | + credentials.add(credential); |
| 147 | + } |
| 148 | + return credentials; |
| 149 | + } |
| 150 | + |
| 151 | + private OpenSamlSigningUtils() { |
| 152 | + |
| 153 | + } |
| 154 | + |
| 155 | + static class QueryParametersPartial { |
| 156 | + |
| 157 | + final RelyingPartyRegistration registration; |
| 158 | + |
| 159 | + final Map<String, String> components = new LinkedHashMap<>(); |
| 160 | + |
| 161 | + QueryParametersPartial(RelyingPartyRegistration registration) { |
| 162 | + this.registration = registration; |
| 163 | + } |
| 164 | + |
| 165 | + QueryParametersPartial param(String key, String value) { |
| 166 | + this.components.put(key, value); |
| 167 | + return this; |
| 168 | + } |
| 169 | + |
| 170 | + Map<String, String> parameters() { |
| 171 | + SignatureSigningParameters parameters = resolveSigningParameters(this.registration); |
| 172 | + Credential credential = parameters.getSigningCredential(); |
| 173 | + String algorithmUri = parameters.getSignatureAlgorithm(); |
| 174 | + this.components.put(Saml2ParameterNames.SIG_ALG, algorithmUri); |
| 175 | + UriComponentsBuilder builder = UriComponentsBuilder.newInstance(); |
| 176 | + for (Map.Entry<String, String> component : this.components.entrySet()) { |
| 177 | + builder.queryParam(component.getKey(), |
| 178 | + UriUtils.encode(component.getValue(), StandardCharsets.ISO_8859_1)); |
| 179 | + } |
| 180 | + String queryString = builder.build(true).toString().substring(1); |
| 181 | + try { |
| 182 | + byte[] rawSignature = XMLSigningUtil.signWithURI(credential, algorithmUri, |
| 183 | + queryString.getBytes(StandardCharsets.UTF_8)); |
| 184 | + String b64Signature = Saml2Utils.samlEncode(rawSignature); |
| 185 | + this.components.put(Saml2ParameterNames.SIGNATURE, b64Signature); |
| 186 | + } |
| 187 | + catch (SecurityException ex) { |
| 188 | + throw new Saml2Exception(ex); |
| 189 | + } |
| 190 | + return this.components; |
| 191 | + } |
| 192 | + |
| 193 | + } |
| 194 | + |
| 195 | +} |
0 commit comments