2 * Copyright [2005] [University Corporation for Advanced Internet Development, Inc.] Licensed under the Apache License,
3 * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy
4 * of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in
5 * writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
6 * OF ANY KIND, either express or implied. See the License for the specific language governing permissions and
7 * limitations under the License.
10 package edu.internet2.middleware.shibboleth.idp;
14 import java.security.Principal;
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.Iterator;
19 import org.apache.log4j.Logger;
20 import org.apache.xml.security.signature.XMLSignature;
21 import org.opensaml.InvalidCryptoException;
22 import org.opensaml.SAMLAssertion;
23 import org.opensaml.SAMLAttribute;
24 import org.opensaml.SAMLException;
25 import org.opensaml.SAMLResponse;
26 import org.opensaml.artifact.Artifact;
27 import org.w3c.dom.Element;
29 import edu.internet2.middleware.shibboleth.aa.AAAttribute;
30 import edu.internet2.middleware.shibboleth.aa.AAAttributeSet;
31 import edu.internet2.middleware.shibboleth.aa.AAException;
32 import edu.internet2.middleware.shibboleth.aa.arp.ArpEngine;
33 import edu.internet2.middleware.shibboleth.aa.arp.ArpProcessingException;
34 import edu.internet2.middleware.shibboleth.aa.attrresolv.AttributeResolver;
35 import edu.internet2.middleware.shibboleth.artifact.ArtifactMapper;
36 import edu.internet2.middleware.shibboleth.common.Credential;
37 import edu.internet2.middleware.shibboleth.common.NameMapper;
38 import edu.internet2.middleware.shibboleth.common.RelyingParty;
39 import edu.internet2.middleware.shibboleth.common.ServiceProviderMapper;
40 import edu.internet2.middleware.shibboleth.common.ShibbolethConfigurationException;
41 import edu.internet2.middleware.shibboleth.common.Trust;
42 import edu.internet2.middleware.shibboleth.common.provider.ShibbolethTrust;
43 import edu.internet2.middleware.shibboleth.metadata.EntitiesDescriptor;
44 import edu.internet2.middleware.shibboleth.metadata.EntityDescriptor;
45 import edu.internet2.middleware.shibboleth.metadata.Metadata;
46 import edu.internet2.middleware.shibboleth.metadata.MetadataProviderFactory;
47 import edu.internet2.middleware.shibboleth.metadata.MetadataException;
50 * Delivers core IdP functionality (Attribute resolution, ARP filtering, Metadata lookup, Signing, Mapping between local &
51 * SAML identifiers, etc.) to components that process protocol-specific requests.
53 * @author Walter Hoehn
55 public class IdPProtocolSupport implements Metadata {
57 private static Logger log = Logger.getLogger(IdPProtocolSupport.class.getName());
58 private Logger transactionLog;
59 private IdPConfig config;
60 private ArrayList metadata = new ArrayList();
61 private NameMapper nameMapper;
62 private ServiceProviderMapper spMapper;
63 private ArpEngine arpEngine;
64 private AttributeResolver resolver;
65 private ArtifactMapper artifactMapper;
66 private Semaphore throttle;
67 private Trust trust = new ShibbolethTrust();
69 IdPProtocolSupport(IdPConfig config, Logger transactionLog, NameMapper nameMapper, ServiceProviderMapper spMapper,
70 ArpEngine arpEngine, AttributeResolver resolver, ArtifactMapper artifactMapper)
71 throws ShibbolethConfigurationException {
73 this.transactionLog = transactionLog;
75 this.nameMapper = nameMapper;
76 this.spMapper = spMapper;
77 spMapper.setMetadata(this);
78 this.arpEngine = arpEngine;
79 this.resolver = resolver;
80 this.artifactMapper = artifactMapper;
82 // Load a semaphore that throttles how many requests the IdP will handle at once
83 throttle = new Semaphore(config.getMaxThreads());
86 public Logger getTransactionLog() {
88 return transactionLog;
91 public IdPConfig getIdPConfig() {
96 public NameMapper getNameMapper() {
101 public ServiceProviderMapper getServiceProviderMapper() {
106 public void signAssertions(SAMLAssertion[] assertions, RelyingParty relyingParty) throws InvalidCryptoException,
109 if (relyingParty.getIdentityProvider().getSigningCredential() == null
110 || relyingParty.getIdentityProvider().getSigningCredential().getPrivateKey() == null) { throw new InvalidCryptoException(
111 SAMLException.RESPONDER, "Invalid signing credential."); }
113 for (int i = 0; i < assertions.length; i++) {
114 String assertionAlgorithm;
115 if (relyingParty.getIdentityProvider().getSigningCredential().getCredentialType() == Credential.RSA) {
116 assertionAlgorithm = XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA1;
117 } else if (relyingParty.getIdentityProvider().getSigningCredential().getCredentialType() == Credential.DSA) {
118 assertionAlgorithm = XMLSignature.ALGO_ID_SIGNATURE_DSA;
120 throw new InvalidCryptoException(SAMLException.RESPONDER,
121 "The Shibboleth IdP currently only supports signing with RSA and DSA keys.");
126 assertions[i].sign(assertionAlgorithm, relyingParty.getIdentityProvider().getSigningCredential()
127 .getPrivateKey(), Arrays.asList(relyingParty.getIdentityProvider().getSigningCredential()
128 .getX509CertificateChain()));
135 public void signResponse(SAMLResponse response, RelyingParty relyingParty) throws SAMLException {
137 // Make sure we have an appropriate credential
138 if (relyingParty.getIdentityProvider().getSigningCredential() == null
139 || relyingParty.getIdentityProvider().getSigningCredential().getPrivateKey() == null) { throw new InvalidCryptoException(
140 SAMLException.RESPONDER, "Invalid signing credential."); }
143 String responseAlgorithm;
144 if (relyingParty.getIdentityProvider().getSigningCredential().getCredentialType() == Credential.RSA) {
145 responseAlgorithm = XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA1;
146 } else if (relyingParty.getIdentityProvider().getSigningCredential().getCredentialType() == Credential.DSA) {
147 responseAlgorithm = XMLSignature.ALGO_ID_SIGNATURE_DSA;
149 throw new InvalidCryptoException(SAMLException.RESPONDER,
150 "The Shibboleth IdP currently only supports signing with RSA and DSA keys.");
154 response.sign(responseAlgorithm, relyingParty.getIdentityProvider().getSigningCredential().getPrivateKey(),
155 Arrays.asList(relyingParty.getIdentityProvider().getSigningCredential().getX509CertificateChain()));
161 protected void addMetadataProvider(Element element) {
163 log.debug("Found Metadata Provider configuration element.");
164 if (!element.getTagName().equals("MetadataProvider")) {
165 log.error("Error while attemtping to load Metadata Provider. Malformed provider specificaion.");
170 metadata.add(MetadataProviderFactory.loadProvider(element));
171 } catch (MetadataException e) {
172 log.error("Unable to load Metadata Provider. Skipping...");
176 public int providerCount() {
178 return metadata.size();
181 public EntityDescriptor lookup(String providerId, boolean strict) {
183 Iterator iterator = metadata.iterator();
184 while (iterator.hasNext()) {
185 EntityDescriptor provider = ((Metadata) iterator.next()).lookup(providerId);
186 if (provider != null) { return provider; }
191 public EntityDescriptor lookup(Artifact artifact, boolean strict) {
193 Iterator iterator = metadata.iterator();
194 while (iterator.hasNext()) {
195 EntityDescriptor provider = ((Metadata) iterator.next()).lookup(artifact);
196 if (provider != null) { return provider; }
201 public EntityDescriptor lookup(String id) {
203 return lookup(id, true);
206 public EntityDescriptor lookup(Artifact artifact) {
208 return lookup(artifact, true);
211 public EntityDescriptor getRootEntity() {
216 public EntitiesDescriptor getRootEntities() {
221 public SAMLAttribute[] getReleaseAttributes(Principal principal, RelyingParty relyingParty, String requester,
222 URL resource) throws AAException {
225 URI[] potentialAttributes = arpEngine.listPossibleReleaseAttributes(principal, requester, resource);
226 return getReleaseAttributes(principal, relyingParty, requester, resource, potentialAttributes);
228 } catch (ArpProcessingException e) {
229 log.error("An error occurred while processing the ARPs for principal (" + principal.getName() + ") :"
231 throw new AAException("Error retrieving data for principal.");
235 public SAMLAttribute[] getReleaseAttributes(Principal principal, RelyingParty relyingParty, String requester,
236 URL resource, URI[] attributeNames) throws AAException {
239 AAAttributeSet attributeSet = new AAAttributeSet();
240 for (int i = 0; i < attributeNames.length; i++) {
242 AAAttribute attribute = null;
243 if (relyingParty.wantsSchemaHack()) {
244 attribute = new AAAttribute(attributeNames[i].toString(), true);
246 attribute = new AAAttribute(attributeNames[i].toString(), false);
249 attributeSet.add(attribute);
252 return resolveAttributes(principal, requester, relyingParty.getIdentityProvider().getProviderId(),
253 resource, attributeSet);
255 } catch (SAMLException e) {
256 log.error("An error occurred while creating attributes for principal (" + principal.getName() + ") :"
258 throw new AAException("Error retrieving data for principal.");
260 } catch (ArpProcessingException e) {
261 log.error("An error occurred while processing the ARPs for principal (" + principal.getName() + ") :"
263 throw new AAException("Error retrieving data for principal.");
267 public SAMLAttribute[] resolveAttributes(Principal principal, String requester, String responder, URL resource,
268 AAAttributeSet attributeSet) throws ArpProcessingException {
270 resolver.resolveAttributes(principal, requester, responder, attributeSet);
271 arpEngine.filterAttributes(attributeSet, principal, requester, resource);
272 return attributeSet.getAttributes();
275 public SAMLAttribute[] resolveAttributesNoPolicies(Principal principal, String requester, String responder,
276 AAAttributeSet attributeSet) {
278 resolver.resolveAttributes(principal, requester, responder, attributeSet);
279 return attributeSet.getAttributes();
283 * Cleanup resources that won't be released when this object is garbage-collected
285 public void destroy() {
291 public ArtifactMapper getArtifactMapper() {
293 return artifactMapper;
296 public Trust getTrust() {
301 private class Semaphore {
305 public Semaphore(int value) {
310 public synchronized void enter() {
316 } catch (InterruptedException e) {
317 // squelch and continue
322 public synchronized void exit() {