fix: detached signature is now used

This commit is contained in:
theexiile1305 2024-10-09 13:53:15 +02:00
parent b4370ded12
commit 4700691380
No known key found for this signature in database
GPG key ID: A1BDDE98F2BF6E40
3 changed files with 23 additions and 14 deletions

5
msg.go
View file

@ -1004,10 +1004,13 @@ func (m *Msg) createSignaturePart(encoding Encoding, contentType ContentType, ch
message := m.sMime.createMessage(encoding, contentType, charSet, body) message := m.sMime.createMessage(encoding, contentType, charSet, body)
signaturePart := m.newPart(typeSMimeSigned, WithPartEncoding(EncodingB64), WithSMimeSinging()) 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 return nil, err
} }
signaturePart.SetContent(*signedMessage)
return signaturePart, nil return signaturePart, nil
} }

View file

@ -261,7 +261,12 @@ func (mw *msgWriter) writePart(part *Part, charset Charset) {
if partCharset.String() == "" { if partCharset.String() == "" {
partCharset = charset 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() contentTransferEnc := part.encoding.String()
if mw.depth == 0 { if mw.depth == 0 {
mw.writeHeader(HeaderContentType, contentType) mw.writeHeader(HeaderContentType, contentType)

25
sime.go
View file

@ -61,33 +61,34 @@ func newSMime(keyPair *tls.Certificate) (*SMime, error) {
}, nil }, nil
} }
// sign with the S/MIME method the message of the actual *Part // signMessage signs the message with S/MIME
func (sm *SMime) sign(signaturePart *Part, message string) error { func (sm *SMime) signMessage(message string) (*string, error) {
lines := parseLines([]byte(message)) lines := parseLines([]byte(message))
toBeSigned := lines.bytesFromLines([]byte("\r\n")) toBeSigned := lines.bytesFromLines([]byte("\r\n"))
tmp, err := pkcs7.NewSignedData(toBeSigned) signedData, err := pkcs7.NewSignedData(toBeSigned)
tmp.SetDigestAlgorithm(pkcs7.OIDDigestAlgorithmSHA256) signedData.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 = signedData.AddSignerChain(sm.certificate, sm.privateKey, sm.parentCertificates, pkcs7.SignerInfoConfig{}); err != nil {
return ErrCouldNotAddSigner return nil, ErrCouldNotAddSigner
} }
signatureDER, err := tmp.Finish() signedData.Detach()
signatureDER, err := signedData.Finish()
if err != nil { if err != nil {
return ErrCouldNotFinishSigning return nil, ErrCouldNotFinishSigning
} }
pemMsg, err := encodeToPEM(signatureDER) pemMsg, err := encodeToPEM(signatureDER)
if err != nil { 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 // createMessage prepares the message that will be used for the sign method later