Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

View File

@@ -0,0 +1,127 @@
//
// System.Security.Cryptography.AsymmetricAlgorithm Class implementation
//
// Authors:
// Thomas Neidhart (tome@sbox.tugraz.at)
// Sebastien Pouliot (sebastien@ximian.com)
//
// Portions (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) 2004-2005, 2008 Novell, Inc (http://www.novell.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.
//
using System.Globalization;
using System.Runtime.InteropServices;
namespace System.Security.Cryptography {
[ComVisible (true)]
public abstract class AsymmetricAlgorithm : IDisposable {
protected int KeySizeValue;
protected KeySizes[] LegalKeySizesValue;
protected AsymmetricAlgorithm ()
{
}
public abstract string KeyExchangeAlgorithm {
get;
}
public virtual int KeySize {
get { return this.KeySizeValue; }
set {
if (!KeySizes.IsLegalKeySize (this.LegalKeySizesValue, value))
throw new CryptographicException (Locale.GetText ("Key size not supported by algorithm."));
this.KeySizeValue = value;
}
}
public virtual KeySizes[] LegalKeySizes {
get { return this.LegalKeySizesValue; }
}
public abstract string SignatureAlgorithm {
get;
}
#if NET_4_0
public void Dispose ()
#else
void IDisposable.Dispose ()
#endif
{
Dispose (true);
GC.SuppressFinalize (this); // Finalization is now unnecessary
}
public void Clear ()
{
Dispose (false);
}
#if NET_4_0
protected virtual void Dispose (bool disposing)
{
}
#else
protected abstract void Dispose (bool disposing);
#endif
public abstract void FromXmlString (string xmlString);
public abstract string ToXmlString (bool includePrivateParameters);
public static AsymmetricAlgorithm Create ()
{
#if FULL_AOT_RUNTIME
return new RSACryptoServiceProvider ();
#else
return Create ("System.Security.Cryptography.AsymmetricAlgorithm");
#endif
}
public static AsymmetricAlgorithm Create (string algName)
{
return (AsymmetricAlgorithm) CryptoConfig.CreateFromName (algName);
}
// parsing helper shared between DSA and RSA
internal static byte [] GetNamedParam (string xml, string param)
{
string start_element = "<" + param + ">";
int start = xml.IndexOf (start_element);
if (start == -1)
return null;
string end_element = "</" + param + ">";
int end = xml.IndexOf (end_element);
if ((end == -1) || (end <= start))
return null;
start += start_element.Length;
string base64 = xml.Substring (start, end - start);
return Convert.FromBase64String (base64);
}
}
}

View File

@@ -0,0 +1,48 @@
//
// System.Security.Cryptography AsymmetricKeyExchangeDeformatter Class implementation
//
// Authors:
// Thomas Neidhart (tome@sbox.tugraz.at)
//
// Copyright (C) 2004-2006 Novell, Inc (http://www.novell.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.
//
using System.Runtime.InteropServices;
namespace System.Security.Cryptography {
[ComVisible (true)]
public abstract class AsymmetricKeyExchangeDeformatter {
protected AsymmetricKeyExchangeDeformatter ()
{
}
public abstract string Parameters {
get;
set;
}
public abstract byte[] DecryptKeyExchange (byte[] rgb);
public abstract void SetKey (AsymmetricAlgorithm key);
}
}

View File

@@ -0,0 +1,51 @@
//
// System.Security.Cryptography AsymmetricKeyExchangeFormatter Class implementation
//
// Authors:
// Thomas Neidhart (tome@sbox.tugraz.at)
//
// Copyright (C) 2004-2006 Novell, Inc (http://www.novell.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.
//
using System.Runtime.InteropServices;
namespace System.Security.Cryptography {
[ComVisible (true)]
public abstract class AsymmetricKeyExchangeFormatter {
protected AsymmetricKeyExchangeFormatter ()
{
}
public abstract string Parameters {
get;
}
public abstract byte[] CreateKeyExchange (byte[] data);
public abstract byte[] CreateKeyExchange (byte[] data, Type symAlgType);
public abstract void SetKey (AsymmetricAlgorithm key);
}
}

View File

@@ -0,0 +1,57 @@
//
// System.Security.Cryptography AsymmetricSignatureDeformatter Class implementation
//
// Authors:
// Thomas Neidhart (tome@sbox.tugraz.at)
//
// Copyright (C) 2004-2006 Novell, Inc (http://www.novell.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.
//
using System.Runtime.InteropServices;
namespace System.Security.Cryptography {
[ComVisible (true)]
public abstract class AsymmetricSignatureDeformatter {
protected AsymmetricSignatureDeformatter ()
{
}
public abstract void SetHashAlgorithm (string strName);
public abstract void SetKey (AsymmetricAlgorithm key);
public abstract bool VerifySignature (byte[] rgbHash, byte[] rgbSignature);
public virtual bool VerifySignature (HashAlgorithm hash, byte[] rgbSignature)
{
if (hash == null)
throw new ArgumentNullException ("hash");
SetHashAlgorithm (hash.ToString ());
return VerifySignature (hash.Hash, rgbSignature);
}
}
}

View File

@@ -0,0 +1,55 @@
//
// System.Security.Cryptography AsymmetricSignatureFormatter Class implementation
//
// Authors:
// Thomas Neidhart (tome@sbox.tugraz.at)
//
// Copyright (C) 2004-2006 Novell, Inc (http://www.novell.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.
//
using System.Runtime.InteropServices;
namespace System.Security.Cryptography {
[ComVisible (true)]
public abstract class AsymmetricSignatureFormatter {
protected AsymmetricSignatureFormatter ()
{ }
public abstract void SetHashAlgorithm (string strName);
public abstract void SetKey (AsymmetricAlgorithm key);
public abstract byte[] CreateSignature (byte[] rgbHash);
public virtual byte[] CreateSignature (HashAlgorithm hash)
{
if (hash == null)
throw new ArgumentNullException ("hash");
SetHashAlgorithm (hash.ToString ());
return CreateSignature (hash.Hash);
}
}
}

View File

@@ -0,0 +1,84 @@
//
// System.Security.Cryptography.Base64Constants
//
// Authors:
// Sergey Chaban (serge@wildwestsoftware.com)
// Sebastien Pouliot <sebastien@ximian.com>
//
// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.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.
//
namespace System.Security.Cryptography {
internal static class Base64Constants {
// Pre-calculated tables
public static readonly byte[] EncodeTable = {
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
0x77, 0x78, 0x79, 0x7A, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2B, 0x2F
};
public static readonly byte[] DecodeTable = {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3E, 0xFF, 0xFF, 0xFF, 0x3F,
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33
};
// This code was used to generated the tables
/*
// This is the Base64 alphabet as described in RFC 2045 (Table 1, page 25).
private static string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
private static byte[] encodeTable;
private static byte[] decodeTable;
static Base64Constants ()
{
int len = ALPHABET.Length;
encodeTable = new byte [len];
for (int i=0; i < len; i++) {
encodeTable [i] = (byte) ALPHABET [i];
}
decodeTable = new byte [1 + (int)'z'];
for (int i=0; i < decodeTable.Length; i++) {
decodeTable [i] = Byte.MaxValue;
}
for (int i=0; i < len; i++) {
char ch = ALPHABET [i];
decodeTable [(int)ch] = (byte) i;
}
}
*/
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,44 @@
//
// System.Security.Cryptography CipherMode enumeration
//
// Authors:
// Matthew S. Ford (Matthew.S.Ford@Rose-Hulman.Edu)
//
// Copyright 2001 by Matthew S. Ford.
// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.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.
//
using System.Runtime.InteropServices;
namespace System.Security.Cryptography {
[Serializable]
[ComVisible (true)]
public enum CipherMode {
CBC = 0x1, // Cipher Block Chaining
ECB, // Electronic Codebook
OFB, // Output Feedback
CFB, // Cipher Feedback
CTS, // Cipher Text Stealing
}
}

View File

@@ -0,0 +1,112 @@
//
// System.Security.Cryptography CryptoAPITransform.cs
//
// Authors:
// Thomas Neidhart (tome@sbox.tugraz.at)
// Sebastien Pouliot (sebastien@ximian.com)
//
// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.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.
//
using System.Runtime.InteropServices;
using System.Security.Permissions;
namespace System.Security.Cryptography {
// Note: This class isn't used by Mono as all algorithms are provided with
// 100% managed implementations.
[ComVisible (true)]
public sealed class CryptoAPITransform : ICryptoTransform {
private bool m_disposed;
internal CryptoAPITransform ()
{
m_disposed = false;
}
public bool CanReuseTransform {
get { return true; }
}
public bool CanTransformMultipleBlocks {
get { return true; }
}
public int InputBlockSize {
get { return 0; }
}
public IntPtr KeyHandle {
[SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
get { return IntPtr.Zero; }
}
public int OutputBlockSize {
get { return 0; }
}
#if NET_4_0
public void Dispose ()
#else
void IDisposable.Dispose ()
#endif
{
Dispose (true);
GC.SuppressFinalize (this); // Finalization is now unnecessary
}
public void Clear ()
{
Dispose (false);
}
private void Dispose (bool disposing)
{
if (!m_disposed) {
// dispose unmanaged objects
if (disposing) {
// dispose managed objects
}
m_disposed = true;
}
}
public int TransformBlock (byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
{
return 0;
}
public byte[] TransformFinalBlock (byte[] inputBuffer, int inputOffset, int inputCount)
{
// Reset (); should be called here before returning final data
return null;
}
[ComVisible (false)]
public void Reset ()
{
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,234 @@
//
// CryptoConfig.cs: Handles cryptographic implementations and OIDs mappings.
//
// Authors:
// Sebastien Pouliot (sebastien@xamarin.com)
// Tim Coleman (tim@timcoleman.com)
//
// (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) Tim Coleman, 2004
// Copyright (C) 2004-2007,2011 Novell, Inc (http://www.novell.com)
// Copyright 2011 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 FULL_AOT_RUNTIME
// This is a special version of CryptoConfig that is not configurable and
// every "choice" is statiscally compiled. As long as CreateFromName is not
// used the linker will be able to eliminate the crypto code from the applications
using System.Runtime.InteropServices;
using System.Security.Permissions;
namespace System.Security.Cryptography {
[ComVisible (true)]
public partial class CryptoConfig {
// try to avoid hitting the CreateFromName overloads to help the linker
public static object CreateFromName (string name)
{
return CreateFromName (name, null);
}
[PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
public static object CreateFromName (string name, params object[] args)
{
if (name == null)
throw new ArgumentNullException ("name");
switch (name.ToLowerInvariant ()) {
case "system.security.cryptography.dsacryptoserviceprovider":
case "system.security.cryptography.dsa":
case "dsa":
return new DSACryptoServiceProvider ();
case "system.security.cryptography.dsasignaturedeformatter":
return new DSASignatureDeformatter ();
case "system.security.cryptography.dsasignatureformatter":
return new DSASignatureFormatter ();
case "system.security.cryptography.dsasignaturedescription":
case "http://www.w3.org/2000/09/xmldsig#dsa-sha1":
return new DSASignatureDescription ();
case "system.security.cryptography.descryptoserviceprovider":
case "system.security.cryptography.des":
case "des":
return new DESCryptoServiceProvider ();
case "system.security.cryptography.hmacmd5":
case "hmacmd5":
return new HMACMD5 ();
case "system.security.cryptography.hmacripemd160":
case "hmacripemd160":
case "http://www.w3.org/2001/04/xmldsig-more#hmac-ripemd160":
return new HMACRIPEMD160 ();
case "system.security.cryptography.keyedhashalgorithm":
case "system.security.cryptography.hmac":
case "system.security.cryptography.hmacsha1":
case "hmacsha1":
return new HMACSHA1 ();
case "system.security.cryptography.hmacsha256":
case "hmacsha256":
case "http://www.w3.org/2001/04/xmldsig-more#hmac-sha256":
return new HMACSHA256 ();
case "system.security.cryptography.hmacsha384":
case "hmacsha384":
case "http://www.w3.org/2001/04/xmldsig-more#hmac-sha384":
return new HMACSHA384 ();
case "system.security.cryptography.hmacsha512":
case "hmacsha512":
case "http://www.w3.org/2001/04/xmldsig-more#hmac-sha512":
return new HMACSHA512 ();
case "system.security.cryptography.mactripledes":
case "mactripledes":
return new MACTripleDES ();
case "system.security.cryptography.md5cryptoserviceprovider":
case "system.security.cryptography.md5":
case "md5":
return new MD5CryptoServiceProvider ();
case "system.security.cryptography.rc2cryptoserviceprovider":
case "system.security.cryptography.rc2":
case "rc2":
return new RC2CryptoServiceProvider ();
case "system.security.cryptography.symmetricalgorithm":
case "system.security.cryptography.rijndaelmanaged":
case "system.security.cryptography.rijndael":
case "rijndael":
return new RijndaelManaged ();
case "system.security.cryptography.ripemd160managed":
case "system.security.cryptography.ripemd160":
case "ripemd-160":
case "ripemd160":
return new RIPEMD160Managed ();
case "system.security.cryptography.rngcryptoserviceprovider":
case "system.security.cryptography.randomnumbergenerator":
case "randomnumbergenerator":
return new RNGCryptoServiceProvider ();
case "system.security.cryptography.asymmetricalgorithm":
case "system.security.cryptography.rsa":
case "rsa":
return new RSACryptoServiceProvider ();
case "system.security.cryptography.rsapkcs1signaturedeformatter":
return new RSAPKCS1SignatureDeformatter ();
case "system.security.cryptography.rsapkcs1signatureformatter":
return new RSAPKCS1SignatureFormatter ();
case "system.security.cryptography.rsapkcs1sha1signaturedescription":
case "http://www.w3.org/2000/09/xmldsig#rsa-sha1":
return new RSAPKCS1SHA1SignatureDescription ();
case "system.security.cryptography.hashalgorithm":
case "system.security.cryptography.sha1":
case "system.security.cryptography.sha1cryptoserviceprovider":
case "sha1":
case "sha":
case "http://www.w3.org/2000/09/xmldsig#sha1":
return new SHA1CryptoServiceProvider ();
case "system.security.cryptography.sha1managed":
return new SHA1Managed ();
case "system.security.cryptography.sha256managed":
case "system.security.cryptography.sha256":
case "sha256":
case "sha-256":
case "http://www.w3.org/2001/04/xmlenc#sha256":
return new SHA256Managed ();
case "system.security.cryptography.sha384managed":
case "system.security.cryptography.sha384":
case "sha384":
case "sha-384":
return new SHA384Managed ();
case "system.security.cryptography.sha512managed":
case "system.security.cryptography.sha512":
case "sha512":
case "sha-512":
case "http://www.w3.org/2001/04/xmlenc#sha512":
return new SHA512Managed ();
case "system.security.cryptography.tripledescryptoserviceprovider":
case "system.security.cryptography.tripledes":
case "triple des":
case "tripledes":
case "3des":
return new TripleDESCryptoServiceProvider ();
case "x509chain":
name = "System.Security.Cryptography.X509Certificates.X509Chain, System";
break;
case "aes":
name = "System.Security.Cryptography.AesManaged, System.Core";
break;
}
try {
// last resort, the request type might be available (if care is taken for the type not to be linked
// away) and that can allow some 3rd party code to work (e.g. extra algorithms) and make a few more
// unit tests happy
return Activator.CreateInstance (Type.GetType (name));
}
catch {
// method doesn't throw any exception
return null;
}
}
public static string MapNameToOID (string name)
{
if (name == null)
throw new ArgumentNullException ("name");
switch (name.ToLowerInvariant ()) {
case "system.security.cryptography.sha1cryptoserviceprovider":
case "system.security.cryptography.sha1managed":
case "system.security.cryptography.sha1":
case "sha1":
return "1.3.14.3.2.26";
case "system.security.cryptography.md5cryptoserviceprovider":
case "system.security.cryptography.md5":
case "md5":
return "1.2.840.113549.2.5";
case "system.security.cryptography.sha256managed":
case "system.security.cryptography.sha256":
case "sha256":
return "2.16.840.1.101.3.4.2.1";
case "system.security.cryptography.sha384managed":
case "system.security.cryptography.sha384":
case "sha384":
return "2.16.840.1.101.3.4.2.2";
case "system.security.cryptography.sha512managed":
case "system.security.cryptography.sha512":
case "sha512":
return "2.16.840.1.101.3.4.2.3";
case "system.security.cryptography.ripemd160managed":
case "system.security.cryptography.ripemd160":
case "ripemd160":
return "1.3.36.3.2.1";
case "tripledeskeywrap":
return "1.2.840.113549.1.9.16.3.6";
case "des":
return "1.3.14.3.2.7";
case "tripledes":
return "1.2.840.113549.3.7";
case "rc2":
return "1.2.840.113549.3.2";
default:
return null;
}
}
}
}
#endif

View File

@@ -0,0 +1,129 @@
//
// CryptoConfig.cs: Handles cryptographic implementations and OIDs mappings.
//
// Author:
// Sebastien Pouliot (sebastien@ximian.com)
// Tim Coleman (tim@timcoleman.com)
//
// (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) Tim Coleman, 2004
// Copyright (C) 2004-2007, 2009 Novell, Inc (http://www.novell.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.
//
namespace System.Security.Cryptography {
public partial class CryptoConfig {
public static byte[] EncodeOID (string str)
{
if (str == null)
throw new ArgumentNullException ("str");
char[] delim = { '.' };
string[] parts = str.Split (delim);
// according to X.208 n is always at least 2
if (parts.Length < 2) {
throw new CryptographicUnexpectedOperationException (
Locale.GetText ("OID must have at least two parts"));
}
// we're sure that the encoded OID is shorter than its string representation
byte[] oid = new byte [str.Length];
// now encoding value
try {
byte part0 = Convert.ToByte (parts [0]);
// OID[0] > 2 is invalid but "supported" in MS BCL
// uncomment next line to trap this error
// if (part0 > 2) throw new CryptographicUnexpectedOperationException ();
byte part1 = Convert.ToByte (parts [1]);
// OID[1] >= 40 is illegal for OID[0] < 2 because of the % 40
// however the syntax is "supported" in MS BCL
// uncomment next 2 lines to trap this error
//if ((part0 < 2) && (part1 >= 40))
// throw new CryptographicUnexpectedOperationException ();
oid[2] = Convert.ToByte (part0 * 40 + part1);
}
catch {
throw new CryptographicUnexpectedOperationException (
Locale.GetText ("Invalid OID"));
}
int j = 3;
for (int i = 2; i < parts.Length; i++) {
long x = Convert.ToInt64 (parts [i]);
if (x > 0x7F) {
byte[] num = EncodeLongNumber (x);
Buffer.BlockCopy (num, 0, oid, j, num.Length);
j += num.Length;
}
else
oid[j++] = Convert.ToByte (x);
}
int k = 2;
// copy the exact number of byte required
byte[] oid2 = new byte [j];
oid2[0] = 0x06; // always - this tag means OID
// Length (of value)
if (j > 0x7F) {
// for compatibility with MS BCL
throw new CryptographicUnexpectedOperationException (
Locale.GetText ("OID > 127 bytes"));
// comment exception and uncomment next 3 lines to remove restriction
//byte[] num = EncodeLongNumber (j);
//Buffer.BlockCopy (num, 0, oid, j, num.Length);
//k = num.Length + 1;
}
else
oid2 [1] = Convert.ToByte (j - 2);
Buffer.BlockCopy (oid, k, oid2, k, j - k);
return oid2;
}
// encode (7bits array) number greater than 127
private static byte[] EncodeLongNumber (long x)
{
// for MS BCL compatibility
// comment next two lines to remove restriction
if ((x > Int32.MaxValue) || (x < Int32.MinValue))
throw new OverflowException (Locale.GetText ("Part of OID doesn't fit in Int32"));
long y = x;
// number of bytes required to encode this number
int n = 1;
while (y > 0x7F) {
y = y >> 7;
n++;
}
byte[] num = new byte [n];
// encode all bytes
for (int i = 0; i < n; i++) {
y = x >> (7 * i);
y = y & 0x7F;
if (i != 0)
y += 0x80;
num[n-i-1] = Convert.ToByte (y);
}
return num;
}
}
}

View File

@@ -0,0 +1,383 @@
//
// System.Security.Cryptography CryptoStream.cs
//
// Authors:
// Thomas Neidhart (tome@sbox.tugraz.at)
// Sebastien Pouliot (sebastien@ximian.com)
//
// Portions (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) 2004-2005, 2007 Novell, Inc (http://www.novell.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.
//
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
#if NET_4_5
using System.Threading;
using System.Threading.Tasks;
#endif
namespace System.Security.Cryptography {
[ComVisible (true)]
public class CryptoStream : Stream {
private Stream _stream;
private ICryptoTransform _transform;
private CryptoStreamMode _mode;
private byte[] _currentBlock;
private bool _disposed;
private bool _flushedFinalBlock;
private int _partialCount;
private bool _endOfStream;
private byte[] _waitingBlock;
private int _waitingCount;
private byte[] _transformedBlock;
private int _transformedPos;
private int _transformedCount;
private byte[] _workingBlock;
private int _workingCount;
public CryptoStream (Stream stream, ICryptoTransform transform, CryptoStreamMode mode)
{
if (mode == CryptoStreamMode.Read) {
if (!stream.CanRead)
throw new ArgumentException (Locale.GetText ("Can't read on stream"));
} else if (mode == CryptoStreamMode.Write) {
if (!stream.CanWrite)
throw new ArgumentException (Locale.GetText ("Can't write on stream"));
} else {
throw new ArgumentException ("mode");
}
_stream = stream;
_transform = transform;
_mode = mode;
_disposed = false;
if (transform != null) {
_workingBlock = new byte [transform.InputBlockSize];
}
}
public override bool CanRead {
get { return (_mode == CryptoStreamMode.Read); }
}
public override bool CanSeek {
get { return false; }
}
public override bool CanWrite {
get { return (_mode == CryptoStreamMode.Write); }
}
public override long Length {
get { throw new NotSupportedException ("Length"); }
}
public override long Position {
get { throw new NotSupportedException ("Position"); }
set { throw new NotSupportedException ("Position"); }
}
public void Clear ()
{
Close ();
}
public override int Read ([In,Out] byte[] buffer, int offset, int count)
{
if (_mode != CryptoStreamMode.Read) {
throw new NotSupportedException (
Locale.GetText ("not in Read mode"));
}
if (offset < 0) {
throw new ArgumentOutOfRangeException ("offset",
Locale.GetText ("negative"));
}
if (count < 0) {
throw new ArgumentOutOfRangeException ("count",
Locale.GetText ("negative"));
}
// yes - buffer.Length will throw a NullReferenceException if buffer is null
// but by doing so we match MS implementation
// re-ordered to avoid integer overflow
if (offset > buffer.Length - count) {
throw new ArgumentException ("(offset+count)",
Locale.GetText ("buffer overflow"));
}
// for some strange reason ObjectDisposedException isn't throw
if (_workingBlock == null) {
return 0;
}
int result = 0;
if ((count == 0) || ((_transformedPos == _transformedCount) && (_endOfStream)))
return result;
if (_waitingBlock == null) {
_transformedBlock = new byte [_transform.OutputBlockSize << 2];
_transformedPos = 0;
_transformedCount = 0;
_waitingBlock = new byte [_transform.InputBlockSize];
_waitingCount = _stream.Read (_waitingBlock, 0, _waitingBlock.Length);
}
while (count > 0) {
// transformed but not yet returned
int length = (_transformedCount - _transformedPos);
// need more data - at least one full block must be available if we haven't reach the end of the stream
if (length < _transform.InputBlockSize) {
int transformed = 0;
// load a new block
_workingCount = _stream.Read (_workingBlock, 0, _transform.InputBlockSize);
_endOfStream = (_workingCount < _transform.InputBlockSize);
if (!_endOfStream) {
// transform the waiting block
transformed = _transform.TransformBlock (_waitingBlock, 0, _waitingBlock.Length, _transformedBlock, _transformedCount);
// transfer temporary to waiting
Buffer.BlockCopy (_workingBlock, 0, _waitingBlock, 0, _workingCount);
_waitingCount = _workingCount;
}
else {
if (_workingCount > 0) {
// transform the waiting block
transformed = _transform.TransformBlock (_waitingBlock, 0, _waitingBlock.Length, _transformedBlock, _transformedCount);
// transfer temporary to waiting
Buffer.BlockCopy (_workingBlock, 0, _waitingBlock, 0, _workingCount);
_waitingCount = _workingCount;
length += transformed;
_transformedCount += transformed;
}
if (!_flushedFinalBlock) {
byte[] input = _transform.TransformFinalBlock (_waitingBlock, 0, _waitingCount);
transformed = input.Length;
Buffer.BlockCopy (input, 0, _transformedBlock, _transformedCount, input.Length);
// zeroize this last block
Array.Clear (input, 0, input.Length);
_flushedFinalBlock = true;
}
}
length += transformed;
_transformedCount += transformed;
}
// compaction
if (_transformedPos > _transform.OutputBlockSize) {
Buffer.BlockCopy (_transformedBlock, _transformedPos, _transformedBlock, 0, length);
_transformedCount -= _transformedPos;
_transformedPos = 0;
}
length = ((count < length) ? count : length);
if (length > 0) {
Buffer.BlockCopy (_transformedBlock, _transformedPos, buffer, offset, length);
_transformedPos += length;
result += length;
offset += length;
count -= length;
}
// there may not be enough data in the stream for a
// complete block
if (((length != _transform.InputBlockSize) && (_waitingCount != _transform.InputBlockSize)) || (_endOfStream)) {
count = 0; // no more data can be read
}
}
return result;
}
public override void Write (byte[] buffer, int offset, int count)
{
if (_mode != CryptoStreamMode.Write) {
throw new NotSupportedException (
Locale.GetText ("not in Write mode"));
}
if (offset < 0) {
throw new ArgumentOutOfRangeException ("offset",
Locale.GetText ("negative"));
}
if (count < 0) {
throw new ArgumentOutOfRangeException ("count",
Locale.GetText ("negative"));
}
// re-ordered to avoid integer overflow
if (offset > buffer.Length - count) {
throw new ArgumentException ("(offset+count)",
Locale.GetText ("buffer overflow"));
}
if (_stream == null)
throw new ArgumentNullException ("inner stream was disposed");
int buffer_length = count;
// partial block (in progress)
if ((_partialCount > 0) && (_partialCount != _transform.InputBlockSize)) {
int remainder = _transform.InputBlockSize - _partialCount;
remainder = ((count < remainder) ? count : remainder);
Buffer.BlockCopy (buffer, offset, _workingBlock, _partialCount, remainder);
_partialCount += remainder;
offset += remainder;
count -= remainder;
}
int bufferPos = offset;
while (count > 0) {
if (_partialCount == _transform.InputBlockSize) {
if (_currentBlock == null)
_currentBlock = new byte [_transform.OutputBlockSize];
// use partial block to avoid (re)allocation
int len = _transform.TransformBlock (_workingBlock, 0, _partialCount, _currentBlock, 0);
_stream.Write (_currentBlock, 0, len);
// reset
_partialCount = 0;
}
if (_transform.CanTransformMultipleBlocks) {
// get the biggest multiple of InputBlockSize in count (without mul or div)
int size = (count & ~(_transform.InputBlockSize - 1));
int rem = (count & (_transform.InputBlockSize - 1));
// avoid reallocating memory at each call (reuse same buffer whenever possible)
int sizeWorkingBlock = (1 + size / _transform.InputBlockSize) * _transform.OutputBlockSize;
if (_workingBlock.Length < sizeWorkingBlock) {
Array.Clear (_workingBlock, 0, _workingBlock.Length);
_workingBlock = new byte [sizeWorkingBlock];
}
if (size > 0) {
int len = _transform.TransformBlock (buffer, offset, size, _workingBlock, 0);
_stream.Write (_workingBlock, 0, len);
}
if (rem > 0)
Buffer.BlockCopy (buffer, buffer_length - rem, _workingBlock, 0, rem);
_partialCount = rem;
count = 0; // the last block, if any, is in _workingBlock
} else {
int len = Math.Min (_transform.InputBlockSize - _partialCount, count);
Buffer.BlockCopy (buffer, bufferPos, _workingBlock, _partialCount, len);
bufferPos += len;
_partialCount += len;
count -= len;
// here block may be full, but we wont TransformBlock it until next iteration
// so that the last block will be called in FlushFinalBlock using TransformFinalBlock
}
}
}
public override void Flush ()
{
}
public void FlushFinalBlock ()
{
if (_flushedFinalBlock)
throw new NotSupportedException (Locale.GetText ("This method cannot be called twice."));
if (_disposed)
throw new NotSupportedException (Locale.GetText ("CryptoStream was disposed."));
_flushedFinalBlock = true;
byte[] finalBuffer = _transform.TransformFinalBlock (_workingBlock, 0, _partialCount);
if (_stream != null && _mode == CryptoStreamMode.Write) {
_stream.Write (finalBuffer, 0, finalBuffer.Length);
}
if (_stream is CryptoStream) {
// for cascading crypto streams
(_stream as CryptoStream).FlushFinalBlock ();
} else {
_stream.Flush ();
}
// zeroize
Array.Clear (finalBuffer, 0, finalBuffer.Length);
}
public override long Seek (long offset, SeekOrigin origin)
{
throw new NotSupportedException ("Seek");
}
// LAMESPEC: Exception NotSupportedException not documented
public override void SetLength (long value)
{
throw new NotSupportedException ("SetLength");
}
protected override void Dispose (bool disposing)
{
if (!_disposed) {
if (disposing) {
if (!_flushedFinalBlock) {
FlushFinalBlock ();
}
if (_stream != null)
_stream.Close ();
}
_disposed = true;
// always cleared for security reason
if (_workingBlock != null)
Array.Clear (_workingBlock, 0, _workingBlock.Length);
if (_currentBlock != null)
Array.Clear (_currentBlock, 0, _currentBlock.Length);
if (disposing) {
_stream = null;
_workingBlock = null;
_currentBlock = null;
}
}
}
#if NET_4_0
public bool HasFlushedFinalBlock {
get { return _flushedFinalBlock; }
}
#endif
#if NET_4_5
public override Task FlushAsync (CancellationToken cancellationToken)
{
return base.FlushAsync (cancellationToken);
}
public override Task<int> ReadAsync (byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
return base.ReadAsync (buffer, offset, count, cancellationToken);
}
public override Task WriteAsync (byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
return base.WriteAsync (buffer, offset, count, cancellationToken);
}
#endif
}
}

View File

@@ -0,0 +1,41 @@
//
// System.Security.Cryptography CryptoStreamMode enumeration
//
// Authors:
// Matthew S. Ford (Matthew.S.Ford@Rose-Hulman.Edu)
//
// Copyright 2001 by authors.
// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.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.
//
using System.Runtime.InteropServices;
namespace System.Security.Cryptography {
[Serializable]
[ComVisible (true)]
public enum CryptoStreamMode {
Read,
Write
}
}

View File

@@ -0,0 +1,76 @@
//
// System.Security.Cryptography.CryptographicException.cs
//
// Authors:
// Thomas Neidhart (tome@sbox.tugraz.at)
// Sebastien Pouliot <sebastien@ximian.com>
//
// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.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.
//
using System;
using System.Globalization;
using System.Runtime.Serialization;
using System.Runtime.InteropServices;
namespace System.Security.Cryptography {
[Serializable]
[ComVisible (true)]
public class CryptographicException : SystemException, _Exception {
public CryptographicException ()
: base (Locale.GetText ("Error occured during a cryptographic operation."))
{
// default to CORSEC_E_CRYPTO
// defined as EMAKEHR(0x1430) in CorError.h
HResult = unchecked ((int)0x80131430);
}
public CryptographicException (int hr)
{
HResult = hr;
}
public CryptographicException (string message)
: base (message)
{
HResult = unchecked ((int)0x80131430);
}
public CryptographicException (string message, Exception inner)
: base (message, inner)
{
HResult = unchecked ((int)0x80131430);
}
public CryptographicException (string format, string insert)
: base (String.Format (format, insert))
{
HResult = unchecked ((int)0x80131430);
}
protected CryptographicException (SerializationInfo info, StreamingContext context)
: base (info, context)
{
}
}
}

View File

@@ -0,0 +1,70 @@
//
// System.Security.Cryptography.CryptographicUnexpectedOperationException.cs
//
// Authors:
// Thomas Neidhart (tome@sbox.tugraz.at)
// Sebastien Pouliot <sebastien@ximian.com>
//
// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.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.
//
using System.Globalization;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
namespace System.Security.Cryptography {
[Serializable]
[ComVisible (true)]
public class CryptographicUnexpectedOperationException : CryptographicException {
public CryptographicUnexpectedOperationException ()
: base (Locale.GetText ("Unexpected error occured during a cryptographic operation."))
{
// Default to CORSEC_E_CRYPTO_UNEX_OPER (CorError.h)
HResult = unchecked ((int)0x80131431);
}
public CryptographicUnexpectedOperationException (string message)
: base (message)
{
HResult = unchecked ((int)0x80131431);
}
public CryptographicUnexpectedOperationException (string message, Exception inner)
: base (message, inner)
{
HResult = unchecked ((int)0x80131431);
}
public CryptographicUnexpectedOperationException (string format, string insert)
: base (String.Format(format, insert))
{
HResult = unchecked ((int)0x80131431);
}
protected CryptographicUnexpectedOperationException (SerializationInfo info, StreamingContext context)
: base (info, context)
{
}
}
}

View File

@@ -0,0 +1,111 @@
//
// CspKeyContainerInfo.cs: Information about CSP based key containers
//
// Author:
// Sebastien Pouliot <sebastien@ximian.com>
//
// (C) 2003 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.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.
//
using System.Runtime.InteropServices;
using System.Security.AccessControl;
namespace System.Security.Cryptography {
[ComVisible (true)]
public sealed class CspKeyContainerInfo {
private CspParameters _params;
internal bool _random;
// constructors
public CspKeyContainerInfo (CspParameters parameters)
{
_params = parameters;
_random = true; // by default we always generate a key
}
// properties
// always true for Mono
public bool Accessible {
get { return true; }
}
// always null for Mono
public CryptoKeySecurity CryptoKeySecurity {
get { return null; }
}
// always true for Mono
public bool Exportable {
get { return true; }
}
// always false for Mono
public bool HardwareDevice {
get { return false; }
}
public string KeyContainerName {
get { return _params.KeyContainerName; }
}
public KeyNumber KeyNumber {
get { return (KeyNumber)_params.KeyNumber; }
}
// always false for Mono
public bool MachineKeyStore {
get { return false; }
}
// always false for Mono
public bool Protected {
get { return false; }
}
public string ProviderName {
get { return _params.ProviderName; }
}
public int ProviderType {
get { return _params.ProviderType; }
}
// true if generated, false if imported
public bool RandomlyGenerated {
get { return _random; }
}
// always false for Mono
public bool Removable {
get { return false; }
}
public string UniqueKeyContainerName {
get { return _params.ProviderName + "\\" + _params.KeyContainerName; }
}
}
}

View File

@@ -0,0 +1,127 @@
//
// System.Security.Cryptography.CspParameters.cs
//
// Authors:
// Thomas Neidhart (tome@sbox.tugraz.at)
// Sebastien Pouliot <sebastien@ximian.com>
//
// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.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.
//
using System.Runtime.InteropServices;
using System.Security.AccessControl;
namespace System.Security.Cryptography {
[ComVisible (true)]
public sealed class CspParameters {
private CspProviderFlags _Flags;
public CspParameters ()
: this (1)
{
}
public CspParameters (int dwTypeIn)
: this (dwTypeIn, null)
{
}
public CspParameters (int dwTypeIn, string strProviderNameIn)
: this (dwTypeIn, null, null)
{
}
public CspParameters (int dwTypeIn, string strProviderNameIn, string strContainerNameIn)
{
ProviderType = dwTypeIn;
ProviderName = strProviderNameIn;
KeyContainerName = strContainerNameIn;
// not defined in specs, only tested from M$ impl
KeyNumber = -1;
}
public string KeyContainerName;
public int KeyNumber;
public string ProviderName;
public int ProviderType;
public CspProviderFlags Flags {
get { return _Flags; }
set { _Flags = value; }
}
private SecureString _password;
private IntPtr _windowHandle;
public CspParameters (int providerType, string providerName, string keyContainerName,
CryptoKeySecurity cryptoKeySecurity, IntPtr parentWindowHandle)
: this (providerType, providerName, keyContainerName)
{
if (cryptoKeySecurity != null)
CryptoKeySecurity = cryptoKeySecurity;
_windowHandle = parentWindowHandle;
}
public CspParameters (int providerType, string providerName, string keyContainerName,
CryptoKeySecurity cryptoKeySecurity, SecureString keyPassword)
: this (providerType, providerName, keyContainerName)
{
if (cryptoKeySecurity != null)
CryptoKeySecurity = cryptoKeySecurity;
_password = keyPassword;
}
internal CspParameters(CspParameters parameters)
: this(parameters.ProviderType, parameters.ProviderName, parameters.KeyContainerName)
{
if (parameters.CryptoKeySecurity != null)
CryptoKeySecurity = parameters.CryptoKeySecurity;
_Flags = parameters.Flags;
KeyNumber = parameters.KeyNumber;
_password = parameters.KeyPassword;
_windowHandle = parameters.ParentWindowHandle;
}
[MonoTODO ("access control isn't implemented")]
public CryptoKeySecurity CryptoKeySecurity {
get { throw new NotImplementedException (); }
set { throw new NotImplementedException (); }
}
public SecureString KeyPassword {
get { return _password; }
set { _password = value; }
}
public IntPtr ParentWindowHandle {
get { return _windowHandle; }
set { _windowHandle = value; }
}
}
}

View File

@@ -0,0 +1,51 @@
//
// System.Security.Cryptography CspProviderFlags enumeration
//
// Authors:
// Thomas Neidhart <tome@sbox.tugraz.at>
// Sebastien Pouliot <sebastien@ximian.com>
//
// Copyright (C) 2004-2005, 2011 Novell, Inc (http://www.novell.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.
//
using System.Runtime.InteropServices;
namespace System.Security.Cryptography {
[Flags]
[Serializable]
[ComVisible (true)]
public enum CspProviderFlags {
UseMachineKeyStore = 1,
UseDefaultKeyContainer = 2,
UseExistingKey = 8,
NoFlags = 0,
NoPrompt = 64,
UseArchivableKey = 16,
UseNonExportableKey = 4,
UseUserProtectedKey = 32,
#if NET_4_0
CreateEphemeralKey = 128
#endif
}
}

View File

@@ -0,0 +1,188 @@
//
// System.Security.Cryptography.DES.cs
//
// Author:
// Sergey Chaban (serge@wildwestsoftware.com)
// Sebastien Pouliot <sebastien@ximian.com>
//
// Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) 2004-2006 Novell, Inc (http://www.novell.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.
//
using System.Globalization;
using System.Runtime.InteropServices;
// References:
// a. FIPS PUB 46-3: Data Encryption Standard
// http://csrc.nist.gov/publications/fips/fips46-3/fips46-3.pdf
namespace System.Security.Cryptography {
[ComVisible (true)]
public abstract class DES : SymmetricAlgorithm {
private const int keySizeByte = 8;
protected DES ()
{
KeySizeValue = 64;
BlockSizeValue = 64;
FeedbackSizeValue = 8;
LegalKeySizesValue = new KeySizes[1];
LegalKeySizesValue[0] = new KeySizes(64, 64, 0);
LegalBlockSizesValue = new KeySizes[1];
LegalBlockSizesValue[0] = new KeySizes(64, 64, 0);
}
public static new DES Create ()
{
#if FULL_AOT_RUNTIME
return new System.Security.Cryptography.DESCryptoServiceProvider ();
#else
return Create ("System.Security.Cryptography.DES");
#endif
}
public static new DES Create (string algName)
{
return (DES) CryptoConfig.CreateFromName (algName);
}
// Ek(Ek(m)) = m
internal static readonly byte[,] weakKeys = {
{ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 },
{ 0x1F, 0x1F, 0x1F, 0x1F, 0x0F, 0x0F, 0x0F, 0x0F },
{ 0xE1, 0xE1, 0xE1, 0xE1, 0xF1, 0xF1, 0xF1, 0xF1 },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
};
// Ek1(Ek2(m)) = m
internal static readonly byte[,] semiWeakKeys = {
{ 0x00, 0x1E, 0x00, 0x1E, 0x00, 0x0E, 0x00, 0x0E }, // map to packed key 011F011F010E010E
{ 0x00, 0xE0, 0x00, 0xE0, 0x00, 0xF0, 0x00, 0xF0 }, // map to packed key 01E001E001F101F1
{ 0x00, 0xFE, 0x00, 0xFE, 0x00, 0xFE, 0x00, 0xFE }, // map to packed key 01FE01FE01FE01FE
{ 0x1E, 0x00, 0x1E, 0x00, 0x0E, 0x00, 0x0E, 0x00 }, // map to packed key 1F011F010E010E01
{ 0x1E, 0xE0, 0x1E, 0xE0, 0x0E, 0xF0, 0x0E, 0xF0 }, // map to packed key 1FE01FE00EF10EF1
{ 0x1E, 0xFE, 0x1E, 0xFE, 0x0E, 0xFE, 0x0E, 0xFE }, // map to packed key 1FFE1FFE0EFE0EFE
{ 0xE0, 0x00, 0xE0, 0x00, 0xF0, 0x00, 0xF0, 0x00 }, // map to packed key E001E001F101F101
{ 0xE0, 0x1E, 0xE0, 0x1E, 0xF0, 0x0E, 0xF0, 0x0E }, // map to packed key E01FE01FF10EF10E
{ 0xE0, 0xFE, 0xE0, 0xFE, 0xF0, 0xFE, 0xF0, 0xFE }, // map to packed key E0FEE0FEF1FEF1FE
{ 0xFE, 0x00, 0xFE, 0x00, 0xFE, 0x00, 0xFE, 0x00 }, // map to packed key FE01FE01FE01FE01
{ 0xFE, 0x1E, 0xFE, 0x1E, 0xFE, 0x0E, 0xFE, 0x0E }, // map to packed key FE1FFE1FFE0EFE0E
{ 0xFE, 0xE0, 0xFE, 0xE0, 0xFE, 0xF0, 0xFE, 0xF0 }, // map to packed key FEE0FEE0FEF1FEF1
};
public static bool IsWeakKey (byte[] rgbKey)
{
if (rgbKey == null)
throw new CryptographicException (Locale.GetText ("Null Key"));
if (rgbKey.Length != keySizeByte)
throw new CryptographicException (Locale.GetText ("Wrong Key Length"));
// (fast) pre-check with "weak bytes"
for (int i=0; i < rgbKey.Length; i++) {
switch (rgbKey [i] | 0x11) {
case 0x11:
case 0x1F:
case 0xF1:
case 0xFF:
break;
default:
return false;
}
}
// compare with known weak keys
for (int i=0; i < (weakKeys.Length >> 3); i++) {
int j = 0;
for (; j < rgbKey.Length; j++) {
if ((rgbKey [j] ^ weakKeys [i,j]) > 1)
break;
}
if (j==8)
return true;
}
return false;
}
public static bool IsSemiWeakKey (byte[] rgbKey)
{
if (rgbKey == null)
throw new CryptographicException (Locale.GetText ("Null Key"));
if (rgbKey.Length != keySizeByte)
throw new CryptographicException (Locale.GetText ("Wrong Key Length"));
// (fast) pre-check with "weak bytes"
for (int i=0; i < rgbKey.Length; i++) {
switch (rgbKey [i] | 0x11) {
case 0x11:
case 0x1F:
case 0xF1:
case 0xFF:
break;
default:
return false;
}
}
// compare with known weak keys
for (int i=0; i < (semiWeakKeys.Length >> 3); i++) {
int j = 0;
for (; j < rgbKey.Length; j++) {
if ((rgbKey [j] ^ semiWeakKeys [i,j]) > 1)
break;
}
if (j==8)
return true;
}
return false;
}
public override byte[] Key {
get {
if (KeyValue == null) {
// GenerateKey is responsible to return a valid key
// e.g. no weak or semi-weak keys
GenerateKey ();
}
return (byte[]) KeyValue.Clone ();
}
set {
if (value == null)
throw new ArgumentNullException ("Key");
if (value.Length != keySizeByte)
throw new ArgumentException (Locale.GetText ("Wrong Key Length"));
if (IsWeakKey (value))
throw new CryptographicException (Locale.GetText ("Weak Key"));
if (IsSemiWeakKey (value))
throw new CryptographicException (Locale.GetText ("Semi Weak Key"));
KeyValue = (byte[]) value.Clone ();
}
}
}
}

Some files were not shown because too many files have changed in this diff Show More