Imported Upstream version 4.0.0~alpha1

Former-commit-id: 806294f5ded97629b74c85c09952f2a74fe182d9
This commit is contained in:
Jo Shields
2015-04-07 09:35:12 +01:00
parent 283343f570
commit 3c1f479b9d
22469 changed files with 2931443 additions and 869343 deletions

View File

@@ -0,0 +1,77 @@
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace Microsoft.InfoCards
{
using System;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using System.Runtime.ConstrainedExecution;
using System.Security;
using Microsoft.InfoCards.Diagnostics;
using IDT = Microsoft.InfoCards.Diagnostics.InfoCardTrace;
//
// Summary:
// Provides a wrapper over memory allocated by GlobalAlloc
// guaranteeing that it will be freed during rude thread / appdomain unloads.
// Remarks:
// There is a small ---- in the usage of this class, as it is used to wrap return parameters
// immediatley following the function return.
//
internal class GlobalAllocSafeHandle : SafeHandle
{
[SuppressUnmanagedCodeSecurity]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[DllImport("Kernel32.dll", EntryPoint = "RtlZeroMemory", SetLastError = false)]
public static extern void ZeroMemory(IntPtr dest, Int32 size);
[SuppressUnmanagedCodeSecurity]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[DllImport("kernel32.dll", CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr GlobalFree(IntPtr hMem);
//
// How many bytes we currently wrap. This can be zero, as our usage allows for a valid handle
// backed by 0 bytes of allocated memory - specificially TransformBlock and TransformFinalBlock
// can return this by design.
//
private int m_bytes;
private GlobalAllocSafeHandle() : base(IntPtr.Zero, true) { m_bytes = 0; }
public int Length
{
set { m_bytes = value; }
get { return m_bytes; }
}
public override bool IsInvalid
{
get
{
return (IntPtr.Zero == base.handle);
}
}
//
// Summary:
// Clear the data held and release the memory.
//
protected override bool ReleaseHandle()
{
if (m_bytes > 0)
{
ZeroMemory(base.handle, m_bytes);
GlobalFree(base.handle);
m_bytes = 0;
}
return true;
}
}
}

View File

@@ -0,0 +1,89 @@
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace Microsoft.InfoCards
{
using System;
using System.Runtime.InteropServices;
using Microsoft.InfoCards.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.ConstrainedExecution;
using System.Security;
using IDT = Microsoft.InfoCards.Diagnostics.InfoCardTrace;
//
// Summary:
// Provides a wrapper over HGlobal alloc'd memory guaranteeing that the
// contents will be released in the presence of rude app domain and thread aborts.
//
internal class HGlobalSafeHandle : SafeHandle
{
public static HGlobalSafeHandle Construct()
{
return new HGlobalSafeHandle();
}
public static HGlobalSafeHandle Construct(string managedString)
{
IDT.DebugAssert(!String.IsNullOrEmpty(managedString), "null string");
int bytes = (managedString.Length + 1) * 2;
return new HGlobalSafeHandle(Marshal.StringToHGlobalUni(managedString), bytes);
}
public static HGlobalSafeHandle Construct(int bytes)
{
IDT.DebugAssert(bytes > 0, "attempt to allocate a handle with <= 0 bytes");
return new HGlobalSafeHandle(Marshal.AllocHGlobal(bytes), bytes);
}
[SuppressUnmanagedCodeSecurity]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[DllImport("Kernel32.dll", EntryPoint = "RtlZeroMemory", SetLastError = false)]
public static extern void ZeroMemory(IntPtr dest, Int32 size);
//
// The number of bytes allocated.
//
private int m_bytes;
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
private HGlobalSafeHandle(IntPtr toManage, int length)
: base(IntPtr.Zero, true)
{
m_bytes = length;
SetHandle(toManage);
}
private HGlobalSafeHandle() : base(IntPtr.Zero, true) { }
public override bool IsInvalid
{
get
{
return (IntPtr.Zero == base.handle);
}
}
//
// Summary:
// Zero the string contents and release the handle
//
protected override bool ReleaseHandle()
{
IDT.DebugAssert(!IsInvalid, "handle is invalid in release handle");
IDT.DebugAssert(0 != m_bytes, "invalid size");
ZeroMemory(base.handle, m_bytes);
Marshal.FreeHGlobal(base.handle);
return true;
}
}
}

View File

@@ -0,0 +1,55 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.InfoCards
{
using System.IdentityModel.Tokens;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;
// copied from IdentityModel\CryptoHelper.cs and they need to be kept in [....]. After V1, we need to rethink how we can have
// a single place to ask this question. Perhaps even add it as an extensibility
internal static class InfoCardCryptoHelper
{
internal static bool IsAsymmetricAlgorithm(string algorithm)
{
switch (algorithm)
{
case SecurityAlgorithms.DsaSha1Signature:
case SecurityAlgorithms.RsaSha1Signature:
case SecurityAlgorithms.RsaSha256Signature:
case SecurityAlgorithms.RsaOaepKeyWrap:
case SecurityAlgorithms.RsaV15KeyWrap:
return true;
default:
return false;
}
}
internal static bool IsSymmetricAlgorithm(string algorithm)
{
switch (algorithm)
{
case SecurityAlgorithms.HmacSha1Signature:
case SecurityAlgorithms.HmacSha256Signature:
case SecurityAlgorithms.Aes128Encryption:
case SecurityAlgorithms.Aes192Encryption:
case SecurityAlgorithms.Aes256Encryption:
case SecurityAlgorithms.TripleDesEncryption:
case SecurityAlgorithms.Aes128KeyWrap:
case SecurityAlgorithms.Aes192KeyWrap:
case SecurityAlgorithms.Aes256KeyWrap:
case SecurityAlgorithms.TripleDesKeyWrap:
case SecurityAlgorithms.Psha1KeyDerivation:
case SecurityAlgorithms.Psha1KeyDerivationDec2005:
return true;
default:
return false;
}
}
}
}