2012-09-10 61 views
5

我正在构建需要.bks密钥库进行身份验证的iPhone应用程序。我没有发现任何有关iOS应用程序的信息。使用。 iPhone应用程序中的bks密钥库证书

我想知道苹果是否允许在他们的应用程序中使用密钥库以及如何在iOS上开始。证书使用BouncyCastle创建。我找到了关于它的信息,但对于iOS我没有运气。任何帮助将不胜感激。

回答

2

你可以导出你的密钥库需要这样

keytool -exportcert -keystore <keystore> -file some.cer 

您可能需要告诉密钥工具有关的存储类型和存储提供商,look here证书(或多个)。

您可以阅读.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); 
} 

当您需要的证书,你可以从钥匙扣得到这样的:

- (SecCertificateRef) getCertFromKeyChain 
{ 
    CFTypeRef ref = NULL; 
    SecItemCopyMatching((__bridge CFDictionaryRef)([self clientCertificateQuery]), &ref); 

    return (SecCertificateRef) ref; 
} 

的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; 
} 

还有一个读取PCKS12存储的功能(您仍然需要将BKS存储转换为该格式)。它被称为SecPKCS12Import,并且您不需要将证书导入到您的iOS钥匙串中。我没有运气,无论如何都需要钥匙链的证书,但这里是something about this

更新:

由于camdaochemgio使用上述方法不推荐,包括一个应用程序,包含机密信息(如私钥)的证书时评论中指出。因为.cer文件不受保护,可以很容易地从.ipa中提取。

PKCS#P12支持密码保护,所以最好使用它。

您可以隐蔽你的密钥库这样的PKCS#P12(采取from here):

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 

然后你就可以加载.p12文件像这样(学分去here

// 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); 
} 

末但并非最不重要的一些有关此主题的链接:

+0

这种方法并不安全,因为该证书文件存储在主束。有些人可能会提取捆绑包,然后无密码地获取文件。作为Android的密钥库更好,但我不知道在iOS中做密钥库。任何想法? –

+0

@camdaochemgio对,我更新了关于如何使用PCKS12商店的帖子。 – sofacoder

相关问题