From cec0e42699228e99acf4f1416bff0cc04908a39d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Sim=C3=B5es?= Date: Thu, 18 Jul 2019 13:14:48 +0100 Subject: [PATCH] Add X509Certificate2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add X509Certificate2 class. - Bump native version to 100.1.2.0. - Bump version to 1.2.0-preview. Signed-off-by: José Simões --- .../Properties/AssemblyInfo.cs | 2 +- .../System.Net.nfproj | 5 +- .../X509Certificates/X509Certificate2.cs | 197 ++++++++++++++++++ source/version.json | 2 +- 4 files changed, 203 insertions(+), 3 deletions(-) create mode 100644 source/nanoFramework.System.Net/X509Certificates/X509Certificate2.cs diff --git a/source/nanoFramework.System.Net/Properties/AssemblyInfo.cs b/source/nanoFramework.System.Net/Properties/AssemblyInfo.cs index ed2a2b1..a9d0c9d 100644 --- a/source/nanoFramework.System.Net/Properties/AssemblyInfo.cs +++ b/source/nanoFramework.System.Net/Properties/AssemblyInfo.cs @@ -12,7 +12,7 @@ //////////////////////////////////////////////////////////////// // update this whenever the native assembly signature changes // -[assembly: AssemblyNativeVersion("100.1.1.0")] +[assembly: AssemblyNativeVersion("100.1.2.0")] //////////////////////////////////////////////////////////////// // Setting ComVisible to false makes the types in this assembly not visible diff --git a/source/nanoFramework.System.Net/System.Net.nfproj b/source/nanoFramework.System.Net/System.Net.nfproj index d7025a2..b00209c 100644 --- a/source/nanoFramework.System.Net/System.Net.nfproj +++ b/source/nanoFramework.System.Net/System.Net.nfproj @@ -89,6 +89,7 @@ + @@ -101,10 +102,12 @@ ..\packages\nanoFramework.CoreLibrary.1.2.6-preview.16\lib\mscorlib.dll True + True ..\packages\nanoFramework.Runtime.Events.1.0.8-preview.20\lib\nanoFramework.Runtime.Events.dll True + True @@ -120,4 +123,4 @@ - + \ No newline at end of file diff --git a/source/nanoFramework.System.Net/X509Certificates/X509Certificate2.cs b/source/nanoFramework.System.Net/X509Certificates/X509Certificate2.cs new file mode 100644 index 0000000..9ff4e61 --- /dev/null +++ b/source/nanoFramework.System.Net/X509Certificates/X509Certificate2.cs @@ -0,0 +1,197 @@ +// +// Copyright (c) 2019 The nanoFramework project contributors +// Portions Copyright (c) Microsoft Corporation. All rights reserved. +// See LICENSE file in the project root for full license information. +// + +using System.Runtime.CompilerServices; +using System.Text; + +namespace System.Security.Cryptography.X509Certificates +{ + /// + /// Represents an X.509 certificate. + /// + public class X509Certificate2 : X509Certificate + { +#pragma warning disable S3459 // Unassigned members should be removed + // these fields are required and set in native code + private readonly byte[] _privateKey; +#pragma warning restore S3459 // Unassigned members should be removed + + /// + /// Initializes a new instance of the class. + /// + public X509Certificate2() + : base() + { + } + + /// + /// Initializes a new instance of the class using information from a byte array. + /// + /// A byte array containing data from an X.509 certificate. + public X509Certificate2(byte[] rawData) + : base(rawData) + { + } + + /// + /// Initializes a new instance of the class using a byte array and a password. + /// + /// A byte array containing data from an X.509 certificate. + /// The password required to access the X.509 certificate data. + public X509Certificate2(byte[] rawData, string password) + : base(rawData, password) + { + } + + + /// + /// Initializes a new instance of the class using a string with the content of an X.509 certificate. + /// + /// A string containing a X.509 certificate. + /// + /// This methods is exclusive of nanoFramework. The equivalent .NET constructor accepts a file name as the parameter. + /// + public X509Certificate2(string certificate) + : base(certificate) + { + } + + /// + /// Initializes a new instance of the class using a string with the content of an X.509 certificate and a password used to access the certificate. + /// + /// A string containing a X.509 certificate. + /// The password required to access the X.509 certificate data. + /// + /// This methods is exclusive of nanoFramework. The equivalent .NET constructor accepts a file name as the parameter. + /// + public X509Certificate2(string certificate, string password) + : base(certificate, password) + { + } + + /// + /// Initializes a new instance of the class using a string with the content of an X.509 public certificate, the private key and a password used to access the certificate. + /// + /// A string containing a X.509 certificate. + /// A string containing a PEM private key. + /// The password required to access the X.509 certificate data. + /// + /// This methods is exclusive of nanoFramework. There is no equivalent in .NET framework. + /// + public X509Certificate2(string certificate, string key, string password) + : base(certificate, password) + { + var tempKey = Encoding.UTF8.GetBytes(key); + + ////////////////////////////////////////////// + // because this is parsing from a string // + // we need to keep the terminator // + ////////////////////////////////////////////// + var keyBuffer = new byte[tempKey.Length + 1]; + Array.Copy(tempKey, keyBuffer, tempKey.Length); + keyBuffer[keyBuffer.Length - 1] = 0; + + _privateKey = keyBuffer; + + DecodePrivateKeyNative(keyBuffer, password); + } + + /// + /// Initializes a new instance of the class using a string with the content of an X.509 public certificate, the private key and a password used to access the certificate. + /// + /// A byte array containing data from an X.509 certificate. + /// A string containing a PEM private key. + /// The password required to access the X.509 certificate data. + /// + /// This methods is exclusive of nanoFramework. There is no equivalent in .NET framework. + /// + public X509Certificate2(byte[] rawData, string key, string password) + : base(rawData, password) + { + var tempKey = Encoding.UTF8.GetBytes(key); + + ////////////////////////////////////////////// + // because this is parsing from a string // + // we need to keep the terminator // + ////////////////////////////////////////////// + var keyBuffer = new byte[tempKey.Length + 1]; + Array.Copy(tempKey, keyBuffer, tempKey.Length); + keyBuffer[keyBuffer.Length - 1] = 0; + + _privateKey = keyBuffer; + + DecodePrivateKeyNative(keyBuffer, password); + } + + /// + /// Initializes a new instance of the class using a string with the content of an X.509 public certificate, the private key and a password used to access the certificate. + /// + /// A byte array containing data from an X.509 certificate. + /// A byte array containing a PEM private key. + /// The password required to access the X.509 certificate data. + /// + /// This methods is exclusive of nanoFramework. There is no equivalent in .NET framework. + /// + public X509Certificate2(byte[] rawData, byte[] key, string password) + : base(rawData, password) + { + _privateKey = key; + + DecodePrivateKeyNative(key, password); + } + + /// + /// Gets a value that indicates whether an X509Certificate2 object contains a private key. + /// + /// if the object contains a private key; otherwise, . + public bool HasPrivateKey + { + get + { + return (_privateKey != null); + } + } + + /// + /// Gets the date in local time after which a certificate is no longer valid. + /// + /// A object that represents the expiration date for the certificate. + public DateTime NotAfter + { + get + { + return _expirationDate; + } + } + + /// + /// Gets the date in local time on which a certificate becomes valid. + /// + /// A object that represents the effective date of the certificate. + public DateTime NotBefore + { + get + { + return _effectiveDate; + } + } + + /// + /// Gets the raw data of a certificate. + /// + /// The raw data of the certificate as a byte array. + public byte[] RawData + { + get + { + return base.GetRawCertData(); + } + } + + [MethodImpl(MethodImplOptions.InternalCall)] + internal static extern void DecodePrivateKeyNative(byte[] keyBuffer, string password); + } +} diff --git a/source/version.json b/source/version.json index 0467bbb..438790e 100644 --- a/source/version.json +++ b/source/version.json @@ -1,6 +1,6 @@ { "$schema": "https://raw.githubusercontent.com/AArnott/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json", - "version": "1.1.1-preview.{height}", + "version": "1.2.0-preview.{height}", "release": { "branchName" : "release-v{version}", "versionIncrement" : "minor",