mirror of
https://github.com/wneessen/go-mail.git
synced 2024-11-22 13:50:49 +01:00
fix: detached signature is now used
This commit is contained in:
parent
b4370ded12
commit
4700691380
3 changed files with 23 additions and 14 deletions
5
msg.go
5
msg.go
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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
25
sime.go
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue