<?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>Huddled Masses &#187; Digital signature</title>
	<atom:link href="http://huddledmasses.org/tag/digital-signature/feed/" rel="self" type="application/rss+xml" />
	<link>http://huddledmasses.org</link>
	<description>You can do more than breathe for free...</description>
	<lastBuildDate>Fri, 27 Apr 2012 05:42:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<cloud domain='huddledmasses.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>Signing PowerShell Scripts &#8211; Automatically</title>
		<link>http://huddledmasses.org/signing-powershell-scripts-automatically/</link>
		<comments>http://huddledmasses.org/signing-powershell-scripts-automatically/#comments</comments>
		<pubDate>Sun, 15 Nov 2009 05:53:56 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Authenticode]]></category>
		<category><![CDATA[Code Signing]]></category>
		<category><![CDATA[Cryptography]]></category>
		<category><![CDATA[Digital signature]]></category>
		<category><![CDATA[FileSystemWatcher]]></category>
		<category><![CDATA[PowerShell]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1317</guid>
		<description><![CDATA[I write a lot of PowerShell scripts, and I work on modules a lot, including ones which I load automatically in my profile. Sometimes I forget to sign those modules before I restart my console/host, and then I get errors when they&#8217;re loaded from the profile. I wrote a script to solve this problem by [...]]]></description>
			<content:encoded><![CDATA[	<p>I write a lot of PowerShell scripts, and I work on modules a lot, including ones which I load automatically in my profile.  Sometimes I forget to sign those modules before I restart my console/host, and then I get errors when they&#8217;re loaded from the profile.  I wrote a script to solve this problem by automatically signing files every time they&#8217;re edited, and I figured I&#8217;d share it below, but first let me try explaining what code signing is, and why you should care.</p>

	<h3>What is code signing?</h3>

	<p><a href="http://en.wikipedia.org/wiki/Code_signing">Code signing</a> is the process of digitally signing files to confirm the author and guarantee that the file has not been altered or corrupted.  It&#8217;s essentially the same as signing emails: it guarantees that they were actually authored by the person claiming to be the author, and that they weren&#8217;t changed after the author signed them. (For more information see <a href="http://developer.apple.com/mac/library/documentation/Security/Conceptual/CodeSigningGuide/AboutCS/AboutCS.html#//apple_ref/doc/uid/TP40005929-CH3-SW3">Apple&#8217;s explanation</a> and <a href="http://msdn.microsoft.com/en-us/library/ms537361%28VS.85%29.aspx">Microsoft&#8217;s</a>).</p>

	<h3>How does digital signing work?</h3>

	<p>Basically, digital signing is cryptography, so a full understanding of it really requires taking a 400-level college course, but <a href="http://www.instantssl.com/code-signing/code-signing-process.html">here are the basics</a>:  You take some data (binary bits) and use a well-known algorithm to calculate what is called a &#8220;hash&#8221; of the bits. This hash is mathematically-generated from the source data, and so it can be re-calculated by the receiver.  The hash is then &#8220;signed&#8221; via another algorithm: using a private key (see <a href="http://www.answers.com/topic/pki#Computer_Desktop_Encyclopedia_d_ans">Public Key Infrastructure</a> (<span class="caps">PKI</span>) for more about how these keys are managed) the signer encrypts the hash and attaches it to the file.  </p>

	<p>With public/private key encryption, data that is encrypted by the private key can be decrypted by the public key, so the encryption of the hash (and sometimes the public key, as well) is attached to the file so that the end user can re-calculate the hash and decrypt the signature to verify: a) that it was encrypted by the private key belonging to the author, and b) that the hash matches the hash that the author signed.</p>

	<h3>What&#8217;s all this about keys?</h3>

	<p>In <span class="caps">PKI</span>, the &#8220;key&#8221; is actually part of what&#8217;s called an <a href="http://en.wikipedia.org/wiki/Ssl_certificate"><span class="caps">SSL</span> certificate</a> issued to you by a Certificate Authority (CA) who verifies your identity. Verisign introduced the idea of &#8220;classes&#8221; for certificates, and you can now get free, <strong>Class 1</strong> certificates for email signing from most CA&#8217;s now (like <a href="http://www.thawte.com/secure-email/personal-email-certificates/">Thawte</a> and <a href="http://www.thawte.com/secure-email/personal-email-certificates/">Comodo</a> and <a href="http://www.startssl.com/?app=1">StartCOM</a>), where the only thing they <em>verify</em> is that the person being issued the certificate can send/receive email at that address.</p>

	<p>Of course, if you want to verify yourself as the author of <strong>code,</strong> we want to know more about you than your email address.   So to sign code, you need <em>at least</em> a <strong>Class 2</strong> certificate, for which proof of identity is required.  This typically comes in the form of providing (photos of) your passport, driver&#8217;s license and/or other photo ID to verify that you are who you claim to be, and answering a few questions on the phone to validate that you own the phone number provided&#8230; or providing tax or other documents to prove the existence of an organization and that you&#8217;re qualified to speak for them. There are also additional classes: Class 3, 4, and 5 are extended validation certificates used for servers, software signing, b2b transactions and government security.</p>

	<h3>Why should I sign PowerShell scripts?</h3>

	<p>Well, the chances are, you don&#8217;t need to.  Script signing is really a feature for corporations.  The idea is that scripts from your IT department need to be verifiable and unalterable&#8230; code-signing gives companies a way to lock down and secure PowerShell scripts: setting a global policy requiring script signing, and ensuring that all scripts produced by the corporate administrators are properly signed&#8230;</p>

	<p>As an individual, script-signing doesn&#8217;t really offer you much unless you share your computer with other administrator users.  After all, what it protects you from is  intentional or accidental altering of the script.  If there&#8217;s no one else who has access to your computer, then nobody can alter your scripts.  Of course, if someone <em>hacks</em> your computer enough to alter or create files on your hard drive and wanted to do something malicious, scripts would be a silly thing to target  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=':)' class='wp-smiley' /> </p>

	<p>There is, however, one additional time when you might want to sign your scripts.  If you&#8217;re distributing scripts, whether via <a href="http://PoshCode.org">PoshCode.org</a>, the TechNet script gallery, or on CodePlex or the <span class="caps">MSDN</span> code gallery &#8230; signing them gives you (and your users) the assurance that <strong>you</strong> are the author, and they haven&#8217;t been altered.  In fact, the PoshCode Module which is available on <a href="http://PoshCode.org">PoshCode.org</a>, and which allows you to search and download (as well as upload) PoshCode scripts uses script signing to allow it to safely upgrade itself.  It features a <code>Get-PoshCode -Upgrade</code> command which will check for, and fetch, the latest version of the PoshCode module, and then validate it&#8217;s code signature is the same as the previous one before using it.</p>

	<p>Remember, code signing <strong>cannot</strong>:</p>

	<ul>
		<li>Guarantee that code is free of security vulnerabilities.</li>
		<li>Guarantee that a script doesn&#8217;t load unsafe or altered code during execution.</li>
	</ul>
	<ul>
		<li>Prevent copying or altering of your scripts (although PowerShell policies can prevent <em>accidental execution</em> of altered scripts).</li>
	</ul>

	<h2>How do I sign scripts?</h2>

	<p>Assuming that after skimming all of that, you want to try signing your scripts, you need a certificate. You can <a href="http://huddledmasses.org/code-signing-with-openssl-and-powershell/">generate one yourself</a> (although it&#8217;s not easy, and the signed scripts which will only work locally on your system, and won&#8217;t be trusted by anyone else) or you can get one from a public Certificate Authority (I recommend <a href="http://www.startssl.com/?app=2">StartCom/StartSSL</a> which only costs $40 for two year certificates).  </p>

	<p>Typically, your certificate will be a .pfx file which you can load using <code>Get-PfxCertificate</code>, or you might have imported it into your local certificate store using CertMgr.msc, in which case you can load it using Get-Item Cert:\CurrentUser\My\ with the thumbprint of the certificate.  In either case, you&#8217;ll have a certificate object you can pass to <code>Set-AuthenticodeSignature</code> along with the file path.</p>

	<h1>Wasn&#8217;t there supposed to be something automatic here?</h1>

	<p>Awhile back I wrote an <a href="http://poshcode.org/1473">Authenticode Script Module</a> which has wrappers for signing and testing signatures on scripts.  It uses a metadata file (which you have to create yourself) with a <code>PrivateData</code> variable that points to either the thumbprint of a certificate you&#8217;ve imported to your computer&#8217;s certificate store, or the path to a .pfx file &#8230; and allows you to sign files by just writing <code>sign .\FileToSign.ps1</code> without having to Get-PfxCertificate each time, or even remember to turn on the -TimeStampUrl feature so that your signed scripts stay signed even after the certificate expires.  That script helps a lot, but I&#8217;ve been wanting my text editor to sign the scripts automatically when I save them &#8230;</p>

	<p>I mostly use <a href="http://notepad-plus.sourceforge.net/">Notepad++</a> for editing PowerShell scripts (with a <a href="http://PoshCode.org/notepad++lexer">PowerShell Syntax lexer I helped create</a>), but after playing with the idea of writing a plugin to sign things after saving, I ended up deciding to go with something simpler: a PowerShell script to watch a folder for changes and sign any scripts I edit or save.</p>

	<p>I&#8217;ve added a function I call <code>Start-AutoSign</code> to my <a href="http://poshcode.org/1473">authenticode script module</a> which takes a path and a <code>-Recurse</code> flag and sets up a <a href="http://msdn.com/System.IO.FileSystemWatcher">System.IO.FileSystemWatcher</a> to detect saves, creates, and moves &#8230; and (re)sign the script if it needs it. Here is <code>Start-AutoSign</code> without the help docs or anything (so I can discuss it). You should just download the whole <a href="http://poshcode.org/1473">script module</a> if you want to use it  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=':)' class='wp-smiley' /> </p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #666699; font-weight: bold;">PARAM</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Path</span><span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;.&quot;</span>, <span style="color: #660033; font-weight: bold;">$Filter</span><span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;*.ps*&quot;</span>, <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #666699; font-weight: bold;">Switch</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Recurse</span>, <span style="color: #660033; font-weight: bold;">$CertPath</span>, <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #666699; font-weight: bold;">Switch</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$NoNotify</span><span style="color: #333;">&#41;</span><br />
<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #660033; font-weight: bold;">$NoNotify</span> <span style="color: #000066;">-and</span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Module</span></span> Growl <span style="color: #000066;">-ListAvailable</span> <span style="color: #000066;">-ErrorAction</span> <span style="color: #cc66cc;">0</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Import-<span style="font-style: normal;">Module</span></span> Growl<br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Register-<span style="font-style: normal;">GrowlType</span></span> AutoSign <span style="color: #009900;">&quot;Signing File&quot;</span> <span style="color: #000066;">-ErrorAction</span> <span style="color: #cc66cc;">0</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$NoNotify</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$false</span> <span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$realItem</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Item</span></span> <span style="color: #660033; font-weight: bold;">$Path</span> <span style="color: #000066;">-ErrorAction</span> Stop<br />
<span style="color: #666699; font-weight: bold;">if</span> <span style="color: #333;">&#40;</span><span style="color: #66cc66;">-</span><span style="color: #333399; font-weight: bold; font-style: italic;">not</span> <span style="color: #660033; font-weight: bold;">$realItem</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <span style="color: #666699; font-weight: bold;">return</span> <span style="color: #333;">&#125;</span> <br />
<br />
<span style="color: #660033; font-weight: bold;">$Action</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$InvalidForm</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;The form specified for the subject is not one supported or known by the specified trust provider&quot;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">ForEach</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$file</span> <span style="color: #666699; font-weight: bold;">in</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">ChildItem</span></span> <span style="color: #660033; font-weight: bold;">$eventArgs</span>.<span style="color: #003366;">FullPath</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">AuthenticodeSignature</span></span> <span style="color: #66cc66;">|</span> <br />
&nbsp; &nbsp; &nbsp; Where<span style="color: #66cc66;">-</span>Object <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Status</span> <span style="color: #000066;">-ne</span> <span style="color: #009900;">&quot;Valid&quot;</span> <span style="color: #000066;">-and</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">StatusMessage</span> <span style="color: #000066;">-ne</span> <span style="color: #660033; font-weight: bold;">$invalidForm</span> <span style="color: #333;">&#125;</span> <span style="color: #66cc66;">|</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Select-<span style="font-style: normal;">Object</span></span> <span style="color: #000066;">-ExpandProperty</span> Path <span style="color: #333;">&#41;</span> <br />
&nbsp; &nbsp;<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #660033; font-weight: bold;">$NoNotify</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Send-<span style="font-style: normal;">Growl</span></span> AutoSign <span style="color: #009900;">&quot;Signing File&quot;</span> <span style="color: #009900;">&quot;File $($eventArgs.ChangeType), signing:&quot;</span> <span style="color: #009900;">&quot;$file&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$CertPath</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">AuthenticodeSignature</span></span> <span style="color: #000066;">-FilePath</span> <span style="color: #660033; font-weight: bold;">$file</span> <span style="color: #000066;">-Certificate</span> <span style="color: #660033; font-weight: bold;">$CertPath</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">AuthenticodeSignature</span></span> <span style="color: #000066;">-FilePath</span> <span style="color: #660033; font-weight: bold;">$file</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #660033; font-weight: bold;">$watcher</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> IO.<span style="color: #003366;">FileSystemWatcher</span> <span style="color: #660033; font-weight: bold;">$realItem</span>.<span style="color: #003366;">Fullname</span>, <span style="color: #660033; font-weight: bold;">$filter</span> <span style="color: #000066;">-Property</span> @<span style="color: #333;">&#123;</span> IncludeSubdirectories <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$Recurse</span> <span style="color: #333;">&#125;</span><br />
<span style="color: #0066cc; font-style: italic;">Register-<span style="font-style: normal;">ObjectEvent</span></span> <span style="color: #660033; font-weight: bold;">$watcher</span> <span style="color: #009900;">&quot;Created&quot;</span> <span style="color: #009900;">&quot;AutoSignCreated$($realItem.Fullname)&quot;</span> <span style="color: #000066;">-Action</span> <span style="color: #660033; font-weight: bold;">$Action</span> <span style="color: #66cc66;">&amp;</span>gt; <span style="color: #660033; font-weight: bold;">$null</span><br />
<span style="color: #0066cc; font-style: italic;">Register-<span style="font-style: normal;">ObjectEvent</span></span> <span style="color: #660033; font-weight: bold;">$watcher</span> <span style="color: #009900;">&quot;Changed&quot;</span> <span style="color: #009900;">&quot;AutoSignChanged$($realItem.Fullname)&quot;</span> <span style="color: #000066;">-Action</span> <span style="color: #660033; font-weight: bold;">$Action</span> <span style="color: #66cc66;">&amp;</span>gt; <span style="color: #660033; font-weight: bold;">$null</span><br />
<span style="color: #0066cc; font-style: italic;">Register-<span style="font-style: normal;">ObjectEvent</span></span> <span style="color: #660033; font-weight: bold;">$watcher</span> <span style="color: #009900;">&quot;Renamed&quot;</span> <span style="color: #009900;">&quot;AutoSignChanged$($realItem.Fullname)&quot;</span> <span style="color: #000066;">-Action</span> <span style="color: #660033; font-weight: bold;">$Action</span> <span style="color: #66cc66;">&amp;</span>gt; <span style="color: #660033; font-weight: bold;">$null</span></div>

	<p>A few things to notice: first, I&#8217;m assuming you&#8217;re using <strong>my</strong> Set-AuthenticodeSignature wrapper, so by default I don&#8217;t pass it an actual certificate.  Second, in order to avoid eternally looping and re-signing the script over-and-over, you have to have a way to make sure you don&#8217;t re-sign it when it &#8220;changes&#8221; because you signed it.  The approach I took was to test and see if the signature was valid or not (and if it&#8217;s valid, then don&#8217;t sign it <em>again</em>).  Third: when you <code>Get-AuthenticodeSignature</code>, the Status codes leave much to be desired, returning the same &#8220;UnknownError&#8221; in several cases where they know <em>exactly</em> what the error is.  You can check the StatusMessage and see that they even <strong>tell</strong> you what the error was:</p>

	<ul>
		<li>The form specified for the subject is not one supported or known by the specified trust provider</li>
		<li>A certificate chain could not be built to a trusted root authority</li>
		<li>A certificate chain processed, but terminated in a root certificate which is not trusted by the trust provider</li>
	</ul>
	<ul>
		<li>A required certificate is not within its validity period when verifying against the current system clock or the timestamp in the signed file</li>
	</ul>

	<p>There may be other reasons too that I haven&#8217;t encountered yet, but the one thing we&#8217;re concerned with is the first one that the &#8220;form&#8221; isn&#8217;t supported. That means this is a file type which Set-AuthenticodeSignature can&#8217;t sign. PowerShell&#8217;s signing only works with executables, PowerShell scripts, modules, metadata, and xml files &#8230; so there&#8217;s no point trying to sign anything else.  You may also notice that in my script (by default) I&#8217;m only signing .ps* files (that is: scripts, modules, metadata and ps1xml files), and that&#8217;s because with .dll and .exe files, I would prefer to sign them as part of my build step anyway.</p>

	<p>One last thing: as with a few of my recent scripts, this one is designed to use <a href="http://GrowlForWindows.com">Growl for Windows</a> to notify you whenever it signs anything. If you don&#8217;t have the <a href="http://poshcode.org/1464">Growl module</a> available, it should just skip right over that without any problems, but you can disable it in any case with the <code>-NoNotify</code> switch.  Personally, I like seeing the popup so that I know it&#8217;s working, and know that I&#8217;m not accidentally signing something &#8230; and I love Growl for script notices because I can skin it, configure sounds, and forward the notices to other machines &#8230; or even temporarily disable them, all without having to tweak scripts.</p>

<div class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/3a72101f-e58c-47ed-accc-fc1c866ae964/" title="Reblog this post [with Zemanta]"><img class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=3a72101f-e58c-47ed-accc-fc1c866ae964" alt="Reblog this post [with Zemanta]" /></a><span class="zem-script more-related pretty-attribution"><script type="text/javascript" src="http://static.zemanta.com/readside/loader.js" defer="defer"></script></span></div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/signing-powershell-scripts-automatically/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

