Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make server certificate validation optional #391

Merged
merged 4 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions source/implementations/f7/Meadow.F7/F7PlatformOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,52 @@ public int[] GetProcessorUtilization()
{
return new[] {100 - Core.Interop.Nuttx.meadow_idle_monitor_get_value()};
}


/// <summary>
/// Enum representing different server certificate validation error returns.
/// </summary>
public enum ServerCertificateValidationError
{
InvalidMode = -1,
CannotChangeAfterInitialization = -2
}

/// <inheritdoc/>
public void SetServerCertificateValidationMode(ServerCertificateValidationMode authmode)
{
Resolver.Log.Trace($"Attempting to set the server certificate validation mode to {authmode}");

int authModeInt = (int)authmode;
if (authModeInt < 0 || authModeInt > Enum.GetNames(typeof(ServerCertificateValidationMode)).Length - 1)
{
string errorMessage = $"Invalid validation mode: {authModeInt}";
Resolver.Log.Error($"Invalid validation mode: {authModeInt}");
throw new ArgumentException(errorMessage);
}

int ret = Core.Interop.Nuttx.mono_mbedtls_set_server_cert_authmode(authModeInt);
if (ret == (int)ServerCertificateValidationError.InvalidMode)
{
string errorMessage = $"Invalid validation mode: {authModeInt}";
Resolver.Log.Error($"Invalid validation mode: {authModeInt}");
throw new ArgumentException(errorMessage);
}
else if (ret == (int)ServerCertificateValidationError.CannotChangeAfterInitialization)
{
string errorMessage = $"The server certificate validation mode cannot be changed after the TLS initialization.";
Resolver.Log.Error(errorMessage);
throw new Exception(errorMessage);
}
else if (ret < 0)
{
string errorMessage = $"Error setting validation mode.";
Resolver.Log.Error(errorMessage);
throw new Exception(errorMessage);
}

Resolver.Log.Trace($"Server certificate validation mode set to {authmode} successfully!");

return;
}
}
1 change: 1 addition & 0 deletions source/implementations/f7/Meadow.F7/Interop/Interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ internal static partial class Interop
public static partial class Nuttx
{
public const string LIBRARY_NAME = "nuttx";
public const string MBEDTLS_LIBRARY_NAME = "mbedtls";
}
}
}
12 changes: 12 additions & 0 deletions source/implementations/f7/Meadow.F7/Interop/Interop.mbedtls.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Runtime.InteropServices;
namespace Meadow.Core;

internal static partial class Interop
{
public static partial class Nuttx
{
[DllImport(MBEDTLS_LIBRARY_NAME, SetLastError = true)]
public static extern int mono_mbedtls_set_server_cert_authmode(int authmode);
}
}
Loading