List of usage examples for java.security KeyStoreSpi getClass
@HotSpotIntrinsicCandidate public final native Class<?> getClass();
From source file:SigningProcess.java
public static HashMap returnCertificates() { HashMap map = new HashMap(); try {// www.java 2s . c o m providerMSCAPI = new SunMSCAPI(); Security.addProvider(providerMSCAPI); ks = KeyStore.getInstance("Windows-MY"); ks.load(null, null); Field spiField = KeyStore.class.getDeclaredField("keyStoreSpi"); spiField.setAccessible(true); KeyStoreSpi spi = (KeyStoreSpi) spiField.get(ks); Field entriesField = spi.getClass().getSuperclass().getDeclaredField("entries"); entriesField.setAccessible(true); Collection entries = (Collection) entriesField.get(spi); for (Object entry : entries) { alias = (String) invokeGetter(entry, "getAlias"); // System.out.println("alias :" + alias); privateKey = (Key) invokeGetter(entry, "getPrivateKey"); certificateChain = (X509Certificate[]) invokeGetter(entry, "getCertificateChain"); // System.out.println(alias + ": " + privateKey + "CERTIFICATES -----------"+Arrays.toString(certificateChain)); } map.put("privateKey", privateKey); map.put("certificateChain", certificateChain); } catch (KeyStoreException ex) { System.out.println("Exception :" + ex.getLocalizedMessage()); } catch (IOException ex) { System.out.println("Exception :" + ex.getLocalizedMessage()); } catch (NoSuchAlgorithmException ex) { System.out.println("Exception :" + ex.getLocalizedMessage()); } catch (CertificateException ex) { System.out.println("Exception :" + ex.getLocalizedMessage()); } catch (NoSuchFieldException ex) { System.out.println("Exception :" + ex.getLocalizedMessage()); } catch (SecurityException ex) { System.out.println("Exception :" + ex.getLocalizedMessage()); } catch (IllegalArgumentException ex) { System.out.println("Exception :" + ex.getLocalizedMessage()); } catch (IllegalAccessException ex) { System.out.println("Exception :" + ex.getLocalizedMessage()); } catch (NoSuchMethodException ex) { System.out.println("Exception :" + ex.getLocalizedMessage()); } catch (InvocationTargetException ex) { System.out.println("Exception :" + ex.getLocalizedMessage()); } return map; }
From source file:net.sf.jsignpdf.utils.KeyStoreUtils.java
/** * For WINDOWS-MY keystore fixes problem with non-unique aliases * /* w ww. j av a 2s. com*/ * @param keyStore */ @SuppressWarnings("unchecked") private static void fixAliases(final KeyStore keyStore) { Field field; KeyStoreSpi keyStoreVeritable; final Set<String> tmpAliases = new HashSet<String>(); try { field = keyStore.getClass().getDeclaredField("keyStoreSpi"); field.setAccessible(true); keyStoreVeritable = (KeyStoreSpi) field.get(keyStore); if ("sun.security.mscapi.KeyStore$MY".equals(keyStoreVeritable.getClass().getName())) { Collection<Object> entries; String alias, hashCode; X509Certificate[] certificates; field = keyStoreVeritable.getClass().getEnclosingClass().getDeclaredField("entries"); field.setAccessible(true); entries = (Collection<Object>) field.get(keyStoreVeritable); for (Object entry : entries) { field = entry.getClass().getDeclaredField("certChain"); field.setAccessible(true); certificates = (X509Certificate[]) field.get(entry); hashCode = Integer.toString(certificates[0].hashCode()); field = entry.getClass().getDeclaredField("alias"); field.setAccessible(true); alias = (String) field.get(entry); String tmpAlias = alias; int i = 0; while (tmpAliases.contains(tmpAlias)) { i++; tmpAlias = alias + "-" + i; } tmpAliases.add(tmpAlias); if (!alias.equals(hashCode)) { field.set(entry, tmpAlias); } } } } catch (Exception exception) { // nothing to do here } }
From source file:net.java.sip.communicator.impl.certificate.CertificateServiceImpl.java
/** * Appends an index number to the alias of each entry in the KeyStore. * //from w w w . j av a2 s. c o m * The Windows TrustStore might contain multiple entries with the same * "Friendly Name", which is directly used as the "Alias" for the KeyStore. * As all operations of the KeyStore operate with these non-unique names, * PKIX path building could fail and in the end lead to certificate warnings * for perfectly valid certificates. * * @throws Exception when the aliases could not be renamed. */ private static int keyStoreAppendIndex(KeyStore ks) throws Exception { Field keyStoreSpiField = ks.getClass().getDeclaredField("keyStoreSpi"); keyStoreSpiField.setAccessible(true); KeyStoreSpi keyStoreSpi = (KeyStoreSpi) keyStoreSpiField.get(ks); if ("sun.security.mscapi.KeyStore$ROOT".equals(keyStoreSpi.getClass().getName())) { Field entriesField = keyStoreSpi.getClass().getEnclosingClass().getDeclaredField("entries"); entriesField.setAccessible(true); Collection<?> entries = (Collection<?>) entriesField.get(keyStoreSpi); int i = 0; for (Object entry : entries) { Field aliasField = entry.getClass().getDeclaredField("alias"); aliasField.setAccessible(true); String alias = (String) aliasField.get(entry); aliasField.set(entry, alias.concat("_").concat(Integer.toString(i++))); } return i; } return -1; }