DEB packaging of PZ Signer from ePUAP (including Java 9+ fixes)
Jacek Kowalski
2019-09-25 207ea2ca32736f2d08d48410bc9ec6246c8c1c13
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
package com.pentacomp.signer;
 
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.KeyStoreSpi;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.Provider;
import java.security.Security;
import java.security.UnrecoverableKeyException;
import java.security.cert.X509Certificate;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Map;
import java.util.TreeMap;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.auth.x500.X500Principal;
 
import org.eclipse.swt.widgets.Shell;
 
public class KeyStoreManager {
    private KeyStore keyStore;
    private Map<String, String> aliasMap;
    private static Boolean java9SunPKCS11;
 
    public static KeyStoreManager initialize(Shell shell) throws GeneralSecurityException {
        KeyStoreManager ksm = new KeyStoreManager();
        ksm.init(shell);
        return ksm;
    }
 
    private void init(Shell shell) throws GeneralSecurityException {
        try {
            if (isWindows()) {
                this.keyStore = KeyStore.getInstance("Windows-MY");
                this.keyStore.load(null, null);
                fixAliases();
            } else {
                this.keyStore = process(shell);
            }
 
            this.aliasMap = new TreeMap();
            Enumeration<String> aliases = this.keyStore.aliases();
            while (aliases.hasMoreElements()) {
                String alias = (String) aliases.nextElement();
                System.out.println("! " + alias + ": " + this.keyStore.isKeyEntry(alias) + " / " + this.keyStore.isCertificateEntry(alias));
                X509Certificate cert = (X509Certificate) this.keyStore.getCertificate(alias);
                if (cert != null) {
                    this.aliasMap.put(extractName(cert.getSubjectX500Principal()) + " (" + extractName(cert.getIssuerX500Principal()) + " / " + cert.getSerialNumber().toString(16) + ")", alias);
                }
            }
        } catch (Exception e) {
            throw new GeneralSecurityException("Błąd podczas ładowania keystore'a", e);
        }
    }
 
    private KeyStore process(Shell shell) throws Exception {
        String libpath = System.getProperty("pz.signer.pkcs11.libpath");
        long[] slots = listSlots(libpath);
        for (int i = slots.length - 1; i >= 0; i--) {
            System.out.println("!!! slot " + slots[i]);
            try {
                Provider provider;
                do {
                    String cfg = "library=" + libpath + "\nname=XaDESAppletCrypto\nslot=" + slots[i] + "\n";
                    provider = createPkcs11Provider(new ByteArrayInputStream(cfg.getBytes()));
                } while (provider.getService("KeyStore", "PKCS11") == null);
                if (Security.getProvider(provider.getName()) != null) {
                    Security.removeProvider(provider.getName());
                }
                Security.addProvider(provider);
                KeyStore.Builder keystoreBuilder = KeyStore.Builder.newInstance("PKCS11", provider, new KeyStore.CallbackHandlerProtection(new Kallback(shell)));
                return keystoreBuilder.getKeyStore();
            } catch (Exception e) {
                e.printStackTrace();
 
                if (!e.getMessage().contains("CKR_TOKEN_NOT_RECOGNIZED")) {
                    throw e;
                }
            }
        }
 
        return null;
    }
 
    private class Kallback implements CallbackHandler {
        private char[] pin;
        private Shell shell;
 
        public Kallback(Shell shell) {
            this.shell = shell;
        }
 
        public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
            for (Callback callback : callbacks) {
                if (callback instanceof PasswordCallback) {
                    System.out.println("!!! callback... pin.length='" + ((this.pin != null) ? Integer.valueOf(this.pin.length) : null) + "'");
                    PasswordCallback pc = (PasswordCallback) callback;
                    if (this.pin == null || this.pin.length == 0) {
                        PinDialog dlg = new PinDialog(this.shell);
                        dlg.open();
                        if (dlg.getPin() != null) {
                            this.pin = dlg.getPin();
                            pc.setPassword(this.pin);
                        }
                    }
                } else {
                    throw new UnsupportedCallbackException(callback, "Unrecognized Callback");
                }
            }
        }
    }
 
    private void fixAliases() {
        try {
            Field field = this.keyStore.getClass().getDeclaredField("keyStoreSpi");
            field.setAccessible(true);
            KeyStoreSpi keyStoreVeritable = (KeyStoreSpi) field.get(this.keyStore);
            if ("sun.security.mscapi.KeyStore$MY".equals(keyStoreVeritable.getClass().getName())) {
                field = keyStoreVeritable.getClass().getEnclosingClass().getDeclaredField("entries");
                field.setAccessible(true);
                Collection entries = (Collection) field.get(keyStoreVeritable);
                for (Object entry : entries) {
                    field = entry.getClass().getDeclaredField("certChain");
                    field.setAccessible(true);
                    X509Certificate[] certificates = (X509Certificate[]) field.get(entry);
                    String hashCode = Integer.toString(certificates[0].hashCode(), 16);
                    field = entry.getClass().getDeclaredField("alias");
                    field.setAccessible(true);
                    String alias = (String) field.get(entry);
                    if (!alias.equals(hashCode)) {
                        field.set(entry, alias.concat("-").concat(hashCode));
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    private static boolean isWindows() {
        return System.getProperty("os.name").toUpperCase().contains("WINDOWS");
    }
 
    private static long[] listSlots(String path) throws Exception {
        Method[] methods = Class.forName("sun.security.pkcs11.wrapper.PKCS11").getMethods();
        for (Method method : methods) {
            if (method.getName().equals("getInstance")) {
                Object o = method.invoke(null, new Object[]{path, "C_GetFunctionList", null, Boolean.valueOf(false)});
                Method m = o.getClass().getMethod("C_GetSlotList", new Class[]{boolean.class});
                if (!m.isAccessible()) {
                    m.setAccessible(true);
                }
                return (long[]) m.invoke(o, new Object[]{Boolean.valueOf(true)});
            }
        }
        return new long[0];
    }
 
    private static boolean isJava9SunPKCS11() {
        if (java9SunPKCS11 != null) {
            return java9SunPKCS11;
        }
        java9SunPKCS11 = Boolean.FALSE;
        try {
            Provider provider = Security.getProvider("SunPKCS11");
            if (provider != null) {
                provider.getClass().getMethod("configure", String.class);
                java9SunPKCS11 = Boolean.TRUE;
            }
        } catch (NoSuchMethodException ignore) {
        }
        return java9SunPKCS11;
    }
 
    private static Provider createPkcs11ProviderJava9(InputStream configStream) throws Exception {
        Provider provider = Security.getProvider("SunPKCS11");
        Method configure = provider.getClass().getMethod("configure", String.class);
        File configFile = File.createTempFile("pkcs11", ".cfg");
        configFile.deleteOnExit();
        Files.copy(configStream, configFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
        return (Provider) configure.invoke(provider, configFile.getAbsolutePath());
    }
 
    private static Provider createPkcs11ProviderJava8(InputStream configStream) throws Exception {
        Class providerClass = Class.forName("sun.security.pkcs11.SunPKCS11");
        Constructor<Provider> ctor = providerClass.getConstructor(new Class[]{InputStream.class});
        return (Provider) ctor.newInstance(new Object[]{configStream});
    }
 
    private static Provider createPkcs11Provider(InputStream configStream) throws Exception {
        if (isJava9SunPKCS11()) {
            return createPkcs11ProviderJava9(configStream);
        } else {
            return createPkcs11ProviderJava8(configStream);
        }
    }
 
    private static String extractName(X500Principal p) {
        String[] parts = p.getName().split(",");
        for (String part : parts) {
            String[] el = part.split("=");
            if ("cn".equals(el[0].trim().toLowerCase())) {
                return el[1].trim().replaceAll("\\\\00", "");
            }
        }
        return p.getName().replaceAll("\\\\00", "");
    }
 
    public Collection<String> getAliasLabels() {
        return this.aliasMap.keySet();
    }
 
    public String getAlias(String text) {
        return (String) this.aliasMap.get(text);
    }
 
    public PrivateKey getPrivateKey(String alias) throws UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException {
        return (PrivateKey) this.keyStore.getKey(alias, null);
    }
 
    public X509Certificate getCertificate(String alias) throws KeyStoreException {
        return (X509Certificate) this.keyStore.getCertificate(alias);
    }
}