aboutsummaryrefslogtreecommitdiffstats
path: root/security-utils/src/main/java/com/yahoo/security/MutableX509KeyManager.java
blob: 4b90f8bca145a423029f3effb24c28826833fb8f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.security;

import javax.net.ssl.SSLEngine;
import javax.net.ssl.X509ExtendedKeyManager;
import java.net.Socket;
import java.security.KeyStore;
import java.security.Principal;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;
import java.util.WeakHashMap;

/**
 * A {@link X509ExtendedKeyManager} which can be updated with new certificate chain and private key while in use.
 *
 * The implementations assumes that aliases are retrieved from the same thread as the certificate chain and private key.
 * This is case for OpenJDK 11.
 *
 * @author bjorncs
 */
public class MutableX509KeyManager extends X509ExtendedKeyManager {

    private final Object monitor = new Object();
    // Not using ThreadLocal as we want the thread local x509 key manager instances to be garbage collected
    // when either the thread dies or the MutableX509KeyManager instance is collected (latter not the case for ThreadLocal).
    private final WeakHashMap<Thread, X509ExtendedKeyManager> threadLocalManager = new WeakHashMap<>();
    private X509ExtendedKeyManager currentManager;

    public MutableX509KeyManager(KeyStore keystore, char[] password) {
        synchronized (monitor) {
            this.currentManager = KeyManagerUtils.createDefaultX509KeyManager(keystore, password);
        }
    }

    public MutableX509KeyManager() {
        synchronized (monitor) {
            this.currentManager = KeyManagerUtils.createDefaultX509KeyManager();
        }
    }

    public void updateKeystore(KeyStore keystore, char[] password) {
        synchronized (monitor) {
            this.currentManager = KeyManagerUtils.createDefaultX509KeyManager(keystore, password);
        }
    }

    public void useDefaultKeystore() {
        synchronized (monitor) {
            this.currentManager = KeyManagerUtils.createDefaultX509KeyManager();
        }
    }

    public X509ExtendedKeyManager currentManager() {
        synchronized (monitor) {
            return currentManager;
        }
    }

    @Override
    public String[] getServerAliases(String keyType, Principal[] issuers) {
        return updateAndGetThreadLocalManager()
                .getServerAliases(keyType, issuers);
    }

    @Override
    public String[] getClientAliases(String keyType, Principal[] issuers) {
        return updateAndGetThreadLocalManager()
                .getClientAliases(keyType, issuers);
    }

    @Override
    public String chooseServerAlias(String keyType, Principal[] issuers, Socket socket) {
        return updateAndGetThreadLocalManager()
                .chooseServerAlias(keyType, issuers, socket);
    }

    @Override
    public String chooseClientAlias(String[] keyType, Principal[] issuers, Socket socket) {
        return updateAndGetThreadLocalManager()
                .chooseClientAlias(keyType, issuers, socket);
    }

    @Override
    public String chooseEngineServerAlias(String keyType, Principal[] issuers, SSLEngine engine) {
        return updateAndGetThreadLocalManager()
                .chooseEngineServerAlias(keyType, issuers, engine);
    }

    @Override
    public String chooseEngineClientAlias(String[] keyType, Principal[] issuers, SSLEngine engine) {
        return updateAndGetThreadLocalManager()
                .chooseEngineClientAlias(keyType, issuers, engine);
    }

    private X509ExtendedKeyManager updateAndGetThreadLocalManager() {
        synchronized (monitor) {
            X509ExtendedKeyManager currentManager = this.currentManager;
            threadLocalManager.put(Thread.currentThread(), currentManager);
            return currentManager;
        }
    }

    @Override
    public X509Certificate[] getCertificateChain(String alias) {
        if (alias == null) return null; // this method can be called with 'null' alias prior to any alias getter methods.
        return getThreadLocalManager()
                .getCertificateChain(alias);
    }

    @Override
    public PrivateKey getPrivateKey(String alias) {
        if (alias == null) return null; // this method can be called with 'null' alias prior to any alias getter methods.
        return getThreadLocalManager()
                .getPrivateKey(alias);
    }

    private X509ExtendedKeyManager getThreadLocalManager() {
        synchronized (monitor) {
            X509ExtendedKeyManager manager = threadLocalManager.get(Thread.currentThread());
            if (manager == null) {
                throw new IllegalStateException("Methods to retrieve valid aliases has not been called previously from this thread");
            }
            return manager;
        }
    }
}