AlgorithmIdentifier.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. namespace Org.BouncyCastle.Asn1.X509
  4. {
  5. public class AlgorithmIdentifier
  6. : Asn1Encodable
  7. {
  8. private readonly DerObjectIdentifier algorithm;
  9. private readonly Asn1Encodable parameters;
  10. public static AlgorithmIdentifier GetInstance(
  11. Asn1TaggedObject obj,
  12. bool explicitly)
  13. {
  14. return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
  15. }
  16. public static AlgorithmIdentifier GetInstance(
  17. object obj)
  18. {
  19. if (obj == null)
  20. return null;
  21. if (obj is AlgorithmIdentifier)
  22. return (AlgorithmIdentifier)obj;
  23. return new AlgorithmIdentifier(Asn1Sequence.GetInstance(obj));
  24. }
  25. public AlgorithmIdentifier(
  26. DerObjectIdentifier algorithm)
  27. {
  28. this.algorithm = algorithm;
  29. }
  30. [Obsolete("Use version taking a DerObjectIdentifier")]
  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. [Obsolete("Use 'Algorithm' property instead")]
  59. public virtual DerObjectIdentifier ObjectID
  60. {
  61. get { return algorithm; }
  62. }
  63. /// <summary>
  64. /// Return the parameters structure in the Parameters entry of this identifier.
  65. /// </summary>
  66. public virtual Asn1Encodable Parameters
  67. {
  68. get { return parameters; }
  69. }
  70. /**
  71. * Produce an object suitable for an Asn1OutputStream.
  72. * <pre>
  73. * AlgorithmIdentifier ::= Sequence {
  74. * algorithm OBJECT IDENTIFIER,
  75. * parameters ANY DEFINED BY algorithm OPTIONAL }
  76. * </pre>
  77. */
  78. public override Asn1Object ToAsn1Object()
  79. {
  80. Asn1EncodableVector v = new Asn1EncodableVector(algorithm);
  81. v.AddOptional(parameters);
  82. return new DerSequence(v);
  83. }
  84. }
  85. }
  86. #endif