From 4700691380cf247ce259cc58d76cb1a5a27e759a Mon Sep 17 00:00:00 2001 From: theexiile1305 Date: Wed, 9 Oct 2024 13:53:15 +0200 Subject: [PATCH] fix: detached signature is now used --- msg.go | 5 ++++- msgwriter.go | 7 ++++++- sime.go | 25 +++++++++++++------------ 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/msg.go b/msg.go index 286e533..e0d83d6 100644 --- a/msg.go +++ b/msg.go @@ -1004,10 +1004,13 @@ func (m *Msg) createSignaturePart(encoding Encoding, contentType ContentType, ch message := m.sMime.createMessage(encoding, contentType, charSet, body) signaturePart := m.newPart(typeSMimeSigned, WithPartEncoding(EncodingB64), WithSMimeSinging()) - if err := m.sMime.sign(signaturePart, message); err != nil { + signedMessage, err := m.sMime.signMessage(message) + if err != nil { return nil, err } + signaturePart.SetContent(*signedMessage) + return signaturePart, nil } diff --git a/msgwriter.go b/msgwriter.go index 41b3490..8182168 100644 --- a/msgwriter.go +++ b/msgwriter.go @@ -261,7 +261,12 @@ func (mw *msgWriter) writePart(part *Part, charset Charset) { if partCharset.String() == "" { partCharset = charset } - contentType := fmt.Sprintf("%s; charset=%s", part.contentType, partCharset) + + contentType := part.contentType.String() + if !part.IsSMimeSigned() { + contentType = strings.Join([]string{contentType, "; charset=", partCharset.String()}, "") + } + contentTransferEnc := part.encoding.String() if mw.depth == 0 { mw.writeHeader(HeaderContentType, contentType) diff --git a/sime.go b/sime.go index 35dabfb..de2decc 100644 --- a/sime.go +++ b/sime.go @@ -61,33 +61,34 @@ func newSMime(keyPair *tls.Certificate) (*SMime, error) { }, nil } -// sign with the S/MIME method the message of the actual *Part -func (sm *SMime) sign(signaturePart *Part, message string) error { +// signMessage signs the message with S/MIME +func (sm *SMime) signMessage(message string) (*string, error) { lines := parseLines([]byte(message)) toBeSigned := lines.bytesFromLines([]byte("\r\n")) - tmp, err := pkcs7.NewSignedData(toBeSigned) - tmp.SetDigestAlgorithm(pkcs7.OIDDigestAlgorithmSHA256) + signedData, err := pkcs7.NewSignedData(toBeSigned) + signedData.SetDigestAlgorithm(pkcs7.OIDDigestAlgorithmSHA256) if err != nil { - return ErrCouldNotInitialize + return nil, ErrCouldNotInitialize } - if err = tmp.AddSignerChain(sm.certificate, sm.privateKey, sm.parentCertificates, pkcs7.SignerInfoConfig{}); err != nil { - return ErrCouldNotAddSigner + if err = signedData.AddSignerChain(sm.certificate, sm.privateKey, sm.parentCertificates, pkcs7.SignerInfoConfig{}); err != nil { + return nil, ErrCouldNotAddSigner } - signatureDER, err := tmp.Finish() + signedData.Detach() + + signatureDER, err := signedData.Finish() if err != nil { - return ErrCouldNotFinishSigning + return nil, ErrCouldNotFinishSigning } pemMsg, err := encodeToPEM(signatureDER) if err != nil { - return ErrCouldNoEncodeToPEM + return nil, ErrCouldNoEncodeToPEM } - signaturePart.SetContent(*pemMsg) - return nil + return pemMsg, nil } // createMessage prepares the message that will be used for the sign method later