Skip to content

Free-team cert with a leading-zero serial is never matched results in 'certificates must be revoked' prompt on every run #305

Closed

Description

@0x53

Summary

Certificate.serialNumber() strips leading 0 hex characters, but the App Store Connect API returns serialNumber with them. When the development certificate's serial starts with 0, the stored SigningInfo never matches Apple's certificate. Every xtool dev after the first one then asks to revoke a certificate that is actually valid and known locally.

Environment

  • xtool 1.21.0 (x86_64 AppImage), Ubuntu 24.04 on WSL2
  • Swift 6.4.0, Xcode 27 SDK
  • Free (personal) team, iPhone 17 / iOS 27.0

Steps to reproduce

  1. Use a free team with no existing certificates and run xtool dev. xtool creates a certificate and the install works.
  2. If that certificate's serial starts with 0 (ours was 033742570D6F…), run xtool dev again.

Expected: the stored certificate is reused.
Actual:

[Provisioning]  33%
The following certificates must be revoked:
- Apple Development: <name> (expires 9 Oct 2027 at 1:10 PM)
Continue? (yes/no):

Evidence

  • xtool ds certificates list shows one certificate, serial number: 033742570D6F9684A23311711956A084.
  • The certificate saved in ~/.config/xtool/data/<teamID> has the same serial (openssl x509 -serial gives serial=033742570D6F9684A23311711956A084), and its private key matches the certificate's public key.
  • serialNumber() turns this into 33742570D6F9684A23311711956A084, so the first(where: { $0.attributes?.serialNumber == knownSerialNumber }) check in DeveloperServicesFetchCertificateOperation.perform() fails, and it falls through to replaceCertificates(_, requireConfirmation: true).

Cause

Sources/XKit/Model/Certificate.swift:

public func serialNumber() -> String {
    // big endian, hex-encoded
    let content = raw.serialNumber.bytes.lazy
        .map { String(format: "%02hhX", $0) }
        .joined()
        .drop { $0 == "0" }   // drops leading zero *nibbles*, e.g. "03…" -> "3…"
    return String(content)
}

My guess is that the drop is there to remove the 0x00 padding byte DER adds when the serial's high bit is set. Because it works on characters, it also removes a significant leading 0 nibble. Unless the serial is regenerated, the next run (or about 1 in 8 replacement certs) hits this again.

The same value is also passed as filter[serialNumber] in DeveloperServicesFetchProfileOperation, so profile lookup may be affected as well (not verified).

Suggested fix

Strip leading zero bytes before hex-encoding, rather than zero characters:

public func serialNumber() -> String {
    // big endian, hex-encoded, without DER sign-padding bytes
    raw.serialNumber.bytes
        .drop { $0 == 0 }
        .map { String(format: "%02hhX", $0) }
        .joined()
}

This gives 033742… in our case. For high-bit serials it removes the 00 padding byte, as the current code does. I haven't checked how Apple formats high-bit serials, so a more defensive option is to compare both sides with leading zeros removed (trimmingPrefix("0") on each) instead of changing serialNumber().

Activity

  1. added
    bug
    on Oct 9, 2026
  2. added a commit that references this issue on Oct 9, 2026
    7634ff3
  3. added a commit that references this issue on Oct 14, 2026
    cbe0579
  4. added a commit that references this issue on Oct 15, 2026
    3b68b37
  5. added a commit that references this issue on Oct 15, 2026
    316a11e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions