View Javadoc

1   /*
2    * Copyright 2008 University Corporation for Advanced Internet Development, Inc.
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    * http://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.opensaml.xml.security.x509;
18  
19  import java.security.PrivateKey;
20  import java.security.PublicKey;
21  import java.security.cert.X509CRL;
22  import java.security.cert.X509Certificate;
23  import java.util.Arrays;
24  import java.util.Collection;
25  import java.util.Collections;
26  
27  import javax.net.ssl.X509KeyManager;
28  
29  import org.opensaml.xml.security.credential.BasicCredential;
30  import org.opensaml.xml.util.DatatypeHelper;
31  
32  /** A class that wraps a {@link X509KeyManager} and exposes it as an {@link X509Credential}. */
33  public class X509KeyManagerX509CredentialAdapter extends BasicCredential implements X509Credential {
34  
35      /** Alias used to reference the credential in the key manager. */
36      private String credentialAlias;
37  
38      /** Wrapped key manager. */
39      private X509KeyManager keyManager;
40  
41      /**
42       * Constructor.
43       * 
44       * @param manager wrapped key manager
45       * @param alias alias used to reference the credential in the key manager
46       */
47      public X509KeyManagerX509CredentialAdapter(X509KeyManager manager, String alias) {
48          if (manager == null) {
49              throw new IllegalArgumentException("Key manager may not be null");
50          }
51          keyManager = manager;
52  
53          credentialAlias = DatatypeHelper.safeTrimOrNullString(alias);
54          if (credentialAlias == null) {
55              throw new IllegalArgumentException("Entity alias may not be null");
56          }
57      }
58  
59      /** {@inheritDoc} */
60      public Collection<X509CRL> getCRLs() {
61          return Collections.EMPTY_LIST;
62      }
63  
64      /** {@inheritDoc} */
65      public X509Certificate getEntityCertificate() {
66          X509Certificate[] certs = keyManager.getCertificateChain(credentialAlias);
67          if (certs != null && certs.length > 0) {
68              return certs[0];
69          }
70  
71          return null;
72      }
73  
74      /** {@inheritDoc} */
75      public Collection<X509Certificate> getEntityCertificateChain() {
76          X509Certificate[] certs = keyManager.getCertificateChain(credentialAlias);
77          if (certs != null && certs.length > 0) {
78              return Arrays.asList(certs);
79          }
80  
81          return null;
82      }
83  
84      /** {@inheritDoc} */
85      public PrivateKey getPrivateKey() {
86          return keyManager.getPrivateKey(credentialAlias);
87      }
88  
89      /** {@inheritDoc} */
90      public PublicKey getPublicKey() {
91          return getEntityCertificate().getPublicKey();
92      }
93  }