AlgorithmIdentifier.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509
  5. {
  6. public class AlgorithmIdentifier
  7. : Asn1Encodable
  8. {
  9. private readonly DerObjectIdentifier algorithm;
  10. private readonly Asn1Encodable parameters;
  11. public static AlgorithmIdentifier GetInstance(
  12. Asn1TaggedObject obj,
  13. bool explicitly)
  14. {
  15. return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
  16. }
  17. public static AlgorithmIdentifier GetInstance(
  18. object obj)
  19. {
  20. if (obj == null)
  21. return null;
  22. if (obj is AlgorithmIdentifier)
  23. return (AlgorithmIdentifier)obj;
  24. return new AlgorithmIdentifier(Asn1Sequence.GetInstance(obj));
  25. }
  26. public AlgorithmIdentifier(
  27. DerObjectIdentifier algorithm)
  28. {
  29. this.algorithm = algorithm;
  30. }
  31. public AlgorithmIdentifier(
  32. string algorithm)
  33. {
  34. this.algorithm = new DerObjectIdentifier(algorithm);
  35. }
  36. public AlgorithmIdentifier(
  37. DerObjectIdentifier algorithm,
  38. Asn1Encodable parameters)
  39. {
  40. this.algorithm = algorithm;
  41. this.parameters = parameters;
  42. }
  43. internal AlgorithmIdentifier(
  44. Asn1Sequence seq)
  45. {
  46. if (seq.Count < 1 || seq.Count > 2)
  47. throw new ArgumentException("Bad sequence size: " + seq.Count);
  48. this.algorithm = DerObjectIdentifier.GetInstance(seq[0]);
  49. this.parameters = seq.Count < 2 ? null : seq[1];
  50. }
  51. /// <summary>
  52. /// Return the OID in the Algorithm entry of this identifier.
  53. /// </summary>
  54. public virtual DerObjectIdentifier Algorithm
  55. {
  56. get { return algorithm; }
  57. }
  58. public virtual DerObjectIdentifier ObjectID
  59. {
  60. get { return algorithm; }
  61. }
  62. /// <summary>
  63. /// Return the parameters structure in the Parameters entry of this identifier.
  64. /// </summary>
  65. public virtual Asn1Encodable Parameters
  66. {
  67. get { return parameters; }
  68. }
  69. /**
  70. * Produce an object suitable for an Asn1OutputStream.
  71. * <pre>
  72. * AlgorithmIdentifier ::= Sequence {
  73. * algorithm OBJECT IDENTIFIER,
  74. * parameters ANY DEFINED BY algorithm OPTIONAL }
  75. * </pre>
  76. */
  77. public override Asn1Object ToAsn1Object()
  78. {
  79. Asn1EncodableVector v = new Asn1EncodableVector(algorithm);
  80. v.AddOptional(parameters);
  81. return new DerSequence(v);
  82. }
  83. }
  84. }
  85. #pragma warning restore
  86. #endif