-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/maven/fixes/9.0' into maven/rele…
…ase/9.0
- Loading branch information
Showing
5 changed files
with
126 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
.../main/java/org/edu_sharing/spring/security/saml2/SecurityConfigurationSamlEncryption.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package org.edu_sharing.spring.security.saml2; | ||
|
||
import org.apache.xml.security.encryption.XMLCipherParameters; | ||
import org.edu_sharing.spring.conditions.ConditionalOnProperty; | ||
import org.opensaml.saml.saml2.core.EncryptedID; | ||
import org.opensaml.saml.saml2.core.LogoutRequest; | ||
import org.opensaml.saml.saml2.core.NameID; | ||
import org.opensaml.saml.saml2.encryption.Encrypter; | ||
import org.opensaml.security.credential.BasicCredential; | ||
import org.opensaml.security.x509.BasicX509Credential; | ||
import org.opensaml.xmlsec.encryption.support.DataEncryptionParameters; | ||
import org.opensaml.xmlsec.encryption.support.EncryptionException; | ||
import org.opensaml.xmlsec.encryption.support.KeyEncryptionParameters; | ||
import org.opensaml.xmlsec.keyinfo.impl.X509KeyInfoGeneratorFactory; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.saml2.Saml2Exception; | ||
import org.springframework.security.saml2.core.Saml2X509Credential; | ||
import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticatedPrincipal; | ||
import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration; | ||
import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistrationRepository; | ||
import org.springframework.security.saml2.provider.service.web.authentication.logout.OpenSaml4LogoutRequestResolver; | ||
import org.springframework.security.saml2.provider.service.web.authentication.logout.Saml2LogoutRequestResolver; | ||
|
||
import javax.crypto.KeyGenerator; | ||
import javax.crypto.SecretKey; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.cert.X509Certificate; | ||
|
||
@ConditionalOnProperty(name = "security.sso.saml.encryptLogoutRequest", havingValue = "true") | ||
@Configuration | ||
public class SecurityConfigurationSamlEncryption { | ||
@Bean | ||
Saml2LogoutRequestResolver logoutRequestResolver(RelyingPartyRegistrationRepository registrations) { | ||
OpenSaml4LogoutRequestResolver logoutRequestResolver = | ||
new OpenSaml4LogoutRequestResolver(registrations); | ||
logoutRequestResolver.setParametersConsumer((parameters) -> { | ||
String name = ((Saml2AuthenticatedPrincipal) parameters.getAuthentication().getPrincipal()).getFirstAttribute("CustomAttribute"); | ||
String format = "urn:oasis:names:tc:SAML:2.0:nameid-format:transient"; | ||
LogoutRequest logoutRequest = parameters.getLogoutRequest(); | ||
NameID nameId = logoutRequest.getNameID(); | ||
nameId.setValue(name); | ||
nameId.setFormat(format); | ||
|
||
RelyingPartyRegistration.AssertingPartyDetails assertingPartyDetails = parameters.getRelyingPartyRegistration().getAssertingPartyDetails(); | ||
|
||
Saml2X509Credential credential = (assertingPartyDetails.getEncryptionX509Credentials().size() > 0 ) | ||
//when a key descriptor <md:KeyDescriptor use="encryption"> is defined in idp metadata | ||
? assertingPartyDetails.getEncryptionX509Credentials().iterator().next() | ||
//else fallback to default certificate | ||
: assertingPartyDetails.getVerificationX509Credentials().iterator().next(); | ||
|
||
EncryptedID encrypted = encrypted(nameId, credential); | ||
logoutRequest.setEncryptedID(encrypted); | ||
logoutRequest.setNameID(null); | ||
}); | ||
return logoutRequestResolver; | ||
} | ||
|
||
static EncryptedID encrypted(NameID nameId, Saml2X509Credential credential) { | ||
|
||
Encrypter encrypter = getEncrypter(credential); | ||
try { | ||
return encrypter.encrypt(nameId); | ||
} | ||
catch (EncryptionException ex) { | ||
throw new Saml2Exception("Unable to encrypt nameID.", ex); | ||
} | ||
} | ||
|
||
private static Encrypter getEncrypter(Saml2X509Credential credential) { | ||
String dataAlgorithm = XMLCipherParameters.AES_256; | ||
BasicCredential dataCredential = new BasicCredential(generateSecretKey()); | ||
DataEncryptionParameters dataEncryptionParameters = new DataEncryptionParameters(); | ||
dataEncryptionParameters.setEncryptionCredential(dataCredential); | ||
dataEncryptionParameters.setAlgorithm(dataAlgorithm); | ||
|
||
String keyAlgorithm = XMLCipherParameters.RSA_OAEP; | ||
KeyEncryptionParameters keyEncryptionParameters = new KeyEncryptionParameters(); | ||
keyEncryptionParameters.setEncryptionCredential( new BasicX509Credential(credential.getCertificate())); | ||
keyEncryptionParameters.setAlgorithm(keyAlgorithm); | ||
|
||
X509KeyInfoGeneratorFactory factory = new X509KeyInfoGeneratorFactory(); | ||
factory.setEmitEntityIDAsKeyName(false); | ||
factory.setEmitX509SubjectName(true); | ||
|
||
keyEncryptionParameters.setKeyInfoGenerator(factory.newInstance()); | ||
dataEncryptionParameters.setKeyInfoGenerator(factory.newInstance()); | ||
|
||
Encrypter encrypter = new Encrypter(dataEncryptionParameters,keyEncryptionParameters); | ||
encrypter.setKeyPlacement(Encrypter.KeyPlacement.INLINE); | ||
|
||
return encrypter; | ||
} | ||
|
||
private static SecretKey generateSecretKey() { | ||
KeyGenerator keyGen = null; | ||
try { | ||
keyGen = KeyGenerator.getInstance("AES"); | ||
} catch (NoSuchAlgorithmException e) { | ||
throw new RuntimeException(e); | ||
} | ||
keyGen.init(256); // for AES-256 | ||
SecretKey secretKey = keyGen.generateKey(); | ||
return secretKey; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters