Skip to content

Code signing support - #130

Open
banister wants to merge 43 commits into
larsch:masterfrom
banister:code-signing-support
Open

Code signing support#130
banister wants to merge 43 commits into
larsch:masterfrom
banister:code-signing-support

Conversation

@banister

@banister banister commented Dec 14, 2017

Copy link
Copy Markdown

First I want to say that Ocra is a great tool, it solves a real problem in a very clever way 😄.

However, at work we've come across a limitation - currently an executable built by Ocra cannot be digitally signed using the Windows Authenticode code signing system

I looked into it, and think I got my head around the issue, so I thought I'd open up a PR to add code-signing support.

Ocra expects the executable to look like this:

| stub.exe content | opcodes | opcodes offset | ocra signature |

But, after Code Signing they look like this instead:

| stub.exe content | opcodes | opcodes offset | ocra signature | dig sig |

This breaks Ocra's expectations and prevents the signed executables from working.

The proposed fix is to update stub.c to use the executable headers (specifically the DataDirectory[IMAGE_DIRECTORY_ENTRY_SECURITY] header) to retrieve the size and location of the embedded Digital Signature.

Using this information we can tell the Ocra generated executable where to look for its own signature and opcodes. This allows an executable to continue to work whether it's been signed or not.

Tests:

I wrote tests for this using a custom fake code signing object (test/fake_code_signer.rb) as i thought the requirement for code signing tools was too much.

This FakeCodeSigner correctly updates the headers and appends a "digital signature" to the file, however the signature that gets appended is just jibberish. But this is enough to test the Ocra code, which is also indifferent to the actual signature content.

To perform a real code sign, do the following:

First install the code signing tools (I forget the link for this now, but googling will help you find makecert, signtool etc)

Then create a self-signed cert using:

$ makecert.exe -n "CN=CARoot" -r -pe -a sha512 -len 4096 -cy authority -sv CARoot.pvk -sr LocalMachine -ss Root CARoot.cer

Generate the pfx file using:

$ pvk2pfx.exe -pvk CARoot.pvk -spc CARoot.cer -pfx CARoot.pfx -po Test123

Then sign an Ocra binary using:

$ signtool sign /f ./CARoot.pfx /p Test123 helloworld.exe

Finally test if helloworld.exe works using the updated code.

EDIT: Also happy to rebase if you would prefer I pruned some of the commits

… previous EOF of the executable)

But seems to be around 4 bytes of null buffer between end of file and the start of sig
Still struggling with ocraSignatureLocation
unfortunately the digital sig does not appear immediately after the ocra sig.
There appears to be a random number of null bytes between them. This code skips over those null bytes until it finds the ocra sig, then returns the start of the sig
* foo -> searchPtr
* introduce SECURITY_ENTRY macro
* C style comments (/* */)
* Moved some code around
may not -> cannot
@banister
banister force-pushed the code-signing-support branch from e0375d1 to d9f208b Compare December 14, 2017 03:50
@banister
banister force-pushed the code-signing-support branch from cd6df81 to ebc6d1e Compare December 14, 2017 04:56
@banister
banister force-pushed the code-signing-support branch from ebc6d1e to 43590f5 Compare December 14, 2017 04:57
@codebykevin

Copy link
Copy Markdown

While I have a Github account, I have not figured out how to merge this pull request into my own Github fork of Ocra, so I have my own fork here:

https://www.codebykevin.com/fossil.cgi/stringscan/dir?ci=72deeb91ec0fb17e&name=ocra-master

I simply took your stub.c file and replaced the one in the current gem of Ocra, and rebuilt stub.exe. Code-signing works perfectly and I plan to deploy it in the next Windows release of my Ruby-Tk app, Stringscan: https://www.codebykevin.com/stringscan.html

Thanks for contributing this! Hopefully the Ocra developer will merge this pull request in a future release.

Largo added a commit to Largo/ocran that referenced this pull request Mar 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants