RecipientKeyIdentifier.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cms
  6. {
  7. public class RecipientKeyIdentifier
  8. : Asn1Encodable
  9. {
  10. private Asn1OctetString subjectKeyIdentifier;
  11. private DerGeneralizedTime date;
  12. private OtherKeyAttribute other;
  13. public RecipientKeyIdentifier(
  14. Asn1OctetString subjectKeyIdentifier,
  15. DerGeneralizedTime date,
  16. OtherKeyAttribute other)
  17. {
  18. this.subjectKeyIdentifier = subjectKeyIdentifier;
  19. this.date = date;
  20. this.other = other;
  21. }
  22. public RecipientKeyIdentifier(
  23. byte[] subjectKeyIdentifier)
  24. : this(subjectKeyIdentifier, null, null)
  25. {
  26. }
  27. public RecipientKeyIdentifier(
  28. byte[] subjectKeyIdentifier,
  29. DerGeneralizedTime date,
  30. OtherKeyAttribute other)
  31. {
  32. this.subjectKeyIdentifier = new DerOctetString(subjectKeyIdentifier);
  33. this.date = date;
  34. this.other = other;
  35. }
  36. public RecipientKeyIdentifier(
  37. Asn1Sequence seq)
  38. {
  39. subjectKeyIdentifier = Asn1OctetString.GetInstance(
  40. seq[0]);
  41. switch(seq.Count)
  42. {
  43. case 1:
  44. break;
  45. case 2:
  46. if (seq[1] is DerGeneralizedTime)
  47. {
  48. date = (DerGeneralizedTime) seq[1];
  49. }
  50. else
  51. {
  52. other = OtherKeyAttribute.GetInstance(seq[2]);
  53. }
  54. break;
  55. case 3:
  56. date = (DerGeneralizedTime) seq[1];
  57. other = OtherKeyAttribute.GetInstance(seq[2]);
  58. break;
  59. default:
  60. throw new ArgumentException("Invalid RecipientKeyIdentifier");
  61. }
  62. }
  63. /**
  64. * return a RecipientKeyIdentifier object from a tagged object.
  65. *
  66. * @param _ato the tagged object holding the object we want.
  67. * @param _explicit true if the object is meant to be explicitly
  68. * tagged false otherwise.
  69. * @exception ArgumentException if the object held by the
  70. * tagged object cannot be converted.
  71. */
  72. public static RecipientKeyIdentifier GetInstance(
  73. Asn1TaggedObject ato,
  74. bool explicitly)
  75. {
  76. return GetInstance(Asn1Sequence.GetInstance(ato, explicitly));
  77. }
  78. /**
  79. * return a RecipientKeyIdentifier object from the given object.
  80. *
  81. * @param _obj the object we want converted.
  82. * @exception ArgumentException if the object cannot be converted.
  83. */
  84. public static RecipientKeyIdentifier GetInstance(
  85. object obj)
  86. {
  87. if (obj == null || obj is RecipientKeyIdentifier)
  88. return (RecipientKeyIdentifier) obj;
  89. if (obj is Asn1Sequence)
  90. return new RecipientKeyIdentifier((Asn1Sequence) obj);
  91. throw new ArgumentException("Invalid RecipientKeyIdentifier: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  92. }
  93. public Asn1OctetString SubjectKeyIdentifier
  94. {
  95. get { return subjectKeyIdentifier; }
  96. }
  97. public DerGeneralizedTime Date
  98. {
  99. get { return date; }
  100. }
  101. public OtherKeyAttribute OtherKeyAttribute
  102. {
  103. get { return other; }
  104. }
  105. /**
  106. * Produce an object suitable for an Asn1OutputStream.
  107. * <pre>
  108. * RecipientKeyIdentifier ::= Sequence {
  109. * subjectKeyIdentifier SubjectKeyIdentifier,
  110. * date GeneralizedTime OPTIONAL,
  111. * other OtherKeyAttribute OPTIONAL
  112. * }
  113. *
  114. * SubjectKeyIdentifier ::= OCTET STRING
  115. * </pre>
  116. */
  117. public override Asn1Object ToAsn1Object()
  118. {
  119. Asn1EncodableVector v = new Asn1EncodableVector(subjectKeyIdentifier);
  120. v.AddOptional(date, other);
  121. return new DerSequence(v);
  122. }
  123. }
  124. }
  125. #pragma warning restore
  126. #endif