Azure / azure-iot-protocol-gateway

Azure IoT protocol gateway enables protocol translation for Azure IoT Hub
Other
225 stars 151 forks source link

how pass X509Certificate2 to potocol gateway #93

Open zxarcg opened 7 years ago

zxarcg commented 7 years ago

my device is in internal network, and only through internal protocol gateway to access iot hub. every device has X509Certificate2 , how can i pass this X509Certificate2 to gateway ,then gateway act as proxy to use these X509Certificate2 to authen toward iot hub ?

protocol gateway default support sastoken, i used the following code, it work well, but i have choice to pass X509Certificate2 to protocol gateway ? i don't want to modify any device client relatived code, i want to let device client to keep transpent(regardless protocol gateway or iot hub)

sample code as followed:

private const string DeviceConnectionString = "GatewayHostName=127.0.0.1;HostName=xxx.azure-devices.cn;DeviceId=xxx;SharedAccessKey=xxx";

var x509Certificate = new X509Certificate2(@"E:\xxx.cer"); var setting = new MqttTransportSettings(TransportType.Mqtt_Tcp_Only); setting.RemoteCertificateValidationCallback += (sender, cert, chain, error) => { return true; }; setting.ClientCertificate = x509Certificate; var settings = new ITransportSettings[] { setting };

            DeviceClient deviceClient = DeviceClient.CreateFromConnectionString(DeviceConnectionString, settings);

deviceClient.OpenAsync(); deviceClient.SendEvent(...);

zxarcg commented 7 years ago

i can pass certificate by willmessage in connectpacket, but i don't like this approch, anything else? code as followed:

var x509Certificate = new X509Certificate2(@"E:\LLNC1AAA0EA001030.cer"); var buf = x509Certificate.GetRawCertData(); var setting = new MqttTransportSettings(TransportType.Mqtt_Tcp_Only); setting.RemoteCertificateValidationCallback += (sender, cert, chain, error) => { return true; }; setting.HasWill = true; setting.WillMessage = new WillMessage(QualityOfService.AtMostOnce, new Message(buf));

nayato commented 7 years ago

there's no good way to pass-through client certificate. Either use Hub-scoped credentials on the way to backend or if you can, store client certs locally on PG nodes for lookup/pull for forwarding.