1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package io.earcam.utilitarian.security;
20
21 import java.security.KeyStore;
22 import java.security.PrivateKey;
23 import java.security.PublicKey;
24 import java.security.cert.Certificate;
25 import java.util.Objects;
26
27 import javax.security.auth.Destroyable;
28
29 import io.earcam.unexceptional.Exceptional;
30
31 public class OpenedKeyStore implements Destroyable, AutoCloseable {
32
33 private KeyStore store;
34 private KeyPairCredential credential;
35
36
37 public OpenedKeyStore(KeyStore store, KeyPairCredential credential)
38 {
39 Objects.requireNonNull(store, "store");
40 this.store = store;
41 this.credential = credential;
42 }
43
44
45 public OpenedKeyStore(KeyStore store, String alias, char[] password)
46 {
47 this(store, new KeyPairCredential(KeyStores.keyPair(store, alias, password), alias, password));
48 }
49
50
51 @Override
52 public void close()
53 {
54 destroy();
55 }
56
57
58 @Override
59 public void destroy()
60 {
61 store = null;
62 credential.destroy();
63 }
64
65
66 @Override
67 public boolean isDestroyed()
68 {
69 return store == null;
70 }
71
72
73 public KeyStore store()
74 {
75 return store;
76 }
77
78
79 public PublicKey publicKey()
80 {
81 return credential.publicKey();
82 }
83
84
85 public PrivateKey privateKey()
86 {
87 return credential.privateKey();
88 }
89
90
91 public Certificate[] getCertificateChain()
92 {
93 return Exceptional.apply(store::getCertificateChain, credential.name());
94 }
95
96
97 public Certificate getCertificate()
98 {
99 return Exceptional.apply(store::getCertificate, credential.name());
100 }
101 }