mirror of
https://github.com/wneessen/go-mail.git
synced 2024-12-23 02:50:39 +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)
|
||||
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
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
25
sime.go
25
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
|
||||
|
|
Loading…
Reference in a new issue