<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Mattias Lindberg : Security</title><link>http://blogs.msdn.com/mattlind/archive/tags/Security/default.aspx</link><description>Tags: Security</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Validating XML Digital Signatures with References Using Unrecognized URI Prefixes</title><link>http://blogs.msdn.com/mattlind/archive/2006/10/10/Validating-XML-Digital-Signatures-with-References-Using-Unrecognized-URI-Prefixes.aspx</link><pubDate>Tue, 10 Oct 2006 15:16:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:811419</guid><dc:creator>mattlind</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/mattlind/comments/811419.aspx</comments><wfw:commentRss>http://blogs.msdn.com/mattlind/commentrss.aspx?PostID=811419</wfw:commentRss><description>&lt;P&gt;During the last few month I have been working on and off on a solution which implements security features for a larger project, part of this work has been to create a wrapper around the XML Digital Signature functionality of .NET Framework 2.0. I thought that I would share a solution to a problem related to validating messages that has reference URIs&amp;nbsp;on the form "cid:&amp;lt;somename&amp;gt;", some of you&amp;nbsp;may recognize this as a reference to a Content-ID (cid) in a MIME message. &lt;/P&gt;
&lt;P&gt;I had a signed message that looked something like the sample at the end of this post. Make note of the &amp;lt;Reference&amp;gt; elements and examine the URI attributes.&lt;/P&gt;
&lt;P&gt;This XML message was the body of a multipart MIME message and each URI references a message part in the MIME message through the&amp;nbsp;MIME&amp;nbsp;Content-ID. If you load this message in a SignedXml instance and call&amp;nbsp; CheckSignature you will receive the error: "The URI prefix is not recognized". This error is not unreasonable as SignedXml tries to use the URI to resolve the reference but cannot do that, but I still needed to validate the reference.&lt;/P&gt;
&lt;P&gt;I tried various ways to work around this problem. The one that I thought had&amp;nbsp;most merit tried to add Reference instances (associated to the SignedXml instance) which were loaded with the proper data and URI, but neither that nor any other attempt involving pre-populating data in the SignedXml&amp;nbsp;were successful. However, while working with this problem I was examining the call stack of the "The URI prefix is not recognized" error when the solution became obvious to me. Below is a recreation of the call stack:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;System.NotSupportedException: The URI prefix is not recognized.&lt;BR&gt;at System.Net.WebRequest.Create(Uri requestUri, Boolean useUriBase)&lt;BR&gt;at System.Net.WebRequest.Create(Uri requestUri)&lt;BR&gt;at System.Security.Cryptography.Xml.Reference.CalculateHashValue(XmlDocument document, CanonicalXmlNodeList refList)&lt;BR&gt;at System.Security.Cryptography.Xml.SignedXml.CheckDigestedReferences()&lt;BR&gt;at System.Security.Cryptography.Xml.SignedXml.CheckSignature()&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I realized that it was WebRequest.Create that threw the error, but as this is an extensible part of .NET all I have to do is to registering "cid" as a valid URI with .NET! If you enable "cid" by calling &lt;A href="http://msdn2.microsoft.com/en-us/library/system.net.webrequest.registerprefix.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/system.net.webrequest.registerprefix.aspx"&gt;WebRequest.RegisterPrefix&lt;/A&gt;&amp;nbsp;then WebRequest.Create will be able to lookup the reference based on the URI. Below is a description of the steps involved in the solution:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;To register "cid" as an URI in .NET you do this: &lt;FONT face="Courier New"&gt;bool registrationResult = WebRequest.RegisterPrefix("cid:", new CidRequestCreator());&lt;/FONT&gt; 
&lt;LI&gt;Use the classes and techniques described in &lt;A href="http://support.microsoft.com/kb/812409/EN-US/" mce_href="http://support.microsoft.com/kb/812409/EN-US/"&gt;http://support.microsoft.com/kb/812409/EN-US/&lt;/A&gt;&amp;nbsp;to create the CidRequestCreator, CidWebRequest and CidWebResponse. The KB article describes how to do implement support for the "FTP" URI prefix, so I modified it to work with "cid" the way I wanted it to (read the data stream&amp;nbsp;from a file based on the URI). 
&lt;LI&gt;Before calling SignedXml.CheckSignature you need to save the attachments to the folder from which the CidWebRequest classes read messages.&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Below is a high-level example of the logical steps that needs to be performed in my solution:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;RegisterCidUriPrefix();&lt;BR&gt;foreach(string&amp;nbsp;uri in _attachments.Keys)&lt;BR&gt;{&lt;BR&gt;&amp;nbsp; SaveAttachmentToDisk(uri, _attachments[uri]);&lt;BR&gt;}&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;BR&gt;SignedXml signedXml = new SignedXml(xmlDocument); &lt;BR&gt;XmlNodeList nodeList = xmlDocument.GetElementsByTagName("Signature");&lt;BR&gt;signedXml.LoadXml((XmlElement)nodeList[0]); &lt;BR&gt;&lt;BR&gt;res = signedXml.CheckSignature();&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Please note that RegisterPrefix is only required to be called once per process (or it might be per application domain), this means that if you call it multiple times it will only return true the first time and additional calls will return false. I do not know how expensive the call the RegisterPrefix is, but it might be a good optimization to keep track of if you already have called RegisterPrefix and avoid calling it multiple times.&lt;/P&gt;
&lt;P&gt;While customizing the FTP-sample I was able to remove most of the properties of request and response classes, but I also needed to add a Close method to the CidWebResponse class as the stream to the file needs to be closed. The file stream was opened in the constructor of the CidWebRequest class.&lt;/P&gt;
&lt;P&gt;Sample of a signed XML message:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&amp;lt;SOAP:Envelope xmlns:SOAP="&lt;/FONT&gt;&lt;A href="http://schemas.xmlsoap.org/soap/envelope/%22" mce_href='http://schemas.xmlsoap.org/soap/envelope/"'&gt;&lt;FONT face="Courier New" size=2&gt;http://schemas.xmlsoap.org/soap/envelope/"&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face="Courier New" size=2&gt;&amp;gt;&lt;BR&gt;&amp;nbsp; &amp;lt;SOAP:Header&amp;gt;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;DroppedStuffNotRelevantToThisDiscussion&amp;nbsp;/&amp;gt;&lt;BR&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&amp;lt;Signature xmlns:ns0="&lt;/FONT&gt;&lt;A href="http://schemas.xmlsoap.org/soap/envelope/%22" mce_href='http://schemas.xmlsoap.org/soap/envelope/"'&gt;&lt;FONT face="Courier New" size=2&gt;http://schemas.xmlsoap.org/soap/envelope/"&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face="Courier New" size=2&gt; xmlns="&lt;/FONT&gt;&lt;A href='http://www.w3.org/2000/09/xmldsig#"' mce_href='http://www.w3.org/2000/09/xmldsig#"'&gt;&lt;FONT face="Courier New" size=2&gt;http://www.w3.org/2000/09/xmldsig#"&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face="Courier New" size=2&gt;&amp;gt;&lt;BR&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&amp;lt;SignedInfo&amp;gt;&lt;BR&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&amp;lt;CanonicalizationMethod Algorithm="&lt;/FONT&gt;&lt;A href="http://www.w3.org/TR/2001/REC-xml-c14n-20010315%22" mce_href='http://www.w3.org/TR/2001/REC-xml-c14n-20010315"'&gt;&lt;FONT face="Courier New" size=2&gt;http://www.w3.org/TR/2001/REC-xml-c14n-20010315"&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face="Courier New" size=2&gt; /&amp;gt;&lt;BR&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&amp;lt;SignatureMethod Algorithm="&lt;/FONT&gt;&lt;A href='http://www.w3.org/2000/09/xmldsig#rsa-sha1"' mce_href='http://www.w3.org/2000/09/xmldsig#rsa-sha1"'&gt;&lt;FONT face="Courier New" size=2&gt;http://www.w3.org/2000/09/xmldsig#rsa-sha1"&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face="Courier New" size=2&gt; /&amp;gt;&lt;BR&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&amp;lt;Reference URI=""&amp;gt;&lt;BR&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&amp;lt;Transforms&amp;gt;&lt;BR&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&amp;lt;Transform Algorithm="&lt;/FONT&gt;&lt;A href='http://www.w3.org/2000/09/xmldsig#enveloped-signature"' mce_href='http://www.w3.org/2000/09/xmldsig#enveloped-signature"'&gt;&lt;FONT face="Courier New" size=2&gt;http://www.w3.org/2000/09/xmldsig#enveloped-signature"&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face="Courier New" size=2&gt; /&amp;gt;&lt;BR&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&amp;lt;Transform Algorithm="&lt;/FONT&gt;&lt;A href="http://www.w3.org/TR/1999/REC-xpath-19991116%22" mce_href='http://www.w3.org/TR/1999/REC-xpath-19991116"'&gt;&lt;FONT face="Courier New" size=2&gt;http://www.w3.org/TR/1999/REC-xpath-19991116"&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face="Courier New" size=2&gt;&amp;gt;&lt;BR&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&amp;lt;XPath xmlns:SOAP="&lt;/FONT&gt;&lt;A href="http://schemas.xmlsoap.org/soap/envelope/%22" mce_href='http://schemas.xmlsoap.org/soap/envelope/"'&gt;&lt;FONT face="Courier New" size=2&gt;http://schemas.xmlsoap.org/soap/envelope/"&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face="Courier New" size=2&gt;&amp;gt;not(ancestor-or-self::node()[@SOAP:actor="urn:oasis:names:tc:ebxml-msg:actor:nextMSH"] | ancestor-or-self::node()[@SOAP:actor="&lt;/FONT&gt;&lt;A href="http://schemas.xmlsoap.org/soap/actor/next%22])" mce_href='http://schemas.xmlsoap.org/soap/actor/next"])'&gt;&lt;FONT face="Courier New" size=2&gt;http://schemas.xmlsoap.org/soap/actor/next"])&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face="Courier New" size=2&gt;&amp;lt;/XPath&amp;gt;&lt;BR&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&amp;lt;/Transform&amp;gt;&lt;BR&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&amp;lt;/Transforms&amp;gt;&lt;BR&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&amp;lt;DigestMethod Algorithm="&lt;/FONT&gt;&lt;A href='http://www.w3.org/2000/09/xmldsig#sha1"' mce_href='http://www.w3.org/2000/09/xmldsig#sha1"'&gt;&lt;FONT face="Courier New" size=2&gt;http://www.w3.org/2000/09/xmldsig#sha1"&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face="Courier New" size=2&gt; /&amp;gt;&lt;BR&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&amp;lt;DigestValue&amp;gt;qBkMHkzxhFGG9HJ1j01u+rrVfGM=&amp;lt;/DigestValue&amp;gt;&lt;BR&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&amp;lt;/Reference&amp;gt;&lt;BR&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&amp;lt;Reference URI="cid:msg400123456"&amp;gt;&lt;BR&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&amp;lt;DigestMethod Algorithm="&lt;/FONT&gt;&lt;A href='http://www.w3.org/2000/09/xmldsig#sha1"' mce_href='http://www.w3.org/2000/09/xmldsig#sha1"'&gt;&lt;FONT face="Courier New" size=2&gt;http://www.w3.org/2000/09/xmldsig#sha1"&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face="Courier New" size=2&gt; /&amp;gt;&lt;BR&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&amp;lt;DigestValue&amp;gt;WHi7LauQVMt1IfJ3fXqorKEnWFs=&amp;lt;/DigestValue&amp;gt;&lt;BR&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&amp;lt;/Reference&amp;gt;&lt;BR&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&amp;lt;Reference URI="cid:msg400123456"&amp;gt;&lt;BR&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&amp;lt;DigestMethod Algorithm="&lt;/FONT&gt;&lt;A href='http://www.w3.org/2000/09/xmldsig#sha1"' mce_href='http://www.w3.org/2000/09/xmldsig#sha1"'&gt;&lt;FONT face="Courier New" size=2&gt;http://www.w3.org/2000/09/xmldsig#sha1"&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face="Courier New" size=2&gt; /&amp;gt;&lt;BR&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&amp;lt;DigestValue&amp;gt;+3ZwEOqRmtGtrSfzhicq8lem0w4=&amp;lt;/DigestValue&amp;gt;&lt;BR&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&amp;lt;/Reference&amp;gt;&lt;BR&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&amp;lt;/SignedInfo&amp;gt;&lt;BR&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&amp;lt;SignatureValue&amp;gt;Ay1DK.../OdTLc=&amp;lt;/SignatureValue&amp;gt;&lt;BR&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&amp;lt;KeyInfo&amp;gt;&lt;BR&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&amp;lt;X509Data&amp;gt;&lt;BR&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&amp;lt;X509Certificate&amp;gt;MIID9...stEw&amp;lt;/X509Certificate&amp;gt;&lt;BR&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&amp;lt;/X509Data&amp;gt;&lt;BR&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&amp;lt;/KeyInfo&amp;gt;&lt;BR&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&amp;lt;/Signature&amp;gt;&lt;BR&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&amp;lt;/SOAP:Header&amp;gt;&lt;BR&gt;&amp;lt;SOAP:Body&amp;gt;&lt;BR&gt;&amp;lt;/SOAP:Body&amp;gt;&lt;BR&gt;&amp;lt;/SOAP:Envelope&amp;gt;&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=811419" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/mattlind/archive/tags/.NET+Framework/default.aspx">.NET Framework</category><category domain="http://blogs.msdn.com/mattlind/archive/tags/Security/default.aspx">Security</category></item><item><title>Lesson learned: Cannot unlock software certificate pincode from code</title><link>http://blogs.msdn.com/mattlind/archive/2006/06/14/631322.aspx</link><pubDate>Wed, 14 Jun 2006 22:41:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:631322</guid><dc:creator>mattlind</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/mattlind/comments/631322.aspx</comments><wfw:commentRss>http://blogs.msdn.com/mattlind/commentrss.aspx?PostID=631322</wfw:commentRss><description>&lt;P&gt;&lt;FONT face=Verdana&gt;Due to a number of reasons we needed to ensure that the sender of a message, which we were going to sign, really knew the secret &lt;/FONT&gt;&lt;FONT face=Verdana&gt;password/pincode to the certificate.&amp;nbsp;The basic design of this is actually flawed but we could not change this as we only replace one component in a larger flow of information. But that is not relevant to the topic of this blog...&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;We were looking at utilizing Windows certificate store to hold the certificates so it was natural &lt;/FONT&gt;&lt;FONT face=Verdana&gt;(we thought) to associate a pincode when importing the certificate.&amp;nbsp;What happens is that when you try to open the certificate&amp;nbsp;you will be prompted by Windows to enter the pincode through an interactive dialog.&amp;nbsp;&lt;/FONT&gt;&lt;FONT face=Verdana&gt;Now all we had to do was to write some code that, in run-time, unlocked the certificate without displaying the interactive dialog.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;We first tried used the &lt;/FONT&gt;&lt;FONT face=Verdana&gt;CspParameters.KeyPassword property, but that didn't work due to what seems to a problem with .NET. This would have been really nice and clean solution, using new .NET 2.0 functionality to solve a problem. Had the fortune to discuss this with Shawn Farkas who had some good ideas.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;As a result we tried doing CryptoAPI and .NET together. Our code looked something like this:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;// Our method for getting a certificate&lt;BR&gt;X509Certificate2 cert = InternalImpl.RetrieveX509CertificateByOrgNumber("984718268");&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;bResult = CryptAcquireCertificatePrivateKey(cert.Handle, 0, IntPtr.Zero, hCryptProv, dwKeySpec, boolRes);&lt;BR&gt;if (!bResult)&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;int err = Marshal.GetLastWin32Error();&lt;BR&gt;&amp;nbsp;throw new Win32Exception();&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;bResult = CryptSetProvParam(hCryptProv, PP_SIGNATURE_PIN, new ASCIIEncoding().GetBytes("pincode"), 0) &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;// Do something interesting with the hCryptProv...&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;&lt;FONT face=Verdana&gt;But we just kept getting error &lt;FONT face="Courier New"&gt;0x00000057&lt;/FONT&gt; (&lt;FONT face="Courier New"&gt;ERROR_INVALID_PARAMETER&lt;/FONT&gt;) when calling CryptSetProvParam. We changed the code and dropped the X509Certificate2 class and used a pure CryptoAPI implementation, but the problem remained the same.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;What to do? I sent a mail to an internal mailing list and got a quick response, &lt;EM&gt;"CryptSetProvParam(PP_SIGNATURE_PIN) is used to set a PIN on a provider context for a smartcard csp. It is not recognized by software csps."&lt;/EM&gt; This means that what we were trying to do is not supported!&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;I guess that the reason for this is that if you lock down a certificate in certificate store you do it to force user intervention. If you could code around this you would break the security.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;Lesson learned:&lt;BR&gt;Certificates that have been locked down using a pincode in the certificate store cannot be unlocked using code. A user will always have to enter the pin interactively.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;I hope that describing this experience will help someone in the future.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;How did we solve our problem?&lt;BR&gt;We decided to put the certificate files (.p12 and .cert) on disk instead and open them using one of the constructor of X509Certificate2 that takes a password as parameter.&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;cert = new X509Certificate2(fullpath, password);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;Actually this is the same design as the old system used, I guess there was a reason for it... But we really wanted to use the certificate store, so did an attempt and failed.&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=631322" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/mattlind/archive/tags/Security/default.aspx">Security</category></item><item><title>System.Security.Cryptography - Lots of new stuff in .NET 2.0</title><link>http://blogs.msdn.com/mattlind/archive/2006/06/13/630093.aspx</link><pubDate>Wed, 14 Jun 2006 01:05:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:630093</guid><dc:creator>mattlind</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/mattlind/comments/630093.aspx</comments><wfw:commentRss>http://blogs.msdn.com/mattlind/commentrss.aspx?PostID=630093</wfw:commentRss><description>&lt;P&gt;&lt;FONT face=Verdana&gt;Recently I've been working in a BizTalk project where my main task has been to develop a module that is used to help signing and encrypting messages. I have then had the opportunity to dive a bit deeper into the functionality provided by the classes in System.Security.Cryptography. &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;I have previously worked with some of the classes but I now realize that there are much new functionality in .NET 2.0. Important examples of new functionality are DPAPI and GZIP support. &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;Data Protection API (DPAPI) is part of the Win32 API and has been available for a long time on WIndows, but using it on .NET has always required using P/Invoke to declare these APIs. Now there is a class called &lt;SPAN id=nsrTitle&gt;DpapiCryptographer which makes it much easier to use and more accessible to everyone.&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;FONT face=Verdana&gt;GZIP support is provided by GZipStream. Even though the compression ratio could be improved it appears to be fully compatible with other GZIP implementations. In my tests a 45 kB file was compressed to 6.5 kB by GZipStream while using the external tool GZIP.EXE resulted in a 4.5 kB file. &lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;FONT face=Verdana&gt;A great resource regarding cryptography in .NET is Shawn's .NET Security Blog, specifically the Cryptograpghy category which is found at &lt;/FONT&gt;&lt;A href="http://blogs.msdn.com/shawnfa/archive/category/2868.aspx"&gt;&lt;FONT face=Verdana&gt;http://blogs.msdn.com/shawnfa/archive/category/2868.aspx&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana&gt;. &lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=630093" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/mattlind/archive/tags/Security/default.aspx">Security</category></item></channel></rss>