package com.baulsupp.oksocial;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import sun.security.pkcs11.SunPKCS11;

public class OpenSCUtil {
  public static KeyManager[] getKeyManagers(char[] password)
      throws KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException,
      UnrecoverableKeyException {
    String config = "name=OpenSC\nlibrary=/Library/OpenSC/lib/opensc-pkcs11.so\n";
    SunPKCS11 pkcs11 =
        new SunPKCS11(new ByteArrayInputStream(config.getBytes(Charset.forName("UTF-8"))));
    Security.addProvider(pkcs11);

    KeyStore keystore = KeyStore.getInstance("PKCS11", pkcs11);
    keystore.load(null, password);

    KeyManagerFactory kmf = KeyManagerFactory.getInstance("NewSunX509");
    kmf.init(keystore, null);

    return kmf.getKeyManagers();
  }
}

@@ -78,26 +78,26 @@
 ava.io.I
-OException
+nputStream
 ;%0Aimport
@@ -106,58 +106,74 @@
 ava.
-nio.charset.Charset;%0Aimport java.security.KeyStore
+lang.reflect.Constructor;%0Aimport java.nio.charset.StandardCharsets
 ;%0Aim
@@ -199,25 +199,16 @@
 KeyStore
-Exception
 ;%0Aimport
@@ -226,159 +226,47 @@
 ity.
-NoSuchAlgorithmException;%0Aimport java.security.Security;%0Aimport java.security.UnrecoverableKeyException;%0Aimport java.security.cert.CertificateException
+Provider;%0Aimport java.security.Security
 ;%0Aim
@@ -339,46 +339,8 @@
 ory;
-%0Aimport sun.security.pkcs11.SunPKCS11;
 %0A%0Apu
@@ -427,22 +427,16 @@
 ord)
-%0A     
  throws 
 KeyS
@@ -435,250 +435,290 @@
 ows 
-KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException,%0A      UnrecoverableKeyException %7B%0A    String config = %22name=OpenSC%5Cnlibrary=/Library/OpenSC/lib/opensc-pkcs11.so%5Cn%22;%0A    SunPKCS11 pkcs11 =%0A        new SunPKCS11(
+Exception %7B%0A    String config = %22name=OpenSC%5Cnlibrary=/Library/OpenSC/lib/opensc-pkcs11.so%5Cn%22;%0A    Constructor%3C?%3E ctor =%0A        Class.forName(%22sun.security.pkcs11.SunPKCS11%22).getConstructor(InputStream.class);%0A    Provider pkcs11 =%0A        (Provider) ctor.newInstance(%0A            
 new 
@@ -758,32 +758,30 @@
 tes(
-Charset.forName(%22UTF-8%22)
+StandardCharsets.UTF_8
 )));

remove hard dependency on internal sun class