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