Skip to content

Commit 29dc167

Browse files
authored
Fix enrolment when using client certificate (#181)
1 parent dfca354 commit 29dc167

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

nanoFramework.Azure.Devices.Client/DeviceClient.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public class DeviceClient : IDisposable
3030
private readonly string _telemetryTopic;
3131
private readonly X509Certificate2 _clientCert;
3232
private readonly string _deviceMessageTopic;
33-
private readonly string _privateKey;
3433
private Twin _twin;
3534
private bool _twinReceived;
3635
private MqttClient _mqttc;
@@ -40,7 +39,7 @@ public class DeviceClient : IDisposable
4039
private readonly object _lock = new object();
4140
private Timer _timerTokenRenew;
4241
private readonly X509Certificate _azureRootCACert;
43-
private bool _isCertificate;
42+
private bool _hasClientCertificate;
4443

4544
/// <summary>
4645
/// Device twin updated event.
@@ -70,9 +69,7 @@ public class DeviceClient : IDisposable
7069
public DeviceClient(string iotHubName, string deviceId, string moduleId, string sasKey, MqttQoSLevel qosLevel = MqttQoSLevel.AtLeastOnce, X509Certificate azureCert = null, string modelId = null)
7170

7271
{
73-
_isCertificate = false;
7472
_clientCert = null;
75-
_privateKey = null;
7673
_iotHubName = iotHubName;
7774
ModelId = modelId;
7875
ModuleId = moduleId;
@@ -109,10 +106,8 @@ public DeviceClient(string iotHubName, string deviceId, string moduleId, string
109106
/// /// <param name="modelId">Azure Plug and Play model ID.</param>
110107
public DeviceClient(string iotHubName, string deviceId, string moduleId, X509Certificate2 clientCert, MqttQoSLevel qosLevel = MqttQoSLevel.AtMostOnce, X509Certificate azureCert = null, string modelId = null)
111108
{
112-
_isCertificate = true;
109+
_hasClientCertificate = true;
113110
_clientCert = clientCert;
114-
// In case we are using the store, the magic should happen automaticall
115-
_privateKey = _clientCert != null ? Convert.ToBase64String(clientCert.PrivateKey) : null;
116111
_iotHubName = iotHubName;
117112
ModelId = modelId;
118113
ModuleId = moduleId;
@@ -224,8 +219,10 @@ public bool Open()
224219

225220
Helper.ComposeTelemetryInformation(ref userName);
226221

222+
// need to compute SHA if not using client certificate
223+
string key = _hasClientCertificate ? null : Helper.GetSharedAccessSignature(null, _sasKey, $"{_iotHubName}/devices/{_deviceId}", new TimeSpan(24, 0, 0));
224+
227225
// Now connect the device
228-
string key = _isCertificate ? _privateKey : Helper.GetSharedAccessSignature(null, _sasKey, $"{_iotHubName}/devices/{_deviceId}", new TimeSpan(24, 0, 0));
229226
_mqttc.Connect(
230227
_deviceId,
231228
userName,

0 commit comments

Comments
 (0)