Skip to content
This repository was archived by the owner on Mar 7, 2026. It is now read-only.

Commit 21d8b08

Browse files
authored
Refactor getCertificateName method for better clarity
Updated the getCertificateName method to prefer TeamName from the plist data when available. Improved error handling and added comments for clarity.
1 parent 7e4daab commit 21d8b08

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

Sources/prosign/certificates/CustomCertificatesManager.swift

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,22 +87,34 @@ public final class CertificatesManager {
8787
}
8888

8989
/// Get the certificate's display name (subject summary)
90-
public static func getCertificateName(p12Data: Data, password: String) -> String? {
91-
let options = [kSecImportExportPassphrase as String: password] as CFDictionary
92-
var itemsCF: CFArray?
93-
let importStatus = SecPKCS12Import(p12Data as CFData, options, &itemsCF)
94-
guard importStatus == errSecSuccess,
95-
let items = itemsCF as? [[String: Any]],
96-
let first = items.first else {
90+
public static func getCertificateName(p12Data: Data, password: String, mobileProvisionData: Data) -> String? {
91+
let startTag = Data("<plist".utf8)
92+
let endTag = Data("</plist>".utf8)
93+
guard let startRange = mobileProvisionData.range(of: startTag),
94+
let endRange = mobileProvisionData.range(of: endTag) else {
9795
return nil
9896
}
99-
let identity = first[kSecImportItemIdentity as String] as! SecIdentity
100-
var certRef: SecCertificate?
101-
let certStatus = SecIdentityCopyCertificate(identity, &certRef)
102-
guard certStatus == errSecSuccess, let cert = certRef else {
97+
98+
let plistDataSlice = mobileProvisionData[startRange.lowerBound..<endRange.upperBound]
99+
let plistData = Data(plistDataSlice)
100+
101+
// Parse plist into a dictionary
102+
guard let parsed = try? PropertyListSerialization.propertyList(from: plistData, options: [], format: nil),
103+
let dict = parsed as? [String: Any] else {
103104
return nil
104105
}
105-
return SecCertificateCopySubjectSummary(cert) as String?
106+
107+
// Prefer TeamName if present
108+
if let teamName = dict["TeamName"] as? String, !teamName.isEmpty {
109+
return teamName
110+
}
111+
112+
// Fallback to Name (string)
113+
if let name = dict["Name"] as? String, !name.isEmpty {
114+
return name
115+
}
116+
117+
return nil
106118
}
107119

108120
/// Top-level check: returns result
@@ -157,4 +169,5 @@ public final class CertificatesManager {
157169
return .failure(error)
158170
}
159171
}
160-
}
172+
173+
}

0 commit comments

Comments
 (0)