Skip to content

Commit 754a8f1

Browse files
authored
Add X509Certificate2 (#89)
1 parent 018b371 commit 754a8f1

File tree

4 files changed

+203
-3
lines changed

4 files changed

+203
-3
lines changed

source/nanoFramework.System.Net/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
////////////////////////////////////////////////////////////////
1414
// update this whenever the native assembly signature changes //
15-
[assembly: AssemblyNativeVersion("100.1.1.0")]
15+
[assembly: AssemblyNativeVersion("100.1.2.0")]
1616
////////////////////////////////////////////////////////////////
1717

1818
// Setting ComVisible to false makes the types in this assembly not visible

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
<Compile Include="Sockets\SocketsNative.cs" />
9090
<Compile Include="Sockets\SocketType.cs" />
9191
<Compile Include="X509Certificates\X509Certificate.cs" />
92+
<Compile Include="X509Certificates\X509Certificate2.cs" />
9293
</ItemGroup>
9394
<ItemGroup>
9495
<None Include="key.snk" />
@@ -101,10 +102,12 @@
101102
<Reference Include="mscorlib, Version=1.2.6.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
102103
<HintPath>..\packages\nanoFramework.CoreLibrary.1.2.6-preview.16\lib\mscorlib.dll</HintPath>
103104
<Private>True</Private>
105+
<SpecificVersion>True</SpecificVersion>
104106
</Reference>
105107
<Reference Include="nanoFramework.Runtime.Events, Version=1.0.8.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
106108
<HintPath>..\packages\nanoFramework.Runtime.Events.1.0.8-preview.20\lib\nanoFramework.Runtime.Events.dll</HintPath>
107109
<Private>True</Private>
110+
<SpecificVersion>True</SpecificVersion>
108111
</Reference>
109112
</ItemGroup>
110113
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
@@ -120,4 +123,4 @@
120123
</PropertyGroup>
121124
<Error Condition="!Exists('..\packages\Nerdbank.GitVersioning.3.0.19-beta\build\Nerdbank.GitVersioning.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Nerdbank.GitVersioning.3.0.19-beta\build\Nerdbank.GitVersioning.targets'))" />
122125
</Target>
123-
</Project>
126+
</Project>
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
//
2+
// Copyright (c) 2019 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.Text;
9+
10+
namespace System.Security.Cryptography.X509Certificates
11+
{
12+
/// <summary>
13+
/// Represents an X.509 certificate.
14+
/// </summary>
15+
public class X509Certificate2 : X509Certificate
16+
{
17+
#pragma warning disable S3459 // Unassigned members should be removed
18+
// these fields are required and set in native code
19+
private readonly byte[] _privateKey;
20+
#pragma warning restore S3459 // Unassigned members should be removed
21+
22+
/// <summary>
23+
/// Initializes a new instance of the <see cref="X509Certificate2"/> class.
24+
/// </summary>
25+
public X509Certificate2()
26+
: base()
27+
{
28+
}
29+
30+
/// <summary>
31+
/// Initializes a new instance of the <see cref="X509Certificate2"/> class using information from a byte array.
32+
/// </summary>
33+
/// <param name="rawData">A byte array containing data from an X.509 certificate.</param>
34+
public X509Certificate2(byte[] rawData)
35+
: base(rawData)
36+
{
37+
}
38+
39+
/// <summary>
40+
/// Initializes a new instance of the <see cref="X509Certificate2"/> class using a byte array and a password.
41+
/// </summary>
42+
/// <param name="rawData">A byte array containing data from an X.509 certificate.</param>
43+
/// <param name="password">The password required to access the X.509 certificate data.</param>
44+
public X509Certificate2(byte[] rawData, string password)
45+
: base(rawData, password)
46+
{
47+
}
48+
49+
50+
/// <summary>
51+
/// Initializes a new instance of the <see cref="X509Certificate2"/> class using a string with the content of an X.509 certificate.
52+
/// </summary>
53+
/// <param name="certificate">A string containing a X.509 certificate.</param>
54+
/// <remarks>
55+
/// This methods is exclusive of nanoFramework. The equivalent .NET constructor accepts a file name as the parameter.
56+
/// </remarks>
57+
public X509Certificate2(string certificate)
58+
: base(certificate)
59+
{
60+
}
61+
62+
/// <summary>
63+
/// Initializes a new instance of the <see cref="X509Certificate2"/> class using a string with the content of an X.509 certificate and a password used to access the certificate.
64+
/// </summary>
65+
/// <param name="certificate">A string containing a X.509 certificate.</param>
66+
/// <param name="password">The password required to access the X.509 certificate data.</param>
67+
/// <remarks>
68+
/// This methods is exclusive of nanoFramework. The equivalent .NET constructor accepts a file name as the parameter.
69+
/// </remarks>
70+
public X509Certificate2(string certificate, string password)
71+
: base(certificate, password)
72+
{
73+
}
74+
75+
/// <summary>
76+
/// Initializes a new instance of the <see cref="X509Certificate2"/> class using a string with the content of an X.509 public certificate, the private key and a password used to access the certificate.
77+
/// </summary>
78+
/// <param name="certificate">A string containing a X.509 certificate.</param>
79+
/// <param name="key">A string containing a PEM private key.</param>
80+
/// <param name="password">The password required to access the X.509 certificate data.</param>
81+
/// <remarks>
82+
/// This methods is exclusive of nanoFramework. There is no equivalent in .NET framework.
83+
/// </remarks>
84+
public X509Certificate2(string certificate, string key, string password)
85+
: base(certificate, password)
86+
{
87+
var tempKey = Encoding.UTF8.GetBytes(key);
88+
89+
//////////////////////////////////////////////
90+
// because this is parsing from a string //
91+
// we need to keep the terminator //
92+
//////////////////////////////////////////////
93+
var keyBuffer = new byte[tempKey.Length + 1];
94+
Array.Copy(tempKey, keyBuffer, tempKey.Length);
95+
keyBuffer[keyBuffer.Length - 1] = 0;
96+
97+
_privateKey = keyBuffer;
98+
99+
DecodePrivateKeyNative(keyBuffer, password);
100+
}
101+
102+
/// <summary>
103+
/// Initializes a new instance of the <see cref="X509Certificate2"/> class using a string with the content of an X.509 public certificate, the private key and a password used to access the certificate.
104+
/// </summary>
105+
/// <param name="rawData">A byte array containing data from an X.509 certificate.</param>
106+
/// <param name="key">A string containing a PEM private key.</param>
107+
/// <param name="password">The password required to access the X.509 certificate data.</param>
108+
/// <remarks>
109+
/// This methods is exclusive of nanoFramework. There is no equivalent in .NET framework.
110+
/// </remarks>
111+
public X509Certificate2(byte[] rawData, string key, string password)
112+
: base(rawData, password)
113+
{
114+
var tempKey = Encoding.UTF8.GetBytes(key);
115+
116+
//////////////////////////////////////////////
117+
// because this is parsing from a string //
118+
// we need to keep the terminator //
119+
//////////////////////////////////////////////
120+
var keyBuffer = new byte[tempKey.Length + 1];
121+
Array.Copy(tempKey, keyBuffer, tempKey.Length);
122+
keyBuffer[keyBuffer.Length - 1] = 0;
123+
124+
_privateKey = keyBuffer;
125+
126+
DecodePrivateKeyNative(keyBuffer, password);
127+
}
128+
129+
/// <summary>
130+
/// Initializes a new instance of the <see cref="X509Certificate2"/> class using a string with the content of an X.509 public certificate, the private key and a password used to access the certificate.
131+
/// </summary>
132+
/// <param name="rawData">A byte array containing data from an X.509 certificate.</param>
133+
/// <param name="key">A byte array containing a PEM private key.</param>
134+
/// <param name="password">The password required to access the X.509 certificate data.</param>
135+
/// <remarks>
136+
/// This methods is exclusive of nanoFramework. There is no equivalent in .NET framework.
137+
/// </remarks>
138+
public X509Certificate2(byte[] rawData, byte[] key, string password)
139+
: base(rawData, password)
140+
{
141+
_privateKey = key;
142+
143+
DecodePrivateKeyNative(key, password);
144+
}
145+
146+
/// <summary>
147+
/// Gets a value that indicates whether an X509Certificate2 object contains a private key.
148+
/// </summary>
149+
/// <value><see langword="true"/> if the <see cref="X509Certificate2"/> object contains a private key; otherwise, <see langword="false"/>.</value>
150+
public bool HasPrivateKey
151+
{
152+
get
153+
{
154+
return (_privateKey != null);
155+
}
156+
}
157+
158+
/// <summary>
159+
/// Gets the date in local time after which a certificate is no longer valid.
160+
/// </summary>
161+
/// <value>A <see cref="DateTime"/> object that represents the expiration date for the certificate.</value>
162+
public DateTime NotAfter
163+
{
164+
get
165+
{
166+
return _expirationDate;
167+
}
168+
}
169+
170+
/// <summary>
171+
/// Gets the date in local time on which a certificate becomes valid.
172+
/// </summary>
173+
/// <value>A <see cref="DateTime"/> object that represents the effective date of the certificate.</value>
174+
public DateTime NotBefore
175+
{
176+
get
177+
{
178+
return _effectiveDate;
179+
}
180+
}
181+
182+
/// <summary>
183+
/// Gets the raw data of a certificate.
184+
/// </summary>
185+
/// <value>The raw data of the certificate as a byte array.</value>
186+
public byte[] RawData
187+
{
188+
get
189+
{
190+
return base.GetRawCertData();
191+
}
192+
}
193+
194+
[MethodImpl(MethodImplOptions.InternalCall)]
195+
internal static extern void DecodePrivateKeyNative(byte[] keyBuffer, string password);
196+
}
197+
}

source/version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "https://raw.githubusercontent.com/AArnott/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
3-
"version": "1.1.1-preview.{height}",
3+
"version": "1.2.0-preview.{height}",
44
"release": {
55
"branchName" : "release-v{version}",
66
"versionIncrement" : "minor",

0 commit comments

Comments
 (0)