view rt/net/sourceforge/jnlp/security/HttpsCertVerifier.java @ 1319:d95ddc227d01

2009-01-20 Lillian Angel <langel@redhat.com> * rt/net/sourceforge/jnlp/DefaultLaunchHandler.java: Removed debug lines. * rt/net/sourceforge/jnlp/runtime/JNLPClassLoader.java: Likewise. * rt/net/sourceforge/jnlp/security/AccessWarningPane.java: Updated imports. * rt/net/sourceforge/jnlp/security/AppletWarningPane.java: Updated imports. * rt/net/sourceforge/jnlp/security/CertWarningPane.java: Updated imports, added certVerifier global variable. (CertWarningPane): Initialized certVerifier. (installComponents): Added checks to determine if certificate is for an https site, and set the name/publisher/from variables appropriately. Also, customized warning pane label for https site. * rt/net/sourceforge/jnlp/security/HttpsCertVerifier.java: (getDetails): Implemented. (addToDetails): Likewise. (R): Likewise. (getPublisher): Likewise. (getRoot): Likewise. (getRootInCacerts): Likewise. (hasSigningIssues): Likewise. (noSigningIssues): Likewise. * rt/net/sourceforge/jnlp/security/MoreInfoPane.java: Fixed imports. * rt/net/sourceforge/jnlp/security/SecurityDialogUI.java: Fixed imports. * rt/net/sourceforge/jnlp/security/SecurityWarningDialog.java: Fixed imports. * rt/net/sourceforge/jnlp/security/SingleCertInfoPane.java: Fixed imports. * rt/net/sourceforge/jnlp/security/viewer/CertificatePane.java: Fixed imports. * rt/net/sourceforge/jnlp/tools/KeyTool.java: Removed debug lines. * rt/net/sourceforge/jnlp/security/CertVerifier.java: Moved file below, here. * rt/net/sourceforge/jnlp/tools/CertVerifier.java: Removed. * rt/net/sourceforge/jnlp/security/VariableX509TrustManager.java: Removed debug lines.
author Lillian Angel <langel@redhat.com>
date Tue, 20 Jan 2009 18:37:40 -0500
parents c63cc0eff68b
children 09264346d5ca
line wrap: on
line source

/* VariableX509TrustManager.java
   Copyright (C) 2009 Red Hat, Inc.

This file is part of IcedTea.

IcedTea is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 2.

IcedTea 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
General Public License for more details.

You should have received a copy of the GNU General Public License
along with IcedTea; see the file COPYING.  If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version.
*/

package net.sourceforge.jnlp.security;

import java.security.cert.CertPath;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.CertificateNotYetValidException;
import java.security.cert.CertificateExpiredException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;

import net.sourceforge.jnlp.runtime.JNLPRuntime;
import net.sourceforge.jnlp.tools.KeyTool;
 
public class HttpsCertVerifier implements CertVerifier {

    private VariableX509TrustManager tm;
    private X509Certificate[] chain;
    private String authType;
    private ArrayList<String> details = new ArrayList<String>();
    
    public HttpsCertVerifier(VariableX509TrustManager tm, X509Certificate[] chain, String authType) {
        this.tm = tm;
        this.chain = chain;
        this.authType = authType;
    }

    public boolean getAlreadyTrustPublisher() {
        try {
            tm.checkServerTrusted(chain, authType, true);
            return true;
        } catch (CertificateException ce) {
            return false;
        }
    }

    public ArrayList<CertPath> getCerts() {
        
        ArrayList<X509Certificate> list = new ArrayList<X509Certificate>();
        for (int i=0; i < chain.length; i++)
            list.add(chain[i]);

        ArrayList<CertPath> certPaths = new ArrayList<CertPath>();
        
        try {
            certPaths.add(CertificateFactory.getInstance("X.509").generateCertPath(list));
        } catch (CertificateException ce) {
            ce.printStackTrace();
            
            // carry on
        }

        return certPaths; 
    }

    public ArrayList<String> getDetails() {
	boolean hasExpiredCert=false; 
	boolean hasExpiringCert=false;
	boolean notYetValidCert=false;
	boolean isUntrusted=false; 

	if (! getAlreadyTrustPublisher())
              isUntrusted = true;

	for (int i=0; i < chain.length; i++)
	{
	   X509Certificate cert = chain[i];	

           long now = System.currentTimeMillis();
           long SIX_MONTHS = 180*24*60*60*1000L;
	   long notAfter = cert.getNotAfter().getTime();
           if (notAfter < now) {
             hasExpiredCert = true;
           } else if (notAfter < now + SIX_MONTHS) {
             hasExpiringCert = true;
           }
	
	   try {
	     cert.checkValidity();
	   } catch (CertificateNotYetValidException cnyve) {
             notYetValidCert = true;
	   } catch (CertificateExpiredException cee) {
	     hasExpiredCert = true;
	   }
	}

	if (isUntrusted || hasExpiredCert || hasExpiringCert || notYetValidCert) {
	      if (isUntrusted)
	        addToDetails(R("SUntrustedCertificate"));
              if (hasExpiredCert)
                addToDetails(R("SHasExpiredCert"));
              if (hasExpiringCert)
                addToDetails(R("SHasExpiringCert"));
              if (notYetValidCert)
                addToDetails(R("SNotYetValidCert"));
        }
	return details;
    }

    private void addToDetails(String detail) {
      if (!details.contains(detail))
        details.add(detail);
    }

    private static String R(String key) {
      return JNLPRuntime.getMessage(key);
    }

    public Certificate getPublisher() {
      if (chain.length > 0)
        return (Certificate)chain[0];
      return null;
    }

    public Certificate getRoot() {
      if (chain.length > 0) 
        return (Certificate)chain[chain.length - 1];
      return null;
    }

    public boolean getRootInCacerts() {
	try {
	  KeyTool kt = new KeyTool();
          return kt.checkCacertsForCertificate(getRoot());
        } catch (Exception e) {
	}
	return false;
    }

    public boolean hasSigningIssues() {
        return false;
    }

    public boolean noSigningIssues() {
        return false;
    }

}