Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development > iPhone SDK Development - Advanced Discussion

Reply
 
LinkBack Thread Tools Display Modes
Old 08-10-2011, 02:40 PM   #1 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 5
clarket2 is on a distinguished road
Default iOS SecKeyRawSign issue

I've been able to use SecKeyRawSign (and verify the signature using SecKeyRawVerify) on the iPhone, however, when I try to verify the signaure server side in PHP using openssl_verify() I get a result that the signature is invalid. I'm using SecPaddingPKCS1SHA1 in the iPhone client and openssl_verify is using the default OPENSSL_ALGO_SHA1 algorithm. I base64 encode before sending the signed data and then base64 decode on the server before trying to verify the signature.

I've also tries sttipping out the "-----BEGIN PUBLIC KEY-----" and "-----END PUBLIC KEY-----" headers in the public key and base64 decoding before using openssl_verity().

If I do a test in PHP only to create some signed data and verify the signature its fine.

On thing Ive noticed is that the signature is different each time when signing the same text, I can see this as I write to NSLog, is this corrent?

Any help would be great and I'll post my code when working as I know other people have issues with signing, I can also provide examples of asymmetric encryption that I have working.


Here is my objective-c:

Code:
uint8_t *plainBuffer;
        uint8_t * signedBytes = NULL;
    size_t signedBytesSize = 0;
        OSStatus sanityCheck = noErr;
        NSData * signedHash = nil;

        char inputString = *inputText;
        int len = strlen(&inputString);

        plainBuffer = (uint8_t *)calloc(len, sizeof(uint8_t));

        strncpy( (char *)plainBuffer, &inputString, len);


        NSString * path = [[NSBundle mainBundle]
pathForResource:@"iphone-cert" ofType:@"p12"];
        assert(path != nil);

        NSData * data = [NSData dataWithContentsOfFile:path];

    assert(data != nil);

    //CFArrayRef tmpCFArrayRef = CFArrayCreate(kCFAllocatorDefault,
NULL, 0, NULL);//(CFArrayRef)items;

    NSMutableDictionary * options = [[NSMutableDictionary alloc] init];


        // Set the public key query dictionary.

    [options setObject:@"password" forKey:(id)kSecImportExportPassphrase];

        CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL);

        OSStatus securityError = SecPKCS12Import((CFDataRef) data,
(CFDictionaryRef)options, &items);

        CFDictionaryRef identityDict = CFArrayGetValueAtIndex(items, 0);
        SecIdentityRef identityApp =
(SecIdentityRef)CFDictionaryGetValue(identityDict,
kSecImportItemIdentity);
        //NSLog(@"%@", securityError);


        SecKeyRef privateKeyRef;
    SecIdentityCopyPrivateKey(identityApp, &privateKeyRef);
        //NSLog(@"%@", privateKeyRef);


        signedBytesSize = SecKeyGetBlockSize(privateKeyRef);

    // Malloc a buffer to hold signature.
    signedBytes = malloc( signedBytesSize * sizeof(uint8_t) );
    memset((void *)signedBytes, 0x0, signedBytesSize);



    sanityCheck = SecKeyRawSign(    privateKeyRef,
                                                                kTypeOfSigPadding,
                                                                (const uint8_t *)&inputString,
                                                                kChosenDigestLength,
                                                                (uint8_t *)signedBytes,
                                                                &signedBytesSize
                                );

        signedHash = [NSData dataWithBytes:(const void *)signedBytes
length:(NSUInteger)signedBytesSize];

        //NSLog(@"%@", signedHash);

        [Base64 initialize];
        NSString *b64EncStr = [Base64 encode:signedHash];
        //NSLog(@"%@", b64EncStr);
        //NSLog(@"String Length: %d", b64EncStr.length);

        //Verify Signture
        NSString *certPath = [[NSBundle mainBundle]
pathForResource:@"iphone1" ofType:@"cer"];
    SecCertificateRef myCertificate = nil;
    NSData *certificateData = [[NSData alloc] initWithContentsOfFile:certPath];

    myCertificate = SecCertificateCreateWithData(kCFAllocatorDefault,
(CFDataRef)certificateData);
        NSLog(@"Status: %@", myCertificate);

    SecCertificateRef certArray[1] = {myCertificate};
        CFArrayRef myCerts = CFArrayCreate(NULL, (const void **) &certArray, 1, NULL);

        SecPolicyRef myPolicy = SecPolicyCreateBasicX509();

        SecTrustRef myTrust;
        SecTrustResultType trustResult;

    OSStatus status = SecTrustCreateWithCertificates(myCerts,
myPolicy, &myTrust);
        //NSLog(@"Status: %i", errSecSuccess);
        //NSLog(@"Status: %i", status);

        //SecTrustSetAnchorCertificates(myTrust, myCerts);
        //NSLog(@"Anchor: %i", errSecSuccess);

        //CFRelease(myCerts);
        //CFRelease(myCACert);

        OSStatus trustStatus = SecTrustEvaluate(myTrust, &trustResult);
        if (trustStatus == errSecSuccess) {
                NSLog(@"We can trust this certificate");
        } else {
                NSLog(@"We CANNOT trust this certificate");
        }

        //NSLog(@"Status: %i", errSecSuccess);

        SecKeyRef publicKey = SecTrustCopyPublicKey(myTrust);
        NSLog(@"Public Key: %d", publicKey);



        OSStatus verifySig = SecKeyRawVerify(publicKey,
                                                                                 kTypeOfSigPadding,
                                                                                 (const uint8_t *)&inputString,
                                                                                 kChosenDigestLength,
                                                                                 (uint8_t *)signedBytes,
                                                                                 signedBytesSize
                                                                                 );
        if (verifySig == errSecSuccess) {
                NSLog(@"Valid Signature");
        } else {
            NSLog(@"Invalid Signature");
        }
Here is my PHP code;

Code:
//verify signature first
    $signature = base64_decode($sig);
    $data=$uname.$pword;
    $fp=fopen("keys/client/iphone-pubkey.pem","r");
    $pub_key=fread($fp,8192);
    fclose($fp);


    $pub_key=base64_decode($key);

    $ok = openssl_verify($data, $signature, $pub_key);
if ($ok == 1) {
    $okResult= "Signature is good";
} elseif ($ok == 0) {
    $okResult= "Signature is bad";
} else {
    $okResult= "Signature is ugly, error checking signature";
}
clarket2 is offline   Reply With Quote
Old 08-12-2011, 08:35 AM   #2 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 5
clarket2 is on a distinguished road
Default

Has anyone any thoughts on this?
clarket2 is offline   Reply With Quote
Reply

Bookmarks

Tags
digital signatures

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 394
16 members and 378 guests
Absentia, bignoggins, Diligent, dre, epaga, hussain1982, jbro, LunarMoon, momolgtm, Newbie123, omagod, Paul10, pinacate, revg, skog, taylor202
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,643
Threads: 94,110
Posts: 402,860
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Diligent
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:50 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0