很难让一个非常基本的 SecKeySign() 版本工作(即将工作的 OSX SecSignTransformCreate()/SecTransformSetAttribute()/SecTransformExecute() 移植到 iOS):
该代码几乎与http://developer.apple.com/library/ios/#samplecode/CryptoExercise/Listings/Classes_SecKeyWrapper_m.html 一致- 尽管进一步简化。
首先 - 设置 - 按照上面的链接。没有变化。
const char someData[] = "No one loves pain itself, but those who seek...";
NSData * blob = [NSData dataWithBytes:someData length:sizeof(someData)];
assert(blob);
SecKeyRef publicKeyRef, privateKeyRef;
int keySize = 2048;
OSStatus sanityCheck = noErr;
NSMutableDictionary * privateKeyAttr = [[NSMutableDictionary alloc] init];
NSMutableDictionary * publicKeyAttr = [[NSMutableDictionary alloc] init];
NSMutableDictionary * keyPairAttr = [[NSMutableDictionary alloc] init];
// attribute dictionaries for 2048 bit RSA key pair.
//
[keyPairAttr setObject:(__bridge id)kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType];
[keyPairAttr setObject:[NSNumber numberWithUnsignedInteger:keySize] forKey:(__bridge id)kSecAttrKeySizeInBits];
[privateKeyAttr setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecAttrIsPermanent];
[publicKeyAttr setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecAttrIsPermanent];
[keyPairAttr setObject:privateKeyAttr forKey:(__bridge id)kSecPrivateKeyAttrs];
[keyPairAttr setObject:publicKeyAttr forKey:(__bridge id)kSecPublicKeyAttrs];
实际工作从生成密钥对开始:
sanityCheck = SecKeyGeneratePair((__bridge CFDictionaryRef)keyPairAttr, &publicKeyRef, &privateKeyRef);
assert(sanityCheck == noErr);
NSLog(@"Pub/Priv: %@/%@", publicKeyRef, privateKeyRef);
据我所知,这非常有效。
问题在于使用它们签名;或者更确切地说是签名:
// Zero-ed Buffer for the signature.
//
size_t signatureBytesSize = SecKeyGetBlockSize(privateKeyRef);
assert(signatureBytesSize == keySize / 8);
uint8_t * signatureBytes = malloc( signatureBytesSize * sizeof(uint8_t) );
memset((void *)signatureBytes, 0x0, signatureBytesSize);
// Sign the binary blob; with type 1 padding.
//
sanityCheck = SecKeyRawSign(privateKeyRef,
kSecPaddingPKCS1,
(const uint8_t *)[blob bytes], [blob length],
(uint8_t *)signatureBytes, &signatureBytesSize
);
assert(sanityCheck == noErr);
它总是返回 -50/errSecParam(传递给函数的一个或多个参数无效。)。
有什么建议么 ?这是在实际的 iPhone 上吗?
谢谢,
德。