【问题标题】:How to obtain codesigned application certificate info如何获取代码签名的应用程序证书信息
【发布时间】:2010-12-21 09:24:48
【问题描述】:

我很难找到代码设计问题的答案。

我们有一个在 Cocoa 下编写的 Mac OS 应用程序。最后 - 我们进行了代码设计,但我想在可执行文件本身内添加额外的安全检查。

我的想法是验证当前可执行文件在启动时签署的证书的指纹。如果它丢失或无效(检查应用程序中的硬编码哈希) - 我们将其关闭。

到目前为止,我还无法获得用于以编程方式对可执行文件进行代码签名并检查其数据的证书。

有人知道怎么做吗?

非常感谢! 马丁·K。

【问题讨论】:

    标签: cocoa security macos certificate codesign


    【解决方案1】:

    谢谢朋友!

    我设法用新功能在 10.6 上实现了它,但问题是我的目标是 10.5 和 10.6,至少在一段时间过去之前。

    我必须尽快在 libsecurity_codesigning 上投入更多时间,以便在 10.5 中也能完成。

    但是,对于在这里寻找现成解决方案的人来说,这就是我最终得到的结果:

    SecStaticCodeRef ref = NULL;
    
    NSURL * url = [NSURL URLWithString:[[NSBundle mainBundle] executablePath]]; 
    
    OSStatus status;
    
    // obtain the cert info from the executable
    status = SecStaticCodeCreateWithPath((CFURLRef)url, kSecCSDefaultFlags, &ref);
    
    if (ref == NULL) exit(EXIT_STATUS_ON_BAD_CODE_SIGNATURE);
    if (status != noErr) exit(EXIT_STATUS_ON_BAD_CODE_SIGNATURE);
    
    SecRequirementRef req = NULL;
    
    // this is the public SHA1 fingerprint of the cert match string
    NSString * reqStr = [NSString stringWithFormat:@"%@ %@ = %@%@%@",
        @"certificate",
        @"leaf",
        @"H\"66875745923F01",
        @"F122B387B0F943",
        @"X7D981183151\""
        ];
    
    // create the requirement to check against
    status = SecRequirementCreateWithString((CFStringRef)reqStr, kSecCSDefaultFlags, &req);
    
    if (status != noErr) exit(EXIT_STATUS_ON_BAD_CODE_SIGNATURE);
    if (req == NULL) exit(EXIT_STATUS_ON_BAD_CODE_SIGNATURE);
    
    status = SecStaticCodeCheckValidity(ref, kSecCSCheckAllArchitectures, req);
    
    if (status != noErr) exit(EXIT_STATUS_ON_BAD_CODE_SIGNATURE);
    
    CFRelease(ref);
    CFRelease(req);
    
    LogDebug(@"Code signature was checked and it seems OK");
    

    【讨论】:

    • 感谢分享!顺便说一句,我假设第二行中的 NSURL * url = [[NSBundle mainBundle] bundleURL] 可用于检查整个包的有效性,包括资源和可执行文件。对吗?
    • 不,但我认为现在有点晚了:)
    【解决方案2】:

    如果您的目标是 10.6+,您可以使用安全框架 (documentation) 中的代码签名功能,尤其是 SecCodeCheckValidity。否则,代码签名系统的源代码在libsecurity_codesigning

    由于您使用代码签名来验证您的代码,您还应该使用 SecCodeCopyDesignatedRequirement 验证指定的需求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-13
      • 2013-04-02
      • 1970-01-01
      • 2013-01-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多