Repository navigation
Hash each certificate's SPKI as encoded instead of rebuilding it #372
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
alexanderdibenedetto
merged 5 commits into
v4-0-0
from
adb/issue-236-ready-spki-straight-from-certificate-der-encoding-and-hash-as-is
Oct 23, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
58ba842
Hash each certificate's SPKI as encoded instead of rebuilding it (#236)
8e90712
Stop building arm64e and arm64e.x1 simulator slices
207b589
Read the certificate's SPKI in Swift (#236)
7227e22
Replace the Python helper scripts
113795f
Hash the SPKI as encoded in the pin script; version the SPKI cache
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /* | ||
|
|
||
| SubjectPublicKeyInfo.swift | ||
| TrustKit | ||
|
|
||
| Copyright 2026 The TrustKit Project Authors | ||
| Licensed under the MIT license, see associated LICENSE file for terms. | ||
| See AUTHORS file for the list of project authors. | ||
|
|
||
| */ | ||
|
|
||
| import Foundation | ||
|
|
||
| @objc(TSKSubjectPublicKeyInfo) | ||
| public final class SubjectPublicKeyInfo: NSObject { | ||
| /// Returns the DER-encoded Subject Public Key Info of a DER-encoded certificate, exactly as it | ||
| /// is encoded in the certificate, or nil if the certificate could not be read. | ||
| @objc public static func data(fromCertificate certificate: Data) -> Data? { | ||
| // An Array keeps every read below bounds-checked, including in release builds | ||
| let der = [UInt8](certificate) | ||
| guard let subjectPublicKeyInfo = subjectPublicKeyInfoRange(in: der) else { | ||
| return nil | ||
| } | ||
| return Data(der[subjectPublicKeyInfo]) | ||
| } | ||
| } | ||
|
|
||
| private struct DERElement { | ||
| let tag: UInt8 | ||
| let content: Range<Int> | ||
| let element: Range<Int> | ||
| } | ||
|
|
||
| private func subjectPublicKeyInfoRange(in der: [UInt8]) -> Range<Int>? { | ||
| guard let certificate = readElement(der, in: der.indices), certificate.tag == 0x30, | ||
| let tbsCertificate = readElement(der, in: certificate.content), tbsCertificate.tag == 0x30 else { | ||
| return nil | ||
| } | ||
|
|
||
| // RFC 5280 4.1: an optional [0] version, then serialNumber, signature, issuer, validity and subject | ||
| var remaining = tbsCertificate.content | ||
| if let version = readElement(der, in: remaining), version.tag == 0xa0 { | ||
| remaining = version.element.upperBound..<remaining.upperBound | ||
| } | ||
| for expectedTag: UInt8 in [0x02, 0x30, 0x30, 0x30, 0x30] { | ||
| guard let field = readElement(der, in: remaining), field.tag == expectedTag else { | ||
| return nil | ||
| } | ||
| remaining = field.element.upperBound..<remaining.upperBound | ||
| } | ||
|
|
||
| guard let subjectPublicKeyInfo = readElement(der, in: remaining), subjectPublicKeyInfo.tag == 0x30 else { | ||
| return nil | ||
| } | ||
| return subjectPublicKeyInfo.element | ||
| } | ||
|
|
||
| private func readElement(_ der: [UInt8], in range: Range<Int>) -> DERElement? { | ||
| guard range.count >= 2 else { | ||
| return nil | ||
| } | ||
| let tag = der[range.lowerBound] | ||
| var contentStart = range.lowerBound + 2 | ||
| var length = Int(der[range.lowerBound + 1]) | ||
| if length & 0x80 != 0 { | ||
| // DER has no indefinite length (0x80), and no certificate needs more than 3 length bytes | ||
| let lengthBytes = length & 0x7f | ||
| guard (1...3).contains(lengthBytes), range.upperBound - contentStart >= lengthBytes else { | ||
| return nil | ||
| } | ||
| length = der[contentStart..<contentStart + lengthBytes].reduce(0) { $0 << 8 | Int($1) } | ||
| contentStart += lengthBytes | ||
| } | ||
| guard range.upperBound - contentStart >= length else { | ||
| return nil | ||
| } | ||
| return DERElement(tag: tag, | ||
| content: contentStart..<contentStart + length, | ||
| element: range.lowerBound..<contentStart + length) | ||
| } |
Binary file not shown.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.