You've already forked linux-packaging-mono
Imported Upstream version 5.18.0.142
Former-commit-id: 7467d4b717762eeaf652d77f1486dd11ffb1ff1f
This commit is contained in:
parent
e52655b4dc
commit
0abdbe5a7d
176
mcs/class/System/Mono/SystemCertificateProvider.cs
Normal file
176
mcs/class/System/Mono/SystemCertificateProvider.cs
Normal file
@@ -0,0 +1,176 @@
|
||||
//
|
||||
// SystemCertificateProvider.cs
|
||||
//
|
||||
// Author:
|
||||
// Martin Baulig <mabaul@microsoft.com>
|
||||
//
|
||||
// Copyright (c) 2018 Xamarin, Inc.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
#if MONO_FEATURE_BTLS || MONO_FEATURE_APPLETLS
|
||||
#if MONO_SECURITY_ALIAS
|
||||
extern alias MonoSecurity;
|
||||
#endif
|
||||
|
||||
#if MONO_SECURITY_ALIAS
|
||||
using MonoSecurity::Mono.Security.Interface;
|
||||
#else
|
||||
using Mono.Security.Interface;
|
||||
#endif
|
||||
using MNS = Mono.Net.Security;
|
||||
#endif
|
||||
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using Microsoft.Win32.SafeHandles;
|
||||
|
||||
namespace Mono
|
||||
{
|
||||
class SystemCertificateProvider : ISystemCertificateProvider
|
||||
{
|
||||
#if MONO_FEATURE_BTLS || MONO_FEATURE_APPLETLS
|
||||
public MonoTlsProvider Provider {
|
||||
get {
|
||||
EnsureInitialized ();
|
||||
return provider;
|
||||
}
|
||||
}
|
||||
|
||||
static MonoTlsProvider provider;
|
||||
#endif
|
||||
|
||||
static X509PalImpl GetX509Pal ()
|
||||
{
|
||||
#if MONO_FEATURE_APPLETLS
|
||||
if (provider?.ID == MNS.MonoTlsProviderFactory.AppleTlsId)
|
||||
return new Mono.AppleTls.X509PalImplApple ();
|
||||
#elif MONO_FEATURE_APPLE_X509
|
||||
return new Mono.AppleTls.X509PalImplApple ();
|
||||
#endif
|
||||
#if MONO_FEATURE_BTLS
|
||||
if (provider?.ID == MNS.MonoTlsProviderFactory.BtlsId)
|
||||
return new Mono.Btls.X509PalImplBtls (provider);
|
||||
#endif
|
||||
|
||||
return new X509PalImplMono ();
|
||||
}
|
||||
|
||||
static int initialized;
|
||||
static X509PalImpl x509pal;
|
||||
|
||||
static void EnsureInitialized ()
|
||||
{
|
||||
/*
|
||||
* We need to lazily initialize because we might be called from
|
||||
* MonoTlsProviderFactory.InitializeInternal().
|
||||
*
|
||||
*/
|
||||
if (Interlocked.CompareExchange (ref initialized, 1, 0) != 0)
|
||||
return;
|
||||
|
||||
#if MONO_FEATURE_BTLS || MONO_FEATURE_APPLETLS
|
||||
provider = MonoTlsProviderFactory.GetProvider ();
|
||||
#endif
|
||||
x509pal = GetX509Pal ();
|
||||
}
|
||||
|
||||
public X509PalImpl X509Pal {
|
||||
get {
|
||||
EnsureInitialized ();
|
||||
return x509pal;
|
||||
}
|
||||
}
|
||||
|
||||
public X509CertificateImpl Import (
|
||||
byte[] data, CertificateImportFlags importFlags = CertificateImportFlags.None)
|
||||
{
|
||||
if (data == null || data.Length == 0)
|
||||
return null;
|
||||
|
||||
X509CertificateImpl impl = null;
|
||||
if ((importFlags & CertificateImportFlags.DisableNativeBackend) == 0) {
|
||||
impl = X509Pal.Import (data);
|
||||
if (impl != null)
|
||||
return impl;
|
||||
}
|
||||
|
||||
if ((importFlags & CertificateImportFlags.DisableAutomaticFallback) != 0)
|
||||
return null;
|
||||
|
||||
return X509Pal.ImportFallback (data);
|
||||
}
|
||||
|
||||
X509CertificateImpl ISystemCertificateProvider.Import (
|
||||
byte[] data, SafePasswordHandle password, X509KeyStorageFlags keyStorageFlags,
|
||||
CertificateImportFlags importFlags)
|
||||
{
|
||||
return Import (data, password, keyStorageFlags, importFlags);
|
||||
}
|
||||
|
||||
public X509Certificate2Impl Import (
|
||||
byte[] data, SafePasswordHandle password, X509KeyStorageFlags keyStorageFlags,
|
||||
CertificateImportFlags importFlags = CertificateImportFlags.None)
|
||||
{
|
||||
if (data == null || data.Length == 0)
|
||||
return null;
|
||||
|
||||
X509Certificate2Impl impl = null;
|
||||
if ((importFlags & CertificateImportFlags.DisableNativeBackend) == 0) {
|
||||
impl = X509Pal.Import (data, password, keyStorageFlags);
|
||||
if (impl != null)
|
||||
return impl;
|
||||
}
|
||||
|
||||
if ((importFlags & CertificateImportFlags.DisableAutomaticFallback) != 0)
|
||||
return null;
|
||||
|
||||
return X509Pal.ImportFallback (data, password, keyStorageFlags);
|
||||
}
|
||||
|
||||
X509CertificateImpl ISystemCertificateProvider.Import (X509Certificate cert, CertificateImportFlags importFlags)
|
||||
{
|
||||
return Import (cert, importFlags);
|
||||
}
|
||||
|
||||
public X509Certificate2Impl Import (
|
||||
X509Certificate cert, CertificateImportFlags importFlags = CertificateImportFlags.None)
|
||||
{
|
||||
if (cert.Impl == null)
|
||||
return null;
|
||||
|
||||
X509Certificate2Impl impl = null;
|
||||
if ((importFlags & CertificateImportFlags.DisableNativeBackend) == 0) {
|
||||
impl = X509Pal.Import (cert);
|
||||
if (impl != null)
|
||||
return impl;
|
||||
}
|
||||
|
||||
impl = cert.Impl as X509Certificate2Impl;
|
||||
if (impl != null)
|
||||
return (X509Certificate2Impl)impl.Clone ();
|
||||
|
||||
if ((importFlags & CertificateImportFlags.DisableAutomaticFallback) != 0)
|
||||
return null;
|
||||
|
||||
return X509Pal.ImportFallback (cert.GetRawCertData ());
|
||||
}
|
||||
}
|
||||
}
|
69
mcs/class/System/Mono/SystemDependencyProvider.cs
Normal file
69
mcs/class/System/Mono/SystemDependencyProvider.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
//
|
||||
// SystemDependencyProvider.cs
|
||||
//
|
||||
// Author:
|
||||
// Martin Baulig <mabaul@microsoft.com>
|
||||
//
|
||||
// Copyright (c) 2018 Xamarin, Inc.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace Mono
|
||||
{
|
||||
/*
|
||||
* The purpose of this class is to allow code in `corlib.dll` to access `System.dll` APIs.
|
||||
*/
|
||||
class SystemDependencyProvider : ISystemDependencyProvider
|
||||
{
|
||||
static SystemDependencyProvider instance;
|
||||
|
||||
public static SystemDependencyProvider Instance {
|
||||
get {
|
||||
Initialize ();
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
|
||||
internal static void Initialize ()
|
||||
{
|
||||
if (instance == null)
|
||||
Interlocked.CompareExchange (ref instance, new SystemDependencyProvider (), null);
|
||||
}
|
||||
|
||||
ISystemCertificateProvider ISystemDependencyProvider.CertificateProvider => CertificateProvider;
|
||||
|
||||
public SystemCertificateProvider CertificateProvider {
|
||||
get;
|
||||
}
|
||||
|
||||
public X509PalImpl X509Pal => CertificateProvider.X509Pal;
|
||||
|
||||
SystemDependencyProvider ()
|
||||
{
|
||||
CertificateProvider = new SystemCertificateProvider ();
|
||||
|
||||
/*
|
||||
* Register ourselves with corlib's `DependencyInjector`.
|
||||
*/
|
||||
DependencyInjector.Register (this);
|
||||
}
|
||||
}
|
||||
}
|
38
mcs/class/System/Mono/X509Pal.cs
Normal file
38
mcs/class/System/Mono/X509Pal.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
//
|
||||
// X509Pal.cs
|
||||
//
|
||||
// Author:
|
||||
// Martin Baulig <mabaul@microsoft.com>
|
||||
//
|
||||
// Copyright (c) 2018 Xamarin, Inc.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Security.Cryptography;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
|
||||
namespace Mono
|
||||
{
|
||||
static class X509Pal
|
||||
{
|
||||
public static X509PalImpl Instance => SystemDependencyProvider.Instance.X509Pal;
|
||||
}
|
||||
}
|
50
mcs/class/System/Mono/X509PalImpl.Mono.cs
Normal file
50
mcs/class/System/Mono/X509PalImpl.Mono.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
//
|
||||
// X509PalImpl.Mono.cs
|
||||
//
|
||||
// Author:
|
||||
// Martin Baulig <mabaul@microsoft.com>
|
||||
//
|
||||
// Copyright (c) 2018 Xamarin, Inc.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
using System;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using Microsoft.Win32.SafeHandles;
|
||||
|
||||
namespace Mono
|
||||
{
|
||||
class X509PalImplMono : X509PalImpl
|
||||
{
|
||||
public override X509CertificateImpl Import (byte[] data)
|
||||
{
|
||||
return ImportFallback (data);
|
||||
}
|
||||
|
||||
public override X509Certificate2Impl Import (
|
||||
byte[] data, SafePasswordHandle password, X509KeyStorageFlags keyStorageFlags)
|
||||
{
|
||||
return ImportFallback (data, password, keyStorageFlags);
|
||||
}
|
||||
|
||||
public override X509Certificate2Impl Import (X509Certificate cert)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
88
mcs/class/System/Mono/X509PalImpl.cs
Normal file
88
mcs/class/System/Mono/X509PalImpl.cs
Normal file
@@ -0,0 +1,88 @@
|
||||
//
|
||||
// X509PalImpl.cs
|
||||
//
|
||||
// Author:
|
||||
// Martin Baulig <mabaul@microsoft.com>
|
||||
//
|
||||
// Copyright (c) 2018 Xamarin, Inc.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Security.Cryptography;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using Microsoft.Win32.SafeHandles;
|
||||
|
||||
namespace Mono
|
||||
{
|
||||
abstract class X509PalImpl
|
||||
{
|
||||
public abstract X509CertificateImpl Import (byte[] data);
|
||||
|
||||
public abstract X509Certificate2Impl Import (
|
||||
byte[] data, SafePasswordHandle password, X509KeyStorageFlags keyStorageFlags);
|
||||
|
||||
public abstract X509Certificate2Impl Import (X509Certificate cert);
|
||||
|
||||
static byte[] PEM (string type, byte[] data)
|
||||
{
|
||||
string pem = Encoding.ASCII.GetString (data);
|
||||
string header = String.Format ("-----BEGIN {0}-----", type);
|
||||
string footer = String.Format ("-----END {0}-----", type);
|
||||
int start = pem.IndexOf (header) + header.Length;
|
||||
int end = pem.IndexOf (footer, start);
|
||||
string base64 = pem.Substring (start, (end - start));
|
||||
return Convert.FromBase64String (base64);
|
||||
}
|
||||
|
||||
protected static byte[] ConvertData (byte[] data)
|
||||
{
|
||||
if (data == null || data.Length == 0)
|
||||
return data;
|
||||
|
||||
// does it looks like PEM ?
|
||||
if (data[0] != 0x30) {
|
||||
try {
|
||||
return PEM ("CERTIFICATE", data);
|
||||
} catch {
|
||||
// let the implementation take care of it.
|
||||
}
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
internal X509Certificate2Impl ImportFallback (byte[] data)
|
||||
{
|
||||
data = ConvertData (data);
|
||||
|
||||
var impl = new X509Certificate2ImplMono ();
|
||||
using (var handle = new SafePasswordHandle ((string)null))
|
||||
impl.Import (data, handle, X509KeyStorageFlags.DefaultKeySet);
|
||||
return impl;
|
||||
}
|
||||
|
||||
internal X509Certificate2Impl ImportFallback (byte[] data, SafePasswordHandle password, X509KeyStorageFlags keyStorageFlags)
|
||||
{
|
||||
var impl = new X509Certificate2ImplMono ();
|
||||
impl.Import (data, password, keyStorageFlags);
|
||||
return impl;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user