View | Details | Raw Unified | Return to bug 52033
Collapse All | Expand All

(-)src/core/org/apache/jmeter/util/SSLManager.java (-3 / +15 lines)
Lines 75-80 Link Here
75
    protected String defaultpw = System.getProperty(KEY_STORE_PASSWORD);
75
    protected String defaultpw = System.getProperty(KEY_STORE_PASSWORD);
76
76
77
    /**
77
    /**
78
     * Load jks on startup jmeter
79
     */
80
    static {
81
    	 JmeterKeyStore key = getInstance().getKeyStore();
82
    	 log.info("Start keystore. Total: " + key.getAliases().length + " aliase(s)");
83
    }
84
    
85
    /**
78
     * Resets the SSLManager so that we can create a new one with a new keystore
86
     * Resets the SSLManager so that we can create a new one with a new keystore
79
     */
87
     */
80
    public static synchronized void reset() {
88
    public static synchronized void reset() {
Lines 97-103 Link Here
97
     * not set, this method will prompt you to enter it. Unfortunately, there is
105
     * not set, this method will prompt you to enter it. Unfortunately, there is
98
     * no PasswordEntryField available from JOptionPane.
106
     * no PasswordEntryField available from JOptionPane.
99
     */
107
     */
100
    protected JmeterKeyStore getKeyStore() {
108
    protected synchronized JmeterKeyStore getKeyStore() {
101
        if (null == this.keyStore) {
109
        if (null == this.keyStore) {
102
            String defaultName = JMeterUtils.getJMeterProperties()
110
            String defaultName = JMeterUtils.getJMeterProperties()
103
                .getProperty("user.home")  // $NON-NLS-1$
111
                .getProperty("user.home")  // $NON-NLS-1$
Lines 124-132 Link Here
124
                File initStore = new File(fileName);
132
                File initStore = new File(fileName);
125
133
126
                if (initStore.exists()) {
134
                if (initStore.exists()) {
127
                    fileInputStream = new FileInputStream(initStore);
135
                	fileInputStream = new FileInputStream(initStore);
136
        			
128
                    this.keyStore.load(fileInputStream, getPassword());
137
                    this.keyStore.load(fileInputStream, getPassword());
129
                    log.info("Keystore loaded OK from file, found alias: "+keyStore.getAlias());
138
			
139
                    String[] names = keyStore.getAliases();			
140
                    
141
                    log.info("Total of " + names.length + " aliases loaded OK from keystore");
130
                } else {
142
                } else {
131
                    log.warn("Keystore file not found, loading empty keystore");
143
                    log.warn("Keystore file not found, loading empty keystore");
132
                    this.defaultpw = ""; // Ensure not null
144
                    this.defaultpw = ""; // Ensure not null
(-)src/core/org/apache/jmeter/util/JsseSSLManager.java (-15 / +60 lines)
Lines 70-75 Link Here
70
70
71
    private static final int cps;
71
    private static final int cps;
72
72
73
	private static int last_user = 0;
74
	private static String[] names = null;
75
	private static X509Certificate[][] chains = null;
76
	private static PrivateKey[] keys = null;
77
    
73
    static {
78
    static {
74
        log.info("Using default SSL protocol: "+DEFAULT_SSL_PROTOCOL);
79
        log.info("Using default SSL protocol: "+DEFAULT_SSL_PROTOCOL);
75
        log.info("SSL session context: "+(SHARED_SESSION_CONTEXT ? "shared" : "per-thread"));
80
        log.info("SSL session context: "+(SHARED_SESSION_CONTEXT ? "shared" : "per-thread"));
Lines 154-161 Link Here
154
 * So we leave it to the defaults set up in the SSL Context
159
 * So we leave it to the defaults set up in the SSL Context
155
 *
160
 *
156
 */
161
 */
157
//          HttpsURLConnection secureConn = (HttpsURLConnection) conn;
162
			try {
158
//          secureConn.setSSLSocketFactory(this.getContext().getSocketFactory());
163
				HttpsURLConnection secureConn = (HttpsURLConnection) conn;
164
				secureConn.setSSLSocketFactory(this.getContext()
165
						.getSocketFactory());
166
			} catch (Exception e) {
167
				log.error("Error setting ssl context.");
168
			}
159
        } else {
169
        } else {
160
            log.warn("Unexpected HttpURLConnection class: "+conn.getClass().getName());
170
            log.warn("Unexpected HttpURLConnection class: "+conn.getClass().getName());
161
        }
171
        }
Lines 313-321 Link Here
313
         * @return the ClientAliases value
323
         * @return the ClientAliases value
314
         */
324
         */
315
        public String[] getClientAliases(String keyType, Principal[] issuers) {
325
        public String[] getClientAliases(String keyType, Principal[] issuers) {
316
            log.debug("WrappedX509Manager: getClientAliases: ");
326
			if (names == null)
317
            log.debug(this.store.getAlias());
327
				names = this.store.getAliases();
318
            return new String[] { this.store.getAlias() };
328
329
			for (int i = 0; i < names.length; i++)
330
				log.debug(names[i]);
331
332
			return names;
319
        }
333
        }
320
334
321
        /**
335
        /**
Lines 342-349 Link Here
342
         * @return The CertificateChain value
356
         * @return The CertificateChain value
343
         */
357
         */
344
        public X509Certificate[] getCertificateChain(String alias) {
358
        public X509Certificate[] getCertificateChain(String alias) {
345
            log.debug("WrappedX509Manager: getCertificateChain(" + alias + ")");
359
			log.info("WrappedX509Manager: getCertificateChain(" + alias + ")");
346
            return this.store.getCertificateChain();
360
361
			if (chains == null)
362
				chains = this.store.getCertificateChains();
363
364
			if (names == null)
365
				names = this.store.getAliases();
366
367
			for (int i = 0; i < names.length; i++) {
368
				if (names[i] == alias)
369
					return chains[i];
370
			}
371
372
			return null;
347
        }
373
        }
348
374
349
        /**
375
        /**
Lines 354-361 Link Here
354
         * @return The PrivateKey value
380
         * @return The PrivateKey value
355
         */
381
         */
356
        public PrivateKey getPrivateKey(String alias) {
382
        public PrivateKey getPrivateKey(String alias) {
357
            log.debug("WrappedX509Manager: getPrivateKey: " + this.store.getPrivateKey());
383
			if (keys == null)
358
            return this.store.getPrivateKey();
384
				keys = this.store.getPrivateKeys();
385
386
			if (names == null)
387
				names = this.store.getAliases();
388
389
			for (int i = 0; i < names.length; i++) {
390
				if (names[i] == alias) {
391
					log.debug("WrappedX509Manager: getPrivateKey: " + keys[i]);
392
					return keys[i];
393
				}
394
			}
395
396
			return null;
359
        }
397
        }
360
398
361
        /**
399
        /**
Lines 372-383 Link Here
372
         * @see javax.net.ssl.X509KeyManager#chooseClientAlias(String[], Principal[], Socket)
410
         * @see javax.net.ssl.X509KeyManager#chooseClientAlias(String[], Principal[], Socket)
373
         */
411
         */
374
        public String chooseClientAlias(String[] keyType, Principal[] issuers, Socket socket) {
412
        public String chooseClientAlias(String[] keyType, Principal[] issuers, Socket socket) {
375
            String alias = this.store.getAlias();
413
			if (names == null)	
376
            log.debug("ClientAlias: " + alias);
414
				names = this.store.getAliases();
377
            if (alias == null || alias.length() == 0) {
415
378
                log.debug("ClientAlias not found.");
416
			log.debug("keyType: " + keyType[0]);
379
            }
417
380
            return alias;
418
			if (last_user >= names.length)
419
				last_user = 0;
420
421
			log.debug("ClientAlias: " + names[last_user]);
422
			if (names[last_user] == null || names[last_user].length() == 0)
423
				log.debug("ClientAlias not found.");
424
425
			return names[last_user++];
381
        }
426
        }
382
427
383
        /**
428
        /**
(-)src/core/org/apache/jmeter/util/keystore/JmeterKeyStore.java (-3 / +3 lines)
Lines 36-49 Link Here
36
    /**
36
    /**
37
     * Get the ordered certificate chain.
37
     * Get the ordered certificate chain.
38
     */
38
     */
39
    public abstract X509Certificate[] getCertificateChain();
39
    public abstract X509Certificate[][] getCertificateChains();
40
40
41
    public abstract String getAlias();
41
    public abstract String[] getAliases();
42
42
43
    /**
43
    /**
44
     * Return the private Key
44
     * Return the private Key
45
     */
45
     */
46
    public abstract PrivateKey getPrivateKey();
46
    public abstract PrivateKey[] getPrivateKeys();
47
47
48
    public static final JmeterKeyStore getInstance(String type) throws Exception {
48
    public static final JmeterKeyStore getInstance(String type) throws Exception {
49
        // JAVA 1.4 now handles all keystore types, so just use default
49
        // JAVA 1.4 now handles all keystore types, so just use default
(-)src/core/org/apache/jmeter/util/keystore/DefaultKeyStore.java (-23 / +76 lines)
Lines 23-28 Link Here
23
import java.security.PrivateKey;
23
import java.security.PrivateKey;
24
import java.security.cert.Certificate;
24
import java.security.cert.Certificate;
25
import java.security.cert.X509Certificate;
25
import java.security.cert.X509Certificate;
26
import java.util.ArrayList;
26
import java.util.Enumeration;
27
import java.util.Enumeration;
27
28
28
/**
29
/**
Lines 30-70 Link Here
30
 *
31
 *
31
 */
32
 */
32
public class DefaultKeyStore extends JmeterKeyStore {
33
public class DefaultKeyStore extends JmeterKeyStore {
33
    private X509Certificate[] certChain;
34
	private X509Certificate[][] certChains;
35
	@SuppressWarnings("rawtypes")
36
	private ArrayList v_certChains = new ArrayList();
34
37
35
    private PrivateKey key;
38
	private PrivateKey[] keys;
39
	@SuppressWarnings("rawtypes")
40
	private ArrayList v_keys = new ArrayList();
36
41
37
    private String alias;
42
	private String[] names;
43
	@SuppressWarnings("rawtypes")
44
	private ArrayList v_names = new ArrayList();
38
45
39
    private final KeyStore store;
46
    private final KeyStore store;
40
47
48
	private static final String KEY_STORE_START_INDEX = "javax.net.ssl.keyStoreStartIndex"; // $NON-NLS-1$
49
	private static final String KEY_STORE_END_INDEX = "javax.net.ssl.keyStoreEndIndex"; // $NON-NLS-1$
50
	protected int startIndex = 0;
51
	protected int endIndex = 0;
52
	
41
    public DefaultKeyStore(String type) throws Exception {
53
    public DefaultKeyStore(String type) throws Exception {
42
        this.store = KeyStore.getInstance(type);
54
        this.store = KeyStore.getInstance(type);
55
        
56
		try {
57
			startIndex = Integer.parseInt(System
58
					.getProperty(KEY_STORE_START_INDEX));
59
			endIndex = Integer
60
					.parseInt(System.getProperty(KEY_STORE_END_INDEX));
61
		} catch (Exception e) {
62
		}
43
    }
63
    }
44
64
45
    /** {@inheritDoc} */
65
    /** {@inheritDoc} */
46
    @Override
66
    @Override
67
    @SuppressWarnings({ "unchecked", "unused" })
47
    public void load(InputStream is, String pword) throws Exception {
68
    public void load(InputStream is, String pword) throws Exception {
48
        store.load(is, pword.toCharArray());
69
		store.load(is, pword.toCharArray());
49
        PrivateKey _key = null;
70
		PrivateKey _key = null;
50
        X509Certificate[] _certChain = null;
71
		X509Certificate[] _certChain = null;
72
		String cur_name = null;
51
73
52
        if (null != is){ // No point checking an empty keystore
74
        if (null != is){ // No point checking an empty keystore
53
75
76
        	int index = 0;
77
        	
54
            Enumeration<String> aliases = store.aliases();
78
            Enumeration<String> aliases = store.aliases();
55
            while (aliases.hasMoreElements()) {
79
            while (aliases.hasMoreElements()) {
56
                this.alias = aliases.nextElement();
80
            	
57
                if (store.isKeyEntry(alias)) {
81
            	cur_name = (String) aliases.nextElement();
58
                    _key = (PrivateKey) store.getKey(alias, pword.toCharArray());
82
                
59
                    Certificate[] chain = store.getCertificateChain(alias);
83
            	if (store.isKeyEntry(cur_name)) {
60
                    _certChain = new X509Certificate[chain.length];
84
                	
85
                	if ((index >= startIndex && index < endIndex) || (startIndex == 0 && endIndex == 0)) {
86
                	
87
						_key = (PrivateKey) store.getKey(cur_name,
88
								pword.toCharArray());
89
						Certificate[] chain = store
90
								.getCertificateChain(cur_name);
91
						_certChain = new X509Certificate[chain.length];
92
93
						for (int i = 0; i < chain.length; i++) {
94
							_certChain[i] = (X509Certificate) chain[i];
95
						}
96
97
						if (null == _key) {
98
							throw new Exception("No key found");
99
						}
100
101
						if (null == _certChain) {
102
							throw new Exception("No certificate chain found");
103
						}
61
104
62
                    for (int i = 0; i < chain.length; i++) {
105
						this.v_names.add(cur_name);
63
                        _certChain[i] = (X509Certificate) chain[i];
106
						this.v_keys.add(_key);
64
                    }
107
						this.v_certChains.add(_certChain);
65
108
66
                    break;
109
                	}
67
                }
110
                }
111
                index++;
68
            }
112
            }
69
113
70
            if (null == _key) {
114
            if (null == _key) {
Lines 75-99 Link Here
75
            }
119
            }
76
        }
120
        }
77
121
78
        this.key = _key;
122
		int v_size = this.v_names.size();
79
        this.certChain = _certChain;
123
124
		this.names = new String[v_size];
125
		this.names = (String[]) v_names.toArray(names);
126
127
		this.keys = new PrivateKey[v_size];
128
		this.keys = (PrivateKey[]) v_keys.toArray(keys);
129
130
		this.certChains = new X509Certificate[v_size][];
131
		this.certChains = (X509Certificate[][]) v_certChains
132
				.toArray(certChains);
80
    }
133
    }
81
134
82
    /** {@inheritDoc} */
135
    /** {@inheritDoc} */
83
    @Override
136
    @Override
84
    public final X509Certificate[] getCertificateChain() {
137
    public final X509Certificate[][] getCertificateChains() {
85
        return this.certChain;
138
        return this.certChains;
86
    }
139
    }
87
140
88
    /** {@inheritDoc} */
141
    /** {@inheritDoc} */
89
    @Override
142
    @Override
90
    public final PrivateKey getPrivateKey() {
143
    public final PrivateKey[] getPrivateKeys() {
91
        return this.key;
144
        return this.keys;
92
    }
145
    }
93
146
94
    /** {@inheritDoc} */
147
    /** {@inheritDoc} */
95
    @Override
148
    @Override
96
    public final String getAlias() {
149
    public final String[] getAliases() {
97
        return this.alias;
150
        return this.names;
98
    }
151
    }
99
}
152
}
100
153

Return to bug 52033