Path: blob/master/test/jdk/sun/security/ssl/GenSSLConfigs/Handler.java
41152 views
/*1* Copyright (c) 1997, 2001, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.*;24import java.net.Socket;25import java.net.SocketException;2627import javax.net.ssl.*;2829//30// Base connection handler class -- server and client roles are almost31// identical, this class holds everything except what's different.32//33abstract class Handler extends TestThread34implements HandshakeCompletedListener35{36protected SSLSocket s;37protected boolean roleIsClient;3839// generates the stream of test data40private Traffic traffic;4142// for optional use in renegotiation43private String renegotiateSuites [];4445// Test flag: did we pass this test?46private boolean pass = false;474849Handler (String name)50{51super (name);52}535455public void setRenegotiateSuites (String suites [])56{ renegotiateSuites = suites; }575859abstract public void setReverseRole (boolean flag);606162// XXX override setVerbosity() and pass that to63// the traffic generation module646566public void run ()67{68try {69traffic = new Traffic (s.getInputStream (), s.getOutputStream ());70} catch (IOException e) {71e.printStackTrace ();72return;73}7475if (prng != null)76traffic.setPRNG (prng);7778if (listenHandshake || doRenegotiate)79s.addHandshakeCompletedListener (this);8081try {82if (initiateHandshake)83s.startHandshake ();8485// XXX if use client auth ...8687doTraffic (0);8889if (doRenegotiate)90s.startHandshake ();9192doTraffic (iterations);9394// XXX abortive shutdown should be a test option9596s.close ();9798// XXX want a close-this-session-down option99100} catch (IOException e) {101String message = e.getMessage ();102103synchronized (out) {104if (message.equalsIgnoreCase ("no cipher suites in common")) {105out.println ("%% " + getName () + " " + message);106107} else {108out.println ("%% " + getName ());109e.printStackTrace (out);110}111}112113} catch (Throwable t) {114synchronized (out) {115out.println ("%% " + getName ());116t.printStackTrace (out);117}118}119}120121122public boolean passed ()123{ return pass; }124125126private void doTraffic (int n)127throws IOException128{129try {130if (roleIsClient)131traffic.initiate (n);132else133traffic.respond (n);134135pass = true;136137} catch (SSLException e) {138String m = e.getMessage ();139140//141// As of this writing, self-signed certs won't be accepted142// by the simple trust decider. That rules out testing all143// of the SSL_DHE_DSS_* flavors for now, and for testers144// that don't have a Verisign cert, it also rules out testing145// SSL_RSA_* flavors.146//147// XXX need two things to fix this "right": (a) ability to148// let the 'simple trust decider import arbitrary certs, as149// exported by a keystore; (b) specialized exceptions, since150// comparing message strings is bogus.151//152if (m.equalsIgnoreCase ("untrusted server cert chain")153|| m.equalsIgnoreCase (154"Received fatal alert: certificate_unknown")) {155System.out.println ("%% " + Thread.currentThread ().getName ()156+ ", " + m);157s.close ();158} else159throw e;160161} catch (SocketException e) {162String m = e.getMessage ();163164if (m.equalsIgnoreCase ("Socket closed"))165System.out.println ("%% " + Thread.currentThread ().getName ()166+ ", " + m);167else168throw e;169170} catch (EOFException e) {171// ignore172}173}174175176public void handshakeCompleted (HandshakeCompletedEvent event)177{178if (verbosity >= 1) {179Socket sock = (Socket) event.getSource ();180181out.println ("%% " + getName ()182+ ", port " + sock.getLocalPort ()183+ " to " + sock.getInetAddress ().getHostName ()184+ ":" + sock.getPort ()185+ ", " + event.getCipherSuite ());186187// if more verbosity, give cert chain188}189}190}191192193