mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-12-25 12:27:58 +01:00
Add test for adding image to key
This commit is contained in:
parent
a40c6ac755
commit
b164900a59
3 changed files with 39 additions and 24 deletions
|
@ -4,8 +4,10 @@
|
||||||
|
|
||||||
package org.pgpainless.key.generation
|
package org.pgpainless.key.generation
|
||||||
|
|
||||||
import org.bouncycastle.bcpg.attr.ImageAttribute
|
import java.io.IOException
|
||||||
|
import java.io.InputStream
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
import org.bouncycastle.bcpg.attr.ImageAttribute
|
||||||
import org.bouncycastle.openpgp.PGPSecretKey
|
import org.bouncycastle.openpgp.PGPSecretKey
|
||||||
import org.bouncycastle.openpgp.PGPSecretKeyRing
|
import org.bouncycastle.openpgp.PGPSecretKeyRing
|
||||||
import org.bouncycastle.openpgp.PGPUserAttributeSubpacketVector
|
import org.bouncycastle.openpgp.PGPUserAttributeSubpacketVector
|
||||||
|
@ -19,8 +21,6 @@ import org.pgpainless.key.generation.type.KeyType
|
||||||
import org.pgpainless.key.protection.SecretKeyRingProtector
|
import org.pgpainless.key.protection.SecretKeyRingProtector
|
||||||
import org.pgpainless.policy.Policy
|
import org.pgpainless.policy.Policy
|
||||||
import org.pgpainless.signature.subpackets.SelfSignatureSubpackets
|
import org.pgpainless.signature.subpackets.SelfSignatureSubpackets
|
||||||
import java.io.File
|
|
||||||
import java.io.IOException
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* OpenPGP key builder. This implementation supersedes the old [KeyRingBuilder].
|
* OpenPGP key builder. This implementation supersedes the old [KeyRingBuilder].
|
||||||
|
@ -41,16 +41,14 @@ open class GenerateOpenPgpKey(
|
||||||
protected val preferences: AlgorithmSuite
|
protected val preferences: AlgorithmSuite
|
||||||
) {
|
) {
|
||||||
|
|
||||||
/**
|
/** Make sure, that the chosen [KeyType] is allowed. */
|
||||||
* Make sure, that the chosen [KeyType] is allowed.
|
|
||||||
*/
|
|
||||||
open fun sanitizePublicKeyAlgorithms(keyType: KeyType, policy: Policy) {
|
open fun sanitizePublicKeyAlgorithms(keyType: KeyType, policy: Policy) {
|
||||||
verifyAlgorithmComplianceWithPolicy(keyType, policy)
|
verifyAlgorithmComplianceWithPolicy(keyType, policy)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Make sure, that the chosen [KeyType] complies to the given [Policy] by comparing it to the
|
* Make sure, that the chosen [KeyType] complies to the given [Policy] by comparing it to
|
||||||
* [Policy.PublicKeyAlgorithmPolicy].
|
* the [Policy.PublicKeyAlgorithmPolicy].
|
||||||
*
|
*
|
||||||
* @throws IllegalArgumentException if [keyType] fails to be accepted by [policy]
|
* @throws IllegalArgumentException if [keyType] fails to be accepted by [policy]
|
||||||
*/
|
*/
|
||||||
|
@ -73,7 +71,8 @@ open class GenerateOpenPgpKey(
|
||||||
fun buildV4Key(
|
fun buildV4Key(
|
||||||
keyType: KeyType,
|
keyType: KeyType,
|
||||||
flags: List<KeyFlag>? = listOf(KeyFlag.CERTIFY_OTHER)
|
flags: List<KeyFlag>? = listOf(KeyFlag.CERTIFY_OTHER)
|
||||||
): V4GenerateOpenPgpKey = V4GenerateOpenPgpKey(keyType, flags, policy, referenceTime, preferences)
|
): V4GenerateOpenPgpKey =
|
||||||
|
V4GenerateOpenPgpKey(keyType, flags, policy, referenceTime, preferences)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builder for version 4 OpenPGP keys.
|
* Builder for version 4 OpenPGP keys.
|
||||||
|
@ -146,24 +145,22 @@ open class GenerateOpenPgpKey(
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add the contents of a JPEG file as image attribute to the key.
|
* Add the contents of a JPEG input stream as image attribute to the key.
|
||||||
*
|
*
|
||||||
* @param jpegFile file containing a JPEG image
|
* @param jpegInputStream input stream containing a JPEG image
|
||||||
* @param subpacketsCallback callback to modify the user-attribute binding signature subpackets.
|
* @param subpacketsCallback callback to modify the user-attribute binding signature
|
||||||
|
* subpackets.
|
||||||
* @return this
|
* @return this
|
||||||
*/
|
*/
|
||||||
@Throws(IOException::class)
|
@Throws(IOException::class)
|
||||||
fun addJpegImage(
|
fun addJpegImage(
|
||||||
jpegFile: File,
|
jpegInputStream: InputStream,
|
||||||
subpacketsCallback: SelfSignatureSubpackets.Callback = SelfSignatureSubpackets.nop()
|
subpacketsCallback: SelfSignatureSubpackets.Callback = SelfSignatureSubpackets.nop()
|
||||||
) = apply {
|
) = apply {
|
||||||
jpegFile.inputStream()
|
PGPUserAttributeSubpacketVectorGenerator()
|
||||||
.let { Streams.readAll(it) }
|
.apply { setImageAttribute(ImageAttribute.JPEG, Streams.readAll(jpegInputStream)) }
|
||||||
.let {
|
.generate()
|
||||||
PGPUserAttributeSubpacketVectorGenerator().apply {
|
.let { addUserAttribute(it, subpacketsCallback) }
|
||||||
setImageAttribute(ImageAttribute.JPEG, it)
|
|
||||||
}.generate()
|
|
||||||
}.let { addUserAttribute(it, subpacketsCallback) }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1,9 +1,15 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Paul Schaub <vanitasvitae@fsfe.org>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
package org.pgpainless.key.generation
|
package org.pgpainless.key.generation
|
||||||
|
|
||||||
import org.bouncycastle.bcpg.attr.ImageAttribute
|
import org.bouncycastle.bcpg.attr.ImageAttribute
|
||||||
import org.bouncycastle.openpgp.PGPUserAttributeSubpacketVectorGenerator
|
import org.bouncycastle.openpgp.PGPUserAttributeSubpacketVectorGenerator
|
||||||
|
import org.junit.jupiter.api.Assertions.assertTrue
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
import org.junit.jupiter.api.assertThrows
|
import org.junit.jupiter.api.assertThrows
|
||||||
|
import org.opentest4j.TestAbortedException
|
||||||
import org.pgpainless.PGPainless
|
import org.pgpainless.PGPainless
|
||||||
import org.pgpainless.algorithm.KeyFlag
|
import org.pgpainless.algorithm.KeyFlag
|
||||||
import org.pgpainless.algorithm.PublicKeyAlgorithm
|
import org.pgpainless.algorithm.PublicKeyAlgorithm
|
||||||
|
@ -14,6 +20,7 @@ import org.pgpainless.key.generation.type.xdh.XDHSpec
|
||||||
import org.pgpainless.key.protection.SecretKeyRingProtector
|
import org.pgpainless.key.protection.SecretKeyRingProtector
|
||||||
import org.pgpainless.policy.Policy
|
import org.pgpainless.policy.Policy
|
||||||
import org.pgpainless.util.DateUtil
|
import org.pgpainless.util.DateUtil
|
||||||
|
import java.io.InputStream
|
||||||
|
|
||||||
class GenerateOpenPgpKeyTest {
|
class GenerateOpenPgpKeyTest {
|
||||||
|
|
||||||
|
@ -27,7 +34,8 @@ class GenerateOpenPgpKeyTest {
|
||||||
.addUserAttribute(
|
.addUserAttribute(
|
||||||
PGPUserAttributeSubpacketVectorGenerator()
|
PGPUserAttributeSubpacketVectorGenerator()
|
||||||
.apply { setImageAttribute(ImageAttribute.JPEG, byteArrayOf()) }
|
.apply { setImageAttribute(ImageAttribute.JPEG, byteArrayOf()) }
|
||||||
.generate())
|
.generate(),
|
||||||
|
)
|
||||||
.addEncryptionSubkey(KeyType.XDH(XDHSpec._X25519))
|
.addEncryptionSubkey(KeyType.XDH(XDHSpec._X25519))
|
||||||
.addSigningSubkey(KeyType.EDDSA(EdDSACurve._Ed25519))
|
.addSigningSubkey(KeyType.EDDSA(EdDSACurve._Ed25519))
|
||||||
.build(SecretKeyRingProtector.unprotectedKeys())
|
.build(SecretKeyRingProtector.unprotectedKeys())
|
||||||
|
@ -59,7 +67,8 @@ class GenerateOpenPgpKeyTest {
|
||||||
val policy =
|
val policy =
|
||||||
Policy(
|
Policy(
|
||||||
publicKeyAlgorithmPolicy =
|
publicKeyAlgorithmPolicy =
|
||||||
Policy.PublicKeyAlgorithmPolicy(mapOf(PublicKeyAlgorithm.RSA_GENERAL to 4096)))
|
Policy.PublicKeyAlgorithmPolicy(mapOf(PublicKeyAlgorithm.RSA_GENERAL to 4096)),
|
||||||
|
)
|
||||||
val builder = GenerateOpenPgpKey(policy)
|
val builder = GenerateOpenPgpKey(policy)
|
||||||
|
|
||||||
assertThrows<IllegalArgumentException> {
|
assertThrows<IllegalArgumentException> {
|
||||||
|
@ -74,8 +83,17 @@ class GenerateOpenPgpKeyTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testKeyGenerationWithJPEGAttribute() {
|
fun testKeyGenerationWithJPEGAttribute() {
|
||||||
GenerateOpenPgpKey(Policy.getInstance())
|
val key = GenerateOpenPgpKey(Policy.getInstance())
|
||||||
.buildV4Key(KeyType.EDDSA(EdDSACurve._Ed25519))
|
.buildV4Key(KeyType.EDDSA(EdDSACurve._Ed25519))
|
||||||
.addJpegImage()
|
.addJpegImage(requireResource("suzanne.jpg"))
|
||||||
|
.build()
|
||||||
|
|
||||||
|
assertTrue(key.publicKey.userAttributes.hasNext())
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun requireResource(resourceName: String): InputStream {
|
||||||
|
return javaClass.classLoader.getResourceAsStream(resourceName)
|
||||||
|
?: throw TestAbortedException(
|
||||||
|
"Cannot read resource $resourceName: InputStream is null.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
BIN
pgpainless-core/src/test/resources/suzanne.jpg
Normal file
BIN
pgpainless-core/src/test/resources/suzanne.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 23 KiB |
Loading…
Reference in a new issue