in mid 2024, my friend c22 proposed me to join QuickSign, a project by haxi0: a local ipa signing app for ios. the idea eventually grew into a browser version, since a browser would make the signing part usable without a mac. we initially used zsign, but i wanted to understand the signature format well enough to write our own signer. this post is about that part of the project.

one important distinction up front: the current adhoc repo signs a mach-o executable, not an entire ipa. an ipa is a zip containing an app bundle. signing the main executable is only one piece of signing that bundle; resources, nested code, and the provisioning profile are separate work. the title is where the project started, while the code here is the executable-signing piece.
finding the format
me and rootfs were tasked with the signing code. i wanted rust, rootfs wanted c, and we went with c. zsign was useful as a reference, but i had trouble separating the mach-o format from all the other work an ipa signer does. the go linker gave me a much smaller example of the code signature blob layout. my first attempt generated the blob as a separate file. the missing step was getting the mach-o to point at it, with the right bytes hashed in the right order.
where the signature goes
a mach-o starts with a header and a table of load commands. LC_CODE_SIGNATURE is a 16-byte command containing the file offset and size of the signature. in the repo, parse_macho reads that table, finds __LINKEDIT and any existing signature command, and checks where the first section or segment data begins. add_or_update_code_signature either rewrites the existing command or adds one in the unused space after the other load commands. if there isn't enough header padding, it stops instead of moving the rest of the file around.
that load command is also inside the part of the file we're about to hash. this is the detail that made the separate-blob prototype incomplete: the final offset and size need to be written before calculating the page hashes. the signer puts the new signature after the existing code, aligned to 16 bytes, and updates __LINKEDIT's file size to include it.
the parser currently accepts one thin, 64-bit, little-endian MH_EXECUTE. it doesn't handle a fat binary with multiple architectures or a dylib. keeping that scope small made it easier to see exactly which offsets i was writing.
building the code directory
the main blob is a CodeDirectory. it contains the signing identifier, the size of the covered part of the executable, and a table of hashes. this signer uses sha-256 and 4096-byte pages. nCodeSlots is the number of pages needed to cover everything before the signature, and the last hash covers a shorter page if the length isn't a multiple of 4096. the signature blob itself is outside that covered range, so it doesn't have to hash itself.
build_code_directory writes the fields in big-endian order and hashes each page from the output buffer, after the load command has been patched. that order matters: hashing the original input and then changing a load command would immediately invalidate the result. the code directory also has room for special slots. in certificate mode, slot 2 hashes the requirements blob and slot 5 hashes the optional entitlements blob. these hashes sit before ordinary code slot zero in the hash table.
assemble_superblob then packs the code directory and any other blobs behind a small index. the index says which slot each blob occupies and its offset from the start of the superblob. ad-hoc mode only needs the code directory. it marks the directory with the ad-hoc flags and has no certificate or CMS signature.
certificate signing
the certificate path reads a .p12 file containing a certificate and private key. identity_load_p12 parses it with openssl, and build_requirements_blob creates a designated requirement for the signing identifier and certificate. an apple-style team id goes into the code directory only when the certificate appears to be apple-issued; with a self-signed certificate, the requirement uses the certificate's sha-1 hash instead. the certificate check in this code is a heuristic based on an apple extension oid, not certificate-chain validation.
after the code directory is complete, identity_sign_blob makes a detached CMS signature over its bytes and wraps the DER output in a CSMAGIC_BLOBWRAPPER. that wrapper becomes slot 0x10000 in the superblob. the code measures a trial CMS signature first because the final signature size is written into LC_CODE_SIGNATURE, and that command must be set before the code directory hashes it. it reserves a little extra space for signature-size variation, then signs the real code directory.
the repo's make test target signs a small executable in both modes and asks macos codesign --verify --strict to check the results. that verifies the mach-o signatures produced by this code; it doesn't prove an ipa made from them will install on ios.
getting it into a browser
the browser version compiles the same c signing code with emscripten. openssl didn't just drop into that build: the makefile builds a static libcrypto for wasm with assembly and threads disabled, then links it into the signer. the wasm build exports two entry points, one for ad-hoc signing and one for .p12 signing. the web page reads the selected files into memory, copies them into the wasm heap, calls the signer, and turns the returned bytes into a download. the signing operation runs locally in the browser.


getting that first wasm build to return a signed mach-o was the fun part. it was also a much more precise milestone than “signing an ipa without a mac.” to get all the way there, the tool still needs to sign every nested binary, build the bundle's _CodeSignature/CodeResources seal, handle the provisioning profile and entitlements together, and pack the result back into an ipa. for now, the repo is a working look at the mach-o signature itself, from load command to code directory to optional certificate signature.