home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 June / CHIPHEFT062001.ISO / browser / nc32lyc / comm.z / java40.jar / netscape / net / SSLSocketImpl.class (.txt) < prev    next >
Encoding:
Java Class File  |  2000-08-15  |  7.2 KB  |  373 lines

  1. package netscape.net;
  2.  
  3. import java.io.FileDescriptor;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.net.InetAddress;
  8. import java.net.ProtocolException;
  9. import java.net.SocketException;
  10. import java.net.SocketImpl;
  11. import java.net.UnknownHostException;
  12. import java.util.Enumeration;
  13. import java.util.Vector;
  14. import netscape.security.ForbiddenTargetException;
  15. import netscape.security.PrivilegeManager;
  16.  
  17. class SSLSocketImpl extends SocketImpl {
  18.    int timeout;
  19.    private boolean requireClientAuth = false;
  20.    private boolean useClientMode = false;
  21.    private boolean clientModeInitialized = false;
  22.    private boolean trustedForClientAuth = false;
  23.    Vector handshakeListeners = new Vector();
  24.    Thread callbackNotifier;
  25.    SSLSocket socket;
  26.  
  27.    SSLSocketImpl() {
  28.       this.socket = null;
  29.  
  30.       try {
  31.          this.trustedForClientAuth = false;
  32.  
  33.          try {
  34.             PrivilegeManager.checkPrivilegeEnabled("ClientAuth");
  35.          } catch (NoClassDefFoundError var1) {
  36.          }
  37.  
  38.          this.trustedForClientAuth = true;
  39.       } catch (ForbiddenTargetException var2) {
  40.       }
  41.    }
  42.  
  43.    SSLSocketImpl(SSLSocket var1) {
  44.       this.socket = var1;
  45.  
  46.       try {
  47.          this.trustedForClientAuth = false;
  48.  
  49.          try {
  50.             PrivilegeManager.checkPrivilegeEnabled("ClientAuth");
  51.          } catch (NoClassDefFoundError var2) {
  52.          }
  53.  
  54.          this.trustedForClientAuth = true;
  55.       } catch (ForbiddenTargetException var3) {
  56.       }
  57.    }
  58.  
  59.    protected synchronized void create(boolean var1) throws IOException {
  60.       if (!var1) {
  61.          throw new IOException("UDP datagram sockets not supported in SSL.");
  62.       } else {
  63.          super.fd = new FileDescriptor();
  64.          this.socketCreate(var1);
  65.       }
  66.    }
  67.  
  68.    protected void connect(String var1, int var2) throws UnknownHostException, IOException {
  69.       Object var3 = null;
  70.  
  71.       try {
  72.          InetAddress var4 = InetAddress.getByName(var1);
  73.  
  74.          try {
  75.             this.connectToAddress(var4, var2);
  76.             return;
  77.          } catch (IOException var6) {
  78.             var3 = var6;
  79.          }
  80.       } catch (UnknownHostException var7) {
  81.          var3 = var7;
  82.       }
  83.  
  84.       this.close();
  85.       throw var3;
  86.    }
  87.  
  88.    protected void connect(InetAddress var1, int var2) throws IOException {
  89.       super.port = var2;
  90.       super.address = var1;
  91.  
  92.       try {
  93.          this.connectToAddress(var1, var2);
  94.       } catch (IOException var4) {
  95.          this.close();
  96.          throw var4;
  97.       }
  98.    }
  99.  
  100.    private void connectToAddress(InetAddress var1, int var2) throws IOException {
  101.       this.doConnect(var1, var2);
  102.    }
  103.  
  104.    public void setNeedClientAuth(boolean var1) {
  105.       this.requireClientAuth = var1;
  106.       this.socketSetNeedClientAuth(var1);
  107.    }
  108.  
  109.    public boolean getNeedClientAuth() {
  110.       return this.requireClientAuth;
  111.    }
  112.  
  113.    public void setUseClientMode(boolean var1) {
  114.       this.clientModeInitialized = true;
  115.       this.useClientMode = var1;
  116.    }
  117.  
  118.    public boolean getUseClientMode() {
  119.       return this.useClientMode;
  120.    }
  121.  
  122.    boolean isClientModeInitialized() {
  123.       return this.clientModeInitialized;
  124.    }
  125.  
  126.    public void setOption(int var1, Object var2) throws SocketException {
  127.       int var3 = 0;
  128.       boolean var4 = true;
  129.       switch (var1) {
  130.          case 1:
  131.             if (var2 == null || !(var2 instanceof Boolean)) {
  132.                throw new SocketException("bad parameter for TCP_NODELAY");
  133.             }
  134.  
  135.             var4 = (Boolean)var2;
  136.             break;
  137.          case 15:
  138.             throw new SocketException("Cannot re-bind socket");
  139.          case 128:
  140.             if (var2 == null || !(var2 instanceof Integer) && !(var2 instanceof Boolean)) {
  141.                throw new SocketException("Bad parameter for option");
  142.             }
  143.  
  144.             if (var2 instanceof Boolean) {
  145.                var4 = false;
  146.                var3 = 0;
  147.             } else {
  148.                var3 = (Integer)var2;
  149.             }
  150.             break;
  151.          case 4102:
  152.             if (var2 != null && var2 instanceof Integer) {
  153.                int var5 = (Integer)var2;
  154.                if (var5 < 0) {
  155.                   throw new IllegalArgumentException("timeout < 0");
  156.                }
  157.  
  158.                this.timeout = var5;
  159.                return;
  160.             }
  161.  
  162.             throw new SocketException("Bad parameter for SO_TIMEOUT");
  163.          default:
  164.             throw new SocketException("unrecognized TCP option: " + var1);
  165.       }
  166.  
  167.       this.socketSetOptionIntVal(var1, var4, var3);
  168.    }
  169.  
  170.    public Object getOption(int var1) throws SocketException {
  171.       if (var1 == 4102) {
  172.          return new Integer(this.timeout);
  173.       } else {
  174.          int var2 = this.socketGetOption(var1);
  175.          switch (var1) {
  176.             case 1:
  177.                if (var2 == -1) {
  178.                   return new Boolean(false);
  179.                }
  180.  
  181.                return new Boolean(true);
  182.             case 15:
  183.                InetAddress var3 = new InetAddress(var2);
  184.                return var3;
  185.             case 128:
  186.                if (var2 == -1) {
  187.                   return new Boolean(false);
  188.                }
  189.  
  190.                return new Integer(var2);
  191.             default:
  192.                return null;
  193.          }
  194.       }
  195.    }
  196.  
  197.    private void doConnect(InetAddress var1, int var2) throws IOException {
  198.       ProtocolException var3 = null;
  199.       if (!this.isClientModeInitialized()) {
  200.          this.setUseClientMode(true);
  201.       }
  202.  
  203.       int var4 = 0;
  204.  
  205.       while(var4 < 3) {
  206.          try {
  207.             this.socketConnect(var1, var2);
  208.             return;
  209.          } catch (ProtocolException var6) {
  210.             this.close();
  211.             super.fd = new FileDescriptor();
  212.             this.socketCreate(true);
  213.             var3 = var6;
  214.             ++var4;
  215.          } catch (IOException var7) {
  216.             this.close();
  217.             throw var7;
  218.          }
  219.       }
  220.  
  221.       this.close();
  222.       throw var3;
  223.    }
  224.  
  225.    private boolean usingSocks() {
  226.       return false;
  227.    }
  228.  
  229.    protected synchronized void bind(InetAddress var1, int var2) throws IOException {
  230.       this.socketBind(var1, var2);
  231.    }
  232.  
  233.    protected synchronized void listen(int var1) throws IOException {
  234.       this.socketListen(var1);
  235.    }
  236.  
  237.    protected synchronized void accept(SocketImpl var1) throws IOException {
  238.       this.socketAccept(var1);
  239.    }
  240.  
  241.    protected synchronized InputStream getInputStream() throws IOException {
  242.       return new SSLInputStream(this);
  243.    }
  244.  
  245.    protected synchronized OutputStream getOutputStream() throws IOException {
  246.       return new SSLOutputStream(this);
  247.    }
  248.  
  249.    protected synchronized int available() throws IOException {
  250.       return this.socketAvailable();
  251.    }
  252.  
  253.    protected void close() throws IOException {
  254.       if (super.fd != null && super.fd.valid()) {
  255.          this.socketClose();
  256.          super.fd = null;
  257.       }
  258.  
  259.    }
  260.  
  261.    protected void finalize() throws IOException {
  262.       this.close();
  263.    }
  264.  
  265.    private native void socketCreate(boolean var1) throws IOException;
  266.  
  267.    private native void socketConnect(InetAddress var1, int var2) throws IOException;
  268.  
  269.    private native void socketBind(InetAddress var1, int var2) throws IOException;
  270.  
  271.    private native void socketListen(int var1) throws IOException;
  272.  
  273.    private native void socketAccept(SocketImpl var1) throws IOException;
  274.  
  275.    private native int socketAvailable() throws IOException;
  276.  
  277.    private native void socketClose() throws IOException;
  278.  
  279.    private native void socketSetNeedClientAuth(boolean var1);
  280.  
  281.    private native void socketSetOptionIntVal(int var1, boolean var2, int var3) throws SocketException;
  282.  
  283.    private native int socketGetOption(int var1) throws SocketException;
  284.  
  285.    protected FileDescriptor getFileDescriptor() {
  286.       return super.fd;
  287.    }
  288.  
  289.    protected InetAddress getInetAddress() {
  290.       return super.address;
  291.    }
  292.  
  293.    protected int getPort() {
  294.       return super.port;
  295.    }
  296.  
  297.    protected int getLocalPort() {
  298.       return super.localport;
  299.    }
  300.  
  301.    public void removeHandshakeCompletedListener(SSLHandshakeCompletedListener var1) {
  302.       this.handshakeListeners.removeElement(var1);
  303.    }
  304.  
  305.    public void addHandshakeCompletedListener(SSLHandshakeCompletedListener var1) {
  306.       if (this.socket == null) {
  307.          throw new RuntimeException("no socket in client-side SSLSocketImpl?");
  308.       } else {
  309.          if (this.callbackNotifier == null) {
  310.             this.callbackNotifier = new SSLCallbackNotifier(this);
  311.             this.callbackNotifier.start();
  312.          }
  313.  
  314.          this.handshakeListeners.addElement(var1);
  315.       }
  316.    }
  317.  
  318.    void callHandshakeCompletedListeners() {
  319.       try {
  320.          Thread var1 = this.callbackNotifier;
  321.          synchronized(var1){}
  322.  
  323.          try {
  324.             this.callbackNotifier.notify();
  325.          } catch (Throwable var4) {
  326.             throw var4;
  327.          }
  328.  
  329.       } catch (NullPointerException var5) {
  330.       }
  331.    }
  332.  
  333.    void doCallHandshakeCompletedListeners() {
  334.       if (this.socket == null) {
  335.          throw new RuntimeException("no socket in SSLSocketImpl?");
  336.       } else {
  337.          Vector var1 = this.handshakeListeners;
  338.          synchronized(var1){}
  339.  
  340.          try {
  341.             SSLHandshakeCompletedEvent var5 = new SSLHandshakeCompletedEvent(this.socket);
  342.             Enumeration var3 = this.handshakeListeners.elements();
  343.  
  344.             while(var3.hasMoreElements()) {
  345.                SSLHandshakeCompletedListener var4 = (SSLHandshakeCompletedListener)var3.nextElement();
  346.                var4.handshakeCompleted(var5);
  347.             }
  348.          } catch (Throwable var7) {
  349.             throw var7;
  350.          }
  351.  
  352.       }
  353.    }
  354.  
  355.    boolean allowClientAuth() {
  356.       return this.trustedForClientAuth;
  357.    }
  358.  
  359.    native SSLSecurityStatus getStatus();
  360.  
  361.    native void resetHandshake();
  362.  
  363.    native void forceHandshake();
  364.  
  365.    native void redoHandshake();
  366.  
  367.    static {
  368.       SecurityManager.enablePrivilege("SuperUser");
  369.       SecurityManager.enablePrivilege("UniversalPropertyRead");
  370.       System.loadLibrary(System.getProperty("netscape.net.dll", "net"));
  371.    }
  372. }
  373.