如何在xmpp IOS中使用SSL证书?

时间:2016-04-07 11:44:12

标签: ios ssl-certificate chat openfire xmppframework

您好我正在使用XMPP Framework,每件事情都运转良好。现在由于安全原因,我需要使用ssl Certificate.But我不知道如何在我的应用程序中实现,即使我不知道SSL证书的基本内容,所以任何正文可以帮助我如何创建SSL。以及如何在ios中的xmpp中实现,我正在使用openfire服务器。

1 个答案:

答案 0 :(得分:0)

  

嘿,我在XMPP中成功实施了SSL,请按照以下步骤操作:

步骤1:

-(BOOL) connect {

    [self setupStream];

    [xmppStream setHostName:xxx];
    [xmppStream setHostPort:zzz];
    [xmppStream setMyJID:yyy];

    NSError *error = nil;

    if (![xmppStream oldSchoolSecureConnectWithTimeout:XMPPStreamTimeoutNone error:&error]) {

    nslog(@"Error %@",error)

        return NO;
    }

    return YES;

}

步骤2:

- (void)setupStream {

    customCertEvaluation = NO;
    allowSelfSignedCertificates = NO;
    allowSSLHostNameMismatch = NO;


    xmppStream = [[XMPPStream alloc] init];
    [xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];

    xmppStream.startTLSPolicy = XMPPStreamStartTLSPolicyPreferred;
    // or
    // xmppStream.startTLSPolicy = XMPPStreamStartTLSPolicyRequired;

}

步骤3:根据您的服务器需求设置标志

 - (void)xmppStream:(XMPPStream *)sender willSecureWithSettings:(NSMutableDictionary *)settings
    {
        NSString *expectedCertName = [xmppStream.myJID domain];

        if (customCertEvaluation){
            [settings setObject:@(YES) forKey:GCDAsyncSocketManuallyEvaluateTrust];
        }
        //    if (allowSelfSignedCertificates){
        //        [settings setObject:[NSNumber numberWithBool:YES] forKey:(NSString *)kCFStreamSSLAllowsAnyRoot];
        //    }
        if (allowSSLHostNameMismatch){
            [settings setObject:[NSNull null] forKey:(NSString *)kCFStreamSSLPeerName];
        }
        else{
            if (expectedCertName)
                [settings setObject:expectedCertName forKey:(NSString *)kCFStreamSSLPeerName];
        }
    }