mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2025-12-17 23:56:56 +00:00
GODT-213: Message Builder
This commit is contained in:
@ -65,16 +65,22 @@ func (h *header) UnmarshalJSON(b []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
const (
|
||||
DispositionInline = "inline"
|
||||
DispositionAttachment = "attachment"
|
||||
)
|
||||
|
||||
// Attachment represents a message attachment.
|
||||
type Attachment struct {
|
||||
ID string `json:",omitempty"`
|
||||
MessageID string `json:",omitempty"` // msg v3 ???
|
||||
Name string `json:",omitempty"`
|
||||
Size int64 `json:",omitempty"`
|
||||
MIMEType string `json:",omitempty"`
|
||||
ContentID string `json:",omitempty"`
|
||||
KeyPackets string `json:",omitempty"`
|
||||
Signature string `json:",omitempty"`
|
||||
ID string `json:",omitempty"`
|
||||
MessageID string `json:",omitempty"` // msg v3 ???
|
||||
Name string `json:",omitempty"`
|
||||
Size int64 `json:",omitempty"`
|
||||
MIMEType string `json:",omitempty"`
|
||||
ContentID string `json:",omitempty"`
|
||||
Disposition string
|
||||
KeyPackets string `json:",omitempty"`
|
||||
Signature string `json:",omitempty"`
|
||||
|
||||
Header textproto.MIMEHeader `json:"-"`
|
||||
}
|
||||
|
||||
@ -93,7 +93,7 @@ func (c *client) DecryptAndVerifyCards(cards []Card) ([]Card, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
card.Data = signedCard
|
||||
card.Data = string(signedCard)
|
||||
}
|
||||
if isSignedCardType(card.Type) {
|
||||
err := c.verify(card.Data, card.Signature)
|
||||
|
||||
@ -190,13 +190,13 @@ func encrypt(encrypter *crypto.KeyRing, plain string, signer *crypto.KeyRing) (a
|
||||
return pgpMessage.GetArmored()
|
||||
}
|
||||
|
||||
func (c *client) decrypt(armored string) (plain string, err error) {
|
||||
func (c *client) decrypt(armored string) (plain []byte, err error) {
|
||||
return decrypt(c.userKeyRing, armored)
|
||||
}
|
||||
|
||||
func decrypt(decrypter *crypto.KeyRing, armored string) (plainBody string, err error) {
|
||||
func decrypt(decrypter *crypto.KeyRing, armored string) (plainBody []byte, err error) {
|
||||
if decrypter == nil {
|
||||
return "", ErrNoKeyringAvailable
|
||||
return nil, ErrNoKeyringAvailable
|
||||
}
|
||||
pgpMessage, err := crypto.NewPGPMessageFromArmored(armored)
|
||||
if err != nil {
|
||||
@ -206,7 +206,7 @@ func decrypt(decrypter *crypto.KeyRing, armored string) (plainBody string, err e
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return plainMessage.GetString(), nil
|
||||
return plainMessage.GetBinary(), nil
|
||||
}
|
||||
|
||||
func (c *client) sign(plain string) (armoredSignature string, err error) {
|
||||
|
||||
@ -272,26 +272,26 @@ func (m *Message) IsLegacyMessage() bool {
|
||||
strings.Contains(m.Body, MessageTail)
|
||||
}
|
||||
|
||||
func (m *Message) Decrypt(kr *crypto.KeyRing) (err error) {
|
||||
func (m *Message) Decrypt(kr *crypto.KeyRing) ([]byte, error) {
|
||||
if m.IsLegacyMessage() {
|
||||
return m.DecryptLegacy(kr)
|
||||
return m.decryptLegacy(kr)
|
||||
}
|
||||
|
||||
if !m.IsBodyEncrypted() {
|
||||
return
|
||||
return []byte(m.Body), nil
|
||||
}
|
||||
|
||||
armored := strings.TrimSpace(m.Body)
|
||||
|
||||
body, err := decrypt(kr, armored)
|
||||
if err != nil {
|
||||
return
|
||||
return nil, err
|
||||
}
|
||||
|
||||
m.Body = body
|
||||
return
|
||||
return body, nil
|
||||
}
|
||||
|
||||
func (m *Message) DecryptLegacy(kr *crypto.KeyRing) (err error) {
|
||||
func (m *Message) decryptLegacy(kr *crypto.KeyRing) (dec []byte, err error) {
|
||||
randomKeyStart := strings.Index(m.Body, RandomKeyHeader) + len(RandomKeyHeader)
|
||||
randomKeyEnd := strings.Index(m.Body, RandomKeyTail)
|
||||
randomKey := m.Body[randomKeyStart:randomKeyEnd]
|
||||
@ -300,7 +300,7 @@ func (m *Message) DecryptLegacy(kr *crypto.KeyRing) (err error) {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
bytesKey, err := decodeBase64UTF8(signedKey)
|
||||
bytesKey, err := decodeBase64UTF8(string(signedKey))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@ -345,8 +345,7 @@ func (m *Message) DecryptLegacy(kr *crypto.KeyRing) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
m.Body = string(bytesPlaintext)
|
||||
return err
|
||||
return bytesPlaintext, nil
|
||||
}
|
||||
|
||||
func decodeBase64UTF8(input string) (output []byte, err error) {
|
||||
|
||||
@ -134,9 +134,9 @@ func TestMessage_IsBodyEncrypted(t *testing.T) {
|
||||
|
||||
func TestMessage_Decrypt(t *testing.T) {
|
||||
msg := &Message{Body: testMessageEncrypted}
|
||||
err := msg.Decrypt(testPrivateKeyRing)
|
||||
dec, err := msg.Decrypt(testPrivateKeyRing)
|
||||
Ok(t, err)
|
||||
Equals(t, testMessageCleartext, msg.Body)
|
||||
Equals(t, testMessageCleartext, string(dec))
|
||||
}
|
||||
|
||||
func TestMessage_Decrypt_Legacy(t *testing.T) {
|
||||
@ -153,17 +153,17 @@ func TestMessage_Decrypt_Legacy(t *testing.T) {
|
||||
|
||||
msg := &Message{Body: testMessageEncryptedLegacy}
|
||||
|
||||
err = msg.Decrypt(testPrivateKeyRingLegacy)
|
||||
dec, err := msg.Decrypt(testPrivateKeyRingLegacy)
|
||||
Ok(t, err)
|
||||
|
||||
Equals(t, testMessageCleartextLegacy, msg.Body)
|
||||
Equals(t, testMessageCleartextLegacy, string(dec))
|
||||
}
|
||||
|
||||
func TestMessage_Decrypt_signed(t *testing.T) {
|
||||
msg := &Message{Body: testMessageSigned}
|
||||
err := msg.Decrypt(testPrivateKeyRing)
|
||||
dec, err := msg.Decrypt(testPrivateKeyRing)
|
||||
Ok(t, err)
|
||||
Equals(t, testMessageCleartext, msg.Body)
|
||||
Equals(t, testMessageCleartext, string(dec))
|
||||
}
|
||||
|
||||
func TestMessage_Encrypt(t *testing.T) {
|
||||
@ -176,10 +176,10 @@ func TestMessage_Encrypt(t *testing.T) {
|
||||
msg := &Message{Body: testMessageCleartext}
|
||||
Ok(t, msg.Encrypt(testPrivateKeyRing, testPrivateKeyRing))
|
||||
|
||||
err = msg.Decrypt(testPrivateKeyRing)
|
||||
dec, err := msg.Decrypt(testPrivateKeyRing)
|
||||
Ok(t, err)
|
||||
|
||||
Equals(t, testMessageCleartext, msg.Body)
|
||||
Equals(t, testMessageCleartext, string(dec))
|
||||
Equals(t, testIdentity, signer.GetIdentities()[0])
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user