1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-26 22:32:07 +01:00

Use ArmoredOutputStreamFactory to hide version string in ascii armor

Partially fixes #82
This commit is contained in:
Paul Schaub 2021-02-19 19:50:36 +01:00
parent ea89289852
commit c75a192513
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
9 changed files with 51 additions and 10 deletions

View file

@ -51,6 +51,7 @@ import org.pgpainless.decryption_verification.DetachedSignature;
import org.pgpainless.decryption_verification.OpenPgpMetadata; import org.pgpainless.decryption_verification.OpenPgpMetadata;
import org.pgpainless.implementation.ImplementationFactory; import org.pgpainless.implementation.ImplementationFactory;
import org.pgpainless.key.OpenPgpV4Fingerprint; import org.pgpainless.key.OpenPgpV4Fingerprint;
import org.pgpainless.util.ArmoredOutputStreamFactory;
import org.pgpainless.util.Passphrase; import org.pgpainless.util.Passphrase;
/** /**
@ -146,7 +147,7 @@ public final class EncryptionStream extends OutputStream {
} }
LOGGER.log(LEVEL, "Wrap encryption output in ASCII armor"); LOGGER.log(LEVEL, "Wrap encryption output in ASCII armor");
armorOutputStream = new ArmoredOutputStream(outermostStream); armorOutputStream = ArmoredOutputStreamFactory.get(outermostStream);
outermostStream = armorOutputStream; outermostStream = armorOutputStream;
} }

View file

@ -41,7 +41,7 @@ public class ArmorUtils {
public static String toAsciiArmoredString(InputStream inputStream) throws IOException { public static String toAsciiArmoredString(InputStream inputStream) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
ArmoredOutputStream armor = new ArmoredOutputStream(out); ArmoredOutputStream armor = ArmoredOutputStreamFactory.get(out);
Streams.pipeAll(inputStream, armor); Streams.pipeAll(inputStream, armor);
armor.close(); armor.close();

View file

@ -0,0 +1,34 @@
/*
* Copyright 2021 Paul Schaub.
*
* 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.
*/
package org.pgpainless.util;
import java.io.OutputStream;
import org.bouncycastle.bcpg.ArmoredOutputStream;
/**
* Factory to create configured {@link ArmoredOutputStream ArmoredOutputStreams}.
*/
public class ArmoredOutputStreamFactory {
public static final String VERSION = "PGPainless";
public static ArmoredOutputStream get(OutputStream outputStream) {
ArmoredOutputStream armoredOutputStream = new ArmoredOutputStream(outputStream);
armoredOutputStream.setHeader(ArmoredOutputStream.VERSION_HDR, VERSION);
return armoredOutputStream;
}
}

View file

@ -52,6 +52,7 @@ import org.pgpainless.key.generation.type.rsa.RsaLength;
import org.pgpainless.key.protection.SecretKeyRingProtector; import org.pgpainless.key.protection.SecretKeyRingProtector;
import org.pgpainless.key.protection.UnprotectedKeysProtector; import org.pgpainless.key.protection.UnprotectedKeysProtector;
import org.pgpainless.key.util.KeyRingUtils; import org.pgpainless.key.util.KeyRingUtils;
import org.pgpainless.util.ArmoredOutputStreamFactory;
import org.pgpainless.util.BCUtil; import org.pgpainless.util.BCUtil;
public class EncryptDecryptTest { public class EncryptDecryptTest {
@ -205,7 +206,7 @@ public class EncryptDecryptTest {
Set<PGPSignature> signatureSet = metadata.getSignatures(); Set<PGPSignature> signatureSet = metadata.getSignatures();
ByteArrayOutputStream sigOut = new ByteArrayOutputStream(); ByteArrayOutputStream sigOut = new ByteArrayOutputStream();
ArmoredOutputStream armorOut = new ArmoredOutputStream(sigOut); ArmoredOutputStream armorOut = ArmoredOutputStreamFactory.get(sigOut);
signatureSet.iterator().next().encode(armorOut); signatureSet.iterator().next().encode(armorOut);
armorOut.close(); armorOut.close();
String armorSig = sigOut.toString(); String armorSig = sigOut.toString();

View file

@ -30,6 +30,7 @@ import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless; import org.pgpainless.PGPainless;
import org.pgpainless.key.OpenPgpV4Fingerprint; import org.pgpainless.key.OpenPgpV4Fingerprint;
import org.pgpainless.key.util.KeyRingUtils; import org.pgpainless.key.util.KeyRingUtils;
import org.pgpainless.util.ArmoredOutputStreamFactory;
public class GenerateKeyTest { public class GenerateKeyTest {
@ -41,13 +42,13 @@ public class GenerateKeyTest {
PGPPublicKeyRing publicKeys = KeyRingUtils.publicKeyRingFrom(secretKeys); PGPPublicKeyRing publicKeys = KeyRingUtils.publicKeyRingFrom(secretKeys);
ByteArrayOutputStream bytes = new ByteArrayOutputStream(); ByteArrayOutputStream bytes = new ByteArrayOutputStream();
ArmoredOutputStream armor = new ArmoredOutputStream(bytes); ArmoredOutputStream armor = ArmoredOutputStreamFactory.get(bytes);
secretKeys.encode(armor); secretKeys.encode(armor);
armor.close(); armor.close();
String publicKey = new String(bytes.toByteArray()); String publicKey = new String(bytes.toByteArray());
bytes = new ByteArrayOutputStream(); bytes = new ByteArrayOutputStream();
armor = new ArmoredOutputStream(bytes); armor = ArmoredOutputStreamFactory.get(bytes);
secretKeys.encode(armor); secretKeys.encode(armor);
armor.close(); armor.close();
String privateKey = new String(bytes.toByteArray()); String privateKey = new String(bytes.toByteArray());

View file

@ -35,6 +35,7 @@ import org.pgpainless.algorithm.KeyFlag;
import org.pgpainless.key.generation.type.KeyType; import org.pgpainless.key.generation.type.KeyType;
import org.pgpainless.key.generation.type.rsa.RsaLength; import org.pgpainless.key.generation.type.rsa.RsaLength;
import org.pgpainless.key.util.KeyRingUtils; import org.pgpainless.key.util.KeyRingUtils;
import org.pgpainless.util.ArmoredOutputStreamFactory;
public class GenerateKeyWithAdditionalUserIdTest { public class GenerateKeyWithAdditionalUserIdTest {
@ -65,7 +66,7 @@ public class GenerateKeyWithAdditionalUserIdTest {
assertFalse(userIds.hasNext()); assertFalse(userIds.hasNext());
ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ArmoredOutputStream armor = new ArmoredOutputStream(byteOut); ArmoredOutputStream armor = ArmoredOutputStreamFactory.get(byteOut);
secretKeys.encode(armor); secretKeys.encode(armor);
armor.close(); armor.close();

View file

@ -28,6 +28,7 @@ import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless; import org.pgpainless.PGPainless;
import org.pgpainless.key.protection.UnprotectedKeysProtector; import org.pgpainless.key.protection.UnprotectedKeysProtector;
import org.pgpainless.util.ArmoredOutputStreamFactory;
/** /**
* Test that makes sure that PGPainless can deal with keys that carry a key * Test that makes sure that PGPainless can deal with keys that carry a key
@ -88,14 +89,14 @@ public class RevokeKeyWithGenericCertificationSignatureTest {
PGPPublicKey pkr = secretKeyRing.getPublicKeys().next(); PGPPublicKey pkr = secretKeyRing.getPublicKeys().next();
ByteArrayOutputStream pubOutBytes = new ByteArrayOutputStream(); ByteArrayOutputStream pubOutBytes = new ByteArrayOutputStream();
try (ArmoredOutputStream pubOut = new ArmoredOutputStream(pubOutBytes)) { try (ArmoredOutputStream pubOut = ArmoredOutputStreamFactory.get(pubOutBytes)) {
pkr.encode(pubOut); pkr.encode(pubOut);
} }
pubOutBytes.close(); pubOutBytes.close();
PGPSecretKey skr = secretKeyRing.getSecretKeys().next(); PGPSecretKey skr = secretKeyRing.getSecretKeys().next();
ByteArrayOutputStream secOutBytes = new ByteArrayOutputStream(); ByteArrayOutputStream secOutBytes = new ByteArrayOutputStream();
try (ArmoredOutputStream privOut = new ArmoredOutputStream(secOutBytes)) { try (ArmoredOutputStream privOut = ArmoredOutputStreamFactory.get(secOutBytes)) {
skr.encode(privOut); skr.encode(privOut);
} }
secOutBytes.close(); secOutBytes.close();

View file

@ -27,6 +27,7 @@ import org.bouncycastle.openpgp.PGPException;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless; import org.pgpainless.PGPainless;
import org.pgpainless.algorithm.SymmetricKeyAlgorithm; import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
import org.pgpainless.util.ArmoredOutputStreamFactory;
import org.pgpainless.util.Passphrase; import org.pgpainless.util.Passphrase;
public class LegacySymmetricEncryptionTest { public class LegacySymmetricEncryptionTest {
@ -52,7 +53,7 @@ public class LegacySymmetricEncryptionTest {
Passphrase passphrase = new Passphrase(password.toCharArray()); Passphrase passphrase = new Passphrase(password.toCharArray());
byte[] enc = PGPainless.encryptWithPassword(plain, passphrase, SymmetricKeyAlgorithm.AES_128); byte[] enc = PGPainless.encryptWithPassword(plain, passphrase, SymmetricKeyAlgorithm.AES_128);
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
ArmoredOutputStream armor = new ArmoredOutputStream(out); ArmoredOutputStream armor = ArmoredOutputStreamFactory.get(out);
armor.write(enc); armor.write(enc);
armor.flush(); armor.flush();
armor.close(); armor.close();

View file

@ -17,6 +17,7 @@ package org.pgpainless.sop.commands;
import org.bouncycastle.bcpg.ArmoredOutputStream; import org.bouncycastle.bcpg.ArmoredOutputStream;
import org.bouncycastle.util.io.Streams; import org.bouncycastle.util.io.Streams;
import org.pgpainless.util.ArmoredOutputStreamFactory;
import picocli.CommandLine; import picocli.CommandLine;
import java.io.IOException; import java.io.IOException;
@ -49,7 +50,7 @@ public class Armor implements Runnable {
@Override @Override
public void run() { public void run() {
try (PushbackInputStream pbIn = new PushbackInputStream(System.in); ArmoredOutputStream armoredOutputStream = new ArmoredOutputStream(System.out)) { try (PushbackInputStream pbIn = new PushbackInputStream(System.in); ArmoredOutputStream armoredOutputStream = ArmoredOutputStreamFactory.get(System.out)) {
byte[] start = new byte[14]; byte[] start = new byte[14];
int read = pbIn.read(start); int read = pbIn.read(start);
pbIn.unread(read); pbIn.unread(read);