SecKeyRawVerify和OSError -9809

我正在使用数字证书在我的应用程序中签署数据文件。 当对SecKeyRawVerify的调用返回-9809时,下面的代码片段失败。 这是在iPhone上运行。 我甚至无法准确确定此错误代码的含义

先前的安全框架调用加载并创建SecTrustRef,从中获取公钥似乎没问题 – 没有错误。 唯一的小问题是对SecTrustEvaluate的调用返回了kSecTrustResultUnspecified ,但我认为这是因为我使用的策略是SecPolicyCreateBasicX509调用返回的样板。

非常感谢任何帮助或见解。

谢谢

 SecKeyRef keyRef = SecTrustCopyPublicKey (trustRef); fileURL = [[NSBundle mainBundle] URLForResource:@"data" withExtension:@"txt"]; NSData *data = [NSData dataWithContentsOfURL:fileURL]; fileURL = [[NSBundle mainBundle] URLForResource:@"data" withExtension:@"sgn"]; NSData *signature = [NSData dataWithContentsOfURL:fileURL]; NSLog(@"Hash block size = %zu",SecKeyGetBlockSize(keyRef)); status = SecKeyRawVerify (keyRef, kSecPaddingPKCS1SHA1, (const uint8_t *)[data bytes], (size_t)[data length], (const uint8_t *)[signature bytes], (size_t)[signature length] ); 

该错误在/System/Library/Frameworks/Security.framework/Headers/SecureTransport.h定义为(以及其他相关错误)为errSSLCrypto 。 那里的评论称之为“潜在的加密错误”,这不是一个特别描述性的描述。

一想法: kSecTrustResultUnspecified意味着信任级别等于默认系统策略。 链中的所有证书都是可信的吗?

我发现了正在发生的事情。 SecKeyRawVerify调用将数据的摘要作为输入,而不是数据本身。 下面的代码有效 – 顺便说一句,如果由于基础数据已更改而未validation签名,则状态返回为-9809。

谢谢

 CC_SHA1((const void *)[data bytes], [data length], (unsigned char *)hash); status = SecKeyRawVerify (keyRef, kSecPaddingPKCS1SHA1, hash, 20, (const uint8_t *)[signature bytes], SecKeyGetBlockSize(keyRef) );