Repository: pgpainless/pgpainless
Issue: addSubkey() ignores key flags

PGPainless ignores the user set key flags when adding a subkey like follows:
```
        secretKey = PGPainless.modifyKeyRing(secretKey)
                .addSubKey(
                        KeySpec.getBuilder(KeyType.ECDH(EllipticCurve._BRAINPOOLP512R1))
                                .withKeyFlags(KeyFlag.ENCRYPT_COMMS, KeyFlag.ENCRYPT_STORAGE)
                                .withDefaultAlgorithms(),
                        subkeyPassphrase,
                        protector)
                .done();
```

Instead of `ENCRYPT_COMMS, ENCRYPT_STORAGE`, the subkey is carrying the flag `CERTIFY_OTHER`, as it erroneously inherits the key flags from the primary key.

This is a problem with the API.

Changed files:
pgpainless-core/src/main/java/org/pgpainless/key/modification/secretkeyring/SecretKeyRingEditor.java
pgpainless-core/src/main/java/org/pgpainless/key/modification/secretkeyring/SecretKeyRingEditorInterface.java
pgpainless-core/src/test/java/org/pgpainless/key/modification/AddSubKeyTest.java
pgpainless-core/src/main/java/org/pgpainless/key/generation/KeySpec.java

diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeySpec.java b/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeySpec.java
index 60639849..4ecce7be 100644
--- a/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeySpec.java
+++ b/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeySpec.java
@@ -42,7 +42,7 @@ public class KeySpec {
     }
 
     @Nullable
-    PGPSignatureSubpacketVector getSubpackets() {
+    public PGPSignatureSubpacketVector getSubpackets() {
         return subpacketGenerator != null ? subpacketGenerator.generate() : null;
     }
 
diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/modification/secretkeyring/SecretKeyRingEditor.java b/pgpainless-core/src/main/java/org/pgpainless/key/modification/secretkeyring/SecretKeyRingEditor.java
index ee8fca9f..7a86c875 100644
--- a/pgpainless-core/src/main/java/org/pgpainless/key/modification/secretkeyring/SecretKeyRingEditor.java
+++ b/pgpainless-core/src/main/java/org/pgpainless/key/modification/secretkeyring/SecretKeyRingEditor.java
@@ -167,12 +167,16 @@ public class SecretKeyRingEditor implements SecretKeyRingEditorInterface {
         PGPSecretKey secretSubKey = generateSubKey(keySpec, subKeyPassphrase);
         SecretKeyRingProtector subKeyProtector = PasswordBasedSecretKeyRingProtector
                 .forKey(secretSubKey, subKeyPassphrase);
+        PGPSignatureSubpacketVector hashedSubpackets = keySpec.getSubpackets();
+        PGPSignatureSubpacketVector unhashedSubpackets = null;
 
-        return addSubKey(secretSubKey, subKeyProtector, secretKeyRingProtector);
+        return addSubKey(secretSubKey, hashedSubpackets, unhashedSubpackets, subKeyProtector, secretKeyRingProtector);
     }
 
     @Override
     public SecretKeyRingEditorInterface addSubKey(PGPSecretKey secretSubKey,
+                                                  PGPSignatureSubpacketVector hashedSubpackets,
+                                                  PGPSignatureSubpacketVector unhashedSubpackets,
                                                   SecretKeyRingProtector subKeyProtector,
                                                   SecretKeyRingProtector keyRingProtector)
             throws PGPException {
@@ -196,7 +200,7 @@ public class SecretKeyRingEditor implements SecretKeyRingEditorInterface {
         PGPKeyRingGenerator keyRingGenerator = new PGPKeyRingGenerator(
                 secretKeyRing, ringDecryptor, digestCalculator, contentSignerBuilder, subKeyEncryptor);
 
-        keyRingGenerator.addSubKey(subKeyPair);
+        keyRingGenerator.addSubKey(subKeyPair, hashedSubpackets, unhashedSubpackets);
         secretKeyRing = keyRingGenerator.generateSecretKeyRing();
 
         return this;
diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/modification/secretkeyring/SecretKeyRingEditorInterface.java b/pgpainless-core/src/main/java/org/pgpainless/key/modification/secretkeyring/SecretKeyRingEditorInterface.java
index b5ce6723..8f936636 100644
--- a/pgpainless-core/src/main/java/org/pgpainless/key/modification/secretkeyring/SecretKeyRingEditorInterface.java
+++ b/pgpainless-core/src/main/java/org/pgpainless/key/modification/secretkeyring/SecretKeyRingEditorInterface.java
@@ -25,6 +25,7 @@ import org.bouncycastle.openpgp.PGPException;
 import org.bouncycastle.openpgp.PGPSecretKey;
 import org.bouncycastle.openpgp.PGPSecretKeyRing;
 import org.bouncycastle.openpgp.PGPSignature;
+import org.bouncycastle.openpgp.PGPSignatureSubpacketVector;
 import org.pgpainless.key.OpenPgpV4Fingerprint;
 import org.pgpainless.key.generation.KeySpec;
 import org.pgpainless.key.protection.KeyRingProtectionSettings;
@@ -99,7 +100,10 @@ public interface SecretKeyRingEditorInterface {
                                            SecretKeyRingProtector secretKeyRingProtector)
             throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException;
 
-    SecretKeyRingEditorInterface addSubKey(PGPSecretKey subKey, SecretKeyRingProtector subKeyProtector, SecretKeyRingProtector keyRingProtector)
+    SecretKeyRingEditorInterface addSubKey(PGPSecretKey subKey,
+                                           PGPSignatureSubpacketVector hashedSubpackets,
+                                           PGPSignatureSubpacketVector unhashedSubpackets,
+                                           SecretKeyRingProtector subKeyProtector, SecretKeyRingProtector keyRingProtector)
             throws PGPException;
 
     /**
diff --git a/pgpainless-core/src/test/java/org/pgpainless/key/modification/AddSubKeyTest.java b/pgpainless-core/src/test/java/org/pgpainless/key/modification/AddSubKeyTest.java
index 814f0d65..bb2dca10 100644
--- a/pgpainless-core/src/test/java/org/pgpainless/key/modification/AddSubKeyTest.java
+++ b/pgpainless-core/src/test/java/org/pgpainless/key/modification/AddSubKeyTest.java
@@ -15,12 +15,14 @@
  */
 package org.pgpainless.key.modification;
 
+import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotEquals;
 
 import java.io.IOException;
 import java.security.InvalidAlgorithmParameterException;
 import java.security.NoSuchAlgorithmException;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
 
@@ -38,6 +40,7 @@ import org.pgpainless.key.TestKeys;
 import org.pgpainless.key.generation.KeySpec;
 import org.pgpainless.key.generation.type.ecc.EllipticCurve;
 import org.pgpainless.key.generation.type.ecc.ecdsa.ECDSA;
+import org.pgpainless.key.info.KeyRingInfo;
 import org.pgpainless.key.protection.PasswordBasedSecretKeyRingProtector;
 import org.pgpainless.key.protection.SecretKeyRingProtector;
 import org.pgpainless.key.protection.UnlockSecretKey;
@@ -78,5 +81,8 @@ public class AddSubKeyTest {
         SecretKeyRingProtector protector = SecretKeyRingProtector.unlockAllKeysWith(
                 Passphrase.fromPassword("subKeyPassphrase"), secretKeys);
         PGPPrivateKey privateKey = UnlockSecretKey.unlockSecretKey(subKey, protector);
+
+        KeyRingInfo info = new KeyRingInfo(secretKeys);
+        assertEquals(Collections.singletonList(KeyFlag.SIGN_DATA), info.getKeyFlagsOf(subKeyId));
     }
 }