【发布时间】:2019-02-27 21:15:49
【问题描述】:
我希望使用自签名 x509certificate 进行 TLS 保护,这些应用程序使用 M2MqttDotnetCore 客户端连接到 mosquitto mqtt 代理的许多现有 XamarinForms 应用程序。
为此,我创建了一个简单的示例 XamarinForms 发布/订阅聊天应用程序,以了解如何保护可以在此 GitHub 存储库中正常运行的 XamarinForms mqtt 客户端应用程序。 jhalbrecht/XamarinFormsMqttSample
我在 Mosquitto_pub、python 和一个 .net 控制台应用程序中有示例,它们实现了通过 TLS 和自签名证书通过端口 8883 成功连接到 mosquitto 代理的目标。 XamarinForms UWP 应用也可以在不安全和安全的情况下工作。 我无法让 Android 应用在端口 8883 上使用 TLS,Android 应用确实在端口 1883 上不安全地工作。这是来自 Visual Studio 2017 的运行时日志
[0:] M2Mqtt.Exceptions.MqttConnectionException: Exception connecting to the broker ---> System.AggregateException: One or more errors occurred. ---> System.Security.Authentication.AuthenticationException: A call to SSPI failed, see inner exception. ---> Mono.Btls.MonoBtlsException: Ssl error:1000007d:SSL routines:OPENSSL_internal:CERTIFICATE_VERIFY_FAILED
at /Users/builder/jenkins/workspace/xamarin-android-d15-9/xamarin-android/external/mono/external/boringssl/ssl/handshake_client.c:1132
at Mono.Btls.MonoBtlsContext.ProcessHandshake () [0x00038] in <fb6d78e506844b3b96d5b35aa047fbbd>:0
at Mono.Net.Security.MobileAuthenticatedStream.ProcessHandshake (Mono.Net.Security.AsyncOperationStatus status) [0x0003e] in <fb6d78e506844b3b96d5b35aa047fbbd>:0
at (wrapper remoting-invoke-with-check) Mono.Net.Security.MobileAuthenticatedStream.ProcessHandshake(Mono.Net.Security.AsyncOperationStatus)
at Mono.Net.Security.AsyncHandshakeRequest.Run (Mono.Net.Security.AsyncOperationStatus status) [0x00006] in <fb6d78e506844b3b96d5b35aa047fbbd>:0
at Mono.Net.Security.AsyncProtocolRequest+<ProcessOperation>d__24.MoveNext () [0x000ff] in <fb6d78e506844b3b96d5b35aa047fbbd>:0
--- End of stack trace from previous location where exception was thrown ---
at Mono.Net.Security.AsyncProtocolRequest+<StartOperation>d__23.MoveNext () [0x0008b] in <fb6d78e506844b3b96d5b35aa047fbbd>:0
--- End of inner exception stack trace ---
at Mono.Net.Security.MobileAuthenticatedStream+<ProcessAuthentication>d__47.MoveNext () [0x00254] in <fb6d78e506844b3b96d5b35aa047fbbd>:0
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional (System.Boolean includeTaskCanceledExceptions) [0x00011] in <d4a23bbd2f544c30a48c44dd622ce09f>:0
at System.Threading.Tasks.Task.Wait (System.Int32 millisecondsTimeout, System.Threading.CancellationToken cancellationToken) [0x00043] in <d4a23bbd2f544c30a48c44dd622ce09f>:0
at System.Threading.Tasks.Task.Wait () [0x00000] in <d4a23bbd2f544c30a48c44dd622ce09f>:0
at M2Mqtt.Net.MqttNetworkChannel.Connect () [0x000a8] in <72fbe921f857483bafbb8b397ec98dd1>:0
at M2Mqtt.MqttClient.Connect (System.String clientId, System.String username, System.String password, System.Boolean willRetain, System.Byte willQosLevel, System.Boolean willFlag, System.String willTopic, System.String willMessage, System.Boolean cleanSession, System.UInt16 keepAlivePeriod) [0x0001e] in <72fbe921f857483bafbb8b397ec98dd1>:0
--- End of inner exception stack trace ---
at M2Mqtt.MqttClient.Connect (System.String clientId, System.String username, System.String password, System.Boolean willRetain, System.Byte willQosLevel, System.Boolean willFlag, System.String willTopic, System.String willMessage, System.Boolean cleanSession, System.UInt16 keepAlivePeriod) [0x00037] in <72fbe921f857483bafbb8b397ec98dd1>:0
at M2Mqtt.MqttClient.Connect (System.String clientId) [0x00000] in <72fbe921f857483bafbb8b397ec98dd1>:0
at MqttDataServices.Services.MqttDataService+<Initialize>d__5.MoveNext () [0x00266] in C:\jstuff\MqttSample\MqttDataServices\Services\MqttDataService.cs:183
我目前加载和访问 X509 证书的方式并不安全,也不是最佳做法。有用。我希望最终学习如何访问每个移动平台的设备 ca 密钥库。我使用cross-platform plug-in FilePicker 加载证书,对其进行base64 编码并保存。
FileData fileData = await Plugin.FilePicker.CrossFilePicker.Current.PickFile();
if (fileData == null)
return; // user canceled file picking
string fileName = fileData.FileName;
string content = Convert.ToBase64String(fileData.DataArray, 0, fileData.DataArray.Length,
Base64FormattingOptions.None);
string deviceFileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), fileName);
File.WriteAllText(deviceFileName, content);
我已通过 twitter 联系了一些 Xamarin 人员。我在上面提到的存储库中有一个open issue 讨论我相信来自微软的@baulig 给了我答案的问题,但是我目前不知道如何实现它。
我刚刚查看了证书验证代码,它的作用是 本质上
var certStore = KeyStore.GetInstance ("AndroidCAStore"); certStore.Load(null);这是入口点: https://github.com/mono/mono/blob/master/mcs/class/System/Mono.Btls/MonoBtlsX509LookupAndroid.cs, 它调用此代码 https://github.com/mono/mono/blob/master/mcs/class/System/System/AndroidPlatform.cs#L101 然后在此处调用
xamarin-android代码: https://github.com/xamarin/xamarin-android/blob/master/src/Mono.Android/Android.Runtime/AndroidEnvironment.cs
KeyStore应该是这个类: https://developer.xamarin.com/api/type/Java.Security.KeyStore/.所以你应该可以通过
Java.Security.KeyStore做到这一点。
- 需要在 AndroidManifest.xml 中授予哪些权限?
- 我可以研究哪些术语才能正确访问平台 ca 密钥库?
初次发帖后的补充
- 2019 年 2 月 27 日(MST)下午 2:51
添加了来自 MqttDataService.cs 的证书和 mqtt 客户端创建
X509Certificate caCert = X509Certificate.CreateFromCertFile(Path.Combine(filesDirectoryBasePath, "ca.crt"));
string thePfxPathOnDevice = Path.Combine(filesDirectoryBasePath, "xamarinclient.pfx");
string theBase64EncodedPfx = File.ReadAllText(thePfxPathOnDevice);
byte[] certificate = Convert.FromBase64String(theBase64EncodedPfx);
X509Certificate2 clientCert = new X509Certificate2(certificate, "xamarin");
_client = new MqttClient(
GetHostName(_xpdSetting.MqttBrokerAddress),
Int32.Parse(_xpdSetting.MqttBrokerTlsPort),
_xpdSetting.UseTls,
caCert,
clientCert,
MqttSslProtocols.TLSv1_2
//MyRemoteCertificateValidationCallback
);
【问题讨论】:
-
据我所知@SushiHangover 我将证书和 mqtt 客户端代码添加到原始问题中。
标签: ssl xamarin xamarin.forms mqtt mosquitto