<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.technet.com/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-US"><title type="html">My Nuggets</title><subtitle type="html" /><id>http://blogs.technet.com/b/vishalagarwal/atom.aspx</id><link rel="alternate" type="text/html" href="http://blogs.technet.com/b/vishalagarwal/" /><link rel="self" type="application/atom+xml" href="http://blogs.technet.com/b/vishalagarwal/atom.aspx" /><generator uri="http://telligent.com" version="5.6.50428.7875">Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><updated>2009-04-30T21:33:00Z</updated><entry><title>Verifying the private key property for a certificate in the store</title><link rel="alternate" type="text/html" href="http://blogs.technet.com/b/vishalagarwal/archive/2010/03/30/verifying-the-private-key-property-for-a-certificate-in-the-store.aspx" /><id>http://blogs.technet.com/b/vishalagarwal/archive/2010/03/30/verifying-the-private-key-property-for-a-certificate-in-the-store.aspx</id><published>2010-03-30T07:05:00Z</published><updated>2010-03-30T07:05:00Z</updated><content type="html">&lt;P&gt;I was recently asked as to how to figure out if the private key associated with a certificate is exportable or not. Typically the following code should work:&lt;/P&gt;
&lt;P&gt;&amp;gt;$cert = (dir cert:\localmachine\my)[0]&lt;BR&gt;&amp;gt;$cert.PrivateKey.CspKeyContainerInfo.Exportable&lt;BR&gt;&lt;SPAN style="FONT-FAMILY: 'Calibri','sans-serif'; FONT-SIZE: 11pt; mso-fareast-font-family: Calibri; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA; mso-fareast-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin; mso-bidi-theme-font: minor-bidi"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-FAMILY: 'Calibri','sans-serif'; FONT-SIZE: 11pt; mso-fareast-font-family: Calibri; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA; mso-fareast-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin; mso-bidi-theme-font: minor-bidi"&gt;However, at times you would notice that $cert.PrivateKey is really null. However if you run "certutil -v -verify My 0", you can observe the private key and its properties. What's the difference?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-FAMILY: 'Calibri','sans-serif'; FONT-SIZE: 11pt; mso-fareast-font-family: Calibri; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA; mso-fareast-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin; mso-bidi-theme-font: minor-bidi"&gt;The problem is that with the introduction of KSP, if the private key is stored in a KSP .NET classes are unable to find the private key (and hence the privateKey object is null). This is because .NET does not yet support KSP based AsymmetricAlgorithm objects (I'm not aware of it atleast for V3.5).&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-FAMILY: 'Calibri','sans-serif'; FONT-SIZE: 11pt; mso-fareast-font-family: Calibri; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA; mso-fareast-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin; mso-bidi-theme-font: minor-bidi"&gt;Fortunately there is a way to get around this using Certificate Enrollment Interface ISignerCertificate&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-FAMILY: 'Calibri','sans-serif'; FONT-SIZE: 11pt; mso-fareast-font-family: Calibri; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA; mso-fareast-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin; mso-bidi-theme-font: minor-bidi"&gt;&amp;gt;$cert = (dir cert:\localmachine\my)[0]&lt;BR&gt;&amp;gt; $sc = new-object -com "X509Enrollment.CSignerCertificate.1"&lt;BR&gt;&amp;gt; $sc.Initialize(1, 0, 4, $cert.GetRawCertDataString())&lt;BR&gt;&amp;gt;$sc.PrivateKey.ExportPolicy&lt;BR&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-FAMILY: 'Calibri','sans-serif'; FONT-SIZE: 11pt; mso-fareast-font-family: Calibri; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA; mso-fareast-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin; mso-bidi-theme-font: minor-bidi"&gt;This is pretty powerful as IX509PrivateKey interface is much more richer and this solution should work regardless of whether the key is KSP based or CSP based.&lt;/SPAN&gt;&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3321950" width="1" height="1"&gt;</content><author><name>VishalAgarwal</name><uri>http://blogs.technet.com/VishalAgarwal/ProfileUrlRedirect.ashx</uri></author><category term="powershell ISignerCertificate PrivateKey KSP" scheme="http://blogs.technet.com/b/vishalagarwal/archive/tags/powershell+ISignerCertificate+PrivateKey+KSP/" /></entry><entry><title>Instantiate Microsoft Certificate Authority management interface using powershell</title><link rel="alternate" type="text/html" href="http://blogs.technet.com/b/vishalagarwal/archive/2009/10/22/instantiate-microsoft-certificate-authority-management-interface-using-powershell.aspx" /><id>http://blogs.technet.com/b/vishalagarwal/archive/2009/10/22/instantiate-microsoft-certificate-authority-management-interface-using-powershell.aspx</id><published>2009-10-22T06:28:00Z</published><updated>2009-10-22T06:28:00Z</updated><content type="html">&lt;P&gt;Microsoft's Certificate Authority management interface as mentioned in &lt;A href="http://msdn.microsoft.com/en-us/library/aa383234(VS.85).aspx"&gt;http://msdn.microsoft.com/en-us/library/aa383234(VS.85).aspx&lt;/A&gt;&amp;nbsp;is implemented in certadm.dll. certadm.dll does not ship by default on client system and is part of admin pack (or called Remote Server Administration tools &lt;A href="http://www.microsoft.com/downloads/details.aspx?FamilyId=9FF6E897-23CE-4A36-B7FC-D52065DE9960&amp;amp;displaylang=en"&gt;http://www.microsoft.com/downloads/details.aspx?FamilyId=9FF6E897-23CE-4A36-B7FC-D52065DE9960&amp;amp;displaylang=en&lt;/A&gt;) that can be installed on the client system. Once installed, you can quickly test the interface using powershell with this sample script:&lt;/P&gt;
&lt;P&gt;&amp;gt; $certadmin = new-object -com "CertificateAuthority.Admin.1"&lt;BR&gt;&amp;gt; $name = $certadmin.GetCAProperty("camachinename\CAName", 6, 0, 4, 0)&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3288345" width="1" height="1"&gt;</content><author><name>VishalAgarwal</name><uri>http://blogs.technet.com/VishalAgarwal/ProfileUrlRedirect.ashx</uri></author><category term="powershell" scheme="http://blogs.technet.com/b/vishalagarwal/archive/tags/powershell/" /><category term="CA" scheme="http://blogs.technet.com/b/vishalagarwal/archive/tags/CA/" /><category term="Certificate Authority" scheme="http://blogs.technet.com/b/vishalagarwal/archive/tags/Certificate+Authority/" /></entry><entry><title>exporting certificate from user store to PFX using powershell</title><link rel="alternate" type="text/html" href="http://blogs.technet.com/b/vishalagarwal/archive/2009/09/07/exporting-certificate-from-user-store-to-pfx-using-powershell.aspx" /><id>http://blogs.technet.com/b/vishalagarwal/archive/2009/09/07/exporting-certificate-from-user-store-to-pfx-using-powershell.aspx</id><published>2009-09-07T08:27:00Z</published><updated>2009-09-07T08:27:00Z</updated><content type="html">&lt;P style="MARGIN: 0in 0in 0pt" class=MsoPlainText&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;Alright, so today someone tried to contact me with an&amp;nbsp;interesting&amp;nbsp;email about exporting the certificate user store to PFX using powershell.&amp;nbsp; Below is the code that was contained in the email:&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoPlainText&gt;&lt;FONT size=3 face=Calibri&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoPlainText&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;$cert = (dir cert:\currentuser\my)[0]&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoPlainText&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;$type = [System.Security.Cryptography.X509Certificates.X509ContentType]::pfx&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoPlainText&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;$pass = read-host "pass" -assecurestring &lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoPlainText&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;$bytes = $cert.export($type, $pass)&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoPlainText&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;&lt;/FONT&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoPlainText&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;so far so good. Last line of the code was:&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;FONT face=Calibri&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoPlainText&gt;&lt;FONT size=3&gt;[system.convert]::ToBase64String($bytes) &amp;gt; file.pfx&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoPlainText&gt;&lt;FONT size=3&gt;Now this is where things got interesting as for the resulting PFX, certificate import&amp;nbsp;wizard&amp;nbsp;&amp;nbsp;does not seem to accept the same password. Why?? The problem is that certificate import wizard does not seem to convert the base64 data back to binary. Now instead of converting to base64 if you were to use the binary data itself as in:&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoPlainText&gt;&lt;FONT size=3&gt;[System.IO.File]::WriteAllBytes("file.pfx", $bytes)&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoPlainText&gt;&lt;FONT size=3&gt;This works well with the certificate import wizard or other tools.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3279500" width="1" height="1"&gt;</content><author><name>VishalAgarwal</name><uri>http://blogs.technet.com/VishalAgarwal/ProfileUrlRedirect.ashx</uri></author><category term="export certificate pfx powershell" scheme="http://blogs.technet.com/b/vishalagarwal/archive/tags/export+certificate+pfx+powershell/" /></entry><entry><title>Importing a PFX into user store using powershell</title><link rel="alternate" type="text/html" href="http://blogs.technet.com/b/vishalagarwal/archive/2009/08/28/importing-a-pfx-into-user-store-using-powershell.aspx" /><id>http://blogs.technet.com/b/vishalagarwal/archive/2009/08/28/importing-a-pfx-into-user-store-using-powershell.aspx</id><published>2009-08-28T03:41:00Z</published><updated>2009-08-28T03:41:00Z</updated><content type="html">&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="COLOR: #1f497d"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;This is a short post as someone asked me to give a sample for importing a pfx into user store using powershell:&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="COLOR: #1f497d"&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="COLOR: #1f497d"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;$pfxcert = new-object system.security.cryptography.x509certificates.x509certificate2&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="COLOR: #1f497d"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;$pfxcert.Import("pfxtest.pfx", "mypwd", [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]"UserKeySet")&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="COLOR: #1f497d"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;$store = new-object system.security.cryptography.X509Certificates.X509Store -argumentlist "MY", CurrentUser&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="COLOR: #1f497d"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;$store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]"ReadWrite")&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="COLOR: #1f497d"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;$store.Add($pfxcert)&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3277677" width="1" height="1"&gt;</content><author><name>VishalAgarwal</name><uri>http://blogs.technet.com/VishalAgarwal/ProfileUrlRedirect.ashx</uri></author><category term="pfx store powershell" scheme="http://blogs.technet.com/b/vishalagarwal/archive/tags/pfx+store+powershell/" /></entry><entry><title>Generating a certificate (self-signed) using powershell and CertEnroll interfaces</title><link rel="alternate" type="text/html" href="http://blogs.technet.com/b/vishalagarwal/archive/2009/08/22/generating-a-certificate-self-signed-using-powershell-and-certenroll-interfaces.aspx" /><id>http://blogs.technet.com/b/vishalagarwal/archive/2009/08/22/generating-a-certificate-self-signed-using-powershell-and-certenroll-interfaces.aspx</id><published>2009-08-22T03:00:00Z</published><updated>2009-08-22T03:00:00Z</updated><content type="html">&lt;P&gt;In this article I will explore using the certenroll interfaces to create certificates for&amp;nbsp;testing/local usage. To scope&amp;nbsp;the discussion,&amp;nbsp;we would look at various options exposed via makecert.exe&amp;nbsp;tool (&lt;A href="http://msdn.microsoft.com/en-us/library/aa386968(VS.85).aspx" mce_href="http://msdn.microsoft.com/en-us/library/aa386968(VS.85).aspx"&gt;http://msdn.microsoft.com/en-us/library/aa386968(VS.85).aspx&lt;/A&gt;&amp;nbsp;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;We will start by looking at a sample powershell script that creates a self-signed machine certificate that has "server auth" eku:&lt;/P&gt;
&lt;P&gt;$name = new-object -com "X509Enrollment.CX500DistinguishedName.1"&lt;BR&gt;$name.Encode("CN=TestServer", 0)&lt;/P&gt;
&lt;P&gt;$key = new-object -com "X509Enrollment.CX509PrivateKey.1"&lt;BR&gt;$key.ProviderName = "Microsoft RSA SChannel Cryptographic Provider"&lt;BR&gt;$key.KeySpec = 1&lt;BR&gt;$key.Length = 1024&lt;BR&gt;$key.SecurityDescriptor = "D:PAI(A;;0xd01f01ff;;;SY)(A;;0xd01f01ff;;;BA)(A;;0x80120089;;;NS)"&lt;BR&gt;$key.MachineContext = 1&lt;BR&gt;$key.Create()&lt;/P&gt;
&lt;P&gt;$serverauthoid = new-object -com "X509Enrollment.CObjectId.1"&lt;BR&gt;$serverauthoid.InitializeFromValue("1.3.6.1.5.5.7.3.1")&lt;BR&gt;$ekuoids = new-object -com "X509Enrollment.CObjectIds.1"&lt;BR&gt;$ekuoids.add($serverauthoid)&lt;BR&gt;$ekuext = new-object -com "X509Enrollment.CX509ExtensionEnhancedKeyUsage.1"&lt;BR&gt;$ekuext.InitializeEncode($ekuoids)&lt;/P&gt;
&lt;P&gt;$cert = new-object -com "X509Enrollment.CX509CertificateRequestCertificate.1"&lt;BR&gt;$cert.InitializeFromPrivateKey(2, $key, "")&lt;BR&gt;$cert.Subject = $name&lt;BR&gt;$cert.Issuer = $cert.Subject&lt;BR&gt;$cert.NotBefore = get-date&lt;BR&gt;$cert.NotAfter = $cert.NotBefore.AddDays(90)&lt;BR&gt;$cert.X509Extensions.Add($ekuext)&lt;BR&gt;$cert.Encode()&lt;/P&gt;
&lt;P&gt;$enrollment = new-object -com "X509Enrollment.CX509Enrollment.1"&lt;BR&gt;$enrollment.InitializeFromRequest($cert)&lt;BR&gt;$certdata = $enrollment.CreateRequest(0)&lt;BR&gt;$enrollment.InstallResponse(2, $certdata, 0, "")&lt;/P&gt;
&lt;P&gt;Let's investigate the sample line by line and see how it connects to the various options in makecert.exe&lt;/P&gt;
&lt;P&gt;The first 2 lines initializes the desired Subject name in the certificate:&lt;/P&gt;
&lt;P&gt;$name = new-object -com "X509Enrollment.CX500DistinguishedName.1"&lt;BR&gt;$name.Encode("CN=TestServer", 0)&lt;/P&gt;
&lt;P&gt;This covers the -n option of the makecert.exe. Infact the X509Enrollment.CX500DistinguishedName exposes all the various encoding options available (for example if you have comma in the CN value) which makecert.exe might not be able to do.&lt;/P&gt;
&lt;P&gt;The next block creates the subject's private key:&lt;/P&gt;
&lt;P&gt;$key = new-object -com "X509Enrollment.CX509PrivateKey.1"&lt;BR&gt;$key.ProviderName = "Microsoft RSA SChannel Cryptographic Provider"&lt;BR&gt;$key.KeySpec = 1&lt;BR&gt;$key.Length = 1024&lt;BR&gt;$key.SecurityDescriptor = "D:PAI(A;;0xd01f01ff;;;SY)(A;;0xd01f01ff;;;BA)(A;;0x80120089;;;NS)"&lt;BR&gt;$key.MachineContext = 1&lt;BR&gt;$key.Create()&lt;/P&gt;
&lt;P&gt;X509Enrollment.CX509PrivateKey gives you full control of what type of public/private key pair you want to create, down to what the desired container name should be, what should be provider type, provider name, desired key length, desired key spec, machine key Vs User key, who should be able to access the key, etc.&amp;nbsp;This covers -sp/-sy/-sky/-pe/-sk/-len options of makecert.exe&lt;/P&gt;
&lt;P&gt;Next block goes over and showcases the powerful set of various extension&amp;nbsp;interfaces exposed via certenroll.&lt;/P&gt;
&lt;P&gt;$serverauthoid = new-object -com "X509Enrollment.CObjectId.1"&lt;BR&gt;$serverauthoid.InitializeFromValue("1.3.6.1.5.5.7.3.1")&lt;BR&gt;$ekuoids = new-object -com "X509Enrollment.CObjectIds.1"&lt;BR&gt;$ekuoids.add($serverauthoid)&lt;BR&gt;$ekuext = new-object -com "X509Enrollment.CX509ExtensionEnhancedKeyUsage.1"&lt;BR&gt;$ekuext.InitializeEncode($ekuoids)&lt;/P&gt;
&lt;P&gt;This particular sample covers EKU extension. Full list is available here: &lt;A href="http://msdn.microsoft.com/en-us/library/ee338596(VS.85).aspx" mce_href="http://msdn.microsoft.com/en-us/library/ee338596(VS.85).aspx"&gt;http://msdn.microsoft.com/en-us/library/ee338596(VS.85).aspx&lt;/A&gt;. For extensions not in the list, The generic IX509Extension interface can be used directly (although you own encoding of the extension value correctly in ASN.1). This should cover the options -l (for policy extension)/ -cy and -h (for basic constraint extension/ -eku/-nscp (you will have to use the generic IX509Extension interface here, I would be curious if anyone actually needs this extension. Drop me a comment if you are unable to create sample for that).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;Next block of script uses IX509CertificateRequestCertificate interface to actual create the self-signed certificate.&lt;/P&gt;
&lt;P&gt;$cert = new-object -com "X509Enrollment.CX509CertificateRequestCertificate.1"&lt;BR&gt;$cert.InitializeFromPrivateKey(2, $key, "")&lt;BR&gt;$cert.Subject = $name&lt;BR&gt;$cert.Issuer = $cert.Subject&lt;BR&gt;$cert.NotBefore = get-date&lt;BR&gt;$cert.NotAfter = $cert.NotBefore.AddDays(90)&lt;BR&gt;$cert.X509Extensions.Add($ekuext)&lt;BR&gt;$cert.Encode()&lt;/P&gt;
&lt;P&gt;Various options in this interface are pretty self-explainatory. The NotBefore and NotAfter properties cover the -m and -e option of makecert.exe. SerialNumber property can be used to cover -# option of makecert.exe. SignatureInformation property covers the -a option of makecert.exe. SignerCertificate property can be used when you don't want the certificate to be self-signed.&lt;/P&gt;
&lt;P&gt;The last&amp;nbsp;block of script actually&amp;nbsp;installs the just created certificate into the&amp;nbsp;physical store so that it is available to the desired application for usage.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;$enrollment = new-object -com "X509Enrollment.CX509Enrollment.1"&lt;BR&gt;$enrollment.InitializeFromRequest($cert)&lt;BR&gt;$certdata = $enrollment.CreateRequest(0)&lt;BR&gt;$enrollment.InstallResponse(2, $certdata, 0, "")&lt;/P&gt;
&lt;P&gt;This is the critical step as IX509Enrollment interface ensures that Certificate is stored in appropriate store as well as ensure that the private key is accessible (by associated correct KeyProvInfo property on the certificate).&lt;/P&gt;
&lt;P&gt;I've deliberately stayed away from Storage (Issuers/Subject store name/location) as those can be easily taken care by X509Store/X509Certificate2 class objects exposed via .NET.&lt;/P&gt;
&lt;P&gt;The only thing that is missing is the direct support of PVK file (both input or output). However for that there are various tools (including third-party) that can convert the content from PFX to PVK and vice-versa. These tools can be leveraged to produce/consume PVK files as and when needed.&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3276040" width="1" height="1"&gt;</content><author><name>VishalAgarwal</name><uri>http://blogs.technet.com/VishalAgarwal/ProfileUrlRedirect.ashx</uri></author><category term="powershell" scheme="http://blogs.technet.com/b/vishalagarwal/archive/tags/powershell/" /><category term="makecert.exe" scheme="http://blogs.technet.com/b/vishalagarwal/archive/tags/makecert-exe/" /><category term="X509Enrollment" scheme="http://blogs.technet.com/b/vishalagarwal/archive/tags/X509Enrollment/" /></entry><entry><title>Adding certificates for a serialized store (sst) file to an actual physical store</title><link rel="alternate" type="text/html" href="http://blogs.technet.com/b/vishalagarwal/archive/2009/08/13/adding-certificates-for-a-serialized-store-sst-file-to-an-actual-physical-store.aspx" /><id>http://blogs.technet.com/b/vishalagarwal/archive/2009/08/13/adding-certificates-for-a-serialized-store-sst-file-to-an-actual-physical-store.aspx</id><published>2009-08-13T23:50:00Z</published><updated>2009-08-13T23:50:00Z</updated><content type="html">&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="COLOR: #1f497d"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;In my previous post I used the CMS type to open a PKCS7. Apparently X509Certificate2Collection Import method can&amp;nbsp;also be used to open up a PKCS7. This&amp;nbsp;would be far more simpler then using CMS.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="COLOR: #1f497d"&gt;&lt;FONT size=3 face=Calibri&gt;Additionally, you might be asked to add the certificates you obtained from PKCS7 file or a serialized store (sst) file to an actual store. Below is an example powershell script to accomplish it:&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="COLOR: #1f497d"&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="COLOR: #1f497d"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;[reflection.assembly]::LoadWithPartialName("System.Security")&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="COLOR: #1f497d"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;$certs = new-object system.security.cryptography.x509certificates.x509certificate2collection&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="COLOR: #1f497d"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;$certs.import("additionalroots.sst")&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="COLOR: #1f497d"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;$store = new-object system.security.cryptography.X509Certificates.X509Store -argumentlist "AuthRoot", LocalMachine&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" class=MsoNormal&gt;&lt;SPAN style="COLOR: #1f497d"&gt;&lt;FONT size=3&gt;&lt;FONT face=Calibri&gt;$store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]"ReadWrite")&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;SPAN style="FONT-FAMILY: 'Calibri','sans-serif'; COLOR: #1f497d; FONT-SIZE: 11pt; mso-fareast-font-family: Calibri; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA; mso-fareast-theme-font: minor-latin"&gt;$store.AddRange($certs)&lt;/SPAN&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3273266" width="1" height="1"&gt;</content><author><name>VishalAgarwal</name><uri>http://blogs.technet.com/VishalAgarwal/ProfileUrlRedirect.ashx</uri></author><category term="pkcs7" scheme="http://blogs.technet.com/b/vishalagarwal/archive/tags/pkcs7/" /><category term="powershell" scheme="http://blogs.technet.com/b/vishalagarwal/archive/tags/powershell/" /><category term="X509Certificate2" scheme="http://blogs.technet.com/b/vishalagarwal/archive/tags/X509Certificate2/" /><category term="X509certificate2Collection" scheme="http://blogs.technet.com/b/vishalagarwal/archive/tags/X509certificate2Collection/" /><category term="X509Store" scheme="http://blogs.technet.com/b/vishalagarwal/archive/tags/X509Store/" /></entry><entry><title>PKCS7 (p7b) bag of certificates and powershell</title><link rel="alternate" type="text/html" href="http://blogs.technet.com/b/vishalagarwal/archive/2009/04/30/pkcs7-p7b-bag-of-certificates-and-powershell.aspx" /><id>http://blogs.technet.com/b/vishalagarwal/archive/2009/04/30/pkcs7-p7b-bag-of-certificates-and-powershell.aspx</id><published>2009-04-30T23:33:00Z</published><updated>2009-04-30T23:33:00Z</updated><content type="html">&lt;P&gt;Recently I was asked how to extract the certificates within a PKCS7 (p7b) files using powershell. After a little research the following seems to work fine:&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-FAMILY: 'Calibri','sans-serif'; FONT-SIZE: 11pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA"&gt;[reflection.assembly]::LoadWithPartialName("System.Security")&lt;BR&gt;$data = [System.IO.File]::ReadAllBytes("certificates.p7b")&lt;BR&gt;$cms = new-object system.security.cryptography.pkcs.signedcms&lt;BR&gt;$cms.Decode($data)&lt;BR&gt;$cms.Certificates | foreach {New-Object System.Security.Cryptography.X509Certificates.X509Certificate2 $_} | echo&lt;/SPAN&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3233735" width="1" height="1"&gt;</content><author><name>VishalAgarwal</name><uri>http://blogs.technet.com/VishalAgarwal/ProfileUrlRedirect.ashx</uri></author><category term="signedcms" scheme="http://blogs.technet.com/b/vishalagarwal/archive/tags/signedcms/" /><category term="p7b" scheme="http://blogs.technet.com/b/vishalagarwal/archive/tags/p7b/" /><category term="pkcs7" scheme="http://blogs.technet.com/b/vishalagarwal/archive/tags/pkcs7/" /><category term="powershell" scheme="http://blogs.technet.com/b/vishalagarwal/archive/tags/powershell/" /></entry></feed>