/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at
* trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
* add the following below this CDDL HEADER, with the fields enclosed
* by brackets "[]" replaced with your own identifying information:
* Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
*
* Copyright 2008-2009 Sun Microsystems, Inc.
* Portions Copyright 2009 Parametric Technology Corporation (PTC)
* Portions Copyright 2011 ForgeRock AS
*/
/**
* This class is in charge of checking whether the certificates that are
* presented are trusted or not.
* This implementation tries to check also that the subject DN of the
* certificate corresponds to the host passed using the setHostName method.
*
* The constructor tries to use a default TrustManager from the system and if
* it cannot be retrieved this class will only accept the certificates
* explicitly accepted by the user (and specified by calling acceptCertificate).
*
* NOTE: this class is not aimed to be used when we have connections in
* parallel.
*/
{
/**
* The enumeration for the different causes for which the trust manager can
* refuse to accept a certificate.
*/
public enum Cause
{
/**
* The certificate was not trusted.
*/
/**
* The certificate's subject DN's value and the host name we tried to
* connect to do not match.
*/
}
/*
* The following ArrayList contain information about the certificates
* explicitly accepted by the user.
*/
new ArrayList<X509Certificate[]>();
/**
* The default constructor.
*
* @param keystore The keystore to use for this trustmanager.
*/
{
//provider.
userSpecifiedAlgo = "IbmX509";
userSpecifiedProvider = "IBMJSSE2";
// Have some fallbacks to choose the provider and algorith of the key
// manager. First see if the user wanted to use something specific,
// then try with the SunJSSE provider and SunX509 algorithm. Finally,
// fallback to the default algorithm of the JVM.
{
"SunJSSE",
null,
};
String[] preferredAlgo =
{
"SunX509",
"SunX509",
};
{
{
continue;
}
try
{
{
}
else
{
}
{
if (trustManagers[j] instanceof X509TrustManager)
{
break;
}
}
}
catch (NoSuchProviderException e)
{
}
catch (NoSuchAlgorithmException e)
{
}
catch (KeyStoreException e)
{
}
}
}
/**
* {@inheritDoc}
*/
throws CertificateException
{
boolean explicitlyAccepted = false;
try
{
if (trustManager != null)
{
try
{
}
catch (CertificateException ce)
{
explicitlyAccepted = true;
}
}
else
{
explicitlyAccepted = true;
}
}
catch (CertificateException ce)
{
chain);
throw e;
}
if (!explicitlyAccepted)
{
try
{
}
catch (CertificateException ce)
{
chain);
throw e;
}
}
}
/**
* {@inheritDoc}
*/
{
boolean explicitlyAccepted = false;
try
{
if (trustManager != null)
{
try
{
}
catch (CertificateException ce)
{
explicitlyAccepted = true;
}
}
else
{
explicitlyAccepted = true;
}
}
catch (CertificateException ce)
{
throw e;
}
if (!explicitlyAccepted)
{
try
{
}
catch (CertificateException ce)
{
chain);
throw e;
}
}
}
/**
* {@inheritDoc}
*/
{
if (trustManager != null)
{
return trustManager.getAcceptedIssuers();
}
else
{
return new X509Certificate[0];
}
}
/**
* This method is called when the user accepted a certificate.
* @param chain the certificate chain accepted by the user.
* @param authType the authentication type.
* @param host the host we tried to connect and that presented the
* certificate.
*/
{
}
/**
* Sets the host name we are trying to contact in a secure mode. This
* method is used if we want to verify the correspondance between the
* hostname and the subject DN of the certificate that is being presented.
* If this method is never called (or called passing null) no verification
* will be made on the host name.
* @param host the host name we are trying to contact in a secure mode.
*/
{
}
/**
* This is a method used to set to null the different members that provide
* information about the last refused certificate. It is recommended to
* call this method before trying to establish a connection using this
* trust manager.
*/
public void resetLastRefusedItems()
{
}
/**
* Creates a copy of this ApplicationTrustManager.
* @return a copy of this ApplicationTrustManager.
*/
{
return copy;
}
/**
* Verifies whether the provided chain and authType have been already accepted
* by the user or not. If they have not a CertificateException is thrown.
* @param chain the certificate chain to analyze.
* @param authType the authentication type.
* @throws CertificateException if the provided certificate chain and the
* authentication type have not been accepted explicitly by the user.
*/
{
boolean found = false;
{
{
{
}
}
}
if (!found)
{
throw new OpendsCertificateException(
"Certificate not in list of accepted certificates", chain);
}
}
/**
* Verifies that the provided certificate chains subject DN corresponds to the
* host name specified with the setHost method.
* @param chain the certificate chain to analyze.
* @throws CertificateException if the subject DN of the certificate does
* not match with the host name specified with the method setHost.
*/
throws CertificateException
{
{
boolean matches = false;
try
{
if (!matches)
{
" and does not match host value: "+host);
// Try with the accepted hosts names
{
{
{
}
}
}
}
}
catch (Throwable t)
{
}
if (!matches)
{
throw new OpendsCertificateException(
"Hostname mismatch between host name " + host
chain);
}
}
}
/**
* Returns the authentication type for the last refused certificate.
* @return the authentication type for the last refused certificate.
*/
{
return lastRefusedAuthType;
}
/**
* Returns the last cause for refusal of a certificate.
* @return the last cause for refusal of a certificate.
*/
{
return lastRefusedCause;
}
/**
* Returns the certificate chain for the last refused certificate.
* @return the certificate chain for the last refused certificate.
*/
{
return lastRefusedChain;
}
/**
* Checks whether two host names match. It accepts the use of wildcard in the
* host name.
* @param host1 the first host name.
* @param host2 the second host name.
* @return <CODE>true</CODE> if the host match and <CODE>false</CODE>
* otherwise.
*/
{
{
throw new IllegalArgumentException("The host1 parameter cannot be null");
}
{
throw new IllegalArgumentException("The host2 parameter cannot be null");
}
{
{
}
}
return hostMatch;
}
}