diff --git a/dkim.go b/dkim.go index b0278f0..6c0f061 100644 --- a/dkim.go +++ b/dkim.go @@ -11,12 +11,10 @@ import ( "crypto/x509" "encoding/base64" "encoding/pem" - //"fmt" "hash" "io/ioutil" "regexp" "strings" - //"time" ) const ( @@ -89,7 +87,6 @@ func NewSigOptions() sigOptions { // Sign signs an email func Sign(email *bytes.Reader, options sigOptions) (*bytes.Reader, error) { var privateKey *rsa.PrivateKey - // check && sanitize config // PrivateKey (required & TODO: valid) if options.PrivateKey == "" { @@ -99,7 +96,7 @@ func Sign(email *bytes.Reader, options sigOptions) (*bytes.Reader, error) { d, _ := pem.Decode([]byte(options.PrivateKey)) key, err := x509.ParsePKCS1PrivateKey(d.Bytes) if err != nil { - return nil, err + return nil, ErrCandNotParsePrivateKey } privateKey = key @@ -237,8 +234,7 @@ func canonicalize(emailReader *bytes.Reader, options sigOptions) (headers, body return } - //fmt.Println(email) - // todo \n -> \r\n + // TODO: \n -> \r\n parts := bytes.SplitN(email, []byte{13, 10, 13, 10}, 2) if len(parts) != 2 { diff --git a/dkimHeader.go b/dkimHeader.go index 41613c7..bf50303 100644 --- a/dkimHeader.go +++ b/dkimHeader.go @@ -206,7 +206,7 @@ func NewDkimHeaderBySigOptions(options sigOptions) *DkimHeader { } // GetHeaderBase return base header for signers -// Todo: some refactoring... +// Todo: some refactoring needed... func (d *DkimHeader) GetHeaderBase(bodyHash string) string { h := "DKIM-Signature: v=" + d.Version + "; a=" + d.Algorithm + "; q=" + strings.Join(d.QueryMethods, ":") + "; c=" + d.MessageCanonicalization + ";" + CRLF + TAB subh := "s=" + d.Selector + ";" @@ -298,5 +298,3 @@ func (d *DkimHeader) GetHeaderBase(bodyHash string) string { h += subh + ";" + FWS + "b=" return h } - -//'test._domainkey.tmail.io:v=DKIM1;k=rsa;s=email;h=sha256;t=y;p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDNUXO+Qsl1tw+GjrqFajz0ERSEUs1FHSL/+udZRWn1Atw8gz0+tcGqhWChBDeU9gY5sKLEAZnX3FjC/T/IbqeiSM68kS5vLkzRI84eiJrm3+IieUqIIicsO+WYxQs+JgVx5XhpPjX4SQjHtwEC2xKkWnEv+VPgO1JWdooURcSC6QIDAQAB':300:: diff --git a/errors.go b/errors.go index 0dd2129..5920a05 100644 --- a/errors.go +++ b/errors.go @@ -20,6 +20,9 @@ var ( // If bad Canonicalization parameter ErrSignBadCanonicalization = errors.New("bad Canonicalization parameter") + // when unable to parse private key + ErrCandNotParsePrivateKey = errors.New("can not parse private key, check format (pem) and validity") + // Bad algorithm ErrSignBadAlgo = errors.New("bad algorithm. Only rsa-sha1 or rsa-sha256 are permitted")