DEV Community

My .p12 Was Encrypted Correctly. That's Why the iOS Build Failed.

The build failed with one line and nothing else: PKCS12_MAC_VERIFICATION_FAILED. No stack trace, no line number, no file.

It came out of the credential import step on the macOS build machine, which meant the failure was not in my app. It was in the 4,457-byte file I was handing the toolchain to prove who I am.

I had spent the previous three weeks treating that file as a constant. It was the one thing I was sure about. The app code changed daily; the certificate was issued by Apple and downloaded from a portal that does not make mistakes. So when the build broke, I looked everywhere except at the certificate - at the provisioning profile, at the bundle identifier, at the credentials.json schema, at the Node version, at the EAS CLI version. All of those were wrong at various points, and fixing them did not fix the build.

The certificate was the bug. Specifically, it was too modern.

What the file actually is

A .p12 is a container: one or more X.509 certificates, the private key matching the leaf certificate, and a MAC - a checksum computed over the contents using a key derived from the password.

When a tool imports it, it does two things in order. First it derives a key from your password and verifies the MAC, which tells it the file was not corrupted or tampered with. Then it decrypts the private key. The MAC step is where mine died.

And the reason is embarrassingly simple: OpenSSL 3, which is what openssl pkcs12 -export runs on any current machine, defaults to writing PKCS#12 files with modern parameters - AES-256-CBC for the encrypted content, SHA-256 for the MAC, PBKDF2 for key derivation. That is the correct choice in 2026. It is also a format the macOS Security framework refuses to verify when you import a .p12 non-interactively.

I did not figure this out by reading Apple's source. I figured it out by measuring the files that did work. Three .p12 files in my repo, all of which had built successfully at some point:

for f in *.p12; do echo "== $f ($(stat -c%s "$f") bytes)"
openssl pkcs12 -in "$f" -info -noout -passin pass:**** 2>&1 | head -4 done
== posttrack_distribution.p12 (4457 bytes)
MAC: sha1, Iteration 2048
PKCS7 Encrypted data: pbeWithSHA1And3-KeyTripleDES-CBC, Iteration 2048
== subguard_distribution.p12 (4237 bytes)
MAC: sha1, Iteration 2048
PKCS7 Encrypted data: pbeWithSHA1And3-KeyTripleDES-CBC, Iteration 2048
== posttrack_distribution_v2.p12 (3210 bytes)
MAC: sha1, Iteration 2048
PKCS7 Encrypted data: pbeWithSHA1And3-KeyTripleDES-CBC, Iteration 2048

Every working file: SHA-1 MAC, 2,048 iterations, 3DES. Every file that failed: SHA-256 MAC, AES-256. The difference between a green build and a cryptic import error was a default value in a tool I never invoked directly.

The fix is one flag:

openssl pkcs12 -export \
  -inkey distribution.key \
  -in distribution.cer \
  -certfile AppleWWDRCA.cer \
  -name "Apple Distribution: ..." \
  -out distribution.p12 \
  -legacy

-legacy restores the old cipher set. Same key, same certificate, same password, same profile. The build went green on the first attempt after that, which is the most annoying kind of fix: nothing about the application changed, so nothing about the application was ever going to reveal it.

The second failure wearing the same mask

Two weeks later the build failed again, and this time the error was different but pointed at the same file:

error: "Apple Distribution: ..." is not valid for code signing

This one is not about encryption at all. A .p12 can contain more than the leaf certificate - normally it also carries the intermediate CA that signed it, so the importer can build a chain to a trusted root. Mine carried two certificates. I checked the validity of both instead of just the one I cared about:

openssl pkcs12 -in distribution.p12 -nokeys -passin pass:**** \
  | openssl x509 -noout -subject -dates

The leaf was fine. The intermediate was Apple Worldwide Developer Relations Certification Authority, issued in 2013, expired 2023-02-07. A certificate chain with a dead link in the middle is not a chain, so codesign reported the leaf as invalid for signing - which is technically true and practically useless as a message.

The healthy files in the same repo carried the G3 intermediate: valid from 2020-02-19 to 2030-02-20. Same subject line, different generation of the certificate, completely different outcome. There is no way to tell them apart without printing the dates.

The third failure, which was mine

The certificate I generated to fix all of this was issued at Sep 30 04:06:58 2026 GMT, serial 1A96FAF0F0AF373932470C5302B4207C. Within weeks, builds using it started failing with a serial number I recognised from that very output, because Apple had revoked it.

That is the part that should have been obvious from the beginning. When I moved to local credentials, I did not just move a file. I took ownership of a lifecycle. A certificate has an issue date, an expiry date, a chain, a revocation status, and a private key that must be exported in a specific format. EAS's remote credential management does all of that for you and rotates certificates automatically. I opted out of it because the Apple ID behind the account has two-factor authentication, and a headless build pipeline cannot answer a 2FA prompt. So every credential became a manual artefact that only gets looked at when a build fails.

What I gave up

Automatic certificate rotation. Remote credentials mean Apple-side renewal happens without me. Local credentials mean the expiry date is a date I have to remember. I traded a platform responsibility for a personal one, and then got surprised when it became a personal problem.

The honest accounting: local credentials bought me unattended builds on a machine that is already authenticated; the price is that every certificate event is now an incident.

Reproducible builds. These builds run on one Mac, from a working directory with local state, not from a clean CI checkout. The upside is that I can build without uploading source to a third-party builder. The downside is that "works on my machine" is now a literal description of the release process, and a .p12 sitting in a directory is part of the build inputs.

Build time, deliberately. For a while I used prebuilt React Native binaries, which cut a local build to about 20 minutes. They download from Maven Central, and on a network where that host is unreliable the download fails silently - the build only collapses later, at the archive step, with an error that has nothing to do with the real cause. I turned prebuilt binaries off and compile RN from source: 30 to 45 minutes per build, every build, in exchange for a failure mode that happens at the beginning and names itself. I would make that trade again, but it means a certificate mistake costs 45 minutes per attempt, not 20.

The check I run now

Before any build, one command that prints the three things that actually break:

openssl pkcs12 -in "$P12" -info -noout -passin pass:"$PASS" | grep -E 'MAC:|Encrypted'
openssl pkcs12 -in "$P12" -nokeys -passin pass:"$PASS" \
  | openssl x509 -noout -subject -dates -serial

If the first line is not sha1 and the second is not pbeWithSHA1And3-KeyTripleDES-CBC, the build will fail at import, and no amount of application debugging will help. If any certificate in the output has an expiry in the past, or a notBefore older than a few years on an intermediate, the chain is broken. If the serial does not match the certificate currently listed as active in the developer portal, it has been revoked and the build will fail for a reason that looks like a code signing problem.

That is about four seconds of output, and it would have saved three weeks spread across three different apps.

Question

When a tool rejects a file that is cryptographically valid, how far do you go before you stop trusting the tool and start trusting the file? I only found this because I happened to have three older .p12 files lying around to compare against - if I had deleted them, I would probably still be regenerating certificates and guessing. Do you keep a known-good artefact around specifically to diff against when something breaks, or is that a habit that only starts to make sense after you've been burned once?

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.