/*
 * 
 * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * The contents of this file are subject to the Sun Community Source
 * License v3.0/Jini Technology Specific Attachment v 1.0 (the
 * "License"). You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 * http://wwws.sun.com/software/jini/licensing/SCSL3_JiniTSA1.html.
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Reference Code is Jini Technology Core Platform code, v2.0. The
 * Developer of the Reference Code is Sun Microsystems, Inc.
 * 
 * Contributor(s): Sun Microsystems, Inc.
 * 
 * The contents of this file comply with the Jini Technology Core
 * Platform Compatibility Kit, v2.0A.
 * 
 * Tester(s): Sun Microsystems, Inc.
 * 
 * Test Platform(s):
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Solaris(TM)SPARC/x86
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Linux (Intel x86)
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Windows (Intel Platform)
 * 
 */

package net.jini.jeri.ssl;

import java.rmi.RemoteException;
import javax.net.SocketFactory;
import javax.security.auth.x500.X500Principal;
import net.jini.security.Security;
import net.jini.security.TrustVerifier;

/**
 * Trust verifier for the {@link SslEndpoint}, {@link HttpsEndpoint}, and
 * {@link ConfidentialityStrength} classes. Also trusts principals of type
 * {@link X500Principal}. This class is intended to be specified in a resource
 * to configure the operation of {@link Security#verifyObjectTrust
 * Security.verifyObjectTrust}.
 *
 * @author Sun Microsystems, Inc.
 * @since 2.0
 * @see SslEndpoint
 * @see HttpsEndpoint
 * @see ConfidentialityStrength
 */
public final class SslTrustVerifier implements TrustVerifier {

    /** Creates an instance of this class. */
    public SslTrustVerifier() { }

    /**
     * Returns <code>true</code> if the object is an instance of {@link
     * SslEndpoint} or {@link HttpsEndpoint}, and it's {@link SocketFactory} is
     * either <code>null</code> or trusted by the specified
     * <code>TrustVerifier.Context</code>; or if the object is an instance of
     * {@link ConfidentialityStrength} or {@link X500Principal}; and returns
     * <code>false</code> otherwise.
     *
     * @throws RemoteException if a communication-related exception occurs
     *	       when verifying a socket factory
     * @throws SecurityException if a security exception occurs when verifying
     *	       a socket factory
     * @throws NullPointerException {@inheritDoc}
     */
    public boolean isTrustedObject(Object obj, TrustVerifier.Context ctx)
	throws RemoteException
    {
	SocketFactory factory;
	if (obj == null || ctx == null) {
	    throw new NullPointerException();
	} else if (obj instanceof ConfidentialityStrength ||
		   obj instanceof X500Principal)
	{
	    return true;
	} else if (obj instanceof SslEndpoint) {
	    factory = ((SslEndpoint) obj).getSocketFactory();
	} else if (obj instanceof HttpsEndpoint) {
	    factory = ((HttpsEndpoint) obj).getSocketFactory();
	} else {
	    return false;
	}
	return factory == null || ctx.isTrustedObject(factory);
    }
}
