Codesigning - codesign exited with code 3, only on emulator

For a small .Net MAUI project, when building to an emulator, the build output fails with the following error and stack trace:

Target _CodesignVerify:
  Using "CodesignVerify" task from assembly "/usr/local/share/dotnet/packs/Microsoft.iOS.Sdk/16.2.29/tools/msbuild/iOS/../iOS/Xamarin.iOS.Tasks.dll".
  Task "CodesignVerify"
    Environment Variables passed to tool:
      CODESIGN_ALLOCATE=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/codesign_allocate
    /usr/bin/codesign --verify -vvvv "-R=anchor apple generic and certificate 1[field.1.2.840.113635.100.6.2.1] exists and (certificate leaf[field.1.2.840.113635.100.6.1.2] exists or certificate leaf[field.1.2.840.113635.100.6.1.4] exists)" bin/Debug/net6.0-ios/iossimulator-x64/App.Project.app 
    bin/Debug/net6.0-ios/iossimulator-x64/App.Project.app: valid on disk
    bin/Debug/net6.0-ios/iossimulator-x64/App.Project.app: satisfies its Designated Requirement
    test-requirement: code failed to satisfy specified code requirement(s)
    /usr/local/share/dotnet/packs/Microsoft.iOS.Sdk/16.2.29/tools/msbuild/iOS/Xamarin.Shared.targets(1930,3): error MSB6006: "codesign" exited with code 3.
  Done executing task "CodesignVerify" -- FAILED.
Done building target "_CodesignVerify" in project "App.Project.csproj" -- FAILED.

Done building project "App.Project.csproj" -- FAILED.

Build FAILED.

/usr/local/share/dotnet/packs/Microsoft.iOS.Sdk/16.2.29/tools/msbuild/iOS/Xamarin.Shared.targets(1930,3): error MSB6006: "codesign" exited with code 3.
    0 Warning(s)
    1 Error(s)

I'm building through Visual Studio for Mac. The project only has platform folders for iOS and Android.

The app builds fine to a local iOS device, connected via USB. I'm building to an iPhone SE. The error does not occur on a new Maui project. Any suggestions for how I should approach this error? I'm at a loss.

I recommend that you escalate this via the support channel for the third-party tools you’re using. It seems that they are using codesign to explicitly check a code signing requirement (the --verify option combined with the -R argument), which doesn’t make a lot of sense for the iOS Simulator because it doesn’t support iOS-style code signing [1].

Notably, if you use Xcode to build an app for the simulator and dump its requirements you see this:

% codesign -d -vvv -r - Test727370.app
…
CodeDirectory v=20400 size=896 flags=0x2(adhoc) hashes=17+7 …
…
# designated => cdhash H"ec94b66a115cc147b60e13c87f99e2c9ee1f0d75" or cdhash H"a095a50c9d6a447f08b850646cb8bd05c57b9f50"

The adhoc flag is set, indicating that the code uses ad hoc signing. And the code signing requirement is based entirely on the code’s cdhash (code directory hash) value, which is typically what you see for ad hoc signed code. Such code will never pass the requirement check being run by your tools.

If you’re curious about the technical details here, have a read of my Inside Code Signing technotes, starting with TN3125 Inside Code Signing: Provisioning Profiles. TN3126 explains what a cdhash is and TN3127 explains code signing requirements.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] This is one of the many things that makes the iOS Simulator a simulator, not an emulator.

Codesigning - codesign exited with code 3, only on emulator
 
 
Q