SecCertificateRef:如何获取证书信息?

2024-03-05

我有一个证书 (SecCertificateRef),我可以检查它是否有效,并且可以使用 SecCertificateCopySubjectSummary 提取“摘要”。

“总结”到底是什么?我不理解术语“包含人类可读的证书内容摘要的字符串”。在苹果文档中。我认为,他们的意思是证书中的“CN”,对吗?

有什么方法可以从 SecCertificateRef 中获取清晰的 X509 信息吗?转换为钥匙串对象有帮助吗?

我想要这样的东西,我特别关注“CN”,将其与我提交的 URL 进行比较,以避免中间人攻击。 (或者有更好的想法吗?)

这就是我想要的:

Version: 3 (0x2)
        Serial Number: 1 (0x1)
        Signature Algorithm: md5WithRSAEncryption
        Issuer: C=XY, ST=Austria, L=Graz, O=TrustMe Ltd, OU=Certificate Authority, CN=CA/[email protected] /cdn-cgi/l/email-protection
        Validity
            Not Before: Oct 29 17:39:10 2000 GMT
            Not After : Oct 29 17:39:10 2001 GMT
        Subject: C=DE, ST=Austria, L=Vienna, O=Home, OU=Web Lab, CN=anywhere.com/[email protected] /cdn-cgi/l/email-protection
        Subject Public Key Info:
            Public Key Algorithm: rsaEncryption
            RSA Public Key: (1024 bit)
                Modulus (1024 bit):
                    00:c4:40:4c:6e:14:1b:61:36:84:24:b2:61:c0:b5:
                    d7:e4:7a:a5:4b:94:ef:d9:5e:43:7f:c1:64:80:fd:
                    9f:50:41:6b:70:73:80:48:90:f3:58:bf:f0:4c:b9:
                    90:32:81:59:18:16:3f:19:f4:5f:11:68:36:85:f6:
                    1c:a9:af:fa:a9:a8:7b:44:85:79:b5:f1:20:d3:25:
                    7d:1c:de:68:15:0c:b6:bc:59:46:0a:d8:99:4e:07:
                    50:0a:5d:83:61:d4:db:c9:7d:c3:2e:eb:0a:8f:62:
                    8f:7e:00:e1:37:67:3f:36:d5:04:38:44:44:77:e9:
                    f0:b4:95:f5:f9:34:9f:f8:43
                Exponent: 65537 (0x10001)
        X509v3 extensions:
            X509v3 Subject Alternative Name:
                email:[email protected] /cdn-cgi/l/email-protection
            Netscape Comment:
                mod_ssl generated test server certificate
            Netscape Cert Type:
                SSL Server
    Signature Algorithm: md5WithRSAEncryption
        12:ed:f7:b3:5e:a0:93:3f:a0:1d:60:cb:47:19:7d:15:59:9b:
        3b:2c:a8:a3:6a:03:43:d0:85:d3:86:86:2f:e3:aa:79:39:e7:
        82:20:ed:f4:11:85:a3:41:5e:5c:8d:36:a2:71:b6:6a:08:f9:
        cc:1e:da:c4:78:05:75:8f:9b:10:f0:15:f0:9e:67:a0:4e:a1:
        4d:3f:16:4c:9b:19:56:6a:f2:af:89:54:52:4a:06:34:42:0d:
        d5:40:25:6b:b0:c0:a2:03:18:cd:d1:07:20:b6:e5:c5:1e:21:
        44:e7:c5:09:d2:d5:94:9d:6c:13:07:2f:3b:7c:4c:64:90:bf:
        ff:8e

我等不及赏金的答案,所以我自己找到了解决方案。正如其他人所说,Security.framework 没有为您提供获取此信息的方法,因此您需要要求 OpenSSL 为您解析证书数据:

#import <openssl/x509.h>

// ...

NSData *certificateData = (NSData *) SecCertificateCopyData(certificate);

const unsigned char *certificateDataBytes = (const unsigned char *)[certificateData bytes];
X509 *certificateX509 = d2i_X509(NULL, &certificateDataBytes, [certificateData length]);

NSString *issuer = CertificateGetIssuerName(certificateX509);
NSDate *expiryDate = CertificateGetExpiryDate(certificateX509);

Where CertificateGetIssuerName and CertificateGetExpiryDate如下面所述:

static NSString * CertificateGetIssuerName(X509 *certificateX509)
{
    NSString *issuer = nil;
    if (certificateX509 != NULL) {
        X509_NAME *issuerX509Name = X509_get_issuer_name(certificateX509);

        if (issuerX509Name != NULL) {
            int nid = OBJ_txt2nid("O"); // organization
            int index = X509_NAME_get_index_by_NID(issuerX509Name, nid, -1);

            X509_NAME_ENTRY *issuerNameEntry = X509_NAME_get_entry(issuerX509Name, index);

            if (issuerNameEntry) {
                ASN1_STRING *issuerNameASN1 = X509_NAME_ENTRY_get_data(issuerNameEntry);

                if (issuerNameASN1 != NULL) {
                    unsigned char *issuerName = ASN1_STRING_data(issuerNameASN1);
                    issuer = [NSString stringWithUTF8String:(char *)issuerName];
                }
            }
        }
    }

    return issuer;
}

static NSDate *CertificateGetExpiryDate(X509 *certificateX509)
{
    NSDate *expiryDate = nil;

    if (certificateX509 != NULL) {
        ASN1_TIME *certificateExpiryASN1 = X509_get_notAfter(certificateX509);
        if (certificateExpiryASN1 != NULL) {
            ASN1_GENERALIZEDTIME *certificateExpiryASN1Generalized = ASN1_TIME_to_generalizedtime(certificateExpiryASN1, NULL);
            if (certificateExpiryASN1Generalized != NULL) {
                unsigned char *certificateExpiryData = ASN1_STRING_data(certificateExpiryASN1Generalized);

                // ASN1 generalized times look like this: "20131114230046Z"
                //                                format:  YYYYMMDDHHMMSS
                //                               indices:  01234567890123
                //                                                   1111
                // There are other formats (e.g. specifying partial seconds or 
                // time zones) but this is good enough for our purposes since
                // we only use the date and not the time.
                //
                // (Source: http://www.obj-sys.com/asn1tutorial/node14.html)

                NSString *expiryTimeStr = [NSString stringWithUTF8String:(char *)certificateExpiryData];
                NSDateComponents *expiryDateComponents = [[NSDateComponents alloc] init];

                expiryDateComponents.year   = [[expiryTimeStr substringWithRange:NSMakeRange(0, 4)] intValue];
                expiryDateComponents.month  = [[expiryTimeStr substringWithRange:NSMakeRange(4, 2)] intValue];
                expiryDateComponents.day    = [[expiryTimeStr substringWithRange:NSMakeRange(6, 2)] intValue];
                expiryDateComponents.hour   = [[expiryTimeStr substringWithRange:NSMakeRange(8, 2)] intValue];
                expiryDateComponents.minute = [[expiryTimeStr substringWithRange:NSMakeRange(10, 2)] intValue];
                expiryDateComponents.second = [[expiryTimeStr substringWithRange:NSMakeRange(12, 2)] intValue];

                NSCalendar *calendar = [NSCalendar currentCalendar];
                expiryDate = [calendar dateFromComponents:expiryDateComponents];

                [expiryDateComponents release];
            }
        }
    }

    return expiryDate;
}

出于我的目的,我实际上只需要发行人的组织名称和到期日期,这就是我在下面包含的所有代码。但是,基于此,您应该能够通过阅读以下内容来弄清楚其余的内容x509.h头文件。

Edit:

以下是如何获取证书。我没有进行任何错误处理等。你需要检查一下trustResult, err等等,例如。

NSURLAuthenticationChallenge *challenge;
SecTrustResultType trustResult;
SecTrustRef trust = challenge.protectionSpace.serverTrust;
OSStatus err = SecTrustEvaluate(trust, &trustResult);
SecCertificateRef certificate = SecGetLeafCertificate(trust); // See Apple docs for implementation of SecGetLeafCertificate
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SecCertificateRef:如何获取证书信息? 的相关文章

  • 进入后台时 Alamofire 请求卡住?

    我正在使用 Alamofire 调用 Web 服务 该服务需要相当长的时间才能加载 如果应用程序进入后台 当我返回应用程序时 我会被加载程序卡住 我想这是因为调用永远不会向我的完成处理程序返回任何内容 我该如何解决这个问题 您可以使用后台抓
  • 选择 UITableViewCell 时 UIView 背景颜色消失

    我在界面生成器中构建了一个简单的 tableViewCell 它包含一个包含图像的 UIView 现在 当我选择单元格时 会显示默认的蓝色选择背景 但 UIView 的背景颜色消失了 我的 UITableViewCell 的实现文件没有做任
  • iPhone 3GS 上的 ARM 与 Thumb 性能比较,非浮点代码

    我想知道是否有人有关于 iPhone 3GS 上 ARM 与 Thumb 代码性能的硬性数据 特别是对于非浮点 VFP 或 NEON 代码 我知道 Thumb 模式下的浮点性能问题 更大的 ARM 指令的额外代码大小是否会在某个时刻成为性能
  • 如何在应用程序项目中使用 Cocoa Touch 框架

    我熟悉构建单个 iOS 应用程序 但我坚持使用 Cocoa Touch 框架向多个应用程序共享通用代码 问题 框架的头文件不可见 无法链接到消费应用程序项目 我做了什么 1 我创建了一个名为 libTestFramework 的项目 Coc
  • UISlider不会自动重绘

    我的应用程序上有一个 UISlider 有时我不仅需要更新它的值 还需要更新它的minimumValue 值已更改 但如果我调用 setValue 方法或为滑块分配新值 它会具有新值 但滑块不会将自身重新绘制到该新值应有的位置 我怎样才能重
  • IDFA 使用不当,您的应用不遵守 ios 中的限制广告跟踪设置

    I have checked the iTC settings I have uploaded the same app 2 days ago and it works fine but when today I uploaded the
  • 如何区分 iTunes Connect / Apple TestFlight 上的 STAGE 和 PRODUCTION 版本?

    阶段构建与阶段服务器的对话 阶段服务器与生产服务器尽可能相同 以用于测试目的 生产构建与生产服务器的通信 生产服务器存储真实的关键数据 这些构建本质上是针对同一应用程序的 但是 iTunes Connect 界面将向您显示以下内容 即构建由
  • 用于具有转换的非导航应用程序的视图控制器/NIB 架构?

    我正在修补一个 iPad 应用程序 就像许多 iPad 应用程序一样 它不使用 UINavigation 根视图控制系统 因此我没有每个应用程序 视图 的自然所有权 我基本上有两个基本视图 文档列表视图和文档编辑视图 我正在使用 UIVie
  • 如何为图层阴影不透明度设置动画?

    我有一个视图 我已将 LayerOpacity 设置为 1 theView layer shadowOpacity 1 0 当视图位于屏幕下方时 这看起来很好 当我将此视图向上移动以与另一个有阴影的视图齐平时 它们看起来不太好 有没有办法让
  • Apple Developer 应用程序门户不再可以生成新的 Bundle Seed ID

    iOS 开发者门户中的新界面不再为您的应用程序 ID 提供 生成新的 按钮 取而代之的是 使用团队 ID 这将导致使用相同的种子 ID 任何人都知道为什么要进行更改以及您应该如何使用新的捆绑包种子 ID 随意补一些 不再可能生成新的种子 I
  • MKMapView 拦截/劫持 iPhone 触摸事件

    3 0 SDK 是否存在禁用实时缩放并拦截 MKMapView 放大手势的 bug 我有一些真正简单的代码 因此我可以检测点击事件 但有两个问题 放大手势始终被解释为缩小手势 所有缩放手势都不会实时更新地图视图 在 hitTest 中 如果
  • 将 Armadillo C++ 库导入 Xcode

    我是 Mac 用户 正在尝试安装和导入 C Armadillo 库 以下是我到目前为止所采取的步骤 1 我从其网站下载了犰狳库 2 我仔细阅读了下载文件中的 Readme txt 文件 解释了如何安装它 3 我使用CMake将犰狳下载文件制
  • iOS 如何触发视频退出全屏后继续播放?

    我正在构建一个在 iOS 中播放视频的网站 我有一个在 iOS 中工作的全屏按钮 但是退出全屏时视频会暂停 有谁知道一种方法可以强制视频在退出全屏时继续播放 或者如何设置一个侦听器来触发视频在退出全屏时自动播放 这是我的代码
  • 默认情况下在 MPMovies PlayerViewController 中显示字幕

    有什么方法可以在默认情况下由 MPMovies PlayerViewController 加载的电影上显示字幕吗 这部电影在视频中嵌入了字幕 谢谢你 不幸的是 这个功能在MPMoviePlayer播放类 这显然相当烦人 您应该考虑向 App
  • 使用 BGTaskScheduler 进行后台获取与调试模拟完美配合,但在实践中却不起作用

    我在 appDelegate 的 didFinishLaunchingWithOptions 中注册后台获取任务 BGTaskScheduler shared register forTaskWithIdentifier Backgroun
  • 如何为 Nslocal 通知设置自定义重复间隔......?

    我是 iphone 开发新手 我正在尝试在我的项目中使用 NslocalNotification 我需要每 2 小时或每两天或每两个月等给出提醒 目前我正在使用 NslocalNotification 重复间隔 但它仅适用于使用 Ncale
  • 命令 /Applications/Xcode-beta.app/Contents/Developer/usr/bin/atool 失败,退出代码 255

    它发生在新的 Xcode7 beta 中 CompileAssetCatalog Users admin Library Developer Xcode DerivedData InstaAd ddgdnbxlpxipirebpndfmgr
  • UILabel 中的文本未垂直居中

    我使用以下代码创建了一个标签 func setupValueLabel valueLabel numberOfLines 1 valueLabel font UIFont name Avenir Black size 50 valueLab
  • 带有文本字段的 iPhone AlertView

    我有一个UIAlertView with a UITextField在里面 我想输入mail id并提交于UIAlertView s ok按钮 但是UITextField in the UIAlertView没有回复 请帮助我 thankz
  • iPhone Twitter SDK 与 iOS 5 设备的集成问题

    我已成功将 Twitter Sharekit 与我的 iPad 应用程序源集成 当我在模拟器和装有 iOS 4 X 的 iPad 1 上测试该应用程序时 它运行完美 并且成功发布了推文 但是 如果我在装有 iOS 5 的 iPad 2 上安

随机推荐