KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > soap > handlers > security > StandaloneCrypto


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.soap.handlers.security;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.security.GeneralSecurityException JavaDoc;
22 import java.security.Key JavaDoc;
23 import java.security.KeyStore JavaDoc;
24 import java.security.KeyStoreException JavaDoc;
25 import java.security.PrivateKey JavaDoc;
26 import java.security.cert.Certificate JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Set JavaDoc;
32
33 import org.apache.ws.security.components.crypto.CredentialException;
34 import org.springframework.core.io.Resource;
35
36 public class StandaloneCrypto extends BaseCrypto {
37
38     private Resource keyStoreUrl;
39     private String JavaDoc keyStoreType;
40     private String JavaDoc keyStorePassword;
41     private KeyStore JavaDoc keyStore;
42     private String JavaDoc keyPassword;
43     
44     /**
45      * @return the keyPassword
46      */

47     public String JavaDoc getKeyPassword() {
48         return keyPassword;
49     }
50
51     /**
52      * @param keyPassword the keyPassword to set
53      */

54     public void setKeyPassword(String JavaDoc keyPassword) {
55         this.keyPassword = keyPassword;
56     }
57
58     /**
59      * @return the keyStorePassword
60      */

61     public String JavaDoc getKeyStorePassword() {
62         return keyStorePassword;
63     }
64
65     /**
66      * @param keyStorePassword the keyStorePassword to set
67      */

68     public void setKeyStorePassword(String JavaDoc keyStorePassword) {
69         this.keyStorePassword = keyStorePassword;
70     }
71
72     /**
73      * @return the keyStoreType
74      */

75     public String JavaDoc getKeyStoreType() {
76         return keyStoreType;
77     }
78
79     /**
80      * @param keyStoreType the keyStoreType to set
81      */

82     public void setKeyStoreType(String JavaDoc keyStoreType) {
83         this.keyStoreType = keyStoreType;
84     }
85
86     /**
87      * @return the keyStoreUrl
88      */

89     public Resource getKeyStoreUrl() {
90         return keyStoreUrl;
91     }
92
93     /**
94      * @param keyStoreUrl the keyStoreUrl to set
95      */

96     public void setKeyStoreUrl(Resource keyStoreUrl) {
97         this.keyStoreUrl = keyStoreUrl;
98     }
99
100     protected String JavaDoc[] getAliases() throws KeyStoreException JavaDoc {
101         List JavaDoc aliases = Collections.list(loadKeyStore().aliases());
102         return (String JavaDoc[]) aliases.toArray(new String JavaDoc[aliases.size()]);
103     }
104
105     protected Certificate JavaDoc getCertificate(String JavaDoc alias) throws KeyStoreException JavaDoc {
106         return loadKeyStore().getCertificate(alias);
107     }
108
109     protected String JavaDoc getCertificateAlias(Certificate JavaDoc cert) throws KeyStoreException JavaDoc {
110         return loadKeyStore().getCertificateAlias(cert);
111     }
112
113     protected Certificate JavaDoc[] getCertificateChain(String JavaDoc alias) throws KeyStoreException JavaDoc {
114         return loadKeyStore().getCertificateChain(alias);
115     }
116
117     public PrivateKey JavaDoc getPrivateKey(String JavaDoc alias, String JavaDoc password) throws Exception JavaDoc {
118         // The password given here is a dummy password
119
// See WSSecurityHandler.DefaultHandler#processSignature
120
password = keyPassword;
121         if (password == null) {
122             password = keyStorePassword;
123         }
124         if (alias == null) {
125             throw new Exception JavaDoc("alias is null");
126         }
127         KeyStore JavaDoc keystore = loadKeyStore();
128         boolean b = keystore.isKeyEntry(alias);
129         if (!b) {
130             throw new Exception JavaDoc("Cannot find key for alias: " + alias);
131         }
132         Key JavaDoc keyTmp = keystore.getKey(alias, (password == null || password.length() == 0) ? new char[0] : password.toCharArray());
133         if (!(keyTmp instanceof PrivateKey JavaDoc)) {
134             throw new Exception JavaDoc("Key is not a private key, alias: " + alias);
135         }
136         return (PrivateKey JavaDoc) keyTmp;
137     }
138
139     protected String JavaDoc[] getTrustCertificates() throws KeyStoreException JavaDoc {
140         KeyStore JavaDoc keystore = loadKeyStore();
141         Set JavaDoc hashSet = new HashSet JavaDoc();
142         Enumeration JavaDoc aliases = keystore.aliases();
143         while (aliases.hasMoreElements()) {
144             String JavaDoc alias = (String JavaDoc) aliases.nextElement();
145             if (keystore.isCertificateEntry(alias)) {
146                 hashSet.add(alias);
147             }
148         }
149         return (String JavaDoc[]) hashSet.toArray(new String JavaDoc[hashSet.size()]);
150     }
151     
152     /**
153      * Loads the the keystore.
154      *
155      * @throws CredentialException
156      */

157     public synchronized KeyStore JavaDoc loadKeyStore() throws KeyStoreException JavaDoc {
158         if (keyStore != null) {
159             return keyStore;
160         }
161         if (keyStoreUrl == null) {
162             throw new IllegalArgumentException JavaDoc("keyStoreUrl not specified in this StandaloneCrypto");
163         }
164         InputStream JavaDoc input = null;
165         try {
166             input = keyStoreUrl.getInputStream();
167             String JavaDoc provider = getProvider();
168             String JavaDoc type = keyStoreType != null ? keyStoreType : KeyStore.getDefaultType();
169             if (provider == null || provider.length() == 0) {
170                 keyStore = KeyStore.getInstance(type);
171             } else {
172                 keyStore = KeyStore.getInstance(type, provider);
173             }
174             keyStore.load(input, (keyStorePassword == null || keyStorePassword.length() == 0) ? new char[0] : keyStorePassword.toCharArray());
175             return keyStore;
176         } catch (IOException JavaDoc e) {
177             throw new KeyStoreException JavaDoc(e);
178         } catch (GeneralSecurityException JavaDoc e) {
179             throw new KeyStoreException JavaDoc(e);
180         } catch (Exception JavaDoc e) {
181             throw new KeyStoreException JavaDoc(e);
182         } finally {
183             if (input != null) {
184                 try { input.close(); } catch (Exception JavaDoc ignore) {}
185             }
186         }
187     }
188
189 }
190
Popular Tags