LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-27-2018, 12:21 PM   #1
rohit_k
LQ Newbie
 
Registered: Oct 2007
Location: Pune
Distribution: RHEL, SOLARIS, SUSE
Posts: 20

Rep: Reputation: 1
[Python] Not able to sign emails using Dkimpy


I am trying to sign email using Python and dkimpy. Following is my code:
Code:
            try:
                with open(private_key) as file:
                    key = file.read()
            except Exception,error:
                raise Exception("Cannot open private key file %s. Error seen: %s" %(private_key,str(error)))
            else:
                sig = dkim.sign(
                        message=msg.as_string(),
                        selector=str(selector),
                        domain=sender_domain,
                        privkey=key,
                        include_headers=headers_list,
                        canonicalize=canonicalize,
                        signature_algorithm=sign_algo,
                        )
            # add DKIM-Signature header to email
            msg.add_header('DKIM-Signature',sig.lstrip("DKIM-Signature: "))

            # create a new email file. New email file will have 'signed_' prefix
            base_dir = os.path.dirname(eml)
            signed_email = os.path.basename(eml)
            signed_email = os.path.join(base_dir,'signed_' + signed_email)
            with open(signed_email, "w") as file:
                file.write(msg.as_string())
When header canonicalize is 'relaxed', my code works well. But when header canonicalize is 'simple', email is not correctly signed.

Code:
canonicalize = ('relaxed', 'simple')  <--- works
canonicalize = ('simple', 'relaxed')  <--- NOT WORKING
I am using 'opendkim-testmsg' to test signed emails
Code:
opendkim-testmsg < signed_test.msg
opendkim-testmsg: dkim_eom(): Bad signature
I am not able to figure out whats wrong
Please help.
 
Old 08-27-2018, 01:40 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by rohit_k View Post
I am trying to sign email using Python and dkimpy. Following is my code:
Code:
            try:
                with open(private_key) as file:
                    key = file.read()
            except Exception,error:
                raise Exception("Cannot open private key file %s. Error seen: %s" %(private_key,str(error)))
            else:
                sig = dkim.sign(
                        message=msg.as_string(),
                        selector=str(selector),
                        domain=sender_domain,
                        privkey=key,
                        include_headers=headers_list,
                        canonicalize=canonicalize,
                        signature_algorithm=sign_algo,
                        )
            # add DKIM-Signature header to email
            msg.add_header('DKIM-Signature',sig.lstrip("DKIM-Signature: "))

            # create a new email file. New email file will have 'signed_' prefix
            base_dir = os.path.dirname(eml)
            signed_email = os.path.basename(eml)
            signed_email = os.path.join(base_dir,'signed_' + signed_email)
            with open(signed_email, "w") as file:
                file.write(msg.as_string())
When header canonicalize is 'relaxed', my code works well. But when header canonicalize is 'simple', email is not correctly signed.
Code:
canonicalize = ('relaxed', 'simple')  <--- works
canonicalize = ('simple', 'relaxed')  <--- NOT WORKING
I am using 'opendkim-testmsg' to test signed emails
Code:
opendkim-testmsg < signed_test.msg
opendkim-testmsg: dkim_eom(): Bad signature
How did you configure opendkim.conf? And have you been through the docs on this?
https://gathman.org/pydkim/
 
Old 08-27-2018, 10:25 PM   #3
rohit_k
LQ Newbie
 
Registered: Oct 2007
Location: Pune
Distribution: RHEL, SOLARIS, SUSE
Posts: 20

Original Poster
Rep: Reputation: 1
opendkim.conf will not come into picture here. I am directly using Python module to sign email.
 
Old 08-28-2018, 12:59 PM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by rohit_k View Post
opendkim.conf will not come into picture here. I am directly using Python module to sign email.
Then where are you getting the signature? Your keys?? You've not posted the complete code...where did you get it?

And there are MANY Python programs that do DKIM signing you can look at as examples.
https://russell.ballestrini.net/quic...l-with-python/

Last edited by TB0ne; 08-28-2018 at 01:29 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
send out emails fine but can't receive incoming emails said76 Linux - Server 7 07-05-2012 01:37 AM
[SOLVED] How to DKIM sign emails by amavisd-new XXLRay Linux - Server 16 03-03-2011 07:27 AM
[SOLVED] DKIM doesn't Sign emails from external hosts raghugs Linux - Server 2 10-08-2009 08:35 AM
sign on invisible in gaim - NOT invi after sign on saravkrish Linux - Software 7 09-12-2005 10:55 PM
convert html emails to plain text emails andredude Linux - General 6 03-20-2005 12:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration