1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-09-09 10:19:39 +02:00

EncryptImpl: Emit session-key

This commit is contained in:
Paul Schaub 2025-06-16 13:34:37 +02:00
parent fdcdf6270f
commit e611311f2c
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
3 changed files with 17 additions and 2 deletions

View file

@ -17,9 +17,11 @@ import org.pgpainless.algorithm.SymmetricKeyAlgorithm
import org.pgpainless.bouncycastle.extensions.matches
import org.pgpainless.key.SubkeyIdentifier
import org.pgpainless.util.MultiMap
import org.pgpainless.util.SessionKey
data class EncryptionResult(
val encryptionMechanism: MessageEncryptionMechanism,
val sessionKey: SessionKey?,
val compressionAlgorithm: CompressionAlgorithm,
val detachedDocumentSignatures: OpenPGPSignatureSet<OpenPGPDocumentSignature>,
val recipients: Set<SubkeyIdentifier>,
@ -84,6 +86,7 @@ data class EncryptionResult(
private var _fileName = ""
private var _modificationDate = Date(0)
private var _encoding = StreamEncoding.BINARY
private var _sessionKey: SessionKey? = null
fun setEncryptionMechanism(mechanism: MessageEncryptionMechanism): Builder = apply {
_encryptionMechanism = mechanism
@ -105,6 +108,8 @@ data class EncryptionResult(
(recipients as MutableSet).add(recipient)
}
fun setSessionKey(sessionKey: SessionKey) = apply { _sessionKey = sessionKey }
fun addDetachedSignature(signature: OpenPGPDocumentSignature): Builder = apply {
detachedSignatures.add(signature)
}
@ -114,6 +119,7 @@ data class EncryptionResult(
return EncryptionResult(
_encryptionMechanism,
_sessionKey,
_compressionAlgorithm!!,
OpenPGPSignatureSet(detachedSignatures),
recipients,

View file

@ -20,6 +20,7 @@ import org.pgpainless.algorithm.CompressionAlgorithm
import org.pgpainless.algorithm.StreamEncoding
import org.pgpainless.bouncycastle.extensions.pgpDataEncryptorBuilder
import org.pgpainless.util.ArmoredOutputStreamFactory
import org.pgpainless.util.SessionKey
// 1 << 8 causes wrong partial body length encoding
// 1 << 9 fixes this.
@ -93,6 +94,11 @@ class EncryptionStream(
options.encryptionOptions.encryptionKeyIdentifiers.forEach { r ->
resultBuilder.addRecipient(r)
}
encryptedDataGenerator.setSessionKeyExtractionCallback { pgpSessionKey ->
if (pgpSessionKey != null) {
resultBuilder.setSessionKey(SessionKey(pgpSessionKey))
}
}
publicKeyEncryptedStream =
encryptedDataGenerator.open(outermostStream, ByteArray(BUFFER_SIZE)).also { stream ->

View file

@ -26,6 +26,7 @@ import org.pgpainless.util.Passphrase
import sop.EncryptionResult
import sop.Profile
import sop.ReadyWithResult
import sop.SessionKey
import sop.enums.EncryptAs
import sop.exception.SOPGPException
import sop.operation.Encrypt
@ -98,8 +99,10 @@ class EncryptImpl(private val api: PGPainless) : Encrypt {
api.generateMessage().onOutputStream(outputStream).withOptions(options)
Streams.pipeAll(plaintext, encryptionStream)
encryptionStream.close()
// TODO: Extract and emit session key once BC supports that
return EncryptionResult(null)
return EncryptionResult(
encryptionStream.result.sessionKey?.let {
SessionKey(it.algorithm.algorithmId.toByte(), it.key)
})
}
}
} catch (e: PGPException) {