Source for gnu.javax.crypto.sasl.SaslOutputStream

   1: /* SaslOutputStream.java --
   2:    Copyright (C) 2003, 2006, 2010 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.crypto.sasl;
  40: 
  41: import gnu.java.security.Configuration;
  42: import gnu.java.security.util.Util;
  43: 
  44: import java.io.IOException;
  45: import java.io.OutputStream;
  46: import java.util.logging.Logger;
  47: 
  48: import javax.security.sasl.Sasl;
  49: import javax.security.sasl.SaslClient;
  50: import javax.security.sasl.SaslServer;
  51: 
  52: /**
  53:  * An output stream that uses either a {@link SaslClient} or a {@link SaslServer}
  54:  * to process the data through these entities' security layer filter(s).
  55:  */
  56: public class SaslOutputStream
  57:     extends OutputStream
  58: {
  59:   private static final Logger log = Configuration.DEBUG ?
  60:                 Logger.getLogger(SaslOutputStream.class.getName()) : null;
  61:   private SaslClient client;
  62:   private SaslServer server;
  63:   private int maxRawSendSize;
  64:   private OutputStream dest;
  65: 
  66:   public SaslOutputStream(SaslClient client, OutputStream dest)
  67:       throws IOException
  68:   {
  69:     super();
  70: 
  71:     this.client = client;
  72:     String size = (String) client.getNegotiatedProperty(Sasl.RAW_SEND_SIZE);
  73:     maxRawSendSize = Integer.parseInt(size);
  74:     server = null;
  75:     this.dest = dest;
  76:   }
  77: 
  78:   public SaslOutputStream(SaslServer server, OutputStream dest)
  79:       throws IOException
  80:   {
  81:     super();
  82: 
  83:     this.server = server;
  84:     String size = (String) server.getNegotiatedProperty(Sasl.RAW_SEND_SIZE);
  85:     maxRawSendSize = Integer.parseInt(size);
  86:     client = null;
  87:     this.dest = dest;
  88:   }
  89: 
  90:   public void close() throws IOException
  91:   {
  92:     dest.flush();
  93:     dest.close();
  94:   }
  95: 
  96:   public void flush() throws IOException
  97:   {
  98:     dest.flush();
  99:   }
 100: 
 101:   /**
 102:    * When writing octets to the resulting stream, if a security layer has been
 103:    * negotiated, each piece of data written (by a single invocation of
 104:    * <code>write()</code>) will be encapsulated as a SASL buffer, as defined in
 105:    * RFC 2222, and then written to the underlying <i>dest</i> output stream.
 106:    */
 107:   public void write(int b) throws IOException
 108:   {
 109:     write(new byte[] { (byte) b });
 110:   }
 111: 
 112:   /**
 113:    * When writing octets to the resulting stream, if a security layer has been
 114:    * negotiated, each piece of data written (by a single invocation of
 115:    * <code>write()</code>) will be encapsulated as a SASL buffer, as defined in
 116:    * RFC 2222, and then written to the underlying <i>dest</i> output stream.
 117:    */
 118:   public void write(byte[] b, int off, int len) throws IOException
 119:   {
 120:     if (Configuration.DEBUG)
 121:       log.entering(this.getClass().getName(), "write");
 122:     if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length)
 123:         || ((off + len) < 0))
 124:       throw new IndexOutOfBoundsException("off=" + off + ", len=" + len
 125:                                           + ", b.length=" + b.length);
 126:     if (len == 0)
 127:       {
 128:         if (Configuration.DEBUG)
 129:           log.exiting(this.getClass().getName(), "write");
 130:         return;
 131:       }
 132:     int chunckSize, length, chunck = 1;
 133:     byte[] output = null, result;
 134:     if (Configuration.DEBUG)
 135:       log.finer("About to wrap " + len + " byte(s)...");
 136:     while (len > 0)
 137:       {
 138:         chunckSize = (len > maxRawSendSize ? maxRawSendSize : len);
 139:         if (Configuration.DEBUG)
 140:           {
 141:             log.finer("Outgoing buffer (before security) (hex): "
 142:                       + Util.dumpString(b, off, chunckSize));
 143:             log.finer("Outgoing buffer (before security) (str): \""
 144:                       + new String(b, off, chunckSize) + "\"");
 145:           }
 146:         if (client != null)
 147:           output = client.wrap(b, off, chunckSize);
 148:         else
 149:           output = server.wrap(b, off, chunckSize);
 150: 
 151:         if (Configuration.DEBUG)
 152:           {
 153:             log.finer("Outgoing buffer (after security) (hex): "
 154:                       + Util.dumpString(output));
 155:             log.finer("Outgoing buffer (after security) (str): \""
 156:                       + new String(output) + "\"");
 157:           }
 158:         length = output.length;
 159:         result = new byte[length + 4];
 160:         result[0] = (byte)(length >>> 24);
 161:         result[1] = (byte)(length >>> 16);
 162:         result[2] = (byte)(length >>> 8);
 163:         result[3] = (byte) length;
 164:         System.arraycopy(output, 0, result, 4, length);
 165:         dest.write(result);
 166:         off += chunckSize;
 167:         len -= chunckSize;
 168:         if (Configuration.DEBUG)
 169:           log.finer("Wrapped chunck #" + chunck);
 170:         chunck++;
 171:       }
 172:     dest.flush();
 173:     if (Configuration.DEBUG)
 174:       log.exiting(this.getClass().getName(), "write");
 175:   }
 176: }