From a40c6ac75518b276d8f68014c831bdea8a36d648 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Fri, 2 Feb 2024 16:01:47 +0100 Subject: [PATCH] WIP: Add method to add image attribute --- .../key/generation/GenerateOpenPgpKey.kt | 26 +++++++++++++++++++ .../key/generation/GenerateOpenPgpKeyTest.kt | 7 +++++ 2 files changed, 33 insertions(+) diff --git a/pgpainless-core/src/main/kotlin/org/pgpainless/key/generation/GenerateOpenPgpKey.kt b/pgpainless-core/src/main/kotlin/org/pgpainless/key/generation/GenerateOpenPgpKey.kt index 9419693d..726ff644 100644 --- a/pgpainless-core/src/main/kotlin/org/pgpainless/key/generation/GenerateOpenPgpKey.kt +++ b/pgpainless-core/src/main/kotlin/org/pgpainless/key/generation/GenerateOpenPgpKey.kt @@ -4,11 +4,14 @@ package org.pgpainless.key.generation +import org.bouncycastle.bcpg.attr.ImageAttribute import java.util.* import org.bouncycastle.openpgp.PGPSecretKey import org.bouncycastle.openpgp.PGPSecretKeyRing import org.bouncycastle.openpgp.PGPUserAttributeSubpacketVector +import org.bouncycastle.openpgp.PGPUserAttributeSubpacketVectorGenerator import org.bouncycastle.openpgp.operator.PBESecretKeyEncryptor +import org.bouncycastle.util.io.Streams import org.pgpainless.algorithm.AlgorithmSuite import org.pgpainless.algorithm.KeyFlag import org.pgpainless.implementation.ImplementationFactory @@ -16,6 +19,8 @@ import org.pgpainless.key.generation.type.KeyType import org.pgpainless.key.protection.SecretKeyRingProtector import org.pgpainless.policy.Policy import org.pgpainless.signature.subpackets.SelfSignatureSubpackets +import java.io.File +import java.io.IOException /** * OpenPGP key builder. This implementation supersedes the old [KeyRingBuilder]. @@ -140,6 +145,27 @@ open class GenerateOpenPgpKey( attribute, subpacketsCallback = preferencesCallback.then(subpacketsCallback)) } + /** + * Add the contents of a JPEG file as image attribute to the key. + * + * @param jpegFile file containing a JPEG image + * @param subpacketsCallback callback to modify the user-attribute binding signature subpackets. + * @return this + */ + @Throws(IOException::class) + fun addJpegImage( + jpegFile: File, + subpacketsCallback: SelfSignatureSubpackets.Callback = SelfSignatureSubpackets.nop() + ) = apply { + jpegFile.inputStream() + .let { Streams.readAll(it) } + .let { + PGPUserAttributeSubpacketVectorGenerator().apply { + setImageAttribute(ImageAttribute.JPEG, it) + }.generate() + }.let { addUserAttribute(it, subpacketsCallback) } + } + /** * Add a subkey to the key. The subpackets of the binding signature will be populated with * issuer information, the passed in [bindingTime] as signature creation time and given diff --git a/pgpainless-core/src/test/kotlin/org/pgpainless/key/generation/GenerateOpenPgpKeyTest.kt b/pgpainless-core/src/test/kotlin/org/pgpainless/key/generation/GenerateOpenPgpKeyTest.kt index 65a3956b..978924c4 100644 --- a/pgpainless-core/src/test/kotlin/org/pgpainless/key/generation/GenerateOpenPgpKeyTest.kt +++ b/pgpainless-core/src/test/kotlin/org/pgpainless/key/generation/GenerateOpenPgpKeyTest.kt @@ -71,4 +71,11 @@ class GenerateOpenPgpKeyTest { v4Builder.addSigningSubkey(KeyType.RSA(RsaLength._2048)) // too weak } } + + @Test + fun testKeyGenerationWithJPEGAttribute() { + GenerateOpenPgpKey(Policy.getInstance()) + .buildV4Key(KeyType.EDDSA(EdDSACurve._Ed25519)) + .addJpegImage() + } }