You've already forked linux-packaging-mono
Imported Upstream version 4.8.0.309
Former-commit-id: 5f9c6ae75f295e057a7d2971f3a6df4656fa8850
This commit is contained in:
parent
ee1447783b
commit
94b2861243
@@ -55,33 +55,26 @@ using System.Runtime.InteropServices;
|
||||
[assembly: NeutralResourcesLanguage ("en-US")]
|
||||
|
||||
[assembly: ComVisible (false)]
|
||||
#if !NET_2_1
|
||||
#if !MOBILE
|
||||
[assembly: ComCompatibleVersion (1, 0, 3300, 0)]
|
||||
[assembly: AllowPartiallyTrustedCallers]
|
||||
#endif
|
||||
|
||||
#if !BOOTSTRAP_WITH_OLDLIB
|
||||
[assembly: AssemblyDelaySign (true)]
|
||||
#if NET_2_1
|
||||
#if MOBILE
|
||||
[assembly: AssemblyKeyFile ("../silverlight.pub")]
|
||||
#else
|
||||
[assembly: AssemblyKeyFile ("../ecma.pub")]
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if NET_2_1 && !MOBILE
|
||||
[assembly: InternalsVisibleTo ("System.Net, PublicKey=00240000048000009400000006020000002400005253413100040000010001008D56C76F9E8649383049F383C44BE0EC204181822A6C31CF5EB7EF486944D032188EA1D3920763712CCB12D75FB77E9811149E6148E5D32FBAAB37611C1878DDC19E20EF135D0CB2CFF2BFEC3D115810C3D9069638FE4BE215DBF795861920E5AB6F7DB2E2CEEF136AC23D5DD2BF031700AEC232F6C6B1C785B4305C123B37AB")]
|
||||
#else
|
||||
[assembly: InternalsVisibleTo ("System.ComponentModel.DataAnnotations, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9")]
|
||||
[assembly: InternalsVisibleTo ("System.Net.Http, PublicKey=002400000480000094000000060200000024000052534131000400000100010007d1fa57c4aed9f0a32e84aa0faefd0de9e8fd6aec8f87fb03766c834c99921eb23be79ad9d5dcc1dd9ad236132102900b723cf980957fc4e177108fc607774f29e8320e92ea05ece4e821c0a5efe8f1645c4c0c93c1ab99285d622caa652c1dfad63d745d6f2de5f17e5eaf0fc4963d261c8a12436518206dc093344d5ad293")]
|
||||
[assembly: StringFreezing]
|
||||
[assembly: DefaultDependency (LoadHint.Always)]
|
||||
#endif
|
||||
|
||||
[assembly: InternalsVisibleTo ("Mono.Security.Providers.NewSystemSource, PublicKey=002400000480000094000000060200000024000052534131000400000100010079159977d2d03a8e6bea7a2e74e8d1afcc93e8851974952bb480a12c9134474d04062447c37e0e68c080536fcf3c3fbe2ff9c979ce998475e506e8ce82dd5b0f350dc10e93bf2eeecf874b24770c5081dbea7447fddafa277b22de47d6ffea449674a4f9fccf84d15069089380284dbdd35f46cdff12a1bd78e4ef0065d016df")]
|
||||
[assembly: InternalsVisibleTo ("Mono.Security.Providers.OldTls, PublicKey=002400000480000094000000060200000024000052534131000400000100010079159977d2d03a8e6bea7a2e74e8d1afcc93e8851974952bb480a12c9134474d04062447c37e0e68c080536fcf3c3fbe2ff9c979ce998475e506e8ce82dd5b0f350dc10e93bf2eeecf874b24770c5081dbea7447fddafa277b22de47d6ffea449674a4f9fccf84d15069089380284dbdd35f46cdff12a1bd78e4ef0065d016df")]
|
||||
[assembly: InternalsVisibleTo ("Mono.Security.Providers.NewTls, PublicKey=002400000480000094000000060200000024000052534131000400000100010079159977d2d03a8e6bea7a2e74e8d1afcc93e8851974952bb480a12c9134474d04062447c37e0e68c080536fcf3c3fbe2ff9c979ce998475e506e8ce82dd5b0f350dc10e93bf2eeecf874b24770c5081dbea7447fddafa277b22de47d6ffea449674a4f9fccf84d15069089380284dbdd35f46cdff12a1bd78e4ef0065d016df")]
|
||||
[assembly: InternalsVisibleTo ("Mono.Security.Providers.DotNet, PublicKey=002400000480000094000000060200000024000052534131000400000100010079159977d2d03a8e6bea7a2e74e8d1afcc93e8851974952bb480a12c9134474d04062447c37e0e68c080536fcf3c3fbe2ff9c979ce998475e506e8ce82dd5b0f350dc10e93bf2eeecf874b24770c5081dbea7447fddafa277b22de47d6ffea449674a4f9fccf84d15069089380284dbdd35f46cdff12a1bd78e4ef0065d016df")]
|
||||
[assembly: InternalsVisibleTo ("btls-cert-sync, PublicKey=002400000480000094000000060200000024000052534131000400000100010079159977d2d03a8e6bea7a2e74e8d1afcc93e8851974952bb480a12c9134474d04062447c37e0e68c080536fcf3c3fbe2ff9c979ce998475e506e8ce82dd5b0f350dc10e93bf2eeecf874b24770c5081dbea7447fddafa277b22de47d6ffea449674a4f9fccf84d15069089380284dbdd35f46cdff12a1bd78e4ef0065d016df")]
|
||||
[assembly: InternalsVisibleTo ("Mono.Btls.Interface, PublicKey=002400000480000094000000060200000024000052534131000400000100010079159977d2d03a8e6bea7a2e74e8d1afcc93e8851974952bb480a12c9134474d04062447c37e0e68c080536fcf3c3fbe2ff9c979ce998475e506e8ce82dd5b0f350dc10e93bf2eeecf874b24770c5081dbea7447fddafa277b22de47d6ffea449674a4f9fccf84d15069089380284dbdd35f46cdff12a1bd78e4ef0065d016df")]
|
||||
[assembly: InternalsVisibleTo ("Mono.Security, PublicKey=002400000480000094000000060200000024000052534131000400000100010079159977d2d03a8e6bea7a2e74e8d1afcc93e8851974952bb480a12c9134474d04062447c37e0e68c080536fcf3c3fbe2ff9c979ce998475e506e8ce82dd5b0f350dc10e93bf2eeecf874b24770c5081dbea7447fddafa277b22de47d6ffea449674a4f9fccf84d15069089380284dbdd35f46cdff12a1bd78e4ef0065d016df")]
|
||||
|
||||
[assembly: InternalsVisibleTo ("Xamarin.BoringTls, PublicKey=002400000480000094000000060200000024000052534131000400001100000099dd12eda85767ae6f06023ee28e711c7e5a212462095c83868c29db75eddf6d8e296e03824c14fedd5f55553fed0b6173be3cc985a4b7f9fb7c83ccff8ba3938563b3d1f45a81122f12a1bcb73edcaad61a8456c7595a6da5184b4dd9d10f011b949ef1391fccfeab1ba62aa51c267ef8bd57ef1b6ba5a4c515d0badb81a78f")]
|
||||
|
@@ -19,7 +19,17 @@ TEST_RESOURCES = \
|
||||
Test/System/test-uri-props-manual.txt \
|
||||
Test/System/test-uri-relative-props.txt
|
||||
|
||||
TEST_LIB_REFS = System.Drawing Mono.Security System.Data System.Xml System.Core System.Configuration
|
||||
ifndef NO_MONO_SECURITY
|
||||
MONO_SECURITY=Mono.Security
|
||||
MONO_SECURITY_REF=MonoSecurity=Mono.Security
|
||||
MONO_SECURITY_DLL=$(the_libdir_base)Mono.Security.dll
|
||||
endif
|
||||
|
||||
ifndef MOBILE_PROFILE
|
||||
TEST_LIB_REFS = System.Drawing $(MONO_SECURITY) System.Data System.Xml System.Core System.Configuration
|
||||
else
|
||||
TEST_LIB_REFS = $(MONO_SECURITY) System.Data System.Xml System.Core
|
||||
endif
|
||||
|
||||
TEST_MCS_FLAGS = -nowarn:618,672,219,67,169,612 \
|
||||
$(foreach f, $(TEST_RESOURCES), -resource:$(f),$(notdir $(f)))
|
||||
@@ -32,6 +42,18 @@ endif
|
||||
|
||||
LIB_MCS_FLAGS = -nowarn:618 -d:CONFIGURATION_2_0 $(REFERENCE_SOURCES_FLAGS) -unsafe $(RESOURCE_FILES:%=-resource:%)
|
||||
|
||||
ifndef PROFILE_DISABLE_BTLS
|
||||
ifdef HAVE_BTLS
|
||||
|
||||
LIB_MCS_FLAGS += -d:MONO_FEATURE_BTLS
|
||||
|
||||
ifdef HAVE_DYNAMIC_BTLS
|
||||
LIB_MCS_FLAGS += -d:MONO_FEATURE_DYNAMIC_BTLS
|
||||
endif
|
||||
|
||||
endif
|
||||
endif
|
||||
|
||||
ifndef NO_THREAD_ABORT
|
||||
REFERENCE_SOURCES_FLAGS += -d:MONO_FEATURE_THREAD_ABORT
|
||||
TEST_MCS_FLAGS += -d:MONO_FEATURE_THREAD_ABORT
|
||||
@@ -42,6 +64,11 @@ REFERENCE_SOURCES_FLAGS += -d:MONO_FEATURE_THREAD_SUSPEND_RESUME
|
||||
TEST_MCS_FLAGS += -d:MONO_FEATURE_THREAD_SUSPEND_RESUME
|
||||
endif
|
||||
|
||||
ifndef NO_MULTIPLE_APPDOMAINS
|
||||
REFERENCE_SOURCES_FLAGS += -d:MONO_FEATURE_MULTIPLE_APPDOMAINS
|
||||
TEST_MCS_FLAGS += -d:MONO_FEATURE_MULTIPLE_APPDOMAINS
|
||||
endif
|
||||
|
||||
TXT_RESOURCE_STRINGS = ../referencesource/System/System.txt
|
||||
|
||||
#
|
||||
@@ -58,7 +85,7 @@ endif
|
||||
#
|
||||
ifeq (secxml/, $(intermediate))
|
||||
LOCAL_MCS_FLAGS =
|
||||
LIB_REFS += bare/System.Xml MonoSecurity=Mono.Security
|
||||
LIB_REFS += bare/System.Xml $(MONO_SECURITY_REF)
|
||||
LIB_MCS_FLAGS += -d:SECURITY_DEP -d:XML_DEP $(EXTERN_ALIAS_FLAGS)
|
||||
endif
|
||||
|
||||
@@ -66,7 +93,7 @@ endif
|
||||
# Flags used to build the final version of System (when intermediate is not defined)
|
||||
#
|
||||
ifndef intermediate
|
||||
LIB_REFS += System.Xml MonoSecurity=Mono.Security
|
||||
LIB_REFS += System.Xml $(MONO_SECURITY_REF)
|
||||
LIB_MCS_FLAGS += -d:SECURITY_DEP -d:XML_DEP $(EXTERN_ALIAS_FLAGS)
|
||||
|
||||
ifndef MOBILE_PROFILE
|
||||
@@ -90,7 +117,7 @@ include ../../build/library.make
|
||||
system_library_deps := \
|
||||
$(secxml_libdir)/System.dll \
|
||||
$(the_libdir_base)System.Xml.dll \
|
||||
$(the_libdir_base)Mono.Security.dll \
|
||||
$(MONO_SECURITY_DLL) \
|
||||
$(bare_libdir)/System.dll
|
||||
|
||||
ifndef MOBILE_PROFILE
|
||||
@@ -99,7 +126,7 @@ endif
|
||||
|
||||
artifacts = $(system_library_deps) \
|
||||
$(bare_libdir)/System.Xml.dll \
|
||||
$(the_libdir_base)Mono.Security.dll \
|
||||
$(MONO_SECURITY_DLL) \
|
||||
$(the_libdir_base)System.Configuration.dll
|
||||
|
||||
.NOTPARALLEL: $(system_library_deps)
|
||||
@@ -114,7 +141,7 @@ $(bare_libdir)/System.dll:
|
||||
endif
|
||||
|
||||
ifneq (secxml/,$(intermediate))
|
||||
$(secxml_libdir)/System.dll: $(bare_libdir)/System.dll $(bare_libdir)/System.Xml.dll $(the_libdir_base)Mono.Security.dll $(bare_libdir)/System.dll
|
||||
$(secxml_libdir)/System.dll: $(bare_libdir)/System.dll $(bare_libdir)/System.Xml.dll $(MONO_SECURITY_DLL) $(bare_libdir)/System.dll
|
||||
$(MAKE) intermediate=secxml/ $(secxml_libdir)/System.dll
|
||||
else
|
||||
build-sec:
|
||||
|
@@ -34,25 +34,16 @@ using System.Security;
|
||||
|
||||
namespace Microsoft.Win32.SafeHandles
|
||||
{
|
||||
public sealed class SafeX509ChainHandle : SafeHandle
|
||||
public sealed class SafeX509ChainHandle : SafeHandleZeroOrMinusOneIsInvalid
|
||||
{
|
||||
[MonoTODO]
|
||||
public override bool IsInvalid
|
||||
{
|
||||
get
|
||||
{
|
||||
throw new NotImplementedException ();
|
||||
}
|
||||
}
|
||||
|
||||
[MonoTODO]
|
||||
internal SafeX509ChainHandle() : base ((IntPtr)0, false)
|
||||
internal SafeX509ChainHandle (IntPtr handle)
|
||||
: base (true)
|
||||
{
|
||||
throw new NotImplementedException ();
|
||||
}
|
||||
|
||||
[MonoTODO]
|
||||
protected override bool ReleaseHandle()
|
||||
protected override bool ReleaseHandle ()
|
||||
{
|
||||
throw new NotImplementedException ();
|
||||
}
|
||||
|
449
mcs/class/System/Mono.Btls/MonoBtlsBio.cs
Normal file
449
mcs/class/System/Mono.Btls/MonoBtlsBio.cs
Normal file
@@ -0,0 +1,449 @@
|
||||
//
|
||||
// MonoBtlsBio.cs
|
||||
//
|
||||
// Author:
|
||||
// Martin Baulig <martin.baulig@xamarin.com>
|
||||
//
|
||||
// Copyright (c) 2016 Xamarin Inc. (http://www.xamarin.com)
|
||||
//
|
||||
// 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 SECURITY_DEP && MONO_FEATURE_BTLS
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
#if MONOTOUCH
|
||||
using MonoTouch;
|
||||
#endif
|
||||
|
||||
namespace Mono.Btls
|
||||
{
|
||||
class MonoBtlsBio : MonoBtlsObject
|
||||
{
|
||||
internal MonoBtlsBio (BoringBioHandle handle)
|
||||
: base (handle)
|
||||
{
|
||||
}
|
||||
|
||||
new protected internal BoringBioHandle Handle {
|
||||
get { return (BoringBioHandle)base.Handle; }
|
||||
}
|
||||
|
||||
protected internal class BoringBioHandle : MonoBtlsHandle
|
||||
{
|
||||
public BoringBioHandle (IntPtr handle)
|
||||
: base (handle, true)
|
||||
{
|
||||
}
|
||||
|
||||
protected override bool ReleaseHandle ()
|
||||
{
|
||||
if (handle != IntPtr.Zero) {
|
||||
mono_btls_bio_free (handle);
|
||||
handle = IntPtr.Zero;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static MonoBtlsBio CreateMonoStream (Stream stream)
|
||||
{
|
||||
return MonoBtlsBioMono.CreateStream (stream, false);
|
||||
}
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_bio_read (IntPtr bio, IntPtr data, int len);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_bio_write (IntPtr bio, IntPtr data, int len);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_bio_flush (IntPtr bio);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_bio_indent (IntPtr bio, uint indent, uint max_indent);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_bio_hexdump (IntPtr bio, IntPtr data, int len, uint indent);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static void mono_btls_bio_print_errors (IntPtr bio);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static void mono_btls_bio_free (IntPtr handle);
|
||||
|
||||
public int Read (byte[] buffer, int offset, int size)
|
||||
{
|
||||
CheckThrow ();
|
||||
var data = Marshal.AllocHGlobal (size);
|
||||
if (data == IntPtr.Zero)
|
||||
throw new OutOfMemoryException ();
|
||||
|
||||
bool release = false;
|
||||
try {
|
||||
Handle.DangerousAddRef (ref release);
|
||||
var ret = mono_btls_bio_read (Handle.DangerousGetHandle (), data, size);
|
||||
if (ret > 0)
|
||||
Marshal.Copy (data, buffer,offset, ret);
|
||||
return ret;
|
||||
} finally {
|
||||
if (release)
|
||||
Handle.DangerousRelease ();
|
||||
Marshal.FreeHGlobal (data);
|
||||
}
|
||||
}
|
||||
|
||||
public int Write (byte[] buffer, int offset, int size)
|
||||
{
|
||||
CheckThrow ();
|
||||
var data = Marshal.AllocHGlobal (size);
|
||||
if (data == IntPtr.Zero)
|
||||
throw new OutOfMemoryException ();
|
||||
|
||||
bool release = false;
|
||||
try {
|
||||
Handle.DangerousAddRef (ref release);
|
||||
Marshal.Copy (buffer, offset, data, size);
|
||||
return mono_btls_bio_write (Handle.DangerousGetHandle (), data, size);
|
||||
} finally {
|
||||
if (release)
|
||||
Handle.DangerousRelease ();
|
||||
Marshal.FreeHGlobal (data);
|
||||
}
|
||||
}
|
||||
|
||||
public int Flush ()
|
||||
{
|
||||
CheckThrow ();
|
||||
bool release = false;
|
||||
try {
|
||||
Handle.DangerousAddRef (ref release);
|
||||
return mono_btls_bio_flush (Handle.DangerousGetHandle ());
|
||||
} finally {
|
||||
if (release)
|
||||
Handle.DangerousRelease ();
|
||||
}
|
||||
}
|
||||
|
||||
public int Indent (uint indent, uint max_indent)
|
||||
{
|
||||
CheckThrow ();
|
||||
bool release = false;
|
||||
try {
|
||||
Handle.DangerousAddRef (ref release);
|
||||
return mono_btls_bio_indent (Handle.DangerousGetHandle (), indent, max_indent);
|
||||
} finally {
|
||||
if (release)
|
||||
Handle.DangerousRelease ();
|
||||
}
|
||||
}
|
||||
|
||||
public int HexDump (byte[] buffer, uint indent)
|
||||
{
|
||||
CheckThrow ();
|
||||
var data = Marshal.AllocHGlobal (buffer.Length);
|
||||
if (data == IntPtr.Zero)
|
||||
throw new OutOfMemoryException ();
|
||||
|
||||
bool release = false;
|
||||
try {
|
||||
Handle.DangerousAddRef (ref release);
|
||||
Marshal.Copy (buffer, 0, data, buffer.Length);
|
||||
return mono_btls_bio_hexdump (Handle.DangerousGetHandle (), data, buffer.Length, indent);
|
||||
} finally {
|
||||
if (release)
|
||||
Handle.DangerousRelease ();
|
||||
Marshal.FreeHGlobal (data);
|
||||
}
|
||||
}
|
||||
|
||||
public void PrintErrors ()
|
||||
{
|
||||
CheckThrow ();
|
||||
bool release = false;
|
||||
try {
|
||||
Handle.DangerousAddRef (ref release);
|
||||
mono_btls_bio_print_errors (Handle.DangerousGetHandle ());
|
||||
} finally {
|
||||
if (release)
|
||||
Handle.DangerousRelease ();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class MonoBtlsBioMemory : MonoBtlsBio
|
||||
{
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static IntPtr mono_btls_bio_mem_new ();
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_bio_mem_get_data (IntPtr handle, out IntPtr data);
|
||||
|
||||
public MonoBtlsBioMemory ()
|
||||
: base (new BoringBioHandle (mono_btls_bio_mem_new ()))
|
||||
{
|
||||
}
|
||||
|
||||
public byte[] GetData ()
|
||||
{
|
||||
IntPtr data;
|
||||
bool release = false;
|
||||
try {
|
||||
Handle.DangerousAddRef (ref release);
|
||||
var size = mono_btls_bio_mem_get_data (Handle.DangerousGetHandle (), out data);
|
||||
CheckError (size > 0);
|
||||
var buffer = new byte[size];
|
||||
Marshal.Copy (data, buffer, 0, size);
|
||||
return buffer;
|
||||
} finally {
|
||||
if (release)
|
||||
Handle.DangerousRelease ();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
interface IMonoBtlsBioMono
|
||||
{
|
||||
int Read (byte[] buffer, int offset, int size, out bool wantMore);
|
||||
|
||||
bool Write (byte[] buffer, int offset, int size);
|
||||
|
||||
void Flush ();
|
||||
|
||||
void Close ();
|
||||
}
|
||||
|
||||
class MonoBtlsBioMono : MonoBtlsBio
|
||||
{
|
||||
GCHandle handle;
|
||||
IntPtr instance;
|
||||
BioReadFunc readFunc;
|
||||
BioWriteFunc writeFunc;
|
||||
BioControlFunc controlFunc;
|
||||
IntPtr readFuncPtr;
|
||||
IntPtr writeFuncPtr;
|
||||
IntPtr controlFuncPtr;
|
||||
IMonoBtlsBioMono backend;
|
||||
|
||||
public MonoBtlsBioMono (IMonoBtlsBioMono backend)
|
||||
: base (new BoringBioHandle (mono_btls_bio_mono_new ()))
|
||||
{
|
||||
this.backend = backend;
|
||||
handle = GCHandle.Alloc (this);
|
||||
instance = GCHandle.ToIntPtr (handle);
|
||||
readFunc = OnRead;
|
||||
writeFunc = OnWrite;
|
||||
controlFunc = Control;
|
||||
readFuncPtr = Marshal.GetFunctionPointerForDelegate (readFunc);
|
||||
writeFuncPtr = Marshal.GetFunctionPointerForDelegate (writeFunc);
|
||||
controlFuncPtr = Marshal.GetFunctionPointerForDelegate (controlFunc);
|
||||
mono_btls_bio_mono_initialize (Handle.DangerousGetHandle (), instance, readFuncPtr, writeFuncPtr, controlFuncPtr);
|
||||
}
|
||||
|
||||
public static MonoBtlsBioMono CreateStream (Stream stream, bool ownsStream)
|
||||
{
|
||||
return new MonoBtlsBioMono (new StreamBackend (stream, ownsStream));
|
||||
}
|
||||
|
||||
public static MonoBtlsBioMono CreateString (StringWriter writer)
|
||||
{
|
||||
return new MonoBtlsBioMono (new StringBackend (writer));
|
||||
}
|
||||
|
||||
enum ControlCommand
|
||||
{
|
||||
Flush = 1
|
||||
}
|
||||
|
||||
delegate int BioReadFunc (IntPtr bio, IntPtr data, int dataLength, out int wantMore);
|
||||
delegate int BioWriteFunc (IntPtr bio, IntPtr data, int dataLength);
|
||||
delegate long BioControlFunc (IntPtr bio, ControlCommand command, long arg);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static IntPtr mono_btls_bio_mono_new ();
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static void mono_btls_bio_mono_initialize (IntPtr handle, IntPtr instance, IntPtr readFunc, IntPtr writeFunc, IntPtr controlFunc);
|
||||
|
||||
long Control (ControlCommand command, long arg)
|
||||
{
|
||||
switch (command) {
|
||||
case ControlCommand.Flush:
|
||||
backend.Flush ();
|
||||
return 1;
|
||||
|
||||
default:
|
||||
throw new NotImplementedException ();
|
||||
}
|
||||
}
|
||||
|
||||
int OnRead (IntPtr data, int dataLength, out int wantMore)
|
||||
{
|
||||
bool wantMoreBool;
|
||||
var buffer = new byte[dataLength];
|
||||
var ret = backend.Read (buffer, 0, dataLength, out wantMoreBool);
|
||||
wantMore = wantMoreBool ? 1 : 0;
|
||||
if (ret <= 0)
|
||||
return ret;
|
||||
Marshal.Copy (buffer, 0, data, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#if MONOTOUCH
|
||||
[MonoPInvokeCallback (typeof (BioReadFunc))]
|
||||
#endif
|
||||
static int OnRead (IntPtr instance, IntPtr data, int dataLength, out int wantMore)
|
||||
{
|
||||
var c = (MonoBtlsBioMono)GCHandle.FromIntPtr (instance).Target;
|
||||
try {
|
||||
return c.OnRead (data, dataLength, out wantMore);
|
||||
} catch (Exception ex) {
|
||||
c.SetException (ex);
|
||||
wantMore = 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int OnWrite (IntPtr data, int dataLength)
|
||||
{
|
||||
var buffer = new byte[dataLength];
|
||||
Marshal.Copy (data, buffer, 0, dataLength);
|
||||
var ok = backend.Write (buffer, 0, dataLength);
|
||||
return ok ? dataLength : -1;
|
||||
}
|
||||
|
||||
#if MONOTOUCH
|
||||
[MonoPInvokeCallback (typeof (BioWriteFunc))]
|
||||
#endif
|
||||
static int OnWrite (IntPtr instance, IntPtr data, int dataLength)
|
||||
{
|
||||
var c = (MonoBtlsBioMono)GCHandle.FromIntPtr (instance).Target;
|
||||
try {
|
||||
return c.OnWrite (data, dataLength);
|
||||
} catch (Exception ex) {
|
||||
c.SetException (ex);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
#if MONOTOUCH
|
||||
[MonoPInvokeCallback (typeof (BioControlFunc))]
|
||||
#endif
|
||||
static long Control (IntPtr instance, ControlCommand command, long arg)
|
||||
{
|
||||
var c = (MonoBtlsBioMono)GCHandle.FromIntPtr (instance).Target;
|
||||
try {
|
||||
return c.Control (command, arg);
|
||||
} catch (Exception ex) {
|
||||
c.SetException (ex);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Close ()
|
||||
{
|
||||
try {
|
||||
if (backend != null) {
|
||||
backend.Close ();
|
||||
backend = null;
|
||||
}
|
||||
if (handle.IsAllocated)
|
||||
handle.Free ();
|
||||
} finally {
|
||||
base.Close ();
|
||||
}
|
||||
}
|
||||
|
||||
class StreamBackend : IMonoBtlsBioMono
|
||||
{
|
||||
Stream stream;
|
||||
bool ownsStream;
|
||||
|
||||
public Stream InnerStream {
|
||||
get { return stream; }
|
||||
}
|
||||
|
||||
public StreamBackend (Stream stream, bool ownsStream)
|
||||
{
|
||||
this.stream = stream;
|
||||
this.ownsStream = ownsStream;
|
||||
}
|
||||
|
||||
public int Read (byte[] buffer, int offset, int size, out bool wantMore)
|
||||
{
|
||||
wantMore = false;
|
||||
return stream.Read (buffer, offset, size);
|
||||
}
|
||||
|
||||
public bool Write (byte[] buffer, int offset, int size)
|
||||
{
|
||||
stream.Write (buffer, offset, size);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Flush ()
|
||||
{
|
||||
stream.Flush ();
|
||||
}
|
||||
|
||||
public void Close ()
|
||||
{
|
||||
if (ownsStream && stream != null)
|
||||
stream.Dispose ();
|
||||
stream = null;
|
||||
}
|
||||
}
|
||||
|
||||
class StringBackend : IMonoBtlsBioMono
|
||||
{
|
||||
StringWriter writer;
|
||||
Encoding encoding = new UTF8Encoding ();
|
||||
|
||||
public StringBackend (StringWriter writer)
|
||||
{
|
||||
this.writer = writer;
|
||||
}
|
||||
|
||||
public int Read (byte[] buffer, int offset, int size, out bool wantMore)
|
||||
{
|
||||
wantMore = false;
|
||||
return -1;
|
||||
}
|
||||
|
||||
public bool Write (byte[] buffer, int offset, int size)
|
||||
{
|
||||
var text = encoding.GetString (buffer, offset, size);
|
||||
writer.Write (text);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Flush ()
|
||||
{
|
||||
}
|
||||
|
||||
public void Close ()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
454
mcs/class/System/Mono.Btls/MonoBtlsContext.cs
Normal file
454
mcs/class/System/Mono.Btls/MonoBtlsContext.cs
Normal file
@@ -0,0 +1,454 @@
|
||||
//
|
||||
// MonoBtlsContext.cs
|
||||
//
|
||||
// Author:
|
||||
// Martin Baulig <martin.baulig@xamarin.com>
|
||||
//
|
||||
// Copyright (c) 2016 Xamarin Inc. (http://www.xamarin.com)
|
||||
//
|
||||
// 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 SECURITY_DEP && MONO_FEATURE_BTLS
|
||||
#if MONO_SECURITY_ALIAS
|
||||
extern alias MonoSecurity;
|
||||
#endif
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Security.Authentication;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
#if MONO_SECURITY_ALIAS
|
||||
using MonoSecurity::Mono.Security.Interface;
|
||||
#else
|
||||
using Mono.Security.Interface;
|
||||
#endif
|
||||
|
||||
using MNS = Mono.Net.Security;
|
||||
|
||||
namespace Mono.Btls
|
||||
{
|
||||
class MonoBtlsContext : MNS.MobileTlsContext, IMonoBtlsBioMono
|
||||
{
|
||||
X509Certificate remoteCertificate;
|
||||
X509Certificate clientCertificate;
|
||||
X509CertificateImplBtls nativeServerCertificate;
|
||||
X509CertificateImplBtls nativeClientCertificate;
|
||||
MonoBtlsSslCtx ctx;
|
||||
MonoBtlsSsl ssl;
|
||||
MonoBtlsBio bio;
|
||||
MonoBtlsBio errbio;
|
||||
|
||||
MonoTlsConnectionInfo connectionInfo;
|
||||
bool certificateValidated;
|
||||
bool isAuthenticated;
|
||||
bool connected;
|
||||
|
||||
public MonoBtlsContext (
|
||||
MNS.MobileAuthenticatedStream parent,
|
||||
bool serverMode, string targetHost,
|
||||
SslProtocols enabledProtocols, X509Certificate serverCertificate,
|
||||
X509CertificateCollection clientCertificates, bool askForClientCert)
|
||||
: base (parent, serverMode, targetHost, enabledProtocols,
|
||||
serverCertificate, clientCertificates, askForClientCert)
|
||||
{
|
||||
if (serverMode)
|
||||
nativeServerCertificate = GetPrivateCertificate (serverCertificate);
|
||||
}
|
||||
|
||||
static X509CertificateImplBtls GetPrivateCertificate (X509Certificate certificate)
|
||||
{
|
||||
var impl = certificate.Impl as X509CertificateImplBtls;
|
||||
if (impl != null)
|
||||
return (X509CertificateImplBtls)impl.Clone ();
|
||||
|
||||
var password = Guid.NewGuid ().ToString ();
|
||||
var buffer = certificate.Export (X509ContentType.Pfx, password);
|
||||
|
||||
impl = new X509CertificateImplBtls ();
|
||||
impl.Import (buffer, password, X509KeyStorageFlags.DefaultKeySet);
|
||||
return impl;
|
||||
}
|
||||
|
||||
new public MonoBtlsProvider Provider {
|
||||
get { return (MonoBtlsProvider)base.Provider; }
|
||||
}
|
||||
|
||||
int VerifyCallback (MonoBtlsX509StoreCtx storeCtx)
|
||||
{
|
||||
using (var chainImpl = new X509ChainImplBtls (storeCtx))
|
||||
using (var managedChain = new X509Chain (chainImpl)) {
|
||||
var leaf = managedChain.ChainElements[0].Certificate;
|
||||
var result = ValidateCertificate (leaf, managedChain);
|
||||
certificateValidated = true;
|
||||
return result ? 1 : 0;
|
||||
}
|
||||
}
|
||||
|
||||
int SelectCallback ()
|
||||
{
|
||||
Debug ("SELECT CALLBACK!");
|
||||
|
||||
GetPeerCertificate ();
|
||||
if (remoteCertificate == null)
|
||||
throw new TlsException (AlertDescription.InternalError, "Cannot request client certificate before receiving one from the server.");
|
||||
|
||||
var clientCert = SelectClientCertificate (remoteCertificate, null);
|
||||
Debug ("SELECT CALLBACK #1: {0}", clientCert);
|
||||
if (clientCert == null)
|
||||
return 1;
|
||||
|
||||
nativeClientCertificate = GetPrivateCertificate (clientCert);
|
||||
Debug ("SELECT CALLBACK #2: {0}", nativeClientCertificate);
|
||||
clientCertificate = new X509Certificate (nativeClientCertificate);
|
||||
SetPrivateCertificate (nativeClientCertificate);
|
||||
return 1;
|
||||
}
|
||||
|
||||
public override void StartHandshake ()
|
||||
{
|
||||
InitializeConnection ();
|
||||
|
||||
ssl = new MonoBtlsSsl (ctx);
|
||||
|
||||
bio = new MonoBtlsBioMono (this);
|
||||
ssl.SetBio (bio);
|
||||
|
||||
if (IsServer) {
|
||||
SetPrivateCertificate (nativeServerCertificate);
|
||||
} else {
|
||||
ssl.SetServerName (TargetHost);
|
||||
}
|
||||
}
|
||||
|
||||
void SetPrivateCertificate (X509CertificateImplBtls privateCert)
|
||||
{
|
||||
Debug ("SetPrivateCertificate: {0}", privateCert);
|
||||
ssl.SetCertificate (privateCert.X509);
|
||||
ssl.SetPrivateKey (privateCert.NativePrivateKey);
|
||||
var intermediate = privateCert.IntermediateCertificates;
|
||||
if (intermediate == null)
|
||||
return;
|
||||
for (int i = 0; i < intermediate.Count; i++) {
|
||||
var impl = (X509CertificateImplBtls)intermediate [i];
|
||||
Debug ("SetPrivateCertificate - add intermediate: {0}", impl);
|
||||
ssl.AddIntermediateCertificate (impl.X509);
|
||||
}
|
||||
}
|
||||
|
||||
Exception GetException (MonoBtlsSslError status)
|
||||
{
|
||||
var error = MonoBtlsError.GetError ();
|
||||
if (error == null)
|
||||
return new MonoBtlsException (status);
|
||||
|
||||
var text = MonoBtlsError.GetErrorString (error);
|
||||
return new MonoBtlsException ("{0} {1}", status, text);
|
||||
}
|
||||
|
||||
public override bool ProcessHandshake ()
|
||||
{
|
||||
var done = false;
|
||||
while (!done) {
|
||||
Debug ("ProcessHandshake");
|
||||
MonoBtlsError.ClearError ();
|
||||
var status = DoProcessHandshake ();
|
||||
Debug ("ProcessHandshake #1: {0}", status);
|
||||
|
||||
switch (status) {
|
||||
case MonoBtlsSslError.None:
|
||||
if (connected)
|
||||
done = true;
|
||||
else
|
||||
connected = true;
|
||||
break;
|
||||
case MonoBtlsSslError.WantRead:
|
||||
case MonoBtlsSslError.WantWrite:
|
||||
return false;
|
||||
default:
|
||||
throw GetException (status);
|
||||
}
|
||||
}
|
||||
|
||||
ssl.PrintErrors ();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
MonoBtlsSslError DoProcessHandshake ()
|
||||
{
|
||||
if (connected)
|
||||
return ssl.Handshake ();
|
||||
else if (IsServer)
|
||||
return ssl.Accept ();
|
||||
else
|
||||
return ssl.Connect ();
|
||||
}
|
||||
|
||||
public override void FinishHandshake ()
|
||||
{
|
||||
InitializeSession ();
|
||||
|
||||
isAuthenticated = true;
|
||||
}
|
||||
|
||||
void SetupCertificateStore ()
|
||||
{
|
||||
MonoBtlsProvider.SetupCertificateStore (ctx.CertificateStore);
|
||||
|
||||
if (Settings != null && Settings.TrustAnchors != null) {
|
||||
var trust = IsServer ? MonoBtlsX509TrustKind.TRUST_CLIENT : MonoBtlsX509TrustKind.TRUST_SERVER;
|
||||
ctx.CertificateStore.AddCollection (Settings.TrustAnchors, trust);
|
||||
}
|
||||
}
|
||||
|
||||
void InitializeConnection ()
|
||||
{
|
||||
ctx = new MonoBtlsSslCtx ();
|
||||
|
||||
#if MARTIN_DEBUG
|
||||
errbio = MonoBtlsBio.CreateMonoStream (Console.OpenStandardError ());
|
||||
ctx.SetDebugBio (errbio);
|
||||
#endif
|
||||
|
||||
SetupCertificateStore ();
|
||||
|
||||
if (!IsServer || AskForClientCertificate)
|
||||
ctx.SetVerifyCallback (VerifyCallback, false);
|
||||
if (!IsServer)
|
||||
ctx.SetSelectCallback (SelectCallback);
|
||||
|
||||
var host = TargetHost;
|
||||
if (!string.IsNullOrEmpty (host)) {
|
||||
var pos = TargetHost.IndexOf (':');
|
||||
if (pos > 0)
|
||||
host = host.Substring (0, pos);
|
||||
}
|
||||
|
||||
ctx.SetVerifyParam (MonoBtlsProvider.GetVerifyParam (host, IsServer));
|
||||
|
||||
TlsProtocolCode minProtocol, maxProtocol;
|
||||
GetProtocolVersions (out minProtocol, out maxProtocol);
|
||||
|
||||
ctx.SetMinVersion ((int)minProtocol);
|
||||
ctx.SetMaxVersion ((int)maxProtocol);
|
||||
|
||||
if (Settings != null && Settings.EnabledCiphers != null) {
|
||||
var ciphers = new short [Settings.EnabledCiphers.Length];
|
||||
for (int i = 0; i < ciphers.Length; i++)
|
||||
ciphers [i] = (short)Settings.EnabledCiphers [i];
|
||||
ctx.SetCiphers (ciphers, true);
|
||||
}
|
||||
}
|
||||
|
||||
void GetPeerCertificate ()
|
||||
{
|
||||
if (remoteCertificate != null)
|
||||
return;
|
||||
using (var remoteCert = ssl.GetPeerCertificate ()) {
|
||||
if (remoteCert != null)
|
||||
remoteCertificate = MonoBtlsProvider.CreateCertificate (remoteCert);
|
||||
}
|
||||
}
|
||||
|
||||
void InitializeSession ()
|
||||
{
|
||||
GetPeerCertificate ();
|
||||
|
||||
if (IsServer && AskForClientCertificate && !certificateValidated) {
|
||||
if (!ValidateCertificate (null, null))
|
||||
throw new TlsException (AlertDescription.CertificateUnknown);
|
||||
}
|
||||
|
||||
var cipher = (CipherSuiteCode)ssl.GetCipher ();
|
||||
var protocol = (TlsProtocolCode)ssl.GetVersion ();
|
||||
Debug ("GET CONNECTION INFO: {0:x}:{0} {1:x}:{1} {2}", cipher, protocol, (TlsProtocolCode)protocol);
|
||||
|
||||
connectionInfo = new MonoTlsConnectionInfo {
|
||||
CipherSuiteCode = cipher,
|
||||
ProtocolVersion = GetProtocol (protocol)
|
||||
};
|
||||
}
|
||||
|
||||
static TlsProtocols GetProtocol (TlsProtocolCode protocol)
|
||||
{
|
||||
switch (protocol) {
|
||||
case TlsProtocolCode.Tls10:
|
||||
return TlsProtocols.Tls10;
|
||||
case TlsProtocolCode.Tls11:
|
||||
return TlsProtocols.Tls11;
|
||||
case TlsProtocolCode.Tls12:
|
||||
return TlsProtocols.Tls12;
|
||||
default:
|
||||
throw new NotSupportedException ();
|
||||
}
|
||||
}
|
||||
|
||||
public override void Flush ()
|
||||
{
|
||||
throw new NotImplementedException ();
|
||||
}
|
||||
|
||||
public override int Read (byte[] buffer, int offset, int size, out bool wantMore)
|
||||
{
|
||||
Debug ("Read: {0} {1} {2}", buffer.Length, offset, size);
|
||||
|
||||
var data = Marshal.AllocHGlobal (size);
|
||||
if (data == IntPtr.Zero)
|
||||
throw new OutOfMemoryException ();
|
||||
|
||||
try {
|
||||
MonoBtlsError.ClearError ();
|
||||
var status = ssl.Read (data, ref size);
|
||||
Debug ("Read done: {0} {1}", status, size);
|
||||
|
||||
if (status == MonoBtlsSslError.WantRead) {
|
||||
wantMore = true;
|
||||
return 0;
|
||||
} else if (status != MonoBtlsSslError.None) {
|
||||
throw GetException (status);
|
||||
}
|
||||
|
||||
if (size > 0)
|
||||
Marshal.Copy (data, buffer, offset, size);
|
||||
|
||||
wantMore = false;
|
||||
return size;
|
||||
} finally {
|
||||
Marshal.FreeHGlobal (data);
|
||||
}
|
||||
}
|
||||
|
||||
public override int Write (byte[] buffer, int offset, int size, out bool wantMore)
|
||||
{
|
||||
Debug ("Write: {0} {1} {2}", buffer.Length, offset, size);
|
||||
|
||||
var data = Marshal.AllocHGlobal (size);
|
||||
if (data == IntPtr.Zero)
|
||||
throw new OutOfMemoryException ();
|
||||
|
||||
try {
|
||||
MonoBtlsError.ClearError ();
|
||||
Marshal.Copy (buffer, offset, data, size);
|
||||
var status = ssl.Write (data, ref size);
|
||||
Debug ("Write done: {0} {1}", status, size);
|
||||
|
||||
if (status == MonoBtlsSslError.WantWrite) {
|
||||
wantMore = true;
|
||||
return 0;
|
||||
} else if (status != MonoBtlsSslError.None) {
|
||||
throw GetException (status);
|
||||
}
|
||||
|
||||
wantMore = false;
|
||||
return size;
|
||||
} finally {
|
||||
Marshal.FreeHGlobal (data);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Close ()
|
||||
{
|
||||
Debug ("Close!");
|
||||
ssl.Dispose ();
|
||||
}
|
||||
|
||||
void Dispose<T> (ref T disposable)
|
||||
where T : class, IDisposable
|
||||
{
|
||||
try {
|
||||
if (disposable != null)
|
||||
disposable.Dispose ();
|
||||
} catch {
|
||||
;
|
||||
} finally {
|
||||
disposable = null;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Dispose (bool disposing)
|
||||
{
|
||||
try {
|
||||
if (disposing) {
|
||||
Dispose (ref remoteCertificate);
|
||||
Dispose (ref nativeServerCertificate);
|
||||
Dispose (ref nativeClientCertificate);
|
||||
Dispose (ref clientCertificate);
|
||||
Dispose (ref ctx);
|
||||
Dispose (ref ssl);
|
||||
Dispose (ref bio);
|
||||
Dispose (ref errbio);
|
||||
}
|
||||
} finally {
|
||||
base.Dispose (disposing);
|
||||
}
|
||||
}
|
||||
|
||||
int IMonoBtlsBioMono.Read (byte[] buffer, int offset, int size, out bool wantMore)
|
||||
{
|
||||
Debug ("InternalRead: {0} {1}", offset, size);
|
||||
var ret = Parent.InternalRead (buffer, offset, size, out wantMore);
|
||||
Debug ("InternalReadDone: {0} {1}", ret, wantMore);
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool IMonoBtlsBioMono.Write (byte[] buffer, int offset, int size)
|
||||
{
|
||||
Debug ("InternalWrite: {0} {1}", offset, size);
|
||||
var ret = Parent.InternalWrite (buffer, offset, size);
|
||||
Debug ("InternalWrite done: {0}", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void IMonoBtlsBioMono.Flush ()
|
||||
{
|
||||
;
|
||||
}
|
||||
|
||||
void IMonoBtlsBioMono.Close ()
|
||||
{
|
||||
;
|
||||
}
|
||||
|
||||
public override bool HasContext {
|
||||
get { return ssl != null && ssl.IsValid; }
|
||||
}
|
||||
public override bool IsAuthenticated {
|
||||
get { return isAuthenticated; }
|
||||
}
|
||||
public override MonoTlsConnectionInfo ConnectionInfo {
|
||||
get { return connectionInfo; }
|
||||
}
|
||||
internal override bool IsRemoteCertificateAvailable {
|
||||
get { return remoteCertificate != null; }
|
||||
}
|
||||
internal override X509Certificate LocalClientCertificate {
|
||||
get { return clientCertificate; }
|
||||
}
|
||||
public override X509Certificate RemoteCertificate {
|
||||
get { return remoteCertificate; }
|
||||
}
|
||||
public override TlsProtocols NegotiatedProtocol {
|
||||
get { return connectionInfo.ProtocolVersion; }
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
83
mcs/class/System/Mono.Btls/MonoBtlsError.cs
Normal file
83
mcs/class/System/Mono.Btls/MonoBtlsError.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
//
|
||||
// MonoBtlsError.cs
|
||||
//
|
||||
// Author:
|
||||
// Martin Baulig <martin.baulig@xamarin.com>
|
||||
//
|
||||
// Copyright (c) 2015 Xamarin Inc. (http://www.xamarin.com)
|
||||
//
|
||||
// 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 SECURITY_DEP && MONO_FEATURE_BTLS
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
#if MONOTOUCH
|
||||
using MonoTouch;
|
||||
#endif
|
||||
|
||||
namespace Mono.Btls
|
||||
{
|
||||
static class MonoBtlsError
|
||||
{
|
||||
[DllImport (MonoBtlsObject.BTLS_DYLIB)]
|
||||
extern static int mono_btls_error_peek_error ();
|
||||
|
||||
[DllImport (MonoBtlsObject.BTLS_DYLIB)]
|
||||
extern static int mono_btls_error_get_error ();
|
||||
|
||||
[DllImport (MonoBtlsObject.BTLS_DYLIB)]
|
||||
extern static void mono_btls_error_clear_error ();
|
||||
|
||||
[DllImport (MonoBtlsObject.BTLS_DYLIB)]
|
||||
extern static void mono_btls_error_get_error_string_n (int error, IntPtr buf, int len);
|
||||
|
||||
public static int PeekError ()
|
||||
{
|
||||
return mono_btls_error_peek_error ();
|
||||
}
|
||||
|
||||
public static int GetError ()
|
||||
{
|
||||
return mono_btls_error_get_error ();
|
||||
}
|
||||
|
||||
public static void ClearError ()
|
||||
{
|
||||
mono_btls_error_clear_error ();
|
||||
}
|
||||
|
||||
public static string GetErrorString (int error)
|
||||
{
|
||||
var size = 1024;
|
||||
var buffer = Marshal.AllocHGlobal (size);
|
||||
if (buffer == IntPtr.Zero)
|
||||
throw new OutOfMemoryException ();
|
||||
try {
|
||||
mono_btls_error_get_error_string_n (error, buffer, size);
|
||||
return Marshal.PtrToStringAnsi (buffer);
|
||||
} finally {
|
||||
Marshal.FreeHGlobal (buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
53
mcs/class/System/Mono.Btls/MonoBtlsException.cs
Normal file
53
mcs/class/System/Mono.Btls/MonoBtlsException.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
//
|
||||
// MonoBtlsException.cs
|
||||
//
|
||||
// Author:
|
||||
// Martin Baulig <martin.baulig@xamarin.com>
|
||||
//
|
||||
// Copyright (c) 2016 Xamarin Inc. (http://www.xamarin.com)
|
||||
//
|
||||
// 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 SECURITY_DEP && MONO_FEATURE_BTLS
|
||||
using System;
|
||||
|
||||
namespace Mono.Btls
|
||||
{
|
||||
class MonoBtlsException : Exception
|
||||
{
|
||||
public MonoBtlsException ()
|
||||
{
|
||||
}
|
||||
|
||||
public MonoBtlsException (MonoBtlsSslError error)
|
||||
: base (error.ToString ())
|
||||
{
|
||||
}
|
||||
|
||||
public MonoBtlsException (string message)
|
||||
: base (message)
|
||||
{
|
||||
}
|
||||
|
||||
public MonoBtlsException (string format, params object[] args)
|
||||
: base (string.Format (format, args))
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
104
mcs/class/System/Mono.Btls/MonoBtlsKey.cs
Normal file
104
mcs/class/System/Mono.Btls/MonoBtlsKey.cs
Normal file
@@ -0,0 +1,104 @@
|
||||
//
|
||||
// MonoBtlsKey.cs
|
||||
//
|
||||
// Author:
|
||||
// Martin Baulig <martin.baulig@xamarin.com>
|
||||
//
|
||||
// Copyright (c) 2016 Xamarin Inc. (http://www.xamarin.com)
|
||||
//
|
||||
// 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 SECURITY_DEP && MONO_FEATURE_BTLS
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Mono.Btls
|
||||
{
|
||||
class MonoBtlsKey : MonoBtlsObject
|
||||
{
|
||||
internal class BoringKeyHandle : MonoBtlsHandle
|
||||
{
|
||||
internal BoringKeyHandle (IntPtr handle)
|
||||
: base (handle, true)
|
||||
{
|
||||
}
|
||||
|
||||
protected override bool ReleaseHandle ()
|
||||
{
|
||||
mono_btls_key_free (handle);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static void mono_btls_key_free (IntPtr handle);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static IntPtr mono_btls_key_up_ref (IntPtr handle);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_key_get_bytes (IntPtr handle, out IntPtr data, out int size, int include_private_bits);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_key_get_bits (IntPtr handle);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_key_is_rsa (IntPtr handle);
|
||||
|
||||
new internal BoringKeyHandle Handle {
|
||||
get { return (BoringKeyHandle)base.Handle; }
|
||||
}
|
||||
|
||||
internal MonoBtlsKey (BoringKeyHandle handle)
|
||||
: base (handle)
|
||||
{
|
||||
}
|
||||
|
||||
public byte[] GetBytes (bool include_private_bits)
|
||||
{
|
||||
int size;
|
||||
IntPtr data;
|
||||
|
||||
var ret = mono_btls_key_get_bytes (Handle.DangerousGetHandle (), out data, out size, include_private_bits ? 1 : 0);
|
||||
CheckError (ret);
|
||||
|
||||
var buffer = new byte [size];
|
||||
Marshal.Copy (data, buffer, 0, size);
|
||||
FreeDataPtr (data);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public bool IsRsa {
|
||||
get {
|
||||
return mono_btls_key_is_rsa (Handle.DangerousGetHandle ()) != 0;
|
||||
}
|
||||
}
|
||||
|
||||
public MonoBtlsKey Copy ()
|
||||
{
|
||||
CheckThrow ();
|
||||
var copy = mono_btls_key_up_ref (Handle.DangerousGetHandle ());
|
||||
CheckError (copy != IntPtr.Zero);
|
||||
return new MonoBtlsKey (new BoringKeyHandle (copy));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
149
mcs/class/System/Mono.Btls/MonoBtlsObject.cs
Normal file
149
mcs/class/System/Mono.Btls/MonoBtlsObject.cs
Normal file
@@ -0,0 +1,149 @@
|
||||
//
|
||||
// MonoBtlsObject.cs
|
||||
//
|
||||
// Author:
|
||||
// Martin Baulig <martin.baulig@xamarin.com>
|
||||
//
|
||||
// Copyright (c) 2016 Xamarin Inc. (http://www.xamarin.com)
|
||||
//
|
||||
// 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 SECURITY_DEP && MONO_FEATURE_BTLS
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Mono.Btls
|
||||
{
|
||||
abstract class MonoBtlsObject : IDisposable
|
||||
{
|
||||
#if MONO_FEATURE_DYNAMIC_BTLS
|
||||
internal const string BTLS_DYLIB = "libmono-btls-shared";
|
||||
#else
|
||||
internal const string BTLS_DYLIB = "__Internal";
|
||||
#endif
|
||||
|
||||
internal MonoBtlsObject (MonoBtlsHandle handle)
|
||||
{
|
||||
this.handle = handle;
|
||||
}
|
||||
|
||||
protected internal abstract class MonoBtlsHandle : SafeHandle
|
||||
{
|
||||
internal MonoBtlsHandle ()
|
||||
: base (IntPtr.Zero, true)
|
||||
{
|
||||
}
|
||||
|
||||
internal MonoBtlsHandle (IntPtr handle, bool ownsHandle)
|
||||
: base (handle, ownsHandle)
|
||||
{
|
||||
}
|
||||
|
||||
public override bool IsInvalid {
|
||||
get { return handle == IntPtr.Zero; }
|
||||
}
|
||||
}
|
||||
|
||||
internal MonoBtlsHandle Handle {
|
||||
get {
|
||||
CheckThrow ();
|
||||
return handle;
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsValid {
|
||||
get { return handle != null && !handle.IsInvalid; }
|
||||
}
|
||||
|
||||
MonoBtlsHandle handle;
|
||||
Exception lastError;
|
||||
|
||||
protected void CheckThrow ()
|
||||
{
|
||||
if (lastError != null)
|
||||
throw lastError;
|
||||
if (handle == null || handle.IsInvalid)
|
||||
throw new ObjectDisposedException ("MonoBtlsSsl");
|
||||
}
|
||||
|
||||
protected Exception SetException (Exception ex)
|
||||
{
|
||||
if (lastError == null)
|
||||
lastError = ex;
|
||||
return ex;
|
||||
}
|
||||
|
||||
protected void CheckError (bool ok, [CallerMemberName] string callerName = null)
|
||||
{
|
||||
if (!ok) {
|
||||
if (callerName != null)
|
||||
throw new MonoBtlsException ("{0}.{1} failed.", GetType ().Name, callerName);
|
||||
else
|
||||
throw new MonoBtlsException ();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected void CheckError (int ret, [CallerMemberName] string callerName = null)
|
||||
{
|
||||
CheckError (ret == 1, callerName);
|
||||
}
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static void mono_btls_free (IntPtr data);
|
||||
|
||||
protected void FreeDataPtr (IntPtr data)
|
||||
{
|
||||
mono_btls_free (data);
|
||||
}
|
||||
|
||||
protected virtual void Close ()
|
||||
{
|
||||
}
|
||||
|
||||
protected void Dispose (bool disposing)
|
||||
{
|
||||
if (disposing) {
|
||||
try {
|
||||
if (handle != null) {
|
||||
Close ();
|
||||
handle.Dispose ();
|
||||
handle = null;
|
||||
}
|
||||
} finally {
|
||||
var disposedExc = new ObjectDisposedException (GetType ().Name);
|
||||
Interlocked.CompareExchange (ref lastError, disposedExc, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose ()
|
||||
{
|
||||
Dispose (true);
|
||||
GC.SuppressFinalize (this);
|
||||
}
|
||||
|
||||
~MonoBtlsObject ()
|
||||
{
|
||||
Dispose (false);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
145
mcs/class/System/Mono.Btls/MonoBtlsPkcs12.cs
Normal file
145
mcs/class/System/Mono.Btls/MonoBtlsPkcs12.cs
Normal file
@@ -0,0 +1,145 @@
|
||||
//
|
||||
// MonoBtlsPkcs12.cs
|
||||
//
|
||||
// Author:
|
||||
// Martin Baulig <martin.baulig@xamarin.com>
|
||||
//
|
||||
// Copyright (c) 2015 Xamarin Inc. (http://www.xamarin.com)
|
||||
//
|
||||
// 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 SECURITY_DEP && MONO_FEATURE_BTLS
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Mono.Btls
|
||||
{
|
||||
class MonoBtlsPkcs12 : MonoBtlsObject
|
||||
{
|
||||
internal class BoringPkcs12Handle : MonoBtlsHandle
|
||||
{
|
||||
public BoringPkcs12Handle (IntPtr handle)
|
||||
: base (handle, true)
|
||||
{
|
||||
}
|
||||
|
||||
protected override bool ReleaseHandle ()
|
||||
{
|
||||
mono_btls_pkcs12_free (handle);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
new internal BoringPkcs12Handle Handle {
|
||||
get { return (BoringPkcs12Handle)base.Handle; }
|
||||
}
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static void mono_btls_pkcs12_free (IntPtr handle);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static IntPtr mono_btls_pkcs12_new ();
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_pkcs12_get_count (IntPtr handle);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static IntPtr mono_btls_pkcs12_get_cert (IntPtr Handle, int index);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_pkcs12_add_cert (IntPtr chain, IntPtr x509);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern unsafe static int mono_btls_pkcs12_import (IntPtr chain, void* data, int len, IntPtr password);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_pkcs12_has_private_key (IntPtr pkcs12);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static IntPtr mono_btls_pkcs12_get_private_key (IntPtr pkcs12);
|
||||
|
||||
internal MonoBtlsPkcs12 ()
|
||||
: base (new BoringPkcs12Handle (mono_btls_pkcs12_new ()))
|
||||
{
|
||||
}
|
||||
|
||||
internal MonoBtlsPkcs12 (BoringPkcs12Handle handle)
|
||||
: base (handle)
|
||||
{
|
||||
}
|
||||
|
||||
MonoBtlsKey privateKey;
|
||||
|
||||
public int Count {
|
||||
get { return mono_btls_pkcs12_get_count (Handle.DangerousGetHandle ()); }
|
||||
}
|
||||
|
||||
public MonoBtlsX509 GetCertificate (int index)
|
||||
{
|
||||
if (index >= Count)
|
||||
throw new IndexOutOfRangeException ();
|
||||
var handle = mono_btls_pkcs12_get_cert (Handle.DangerousGetHandle (), index);
|
||||
CheckError (handle != IntPtr.Zero);
|
||||
return new MonoBtlsX509 (new MonoBtlsX509.BoringX509Handle (handle));
|
||||
}
|
||||
|
||||
public void AddCertificate (MonoBtlsX509 x509)
|
||||
{
|
||||
mono_btls_pkcs12_add_cert (
|
||||
Handle.DangerousGetHandle (),
|
||||
x509.Handle.DangerousGetHandle ());
|
||||
}
|
||||
|
||||
public unsafe void Import (byte[] buffer, string password)
|
||||
{
|
||||
var passptr = IntPtr.Zero;
|
||||
fixed (void* ptr = buffer)
|
||||
try {
|
||||
if (password != null)
|
||||
passptr = Marshal.StringToHGlobalAnsi (password);
|
||||
var ret = mono_btls_pkcs12_import (
|
||||
Handle.DangerousGetHandle (), ptr,
|
||||
buffer.Length, passptr);
|
||||
CheckError (ret);
|
||||
} finally {
|
||||
if (passptr != IntPtr.Zero)
|
||||
Marshal.FreeHGlobal (passptr);
|
||||
}
|
||||
}
|
||||
|
||||
public bool HasPrivateKey {
|
||||
get { return mono_btls_pkcs12_has_private_key (Handle.DangerousGetHandle ()) != 0; }
|
||||
}
|
||||
|
||||
public MonoBtlsKey GetPrivateKey ()
|
||||
{
|
||||
if (!HasPrivateKey)
|
||||
throw new InvalidOperationException ();
|
||||
if (privateKey == null) {
|
||||
var handle = mono_btls_pkcs12_get_private_key (Handle.DangerousGetHandle ());
|
||||
CheckError (handle != IntPtr.Zero);
|
||||
privateKey = new MonoBtlsKey (new MonoBtlsKey.BoringKeyHandle (handle));
|
||||
}
|
||||
return privateKey;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
295
mcs/class/System/Mono.Btls/MonoBtlsProvider.cs
Normal file
295
mcs/class/System/Mono.Btls/MonoBtlsProvider.cs
Normal file
@@ -0,0 +1,295 @@
|
||||
//
|
||||
// MonoBtlsProvider.cs
|
||||
//
|
||||
// Author:
|
||||
// Martin Baulig <martin.baulig@xamarin.com>
|
||||
//
|
||||
// Copyright (c) 2016 Xamarin Inc. (http://www.xamarin.com)
|
||||
//
|
||||
// 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 SECURITY_DEP && MONO_FEATURE_BTLS
|
||||
#if MONO_SECURITY_ALIAS
|
||||
extern alias MonoSecurity;
|
||||
#endif
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Security.Authentication;
|
||||
|
||||
#if MONO_SECURITY_ALIAS
|
||||
using MonoSecurity::Mono.Security.Interface;
|
||||
using MX = MonoSecurity::Mono.Security.X509;
|
||||
#else
|
||||
using Mono.Security.Interface;
|
||||
using MX = Mono.Security.X509;
|
||||
#endif
|
||||
|
||||
using MNS = Mono.Net.Security;
|
||||
|
||||
namespace Mono.Btls
|
||||
{
|
||||
class MonoBtlsProvider : MonoTlsProvider
|
||||
{
|
||||
static readonly Guid id = new Guid ("432d18c9-9348-4b90-bfbf-9f2a10e1f15b");
|
||||
|
||||
public override Guid ID {
|
||||
get { return id; }
|
||||
}
|
||||
public override string Name {
|
||||
get { return "btls"; }
|
||||
}
|
||||
|
||||
internal MonoBtlsProvider ()
|
||||
{
|
||||
if (!MNS.MonoTlsProviderFactory.IsBtlsSupported ())
|
||||
throw new NotSupportedException ("BTLS is not supported in this runtime.");
|
||||
}
|
||||
|
||||
public override bool SupportsSslStream {
|
||||
get { return true; }
|
||||
}
|
||||
|
||||
public override bool SupportsMonoExtensions {
|
||||
get { return true; }
|
||||
}
|
||||
|
||||
public override bool SupportsConnectionInfo {
|
||||
get { return true; }
|
||||
}
|
||||
|
||||
public override SslProtocols SupportedProtocols {
|
||||
get { return SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls; }
|
||||
}
|
||||
|
||||
public override IMonoSslStream CreateSslStream (
|
||||
Stream innerStream, bool leaveInnerStreamOpen,
|
||||
MonoTlsSettings settings = null)
|
||||
{
|
||||
return new MonoBtlsStream (
|
||||
innerStream, leaveInnerStreamOpen, settings, this);
|
||||
}
|
||||
|
||||
internal override bool HasNativeCertificates {
|
||||
get { return true; }
|
||||
}
|
||||
|
||||
internal override X509Certificate2Impl GetNativeCertificate (
|
||||
byte[] data, string password, X509KeyStorageFlags flags)
|
||||
{
|
||||
var impl = new X509CertificateImplBtls (false);
|
||||
impl.Import (data, password, flags);
|
||||
return impl;
|
||||
}
|
||||
|
||||
internal override X509Certificate2Impl GetNativeCertificate (
|
||||
X509Certificate certificate)
|
||||
{
|
||||
var impl = certificate.Impl as X509CertificateImplBtls;
|
||||
if (impl != null)
|
||||
return (X509Certificate2Impl)impl.Clone ();
|
||||
|
||||
var data = certificate.GetRawCertData ();
|
||||
return new X509CertificateImplBtls (data, MonoBtlsX509Format.DER, false);
|
||||
}
|
||||
|
||||
internal static MonoBtlsX509VerifyParam GetVerifyParam (string targetHost, bool serverMode)
|
||||
{
|
||||
MonoBtlsX509VerifyParam param;
|
||||
if (serverMode)
|
||||
param = MonoBtlsX509VerifyParam.GetSslClient ();
|
||||
else
|
||||
param = MonoBtlsX509VerifyParam.GetSslServer ();
|
||||
|
||||
if (targetHost == null)
|
||||
return param;
|
||||
|
||||
try {
|
||||
var copy = param.Copy ();
|
||||
copy.SetHost (targetHost);
|
||||
return copy;
|
||||
} finally {
|
||||
param.Dispose ();
|
||||
}
|
||||
}
|
||||
|
||||
internal override bool ValidateCertificate (
|
||||
ICertificateValidator2 validator, string targetHost, bool serverMode,
|
||||
X509CertificateCollection certificates, bool wantsChain, ref X509Chain chain,
|
||||
ref MonoSslPolicyErrors errors, ref int status11)
|
||||
{
|
||||
if (chain != null) {
|
||||
var chainImpl = (X509ChainImplBtls)chain.Impl;
|
||||
var success = chainImpl.StoreCtx.VerifyResult == 1;
|
||||
CheckValidationResult (
|
||||
validator, targetHost, serverMode, certificates,
|
||||
wantsChain, chain, chainImpl.StoreCtx,
|
||||
success, ref errors, ref status11);
|
||||
return success;
|
||||
}
|
||||
|
||||
using (var store = new MonoBtlsX509Store ())
|
||||
using (var nativeChain = MonoBtlsProvider.GetNativeChain (certificates))
|
||||
using (var param = GetVerifyParam (targetHost, serverMode))
|
||||
using (var storeCtx = new MonoBtlsX509StoreCtx ()) {
|
||||
SetupCertificateStore (store);
|
||||
|
||||
storeCtx.Initialize (store, nativeChain);
|
||||
|
||||
storeCtx.SetVerifyParam (param);
|
||||
|
||||
var ret = storeCtx.Verify ();
|
||||
|
||||
var success = ret == 1;
|
||||
|
||||
if (wantsChain && chain == null) {
|
||||
chain = GetManagedChain (nativeChain);
|
||||
}
|
||||
|
||||
CheckValidationResult (
|
||||
validator, targetHost, serverMode, certificates,
|
||||
wantsChain, null, storeCtx,
|
||||
success, ref errors, ref status11);
|
||||
return success;
|
||||
}
|
||||
}
|
||||
|
||||
internal static bool ValidateCertificate (MonoBtlsX509Chain chain, MonoBtlsX509VerifyParam param)
|
||||
{
|
||||
using (var store = new MonoBtlsX509Store ())
|
||||
using (var storeCtx = new MonoBtlsX509StoreCtx ()) {
|
||||
SetupCertificateStore (store);
|
||||
|
||||
storeCtx.Initialize (store, chain);
|
||||
|
||||
if (param != null)
|
||||
storeCtx.SetVerifyParam (param);
|
||||
|
||||
var ret = storeCtx.Verify ();
|
||||
|
||||
return ret == 1;
|
||||
}
|
||||
}
|
||||
|
||||
void CheckValidationResult (
|
||||
ICertificateValidator validator, string targetHost, bool serverMode,
|
||||
X509CertificateCollection certificates, bool wantsChain,
|
||||
X509Chain chain, MonoBtlsX509StoreCtx storeCtx,
|
||||
bool success, ref MonoSslPolicyErrors errors, ref int status11)
|
||||
{
|
||||
if (!success) {
|
||||
errors = MonoSslPolicyErrors.RemoteCertificateChainErrors;
|
||||
status11 = unchecked((int)0x800B010B);
|
||||
}
|
||||
}
|
||||
|
||||
internal static void SetupCertificateStore (MonoBtlsX509Store store)
|
||||
{
|
||||
#if MONODROID
|
||||
store.SetDefaultPaths ();
|
||||
store.AddAndroidLookup ();
|
||||
#else
|
||||
var userPath = MonoBtlsX509StoreManager.GetStorePath (MonoBtlsX509StoreType.UserTrustedRoots);
|
||||
if (Directory.Exists (userPath))
|
||||
store.AddDirectoryLookup (userPath, MonoBtlsX509FileType.PEM);
|
||||
var machinePath = MonoBtlsX509StoreManager.GetStorePath (MonoBtlsX509StoreType.MachineTrustedRoots);
|
||||
if (Directory.Exists (machinePath))
|
||||
store.AddDirectoryLookup (machinePath, MonoBtlsX509FileType.PEM);
|
||||
#endif
|
||||
}
|
||||
|
||||
public static string GetSystemStoreLocation ()
|
||||
{
|
||||
#if ANDROID
|
||||
return "/system/etc/security/cacerts";
|
||||
#else
|
||||
var appData = Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData);
|
||||
var path = Path.Combine (appData, ".mono", "certs", "NewTrust");
|
||||
return path;
|
||||
#endif
|
||||
}
|
||||
|
||||
public static X509Certificate CreateCertificate (byte[] data, MonoBtlsX509Format format, bool disallowFallback = false)
|
||||
{
|
||||
using (var impl = new X509CertificateImplBtls (data, format, disallowFallback)) {
|
||||
return new X509Certificate (impl);
|
||||
}
|
||||
}
|
||||
|
||||
public static X509Certificate2 CreateCertificate2 (byte[] data, MonoBtlsX509Format format, bool disallowFallback = false)
|
||||
{
|
||||
using (var impl = new X509CertificateImplBtls (data, format, disallowFallback)) {
|
||||
return new X509Certificate2 (impl);
|
||||
}
|
||||
}
|
||||
|
||||
public static X509Certificate2 CreateCertificate2 (byte[] data, string password, bool disallowFallback = false)
|
||||
{
|
||||
using (var impl = new X509CertificateImplBtls (disallowFallback)) {
|
||||
impl.Import (data, password, X509KeyStorageFlags.DefaultKeySet);
|
||||
return new X509Certificate2 (impl);
|
||||
}
|
||||
}
|
||||
|
||||
public static X509Certificate CreateCertificate (MonoBtlsX509 x509)
|
||||
{
|
||||
using (var impl = new X509CertificateImplBtls (x509, true))
|
||||
return new X509Certificate (impl);
|
||||
}
|
||||
|
||||
public static X509Chain CreateChain ()
|
||||
{
|
||||
using (var impl = new X509ChainImplBtls ())
|
||||
return new X509Chain (impl);
|
||||
}
|
||||
|
||||
public static X509Chain GetManagedChain (MonoBtlsX509Chain chain)
|
||||
{
|
||||
var impl = new X509ChainImplBtls (chain);
|
||||
return new X509Chain (impl);
|
||||
}
|
||||
|
||||
public static MonoBtlsX509 GetBtlsCertificate (X509Certificate certificate)
|
||||
{
|
||||
var impl = certificate.Impl as X509CertificateImplBtls;
|
||||
if (impl != null)
|
||||
return impl.X509.Copy ();
|
||||
|
||||
return MonoBtlsX509.LoadFromData (certificate.GetRawCertData (), MonoBtlsX509Format.DER);
|
||||
}
|
||||
|
||||
public static MonoBtlsX509Chain GetNativeChain (X509CertificateCollection certificates)
|
||||
{
|
||||
var chain = new MonoBtlsX509Chain ();
|
||||
try {
|
||||
foreach (var cert in certificates) {
|
||||
using (var x509 = GetBtlsCertificate (cert))
|
||||
chain.AddCertificate (x509);
|
||||
}
|
||||
return chain;
|
||||
} catch {
|
||||
chain.Dispose ();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
418
mcs/class/System/Mono.Btls/MonoBtlsSsl.cs
Normal file
418
mcs/class/System/Mono.Btls/MonoBtlsSsl.cs
Normal file
@@ -0,0 +1,418 @@
|
||||
//
|
||||
// MonoBtlsSsl.cs
|
||||
//
|
||||
// Author:
|
||||
// Martin Baulig <martin.baulig@xamarin.com>
|
||||
//
|
||||
// Copyright (c) 2015 Xamarin Inc. (http://www.xamarin.com)
|
||||
//
|
||||
// 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 SECURITY_DEP && MONO_FEATURE_BTLS
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
#if MONOTOUCH
|
||||
using MonoTouch;
|
||||
#endif
|
||||
|
||||
namespace Mono.Btls
|
||||
{
|
||||
delegate int MonoBtlsVerifyCallback (MonoBtlsX509StoreCtx ctx);
|
||||
delegate int MonoBtlsSelectCallback ();
|
||||
|
||||
class MonoBtlsSsl : MonoBtlsObject
|
||||
{
|
||||
internal class BoringSslHandle : MonoBtlsHandle
|
||||
{
|
||||
public BoringSslHandle (IntPtr handle)
|
||||
: base (handle, true)
|
||||
{
|
||||
}
|
||||
|
||||
protected override bool ReleaseHandle ()
|
||||
{
|
||||
mono_btls_ssl_destroy (handle);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static void mono_btls_ssl_destroy (IntPtr handle);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static IntPtr mono_btls_ssl_new (IntPtr handle);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_ssl_use_certificate (IntPtr handle, IntPtr x509);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_ssl_use_private_key (IntPtr handle, IntPtr key);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_ssl_add_chain_certificate (IntPtr handle, IntPtr x509);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_ssl_accept (IntPtr handle);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_ssl_connect (IntPtr handle);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_ssl_handshake (IntPtr handle);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static void mono_btls_ssl_close (IntPtr handle);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static void mono_btls_ssl_set_bio (IntPtr handle, IntPtr bio);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_ssl_read (IntPtr handle, IntPtr data, int len);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_ssl_write (IntPtr handle, IntPtr data, int len);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_ssl_get_error (IntPtr handle, int ret_code);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_ssl_get_version (IntPtr handle);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static void mono_btls_ssl_set_min_version (IntPtr handle, int version);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static void mono_btls_ssl_set_max_version (IntPtr handle, int version);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_ssl_get_cipher (IntPtr handle);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_ssl_get_ciphers (IntPtr handle, out IntPtr data);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static IntPtr mono_btls_ssl_get_peer_certificate (IntPtr handle);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_ssl_set_cipher_list (IntPtr handle, IntPtr str);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static void mono_btls_ssl_print_errors_cb (IntPtr func, IntPtr ctx);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_ssl_set_verify_param (IntPtr handle, IntPtr param);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_ssl_set_server_name (IntPtr handle, IntPtr name);
|
||||
|
||||
static BoringSslHandle Create_internal (MonoBtlsSslCtx ctx)
|
||||
{
|
||||
var handle = mono_btls_ssl_new (ctx.Handle.DangerousGetHandle ());
|
||||
if (handle == IntPtr.Zero)
|
||||
throw new MonoBtlsException ();
|
||||
return new BoringSslHandle (handle);
|
||||
}
|
||||
|
||||
PrintErrorsCallbackFunc printErrorsFunc;
|
||||
IntPtr printErrorsFuncPtr;
|
||||
|
||||
public MonoBtlsSsl (MonoBtlsSslCtx ctx)
|
||||
: base (Create_internal (ctx))
|
||||
{
|
||||
printErrorsFunc = PrintErrorsCallback;
|
||||
printErrorsFuncPtr = Marshal.GetFunctionPointerForDelegate (printErrorsFunc);
|
||||
}
|
||||
|
||||
new internal BoringSslHandle Handle {
|
||||
get { return (BoringSslHandle)base.Handle; }
|
||||
}
|
||||
|
||||
public void SetBio (MonoBtlsBio bio)
|
||||
{
|
||||
CheckThrow ();
|
||||
mono_btls_ssl_set_bio (
|
||||
Handle.DangerousGetHandle (),
|
||||
bio.Handle.DangerousGetHandle ());
|
||||
}
|
||||
|
||||
Exception ThrowError ([CallerMemberName] string callerName = null)
|
||||
{
|
||||
string errors;
|
||||
try {
|
||||
if (callerName == null)
|
||||
callerName = GetType ().Name;
|
||||
errors = GetErrors ();
|
||||
} catch {
|
||||
errors = null;
|
||||
}
|
||||
|
||||
if (errors != null) {
|
||||
Console.Error.WriteLine ("ERROR: {0} failed: {1}", callerName, errors);
|
||||
throw new MonoBtlsException ("{0} failed: {1}.", callerName, errors);
|
||||
} else {
|
||||
Console.Error.WriteLine ("ERROR: {0} failed.", callerName);
|
||||
throw new MonoBtlsException ("{0} failed.", callerName);
|
||||
}
|
||||
}
|
||||
|
||||
MonoBtlsSslError GetError (int ret_code)
|
||||
{
|
||||
CheckThrow ();
|
||||
var error = mono_btls_ssl_get_error (
|
||||
Handle.DangerousGetHandle (), ret_code);
|
||||
return (MonoBtlsSslError)error;
|
||||
}
|
||||
|
||||
public void SetCertificate (MonoBtlsX509 x509)
|
||||
{
|
||||
CheckThrow ();
|
||||
|
||||
var ret = mono_btls_ssl_use_certificate (
|
||||
Handle.DangerousGetHandle (),
|
||||
x509.Handle.DangerousGetHandle ());
|
||||
if (ret <= 0)
|
||||
throw ThrowError ();
|
||||
}
|
||||
|
||||
public void SetPrivateKey (MonoBtlsKey key)
|
||||
{
|
||||
CheckThrow ();
|
||||
|
||||
var ret = mono_btls_ssl_use_private_key (
|
||||
Handle.DangerousGetHandle (),
|
||||
key.Handle.DangerousGetHandle ());
|
||||
if (ret <= 0)
|
||||
throw ThrowError ();
|
||||
}
|
||||
|
||||
public void AddIntermediateCertificate (MonoBtlsX509 x509)
|
||||
{
|
||||
CheckThrow ();
|
||||
|
||||
var ret = mono_btls_ssl_add_chain_certificate (
|
||||
Handle.DangerousGetHandle (),
|
||||
x509.Handle.DangerousGetHandle ());
|
||||
if (ret <= 0)
|
||||
throw ThrowError ();
|
||||
}
|
||||
|
||||
public MonoBtlsSslError Accept ()
|
||||
{
|
||||
CheckThrow ();
|
||||
|
||||
var ret = mono_btls_ssl_accept (Handle.DangerousGetHandle ());
|
||||
|
||||
var error = GetError (ret);
|
||||
return error;
|
||||
}
|
||||
|
||||
public MonoBtlsSslError Connect ()
|
||||
{
|
||||
CheckThrow ();
|
||||
|
||||
var ret = mono_btls_ssl_connect (Handle.DangerousGetHandle ());
|
||||
|
||||
var error = GetError (ret);
|
||||
return error;
|
||||
}
|
||||
|
||||
public MonoBtlsSslError Handshake ()
|
||||
{
|
||||
CheckThrow ();
|
||||
|
||||
var ret = mono_btls_ssl_handshake (Handle.DangerousGetHandle ());
|
||||
|
||||
var error = GetError (ret);
|
||||
return error;
|
||||
}
|
||||
|
||||
delegate int PrintErrorsCallbackFunc (IntPtr str, IntPtr len, IntPtr ctx);
|
||||
|
||||
#if MONOTOUCH
|
||||
[MonoPInvokeCallback (typeof (PrintErrorsCallbackFunc))]
|
||||
#endif
|
||||
static int PrintErrorsCallback (IntPtr str, IntPtr len, IntPtr ctx)
|
||||
{
|
||||
var sb = (StringBuilder)GCHandle.FromIntPtr (ctx).Target;
|
||||
try {
|
||||
var text = Marshal.PtrToStringAnsi (str, (int)len);
|
||||
sb.Append (text);
|
||||
return 1;
|
||||
} catch {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public string GetErrors ()
|
||||
{
|
||||
var text = new StringBuilder ();
|
||||
var handle = GCHandle.Alloc (text);
|
||||
|
||||
try {
|
||||
mono_btls_ssl_print_errors_cb (printErrorsFuncPtr, GCHandle.ToIntPtr (handle));
|
||||
return text.ToString ();
|
||||
} finally {
|
||||
if (handle.IsAllocated)
|
||||
handle.Free ();
|
||||
}
|
||||
}
|
||||
|
||||
public void PrintErrors ()
|
||||
{
|
||||
var errors = GetErrors ();
|
||||
if (string.IsNullOrEmpty (errors))
|
||||
return;
|
||||
Console.Error.WriteLine (errors);
|
||||
}
|
||||
|
||||
public MonoBtlsSslError Read (IntPtr data, ref int dataSize)
|
||||
{
|
||||
CheckThrow ();
|
||||
var ret = mono_btls_ssl_read (
|
||||
Handle.DangerousGetHandle (), data, dataSize);
|
||||
|
||||
if (ret >= 0) {
|
||||
dataSize = ret;
|
||||
return MonoBtlsSslError.None;
|
||||
}
|
||||
|
||||
var error = mono_btls_ssl_get_error (
|
||||
Handle.DangerousGetHandle (), ret);
|
||||
dataSize = 0;
|
||||
return (MonoBtlsSslError)error;
|
||||
}
|
||||
|
||||
public MonoBtlsSslError Write (IntPtr data, ref int dataSize)
|
||||
{
|
||||
CheckThrow ();
|
||||
var ret = mono_btls_ssl_write (
|
||||
Handle.DangerousGetHandle (), data, dataSize);
|
||||
|
||||
if (ret >= 0) {
|
||||
dataSize = ret;
|
||||
return MonoBtlsSslError.None;
|
||||
}
|
||||
|
||||
var error = mono_btls_ssl_get_error (
|
||||
Handle.DangerousGetHandle (), ret);
|
||||
dataSize = 0;
|
||||
return (MonoBtlsSslError)error;
|
||||
}
|
||||
|
||||
public int GetVersion ()
|
||||
{
|
||||
CheckThrow ();
|
||||
return mono_btls_ssl_get_version (Handle.DangerousGetHandle ());
|
||||
}
|
||||
|
||||
public void SetMinVersion (int version)
|
||||
{
|
||||
CheckThrow ();
|
||||
mono_btls_ssl_set_min_version (Handle.DangerousGetHandle (), version);
|
||||
}
|
||||
|
||||
public void SetMaxVersion (int version)
|
||||
{
|
||||
CheckThrow ();
|
||||
mono_btls_ssl_set_max_version (Handle.DangerousGetHandle (), version);
|
||||
}
|
||||
|
||||
public int GetCipher ()
|
||||
{
|
||||
CheckThrow ();
|
||||
var cipher = mono_btls_ssl_get_cipher (Handle.DangerousGetHandle ());
|
||||
CheckError (cipher > 0);
|
||||
return cipher;
|
||||
}
|
||||
|
||||
public short[] GetCiphers ()
|
||||
{
|
||||
CheckThrow ();
|
||||
IntPtr data;
|
||||
var count = mono_btls_ssl_get_ciphers (
|
||||
Handle.DangerousGetHandle (), out data);
|
||||
CheckError (count > 0);
|
||||
try {
|
||||
short[] ciphers = new short[count];
|
||||
Marshal.Copy (data, ciphers, 0, count);
|
||||
return ciphers;
|
||||
} finally {
|
||||
FreeDataPtr (data);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetCipherList (string str)
|
||||
{
|
||||
CheckThrow ();
|
||||
IntPtr strPtr = IntPtr.Zero;
|
||||
try {
|
||||
strPtr = Marshal.StringToHGlobalAnsi (str);
|
||||
var ret = mono_btls_ssl_set_cipher_list (
|
||||
Handle.DangerousGetHandle (), strPtr);
|
||||
CheckError (ret);
|
||||
} finally {
|
||||
if (strPtr != IntPtr.Zero)
|
||||
Marshal.FreeHGlobal (strPtr);
|
||||
}
|
||||
}
|
||||
|
||||
public MonoBtlsX509 GetPeerCertificate ()
|
||||
{
|
||||
CheckThrow ();
|
||||
var x509 = mono_btls_ssl_get_peer_certificate (
|
||||
Handle.DangerousGetHandle ());
|
||||
if (x509 == IntPtr.Zero)
|
||||
return null;
|
||||
return new MonoBtlsX509 (new MonoBtlsX509.BoringX509Handle (x509));
|
||||
}
|
||||
|
||||
public void SetVerifyParam (MonoBtlsX509VerifyParam param)
|
||||
{
|
||||
CheckThrow ();
|
||||
var ret = mono_btls_ssl_set_verify_param (
|
||||
Handle.DangerousGetHandle (),
|
||||
param.Handle.DangerousGetHandle ());
|
||||
CheckError (ret);
|
||||
}
|
||||
|
||||
public void SetServerName (string name)
|
||||
{
|
||||
CheckThrow ();
|
||||
IntPtr namePtr = IntPtr.Zero;
|
||||
try {
|
||||
namePtr = Marshal.StringToHGlobalAnsi (name);
|
||||
var ret = mono_btls_ssl_set_server_name (
|
||||
Handle.DangerousGetHandle (), namePtr);
|
||||
CheckError (ret);
|
||||
} finally {
|
||||
if (namePtr != IntPtr.Zero)
|
||||
Marshal.FreeHGlobal (namePtr);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Close ()
|
||||
{
|
||||
mono_btls_ssl_close (Handle.DangerousGetHandle ());
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
260
mcs/class/System/Mono.Btls/MonoBtlsSslCtx.cs
Normal file
260
mcs/class/System/Mono.Btls/MonoBtlsSslCtx.cs
Normal file
@@ -0,0 +1,260 @@
|
||||
//
|
||||
// MonoBtlsSslCtx.cs
|
||||
//
|
||||
// Author:
|
||||
// Martin Baulig <martin.baulig@xamarin.com>
|
||||
//
|
||||
// Copyright (c) 2016 Xamarin Inc. (http://www.xamarin.com)
|
||||
//
|
||||
// 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 SECURITY_DEP && MONO_FEATURE_BTLS
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
#if MONOTOUCH
|
||||
using MonoTouch;
|
||||
#endif
|
||||
|
||||
namespace Mono.Btls
|
||||
{
|
||||
class MonoBtlsSslCtx : MonoBtlsObject
|
||||
{
|
||||
internal class BoringSslCtxHandle : MonoBtlsHandle
|
||||
{
|
||||
public BoringSslCtxHandle (IntPtr handle)
|
||||
: base (handle, true)
|
||||
{
|
||||
}
|
||||
|
||||
protected override bool ReleaseHandle ()
|
||||
{
|
||||
mono_btls_ssl_ctx_free (handle);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
new internal BoringSslCtxHandle Handle {
|
||||
get { return (BoringSslCtxHandle)base.Handle; }
|
||||
}
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static IntPtr mono_btls_ssl_ctx_new ();
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_ssl_ctx_free (IntPtr handle);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static IntPtr mono_btls_ssl_ctx_up_ref (IntPtr handle);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static void mono_btls_ssl_ctx_initialize (IntPtr handle, IntPtr instance);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static void mono_btls_ssl_ctx_set_debug_bio (IntPtr handle, IntPtr bio);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static void mono_btls_ssl_ctx_set_cert_verify_callback (IntPtr handle, IntPtr func, int cert_required);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static void mono_btls_ssl_ctx_set_cert_select_callback (IntPtr handle, IntPtr func);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static void mono_btls_ssl_ctx_set_min_version (IntPtr handle, int version);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static void mono_btls_ssl_ctx_set_max_version (IntPtr handle, int version);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_ssl_ctx_is_cipher_supported (IntPtr handle, short value);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_ssl_ctx_set_ciphers (IntPtr handle, int count, IntPtr data, int allow_unsupported);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_ssl_ctx_set_verify_param (IntPtr handle, IntPtr param);
|
||||
|
||||
delegate int NativeVerifyFunc (IntPtr instance, int preverify_ok, IntPtr ctx);
|
||||
delegate int NativeSelectFunc (IntPtr instance);
|
||||
|
||||
NativeVerifyFunc verifyFunc;
|
||||
NativeSelectFunc selectFunc;
|
||||
IntPtr verifyFuncPtr;
|
||||
IntPtr selectFuncPtr;
|
||||
MonoBtlsVerifyCallback verifyCallback;
|
||||
MonoBtlsSelectCallback selectCallback;
|
||||
MonoBtlsX509Store store;
|
||||
GCHandle instance;
|
||||
IntPtr instancePtr;
|
||||
|
||||
public MonoBtlsSslCtx ()
|
||||
: this (new BoringSslCtxHandle (mono_btls_ssl_ctx_new ()))
|
||||
{
|
||||
}
|
||||
|
||||
internal MonoBtlsSslCtx (BoringSslCtxHandle handle)
|
||||
: base (handle)
|
||||
{
|
||||
instance = GCHandle.Alloc (this);
|
||||
instancePtr = GCHandle.ToIntPtr (instance);
|
||||
mono_btls_ssl_ctx_initialize (
|
||||
handle.DangerousGetHandle (), instancePtr);
|
||||
|
||||
verifyFunc = NativeVerifyCallback;
|
||||
selectFunc = NativeSelectCallback;
|
||||
verifyFuncPtr = Marshal.GetFunctionPointerForDelegate (verifyFunc);
|
||||
selectFuncPtr = Marshal.GetFunctionPointerForDelegate (selectFunc);
|
||||
|
||||
store = new MonoBtlsX509Store (Handle);
|
||||
}
|
||||
|
||||
internal MonoBtlsSslCtx Copy ()
|
||||
{
|
||||
var copy = mono_btls_ssl_ctx_up_ref (Handle.DangerousGetHandle ());
|
||||
return new MonoBtlsSslCtx (new BoringSslCtxHandle (copy));
|
||||
}
|
||||
|
||||
public MonoBtlsX509Store CertificateStore {
|
||||
get { return store; }
|
||||
}
|
||||
|
||||
int VerifyCallback (bool preverify_ok, MonoBtlsX509StoreCtx ctx)
|
||||
{
|
||||
if (verifyCallback != null)
|
||||
return verifyCallback (ctx);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if MONOTOUCH
|
||||
[MonoPInvokeCallback (typeof (NativeVerifyFunc))]
|
||||
#endif
|
||||
static int NativeVerifyCallback (IntPtr instance, int preverify_ok, IntPtr store_ctx)
|
||||
{
|
||||
var c = (MonoBtlsSslCtx)GCHandle.FromIntPtr (instance).Target;
|
||||
using (var ctx = new MonoBtlsX509StoreCtx (preverify_ok, store_ctx)) {
|
||||
try {
|
||||
return c.VerifyCallback (preverify_ok != 0, ctx);
|
||||
} catch (Exception ex) {
|
||||
c.SetException (ex);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int SelectCallback ()
|
||||
{
|
||||
if (selectCallback != null)
|
||||
return selectCallback ();
|
||||
return 1;
|
||||
}
|
||||
|
||||
#if MONOTOUCH
|
||||
[MonoPInvokeCallback (typeof (NativeSelectFunc))]
|
||||
#endif
|
||||
static int NativeSelectCallback (IntPtr instance)
|
||||
{
|
||||
var c = (MonoBtlsSslCtx)GCHandle.FromIntPtr (instance).Target;
|
||||
try {
|
||||
return c.SelectCallback ();
|
||||
} catch (Exception ex) {
|
||||
c.SetException (ex);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public void SetDebugBio (MonoBtlsBio bio)
|
||||
{
|
||||
CheckThrow ();
|
||||
mono_btls_ssl_ctx_set_debug_bio (Handle.DangerousGetHandle (), bio.Handle.DangerousGetHandle ());
|
||||
}
|
||||
|
||||
public void SetVerifyCallback (MonoBtlsVerifyCallback callback, bool client_cert_required)
|
||||
{
|
||||
CheckThrow ();
|
||||
|
||||
verifyCallback = callback;
|
||||
mono_btls_ssl_ctx_set_cert_verify_callback (
|
||||
Handle.DangerousGetHandle (), verifyFuncPtr,
|
||||
client_cert_required ? 1 : 0);
|
||||
}
|
||||
|
||||
public void SetSelectCallback (MonoBtlsSelectCallback callback)
|
||||
{
|
||||
CheckThrow ();
|
||||
|
||||
selectCallback = callback;
|
||||
mono_btls_ssl_ctx_set_cert_select_callback (
|
||||
Handle.DangerousGetHandle (), selectFuncPtr);
|
||||
}
|
||||
|
||||
public void SetMinVersion (int version)
|
||||
{
|
||||
CheckThrow ();
|
||||
mono_btls_ssl_ctx_set_min_version (Handle.DangerousGetHandle (), version);
|
||||
}
|
||||
|
||||
public void SetMaxVersion (int version)
|
||||
{
|
||||
CheckThrow ();
|
||||
mono_btls_ssl_ctx_set_max_version (Handle.DangerousGetHandle (), version);
|
||||
}
|
||||
|
||||
public bool IsCipherSupported (short value)
|
||||
{
|
||||
CheckThrow ();
|
||||
return mono_btls_ssl_ctx_is_cipher_supported (Handle.DangerousGetHandle (), value) != 0;
|
||||
}
|
||||
|
||||
public void SetCiphers (short[] ciphers, bool allow_unsupported)
|
||||
{
|
||||
CheckThrow ();
|
||||
var data = Marshal.AllocHGlobal (ciphers.Length * 2);
|
||||
try {
|
||||
Marshal.Copy (ciphers, 0, data, ciphers.Length);
|
||||
var ret = mono_btls_ssl_ctx_set_ciphers (
|
||||
Handle.DangerousGetHandle (),
|
||||
ciphers.Length, data, allow_unsupported ? 1 : 0);
|
||||
CheckError (ret > 0);
|
||||
} finally {
|
||||
Marshal.FreeHGlobal (data);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetVerifyParam (MonoBtlsX509VerifyParam param)
|
||||
{
|
||||
CheckThrow ();
|
||||
var ret = mono_btls_ssl_ctx_set_verify_param (
|
||||
Handle.DangerousGetHandle (),
|
||||
param.Handle.DangerousGetHandle ());
|
||||
CheckError (ret);
|
||||
}
|
||||
|
||||
protected override void Close ()
|
||||
{
|
||||
if (store != null) {
|
||||
store.Dispose ();
|
||||
store = null;
|
||||
}
|
||||
if (instance.IsAllocated)
|
||||
instance.Free ();
|
||||
base.Close ();
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
@@ -1,5 +1,5 @@
|
||||
//
|
||||
// SSPIConfiguration.cs
|
||||
// MonoBtlsSslError.cs
|
||||
//
|
||||
// Author:
|
||||
// Martin Baulig <martin.baulig@xamarin.com>
|
||||
@@ -23,31 +23,25 @@
|
||||
// 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_NEW_TLS && SECURITY_DEP
|
||||
#if MONO_SECURITY_ALIAS
|
||||
extern alias MonoSecurity;
|
||||
using MonoSecurity::Mono.Security.Interface;
|
||||
#else
|
||||
using Mono.Security.Interface;
|
||||
#endif
|
||||
using Mono.Net.Security;
|
||||
|
||||
namespace System.Net.Security
|
||||
#if SECURITY_DEP && MONO_FEATURE_BTLS
|
||||
namespace Mono.Btls
|
||||
{
|
||||
internal interface SSPIConfiguration
|
||||
// keep in sync with boringssl/include/ssl.h
|
||||
enum MonoBtlsSslError
|
||||
{
|
||||
IMonoTlsProvider Provider {
|
||||
get;
|
||||
}
|
||||
|
||||
MonoTlsSettings Settings {
|
||||
get;
|
||||
}
|
||||
|
||||
IMonoTlsEventSink EventSink {
|
||||
get;
|
||||
}
|
||||
None = 0,
|
||||
Ssl = 1,
|
||||
WantRead = 2,
|
||||
WantWrite = 3,
|
||||
WantX509Lookup = 4,
|
||||
Syscall = 5,
|
||||
ZeroReturn = 6,
|
||||
WantConnect = 7,
|
||||
WantAccept = 8,
|
||||
WantChannelIdLookup = 9,
|
||||
PendingSession = 11,
|
||||
PendingCertificate = 12,
|
||||
WantPrivateKeyOperation = 13
|
||||
}
|
||||
}
|
||||
#endif
|
65
mcs/class/System/Mono.Btls/MonoBtlsStream.cs
Normal file
65
mcs/class/System/Mono.Btls/MonoBtlsStream.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
//
|
||||
// MonoBtlsStream.cs
|
||||
//
|
||||
// Author:
|
||||
// Martin Baulig <martin.baulig@xamarin.com>
|
||||
//
|
||||
// Copyright (c) 2016 Xamarin Inc. (http://www.xamarin.com)
|
||||
//
|
||||
// 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 SECURITY_DEP && MONO_FEATURE_BTLS
|
||||
#if MONO_SECURITY_ALIAS
|
||||
extern alias MonoSecurity;
|
||||
#endif
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Security.Authentication;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
|
||||
#if MONO_SECURITY_ALIAS
|
||||
using MonoSecurity::Mono.Security.Interface;
|
||||
#else
|
||||
using Mono.Security.Interface;
|
||||
#endif
|
||||
|
||||
using MNS = Mono.Net.Security;
|
||||
|
||||
namespace Mono.Btls
|
||||
{
|
||||
class MonoBtlsStream : MNS.MobileAuthenticatedStream
|
||||
{
|
||||
public MonoBtlsStream (Stream innerStream, bool leaveInnerStreamOpen, MonoTlsSettings settings, MonoTlsProvider provider)
|
||||
: base (innerStream, leaveInnerStreamOpen, settings, provider)
|
||||
{
|
||||
}
|
||||
|
||||
protected override MNS.MobileTlsContext CreateContext (
|
||||
MNS.MobileAuthenticatedStream parent, bool serverMode, string targetHost,
|
||||
SslProtocols enabledProtocols, X509Certificate serverCertificate,
|
||||
X509CertificateCollection clientCertificates, bool askForClientCert)
|
||||
{
|
||||
return new MonoBtlsContext (
|
||||
parent, serverMode, targetHost,
|
||||
enabledProtocols, serverCertificate,
|
||||
clientCertificates, askForClientCert);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
187
mcs/class/System/Mono.Btls/MonoBtlsUtils.cs
Normal file
187
mcs/class/System/Mono.Btls/MonoBtlsUtils.cs
Normal file
@@ -0,0 +1,187 @@
|
||||
//
|
||||
// MonoBtlsUtils.cs
|
||||
//
|
||||
// Author:
|
||||
// Martin Baulig <martin.baulig@xamarin.com>
|
||||
//
|
||||
// Copyright (c) 2016 Xamarin Inc. (http://www.xamarin.com)
|
||||
//
|
||||
// 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 SECURITY_DEP && MONO_FEATURE_BTLS
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
|
||||
namespace Mono.Btls
|
||||
{
|
||||
static class MonoBtlsUtils
|
||||
{
|
||||
static byte[] emailOid = { 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01 };
|
||||
|
||||
public static bool Compare (byte[] a, byte[] b)
|
||||
{
|
||||
if (a.Length != b.Length)
|
||||
return false;
|
||||
for (int i = 0; i < a.Length; i++) {
|
||||
if (a[i] != b[i])
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool AppendEntry (StringBuilder sb, MonoBtlsX509Name name, int index, string separator, bool quotes)
|
||||
{
|
||||
var type = name.GetEntryType (index);
|
||||
if (type < 0)
|
||||
return false;
|
||||
else if (type == 0) {
|
||||
var oidValue = name.GetEntryOidData (index);
|
||||
if (Compare (oidValue, emailOid))
|
||||
type = MonoBtlsX509NameEntryType.Email;
|
||||
}
|
||||
int tag;
|
||||
var text = name.GetEntryValue (index, out tag);
|
||||
if (text == null)
|
||||
return false;
|
||||
var oid = name.GetEntryOid (index);
|
||||
if (oid == null)
|
||||
return false;
|
||||
|
||||
if (sb.Length > 0)
|
||||
sb.Append (separator);
|
||||
|
||||
switch (type) {
|
||||
case MonoBtlsX509NameEntryType.CountryName:
|
||||
sb.Append ("C=");
|
||||
break;
|
||||
case MonoBtlsX509NameEntryType.OrganizationName:
|
||||
sb.Append ("O=");
|
||||
break;
|
||||
case MonoBtlsX509NameEntryType.OrganizationalUnitName:
|
||||
sb.Append ("OU=");
|
||||
break;
|
||||
case MonoBtlsX509NameEntryType.CommonName:
|
||||
sb.Append ("CN=");
|
||||
break;
|
||||
case MonoBtlsX509NameEntryType.LocalityName:
|
||||
sb.Append ("L=");
|
||||
break;
|
||||
case MonoBtlsX509NameEntryType.StateOrProvinceName:
|
||||
sb.Append ("S="); // NOTE: RFC2253 uses ST=
|
||||
break;
|
||||
case MonoBtlsX509NameEntryType.StreetAddress:
|
||||
sb.Append ("STREET=");
|
||||
break;
|
||||
case MonoBtlsX509NameEntryType.DomainComponent:
|
||||
sb.Append ("DC=");
|
||||
break;
|
||||
case MonoBtlsX509NameEntryType.UserId:
|
||||
sb.Append ("UID=");
|
||||
break;
|
||||
case MonoBtlsX509NameEntryType.Email:
|
||||
sb.Append ("E="); // NOTE: Not part of RFC2253
|
||||
break;
|
||||
case MonoBtlsX509NameEntryType.DnQualifier:
|
||||
sb.Append ("dnQualifier=");
|
||||
break;
|
||||
case MonoBtlsX509NameEntryType.Title:
|
||||
sb.Append ("T=");
|
||||
break;
|
||||
case MonoBtlsX509NameEntryType.Surname:
|
||||
sb.Append ("SN=");
|
||||
break;
|
||||
case MonoBtlsX509NameEntryType.GivenName:
|
||||
sb.Append ("G=");
|
||||
break;
|
||||
case MonoBtlsX509NameEntryType.Initial:
|
||||
sb.Append ("I=");
|
||||
break;
|
||||
default:
|
||||
// unknown OID
|
||||
sb.Append ("OID."); // NOTE: Not present as RFC2253
|
||||
sb.Append (oid);
|
||||
sb.Append ("=");
|
||||
break;
|
||||
}
|
||||
|
||||
// 16bits or 8bits string ? TODO not complete (+special chars!)
|
||||
char[] specials = { ',', '+', '"', '\\', '<', '>', ';' };
|
||||
if (quotes && tag != 0x1E) {
|
||||
if ((text.IndexOfAny (specials, 0, text.Length) > 0) ||
|
||||
text.StartsWith (" ") || (text.EndsWith (" ")))
|
||||
text = "\"" + text + "\"";
|
||||
}
|
||||
|
||||
sb.Append (text);
|
||||
return true;
|
||||
}
|
||||
|
||||
const X500DistinguishedNameFlags AllFlags = X500DistinguishedNameFlags.Reversed |
|
||||
X500DistinguishedNameFlags.UseSemicolons | X500DistinguishedNameFlags.DoNotUsePlusSign |
|
||||
X500DistinguishedNameFlags.DoNotUseQuotes | X500DistinguishedNameFlags.UseCommas |
|
||||
X500DistinguishedNameFlags.UseNewLines | X500DistinguishedNameFlags.UseUTF8Encoding |
|
||||
X500DistinguishedNameFlags.UseT61Encoding | X500DistinguishedNameFlags.ForceUTF8Encoding;
|
||||
|
||||
static string GetSeparator (X500DistinguishedNameFlags flag)
|
||||
{
|
||||
if ((flag & X500DistinguishedNameFlags.UseSemicolons) != 0)
|
||||
return "; ";
|
||||
if ((flag & X500DistinguishedNameFlags.UseCommas) != 0)
|
||||
return ", ";
|
||||
if ((flag & X500DistinguishedNameFlags.UseNewLines) != 0)
|
||||
return Environment.NewLine;
|
||||
return ", "; //default
|
||||
}
|
||||
|
||||
public static string FormatName (MonoBtlsX509Name name, X500DistinguishedNameFlags flag)
|
||||
{
|
||||
if ((flag != 0) && ((flag & AllFlags) == 0))
|
||||
throw new ArgumentException ("flag");
|
||||
|
||||
if (name.GetEntryCount () == 0)
|
||||
return String.Empty;
|
||||
|
||||
// Mono.Security reversed isn't the same as fx 2.0 (which is the reverse of 1.x)
|
||||
bool reversed = ((flag & X500DistinguishedNameFlags.Reversed) != 0);
|
||||
bool quotes = ((flag & X500DistinguishedNameFlags.DoNotUseQuotes) == 0);
|
||||
string separator = GetSeparator (flag);
|
||||
|
||||
return FormatName (name, reversed, separator, quotes);
|
||||
}
|
||||
|
||||
public static string FormatName (MonoBtlsX509Name name, bool reversed, string separator, bool quotes)
|
||||
{
|
||||
var count = name.GetEntryCount ();
|
||||
StringBuilder sb = new StringBuilder ();
|
||||
|
||||
if (reversed) {
|
||||
for (int i = count - 1; i >= 0; i--) {
|
||||
AppendEntry (sb, name, i, separator, quotes);
|
||||
}
|
||||
} else {
|
||||
for (int i = 0; i < count; i++) {
|
||||
AppendEntry (sb, name, i, separator, quotes);
|
||||
}
|
||||
}
|
||||
|
||||
return sb.ToString ();
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
465
mcs/class/System/Mono.Btls/MonoBtlsX509.cs
Normal file
465
mcs/class/System/Mono.Btls/MonoBtlsX509.cs
Normal file
@@ -0,0 +1,465 @@
|
||||
//
|
||||
// MonoBtlsX509.cs
|
||||
//
|
||||
// Author:
|
||||
// Martin Baulig <martin.baulig@xamarin.com>
|
||||
//
|
||||
// Copyright (c) 2016 Xamarin Inc. (http://www.xamarin.com)
|
||||
//
|
||||
// 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 SECURITY_DEP && MONO_FEATURE_BTLS
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace Mono.Btls
|
||||
{
|
||||
class MonoBtlsX509 : MonoBtlsObject
|
||||
{
|
||||
internal class BoringX509Handle : MonoBtlsHandle
|
||||
{
|
||||
public BoringX509Handle (IntPtr handle)
|
||||
: base (handle, true)
|
||||
{
|
||||
}
|
||||
|
||||
protected override bool ReleaseHandle ()
|
||||
{
|
||||
if (handle != IntPtr.Zero)
|
||||
mono_btls_x509_free (handle);
|
||||
return true;
|
||||
}
|
||||
|
||||
public IntPtr StealHandle ()
|
||||
{
|
||||
var retval = Interlocked.Exchange (ref handle, IntPtr.Zero);
|
||||
return retval;
|
||||
}
|
||||
}
|
||||
|
||||
new internal BoringX509Handle Handle {
|
||||
get { return (BoringX509Handle)base.Handle; }
|
||||
}
|
||||
|
||||
internal MonoBtlsX509 (BoringX509Handle handle)
|
||||
: base (handle)
|
||||
{
|
||||
}
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static IntPtr mono_btls_x509_up_ref (IntPtr handle);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static IntPtr mono_btls_x509_from_data (IntPtr data, int len, MonoBtlsX509Format format);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static IntPtr mono_btls_x509_get_subject_name (IntPtr handle);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static IntPtr mono_btls_x509_get_issuer_name (IntPtr handle);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_x509_get_subject_name_string (IntPtr handle, IntPtr buffer, int size);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_x509_get_issuer_name_string (IntPtr handle, IntPtr buffer, int size);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_x509_get_raw_data (IntPtr handle, IntPtr bio, MonoBtlsX509Format format);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_x509_cmp (IntPtr a, IntPtr b);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_x509_get_hash (IntPtr handle, out IntPtr data);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static long mono_btls_x509_get_not_before (IntPtr handle);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static long mono_btls_x509_get_not_after (IntPtr handle);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_x509_get_public_key (IntPtr handle, IntPtr bio);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_x509_get_serial_number (IntPtr handle, IntPtr data, int size, int mono_style);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_x509_get_version (IntPtr handle);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_x509_get_signature_algorithm (IntPtr handle, IntPtr buffer, int size);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_x509_get_public_key_asn1 (IntPtr handle, IntPtr oid, int oid_size, out IntPtr data, out int size);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_x509_get_public_key_parameters (IntPtr handle, IntPtr oid, int oid_size, out IntPtr data, out int size);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static IntPtr mono_btls_x509_get_pubkey (IntPtr handle);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_x509_get_subject_key_identifier (IntPtr handle, out IntPtr data, out int size);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_x509_print (IntPtr handle, IntPtr bio);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static void mono_btls_x509_free (IntPtr handle);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static IntPtr mono_btls_x509_dup (IntPtr handle);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_x509_add_trust_object (IntPtr handle, MonoBtlsX509Purpose purpose);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_x509_add_reject_object (IntPtr handle, MonoBtlsX509Purpose purpose);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_x509_add_explicit_trust (IntPtr handle, MonoBtlsX509TrustKind kind);
|
||||
|
||||
internal MonoBtlsX509 Copy ()
|
||||
{
|
||||
var copy = mono_btls_x509_up_ref (Handle.DangerousGetHandle ());
|
||||
CheckError (copy != IntPtr.Zero);
|
||||
return new MonoBtlsX509 (new BoringX509Handle (copy));
|
||||
}
|
||||
|
||||
// This will actually duplicate the underlying 'X509 *' object instead of
|
||||
// simply increasing the reference count.
|
||||
internal MonoBtlsX509 Duplicate ()
|
||||
{
|
||||
var copy = mono_btls_x509_dup (Handle.DangerousGetHandle ());
|
||||
CheckError (copy != IntPtr.Zero);
|
||||
return new MonoBtlsX509 (new BoringX509Handle (copy));
|
||||
}
|
||||
|
||||
public static MonoBtlsX509 LoadFromData (byte[] buffer, MonoBtlsX509Format format)
|
||||
{
|
||||
var data = Marshal.AllocHGlobal (buffer.Length);
|
||||
if (data == IntPtr.Zero)
|
||||
throw new OutOfMemoryException ();
|
||||
|
||||
try {
|
||||
Marshal.Copy (buffer, 0, data, buffer.Length);
|
||||
var x509 = mono_btls_x509_from_data (data, buffer.Length, format);
|
||||
if (x509 == IntPtr.Zero)
|
||||
throw new MonoBtlsException ("Failed to read certificate from data.");
|
||||
|
||||
return new MonoBtlsX509 (new BoringX509Handle (x509));
|
||||
} finally {
|
||||
Marshal.FreeHGlobal (data);
|
||||
}
|
||||
}
|
||||
|
||||
public MonoBtlsX509Name GetSubjectName ()
|
||||
{
|
||||
var handle = mono_btls_x509_get_subject_name (Handle.DangerousGetHandle ());
|
||||
CheckError (handle != IntPtr.Zero);
|
||||
return new MonoBtlsX509Name (new MonoBtlsX509Name.BoringX509NameHandle (handle, false));
|
||||
}
|
||||
|
||||
public string GetSubjectNameString ()
|
||||
{
|
||||
const int size = 4096;
|
||||
var data = Marshal.AllocHGlobal (size);
|
||||
try {
|
||||
var ret = mono_btls_x509_get_subject_name_string (
|
||||
Handle.DangerousGetHandle (), data, size);
|
||||
CheckError (ret);
|
||||
return Marshal.PtrToStringAnsi (data);
|
||||
} finally {
|
||||
Marshal.FreeHGlobal (data);
|
||||
}
|
||||
}
|
||||
|
||||
public long GetSubjectNameHash ()
|
||||
{
|
||||
CheckThrow ();
|
||||
using (var subject = GetSubjectName ())
|
||||
return subject.GetHash ();
|
||||
}
|
||||
|
||||
public MonoBtlsX509Name GetIssuerName ()
|
||||
{
|
||||
var handle = mono_btls_x509_get_issuer_name (Handle.DangerousGetHandle ());
|
||||
CheckError (handle != IntPtr.Zero);
|
||||
return new MonoBtlsX509Name (new MonoBtlsX509Name.BoringX509NameHandle (handle, false));
|
||||
}
|
||||
|
||||
public string GetIssuerNameString ()
|
||||
{
|
||||
const int size = 4096;
|
||||
var data = Marshal.AllocHGlobal (size);
|
||||
try {
|
||||
var ret = mono_btls_x509_get_issuer_name_string (
|
||||
Handle.DangerousGetHandle (), data, size);
|
||||
CheckError (ret);
|
||||
return Marshal.PtrToStringAnsi (data);
|
||||
} finally {
|
||||
Marshal.FreeHGlobal (data);
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] GetRawData (MonoBtlsX509Format format)
|
||||
{
|
||||
using (var bio = new MonoBtlsBioMemory ()) {
|
||||
var ret = mono_btls_x509_get_raw_data (
|
||||
Handle.DangerousGetHandle (),
|
||||
bio.Handle.DangerousGetHandle (),
|
||||
format);
|
||||
CheckError (ret);
|
||||
return bio.GetData ();
|
||||
}
|
||||
}
|
||||
|
||||
public void GetRawData (MonoBtlsBio bio, MonoBtlsX509Format format)
|
||||
{
|
||||
CheckThrow ();
|
||||
var ret = mono_btls_x509_get_raw_data (
|
||||
Handle.DangerousGetHandle (),
|
||||
bio.Handle.DangerousGetHandle (),
|
||||
format);
|
||||
CheckError (ret);
|
||||
}
|
||||
|
||||
public static int Compare (MonoBtlsX509 a, MonoBtlsX509 b)
|
||||
{
|
||||
return mono_btls_x509_cmp (
|
||||
a.Handle.DangerousGetHandle (),
|
||||
b.Handle.DangerousGetHandle ());
|
||||
}
|
||||
|
||||
public byte[] GetCertHash ()
|
||||
{
|
||||
IntPtr data;
|
||||
var ret = mono_btls_x509_get_hash (Handle.DangerousGetHandle (), out data);
|
||||
CheckError (ret > 0);
|
||||
var buffer = new byte [ret];
|
||||
Marshal.Copy (data, buffer, 0, ret);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public DateTime GetNotBefore ()
|
||||
{
|
||||
var ticks = mono_btls_x509_get_not_before (Handle.DangerousGetHandle ());
|
||||
return new DateTime (1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds (ticks);
|
||||
}
|
||||
|
||||
public DateTime GetNotAfter ()
|
||||
{
|
||||
var ticks = mono_btls_x509_get_not_after (Handle.DangerousGetHandle ());
|
||||
return new DateTime (1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds (ticks);
|
||||
}
|
||||
|
||||
public byte[] GetPublicKeyData ()
|
||||
{
|
||||
using (var bio = new MonoBtlsBioMemory ()) {
|
||||
var ret = mono_btls_x509_get_public_key (
|
||||
Handle.DangerousGetHandle (),
|
||||
bio.Handle.DangerousGetHandle ());
|
||||
CheckError (ret > 0);
|
||||
return bio.GetData ();
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] GetSerialNumber (bool mono_style)
|
||||
{
|
||||
int size = 256;
|
||||
IntPtr data = Marshal.AllocHGlobal (size);
|
||||
try {
|
||||
var ret = mono_btls_x509_get_serial_number (
|
||||
Handle.DangerousGetHandle (), data,
|
||||
size, mono_style ? 1 : 0);
|
||||
CheckError (ret > 0);
|
||||
var buffer = new byte [ret];
|
||||
Marshal.Copy (data, buffer, 0, ret);
|
||||
return buffer;
|
||||
} finally {
|
||||
if (data != IntPtr.Zero)
|
||||
Marshal.FreeHGlobal (data);
|
||||
}
|
||||
}
|
||||
|
||||
public int GetVersion ()
|
||||
{
|
||||
return mono_btls_x509_get_version (Handle.DangerousGetHandle ());
|
||||
}
|
||||
|
||||
public Oid GetSignatureAlgorithm ()
|
||||
{
|
||||
int size = 256;
|
||||
IntPtr data = Marshal.AllocHGlobal (size);
|
||||
try {
|
||||
var ret = mono_btls_x509_get_signature_algorithm (
|
||||
Handle.DangerousGetHandle (), data, size);
|
||||
CheckError (ret > 0);
|
||||
return new Oid (Marshal.PtrToStringAnsi (data));
|
||||
} finally {
|
||||
Marshal.FreeHGlobal (data);
|
||||
}
|
||||
}
|
||||
|
||||
public AsnEncodedData GetPublicKeyAsn1 ()
|
||||
{
|
||||
int size;
|
||||
IntPtr data;
|
||||
|
||||
int oidSize = 256;
|
||||
var oidData = Marshal.AllocHGlobal (256);
|
||||
string oid;
|
||||
|
||||
try {
|
||||
var ret = mono_btls_x509_get_public_key_asn1 (
|
||||
Handle.DangerousGetHandle (), oidData, oidSize,
|
||||
out data, out size);
|
||||
CheckError (ret);
|
||||
oid = Marshal.PtrToStringAnsi (oidData);
|
||||
} finally {
|
||||
Marshal.FreeHGlobal (oidData);
|
||||
}
|
||||
|
||||
try {
|
||||
var buffer = new byte[size];
|
||||
Marshal.Copy (data, buffer, 0, size);
|
||||
return new AsnEncodedData (oid.ToString (), buffer);
|
||||
} finally {
|
||||
if (data != IntPtr.Zero)
|
||||
FreeDataPtr (data);
|
||||
}
|
||||
}
|
||||
|
||||
public AsnEncodedData GetPublicKeyParameters ()
|
||||
{
|
||||
int size;
|
||||
IntPtr data;
|
||||
|
||||
int oidSize = 256;
|
||||
var oidData = Marshal.AllocHGlobal (256);
|
||||
string oid;
|
||||
|
||||
try {
|
||||
var ret = mono_btls_x509_get_public_key_parameters (
|
||||
Handle.DangerousGetHandle (), oidData, oidSize,
|
||||
out data, out size);
|
||||
CheckError (ret);
|
||||
oid = Marshal.PtrToStringAnsi (oidData);
|
||||
} finally {
|
||||
Marshal.FreeHGlobal (oidData);
|
||||
}
|
||||
|
||||
try {
|
||||
var buffer = new byte[size];
|
||||
Marshal.Copy (data, buffer, 0, size);
|
||||
return new AsnEncodedData (oid.ToString (), buffer);
|
||||
} finally {
|
||||
if (data != IntPtr.Zero)
|
||||
FreeDataPtr (data);
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] GetSubjectKeyIdentifier ()
|
||||
{
|
||||
int size;
|
||||
IntPtr data = IntPtr.Zero;
|
||||
|
||||
try {
|
||||
var ret = mono_btls_x509_get_subject_key_identifier (
|
||||
Handle.DangerousGetHandle (), out data, out size);
|
||||
CheckError (ret);
|
||||
var buffer = new byte[size];
|
||||
Marshal.Copy (data, buffer, 0, size);
|
||||
return buffer;
|
||||
} finally {
|
||||
if (data != IntPtr.Zero)
|
||||
FreeDataPtr (data);
|
||||
}
|
||||
}
|
||||
|
||||
public MonoBtlsKey GetPublicKey ()
|
||||
{
|
||||
var handle = mono_btls_x509_get_pubkey (Handle.DangerousGetHandle ());
|
||||
CheckError (handle != IntPtr.Zero);
|
||||
return new MonoBtlsKey (new MonoBtlsKey.BoringKeyHandle (handle));
|
||||
}
|
||||
|
||||
public void Print (MonoBtlsBio bio)
|
||||
{
|
||||
var ret = mono_btls_x509_print (
|
||||
Handle.DangerousGetHandle (),
|
||||
bio.Handle.DangerousGetHandle ());
|
||||
CheckError (ret);
|
||||
}
|
||||
|
||||
public void ExportAsPEM (MonoBtlsBio bio, bool includeHumanReadableForm)
|
||||
{
|
||||
GetRawData (bio, MonoBtlsX509Format.PEM);
|
||||
|
||||
if (!includeHumanReadableForm)
|
||||
return;
|
||||
|
||||
Print (bio);
|
||||
|
||||
var hash = GetCertHash ();
|
||||
var output = new StringBuilder ();
|
||||
output.Append ("SHA1 Fingerprint=");
|
||||
for (int i = 0; i < hash.Length; i++) {
|
||||
if (i > 0)
|
||||
output.Append (":");
|
||||
output.AppendFormat ("{0:X2}", hash [i]);
|
||||
}
|
||||
output.AppendLine ();
|
||||
var outputData = Encoding.ASCII.GetBytes (output.ToString ());
|
||||
bio.Write (outputData, 0, outputData.Length);
|
||||
}
|
||||
|
||||
public void AddTrustObject (MonoBtlsX509Purpose purpose)
|
||||
{
|
||||
CheckThrow ();
|
||||
var ret = mono_btls_x509_add_trust_object (
|
||||
Handle.DangerousGetHandle (), purpose);
|
||||
CheckError (ret);
|
||||
}
|
||||
|
||||
public void AddRejectObject (MonoBtlsX509Purpose purpose)
|
||||
{
|
||||
CheckThrow ();
|
||||
var ret = mono_btls_x509_add_reject_object (
|
||||
Handle.DangerousGetHandle (), purpose);
|
||||
CheckError (ret);
|
||||
}
|
||||
|
||||
public void AddExplicitTrust (MonoBtlsX509TrustKind kind)
|
||||
{
|
||||
CheckThrow ();
|
||||
var ret = mono_btls_x509_add_explicit_trust (
|
||||
Handle.DangerousGetHandle (), kind);
|
||||
CheckError (ret);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
122
mcs/class/System/Mono.Btls/MonoBtlsX509Chain.cs
Normal file
122
mcs/class/System/Mono.Btls/MonoBtlsX509Chain.cs
Normal file
@@ -0,0 +1,122 @@
|
||||
//
|
||||
// MonoBtlsX509Chain.cs
|
||||
//
|
||||
// Author:
|
||||
// Martin Baulig <martin.baulig@xamarin.com>
|
||||
//
|
||||
// Copyright (c) 2016 Xamarin Inc. (http://www.xamarin.com)
|
||||
//
|
||||
// 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 SECURITY_DEP && MONO_FEATURE_BTLS
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Mono.Btls
|
||||
{
|
||||
class MonoBtlsX509Chain : MonoBtlsObject
|
||||
{
|
||||
internal class BoringX509ChainHandle : MonoBtlsHandle
|
||||
{
|
||||
public BoringX509ChainHandle (IntPtr handle)
|
||||
: base (handle, true)
|
||||
{
|
||||
}
|
||||
|
||||
protected override bool ReleaseHandle ()
|
||||
{
|
||||
mono_btls_x509_chain_free (handle);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
new internal BoringX509ChainHandle Handle {
|
||||
get { return (BoringX509ChainHandle)base.Handle; }
|
||||
}
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static IntPtr mono_btls_x509_chain_new ();
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_x509_chain_get_count (IntPtr handle);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static IntPtr mono_btls_x509_chain_get_cert (IntPtr Handle, int index);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_x509_chain_add_cert (IntPtr chain, IntPtr x509);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static IntPtr mono_btls_x509_chain_up_ref (IntPtr handle);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static void mono_btls_x509_chain_free (IntPtr handle);
|
||||
|
||||
public MonoBtlsX509Chain ()
|
||||
: base (new BoringX509ChainHandle (mono_btls_x509_chain_new ()))
|
||||
{
|
||||
}
|
||||
|
||||
internal MonoBtlsX509Chain (BoringX509ChainHandle handle)
|
||||
: base (handle)
|
||||
{
|
||||
}
|
||||
|
||||
public int Count {
|
||||
get { return mono_btls_x509_chain_get_count (Handle.DangerousGetHandle ()); }
|
||||
}
|
||||
|
||||
public MonoBtlsX509 GetCertificate (int index)
|
||||
{
|
||||
if (index >= Count)
|
||||
throw new IndexOutOfRangeException ();
|
||||
var handle = mono_btls_x509_chain_get_cert (
|
||||
Handle.DangerousGetHandle (), index);
|
||||
CheckError (handle != IntPtr.Zero);
|
||||
return new MonoBtlsX509 (new MonoBtlsX509.BoringX509Handle (handle));
|
||||
}
|
||||
|
||||
public void Dump ()
|
||||
{
|
||||
Console.Error.WriteLine ("CHAIN: {0:x} {1}", Handle, Count);
|
||||
for (int i = 0; i < Count; i++) {
|
||||
using (var cert = GetCertificate (i)) {
|
||||
Console.Error.WriteLine (" CERT #{0}: {1}", i, cert.GetSubjectNameString ());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void AddCertificate (MonoBtlsX509 x509)
|
||||
{
|
||||
mono_btls_x509_chain_add_cert (
|
||||
Handle.DangerousGetHandle (),
|
||||
x509.Handle.DangerousGetHandle ());
|
||||
}
|
||||
|
||||
internal MonoBtlsX509Chain Copy ()
|
||||
{
|
||||
var copy = mono_btls_x509_chain_up_ref (Handle.DangerousGetHandle ());
|
||||
CheckError (copy != IntPtr.Zero);
|
||||
return new MonoBtlsX509Chain (new BoringX509ChainHandle (copy));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
185
mcs/class/System/Mono.Btls/MonoBtlsX509Crl.cs
Normal file
185
mcs/class/System/Mono.Btls/MonoBtlsX509Crl.cs
Normal file
@@ -0,0 +1,185 @@
|
||||
//
|
||||
// MonoBtlsX509Crl.cs
|
||||
//
|
||||
// Author:
|
||||
// Martin Baulig <martin.baulig@xamarin.com>
|
||||
//
|
||||
// Copyright (c) 2016 Xamarin Inc. (http://www.xamarin.com)
|
||||
//
|
||||
// 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 SECURITY_DEP && MONO_FEATURE_BTLS
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace Mono.Btls
|
||||
{
|
||||
class MonoBtlsX509Crl : MonoBtlsObject
|
||||
{
|
||||
internal class BoringX509CrlHandle : MonoBtlsHandle
|
||||
{
|
||||
public BoringX509CrlHandle (IntPtr handle)
|
||||
: base (handle, true)
|
||||
{
|
||||
}
|
||||
|
||||
protected override bool ReleaseHandle ()
|
||||
{
|
||||
if (handle != IntPtr.Zero)
|
||||
mono_btls_x509_crl_free (handle);
|
||||
return true;
|
||||
}
|
||||
|
||||
public IntPtr StealHandle ()
|
||||
{
|
||||
var retval = Interlocked.Exchange (ref handle, IntPtr.Zero);
|
||||
return retval;
|
||||
}
|
||||
}
|
||||
|
||||
new internal BoringX509CrlHandle Handle {
|
||||
get { return (BoringX509CrlHandle)base.Handle; }
|
||||
}
|
||||
|
||||
internal MonoBtlsX509Crl (BoringX509CrlHandle handle)
|
||||
: base (handle)
|
||||
{
|
||||
}
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static IntPtr mono_btls_x509_crl_ref (IntPtr handle);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static IntPtr mono_btls_x509_crl_from_data (IntPtr data, int len, MonoBtlsX509Format format);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static IntPtr mono_btls_x509_crl_get_by_cert (IntPtr handle, IntPtr x509);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
unsafe extern static IntPtr mono_btls_x509_crl_get_by_serial (IntPtr handle, void *serial, int len);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static int mono_btls_x509_crl_get_revoked_count (IntPtr handle);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static IntPtr mono_btls_x509_crl_get_revoked (IntPtr handle, int index);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static long mono_btls_x509_crl_get_last_update (IntPtr handle);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static long mono_btls_x509_crl_get_next_update (IntPtr handle);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static long mono_btls_x509_crl_get_version (IntPtr handle);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static IntPtr mono_btls_x509_crl_get_issuer (IntPtr handle);
|
||||
|
||||
[DllImport (BTLS_DYLIB)]
|
||||
extern static void mono_btls_x509_crl_free (IntPtr handle);
|
||||
|
||||
public static MonoBtlsX509Crl LoadFromData (byte[] buffer, MonoBtlsX509Format format)
|
||||
{
|
||||
var data = Marshal.AllocHGlobal (buffer.Length);
|
||||
if (data == IntPtr.Zero)
|
||||
throw new OutOfMemoryException ();
|
||||
|
||||
try {
|
||||
Marshal.Copy (buffer, 0, data, buffer.Length);
|
||||
var crl = mono_btls_x509_crl_from_data (data, buffer.Length, format);
|
||||
if (crl == IntPtr.Zero)
|
||||
throw new MonoBtlsException ("Failed to read CRL from data.");
|
||||
|
||||
return new MonoBtlsX509Crl (new BoringX509CrlHandle (crl));
|
||||
} finally {
|
||||
Marshal.FreeHGlobal (data);
|
||||
}
|
||||
}
|
||||
|
||||
public MonoBtlsX509Revoked GetByCert (MonoBtlsX509 x509)
|
||||
{
|
||||
var revoked = mono_btls_x509_crl_get_by_cert (
|
||||
Handle.DangerousGetHandle (),
|
||||
x509.Handle.DangerousGetHandle ());
|
||||
if (revoked == IntPtr.Zero)
|
||||
return null;
|
||||
return new MonoBtlsX509Revoked (new MonoBtlsX509Revoked.BoringX509RevokedHandle (revoked));
|
||||
}
|
||||
|
||||
public unsafe MonoBtlsX509Revoked GetBySerial (byte[] serial)
|
||||
{
|
||||
fixed (void *ptr = serial)
|
||||
{
|
||||
var revoked = mono_btls_x509_crl_get_by_serial (
|
||||
Handle.DangerousGetHandle (), ptr, serial.Length);
|
||||
if (revoked == IntPtr.Zero)
|
||||
return null;
|
||||
return new MonoBtlsX509Revoked (new MonoBtlsX509Revoked.BoringX509RevokedHandle (revoked));
|
||||
}
|
||||
}
|
||||
|
||||
public int GetRevokedCount ()
|
||||
{
|
||||
return mono_btls_x509_crl_get_revoked_count (Handle.DangerousGetHandle ());
|
||||
}
|
||||
|
||||
public MonoBtlsX509Revoked GetRevoked (int index)
|
||||
{
|
||||
if (index >= GetRevokedCount ())
|
||||
throw new ArgumentOutOfRangeException ();
|
||||
|
||||
var revoked = mono_btls_x509_crl_get_revoked (
|
||||
Handle.DangerousGetHandle (), index);
|
||||
if (revoked == IntPtr.Zero)
|
||||
return null;
|
||||
return new MonoBtlsX509Revoked (new MonoBtlsX509Revoked.BoringX509RevokedHandle (revoked));
|
||||
}
|
||||
|
||||
public DateTime GetLastUpdate ()
|
||||
{
|
||||
var ticks = mono_btls_x509_crl_get_last_update (Handle.DangerousGetHandle ());
|
||||
return new DateTime (1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds (ticks);
|
||||
}
|
||||
|
||||
public DateTime GetNextUpdate ()
|
||||
{
|
||||
var ticks = mono_btls_x509_crl_get_next_update (Handle.DangerousGetHandle ());
|
||||
return new DateTime (1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds (ticks);
|
||||
}
|
||||
|
||||
public long GetVersion ()
|
||||
{
|
||||
return mono_btls_x509_crl_get_version (Handle.DangerousGetHandle ());
|
||||
}
|
||||
|
||||
public MonoBtlsX509Name GetIssuerName ()
|
||||
{
|
||||
var handle = mono_btls_x509_crl_get_issuer (Handle.DangerousGetHandle ());
|
||||
CheckError (handle != IntPtr.Zero);
|
||||
return new MonoBtlsX509Name (new MonoBtlsX509Name.BoringX509NameHandle (handle, false));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
111
mcs/class/System/Mono.Btls/MonoBtlsX509Error.cs
Normal file
111
mcs/class/System/Mono.Btls/MonoBtlsX509Error.cs
Normal file
@@ -0,0 +1,111 @@
|
||||
//
|
||||
// MonoBtlsX509Error.cs
|
||||
//
|
||||
// Author:
|
||||
// Martin Baulig <martin.baulig@xamarin.com>
|
||||
//
|
||||
// Copyright (c) 2016 Xamarin Inc. (http://www.xamarin.com)
|
||||
//
|
||||
// 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 SECURITY_DEP && MONO_FEATURE_BTLS
|
||||
using System;
|
||||
namespace Mono.Btls
|
||||
{
|
||||
// Keep in sync with x509_vfy.h
|
||||
enum MonoBtlsX509Error
|
||||
{
|
||||
OK = 0,
|
||||
/* illegal error (for uninitialized values, to avoid X509_V_OK): 1 */
|
||||
|
||||
UNABLE_TO_GET_ISSUER_CERT = 2,
|
||||
UNABLE_TO_GET_CRL = 3,
|
||||
UNABLE_TO_DECRYPT_CERT_SIGNATURE = 4,
|
||||
UNABLE_TO_DECRYPT_CRL_SIGNATURE = 5,
|
||||
UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY = 6,
|
||||
CERT_SIGNATURE_FAILURE = 7,
|
||||
CRL_SIGNATURE_FAILURE = 8,
|
||||
CERT_NOT_YET_VALID = 9,
|
||||
CERT_HAS_EXPIRED = 10,
|
||||
CRL_NOT_YET_VALID = 11,
|
||||
CRL_HAS_EXPIRED = 12,
|
||||
ERROR_IN_CERT_NOT_BEFORE_FIELD = 13,
|
||||
ERROR_IN_CERT_NOT_AFTER_FIELD = 14,
|
||||
ERROR_IN_CRL_LAST_UPDATE_FIELD = 15,
|
||||
ERROR_IN_CRL_NEXT_UPDATE_FIELD = 16,
|
||||
OUT_OF_MEM = 17,
|
||||
DEPTH_ZERO_SELF_SIGNED_CERT = 18,
|
||||
SELF_SIGNED_CERT_IN_CHAIN = 19,
|
||||
UNABLE_TO_GET_ISSUER_CERT_LOCALLY = 20,
|
||||
UNABLE_TO_VERIFY_LEAF_SIGNATURE = 21,
|
||||
CERT_CHAIN_TOO_LONG = 22,
|
||||
CERT_REVOKED = 23,
|
||||
INVALID_CA = 24,
|
||||
PATH_LENGTH_EXCEEDED = 25,
|
||||
INVALID_PURPOSE = 26,
|
||||
CERT_UNTRUSTED = 27,
|
||||
CERT_REJECTED = 28,
|
||||
/* These are 'informational' when looking for issuer cert */
|
||||
SUBJECT_ISSUER_MISMATCH = 29,
|
||||
AKID_SKID_MISMATCH = 30,
|
||||
AKID_ISSUER_SERIAL_MISMATCH = 31,
|
||||
KEYUSAGE_NO_CERTSIGN = 32,
|
||||
|
||||
UNABLE_TO_GET_CRL_ISSUER = 33,
|
||||
UNHANDLED_CRITICAL_EXTENSION = 34,
|
||||
KEYUSAGE_NO_CRL_SIGN = 35,
|
||||
UNHANDLED_CRITICAL_CRL_EXTENSION = 36,
|
||||
INVALID_NON_CA = 37,
|
||||
PROXY_PATH_LENGTH_EXCEEDED = 38,
|
||||
KEYUSAGE_NO_DIGITAL_SIGNATURE = 39,
|
||||
PROXY_CERTIFICATES_NOT_ALLOWED = 40,
|
||||
|
||||
INVALID_EXTENSION = 41,
|
||||
INVALID_POLICY_EXTENSION = 42,
|
||||
NO_EXPLICIT_POLICY = 43,
|
||||
DIFFERENT_CRL_SCOPE = 44,
|
||||
UNSUPPORTED_EXTENSION_FEATURE = 45,
|
||||
|
||||
UNNESTED_RESOURCE = 46,
|
||||
|
||||
PERMITTED_VIOLATION = 47,
|
||||
EXCLUDED_VIOLATION = 48,
|
||||
SUBTREE_MINMAX = 49,
|
||||
UNSUPPORTED_CONSTRAINT_TYPE = 51,
|
||||
UNSUPPORTED_CONSTRAINT_SYNTAX = 52,
|
||||
UNSUPPORTED_NAME_SYNTAX = 53,
|
||||
CRL_PATH_VALIDATION_ERROR = 54,
|
||||
|
||||
/* Suite B mode algorithm violation */
|
||||
SUITE_B_INVALID_VERSION = 56,
|
||||
SUITE_B_INVALID_ALGORITHM = 57,
|
||||
SUITE_B_INVALID_CURVE = 58,
|
||||
SUITE_B_INVALID_SIGNATURE_ALGORITHM = 59,
|
||||
SUITE_B_LOS_NOT_ALLOWED = 60,
|
||||
SUITE_B_CANNOT_SIGN_P_384_WITH_P_256 = 61,
|
||||
|
||||
/* Host, email and IP check errors */
|
||||
HOSTNAME_MISMATCH = 62,
|
||||
EMAIL_MISMATCH = 63,
|
||||
IP_ADDRESS_MISMATCH = 64,
|
||||
|
||||
/* The application is not happy */
|
||||
APPLICATION_VERIFICATION = 50
|
||||
}
|
||||
}
|
||||
#endif
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user