Source for gnu.javax.net.ssl.SSLCipherSuite

   1: /* SSLCipherSuite.java -- an SSL cipher suite.
   2:    Copyright (C) 2006  Free Software Foundation, Inc.
   3: 
   4: This file is a part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2 of the License, or (at
   9: your option) any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; if not, write to the Free Software
  18: Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  19: USA
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version.  */
  37: 
  38: 
  39: package gnu.javax.net.ssl;
  40: 
  41: import gnu.java.security.Engine;
  42: 
  43: import java.lang.reflect.InvocationTargetException;
  44: import java.nio.ByteBuffer;
  45: import java.security.NoSuchAlgorithmException;
  46: import java.security.Provider;
  47: import java.security.Security;
  48: 
  49: /**
  50:  * An SSL cipher suite.
  51:  */
  52: public abstract class SSLCipherSuite
  53: {
  54:   private static final String SERVICE = "SSLCipherSuite";
  55:   private final String algorithm;
  56:   private final byte[] id;
  57:   private final SSLProtocolVersion version;
  58:   private Provider provider;
  59: 
  60:   protected SSLCipherSuite (final String algorithm, final byte[] id,
  61:                             final SSLProtocolVersion version)
  62:   {
  63:     this.algorithm = algorithm;
  64:     if (id.length != 2)
  65:       throw new IllegalArgumentException ("cipher suite ID must be two bytes");
  66:     this.id = (byte[]) id.clone ();
  67:     this.version = version;
  68:   }
  69: 
  70:   public static final SSLCipherSuite getInstance (SSLProtocolVersion version, byte[] id)
  71:     throws NoSuchAlgorithmException
  72:   {
  73:     return getInstance (version + "-" + ((id[0] & 0xFF) + "/" + (id[1] & 0xFF)));
  74:   }
  75: 
  76:   public static final SSLCipherSuite getInstance (SSLProtocolVersion version,
  77:                                                   byte[] id, Provider provider)
  78:     throws NoSuchAlgorithmException
  79:   {
  80:     return getInstance (version + "-" + (id[0] & 0xFF) + "/" + (id[1] & 0xFF), provider);
  81:   }
  82: 
  83:   public static final SSLCipherSuite getInstance (String name)
  84:     throws NoSuchAlgorithmException
  85:   {
  86:     Provider[] providers = Security.getProviders ();
  87:     for (int i = 0; i < providers.length; i++)
  88:       {
  89:         try
  90:           {
  91:             return getInstance (name, providers[i]);
  92:           }
  93:         catch (NoSuchAlgorithmException nsae)
  94:           {
  95:             // Ignore.
  96:           }
  97:       }
  98: 
  99:     throw new NoSuchAlgorithmException (SERVICE + ": " + name);
 100:   }
 101: 
 102:   public static final SSLCipherSuite getInstance (String name, Provider provider)
 103:     throws NoSuchAlgorithmException
 104:   {
 105:     SSLCipherSuite suite = null;
 106:     try
 107:       {
 108:         suite = (SSLCipherSuite) Engine.getInstance (SERVICE, name, provider);
 109:         suite.provider = provider;
 110:       }
 111:     catch (InvocationTargetException ite)
 112:       {
 113:         // XXX
 114:         NoSuchAlgorithmException nsae = new NoSuchAlgorithmException (name);
 115:         nsae.initCause (ite);
 116:         throw nsae;
 117:       }
 118:     return suite;
 119:   }
 120: 
 121:   public final String getAlgorithm ()
 122:   {
 123:     return algorithm;
 124:   }
 125: 
 126:   public final byte[] getId ()
 127:   {
 128:     return (byte[]) id.clone ();
 129:   }
 130: 
 131:   public final Provider getProvider ()
 132:   {
 133:     return provider;
 134:   }
 135: 
 136:   public final SSLProtocolVersion getProtocolVersion ()
 137:   {
 138:     return version;
 139:   }
 140: 
 141:   public abstract void encipher (ByteBuffer in, ByteBuffer out);
 142: }