From 134e53e02c83b74fc0b0ec4081ccb134d15d81a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Sim=C3=B5es?= Date: Wed, 18 May 2022 15:50:30 +0100 Subject: [PATCH] Improvements in X.509 constructors - Now it's possible to create a certificate with just the certificate data and an unencrypted private key. - Improve documentation comments. --- .../X509Certificates/X509Certificate.cs | 8 ++-- .../X509Certificates/X509Certificate2.cs | 38 +++++++++++++------ 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/nanoFramework.System.Net/X509Certificates/X509Certificate.cs b/nanoFramework.System.Net/X509Certificates/X509Certificate.cs index 9776f94..afc9427 100644 --- a/nanoFramework.System.Net/X509Certificates/X509Certificate.cs +++ b/nanoFramework.System.Net/X509Certificates/X509Certificate.cs @@ -4,7 +4,7 @@ // See LICENSE file in the project root for full license information. // -namespace System.Security.Cryptography.X509Certificates +namespace System.Security.Cryptography.X509Certificates { using System; using System.Runtime.CompilerServices; @@ -61,7 +61,7 @@ public X509Certificate() /// ASN.1 DER is the only certificate format supported by this class. /// public X509Certificate(byte[] certificate) - : this(certificate, "") + : this(certificate, null) { } @@ -76,7 +76,7 @@ public X509Certificate(byte[] certificate) public X509Certificate(byte[] certificate, string password) { _certificate = certificate; - _password = password; + _password = password; ParseCertificate(certificate, password, ref _issuer, ref _subject, ref _effectiveDate, ref _expirationDate); } @@ -101,8 +101,6 @@ public X509Certificate(string certificate) Array.Copy(tempCertificate, _certificate, tempCertificate.Length); _certificate[_certificate.Length - 1] = 0; - _password = ""; - ParseCertificate(_certificate, _password, ref _issuer, ref _subject, ref _effectiveDate, ref _expirationDate); } diff --git a/nanoFramework.System.Net/X509Certificates/X509Certificate2.cs b/nanoFramework.System.Net/X509Certificates/X509Certificate2.cs index 09340a6..e835b89 100644 --- a/nanoFramework.System.Net/X509Certificates/X509Certificate2.cs +++ b/nanoFramework.System.Net/X509Certificates/X509Certificate2.cs @@ -15,7 +15,7 @@ namespace System.Security.Cryptography.X509Certificates public class X509Certificate2 : X509Certificate { #pragma warning disable S3459 // Unassigned members should be removed - // these fields are required and set in native code + // field required to be accessible by native code private readonly byte[] _privateKey; #pragma warning restore S3459 // Unassigned members should be removed @@ -25,6 +25,7 @@ public class X509Certificate2 : X509Certificate public X509Certificate2() : base() { + } /// @@ -75,14 +76,19 @@ public X509Certificate2(string certificate, string 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 X.509 certificate. /// A string containing a PEM private key. - /// The password required to access the X.509 certificate data. + /// The password required to access the X.509 certificate data. Set to if the or are not encrypted and do not require a password. /// /// This methods is exclusive of nanoFramework. There is no equivalent in .NET framework. /// - public X509Certificate2(string certificate, string key, string password) - : base(certificate, password) + public X509Certificate2( + string rawData, + string key, + string password) + : base( + rawData, + password) { var tempKey = Encoding.UTF8.GetBytes(key); @@ -104,12 +110,17 @@ public X509Certificate2(string certificate, string key, string password) /// /// 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. + /// The password required to access the X.509 certificate data. Set to if the or are not encrypted and do not require a password. /// /// This methods is exclusive of nanoFramework. There is no equivalent in .NET framework. /// - public X509Certificate2(byte[] rawData, string key, string password) - : base(rawData, password) + public X509Certificate2( + byte[] rawData, + string key, + string password) + : base( + rawData, + password) { var tempKey = Encoding.UTF8.GetBytes(key); @@ -131,12 +142,17 @@ public X509Certificate2(byte[] rawData, string key, string password) /// /// 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. + /// The password required to access the X.509 certificate data. if the or are not encrypted. /// /// This methods is exclusive of nanoFramework. There is no equivalent in .NET framework. /// - public X509Certificate2(byte[] rawData, byte[] key, string password) - : base(rawData, password) + public X509Certificate2( + byte[] rawData, + byte[] key, + string password) + : base( + rawData, + password) { _privateKey = key;