SecKeyRawSign返回OSStatus = noErr,但返回随机签名对象

时间:2015-02-11 19:58:26

标签: ios security digital-signature seckeyref

我使用SecKeyRawSign对PDF的SHA摘要进行数字签名,SecKeyRawSign报告OSStatus = noErr,但每次运行项目时都会获得随机签名对象。

privateKey始终相同(保存在Keychain中)。我还验证了文档摘要是相同的(转换为base64和hexdump)

所以问题是:给定相同的输入,SecKeyRawSign报告status = noErr,但输出不同。

这是我正在使用的代码:

NSData *hashData = [self getHashData];
SecKeyRef privateKey = [self getPrivateKeyFromKeychain];

size_t signatureBytesSize = SecKeyGetBlockSize(privateKey); 
uint8_t *signatureBytes = malloc(signatureBytesSize * sizeof(uint8_t));
memset((void *)signatureBytes, 0x0, signatureBytesSize);

OSStatus status = noErr;
status = SecKeyRawSign(privateKey, kSecPaddingSHA1, (const unsigned char *)hashData.bytes, SecKeyGetBlockSize(privateKey), signatureBytes, &signatureBytesSize);

if (status == noErr) {
    NSLog(@"Signing OK");
    NSLog(@"Signature: \"%s\"", signatureBytes);
} else {
    NSLog(@"Error signing data: result code: %d", (int)status);
}

我总是得到status = noErr,“Signing OK”,但signatureBytes是随机和随机的。

我试过不同的Padding。还要将签名缓冲区大小设置为密钥大小 - 11(根据文档)

研究文档我找不到有关签名对象类型(sig)的更多细节。我假设它是原始字节,可以转换为Base64。

任何帮助将不胜感激.... e

1 个答案:

答案 0 :(得分:3)

我发现了问题,并且能够签署PDF哈希并验证签名。 我在SecKeyGetBlockSize(privateKey)的{​​{1}}变量

中使用了hashData.length而不是dataToSignLen
SecKeyRawSign