作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要一种干净的方法来提取我的服务器公钥并将其与本地数据进行比较,以防止将来过期/更新 key ,但我似乎无法获得 256 位 key 或表达它作为比较有用的数据...
这是我目前所拥有的...
-(BOOL)trustCertFromChallenge:(NSURLAuthenticationChallenge *)challenge
{
SecTrustResultType trustResult;
SecTrustRef trust = challenge.protectionSpace.serverTrust;
OSStatus status = SecTrustEvaluate(trust, &trustResult);
NSString *localKey = @"MY_PUBLIC_KEY";
NSData *localKeyData = [localKey dataUsingEncoding:NSUTF8StringEncoding];
SecCertificateRef serverCertificate = SecTrustGetCertificateAtIndex(trust, 0);
SecKeyRef key = SecTrustCopyPublicKey(trust);
DLog(@"Cert: %@ Key:%@",serverCertificate,key);
// this prints the correct cert information and key information
// for clarity....
// Key: <SecKeyRef algorithm id: 1, key type: RSAPublicKey, version: 3, block size: 2048 bits, exponent: {hex: 10001, decimal: 65537}, modulus: MY_PUBLIC_KEY, addr: 0x7fa78b80bc00>
// so far so good.. now for grabbing the key
NSData *keyData = [self getPublicKeyBitsFromKey:key];
DLog(@"Local: %@ - %li Key: %@ - %li",[localKeyData description],[localKeyData length],[keyData description],[keyData length]);
if ([localKeyData isEqualToData:keyData])
DLog(@"ITS THE SAME!");
else
DLog(@"NOT THE SAME!");
}
我参加了 Apples Crypto Exercise 并获得了以下...
- (NSData *)getPublicKeyBitsFromKey:(SecKeyRef)givenKey {
static const uint8_t publicKeyIdentifier[] = "com.mydomain.publickey";
NSData *publicTag = [[NSData alloc] initWithBytes:publicKeyIdentifier length:sizeof(publicKeyIdentifier)];
OSStatus sanityCheck = noErr;
NSData *publicKeyBits = nil;
NSMutableDictionary *queryPublicKey = [[NSMutableDictionary alloc] init];
[queryPublicKey setObject:(__bridge id)kSecClassKey forKey:(__bridge id)kSecClass];
[queryPublicKey setObject:publicTag forKey:(__bridge id)kSecAttrApplicationTag];
[queryPublicKey setObject:(__bridge id)kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType];
// Temporarily add key to the Keychain, return as data:
NSMutableDictionary * attributes = [queryPublicKey mutableCopy];
[attributes setObject:(__bridge id)givenKey forKey:(__bridge id)kSecValueRef];
[attributes setObject:@YES forKey:(__bridge id)kSecReturnData];
CFTypeRef result;
sanityCheck = SecItemAdd((__bridge CFDictionaryRef) attributes, &result);
if (sanityCheck == errSecSuccess) {
publicKeyBits = CFBridgingRelease(result);
// Remove from Keychain again:
(void)SecItemDelete((__bridge CFDictionaryRef) queryPublicKey);
}
return publicKeyBits;
}
这将返回 270 个字节,而不是预期的 256 个字节。我无法将其与我的 localData
本地 key 是 512 字节的 ASCII(为什么?)45323636 32323330 派生 key 是 270 字节的 UTF8 223b70a0 56f28f68
首先,我需要从 getPublicKeyBitsFromKey
获取 256 个字节,我还需要以相同的方式表达我的数据以进行比较。
还值得注意的是
NSString *keyString = [NSString stringWithUTF8String:[keyData bytes]];
和
NSString *keyString = [[NSString alloc] initWithBytes:[keyData bytes] length:[keyData length] encoding:NSUTF8StringEncoding];
返回(空)
NSString *keyString = [NSString stringWithCharacters:[keyData bytes] length:[keyData length]];
甚至不记录
如有任何帮助,我们将不胜感激,在此先致谢。
最佳答案
我通过在本地复制 .der 并仅固定它的公钥来解决这个问题。
-(BOOL)trustCertFromChallenge:(NSURLAuthenticationChallenge *)challenge
{
SecTrustResultType trustResult;
SecTrustRef trust = challenge.protectionSpace.serverTrust;
OSStatus status = SecTrustEvaluate(trust, &trustResult);
//DLog(@"Failed: %@",error.localizedDescription);
//DLog(@"Status: %li | Trust: %@ - %li",(long)status,trust,(long)trustResult);
if (status == 0 && (trustResult == kSecTrustResultUnspecified || trustResult == kSecTrustResultProceed)) {
SecKeyRef serverKey = SecTrustCopyPublicKey(trust);
NSString *certPath = [[NSBundle mainBundle] pathForResource:@"MYCert" ofType:@"der"];
NSData *certData = [NSData dataWithContentsOfFile:certPath];
SecCertificateRef localCertificate = SecCertificateCreateWithData(NULL, (__bridge CFDataRef)certData);
SecKeyRef localKey = NULL;
SecTrustRef localTrust = NULL;
SecCertificateRef certRefs[1] = {localCertificate};
CFArrayRef certArray = CFArrayCreate(kCFAllocatorDefault, (void *)certRefs, 1, NULL);
SecPolicyRef policy = SecPolicyCreateBasicX509();
OSStatus status = SecTrustCreateWithCertificates(certArray, policy, &localTrust);
if (status == errSecSuccess)
localKey = SecTrustCopyPublicKey(localTrust);
CFRelease(localTrust);
CFRelease(policy);
CFRelease(certArray);
if (serverKey != NULL && localKey != NULL && [(__bridge id)serverKey isEqual:(__bridge id)localKey])
return YES;
else
return NO;
}
//DLog(@"Failed: %@",error.localizedDescription);
return NO;
}
关于Objective-C/C 从 SecKeyRef 中提取私钥(模数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29962463/
我是一名优秀的程序员,十分优秀!