diff options
-rw-r--r-- | crypto/src/asn1/Asn1InputStream.cs | 9 | ||||
-rw-r--r-- | crypto/src/asn1/Asn1RelativeOid.cs | 25 | ||||
-rw-r--r-- | crypto/src/asn1/DerObjectIdentifier.cs | 2 |
3 files changed, 32 insertions, 4 deletions
diff --git a/crypto/src/asn1/Asn1InputStream.cs b/crypto/src/asn1/Asn1InputStream.cs index 96b0a1c66..a1bc987cc 100644 --- a/crypto/src/asn1/Asn1InputStream.cs +++ b/crypto/src/asn1/Asn1InputStream.cs @@ -377,7 +377,9 @@ namespace Org.BouncyCastle.Asn1 switch (tagNo) { case Asn1Tags.BmpString: + { return CreateDerBmpString(defIn); + } case Asn1Tags.Boolean: { GetBuffer(defIn, tmpBuffers, out var contents); @@ -393,6 +395,11 @@ namespace Org.BouncyCastle.Asn1 bool usedBuffer = GetBuffer(defIn, tmpBuffers, out var contents); return DerObjectIdentifier.CreatePrimitive(contents, clone: usedBuffer); } + case Asn1Tags.RelativeOid: + { + bool usedBuffer = GetBuffer(defIn, tmpBuffers, out var contents); + return Asn1RelativeOid.CreatePrimitive(contents, clone: usedBuffer); + } } byte[] bytes = defIn.ToArray(); @@ -421,8 +428,6 @@ namespace Org.BouncyCastle.Asn1 return Asn1OctetString.CreatePrimitive(bytes); case Asn1Tags.PrintableString: return DerPrintableString.CreatePrimitive(bytes); - case Asn1Tags.RelativeOid: - return Asn1RelativeOid.CreatePrimitive(bytes, false); case Asn1Tags.T61String: return DerT61String.CreatePrimitive(bytes); case Asn1Tags.UniversalString: diff --git a/crypto/src/asn1/Asn1RelativeOid.cs b/crypto/src/asn1/Asn1RelativeOid.cs index f43a85479..3e2b54c9e 100644 --- a/crypto/src/asn1/Asn1RelativeOid.cs +++ b/crypto/src/asn1/Asn1RelativeOid.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Text; +using System.Threading; using Org.BouncyCastle.Math; using Org.BouncyCastle.Utilities; @@ -80,6 +81,8 @@ namespace Org.BouncyCastle.Asn1 private const long LongLimit = (long.MaxValue >> 7) - 0x7F; + private static readonly Asn1RelativeOid[] Cache = new Asn1RelativeOid[64]; + private readonly byte[] m_contents; private string m_identifier; @@ -167,7 +170,27 @@ namespace Org.BouncyCastle.Asn1 internal static Asn1RelativeOid CreatePrimitive(byte[] contents, bool clone) { - return new Asn1RelativeOid(contents, clone); + uint index = (uint)Arrays.GetHashCode(contents); + + index ^= index >> 24; + index ^= index >> 12; + index ^= index >> 6; + index &= 63; + + var originalEntry = Volatile.Read(ref Cache[index]); + if (originalEntry != null && Arrays.AreEqual(contents, originalEntry.m_contents)) + return originalEntry; + + var newEntry = new Asn1RelativeOid(contents, clone); + + var exchangedEntry = Interlocked.CompareExchange(ref Cache[index], newEntry, originalEntry); + if (exchangedEntry != originalEntry) + { + if (exchangedEntry != null && Arrays.AreEqual(contents, exchangedEntry.m_contents)) + return exchangedEntry; + } + + return newEntry; } internal static bool IsValidContents(byte[] contents) diff --git a/crypto/src/asn1/DerObjectIdentifier.cs b/crypto/src/asn1/DerObjectIdentifier.cs index 04792cbdd..51acd232c 100644 --- a/crypto/src/asn1/DerObjectIdentifier.cs +++ b/crypto/src/asn1/DerObjectIdentifier.cs @@ -197,7 +197,7 @@ namespace Org.BouncyCastle.Asn1 internal static DerObjectIdentifier CreatePrimitive(byte[] contents, bool clone) { - int index = Arrays.GetHashCode(contents); + uint index = (uint)Arrays.GetHashCode(contents); index ^= index >> 20; index ^= index >> 10; |