2021-10-07 15:48:52 +02:00
|
|
|
// SPDX-FileCopyrightText: 2018 Paul Schaub <vanitasvitae@fsfe.org>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2018-07-18 18:23:06 +02:00
|
|
|
package org.pgpainless.decryption_verification;
|
2018-06-11 01:33:49 +02:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2021-04-26 13:38:12 +02:00
|
|
|
import javax.annotation.Nonnull;
|
2018-06-11 01:33:49 +02:00
|
|
|
|
2021-07-27 15:09:59 +02:00
|
|
|
import org.bouncycastle.util.io.Streams;
|
2020-08-24 14:55:06 +02:00
|
|
|
|
2021-04-25 13:34:30 +02:00
|
|
|
/**
|
|
|
|
* Decryption Stream that handles updating and verification of detached signatures,
|
|
|
|
* as well as verification of integrity-protected input streams once the stream gets closed.
|
|
|
|
*/
|
2021-09-27 11:47:54 +02:00
|
|
|
public class DecryptionStream extends CloseForResultInputStream {
|
2018-06-11 01:33:49 +02:00
|
|
|
|
|
|
|
private final InputStream inputStream;
|
2021-09-06 15:14:13 +02:00
|
|
|
private final IntegrityProtectedInputStream integrityProtectedInputStream;
|
2021-07-27 15:09:59 +02:00
|
|
|
private final InputStream armorStream;
|
2018-06-11 01:33:49 +02:00
|
|
|
|
2021-09-06 15:14:13 +02:00
|
|
|
/**
|
|
|
|
* Create an input stream that handles decryption and - if necessary - integrity protection verification.
|
|
|
|
*
|
|
|
|
* @param wrapped underlying input stream
|
|
|
|
* @param resultBuilder builder for decryption metadata like algorithms, recipients etc.
|
|
|
|
* @param integrityProtectedInputStream in case of data encrypted using SEIP packet close this stream to check integrity
|
|
|
|
* @param armorStream armor stream to verify CRC checksums
|
|
|
|
*/
|
2021-09-02 18:01:06 +02:00
|
|
|
DecryptionStream(@Nonnull InputStream wrapped,
|
2021-08-18 12:55:24 +02:00
|
|
|
@Nonnull OpenPgpMetadata.Builder resultBuilder,
|
2021-09-06 15:14:13 +02:00
|
|
|
IntegrityProtectedInputStream integrityProtectedInputStream,
|
2021-07-27 15:09:59 +02:00
|
|
|
InputStream armorStream) {
|
2021-09-27 11:47:54 +02:00
|
|
|
super(resultBuilder);
|
2018-06-11 01:33:49 +02:00
|
|
|
this.inputStream = wrapped;
|
2021-09-06 15:14:13 +02:00
|
|
|
this.integrityProtectedInputStream = integrityProtectedInputStream;
|
2021-07-27 15:09:59 +02:00
|
|
|
this.armorStream = armorStream;
|
2018-06-11 01:33:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void close() throws IOException {
|
2021-07-27 15:09:59 +02:00
|
|
|
if (armorStream != null) {
|
|
|
|
Streams.drain(armorStream);
|
|
|
|
}
|
2018-06-11 01:33:49 +02:00
|
|
|
inputStream.close();
|
2021-09-06 15:14:13 +02:00
|
|
|
if (integrityProtectedInputStream != null) {
|
|
|
|
integrityProtectedInputStream.close();
|
2021-02-17 21:04:05 +01:00
|
|
|
}
|
2021-09-27 11:47:54 +02:00
|
|
|
super.close();
|
2018-06-11 01:33:49 +02:00
|
|
|
}
|
|
|
|
|
2021-09-27 11:47:54 +02:00
|
|
|
@Override
|
|
|
|
public int read() throws IOException {
|
|
|
|
int r = inputStream.read();
|
|
|
|
return r;
|
2018-06-11 01:33:49 +02:00
|
|
|
}
|
2021-09-27 11:47:54 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public int read(@Nonnull byte[] bytes, int offset, int length) throws IOException {
|
|
|
|
int read = inputStream.read(bytes, offset, length);
|
|
|
|
return read;
|
|
|
|
}
|
|
|
|
|
2018-06-11 01:33:49 +02:00
|
|
|
}
|