[pgsqlclient-checkins] pgsqlclient_10/Mono.Security/Mono.Security/Mono.Security ASN1.cs,1.2,1.3 ASN1
Status: Inactive
Brought to you by:
carlosga_fb
From: Carlos G. Á. <car...@us...> - 2004-07-20 17:45:03
|
Update of /cvsroot/pgsqlclient/pgsqlclient_10/Mono.Security/Mono.Security/Mono.Security In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv18295 Modified Files: ASN1.cs ASN1Convert.cs BitConverterLE.cs PKCS7.cs Log Message: Prepare for beta 7 Index: PKCS7.cs =================================================================== RCS file: /cvsroot/pgsqlclient/pgsqlclient_10/Mono.Security/Mono.Security/Mono.Security/PKCS7.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** PKCS7.cs 9 May 2004 12:04:00 -0000 1.2 --- PKCS7.cs 20 Jul 2004 17:44:53 -0000 1.3 *************** *** 10,13 **** --- 10,34 ---- // + // + // 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.Collections; *************** *** 528,532 **** private ArrayList crls; private SignerInfo signerInfo; ! private ASN1 mda; public SignedData () --- 549,553 ---- private ArrayList crls; private SignerInfo signerInfo; ! private bool mda; public SignedData () *************** *** 537,540 **** --- 558,562 ---- crls = new ArrayList (); signerInfo = new SignerInfo (); + mda = true; } *************** *** 553,558 **** version = asn1[0][0].Value[0]; - // digestInfo - contentInfo = new ContentInfo (asn1[0][2]); --- 575,578 ---- *************** *** 576,579 **** --- 596,607 ---- else signerInfo = new SignerInfo (); + + // Exchange hash algorithm Oid from SignerInfo + if (signerInfo.HashName != null) { + HashName = OidToName(signerInfo.HashName); + } + + // Check if SignerInfo has authenticated attributes + mda = (signerInfo.AuthenticatedAttributes.Count > 0); } *************** *** 612,615 **** --- 640,699 ---- } + public bool UseAuthenticatedAttributes { + get { return mda; } + set { mda = value; } + } + + public bool VerifySignature (AsymmetricAlgorithm aa) + { + if (aa == null) { + return false; + } + + RSAPKCS1SignatureDeformatter r = new RSAPKCS1SignatureDeformatter (aa); + r.SetHashAlgorithm (hashAlgorithm); + HashAlgorithm ha = HashAlgorithm.Create (hashAlgorithm); + + byte[] signature = signerInfo.Signature; + byte[] hash = null; + + if (mda) { + ASN1 asn = new ASN1 (0x31); + foreach (ASN1 attr in signerInfo.AuthenticatedAttributes) + asn.Add (attr); + + hash = ha.ComputeHash (asn.GetBytes ()); + } else { + hash = ha.ComputeHash (contentInfo.Content[0].Value); + } + + if (hash != null && signature != null) { + return r.VerifySignature (hash, signature); + } + return false; + } + + internal string OidToName (string oid) + { + switch (oid) { + case "1.3.14.3.2.26" : + return "SHA1"; + case "1.2.840.113549.2.2" : + return "MD2"; + case "1.2.840.113549.2.5" : + return "MD5"; + case "2.16.840.1.101.3.4.1" : + return "SHA256"; + case "2.16.840.1.101.3.4.2" : + return "SHA384"; + case "2.16.840.1.101.3.4.3" : + return "SHA512"; + default : + break; + } + // Unknown Oid + return oid; + } + internal ASN1 GetASN1 () { *************** *** 629,639 **** ASN1 ci = contentInfo.ASN1; signedData.Add (ci); ! if ((mda == null) && (hashAlgorithm != null)) { ! // automatically add the messageDigest authenticated attribute ! HashAlgorithm ha = HashAlgorithm.Create (hashAlgorithm); ! 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); } --- 713,738 ---- ASN1 ci = contentInfo.ASN1; signedData.Add (ci); ! if (hashAlgorithm != null) { ! if (mda) { ! // Use authenticated attributes for signature ! ! // Automatically add the contentType authenticated attribute ! ASN1 ctattr = Attribute (Oid.contentType, ci[0]); ! signerInfo.AuthenticatedAttributes.Add (ctattr); ! ! // Automatically add the messageDigest authenticated attribute ! HashAlgorithm ha = HashAlgorithm.Create (hashAlgorithm); ! byte[] idcHash = ha.ComputeHash (ci[1][0].Value); ! ASN1 md = new ASN1 (0x30); ! ASN1 mdattr = Attribute (Oid.messageDigest, md.Add (new ASN1 (0x04, idcHash))); ! signerInfo.AuthenticatedAttributes.Add (mdattr); ! } else { ! // Don't use authenticated attributes for signature -- signature is content ! RSAPKCS1SignatureFormatter r = new RSAPKCS1SignatureFormatter (signerInfo.Key); ! r.SetHashAlgorithm (hashAlgorithm); ! HashAlgorithm ha = HashAlgorithm.Create (hashAlgorithm); ! byte[] sig = ha.ComputeHash (ci[1][0].Value); ! signerInfo.Signature = r.CreateSignature (sig); ! } } *************** *** 799,802 **** --- 898,907 ---- return (byte[]) signature.Clone (); } + + set { + if (value != null) { + signature = (byte[]) value.Clone (); + } + } } *************** *** 824,829 **** signerInfo.Add (AlgorithmIdentifier (hashOid)); // authenticatedAttributes [0] IMPLICIT Attributes OPTIONAL, ! ASN1 aa = signerInfo.Add (new ASN1 (0xA0)); if (authenticatedAttributes.Count > 0) { foreach (ASN1 attr in authenticatedAttributes) aa.Add (attr); --- 929,935 ---- signerInfo.Add (AlgorithmIdentifier (hashOid)); // authenticatedAttributes [0] IMPLICIT Attributes OPTIONAL, ! ASN1 aa = null; if (authenticatedAttributes.Count > 0) { + aa = signerInfo.Add (new ASN1 (0xA0)); foreach (ASN1 attr in authenticatedAttributes) aa.Add (attr); *************** *** 833,843 **** signerInfo.Add (AlgorithmIdentifier (PKCS7.Oid.rsaEncryption)); ! RSAPKCS1SignatureFormatter r = new RSAPKCS1SignatureFormatter (key); ! r.SetHashAlgorithm (hashAlgorithm); ! byte[] tbs = aa.GetBytes (); ! tbs [0] = 0x31; // not 0xA0 for signature ! HashAlgorithm ha = HashAlgorithm.Create (hashAlgorithm); ! byte[] tbsHash = ha.ComputeHash (tbs); ! signature = r.CreateSignature (tbsHash); } else if (key is DSA) { --- 939,952 ---- signerInfo.Add (AlgorithmIdentifier (PKCS7.Oid.rsaEncryption)); ! if (aa != null) { ! // Calculate the signature here; otherwise it must be set from SignedData ! RSAPKCS1SignatureFormatter r = new RSAPKCS1SignatureFormatter (key); ! r.SetHashAlgorithm (hashAlgorithm); ! byte[] tbs = aa.GetBytes (); ! tbs [0] = 0x31; // not 0xA0 for signature ! HashAlgorithm ha = HashAlgorithm.Create (hashAlgorithm); ! byte[] tbsHash = ha.ComputeHash (tbs); ! signature = r.CreateSignature (tbsHash); ! } } else if (key is DSA) { Index: ASN1Convert.cs =================================================================== RCS file: /cvsroot/pgsqlclient/pgsqlclient_10/Mono.Security/Mono.Security/Mono.Security/ASN1Convert.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** ASN1Convert.cs 9 May 2004 12:04:00 -0000 1.2 --- ASN1Convert.cs 20 Jul 2004 17:44:53 -0000 1.3 *************** *** 11,14 **** --- 11,35 ---- // + // + // 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.Collections; *************** *** 43,52 **** // 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")); } } --- 64,75 ---- // UTCTIME return new ASN1 (0x17, Encoding.ASCII.GetBytes ( ! dt.ToUniversalTime ().ToString ("yyMMddHHmmss", ! CultureInfo.InvariantCulture) + "Z")); } else { // GENERALIZEDTIME return new ASN1 (0x18, Encoding.ASCII.GetBytes ( ! dt.ToUniversalTime ().ToString ("yyyyMMddHHmmss", ! CultureInfo.InvariantCulture) + "Z")); } } *************** *** 55,71 **** { 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; } --- 78,99 ---- { byte[] integer = BitConverterLE.GetBytes (value); ! Array.Reverse (integer); ! int x = 0; ! while ((x < integer.Length) && (integer [x] == 0x00)) ! x++; ASN1 asn1 = new ASN1 (0x02); ! switch (x) { ! case 0: ! asn1.Value = integer; ! break; ! case 4: ! asn1.Value = new byte [0]; ! break; ! default: ! byte[] smallerInt = new byte [4 - x]; ! Buffer.BlockCopy (integer, x, smallerInt, 0, smallerInt.Length); ! asn1.Value = smallerInt; ! break; } return asn1; } *************** *** 73,76 **** --- 101,107 ---- static public ASN1 FromOid (string oid) { + if (oid == null) + throw new ArgumentNullException ("oid"); + return new ASN1 (CryptoConfig.EncodeOID (oid)); } *************** *** 78,87 **** 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; } --- 109,121 ---- static public ASN1 FromUnsignedBigInteger (byte[] big) { ! if (big == null) ! throw new ArgumentNullException ("big"); ! ! if (big [0] != 0x00) { ! // this first byte is added so we're sure this is 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 - 1); big = uinteger; } *************** *** 91,96 **** static public int ToInt32 (ASN1 asn1) { if (asn1.Tag != 0x02) ! throw new NotSupportedException ("Only integer can be converted"); int x = 0; for (int i=0; i < asn1.Value.Length; i++) --- 125,133 ---- static public int ToInt32 (ASN1 asn1) { + if (asn1 == null) + throw new ArgumentNullException ("asn1"); if (asn1.Tag != 0x02) ! throw new FormatException ("Only integer can be converted"); ! int x = 0; for (int i=0; i < asn1.Value.Length; i++) *************** *** 103,106 **** --- 140,146 ---- static public string ToOid (ASN1 asn1) { + if (asn1 == null) + throw new ArgumentNullException ("asn1"); + byte[] aOID = asn1.Value; StringBuilder sb = new StringBuilder (); *************** *** 130,133 **** --- 170,176 ---- static public DateTime ToDateTime (ASN1 time) { + if (time == null) + throw new ArgumentNullException ("time"); + string t = Encoding.ASCII.GetString (time.Value); // to support both UTCTime and GeneralizedTime (and not so common format) Index: BitConverterLE.cs =================================================================== RCS file: /cvsroot/pgsqlclient/pgsqlclient_10/Mono.Security/Mono.Security/Mono.Security/BitConverterLE.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** BitConverterLE.cs 9 May 2004 12:04:00 -0000 1.1 --- BitConverterLE.cs 20 Jul 2004 17:44:53 -0000 1.2 *************** *** 7,10 **** --- 7,31 ---- // + // + // 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; Index: ASN1.cs =================================================================== RCS file: /cvsroot/pgsqlclient/pgsqlclient_10/Mono.Security/Mono.Security/Mono.Security/ASN1.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** ASN1.cs 9 May 2004 12:04:00 -0000 1.2 --- ASN1.cs 20 Jul 2004 17:44:53 -0000 1.3 *************** *** 11,14 **** --- 11,35 ---- // + // + // 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.Collections; |