From c1190b541aabebce864b9ba319a98f1c49b60814 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Sim=C3=B5es?= Date: Wed, 12 Dec 2018 11:03:33 +0000 Subject: [PATCH] Fix X509Certificate constructors from string MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - When parsing a string with the certificate need to keep the terminator in the binary representation. - X509Certificate constructor with password was unnecessarily parsing the string to byte array twice. Signed-off-by: José Simões --- .../X509Certificates/X509Certificate.cs | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/source/nanoFramework.System.Net/X509Certificates/X509Certificate.cs b/source/nanoFramework.System.Net/X509Certificates/X509Certificate.cs index e130236..b6da0b3 100644 --- a/source/nanoFramework.System.Net/X509Certificates/X509Certificate.cs +++ b/source/nanoFramework.System.Net/X509Certificates/X509Certificate.cs @@ -91,7 +91,16 @@ public X509Certificate(byte[] certificate, string password) /// public X509Certificate(string certificate) { - _certificate = Encoding.UTF8.GetBytes(certificate); + var tempCertificate = Encoding.UTF8.GetBytes(certificate); + + ////////////////////////////////////////////// + // because this is parsing from a string // + // we need to keep the terminator // + ////////////////////////////////////////////// + _certificate = new byte[tempCertificate.Length + 1]; + Array.Copy(tempCertificate, _certificate, tempCertificate.Length); + _certificate[_certificate.Length - 1] = 0; + _password = ""; ParseCertificate(_certificate, _password, ref _issuer, ref _subject, ref _effectiveDate, ref _expirationDate); @@ -108,10 +117,19 @@ public X509Certificate(string certificate) /// public X509Certificate(string certificate, string password) { - _certificate = Encoding.UTF8.GetBytes(certificate); _password = password; - ParseCertificate(Encoding.UTF8.GetBytes(certificate), _password, ref _issuer, ref _subject, ref _effectiveDate, ref _expirationDate); + var tempCertificate = Encoding.UTF8.GetBytes(certificate); + + ////////////////////////////////////////////// + // because this is parsing from a string // + // we need to keep the terminator // + ////////////////////////////////////////////// + _certificate = new byte[tempCertificate.Length + 1]; + Array.Copy(tempCertificate, _certificate, tempCertificate.Length); + _certificate[_certificate.Length - 1] = 0; + + ParseCertificate(_certificate, _password, ref _issuer, ref _subject, ref _effectiveDate, ref _expirationDate); } ///