13

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

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

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

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

有谁知道如何做到这一点?

非常感谢!马丁·K。

4

2 回答 2

10

谢谢你,朋友!

我设法使用新功能为 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");
于 2009-12-10T01:00:36.793 回答
4

如果您的目标是 10.6+,您可以使用安全框架(文档)中的代码签名功能,特别是 SecCodeCheckValidity。否则,代码签名系统的源代码位于libsecurity_codesigning中。

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

于 2009-11-29T20:41:25.320 回答