SecKeyGetBlockSize or SecKeyRawVerify for Public Key throw EXC_BAD_ACCESS code=2

眉间皱痕 提交于 2019-12-07 05:06:19

问题


Upon trying to implement Security.Framework SecKeyRawVerify iOS function from Apple's example, programm halts with bad pointer error (EXC_BAD_ACCESS code=2). Any help or suggestions would be appreciated.

Here is my code:

- (BOOL)verifySignature:(NSData *)plainText signature:(NSData *)sig {
    size_t signedHashBytesSize = 0;

    OSStatus sanityCheck = noErr;
    SecKeyRef publicKeyA = NULL;

    NSMutableDictionary * queryPublicKeyA = [[NSMutableDictionary alloc] init];
    NSData * publicTag = [NSData dataWithBytes:publicKeyAIdentifier length:strlen((const char *)publicKeyAIdentifier)]; // 

    // Set the public key query dictionary.
    [queryPublicKeyA setObject:(id)kSecClassKey forKey:(id)kSecClass];
    [queryPublicKeyA setObject:publicTag forKey:(id)kSecAttrApplicationTag];
    [queryPublicKeyA setObject:(id)kSecAttrKeyTypeRSA forKey:(id)kSecAttrKeyType];
    [queryPublicKeyA setObject:[NSNumber numberWithBool:YES] forKey:(id)kSecReturnData];

    // Get the key bits.
    sanityCheck = SecItemCopyMatching((CFDictionaryRef)queryPublicKeyA, (CFTypeRef *)&publicKeyA);

    if (sanityCheck == noErr) {       
        // Get the size of the assymetric block.
        signedHashBytesSize = SecKeyGetBlockSize(publicKeyA); // Halts here
        sanityCheck = SecKeyRawVerify(publicKeyA, 
                                  kSecPaddingPKCS1SHA1, 
                                  (const uint8_t *)[[self getHashBytes:plainText] bytes],
                                  CC_SHA1_DIGEST_LENGTH,
                                  (const uint8_t *)[sig bytes],
                                  signedHashBytesSize
                                  ); // And here
    }
    if(publicKeyA) CFRelease(publicKeyA);
    if(queryPublicKeyA) [queryPublicKeyA release]; 

    return (sanityCheck == noErr) ? YES : NO;
}

Link to Apple CryptoExcersize: http://developer.apple.com/library/ios/#samplecode/CryptoExercise/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008019-Intro-DontLinkElementID_2

来源:https://stackoverflow.com/questions/10993505/seckeygetblocksize-or-seckeyrawverify-for-public-key-throw-exc-bad-access-code-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!