[GH-ISSUE #69] Private Key format support for PEM and DER #53

Closed
opened 2026-02-26 10:35:26 +03:00 by kerem · 10 comments
Owner

Originally created by @kingofdevil on GitHub (Nov 6, 2014).
Original GitHub issue: https://github.com/Pro/dkim-exchange/issues/69

Originally assigned to: @Pro on GitHub.

Exchange 2010 SP3, Exchange DKIM Signer v2.0.3.
dkim=fail

Signing a mail item according to DKIM failed with an exception. Check the logged exception for details.
System.Exception: CryptoProvider for domain cangdv.vn is null.
at Exchange.DkimSigner.DkimSigner.GetSignedDkimHeader(DomainElement domain, String unsignedDkimHeader, IEnumerable`1 canonicalizedHeaders) in c:\Users\Administrator\Desktop\dkim-exchange\Src\Exchange.DkimSigner\DkimSigner.cs:line 480
at Exchange.DkimSigner.DkimSigner.CanSign(DomainElement domain, Stream inputStream) in c:\Users\Administrator\Desktop\dkim-exchange\Src\Exchange.DkimSigner\DkimSigner.cs:line 209
at Exchange.DkimSigner.DkimSigningRoutingAgent.SignMailItem(MailItem mailItem) in c:\Users\Administrator\Desktop\dkim-exchange\Src\Exchange.DkimSigner\DkimSigningRoutingAgent.cs:line 96
at Exchange.DkimSigner.DkimSigningRoutingAgent.WhenMessageCategorized(CategorizedMessageEventSource source, QueuedMessageEventArgs e) in c:\Users\Administrator\Desktop\dkim-exchange\Src\Exchange.DkimSigner\DkimSigningRoutingAgent.cs:line 56

Originally created by @kingofdevil on GitHub (Nov 6, 2014). Original GitHub issue: https://github.com/Pro/dkim-exchange/issues/69 Originally assigned to: @Pro on GitHub. Exchange 2010 SP3, Exchange DKIM Signer v2.0.3. dkim=fail Signing a mail item according to DKIM failed with an exception. Check the logged exception for details. System.Exception: CryptoProvider for domain cangdv.vn is null. at Exchange.DkimSigner.DkimSigner.GetSignedDkimHeader(DomainElement domain, String unsignedDkimHeader, IEnumerable`1 canonicalizedHeaders) in c:\Users\Administrator\Desktop\dkim-exchange\Src\Exchange.DkimSigner\DkimSigner.cs:line 480 at Exchange.DkimSigner.DkimSigner.CanSign(DomainElement domain, Stream inputStream) in c:\Users\Administrator\Desktop\dkim-exchange\Src\Exchange.DkimSigner\DkimSigner.cs:line 209 at Exchange.DkimSigner.DkimSigningRoutingAgent.SignMailItem(MailItem mailItem) in c:\Users\Administrator\Desktop\dkim-exchange\Src\Exchange.DkimSigner\DkimSigningRoutingAgent.cs:line 96 at Exchange.DkimSigner.DkimSigningRoutingAgent.WhenMessageCategorized(CategorizedMessageEventSource source, QueuedMessageEventArgs e) in c:\Users\Administrator\Desktop\dkim-exchange\Src\Exchange.DkimSigner\DkimSigningRoutingAgent.cs:line 56
kerem 2026-02-26 10:35:26 +03:00
Author
Owner

@AlexLaroche commented on GitHub (Nov 6, 2014):

The problem is that the agent can not find the RSA key for the domain "cangdv.vn".

The agent isn't suppose to try to sign the email in this case but this function isn't property implement. I think the bug have been introduce with a lot of changement in the code. :(

        /// <summary>
        /// Create the RSACryptoServiceProvider for the domain
        /// </summary>
        /// <param name="basePath">Path of the private key to open</param>
        /// <returns></returns>
        public bool initElement(string basePath)
        {
            string path;
            if (Path.IsPathRooted(PrivateKeyFile))
                path = @"keys\" + PrivateKeyFile;
            else
                path = Path.Combine(basePath, @"keys\" + PrivateKeyFile);

            if (!File.Exists(path))
            {
                throw new FileNotFoundException("PrivateKey for domain " + Domain + " not found: " + path);
            }

            try
            {
                string xmlkey = File.ReadAllText(path, Encoding.ASCII).Trim();
                cryptoProvider = new RSACryptoServiceProvider();
                cryptoProvider.FromXmlString(xmlkey);
            }
            catch(Exception) 
            {
            }

            return true;
        }
<!-- gh-comment-id:62054230 --> @AlexLaroche commented on GitHub (Nov 6, 2014): The problem is that the agent can not find the RSA key for the domain "cangdv.vn". The agent isn't suppose to try to sign the email in this case but this function isn't property implement. I think the bug have been introduce with a lot of changement in the code. :( ``` /// <summary> /// Create the RSACryptoServiceProvider for the domain /// </summary> /// <param name="basePath">Path of the private key to open</param> /// <returns></returns> public bool initElement(string basePath) { string path; if (Path.IsPathRooted(PrivateKeyFile)) path = @"keys\" + PrivateKeyFile; else path = Path.Combine(basePath, @"keys\" + PrivateKeyFile); if (!File.Exists(path)) { throw new FileNotFoundException("PrivateKey for domain " + Domain + " not found: " + path); } try { string xmlkey = File.ReadAllText(path, Encoding.ASCII).Trim(); cryptoProvider = new RSACryptoServiceProvider(); cryptoProvider.FromXmlString(xmlkey); } catch(Exception) { } return true; } ```
Author
Owner

@AlexLaroche commented on GitHub (Nov 6, 2014):

Correction : I think it's because the format of the key isn't in XML.

<!-- gh-comment-id:62056694 --> @AlexLaroche commented on GitHub (Nov 6, 2014): Correction : I think it's because the format of the key isn't in XML.
Author
Owner

@Pro commented on GitHub (Nov 6, 2014):

This looks indeed like an invalid private key format exception. We should add an aditional check after this line:
github.com/Pro/dkim-exchange@85cd031bc0/Src/Exchange.DkimSigner/Configuration/DomainElement.cs (L58)

<!-- gh-comment-id:62063389 --> @Pro commented on GitHub (Nov 6, 2014): This looks indeed like an invalid private key format exception. We should add an aditional check after this line: https://github.com/Pro/dkim-exchange/blob/85cd031bc03468d21ceaf15b7d33a4216d0fe31a/Src/Exchange.DkimSigner/Configuration/DomainElement.cs#L58
Author
Owner

@AlexLaroche commented on GitHub (Nov 7, 2014):

I think we get a exception but we don't manage the exception at the moment.
It should be fix now with my last commit.

<!-- gh-comment-id:62080840 --> @AlexLaroche commented on GitHub (Nov 7, 2014): I think we get a exception but we don't manage the exception at the moment. It should be fix now with my last commit.
Author
Owner

@kingofdevil commented on GitHub (Nov 7, 2014):

Thanks @AlexLaroche,
My domain cangdinhvu.vn, i change log file becase i not want public my domain, sorry :p
Thanks @Pro
I have uninstall and delete C:\Program Files\Exchange DkimSigner folder, and reinstall Exchange DkimSigner. This fixed the problem :)

<!-- gh-comment-id:62085535 --> @kingofdevil commented on GitHub (Nov 7, 2014): Thanks @AlexLaroche, My domain cangdinhvu.vn, i change log file becase i not want public my domain, sorry :p Thanks @Pro I have uninstall and delete C:\Program Files\Exchange DkimSigner folder, and reinstall Exchange DkimSigner. This fixed the problem :)
Author
Owner

@AlexLaroche commented on GitHub (Nov 7, 2014):

Good! :)

<!-- gh-comment-id:62088950 --> @AlexLaroche commented on GitHub (Nov 7, 2014): Good! :)
Author
Owner

@AlexLaroche commented on GitHub (Nov 7, 2014):

Agent only support XML. DkimSigner configuration tool should convert the DER and PEM format key to XML.

<!-- gh-comment-id:62221370 --> @AlexLaroche commented on GitHub (Nov 7, 2014): Agent only support XML. DkimSigner configuration tool should convert the DER and PEM format key to XML.
Author
Owner

@Pro commented on GitHub (Nov 12, 2014):

@AlexLaroche: We should implement a function that also can handle PEM and DER encoded RSA keys here:
https://github.com/Pro/dkim-exchange/blob/master/Src/Exchange.DkimSigner/Configuration/DomainElement.cs#L56

It should still be possible to manually define a RSA key within the config file with a text editor. Thus the functionality within the configuration tool to convert to XML isn't really needed.
The https://github.com/Pro/dkim-exchange/blob/master/Src/Configuration.DkimSigner/RSA/RSACryptoHelper.cs class already contains the required functionality to convert PEM/DER to the RSACryptoServiceProvider format (and thus XML format)

<!-- gh-comment-id:62757380 --> @Pro commented on GitHub (Nov 12, 2014): @AlexLaroche: We should implement a function that also can handle PEM and DER encoded RSA keys here: https://github.com/Pro/dkim-exchange/blob/master/Src/Exchange.DkimSigner/Configuration/DomainElement.cs#L56 It should still be possible to manually define a RSA key within the config file with a text editor. Thus the functionality within the configuration tool to convert to XML isn't really needed. The https://github.com/Pro/dkim-exchange/blob/master/Src/Configuration.DkimSigner/RSA/RSACryptoHelper.cs class already contains the required functionality to convert PEM/DER to the RSACryptoServiceProvider format (and thus XML format)
Author
Owner

@AlexLaroche commented on GitHub (Nov 13, 2014):

@Pro: I let you make the change as you want. I reassign this issue that to you.

<!-- gh-comment-id:62833030 --> @AlexLaroche commented on GitHub (Nov 13, 2014): @Pro: I let you make the change as you want. I reassign this issue that to you.
Author
Owner

@Pro commented on GitHub (Nov 27, 2014):

Added in Version 2.1.0

<!-- gh-comment-id:64795611 --> @Pro commented on GitHub (Nov 27, 2014): Added in Version 2.1.0
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/dkim-exchange-Pro#53
No description provided.