mirror of
https://github.com/wneessen/go-mail.git
synced 2024-11-23 14:10:50 +01:00
Compare commits
1 commit
bf9d55cdd1
...
1e3f4fe757
Author | SHA1 | Date | |
---|---|---|---|
|
1e3f4fe757 |
9 changed files with 100 additions and 408 deletions
|
@ -1,62 +0,0 @@
|
||||||
-----BEGIN CERTIFICATE-----
|
|
||||||
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
|
|
||||||
-----END CERTIFICATE-----
|
|
||||||
-----BEGIN CERTIFICATE-----
|
|
||||||
MIIFazCCA1OgAwIBAgIUKU+ta2rnJE/79L2Uxg4vFoF0RxYwDQYJKoZIhvcNAQEL
|
|
||||||
BQAwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM
|
|
||||||
GEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAeFw0yNDA5MjYxNDUzMzlaFw0zNDA5
|
|
||||||
MjQxNDUzMzlaMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEw
|
|
||||||
HwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwggIiMA0GCSqGSIb3DQEB
|
|
||||||
AQUAA4ICDwAwggIKAoICAQDSOuYqxNsP4Gm/EDauon/SAVWj8PIKFB1OpuguHm13
|
|
||||||
b2l2G6hRuNnAmR6ewP22H/YoyYz1qRchH2qw1uizwnnSS0OY74CsJhd0PV6f4XRR
|
|
||||||
Y+6PotGDPu1fJJM4XI3HjWGdBkJSawZNWjP1dQRJPUHNRttSOrPsG3XT7TjfLjoK
|
|
||||||
jOelTqgwHUGE2n0AtQP7ZFQVn7LLBrukve8zMgivwEL1JFSlKppWFf0SUgpmQVE6
|
|
||||||
3jTAQPPrI/B5z5Ys1j2jv7mJt3/UATcTGmvPTNv94SUrO6nC3TJxKHtR30MALteo
|
|
||||||
EgH/s2O0Ax44iDENgm9p6eb+GCyTWS/eHBAJ6SU76PRymiE57/0GOqyYewuEOuIU
|
|
||||||
FYd6+gglCMe+ayfhI20njHP6RTiQpRjFy+DM8+bkcS89q0sfFSFHR5oFNbAgUgyI
|
|
||||||
bGiaWb+D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|
|
||||||
beuk/XQGIvPcIkbPnmsb4gGaiRMuw+mZ7isJDoQwHUmfqL1EpOYb5mLYHkIqKaCz
|
|
||||||
8t8wTdkimIVIFSxedy7cJCCWdQ/BCyTJoQpXD69PLPzxEi/YK9pB9S8qBtfefu4=
|
|
||||||
-----END CERTIFICATE-----
|
|
|
@ -1,52 +0,0 @@
|
||||||
-----BEGIN PRIVATE KEY-----
|
|
||||||
MIIJQAIBADANBgkqhkiG9w0BAQEFAASCCSowggkmAgEAAoICAQCiLh0JJTRRBhmU
|
|
||||||
yiMKALHtTOK7T20Bwy+fG0SO6RlBc+hSuuX/n6znXcNgOBlQ2Gg3+p1on/bmcKnG
|
|
||||||
N/SCiVBLpROiwxg3blQbZ7B7Jors/MopGk0LIBOXHPtAuYbF4J6ND5Ol6sgeGjMn
|
|
||||||
omwRjZlfeuBlHY345MqvcwH/lPhOlKme+tWD+bsFh08NGS+3NdQGP6dA2bRVrPXh
|
|
||||||
LXStHEmqfKO9EMVLWv+77tYhZESD6XgnA8pWjbdr9jajCsrQWrCG3jqHtzHNxtwf
|
|
||||||
7xfRwwgoUhLEvue6SBVokZGVmDhvWdRt2sjtLcWWJCI3p7M+NXRt5qf6iu24wLBd
|
|
||||||
zIDuWfgooWu5vBzNZjSTh2if6R1as9BdwASwy1n2HMvqpzgA+f/rXDFbvVc7WIKi
|
|
||||||
uGzfWApBrL0qTCQBuSyepH0G4rQ2sJtI5U8QOKBO76nQJq5WDQgefBX4GDI8aJ6q
|
|
||||||
X+teQq1AqERUmLWx4WlTwxSo5X+d1jY9I8f61CRKVfIRgMvAZUhm2h6RnoVIgq7G
|
|
||||||
7W3HdC3RT/f758njI7oIv5bhiyqpgyKr3cYhmn0enjP+YtjY85m51q005qzRLfaT
|
|
||||||
YiwMR4qyJW4ZOEPntPs20CD+e+Pi2JLONpRdcsSrkqZusVjm5PFy2e5RyNFXTupU
|
|
||||||
H2KVrgTRHL3GG2KWF5PmBdkhQfGG1QIDAQABAoICAAL3Iru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T2o5hZFOo6+33m5rYgwqZE0tRi+zLa1U6juBF/GiVbdsqupm88KN6y6
|
|
||||||
9P+vBWUJihN0D06yZBpnk82riiKIprEqe/URkpLy3b0UmCBsTqUOoCbBUabNEocy
|
|
||||||
v7bXMtIXUOo0gm7ZqfYXKHQR3oQbF0wqAxfI0RG0hl2syfqi5WQagMZ+PsW35cMC
|
|
||||||
ggEBAM6kIM/zUnNBFla+oTiPCyoII7nGmGz8dT9IhH0+6T5nNzG4O6D28A3MJcs1
|
|
||||||
C3pfukCCeWOnDNCIQ7C9Hx5XcCDoI4eD6zCRy+7zXxn0FxYS+O6lczB2mXGE69Yp
|
|
||||||
n3qb7P4XqZYex3dT72czJUlY6nFB2e0FmyvSoez8fsH9Ws78c4JGO953klam/emA
|
|
||||||
Hc8nB3CyM8rb2JlM3WeQbmo+Sbi0Yvj+MWM2AnTXx0xyaFXKP4WGD8hAxTvf9tSX
|
|
||||||
3NAPVBXku4zRpvoXyyrcBVd9vwE0qBr3eWCuci8aDD6RAAJgb8HHX9RjRF/phFpY
|
|
||||||
S++xGnGHEUSEl7cWnO8k0RyJzzc=
|
|
||||||
-----END PRIVATE KEY-----
|
|
44
msg.go
44
msg.go
|
@ -7,7 +7,8 @@ package mail
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
"crypto/rsa"
|
||||||
|
"crypto/x509"
|
||||||
"embed"
|
"embed"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -207,8 +208,8 @@ func WithNoDefaultUserAgent() MsgOption {
|
||||||
}
|
}
|
||||||
|
|
||||||
// SignWithSMime configures the Msg to be signed with S/MIME
|
// SignWithSMime configures the Msg to be signed with S/MIME
|
||||||
func (m *Msg) SignWithSMime(keyPair *tls.Certificate) error {
|
func (m *Msg) SignWithSMime(privateKey *rsa.PrivateKey, certificate *x509.Certificate) error {
|
||||||
sMime, err := newSMime(keyPair)
|
sMime, err := NewSMime(privateKey, certificate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -995,31 +996,28 @@ func (m *Msg) applyMiddlewares(msg *Msg) *Msg {
|
||||||
|
|
||||||
// signMessage sign the Msg with S/MIME
|
// signMessage sign the Msg with S/MIME
|
||||||
func (m *Msg) signMessage(msg *Msg) (*Msg, error) {
|
func (m *Msg) signMessage(msg *Msg) (*Msg, error) {
|
||||||
signedPart := msg.GetParts()[0]
|
currentPart := m.GetParts()[0]
|
||||||
body, err := signedPart.GetContent()
|
currentPart.SetEncoding(EncodingUSASCII)
|
||||||
|
currentPart.SetContentType(TypeTextPlain)
|
||||||
|
content, err := currentPart.GetContent()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, errors.New("failed to extract content from part")
|
||||||
}
|
}
|
||||||
|
|
||||||
signaturePart, err := m.createSignaturePart(signedPart.GetEncoding(), signedPart.GetContentType(), signedPart.GetCharset(), body)
|
signedContent, err := m.sMime.Sign(content)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, errors.New("failed to sign message")
|
||||||
}
|
}
|
||||||
|
|
||||||
m.parts = append(m.parts, signaturePart)
|
signedPart := msg.newPart(
|
||||||
|
typeSMimeSigned,
|
||||||
|
WithPartEncoding(EncodingB64),
|
||||||
|
WithContentDisposition(DispositionSMime),
|
||||||
|
)
|
||||||
|
signedPart.SetContent(*signedContent)
|
||||||
|
msg.parts = append(msg.parts, signedPart)
|
||||||
|
|
||||||
return m, err
|
return msg, nil
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Msg) createSignaturePart(encoding Encoding, contentType ContentType, charSet Charset, body []byte) (*Part, error) {
|
|
||||||
message := m.sMime.createMessage(encoding, contentType, charSet, body)
|
|
||||||
signaturePart := m.newPart(typeSMimeSigned, WithPartEncoding(EncodingB64), WithSMimeSinging())
|
|
||||||
|
|
||||||
if err := m.sMime.sign(signaturePart, message); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return signaturePart, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteTo writes the formated Msg into a give io.Writer and satisfies the io.WriteTo interface
|
// WriteTo writes the formated Msg into a give io.Writer and satisfies the io.WriteTo interface
|
||||||
|
@ -1027,7 +1025,7 @@ func (m *Msg) WriteTo(writer io.Writer) (int64, error) {
|
||||||
mw := &msgWriter{writer: writer, charset: m.charset, encoder: m.encoder}
|
mw := &msgWriter{writer: writer, charset: m.charset, encoder: m.encoder}
|
||||||
msg := m.applyMiddlewares(m)
|
msg := m.applyMiddlewares(m)
|
||||||
|
|
||||||
if m.hasSMime() {
|
if m.sMime != nil {
|
||||||
signedMsg, err := m.signMessage(msg)
|
signedMsg, err := m.signMessage(msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
|
@ -1223,7 +1221,7 @@ func (m *Msg) hasAlt() bool {
|
||||||
count++
|
count++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return count > 1 && m.pgptype == 0 && !m.hasSMime()
|
return count > 1 && m.pgptype == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// hasMixed returns true if the Msg has mixed parts
|
// hasMixed returns true if the Msg has mixed parts
|
||||||
|
|
131
msg_test.go
131
msg_test.go
|
@ -1907,23 +1907,6 @@ func TestMsg_hasAlt(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestMsg_hasAlt tests the hasAlt() method of the Msg with active S/MIME
|
|
||||||
func TestMsg_hasAltWithSMime(t *testing.T) {
|
|
||||||
keyPair, err := getDummyCertificate()
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("failed to load dummy certificate. Cause: %v", err)
|
|
||||||
}
|
|
||||||
m := NewMsg()
|
|
||||||
m.SetBodyString(TypeTextPlain, "Plain")
|
|
||||||
m.AddAlternativeString(TypeTextHTML, "<b>HTML</b>")
|
|
||||||
if err := m.SignWithSMime(keyPair); err != nil {
|
|
||||||
t.Errorf("set of certificate was not successful")
|
|
||||||
}
|
|
||||||
if m.hasAlt() {
|
|
||||||
t.Errorf("mail has alternative parts and S/MIME is active, but hasAlt() returned true")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestMsg_hasRelated tests the hasRelated() method of the Msg
|
// TestMsg_hasRelated tests the hasRelated() method of the Msg
|
||||||
func TestMsg_hasRelated(t *testing.T) {
|
func TestMsg_hasRelated(t *testing.T) {
|
||||||
m := NewMsg()
|
m := NewMsg()
|
||||||
|
@ -3263,33 +3246,32 @@ func TestNewMsgWithNoDefaultUserAgent(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestSignWithSMime_ValidKeyPair tests WithSMimeSinging with given key pair
|
// TestWithSMimeSinging_ValidPrivateKey tests WithSMimeSinging with given privateKey
|
||||||
func TestSignWithSMime_ValidKeyPair(t *testing.T) {
|
func TestWithSMimeSinging_ValidPrivateKey(t *testing.T) {
|
||||||
keyPair, err := getDummyCertificate()
|
privateKey, err := getDummyPrivateKey()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("failed to load dummy certificate. Cause: %v", err)
|
t.Errorf("failed to load dummy private key: %s", err)
|
||||||
}
|
}
|
||||||
|
certificate, err := getDummyCertificate(privateKey)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to load dummy certificate: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
m := NewMsg()
|
m := NewMsg()
|
||||||
if err := m.SignWithSMime(keyPair); err != nil {
|
if err := m.SignWithSMime(privateKey, certificate); err != nil {
|
||||||
t.Errorf("failed to set sMime. Cause: %v", err)
|
t.Errorf("failed to set sMime. Cause: %v", err)
|
||||||
}
|
}
|
||||||
if m.sMime.privateKey == nil {
|
if m.sMime.privateKey != privateKey {
|
||||||
t.Errorf("WithSMimeSinging() - no private key is given")
|
t.Errorf("WithSMimeSinging. Expected %v, got: %v", privateKey, m.sMime.privateKey)
|
||||||
}
|
|
||||||
if m.sMime.certificate == nil {
|
|
||||||
t.Errorf("WithSMimeSinging() - no certificate is given")
|
|
||||||
}
|
|
||||||
if len(m.sMime.parentCertificates) != len(keyPair.Certificate[:1]) {
|
|
||||||
t.Errorf("WithSMimeSinging() - no certificate is given")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestSignWithSMime_InvalidKeyPair tests WithSMimeSinging with given invalid key pair
|
// TestWithSMimeSinging_InvalidPrivateKey tests WithSMimeSinging with given invalid privateKey
|
||||||
func TestSignWithSMime_InvalidKeyPair(t *testing.T) {
|
func TestWithSMimeSinging_InvalidPrivateKey(t *testing.T) {
|
||||||
m := NewMsg()
|
m := NewMsg()
|
||||||
|
|
||||||
err := m.SignWithSMime(nil)
|
err := m.SignWithSMime(nil, nil)
|
||||||
if !errors.Is(err, ErrInvalidKeyPair) {
|
if !errors.Is(err, ErrInvalidPrivateKey) {
|
||||||
t.Errorf("failed to check sMimeAuthConfig values correctly: %s", err)
|
t.Errorf("failed to check sMimeAuthConfig values correctly: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3321,84 +3303,3 @@ func FuzzMsg_From(f *testing.F) {
|
||||||
m.Reset()
|
m.Reset()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestMsg_createSignaturePart tests the Msg.createSignaturePart method
|
|
||||||
func TestMsg_createSignaturePart(t *testing.T) {
|
|
||||||
keyPair, err := getDummyCertificate()
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("failed to load dummy certificate. Cause: %v", err)
|
|
||||||
}
|
|
||||||
m := NewMsg()
|
|
||||||
if err := m.SignWithSMime(keyPair); err != nil {
|
|
||||||
t.Errorf("set of certificate was not successful")
|
|
||||||
}
|
|
||||||
body := []byte("This is the body")
|
|
||||||
part, err := m.createSignaturePart(EncodingQP, TypeTextPlain, CharsetUTF7, body)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("createSignaturePart() method failed.")
|
|
||||||
}
|
|
||||||
|
|
||||||
if part.GetEncoding() != EncodingB64 {
|
|
||||||
t.Errorf("createSignaturePart() method failed. Expected encoding: %s, got: %s", EncodingB64, part.GetEncoding())
|
|
||||||
}
|
|
||||||
if part.GetContentType() != typeSMimeSigned {
|
|
||||||
t.Errorf("createSignaturePart() method failed. Expected content type: %s, got: %s", typeSMimeSigned, part.GetContentType())
|
|
||||||
}
|
|
||||||
if part.GetCharset() != CharsetUTF8 {
|
|
||||||
t.Errorf("createSignaturePart() method failed. Expected charset: %s, got: %s", CharsetUTF8, part.GetCharset())
|
|
||||||
}
|
|
||||||
if content, err := part.GetContent(); err != nil || len(content) == len(body) {
|
|
||||||
t.Errorf("createSignaturePart() method failed. Expected content should not be equal: %s, got: %s", body, part.GetEncoding())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestMsg_signMessage tests the Msg.signMessage method
|
|
||||||
func TestMsg_signMessage(t *testing.T) {
|
|
||||||
keyPair, err := getDummyCertificate()
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("failed to load dummy certificate. Cause: %v", err)
|
|
||||||
}
|
|
||||||
body := []byte("This is the body")
|
|
||||||
m := NewMsg()
|
|
||||||
m.SetBodyString(TypeTextPlain, string(body))
|
|
||||||
if err := m.SignWithSMime(keyPair); err != nil {
|
|
||||||
t.Errorf("set of certificate was not successful")
|
|
||||||
}
|
|
||||||
msg, err := m.signMessage(m)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("createSignaturePart() method failed.")
|
|
||||||
}
|
|
||||||
|
|
||||||
parts := msg.GetParts()
|
|
||||||
if len(parts) != 2 {
|
|
||||||
t.Errorf("createSignaturePart() method failed. Expected 2 parts, got: %d", len(parts))
|
|
||||||
}
|
|
||||||
|
|
||||||
signedPart := parts[0]
|
|
||||||
if signedPart.GetEncoding() != EncodingQP {
|
|
||||||
t.Errorf("createSignaturePart() method failed. Expected encoding: %s, got: %s", EncodingB64, signedPart.GetEncoding())
|
|
||||||
}
|
|
||||||
if signedPart.GetContentType() != TypeTextPlain {
|
|
||||||
t.Errorf("createSignaturePart() method failed. Expected content type: %s, got: %s", typeSMimeSigned, signedPart.GetContentType())
|
|
||||||
}
|
|
||||||
if signedPart.GetCharset() != CharsetUTF8 {
|
|
||||||
t.Errorf("createSignaturePart() method failed. Expected charset: %s, got: %s", CharsetUTF8, signedPart.GetCharset())
|
|
||||||
}
|
|
||||||
if content, err := signedPart.GetContent(); err != nil || len(content) != len(body) {
|
|
||||||
t.Errorf("createSignaturePart() method failed. Expected content should be equal: %s, got: %s", body, content)
|
|
||||||
}
|
|
||||||
|
|
||||||
signaturePart := parts[1]
|
|
||||||
if signaturePart.GetEncoding() != EncodingB64 {
|
|
||||||
t.Errorf("createSignaturePart() method failed. Expected encoding: %s, got: %s", EncodingB64, signaturePart.GetEncoding())
|
|
||||||
}
|
|
||||||
if signaturePart.GetContentType() != typeSMimeSigned {
|
|
||||||
t.Errorf("createSignaturePart() method failed. Expected content type: %s, got: %s", typeSMimeSigned, signaturePart.GetContentType())
|
|
||||||
}
|
|
||||||
if signaturePart.GetCharset() != CharsetUTF8 {
|
|
||||||
t.Errorf("createSignaturePart() method failed. Expected charset: %s, got: %s", CharsetUTF8, signaturePart.GetCharset())
|
|
||||||
}
|
|
||||||
if content, err := signaturePart.GetContent(); err != nil || len(content) == len(body) {
|
|
||||||
t.Errorf("createSignaturePart() method failed. Expected content should not be equal: %s, got: %s", body, signaturePart.GetEncoding())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
17
msgwriter.go
17
msgwriter.go
|
@ -100,7 +100,7 @@ func (mw *msgWriter) writeMsg(msg *Msg) {
|
||||||
mw.startMP(MIMERelated, msg.boundary)
|
mw.startMP(MIMERelated, msg.boundary)
|
||||||
mw.writeString(DoubleNewLine)
|
mw.writeString(DoubleNewLine)
|
||||||
}
|
}
|
||||||
if msg.hasAlt() {
|
if msg.hasAlt() && !msg.hasSMime() {
|
||||||
mw.startMP(MIMEAlternative, msg.boundary)
|
mw.startMP(MIMEAlternative, msg.boundary)
|
||||||
mw.writeString(DoubleNewLine)
|
mw.writeString(DoubleNewLine)
|
||||||
}
|
}
|
||||||
|
@ -241,7 +241,7 @@ func (mw *msgWriter) addFiles(files []*File, isAttachment bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if mw.err == nil {
|
if mw.err == nil {
|
||||||
mw.writeBody(file.Writer, encoding, false)
|
mw.writeBody(file.Writer, encoding)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -273,7 +273,7 @@ func (mw *msgWriter) writePart(part *Part, charset Charset) {
|
||||||
mimeHeader.Add(string(HeaderContentTransferEnc), contentTransferEnc)
|
mimeHeader.Add(string(HeaderContentTransferEnc), contentTransferEnc)
|
||||||
mw.newPart(mimeHeader)
|
mw.newPart(mimeHeader)
|
||||||
}
|
}
|
||||||
mw.writeBody(part.writeFunc, part.encoding, part.smime)
|
mw.writeBody(part.writeFunc, part.encoding)
|
||||||
}
|
}
|
||||||
|
|
||||||
// writeString writes a string into the msgWriter's io.Writer interface
|
// writeString writes a string into the msgWriter's io.Writer interface
|
||||||
|
@ -322,7 +322,7 @@ func (mw *msgWriter) writeHeader(key Header, values ...string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// writeBody writes an io.Reader into an io.Writer using provided Encoding
|
// writeBody writes an io.Reader into an io.Writer using provided Encoding
|
||||||
func (mw *msgWriter) writeBody(writeFunc func(io.Writer) (int64, error), encoding Encoding, singingWithSMime bool) {
|
func (mw *msgWriter) writeBody(writeFunc func(io.Writer) (int64, error), encoding Encoding) {
|
||||||
var writer io.Writer
|
var writer io.Writer
|
||||||
var encodedWriter io.WriteCloser
|
var encodedWriter io.WriteCloser
|
||||||
var n int64
|
var n int64
|
||||||
|
@ -337,11 +337,12 @@ func (mw *msgWriter) writeBody(writeFunc func(io.Writer) (int64, error), encodin
|
||||||
lineBreaker := Base64LineBreaker{}
|
lineBreaker := Base64LineBreaker{}
|
||||||
lineBreaker.out = &writeBuffer
|
lineBreaker.out = &writeBuffer
|
||||||
|
|
||||||
if encoding == EncodingQP {
|
switch encoding {
|
||||||
|
case EncodingQP:
|
||||||
encodedWriter = quotedprintable.NewWriter(&writeBuffer)
|
encodedWriter = quotedprintable.NewWriter(&writeBuffer)
|
||||||
} else if encoding == EncodingB64 && !singingWithSMime {
|
case EncodingB64:
|
||||||
encodedWriter = base64.NewEncoder(base64.StdEncoding, &lineBreaker)
|
encodedWriter = base64.NewEncoder(base64.StdEncoding, &lineBreaker)
|
||||||
} else if encoding == NoEncoding {
|
case NoEncoding:
|
||||||
_, err = writeFunc(&writeBuffer)
|
_, err = writeFunc(&writeBuffer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
mw.err = fmt.Errorf("bodyWriter function: %w", err)
|
mw.err = fmt.Errorf("bodyWriter function: %w", err)
|
||||||
|
@ -354,7 +355,7 @@ func (mw *msgWriter) writeBody(writeFunc func(io.Writer) (int64, error), encodin
|
||||||
mw.bytesWritten += n
|
mw.bytesWritten += n
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
} else {
|
default:
|
||||||
encodedWriter = quotedprintable.NewWriter(writer)
|
encodedWriter = quotedprintable.NewWriter(writer)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -154,26 +154,3 @@ func TestMsgWriter_writeMsg_PGP(t *testing.T) {
|
||||||
t.Errorf("writeMsg failed. Expected PGP encoding header but didn't find it in message output")
|
t.Errorf("writeMsg failed. Expected PGP encoding header but didn't find it in message output")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestMsgWriter_writeMsg_SMime tests the writeMsg method of the msgWriter with S/MIME types set
|
|
||||||
func TestMsgWriter_writeMsg_SMime(t *testing.T) {
|
|
||||||
keyPair, err := getDummyCertificate()
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("failed to load dummy certificate. Cause: %v", err)
|
|
||||||
}
|
|
||||||
m := NewMsg()
|
|
||||||
if err := m.SignWithSMime(keyPair); err != nil {
|
|
||||||
t.Errorf("set of certificate was not successful")
|
|
||||||
}
|
|
||||||
_ = m.From(`"Toni Tester" <test@example.com>`)
|
|
||||||
_ = m.To(`"Toni Receiver" <receiver@example.com>`)
|
|
||||||
m.Subject("This is a subject")
|
|
||||||
m.SetBodyString(TypeTextPlain, "This is the body")
|
|
||||||
buf := bytes.Buffer{}
|
|
||||||
mw := &msgWriter{writer: &buf, charset: CharsetUTF8, encoder: mime.QEncoding}
|
|
||||||
mw.writeMsg(m)
|
|
||||||
ms := buf.String()
|
|
||||||
if !strings.Contains(ms, `multipart/signed; protocol="application/pkcs7-signature"; micalg=sha256;`) {
|
|
||||||
t.Errorf("writeMsg failed. Expected PGP encoding header but didn't find it in message output")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -275,7 +275,6 @@ func TestPart_IsSMimeSigned(t *testing.T) {
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
m := NewMsg()
|
m := NewMsg()
|
||||||
m.SetBodyString(TypeTextPlain, "This is a body!")
|
|
||||||
pl, err := getPartList(m)
|
pl, err := getPartList(m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("failed: %s", err)
|
t.Errorf("failed: %s", err)
|
||||||
|
|
141
sime.go
141
sime.go
|
@ -1,20 +1,18 @@
|
||||||
package mail
|
package mail
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"crypto/rsa"
|
"crypto/rsa"
|
||||||
"crypto/tls"
|
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"encoding/pem"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"go.mozilla.org/pkcs7"
|
"go.mozilla.org/pkcs7"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// ErrInvalidKeyPair should be used if key pair is invalid
|
// ErrInvalidPrivateKey should be used if private key is invalid
|
||||||
ErrInvalidKeyPair = errors.New("invalid key pair")
|
ErrInvalidPrivateKey = errors.New("invalid private key")
|
||||||
|
|
||||||
|
// ErrInvalidCertificate should be used if certificate is invalid
|
||||||
|
ErrInvalidCertificate = errors.New("invalid certificate")
|
||||||
|
|
||||||
// ErrCouldNotInitialize should be used if the signed data could not initialize
|
// ErrCouldNotInitialize should be used if the signed data could not initialize
|
||||||
ErrCouldNotInitialize = errors.New("could not initialize signed data")
|
ErrCouldNotInitialize = errors.New("could not initialize signed data")
|
||||||
|
@ -24,136 +22,49 @@ var (
|
||||||
|
|
||||||
// ErrCouldNotFinishSigning should be used if the signing could not be finished
|
// ErrCouldNotFinishSigning should be used if the signing could not be finished
|
||||||
ErrCouldNotFinishSigning = errors.New("could not finish signing")
|
ErrCouldNotFinishSigning = errors.New("could not finish signing")
|
||||||
|
|
||||||
// ErrCouldNoEncodeToPEM should be used if the signature could not be encoded to PEM
|
|
||||||
ErrCouldNoEncodeToPEM = errors.New("could not encode to PEM")
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// SMime is used to sign messages with S/MIME
|
// SMime is used to sign messages with S/MIME
|
||||||
type SMime struct {
|
type SMime struct {
|
||||||
privateKey *rsa.PrivateKey
|
privateKey *rsa.PrivateKey
|
||||||
certificate *x509.Certificate
|
certificate *x509.Certificate
|
||||||
parentCertificates []*x509.Certificate
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewSMime construct a new instance of SMime with a provided *tls.Certificate
|
// NewSMime construct a new instance of SMime with a provided *rsa.PrivateKey
|
||||||
func newSMime(keyPair *tls.Certificate) (*SMime, error) {
|
func NewSMime(privateKey *rsa.PrivateKey, certificate *x509.Certificate) (*SMime, error) {
|
||||||
if keyPair == nil {
|
if privateKey == nil {
|
||||||
return nil, ErrInvalidKeyPair
|
return nil, ErrInvalidPrivateKey
|
||||||
}
|
}
|
||||||
|
|
||||||
parentCertificates := make([]*x509.Certificate, 0)
|
if certificate == nil {
|
||||||
for _, cert := range keyPair.Certificate[1:] {
|
return nil, ErrInvalidCertificate
|
||||||
c, err := x509.ParseCertificate(cert)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
parentCertificates = append(parentCertificates, c)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return &SMime{
|
return &SMime{
|
||||||
privateKey: keyPair.PrivateKey.(*rsa.PrivateKey),
|
privateKey: privateKey,
|
||||||
certificate: keyPair.Leaf,
|
certificate: certificate,
|
||||||
parentCertificates: parentCertificates,
|
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// sign with the S/MIME method the message of the actual *Part
|
// Sign the content with the given privateKey of the method NewSMime
|
||||||
func (sm *SMime) sign(signaturePart *Part, message string) error {
|
func (sm *SMime) Sign(content []byte) (*string, error) {
|
||||||
lines := parseLines([]byte(message))
|
toBeSigned, err := pkcs7.NewSignedData(content)
|
||||||
toBeSigned := lines.bytesFromLines([]byte("\r\n"))
|
|
||||||
|
|
||||||
tmp, err := pkcs7.NewSignedData(toBeSigned)
|
toBeSigned.SetDigestAlgorithm(pkcs7.OIDDigestAlgorithmSHA256)
|
||||||
tmp.SetDigestAlgorithm(pkcs7.OIDDigestAlgorithmSHA256)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ErrCouldNotInitialize
|
return nil, ErrCouldNotInitialize
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = tmp.AddSignerChain(sm.certificate, sm.privateKey, sm.parentCertificates, pkcs7.SignerInfoConfig{}); err != nil {
|
if err = toBeSigned.AddSigner(sm.certificate, sm.privateKey, pkcs7.SignerInfoConfig{}); err != nil {
|
||||||
return ErrCouldNotAddSigner
|
return nil, ErrCouldNotAddSigner
|
||||||
}
|
}
|
||||||
|
|
||||||
signatureDER, err := tmp.Finish()
|
signed, err := toBeSigned.Finish()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ErrCouldNotFinishSigning
|
return nil, ErrCouldNotFinishSigning
|
||||||
}
|
}
|
||||||
|
|
||||||
pemMsg, err := encodeToPEM(signatureDER)
|
signedData := string(signed)
|
||||||
if err != nil {
|
|
||||||
return ErrCouldNoEncodeToPEM
|
|
||||||
}
|
|
||||||
signaturePart.SetContent(*pemMsg)
|
|
||||||
|
|
||||||
return nil
|
return &signedData, nil
|
||||||
}
|
|
||||||
|
|
||||||
// createMessage prepares the message that will be used for the sign method later
|
|
||||||
func (sm *SMime) createMessage(encoding Encoding, contentType ContentType, charset Charset, body []byte) string {
|
|
||||||
return fmt.Sprintf("Content-Transfer-Encoding: %v\r\nContent-Type: %v; charset=%v\r\n\r\n%v", encoding, contentType, charset, string(body))
|
|
||||||
}
|
|
||||||
|
|
||||||
// encodeToPEM uses the method pem.Encode from the standard library but cuts the typical PEM preamble
|
|
||||||
func encodeToPEM(msg []byte) (*string, error) {
|
|
||||||
block := &pem.Block{Bytes: msg}
|
|
||||||
|
|
||||||
var arrayBuffer bytes.Buffer
|
|
||||||
if err := pem.Encode(&arrayBuffer, block); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
r := arrayBuffer.String()
|
|
||||||
r = strings.ReplaceAll(r, "-----BEGIN -----\n", "")
|
|
||||||
r = strings.ReplaceAll(r, "-----END -----\n", "")
|
|
||||||
|
|
||||||
return &r, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// line is the representation of one line of the message that will be used for signing purposes
|
|
||||||
type line struct {
|
|
||||||
line []byte
|
|
||||||
endOfLine []byte
|
|
||||||
}
|
|
||||||
|
|
||||||
// lines is the representation of a message that will be used for signing purposes
|
|
||||||
type lines []line
|
|
||||||
|
|
||||||
// bytesFromLines creates the line representation with the given endOfLine char
|
|
||||||
func (ls lines) bytesFromLines(sep []byte) []byte {
|
|
||||||
var raw []byte
|
|
||||||
for i := range ls {
|
|
||||||
raw = append(raw, ls[i].line...)
|
|
||||||
if len(ls[i].endOfLine) != 0 && sep != nil {
|
|
||||||
raw = append(raw, sep...)
|
|
||||||
} else {
|
|
||||||
raw = append(raw, ls[i].endOfLine...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return raw
|
|
||||||
}
|
|
||||||
|
|
||||||
// parseLines constructs the lines representation of a given message
|
|
||||||
func parseLines(raw []byte) lines {
|
|
||||||
oneLine := line{raw, nil}
|
|
||||||
lines := lines{oneLine}
|
|
||||||
lines = lines.splitLine([]byte("\r\n"))
|
|
||||||
lines = lines.splitLine([]byte("\r"))
|
|
||||||
lines = lines.splitLine([]byte("\n"))
|
|
||||||
return lines
|
|
||||||
}
|
|
||||||
|
|
||||||
// splitLine uses the given endOfLine to split the given line
|
|
||||||
func (ls lines) splitLine(sep []byte) lines {
|
|
||||||
nl := lines{}
|
|
||||||
for _, l := range ls {
|
|
||||||
split := bytes.Split(l.line, sep)
|
|
||||||
if len(split) > 1 {
|
|
||||||
for i := 0; i < len(split)-1; i++ {
|
|
||||||
nl = append(nl, line{split[i], sep})
|
|
||||||
}
|
|
||||||
nl = append(nl, line{split[len(split)-1], l.endOfLine})
|
|
||||||
} else {
|
|
||||||
nl = append(nl, l)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nl
|
|
||||||
}
|
}
|
||||||
|
|
37
util_test.go
37
util_test.go
|
@ -1,22 +1,41 @@
|
||||||
package mail
|
package mail
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"crypto/rand"
|
||||||
|
"crypto/rsa"
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
|
"crypto/x509/pkix"
|
||||||
|
"math/big"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
func getDummyPrivateKey() (*rsa.PrivateKey, error) {
|
||||||
certFilePath = "dummy-chain-cert.pem"
|
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
|
||||||
keyFilePath = "dummy-child-key.pem"
|
if err != nil {
|
||||||
)
|
return nil, err
|
||||||
|
}
|
||||||
|
return privateKey, nil
|
||||||
|
}
|
||||||
|
|
||||||
func getDummyCertificate() (*tls.Certificate, error) {
|
func getDummyCertificate(privateKey *rsa.PrivateKey) (*x509.Certificate, error) {
|
||||||
keyPair, err := tls.LoadX509KeyPair(certFilePath, keyFilePath)
|
template := &x509.Certificate{
|
||||||
|
SerialNumber: big.NewInt(1234),
|
||||||
|
Subject: pkix.Name{Organization: []string{"My Organization"}},
|
||||||
|
NotBefore: time.Now(),
|
||||||
|
NotAfter: time.Now().AddDate(1, 0, 0),
|
||||||
|
KeyUsage: x509.KeyUsageDigitalSignature,
|
||||||
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
||||||
|
}
|
||||||
|
|
||||||
|
certDER, err := x509.CreateCertificate(rand.Reader, template, template, &privateKey.PublicKey, privateKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
keyPair.Leaf, err = x509.ParseCertificate(keyPair.Certificate[0])
|
cert, err := x509.ParseCertificate(certDER)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
return &keyPair, nil
|
return cert, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue