X509ExtensionsGenerator.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509
  7. {
  8. /// <remarks>Generator for X.509 extensions</remarks>
  9. public class X509ExtensionsGenerator
  10. {
  11. private IDictionary extensions = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  12. private IList extOrdering = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  13. /// <summary>Reset the generator</summary>
  14. public void Reset()
  15. {
  16. extensions = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  17. extOrdering = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  18. }
  19. /// <summary>
  20. /// Add an extension with the given oid and the passed in value to be included
  21. /// in the OCTET STRING associated with the extension.
  22. /// </summary>
  23. /// <param name="oid">OID for the extension.</param>
  24. /// <param name="critical">True if critical, false otherwise.</param>
  25. /// <param name="extValue">The ASN.1 object to be included in the extension.</param>
  26. public void AddExtension(
  27. DerObjectIdentifier oid,
  28. bool critical,
  29. Asn1Encodable extValue)
  30. {
  31. byte[] encoded;
  32. try
  33. {
  34. encoded = extValue.GetDerEncoded();
  35. }
  36. catch (Exception e)
  37. {
  38. throw new ArgumentException("error encoding value: " + e);
  39. }
  40. this.AddExtension(oid, critical, encoded);
  41. }
  42. /// <summary>
  43. /// Add an extension with the given oid and the passed in byte array to be wrapped
  44. /// in the OCTET STRING associated with the extension.
  45. /// </summary>
  46. /// <param name="oid">OID for the extension.</param>
  47. /// <param name="critical">True if critical, false otherwise.</param>
  48. /// <param name="extValue">The byte array to be wrapped.</param>
  49. public void AddExtension(
  50. DerObjectIdentifier oid,
  51. bool critical,
  52. byte[] extValue)
  53. {
  54. if (extensions.Contains(oid))
  55. {
  56. throw new ArgumentException("extension " + oid + " already added");
  57. }
  58. extOrdering.Add(oid);
  59. extensions.Add(oid, new X509Extension(critical, new DerOctetString(extValue)));
  60. }
  61. /// <summary>Return true if there are no extension present in this generator.</summary>
  62. /// <returns>True if empty, false otherwise</returns>
  63. public bool IsEmpty
  64. {
  65. get { return extOrdering.Count < 1; }
  66. }
  67. /// <summary>Generate an X509Extensions object based on the current state of the generator.</summary>
  68. /// <returns>An <c>X509Extensions</c> object</returns>
  69. public X509Extensions Generate()
  70. {
  71. return new X509Extensions(extOrdering, extensions);
  72. }
  73. }
  74. }
  75. #pragma warning restore
  76. #endif