Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove PGP keys from the KeyRing #3207

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 1 addition & 22 deletions common/src/main/java/bisq/common/crypto/KeyRing.java
Expand Up @@ -20,18 +20,12 @@
import javax.inject.Inject;
import javax.inject.Singleton;

import org.bouncycastle.openpgp.PGPKeyPair;
import org.bouncycastle.openpgp.PGPPublicKey;

import java.security.KeyPair;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Nullable;

@Getter
@EqualsAndHashCode
@Slf4j
Expand All @@ -41,33 +35,18 @@ public final class KeyRing {
private final KeyPair encryptionKeyPair;
private final PubKeyRing pubKeyRing;

// We generate by default a PGP keypair but the user can set his own if he prefers.
// Not impl. yet but prepared in data structure
@Nullable
@Setter
// TODO remove Nullable once impl.
private PGPKeyPair pgpKeyPair;

@Inject
public KeyRing(KeyStorage keyStorage) {
if (keyStorage.allKeyFilesExist()) {
signatureKeyPair = keyStorage.loadKeyPair(KeyStorage.KeyEntry.MSG_SIGNATURE);
encryptionKeyPair = keyStorage.loadKeyPair(KeyStorage.KeyEntry.MSG_ENCRYPTION);

// TODO not impl
pgpKeyPair = keyStorage.loadPgpKeyPair(KeyStorage.KeyEntry.PGP);
} else {
// First time we create key pairs
signatureKeyPair = Sig.generateKeyPair();
encryptionKeyPair = Encryption.generateKeyPair();

// TODO not impl
pgpKeyPair = PGP.generateKeyPair();
keyStorage.saveKeyRing(this);
}
// TODO remove Nullable once impl.
final PGPPublicKey pgpPublicKey = pgpKeyPair != null ? pgpKeyPair.getPublicKey() : null;
pubKeyRing = new PubKeyRing(signatureKeyPair.getPublic(), encryptionKeyPair.getPublic(), pgpPublicKey);
pubKeyRing = new PubKeyRing(signatureKeyPair.getPublic(), encryptionKeyPair.getPublic());
}

// Don't print keys for security reasons
Expand Down
16 changes: 1 addition & 15 deletions common/src/main/java/bisq/common/crypto/KeyStorage.java
Expand Up @@ -24,8 +24,6 @@
import javax.inject.Named;
import javax.inject.Singleton;

import org.bouncycastle.openpgp.PGPKeyPair;

import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
Expand Down Expand Up @@ -54,8 +52,6 @@

import org.jetbrains.annotations.NotNull;

import javax.annotation.Nullable;

// TODO: use a password protection for key storage
@Singleton
public class KeyStorage {
Expand All @@ -65,9 +61,7 @@ public class KeyStorage {

public enum KeyEntry {
MSG_SIGNATURE("sig", Sig.KEY_ALGO),
MSG_ENCRYPTION("enc", Encryption.ASYM_KEY_ALGO),
// TODO not impl
PGP("pgp", null);
MSG_ENCRYPTION("enc", Encryption.ASYM_KEY_ALGO);

private final String fileName;
private final String algorithm;
Expand Down Expand Up @@ -111,14 +105,6 @@ private boolean fileExists(KeyEntry keyEntry) {
return new File(storageDir + "/" + keyEntry.getFileName() + ".key").exists();
}

// TODO not impl
@SuppressWarnings({"SameParameterValue", "SameReturnValue", "UnusedParameters"})
@Nullable
public PGPKeyPair loadPgpKeyPair(KeyEntry keyEntry) {
return null;
}


public KeyPair loadKeyPair(KeyEntry keyEntry) {
FileUtil.rollingBackup(storageDir, keyEntry.getFileName() + ".key", 20);
// long now = System.currentTimeMillis();
Expand Down
134 changes: 0 additions & 134 deletions common/src/main/java/bisq/common/crypto/PGP.java

This file was deleted.

29 changes: 6 additions & 23 deletions common/src/main/java/bisq/common/crypto/PubKeyRing.java
Expand Up @@ -25,16 +25,12 @@

import com.google.common.annotations.VisibleForTesting;

import org.bouncycastle.openpgp.PGPPublicKey;

import java.security.PublicKey;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Nullable;

/**
* Same as KeyRing but with public keys only.
* Used to send public keys over the wire to other peer.
Expand All @@ -45,22 +41,15 @@
public final class PubKeyRing implements NetworkPayload, UsedForTradeContractJson {
private final byte[] signaturePubKeyBytes;
private final byte[] encryptionPubKeyBytes;
@Nullable
private final String pgpPubKeyAsPem;

private transient PublicKey signaturePubKey;
private transient PublicKey encryptionPubKey;
@Nullable
private transient PGPPublicKey pgpPubKey;

public PubKeyRing(PublicKey signaturePubKey, PublicKey encryptionPubKey, @Nullable PGPPublicKey pgpPubKey) {
public PubKeyRing(PublicKey signaturePubKey, PublicKey encryptionPubKey) {
this.signaturePubKeyBytes = Sig.getPublicKeyBytes(signaturePubKey);
this.encryptionPubKeyBytes = Encryption.getPublicKeyBytes(encryptionPubKey);
this.pgpPubKeyAsPem = PGP.getPEMFromPubKey(pgpPubKey);

this.signaturePubKey = signaturePubKey;
this.encryptionPubKey = encryptionPubKey;
this.pgpPubKey = pgpPubKey;
}


Expand All @@ -69,38 +58,32 @@ public PubKeyRing(PublicKey signaturePubKey, PublicKey encryptionPubKey, @Nullab
///////////////////////////////////////////////////////////////////////////////////////////

@VisibleForTesting
public PubKeyRing(byte[] signaturePubKeyBytes, byte[] encryptionPubKeyBytes, @Nullable String pgpPubKeyAsPem) {
public PubKeyRing(byte[] signaturePubKeyBytes, byte[] encryptionPubKeyBytes) {
this.signaturePubKeyBytes = signaturePubKeyBytes;
this.encryptionPubKeyBytes = encryptionPubKeyBytes;
this.pgpPubKeyAsPem = pgpPubKeyAsPem;

signaturePubKey = Sig.getPublicKeyFromBytes(signaturePubKeyBytes);
encryptionPubKey = Encryption.getPublicKeyFromBytes(encryptionPubKeyBytes);
if (pgpPubKeyAsPem != null)
pgpPubKey = PGP.getPubKeyFromPem(pgpPubKeyAsPem);
}

@Override
public protobuf.PubKeyRing toProtoMessage() {
return protobuf.PubKeyRing.newBuilder()
.setSignaturePubKeyBytes(ByteString.copyFrom(signaturePubKeyBytes))
.setEncryptionPubKeyBytes(ByteString.copyFrom(encryptionPubKeyBytes))
.setPgpPubKeyAsPem(pgpPubKeyAsPem)
.build();
}

public static PubKeyRing fromProto(protobuf.PubKeyRing proto) {
return new PubKeyRing(proto.getSignaturePubKeyBytes().toByteArray(),
proto.getEncryptionPubKeyBytes().toByteArray(),
proto.getPgpPubKeyAsPem());
return new PubKeyRing(
proto.getSignaturePubKeyBytes().toByteArray(),
proto.getEncryptionPubKeyBytes().toByteArray());
}

@Override
public String toString() {
return "PubKeyRing{" +
"signaturePubKeyHex=" + Utilities.bytesAsHexString(signaturePubKeyBytes) +
", encryptionPubKeyHex=" + Utilities.bytesAsHexString(encryptionPubKeyBytes) +
", pgpPubKeyAsString=" + pgpPubKeyAsPem +
'}';
"}";
}
}
2 changes: 1 addition & 1 deletion common/src/main/proto/pb.proto
Expand Up @@ -410,7 +410,7 @@ message Peer {
message PubKeyRing {
bytes signature_pub_key_bytes = 1;
bytes encryption_pub_key_bytes = 2;
string pgp_pub_key_as_pem = 3;
reserved 3; // WAS: string pgp_pub_key_as_pem = 3;
}

message SealedAndSigned {
Expand Down
3 changes: 1 addition & 2 deletions core/src/test/java/bisq/core/arbitration/ArbitratorTest.java
Expand Up @@ -44,7 +44,7 @@ public static Arbitrator getArbitratorMock() {
return new Arbitrator(new NodeAddress("host", 1000),
getBytes(100),
"btcaddress",
new PubKeyRing(getBytes(100), getBytes(100), "key"),
new PubKeyRing(getBytes(100), getBytes(100)),
Lists.newArrayList(),
new Date().getTime(),
getBytes(100),
Expand All @@ -56,4 +56,3 @@ public static byte[] getBytes(@SuppressWarnings("SameParameterValue") int count)
return RandomUtils.nextBytes(count);
}
}

4 changes: 1 addition & 3 deletions core/src/test/java/bisq/core/arbitration/MediatorTest.java
Expand Up @@ -42,7 +42,7 @@ public void testRoundtrip() {

public static Mediator getMediatorMock() {
return new Mediator(new NodeAddress("host", 1000),
new PubKeyRing(getBytes(100), getBytes(100), "key"),
new PubKeyRing(getBytes(100), getBytes(100)),
Lists.newArrayList(),
new Date().getTime(),
getBytes(100),
Expand All @@ -51,6 +51,4 @@ public static Mediator getMediatorMock() {
"info",
null);
}


}