Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/ciphercore/ServerKeyException.java


1   /* $Id: ServerKeyException.java,v 1.1 2001/04/02 08:44:06 cvsbob Exp $ */
2   
3   /*
4    * ServerKeyException.java, signifies a server key authentication error.
5    * Copyright (C) 2001 Robert Bushman.
6    *
7    * I reserve the right to release this program under seperate license.
8    * If you require a special license grant contact Robert Bushman.
9    *
10   * This program is free software; you can redistribute it and/or
11   * modify it under the terms of the GNU General Public License
12   * as published by the Free Software Foundation; either version 2
13   * of the License, or (at your option) any later version.
14   *
15   * This program is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   * GNU General Public License for more details.
19   * 
20   * You should have received a copy of the GNU General Public License
21   * along with this program; if not, write to the Free Software
22   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 
23   * 02111-1307, USA.
24   */
25  
26  package com.ciphercore;
27  
28  import java.math.BigInteger;
29  import java.security.DigestException;
30  
31  import javax.swing.JFrame;
32  import javax.swing.JOptionPane;
33  
34  public class ServerKeyException extends DigestException {
35      
36      // ----------------------------------------------------------
37      // CONSTANTS
38      // ----------------------------------------------------------
39      
40      public static final int
41          TYPE_HOST_UNKNOWN               = 0,
42          TYPE_KEY_VERIFICATION_FAILURE   = 1,
43          TYPE_DIGEST_CORRUPT             = 2,
44          RESPONSE_CONTINUE               = JOptionPane.YES_OPTION,
45          RESPONSE_CANCEL                 = JOptionPane.NO_OPTION;
46      
47      // -----------------------------------------------------------
48      // CONSTRUCTORS AND INITIALIZERS
49      // -----------------------------------------------------------
50      
51      public ServerKeyException( int type ) {
52          super( setMessage( type ) );
53          setType( type );
54      }
55      
56      // ----------------------------------------------------------
57      // PUBLIC API
58      // ----------------------------------------------------------
59      
60      public int doOptionDialog( JFrame parentFrame ) {
61          if( getType() == TYPE_HOST_UNKNOWN ) {
62              return( showNoHashDialog( parentFrame ) );
63          } else {
64              return( showVerificationFailureDialog( parentFrame ) );
65          }
66      }
67      
68      // ---------------------------------------------------------
69      // INTERNAL API
70      // ---------------------------------------------------------
71      
72      protected static String setMessage( int type ) {
73          if( type == TYPE_HOST_UNKNOWN ) {
74              String message =
75                  "\nWARNING: No SHA-1 hash registered for this host.\n"
76                  + "****************************************************\n"
77                  + " THIS CONNECTION IS VULNERABLE TO MAN IN THE MIDDLE \n"
78                  + "****************************************************\n"
79                  + "server hashes are in config/client/server.hashes\n";
80              return( message );
81          } else if( type == TYPE_DIGEST_CORRUPT ) {
82              String message =
83                  "\nWARNING: SHA-1 hash for this host is corrupt.\n"
84                  + "****************************************************\n"
85                  + " THIS CONNECTION IS VULNERABLE TO MAN IN THE MIDDLE \n"
86                  + "****************************************************\n"
87                  + "server hashes are in config/client/server.hashes\n";
88              return( message );
89          } else {
90              String message =
91                  "\nWARNING: Server key failed SHA-1 hash verification.\n"
92                  + "*************************************************\n"
93                  + "       THIS IS A SERIOUS SECURITY BREACH.        \n"
94                  + "*************************************************\n"
95                  + "   A MAN IN THE MIDDLE ATTACK MAY BE OCCURING.   \n"
96                  + "*************************************************\n"
97                  + "server hashes are in config/client/server.hashes\n";
98              return( message );
99          }
100     }
101     
102     protected int showNoHashDialog( JFrame parentFrame ) {
103         Object[] options = { "INSECURE Connection", "Cancel" };
104         int result = JOptionPane.showOptionDialog
105             ( parentFrame,
106               "There is no SHA-1 hash registered for this host.\n"
107               + "This connection will not be secure.\n"
108               + "It will be vulnerable to Man In The Middle.",
109               "SECURITY BREACH (No Hash, Man In The Middle)",
110               JOptionPane.YES_NO_OPTION,
111               JOptionPane.WARNING_MESSAGE,
112               null,
113               options,
114               options[1] );
115         return( result );
116     }
117     
118     protected int showVerificationFailureDialog( JFrame parentFrame ) {
119         Object[] options = { "INSECURE Connection", "Cancel" };
120         int result = JOptionPane.showOptionDialog
121             ( parentFrame,
122               "The server key does not match it's registration.\n"
123               + "This is a serious security breach.\n"
124               + "You should not establish this connection.\n"
125               + "This connection will not be secure.\n"
126               + "It will be vulnerable to Man In The Middle.",
127               "SECURITY BREACH (Server Authentication Failure)",
128               JOptionPane.YES_NO_OPTION,
129               JOptionPane.WARNING_MESSAGE,
130               null,
131               options,
132               options[1] );
133         return( result );
134     }
135     
136     // -----------------------------------------------------------
137     // INSTANCE PARAMETERS AND ACCESSORS
138     // -----------------------------------------------------------
139     
140     // PARAMETERS
141     private int _type;
142     private String _hostName;
143     private BigInteger _currentDigest;
144     private BigInteger _newDigest;
145     
146     // SETTERS
147     protected void setType( int type ) { _type = type; }
148     public void setCurrentDigest( BigInteger digest ) {
149         _currentDigest = digest;
150     }
151     public void setNewDigest( BigInteger digest ) { _newDigest = digest; }
152     public void setHostName( String hostName ) { _hostName = hostName; }
153     
154     // GETTERS
155     public int getType() { return( _type ); }
156     public BigInteger getCurrentDigest() { return( _currentDigest ); }
157     public BigInteger getNewDigest() { return( _newDigest ); }
158     public String getHostName() { return( _hostName ); }
159     
160     // CONVENIENCE METHODS
161     public String getNewDigestBase16() {
162         return( getNewDigest().toString( 16 ) );
163     }
164     public void setCurrentDigest( String digestBase16 ) {
165         if( digestBase16 == null || digestBase16.equals( "" ) ) {
166             setCurrentDigest( (BigInteger)null );
167         } else {
168             setCurrentDigest( new BigInteger( digestBase16, 16 ) );
169         }
170     }
171     public void setCurrentDigest( byte[] digestBytes ) {
172         if( digestBytes == null || digestBytes.length == 0 ) {
173             setCurrentDigest( (BigInteger)null );
174         }
175         else {
176             setCurrentDigest( new BigInteger( digestBytes ) );
177         }
178     }
179     public void setNewDigest( String digestBase16 ) {
180         if( digestBase16 == null || digestBase16.equals( "" ) ) {
181             setNewDigest( (BigInteger)null );
182         } else {
183             setNewDigest( new BigInteger( digestBase16, 16 ) );
184         }
185     }
186     public void setNewDigest( byte[] digestBytes ) {
187         if( digestBytes == null || digestBytes.length == 0 ) {
188             setNewDigest( (BigInteger)null );
189         }
190         else {
191             setNewDigest( new BigInteger( digestBytes ) );
192         }
193     }
194 }
195