summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-06-29 14:07:20 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-06-29 14:07:20 +0700
commitfe7d3e0009ae2ba833be66729221f42ac3aa0464 (patch)
tree7008925a5fb5a59a32fcd40ab0638a8e6888de08
parentRefactor Asn1Dump (diff)
downloadBouncyCastle.NET-ed25519-fe7d3e0009ae2ba833be66729221f42ac3aa0464.tar.xz
Refactoring around Platform
-rw-r--r--crypto/src/asn1/x500/style/IetfUtilities.cs6
-rw-r--r--crypto/src/asn1/x509/TBSCertList.cs4
-rw-r--r--crypto/src/cms/CMSEnvelopedDataStreamGenerator.cs45
-rw-r--r--crypto/src/cms/DefaultSignedAttributeTableGenerator.cs2
-rw-r--r--crypto/src/crypto/BufferedIesCipher.cs2
-rw-r--r--crypto/src/math/ec/ECFieldElement.cs2
-rw-r--r--crypto/src/openpgp/PgpPublicKeyRingBundle.cs33
-rw-r--r--crypto/src/openpgp/PgpSecretKeyRingBundle.cs31
-rw-r--r--crypto/src/openssl/MiscPemGenerator.cs2
-rw-r--r--crypto/src/openssl/Pkcs8Generator.cs6
-rw-r--r--crypto/src/pkcs/Pkcs10CertificationRequest.cs2
-rw-r--r--crypto/src/pkcs/PrivateKeyInfoFactory.cs4
-rw-r--r--crypto/src/pkix/PkixCertPathBuilderResult.cs12
-rw-r--r--crypto/src/pkix/PkixCertPathValidatorResult.cs13
-rw-r--r--crypto/src/pkix/PkixCertPathValidatorUtilities.cs2
-rw-r--r--crypto/src/pkix/PkixNameConstraintValidator.cs15
-rw-r--r--crypto/src/tls/CertificateStatus.cs4
-rw-r--r--crypto/src/tls/ProtocolVersion.cs2
-rw-r--r--crypto/src/tls/SignatureScheme.cs2
19 files changed, 65 insertions, 124 deletions
diff --git a/crypto/src/asn1/x500/style/IetfUtilities.cs b/crypto/src/asn1/x500/style/IetfUtilities.cs
index e269f418d..72ed08dd3 100644
--- a/crypto/src/asn1/x500/style/IetfUtilities.cs
+++ b/crypto/src/asn1/x500/style/IetfUtilities.cs
@@ -93,15 +93,15 @@ namespace Org.BouncyCastle.Asn1.X500.Style
 
         public static string Canonicalize(string s)
         {
-            string value = Platform.ToLowerInvariant(s);
+            string value = s.ToLowerInvariant();
 
             if (value.Length > 0 && value[0] == '#')
             {
                 Asn1Object obj = DecodeObject(value);
 
-                if (obj is IAsn1String)
+                if (obj is IAsn1String str)
                 {
-                    value = Platform.ToLowerInvariant(((IAsn1String)obj).GetString());
+                    value = str.GetString().ToLowerInvariant();
                 }
             }
 
diff --git a/crypto/src/asn1/x509/TBSCertList.cs b/crypto/src/asn1/x509/TBSCertList.cs
index aef41d440..ab847d563 100644
--- a/crypto/src/asn1/x509/TBSCertList.cs
+++ b/crypto/src/asn1/x509/TBSCertList.cs
@@ -263,11 +263,7 @@ namespace Org.BouncyCastle.Asn1.X509
 		public IEnumerable<CrlEntry> GetRevokedCertificateEnumeration()
 		{
 			if (revokedCertificates == null)
-			{
-				// TODO
-				//return EmptyEnumerable.Instance;
 				return new List<CrlEntry>(0);
-			}
 
 			return new RevokedCertificatesEnumeration(revokedCertificates);
 		}
diff --git a/crypto/src/cms/CMSEnvelopedDataStreamGenerator.cs b/crypto/src/cms/CMSEnvelopedDataStreamGenerator.cs
index 261f67a15..90ecf0748 100644
--- a/crypto/src/cms/CMSEnvelopedDataStreamGenerator.cs
+++ b/crypto/src/cms/CMSEnvelopedDataStreamGenerator.cs
@@ -250,51 +250,40 @@ namespace Org.BouncyCastle.Cms
             protected override void Dispose(bool disposing)
             {
                 if (disposing)
-                {
-                    Platform.Dispose(_out);
-
-                    // TODO Parent context(s) should really be closed explicitly
-
-				    _eiGen.Close();
-
-                    if (_outer.unprotectedAttributeGenerator != null)
-                    {
-                        Asn1.Cms.AttributeTable attrTable = _outer.unprotectedAttributeGenerator.GetAttributes(Platform.CreateHashtable());
-
-                        Asn1Set unprotectedAttrs = new BerSet(attrTable.ToAsn1EncodableVector());
-
-                        _envGen.AddObject(new DerTaggedObject(false, 1, unprotectedAttrs));
-                    }
-
-				    _envGen.Close();
-				    _cGen.Close();
+ 				{
+					ImplClose();
                 }
                 base.Dispose(disposing);
             }
 #else
 			public override void Close()
 			{
-                Platform.Dispose(_out);
+				ImplClose();
+				base.Close();
+			}
+#endif
+
+			private void ImplClose()
+            {
+				Platform.Dispose(_out);
 
-                // TODO Parent context(s) should really be closed explicitly
+				// TODO Parent context(s) should really be closed explicitly
 
-                _eiGen.Close();
+				_eiGen.Close();
 
-                if (_outer.unprotectedAttributeGenerator != null)
-                {
+				if (_outer.unprotectedAttributeGenerator != null)
+				{
 					Asn1.Cms.AttributeTable attrTable = _outer.unprotectedAttributeGenerator.GetAttributes(
 						new Dictionary<CmsAttributeTableParameter, object>());
 
-                    Asn1Set unprotectedAttrs = new BerSet(attrTable.ToAsn1EncodableVector());
+					Asn1Set unprotectedAttrs = new BerSet(attrTable.ToAsn1EncodableVector());
 
-                    _envGen.AddObject(new DerTaggedObject(false, 1, unprotectedAttrs));
-                }
+					_envGen.AddObject(new DerTaggedObject(false, 1, unprotectedAttrs));
+				}
 
 				_envGen.Close();
 				_cGen.Close();
-				base.Close();
 			}
-#endif
 		}
 	}
 }
diff --git a/crypto/src/cms/DefaultSignedAttributeTableGenerator.cs b/crypto/src/cms/DefaultSignedAttributeTableGenerator.cs
index e1bec484a..d8b668c4e 100644
--- a/crypto/src/cms/DefaultSignedAttributeTableGenerator.cs
+++ b/crypto/src/cms/DefaultSignedAttributeTableGenerator.cs
@@ -47,7 +47,7 @@ namespace Org.BouncyCastle.Cms
 		 *
 		 * @param parameters source parameters for table generation.
 		 *
-		 * @return a filled in Hashtable of attributes.
+		 * @return a filled in Dictionary of attributes.
 		 */
 		protected virtual IDictionary<DerObjectIdentifier, object> CreateStandardAttributeTable(
 			IDictionary<CmsAttributeTableParameter, object> parameters)
diff --git a/crypto/src/crypto/BufferedIesCipher.cs b/crypto/src/crypto/BufferedIesCipher.cs
index 6dab4ae33..293c15c92 100644
--- a/crypto/src/crypto/BufferedIesCipher.cs
+++ b/crypto/src/crypto/BufferedIesCipher.cs
@@ -36,7 +36,7 @@ namespace Org.BouncyCastle.Crypto
 			this.forEncryption = forEncryption;
 
 			// TODO
-			throw Platform.CreateNotImplementedException("IES");
+			throw new NotImplementedException("IES");
 		}
 
 		public override int GetBlockSize()
diff --git a/crypto/src/math/ec/ECFieldElement.cs b/crypto/src/math/ec/ECFieldElement.cs
index 774dfb9f1..a96556482 100644
--- a/crypto/src/math/ec/ECFieldElement.cs
+++ b/crypto/src/math/ec/ECFieldElement.cs
@@ -262,7 +262,7 @@ namespace Org.BouncyCastle.Math.EC
                 return this;
 
             if (!q.TestBit(0))
-                throw Platform.CreateNotImplementedException("even value of q");
+                throw new NotImplementedException("even value of q");
 
             if (q.TestBit(1)) // q == 4m + 3
             {
diff --git a/crypto/src/openpgp/PgpPublicKeyRingBundle.cs b/crypto/src/openpgp/PgpPublicKeyRingBundle.cs
index 0de7e335e..473d0ae5b 100644
--- a/crypto/src/openpgp/PgpPublicKeyRingBundle.cs
+++ b/crypto/src/openpgp/PgpPublicKeyRingBundle.cs
@@ -1,5 +1,6 @@
 using System;
 using System.Collections.Generic;
+using System.Globalization;
 using System.IO;
 
 using Org.BouncyCastle.Utilities;
@@ -86,47 +87,31 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp
         }
 
 		/// <summary>Allow enumeration of the key rings associated with the passed in userId.</summary>
-		/// <param name="userId">The user ID to be matched.</param>
+		/// <param name="userID">The user ID to be matched.</param>
 		/// <param name="matchPartial">If true, userId need only be a substring of an actual ID string to match.</param>
 		/// <param name="ignoreCase">If true, case is ignored in user ID comparisons.</param>
 		/// <returns>An <c>IEnumerable</c> of key rings which matched (possibly none).</returns>
-		public IEnumerable<PgpPublicKeyRing> GetKeyRings(string userId, bool matchPartial, bool ignoreCase)
+		public IEnumerable<PgpPublicKeyRing> GetKeyRings(string userID, bool matchPartial, bool ignoreCase)
 		{
-			var rings = new List<PgpPublicKeyRing>();
-
-			if (ignoreCase)
-			{
-                userId = Platform.ToUpperInvariant(userId);
-			}
+			var compareInfo = CultureInfo.InvariantCulture.CompareInfo;
+			var compareOptions = ignoreCase ? CompareOptions.OrdinalIgnoreCase : CompareOptions.Ordinal;
 
 			foreach (PgpPublicKeyRing pubRing in GetKeyRings())
 			{
 				foreach (string nextUserID in pubRing.GetPublicKey().GetUserIds())
 				{
-					string next = nextUserID;
-					if (ignoreCase)
-					{
-                        next = Platform.ToUpperInvariant(next);
-                    }
-
 					if (matchPartial)
 					{
-                        if (Platform.IndexOf(next, userId) > -1)
-						{
-							rings.Add(pubRing);
-						}
+						if (compareInfo.IndexOf(nextUserID, userID, compareOptions) >= 0)
+							yield return pubRing;
 					}
 					else
 					{
-						if (next.Equals(userId))
-						{
-							rings.Add(pubRing);
-						}
+						if (compareInfo.Compare(nextUserID, userID, compareOptions) == 0)
+							yield return pubRing;
 					}
 				}
 			}
-
-			return CollectionUtilities.Proxy(rings);
 		}
 
 		/// <summary>Return the PGP public key associated with the given key id.</summary>
diff --git a/crypto/src/openpgp/PgpSecretKeyRingBundle.cs b/crypto/src/openpgp/PgpSecretKeyRingBundle.cs
index 994f4f718..af182de9c 100644
--- a/crypto/src/openpgp/PgpSecretKeyRingBundle.cs
+++ b/crypto/src/openpgp/PgpSecretKeyRingBundle.cs
@@ -1,5 +1,6 @@
 using System;
 using System.Collections.Generic;
+using System.Globalization;
 using System.IO;
 
 using Org.BouncyCastle.Utilities;
@@ -90,43 +91,27 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp
 		/// <param name="matchPartial">If true, userId need only be a substring of an actual ID string to match.</param>
 		/// <param name="ignoreCase">If true, case is ignored in user ID comparisons.</param>
 		/// <returns>An <c>IEnumerable</c> of key rings which matched (possibly none).</returns>
-		public IEnumerable<PgpSecretKeyRing> GetKeyRings(string userId, bool matchPartial, bool ignoreCase)
+		public IEnumerable<PgpSecretKeyRing> GetKeyRings(string userID, bool matchPartial, bool ignoreCase)
 		{
-			var rings = new List<PgpSecretKeyRing>();
-
-			if (ignoreCase)
-			{
-                userId = Platform.ToUpperInvariant(userId);
-            }
+			var compareInfo = CultureInfo.InvariantCulture.CompareInfo;
+			var compareOptions = ignoreCase ? CompareOptions.OrdinalIgnoreCase : CompareOptions.Ordinal;
 
 			foreach (PgpSecretKeyRing secRing in GetKeyRings())
 			{
 				foreach (string nextUserID in secRing.GetSecretKey().UserIds)
 				{
-					string next = nextUserID;
-					if (ignoreCase)
-					{
-                        next = Platform.ToUpperInvariant(next);
-                    }
-
 					if (matchPartial)
 					{
-                        if (Platform.IndexOf(next, userId) > -1)
-						{
-							rings.Add(secRing);
-						}
+						if (compareInfo.IndexOf(nextUserID, userID, compareOptions) >= 0)
+							yield return secRing;
 					}
 					else
 					{
-						if (next.Equals(userId))
-						{
-							rings.Add(secRing);
-						}
+						if (compareInfo.Compare(nextUserID, userID, compareOptions) == 0)
+							yield return secRing;
 					}
 				}
 			}
-
-			return CollectionUtilities.Proxy(rings);
 		}
 
 		/// <summary>Return the PGP secret key associated with the given key id.</summary>
diff --git a/crypto/src/openssl/MiscPemGenerator.cs b/crypto/src/openssl/MiscPemGenerator.cs
index 294cf0afb..ada0b84ed 100644
--- a/crypto/src/openssl/MiscPemGenerator.cs
+++ b/crypto/src/openssl/MiscPemGenerator.cs
@@ -180,7 +180,7 @@ namespace Org.BouncyCastle.OpenSsl
             }
 
 
-            string dekAlgName = Platform.ToUpperInvariant(algorithm);
+            string dekAlgName = algorithm.ToUpperInvariant();
 
             // Note: For backward compatibility
             if (dekAlgName == "DESEDE")
diff --git a/crypto/src/openssl/Pkcs8Generator.cs b/crypto/src/openssl/Pkcs8Generator.cs
index d03ea08d2..0674cce15 100644
--- a/crypto/src/openssl/Pkcs8Generator.cs
+++ b/crypto/src/openssl/Pkcs8Generator.cs
@@ -1,13 +1,7 @@
 using System;
-using System.Collections;
-using System.IO;
 
-using Org.BouncyCastle.Asn1;
-using Org.BouncyCastle.Asn1.Nist;
 using Org.BouncyCastle.Asn1.Pkcs;
-using Org.BouncyCastle.Asn1.X509;
 using Org.BouncyCastle.Crypto;
-using Org.BouncyCastle.Crypto.Parameters;
 using Org.BouncyCastle.Pkcs;
 using Org.BouncyCastle.Security;
 using Org.BouncyCastle.Utilities.IO.Pem;
diff --git a/crypto/src/pkcs/Pkcs10CertificationRequest.cs b/crypto/src/pkcs/Pkcs10CertificationRequest.cs
index b4afceed9..d462fb4db 100644
--- a/crypto/src/pkcs/Pkcs10CertificationRequest.cs
+++ b/crypto/src/pkcs/Pkcs10CertificationRequest.cs
@@ -389,7 +389,7 @@ namespace Org.BouncyCastle.Pkcs
 
                 if (Platform.EndsWith(signature.AlgorithmName, "MGF1"))
                 {
-                    throw Platform.CreateNotImplementedException("signature algorithm with MGF1");
+                    throw new NotImplementedException("signature algorithm with MGF1");
 
                     //					try
                     //					{
diff --git a/crypto/src/pkcs/PrivateKeyInfoFactory.cs b/crypto/src/pkcs/PrivateKeyInfoFactory.cs
index e138141e3..7f52a1d1b 100644
--- a/crypto/src/pkcs/PrivateKeyInfoFactory.cs
+++ b/crypto/src/pkcs/PrivateKeyInfoFactory.cs
@@ -158,7 +158,7 @@ namespace Org.BouncyCastle.Pkcs
                 if (priv.AlgorithmName == "ECGOST3410")
                 {
                     if (priv.PublicKeyParamSet == null)
-                        throw Platform.CreateNotImplementedException("Not a CryptoPro parameter set");
+                        throw new NotImplementedException("Not a CryptoPro parameter set");
 
                     Gost3410PublicKeyAlgParameters gostParams = new Gost3410PublicKeyAlgParameters(
                         priv.PublicKeyParamSet, CryptoProObjectIdentifiers.GostR3411x94CryptoProParamSet);
@@ -195,7 +195,7 @@ namespace Org.BouncyCastle.Pkcs
                 Gost3410PrivateKeyParameters _key = (Gost3410PrivateKeyParameters)privateKey;
 
                 if (_key.PublicKeyParamSet == null)
-                    throw Platform.CreateNotImplementedException("Not a CryptoPro parameter set");
+                    throw new NotImplementedException("Not a CryptoPro parameter set");
 
                 byte[] keyEnc = _key.X.ToByteArrayUnsigned();
                 byte[] keyBytes = new byte[keyEnc.Length];
diff --git a/crypto/src/pkix/PkixCertPathBuilderResult.cs b/crypto/src/pkix/PkixCertPathBuilderResult.cs
index f8003032f..639009a37 100644
--- a/crypto/src/pkix/PkixCertPathBuilderResult.cs
+++ b/crypto/src/pkix/PkixCertPathBuilderResult.cs
@@ -34,12 +34,12 @@ namespace Org.BouncyCastle.Pkix
 
 		public override string ToString()
 		{
-			StringBuilder s = new StringBuilder();
-			s.Append("SimplePKIXCertPathBuilderResult: [\n");
-			s.Append("  Certification Path: ").Append(CertPath).Append('\n');
-			s.Append("  Trust Anchor: ").Append(this.TrustAnchor.TrustedCert.IssuerDN.ToString()).Append('\n');
-			s.Append("  Subject Public Key: ").Append(this.SubjectPublicKey).Append("\n]");
-			return s.ToString();
+			StringBuilder sb = new StringBuilder();
+			sb.AppendLine("SimplePKIXCertPathBuilderResult: [");
+			sb.Append("  Certification Path: ").AppendLine(CertPath.ToString());
+			sb.Append("  Trust Anchor: ").AppendLine(TrustAnchor.TrustedCert.IssuerDN.ToString());
+			sb.Append("  Subject Public Key: ").AppendLine(SubjectPublicKey.ToString());
+			return sb.ToString();
 		}
 	}
 }
diff --git a/crypto/src/pkix/PkixCertPathValidatorResult.cs b/crypto/src/pkix/PkixCertPathValidatorResult.cs
index e316128b0..7df3eab21 100644
--- a/crypto/src/pkix/PkixCertPathValidatorResult.cs
+++ b/crypto/src/pkix/PkixCertPathValidatorResult.cs
@@ -57,13 +57,12 @@ namespace Org.BouncyCastle.Pkix
 
 		public override string ToString() 
 		{
-			StringBuilder sB = new StringBuilder();
-			sB.Append("PKIXCertPathValidatorResult: [ \n");
-			sB.Append("  Trust Anchor: ").Append(this.TrustAnchor).Append('\n');
-			sB.Append("  Policy Tree: ").Append(this.PolicyTree).Append('\n');
-			sB.Append("  Subject Public Key: ").Append(this.SubjectPublicKey).Append("\n]");
-			return sB.ToString();
+			StringBuilder sb = new StringBuilder();
+			sb.AppendLine("PKIXCertPathValidatorResult: [");
+			sb.Append("  Trust Anchor: ").AppendLine(TrustAnchor.ToString());
+			sb.Append("  Policy Tree: ").AppendLine(PolicyTree.ToString());
+			sb.Append("  Subject Public Key: ").AppendLine(SubjectPublicKey.ToString());
+			return sb.ToString();
 		}
-
 	}
 }
diff --git a/crypto/src/pkix/PkixCertPathValidatorUtilities.cs b/crypto/src/pkix/PkixCertPathValidatorUtilities.cs
index 80ae2f83d..fccd0b3c5 100644
--- a/crypto/src/pkix/PkixCertPathValidatorUtilities.cs
+++ b/crypto/src/pkix/PkixCertPathValidatorUtilities.cs
@@ -259,7 +259,7 @@ namespace Org.BouncyCastle.Pkix
 //							baseDN = nsull;
 						}
 
-						throw Platform.CreateNotImplementedException("LDAP cert/CRL stores");
+						throw new NotImplementedException("LDAP cert/CRL stores");
 
 						// use all purpose parameters
 						//X509LDAPCertStoreParameters ldapParams = new X509LDAPCertStoreParameters.Builder(
diff --git a/crypto/src/pkix/PkixNameConstraintValidator.cs b/crypto/src/pkix/PkixNameConstraintValidator.cs
index e3e8c2fa0..5ad4de6f8 100644
--- a/crypto/src/pkix/PkixNameConstraintValidator.cs
+++ b/crypto/src/pkix/PkixNameConstraintValidator.cs
@@ -495,18 +495,14 @@ namespace Org.BouncyCastle.Pkix
             // a particular mailbox
             if (constraint.IndexOf('@') != -1)
             {
-                if (Platform.ToUpperInvariant(email).Equals(Platform.ToUpperInvariant(constraint)))
-                {
+                if (string.Equals(email, constraint, StringComparison.OrdinalIgnoreCase))
                     return true;
-                }
             }
             // on particular host
-            else if (!(constraint[0].Equals('.')))
+            else if (constraint[0] != '.')
             {
-                if (Platform.ToUpperInvariant(sub).Equals(Platform.ToUpperInvariant(constraint)))
-                {
+                if (string.Equals(sub, constraint, StringComparison.OrdinalIgnoreCase))
                     return true;
-                }
             }
             // address in sub domain
             else if (WithinDomain(sub, constraint))
@@ -1852,9 +1848,8 @@ namespace Org.BouncyCastle.Pkix
         private static void Append(StringBuilder sb, string name, object value)
         {
             sb.Append(name);
-            sb.Append(":\n");
-            sb.Append(value);
-            sb.Append('\n');
+            sb.AppendLine(":");
+            sb.AppendLine(value.ToString());
         }
     }
 }
diff --git a/crypto/src/tls/CertificateStatus.cs b/crypto/src/tls/CertificateStatus.cs
index 51f488fd6..7ff11f729 100644
--- a/crypto/src/tls/CertificateStatus.cs
+++ b/crypto/src/tls/CertificateStatus.cs
@@ -4,7 +4,6 @@ using System.IO;
 
 using Org.BouncyCastle.Asn1;
 using Org.BouncyCastle.Asn1.Ocsp;
-using Org.BouncyCastle.Utilities;
 
 namespace Org.BouncyCastle.Tls
 {
@@ -165,8 +164,7 @@ namespace Org.BouncyCastle.Tls
                     }
                 }
 
-                // Match IList capacity to actual size
-                response = Platform.CreateArrayList(ocspResponseList);
+                response = ocspResponseList;
                 break;
             }
             default:
diff --git a/crypto/src/tls/ProtocolVersion.cs b/crypto/src/tls/ProtocolVersion.cs
index e9c08e8cd..fd32848b0 100644
--- a/crypto/src/tls/ProtocolVersion.cs
+++ b/crypto/src/tls/ProtocolVersion.cs
@@ -412,7 +412,7 @@ namespace Org.BouncyCastle.Tls
             CheckUint8(minor);
 
             int v = (major << 8) | minor;
-            string hex = Platform.ToUpperInvariant(Convert.ToString(0x10000 | v, 16).Substring(1));
+            string hex = Convert.ToString(0x10000 | v, 16).Substring(1).ToUpperInvariant();
             return new ProtocolVersion(v, prefix + " 0x" + hex);
         }
     }
diff --git a/crypto/src/tls/SignatureScheme.cs b/crypto/src/tls/SignatureScheme.cs
index ed8e3c21b..56bc52d89 100644
--- a/crypto/src/tls/SignatureScheme.cs
+++ b/crypto/src/tls/SignatureScheme.cs
@@ -199,7 +199,7 @@ namespace Org.BouncyCastle.Tls
 
         public static string GetText(int signatureScheme)
         {
-            string hex = Platform.ToUpperInvariant(Convert.ToString(signatureScheme, 16));
+            string hex = Convert.ToString(signatureScheme, 16).ToUpperInvariant();
             return GetName(signatureScheme) + "(0x" + hex + ")";
         }