15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Distributing Code Using Certificates ❘ 567<br />

}<br />

{<br />

}<br />

CodeAccessPermission permissionA =<br />

new FileIOPermission(FileIOPermissionAccess.AllAccess, @"C:\");<br />

CodeAccessPermission permissionB =<br />

new FileIOPermission(FileIOPermissionAccess.Read, @"C:\temp");<br />

if (permissionB.IsSubsetOf(permissionA))<br />

{<br />

Console.WriteLine("PermissionB is a subset of PermissionA");<br />

}<br />

code snippet ImplicitPermissions/Program.cs<br />

The output looks like this:<br />

PermissionB is a subset of PermissionA<br />

disTribuTing Code using CerTifiCaTes<br />

You can make use of digital certifi cates <strong>and</strong> sign assemblies so that consumers of the software can verify the<br />

identity of the software publisher. Depending on where the application is used, certifi cates may be required.<br />

For example, with ClickOnce the user installing the application can verify the certifi cate to trust the<br />

publisher. Using Windows Error Reporting, Microsoft uses the certifi cate to fi nd out which vendor to map<br />

to the error report.<br />

ClickOnce is explained in Chapter 17, “Deployment.” Windows Error Reporting is<br />

discussed in Appendix A, “Guidelines for Windows 7 <strong>and</strong> Windows Server 2008 R2.”<br />

In a commercial environment, you would obtain a certifi cate from a company such as Verisign or<br />

Thawte. The advantage of buying a certifi cate from a supplier instead of creating your own is that it<br />

provides a high level of trust in the authenticity of the certifi cate; the supplier acts as a trusted third party.<br />

For test purposes, however, .<strong>NET</strong> includes a comm<strong>and</strong> - line utility you can use to create a test certifi cate.<br />

The process of creating certifi cates <strong>and</strong> using them for publishing software is complex, but we walk through<br />

a simple example in this section.<br />

The example code is for a fi ctitious company called ABC Corporation. The company ’ s software product<br />

(simple.exe) should be trusted. First, create a test certifi cate by typing the following comm<strong>and</strong>:<br />

makecert -sv abckey.pvk -r -n "CN=ABC Corporation" abccorptest.cer<br />

The comm<strong>and</strong> creates a test certifi cate under the name ABC Corporation <strong>and</strong> saves it to a fi le called<br />

abccorptest.cer . The - sv abck e y . p v k argument creates a key fi le to store the private key. When creating<br />

the key fi le, you are asked for a password that you should remember.<br />

After creating the certifi cate, you can create a software publisher test certifi cate with the Software Publisher<br />

Certifi cate Test tool ( Cert2spc.exe ):<br />

> cert2spc abccorptest.cer abccorptest.spc<br />

With a certifi cate that is stored in an spc fi le <strong>and</strong> the key fi le that is stored in a pvk fi le, you can create a pfx<br />

fi le t h at c ont a i n s b ot h w it h t he pvk2pfx utility:<br />

> pvk2pfx -pvk abckey.pvk -spc abccorptest.spc -pfx abccorptest.pfx<br />

Now the assembly can be signed with the signtool.exe utility. The sign option is used for signing, -f<br />

specifi es the certifi cate in the pfx fi le, <strong>and</strong> -v is for verbose output:<br />

> signtool sign -f abccorptest.pfx -v simple.exe<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!