SecKeyRawVerify和OSError -9809

dre*_*rew 7 objective-c security-framework ios

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

先前的安全框架调用加载并创建SecTrustRef,从中获取公钥似乎没问题 - 没有错误.唯一的小问题是调用SecTrustEvaluate返回a 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]
                          );
Run Code Online (Sandbox Code Playgroud)

dre*_*rew 2

我已经发现发生了什么事。该SecKeyRawVerify调用将数据摘要作为输入,而不是数据本身。下面的代码有效 - 顺便说一句,如果由于基础数据已更改而未验证签名,则状态返回为 -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)
                          );
Run Code Online (Sandbox Code Playgroud)