From 36d6b01e1e8d02ec99894bb2c8682acff65f55c3 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Fri, 1 Jul 2022 16:53:16 +0700 Subject: Fix static readonly collections --- crypto/src/asn1/x509/X509Name.cs | 186 ++++++++++++++++++++------------------- crypto/src/tsp/TSPAlgorithms.cs | 10 +-- 2 files changed, 102 insertions(+), 94 deletions(-) (limited to 'crypto') diff --git a/crypto/src/asn1/x509/X509Name.cs b/crypto/src/asn1/x509/X509Name.cs index 7c9797e56..d0e304798 100644 --- a/crypto/src/asn1/x509/X509Name.cs +++ b/crypto/src/asn1/x509/X509Name.cs @@ -4,6 +4,7 @@ using System.IO; using System.Text; using Org.BouncyCastle.Asn1.Pkcs; +using Org.BouncyCastle.Utilities.Collections; using Org.BouncyCastle.Utilities.Encoders; namespace Org.BouncyCastle.Asn1.X509 @@ -194,11 +195,10 @@ namespace Org.BouncyCastle.Asn1.X509 * determines whether or not strings should be processed and printed * from back to front. */ -// public static bool DefaultReverse = false; public static bool DefaultReverse { - get { return defaultReverse[0]; } - set { defaultReverse[0] = value; } + get { lock (defaultReverse) return defaultReverse[0]; } + set { lock (defaultReverse) defaultReverse[0] = value; } } private static readonly bool[] defaultReverse = { false }; @@ -207,112 +207,120 @@ namespace Org.BouncyCastle.Asn1.X509 * default look up table translating OID values into their common symbols following * the convention in RFC 2253 with a few extras */ - public static readonly IDictionary DefaultSymbols = + private static readonly IDictionary DefaultSymbolsInternal = new Dictionary(); + public static readonly IDictionary DefaultSymbols = + CollectionUtilities.ReadOnly(DefaultSymbolsInternal); /** * look up table translating OID values into their common symbols following the convention in RFC 2253 */ - public static readonly IDictionary RFC2253Symbols = + private static readonly IDictionary RFC2253SymbolsInternal = new Dictionary(); + public static readonly IDictionary RFC2253Symbols = + CollectionUtilities.ReadOnly(RFC2253SymbolsInternal); /** * look up table translating OID values into their common symbols following the convention in RFC 1779 * */ - public static readonly IDictionary RFC1779Symbols = + private static readonly IDictionary RFC1779SymbolsInternal = new Dictionary(); + public static readonly IDictionary RFC1779Symbols = + CollectionUtilities.ReadOnly(RFC1779SymbolsInternal); /** * look up table translating common symbols into their OIDS. */ - public static readonly IDictionary DefaultLookup = + private static readonly IDictionary DefaultLookupInternal = new Dictionary(StringComparer.OrdinalIgnoreCase); + public static readonly IDictionary DefaultLookup = + CollectionUtilities.ReadOnly(DefaultLookupInternal); static X509Name() { - DefaultSymbols.Add(C, "C"); - DefaultSymbols.Add(O, "O"); - DefaultSymbols.Add(T, "T"); - DefaultSymbols.Add(OU, "OU"); - DefaultSymbols.Add(CN, "CN"); - DefaultSymbols.Add(L, "L"); - DefaultSymbols.Add(ST, "ST"); - DefaultSymbols.Add(SerialNumber, "SERIALNUMBER"); - DefaultSymbols.Add(EmailAddress, "E"); - DefaultSymbols.Add(DC, "DC"); - DefaultSymbols.Add(UID, "UID"); - DefaultSymbols.Add(Street, "STREET"); - DefaultSymbols.Add(Surname, "SURNAME"); - DefaultSymbols.Add(GivenName, "GIVENNAME"); - DefaultSymbols.Add(Initials, "INITIALS"); - DefaultSymbols.Add(Generation, "GENERATION"); - DefaultSymbols.Add(UnstructuredAddress, "unstructuredAddress"); - DefaultSymbols.Add(UnstructuredName, "unstructuredName"); - DefaultSymbols.Add(UniqueIdentifier, "UniqueIdentifier"); - DefaultSymbols.Add(DnQualifier, "DN"); - DefaultSymbols.Add(Pseudonym, "Pseudonym"); - DefaultSymbols.Add(PostalAddress, "PostalAddress"); - DefaultSymbols.Add(NameAtBirth, "NameAtBirth"); - DefaultSymbols.Add(CountryOfCitizenship, "CountryOfCitizenship"); - DefaultSymbols.Add(CountryOfResidence, "CountryOfResidence"); - DefaultSymbols.Add(Gender, "Gender"); - DefaultSymbols.Add(PlaceOfBirth, "PlaceOfBirth"); - DefaultSymbols.Add(DateOfBirth, "DateOfBirth"); - DefaultSymbols.Add(PostalCode, "PostalCode"); - DefaultSymbols.Add(BusinessCategory, "BusinessCategory"); - DefaultSymbols.Add(TelephoneNumber, "TelephoneNumber"); - - RFC2253Symbols.Add(C, "C"); - RFC2253Symbols.Add(O, "O"); - RFC2253Symbols.Add(OU, "OU"); - RFC2253Symbols.Add(CN, "CN"); - RFC2253Symbols.Add(L, "L"); - RFC2253Symbols.Add(ST, "ST"); - RFC2253Symbols.Add(Street, "STREET"); - RFC2253Symbols.Add(DC, "DC"); - RFC2253Symbols.Add(UID, "UID"); - - RFC1779Symbols.Add(C, "C"); - RFC1779Symbols.Add(O, "O"); - RFC1779Symbols.Add(OU, "OU"); - RFC1779Symbols.Add(CN, "CN"); - RFC1779Symbols.Add(L, "L"); - RFC1779Symbols.Add(ST, "ST"); - RFC1779Symbols.Add(Street, "STREET"); - - DefaultLookup.Add("c", C); - DefaultLookup.Add("o", O); - DefaultLookup.Add("t", T); - DefaultLookup.Add("ou", OU); - DefaultLookup.Add("cn", CN); - DefaultLookup.Add("l", L); - DefaultLookup.Add("st", ST); - DefaultLookup.Add("serialnumber", SerialNumber); - DefaultLookup.Add("street", Street); - DefaultLookup.Add("emailaddress", E); - DefaultLookup.Add("dc", DC); - DefaultLookup.Add("e", E); - DefaultLookup.Add("uid", UID); - DefaultLookup.Add("surname", Surname); - DefaultLookup.Add("givenname", GivenName); - DefaultLookup.Add("initials", Initials); - DefaultLookup.Add("generation", Generation); - DefaultLookup.Add("unstructuredaddress", UnstructuredAddress); - DefaultLookup.Add("unstructuredname", UnstructuredName); - DefaultLookup.Add("uniqueidentifier", UniqueIdentifier); - DefaultLookup.Add("dn", DnQualifier); - DefaultLookup.Add("pseudonym", Pseudonym); - DefaultLookup.Add("postaladdress", PostalAddress); - DefaultLookup.Add("nameofbirth", NameAtBirth); - DefaultLookup.Add("countryofcitizenship", CountryOfCitizenship); - DefaultLookup.Add("countryofresidence", CountryOfResidence); - DefaultLookup.Add("gender", Gender); - DefaultLookup.Add("placeofbirth", PlaceOfBirth); - DefaultLookup.Add("dateofbirth", DateOfBirth); - DefaultLookup.Add("postalcode", PostalCode); - DefaultLookup.Add("businesscategory", BusinessCategory); - DefaultLookup.Add("telephonenumber", TelephoneNumber); + DefaultSymbolsInternal.Add(C, "C"); + DefaultSymbolsInternal.Add(O, "O"); + DefaultSymbolsInternal.Add(T, "T"); + DefaultSymbolsInternal.Add(OU, "OU"); + DefaultSymbolsInternal.Add(CN, "CN"); + DefaultSymbolsInternal.Add(L, "L"); + DefaultSymbolsInternal.Add(ST, "ST"); + DefaultSymbolsInternal.Add(SerialNumber, "SERIALNUMBER"); + DefaultSymbolsInternal.Add(EmailAddress, "E"); + DefaultSymbolsInternal.Add(DC, "DC"); + DefaultSymbolsInternal.Add(UID, "UID"); + DefaultSymbolsInternal.Add(Street, "STREET"); + DefaultSymbolsInternal.Add(Surname, "SURNAME"); + DefaultSymbolsInternal.Add(GivenName, "GIVENNAME"); + DefaultSymbolsInternal.Add(Initials, "INITIALS"); + DefaultSymbolsInternal.Add(Generation, "GENERATION"); + DefaultSymbolsInternal.Add(UnstructuredAddress, "unstructuredAddress"); + DefaultSymbolsInternal.Add(UnstructuredName, "unstructuredName"); + DefaultSymbolsInternal.Add(UniqueIdentifier, "UniqueIdentifier"); + DefaultSymbolsInternal.Add(DnQualifier, "DN"); + DefaultSymbolsInternal.Add(Pseudonym, "Pseudonym"); + DefaultSymbolsInternal.Add(PostalAddress, "PostalAddress"); + DefaultSymbolsInternal.Add(NameAtBirth, "NameAtBirth"); + DefaultSymbolsInternal.Add(CountryOfCitizenship, "CountryOfCitizenship"); + DefaultSymbolsInternal.Add(CountryOfResidence, "CountryOfResidence"); + DefaultSymbolsInternal.Add(Gender, "Gender"); + DefaultSymbolsInternal.Add(PlaceOfBirth, "PlaceOfBirth"); + DefaultSymbolsInternal.Add(DateOfBirth, "DateOfBirth"); + DefaultSymbolsInternal.Add(PostalCode, "PostalCode"); + DefaultSymbolsInternal.Add(BusinessCategory, "BusinessCategory"); + DefaultSymbolsInternal.Add(TelephoneNumber, "TelephoneNumber"); + + RFC2253SymbolsInternal.Add(C, "C"); + RFC2253SymbolsInternal.Add(O, "O"); + RFC2253SymbolsInternal.Add(OU, "OU"); + RFC2253SymbolsInternal.Add(CN, "CN"); + RFC2253SymbolsInternal.Add(L, "L"); + RFC2253SymbolsInternal.Add(ST, "ST"); + RFC2253SymbolsInternal.Add(Street, "STREET"); + RFC2253SymbolsInternal.Add(DC, "DC"); + RFC2253SymbolsInternal.Add(UID, "UID"); + + RFC1779SymbolsInternal.Add(C, "C"); + RFC1779SymbolsInternal.Add(O, "O"); + RFC1779SymbolsInternal.Add(OU, "OU"); + RFC1779SymbolsInternal.Add(CN, "CN"); + RFC1779SymbolsInternal.Add(L, "L"); + RFC1779SymbolsInternal.Add(ST, "ST"); + RFC1779SymbolsInternal.Add(Street, "STREET"); + + DefaultLookupInternal.Add("c", C); + DefaultLookupInternal.Add("o", O); + DefaultLookupInternal.Add("t", T); + DefaultLookupInternal.Add("ou", OU); + DefaultLookupInternal.Add("cn", CN); + DefaultLookupInternal.Add("l", L); + DefaultLookupInternal.Add("st", ST); + DefaultLookupInternal.Add("serialnumber", SerialNumber); + DefaultLookupInternal.Add("street", Street); + DefaultLookupInternal.Add("emailaddress", E); + DefaultLookupInternal.Add("dc", DC); + DefaultLookupInternal.Add("e", E); + DefaultLookupInternal.Add("uid", UID); + DefaultLookupInternal.Add("surname", Surname); + DefaultLookupInternal.Add("givenname", GivenName); + DefaultLookupInternal.Add("initials", Initials); + DefaultLookupInternal.Add("generation", Generation); + DefaultLookupInternal.Add("unstructuredaddress", UnstructuredAddress); + DefaultLookupInternal.Add("unstructuredname", UnstructuredName); + DefaultLookupInternal.Add("uniqueidentifier", UniqueIdentifier); + DefaultLookupInternal.Add("dn", DnQualifier); + DefaultLookupInternal.Add("pseudonym", Pseudonym); + DefaultLookupInternal.Add("postaladdress", PostalAddress); + DefaultLookupInternal.Add("nameofbirth", NameAtBirth); + DefaultLookupInternal.Add("countryofcitizenship", CountryOfCitizenship); + DefaultLookupInternal.Add("countryofresidence", CountryOfResidence); + DefaultLookupInternal.Add("gender", Gender); + DefaultLookupInternal.Add("placeofbirth", PlaceOfBirth); + DefaultLookupInternal.Add("dateofbirth", DateOfBirth); + DefaultLookupInternal.Add("postalcode", PostalCode); + DefaultLookupInternal.Add("businesscategory", BusinessCategory); + DefaultLookupInternal.Add("telephonenumber", TelephoneNumber); } private readonly List ordering = new List(); diff --git a/crypto/src/tsp/TSPAlgorithms.cs b/crypto/src/tsp/TSPAlgorithms.cs index 8d7e12ee4..eb5e89439 100644 --- a/crypto/src/tsp/TSPAlgorithms.cs +++ b/crypto/src/tsp/TSPAlgorithms.cs @@ -7,14 +7,14 @@ using Org.BouncyCastle.Asn1.Oiw; using Org.BouncyCastle.Asn1.Pkcs; using Org.BouncyCastle.Asn1.Rosstandart; using Org.BouncyCastle.Asn1.TeleTrust; -using Org.BouncyCastle.Utilities; +using Org.BouncyCastle.Utilities.Collections; namespace Org.BouncyCastle.Tsp { /** * Recognised hash algorithms for the time stamp protocol. */ - public abstract class TspAlgorithms + public static class TspAlgorithms { public static readonly string MD5 = PkcsObjectIdentifiers.MD5.Id; @@ -35,15 +35,15 @@ namespace Org.BouncyCastle.Tsp public static readonly string SM3 = GMObjectIdentifiers.sm3.Id; - public static readonly List Allowed; + public static readonly IList Allowed; static TspAlgorithms() { - Allowed = new List() + Allowed = CollectionUtilities.ReadOnly(new List() { Gost3411, Gost3411_2012_256, Gost3411_2012_512, MD5, RipeMD128, RipeMD160, RipeMD256, Sha1, Sha224, Sha256, Sha384, Sha512, SM3 - }; + }); } } } -- cgit 1.4.1