Refactor api (#6)
* Refactor library, remove duplicates * Rebuild structure to use Messages and Signature models * Use PGPSplitMessage * Remove signature model * Various fixes * Add helpers with tests * Fixes, add some docs, add tests * Add attachment helpers * Add helpers Symmetric encryption * Edit docs + examples * Rename kr to keyRing * Various fixes for documentation * Edit JSON handling functions, add decrypt keyring via token * Add proposal changes doc * Fix CI * Drop *Message functions, join CleartextMessage and BinaryMessage * Change canonicalization and trimming only to text signatures * Add cleartextsignature, detach signature from message model, move helpers * Documentation, remove optional parameters * Move verification to separate model * Don't return message in VerifyDetached * Update table of contents in readme * Appease golint * Run go fmt * Rename Encrypt/DecryptMessageWithPassword to ..WithToken These functions shouldn't be used with user-provided passwords, as they don't do any key-stretching. * Change key generation usernames
This commit is contained in:
parent
82d49bf235
commit
e65ed17b41
34 changed files with 2573 additions and 1478 deletions
|
|
@ -8,11 +8,52 @@ import (
|
|||
"strings"
|
||||
|
||||
gomime "github.com/ProtonMail/go-mime"
|
||||
"github.com/ProtonMail/gopenpgp/constants"
|
||||
|
||||
"golang.org/x/crypto/openpgp"
|
||||
"golang.org/x/crypto/openpgp/packet"
|
||||
)
|
||||
|
||||
// MIMECallbacks defines callback methods to process a MIME message.
|
||||
type MIMECallbacks interface {
|
||||
OnBody(body string, mimetype string)
|
||||
OnAttachment(headers string, data []byte)
|
||||
// Encrypted headers can be in an attachment and thus be placed at the end of the mime structure.
|
||||
OnEncryptedHeaders(headers string)
|
||||
OnVerified(verified int)
|
||||
OnError(err error)
|
||||
}
|
||||
|
||||
// DecryptMIMEMessage decrypts a MIME message.
|
||||
func (keyRing *KeyRing) DecryptMIMEMessage(
|
||||
message *PGPMessage, verifyKey *KeyRing, callbacks MIMECallbacks, verifyTime int64,
|
||||
) {
|
||||
decryptedMessage, verification, err := keyRing.Decrypt(message, verifyKey, verifyTime)
|
||||
if err != nil {
|
||||
callbacks.OnError(err)
|
||||
return
|
||||
}
|
||||
|
||||
body, verified, attachments, attachmentHeaders, err := pgp.parseMIME(decryptedMessage.GetString(), verifyKey)
|
||||
if err != nil {
|
||||
callbacks.OnError(err)
|
||||
return
|
||||
}
|
||||
bodyContent, bodyMimeType := body.GetBody()
|
||||
callbacks.OnBody(bodyContent, bodyMimeType)
|
||||
for i := 0; i < len(attachments); i++ {
|
||||
callbacks.OnAttachment(attachmentHeaders[i], []byte(attachments[i]))
|
||||
}
|
||||
callbacks.OnEncryptedHeaders("")
|
||||
if verification.GetVerification() != constants.SIGNATURE_NOT_SIGNED {
|
||||
callbacks.OnVerified(verification.GetVerification())
|
||||
} else {
|
||||
callbacks.OnVerified(verified)
|
||||
}
|
||||
}
|
||||
|
||||
// ----- INTERNAL FUNCTIONS -----
|
||||
|
||||
func (pgp GopenPGP) parseMIME(
|
||||
mimeBody string, verifierKey *KeyRing,
|
||||
) (*gomime.BodyCollector, int, []string, []string, error) {
|
||||
|
|
@ -49,42 +90,3 @@ func (pgp GopenPGP) parseMIME(
|
|||
|
||||
return body, verified, atts, attHeaders, err
|
||||
}
|
||||
|
||||
// MIMECallbacks defines callback methods to process a MIME message.
|
||||
type MIMECallbacks interface {
|
||||
OnBody(body string, mimetype string)
|
||||
OnAttachment(headers string, data []byte)
|
||||
// Encrypted headers can be in an attachment and thus be placed at the end of the mime structure.
|
||||
OnEncryptedHeaders(headers string)
|
||||
OnVerified(verified int)
|
||||
OnError(err error)
|
||||
}
|
||||
|
||||
// DecryptMIMEMessage decrypts a MIME message.
|
||||
func (pgp *GopenPGP) DecryptMIMEMessage(
|
||||
encryptedText string, verifierKey, privateKeyRing *KeyRing,
|
||||
passphrase string, callbacks MIMECallbacks, verifyTime int64,
|
||||
) {
|
||||
decsignverify, err := pgp.DecryptMessageVerify(encryptedText, verifierKey, privateKeyRing, passphrase, verifyTime)
|
||||
if err != nil {
|
||||
callbacks.OnError(err)
|
||||
return
|
||||
}
|
||||
|
||||
body, verified, attachments, attachmentHeaders, err := pgp.parseMIME(decsignverify.Plaintext, verifierKey)
|
||||
if err != nil {
|
||||
callbacks.OnError(err)
|
||||
return
|
||||
}
|
||||
bodyContent, bodyMimeType := body.GetBody()
|
||||
callbacks.OnBody(bodyContent, bodyMimeType)
|
||||
for i := 0; i < len(attachments); i++ {
|
||||
callbacks.OnAttachment(attachmentHeaders[i], []byte(attachments[i]))
|
||||
}
|
||||
callbacks.OnEncryptedHeaders("")
|
||||
if decsignverify.Verify == notSigned {
|
||||
callbacks.OnVerified(verified)
|
||||
} else {
|
||||
callbacks.OnVerified(decsignverify.Verify)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue