2018-06-13 17:26:48 +02:00
|
|
|
/*
|
2020-08-24 16:26:29 +02:00
|
|
|
* Copyright 2018-2020 Paul Schaub.
|
2018-06-13 17:26:48 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2018-07-18 18:23:06 +02:00
|
|
|
package org.pgpainless.decryption_verification;
|
2018-06-11 01:33:49 +02:00
|
|
|
|
2018-07-31 20:09:16 +02:00
|
|
|
import javax.annotation.Nonnull;
|
2018-06-11 01:33:49 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2021-02-17 21:04:05 +01:00
|
|
|
import java.util.List;
|
2020-08-24 16:26:29 +02:00
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
2018-06-11 01:33:49 +02:00
|
|
|
|
2020-08-24 14:55:06 +02:00
|
|
|
import org.bouncycastle.openpgp.PGPException;
|
2021-02-17 21:04:05 +01:00
|
|
|
import org.pgpainless.util.IntegrityProtectedInputStream;
|
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.
|
|
|
|
*/
|
2018-06-11 01:33:49 +02:00
|
|
|
public class DecryptionStream extends InputStream {
|
|
|
|
|
2020-08-24 16:26:29 +02:00
|
|
|
private static final Logger LOGGER = Logger.getLogger(DecryptionStream.class.getName());
|
|
|
|
|
2018-06-11 01:33:49 +02:00
|
|
|
private final InputStream inputStream;
|
2018-07-23 16:23:23 +02:00
|
|
|
private final OpenPgpMetadata.Builder resultBuilder;
|
2018-06-11 01:33:49 +02:00
|
|
|
private boolean isClosed = false;
|
2021-02-17 21:04:05 +01:00
|
|
|
private List<IntegrityProtectedInputStream> integrityProtectedInputStreamList;
|
2018-06-11 01:33:49 +02:00
|
|
|
|
2021-02-17 21:04:05 +01:00
|
|
|
DecryptionStream(@Nonnull InputStream wrapped, @Nonnull OpenPgpMetadata.Builder resultBuilder,
|
|
|
|
List<IntegrityProtectedInputStream> integrityProtectedInputStreamList) {
|
2018-06-11 01:33:49 +02:00
|
|
|
this.inputStream = wrapped;
|
|
|
|
this.resultBuilder = resultBuilder;
|
2021-02-17 21:04:05 +01:00
|
|
|
this.integrityProtectedInputStreamList = integrityProtectedInputStreamList;
|
2018-06-11 01:33:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int read() throws IOException {
|
2020-08-24 14:55:06 +02:00
|
|
|
int r = inputStream.read();
|
|
|
|
maybeUpdateDetachedSignatures(r);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void maybeUpdateDetachedSignatures(int rByte) {
|
|
|
|
for (DetachedSignature s : resultBuilder.getDetachedSignatures()) {
|
|
|
|
if (rByte != -1) {
|
|
|
|
s.getSignature().update((byte) rByte);
|
|
|
|
}
|
|
|
|
}
|
2018-06-11 01:33:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void close() throws IOException {
|
|
|
|
inputStream.close();
|
2020-08-24 14:55:06 +02:00
|
|
|
maybeVerifyDetachedSignatures();
|
2021-02-17 21:04:05 +01:00
|
|
|
for (IntegrityProtectedInputStream s : integrityProtectedInputStreamList) {
|
|
|
|
s.close();
|
|
|
|
}
|
2018-06-11 01:33:49 +02:00
|
|
|
this.isClosed = true;
|
|
|
|
}
|
|
|
|
|
2021-04-25 13:34:30 +02:00
|
|
|
private void maybeVerifyDetachedSignatures() {
|
2020-08-24 14:55:06 +02:00
|
|
|
for (DetachedSignature s : resultBuilder.getDetachedSignatures()) {
|
|
|
|
try {
|
|
|
|
s.setVerified(s.getSignature().verify());
|
|
|
|
} catch (PGPException e) {
|
2020-08-24 16:26:29 +02:00
|
|
|
LOGGER.log(Level.WARNING, "Could not verify signature of key " + s.getFingerprint(), e);
|
2020-08-24 14:55:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-25 13:34:30 +02:00
|
|
|
/**
|
|
|
|
* Return the result of the decryption.
|
|
|
|
* The result contains metadata about the decryption, such as signatures, used keys and algorithms, as well as information
|
|
|
|
* about the decrypted file/stream.
|
|
|
|
*
|
|
|
|
* Can only be obtained once the stream got successfully closed ({@link #close()}).
|
|
|
|
* @return metadata
|
|
|
|
*/
|
2018-07-23 16:23:23 +02:00
|
|
|
public OpenPgpMetadata getResult() {
|
2018-06-11 01:33:49 +02:00
|
|
|
if (!isClosed) {
|
|
|
|
throw new IllegalStateException("DecryptionStream MUST be closed before the result can be accessed.");
|
|
|
|
}
|
|
|
|
return resultBuilder.build();
|
|
|
|
}
|
|
|
|
}
|