2010-12-20 3 views
2

서명 된 XML 문서를 확인해야합니다. 확인의 일부로 서명 된 인증서와 함께 전달 된 인증서가 신뢰할 수있는 인증서인지 확인해야합니다.인증서가 키 스토어에 있는지 확인하는 방법

신뢰할 수있는 모든 인증서는 trusted.keystore이라는 키 스토어에 추가됩니다.

전달 된 인증서가 유효한 인증서인지 어떻게 확인할 수 있습니까?

나는 다음과 같은 KeySelector을 쓴 적이 있지만, 문제는 방법 isTrustedCertificate에있다

import java.security.Key; 
import java.security.KeyStore; 
import java.security.KeyStoreException; 
import java.security.PublicKey; 
import java.security.cert.Certificate; 
import java.security.cert.X509Certificate; 
import java.util.Enumeration; 
import java.util.Iterator; 

import javax.xml.crypto.AlgorithmMethod; 
import javax.xml.crypto.KeySelector; 
import javax.xml.crypto.KeySelectorException; 
import javax.xml.crypto.KeySelectorResult; 
import javax.xml.crypto.XMLCryptoContext; 
import javax.xml.crypto.XMLStructure; 
import javax.xml.crypto.dsig.SignatureMethod; 
import javax.xml.crypto.dsig.keyinfo.KeyInfo; 
import javax.xml.crypto.dsig.keyinfo.X509Data; 

import org.apache.commons.logging.Log; 
import org.apache.commons.logging.LogFactory; 

public class X509KeySelector extends KeySelector { 
    private static Log log = LogFactory.getLog(X509KeySelector.class); 

    private KeyStore trustedStore; 

    public void setTrustedStore(KeyStore trustedStore) { 
     this.trustedStore = trustedStore; 
    } 

    @SuppressWarnings("rawtypes") 
    public KeySelectorResult select(KeyInfo keyInfo, 
      KeySelector.Purpose purpose, AlgorithmMethod method, 
      XMLCryptoContext context) throws KeySelectorException { 
     if (log.isDebugEnabled()) { 
      log.debug("Selecting key for algorithm: " + method.getAlgorithm()); 
     } 

     Iterator ki = keyInfo.getContent().iterator(); 
     while (ki.hasNext()) { 
      XMLStructure info = (XMLStructure) ki.next(); 
      if (log.isDebugEnabled()) { 
       log.debug("Found xml structure: " + info.toString()); 
      } 

      if (!(info instanceof X509Data)) { 
       if (log.isTraceEnabled()) { 
        log.trace("Ignoring xml structure since it is not a X509Data."); 
       } 
       continue; 
      } 

      X509Data x509Data = (X509Data) info; 
      Iterator xi = x509Data.getContent().iterator(); 
      if (log.isDebugEnabled()) { 
       log.debug("Iterating X509Data: Size: " 
         + x509Data.getContent().size()); 
      } 

      while (xi.hasNext()) { 
       Object o = xi.next(); 
       if (log.isDebugEnabled()) { 
        log.debug("Found object: " + o); 
       } 

       if (!(o instanceof X509Certificate)) { 
        if (log.isTraceEnabled()) { 
         log.trace("Ignoring object since it is not a X509Certificate"); 
        } 
        continue; 
       } 
       X509Certificate cert = (X509Certificate) o; 
       if (!isTrustedCertificate(cert)) { 
        log.warn("Ignoring certificate since it is not a valid certificate. Certificate: " 
          + cert); 
        continue; 
       } 

       final PublicKey key = cert.getPublicKey(); 

       // Make sure the algorithm is compatible 
       // with the method. 
       if (algEquals(method.getAlgorithm(), key.getAlgorithm())) { 
        KeySelectorResult keySelectorResult = new KeySelectorResult() { 
         public Key getKey() { 
          return key; 
         } 
        }; 
        return keySelectorResult; 
       } else { 
        log.warn("Ignoring certificate since the algorithms " 
          + method.getAlgorithm() + " and " 
          + key.getAlgorithm() + " does not match."); 
       } 
      } 
     } 

     log.error("Unable to find a valid certificate."); 
     throw new KeySelectorException("No key found!"); 
    } 

    private boolean isTrustedCertificate(X509Certificate cert) { 
     if (trustedStore == null) { 
      return true; 
     } 

     boolean trusted = false; 
     try { 
      Enumeration<String> aliases = trustedStore.aliases(); 
      while (aliases.hasMoreElements()) { 
       String alias = aliases.nextElement(); 

       Certificate[] certificates = this.trustedStore 
         .getCertificateChain(alias); 
       if (certificates == null) { 
        Certificate certificate = this.trustedStore 
          .getCertificate(alias); 
        if (certificate != null) { 
         certificates = new Certificate[] { certificate }; 
        } 
       } 

       if (certificates != null) { 
        for (Certificate certificate : certificates) { 
         if (!(certificate instanceof X509Certificate)) { 
          continue; 
         } 

         if (cert.getSignature().equals(
           ((X509Certificate) certificate).getSignature())) { 
          trusted = true; 
          break; 
         } 
        } 
        if (trusted) { 
         break; 
        } 
       } 
      } 
     } catch (KeyStoreException e) { 
      log.error(e.toString(), e); 
     } 
     return trusted; 
    } 

    static boolean algEquals(String algURI, String algName) { 
     if ((algName.equalsIgnoreCase("DSA") && algURI 
       .equalsIgnoreCase(SignatureMethod.DSA_SHA1)) 
       || (algName.equalsIgnoreCase("RSA") && algURI 
         .equalsIgnoreCase(SignatureMethod.RSA_SHA1))) { 
      return true; 
     } else { 
      return false; 
     } 
    } 
} 

을 작동하지 않습니다. 어디에서 키 저장소의 모든 별칭을 반복하고 전달 된 인증서와 동일한 지 확인하십시오.

클래스 이름에서 알 수 있듯이 X509 유형 인증서 만 처리합니다.

내가 java.security.SignatureException: Signature does not match. 예외 인증서가 일치하지 않을 경우 인증서 객체의 확인 (PublicKey의) 방법을 발견, 내가 잘못된 길을가는 생각하는 당신에게

답변

1

감사드립니다.

1

처음에는 사용하기가 어려울 수있는 방법을 사용하여이를 확인하는 간단한 방법이 있습니다. KeyStore 클래스에는 getCertificateAlias ​​(Certificate cert)라는 메서드가 있습니다. 확인하려는 인증서를 전달하고 null 반환을받지 못하면 해당 인증서는 KeyStore에 있습니다. 이 같은

시도 뭔가 :

private boolean isTrustedCertificate(X509Certificate cert) { 

    if (trustedStore == null) { 
     return true; 
    } 

    boolean trusted = false; 
    try { 

    if (cert != null) { 

     // Only returns null if cert is NOT in keystore. 
     String alias = trustedStore.getCertificateAlias(cert); 

     if (alias != null) { 
      trusted = true; 
     } 
    } 
    } catch (KeyStoreException e) { 
     log.error(e.toString(), e); 
    } 

    return trusted; 
} 
관련 문제