Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/crypto/CipherInputStream.java
38829 views
1
/*
2
* Copyright (c) 1997, 2018, 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 overriden 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;
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
* private convenience function.
105
*
106
* Entry condition: ostart = ofinish
107
*
108
* Exit condition: ostart <= ofinish
109
*
110
* return (ofinish-ostart) (we have this many bytes for you)
111
* return 0 (no data now, but could have more later)
112
* return -1 (absolutely no more data)
113
*
114
* Note: Exceptions are only thrown after the stream is completely read.
115
* For AEAD ciphers a read() of any length will internally cause the
116
* whole stream to be read fully and verify the authentication tag before
117
* returning decrypted data or exceptions.
118
*/
119
private int getMoreData() throws IOException {
120
if (done) return -1;
121
int readin = input.read(ibuffer);
122
if (readin == -1) {
123
done = true;
124
try {
125
obuffer = cipher.doFinal();
126
} catch (IllegalBlockSizeException | BadPaddingException e) {
127
obuffer = null;
128
throw new IOException(e);
129
}
130
if (obuffer == null)
131
return -1;
132
else {
133
ostart = 0;
134
ofinish = obuffer.length;
135
return ofinish;
136
}
137
}
138
try {
139
obuffer = cipher.update(ibuffer, 0, readin);
140
} catch (IllegalStateException e) {
141
obuffer = null;
142
throw e;
143
}
144
ostart = 0;
145
if (obuffer == null)
146
ofinish = 0;
147
else ofinish = obuffer.length;
148
return ofinish;
149
}
150
151
/**
152
* Constructs a CipherInputStream from an InputStream and a
153
* Cipher.
154
* <br>Note: if the specified input stream or cipher is
155
* null, a NullPointerException may be thrown later when
156
* they are used.
157
* @param is the to-be-processed input stream
158
* @param c an initialized Cipher object
159
*/
160
public CipherInputStream(InputStream is, Cipher c) {
161
super(is);
162
input = is;
163
cipher = c;
164
}
165
166
/**
167
* Constructs a CipherInputStream from an InputStream without
168
* specifying a Cipher. This has the effect of constructing a
169
* CipherInputStream using a NullCipher.
170
* <br>Note: if the specified input stream is null, a
171
* NullPointerException may be thrown later when it is used.
172
* @param is the to-be-processed input stream
173
*/
174
protected CipherInputStream(InputStream is) {
175
super(is);
176
input = is;
177
cipher = new NullCipher();
178
}
179
180
/**
181
* Reads the next byte of data from this input stream. The value
182
* byte is returned as an <code>int</code> in the range
183
* <code>0</code> to <code>255</code>. If no byte is available
184
* because the end of the stream has been reached, the value
185
* <code>-1</code> is returned. This method blocks until input data
186
* is available, the end of the stream is detected, or an exception
187
* is thrown.
188
* <p>
189
*
190
* @return the next byte of data, or <code>-1</code> if the end of the
191
* stream is reached.
192
* @exception IOException if an I/O error occurs.
193
* @since JCE1.2
194
*/
195
public int read() throws IOException {
196
if (ostart >= ofinish) {
197
// we loop for new data as the spec says we are blocking
198
int i = 0;
199
while (i == 0) i = getMoreData();
200
if (i == -1) return -1;
201
}
202
return ((int) obuffer[ostart++] & 0xff);
203
};
204
205
/**
206
* Reads up to <code>b.length</code> bytes of data from this input
207
* stream into an array of bytes.
208
* <p>
209
* The <code>read</code> method of <code>InputStream</code> calls
210
* the <code>read</code> method of three arguments with the arguments
211
* <code>b</code>, <code>0</code>, and <code>b.length</code>.
212
*
213
* @param b the buffer into which the data is read.
214
* @return the total number of bytes read into the buffer, or
215
* <code>-1</code> is there is no more data because the end of
216
* the stream has been reached.
217
* @exception IOException if an I/O error occurs.
218
* @see java.io.InputStream#read(byte[], int, int)
219
* @since JCE1.2
220
*/
221
public int read(byte b[]) throws IOException {
222
return read(b, 0, b.length);
223
}
224
225
/**
226
* Reads up to <code>len</code> bytes of data from this input stream
227
* into an array of bytes. This method blocks until some input is
228
* available. If the first argument is <code>null,</code> up to
229
* <code>len</code> bytes are read and discarded.
230
*
231
* @param b the buffer into which the data is read.
232
* @param off the start offset in the destination array
233
* <code>buf</code>
234
* @param len the maximum number of bytes read.
235
* @return the total number of bytes read into the buffer, or
236
* <code>-1</code> if there is no more data because the end of
237
* the stream has been reached.
238
* @exception IOException if an I/O error occurs.
239
* @see java.io.InputStream#read()
240
* @since JCE1.2
241
*/
242
public int read(byte b[], int off, int len) throws IOException {
243
if (ostart >= ofinish) {
244
// we loop for new data as the spec says we are blocking
245
int i = 0;
246
while (i == 0) i = getMoreData();
247
if (i == -1) return -1;
248
}
249
if (len <= 0) {
250
return 0;
251
}
252
int available = ofinish - ostart;
253
if (len < available) available = len;
254
if (b != null) {
255
System.arraycopy(obuffer, ostart, b, off, available);
256
}
257
ostart = ostart + available;
258
return available;
259
}
260
261
/**
262
* Skips <code>n</code> bytes of input from the bytes that can be read
263
* from this input stream without blocking.
264
*
265
* <p>Fewer bytes than requested might be skipped.
266
* The actual number of bytes skipped is equal to <code>n</code> or
267
* the result of a call to
268
* {@link #available() available},
269
* whichever is smaller.
270
* If <code>n</code> is less than zero, no bytes are skipped.
271
*
272
* <p>The actual number of bytes skipped is returned.
273
*
274
* @param n the number of bytes to be skipped.
275
* @return the actual number of bytes skipped.
276
* @exception IOException if an I/O error occurs.
277
* @since JCE1.2
278
*/
279
public long skip(long n) throws IOException {
280
int available = ofinish - ostart;
281
if (n > available) {
282
n = available;
283
}
284
if (n < 0) {
285
return 0;
286
}
287
ostart += n;
288
return n;
289
}
290
291
/**
292
* Returns the number of bytes that can be read from this input
293
* stream without blocking. The <code>available</code> method of
294
* <code>InputStream</code> returns <code>0</code>. This method
295
* <B>should</B> be overridden by subclasses.
296
*
297
* @return the number of bytes that can be read from this input stream
298
* without blocking.
299
* @exception IOException if an I/O error occurs.
300
* @since JCE1.2
301
*/
302
public int available() throws IOException {
303
return (ofinish - ostart);
304
}
305
306
/**
307
* Closes this input stream and releases any system resources
308
* associated with the stream.
309
* <p>
310
* The <code>close</code> method of <code>CipherInputStream</code>
311
* calls the <code>close</code> method of its underlying input
312
* stream.
313
*
314
* @exception IOException if an I/O error occurs.
315
* @since JCE1.2
316
*/
317
public void close() throws IOException {
318
if (closed) {
319
return;
320
}
321
322
closed = true;
323
input.close();
324
325
// Throw away the unprocessed data and throw no crypto exceptions.
326
// AEAD ciphers are fully readed before closing. Any authentication
327
// exceptions would occur while reading.
328
if (!done) {
329
try {
330
cipher.doFinal();
331
}
332
catch (BadPaddingException | IllegalBlockSizeException ex) {
333
// Catch exceptions as the rest of the stream is unused.
334
}
335
}
336
ostart = 0;
337
ofinish = 0;
338
}
339
340
/**
341
* Tests if this input stream supports the <code>mark</code>
342
* and <code>reset</code> methods, which it does not.
343
*
344
* @return <code>false</code>, since this class does not support the
345
* <code>mark</code> and <code>reset</code> methods.
346
* @see java.io.InputStream#mark(int)
347
* @see java.io.InputStream#reset()
348
* @since JCE1.2
349
*/
350
public boolean markSupported() {
351
return false;
352
}
353
}
354
355