Skip to content

Commit 4c7a9d1

Browse files
authored
Add CertificateManager class (#51)
1 parent 1686ab8 commit 4c7a9d1

File tree

4 files changed

+80
-17
lines changed

4 files changed

+80
-17
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//
2+
// Copyright (c) 2018 The nanoFramework project contributors
3+
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
4+
// See LICENSE file in the project root for full license information.
5+
//
6+
7+
using System.Runtime.CompilerServices;
8+
using System.Security.Cryptography.X509Certificates;
9+
using System.Text;
10+
11+
namespace System.Net.Security
12+
{
13+
/// <summary>
14+
/// Provides an interface to the device certificate store to manage <see cref="X509Certificate"/>.
15+
/// </summary>
16+
public static class CertificateManager
17+
{
18+
/// <summary>
19+
/// Adds a Certificate Authority Root bundle <see cref="X509Certificate"/> to the store.
20+
/// If there is already a CA Root bundle it will be replaced with this one.
21+
/// </summary>
22+
/// <param name="ca">The Certificate Authority certificate bundle to be added store.</param>
23+
/// <returns>
24+
/// True if the certificate bundle was correctly added to the device certificate store.
25+
/// </returns>
26+
/// <remarks>
27+
/// This method is exclusive of nanoFramework. There is no equivalent in .NET framework.
28+
/// </remarks>
29+
public static bool AddCaCertificateBundle(X509Certificate[] ca)
30+
{
31+
// build a string concatenating all the certificates
32+
StringBuilder bundle = new StringBuilder();
33+
34+
foreach(X509Certificate cert in ca)
35+
{
36+
byte[] certRaw = cert.GetRawCertData();
37+
38+
// remove the terminator from each string
39+
bundle.Append(Encoding.UTF8.GetString(certRaw, 0, certRaw.Length - 1));
40+
}
41+
42+
// add terminator
43+
bundle.Append("\0");
44+
45+
return AddCaCertificateBundle(bundle.ToString());
46+
}
47+
48+
/// <summary>
49+
/// Adds a Certificate Authority Root bundle <see cref="X509Certificate"/> to the store.
50+
/// If there is already a CA Root bundle it will be replaced with this one.
51+
/// </summary>
52+
/// <param name="ca">The Certificate Authority certificate bundle to be added store.</param>
53+
/// <returns>
54+
/// True if the certificate bundle was correctly added to the device certificate store.
55+
/// </returns>
56+
/// <remarks>
57+
/// This method is exclusive of nanoFramework. There is no equivalent in .NET framework.
58+
/// </remarks>
59+
public static bool AddCaCertificateBundle(string ca)
60+
{
61+
return AddCaCertificateBundle(Encoding.UTF8.GetBytes(ca));
62+
}
63+
64+
/// <summary>
65+
/// Adds a Certificate Authority Root bundle <see cref="X509Certificate"/> to the store.
66+
/// If there is already a CA Root bundle it will be replaced with this one.
67+
/// </summary>
68+
/// <param name="ca">The Certificate Authority certificate bundle to be added store.</param>
69+
/// <returns>
70+
/// True if the certificate bundle was correctly added to the device certificate store.
71+
/// </returns>
72+
/// <remarks>
73+
/// This method is exclusive of nanoFramework. There is no equivalent in .NET framework.
74+
/// </remarks>
75+
[MethodImpl(MethodImplOptions.InternalCall)]
76+
public static extern bool AddCaCertificateBundle(byte[] ca);
77+
}
78+
}

source/nanoFramework.System.Net/Security/NetworkSecurity.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,6 @@ internal static class SslNative
7575
[MethodImplAttribute(MethodImplOptions.InternalCall)]
7676
internal static extern int SecureClientInit(int sslProtocols, int sslCertVerify, X509Certificate certificate, X509Certificate ca);
7777

78-
[MethodImplAttribute(MethodImplOptions.InternalCall)]
79-
internal static extern void UpdateCertificates(int contextHandle, X509Certificate certificate, X509Certificate[] ca);
80-
8178
[MethodImplAttribute(MethodImplOptions.InternalCall)]
8279
internal static extern void SecureAccept(int contextHandle, object socket);
8380

source/nanoFramework.System.Net/Security/SslStream.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -113,18 +113,6 @@ public void AuthenticateAsServer(X509Certificate serverCertificate, bool clientC
113113
Authenticate(true, "", null, serverCertificate, sslProtocols);
114114
}
115115

116-
/// <summary>
117-
/// Updates the SSL stack to use updated certificates.
118-
/// </summary>
119-
/// <param name="cert">The personal certificate to update.</param>
120-
/// <param name="ca">The certificate authority certificate to update.</param>
121-
public void UpdateCertificates(X509Certificate cert, X509Certificate[] ca)
122-
{
123-
if(_sslContext == -1) throw new InvalidOperationException();
124-
125-
SslNative.UpdateCertificates(_sslContext, cert, ca);
126-
}
127-
128116
internal void Authenticate(bool isServer, string targetHost, X509Certificate certificate, X509Certificate ca, params SslProtocols[] sslProtocols)
129117
{
130118
SslProtocols vers = (SslProtocols)0;

source/nanoFramework.System.Net/System.Net.nfproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
<Compile Include="NetworkInformation\NetworkInterfaceType.cs" />
6767
<Compile Include="NetworkInformation\Wireless80211Configuration.cs" />
6868
<Compile Include="Properties\AssemblyInfo.cs" />
69+
<Compile Include="Security\CertificateManager.cs" />
6970
<Compile Include="Security\NetworkSecurity.cs" />
7071
<Compile Include="Security\SslStream.cs" />
7172
<Compile Include="SocketAddress.cs" />
@@ -98,7 +99,6 @@
9899
</Reference>
99100
<Reference Include="nanoFramework.Runtime.Events, Version=1.0.2.2, Culture=neutral, PublicKeyToken=c07d481e9758c731">
100101
<HintPath>..\packages\nanoFramework.Runtime.Events.1.0.2\lib\nanoFramework.Runtime.Events.dll</HintPath>
101-
<Private>True</Private>
102102
</Reference>
103103
</ItemGroup>
104104
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
@@ -114,4 +114,4 @@
114114
</PropertyGroup>
115115
<Error Condition="!Exists('..\packages\Nerdbank.GitVersioning.3.0.4-beta\build\Nerdbank.GitVersioning.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Nerdbank.GitVersioning.3.0.4-beta\build\Nerdbank.GitVersioning.targets'))" />
116116
</Target>
117-
</Project>
117+
</Project>

0 commit comments

Comments
 (0)