我正在使用以下代码来验证应用程序是否已签名。它位于Objective-C中,并且基于Professional Cocoa Application Security上的代码。

OSStatus secError = noErr;
// retrieve this process's code object
SecCodeRef myCode;
secError = SecCodeCopySelf(kSecCSDefaultFlags, &myCode);
if (noErr != secError)
{
    NSLog(@"unable to retrieve code object, security error %d", secError);
    return -1;
}

// validate the process's identity, using the internal requirements
secError = SecCodeCheckValidity(myCode, kSecCSDefaultFlags, NULL);
switch (secError)
{
    case noErr:
        NSLog(@"this process has a valid signature");
        break;
    case errSecCSUnsigned:
        NSLog(@"this process executable is unsigned");
        break;
    case errSecCSSignatureFailed:
    case errSecCSGuestInvalid:
        NSLog(@"this process has an invalid signature");
        break;
    default:
        NSLog(@"error %d validating signature", secError);
        break;
}

// get the static code object, representing the executable on disk
SecStaticCodeRef fileCode;
secError = SecCodeCopyStaticCode(myCode, kSecCSDefaultFlags, &fileCode);
if (noErr != secError)
{
    NSLog(@"unable to get static code object, security error %d", secError);
    CFRelease(myCode);
    return -1;
}

//some basic information about the code signature
NSDictionary *signingInfo = nil;

secError = SecCodeCopySigningInformation(fileCode, kSecCSDefaultFlags, &signingInfo);
if (noErr != secError)
{
    if(secError == errSecCSSignatureFailed)
        NSLog(@"invalid signature");
    else
        NSLog(@"cannot get signing information, security error %d", secError);
}
else
{
    NSLog(@"signing info: %@", signingInfo);
    [signingInfo release];
}

CFRelease(myCode);
CFRelease(fileCode);


我需要将其转换为纯C,因此我也可以在用C编写的应用程序上使用它。问题之一是NSDictionary *signingInfo = nil;,我尝试通过使用CFDictionaryRef *signingInfo = NULL;解决此问题,但它似乎不起作用。

任何人都有可能将此代码转换为C吗?

谢谢!

最佳答案

您是否尝试过在没有附加*的情况下使用CFDictionaryRef signingInfo = NULL;?核心基础引用已经是一个指针。 CFDictionaryRef是免费电话桥接到NSDictionary *。然后可以将[signingInfo release];转换为CFRelease(signingInfo)。您还应该用其他东西代替NSLog。

10-05 23:51