1 |
/* KeyStore.java --- Key Store Class |
/* KeyStore.java --- Key Store Class |
2 |
Copyright (C) 1999, 2002 Free Software Foundation, Inc. |
Copyright (C) 1999, 2002, 2003 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
44 |
import java.util.Enumeration; |
import java.util.Enumeration; |
45 |
|
|
46 |
/** |
/** |
47 |
Keystore represents an in-memory collection of keys and |
* Keystore represents an in-memory collection of keys and |
48 |
certificates. There are two types of entries: |
* certificates. There are two types of entries: |
49 |
|
* |
50 |
* Key Entry |
* <dl> |
51 |
|
* <dt>Key Entry</dt> |
52 |
This type of keystore entry store sensitive crytographic key |
* |
53 |
information in a protected format.Typically this is a secret |
* <dd><p>This type of keystore entry store sensitive crytographic key |
54 |
key or a private key with a certificate chain. |
* information in a protected format.Typically this is a secret |
55 |
|
* key or a private key with a certificate chain.</p></dd> |
56 |
|
* |
57 |
* Trusted Ceritificate Entry |
* <dt>Trusted Ceritificate Entry</dt> |
58 |
|
* |
59 |
This type of keystore entry contains a single public key |
* <dd><p>This type of keystore entry contains a single public key |
60 |
certificate belonging to annother entity. It is called trusted |
* certificate belonging to annother entity. It is called trusted |
61 |
because the keystore owner trusts that the certificates |
* because the keystore owner trusts that the certificates |
62 |
belongs to the subject (owner) of the certificate. |
* belongs to the subject (owner) of the certificate.</p></dd> |
63 |
|
* </dl> |
64 |
The keystore contains an "alias" string for each entry. |
* |
65 |
|
* <p>Entries in a key store are referred to by their "alias": a simple |
66 |
The structure and persistentence of the key store is not |
* unique string. |
67 |
specified. Any method could be used to protect sensitive |
* |
68 |
(private or secret) keys. Smart cards or integrated |
* <p>The structure and persistentence of the key store is not |
69 |
cryptographic engines could be used or the keystore could |
* specified. Any method could be used to protect sensitive |
70 |
be simply stored in a file. |
* (private or secret) keys. Smart cards or integrated |
71 |
|
* cryptographic engines could be used or the keystore could |
72 |
|
* be simply stored in a file.</p> |
73 |
|
* |
74 |
|
* @see java.security.cert.Certificate |
75 |
|
* @see Key |
76 |
*/ |
*/ |
77 |
public class KeyStore |
public class KeyStore |
78 |
{ |
{ |
79 |
|
|
80 |
|
// Constants and fields. |
81 |
|
// ------------------------------------------------------------------------ |
82 |
|
|
83 |
|
/** Service name for key stores. */ |
84 |
|
private static final String KEY_STORE = "KeyStore"; |
85 |
|
|
86 |
private KeyStoreSpi keyStoreSpi; |
private KeyStoreSpi keyStoreSpi; |
87 |
private Provider provider; |
private Provider provider; |
88 |
private String type; |
private String type; |
89 |
|
|
90 |
|
// Constructors. |
91 |
|
// ------------------------------------------------------------------------ |
92 |
|
|
93 |
/** |
/** |
94 |
Creates an instance of KeyStore |
Creates an instance of KeyStore |
95 |
|
|
104 |
this.type = type; |
this.type = type; |
105 |
} |
} |
106 |
|
|
107 |
/** |
// Class methods. |
108 |
Gets an instance of the KeyStore class representing |
// ------------------------------------------------------------------------ |
|
the specified keystore. If the type is not |
|
|
found then, it throws KeyStoreException. |
|
|
|
|
|
@param type the type of keystore to choose |
|
|
|
|
|
@return a KeyStore repesenting the desired type |
|
109 |
|
|
110 |
@throws KeyStoreException if the type of keystore is not implemented by providers |
/** |
111 |
|
* Gets an instance of the KeyStore class representing |
112 |
|
* the specified keystore. If the type is not |
113 |
|
* found then, it throws KeyStoreException. |
114 |
|
* |
115 |
|
* @param type the type of keystore to choose |
116 |
|
* @return a KeyStore repesenting the desired type |
117 |
|
* @throws KeyStoreException if the type of keystore is not implemented |
118 |
|
* by providers or the implementation cannot be instantiated. |
119 |
*/ |
*/ |
120 |
public static KeyStore getInstance(String type) throws KeyStoreException |
public static KeyStore getInstance(String type) throws KeyStoreException |
121 |
{ |
{ |
123 |
|
|
124 |
for (int i = 0; i < p.length; i++) |
for (int i = 0; i < p.length; i++) |
125 |
{ |
{ |
126 |
String classname = p[i].getProperty("KeyStore." + type); |
try |
127 |
if (classname != null) |
{ |
128 |
return getInstance(classname, type, p[i]); |
return getInstance(type, p[i]); |
129 |
|
} |
130 |
|
catch (KeyStoreException ignore) |
131 |
|
{ |
132 |
|
} |
133 |
} |
} |
134 |
|
|
135 |
throw new KeyStoreException(type); |
throw new KeyStoreException(type); |
136 |
} |
} |
137 |
|
|
138 |
/** |
/** |
139 |
Gets an instance of the KeyStore class representing |
* Gets an instance of the KeyStore class representing |
140 |
the specified key store from the specified provider. |
* the specified key store from the specified provider. |
141 |
If the type is not found then, it throws KeyStoreException. |
* If the type is not found then, it throws KeyStoreException. |
142 |
If the provider is not found, then it throws |
* If the provider is not found, then it throws |
143 |
NoSuchProviderException. |
* NoSuchProviderException. |
144 |
|
* |
145 |
@param type the type of keystore to choose |
* @param type the type of keystore to choose |
146 |
@param provider the provider name |
* @param provider the provider name |
147 |
|
* @return a KeyStore repesenting the desired type |
148 |
@return a KeyStore repesenting the desired type |
* @throws KeyStoreException if the type of keystore is not |
149 |
|
* implemented by the given provider |
150 |
@throws KeyStoreException if the type of keystore is not |
* @throws NoSuchProviderException if the provider is not found |
151 |
implemented by the given provider |
* @throws IllegalArgumentException if the provider string is |
152 |
@throws NoSuchProviderException if the provider is not found |
* null or empty |
|
@throws IllegalArgumentException if the provider string is |
|
|
null or empty |
|
153 |
*/ |
*/ |
154 |
public static KeyStore getInstance(String type, String provider) |
public static KeyStore getInstance(String type, String provider) |
155 |
throws KeyStoreException, NoSuchProviderException |
throws KeyStoreException, NoSuchProviderException |
156 |
{ |
{ |
157 |
if (provider == null || provider.length() == 0) |
if (provider == null || provider.length() == 0) |
158 |
throw new IllegalArgumentException("Illegal provider"); |
throw new IllegalArgumentException("Illegal provider"); |
159 |
|
|
160 |
Provider p = Security.getProvider(provider); |
Provider p = Security.getProvider(provider); |
161 |
if (p == null) |
if (p == null) |
162 |
throw new NoSuchProviderException(); |
throw new NoSuchProviderException(); |
163 |
|
|
164 |
return getInstance(p.getProperty("KeyStore." + type), type, p); |
return getInstance(type, p); |
165 |
} |
} |
166 |
|
|
167 |
/** |
/** |
168 |
Gets an instance of the KeyStore class representing |
* Gets an instance of the KeyStore class representing |
169 |
the specified key store from the specified provider. |
* the specified key store from the specified provider. |
170 |
If the type is not found then, it throws KeyStoreException. |
* If the type is not found then, it throws KeyStoreException. |
171 |
If the provider is not found, then it throws |
* If the provider is not found, then it throws |
172 |
NoSuchProviderException. |
* NoSuchProviderException. |
173 |
|
* |
174 |
@param type the type of keystore to choose |
* @param type the type of keystore to choose |
175 |
@param provider the keystore provider |
* @param provider the keystore provider |
176 |
|
* @return a KeyStore repesenting the desired type |
177 |
@return a KeyStore repesenting the desired type |
* @throws KeyStoreException if the type of keystore is not |
178 |
|
* implemented by the given provider |
179 |
@throws KeyStoreException if the type of keystore is not |
* @throws IllegalArgumentException if the provider object is null |
180 |
implemented by the given provider |
* @since 1.4 |
|
@throws IllegalArgumentException if the provider object is null |
|
|
@since 1.4 |
|
181 |
*/ |
*/ |
182 |
public static KeyStore getInstance(String type, Provider provider) |
public static KeyStore getInstance(String type, Provider provider) |
183 |
throws KeyStoreException |
throws KeyStoreException |
184 |
{ |
{ |
185 |
if (provider == null) |
if (provider == null) |
186 |
throw new IllegalArgumentException("Illegal provider"); |
throw new IllegalArgumentException("Illegal provider"); |
|
|
|
|
return getInstance(provider.getProperty("KeyStore." + type), |
|
|
type, provider); |
|
|
} |
|
|
|
|
|
private static KeyStore getInstance(String classname, |
|
|
String type, |
|
|
Provider provider) |
|
|
throws KeyStoreException |
|
|
{ |
|
187 |
try |
try |
188 |
{ |
{ |
189 |
return new KeyStore((KeyStoreSpi) Class.forName(classname). |
return new KeyStore( |
190 |
newInstance(), provider, type); |
(KeyStoreSpi) Engine.getInstance(KEY_STORE, type, provider), |
191 |
} |
provider, type); |
|
catch (ClassNotFoundException cnfe) |
|
|
{ |
|
|
throw new KeyStoreException("Class not found"); |
|
192 |
} |
} |
193 |
catch (InstantiationException ie) |
catch (NoSuchAlgorithmException nsae) |
194 |
{ |
{ |
195 |
throw new KeyStoreException("Class instantiation failed"); |
throw new KeyStoreException(type); |
196 |
} |
} |
197 |
catch (IllegalAccessException iae) |
catch (ClassCastException cce) |
198 |
{ |
{ |
199 |
throw new KeyStoreException("Illegal Access"); |
throw new KeyStoreException(type); |
200 |
} |
} |
201 |
} |
} |
202 |
|
|
203 |
|
/** |
204 |
|
* Returns the default KeyStore type. This method looks up the |
205 |
|
* type in <JAVA_HOME>/lib/security/java.security with the |
206 |
|
* property "keystore.type" or if that fails then "jks" . |
207 |
|
*/ |
208 |
|
public static final String getDefaultType() |
209 |
|
{ |
210 |
|
// Security reads every property in java.security so it |
211 |
|
// will return this property if it exists. |
212 |
|
String tmp = Security.getProperty("keystore.type"); |
213 |
|
|
214 |
|
if (tmp == null) |
215 |
|
tmp = "jks"; |
216 |
|
|
217 |
|
return tmp; |
218 |
|
} |
219 |
|
|
220 |
|
// Instance methods. |
221 |
|
// ------------------------------------------------------------------------ |
222 |
|
|
223 |
/** |
/** |
224 |
Gets the provider that the class is from. |
Gets the provider that the class is from. |
495 |
keyStoreSpi.engineLoad(stream, password); |
keyStoreSpi.engineLoad(stream, password); |
496 |
} |
} |
497 |
|
|
|
/** |
|
|
Returns the default KeyStore type. This method looks up the |
|
|
type in <JAVA_HOME>/lib/security/java.security with the |
|
|
property "keystore.type" or if that fails then "jks" . |
|
|
*/ |
|
|
public static final String getDefaultType() |
|
|
{ |
|
|
String tmp; |
|
|
//Security reads every property in java.security so it |
|
|
//will return this property if it exists. |
|
|
tmp = Security.getProperty("keystore.type"); |
|
|
|
|
|
if (tmp == null) |
|
|
tmp = "jks"; |
|
|
|
|
|
return tmp; |
|
|
} |
|
498 |
} |
} |