diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-06-27 02:19:14 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-06-27 02:19:14 +0700 |
commit | 4ea1816cffd5c8663bc9ae1234df0a70ef23fcd6 (patch) | |
tree | eb4fe294ef230435928a573fadef3047b4466e9b /crypto/src/asn1/x509/X509ExtensionsGenerator.cs | |
parent | Implement generic IEnumerable in ASN.1 classes (diff) | |
download | BouncyCastle.NET-ed25519-4ea1816cffd5c8663bc9ae1234df0a70ef23fcd6.tar.xz |
Generics migration work
Diffstat (limited to 'crypto/src/asn1/x509/X509ExtensionsGenerator.cs')
-rw-r--r-- | crypto/src/asn1/x509/X509ExtensionsGenerator.cs | 68 |
1 files changed, 27 insertions, 41 deletions
diff --git a/crypto/src/asn1/x509/X509ExtensionsGenerator.cs b/crypto/src/asn1/x509/X509ExtensionsGenerator.cs index 3b952fffa..438c507aa 100644 --- a/crypto/src/asn1/x509/X509ExtensionsGenerator.cs +++ b/crypto/src/asn1/x509/X509ExtensionsGenerator.cs @@ -1,5 +1,6 @@ using System; using System.Collections; +using System.Collections.Generic; using Org.BouncyCastle.Utilities; @@ -8,8 +9,9 @@ namespace Org.BouncyCastle.Asn1.X509 /// <remarks>Generator for X.509 extensions</remarks> public class X509ExtensionsGenerator { - private IDictionary extensions = Platform.CreateHashtable(); - private IList extOrdering = Platform.CreateArrayList(); + private Dictionary<DerObjectIdentifier, X509Extension> m_extensions = + new Dictionary<DerObjectIdentifier, X509Extension>(); + private List<DerObjectIdentifier> m_ordering = new List<DerObjectIdentifier>(); private static readonly IDictionary dupsAllowed = Platform.CreateHashtable(); @@ -19,16 +21,13 @@ namespace Org.BouncyCastle.Asn1.X509 dupsAllowed.Add(X509Extensions.IssuerAlternativeName, true); dupsAllowed.Add(X509Extensions.SubjectDirectoryAttributes, true); dupsAllowed.Add(X509Extensions.CertificateIssuer, true); - } - - /// <summary>Reset the generator</summary> public void Reset() { - extensions = Platform.CreateHashtable(); - extOrdering = Platform.CreateArrayList(); + m_extensions = new Dictionary<DerObjectIdentifier, X509Extension>(); + m_ordering = new List<DerObjectIdentifier>(); } /// <summary> @@ -38,10 +37,7 @@ namespace Org.BouncyCastle.Asn1.X509 /// <param name="oid">OID for the extension.</param> /// <param name="critical">True if critical, false otherwise.</param> /// <param name="extValue">The ASN.1 object to be included in the extension.</param> - public void AddExtension( - DerObjectIdentifier oid, - bool critical, - Asn1Encodable extValue) + public void AddExtension(DerObjectIdentifier oid, bool critical, Asn1Encodable extValue) { byte[] encoded; try @@ -63,38 +59,30 @@ namespace Org.BouncyCastle.Asn1.X509 /// <param name="oid">OID for the extension.</param> /// <param name="critical">True if critical, false otherwise.</param> /// <param name="extValue">The byte array to be wrapped.</param> - public void AddExtension( - DerObjectIdentifier oid, - bool critical, - byte[] extValue) + public void AddExtension(DerObjectIdentifier oid, bool critical, byte[] extValue) { - if (extensions.Contains(oid)) + if (m_extensions.TryGetValue(oid, out X509Extension existingExtension)) { - if (dupsAllowed.Contains(oid)) - { - X509Extension existingExtension = (X509Extension)extensions[oid]; - - Asn1Sequence seq1 = Asn1Sequence.GetInstance(DerOctetString.GetInstance(existingExtension.Value).GetOctets()); - Asn1EncodableVector items = Asn1EncodableVector.FromEnumerable(seq1); - Asn1Sequence seq2 = Asn1Sequence.GetInstance(extValue); - - foreach (Asn1Encodable enc in seq2) - { - items.Add(enc); - } + if (!dupsAllowed.Contains(oid)) + throw new ArgumentException("extension " + oid + " already added"); - extensions[oid] = new X509Extension(existingExtension.IsCritical, new DerOctetString(new DerSequence(items).GetEncoded())); + Asn1Sequence seq1 = Asn1Sequence.GetInstance( + Asn1OctetString.GetInstance(existingExtension.Value).GetOctets()); + Asn1EncodableVector items = Asn1EncodableVector.FromEnumerable(seq1); + Asn1Sequence seq2 = Asn1Sequence.GetInstance(extValue); - } - else + foreach (Asn1Encodable enc in seq2) { - throw new ArgumentException("extension " + oid + " already added"); + items.Add(enc); } + + m_extensions[oid] = new X509Extension(existingExtension.IsCritical, + new DerOctetString(new DerSequence(items).GetEncoded())); } else { - extOrdering.Add(oid); - extensions.Add(oid, new X509Extension(critical, new DerOctetString(extValue))); + m_ordering.Add(oid); + m_extensions.Add(oid, new X509Extension(critical, new DerOctetString(extValue))); } } @@ -113,25 +101,23 @@ namespace Org.BouncyCastle.Asn1.X509 /// <returns>True if empty, false otherwise</returns> public bool IsEmpty { - get { return extOrdering.Count < 1; } + get { return m_ordering.Count < 1; } } /// <summary>Generate an X509Extensions object based on the current state of the generator.</summary> /// <returns>An <c>X509Extensions</c> object</returns> public X509Extensions Generate() { - return new X509Extensions(extOrdering, extensions); + return new X509Extensions(m_ordering, m_extensions); } internal void AddExtension(DerObjectIdentifier oid, X509Extension x509Extension) { - if (extensions.Contains(oid)) - { + if (m_extensions.ContainsKey(oid)) throw new ArgumentException("extension " + oid + " already added"); - } - extOrdering.Add(oid); - extensions.Add(oid, x509Extension); + m_ordering.Add(oid); + m_extensions.Add(oid, x509Extension); } } } |