Lazarus

Using the Lazarus IDE => General => Topic started by: CCRDude on May 12, 2017, 03:18:29 pm

Title: Codesigning (Authenticode) IDE package
Post by: CCRDude on May 12, 2017, 03:18:29 pm
Here's another plugin I wrote for my daily use. This is only useful for those developer who invest into getting a codesigning certificate.

I tend to codesign all Windows files I create with Lazarus. Previously, I've been doing this with post build events. Updating these for all projects was tedious (e.g. when I introduced SHA256 in addition to SHA1 signing, when the certificate has changed, when the timestamping server has changed, when pagehashing was added, ...).

With this package, there's a central IDE Options page to set up certificate, timestamping server, signtool executable location, and a few more attributes, global changes are much easier. The description is picked from the current project. A flag in the Options allows to auto-sign after each build; and there's a menu item to do it manually. Project-specific settings are something I'm thinking about.

LazCodeSigningHelper (http://ccrdude.net/LazCodeSigningHelper/)

Feedback welcome. Screenshots at the link above.
Title: Re: Codesigning (Authenticode) IDE package
Post by: mig-31 on May 12, 2017, 04:21:45 pm
What about other OS: Linux, MacOs?
Title: Re: Codesigning (Authenticode) IDE package
Post by: CCRDude on May 12, 2017, 05:44:54 pm
I would have to check what options these OS have...

macOS has a command line tool "codesign". Since I'm often working from a Mac (just RDP'ing to the Windows dev machine), I probably could easily adapt to macOS-specific codesigning stuff. Just a different Options frame, a different executable to call and different parameters, if TProcess works the same on macOS.

I use Linux on a few servers, but have no idea at all whether there's any similar codesigning available there. I know that Debian packages are signed, for example, but I know nothing about single executables. Can you hint me at how it's done on Linux?

Title: Re: Codesigning (Authenticode) IDE package
Post by: Phil on May 12, 2017, 06:04:06 pm
macOS has a command line tool "codesign". Since I'm often working from a Mac (just RDP'ing to the Windows dev machine), I probably could easily adapt to macOS-specific codesigning stuff. Just a different Options frame, a different executable to call and different parameters, if TProcess works the same on macOS.

Looks like you've done a thorough job. This is certainly something that the Laz IDE has always lacked and that all other IDEs have had for a long time.

It would be nice if this could support OS X too. For anyone interested, here's Apple's documentation on codesigning:

https://developer.apple.com/library/content/documentation/Security/Conceptual/CodeSigningGuide/Introduction/Introduction.html

Back when I still developed Windows desktop apps, I typically only signed the installer, not the executable included in the installer. While it's a good idea to sign all executables, the installer is the first one the user will see and many organizations won't allow unsigned installers to be run. I always just used signtool in a batch file, but I believe Inno Setup also has support for including signing in the .iss script. That's probably the one piece missing here and an important piece, although it may be beyond what should be done in the IDE.

I also RDP into my Windows Server (on AWS) from my Mac. I use Microsoft Remote Desktop - very nice and works perfectly.

Note that the distributed Lazarus IDE executables and installers are never signed. They should be.
Title: Re: Codesigning (Authenticode) IDE package
Post by: Thaddy on May 12, 2017, 06:19:22 pm
Code signing is not normally something you would do unless in a commercial setting.
Furthermore, both Apple and Microsoft require you to obtain certificates from them.
That is not free and requires several other steps to adhere to depending on your requirements.

I hope nobody is under the impression that this plug-in opens up code signing for Authenticode in a general sense.
It is very important and should be stressed that this is NOT the case. It is a feature that is only useful for a limited part of our community.

It is possible to sign with a self generated certificate, though, but you will run into trouble during commercial deployment.

A proper certificate should be obtained through a trusted authority that can sign with a MS rootcertificate. See: http://go.microsoft.com/fwlink/p/?linkid=203607
Title: Re: Codesigning (Authenticode) IDE package
Post by: Phil on May 12, 2017, 06:23:56 pm
Code signing is not normally something you would do unless in a commercial setting.
Furthermore, both Apple and Microsoft require you to obtain certificates from them.
That is not free and requires several other steps to adhere to depending on your requirements.

Apple's is free. Perhaps you're confusing this with Apple's developer program, which is required for App Store submission.



Title: Re: Codesigning (Authenticode) IDE package
Post by: CCRDude on May 12, 2017, 09:07:35 pm
@Phil: indeed InnoSetup has support for signing uninstaller and installer integrated. That's quite helpful, though you still need to enter the full parameter list. I'm not sure how good the integrated support is where it comes to SHA1+SHA256, would need to test whether it supports two calls (right now I use a helper exe I wrote for that purpose).

I was btw. hoping that the packages post build even would affect rebuilding the Lazarus IDE as well, but that didn't happen ;)
While I would support signed installers and executables, the "rebuild from source" aspect would get rid of the signature as soon as you add another package to the IDE.

@Thaddy: You don't need to get them from Microsoft, you can get certificates from a number of companies like Comodo, VeriSign, DigiCert, StartSSL, and a bunch more. You don't need to get them from Microsoft - you get them from the same companies that sell you certificates for websites. And as with https websites, there are advantages, but it's not free, that's true. With StartSSL, I paid once for the identification and could create as many certificates as I wanted later (so after I paid for it to get the website secure, I didn't have to pay for the code signing cert), but StartSSL got some quite bad press recently and I wouldn't recommend them any more.

In todays malware-poisoned world, it's nice to be able to show that a file has not been modified since leaving the original author. A correctly signed file is proof that it isn't infected by a virus (unless the author himself has infected the file). It makes it easier to trust software. Similar to https websites (unless you use the free LetsEncrypt), where you can verify who owns the server. I know that making users feel more comfortable in using a software isn't the main focus of noncommercial software, but users would benefit from it anyway ;)

The required steps are the same as with https certificates - you need to proof your identity. Which comes at a cost.

On Windows since Vista, the UAC dialogs look different if a file has been signed. If you're writing drivers, you need to sign them. If you're writing WSC modules, you need to sign them. Not sure about Windows 10 S, but I would assume that signing files is getting even more important there. Same on the Mac... the system gets more closed with every release. I'm not saying that this is a completely good development, but it has some good aspects.

But speaking about open systems reminds me of something. Nearly twenty years ago, before I even knew about Authenticode, I supplied pgp signatures with the files of my application. That could be something worth adding as well, and would be something useful on Linux.


Anyway, my apologies if anyone was mislead to believe this would make free signing possible, as you seem to imply. That's indeed not the case. See my second sentence: "This is only useful for those developer who invest into getting a codesigning certificate".
Title: Re: Codesigning (Authenticode) IDE package
Post by: Phil on May 12, 2017, 09:16:59 pm
Same on the Mac... the system gets more closed with every release.

Not sure if I can think of any ways in which OS X has become more closed over the last few releases. If anything, it's even easier to develop for since you don't need to be in the paid developer's program anymore to download Xcode or to sign your executables with your Apple ID.

Otherwise, well stated.

I would argue that signing is important for hobbyist-created executables too.
Title: Re: Codesigning (Authenticode) IDE package
Post by: Thaddy on May 12, 2017, 09:24:09 pm
Iwas merely saying that the root certificate must be microsoft's. Of course other ca's do the actual selling to individuals and companies.
Title: Re: Codesigning (Authenticode) IDE package
Post by: CCRDude on May 12, 2017, 09:35:49 pm
@Phil: What I meant is that macOS started to require you to go the Settings > Security to run files you've downloaded from the Internet. In the last macOS release, you could skip this, but in 10.12.4 I think you need this step for the first run of every unsigned downloaded executable, while the settings allow to skip this for App Store apps and signed executables (the third option has been removed now). But you're right, since it got even easier to sign on the Mac, "closed" is probably the wrong term :)

@Thaddy: that's not true either :) It's the same as with browsers, who bring their range of trusted root authorities. My Windows installation currently shows 46 trusted root certification authorities, and only five of them are Microsoft. So the limitation would only be that Windows needs to ship the root certificate of ones specific code signing certificate.

You wouldn't claim that all https websites need to have a certificate based on a Mozilla root certificate to work in Firefox either, it's really very similar :D
Title: Re: Codesigning (Authenticode) IDE package
Post by: Phil on May 12, 2017, 09:50:48 pm
@Phil: What I meant is that macOS started to require you to go the Settings > Security to run files you've downloaded from the Internet. In the last macOS release, you could skip this, but in 10.12.4 I think you need this step for the first run of every unsigned downloaded executable, while the settings allow to skip this for App Store apps and signed executables (the third option has been removed now).

I believe the control-click, choose Open option still works with unsigned apps, per Apple's documentation:

https://support.apple.com/kb/PH25753?viewlocale=en_US&locale=en_US

The "Anywhere" choice in the Security and Privacy preferences always made me a little nervous anyway and I never used it.
Title: Re: Codesigning (Authenticode) IDE package
Post by: valdir.marcos on May 13, 2017, 03:22:10 am
I use Linux on a few servers, but have no idea at all whether there's any similar codesigning available there. I know that Debian packages are signed, for example, but I know nothing about single executables. Can you hint me at how it's done on Linux?

"GPG can be used to create a digital signature for both Debian package files and for APT repository metadata.

Many Debian-based Linux distributions (e.g., Ubuntu) have GPG signature verification of Debian package files (.deb) disabled by default and instead choose to verify GPG signatures of repository metadata and source packages (.dsc). The setting which enables GPG signature checking of the individal .deb packages can be found in /etc/dpkg/dpkg.cfg and is set to no-debsig, but there are important caveats to enabling this option explained below.

Further, most official Debian package files from the publicly accessible repositories do not have GPG signatures. The official repository metadata is GPG signed, as are the source packages, but the .deb packages themselves are not.

If you publish a Debian package and GPG sign the package yourself before distributing it to users, those users’ systems will, in most cases, not verify the signature of the package unless they have done a considerable amount of configuration. However, their system will, in most cases, automatically verify repository metadata."

Reference: https://blog.packagecloud.io/eng/2014/10/28/howto-gpg-sign-verify-deb-packages-apt-repositories/



Maybe, these information can help you to start out:
HOWTO: GPG sign and verify deb packages and APT repositories
https://blog.packagecloud.io/eng/2014/10/28/howto-gpg-sign-verify-deb-packages-apt-repositories/

How to sign files with Ubuntu command-line tools and my own keys?
https://askubuntu.com/questions/679230/how-to-sign-files-with-ubuntu-command-line-tools-and-my-own-keys

GnuPG in Debian
https://wiki.debian.org/GnuPG

Creating a new GPG key
https://keyring.debian.org/creating-key.html

Debian Public Key Server
https://keyring.debian.org/

SecureApt - All about secure apt
https://wiki.debian.org/SecureApt

Subkeys - Using OpenPGP subkeys in Debian development
https://wiki.debian.org/Subkeys

Keysigning
https://wiki.debian.org/Keysigning

Package: signing-party
https://packages.debian.org/sid/signing-party
Title: Re: Codesigning (Authenticode) IDE package
Post by: jwdietrich on May 13, 2017, 09:55:08 am
What about other OS: Linux, MacOs?

There is an article (http://wiki.lazarus.freepascal.org/Code_Signing_for_Mac_OS_X) in the Lazarus wiki describing code signing on the Mac. Perhaps it is helpful for adapting the plug-in for macOS.
Title: Re: Codesigning (Authenticode) IDE package
Post by: CCRDude on May 15, 2017, 08:24:14 pm
Thanks for all the replies :)

I updated the code to support certificate property substrings as an identifier, since that's the easiest way for codesign on macOS (-s <email of Apple dev cert>), made a bunch of fields not yet supported invisible on macOS, and tested with codesign.

It works now on macOS to sign the main binary; I could probably add Info.plist signing as well.

Plus, I need to find out how to get the output of TProcess on macOS. The pipe routines that work on Windows seem to fail still...

I'll push it to the repository once I tested it some more!
Title: Re: Codesigning (Authenticode) IDE package
Post by: jwdietrich on May 15, 2017, 08:38:04 pm
It works now on macOS to sign the main binary; I could probably add Info.plist signing as well.

That is great. AFAIK, it is necessary, however, to sign the whole app on macOS, rather than the binary only.
Title: Re: Codesigning (Authenticode) IDE package
Post by: Phil on May 15, 2017, 09:11:33 pm
It works now on macOS to sign the main binary; I could probably add Info.plist signing as well.

That is great. AFAIK, it is necessary, however, to sign the whole app on macOS, rather than the binary only.

I assume you're referring to signing the disk image, eg,

signtool -s "Mac Developer: Dev Name (DevCertNum)" -v someapp.dmg

https://developer.apple.com/library/content/technotes/tn2206/_index.html#//apple_ref/doc/uid/DTS40007919-CH1-TNTAG18
Title: Re: Codesigning (Authenticode) IDE package
Post by: Phil on May 15, 2017, 09:13:23 pm
I updated the code to support certificate property substrings as an identifier, since that's the easiest way for codesign on macOS (-s <email of Apple dev cert>), made a bunch of fields not yet supported invisible on macOS, and tested with codesign.

Plus, I need to find out how to get the output of TProcess on macOS. The pipe routines that work on Windows seem to fail still...

Not sure what <email of Apple dev cert> refers to.

I've used TProcess on Mac for years without problem. Can you give an example of TProcess code that doesn't work?

Title: Re: Codesigning (Authenticode) IDE package
Post by: jwdietrich on May 15, 2017, 10:13:40 pm
It works now on macOS to sign the main binary; I could probably add Info.plist signing as well.

That is great. AFAIK, it is necessary, however, to sign the whole app on macOS, rather than the binary only.

I assume you're referring to signing the disk image, eg,

signtool -s "Mac Developer: Dev Name (DevCertNum)" -v someapp.dmg

https://developer.apple.com/library/content/technotes/tn2206/_index.html#//apple_ref/doc/uid/DTS40007919-CH1-TNTAG18

No, I don't refer to the disk image. Apps for the Mac are special directory structures called packages. Every app contains a folder called Contents and this again contains a directory named MacOS (along with other folders for resources, frame works, plug-ins etc.). The binary is in the folder Contents/MacOS/, and this is the file that has been generated by Lazarus or Free Pascal. To make the things more complex Lazarus creates the binary outside the app, and the folder MacOS contains an alias to the binary only (which has to be replaced by the original binary before deploying the app).

In the final step before uploading / selling etc. a Mac app it should be signed. This refers to the whole app and not to the binary. In addition, you might want to sign a DMG image, but this is another story.
Title: Re: Codesigning (Authenticode) IDE package
Post by: Phil on May 15, 2017, 10:23:29 pm
In the final step before uploading / selling etc. a Mac app it should be signed. This refers to the whole app and not to the binary. In addition, you might want to sign a DMG image, but this is another story.

Yes, that's what codesign does for an .app bundle. See contents of CodeResources file in bundle's _CodeSignature folder.

The fact that Lazarus creates the executable outside the bundle is a longtime Laz defect.
Title: Re: Codesigning (Authenticode) IDE package
Post by: jwdietrich on May 15, 2017, 10:25:56 pm
The fact that Lazarus creates the executable outside the bundle is a longtime Laz defect.

Well, this defect is easy to circumnavigate, but of course it would be better, if Lazarus would automatically create self-contained applications.
Title: Re: Codesigning (Authenticode) IDE package
Post by: Phil on May 15, 2017, 10:27:11 pm
The fact that Lazarus creates the executable outside the bundle is a longtime Laz defect.

Well, this defect is easy to circumnavigate, but of course it would be better, if Lazarus would automatically create self-contained applications.

You mean like all other Mac IDEs, right?

Title: Re: Codesigning (Authenticode) IDE package
Post by: CCRDude on May 16, 2017, 09:23:37 am
Not sure what <email of Apple dev cert> refers to.

The certificates issued by Apple seem to have the email address as part of their subject, so this was the easiest way I found to identify them (e.g. „Mac Developer: username@domain.com (CAFFEE00)“).

On Windows, I enumerate through the MY store to show a list of certificates to pick from. For the Mac I haven’t found an equivalent yet.

I've used TProcess on Mac for years without problem. Can you give an example of TProcess code that doesn't work?

I think I’ve found my problem:  PepiMK.Authenticode.Sign.External.pas (https://gitlab.com/ccrdude/lazcodesigninghelper/blob/9afef49d0e9dc0cb5c60424ab68cbbed6fe647d2/source/PepiMK.Authenticode.Sign.External.pas), line 369ff. This probably returned too fast before the pipes could get emptied (on Windows, it always worked, it looks like the process there won't close until the pipes are empties). I went ahead and set the poWaitOnExit flag, since the component for now has no live events of console output anyway. Now it shows the output as expected.

To make the things more complex Lazarus creates the binary outside the app, and the folder MacOS contains an alias to the binary only (which has to be replaced by the original binary before deploying the app).

Yes, that makes signing the whole .app more difficult, thanks for pointing that out! My new workaround is for the codesigning to remove the symlink and move the executable into the bundle, then sign the bundle.

The code I just pushed contains these fixes :)
Title: Re: Codesigning (Authenticode) IDE package
Post by: Graeme on May 16, 2017, 12:44:28 pm
In todays malware-poisoned world, it's nice to be able to show that a file has not been modified since leaving the original author. A correctly signed file is proof that it isn't infected by a virus (unless the author himself has infected the file). It makes it easier to trust software.
...snip..
But speaking about open systems reminds me of something. Nearly twenty years ago, before I even knew about Authenticode, I supplied pgp signatures with the files of my application. That could be something worth adding as well, and would be something useful on Linux.
FreeBSD and Linux distros use SHA256 to verify there ISO images for download. I wrote a script that downloads and verifies them for me. I also have another script that burns a ISO image to CD/DVD (when I still used those) and then verified it against the original SHA256 when I downloaded the ISO. That confirms nothing went wrong with the burning of the disk.

FreeBSD also uses SHA256 in its ports (https://en.wikipedia.org/wiki/FreeBSD_Ports) system to verify all user installed software against the originals.

But I don't know of any other "code signing" usage in Unix-type systems. eg: I've never come across a code signed executable, or the OS preventing me from running an executable because it isn't code signed.

Java uses code signing a lot too. Especially when it comes to Java Applets. Modern web browsers and Java plugins prevent Java Applets from running if they are not code-signed. You have to manually override permissions if Java Applets are not code signed.

Just my 2c worth.
Title: Re: Codesigning (Authenticode) IDE package
Post by: jc99 on May 16, 2017, 01:15:50 pm
...
FreeBSD also uses SHA265 in its ports (https://en.wikipedia.org/wiki/FreeBSD_Ports) system to verify all user installed software against the originals.
...
Cool, that's 9-bits more secure than the standard SHA256 ... ;)
Where do I get it ?
Title: Re: Codesigning (Authenticode) IDE package
Post by: Phil on May 16, 2017, 04:08:17 pm
The certificates issued by Apple seem to have the email address as part of their subject, so this was the easiest way I found to identify them (e.g. „Mac Developer: username@domain.com (CAFFEE00)“).

No, perhaps they look like that if someone used their e-mail address as their "name".

On Windows, I enumerate through the MY store to show a list of certificates to pick from. For the Mac I haven’t found an equivalent yet.

You probably want the Security framework:

https://developer.apple.com/reference/security

Yes, that makes signing the whole .app more difficult, thanks for pointing that out! My new workaround is for the codesigning to remove the symlink and move the executable into the bundle, then sign the bundle.

Correct. That Lazarus defect was a big mistake when it was first done that way and is still a big mistake.
Title: Re: Codesigning (Authenticode) IDE package
Post by: Graeme on May 17, 2017, 04:22:16 pm
Cool, that's 9-bits more secure than the standard SHA256 ... ;)
%) Well spotted. Sorry for the typo.
Title: Re: Codesigning (Authenticode) IDE package
Post by: CCRDude on May 19, 2017, 09:30:08 am
Just pushed a version with lots of changes.

* GnuPG is now supported (tested on Windows and macOS) for signing and verifying (should cover Linux, but I'll have to test that).
* Certificate picker lists available GnuPG certificates.
* Verification on Windows and Mac implemented.
* Signing the Lazarus IDE on rebuilding it is now supported.
* Project-specific codesigning settings allow to override global settings.
* For Microsofts signtool, the URL is now picked from a formatted comment in the main source file.

Thanks Phil for pointing out that framework! I just saw that it already has included header translations in SecTrust.pas. That'll most likely be my next update :)

Graeme: hashes are way different from cryptographic signatures. For downloads, it can indeed help if download and hash come from different servers (otherwise on a compromised server, the hash could be replaced as well as the download). But on just one computer, any malware could simply update a hash as well. Still, thanks for the insight into FreeBSD/ports :)
Title: Re: Codesigning (Authenticode) IDE package
Post by: CCRDude on May 22, 2017, 02:16:30 pm
SecTrust does not work for listing certificates, it doesn't implement the correct new functions. Will work on that.

Since Java was mentioned, I started looking into the use of keytool and jarsigner. The FreePascal Java cross-compiler has some issues though. It creates the output in the specified library instead of output path, and creates class files, while only jar files can be signed. I'm going to see if I can include the steps to wrap the classes into a jar that could be signed.

Microsofts SignTool, Apples CodeSign and GnuPG now have separate pages, and each a custom project page. Due to Windows adding more subsystems and signtool available through Wine on the Mac, I didn't want to be in the way of cross-compiling, so the selection of the proper signing tool is now based on the project build modes target os instead of compile time.

(that code not pushed to gitlab yet)
Title: Re: Codesigning (Authenticode) IDE package
Post by: Mick on August 04, 2017, 06:57:45 pm
Hi CCRDude,

At what step your codesigning "task" is setup in the build process?
Is it before or after the task defined in the "Project Options / Compiler Options / Compiler Commands / Execute After"?
Title: Re: Codesigning (Authenticode) IDE package
Post by: CCRDude on September 05, 2017, 09:16:32 am
My apologies; no Internet during vacation and a huge backlog of mails afterwards ;)

See CodeSigningHelper.Menu.pas (http://"https://gitlab.com/ccrdude/lazcodesigninghelper/blob/master/source/CodeSigningHelper.Menu.pas"), lines 446ff: it uses AddHandlerOnProjectBuildingFinished, with second parameter (AsLast) set to True.

In theory, other packages could do the same and would be added even after this.

As for the order within Lazarus, check for lihtProjectBuildingFinished in lazarus/ide/main.pp, around line 6862. This seems to be before ExecuteAfter I'm afraid.

This isn't very good when it comes to codesigning, since codesigning needs to be the last step.

If you've got other actions that modify a binary (manual updates to manifest, PE header, ugly stuff like compressors), a new package without the AsLast parameter set would probably be the best choice.
Title: Re: Codesigning (Authenticode) IDE package
Post by: Mick on September 05, 2017, 10:53:18 am
Yes, my question was exactly about that. I need to use that "ugly stuff" before signing. So, in this case I'm afraid I will need to keep my current solution with calling batch file that takes care of the order and has signing as the last step. I'm afraid I don't have enough level of expertise in this area to implement such package on my own.
Anyway, thanks for the response!
Title: Re: Codesigning (Authenticode) IDE package
Post by: CCRDude on October 31, 2018, 10:05:41 am
I updated the IDE part of this a bit.

I did design the UI part to be most flexible to be able to set up at least two Authenticode certificates, which was a requirement of mine. Switching between them was ugly though, so I threw the whole UI part over board and went another route.

Now, in the global Options various basic profiles can be set up, and the Project Options offer to add any amount of profiles combined with project specific settings. This makes it more easy to have e.g. a simple local certificate and an EV certificate (background for me: EV certificates won't work through RDP).

The whole thing is still partly unusable due to the IDE bug that returns the debugging executable instead of the output binary when building DLLs:
https://bugs.freepascal.org/view.php?id=27729
TinyPortal © 2005-2018