Closed
Description
I have ca.crt, client.crt, client.key
how can I connect mqtt?
self.session = [[MQTTSession alloc] initWithClientId:@"clientid"
userName:@"username"
password:@"password"
keepAlive:60
cleanSession:YES
will:NO
willTopic:nil
willMsg:nil
willQoS:0
willRetainFlag:NO
protocolLevel:4
runLoop:[NSRunLoop currentRunLoop]
forMode:NSRunLoopCommonModes
securityPolicy:[self customSecurityPolicy]
certificates:nil];
[self.session connectToHost:transport.host port:transport.port usingSSL:YES];
- (MQTTSSLSecurityPolicy *)customSecurityPolicy
{
NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"ca" ofType:@"crt"];
NSString *clientcerPath = [[NSBundle mainBundle] pathForResource:@"client" ofType:@"crt"];
NSString *clientkeyPath = [[NSBundle mainBundle] pathForResource:@"client" ofType:@"key"];
NSData *certData = [NSData dataWithContentsOfFile:cerPath];
NSData *certData1 = [NSData dataWithContentsOfFile:clientcerPath];
NSData *certData2 = [NSData dataWithContentsOfFile:clientkeyPath];
MQTTSSLSecurityPolicy *securityPolicy = [MQTTSSLSecurityPolicy policyWithPinningMode:MQTTSSLPinningModeNone];
securityPolicy.allowInvalidCertificates = YES;
securityPolicy.validatesCertificateChain = YES;
securityPolicy.validatesDomainName = NO;
securityPolicy.pinnedCertificates = @[certData,certData1,certData2];
return securityPolicy;
}
right?
Activity
willem4ever commentedon Nov 15, 2017
I have below working for asynchronous connect ...
zhujieshan commentedon Nov 16, 2017
I converted the format of the certificate,and MQTT still can't connect,Are you using 'Two-way certification'? Do not need a client key(client.key)?
zhujieshan commentedon Nov 16, 2017
ca1 and ca2 conversion before what format? p12?
jcavar commentedon Nov 16, 2017
Can you just please use code formatting, it makes it a lot easier to read comments.
willem4ever commentedon Nov 16, 2017
No I do not use a client key, the format of the certificates is openssl der format. Have you checked a valid path is returned. You can also try "securityPolicy.validatesDomainName = NO;" Additionally you check the MQTT side of things by using openssl to connect to the MQTT broker
willem4ever commentedon Nov 16, 2017
Ok to use a client certificate you need to do few more things
attach the client certificate (P12) to the security policy
Do not forget to add the certificates to your bundle .....
zhujieshan commentedon Nov 17, 2017
You really helped me a great favor!This method is useful!Thanks!
NSString* ca = [[NSBundle bundleForClass:[MQTTSession class]] pathForResource:@"ca" ofType:@"der"];
NSString* client = [[NSBundle bundleForClass:[MQTTSession class]] pathForResource:@"certificate" ofType:@"p12"];
MQTTSSLSecurityPolicyTransport *transport = [[MQTTSSLSecurityPolicyTransport alloc]init];
transport.certificates = [MQTTSSLSecurityPolicyTransport clientCertsFromP12:client passphrase:@"password"];
MQTTSSLSecurityPolicy *securityPolicy = [MQTTSSLSecurityPolicy policyWithPinningMode:MQTTSSLPinningModeCertificate];
securityPolicy.pinnedCertificates = @[[NSData dataWithContentsOfFile:ca]];
cjw429672039 commentedon Nov 17, 2017
你好,请教下:
1.你使用的证书是自签证书还是从信任的证书机构购买的证书?
2.证书只能用信任机构颁发的证书吗?
willem4ever commentedon Nov 17, 2017
你好,请教下:
1.你使用的证书是自签证书还是从信任的证书机构购买的证书?
2.证书只能用信任机构颁发的证书吗?
Hello, ask:
I'm using self signed certificates ...
zhujieshan commentedon Nov 18, 2017
我也是使用的是自签证书 后台搞定的
jcavar commentedon Jan 4, 2018
It seems like issue here is resolved but feel free to reopen if not.
MrLinTianbao commentedon Feb 6, 2018
Excuse me, what's going on? Error Domain=NSOSStatusErrorDomain Code=-9807 "(null)" UserInfo={_kCFStreamErrorCodeKey=-9807, _kCFStreamErrorDomainKey=3}
kuangzq commentedon Sep 12, 2018
The framework crashed when connecting to broker with client certificate. My code is
It crashed here in the framework
Some debug information around the crash breakpoint
The version of framework (from Podfile.lock)
Did I miss something or do something wrong? Asking for help. Any advice would be highly appreciated.
marciogranzotto commentedon Dec 20, 2018
Same thing for me. Did you manage to fix it? @kuangzq
ZLDamo commentedon Feb 27, 2020
kuangzq commentedon Feb 27, 2020
ZLDamo commentedon Feb 28, 2020
procjiang commentedon Jan 27, 2021
[MQTTCFSocketTransport] Error while importing pkcs12 为什么会报错啊 ? 我使用的方式有问题吗 ?
NSString *ca = [[NSBundle bundleForClass:[MQTTSession class]] pathForResource:@"cacert" ofType:@"der"];
NSString *client = [[NSBundle bundleForClass:[MQTTSession class]] pathForResource:@"client" ofType:@"p12"];
MQTTSSLSecurityPolicyTransport *transport = [[MQTTSSLSecurityPolicyTransport alloc]init];
transport.host = host;
transport.port = port;
transport.tls = YES;
MQTTSSLSecurityPolicy *securityPolicy = [MQTTSSLSecurityPolicy policyWithPinningMode:MQTTSSLPinningModeCertificate];
securityPolicy.allowInvalidCertificates = YES;
securityPolicy.validatesDomainName = NO;
securityPolicy.validatesCertificateChain = NO;
securityPolicy.pinnedCertificates = @[[NSData dataWithContentsOfFile:ca]];
transport.securityPolicy = securityPolicy;
[self.sessionManager connectTo:host
port:port
tls:YES
keepalive:60
clean:NO
auth:YES
user:[CYUserInfo shareUserInfo].uid
pass:[CYUserInfo shareUserInfo].access_token
will:YES
willTopic:@""
willMsg:nil
willQos:0
willRetainFlag:NO
withClientId:clientId
securityPolicy:securityPolicy
certificates:[MQTTSSLSecurityPolicyTransport clientCertsFromP12:client passphrase:@"password"]];