Restoring PGPPublicKeyRing from bytes discards additional PGPPublicKeys

Paul Schaub <[email protected]>
Newsgroups gmane.comp.encryption.bouncy-castle.devel
Message-ID <[email protected]>
Hi!

In my project I wrote code to generate an elliptic curve key ring. A key
ring consists of an ECDSA key pair used for signing and a second ECDHE
key pair used for encryption.

When I serialize the key ring and create a new instance from bytes, the
key ring only contains the first key pair. The second one gets discarded.

PGPPublicKeyRing ring = ...
byte[] bytes = ring.getEncoded(true);
PGPPublicKeyRing parsed = new PGPPublicKeyRing(bytes);

By stepping through the execution, I figured, that the cause of this
issue is probably, that the constructor of PGPPublicKey only checks for
the packet tag of a PUBLIC_SUBKEY, while the second key has the packet
type PUBLIC_KEY.
See
https://github.com/bcgit/bc-java/blob/master/pg/src/main/java/org/bouncycastle/openpgp/PGPPublicKeyRing.java#L114

I suspect, that the constructor of PGPPublicKey is probably doing the
right thing, but the question is, what did I wrong then when creating my
key ring / why does the getEncoded() method use PUBLIC_KEY as packet tag
instead of PUBLIC_SUBKEY?

I added the subkey as follows:

PGPKeyRingGenerator ringGenerator = new PGPKeyRingGenerator(
    PGPSignature.POSITIVE_CERTIFICATION, certKey,
    userId, calculator,
    hashedSubPackets, null, signer, encryptor);

PGPKeyPair subKey = generateKeyPair(subKeySpec); // Generates a PGPKey pair
ringGenerator.addSubKey(subKey);

My code can be found here:
https://github.com/vanitasvitae/pgpainless/blob/master/src/main/java/org/pgpainless/pgpainless/key/generation/KeyRingBuilder.java

Paul
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.