4

I want to verify a data on iOS 7. Now I have a function:

-(BOOL) PKCSVerifyBytesSHA256withRSA:(NSData*) plainData
                            withSign:(NSData*) signature
                                withKey:(SecKeyRef) public_Key{
        size_t signedHashBytesSize = SecKeyGetBlockSize(public_Key);
        const void* signedHashBytes = [signature bytes];
        size_t hashBytesSize = CC_SHA256_DIGEST_LENGTH;
        uint8_t* hashBytes = malloc(hashBytesSize);
        if (!CC_SHA256([plainData bytes], (CC_LONG)[plainData length], hashBytes)){
            return nil;
        }
        OSStatus status = SecKeyRawVerify(publicKey,
                                          kSecPaddingPKCS1SHA256,
                                          hashBytes,
                                          hashBytesSize,
                                          signedHashBytes,
                                          signedHashBytesSize);
        return status == errSecSuccess;
}

and a key:

NSString *public_key = @"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCT2eIq8i/uAHzvwOWhFU9AuRHjPnGq8wD461ZH7N4/LjoRwPaPF3meyyENJgtDr4EyhV9KN77/3VT+87ZpT6QH9w6Q5XwDmM3jhU6bUhWyIPLzrd5XE2rQKRIMXixflz/8Q327VHKsLoKu44HuCh6XrB+uMWUjwVLCLOi2U0sYdQIDAQAB"; 

I was suggest a link here so I add the - (SecKeyRef)getPublicKeyRef and PKCSVerifyBytesSHA256withRSA to a new class and call

RSA1 *rsa1 =[[RSA1 alloc]init];
    if([rsa1 PKCSVerifyBytesSHA256withRSA:Ddata withSign:Dsig withKey:[rsa1 getPublicKeyRef]]){
        NSLog(@"True");}
    else{
        NSLog(@"nil");}

but I got an error at the line

size_t signedHashBytesSize = SecKeyGetBlockSize(public_Key);
in the PKCSVerifyBytesSHA256withRSA function with the note: Thread 1: EXC_BAD_ACCESS(code=1, address=0x10) Is there any way to solve this problems?
Community
  • 1
  • 1
huynhtridung
  • 43
  • 2
  • 9
  • See [this](http://stackoverflow.com/questions/24612507/nsstring-to-seckeyref) – Raptor Nov 17 '14 at 07:34
  • Can you show me more clearly because I use: if([rsa PKCSVerifyBytesSHA256withRSA:Ddata withSign:Dsig withKey:[rsa getPublicKeyRef:public_key]]) NSLog(@"True"); else NSLog(@"nil"); I got a error: SecCertificateRef cert = SecCertificateCreateWithData(NULL, (__bridge CFDataRef)certData); – huynhtridung Nov 17 '14 at 08:49
  • Can you **edit** your question instead? Hard to read codes in comment. – Raptor Nov 17 '14 at 08:57
  • Can you show me more clearly because I use: `if([rsa PKCSVerifyBytesSHA256withRSA:Ddata withSign:Dsig withKey:[rsa getPublicKeyRef:public_key]]) NSLog(@"True"); else NSLog(@"nil");` I got a error at the getPublicKey function: `SecCertificateRef cert = SecCertificateCreateWithData(NULL, (__bridge CFDataRef)certData);` – huynhtridung Nov 17 '14 at 09:03
  • 2
    (facepalm) Edit the question ABOVE. – Raptor Nov 17 '14 at 09:04
  • In summary, I got a error and xcode point to getPublicKeyRef function (the code in the link you suggest). `SecCertificateRef cert = SecCertificateCreateWithData(NULL, (__bridge CFDataRef)certData);` – huynhtridung Nov 17 '14 at 09:09
  • what kind of error? **update** your question now. Thanks. – Raptor Nov 17 '14 at 09:15
  • I already update question, Thanks @Raptor – huynhtridung Nov 17 '14 at 10:15
  • See [this](http://stackoverflow.com/questions/10993505/seckeygetblocksize-or-seckeyrawverify-for-public-key-throw-exc-bad-access-code-2) – Raptor Nov 17 '14 at 10:18
  • @user3659792, you should not post code snippets in a comment, it is totally unreadable – update your OP instead. – holex Nov 17 '14 at 10:21
  • that's link not enought information for me, can you describe more clearly, @Raptor – huynhtridung Nov 17 '14 at 10:26
  • I already update the question, @holex – huynhtridung Nov 17 '14 at 10:29
  • @user3659792 don't wait for answer, search for more in Google. There are a couple matches. You should try to understand what you are writing. – Raptor Nov 17 '14 at 10:36
  • tks for your help, @Raptor – huynhtridung Nov 17 '14 at 10:51

0 Answers0