Ios 在运行时获取配置文件和证书的详细信息
我想在我的应用程序中获取并显示我的配置文件和分发证书的详细信息,如到期日期和注册公司。我已经尝试过了,但在我的应用程序中无法正常工作。它最初为profilePath本身提供nilIos 在运行时获取配置文件和证书的详细信息,ios,provisioning-profile,Ios,Provisioning Profile,我想在我的应用程序中获取并显示我的配置文件和分发证书的详细信息,如到期日期和注册公司。我已经尝试过了,但在我的应用程序中无法正常工作。它最初为profilePath本身提供nil 我使用的是swift 2.3和Xcode 8.2.1。我曾尝试将这些代码混合并匹配到我的应用程序中,因为我无法将其转换为swift,所以完全无法使用sscanf方法。感谢您的帮助 我没有访问Xcode 8/Swift 3.2的权限,但下面是在Swift 4中执行所需操作所需的代码。我已经在我可以使用的几个配置文件/证书
我使用的是swift 2.3和Xcode 8.2.1。我曾尝试将这些代码混合并匹配到我的应用程序中,因为我无法将其转换为swift,所以完全无法使用sscanf方法。感谢您的帮助 我没有访问Xcode 8/Swift 3.2的权限,但下面是在Swift 4中执行所需操作所需的代码。我已经在我可以使用的几个配置文件/证书上测试了它,它得到了您要求的信息 配置配置文件 我们需要进行一些操作,因为如果不将embedded.mobileprovision文件从十六进制转换为十六进制文件,然后仅取出plist标记之间的内容,则该文件是不可读的
func extractPlist( fromMobileProvisionDataString:String ) -> String
{
// Remove brackets at beginning and end
var range = Range(NSMakeRange(0, 1), in: fromMobileProvisionDataString)
var plistDataString = fromMobileProvisionDataString.replacingCharacters(in:range!, with: "")
range = Range(NSMakeRange(plistDataString.count-1, 1), in: plistDataString)
plistDataString.replaceSubrange(range!, with: "")
// Remove spaces
plistDataString = plistDataString.replacingOccurrences(of: " ", with: "")
// convert hex to ascii
let profileText = hexStringtoAscii( plistDataString )
// I tried using regular expressions and normal NSString operations to get this, but it simply wouldn't work, so I went with this ugly method.
// return extractPlistText(fromProfileString:profileText)
// Remove whitespaces and new lines characters and splits into individual lines.
let profileWords = profileText.components(separatedBy: CharacterSet.newlines)
var plistString = "";
var inPlist = false;
for word in profileWords
{
if( word.contains("<plist") ) { inPlist = true }
if( inPlist ) { plistString.append(" "); plistString.append( word ) }
if (word.contains("</plist")) { inPlist = false }
}
return plistString;
}
func hexStringtoAscii(_ hexString : String) -> String {
let pattern = "(0x)?([0-9a-f]{2})"
let regex = try! NSRegularExpression(pattern: pattern, options: .caseInsensitive)
let nsString = hexString as NSString
let matches = regex.matches(in: hexString, options: [], range: NSMakeRange(0, nsString.length))
let characters = matches.map {
Character(UnicodeScalar(UInt32(nsString.substring(with: $0.range(at: 2)), radix: 16)!)!)
}
return String(characters)
}
我已经验证了这一点,可以从物理设备上的embedded.mobileprovision文件中提取过期日期。从profile plist数据中提取其他元素是很简单的
证书:
要获取证书信息,我可以使用以下方法使其正常工作:
func getCertificateExpirationDate() -> Date?
{
let profilePath: String? = Bundle.main.path(forResource: "embedded", ofType: "mobileprovision")
if( profilePath != nil )
{
let plistData = NSData(contentsOfFile: profilePath!)
let plistDataString = String(format: "%@", plistData!)
var plistString: String = extractPlist(fromMobileProvisionDataString:plistDataString)
// Trying to extract thecert information aswell, but haven't gotten it to work.
let certPattern = "<key>DeveloperCertificates</key>\\s*<array>\\s*<data>([^<]*)</data>"
let certRegex = try! NSRegularExpression(pattern: certPattern, options: .caseInsensitive)
let certCheckingResult : NSTextCheckingResult = certRegex.firstMatch(in: plistString, options: NSRegularExpression.MatchingOptions(rawValue: UInt(0)), range: NSMakeRange(0, plistString.characters.count))!
let certMatchRange : NSRange = certCheckingResult.range(at: 1)
let certDataString : String = (plistString as NSString).substring(with: certMatchRange)
let decodedData = Data(base64Encoded: certDataString, options: [])
let decodedString = String( data: decodedData!, encoding: .ascii )
let cfData = decodedData as! CFData
let certificate: SecCertificate = SecCertificateCreateWithData(nil, cfData)!
var description: CFString = SecCertificateCopySubjectSummary(certificate)!
print( "Certificate name: \(description)")
let certDate = self.extractCertExpirationDate(fromDecodedCertDataString: decodedString!)
print( "Certificate expires: \(certDate)")
let certOrg = self.extractCertOrg(fromDecodedCertDataString: decodedString!)
print( "Certificate organization: \(certOrg)")
return certDate
}
return nil
}
func extractCertExpirationDate( fromDecodedCertDataString: String ) -> Date
{
// Remove new lines characters and split into individual lines.
let certWords = fromDecodedCertDataString.components(separatedBy: CharacterSet.newlines)
var foundWWDRCA = false;
var certStartDate = ""
var certEndDate = ""
var certOrg = ""
for word in certWords
{
if( foundWWDRCA && (certStartDate.isEmpty || certEndDate.isEmpty))
{
var certData = word.prefix(13)
if( certStartDate.isEmpty && !certData.isEmpty )
{
certStartDate = String( certData );
}
else if( certEndDate.isEmpty && !certData.isEmpty )
{
certEndDate = String( certData );
}
}
if( word.contains("Apple Worldwide Developer Relations Certification Authority") ) { foundWWDRCA = true }
}
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale.current
dateFormatter.dateFormat = "yyMMddHHmmssZ"
return dateFormatter.date(from: certEndDate)!
}
func extractCertOrg( fromDecodedCertDataString: String ) -> String
{
// Remove new lines characters and split into individual lines.
let certWords = fromDecodedCertDataString.components(separatedBy: CharacterSet.newlines)
var foundWWDRCA = false;
var certStartDate = ""
var certEndDate = ""
var certOrg = ""
for word in certWords
{
if( foundWWDRCA && (certStartDate.isEmpty || certEndDate.isEmpty))
{
var certData = word.prefix(13)
if( certStartDate.isEmpty && !certData.isEmpty )
{
certStartDate = String( certData );
}
else if( certEndDate.isEmpty && !certData.isEmpty )
{
certEndDate = String( certData );
}
}
else if( foundWWDRCA && word.contains("\u{17}") && certOrg.isEmpty)
{
var orgString = word.suffix(word.count-1)
certOrg = String( orgString.prefix(orgString.count - 1))
}
if( word.contains("Apple Worldwide Developer Relations Certification Authority") ) { foundWWDRCA = true }
}
return certOrg
}
请注意,这仅检查安装时与应用捆绑在一起的配置文件/证书。它不会检查设备上的其他可能有效的配置文件。因此,即使嵌入式配置文件已过期,应用程序仍有可能运行,如果有其他机制用于在设备上安装用于设备管理的配置文件,安装另一个具有较新通配符配置文件的应用程序,等等。但是,如果用于签署应用程序的证书已过期,则即使设备上存在更新的配置文件,它也不会运行
对于证书信息,我仍然认为最安全的方法是使用openssl库对DER编码的x509证书进行解密,但我在base64解码证书数据后能够进行的解析似乎可以提取您需要的信息 当profilepath为零时,您是否在物理设备上运行?如果您在模拟器中运行,则没有embedded.mobileprovision文件,因此您将在模拟器中得到零。@wottle是。我在模拟器上运行。我会试试的。但我还需要其他细节,我需要帮助。谢谢还有什么其他细节?获取嵌入式配置文件的到期日期应该是可能的,但在某些情况下,可能会让您错误地知道应用程序何时不再启动,例如,应用程序附带的嵌入式配置文件明天到期,但设备上安装的更新配置文件在6个月内不会过期。换句话说,在确定应用程序是否应该运行时,iOS可能不会只关注嵌入式资源调配配置文件。获取证书的详细信息可能更为棘手-您可能需要使用openssl库来解码证书。@wottle我需要的其他详细信息是所用证书的到期日期和注册公司。请查看我打印到控制台的证书说明。它可能会给你注册公司所需要的东西。要获得证书过期,您应该能够使用openssl库来获得该证书。我已经为您提供了获取证书的代码,您只需执行以下操作:但我认为从嵌入式资源调配配置文件中提取证书是困难的部分。我有一些代码,我认为可以获取证书的详细信息,但目前在Objective-C中。这也是一个相当脆弱的实现,所以我不能保证它在将来会工作,如果苹果改变他们如何编码证书信息。我会将其转换为swift,并将其发布在更新中。
func getCertificateExpirationDate() -> Date?
{
let profilePath: String? = Bundle.main.path(forResource: "embedded", ofType: "mobileprovision")
if( profilePath != nil )
{
let plistData = NSData(contentsOfFile: profilePath!)
let plistDataString = String(format: "%@", plistData!)
var plistString: String = extractPlist(fromMobileProvisionDataString:plistDataString)
// Trying to extract thecert information aswell, but haven't gotten it to work.
let certPattern = "<key>DeveloperCertificates</key>\\s*<array>\\s*<data>([^<]*)</data>"
let certRegex = try! NSRegularExpression(pattern: certPattern, options: .caseInsensitive)
let certCheckingResult : NSTextCheckingResult = certRegex.firstMatch(in: plistString, options: NSRegularExpression.MatchingOptions(rawValue: UInt(0)), range: NSMakeRange(0, plistString.characters.count))!
let certMatchRange : NSRange = certCheckingResult.range(at: 1)
let certDataString : String = (plistString as NSString).substring(with: certMatchRange)
let decodedData = Data(base64Encoded: certDataString, options: [])
let decodedString = String( data: decodedData!, encoding: .ascii )
let cfData = decodedData as! CFData
let certificate: SecCertificate = SecCertificateCreateWithData(nil, cfData)!
var description: CFString = SecCertificateCopySubjectSummary(certificate)!
print( "Certificate name: \(description)")
let certDate = self.extractCertExpirationDate(fromDecodedCertDataString: decodedString!)
print( "Certificate expires: \(certDate)")
let certOrg = self.extractCertOrg(fromDecodedCertDataString: decodedString!)
print( "Certificate organization: \(certOrg)")
return certDate
}
return nil
}
func extractCertExpirationDate( fromDecodedCertDataString: String ) -> Date
{
// Remove new lines characters and split into individual lines.
let certWords = fromDecodedCertDataString.components(separatedBy: CharacterSet.newlines)
var foundWWDRCA = false;
var certStartDate = ""
var certEndDate = ""
var certOrg = ""
for word in certWords
{
if( foundWWDRCA && (certStartDate.isEmpty || certEndDate.isEmpty))
{
var certData = word.prefix(13)
if( certStartDate.isEmpty && !certData.isEmpty )
{
certStartDate = String( certData );
}
else if( certEndDate.isEmpty && !certData.isEmpty )
{
certEndDate = String( certData );
}
}
if( word.contains("Apple Worldwide Developer Relations Certification Authority") ) { foundWWDRCA = true }
}
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale.current
dateFormatter.dateFormat = "yyMMddHHmmssZ"
return dateFormatter.date(from: certEndDate)!
}
func extractCertOrg( fromDecodedCertDataString: String ) -> String
{
// Remove new lines characters and split into individual lines.
let certWords = fromDecodedCertDataString.components(separatedBy: CharacterSet.newlines)
var foundWWDRCA = false;
var certStartDate = ""
var certEndDate = ""
var certOrg = ""
for word in certWords
{
if( foundWWDRCA && (certStartDate.isEmpty || certEndDate.isEmpty))
{
var certData = word.prefix(13)
if( certStartDate.isEmpty && !certData.isEmpty )
{
certStartDate = String( certData );
}
else if( certEndDate.isEmpty && !certData.isEmpty )
{
certEndDate = String( certData );
}
}
else if( foundWWDRCA && word.contains("\u{17}") && certOrg.isEmpty)
{
var orgString = word.suffix(word.count-1)
certOrg = String( orgString.prefix(orgString.count - 1))
}
if( word.contains("Apple Worldwide Developer Relations Certification Authority") ) { foundWWDRCA = true }
}
return certOrg
}