【问题标题】:How can I get the a certificate's validity period using Apple's Security framework?如何使用 Apple 的安全框架获取证书的有效期?
【发布时间】:2020-10-19 01:02:45
【问题描述】:

我正在解析 macOS 应用程序代码签名中的证书,我想获取到期日期(又名“有效期”)。根据Apple's documentation,证书包含有效期,但没有提及检索它的功能。

我目前正在手动挖掘证书并使用字典中的SecCertificateCopyValues() 手动获取值,但这似乎不是正确的方法。

如何使用 Apple 的 Security 框架(不是OpenSSL)在 CoreFoundation 或 Foundation (Objective-C) 中获得 SecCertificateRef 的有效期 (NSDate)?

谢谢。

【问题讨论】:

  • 有效期由“notBefore”和“notAfter”字段之间的时间组成。检查答案here(我猜你可以猜到notBefore 方法名称)。请注意,此答案明确表示它在 Objective C 中不可用(无论如何都适用于 iOS)。
  • MacOS 也找不到,所以必须自己解析(不推荐)或者使用 OpenSSL 之类的库...
  • OpenSSL 已被弃用多年。我已经可以比这个例子@MaartenBodewes 更容易地获得价值,但我只是想知道他们的文档中提到的 Apple 是否没有官方功能。还是谢谢。

标签: macos security certificate code-signing security-framework


【解决方案1】:

对于那些对我的方法感兴趣的人,我会将我的 sn-p 留在这里。干杯!


#import <Foundation/Foundation.h>

id getX509ValueforKey(SecCertificateRef certificate, CFStringRef kSecPropertyKey) {
    id value;
    CFDictionaryRef valuesDict = SecCertificateCopyValues(certificate, (__bridge CFArrayRef)@[(__bridge id)kSecPropertyKey], NULL);
    if (valuesDict) {
        CFDictionaryRef invalidityDateDictionaryRef = CFDictionaryGetValue(valuesDict, kSecPropertyKey);
        if (invalidityDateDictionaryRef) {
            CFTypeRef invalidityRef = CFDictionaryGetValue(invalidityDateDictionaryRef, kSecPropertyKeyValue);
            if (invalidityRef)
                value = CFBridgingRelease(invalidityRef);
        }
        CFRelease(valuesDict);
    }
    return value;
}

int main(int argc, const char * argv[]) {
    
    SecCertificateRef certificateRef = NULL;
    NSDate *certExpiryDate = getX509ValueforKey(certificateRef, kSecOIDInvalidityDate);
    NSLog(@"certExpiryDate: %@", certExpiryDate);
    
    return noErr;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-23
    • 2021-06-05
    • 1970-01-01
    • 1970-01-01
    • 2012-05-19
    • 2012-03-25
    • 1970-01-01
    • 2022-10-07
    相关资源
    最近更新 更多