X509ExtensionsGenerator.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. private static readonly IDictionary dupsAllowed = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  14. static X509ExtensionsGenerator()
  15. {
  16. dupsAllowed.Add(X509Extensions.SubjectAlternativeName, true);
  17. dupsAllowed.Add(X509Extensions.IssuerAlternativeName, true);
  18. dupsAllowed.Add(X509Extensions.SubjectDirectoryAttributes, true);
  19. dupsAllowed.Add(X509Extensions.CertificateIssuer, true);
  20. }
  21. /// <summary>Reset the generator</summary>
  22. public void Reset()
  23. {
  24. extensions = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable();
  25. extOrdering = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  26. }
  27. /// <summary>
  28. /// Add an extension with the given oid and the passed in value to be included
  29. /// in the OCTET STRING associated with the extension.
  30. /// </summary>
  31. /// <param name="oid">OID for the extension.</param>
  32. /// <param name="critical">True if critical, false otherwise.</param>
  33. /// <param name="extValue">The ASN.1 object to be included in the extension.</param>
  34. public void AddExtension(
  35. DerObjectIdentifier oid,
  36. bool critical,
  37. Asn1Encodable extValue)
  38. {
  39. byte[] encoded;
  40. try
  41. {
  42. encoded = extValue.GetDerEncoded();
  43. }
  44. catch (Exception e)
  45. {
  46. throw new ArgumentException("error encoding value: " + e);
  47. }
  48. this.AddExtension(oid, critical, encoded);
  49. }
  50. /// <summary>
  51. /// Add an extension with the given oid and the passed in byte array to be wrapped
  52. /// in the OCTET STRING associated with the extension.
  53. /// </summary>
  54. /// <param name="oid">OID for the extension.</param>
  55. /// <param name="critical">True if critical, false otherwise.</param>
  56. /// <param name="extValue">The byte array to be wrapped.</param>
  57. public void AddExtension(
  58. DerObjectIdentifier oid,
  59. bool critical,
  60. byte[] extValue)
  61. {
  62. if (extensions.Contains(oid))
  63. {
  64. if (dupsAllowed.Contains(oid))
  65. {
  66. X509Extension existingExtension = (X509Extension)extensions[oid];
  67. Asn1Sequence seq1 = Asn1Sequence.GetInstance(DerOctetString.GetInstance(existingExtension.Value).GetOctets());
  68. Asn1EncodableVector items = Asn1EncodableVector.FromEnumerable(seq1);
  69. Asn1Sequence seq2 = Asn1Sequence.GetInstance(extValue);
  70. foreach (Asn1Encodable enc in seq2)
  71. {
  72. items.Add(enc);
  73. }
  74. extensions[oid] = new X509Extension(existingExtension.IsCritical, new DerOctetString(new DerSequence(items).GetEncoded()));
  75. }
  76. else
  77. {
  78. throw new ArgumentException("extension " + oid + " already added");
  79. }
  80. }
  81. else
  82. {
  83. extOrdering.Add(oid);
  84. extensions.Add(oid, new X509Extension(critical, new DerOctetString(extValue)));
  85. }
  86. }
  87. public void AddExtensions(X509Extensions extensions)
  88. {
  89. foreach (DerObjectIdentifier ident in extensions.ExtensionOids)
  90. {
  91. X509Extension ext = extensions.GetExtension(ident);
  92. AddExtension(ident, ext.critical, ext.Value.GetOctets());
  93. }
  94. }
  95. /// <summary>Return true if there are no extension present in this generator.</summary>
  96. /// <returns>True if empty, false otherwise</returns>
  97. public bool IsEmpty
  98. {
  99. get { return extOrdering.Count < 1; }
  100. }
  101. /// <summary>Generate an X509Extensions object based on the current state of the generator.</summary>
  102. /// <returns>An <c>X509Extensions</c> object</returns>
  103. public X509Extensions Generate()
  104. {
  105. return new X509Extensions(extOrdering, extensions);
  106. }
  107. internal void AddExtension(DerObjectIdentifier oid, X509Extension x509Extension)
  108. {
  109. if (extensions.Contains(oid))
  110. {
  111. throw new ArgumentException("extension " + oid + " already added");
  112. }
  113. extOrdering.Add(oid);
  114. extensions.Add(oid, x509Extension);
  115. }
  116. }
  117. }
  118. #pragma warning restore
  119. #endif