mirror of
https://github.com/pgpainless/pgpainless.git
synced 2025-09-09 18:29:39 +02:00
EncryptImpl: Emit session-key
This commit is contained in:
parent
fdcdf6270f
commit
e611311f2c
3 changed files with 17 additions and 2 deletions
|
@ -17,9 +17,11 @@ import org.pgpainless.algorithm.SymmetricKeyAlgorithm
|
||||||
import org.pgpainless.bouncycastle.extensions.matches
|
import org.pgpainless.bouncycastle.extensions.matches
|
||||||
import org.pgpainless.key.SubkeyIdentifier
|
import org.pgpainless.key.SubkeyIdentifier
|
||||||
import org.pgpainless.util.MultiMap
|
import org.pgpainless.util.MultiMap
|
||||||
|
import org.pgpainless.util.SessionKey
|
||||||
|
|
||||||
data class EncryptionResult(
|
data class EncryptionResult(
|
||||||
val encryptionMechanism: MessageEncryptionMechanism,
|
val encryptionMechanism: MessageEncryptionMechanism,
|
||||||
|
val sessionKey: SessionKey?,
|
||||||
val compressionAlgorithm: CompressionAlgorithm,
|
val compressionAlgorithm: CompressionAlgorithm,
|
||||||
val detachedDocumentSignatures: OpenPGPSignatureSet<OpenPGPDocumentSignature>,
|
val detachedDocumentSignatures: OpenPGPSignatureSet<OpenPGPDocumentSignature>,
|
||||||
val recipients: Set<SubkeyIdentifier>,
|
val recipients: Set<SubkeyIdentifier>,
|
||||||
|
@ -84,6 +86,7 @@ data class EncryptionResult(
|
||||||
private var _fileName = ""
|
private var _fileName = ""
|
||||||
private var _modificationDate = Date(0)
|
private var _modificationDate = Date(0)
|
||||||
private var _encoding = StreamEncoding.BINARY
|
private var _encoding = StreamEncoding.BINARY
|
||||||
|
private var _sessionKey: SessionKey? = null
|
||||||
|
|
||||||
fun setEncryptionMechanism(mechanism: MessageEncryptionMechanism): Builder = apply {
|
fun setEncryptionMechanism(mechanism: MessageEncryptionMechanism): Builder = apply {
|
||||||
_encryptionMechanism = mechanism
|
_encryptionMechanism = mechanism
|
||||||
|
@ -105,6 +108,8 @@ data class EncryptionResult(
|
||||||
(recipients as MutableSet).add(recipient)
|
(recipients as MutableSet).add(recipient)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun setSessionKey(sessionKey: SessionKey) = apply { _sessionKey = sessionKey }
|
||||||
|
|
||||||
fun addDetachedSignature(signature: OpenPGPDocumentSignature): Builder = apply {
|
fun addDetachedSignature(signature: OpenPGPDocumentSignature): Builder = apply {
|
||||||
detachedSignatures.add(signature)
|
detachedSignatures.add(signature)
|
||||||
}
|
}
|
||||||
|
@ -114,6 +119,7 @@ data class EncryptionResult(
|
||||||
|
|
||||||
return EncryptionResult(
|
return EncryptionResult(
|
||||||
_encryptionMechanism,
|
_encryptionMechanism,
|
||||||
|
_sessionKey,
|
||||||
_compressionAlgorithm!!,
|
_compressionAlgorithm!!,
|
||||||
OpenPGPSignatureSet(detachedSignatures),
|
OpenPGPSignatureSet(detachedSignatures),
|
||||||
recipients,
|
recipients,
|
||||||
|
|
|
@ -20,6 +20,7 @@ import org.pgpainless.algorithm.CompressionAlgorithm
|
||||||
import org.pgpainless.algorithm.StreamEncoding
|
import org.pgpainless.algorithm.StreamEncoding
|
||||||
import org.pgpainless.bouncycastle.extensions.pgpDataEncryptorBuilder
|
import org.pgpainless.bouncycastle.extensions.pgpDataEncryptorBuilder
|
||||||
import org.pgpainless.util.ArmoredOutputStreamFactory
|
import org.pgpainless.util.ArmoredOutputStreamFactory
|
||||||
|
import org.pgpainless.util.SessionKey
|
||||||
|
|
||||||
// 1 << 8 causes wrong partial body length encoding
|
// 1 << 8 causes wrong partial body length encoding
|
||||||
// 1 << 9 fixes this.
|
// 1 << 9 fixes this.
|
||||||
|
@ -93,6 +94,11 @@ class EncryptionStream(
|
||||||
options.encryptionOptions.encryptionKeyIdentifiers.forEach { r ->
|
options.encryptionOptions.encryptionKeyIdentifiers.forEach { r ->
|
||||||
resultBuilder.addRecipient(r)
|
resultBuilder.addRecipient(r)
|
||||||
}
|
}
|
||||||
|
encryptedDataGenerator.setSessionKeyExtractionCallback { pgpSessionKey ->
|
||||||
|
if (pgpSessionKey != null) {
|
||||||
|
resultBuilder.setSessionKey(SessionKey(pgpSessionKey))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
publicKeyEncryptedStream =
|
publicKeyEncryptedStream =
|
||||||
encryptedDataGenerator.open(outermostStream, ByteArray(BUFFER_SIZE)).also { stream ->
|
encryptedDataGenerator.open(outermostStream, ByteArray(BUFFER_SIZE)).also { stream ->
|
||||||
|
|
|
@ -26,6 +26,7 @@ import org.pgpainless.util.Passphrase
|
||||||
import sop.EncryptionResult
|
import sop.EncryptionResult
|
||||||
import sop.Profile
|
import sop.Profile
|
||||||
import sop.ReadyWithResult
|
import sop.ReadyWithResult
|
||||||
|
import sop.SessionKey
|
||||||
import sop.enums.EncryptAs
|
import sop.enums.EncryptAs
|
||||||
import sop.exception.SOPGPException
|
import sop.exception.SOPGPException
|
||||||
import sop.operation.Encrypt
|
import sop.operation.Encrypt
|
||||||
|
@ -98,8 +99,10 @@ class EncryptImpl(private val api: PGPainless) : Encrypt {
|
||||||
api.generateMessage().onOutputStream(outputStream).withOptions(options)
|
api.generateMessage().onOutputStream(outputStream).withOptions(options)
|
||||||
Streams.pipeAll(plaintext, encryptionStream)
|
Streams.pipeAll(plaintext, encryptionStream)
|
||||||
encryptionStream.close()
|
encryptionStream.close()
|
||||||
// TODO: Extract and emit session key once BC supports that
|
return EncryptionResult(
|
||||||
return EncryptionResult(null)
|
encryptionStream.result.sessionKey?.let {
|
||||||
|
SessionKey(it.algorithm.algorithmId.toByte(), it.key)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e: PGPException) {
|
} catch (e: PGPException) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue