From b093c24687f353e20db6e28917bc34e2d2cdd769 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Fri, 8 Mar 2024 19:27:23 +0700 Subject: [PATCH] Add ASN1RelativeOID cache --- .../java/org/bouncycastle/asn1/ASN1InputStream.java | 3 ++- .../org/bouncycastle/asn1/ASN1ObjectIdentifier.java | 8 ++++---- .../java/org/bouncycastle/asn1/ASN1RelativeOID.java | 11 +++++++++++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/org/bouncycastle/asn1/ASN1InputStream.java b/core/src/main/java/org/bouncycastle/asn1/ASN1InputStream.java index ff7e3e09d1..5b1fb12343 100644 --- a/core/src/main/java/org/bouncycastle/asn1/ASN1InputStream.java +++ b/core/src/main/java/org/bouncycastle/asn1/ASN1InputStream.java @@ -553,7 +553,8 @@ static ASN1Primitive createPrimitiveDERObject( case PRINTABLE_STRING: return ASN1PrintableString.createPrimitive(defIn.toByteArray()); case RELATIVE_OID: - return ASN1RelativeOID.createPrimitive(defIn.toByteArray(), false); + // TODO Ideally only clone if we used a buffer + return ASN1RelativeOID.createPrimitive(getBuffer(defIn, tmpBuffers), true); case T61_STRING: return ASN1T61String.createPrimitive(defIn.toByteArray()); case UNIVERSAL_STRING: diff --git a/core/src/main/java/org/bouncycastle/asn1/ASN1ObjectIdentifier.java b/core/src/main/java/org/bouncycastle/asn1/ASN1ObjectIdentifier.java index 9f14c472d8..b3029d48bb 100644 --- a/core/src/main/java/org/bouncycastle/asn1/ASN1ObjectIdentifier.java +++ b/core/src/main/java/org/bouncycastle/asn1/ASN1ObjectIdentifier.java @@ -421,7 +421,7 @@ public ASN1ObjectIdentifier intern() return oid; } - private static class OidHandle + static class OidHandle { private final int key; private final byte[] contents; @@ -452,10 +452,10 @@ static ASN1ObjectIdentifier createPrimitive(byte[] contents, boolean clone) { final OidHandle hdl = new OidHandle(contents); ASN1ObjectIdentifier oid = pool.get(hdl); - if (oid == null) + if (oid != null) { - return new ASN1ObjectIdentifier(contents, clone); + return oid; } - return oid; + return new ASN1ObjectIdentifier(contents, clone); } } diff --git a/core/src/main/java/org/bouncycastle/asn1/ASN1RelativeOID.java b/core/src/main/java/org/bouncycastle/asn1/ASN1RelativeOID.java index 4d2dee72dd..1814e179cd 100644 --- a/core/src/main/java/org/bouncycastle/asn1/ASN1RelativeOID.java +++ b/core/src/main/java/org/bouncycastle/asn1/ASN1RelativeOID.java @@ -3,6 +3,8 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.math.BigInteger; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; import org.bouncycastle.util.Arrays; @@ -78,6 +80,9 @@ public static ASN1RelativeOID tryFromID(String identifier) private static final long LONG_LIMIT = (Long.MAX_VALUE >> 7) - 0x7F; + private static final ConcurrentMap pool = + new ConcurrentHashMap(); + private final byte[] contents; private String identifier; @@ -180,6 +185,12 @@ boolean encodeConstructed() static ASN1RelativeOID createPrimitive(byte[] contents, boolean clone) { + final ASN1ObjectIdentifier.OidHandle hdl = new ASN1ObjectIdentifier.OidHandle(contents); + ASN1RelativeOID oid = pool.get(hdl); + if (oid != null) + { + return oid; + } return new ASN1RelativeOID(contents, clone); }