KeyAgreeRecipientInfo.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cms
  7. {
  8. public class KeyAgreeRecipientInfo
  9. : Asn1Encodable
  10. {
  11. private DerInteger version;
  12. private OriginatorIdentifierOrKey originator;
  13. private Asn1OctetString ukm;
  14. private AlgorithmIdentifier keyEncryptionAlgorithm;
  15. private Asn1Sequence recipientEncryptedKeys;
  16. public KeyAgreeRecipientInfo(
  17. OriginatorIdentifierOrKey originator,
  18. Asn1OctetString ukm,
  19. AlgorithmIdentifier keyEncryptionAlgorithm,
  20. Asn1Sequence recipientEncryptedKeys)
  21. {
  22. this.version = new DerInteger(3);
  23. this.originator = originator;
  24. this.ukm = ukm;
  25. this.keyEncryptionAlgorithm = keyEncryptionAlgorithm;
  26. this.recipientEncryptedKeys = recipientEncryptedKeys;
  27. }
  28. public KeyAgreeRecipientInfo(
  29. Asn1Sequence seq)
  30. {
  31. int index = 0;
  32. version = (DerInteger) seq[index++];
  33. originator = OriginatorIdentifierOrKey.GetInstance(
  34. (Asn1TaggedObject) seq[index++], true);
  35. if (seq[index] is Asn1TaggedObject)
  36. {
  37. ukm = Asn1OctetString.GetInstance(
  38. (Asn1TaggedObject) seq[index++], true);
  39. }
  40. keyEncryptionAlgorithm = AlgorithmIdentifier.GetInstance(
  41. seq[index++]);
  42. recipientEncryptedKeys = (Asn1Sequence) seq[index++];
  43. }
  44. /**
  45. * return a KeyAgreeRecipientInfo object from a tagged object.
  46. *
  47. * @param obj the tagged object holding the object we want.
  48. * @param explicitly true if the object is meant to be explicitly
  49. * tagged false otherwise.
  50. * @exception ArgumentException if the object held by the
  51. * tagged object cannot be converted.
  52. */
  53. public static KeyAgreeRecipientInfo GetInstance(
  54. Asn1TaggedObject obj,
  55. bool explicitly)
  56. {
  57. return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
  58. }
  59. /**
  60. * return a KeyAgreeRecipientInfo object from the given object.
  61. *
  62. * @param obj the object we want converted.
  63. * @exception ArgumentException if the object cannot be converted.
  64. */
  65. public static KeyAgreeRecipientInfo GetInstance(
  66. object obj)
  67. {
  68. if (obj == null || obj is KeyAgreeRecipientInfo)
  69. return (KeyAgreeRecipientInfo)obj;
  70. if (obj is Asn1Sequence)
  71. return new KeyAgreeRecipientInfo((Asn1Sequence)obj);
  72. throw new ArgumentException(
  73. "Illegal object in KeyAgreeRecipientInfo: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  74. }
  75. public DerInteger Version
  76. {
  77. get { return version; }
  78. }
  79. public OriginatorIdentifierOrKey Originator
  80. {
  81. get { return originator; }
  82. }
  83. public Asn1OctetString UserKeyingMaterial
  84. {
  85. get { return ukm; }
  86. }
  87. public AlgorithmIdentifier KeyEncryptionAlgorithm
  88. {
  89. get { return keyEncryptionAlgorithm; }
  90. }
  91. public Asn1Sequence RecipientEncryptedKeys
  92. {
  93. get { return recipientEncryptedKeys; }
  94. }
  95. /**
  96. * Produce an object suitable for an Asn1OutputStream.
  97. * <pre>
  98. * KeyAgreeRecipientInfo ::= Sequence {
  99. * version CMSVersion, -- always set to 3
  100. * originator [0] EXPLICIT OriginatorIdentifierOrKey,
  101. * ukm [1] EXPLICIT UserKeyingMaterial OPTIONAL,
  102. * keyEncryptionAlgorithm KeyEncryptionAlgorithmIdentifier,
  103. * recipientEncryptedKeys RecipientEncryptedKeys
  104. * }
  105. *
  106. * UserKeyingMaterial ::= OCTET STRING
  107. * </pre>
  108. */
  109. public override Asn1Object ToAsn1Object()
  110. {
  111. Asn1EncodableVector v = new Asn1EncodableVector(version, new DerTaggedObject(true, 0, originator));
  112. v.AddOptionalTagged(true, 1, ukm);
  113. v.Add(keyEncryptionAlgorithm, recipientEncryptedKeys);
  114. return new DerSequence(v);
  115. }
  116. }
  117. }
  118. #pragma warning restore
  119. #endif