Hi all,
Here's little how-to for signing code. I hope this helps someone out there...
Code signing with a StartSSL Level 2 certificate - a How-To
You will need:
a) OpenSSL for windows version 0.9.8 or better. You can get it from:
http://slproweb.com/products/Win32OpenSSL.htmlb) a conversion utility called pvk.exe, created by Dr. Steven N. Henson. You can get it from:
http://www.drh-consultancy.demon.co.uk/pvk.htmlor from:
http://www.tech-pro.net/files/pvktool.zipIf neither of these links work, you'll probably have to Google it.
c) The Microsoft code signing tools. They come in the .net framework SDK, or as part of Visual Studio. I used the v1.1 SDK that ships with Visual Studio 2003. If you use the .net framework SDK version 2 or greater, you'll use a tool called signtool.exe in place of the signcode.exe tool that's described below. The arguments to the tools are somewhat different. Check the documentation on the Microsoft website for signtool if you're using that one. You should be able to get either SDK from the microsoft website (but since they completely change their website on a weekly basis, I can't tell you where to find it. If I did, I'd be wrong the next week.)
You will create:
mykey.pem - Your private key in PEM format.
myreq.pem - Your certificate signing request in PEM format. You send this to StartSSL to get a certificate.
mycert.pem - Your certificate in PEM format.
mykey.pvk - Your private key in Microsoft's CryptoAPI format (basically a proprietary, undocumented format)
mycert.spc - Your certificate in Microsoft's CryptoAPI format as a Software Publisher's Certificate.
PEM format is readable by any text editor (i.e. notepad) so you can see what you've got just by opening the file in notepad. As an example, once you've created your private key file (mykey.pem) you can open it in notepad and you'll see the file start with the line:
-----BEGIN RSA PRIVATE KEY----- and end with:
-----END RSA PRIVATE KEY-----
First, acquire a Level 2 validation from Startssl.com so you can get a Level 2 certificate.
Next, create a certificate signing request using OpenSSL:
Code:
openssl req -new -newkey rsa:2048 -keyout mykey.pem -out myreq.pem
You will be asked to supply a number of items:
The PEM passphrase
and the PEM passphrase again, for verification
The country name
The state or province name
The locality name
The Organization name
The Organizational unit name
The Common Name
Email address
and a number of 'extra' attributes that you can leave blank:
A challenge password
An optional company name
Once you've filled in all these fields or pressed Enter on the blank values, OpenSSL will create two files - mykey.pem and myreq.pem
Open notepad and open the file myreq.pem in notepad. The first line in the file should read:
Quote:
-----BEGIN CERTIFICATE REQUEST-----
and should end with:
Quote:
-----END CERTIFICATE REQUEST-----
Highlight all the contents of this file, and type Ctrl-C to copy the contents to the clipboard. Once you've done that, you can close notepad.
Log on to the startssl.com secure server, choose the certificate wizard, then select "Object code signing certificate" from the drop down list.
Click on the submission box for the code signing request and type Ctrl-V to paste the contents of the clipboard into this box, then click Ok.
You'll get a new window with your Certificate in a PEM format. Copy the contents of this certificate into a local file. In this how-to, we're calling this certificate file mycert.pem.
At this point you've got 3 files, mykey.pem, myreq.pem and mycert.pem. We're finished with myreq.pem, but we need to convert the other two (mykey.pem and mycert.pem) into the format that Microsoft understands.
Next, you create the file mykey.pvk from mykey.pem by using Dr. Henson's wonderful little tool:
Code:
pvk -in mykey.pem -topvk -out mykey.pvk
Next, create the mycert.spc certificate file. There are two ways to do this. Microsoft has a little utility in their SDK called cert2spc.exe. In their documentation, Microsoft says this utility is only for creating test certificates, but it seems to work quite well on real certificates:
cert2spc mycert.pem mycert.spc
Alternatively, you can use OpenSSL:
Code:
openssl crl2pkcs7 -nocrl -certfile mycert.pem -outform DER -out mycert.spc
FINALLY, you're ready to actually sign an executable:
Code:
signcode -spc mycert.spc -v mykey.pvk -n "Your product name here" \
-t http://timestamp.verisign.com/scripts/timstamp.dll \
"Your executable file name here"
Of course all this should be on one line. I've used the backslash characters to break up the line in this tutorial, but you should enter everything on the one line.
To check that your executable is properly signed, you can use the chktrust utility:
Code:
chktrust /v "Your executable file name here"
Chktrust will open a dialog in which you can see the name of the publisher (you) and the name of the product, and allow you examine the certificate.
Congratulations, you're done!!!!!