Blame SOURCES/0001-NetFX-compatibility-fixes-for-X500DistinguishedName.patch

105ad9
From 7f5ccbde792c7fd94ec27c33861ae1c3118ecf93 Mon Sep 17 00:00:00 2001
105ad9
From: Filip Navara <filip.navara@gmail.com>
105ad9
Date: Sun, 24 Jun 2018 06:29:39 +0200
105ad9
Subject: [PATCH 1/7] NetFX compatibility fixes for X500DistinguishedName.
105ad9
105ad9
* Don't write a separator after the empty DN
105ad9
* Make T61String behave like it does on Windows (UTF-8 with a Latin-1 fallback)
105ad9
* Use the managed decoder on Linux, instead of a lot of P/Invokes back into OpenSSL.
105ad9
---
105ad9
 .../Interop.ASN1.cs                           |  27 ----
105ad9
 .../Interop.X509Name.cs                       |  39 ------
105ad9
 .../Interop.X509NameEntry.cs                  |  61 ---------
105ad9
 .../SafeHandles/SafeX509NameHandle.Unix.cs    |  30 -----
105ad9
 .../Cryptography/DerSequenceReader.cs         |  35 +++++
105ad9
 .../pal_x509_name.cpp                         |   5 +
105ad9
 .../pal_x509_name.h                           |   5 +
105ad9
 .../src/System.Net.Http.csproj                |   3 -
105ad9
 .../src/System.Net.Security.csproj            |   3 -
105ad9
 ...em.Security.Cryptography.Algorithms.csproj |   1 +
105ad9
 ...stem.Security.Cryptography.Encoding.csproj |   1 +
105ad9
 ...ystem.Security.Cryptography.OpenSsl.csproj |   1 +
105ad9
 .../Pal.Unix/X500NameEncoder.ManagedDecode.cs |   4 +-
105ad9
 .../Pal.Unix/X500NameEncoder.OpenSslDecode.cs | 123 ------------------
105ad9
 .../Cryptography/Pal.Unix/X500NameEncoder.cs  |   7 +-
105ad9
 ...urity.Cryptography.X509Certificates.csproj |  10 +-
105ad9
 .../tests/X500DistinguishedNameTests.cs       |  69 ++++++++++
105ad9
 17 files changed, 125 insertions(+), 299 deletions(-)
105ad9
 delete mode 100644 src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.X509NameEntry.cs
105ad9
 delete mode 100644 src/Common/src/Microsoft/Win32/SafeHandles/SafeX509NameHandle.Unix.cs
105ad9
 delete mode 100644 src/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/X500NameEncoder.OpenSslDecode.cs
105ad9
105ad9
diff --git a/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.ASN1.cs b/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.ASN1.cs
105ad9
index 962eaa426f..5bd2e8b9b4 100644
105ad9
--- a/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.ASN1.cs
105ad9
+++ b/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.ASN1.cs
105ad9
@@ -61,22 +61,6 @@ internal static partial class Interop
105ad9
         [DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_Asn1StringFree")]
105ad9
         internal static extern void Asn1StringFree(IntPtr o);
105ad9
 
105ad9
-        internal static string GetOidValue(SafeSharedAsn1ObjectHandle asn1Object)
105ad9
-        {
105ad9
-            Debug.Assert(asn1Object != null);
105ad9
-
105ad9
-            bool added = false;
105ad9
-            asn1Object.DangerousAddRef(ref added);
105ad9
-            try
105ad9
-            {
105ad9
-                return GetOidValue(asn1Object.DangerousGetHandle());
105ad9
-            }
105ad9
-            finally
105ad9
-            {
105ad9
-                asn1Object.DangerousRelease();
105ad9
-            }
105ad9
-        }
105ad9
-
105ad9
         internal static unsafe string GetOidValue(IntPtr asn1ObjectPtr)
105ad9
         {
105ad9
             // OBJ_obj2txt returns the number of bytes that should have been in the answer, but it does not accept
105ad9
@@ -127,14 +111,3 @@ internal static partial class Interop
105ad9
         }
105ad9
     }
105ad9
 }
105ad9
-
105ad9
-namespace Microsoft.Win32.SafeHandles
105ad9
-{
105ad9
-    internal class SafeSharedAsn1ObjectHandle : SafeInteriorHandle
105ad9
-    {
105ad9
-        private SafeSharedAsn1ObjectHandle() :
105ad9
-            base(IntPtr.Zero, ownsHandle: true)
105ad9
-        {
105ad9
-        }
105ad9
-    }
105ad9
-}
105ad9
diff --git a/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.X509Name.cs b/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.X509Name.cs
105ad9
index 0f0fecad24..70097d7db2 100644
105ad9
--- a/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.X509Name.cs
105ad9
+++ b/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.X509Name.cs
105ad9
@@ -15,16 +15,6 @@ internal static partial class Interop
105ad9
         [DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_GetX509NameStackFieldCount")]
105ad9
         internal static extern int GetX509NameStackFieldCount(SafeSharedX509NameStackHandle sk);
105ad9
 
105ad9
-        [DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_PushX509NameStackField")]
105ad9
-        [return: MarshalAs(UnmanagedType.Bool)]
105ad9
-        internal static extern bool PushX509NameStackField(SafeX509NameStackHandle stack, SafeX509NameHandle x509_Name);
105ad9
-
105ad9
-        [DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_RecursiveFreeX509NameStack")]
105ad9
-        internal static extern void RecursiveFreeX509NameStack(IntPtr stack);
105ad9
-
105ad9
-        [DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_NewX509NameStack")]
105ad9
-        internal static extern SafeX509NameStackHandle NewX509NameStack();
105ad9
-
105ad9
         [DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_GetX509NameStackField")]
105ad9
         private static extern SafeSharedX509NameHandle GetX509NameStackField_private(SafeSharedX509NameStackHandle sk,
105ad9
             int loc);
105ad9
@@ -32,15 +22,6 @@ internal static partial class Interop
105ad9
         [DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_GetX509NameRawBytes")]
105ad9
         private static extern int GetX509NameRawBytes(SafeSharedX509NameHandle x509Name, byte[] buf, int cBuf);
105ad9
 
105ad9
-        [DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_DecodeX509Name")]
105ad9
-        internal static extern SafeX509NameHandle DecodeX509Name(byte[] buf, int len);
105ad9
-
105ad9
-        [DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_X509NameDestroy")]
105ad9
-        internal static extern void X509NameDestroy(IntPtr a);
105ad9
-
105ad9
-        [DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_GetX509NameEntryCount")]
105ad9
-        internal static extern int GetX509NameEntryCount(SafeX509NameHandle x509Name);
105ad9
-
105ad9
         internal static X500DistinguishedName LoadX500Name(SafeSharedX509NameHandle namePtr)
105ad9
         {
105ad9
             CheckValidOpenSslHandle(namePtr);
105ad9
@@ -86,25 +67,5 @@ namespace Microsoft.Win32.SafeHandles
105ad9
         {
105ad9
         }
105ad9
     }
105ad9
-
105ad9
-    internal sealed class SafeX509NameStackHandle : SafeHandle
105ad9
-    {
105ad9
-        private SafeX509NameStackHandle() :
105ad9
-            base(IntPtr.Zero, ownsHandle: true)
105ad9
-        {
105ad9
-        }
105ad9
-
105ad9
-        protected override bool ReleaseHandle()
105ad9
-        {
105ad9
-            Interop.Crypto.RecursiveFreeX509NameStack(handle);
105ad9
-            SetHandle(IntPtr.Zero);
105ad9
-            return true;
105ad9
-        }
105ad9
-
105ad9
-        public override bool IsInvalid
105ad9
-        {
105ad9
-            get { return handle == IntPtr.Zero; }
105ad9
-        }
105ad9
-    }
105ad9
 }
105ad9
 
105ad9
diff --git a/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.X509NameEntry.cs b/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.X509NameEntry.cs
105ad9
deleted file mode 100644
105ad9
index eeeea54d0c..0000000000
105ad9
--- a/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.X509NameEntry.cs
105ad9
+++ /dev/null
105ad9
@@ -1,61 +0,0 @@
105ad9
-// Licensed to the .NET Foundation under one or more agreements.
105ad9
-// The .NET Foundation licenses this file to you under the MIT license.
105ad9
-// See the LICENSE file in the project root for more information.
105ad9
-
105ad9
-using System;
105ad9
-using System.Runtime.InteropServices;
105ad9
-using Microsoft.Win32.SafeHandles;
105ad9
-
105ad9
-internal static partial class Interop
105ad9
-{
105ad9
-    internal static partial class Crypto
105ad9
-    {
105ad9
-        [DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_GetX509NameEntry")]
105ad9
-        private static extern SafeSharedX509NameEntryHandle GetX509NameEntry_private(SafeX509NameHandle x509Name, int loc);
105ad9
-
105ad9
-        [DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_GetX509NameEntryOid")]
105ad9
-        private static extern SafeSharedAsn1ObjectHandle GetX509NameEntryOid_private(SafeSharedX509NameEntryHandle nameEntry);
105ad9
-
105ad9
-        [DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_GetX509NameEntryData")]
105ad9
-        private static extern SafeSharedAsn1StringHandle GetX509NameEntryData_private(SafeSharedX509NameEntryHandle nameEntry);
105ad9
-
105ad9
-        internal static SafeSharedX509NameEntryHandle GetX509NameEntry(SafeX509NameHandle x509Name, int loc)
105ad9
-        {
105ad9
-            CheckValidOpenSslHandle(x509Name);
105ad9
-
105ad9
-            return SafeInteriorHandle.OpenInteriorHandle(
105ad9
-                (nameHandle, i) => GetX509NameEntry_private(nameHandle, i),
105ad9
-                x509Name,
105ad9
-                loc);
105ad9
-        }
105ad9
-
105ad9
-        internal static SafeSharedAsn1ObjectHandle GetX509NameEntryOid(SafeSharedX509NameEntryHandle nameEntry)
105ad9
-        {
105ad9
-            CheckValidOpenSslHandle(nameEntry);
105ad9
-
105ad9
-            return SafeInteriorHandle.OpenInteriorHandle(
105ad9
-                handle => GetX509NameEntryOid_private(handle),
105ad9
-                nameEntry);
105ad9
-        }
105ad9
-
105ad9
-        internal static SafeSharedAsn1StringHandle GetX509NameEntryData(SafeSharedX509NameEntryHandle nameEntry)
105ad9
-        {
105ad9
-            CheckValidOpenSslHandle(nameEntry);
105ad9
-
105ad9
-            return SafeInteriorHandle.OpenInteriorHandle(
105ad9
-                handle => GetX509NameEntryData_private(handle),
105ad9
-                nameEntry);
105ad9
-        }
105ad9
-    }
105ad9
-}
105ad9
-
105ad9
-namespace Microsoft.Win32.SafeHandles
105ad9
-{
105ad9
-    internal sealed class SafeSharedX509NameEntryHandle : SafeInteriorHandle
105ad9
-    {
105ad9
-        private SafeSharedX509NameEntryHandle() :
105ad9
-            base(IntPtr.Zero, ownsHandle: true)
105ad9
-        {
105ad9
-        }
105ad9
-    }
105ad9
-}
105ad9
diff --git a/src/Common/src/Microsoft/Win32/SafeHandles/SafeX509NameHandle.Unix.cs b/src/Common/src/Microsoft/Win32/SafeHandles/SafeX509NameHandle.Unix.cs
105ad9
deleted file mode 100644
105ad9
index ae22b92303..0000000000
105ad9
--- a/src/Common/src/Microsoft/Win32/SafeHandles/SafeX509NameHandle.Unix.cs
105ad9
+++ /dev/null
105ad9
@@ -1,30 +0,0 @@
105ad9
-// Licensed to the .NET Foundation under one or more agreements.
105ad9
-// The .NET Foundation licenses this file to you under the MIT license.
105ad9
-// See the LICENSE file in the project root for more information.
105ad9
-
105ad9
-using System;
105ad9
-using System.Security;
105ad9
-using System.Runtime.InteropServices;
105ad9
-
105ad9
-namespace Microsoft.Win32.SafeHandles
105ad9
-{
105ad9
-    internal sealed class SafeX509NameHandle : SafeHandle
105ad9
-    {
105ad9
-        private SafeX509NameHandle() :
105ad9
-            base(IntPtr.Zero, ownsHandle: true)
105ad9
-        {
105ad9
-        }
105ad9
-
105ad9
-        protected override bool ReleaseHandle()
105ad9
-        {
105ad9
-            Interop.Crypto.X509NameDestroy(handle);
105ad9
-            SetHandle(IntPtr.Zero);
105ad9
-            return true;
105ad9
-        }
105ad9
-
105ad9
-        public override bool IsInvalid
105ad9
-        {
105ad9
-            get { return handle == IntPtr.Zero; }
105ad9
-        }
105ad9
-    }
105ad9
-}
105ad9
diff --git a/src/Common/src/System/Security/Cryptography/DerSequenceReader.cs b/src/Common/src/System/Security/Cryptography/DerSequenceReader.cs
105ad9
index b68c9da093..68d9ac1bd2 100644
105ad9
--- a/src/Common/src/System/Security/Cryptography/DerSequenceReader.cs
105ad9
+++ b/src/Common/src/System/Security/Cryptography/DerSequenceReader.cs
105ad9
@@ -30,6 +30,9 @@ namespace System.Security.Cryptography
105ad9
 
105ad9
         internal static DateTimeFormatInfo s_validityDateTimeFormatInfo;
105ad9
 
105ad9
+        private static System.Text.Encoding s_utf8EncodingWithExceptionFallback;
105ad9
+        private static System.Text.Encoding s_latin1Encoding;
105ad9
+
105ad9
         private readonly byte[] _data;
105ad9
         private readonly int _end;
105ad9
         private int _position;
105ad9
@@ -383,6 +386,38 @@ namespace System.Security.Cryptography
105ad9
             return TrimTrailingNulls(ia5String);
105ad9
         }
105ad9
 
105ad9
+        internal string ReadT61String()
105ad9
+        {
105ad9
+            EatTag(DerTag.T61String);
105ad9
+            int contentLength = EatLength();
105ad9
+            string t61String;
105ad9
+
105ad9
+            // Technically the T.61 encoding (code page 20261) should be used here, but many
105ad9
+            // implementations don't follow that and use different character sets. CryptoAPI
105ad9
+            // on NetFX seems to interpret it as UTF-8 with fallback to ISO 8859-1. OpenSSL
105ad9
+            // seems to interpret it as ISO 8859-1 with no support for UTF-8.
105ad9
+            // https://github.com/dotnet/corefx/issues/27466
105ad9
+
105ad9
+            System.Text.Encoding utf8EncodingWithExceptionFallback = LazyInitializer.EnsureInitialized(
105ad9
+                ref s_utf8EncodingWithExceptionFallback,
105ad9
+                () => new UTF8Encoding(false, true));
105ad9
+            System.Text.Encoding latin1Encoding = LazyInitializer.EnsureInitialized(
105ad9
+                ref s_latin1Encoding,
105ad9
+                () => System.Text.Encoding.GetEncoding("iso-8859-1"));
105ad9
+            
105ad9
+            try
105ad9
+            {
105ad9
+                t61String = utf8EncodingWithExceptionFallback.GetString(_data, _position, contentLength);
105ad9
+            }
105ad9
+            catch (DecoderFallbackException)
105ad9
+            {
105ad9
+                t61String = latin1Encoding.GetString(_data, _position, contentLength);
105ad9
+            }
105ad9
+            _position += contentLength;
105ad9
+
105ad9
+            return TrimTrailingNulls(t61String);
105ad9
+        }
105ad9
+
105ad9
         internal DateTime ReadX509Date()
105ad9
         {
105ad9
             byte tag = PeekTag();
105ad9
diff --git a/src/Native/Unix/System.Security.Cryptography.Native/pal_x509_name.cpp b/src/Native/Unix/System.Security.Cryptography.Native/pal_x509_name.cpp
105ad9
index 2195b74906..02cbfee4a2 100644
105ad9
--- a/src/Native/Unix/System.Security.Cryptography.Native/pal_x509_name.cpp
105ad9
+++ b/src/Native/Unix/System.Security.Cryptography.Native/pal_x509_name.cpp
105ad9
@@ -14,6 +14,11 @@ extern "C" X509_NAME* CryptoNative_GetX509NameStackField(X509NameStack* sk, int3
105ad9
     return sk_X509_NAME_value(sk, loc);
105ad9
 }
105ad9
 
105ad9
+/*
105ad9
+ * The following methods are kept around for compatiblity during servicing.
105ad9
+ * They are not used.
105ad9
+ */
105ad9
+
105ad9
 extern "C" X509_NAME* CryptoNative_DecodeX509Name(const uint8_t* buf, int32_t len)
105ad9
 {
105ad9
     if (!buf || !len)
105ad9
diff --git a/src/Native/Unix/System.Security.Cryptography.Native/pal_x509_name.h b/src/Native/Unix/System.Security.Cryptography.Native/pal_x509_name.h
105ad9
index 76a77ccc1f..b24a794bbd 100644
105ad9
--- a/src/Native/Unix/System.Security.Cryptography.Native/pal_x509_name.h
105ad9
+++ b/src/Native/Unix/System.Security.Cryptography.Native/pal_x509_name.h
105ad9
@@ -18,6 +18,11 @@ Direct shim to sk_X509_NAME_value
105ad9
 */
105ad9
 extern "C" X509_NAME* CryptoNative_GetX509NameStackField(X509NameStack* sk, int32_t loc);
105ad9
 
105ad9
+/*
105ad9
+ * The following methods are kept around for compatiblity during servicing.
105ad9
+ * They are not used.
105ad9
+ */
105ad9
+
105ad9
 /*
105ad9
 Shims the d2i_X509_NAME method and makes it easier to invoke from managed code.
105ad9
 */
105ad9
diff --git a/src/System.Net.Http/src/System.Net.Http.csproj b/src/System.Net.Http/src/System.Net.Http.csproj
105ad9
index 3fa7cdd26f..66e5b8d5f8 100644
105ad9
--- a/src/System.Net.Http/src/System.Net.Http.csproj
105ad9
+++ b/src/System.Net.Http/src/System.Net.Http.csproj
105ad9
@@ -530,9 +530,6 @@
105ad9
     <Compile Include="$(CommonPath)\Microsoft\Win32\SafeHandles\SafeX509Handles.Unix.cs">
105ad9
       <Link>Common\Microsoft\Win32\SafeHandles\SafeX509Handles.Unix.cs</Link>
105ad9
     </Compile>
105ad9
-    <Compile Include="$(CommonPath)\Microsoft\Win32\SafeHandles\SafeX509NameHandle.Unix.cs">
105ad9
-      <Link>Common\Microsoft\Win32\SafeHandles\SafeX509NameHandle.Unix.cs</Link>
105ad9
-    </Compile>
105ad9
     <Compile Include="$(CommonPath)\Microsoft\Win32\SafeHandles\X509ExtensionSafeHandles.Unix.cs">
105ad9
       <Link>Common\Microsoft\Win32\SafeHandles\X509ExtensionSafeHandles.Unix.cs</Link>
105ad9
     </Compile>
105ad9
diff --git a/src/System.Net.Security/src/System.Net.Security.csproj b/src/System.Net.Security/src/System.Net.Security.csproj
105ad9
index 9dff20160e..bf8d773a4e 100644
105ad9
--- a/src/System.Net.Security/src/System.Net.Security.csproj
105ad9
+++ b/src/System.Net.Security/src/System.Net.Security.csproj
105ad9
@@ -328,9 +328,6 @@
105ad9
     <Compile Include="$(CommonPath)\Microsoft\Win32\SafeHandles\SafeX509Handles.Unix.cs">
105ad9
       <Link>Common\Microsoft\Win32\SafeHandles\SafeX509Handles.Unix.cs</Link>
105ad9
     </Compile>
105ad9
-    <Compile Include="$(CommonPath)\Microsoft\Win32\SafeHandles\SafeX509NameHandle.Unix.cs">
105ad9
-      <Link>Common\Microsoft\Win32\SafeHandles\SafeX509NameHandle.Unix.cs</Link>
105ad9
-    </Compile>
105ad9
     <Compile Include="$(CommonPath)\Microsoft\Win32\SafeHandles\X509ExtensionSafeHandles.Unix.cs">
105ad9
       <Link>Common\Microsoft\Win32\SafeHandles\X509ExtensionSafeHandles.Unix.cs</Link>
105ad9
     </Compile>
105ad9
diff --git a/src/System.Security.Cryptography.Algorithms/src/System.Security.Cryptography.Algorithms.csproj b/src/System.Security.Cryptography.Algorithms/src/System.Security.Cryptography.Algorithms.csproj
105ad9
index 9b1cb26508..fbe6f4c269 100644
105ad9
--- a/src/System.Security.Cryptography.Algorithms/src/System.Security.Cryptography.Algorithms.csproj
105ad9
+++ b/src/System.Security.Cryptography.Algorithms/src/System.Security.Cryptography.Algorithms.csproj
105ad9
@@ -558,6 +558,7 @@
105ad9
     <Reference Include="System.Security.Cryptography.Encoding" />
105ad9
     <Reference Include="System.Security.Cryptography.Primitives" />
105ad9
     <Reference Include="System.Threading" />
105ad9
+    <Reference Include="System.Text.Encoding.Extensions" />
105ad9
   </ItemGroup>
105ad9
   <ItemGroup Condition="'$(TargetsUnix)' == 'true'">
105ad9
     <Reference Include="System.Runtime.Numerics" />
105ad9
diff --git a/src/System.Security.Cryptography.Encoding/src/System.Security.Cryptography.Encoding.csproj b/src/System.Security.Cryptography.Encoding/src/System.Security.Cryptography.Encoding.csproj
105ad9
index 9681ef62e4..388d4f36a6 100644
105ad9
--- a/src/System.Security.Cryptography.Encoding/src/System.Security.Cryptography.Encoding.csproj
105ad9
+++ b/src/System.Security.Cryptography.Encoding/src/System.Security.Cryptography.Encoding.csproj
105ad9
@@ -106,6 +106,7 @@
105ad9
     <Reference Include="System.Runtime.Extensions" />
105ad9
     <Reference Include="System.Runtime.InteropServices" />
105ad9
     <Reference Include="System.Security.Cryptography.Primitives" />
105ad9
+    <Reference Include="System.Text.Encoding.Extensions" />
105ad9
   </ItemGroup>
105ad9
   <ItemGroup Condition=" '$(TargetsOSX)' == 'true' ">
105ad9
     <Reference Include="System.Runtime.Numerics" />
105ad9
diff --git a/src/System.Security.Cryptography.OpenSsl/src/System.Security.Cryptography.OpenSsl.csproj b/src/System.Security.Cryptography.OpenSsl/src/System.Security.Cryptography.OpenSsl.csproj
105ad9
index 095d62bc6d..3846bcad23 100644
105ad9
--- a/src/System.Security.Cryptography.OpenSsl/src/System.Security.Cryptography.OpenSsl.csproj
105ad9
+++ b/src/System.Security.Cryptography.OpenSsl/src/System.Security.Cryptography.OpenSsl.csproj
105ad9
@@ -160,6 +160,7 @@
105ad9
     <Reference Include="System.Security.Cryptography.Algorithms" />
105ad9
     <Reference Include="System.Security.Cryptography.Encoding" />
105ad9
     <Reference Include="System.Security.Cryptography.Primitives" />
105ad9
+    <Reference Include="System.Text.Encoding.Extensions" />
105ad9
     <Reference Include="System.Threading" />
105ad9
   </ItemGroup>
105ad9
   <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
105ad9
diff --git a/src/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/X500NameEncoder.ManagedDecode.cs b/src/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/X500NameEncoder.ManagedDecode.cs
105ad9
index 094e95c20d..0285415ba3 100644
105ad9
--- a/src/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/X500NameEncoder.ManagedDecode.cs
105ad9
+++ b/src/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/X500NameEncoder.ManagedDecode.cs
105ad9
@@ -127,7 +127,7 @@ namespace Internal.Cryptography.Pal
105ad9
                 }
105ad9
             }
105ad9
 
105ad9
-            if (addTrailingDelimiter)
105ad9
+            if (addTrailingDelimiter && decodedName.Length > 0)
105ad9
             {
105ad9
                 decodedName.Append(dnSeparator);
105ad9
             }
105ad9
@@ -150,7 +150,7 @@ namespace Internal.Cryptography.Pal
105ad9
                 case DerSequenceReader.DerTag.UTF8String:
105ad9
                     return tavReader.ReadUtf8String();
105ad9
                 case DerSequenceReader.DerTag.T61String:
105ad9
-                    return "";
105ad9
+                    return tavReader.ReadT61String();
105ad9
                 default:
105ad9
                     throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding);
105ad9
             }
105ad9
diff --git a/src/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/X500NameEncoder.OpenSslDecode.cs b/src/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/X500NameEncoder.OpenSslDecode.cs
105ad9
deleted file mode 100644
105ad9
index 0c03283475..0000000000
105ad9
--- a/src/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/X500NameEncoder.OpenSslDecode.cs
105ad9
+++ /dev/null
105ad9
@@ -1,123 +0,0 @@
105ad9
-// Licensed to the .NET Foundation under one or more agreements.
105ad9
-// The .NET Foundation licenses this file to you under the MIT license.
105ad9
-// See the LICENSE file in the project root for more information.
105ad9
-
105ad9
-using System.Text;
105ad9
-
105ad9
-using Microsoft.Win32.SafeHandles;
105ad9
-
105ad9
-namespace Internal.Cryptography.Pal
105ad9
-{
105ad9
-    internal static partial class X500NameEncoder
105ad9
-    {
105ad9
-        private static string X500DistinguishedNameDecode(
105ad9
-            byte[] encodedName,
105ad9
-            bool printOid,
105ad9
-            bool reverse,
105ad9
-            bool quoteIfNeeded,
105ad9
-            string dnSeparator,
105ad9
-            string multiValueSeparator,
105ad9
-            bool addTrailingDelimiter)
105ad9
-        {
105ad9
-            using (SafeX509NameHandle x509Name = Interop.Crypto.DecodeX509Name(encodedName, encodedName.Length))
105ad9
-            {
105ad9
-                if (x509Name.IsInvalid)
105ad9
-                {
105ad9
-                    Interop.Crypto.ErrClearError();
105ad9
-                    return "";
105ad9
-                }
105ad9
-
105ad9
-                // We need to allocate a StringBuilder to hold the data as we're building it, and there's the usual
105ad9
-                // arbitrary process of choosing a number that's "big enough" to minimize reallocations without wasting
105ad9
-                // too much space in the average case.
105ad9
-                //
105ad9
-                // So, let's look at an example of what our output might be.
105ad9
-                //
105ad9
-                // GitHub.com's SSL cert has a "pretty long" subject (partially due to the unknown OIDs):
105ad9
-                //   businessCategory=Private Organization
105ad9
-                //   1.3.6.1.4.1.311.60.2.1.3=US
105ad9
-                //   1.3.6.1.4.1.311.60.2.1.2=Delaware
105ad9
-                //   serialNumber=5157550
105ad9
-                //   street=548 4th Street
105ad9
-                //   postalCode=94107
105ad9
-                //   C=US
105ad9
-                //   ST=California
105ad9
-                //   L=San Francisco
105ad9
-                //   O=GitHub, Inc.
105ad9
-                //   CN=github.com
105ad9
-                //
105ad9
-                // Which comes out to 228 characters using OpenSSL's default pretty-print
105ad9
-                // (openssl x509 -in github.cer -text -noout)
105ad9
-                // Throw in some "maybe-I-need-to-quote-this" quotes, and a couple of extra/extra-long O/OU values
105ad9
-                // and round that up to the next programmer number, and you get that 512 should avoid reallocations
105ad9
-                // in all but the most dire of cases.
105ad9
-                StringBuilder decodedName = new StringBuilder(512);
105ad9
-                int entryCount = Interop.Crypto.GetX509NameEntryCount(x509Name);
105ad9
-                bool printSpacing = false;
105ad9
-
105ad9
-                for (int i = 0; i < entryCount; i++)
105ad9
-                {
105ad9
-                    int loc = reverse ? entryCount - i - 1 : i;
105ad9
-
105ad9
-                    using (SafeSharedX509NameEntryHandle nameEntry = Interop.Crypto.GetX509NameEntry(x509Name, loc))
105ad9
-                    {
105ad9
-                        Interop.Crypto.CheckValidOpenSslHandle(nameEntry);
105ad9
-
105ad9
-                        string thisOidValue;
105ad9
-
105ad9
-                        using (SafeSharedAsn1ObjectHandle oidHandle = Interop.Crypto.GetX509NameEntryOid(nameEntry))
105ad9
-                        {
105ad9
-                            thisOidValue = Interop.Crypto.GetOidValue(oidHandle);
105ad9
-                        }
105ad9
-
105ad9
-                        if (printSpacing)
105ad9
-                        {
105ad9
-                            decodedName.Append(dnSeparator);
105ad9
-                        }
105ad9
-                        else
105ad9
-                        {
105ad9
-                            printSpacing = true;
105ad9
-                        }
105ad9
-
105ad9
-                        if (printOid)
105ad9
-                        {
105ad9
-                            AppendOid(decodedName, thisOidValue);
105ad9
-                        }
105ad9
-
105ad9
-                        string rdnValue;
105ad9
-
105ad9
-                        using (SafeSharedAsn1StringHandle valueHandle = Interop.Crypto.GetX509NameEntryData(nameEntry))
105ad9
-                        {
105ad9
-                            rdnValue = Interop.Crypto.Asn1StringToManagedString(valueHandle);
105ad9
-                        }
105ad9
-
105ad9
-                        bool quote = quoteIfNeeded && NeedsQuoting(rdnValue);
105ad9
-
105ad9
-                        if (quote)
105ad9
-                        {
105ad9
-                            decodedName.Append('"');
105ad9
-
105ad9
-                            // If the RDN itself had a quote within it, that quote needs to be escaped
105ad9
-                            // with another quote.
105ad9
-                            rdnValue = rdnValue.Replace("\"", "\"\"");
105ad9
-                        }
105ad9
-
105ad9
-                        decodedName.Append(rdnValue);
105ad9
-
105ad9
-                        if (quote)
105ad9
-                        {
105ad9
-                            decodedName.Append('"');
105ad9
-                        }
105ad9
-                    }
105ad9
-                }
105ad9
-
105ad9
-                if (addTrailingDelimiter)
105ad9
-                {
105ad9
-                    decodedName.Append(dnSeparator);
105ad9
-                }
105ad9
-
105ad9
-                return decodedName.ToString();
105ad9
-            }
105ad9
-        }
105ad9
-    }
105ad9
-}
105ad9
diff --git a/src/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/X500NameEncoder.cs b/src/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/X500NameEncoder.cs
105ad9
index 56da9ae40c..005e09abe3 100644
105ad9
--- a/src/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/X500NameEncoder.cs
105ad9
+++ b/src/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/X500NameEncoder.cs
105ad9
@@ -40,7 +40,7 @@ namespace Internal.Cryptography.Pal
105ad9
             byte[] encodedName,
105ad9
             bool printOid,
105ad9
             X500DistinguishedNameFlags flags,
105ad9
-            bool addTrailingDelimieter=false)
105ad9
+            bool addTrailingDelimiter = false)
105ad9
         {
105ad9
             bool reverse = (flags & X500DistinguishedNameFlags.Reversed) == X500DistinguishedNameFlags.Reversed;
105ad9
             bool quoteIfNeeded = (flags & X500DistinguishedNameFlags.DoNotUseQuotes) != X500DistinguishedNameFlags.DoNotUseQuotes;
105ad9
@@ -51,7 +51,8 @@ namespace Internal.Cryptography.Pal
105ad9
             {
105ad9
                 dnSeparator = "; ";
105ad9
             }
105ad9
-            else if ((flags & X500DistinguishedNameFlags.UseNewLines) == X500DistinguishedNameFlags.UseNewLines)
105ad9
+            // Explicit UseCommas has preference over explicit UseNewLines.
105ad9
+            else if ((flags & (X500DistinguishedNameFlags.UseNewLines | X500DistinguishedNameFlags.UseCommas)) == X500DistinguishedNameFlags.UseNewLines)
105ad9
             {
105ad9
                 dnSeparator = Environment.NewLine;
105ad9
             }
105ad9
@@ -73,7 +74,7 @@ namespace Internal.Cryptography.Pal
105ad9
                     quoteIfNeeded,
105ad9
                     dnSeparator,
105ad9
                     multiValueSparator,
105ad9
-                    addTrailingDelimieter);
105ad9
+                    addTrailingDelimiter);
105ad9
             }
105ad9
             catch (CryptographicException)
105ad9
             {
105ad9
diff --git a/src/System.Security.Cryptography.X509Certificates/src/System.Security.Cryptography.X509Certificates.csproj b/src/System.Security.Cryptography.X509Certificates/src/System.Security.Cryptography.X509Certificates.csproj
105ad9
index 20b0328943..3bdef009b9 100644
105ad9
--- a/src/System.Security.Cryptography.X509Certificates/src/System.Security.Cryptography.X509Certificates.csproj
105ad9
+++ b/src/System.Security.Cryptography.X509Certificates/src/System.Security.Cryptography.X509Certificates.csproj
105ad9
@@ -169,7 +169,6 @@
105ad9
     <Compile Include="Internal\Cryptography\Pal.Unix\PkcsFormatReader.cs" />
105ad9
     <Compile Include="Internal\Cryptography\Pal.Unix\SingleCertLoader.cs" />
105ad9
     <Compile Include="Internal\Cryptography\Pal.Unix\StorePal.cs" />
105ad9
-    <Compile Include="Internal\Cryptography\Pal.Unix\X500NameEncoder.OpenSslDecode.cs" />
105ad9
     <Compile Include="Internal\Cryptography\Pal.Unix\X509Pal.cs" />
105ad9
     <Compile Include="Internal\Cryptography\Pal.Unix\X509Persistence.cs" />
105ad9
     <Compile Include="$(CommonPath)\Interop\Unix\Interop.Libraries.cs">
105ad9
@@ -223,9 +222,6 @@
105ad9
     <Compile Include="$(CommonPath)\Interop\Unix\System.Security.Cryptography.Native\Interop.X509Name.cs">
105ad9
       <Link>Common\Interop\Unix\System.Security.Cryptography.Native\Interop.X509Name.cs</Link>
105ad9
     </Compile>
105ad9
-    <Compile Include="$(CommonPath)\Interop\Unix\System.Security.Cryptography.Native\Interop.X509NameEntry.cs">
105ad9
-      <Link>Common\Interop\Unix\System.Security.Cryptography.Native\Interop.X509NameEntry.cs</Link>
105ad9
-    </Compile>
105ad9
     <Compile Include="$(CommonPath)\Interop\Unix\System.Security.Cryptography.Native\Interop.X509Stack.cs">
105ad9
       <Link>Common\Interop\Unix\System.Security.Cryptography.Native\Interop.X509Stack.cs</Link>
105ad9
     </Compile>
105ad9
@@ -274,9 +270,6 @@
105ad9
     <Compile Include="$(CommonPath)\Microsoft\Win32\SafeHandles\SafeX509Handles.Unix.cs">
105ad9
       <Link>Common\Microsoft\Win32\SafeHandles\SafeX509Handles.Unix.cs</Link>
105ad9
     </Compile>
105ad9
-    <Compile Include="$(CommonPath)\Microsoft\Win32\SafeHandles\SafeX509NameHandle.Unix.cs">
105ad9
-      <Link>Common\Microsoft\Win32\SafeHandles\SafeX509NameHandle.Unix.cs</Link>
105ad9
-    </Compile>
105ad9
     <Compile Include="$(CommonPath)\Microsoft\Win32\SafeHandles\X509ExtensionSafeHandles.Unix.cs">
105ad9
       <Link>Common\Microsoft\Win32\SafeHandles\X509ExtensionSafeHandles.Unix.cs</Link>
105ad9
     </Compile>
105ad9
@@ -390,7 +383,6 @@
105ad9
     <Compile Include="Internal\Cryptography\Pal.OSX\StorePal.ExportPal.cs" />
105ad9
     <Compile Include="Internal\Cryptography\Pal.OSX\StorePal.LoaderPal.cs" />
105ad9
     <Compile Include="Internal\Cryptography\Pal.OSX\X509Pal.cs" />
105ad9
-    <Compile Include="Internal\Cryptography\Pal.Unix\X500NameEncoder.ManagedDecode.cs" />
105ad9
   </ItemGroup>
105ad9
   <ItemGroup Condition=" '$(TargetsUnix)' == 'true'">
105ad9
     <Compile Include="Internal\Cryptography\Pal.Unix\CertificatePolicy.cs" />
105ad9
@@ -398,6 +390,7 @@
105ad9
     <Compile Include="Internal\Cryptography\Pal.Unix\ManagedCertificateFinder.cs" />
105ad9
     <Compile Include="Internal\Cryptography\Pal.Unix\ManagedX509ExtensionProcessor.cs" />
105ad9
     <Compile Include="Internal\Cryptography\Pal.Unix\X500NameEncoder.cs" />
105ad9
+    <Compile Include="Internal\Cryptography\Pal.Unix\X500NameEncoder.ManagedDecode.cs" />
105ad9
   </ItemGroup>
105ad9
   <ItemGroup>
105ad9
     <Reference Include="System.Buffers" />
105ad9
@@ -417,6 +410,7 @@
105ad9
     <Reference Include="System.Security.Cryptography.Algorithms" />
105ad9
     <Reference Include="System.Security.Cryptography.Encoding" />
105ad9
     <Reference Include="System.Security.Cryptography.Primitives" />
105ad9
+    <Reference Include="System.Text.Encoding.Extensions" />
105ad9
     <Reference Include="System.Threading" />
105ad9
   </ItemGroup>
105ad9
   <ItemGroup Condition="'$(TargetsWindows)' == 'true'">
105ad9
diff --git a/src/System.Security.Cryptography.X509Certificates/tests/X500DistinguishedNameTests.cs b/src/System.Security.Cryptography.X509Certificates/tests/X500DistinguishedNameTests.cs
105ad9
index 027823ffbd..95ac1ed519 100644
105ad9
--- a/src/System.Security.Cryptography.X509Certificates/tests/X500DistinguishedNameTests.cs
105ad9
+++ b/src/System.Security.Cryptography.X509Certificates/tests/X500DistinguishedNameTests.cs
105ad9
@@ -126,6 +126,16 @@ namespace System.Security.Cryptography.X509Certificates.Tests
105ad9
 
105ad9
             Assert.Equal(notQuoted, dn.Decode(X500DistinguishedNameFlags.DoNotUseQuotes));
105ad9
         }
105ad9
+        
105ad9
+        [Theory]
105ad9
+        [MemberData(nameof(T61Cases))]
105ad9
+        public static void T61Strings(string expected, string hexEncoded)
105ad9
+        {
105ad9
+            byte[] encoded = hexEncoded.HexToByteArray();
105ad9
+            X500DistinguishedName dn = new X500DistinguishedName(encoded);
105ad9
+
105ad9
+            Assert.Equal(expected, dn.Name);
105ad9
+        }
105ad9
 
105ad9
         [Fact]
105ad9
         public static void PrintComplexReversed()
105ad9
@@ -158,6 +168,22 @@ namespace System.Security.Cryptography.X509Certificates.Tests
105ad9
             Assert.EndsWith(expected, dn.Decode(X500DistinguishedNameFlags.None), StringComparison.Ordinal);
105ad9
         }
105ad9
 
105ad9
+        [Fact]
105ad9
+        public static void EdgeCaseEmptyFormat()
105ad9
+        {
105ad9
+            X500DistinguishedName dn = new X500DistinguishedName("");
105ad9
+            Assert.Equal(String.Empty, dn.Format(true));
105ad9
+            Assert.Equal(String.Empty, dn.Format(false));
105ad9
+        }
105ad9
+
105ad9
+        [Fact]
105ad9
+        public static void EdgeCaseUseCommaAndNewLines()
105ad9
+        {
105ad9
+            const string rname = "C=US, O=\"RSA Data Security, Inc.\", OU=Secure Server Certification Authority";
105ad9
+            X500DistinguishedName dn = new X500DistinguishedName(rname, X500DistinguishedNameFlags.None);
105ad9
+            Assert.Equal(rname, dn.Decode(X500DistinguishedNameFlags.UseCommas | X500DistinguishedNameFlags.UseNewLines));
105ad9
+        }
105ad9
+
105ad9
         public static readonly object[][] WhitespaceBeforeCases =
105ad9
         {
105ad9
             // Regular space.
105ad9
@@ -386,6 +412,49 @@ namespace System.Security.Cryptography.X509Certificates.Tests
105ad9
             },
105ad9
         };
105ad9
 
105ad9
+        public static readonly object[][] T61Cases =
105ad9
+        {
105ad9
+            // https://github.com/dotnet/corefx/issues/27466
105ad9
+            new object[]
105ad9
+            {
105ad9
+                "CN=GrapeCity inc., OU=Tools Development, O=GrapeCity inc., " +
105ad9
+                "L=Sendai Izumi-ku, S=Miyagi, C=JP",
105ad9
+                "308186310b3009060355040613024a50310f300d060355040813064d69796167" +
105ad9
+                "69311830160603550407130f53656e64616920497a756d692d6b753117301506" +
105ad9
+                "0355040a140e47726170654369747920696e632e311a3018060355040b141154" +
105ad9
+                "6f6f6c7320446576656c6f706d656e74311730150603550403140e4772617065" +
105ad9
+                "4369747920696e632e"
105ad9
+            },
105ad9
+
105ad9
+            // Mono test case taken from old bug report
105ad9
+            new object[]
105ad9
+            {
105ad9
+                "SERIALNUMBER=CVR:13471967-UID:121212121212, E=vhm@use.test.dk, " +
105ad9
+                "CN=Hedeby's M\u00f8belhandel - Salgsafdelingen, " + 
105ad9
+                "O=Hedeby's M\u00f8belhandel // CVR:13471967, C=DK",
105ad9
+                "3081B5310B300906035504061302444B312D302B060355040A14244865646562" +
105ad9
+                "792773204DF862656C68616E64656C202F2F204356523A313334373139363731" +
105ad9
+                "2F302D060355040314264865646562792773204DF862656C68616E64656C202D" +
105ad9
+                "2053616C6773616664656C696E67656E311E301C06092A864886F70D01090116" +
105ad9
+                "0F76686D407573652E746573742E646B312630240603550405131D4356523A31" +
105ad9
+                "333437313936372D5549443A313231323132313231323132"
105ad9
+            },
105ad9
+
105ad9
+            // Valid UTF-8 string is interpreted as UTF-8
105ad9
+            new object[]
105ad9
+            {
105ad9
+                "C=\u00a2",
105ad9
+                "300D310B300906035504061402C2A2"
105ad9
+            },
105ad9
+
105ad9
+            // Invalid UTF-8 string with valid UTF-8 sequence is interpreted as ISO 8859-1
105ad9
+            new object[]
105ad9
+            {
105ad9
+                "L=\u00c2\u00a2\u00f8",
105ad9
+                "300E310C300A06035504071403C2A2F8"
105ad9
+            },
105ad9
+        };
105ad9
+
105ad9
         private const string MicrosoftDotComSubject =
105ad9
             "3082010F31133011060B2B0601040182373C02010313025553311B3019060B2B" +
105ad9
             "0601040182373C0201020C0A57617368696E67746F6E311D301B060355040F13" +
105ad9
-- 
105ad9
2.20.1
105ad9