gpt4 book ai didi

ios - iOS 和 MacOSX 上的私钥签名不同

转载 作者:可可西里 更新时间:2023-11-01 05:22:46 24 4
gpt4 key购买 nike

我在 NSData 类上实现了一个类别方法,该方法使用 SHA-1 哈希返回数据签名,然后使用私钥进行加密,如下所示:

- (NSData *)signatureWithKey:(SecKeyRef)keyRef {

if (keyRef == NULL) {
return nil;
}

NSData *sha1Digest = [self dataWithSHA1Digest];

size_t maxLength = SecKeyGetBlockSize(keyRef) - 11;

if ([sha1Digest length] > maxLength) {
NSString *reason = [NSString stringWithFormat:@"Digest is too long to sign with this key, max length is %ld and actual length is %ld", maxLength, (unsigned long)[self length]];
NSException *ex = [NSException exceptionWithName:@"BMInvalidArgumentException" reason:reason userInfo:nil];
@throw ex;
}

#if TARGET_OS_IPHONE
OSStatus status = noErr;

uint8_t *plainBuffer = (uint8_t *)[sha1Digest bytes];
size_t plainBufferSize = [sha1Digest length];
size_t cipherBufferSize = SecKeyGetBlockSize(keyRef);
uint8_t *cipherBuffer = malloc(cipherBufferSize * sizeof(uint8_t));

status = SecKeyRawSign(keyRef,
kSecPaddingPKCS1SHA1,
plainBuffer,
plainBufferSize,
&cipherBuffer[0],
&cipherBufferSize
);

if (status == noErr) {
return [NSData dataWithBytesNoCopy:cipherBuffer length:cipherBufferSize freeWhenDone:YES];
}

free(cipherBuffer);
return nil;
#else
CFErrorRef error = NULL;
SecTransformRef signer = NULL;
CFTypeRef signature = NULL;
if ((signer = SecSignTransformCreate(keyRef, &error))) {
if (SecTransformSetAttribute(
signer,
kSecTransformInputAttributeName,
(CFDataRef)sha1Digest,
&error)) {
signature = SecTransformExecute(signer, &error);
}
}

if (error) {
LogWarn(@"Could not sign: %@", error);
CFRelease(error);
}

if (signer) {
CFRelease(signer);
}

if (signature) {
NSData *data = [NSData dataWithData:(NSData *)signature];
CFRelease(signature);
return data;
} else {
return nil;
}

#endif

}

现在奇怪的是,使用相同的私钥(从 p12 文件加载)在签署相同数据时,iOS 和 MacOSX 会得到两个不同的结果。我对此完全不解。您可能会注意到上面的方法使用安全转换为 MacOSX 使用了不同的实现,但即使我在 MacOSX 上使用 iOS 实现(它给出了编译警告但工作正常)我得到了相同的结果。

从文件中加载私钥的方法如下:

+ (SecKeyRef)newPrivateKeyRefWithPassword:(NSString *)password fromData:(NSData *)data {
NSMutableDictionary * options = [[NSMutableDictionary alloc] init];

SecKeyRef privateKeyRef = NULL;

// Set the public key query dictionary
//change to your .pfx password here
[options setObject:password forKey:(id)kSecImportExportPassphrase];

CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL);

OSStatus securityError = SecPKCS12Import((CFDataRef)data,
(CFDictionaryRef)options, &items);

if (securityError == noErr && CFArrayGetCount(items) > 0) {
CFDictionaryRef identityDict = CFArrayGetValueAtIndex(items, 0);
SecIdentityRef identityApp =
(SecIdentityRef)CFDictionaryGetValue(identityDict,
kSecImportItemIdentity);

securityError = SecIdentityCopyPrivateKey(identityApp, &privateKeyRef);
if (securityError != noErr) {
privateKeyRef = NULL;
}
}
[options release];
if (items) CFRelease(items);
return privateKeyRef;
}

这是我使用的测试用例。请注意,在 iOS 和 MacOSX 上打印了两个不同的字符串:

    NSString *test = @"bla";
NSData *testData = [test dataUsingEncoding:NSUTF8StringEncoding];

NSString *p12Path= [[NSBundle mainBundle] pathForResource:@"private_key" ofType:@"p12"];

NSData *p12Data = [NSData dataWithContentsOfFile:p12Path];

SecKeyRef keyRef = [BMSecurityHelper newPrivateKeyRefWithPassword:@"xxxxxxxx" fromData:p12Data];

NSData *signatureData = [testData signatureWithKey:keyRef];
NSString *signatureString = [BMEncodingHelper base64EncodedStringForData:signatureData withLineLength:0];

if (keyRef) CFRelease(keyRef);

NSLog(@"signatureString: %@", signatureString);

最佳答案

如果您能回答自己的问题,那就太好了。我错过了以下内容:在 MacOSX 下,安全转换也会自动计算 SHA-1 哈希,这与 iOS 实现形成对比。

我通过在 MacOSX 实现中添加以下内容解决了这个问题:

SecTransformSetAttribute(signer, kSecInputIsAttributeName, kSecInputIsDigest, &error)

关于ios - iOS 和 MacOSX 上的私钥签名不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17348420/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com