Mr Aleph
Mr Aleph

Reputation: 1895

Converting Objective-C code to plain C

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!

Upvotes: 2

Views: 816

Answers (2)

Martin Hering
Martin Hering

Reputation: 429

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.

Upvotes: 3

David Heffernan
David Heffernan

Reputation: 612784

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

Upvotes: 3

Related Questions