[pgsqlclient-checkins] pgsqlclient_10/Mono.Security/Mono.Security/Mono.Security BitConverterLE.cs,NO
Status: Inactive
Brought to you by:
carlosga_fb
From: Carlos Guzm?n ?l. <car...@us...> - 2004-05-09 12:04:14
|
Update of /cvsroot/pgsqlclient/pgsqlclient_10/Mono.Security/Mono.Security/Mono.Security In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv29860 Modified Files: ASN1.cs ASN1Convert.cs PKCS7.cs Added Files: BitConverterLE.cs Log Message: Sync Mono.Security stuff wit Mono Beta 1 sources Index: PKCS7.cs =================================================================== RCS file: /cvsroot/pgsqlclient/pgsqlclient_10/Mono.Security/Mono.Security/Mono.Security/PKCS7.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** PKCS7.cs 5 Mar 2004 23:14:21 -0000 1.1 --- PKCS7.cs 9 May 2004 12:04:00 -0000 1.2 *************** *** 4,17 **** // // Author: ! // Sebastien Pouliot (spo...@mo...) // // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com) // - using System; using System.Collections; using System.Security.Cryptography; - //using System.Security.Cryptography.X509Certificates; using Mono.Security.X509; --- 4,16 ---- // // Author: ! // Sebastien Pouliot <seb...@xi...> // // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com) + // (C) 2004 Novell (http://www.novell.com) // using System; using System.Collections; using System.Security.Cryptography; using Mono.Security.X509; *************** *** 19,45 **** namespace Mono.Security { ! internal class PKCS7 { ! // pkcs 1 ! public const string rsaEncryption = "1.2.840.113549.1.1.1"; ! // pkcs 7 ! public const string data = "1.2.840.113549.1.7.1"; ! public const string signedData = "1.2.840.113549.1.7.2"; ! public const string envelopedData = "1.2.840.113549.1.7.3"; ! public const string signedAndEnvelopedData = "1.2.840.113549.1.7.4"; ! public const string digestedData = "1.2.840.113549.1.7.5"; ! public const string encryptedData = "1.2.840.113549.1.7.6"; ! // pkcs 9 ! public const string contentType = "1.2.840.113549.1.9.3"; ! public const string messageDigest = "1.2.840.113549.1.9.4"; ! public const string signingTime = "1.2.840.113549.1.9.5"; ! public const string countersignature = "1.2.840.113549.1.9.6"; ! public PKCS7 () {} static public ASN1 Attribute (string oid, ASN1 value) { ASN1 attr = new ASN1 (0x30); ! attr.Add (ASN1Convert.FromOID (oid)); ASN1 aset = attr.Add (new ASN1 (0x31)); aset.Add (value); --- 18,57 ---- namespace Mono.Security { ! #if INSIDE_CORLIB ! internal ! #else ! public ! #endif ! sealed class PKCS7 { ! public class Oid { ! // pkcs 1 ! public const string rsaEncryption = "1.2.840.113549.1.1.1"; ! // pkcs 7 ! public const string data = "1.2.840.113549.1.7.1"; ! public const string signedData = "1.2.840.113549.1.7.2"; ! public const string envelopedData = "1.2.840.113549.1.7.3"; ! public const string signedAndEnvelopedData = "1.2.840.113549.1.7.4"; ! public const string digestedData = "1.2.840.113549.1.7.5"; ! public const string encryptedData = "1.2.840.113549.1.7.6"; ! // pkcs 9 ! public const string contentType = "1.2.840.113549.1.9.3"; ! public const string messageDigest = "1.2.840.113549.1.9.4"; ! public const string signingTime = "1.2.840.113549.1.9.5"; ! public const string countersignature = "1.2.840.113549.1.9.6"; ! public Oid () ! { ! } ! } ! ! private PKCS7 () ! { ! } static public ASN1 Attribute (string oid, ASN1 value) { ASN1 attr = new ASN1 (0x30); ! attr.Add (ASN1Convert.FromOid (oid)); ASN1 aset = attr.Add (new ASN1 (0x31)); aset.Add (value); *************** *** 50,63 **** { ASN1 ai = new ASN1 (0x30); ! ai.Add (ASN1Convert.FromOID (oid)); ai.Add (new ASN1 (0x05)); // NULL return ai; } ! static public ASN1 AlgorithmIdentifier (string oid, ASN1 param) { ASN1 ai = new ASN1 (0x30); ! ai.Add (ASN1Convert.FromOID (oid)); ! ai.Add (param); return ai; } --- 62,75 ---- { ASN1 ai = new ASN1 (0x30); ! ai.Add (ASN1Convert.FromOid (oid)); ai.Add (new ASN1 (0x05)); // NULL return ai; } ! static public ASN1 AlgorithmIdentifier (string oid, ASN1 parameters) { ASN1 ai = new ASN1 (0x30); ! ai.Add (ASN1Convert.FromOid (oid)); ! ai.Add (parameters); return ai; } *************** *** 126,130 **** if (asn1[0].Tag != 0x06) throw new ArgumentException ("Invalid contentType"); ! contentType = ASN1Convert.ToOID (asn1[0]); if (asn1.Count > 1) { if (asn1[1].Tag != 0xA0) --- 138,142 ---- if (asn1[0].Tag != 0x06) throw new ArgumentException ("Invalid contentType"); ! contentType = ASN1Convert.ToOid (asn1[0]); if (asn1.Count > 1) { if (asn1[1].Tag != 0xA0) *************** *** 153,157 **** ASN1 contentInfo = new ASN1 (0x30); // contentType ContentType, -> ContentType ::= OBJECT IDENTIFIER ! contentInfo.Add (ASN1Convert.FromOID (contentType)); // content [0] EXPLICIT ANY DEFINED BY contentType OPTIONAL if ((content != null) && (content.Count > 0)) --- 165,169 ---- ASN1 contentInfo = new ASN1 (0x30); // contentType ContentType, -> ContentType ::= OBJECT IDENTIFIER ! contentInfo.Add (ASN1Convert.FromOid (contentType)); // content [0] EXPLICIT ANY DEFINED BY contentType OPTIONAL if ((content != null) && (content.Count > 0)) *************** *** 184,188 **** public EncryptedData (byte[] data) ! : this (new ASN1 (data)) {} public EncryptedData (ASN1 asn1) : this () --- 196,202 ---- public EncryptedData (byte[] data) ! : this (new ASN1 (data)) ! { ! } public EncryptedData (ASN1 asn1) : this () *************** *** 202,211 **** if (contentType.Tag != 0x06) throw new ArgumentException ("missing EncryptedContentInfo.ContentType"); ! _content = new ContentInfo (ASN1Convert.ToOID (contentType)); ASN1 contentEncryptionAlgorithm = encryptedContentInfo [1]; if (contentEncryptionAlgorithm.Tag != 0x30) throw new ArgumentException ("missing EncryptedContentInfo.ContentEncryptionAlgorithmIdentifier"); ! _encryptionAlgorithm = new ContentInfo (ASN1Convert.ToOID (contentEncryptionAlgorithm [0])); _encryptionAlgorithm.Content = contentEncryptionAlgorithm [1]; --- 216,225 ---- if (contentType.Tag != 0x06) throw new ArgumentException ("missing EncryptedContentInfo.ContentType"); ! _content = new ContentInfo (ASN1Convert.ToOid (contentType)); ASN1 contentEncryptionAlgorithm = encryptedContentInfo [1]; if (contentEncryptionAlgorithm.Tag != 0x30) throw new ArgumentException ("missing EncryptedContentInfo.ContentEncryptionAlgorithmIdentifier"); ! _encryptionAlgorithm = new ContentInfo (ASN1Convert.ToOid (contentEncryptionAlgorithm [0])); _encryptionAlgorithm.Content = contentEncryptionAlgorithm [1]; *************** *** 229,233 **** public byte[] EncryptedContent { ! get { return _encrypted; } } --- 243,251 ---- public byte[] EncryptedContent { ! get { ! if (_encrypted == null) ! return null; ! return (byte[]) _encrypted.Clone (); ! } } *************** *** 284,288 **** public EnvelopedData (byte[] data) ! : this (new ASN1 (data)) {} public EnvelopedData (ASN1 asn1) : this () --- 302,308 ---- public EnvelopedData (byte[] data) ! : this (new ASN1 (data)) ! { ! } public EnvelopedData (ASN1 asn1) : this () *************** *** 312,321 **** if (contentType.Tag != 0x06) throw new ArgumentException ("missing EncryptedContentInfo.ContentType"); ! _content = new ContentInfo (ASN1Convert.ToOID (contentType)); ASN1 contentEncryptionAlgorithm = encryptedContentInfo [1]; if (contentEncryptionAlgorithm.Tag != 0x30) throw new ArgumentException ("missing EncryptedContentInfo.ContentEncryptionAlgorithmIdentifier"); ! _encryptionAlgorithm = new ContentInfo (ASN1Convert.ToOID (contentEncryptionAlgorithm [0])); _encryptionAlgorithm.Content = contentEncryptionAlgorithm [1]; --- 332,341 ---- if (contentType.Tag != 0x06) throw new ArgumentException ("missing EncryptedContentInfo.ContentType"); ! _content = new ContentInfo (ASN1Convert.ToOid (contentType)); ASN1 contentEncryptionAlgorithm = encryptedContentInfo [1]; if (contentEncryptionAlgorithm.Tag != 0x30) throw new ArgumentException ("missing EncryptedContentInfo.ContentEncryptionAlgorithmIdentifier"); ! _encryptionAlgorithm = new ContentInfo (ASN1Convert.ToOid (contentEncryptionAlgorithm [0])); _encryptionAlgorithm.Content = contentEncryptionAlgorithm [1]; *************** *** 343,347 **** public byte[] EncryptedContent { ! get { return _encrypted; } } --- 363,371 ---- public byte[] EncryptedContent { ! get { ! if (_encrypted == null) ! return null; ! return (byte[]) _encrypted.Clone (); ! } } *************** *** 361,365 **** ASN1 digestAlgorithms = signedData.Add (new ASN1 (0x31)); if (hashAlgorithm != null) { ! string hashOid = CryptoConfig.MapNameToOID (hashAlgorithm); digestAlgorithms.Add (AlgorithmIdentifier (hashOid)); } --- 385,389 ---- ASN1 digestAlgorithms = signedData.Add (new ASN1 (0x31)); if (hashAlgorithm != null) { ! string hashOid = CryptoConfig.MapNameToOid (hashAlgorithm); digestAlgorithms.Add (AlgorithmIdentifier (hashOid)); } *************** *** 444,448 **** ASN1 keyEncryptionAlgorithm = data [2]; ! _oid = ASN1Convert.ToOID (keyEncryptionAlgorithm [0]); ASN1 encryptedKey = data [3]; --- 468,472 ---- ASN1 keyEncryptionAlgorithm = data [2]; ! _oid = ASN1Convert.ToOid (keyEncryptionAlgorithm [0]); ASN1 encryptedKey = data [3]; *************** *** 455,463 **** public byte[] Key { ! get { return _key; } } public byte[] SubjectKeyIdentifier { ! get { return _ski; } } --- 479,495 ---- public byte[] Key { ! get { ! if (_key == null) ! return null; ! return (byte[]) _key.Clone (); ! } } public byte[] SubjectKeyIdentifier { ! get { ! if (_ski == null) ! return null; ! return (byte[]) _ski.Clone (); ! } } *************** *** 467,471 **** public byte[] Serial { ! get { return _serial; } } --- 499,507 ---- public byte[] Serial { ! get { ! if (_serial == null) ! return null; ! return (byte[]) _serial.Clone (); ! } } *************** *** 504,508 **** public SignedData (byte[] data) ! : this (new ASN1 (data)) {} public SignedData (ASN1 asn1) --- 540,546 ---- public SignedData (byte[] data) ! : this (new ASN1 (data)) ! { ! } public SignedData (ASN1 asn1) *************** *** 552,556 **** } ! public ArrayList CRLs { get { return crls; } } --- 590,594 ---- } ! public ArrayList Crls { get { return crls; } } *************** *** 596,600 **** byte[] idcHash = ha.ComputeHash (ci[1][0].Value); ASN1 md = new ASN1 (0x30); ! mda = Attribute (messageDigest, md.Add (new ASN1 (0x04, idcHash))); signerInfo.AuthenticatedAttributes.Add (mda); } --- 634,638 ---- byte[] idcHash = ha.ComputeHash (ci[1][0].Value); ASN1 md = new ASN1 (0x30); ! mda = Attribute (Oid.messageDigest, md.Add (new ASN1 (0x04, idcHash))); signerInfo.AuthenticatedAttributes.Add (mda); } *************** *** 684,688 **** // digestAlgorithm DigestAlgorithmIdentifier ASN1 digestAlgorithm = asn1 [0][2]; ! hashAlgorithm = ASN1Convert.ToOID (digestAlgorithm [0]); // authenticatedAttributes [0] IMPLICIT Attributes OPTIONAL --- 722,726 ---- // digestAlgorithm DigestAlgorithmIdentifier ASN1 digestAlgorithm = asn1 [0][2]; ! hashAlgorithm = ASN1Convert.ToOid (digestAlgorithm [0]); // authenticatedAttributes [0] IMPLICIT Attributes OPTIONAL *************** *** 697,701 **** // digestEncryptionAlgorithm DigestEncryptionAlgorithmIdentifier ASN1 digestEncryptionAlgorithm = asn1 [0][n++]; ! string digestEncryptionAlgorithmOid = ASN1Convert.ToOID (digestEncryptionAlgorithm [0]); // encryptedDigest EncryptedDigest --- 735,739 ---- // digestEncryptionAlgorithm DigestEncryptionAlgorithmIdentifier ASN1 digestEncryptionAlgorithm = asn1 [0][n++]; ! string digestEncryptionAlgorithmOid = ASN1Convert.ToOid (digestEncryptionAlgorithm [0]); // encryptedDigest EncryptedDigest *************** *** 717,725 **** public byte[] SerialNumber { ! get { return (byte[]) serial.Clone (); } } public byte[] SubjectKeyIdentifier { ! get { return (byte[]) ski.Clone (); } } --- 755,771 ---- public byte[] SerialNumber { ! get { ! if (serial == null) ! return null; ! return (byte[]) serial.Clone (); ! } } public byte[] SubjectKeyIdentifier { ! get { ! if (ski == null) ! return null; ! return (byte[]) ski.Clone (); ! } } *************** *** 748,752 **** public byte[] Signature { ! get { return (byte[]) signature.Clone (); } } --- 794,802 ---- public byte[] Signature { ! get { ! if (signature == null) ! return null; ! return (byte[]) signature.Clone (); ! } } *************** *** 781,785 **** // digestEncryptionAlgorithm DigestEncryptionAlgorithmIdentifier, if (key is RSA) { ! signerInfo.Add (AlgorithmIdentifier (PKCS7.rsaEncryption)); RSAPKCS1SignatureFormatter r = new RSAPKCS1SignatureFormatter (key); --- 831,835 ---- // digestEncryptionAlgorithm DigestEncryptionAlgorithmIdentifier, if (key is RSA) { ! signerInfo.Add (AlgorithmIdentifier (PKCS7.Oid.rsaEncryption)); RSAPKCS1SignatureFormatter r = new RSAPKCS1SignatureFormatter (key); Index: ASN1Convert.cs =================================================================== RCS file: /cvsroot/pgsqlclient/pgsqlclient_10/Mono.Security/Mono.Security/Mono.Security/ASN1Convert.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** ASN1Convert.cs 10 Feb 2004 09:42:35 -0000 1.1 --- ASN1Convert.cs 9 May 2004 12:04:00 -0000 1.2 *************** *** 2,16 **** // ASN1Convert.cs: Abstract Syntax Notation 1 convertion routines // ! // Author: ! // Sebastien Pouliot (spo...@mo...) // // (C) 2003 Motus Technologies Inc. (http://www.motus.com) // using System; using System.Collections; using System.Security.Cryptography; using System.Text; - using System.Globalization; namespace Mono.Security { --- 2,19 ---- // ASN1Convert.cs: Abstract Syntax Notation 1 convertion routines // ! // Authors: ! // Sebastien Pouliot <seb...@xi...> ! // Jesper Pedersen <je...@it...> // // (C) 2003 Motus Technologies Inc. (http://www.motus.com) + // (C) 2004 Novell (http://www.novell.com) + // (C) 2004 IT+ A/S (http://www.itplus.dk) // using System; using System.Collections; + using System.Globalization; using System.Security.Cryptography; using System.Text; namespace Mono.Security { *************** *** 20,24 **** // http://www.itu.int/ITU-T/studygroups/com17/languages/ ! internal class ASN1Convert { // RFC3280, section 4.2.1.5 --- 23,36 ---- // http://www.itu.int/ITU-T/studygroups/com17/languages/ ! #if INSIDE_CORLIB ! internal ! #else ! public ! #endif ! sealed class ASN1Convert { ! ! private ASN1Convert () ! { ! } // RFC3280, section 4.2.1.5 *************** *** 30,38 **** if (dt.Year < 2050) { // UTCTIME ! return new ASN1 (0x17, Encoding.ASCII.GetBytes (dt.ToString ("yyMMddHHmmss") + "Z")); } else { // GENERALIZEDTIME ! return new ASN1 (0x18, Encoding.ASCII.GetBytes (dt.ToString ("yyyyMMddHHmmss") + "Z")); } } --- 42,52 ---- if (dt.Year < 2050) { // UTCTIME ! return new ASN1 (0x17, Encoding.ASCII.GetBytes ( ! dt.ToString ("yyMMddHHmmss", CultureInfo.InvariantCulture) + "Z")); } else { // GENERALIZEDTIME ! return new ASN1 (0x18, Encoding.ASCII.GetBytes ( ! dt.ToString ("yyyyMMddHHmmss", CultureInfo.InvariantCulture) + "Z")); } } *************** *** 40,74 **** static public ASN1 FromInt32 (Int32 value) { ! byte[] integer = BitConverter.GetBytes (value); int x = 3; while (integer [x] == 0x00) x--; ASN1 asn1 = new ASN1 (0x02); ! if (x == 3) ! asn1.Value = integer; ! else { ! byte[] smallerInt = new byte [x + 1]; ! Array.Copy (integer, 0, smallerInt, 0, smallerInt.Length); ! asn1.Value = smallerInt; } return asn1; } ! static public ASN1 FromOID (string oid) { return new ASN1 (CryptoConfig.EncodeOID (oid)); } ! static public ASN1 FromUnsignedBigInteger (byte[] integer) { ! if (integer [0] == 0x00) { // this first byte is added so we're sure it's an unsigned integer // however we can't feed it into RSAParameters or DSAParameters ! int length = integer.Length + 1; byte[] uinteger = new byte [length]; ! Array.Copy (integer, 0, uinteger, 1, length); ! integer = uinteger; } ! return new ASN1 (0x02, integer); } --- 54,90 ---- static public ASN1 FromInt32 (Int32 value) { ! byte[] integer = BitConverterLE.GetBytes (value); int x = 3; while (integer [x] == 0x00) x--; ASN1 asn1 = new ASN1 (0x02); ! ! byte[] smallerInt = new byte [x + 1]; ! int index = smallerInt.Length - 1; ! for (int i = 0; i < smallerInt.Length; i++) { ! smallerInt [index] = integer [i]; ! index--; } + asn1.Value = smallerInt; + return asn1; } ! static public ASN1 FromOid (string oid) { return new ASN1 (CryptoConfig.EncodeOID (oid)); } ! static public ASN1 FromUnsignedBigInteger (byte[] big) { ! if (big [0] == 0x00) { // this first byte is added so we're sure it's an unsigned integer // however we can't feed it into RSAParameters or DSAParameters ! int length = big.Length + 1; byte[] uinteger = new byte [length]; ! Buffer.BlockCopy (big, 0, uinteger, 1, length); ! big = uinteger; } ! return new ASN1 (0x02, big); } *************** *** 85,89 **** // Convert a binary encoded OID to human readable string representation of // an OID (IETF style). Based on DUMPASN1.C from Peter Gutmann. ! static public string ToOID (ASN1 asn1) { byte[] aOID = asn1.Value; --- 101,105 ---- // Convert a binary encoded OID to human readable string representation of // an OID (IETF style). Based on DUMPASN1.C from Peter Gutmann. ! static public string ToOid (ASN1 asn1) { byte[] aOID = asn1.Value; *************** *** 97,103 **** x = 2; } ! sb.Append (x.ToString ()); sb.Append ("."); ! sb.Append (y.ToString ()); ulong val = 0; for (x = 1; x < aOID.Length; x++) { --- 113,119 ---- x = 2; } ! sb.Append (x.ToString (CultureInfo.InvariantCulture)); sb.Append ("."); ! sb.Append (y.ToString (CultureInfo.InvariantCulture)); ulong val = 0; for (x = 1; x < aOID.Length; x++) { *************** *** 105,109 **** if ( !((aOID [x] & 0x80) == 0x80)) { sb.Append ("."); ! sb.Append (val.ToString ()); val = 0; } --- 121,125 ---- if ( !((aOID [x] & 0x80) == 0x80)) { sb.Append ("."); ! sb.Append (val.ToString (CultureInfo.InvariantCulture)); val = 0; } *************** *** 123,127 **** case 13: // RFC3280: 4.1.2.5.1 UTCTime ! int year = Convert.ToInt16 (t.Substring (0, 2)); // Where YY is greater than or equal to 50, the // year SHALL be interpreted as 19YY; and --- 139,143 ---- case 13: // RFC3280: 4.1.2.5.1 UTCTime ! int year = Convert.ToInt16 (t.Substring (0, 2), CultureInfo.InvariantCulture); // Where YY is greater than or equal to 50, the // year SHALL be interpreted as 19YY; and *************** *** 138,142 **** break; } ! return DateTime.ParseExact (t, mask, CultureInfo.CurrentCulture.DateTimeFormat, DateTimeStyles.AdjustToUniversal); } } --- 154,158 ---- break; } ! return DateTime.ParseExact (t, mask, null); } } --- NEW FILE: BitConverterLE.cs --- // // Mono.Security.BitConverterLE.cs // Like System.BitConverter but always little endian // // Author: // Bernie Solomon // using System; namespace Mono.Security { internal sealed class BitConverterLE { private BitConverterLE () { } unsafe private static byte[] GetUShortBytes (byte *bytes) { if (BitConverter.IsLittleEndian) return new byte [] { bytes [0], bytes [1] }; else return new byte [] { bytes [1], bytes [0] }; } unsafe private static byte[] GetUIntBytes (byte *bytes) { if (BitConverter.IsLittleEndian) return new byte [] { bytes [0], bytes [1], bytes [2], bytes [3] }; else return new byte [] { bytes [3], bytes [2], bytes [1], bytes [0] }; } unsafe private static byte[] GetULongBytes (byte *bytes) { if (BitConverter.IsLittleEndian) return new byte [] { bytes [0], bytes [1], bytes [2], bytes [3], bytes [4], bytes [5], bytes [6], bytes [7] }; else return new byte [] { bytes [7], bytes [6], bytes [5], bytes [4], bytes [3], bytes [2], bytes [1], bytes [0] }; } unsafe internal static byte[] GetBytes (bool value) { return new byte [] { value ? (byte)1 : (byte)0 }; } unsafe internal static byte[] GetBytes (char value) { return GetUShortBytes ((byte *) &value); } unsafe internal static byte[] GetBytes (short value) { return GetUShortBytes ((byte *) &value); } unsafe internal static byte[] GetBytes (int value) { return GetUIntBytes ((byte *) &value); } unsafe internal static byte[] GetBytes (long value) { return GetULongBytes ((byte *) &value); } unsafe internal static byte[] GetBytes (ushort value) { return GetUShortBytes ((byte *) &value); } unsafe internal static byte[] GetBytes (uint value) { return GetUIntBytes ((byte *) &value); } unsafe internal static byte[] GetBytes (ulong value) { return GetULongBytes ((byte *) &value); } unsafe internal static byte[] GetBytes (float value) { return GetUIntBytes ((byte *) &value); } unsafe internal static byte[] GetBytes (double value) { return GetULongBytes ((byte *) &value); } unsafe private static void UShortFromBytes (byte *dst, byte[] src, int startIndex) { if (BitConverter.IsLittleEndian) { dst [0] = src [startIndex]; dst [1] = src [startIndex + 1]; } else { dst [0] = src [startIndex + 1]; dst [1] = src [startIndex]; } } unsafe private static void UIntFromBytes (byte *dst, byte[] src, int startIndex) { if (BitConverter.IsLittleEndian) { dst [0] = src [startIndex]; dst [1] = src [startIndex + 1]; dst [2] = src [startIndex + 2]; dst [3] = src [startIndex + 3]; } else { dst [0] = src [startIndex + 3]; dst [1] = src [startIndex + 2]; dst [2] = src [startIndex + 1]; dst [3] = src [startIndex]; } } unsafe private static void ULongFromBytes (byte *dst, byte[] src, int startIndex) { if (BitConverter.IsLittleEndian) { for (int i = 0; i < 8; ++i) dst [i] = src [startIndex + i]; } else { for (int i = 0; i < 8; ++i) dst [i] = src [startIndex + (7 - i)]; } } unsafe internal static bool ToBoolean (byte[] value, int startIndex) { return value [startIndex] != 0; } unsafe internal static char ToChar (byte[] value, int startIndex) { char ret; UShortFromBytes ((byte *) &ret, value, startIndex); return ret; } unsafe internal static short ToInt16 (byte[] value, int startIndex) { short ret; UShortFromBytes ((byte *) &ret, value, startIndex); return ret; } unsafe internal static int ToInt32 (byte[] value, int startIndex) { int ret; UIntFromBytes ((byte *) &ret, value, startIndex); return ret; } unsafe internal static long ToInt64 (byte[] value, int startIndex) { long ret; ULongFromBytes ((byte *) &ret, value, startIndex); return ret; } unsafe internal static ushort ToUInt16 (byte[] value, int startIndex) { ushort ret; UShortFromBytes ((byte *) &ret, value, startIndex); return ret; } unsafe internal static uint ToUInt32 (byte[] value, int startIndex) { uint ret; UIntFromBytes ((byte *) &ret, value, startIndex); return ret; } unsafe internal static ulong ToUInt64 (byte[] value, int startIndex) { ulong ret; ULongFromBytes ((byte *) &ret, value, startIndex); return ret; } unsafe internal static float ToSingle (byte[] value, int startIndex) { float ret; UIntFromBytes ((byte *) &ret, value, startIndex); return ret; } unsafe internal static double ToDouble (byte[] value, int startIndex) { double ret; ULongFromBytes ((byte *) &ret, value, startIndex); return ret; } } } Index: ASN1.cs =================================================================== RCS file: /cvsroot/pgsqlclient/pgsqlclient_10/Mono.Security/Mono.Security/Mono.Security/ASN1.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** ASN1.cs 10 Feb 2004 09:42:35 -0000 1.1 --- ASN1.cs 9 May 2004 12:04:00 -0000 1.2 *************** *** 2,258 **** // ASN1.cs: Abstract Syntax Notation 1 - micro-parser and generator // ! // Author: ! // Sebastien Pouliot (spo...@mo...) // // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com) // using System; using System.Collections; namespace Mono.Security { ! // References: ! // a. ITU ASN.1 standards (free download) ! // http://www.itu.int/ITU-T/studygroups/com17/languages/ ! internal class ASN1 { ! protected byte m_nTag; ! protected byte[] m_aValue; ! protected ArrayList elist; ! public ASN1 () : this (0x00, null) {} ! public ASN1 (byte tag) : this (tag, null) {} ! public ASN1 (byte tag, byte[] data) ! { ! m_nTag = tag; ! m_aValue = data; ! } ! public ASN1 (byte[] data) ! { ! m_nTag = data [0]; ! int nLenLength = 0; ! int nLength = data [1]; ! if (nLength > 0x80) { ! // composed length ! nLenLength = nLength - 0x80; ! nLength = 0; ! for (int i = 0; i < nLenLength; i++) { ! nLength *= 256; ! nLength += data [i + 2]; } - } ! m_aValue = new byte [nLength]; ! Array.Copy (data, (2 + nLenLength), m_aValue, 0, nLength); ! if ((m_nTag & 0x20) == 0x20) { ! int nStart = (2 + nLenLength); ! Decode (data, ref nStart, data.Length); } - } ! public int Count { ! get { ! if (elist == null) ! return 0; ! return elist.Count; } - } - - public byte Tag { - get { return m_nTag; } - } ! public int Length { ! get { ! if (m_aValue != null) ! return m_aValue.Length; ! else ! return 0; } - } ! public byte[] Value { ! get { ! if (m_aValue == null) ! GetBytes (); ! return (byte[]) m_aValue.Clone (); ! } ! set { ! if (value != null) ! m_aValue = (byte[]) value.Clone (); } - } ! private bool CompareArray (byte[] array1, byte[] array2) ! { ! bool bResult = (array1.Length == array2.Length); ! if (bResult) { ! for (int i = 0; i < array1.Length; i++) { ! if (array1[i] != array2[i]) ! return false; } } - return bResult; - } ! public bool Equals (byte[] asn1) ! { ! return CompareArray (this.GetBytes (), asn1); ! } ! public bool CompareValue (byte[] aValue) ! { ! return CompareArray (m_aValue, aValue); ! } ! public virtual ASN1 Add (ASN1 asn1) ! { ! if (asn1 != null) { ! if (elist == null) ! elist = new ArrayList (); ! elist.Add (asn1); } - return asn1; - } ! public virtual byte[] GetBytes () ! { ! byte[] val = null; ! if (m_aValue != null) { ! val = m_aValue; } ! else if (Count > 0) { ! int esize = 0; ! ArrayList al = new ArrayList (); ! foreach (ASN1 a in elist) { ! byte[] item = a.GetBytes (); ! al.Add (item); ! esize += item.Length; } ! val = new byte [esize]; ! int pos = 0; ! for (int i=0; i < elist.Count; i++) { ! byte[] item = (byte[]) al[i]; ! Array.Copy (item, 0, val, pos, item.Length); ! pos += item.Length; } - } ! byte[] der; ! int nLengthLen = 0; ! if (val != null) { ! int nLength = val.Length; ! // special for length > 127 ! if (nLength > 127) { ! if (nLength < 256) { ! der = new byte [3 + nLength]; ! Array.Copy (val, 0, der, 3, nLength); ! nLengthLen += 0x81; ! der[2] = (byte)(nLength); } else { ! der = new byte [4 + nLength]; ! Array.Copy (val, 0, der, 4, nLength); ! nLengthLen += 0x82; ! der[2] = (byte)(nLength / 256); ! der[3] = (byte)(nLength % 256); } } ! else { ! der = new byte [2 + nLength]; ! Array.Copy (val, 0, der, 2, nLength); ! nLengthLen = nLength; ! } ! if (m_aValue == null) ! m_aValue = val; ! } ! else ! der = new byte[2]; ! der[0] = m_nTag; ! der[1] = (byte)nLengthLen; ! return der; ! } ! // Note: Recursive ! protected void Decode (byte[] asn1, ref int anPos, int anLength) ! { ! byte nTag; ! int nLength; ! byte[] aValue; ! // minimum is 2 bytes (tag + length of 0) ! while (anPos < anLength - 1) { ! int nPosOri = anPos; ! DecodeTLV (asn1, ref anPos, out nTag, out nLength, out aValue); ! ASN1 elm = Add (new ASN1 (nTag, aValue)); ! if ((nTag & 0x20) == 0x20) { ! int nConstructedPos = anPos; ! elm.Decode (asn1, ref nConstructedPos, nConstructedPos + nLength); } - anPos += nLength; // value length } - } ! // TLV : Tag - Length - Value ! protected void DecodeTLV (byte[] asn1, ref int anPos, out byte anTag, out int anLength, out byte[] aValue) ! { ! anTag = asn1 [anPos++]; ! anLength = asn1 [anPos++]; ! // special case where L contains the Length of the Length + 0x80 ! if ((anLength & 0x80) == 0x80) { ! int nLengthLen = anLength & 0x7F; ! anLength = 0; ! for (int i = 0; i < nLengthLen; i++) ! anLength = anLength * 256 + asn1 [anPos++]; } ! aValue = new byte [anLength]; ! Array.Copy (asn1, anPos, aValue, 0, anLength); ! } ! public ASN1 this [int index] { ! get { try { ! if (index >= elist.Count) return null; - return (ASN1) elist [index]; } ! catch { return null; } } - } ! public ASN1 Element (int index, byte anTag) ! { ! try { ! if (index >= elist.Count) ! return null; ! ASN1 elm = (ASN1) elist [index]; ! if (elm.Tag == anTag) ! return elm; ! else ! return null; } ! catch { ! return null; } } } - - } --- 2,307 ---- // ASN1.cs: Abstract Syntax Notation 1 - micro-parser and generator // ! // Authors: ! // Sebastien Pouliot <seb...@xi...> ! // Jesper Pedersen <je...@it...> // // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com) + // (C) 2004 Novell (http://www.novell.com) + // (C) 2004 IT+ A/S (http://www.itplus.dk) // using System; using System.Collections; + using System.IO; + using System.Text; namespace Mono.Security { ! // References: ! // a. ITU ASN.1 standards (free download) ! // http://www.itu.int/ITU-T/studygroups/com17/languages/ ! #if INSIDE_CORLIB ! internal ! #else ! public ! #endif ! class ASN1 { ! private byte m_nTag; ! private byte[] m_aValue; ! private ArrayList elist; ! public ASN1 () : this (0x00, null) {} ! public ASN1 (byte tag) : this (tag, null) {} ! public ASN1 (byte tag, byte[] data) ! { ! m_nTag = tag; ! m_aValue = data; ! } ! public ASN1 (byte[] data) ! { ! m_nTag = data [0]; ! int nLenLength = 0; ! int nLength = data [1]; ! if (nLength > 0x80) { ! // composed length ! nLenLength = nLength - 0x80; ! nLength = 0; ! for (int i = 0; i < nLenLength; i++) { ! nLength *= 256; ! nLength += data [i + 2]; ! } } ! m_aValue = new byte [nLength]; ! Buffer.BlockCopy (data, (2 + nLenLength), m_aValue, 0, nLength); ! if ((m_nTag & 0x20) == 0x20) { ! int nStart = (2 + nLenLength); ! Decode (data, ref nStart, data.Length); ! } } ! public int Count { ! get { ! if (elist == null) ! return 0; ! return elist.Count; ! } } ! public byte Tag { ! get { return m_nTag; } } ! public int Length { ! get { ! if (m_aValue != null) ! return m_aValue.Length; ! else ! return 0; ! } } ! public byte[] Value { ! get { ! if (m_aValue == null) ! GetBytes (); ! return (byte[]) m_aValue.Clone (); ! } ! set { ! if (value != null) ! m_aValue = (byte[]) value.Clone (); } } ! private bool CompareArray (byte[] array1, byte[] array2) ! { ! bool bResult = (array1.Length == array2.Length); ! if (bResult) { ! for (int i = 0; i < array1.Length; i++) { ! if (array1[i] != array2[i]) ! return false; ! } ! } ! return bResult; ! } ! public bool Equals (byte[] asn1) ! { ! return CompareArray (this.GetBytes (), asn1); ! } ! public bool CompareValue (byte[] value) ! { ! return CompareArray (m_aValue, value); } ! public ASN1 Add (ASN1 asn1) ! { ! if (asn1 != null) { ! if (elist == null) ! elist = new ArrayList (); ! elist.Add (asn1); ! } ! return asn1; } ! ! public virtual byte[] GetBytes () ! { ! byte[] val = null; ! if (m_aValue != null) { ! val = m_aValue; } ! else if (Count > 0) { ! int esize = 0; ! ArrayList al = new ArrayList (); ! foreach (ASN1 a in elist) { ! byte[] item = a.GetBytes (); ! al.Add (item); ! esize += item.Length; ! } ! val = new byte [esize]; ! int pos = 0; ! for (int i=0; i < elist.Count; i++) { ! byte[] item = (byte[]) al[i]; ! Buffer.BlockCopy (item, 0, val, pos, item.Length); ! pos += item.Length; ! } } ! byte[] der; ! int nLengthLen = 0; ! if (val != null) { ! int nLength = val.Length; ! // special for length > 127 ! if (nLength > 127) { ! if (nLength < 256) { ! der = new byte [3 + nLength]; ! Buffer.BlockCopy (val, 0, der, 3, nLength); ! nLengthLen += 0x81; ! der[2] = (byte)(nLength); ! } ! else { ! der = new byte [4 + nLength]; ! Buffer.BlockCopy (val, 0, der, 4, nLength); ! nLengthLen += 0x82; ! der[2] = (byte)(nLength / 256); ! der[3] = (byte)(nLength % 256); ! } } else { ! der = new byte [2 + nLength]; ! Buffer.BlockCopy (val, 0, der, 2, nLength); ! nLengthLen = nLength; } + if (m_aValue == null) + m_aValue = val; } ! else ! der = new byte[2]; ! der[0] = m_nTag; ! der[1] = (byte)nLengthLen; ! return der; ! } ! // Note: Recursive ! protected void Decode (byte[] asn1, ref int anPos, int anLength) ! { ! byte nTag; ! int nLength; ! byte[] aValue; ! // minimum is 2 bytes (tag + length of 0) ! while (anPos < anLength - 1) { ! int nPosOri = anPos; ! DecodeTLV (asn1, ref anPos, out nTag, out nLength, out aValue); ! ASN1 elm = Add (new ASN1 (nTag, aValue)); ! if ((nTag & 0x20) == 0x20) { ! int nConstructedPos = anPos; ! elm.Decode (asn1, ref nConstructedPos, nConstructedPos + nLength); ! } ! anPos += nLength; // value length } } ! // TLV : Tag - Length - Value ! protected void DecodeTLV (byte[] asn1, ref int pos, out byte tag, out int length, out byte[] content) ! { ! tag = asn1 [pos++]; ! length = asn1 [pos++]; ! // special case where L contains the Length of the Length + 0x80 ! if ((length & 0x80) == 0x80) { ! int nLengthLen = length & 0x7F; ! length = 0; ! for (int i = 0; i < nLengthLen; i++) ! length = length * 256 + asn1 [pos++]; ! } ! ! content = new byte [length]; ! Buffer.BlockCopy (asn1, pos, content, 0, length); } ! public ASN1 this [int index] { ! get { ! try { ! if ((elist == null) || (index >= elist.Count)) ! return null; ! return (ASN1) elist [index]; ! } ! catch (ArgumentOutOfRangeException) { ! return null; ! } ! } ! } ! public ASN1 Element (int index, byte anTag) ! { try { ! if ((elist == null) || (index >= elist.Count)) ! return null; ! ! ASN1 elm = (ASN1) elist [index]; ! if (elm.Tag == anTag) ! return elm; ! else return null; } ! catch (ArgumentOutOfRangeException) { return null; } } ! public override string ToString() ! { ! string lineSeperator = Environment.NewLine; ! StringBuilder hexLine = new StringBuilder (); ! ! // Add tag ! hexLine.Append ("Tag: "); ! hexLine.Append (System.Convert.ToString (Tag, 16)); ! hexLine.Append (lineSeperator); ! ! // Add value ! hexLine.Append ("Value: "); ! hexLine.Append (lineSeperator); ! for (int i = 0; i < Value.Length; i++) { ! if (Value[i] < 16) { ! hexLine.Append ("0"); ! } ! hexLine.Append (System.Convert.ToString (Value [i], 16)); ! hexLine.Append (" "); ! if ((i+1) % 16 == 0) { ! hexLine.Append (lineSeperator); ! } ! } ! return hexLine.ToString (); } ! ! public void SaveToFile (string filename) ! { ! if (filename == null) ! throw new ArgumentNullException ("filename"); ! ! using (FileStream fs = File.OpenWrite (filename)) { ! byte[] data = GetBytes (); ! fs.Write (data, 0, data.Length); ! fs.Flush (); ! fs.Close (); ! } } } } |