use of org.opensaml.security.credential.BasicCredential in project cas by apereo.
the class BasicResourceCredentialFactoryBean method getObject.
@Override
public BasicCredential getObject() throws Exception {
val privateKey = getPrivateKey();
val publicKey = getPublicKey();
val secretKey = getSecretKey();
var credential = (BasicCredential) null;
if (null != publicKey) {
if (null == privateKey) {
credential = new BasicCredential(publicKey);
} else {
if (!KeySupport.matchKeyPair(publicKey, privateKey)) {
throw new BeanCreationException("Public and private keys do not match");
}
credential = new BasicCredential(publicKey, privateKey);
}
} else if (null != secretKey) {
credential = new BasicCredential(secretKey);
} else {
throw new BeanCreationException("Neither public key nor secret key specified");
}
if (null != getUsageType()) {
credential.setUsageType(UsageType.valueOf(getUsageType()));
}
return credential;
}
use of org.opensaml.security.credential.BasicCredential in project cas by apereo.
the class SamlIdPObjectEncrypter method configureKeyDecryptionCredential.
/**
* Configure key decryption credential credential.
*
* @param peerEntityId the peer entity id
* @param adaptor the adaptor
* @param service the service
* @param decryptionConfiguration the decryption configuration
* @return the credential
* @throws Exception the exception
*/
protected Credential configureKeyDecryptionCredential(final String peerEntityId, final SamlRegisteredServiceServiceProviderMetadataFacade adaptor, final SamlRegisteredService service, final BasicDecryptionConfiguration decryptionConfiguration) throws Exception {
val mdCredentialResolver = new SamlIdPMetadataCredentialResolver();
val providers = new ArrayList<KeyInfoProvider>(5);
providers.add(new RSAKeyValueProvider());
providers.add(new DSAKeyValueProvider());
providers.add(new InlineX509DataProvider());
providers.add(new DEREncodedKeyValueProvider());
providers.add(new KeyInfoReferenceProvider());
val keyInfoResolver = new BasicProviderKeyInfoCredentialResolver(providers);
mdCredentialResolver.setKeyInfoCredentialResolver(keyInfoResolver);
val roleDescriptorResolver = SamlIdPUtils.getRoleDescriptorResolver(adaptor, samlIdPProperties.getMetadata().getCore().isRequireValidMetadata());
mdCredentialResolver.setRoleDescriptorResolver(roleDescriptorResolver);
mdCredentialResolver.initialize();
val criteriaSet = new CriteriaSet();
criteriaSet.add(new DecryptionConfigurationCriterion(decryptionConfiguration));
criteriaSet.add(new EntityIdCriterion(peerEntityId));
criteriaSet.add(new EntityRoleCriterion(SPSSODescriptor.DEFAULT_ELEMENT_NAME));
criteriaSet.add(new UsageCriterion(UsageType.ENCRYPTION));
criteriaSet.add(new SamlIdPSamlRegisteredServiceCriterion(service));
LOGGER.debug("Attempting to resolve the decryption key for entity id [{}]", peerEntityId);
val credential = Objects.requireNonNull(mdCredentialResolver.resolveSingle(criteriaSet));
val encryptinKey = samlIdPMetadataLocator.resolveEncryptionKey(Optional.ofNullable(service));
val bean = new PrivateKeyFactoryBean();
bean.setSingleton(false);
bean.setLocation(encryptinKey);
val privateKey = Objects.requireNonNull(bean.getObject());
val basicCredential = new BasicCredential(Objects.requireNonNull(credential.getPublicKey()), privateKey);
decryptionConfiguration.setKEKKeyInfoCredentialResolver(new StaticKeyInfoCredentialResolver(basicCredential));
val list = new ArrayList<EncryptedKeyResolver>(3);
list.add(new InlineEncryptedKeyResolver());
list.add(new EncryptedElementTypeEncryptedKeyResolver());
list.add(new SimpleRetrievalMethodEncryptedKeyResolver());
val encryptedKeyResolver = new ChainingEncryptedKeyResolver(list);
decryptionConfiguration.setEncryptedKeyResolver(encryptedKeyResolver);
return credential;
}
use of org.opensaml.security.credential.BasicCredential in project cas by apereo.
the class DefaultSamlIdPObjectSigner method getResolvedSigningCredential.
private AbstractCredential getResolvedSigningCredential(final Credential credential, final PrivateKey privateKey, final SamlRegisteredService service) {
try {
val samlIdp = casProperties.getAuthn().getSamlIdp();
val credType = SamlIdPResponseProperties.SignatureCredentialTypes.valueOf(StringUtils.defaultIfBlank(service.getSigningCredentialType(), samlIdp.getResponse().getCredentialType().name()).toUpperCase());
LOGGER.trace("Requested credential type [{}] is found for service [{}]", credType, service.getName());
switch(credType) {
case BASIC:
LOGGER.debug("Building credential signing key [{}] based on requested credential type", credType);
if (credential.getPublicKey() == null) {
throw new IllegalArgumentException("Unable to identify the public key from the signing credential");
}
return finalizeSigningCredential(new BasicCredential(credential.getPublicKey(), privateKey), credential);
case X509:
default:
if (credential instanceof BasicX509Credential) {
val certificate = BasicX509Credential.class.cast(credential).getEntityCertificate();
LOGGER.debug("Locating signature signing certificate from credential [{}]", CertUtils.toString(certificate));
return finalizeSigningCredential(new BasicX509Credential(certificate, privateKey), credential);
}
val signingCert = samlIdPMetadataLocator.resolveSigningCertificate(Optional.of(service));
LOGGER.debug("Locating signature signing certificate file from [{}]", signingCert);
val certificate = SamlUtils.readCertificate(signingCert);
return finalizeSigningCredential(new BasicX509Credential(certificate, privateKey), credential);
}
} catch (final Exception e) {
LoggingUtils.error(LOGGER, e);
}
return null;
}
use of org.opensaml.security.credential.BasicCredential in project verify-hub by alphagov.
the class AuthnResponseFactory method aResponseFromIdpBuilder.
public ResponseBuilder aResponseFromIdpBuilder(String idpEntityId, String ipAddressSeenByIdp, String requestId, DateTime issueInstant, String authnStatementAssertionId, String authnAssertionSubjectPid, String authnAssertionIssuer, String authnAssertionInResponseTo, String mdsStatementAssertionId, String mdsAssertionSubjectPid, String mdsAssertionIssuer, String mdsAssertionInResponseTo, Optional<BasicCredential> basicCredential) {
TestCredentialFactory idpSigningCredentialFactory = new TestCredentialFactory(publicSigningCerts.get(idpEntityId), privateSigningKeys.get(idpEntityId));
final Subject mdsAssertionSubject = SubjectBuilder.aSubject().withPersistentId(mdsAssertionSubjectPid).withSubjectConfirmation(SubjectConfirmationBuilder.aSubjectConfirmation().withSubjectConfirmationData(SubjectConfirmationDataBuilder.aSubjectConfirmationData().withInResponseTo(mdsAssertionInResponseTo).build()).build()).build();
final Subject authnAssertionSubject = SubjectBuilder.aSubject().withNameId(buildNameID(authnAssertionSubjectPid)).withSubjectConfirmation(SubjectConfirmationBuilder.aSubjectConfirmation().withSubjectConfirmationData(SubjectConfirmationDataBuilder.aSubjectConfirmationData().withInResponseTo(authnAssertionInResponseTo).build()).build()).build();
final Conditions mdsAssertionConditions = ConditionsBuilder.aConditions().validFor(new Duration(1000 * 60 * 60)).build();
final AttributeStatement matchingDatasetAttributeStatement = MatchingDatasetAttributeStatementBuilder_1_1.aMatchingDatasetAttributeStatement_1_1().build();
final Credential encryptingCredential;
if (basicCredential.isPresent()) {
encryptingCredential = basicCredential.get();
} else {
encryptingCredential = hubEncryptionCredentialFactory.getEncryptingCredential();
}
final Credential signingCredential = idpSigningCredentialFactory.getSigningCredential();
final AssertionBuilder mdsAssertion = AssertionBuilder.anAssertion().withId(generateId()).withIssuer(IssuerBuilder.anIssuer().withIssuerId(mdsAssertionIssuer).build()).withSubject(mdsAssertionSubject).withConditions(mdsAssertionConditions).withId(mdsStatementAssertionId).addAttributeStatement(matchingDatasetAttributeStatement);
final AssertionBuilder authnAssertion = AssertionBuilder.anAssertion().withId(generateId()).addAttributeStatement(anAttributeStatement().addAttribute(anIPAddress().withValue(ipAddressSeenByIdp).build()).build()).withIssuer(IssuerBuilder.anIssuer().withIssuerId(authnAssertionIssuer).build()).withSubject(authnAssertionSubject).withId(authnStatementAssertionId).withIssueInstant(issueInstant).addAuthnStatement(AuthnStatementBuilder.anAuthnStatement().build());
ResponseBuilder responseBuilder = ResponseBuilder.aResponse().withId(generateId()).withIssuer(IssuerBuilder.anIssuer().withIssuerId(idpEntityId).build()).withSigningCredential(signingCredential).withInResponseTo(requestId).addEncryptedAssertion(mdsAssertion.withSignature(SignatureBuilder.aSignature().withSigningCredential(signingCredential).build()).buildWithEncrypterCredential(encryptingCredential)).addEncryptedAssertion(authnAssertion.withSignature(SignatureBuilder.aSignature().withSigningCredential(signingCredential).build()).buildWithEncrypterCredential(encryptingCredential));
return responseBuilder;
}
use of org.opensaml.security.credential.BasicCredential in project verify-hub by alphagov.
the class AssertionDecrypter method decrypt.
private Assertion decrypt(EncryptedAssertion encryptedAssertion) {
Decrypter decrypter = new DecrypterFactory().createDecrypter(List.of(new BasicCredential(publicKey, privateKey)));
decrypter.setRootInNewDocument(true);
try {
return decrypter.decrypt(encryptedAssertion);
} catch (DecryptionException e) {
throw new RuntimeException(e);
}
}
Aggregations