Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/javax/crypto/CipherInputStream.java
41152 views
1
/*
2
* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package javax.crypto;
27
28
import java.io.InputStream;
29
import java.io.FilterInputStream;
30
import java.io.IOException;
31
import javax.crypto.BadPaddingException;
32
import javax.crypto.IllegalBlockSizeException;
33
34
/**
35
* A CipherInputStream is composed of an InputStream and a Cipher so
36
* that read() methods return data that are read in from the
37
* underlying InputStream but have been additionally processed by the
38
* Cipher. The Cipher must be fully initialized before being used by
39
* a CipherInputStream.
40
*
41
* <p> For example, if the Cipher is initialized for decryption, the
42
* CipherInputStream will attempt to read in data and decrypt them,
43
* before returning the decrypted data.
44
*
45
* <p> This class adheres strictly to the semantics, especially the
46
* failure semantics, of its ancestor classes
47
* java.io.FilterInputStream and java.io.InputStream. This class has
48
* exactly those methods specified in its ancestor classes, and
49
* overrides them all. Moreover, this class catches all exceptions
50
* that are not thrown by its ancestor classes. In particular, the
51
* <code>skip</code> method skips, and the <code>available</code>
52
* method counts only data that have been processed by the encapsulated Cipher.
53
* This class may catch BadPaddingException and other exceptions thrown by
54
* failed integrity checks during decryption. These exceptions are not
55
* re-thrown, so the client may not be informed that integrity checks
56
* failed. Because of this behavior, this class may not be suitable
57
* for use with decryption in an authenticated mode of operation (e.g. GCM).
58
* Applications that require authenticated encryption can use the Cipher API
59
* directly as an alternative to using this class.
60
*
61
* <p> It is crucial for a programmer using this class not to use
62
* methods that are not defined or overridden in this class (such as a
63
* new method or constructor that is later added to one of the super
64
* classes), because the design and implementation of those methods
65
* are unlikely to have considered security impact with regard to
66
* CipherInputStream.
67
*
68
* @author Li Gong
69
* @see java.io.InputStream
70
* @see java.io.FilterInputStream
71
* @see javax.crypto.Cipher
72
* @see javax.crypto.CipherOutputStream
73
*
74
* @since 1.4
75
*/
76
77
public class CipherInputStream extends FilterInputStream {
78
79
// the cipher engine to use to process stream data
80
private Cipher cipher;
81
82
// the underlying input stream
83
private InputStream input;
84
85
/* the buffer holding data that have been read in from the
86
underlying stream, but have not been processed by the cipher
87
engine. the size 512 bytes is somewhat randomly chosen */
88
private byte[] ibuffer = new byte[512];
89
90
// having reached the end of the underlying input stream
91
private boolean done = false;
92
93
/* the buffer holding data that have been processed by the cipher
94
engine, but have not been read out */
95
private byte[] obuffer = null;
96
// the offset pointing to the next "new" byte
97
private int ostart = 0;
98
// the offset pointing to the last "new" byte
99
private int ofinish = 0;
100
// stream status
101
private boolean closed = false;
102
103
/**
104
* Ensure obuffer is big enough for the next update or doFinal
105
* operation, given the input length <code>inLen</code> (in bytes)
106
* The ostart and ofinish indices are reset to 0.
107
*
108
* @param inLen the input length (in bytes)
109
*/
110
private void ensureCapacity(int inLen) {
111
int minLen = cipher.getOutputSize(inLen);
112
if (obuffer == null || obuffer.length < minLen) {
113
obuffer = new byte[minLen];
114
}
115
ostart = 0;
116
ofinish = 0;
117
}
118
119
/**
120
* Private convenience function, read in data from the underlying
121
* input stream and process them with cipher. This method is called
122
* when the processed bytes inside obuffer has been exhausted.
123
*
124
* Entry condition: ostart = ofinish
125
*
126
* Exit condition: ostart = 0 AND ostart <= ofinish
127
*
128
* return (ofinish-ostart) (we have this many bytes for you)
129
* return 0 (no data now, but could have more later)
130
* return -1 (absolutely no more data)
131
*
132
* Note: Exceptions are only thrown after the stream is completely read.
133
* For AEAD ciphers a read() of any length will internally cause the
134
* whole stream to be read fully and verify the authentication tag before
135
* returning decrypted data or exceptions.
136
*/
137
private int getMoreData() throws IOException {
138
if (done) return -1;
139
int readin = input.read(ibuffer);
140
141
if (readin == -1) {
142
done = true;
143
ensureCapacity(0);
144
try {
145
ofinish = cipher.doFinal(obuffer, 0);
146
} catch (IllegalBlockSizeException | BadPaddingException
147
| ShortBufferException e) {
148
throw new IOException(e);
149
}
150
if (ofinish == 0) {
151
return -1;
152
} else {
153
return ofinish;
154
}
155
}
156
ensureCapacity(readin);
157
try {
158
ofinish = cipher.update(ibuffer, 0, readin, obuffer, ostart);
159
} catch (IllegalStateException e) {
160
throw e;
161
} catch (ShortBufferException e) {
162
throw new IOException(e);
163
}
164
return ofinish;
165
}
166
167
/**
168
* Constructs a CipherInputStream from an InputStream and a
169
* Cipher.
170
* <br>Note: if the specified input stream or cipher is
171
* null, a NullPointerException may be thrown later when
172
* they are used.
173
* @param is the to-be-processed input stream
174
* @param c an initialized Cipher object
175
*/
176
public CipherInputStream(InputStream is, Cipher c) {
177
super(is);
178
input = is;
179
cipher = c;
180
}
181
182
/**
183
* Constructs a CipherInputStream from an InputStream without
184
* specifying a Cipher. This has the effect of constructing a
185
* CipherInputStream using a NullCipher.
186
* <br>Note: if the specified input stream is null, a
187
* NullPointerException may be thrown later when it is used.
188
* @param is the to-be-processed input stream
189
*/
190
protected CipherInputStream(InputStream is) {
191
super(is);
192
input = is;
193
cipher = new NullCipher();
194
}
195
196
/**
197
* Reads the next byte of data from this input stream. The value
198
* byte is returned as an <code>int</code> in the range
199
* <code>0</code> to <code>255</code>. If no byte is available
200
* because the end of the stream has been reached, the value
201
* <code>-1</code> is returned. This method blocks until input data
202
* is available, the end of the stream is detected, or an exception
203
* is thrown.
204
*
205
* @return the next byte of data, or <code>-1</code> if the end of the
206
* stream is reached.
207
* @exception IOException if an I/O error occurs.
208
*/
209
@Override
210
public int read() throws IOException {
211
if (ostart >= ofinish) {
212
// we loop for new data as the spec says we are blocking
213
int i = 0;
214
while (i == 0) i = getMoreData();
215
if (i == -1) return -1;
216
}
217
return ((int) obuffer[ostart++] & 0xff);
218
};
219
220
/**
221
* Reads up to <code>b.length</code> bytes of data from this input
222
* stream into an array of bytes.
223
* <p>
224
* The <code>read</code> method of <code>InputStream</code> calls
225
* the <code>read</code> method of three arguments with the arguments
226
* <code>b</code>, <code>0</code>, and <code>b.length</code>.
227
*
228
* @param b the buffer into which the data is read.
229
* @return the total number of bytes read into the buffer, or
230
* <code>-1</code> is there is no more data because the end of
231
* the stream has been reached.
232
* @exception IOException if an I/O error occurs.
233
* @see java.io.InputStream#read(byte[], int, int)
234
*/
235
@Override
236
public int read(byte b[]) throws IOException {
237
return read(b, 0, b.length);
238
}
239
240
/**
241
* Reads up to <code>len</code> bytes of data from this input stream
242
* into an array of bytes. This method blocks until some input is
243
* available. If the first argument is <code>null,</code> up to
244
* <code>len</code> bytes are read and discarded.
245
*
246
* @param b the buffer into which the data is read.
247
* @param off the start offset in the destination array
248
* <code>buf</code>
249
* @param len the maximum number of bytes read.
250
* @return the total number of bytes read into the buffer, or
251
* <code>-1</code> if there is no more data because the end of
252
* the stream has been reached.
253
* @exception IOException if an I/O error occurs.
254
* @see java.io.InputStream#read()
255
*/
256
@Override
257
public int read(byte b[], int off, int len) throws IOException {
258
if (ostart >= ofinish) {
259
// we loop for new data as the spec says we are blocking
260
int i = 0;
261
while (i == 0) i = getMoreData();
262
if (i == -1) return -1;
263
}
264
if (len <= 0) {
265
return 0;
266
}
267
int available = ofinish - ostart;
268
if (len < available) available = len;
269
if (b != null) {
270
System.arraycopy(obuffer, ostart, b, off, available);
271
}
272
ostart = ostart + available;
273
return available;
274
}
275
276
/**
277
* Skips <code>n</code> bytes of input from the bytes that can be read
278
* from this input stream without blocking.
279
*
280
* <p>Fewer bytes than requested might be skipped.
281
* The actual number of bytes skipped is equal to <code>n</code> or
282
* the result of a call to
283
* {@link #available() available},
284
* whichever is smaller.
285
* If <code>n</code> is less than zero, no bytes are skipped.
286
*
287
* <p>The actual number of bytes skipped is returned.
288
*
289
* @param n the number of bytes to be skipped.
290
* @return the actual number of bytes skipped.
291
* @exception IOException if an I/O error occurs.
292
*/
293
@Override
294
public long skip(long n) throws IOException {
295
int available = ofinish - ostart;
296
if (n > available) {
297
n = available;
298
}
299
if (n < 0) {
300
return 0;
301
}
302
ostart += n;
303
return n;
304
}
305
306
/**
307
* Returns the number of bytes that can be read from this input
308
* stream without blocking. The <code>available</code> method of
309
* <code>InputStream</code> returns <code>0</code>. This method
310
* <B>should</B> be overridden by subclasses.
311
*
312
* @return the number of bytes that can be read from this input stream
313
* without blocking.
314
* @exception IOException if an I/O error occurs.
315
*/
316
@Override
317
public int available() throws IOException {
318
return (ofinish - ostart);
319
}
320
321
/**
322
* Closes this input stream and releases any system resources
323
* associated with the stream.
324
* <p>
325
* The <code>close</code> method of <code>CipherInputStream</code>
326
* calls the <code>close</code> method of its underlying input
327
* stream.
328
*
329
* @exception IOException if an I/O error occurs.
330
*/
331
@Override
332
public void close() throws IOException {
333
if (closed) {
334
return;
335
}
336
closed = true;
337
input.close();
338
339
// Throw away the unprocessed data and throw no crypto exceptions.
340
// AEAD ciphers are fully readed before closing. Any authentication
341
// exceptions would occur while reading.
342
if (!done) {
343
ensureCapacity(0);
344
try {
345
cipher.doFinal(obuffer, 0);
346
} catch (BadPaddingException | IllegalBlockSizeException
347
| ShortBufferException ex) {
348
// Catch exceptions as the rest of the stream is unused.
349
}
350
}
351
obuffer = null;
352
}
353
354
/**
355
* Tests if this input stream supports the <code>mark</code>
356
* and <code>reset</code> methods, which it does not.
357
*
358
* @return <code>false</code>, since this class does not support the
359
* <code>mark</code> and <code>reset</code> methods.
360
* @see java.io.InputStream#mark(int)
361
* @see java.io.InputStream#reset()
362
*/
363
@Override
364
public boolean markSupported() {
365
return false;
366
}
367
}
368
369