View Javadoc

1   /*
2    * Copyright  2003-2004 The Apache Software Foundation.
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  
18  package org.apache.ws.security.components.crypto;
19  
20  import org.apache.ws.security.WSSecurityException;
21  
22  import java.io.InputStream;
23  import java.math.BigInteger;
24  import java.security.KeyStore;
25  import java.security.PrivateKey;
26  import java.security.cert.Certificate;
27  import java.security.cert.CertificateFactory;
28  import java.security.cert.X509Certificate;
29  
30  /***
31   * Crypto.
32   * <p/>
33   *
34   * @author Davanum Srinivas (dims@yahoo.com).
35   */
36  public interface Crypto {
37      /***
38       * load a X509Certificate from the input stream.
39       * <p/>
40       *
41       * @param in The <code>InputStream</code> array containg the X509 data
42       * @return An X509 certificate
43       * @throws WSSecurityException
44       */
45      X509Certificate loadCertificate(InputStream in) throws WSSecurityException;
46  
47      /***
48       * Construct an array of X509Certificate's from the byte array.
49       * <p/>
50       *
51       * @param data    The <code>byte</code> array containg the X509 data
52       * @param reverse If set the first certificate in input data will
53       *                the last in the array
54       * @return An array of X509 certificates, ordered according to
55       *         the reverse flag
56       * @throws WSSecurityException
57       */
58      X509Certificate[] getX509Certificates(byte[] data, boolean reverse) throws WSSecurityException;
59  
60      /***
61       * get a byte array given an array of X509 certificates.
62       * <p/>
63       *
64       * @param reverse If set the first certificate in the array data will
65       *                the last in the byte array
66       * @param certs   The certificates to convert
67       * @return The byte array for the certficates ordered according
68       *         to the reverse flag
69       * @throws WSSecurityException
70       */
71      byte[] getCertificateData(boolean reverse, X509Certificate[] certs) throws WSSecurityException;
72  
73      /***
74       * Gets the private key identified by <code>alias</> and <code>password</code>.
75       * <p/>
76       *
77       * @param alias    The alias (<code>KeyStore</code>) of the key owner
78       * @param password The password needed to access the private key
79       * @return The private key
80       * @throws Exception
81       */
82      public PrivateKey getPrivateKey(String alias, String password) throws Exception;
83  
84      /***
85       * get the list of certificates for a given alias. This method
86       * reads a new certificate chain and overwrites a previously
87       * stored certificate chain.
88       * <p/>
89       *
90       * @param alias Lookup certificate chain for this alias
91       * @return Array of X509 certificates for this alias name, or
92       *         null if this alias does not exist in the keystore
93       */
94      public X509Certificate[] getCertificates(String alias) throws WSSecurityException;
95  
96      /***
97       * Return a X509 Certificate alias in the keystore according to a given Certificate
98       * <p/>
99       *
100      * @param cert The certificate to lookup
101      * @return alias name of the certificate that matches the given certificate
102      *         or null if no such certificate was found.
103      *         <p/>
104      *         See comment above
105      *         <p/>
106      *         See comment above
107      */
108     /*
109      * See comment above
110      */
111     public String getAliasForX509Cert(Certificate cert) throws WSSecurityException;
112 
113     /***
114      * Lookup a X509 Certificate in the keystore according to a given
115      * the issuer of a Certficate.
116      * <p/>
117      * The search gets all alias names of the keystore and gets the certificate chain
118      * for each alias. Then the Issuer fo each certificate of the chain
119      * is compared with the parameters.
120      *
121      * @param issuer The issuer's name for the certificate
122      * @return alias name of the certificate that matches the issuer name
123      *         or null if no such certificate was found.
124      */
125     public String getAliasForX509Cert(String issuer) throws WSSecurityException;
126 
127     /***
128      * Search a X509 Certificate in the keystore according to a given serial number and
129      * the issuer of a Certficate.
130      * <p/>
131      * The search gets all alias names of the keystore and gets the certificate chain
132      * for each alias. Then the SerialNumber and Issuer fo each certificate of the chain
133      * is compared with the parameters.
134      *
135      * @param issuer       The issuer's name for the certificate
136      * @param serialNumber The serial number of the certificate from the named issuer
137      * @return alias name of the certificate that matches serialNumber and issuer name
138      *         or null if no such certificate was found.
139      */
140     public String getAliasForX509Cert(String issuer, BigInteger serialNumber) throws WSSecurityException;
141 
142     /***
143      * Lookup a X509 Certificate in the keystore according to a given
144      * SubjectKeyIdentifier.
145      * <p/>
146      * The search gets all alias names of the keystore and gets the certificate chain
147      * or certificate for each alias. Then the SKI for each user certificate
148      * is compared with the SKI parameter.
149      *
150      * @param skiBytes The SKI info bytes
151      * @return alias name of the certificate that matches serialNumber and issuer name
152      *         or null if no such certificate was found.
153      */
154     public String getAliasForX509Cert(byte[] skiBytes) throws WSSecurityException;
155 
156     /***
157      * Retrieves the alias name of the default certificate which has been
158      * specified as a property. This should be the certificate that is used for
159      * signature and encryption. This alias corresponds to the certificate that
160      * should be used whenever KeyInfo is not poresent in a signed or
161      * an encrypted message. May return null.
162      *
163      * @return alias name of the default X509 certificate.
164      */
165     public String getDefaultX509Alias();
166 
167     /***
168      * Reads the SubjectKeyIdentifier information from the certificate.
169      * <p/>
170      *
171      * @param cert The certificate to read SKI
172      * @return The byte array conating the binary SKI data
173      */
174     public byte[] getSKIBytesFromCert(X509Certificate cert) throws WSSecurityException;
175 
176     /***
177      * Gets the Keystore that was loaded by the underlying implementation
178      *
179      * @return the Keystore
180      */
181     public KeyStore getKeyStore();
182 
183     /***
184      * Gets the CertificateFactory instantiated by the underlying implementation
185      *
186      * @return the CertificateFactory
187      * @throws WSSecurityException
188      */
189     public CertificateFactory getCertificateFactory() throws WSSecurityException;
190 
191     /***
192      * Uses the CertPath API to validate a given certificate chain
193      * <p/>
194      *
195      * @param certs Certificate chain to validate
196      * @return true if the certificate chain is valid, false otherwise
197      * @throws WSSecurityException
198      */
199     public boolean validateCertPath(X509Certificate[] certs) throws WSSecurityException;
200 
201     /***
202      * Lookup X509 Certificates in the keystore according to a given DN of the subject of the certificate
203      * <p/>
204      *
205      * @param subjectDN The DN of subject to look for in the keystore
206      * @return Vector with all alias of certificates with the same DN as given in the parameters
207      * @throws WSSecurityException
208      */
209     public String[] getAliasesForDN(String subjectDN) throws WSSecurityException;
210 }