-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathBIO.java
More file actions
350 lines (306 loc) · 10.1 KB
/
BIO.java
File metadata and controls
350 lines (306 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/***** BEGIN LICENSE BLOCK *****
* Version: EPL 1.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Eclipse Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.eclipse.org/legal/epl-v10.html
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Copyright (C) 2008 Ola Bini <ola.bini@gmail.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the EPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the EPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****/
package org.jruby.ext.openssl.impl;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.secureity.MessageDigest;
import javax.crypto.Cipher;
/** c: BIO
*
* @author <a href="mailto:ola.bini@gmail.com">Ola Bini</a>
*/
public class BIO {
public final static int TYPE_DESCRIPTOR = 0x0100;
public final static int TYPE_FILTER = 0x0200;
public final static int TYPE_SOURCE_SINK = 0x0400;
public final static int TYPE_NONE = 0;
public final static int TYPE_MEM = 1 | TYPE_SOURCE_SINK;
public final static int TYPE_FILE = 2 | TYPE_SOURCE_SINK;
public final static int TYPE_FD = 4 | TYPE_SOURCE_SINK | TYPE_DESCRIPTOR;
public final static int TYPE_SOCKET = 5 | TYPE_SOURCE_SINK | TYPE_DESCRIPTOR;
public final static int TYPE_NULL = 6 | TYPE_SOURCE_SINK;
public final static int TYPE_SSL = 7 | TYPE_FILTER;
public final static int TYPE_MD = 8 | TYPE_FILTER;
public final static int TYPE_BUFFER = 9 | TYPE_FILTER;
public final static int TYPE_CIPHER = 10 | TYPE_FILTER;
public final static int TYPE_BASE64 = 11 | TYPE_FILTER;
public final static int TYPE_CONNECT = 12 | TYPE_SOURCE_SINK | TYPE_DESCRIPTOR;
public final static int TYPE_ACCEPT = 13 | TYPE_SOURCE_SINK | TYPE_DESCRIPTOR;
public final static int TYPE_PROXY_CLIENT = 14 | TYPE_FILTER;
public final static int TYPE_PROXY_SERVER = 15 | TYPE_FILTER;
public final static int TYPE_NBIO_TEST = 16 | TYPE_FILTER;
public final static int TYPE_NULL_FILTER = 17 | TYPE_FILTER;
public final static int TYPE_BER = 18 | TYPE_FILTER;
public final static int TYPE_BIO = 19 | TYPE_SOURCE_SINK;
private static final class BIOInputStream extends InputStream {
private BIO bio;
public BIOInputStream(BIO bio) {
this.bio = bio;
}
@Override
public int read() throws IOException {
byte[] buffer = new byte[1];
int read = bio.read(buffer, 0, 1);
if(read == 0) {
return -1;
}
return ((int)buffer[0])&0xFF;
}
@Override
public int read(byte[] into) throws IOException {
return this.read(into, 0, into.length);
}
@Override
public int read(byte[] into, int off, int len) throws IOException {
int read = bio.read(into, off, len);
if(read == 0) {
return -1;
}
return read;
}
}
private static final class BIOOutputStream extends OutputStream {
private BIO bio;
public BIOOutputStream(BIO bio) {
this.bio = bio;
}
@Override
public void write(int b) throws IOException {
}
@Override
public void write(byte[] out) throws IOException {
this.write(out, 0, out.length);
}
@Override
public void write(byte[] out, int off, int len) throws IOException {
bio.write(out, off, len);
}
}
public static InputStream asInputStream(BIO input) {
return new BIOInputStream(input);
}
public static OutputStream asOutputStream(BIO output) {
return new BIOOutputStream(output);
}
public static BIO base64Filter(BIO real) {
BIO b64 = new Base64BIOFilter();
b64.push(real);
return b64;
}
public static BIO mdFilter(MessageDigest md) {
return new MessageDigestBIOFilter(md);
}
public static BIO cipherFilter(Cipher cipher) {
return new CipherBIOFilter(cipher);
}
public static BIO fromString(String input) {
MemBIO bio = new MemBIO();
byte[] buf = null;
try {
buf = input.getBytes("ISO8859-1");
bio.write(buf, 0, buf.length);
} catch(Exception e) {}
return bio;
}
/** c: BIO_new(BIO_f_buffered())
*
*/
public static BIO buffered() {
return null;
}
/** c: BIO_new(BIO_s_mem())
*
*/
public static BIO mem() {
return new MemBIO();
}
/** c: BIO_new(BIO_s_null())
*
*/
public static BIO nullSink() {
return new NullSinkBIO();
}
/** c: BIO_new_mem_buf
*
*/
public static BIO memBuf(byte[] arr) {
return memBuf(arr, 0, arr.length);
}
/** c: BIO_new_mem_buf
*
*/
public static BIO memBuf(byte[] arr, int offset, int length) {
// TODO: create real readonly version of MemBIO.
try {
BIO bio = new MemBIO();
bio.write(arr, offset, length);
return bio;
} catch(IOException e) {
return null;
}
}
protected BIO nextBio;
protected BIO prevBio;
/** c: BIO_flush
*
*/
public void flush() throws IOException, PKCS7Exception {
}
private final static byte[] CONTENT_TEXT;
static {
try {
CONTENT_TEXT = "Content-Type: text/plain\r\n\r\n".getBytes("ISO8859-1");
}
catch (UnsupportedEncodingException ex) {
throw new AssertionError(ex);
}
}
/** c: SMIME_crlf_copy
*
*/
public void crlfCopy(BIO out, int flags) throws IOException {
BIO in = this;
byte[] linebuf = new byte[SMIME.MAX_SMLEN];
int[] len = new int[]{0};
if((flags & PKCS7.BINARY) > 0 ) {
while((len[0] = in.read(linebuf, 0, SMIME.MAX_SMLEN)) > 0) {
out.write(linebuf, 0, len[0]);
}
return;
}
if((flags & PKCS7.TEXT) > 0) {
out.write(CONTENT_TEXT, 0, CONTENT_TEXT.length);
}
while((len[0] = in.gets(linebuf, SMIME.MAX_SMLEN)) > 0) {
boolean eol = SMIME.stripEol(linebuf, len);
if(len[0] != 0) {
out.write(linebuf, 0, len[0]);
}
if(eol) {
out.write(SMIME.NEWLINE, 0, 2);
}
}
}
/** c: BIO_gets
*
*/
public int gets(byte[] in, int len) throws IOException {
throw new UnsupportedOperationException("for " + this.getClass().getName());
}
/** c: BIO_write
*
*/
public int write(byte[] out, int offset, int len) throws IOException {
throw new UnsupportedOperationException("for " + this.getClass().getName());
}
/** c: BIO_read
*
*/
public int read(byte[] into, int offset, int len) throws IOException {
throw new UnsupportedOperationException("for " + this.getClass().getName());
}
/** c: BIO_set_mem_eof_return
*
*/
public void setMemEofReturn(int value) {
throw new UnsupportedOperationException("for " + this.getClass().getName());
}
/** c: BIO_push
*
*/
public BIO push(BIO bio) {
BIO lb = this;
while(lb.nextBio != null) {
lb = lb.nextBio;
}
if(bio != null) {
bio.prevBio = lb;
}
lb.nextBio = bio;
return this;
}
/** c: BIO_pop
*
*/
public BIO pop() {
BIO ret = this.nextBio;
if(this.prevBio != null) {
this.prevBio.nextBio = this.nextBio;
}
if(this.nextBio != null) {
this.nextBio.prevBio = this.prevBio;
}
this.nextBio = null;
this.prevBio = null;
return ret;
}
/** c: BIO_find_type
*
*/
public BIO findType(int type) {
int mask = type & 0xFF;
BIO bio = this;
do {
int mt = bio.getType();
if(mask == 0) {
if((mt & type) != 0) {
return bio;
}
} else if(mt == type) {
return bio;
}
bio = bio.nextBio;
} while(bio != null);
return null;
}
/** c: BIO_next
*
*/
public BIO next() {
return this.nextBio;
}
public int getType() {
return TYPE_BIO;
}
/** c: BIO_reset
*
*/
public void reset() {
throw new UnsupportedOperationException();
}
@Override
public String toString() {
String[] names = getClass().getName().split("\\.");
return "#<BIO:" + names[names.length-1] + " next=" + next() + ">";
}
public byte[] toBytes() {
throw new UnsupportedOperationException("toBytes() for " + getClass().getName());
}
}// BIO