From 71adf4b7626e7b72c5055aed0bc34c687acc2911 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Fri, 10 May 2024 20:52:46 +0700 Subject: Refactoring in X509ExtensionsGenerator --- crypto/src/asn1/x509/X509ExtensionsGenerator.cs | 33 +++++++++++-------------- 1 file changed, 14 insertions(+), 19 deletions(-) (limited to 'crypto') diff --git a/crypto/src/asn1/x509/X509ExtensionsGenerator.cs b/crypto/src/asn1/x509/X509ExtensionsGenerator.cs index 02c5f6996..df1d0b5b7 100644 --- a/crypto/src/asn1/x509/X509ExtensionsGenerator.cs +++ b/crypto/src/asn1/x509/X509ExtensionsGenerator.cs @@ -56,29 +56,24 @@ namespace Org.BouncyCastle.Asn1.X509 /// The byte array to be wrapped. public void AddExtension(DerObjectIdentifier oid, bool critical, byte[] extValue) { - if (m_extensions.TryGetValue(oid, out X509Extension existingExtension)) - { - if (!m_dupsAllowed.Contains(oid)) - throw new ArgumentException("extension " + oid + " already added"); - - Asn1Sequence seq1 = Asn1Sequence.GetInstance( - Asn1OctetString.GetInstance(existingExtension.Value).GetOctets()); - Asn1EncodableVector items = Asn1EncodableVector.FromEnumerable(seq1); - Asn1Sequence seq2 = Asn1Sequence.GetInstance(extValue); - - foreach (Asn1Encodable enc in seq2) - { - items.Add(enc); - } - - m_extensions[oid] = new X509Extension(existingExtension.IsCritical, - new DerOctetString(new DerSequence(items).GetEncoded())); - } - else + if (!m_extensions.TryGetValue(oid, out X509Extension existingExtension)) { m_ordering.Add(oid); m_extensions.Add(oid, new X509Extension(critical, new DerOctetString(extValue))); + return; } + + if (!m_dupsAllowed.Contains(oid)) + throw new ArgumentException("extension " + oid + " already added"); + + Asn1Sequence seq1 = Asn1Sequence.GetInstance(existingExtension.Value.GetOctets()); + Asn1Sequence seq2 = Asn1Sequence.GetInstance(extValue); + + var concat = DerSequence.Concatenate(seq1, seq2); + + m_extensions[oid] = new X509Extension( + existingExtension.IsCritical | critical, + new DerOctetString(concat.GetEncoded(Asn1Encodable.Der))); } public void AddExtensions(X509Extensions extensions) -- cgit 1.4.1