X509ExtensionsGenerator.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections.Generic;
  5. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509
  6. {
  7. /// <remarks>Generator for X.509 extensions</remarks>
  8. public class X509ExtensionsGenerator
  9. {
  10. private Dictionary<DerObjectIdentifier, X509Extension> m_extensions =
  11. new Dictionary<DerObjectIdentifier, X509Extension>();
  12. private List<DerObjectIdentifier> m_ordering = new List<DerObjectIdentifier>();
  13. private static readonly ISet<DerObjectIdentifier> m_dupsAllowed = new HashSet<DerObjectIdentifier>()
  14. {
  15. X509Extensions.SubjectAlternativeName,
  16. X509Extensions.IssuerAlternativeName,
  17. X509Extensions.SubjectDirectoryAttributes,
  18. X509Extensions.CertificateIssuer
  19. };
  20. /// <summary>Reset the generator</summary>
  21. public void Reset()
  22. {
  23. m_extensions = new Dictionary<DerObjectIdentifier, X509Extension>();
  24. m_ordering = new List<DerObjectIdentifier>();
  25. }
  26. /// <summary>
  27. /// Add an extension with the given oid and the passed in value to be included
  28. /// in the OCTET STRING associated with the extension.
  29. /// </summary>
  30. /// <param name="oid">OID for the extension.</param>
  31. /// <param name="critical">True if critical, false otherwise.</param>
  32. /// <param name="extValue">The ASN.1 object to be included in the extension.</param>
  33. public void AddExtension(DerObjectIdentifier oid, bool critical, Asn1Encodable extValue)
  34. {
  35. byte[] encoded;
  36. try
  37. {
  38. encoded = extValue.GetDerEncoded();
  39. }
  40. catch (Exception e)
  41. {
  42. throw new ArgumentException("error encoding value: " + e);
  43. }
  44. this.AddExtension(oid, critical, encoded);
  45. }
  46. /// <summary>
  47. /// Add an extension with the given oid and the passed in byte array to be wrapped
  48. /// in the OCTET STRING associated with the extension.
  49. /// </summary>
  50. /// <param name="oid">OID for the extension.</param>
  51. /// <param name="critical">True if critical, false otherwise.</param>
  52. /// <param name="extValue">The byte array to be wrapped.</param>
  53. public void AddExtension(DerObjectIdentifier oid, bool critical, byte[] extValue)
  54. {
  55. if (m_extensions.TryGetValue(oid, out X509Extension existingExtension))
  56. {
  57. if (!m_dupsAllowed.Contains(oid))
  58. throw new ArgumentException("extension " + oid + " already added");
  59. Asn1Sequence seq1 = Asn1Sequence.GetInstance(
  60. Asn1OctetString.GetInstance(existingExtension.Value).GetOctets());
  61. Asn1EncodableVector items = Asn1EncodableVector.FromEnumerable(seq1);
  62. Asn1Sequence seq2 = Asn1Sequence.GetInstance(extValue);
  63. foreach (Asn1Encodable enc in seq2)
  64. {
  65. items.Add(enc);
  66. }
  67. m_extensions[oid] = new X509Extension(existingExtension.IsCritical,
  68. new DerOctetString(new DerSequence(items).GetEncoded()));
  69. }
  70. else
  71. {
  72. m_ordering.Add(oid);
  73. m_extensions.Add(oid, new X509Extension(critical, new DerOctetString(extValue)));
  74. }
  75. }
  76. public void AddExtensions(X509Extensions extensions)
  77. {
  78. foreach (DerObjectIdentifier ident in extensions.ExtensionOids)
  79. {
  80. X509Extension ext = extensions.GetExtension(ident);
  81. AddExtension(ident, ext.critical, ext.Value.GetOctets());
  82. }
  83. }
  84. /// <summary>Return true if there are no extension present in this generator.</summary>
  85. /// <returns>True if empty, false otherwise</returns>
  86. public bool IsEmpty
  87. {
  88. get { return m_ordering.Count < 1; }
  89. }
  90. /// <summary>Generate an X509Extensions object based on the current state of the generator.</summary>
  91. /// <returns>An <c>X509Extensions</c> object</returns>
  92. public X509Extensions Generate()
  93. {
  94. return new X509Extensions(m_ordering, m_extensions);
  95. }
  96. internal void AddExtension(DerObjectIdentifier oid, X509Extension x509Extension)
  97. {
  98. if (m_extensions.ContainsKey(oid))
  99. throw new ArgumentException("extension " + oid + " already added");
  100. m_ordering.Add(oid);
  101. m_extensions.Add(oid, x509Extension);
  102. }
  103. }
  104. }
  105. #pragma warning restore
  106. #endif