aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_http_service/src/main/java/com/yahoo/jdisc/http/ssl/pem/PemKeyStore.java
blob: b52e923662f9c9b05574944db7f7538e7aee4241 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.http.ssl.pem;

import com.google.common.base.Preconditions;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.cert.X509CertificateHolder;
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openssl.PEMException;
import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;

import javax.annotation.concurrent.GuardedBy;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.Key;
import java.security.KeyStore;
import java.security.KeyStore.LoadStoreParameter;
import java.security.KeyStoreException;
import java.security.KeyStoreSpi;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Consumer;

import static com.yahoo.jdisc.http.server.jetty.Exceptions.throwUnchecked;

/**
 * Exposes keys and certificates from unencrypted PEM keystore.
 *
 * @author Tony Vaagenes
 * @author bjorncs
 */
public class PemKeyStore extends KeyStoreSpi {

    private static String KEY_ALIAS = "KEY";

    static List<String> aliases = Collections.emptyList();
    static Map<String, String> attributes = Collections.emptyMap();
    private static final BouncyCastleProvider bouncyCastleProvider = new BouncyCastleProvider();

    @GuardedBy("this")
    private StoreRole storeRole;
    @GuardedBy("this")
    private Key privateKey;
    @GuardedBy("this")
    private final Map<String, Certificate> aliasToCertificate = new LinkedHashMap<>();

    /**
     * The user is responsible for closing any readers given in the parameter.
     */
    @Override
    public synchronized void engineLoad(LoadStoreParameter parameter) throws IOException {
        if (storeRole != null)
            throw new IllegalStateException("Already initialized.");

        if (parameter instanceof KeyStoreLoadParameter) {
            storeRole = new KeyStoreRole();
            loadKeyStore((KeyStoreLoadParameter) parameter);
        } else if (parameter instanceof TrustStoreLoadParameter) {
            storeRole = new TrustStoreRole();
            loadTrustStore((TrustStoreLoadParameter) parameter);
        } else {
            throw new IllegalArgumentException("Expected key store or trust store load parameter, got " + parameter.getClass());
        }
    }

    private void loadTrustStore(TrustStoreLoadParameter parameter) throws IOException {
        withPemParser(parameter.certificateReader, this::loadCertificates);
    }

    private void loadKeyStore(KeyStoreLoadParameter parameter) throws IOException{
        withPemParser(parameter.keyReader, this::loadPrivateKey);
        withPemParser(parameter.certificateReader,  this::loadCertificates);
    }

    private static void withPemParser(ReaderForPath reader, Consumer<PEMParser> f) throws IOException {
        try {
            //parser.close() will close the underlying reader,
            //which we want to avoid.
            //See engineLoad comment.
            PEMParser parser = new PEMParser(reader.reader);
            f.accept(parser);
        } catch (Exception e) {
            throw new RuntimeException("Failed loading pem key store " + reader.path, e);
        }
    }

    private void loadPrivateKey(PEMParser parser) {
        try {
            Object object = parser.readObject();
            PrivateKeyInfo privateKeyInfo;
            if (object instanceof PEMKeyPair) { // Legacy PKCS1
                privateKeyInfo = ((PEMKeyPair) object).getPrivateKeyInfo();
            } else if (object instanceof PrivateKeyInfo) { // PKCS8
                privateKeyInfo = (PrivateKeyInfo) object;
            } else {
                throw new UnsupportedOperationException(
                        "Expected " + PrivateKeyInfo.class + " or " + PEMKeyPair.class + ", got " + object.getClass());
            }

            Object nextObject = parser.readObject();
            if (nextObject != null) {
                throw new UnsupportedOperationException(
                        "Expected a single private key, but found a second element " + nextObject.getClass());
            }

            setPrivateKey(privateKeyInfo);
        } catch (Exception e) {
            throw throwUnchecked(e);
        }
    }

    private synchronized void setPrivateKey(PrivateKeyInfo privateKey) throws PEMException {
        JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider(bouncyCastleProvider);
        this.privateKey = converter.getPrivateKey(privateKey);
    }

    private void loadCertificates(PEMParser parser) {
        try {
            Object pemObject;
            while ((pemObject = parser.readObject()) != null) {
                addCertificate(pemObject);
            }

            if (aliasToCertificate.isEmpty())
                throw new RuntimeException("No certificates available");
        } catch (Exception e) {
            throw throwUnchecked(e);
        }
    }

    private synchronized void addCertificate(Object pemObject) throws CertificateException {
        if (pemObject instanceof X509CertificateHolder) {
            JcaX509CertificateConverter converter = new JcaX509CertificateConverter().setProvider(bouncyCastleProvider);
            String alias = "cert-" + aliasToCertificate.size();
            aliasToCertificate.put(alias, converter.getCertificate((X509CertificateHolder) pemObject));
        } else {
            throw new UnsupportedOperationException("Expected X509 certificate, got " + pemObject.getClass());
        }
    }

    @Override
    public synchronized Enumeration<String> engineAliases() {
        return Collections.enumeration(storeRole.engineAliases());

    }

    @Override
    public synchronized boolean engineIsKeyEntry(String alias) {
        return KEY_ALIAS.equals(alias);
    }

    @Override
    public synchronized Key engineGetKey(String alias, char[] password) throws NoSuchAlgorithmException, UnrecoverableKeyException {
        Preconditions.checkArgument(KEY_ALIAS.equals(alias));
        return privateKey;
    }

    @Override
    public synchronized boolean engineIsCertificateEntry(String alias) {
        return aliasToCertificate.containsKey(alias);
    }


    @Override
    public synchronized Certificate engineGetCertificate(String alias) {
        return aliasToCertificate.get(alias);
    }

    @Override
    public synchronized Certificate[] engineGetCertificateChain(String alias) {
        Preconditions.checkArgument(KEY_ALIAS.equals(alias));
        return aliasToCertificate.values().toArray(new Certificate[aliasToCertificate.size()]);
    }


    @Override
    public synchronized boolean engineContainsAlias(String alias) {
        return storeRole.engineContainsAlias(alias);
    }

    @Override
    public synchronized int engineSize() {
        return storeRole.engineSize();
    }

    @Override
    public synchronized String engineGetCertificateAlias(final Certificate certificate) {
        for (Entry<String, Certificate> entry : aliasToCertificate.entrySet()) {
            if (entry.getValue() == certificate)
                return entry.getKey();
        }

        return null;
    }

    @Override
    public synchronized Date engineGetCreationDate(String alias) {
        throw new UnsupportedOperationException();
    }

    @Override
    public synchronized void engineSetKeyEntry(String alias, Key key, char[] password, Certificate[] chain) throws KeyStoreException {
        throw new UnsupportedOperationException();
    }

    @Override
    public synchronized void engineSetKeyEntry(String alias, byte[] key, Certificate[] chain) throws KeyStoreException {
        throw new UnsupportedOperationException();
    }

    @Override
    public synchronized void engineSetCertificateEntry(String alias, Certificate cert) throws KeyStoreException {
        throw new UnsupportedOperationException();
    }

    @Override
    public synchronized void engineDeleteEntry(String alias) throws KeyStoreException {
        throw new UnsupportedOperationException();
    }


    @Override
    public synchronized void engineStore(OutputStream stream, char[] password) throws IOException, NoSuchAlgorithmException, CertificateException {
        throw new UnsupportedOperationException();
    }

    @Override
    public synchronized void engineLoad(InputStream stream, char[] password) throws IOException, NoSuchAlgorithmException, CertificateException {
        throw new UnsupportedOperationException();
    }

    private interface StoreRole {
        Collection<String> engineAliases();
        boolean engineContainsAlias(String alias);
        int engineSize();
    }

    private class KeyStoreRole implements StoreRole {
        @Override
        public Collection<String> engineAliases() {
            return Collections.singletonList(KEY_ALIAS);
        }

        @Override
        public boolean engineContainsAlias(String alias) {
            return KEY_ALIAS.equals(alias);
        }

        @Override
        public int engineSize() {
            return 1;
        }
    }

    private class TrustStoreRole implements StoreRole{
        @Override
        public Collection<String> engineAliases() {
            return aliasToCertificate.keySet();
        }

        @Override
        public boolean engineContainsAlias(String alias) {
            return aliasToCertificate.containsKey(alias);
        }

        @Override
        public int engineSize() {
            return aliasToCertificate.size();
        }
    }

    // A reader along with the path used to construct it.
    private static class ReaderForPath {
        final Reader reader;
        final Path path;

        private ReaderForPath(Reader reader, Path path) {
            this.reader = reader;
            this.path = path;
        }

        static ReaderForPath of(Path path) {
            try {
                return new ReaderForPath(Files.newBufferedReader(path), path);
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }
    }

    static class TrustStoreLoadParameter implements KeyStore.LoadStoreParameter {
        final ReaderForPath certificateReader;

        TrustStoreLoadParameter(Path certificateReader) {
            this.certificateReader = ReaderForPath.of(certificateReader);
        }

        @Override
        public KeyStore.ProtectionParameter getProtectionParameter() {
            return null;
        }
    }

    static class KeyStoreLoadParameter implements KeyStore.LoadStoreParameter {
        final ReaderForPath certificateReader;
        final ReaderForPath keyReader;

        KeyStoreLoadParameter(Path certificateReader, Path keyReader) {
            this.certificateReader = ReaderForPath.of(certificateReader);
            this.keyReader = ReaderForPath.of(keyReader);
        }

        @Override
        public KeyStore.ProtectionParameter getProtectionParameter() {
            return null;
        }
    }

}