<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sam Beauvois &#187; Certificate</title>
	<atom:link href="http://www.sambeauvois.be/blog/tag/certificate/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sambeauvois.be/blog</link>
	<description>general dev, .net and other stuff</description>
	<lastBuildDate>Tue, 31 Jan 2012 13:38:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>The IX509CertificateRequestPkcs10 InitializeFromTemplateName adventure</title>
		<link>http://www.sambeauvois.be/blog/2010/04/the-ix509certificaterequestpkcs10-initializefromtemplatename-adventure/</link>
		<comments>http://www.sambeauvois.be/blog/2010/04/the-ix509certificaterequestpkcs10-initializefromtemplatename-adventure/#comments</comments>
		<pubDate>Wed, 28 Apr 2010 16:54:31 +0000</pubDate>
		<dc:creator>Sam Beauvois</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Source Code]]></category>
		<category><![CDATA[Certificate]]></category>

		<guid isPermaLink="false">http://www.sambeauvois.be/blog/2010/04/the-ix509certificaterequestpkcs10-initializefromtemplatename-adventure/</guid>
		<description><![CDATA[This week has been researches, tests and headaches to be able to make request on a Certificate Authority server from a web application. My client has a Win server 2008 CA server for my developments On this server I have a certificate template named “User Template” My assignment was to request a certificate via an [...]]]></description>
			<content:encoded><![CDATA[<p>This week has been researches, tests and headaches to be able to make request on a Certificate Authority server from a web application.</p>
<p>My client has a Win server 2008 CA server for my developments</p>
<p>On this server I have a certificate template named “User Template”</p>
<p><a href="http://www.sambeauvois.be/blog/wp-content/uploads/2010/04/Certificate_server_templates.png" target="_blank"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Certificate_server_templates" src="http://www.sambeauvois.be/blog/wp-content/uploads/2010/04/Certificate_server_templates_thumb.png" border="0" alt="Certificate_server_templates" width="484" height="297" /></a></p>
<p>My assignment was to request a certificate via an ASP.NET application.</p>
<p>After researches on how to do that, I found this blog post that helped me a lot : <a title="http://blogs.msdn.com/alejacma/archive/2008/09/05/how-to-create-a-certificate-request-with-certenroll-and-net-c.aspx" href="http://blogs.msdn.com/alejacma/archive/2008/09/05/how-to-create-a-certificate-request-with-certenroll-and-net-c.aspx" target="_blank">http://blogs.msdn.com/alejacma/archive/2008/09/05/how-to-create-a-certificate-request-with-certenroll-and-net-c.aspx</a></p>
<p>In my development environment, I create a request for a certificate by using  this code</p>
<pre class="brush: csharp; title: ;">
 CERTENROLLLib.CX509CertificateRequestPkcs10Class request = new CERTENROLLLib.CX509CertificateRequestPkcs10Class();
 string templateName = &quot;User Template&quot;;
 try
 {
    request.InitializeFromTemplateName(CERTENROLLLib.X509CertificateEnrollmentContext.ContextUser, templateName);
 }
 catch (Exception ex)
 {
   log.DebugFormat(&quot;Error InitializeFromTemplateName : message {0}, inner : {1}, stack : {2}, source : {3}, target : {4} &quot;,
    ex.Message,
    ex.InnerException,
    ex.StackTrace,
    ex.Source,
    ex.TargetSite);
 }
</pre>
<p>And it worked just fine !</p>
<p>Then we moved the published solution to the staging environment, and the problems arrived . . .</p>
<p>It didn’t worked !</p>
<p>The error message said:</p>
<p>&#8220;CertEnroll::CX509CertificateRequestPkcs10::InitializeFromTemplateName: The requested certificate template is not supported by this CA. 0&#215;80094800 (-2146875392)&#8221;</p>
<p>stack :</p>
<p>&#8220;at CERTENROLLLib.CX509CertificateRequestPkcs10Class.InitializeFromTemplateName(X509CertificateEnrollmentContext Context, String strTemplateName)&#8221;</p>
<p><a href="http://www.sambeauvois.be/blog/wp-content/uploads/2010/04/cererror.png" target="_blank"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="cererror" src="http://www.sambeauvois.be/blog/wp-content/uploads/2010/04/cererror_thumb.png" border="0" alt="cererror" width="484" height="294" /></a></p>
<p>We checked the security everywhere ( CA server, Certificate templates, IIS Account, …), give access to everyone on the CA, on the template, … without any success</p>
<p>Then, after days and hours of search and tests, I decided to re-read the method documentation : <a title="http://msdn.microsoft.com/en-us/library/aa377533%28v=VS.85%29.aspx" href="http://msdn.microsoft.com/en-us/library/aa377533%28v=VS.85%29.aspx" target="_blank">http://msdn.microsoft.com/en-us/library/aa377533%28v=VS.85%29.aspx</a></p>
<p>And I noticed the second parameter description:</p>
<blockquote>
<dt><em>strTemplateName</em> [in] </dt>
<dd>Pointer to a <strong>BSTR</strong> variable that contains the Common Name (CN) of the template as it appears in Active Directory or the dotted decimal <a href="http://msdn.microsoft.com/en-us/library/ms721599%28v=VS.85%29.aspx#_security_object_identifier_gly" target="_blank"><em>object identifier</em></a>.</p>
</dd>
</blockquote>
<p>I had never tried using the dotted decimal object identifier, so i give it a shot.</p>
<p>I retrieved the object identifier on a certificate previously created with the “User Template”</p>
<p>On the CA server, I had a certificate request with the template “User Template”, so I right-click on it, go to All Tasks and click on the “View attributes/Extensions. . .” menu item.</p>
<p><a href="http://www.sambeauvois.be/blog/wp-content/uploads/2010/04/image14.png" target="_blank"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" src="http://www.sambeauvois.be/blog/wp-content/uploads/2010/04/image_thumb14.png" border="0" alt="image" width="484" height="304" /></a></p>
<p><a href="http://www.sambeauvois.be/blog/wp-content/uploads/2010/04/image15.png" target="_blank"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" src="http://www.sambeauvois.be/blog/wp-content/uploads/2010/04/image_thumb15.png" border="0" alt="image" width="484" height="103" /></a></p>
<p>A property windows opened, I go to the Extensions tab and click on the “Certificate Template Information” item.</p>
<p><a href="http://www.sambeauvois.be/blog/wp-content/uploads/2010/04/image16.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" src="http://www.sambeauvois.be/blog/wp-content/uploads/2010/04/image_thumb16.png" border="0" alt="image" width="425" height="484" /></a></p>
<p>In the information panel, I can see the famous dotted decimal object identifier between brackets.</p>
<p>I copy-paste this identifier in my code :</p>
<pre class="brush: csharp; title: ;">
 CERTENROLLLib.CX509CertificateRequestPkcs10Class request = new CERTENROLLLib.CX509CertificateRequestPkcs10Class();
 string templateName = &quot;1.3.6.1.4.1.311.21.8.3531346.8488945.6374567.164989.5001604.52.12582268.10747996&quot;;
 try
 {
    request.InitializeFromTemplateName(CERTENROLLLib.X509CertificateEnrollmentContext.ContextUser, templateName);
 }
 catch (Exception ex)
 {
    log.DebugFormat(&quot;Error InitializeFromTemplateName : message {0}, inner : {1}, stack : {2}, source : {3}, target : {4} &quot;,
      ex.Message,
      ex.InnerException,
      ex.StackTrace,
      ex.Source,
      ex.TargetSite);
 }
</pre>
<p>I compiled, I tried in the development environment and it worked !!</p>
<p>(note: there is just a minimal change in my code since I put the “templateName” in a configuration file in order to be able to modify it)</p>
<p>So we moved the published solution to the staging environment,</p>
<p>retrieve the dotted decimal object identifier corresponding to the “User Template” on the staging CA server,</p>
<p>copy-pasted this identifier in the configuration file,</p>
<p>executed a “IISRESET” command and try to execute the application.</p>
<p>And it worked !</p>
<p>We can&#8217;t figured out why it worked in the development environment but not in the staging one while both are equals. So If anyone knows about it, please let me know.</p>
<p>So, don’t trust the template name and use the object identifier !</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sambeauvois.be/blog/2010/04/the-ix509certificaterequestpkcs10-initializefromtemplatename-adventure/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

