01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: */
18:
19: package org.apache.jmeter.util.keystore;
20:
21: import java.io.InputStream;
22: import java.security.KeyStore;
23: import java.security.PrivateKey;
24: import java.security.cert.Certificate;
25: import java.security.cert.X509Certificate;
26: import java.util.Enumeration;
27:
28: /**
29: * Use this Keystore to wrap the normal KeyStore implementation.
30: *
31: * @author <a href="bloritsch@apache.org">Berin Loritsch</a>
32: */
33: public class DefaultKeyStore extends JmeterKeyStore {
34: private X509Certificate[] certChain;
35:
36: private PrivateKey key;
37:
38: private String alias;
39:
40: private final KeyStore store;
41:
42: public DefaultKeyStore(String type) throws Exception {
43: this .store = KeyStore.getInstance(type);
44: }
45:
46: public void load(InputStream is, String pword) throws Exception {
47: store.load(is, pword.toCharArray());
48: PrivateKey _key = null;
49: X509Certificate[] _certChain = null;
50:
51: if (null != is) { // No point checking an empty keystore
52:
53: Enumeration aliases = store.aliases();
54: while (aliases.hasMoreElements()) {
55: this .alias = (String) aliases.nextElement();
56: if (store.isKeyEntry(alias)) {
57: _key = (PrivateKey) store.getKey(alias, pword
58: .toCharArray());
59: Certificate[] chain = store
60: .getCertificateChain(alias);
61: _certChain = new X509Certificate[chain.length];
62:
63: for (int i = 0; i < chain.length; i++) {
64: _certChain[i] = (X509Certificate) chain[i];
65: }
66:
67: break;
68: }
69: }
70:
71: if (null == _key) {
72: throw new Exception("No key found");
73: }
74: if (null == _certChain) {
75: throw new Exception("No certificate chain found");
76: }
77: }
78:
79: this .key = _key;
80: this .certChain = _certChain;
81: }
82:
83: public final X509Certificate[] getCertificateChain() {
84: return this .certChain;
85: }
86:
87: public final PrivateKey getPrivateKey() {
88: return this .key;
89: }
90:
91: public final String getAlias() {
92: return this.alias;
93: }
94: }
|