Rad*_*adu 5 iphone bouncycastle objective-c ios
我正在构建一个需要.bks密钥库进行身份验证的iPhone应用程序.我没有找到任何有关iOS应用程序的信息.
我想知道苹果是否允许在他们的应用程序中使用密钥库以及如何开始使用iOS.证书使用BouncyCastle创建.我找不到有关Android的信息,但对于iOS,我没有运气.任何帮助将不胜感激.
您可以像这样从密钥库导出所需的证书
keytool -exportcert -keystore <keystore> -file some.cer
Run Code Online (Sandbox Code Playgroud)
您可能需要告诉 keytool 有关商店类型和商店提供商的信息,请查看此处。
您可以使用以下代码将该 .cer 文件读入 iOS 钥匙串:
- (void) importCertToKeyChain: (NSData *) data
{
// Delete the old certificate, otherwise SecItemAdd complains.
OSStatus oss = SecItemDelete((__bridge CFDictionaryRef)([self clientCertificateQuery]));
// Import the certificate
SecCertificateRef certRef = NULL;
certRef = SecCertificateCreateWithData(NULL, (__bridge CFDataRef)(data));
NSDictionary *att = [NSDictionary dictionaryWithObjectsAndKeys: (__bridge id)(kSecClassCertificate), kSecClass, (__bridge id) certRef, kSecValueRef, nil];
oss = SecItemAdd((__bridge CFDictionaryRef)(att), NULL);
}
Run Code Online (Sandbox Code Playgroud)
当您需要证书时,您可以从钥匙串中获取,如下所示:
- (SecCertificateRef) getCertFromKeyChain
{
CFTypeRef ref = NULL;
SecItemCopyMatching((__bridge CFDictionaryRef)([self clientCertificateQuery]), &ref);
return (SecCertificateRef) ref;
}
Run Code Online (Sandbox Code Playgroud)
clientCertificateQuery 看起来像这样。
static NSString *clientCertSubject = @"TestSubjectClient";
-(NSMutableDictionary *) clientCertificateQuery
{
NSMutableDictionary *query = [[NSMutableDictionary alloc] init];
[query setObject:(__bridge id) kSecClassCertificate forKey:(__bridge id)kSecClass];
[query setObject:clientCertSubject forKey:(__bridge id<NSCopying>)(kSecMatchSubjectContains)];
[query setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecReturnRef];
id)kSecAttrKeyType];
return query;
}
Run Code Online (Sandbox Code Playgroud)
还有一个读取 PCKS12 存储的函数(您仍然需要将 BKS 存储转换为该格式)。它被调用了SecPKCS12Import,有了它你就不需要将证书导入到你的 iOS 钥匙串中。我没有运气,无论如何都需要钥匙串中的证书,但这里有一些关于这个的事情。
更新:
正如 camdaochemgio 在评论中指出的那样,在应用程序中包含包含秘密信息(如私钥)的证书时,不建议使用上述方法。因为 .cer 文件不受保护,可以轻松从 .ipa 中提取。
PKCS#P12 支持密码保护,因此最好使用它。
您可以像这样将密钥库转换为 PKCS#P12(取自此处):
keytool -importkeystore -srckeystore KEYSTORE.jks -destkeystore KEYSTORE.p12 -srcstoretype BKS -deststoretype PKCS12 -srcstorepass mysecret -deststorepass mysecret -srcalias myalias -destalias myalias -srckeypass mykeypass -destkeypass mykeypass -noprompt
Run Code Online (Sandbox Code Playgroud)
然后你可以像这样加载 .p12 文件(积分在此处)
// Load Certificate
NSString *path = [[NSBundle mainBundle] pathForResource:@"cert" ofType:@"p12"];
NSData *p12data = [NSData dataWithContentsOfFile:path];
CFDataRef inP12data = (__bridge CFDataRef)p12data;
// Only password based PKCS#12 blobs are supported
CFStringRef password = CFSTR("Password");
const void *keys[] = { kSecImportExportPassphrase };
const void *values[] = { password };
CFDictionaryRef options = CFDictionaryCreate(NULL, keys, values, 1, NULL, NULL);
// The import
CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL);
OSStatus securityError = SecPKCS12Import(inP12data, options, &items);
if (securityError == 0)
{
// Exploring the content
CFDictionaryRef myIdentityAndTrust = CFArrayGetValueAtIndex(items, 0);
const void *tempIdentity = NULL;
tempIdentity = CFDictionaryGetValue(myIdentityAndTrust, kSecImportItemIdentity);
*identity = (SecIdentityRef)tempIdentity;
const void *tempTrust = NULL;
tempTrust = CFDictionaryGetValue(myIdentityAndTrust, kSecImportItemTrust);
*trust = (SecTrustRef)tempTrust;
}
if (options) {
CFRelease(options);
}
Run Code Online (Sandbox Code Playgroud)
最后但并非最不重要的一些有关此主题的链接:
| 归档时间: |
|
| 查看次数: |
2412 次 |
| 最近记录: |