2

I'm using the following code to verify the application is signed. It's in Objective-C and it's based on the code found on 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); 

I need to convert this to plain C so I can also use it on the apps I am writing in C. One of the problem is the NSDictionary *signingInfo = nil; which I tried to solve by using CFDictionaryRef *signingInfo = NULL; but it doesn't seem to work.

Any chance anyone could translate this code to C?

Thanks!

1
  • And here we go, mind explaining WHY the downvotes? If you have something to say PLEASE say to my face and not just downvote Commented Jan 20, 2012 at 14:44

2 Answers 2

3

Have you tried using CFDictionaryRef signingInfo = NULL; without the extra *? A core foundation ref already is a pointer. CFDictionaryRef is toll-free bridged to NSDictionary*. [signingInfo release]; can then be translated to CFRelease(signingInfo). You should also replace NSLog with something else.

Sign up to request clarification or add additional context in comments.

1 Comment

Great, thanks! And thanks for pointing out the CFRelease(signingInfo)
3

CFDictionaryRef already is a pointer. So you should use CFDictionaryRef rather than CFDictionaryRef*.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.