如何在XMPPFrameWork中导入证书

时间:2014-07-23 10:48:18

标签: ios xmppframework

我正在我的应用程序中实现XMPP框架。我在服务器(.p12)发送的应用程序包中有一个证书。如何在我的代码中导入它以及如何将它们发送到服务器进行身份验证。

  - (void)xmppStream:(XMPPStream *)sender willSecureWithSettings:(NSMutableDictionary *)settings
{
DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);

NSString *expectedCertName = [xmppStream.myJID domain];
if (expectedCertName)
{
    [settings setObject:expectedCertName forKey:(NSString *)kCFStreamSSLPeerName];
}

if (customCertEvaluation)
{

NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSString *path = [bundle pathForResource:@"TestKey" ofType:@"p12"];
NSData *pkcs12data = [[NSData alloc] initWithContentsOfFile:path];

// Import .p12 data
CFArrayRef keyref = NULL;
OSStatus sanityCheck = SecPKCS12Import((__bridge CFDataRef)pkcs12data,
                                       (__bridge CFDictionaryRef)[NSDictionary
                                                                  dictionaryWithObject:path
                                                                  forKey:(__bridge id)kSecImportExportPassphrase],
                                       &keyref);
if (sanityCheck != noErr) {
    NSLog(@"Error while importing pkcs12 [%d]", (int)sanityCheck);
} else
    NSLog(@"Success opening p12 certificate.");

// Identity
CFDictionaryRef identityDict = CFArrayGetValueAtIndex(keyref, 0);
SecIdentityRef identityRef = (SecIdentityRef)CFDictionaryGetValue(identityDict,
                                                                  kSecImportItemIdentity);

// Cert
SecCertificateRef cert = NULL;
OSStatus status = SecIdentityCopyCertificate(identityRef, &cert);
if (status)
    NSLog(@"SecIdentityCopyCertificate failed.");

// the certificates array, containing the identity then the root certificate
NSArray *myCerts = [[NSArray alloc] initWithObjects:(__bridge id)identityRef, (__bridge id)cert, nil];

      NSMutableDictionary *SSLOptions = [[NSMutableDictionary alloc] init];
    [settings setObject:@(YES) forKey:GCDAsyncSocketManuallyEvaluateTrust];//normal @(yes) in set object
    [SSLOptions setObject:myCerts forKey:(NSString *)kCFStreamSSLCertificates];
   [SSLOptions setObject:[NSNumber numberWithBool:NO] forKey:(NSString *)kCFStreamSSLIsServer];
   [outputStream setProperty:SSLOptions forKey:(__bridge id)kCFStreamPropertySSLSettings];
}
}

这是我的代码,它不起作用。我是新手,所以我不知道它是否正确。任何帮助将不胜感激。

0 个答案:

没有答案