Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

NetFX compatibility fixes for X500DistinguishedName. #30572

Merged
merged 12 commits into from
Jun 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,6 @@ internal static IntPtr GetObjectDefinitionByName(string friendlyName)
[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_Asn1StringFree")]
internal static extern void Asn1StringFree(IntPtr o);

internal static string GetOidValue(SafeSharedAsn1ObjectHandle asn1Object)
{
Debug.Assert(asn1Object != null);

bool added = false;
asn1Object.DangerousAddRef(ref added);
try
{
return GetOidValue(asn1Object.DangerousGetHandle());
}
finally
{
asn1Object.DangerousRelease();
}
}

internal static unsafe string GetOidValue(IntPtr asn1ObjectPtr)
{
// OBJ_obj2txt returns the number of bytes that should have been in the answer, but it does not accept
Expand Down Expand Up @@ -127,14 +111,3 @@ internal static unsafe string GetOidValue(IntPtr asn1ObjectPtr)
}
}
}

namespace Microsoft.Win32.SafeHandles
{
internal class SafeSharedAsn1ObjectHandle : SafeInteriorHandle
{
private SafeSharedAsn1ObjectHandle() :
base(IntPtr.Zero, ownsHandle: true)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,13 @@ internal static partial class Crypto
[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_GetX509NameStackFieldCount")]
internal static extern int GetX509NameStackFieldCount(SafeSharedX509NameStackHandle sk);

[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_PushX509NameStackField")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool PushX509NameStackField(SafeX509NameStackHandle stack, SafeX509NameHandle x509_Name);

[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_RecursiveFreeX509NameStack")]
internal static extern void RecursiveFreeX509NameStack(IntPtr stack);

[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_NewX509NameStack")]
internal static extern SafeX509NameStackHandle NewX509NameStack();

[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_GetX509NameStackField")]
private static extern SafeSharedX509NameHandle GetX509NameStackField_private(SafeSharedX509NameStackHandle sk,
int loc);

[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_GetX509NameRawBytes")]
private static extern int GetX509NameRawBytes(SafeSharedX509NameHandle x509Name, byte[] buf, int cBuf);

[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_DecodeX509Name")]
internal static extern SafeX509NameHandle DecodeX509Name(byte[] buf, int len);

[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_X509NameDestroy")]
internal static extern void X509NameDestroy(IntPtr a);

[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_GetX509NameEntryCount")]
internal static extern int GetX509NameEntryCount(SafeX509NameHandle x509Name);

internal static X500DistinguishedName LoadX500Name(SafeSharedX509NameHandle namePtr)
{
CheckValidOpenSslHandle(namePtr);
Expand Down Expand Up @@ -86,25 +67,5 @@ private SafeSharedX509NameStackHandle() :
{
}
}

internal sealed class SafeX509NameStackHandle : SafeHandle
{
private SafeX509NameStackHandle() :
base(IntPtr.Zero, ownsHandle: true)
{
}

protected override bool ReleaseHandle()
{
Interop.Crypto.RecursiveFreeX509NameStack(handle);
SetHandle(IntPtr.Zero);
return true;
}

public override bool IsInvalid
{
get { return handle == IntPtr.Zero; }
}
}
}

This file was deleted.

This file was deleted.

35 changes: 35 additions & 0 deletions src/Common/src/System/Security/Cryptography/DerSequenceReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ internal class DerSequenceReader

internal static DateTimeFormatInfo s_validityDateTimeFormatInfo;

private static System.Text.Encoding s_utf8EncodingWithExceptionFallback;
private static System.Text.Encoding s_latin1Encoding;

private readonly byte[] _data;
private readonly int _end;
private int _position;
Expand Down Expand Up @@ -383,6 +386,38 @@ internal string ReadIA5String()
return TrimTrailingNulls(ia5String);
}

internal string ReadT61String()
{
EatTag(DerTag.T61String);
int contentLength = EatLength();
string t61String;

// Technically the T.61 encoding (code page 20261) should be used here, but many
// implementations don't follow that and use different character sets. CryptoAPI
// on NetFX seems to interpret it as UTF-8 with fallback to ISO 8859-1. OpenSSL
// seems to interpret it as ISO 8859-1 with no support for UTF-8.
// https://github.com/dotnet/corefx/issues/27466

System.Text.Encoding utf8EncodingWithExceptionFallback = LazyInitializer.EnsureInitialized(
ref s_utf8EncodingWithExceptionFallback,
() => new UTF8Encoding(false, true));
System.Text.Encoding latin1Encoding = LazyInitializer.EnsureInitialized(
ref s_latin1Encoding,
() => System.Text.Encoding.GetEncoding("iso-8859-1"));

try
{
t61String = utf8EncodingWithExceptionFallback.GetString(_data, _position, contentLength);
}
catch (DecoderFallbackException)
{
t61String = latin1Encoding.GetString(_data, _position, contentLength);
}
_position += contentLength;

return TrimTrailingNulls(t61String);
}

internal DateTime ReadX509Date()
{
byte tag = PeekTag();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,61 +13,3 @@ X509_NAME* CryptoNative_GetX509NameStackField(X509NameStack* sk, int32_t loc)
{
return sk_X509_NAME_value(sk, loc);
}

X509_NAME* CryptoNative_DecodeX509Name(const uint8_t* buf, int32_t len)
{
if (!buf || !len)
{
return NULL;
}

return d2i_X509_NAME(NULL, &buf, len);
}

void CryptoNative_X509NameDestroy(X509_NAME* a)
{
if (a != NULL)
{
X509_NAME_free(a);
}
}

STACK_OF(X509_NAME) * CryptoNative_NewX509NameStack()
{
return sk_X509_NAME_new_null();
}

int32_t CryptoNative_PushX509NameStackField(STACK_OF(X509_NAME) * stack, X509_NAME* x509Name)
{
if (!stack)
{
return 0;
}

return sk_X509_NAME_push(stack, x509Name);
}

void CryptoNative_RecursiveFreeX509NameStack(STACK_OF(X509_NAME) * stack)
{
sk_X509_NAME_pop_free(stack, X509_NAME_free);
}

int32_t CryptoNative_GetX509NameEntryCount(X509_NAME* x509Name)
{
return X509_NAME_entry_count(x509Name);
}

X509_NAME_ENTRY* CryptoNative_GetX509NameEntry(X509_NAME* x509Name, int32_t loc)
{
return X509_NAME_get_entry(x509Name, loc);
}

ASN1_OBJECT* CryptoNative_GetX509NameEntryOid(X509_NAME_ENTRY* nameEntry)
{
return X509_NAME_ENTRY_get_object(nameEntry);
}

ASN1_STRING* CryptoNative_GetX509NameEntryData(X509_NAME_ENTRY* nameEntry)
{
return X509_NAME_ENTRY_get_data(nameEntry);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,66 +18,3 @@ DLLEXPORT int32_t CryptoNative_GetX509NameStackFieldCount(X509NameStack* sk);
Direct shim to sk_X509_NAME_value
*/
DLLEXPORT X509_NAME* CryptoNative_GetX509NameStackField(X509NameStack* sk, int32_t loc);

/*
Shims the d2i_X509_NAME method and makes it easier to invoke from managed code.
*/
DLLEXPORT X509_NAME* CryptoNative_DecodeX509Name(const uint8_t* buf, int32_t len);

/*
Cleans up and deletes an X509_NAME instance.

Implemented by calling X509_NAME_free.

No-op if a is null.
The given X509_NAME pointer is invalid after this call.
Always succeeds.
*/
DLLEXPORT void CryptoNative_X509NameDestroy(X509_NAME* a);

/*
Function:
NewX509NameStack

Direct shim to sk_X509_NAME_new_null
*/
DLLEXPORT STACK_OF(X509_NAME) * CryptoNative_NewX509NameStack(void);

/*
Function:
PushX509NameStackField

Direct shim to sk_X509_NAME_push
Return values:
1 on success
0 on a NULL stack, or an error within sk_X509_NAME_push
*/
DLLEXPORT int32_t CryptoNative_PushX509NameStackField(STACK_OF(X509_NAME) * stack, X509_NAME* x509Name);

/*
Function:
RecursiveFreeX509NameStack

Direct shim to sk_X509_NAME_pop_free
*/
DLLEXPORT void CryptoNative_RecursiveFreeX509NameStack(STACK_OF(X509_NAME) * stack);

/*
Direct shim to X509_NAME_entry_count
*/
DLLEXPORT int32_t CryptoNative_GetX509NameEntryCount(X509_NAME* x509Name);

/*
Direct shim to X509_NAME_get_entry
*/
DLLEXPORT X509_NAME_ENTRY* CryptoNative_GetX509NameEntry(X509_NAME* x509Name, int32_t loc);

/*
Direct shim to X509_NAME_ENTRY_get_object
*/
DLLEXPORT ASN1_OBJECT* CryptoNative_GetX509NameEntryOid(X509_NAME_ENTRY* nameEntry);

/*
Direct shim to X509_NAME_ENTRY_get_data
*/
DLLEXPORT ASN1_STRING* CryptoNative_GetX509NameEntryData(X509_NAME_ENTRY* nameEntry);
3 changes: 0 additions & 3 deletions src/System.Net.Http/src/System.Net.Http.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -533,9 +533,6 @@
<Compile Include="$(CommonPath)\Microsoft\Win32\SafeHandles\SafeX509Handles.Unix.cs">
<Link>Common\Microsoft\Win32\SafeHandles\SafeX509Handles.Unix.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Microsoft\Win32\SafeHandles\SafeX509NameHandle.Unix.cs">
<Link>Common\Microsoft\Win32\SafeHandles\SafeX509NameHandle.Unix.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Microsoft\Win32\SafeHandles\X509ExtensionSafeHandles.Unix.cs">
<Link>Common\Microsoft\Win32\SafeHandles\X509ExtensionSafeHandles.Unix.cs</Link>
</Compile>
Expand Down
3 changes: 0 additions & 3 deletions src/System.Net.Security/src/System.Net.Security.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,6 @@
<Compile Include="$(CommonPath)\Microsoft\Win32\SafeHandles\SafeX509Handles.Unix.cs">
<Link>Common\Microsoft\Win32\SafeHandles\SafeX509Handles.Unix.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Microsoft\Win32\SafeHandles\SafeX509NameHandle.Unix.cs">
<Link>Common\Microsoft\Win32\SafeHandles\SafeX509NameHandle.Unix.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\Microsoft\Win32\SafeHandles\X509ExtensionSafeHandles.Unix.cs">
<Link>Common\Microsoft\Win32\SafeHandles\X509ExtensionSafeHandles.Unix.cs</Link>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
<Reference Include="System.Runtime.Extensions" />
<Reference Include="System.Runtime.InteropServices" />
<Reference Include="System.Security.Cryptography.Primitives" />
<Reference Include="System.Text.Encoding.Extensions" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetsOSX)' == 'true' ">
<Reference Include="System.Runtime.Numerics" />
Expand Down
Loading