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;
13 import java.security.Principal;
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.Collection;
17 import java.util.HashMap;
18 import java.util.Iterator;
21 import org.apache.log4j.Logger;
22 import org.apache.xml.security.signature.XMLSignature;
23 import org.opensaml.InvalidCryptoException;
24 import org.opensaml.SAMLAssertion;
25 import org.opensaml.SAMLAttribute;
26 import org.opensaml.SAMLException;
27 import org.opensaml.SAMLResponse;
28 import org.opensaml.artifact.Artifact;
29 import org.w3c.dom.Element;
31 import edu.internet2.middleware.shibboleth.aa.AAAttribute;
32 import edu.internet2.middleware.shibboleth.aa.AAException;
33 import edu.internet2.middleware.shibboleth.aa.arp.ArpEngine;
34 import edu.internet2.middleware.shibboleth.aa.arp.ArpProcessingException;
35 import edu.internet2.middleware.shibboleth.aa.attrresolv.AttributeResolver;
36 import edu.internet2.middleware.shibboleth.artifact.ArtifactMapper;
37 import edu.internet2.middleware.shibboleth.common.Credential;
38 import edu.internet2.middleware.shibboleth.common.NameMapper;
39 import edu.internet2.middleware.shibboleth.common.RelyingParty;
40 import edu.internet2.middleware.shibboleth.common.ServiceProviderMapper;
41 import edu.internet2.middleware.shibboleth.common.ShibbolethConfigurationException;
42 import edu.internet2.middleware.shibboleth.common.Trust;
43 import edu.internet2.middleware.shibboleth.common.provider.ShibbolethTrust;
44 import edu.internet2.middleware.shibboleth.metadata.EntitiesDescriptor;
45 import edu.internet2.middleware.shibboleth.metadata.EntityDescriptor;
46 import edu.internet2.middleware.shibboleth.metadata.Metadata;
47 import edu.internet2.middleware.shibboleth.metadata.MetadataException;
48 import edu.internet2.middleware.shibboleth.metadata.MetadataProviderFactory;
51 * Delivers core IdP functionality (Attribute resolution, ARP filtering, Metadata lookup, Signing, Mapping between local &
52 * SAML identifiers, etc.) to components that process protocol-specific requests.
54 * @author Walter Hoehn
56 public class IdPProtocolSupport implements Metadata {
58 private static Logger log = Logger.getLogger(IdPProtocolSupport.class.getName());
59 private Logger transactionLog;
60 private IdPConfig config;
61 private ArrayList<Metadata> metadata = new ArrayList<Metadata>();
62 private NameMapper nameMapper;
63 private ServiceProviderMapper spMapper;
64 private ArpEngine arpEngine;
65 private AttributeResolver resolver;
66 private ArtifactMapper artifactMapper;
67 private Semaphore throttle;
68 private Trust trust = new ShibbolethTrust();
70 IdPProtocolSupport(IdPConfig config, Logger transactionLog, NameMapper nameMapper, ServiceProviderMapper spMapper,
71 ArpEngine arpEngine, AttributeResolver resolver, ArtifactMapper artifactMapper)
72 throws ShibbolethConfigurationException {
74 this.transactionLog = transactionLog;
76 this.nameMapper = nameMapper;
77 this.spMapper = spMapper;
78 spMapper.setMetadata(this);
79 this.arpEngine = arpEngine;
80 this.resolver = resolver;
81 this.artifactMapper = artifactMapper;
83 // Load a semaphore that throttles how many requests the IdP will handle at once
84 throttle = new Semaphore(config.getMaxThreads());
87 public Logger getTransactionLog() {
89 return transactionLog;
92 public IdPConfig getIdPConfig() {
97 public NameMapper getNameMapper() {
102 public ServiceProviderMapper getServiceProviderMapper() {
107 public void signAssertions(SAMLAssertion[] assertions, RelyingParty relyingParty) throws InvalidCryptoException,
110 if (relyingParty.getIdentityProvider().getSigningCredential() == null
111 || relyingParty.getIdentityProvider().getSigningCredential().getPrivateKey() == null) { throw new InvalidCryptoException(
112 SAMLException.RESPONDER, "Invalid signing credential."); }
114 for (int i = 0; i < assertions.length; i++) {
115 String assertionAlgorithm;
116 if (relyingParty.getIdentityProvider().getSigningCredential().getCredentialType() == Credential.RSA) {
117 assertionAlgorithm = XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA1;
118 } else if (relyingParty.getIdentityProvider().getSigningCredential().getCredentialType() == Credential.DSA) {
119 assertionAlgorithm = XMLSignature.ALGO_ID_SIGNATURE_DSA;
121 throw new InvalidCryptoException(SAMLException.RESPONDER,
122 "The Shibboleth IdP currently only supports signing with RSA and DSA keys.");
127 assertions[i].sign(assertionAlgorithm, relyingParty.getIdentityProvider().getSigningCredential()
128 .getPrivateKey(), Arrays.asList(relyingParty.getIdentityProvider().getSigningCredential()
129 .getX509CertificateChain()));
136 public void signResponse(SAMLResponse response, RelyingParty relyingParty) throws SAMLException {
138 // Make sure we have an appropriate credential
139 if (relyingParty.getIdentityProvider().getSigningCredential() == null
140 || relyingParty.getIdentityProvider().getSigningCredential().getPrivateKey() == null) { throw new InvalidCryptoException(
141 SAMLException.RESPONDER, "Invalid signing credential."); }
144 String responseAlgorithm;
145 if (relyingParty.getIdentityProvider().getSigningCredential().getCredentialType() == Credential.RSA) {
146 responseAlgorithm = XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA1;
147 } else if (relyingParty.getIdentityProvider().getSigningCredential().getCredentialType() == Credential.DSA) {
148 responseAlgorithm = XMLSignature.ALGO_ID_SIGNATURE_DSA;
150 throw new InvalidCryptoException(SAMLException.RESPONDER,
151 "The Shibboleth IdP currently only supports signing with RSA and DSA keys.");
155 response.sign(responseAlgorithm, relyingParty.getIdentityProvider().getSigningCredential().getPrivateKey(),
156 Arrays.asList(relyingParty.getIdentityProvider().getSigningCredential().getX509CertificateChain()));
162 protected void addMetadataProvider(Element element) {
164 log.debug("Found Metadata Provider configuration element.");
165 if (!element.getTagName().equals("MetadataProvider")) {
166 log.error("Error while attemtping to load Metadata Provider. Malformed provider specificaion.");
171 metadata.add(MetadataProviderFactory.loadProvider(element));
172 } catch (MetadataException e) {
173 log.error("Unable to load Metadata Provider. Skipping...");
177 public int providerCount() {
179 return metadata.size();
182 public EntityDescriptor lookup(String providerId, boolean strict) {
184 Iterator iterator = metadata.iterator();
185 while (iterator.hasNext()) {
186 EntityDescriptor provider = ((Metadata) iterator.next()).lookup(providerId);
187 if (provider != null) { return provider; }
192 public EntityDescriptor lookup(Artifact artifact, boolean strict) {
194 Iterator iterator = metadata.iterator();
195 while (iterator.hasNext()) {
196 EntityDescriptor provider = ((Metadata) iterator.next()).lookup(artifact);
197 if (provider != null) { return provider; }
202 public EntityDescriptor lookup(String id) {
204 return lookup(id, true);
207 public EntityDescriptor lookup(Artifact artifact) {
209 return lookup(artifact, true);
212 public EntityDescriptor getRootEntity() {
217 public EntitiesDescriptor getRootEntities() {
222 public Collection<? extends SAMLAttribute> getReleaseAttributes(Principal principal, RelyingParty relyingParty,
223 String requester) throws AAException {
226 Collection<URI> potentialAttributes = arpEngine.listPossibleReleaseAttributes(principal, requester);
227 return getReleaseAttributes(principal, relyingParty, requester, potentialAttributes);
229 } catch (ArpProcessingException e) {
230 log.error("An error occurred while processing the ARPs for principal (" + principal.getName() + ") :"
232 throw new AAException("Error retrieving data for principal.");
236 public Collection<? extends SAMLAttribute> getReleaseAttributes(Principal principal, RelyingParty relyingParty,
237 String requester, Collection<URI> attributeNames) throws AAException {
240 Map<String, AAAttribute> attributes = new HashMap<String, AAAttribute>();
241 for (URI name : attributeNames) {
243 AAAttribute attribute = null;
244 if (relyingParty.wantsSchemaHack()) {
245 attribute = new AAAttribute(name.toString(), true);
247 attribute = new AAAttribute(name.toString(), false);
249 attributes.put(attribute.getName(), attribute);
252 return resolveAttributes(principal, requester, relyingParty.getIdentityProvider().getProviderId(),
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 Collection<? extends SAMLAttribute> resolveAttributes(Principal principal, String requester,
268 String responder, Map<String, AAAttribute> attributeSet) throws ArpProcessingException {
270 resolver.resolveAttributes(principal, requester, responder, attributeSet);
271 arpEngine.filterAttributes(attributeSet.values(), principal, requester);
272 return attributeSet.values();
275 public Collection<? extends SAMLAttribute> resolveAttributesNoPolicies(Principal principal, String requester,
276 String responder, Map<String, AAAttribute> attributeSet) {
278 resolver.resolveAttributes(principal, requester, responder, attributeSet);
279 return attributeSet.values();
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() {