io.rubrica.keystore.KeyStoreUtilities Maven / Gradle / Ivy
/*
* Copyright 2009-2017 Rubrica
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see .
*/
package io.rubrica.keystore;
import java.lang.reflect.Field;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.KeyStoreSpi;
import java.security.cert.CertificateExpiredException;
import java.security.cert.CertificateNotYetValidException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;
import io.rubrica.util.CertificateUtils;
/**
* Tratamos los alias repetidos, situacion problematica afectada por el bug
* http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6483657 Este solo se da
* con SunMSCAPI
*
* @author Ricardo Arguello
*/
public class KeyStoreUtilities {
private static final Logger logger = Logger.getLogger(KeyStoreUtilities.class.getName());
public static boolean tieneAliasRepetidos(KeyStore keyStore) {
try {
ArrayList aliases = Collections.list(keyStore.aliases());
HashSet uniqAliases = new HashSet(aliases);
return (aliases.size() > uniqAliases.size());
} catch (KeyStoreException e) {
throw new IllegalStateException(e);
}
}
/**
* For WINDOWS-MY keystore fixes problem with non-unique aliases
*
* @param keyStore
*/
@SuppressWarnings("unchecked")
public static void fixAliases(final KeyStore keyStore) {
Field field;
KeyStoreSpi keyStoreVeritable;
final Set tmpAliases = new HashSet();
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
© 2015 - 2025 Weber Informatics LLC | Privacy Policy