2022-07-07 20:11:50 +00:00
|
|
|
package crypto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type signAndEncryptWriteCloser struct {
|
|
|
|
signWriter WriteCloser
|
|
|
|
encryptWriter WriteCloser
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *signAndEncryptWriteCloser) Write(b []byte) (int, error) {
|
|
|
|
return w.signWriter.Write(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *signAndEncryptWriteCloser) Close() error {
|
|
|
|
if err := w.signWriter.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return w.encryptWriter.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncryptStream is used to encrypt data as a Writer.
|
|
|
|
// It takes a writer for the encrypted data packet and returns a writer for the plaintext data.
|
|
|
|
// If signKeyRing is not nil, it is used to do an embedded signature.
|
|
|
|
func (sk *SessionKey) EncryptStream(
|
|
|
|
dataPacketWriter Writer,
|
|
|
|
plainMessageMetadata *PlainMessageMetadata,
|
|
|
|
signKeyRing *KeyRing,
|
|
|
|
) (plainMessageWriter WriteCloser, err error) {
|
2024-10-16 11:40:38 +00:00
|
|
|
return sk.encryptStream(
|
|
|
|
dataPacketWriter,
|
|
|
|
plainMessageMetadata,
|
|
|
|
signKeyRing,
|
|
|
|
false,
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
}
|
2022-07-07 20:11:50 +00:00
|
|
|
|
2024-10-16 11:40:38 +00:00
|
|
|
// EncryptStreamWithContext is used to encrypt data as a Writer.
|
|
|
|
// It takes a writer for the encrypted data packet and returns a writer for the plaintext data.
|
|
|
|
// If signKeyRing is not nil, it is used to do an embedded signature.
|
|
|
|
// * signingContext : (optional) the context for the signature.
|
|
|
|
func (sk *SessionKey) EncryptStreamWithContext(
|
|
|
|
dataPacketWriter Writer,
|
|
|
|
plainMessageMetadata *PlainMessageMetadata,
|
|
|
|
signKeyRing *KeyRing,
|
|
|
|
signingContext *SigningContext,
|
|
|
|
) (plainMessageWriter WriteCloser, err error) {
|
|
|
|
return sk.encryptStream(
|
|
|
|
dataPacketWriter,
|
|
|
|
plainMessageMetadata,
|
|
|
|
signKeyRing,
|
|
|
|
false,
|
|
|
|
signingContext,
|
|
|
|
)
|
|
|
|
}
|
2022-07-07 20:11:50 +00:00
|
|
|
|
2024-10-16 11:40:38 +00:00
|
|
|
// EncryptStreamWithCompression is used to encrypt data as a Writer.
|
|
|
|
// The plaintext data is compressed before being encrypted.
|
|
|
|
// It takes a writer for the encrypted data packet and returns a writer for the plaintext data.
|
|
|
|
// If signKeyRing is not nil, it is used to do an embedded signature.
|
|
|
|
// * signingContext : (optional) the context for the signature.
|
|
|
|
func (sk *SessionKey) EncryptStreamWithCompression(
|
|
|
|
dataPacketWriter Writer,
|
|
|
|
plainMessageMetadata *PlainMessageMetadata,
|
|
|
|
signKeyRing *KeyRing,
|
|
|
|
) (plainMessageWriter WriteCloser, err error) {
|
|
|
|
return sk.encryptStream(
|
|
|
|
dataPacketWriter,
|
|
|
|
plainMessageMetadata,
|
|
|
|
signKeyRing,
|
|
|
|
true,
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
}
|
2022-07-07 20:11:50 +00:00
|
|
|
|
2024-10-16 11:40:38 +00:00
|
|
|
// EncryptStreamWithContextAndCompression is used to encrypt data as a Writer.
|
|
|
|
// The plaintext data is compressed before being encrypted.
|
|
|
|
// It takes a writer for the encrypted data packet and returns a writer for the plaintext data.
|
|
|
|
// If signKeyRing is not nil, it is used to do an embedded signature.
|
|
|
|
// * signingContext : (optional) the context for the signature.
|
|
|
|
func (sk *SessionKey) EncryptStreamWithContextAndCompression(
|
|
|
|
dataPacketWriter Writer,
|
|
|
|
plainMessageMetadata *PlainMessageMetadata,
|
|
|
|
signKeyRing *KeyRing,
|
|
|
|
signingContext *SigningContext,
|
|
|
|
) (plainMessageWriter WriteCloser, err error) {
|
|
|
|
return sk.encryptStream(
|
|
|
|
dataPacketWriter,
|
|
|
|
plainMessageMetadata,
|
|
|
|
signKeyRing,
|
|
|
|
true,
|
|
|
|
signingContext,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sk *SessionKey) encryptStream(
|
|
|
|
dataPacketWriter Writer,
|
|
|
|
plainMessageMetadata *PlainMessageMetadata,
|
|
|
|
signKeyRing *KeyRing,
|
|
|
|
compress bool,
|
|
|
|
signingContext *SigningContext,
|
|
|
|
) (plainMessageWriter WriteCloser, err error) {
|
2022-07-07 20:11:50 +00:00
|
|
|
encryptWriter, signWriter, err := encryptStreamWithSessionKey(
|
2024-10-16 11:40:38 +00:00
|
|
|
plainMessageMetadata,
|
2022-07-07 20:11:50 +00:00
|
|
|
dataPacketWriter,
|
|
|
|
sk,
|
2024-10-16 11:40:38 +00:00
|
|
|
signKeyRing,
|
|
|
|
compress,
|
|
|
|
signingContext,
|
2022-07-07 20:11:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if signWriter != nil {
|
|
|
|
plainMessageWriter = &signAndEncryptWriteCloser{signWriter, encryptWriter}
|
|
|
|
} else {
|
|
|
|
plainMessageWriter = encryptWriter
|
|
|
|
}
|
|
|
|
return plainMessageWriter, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecryptStream is used to decrypt a data packet as a Reader.
|
|
|
|
// It takes a reader for the data packet
|
|
|
|
// and returns a PlainMessageReader for the plaintext data.
|
|
|
|
// If verifyKeyRing is not nil, PlainMessageReader.VerifySignature() will
|
|
|
|
// verify the embedded signature with the given key ring and verification time.
|
|
|
|
func (sk *SessionKey) DecryptStream(
|
|
|
|
dataPacketReader Reader,
|
|
|
|
verifyKeyRing *KeyRing,
|
|
|
|
verifyTime int64,
|
|
|
|
) (plainMessage *PlainMessageReader, err error) {
|
2024-10-16 11:40:38 +00:00
|
|
|
return decryptStreamWithSessionKeyAndContext(
|
|
|
|
sk,
|
|
|
|
dataPacketReader,
|
|
|
|
verifyKeyRing,
|
|
|
|
verifyTime,
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecryptStreamWithContext is used to decrypt a data packet as a Reader.
|
|
|
|
// It takes a reader for the data packet
|
|
|
|
// and returns a PlainMessageReader for the plaintext data.
|
|
|
|
// If verifyKeyRing is not nil, PlainMessageReader.VerifySignature() will
|
|
|
|
// verify the embedded signature with the given key ring and verification time.
|
|
|
|
// * verificationContext (optional): context for the signature verification.
|
|
|
|
func (sk *SessionKey) DecryptStreamWithContext(
|
|
|
|
dataPacketReader Reader,
|
|
|
|
verifyKeyRing *KeyRing,
|
|
|
|
verifyTime int64,
|
|
|
|
verificationContext *VerificationContext,
|
|
|
|
) (plainMessage *PlainMessageReader, err error) {
|
|
|
|
return decryptStreamWithSessionKeyAndContext(
|
2022-07-07 20:11:50 +00:00
|
|
|
sk,
|
|
|
|
dataPacketReader,
|
|
|
|
verifyKeyRing,
|
2024-10-16 11:40:38 +00:00
|
|
|
verifyTime,
|
|
|
|
verificationContext,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func decryptStreamWithSessionKeyAndContext(
|
|
|
|
sessionKey *SessionKey,
|
|
|
|
dataPacketReader Reader,
|
|
|
|
verifyKeyRing *KeyRing,
|
|
|
|
verifyTime int64,
|
|
|
|
verificationContext *VerificationContext,
|
|
|
|
) (plainMessage *PlainMessageReader, err error) {
|
|
|
|
messageDetails, err := decryptStreamWithSessionKey(
|
|
|
|
sessionKey,
|
|
|
|
dataPacketReader,
|
|
|
|
verifyKeyRing,
|
|
|
|
verificationContext,
|
2022-07-07 20:11:50 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "gopenpgp: error in reading message")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &PlainMessageReader{
|
|
|
|
messageDetails,
|
|
|
|
verifyKeyRing,
|
|
|
|
verifyTime,
|
|
|
|
false,
|
2024-10-16 11:40:38 +00:00
|
|
|
verificationContext,
|
2022-07-07 20:11:50 +00:00
|
|
|
}, err
|
|
|
|
}
|