diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2024-05-10 20:52:46 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2024-05-10 20:52:46 +0700 |
commit | 71adf4b7626e7b72c5055aed0bc34c687acc2911 (patch) | |
tree | 91b4e7763a1fcdabb24aae2952e7e49342df7708 | |
parent | Add checks in CMS tests that recipient matches expected certificate (diff) | |
download | BouncyCastle.NET-ed25519-71adf4b7626e7b72c5055aed0bc34c687acc2911.tar.xz |
Refactoring in X509ExtensionsGenerator
-rw-r--r-- | crypto/src/asn1/x509/X509ExtensionsGenerator.cs | 33 |
1 files changed, 14 insertions, 19 deletions
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 /// <param name="extValue">The byte array to be wrapped.</param> 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) |