Skip to content

Commit

Permalink
Work on update device config
Browse files Browse the repository at this point in the history
- Improve Monitor_UpdateConfiguration command to handle configuration blocks larger then the WP buffer size.
- Add new configuration block to store X509 certificates.

Signed-off-by: José Simões <jose.simoes@eclo.solutions>
  • Loading branch information
josesimoes committed Dec 11, 2018
1 parent d0919cf commit a6fec93
Show file tree
Hide file tree
Showing 9 changed files with 334 additions and 33 deletions.
54 changes: 43 additions & 11 deletions source/USB Test App WPF/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,33 @@ namespace Serial_Test_App_WPF
/// </summary>
public partial class MainWindow : Window
{
// Baltimore CyberTrust Root
// from https://cacert.omniroot.com/bc2025.crt

// X509 RSA key PEM format 2048 bytes
private const string baltimoreCACertificate =
@"-----BEGIN CERTIFICATE-----
MIIDdzCCAl+gAwIBAgIEAgAAuTANBgkqhkiG9w0BAQUFADBaMQswCQYDVQQGEwJJ
RTESMBAGA1UEChMJQmFsdGltb3JlMRMwEQYDVQQLEwpDeWJlclRydXN0MSIwIAYD
VQQDExlCYWx0aW1vcmUgQ3liZXJUcnVzdCBSb290MB4XDTAwMDUxMjE4NDYwMFoX
DTI1MDUxMjIzNTkwMFowWjELMAkGA1UEBhMCSUUxEjAQBgNVBAoTCUJhbHRpbW9y
ZTETMBEGA1UECxMKQ3liZXJUcnVzdDEiMCAGA1UEAxMZQmFsdGltb3JlIEN5YmVy
VHJ1c3QgUm9vdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAKMEuyKr
mD1X6CZymrV51Cni4eiVgLGw41uOKymaZN+hXe2wCQVt2yguzmKiYv60iNoS6zjr
IZ3AQSsBUnuId9Mcj8e6uYi1agnnc+gRQKfRzMpijS3ljwumUNKoUMMo6vWrJYeK
mpYcqWe4PwzV9/lSEy/CG9VwcPCPwBLKBsua4dnKM3p31vjsufFoREJIE9LAwqSu
XmD+tqYF/LTdB1kC1FkYmGP1pWPgkAx9XbIGevOF6uvUA65ehD5f/xXtabz5OTZy
dc93Uk3zyZAsuT3lySNTPx8kmCFcB5kpvcY67Oduhjprl3RjM71oGDHweI12v/ye
jl0qhqdNkNwnGjkCAwEAAaNFMEMwHQYDVR0OBBYEFOWdWTCCR1jMrPoIVDaGezq1
BE3wMBIGA1UdEwEB/wQIMAYBAf8CAQMwDgYDVR0PAQH/BAQDAgEGMA0GCSqGSIb3
DQEBBQUAA4IBAQCFDF2O5G9RaEIFoN27TyclhAO992T9Ldcw46QQF+vaKSm2eT92
9hkTI7gQCvlYpNRhcL0EYWoSihfVCr3FvDB81ukMJY2GQE/szKN+OMY3EU/t3Wgx
jkzSswF07r51XgdIGn9w/xZchMB5hbgF/X++ZRGjD8ACtPhSNzkE1akxehi/oCr0
Epn3o0WC4zxe9Z2etciefC7IpJ5OCBRLbf1wbWsaY71k5h+3zvDyny67G7fyUIhz
ksLi4xaNmjICq44Y3ekQEe5+NauQrz4wlHrQMz2nZQ/1/I6eYs9HRCwBXbsdtTLS
R9I4LtD+gdwyah617jzV/OeBHRnDJELqYzmp
-----END CERTIFICATE-----";

public MainWindow()
{
InitializeComponent();
Expand Down Expand Up @@ -757,10 +784,6 @@ private async void SetDeviceConfigButton_Click(object sender, RoutedEventArgs e)
// get device info
var deviceConfig = device.DebugEngine.GetDeviceConfiguration(cts.Token);

// change device configuration using the global configuration class
//deviceConfig.NetworkConfiguraton.MacAddress = new byte[] { 0, 0x80, 0xe1, 0x01, 0x35, 0x56 };
//deviceConfig.NetworkConfiguraton.StartupAddressMode = DeviceConfiguration.AddressMode.DHCP;

// update new network configuration
DeviceConfiguration.NetworkConfigurationProperties newDeviceNetworkConfiguration = new DeviceConfiguration.NetworkConfigurationProperties
{
Expand All @@ -774,16 +797,25 @@ private async void SetDeviceConfigButton_Click(object sender, RoutedEventArgs e)
// write device configuration to device
var returnValue = device.DebugEngine.UpdateDeviceConfiguration(newDeviceNetworkConfiguration, 0);

// add new wireless 802.11 configuration
DeviceConfiguration.Wireless80211ConfigurationProperties newWireless80211Configuration = new DeviceConfiguration.Wireless80211ConfigurationProperties()
//// add new wireless 802.11 configuration
//DeviceConfiguration.Wireless80211ConfigurationProperties newWireless80211Configuration = new DeviceConfiguration.Wireless80211ConfigurationProperties()
//{
// Id = 44,
// Ssid = "Nice_Ssid",
// Password = "1234",
//};

//// write wireless configuration to device
//returnValue = device.DebugEngine.UpdateDeviceConfiguration(newWireless80211Configuration, 0);

// add CA certificate
DeviceConfiguration.X509CertificateProperties newX509Certificate = new DeviceConfiguration.X509CertificateProperties()
{
Id = 44,
Ssid = "Nice_Ssid",
Password = "1234",
Certificate = Encoding.UTF8.GetBytes(baltimoreCACertificate)
};

// write wireless configuration to device
returnValue = device.DebugEngine.UpdateDeviceConfiguration(newWireless80211Configuration, 0);
// write CA certificate to device
returnValue = device.DebugEngine.UpdateDeviceConfiguration(newX509Certificate, 0);

Debug.WriteLine("");
Debug.WriteLine("");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public partial class DeviceConfiguration
/// </summary>
public static string MarkerConfigurationWireless80211AP_v1 = "AP1\0";

/// <summary>
/// X509 Certificate configuration marker
/// </summary>
public static string MarkerConfigurationX509Certificate_v1 = "XC1\0";

/////////////////////////////////////////////////////////////

/// <summary>
Expand All @@ -47,19 +52,27 @@ public partial class DeviceConfiguration
/// </summary>
public List<Wireless80211ConfigurationProperties> Wireless80211Configurations { get; set; }

/// <summary>
/// Collection of <see cref="Wireless80211ConfigurationProperties"/> blocks in a target device.
/// </summary>
public List<X509CertificateProperties> X509Certificates { get; set; }

public DeviceConfiguration()
: this(new List<NetworkConfigurationProperties>(),
new List<Wireless80211ConfigurationProperties>())
new List<Wireless80211ConfigurationProperties>(),
new List<X509CertificateProperties>())
{
}

public DeviceConfiguration(
List<NetworkConfigurationProperties> networkConfiguratons,
List<Wireless80211ConfigurationProperties> networkWirelessConfiguratons
List<Wireless80211ConfigurationProperties> networkWirelessConfiguratons,
List<X509CertificateProperties> x509Certificates
)
{
NetworkConfigurations = networkConfiguratons;
Wireless80211Configurations = networkWirelessConfiguratons;
X509Certificates = x509Certificates;
}

// operator to allow cast_ing a DeviceConfiguration object to DeviceConfigurationBase
Expand All @@ -68,7 +81,8 @@ public static explicit operator DeviceConfigurationBase(DeviceConfiguration valu
return new DeviceConfigurationBase()
{
NetworkConfigurations = value.NetworkConfigurations.Select(i => (NetworkConfigurationBase)i).ToArray(),
Wireless80211Configurations = value.Wireless80211Configurations.Select(i => (Wireless80211ConfigurationBase)i).ToArray()
Wireless80211Configurations = value.Wireless80211Configurations.Select(i => (Wireless80211ConfigurationBase)i).ToArray(),
X509Certificates = value.X509Certificates.Select(i => (X509CertificateBase)i).ToArray()
};
}

Expand Down Expand Up @@ -245,6 +259,41 @@ public static explicit operator Wireless80211ConfigurationBase(Wireless80211Conf

}


[AddINotifyPropertyChangedInterface]
public class X509CertificateProperties : X509CertificatePropertiesBase
{
public bool IsUnknown { get; set; } = true;

public X509CertificateProperties()
{

}

public X509CertificateProperties(X509CertificateBase certificate)
{
CertificateSize = (uint)certificate.Certificate.Length;
Certificate = certificate.Certificate;

// reset unknown flag
IsUnknown = false;
}

// operator to allow cast_ing a X509CertificateBaseProperties object to X509CertificateBaseBase
public static explicit operator X509CertificateBase(X509CertificateProperties value)
{
var x509Certificate = new X509CertificateBase()
{
Marker = Encoding.UTF8.GetBytes(MarkerConfigurationX509Certificate_v1),

CertificateSize = (uint)value.Certificate.Length,
Certificate = value.Certificate,
};

return x509Certificate;
}
}

/////////////////////////////////////////////////////////////

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ public class DeviceConfigurationBase
public NetworkConfigurationBase[] NetworkConfigurations;

public Wireless80211ConfigurationBase[] Wireless80211Configurations { get; internal set; }

public X509CertificateBase[] X509Certificates { get; internal set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public enum DeviceConfigurationOption : byte
/// </summary>
WirelessNetworkAP = 3,

/// <summary>
/// X509 Certificate block
/// </summary>
X509Certificate = 4,

/// <summary>
/// All configuration blocks
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// Copyright (c) 2018 The nanoFramework project contributors
// See LICENSE file in the project root for full license information.
//

namespace nanoFramework.Tools.Debugger
{
public class X509CertificateBase
{
/// <summary>
/// This is the marker placeholder for this configuration block
/// 4 bytes length.
/// </summary>
public byte[] Marker;

/// <summary>
/// Size of the certificate.
/// </summary>
public uint CertificateSize;

/// <summary>
/// Certificate
/// </summary>
public byte[] Certificate;

public X509CertificateBase()
{
// need to init these here to match the expected size on the struct to be sent to the device
Marker = new byte[4];
Certificate = new byte[64];
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// Copyright (c) 2018 The nanoFramework project contributors
// See LICENSE file in the project root for full license information.
//

using PropertyChanged;

namespace nanoFramework.Tools.Debugger
{
[AddINotifyPropertyChangedInterface]
public class X509CertificatePropertiesBase
{
private byte[] _certificate;

public uint CertificateSize { get; set; }

public byte[] Certificate
{
get => _certificate;
set
{
_certificate = value;
CertificateSize = (uint)value.Length;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -335,26 +335,46 @@ public void PrepareForDeserialize(int size, byte[] data, Converter converter)
Password = new byte[64];
}
}

public class X509CertificateConfig : X509CertificateBase, IConverter
{
public X509CertificateConfig()
{
Marker = new byte[4];
CertificateSize = 0xFFFF;
Certificate = new byte[64];
}

public void PrepareForDeserialize(int size, byte[] data, Converter converter)
{
Marker = new byte[4];
CertificateSize = 0xFFFF;
Certificate = new byte[size - 4 - 4];
}
}
}

public class Monitor_UpdateConfiguration
{
public uint Configuration;
public uint BlockIndex;
public uint Length = 0;
public uint Offset = 0;
public byte[] Data = null;

public class Reply
{
public uint ErrorCode;
};

public void PrepareForSend(byte[] data, int length)
public void PrepareForSend(byte[] data, int length, int offset = 0)
{
Length = (uint)length;
Data = new byte[length];

Array.Copy(data, 0, Data, 0, length);
Offset = (uint)offset;

Array.Copy(data, offset, Data, 0, length);
}
}

Expand Down
Loading

0 comments on commit a6fec93

Please sign in to comment.