mirror of
https://github.com/pgpainless/pgpainless.git
synced 2025-12-10 14:21:09 +01:00
WIP: Play around with TeeInputStreams
This commit is contained in:
parent
7537c9520c
commit
efdf2bca0d
3 changed files with 121 additions and 5 deletions
|
|
@ -343,7 +343,8 @@ public class OpenPgpMessageInputStream extends InputStream {
|
|||
return null;
|
||||
}
|
||||
|
||||
private PGPOnePassSignatureList readOnePassSignatures() throws IOException {
|
||||
private PGPOnePassSignatureListWrapper readOnePassSignatures() throws IOException {
|
||||
List<Boolean> encapsulating = new ArrayList<>();
|
||||
ByteArrayOutputStream buf = new ByteArrayOutputStream();
|
||||
BCPGOutputStream bcpgOut = new BCPGOutputStream(buf);
|
||||
int tag;
|
||||
|
|
@ -351,14 +352,16 @@ public class OpenPgpMessageInputStream extends InputStream {
|
|||
Packet packet = bcpgIn.readPacket();
|
||||
if (tag == PacketTags.ONE_PASS_SIGNATURE) {
|
||||
OnePassSignaturePacket sigPacket = (OnePassSignaturePacket) packet;
|
||||
sigPacket.encode(bcpgOut);
|
||||
byte[] bytes = sigPacket.getEncoded();
|
||||
encapsulating.add(bytes[bytes.length - 1] == 1);
|
||||
bcpgOut.write(bytes);
|
||||
}
|
||||
}
|
||||
bcpgOut.close();
|
||||
|
||||
PGPObjectFactory objectFactory = ImplementationFactory.getInstance().getPGPObjectFactory(buf.toByteArray());
|
||||
PGPOnePassSignatureList signatureList = (PGPOnePassSignatureList) objectFactory.nextObject();
|
||||
return signatureList;
|
||||
return new PGPOnePassSignatureListWrapper(signatureList, encapsulating);
|
||||
}
|
||||
|
||||
private PGPSignatureList readSignatures() throws IOException {
|
||||
|
|
@ -490,6 +493,26 @@ public class OpenPgpMessageInputStream extends InputStream {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Workaround for BC not exposing, whether an OPS is encapsulating or not.
|
||||
* TODO: Remove once our PR is merged
|
||||
*
|
||||
* @see <a href="https://github.com/bcgit/bc-java/pull/1232">PR against BC</a>
|
||||
*/
|
||||
private static class PGPOnePassSignatureListWrapper {
|
||||
private final PGPOnePassSignatureList list;
|
||||
private final List<Boolean> encapsulating;
|
||||
|
||||
public PGPOnePassSignatureListWrapper(PGPOnePassSignatureList signatures, List<Boolean> encapsulating) {
|
||||
this.list = signatures;
|
||||
this.encapsulating = encapsulating;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return list.size();
|
||||
}
|
||||
}
|
||||
|
||||
private static class Signatures {
|
||||
final ConsumerOptions options;
|
||||
List<PGPSignature> detachedSignatures = new ArrayList<>();
|
||||
|
|
@ -521,9 +544,9 @@ public class OpenPgpMessageInputStream extends InputStream {
|
|||
}
|
||||
}
|
||||
|
||||
void addOnePassSignatures(PGPOnePassSignatureList signatures) {
|
||||
void addOnePassSignatures(PGPOnePassSignatureListWrapper signatures) {
|
||||
System.out.println("Adding " + signatures.size() + " OPSs");
|
||||
for (PGPOnePassSignature ops : signatures) {
|
||||
for (PGPOnePassSignature ops : signatures.list) {
|
||||
PGPPublicKeyRing certificate = findCertificate(ops.getKeyID());
|
||||
initialize(ops, certificate);
|
||||
this.onePassSignatures.add(ops);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,35 @@
|
|||
package org.pgpainless.decryption_verification;
|
||||
|
||||
import org.bouncycastle.bcpg.BCPGInputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
public class TeeBCPGInputStream extends BCPGInputStream {
|
||||
|
||||
private final OutputStream out;
|
||||
|
||||
public TeeBCPGInputStream(InputStream in, OutputStream outputStream) {
|
||||
super(in);
|
||||
this.out = outputStream;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
int r = super.read();
|
||||
if (r != -1) {
|
||||
out.write(r);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(byte[] buf, int off, int len) throws IOException {
|
||||
int r = super.read(buf, off, len);
|
||||
if (r > 0) {
|
||||
out.write(buf, off, r);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue