EncryptedValue.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Crmf
  6. {
  7. public class EncryptedValue
  8. : Asn1Encodable
  9. {
  10. private readonly AlgorithmIdentifier intendedAlg;
  11. private readonly AlgorithmIdentifier symmAlg;
  12. private readonly DerBitString encSymmKey;
  13. private readonly AlgorithmIdentifier keyAlg;
  14. private readonly Asn1OctetString valueHint;
  15. private readonly DerBitString encValue;
  16. private EncryptedValue(Asn1Sequence seq)
  17. {
  18. int index = 0;
  19. while (seq[index] is Asn1TaggedObject)
  20. {
  21. Asn1TaggedObject tObj = (Asn1TaggedObject)seq[index];
  22. switch (tObj.TagNo)
  23. {
  24. case 0:
  25. intendedAlg = AlgorithmIdentifier.GetInstance(tObj, false);
  26. break;
  27. case 1:
  28. symmAlg = AlgorithmIdentifier.GetInstance(tObj, false);
  29. break;
  30. case 2:
  31. encSymmKey = DerBitString.GetInstance(tObj, false);
  32. break;
  33. case 3:
  34. keyAlg = AlgorithmIdentifier.GetInstance(tObj, false);
  35. break;
  36. case 4:
  37. valueHint = Asn1OctetString.GetInstance(tObj, false);
  38. break;
  39. }
  40. ++index;
  41. }
  42. encValue = DerBitString.GetInstance(seq[index]);
  43. }
  44. public static EncryptedValue GetInstance(object obj)
  45. {
  46. if (obj is EncryptedValue)
  47. return (EncryptedValue)obj;
  48. if (obj != null)
  49. return new EncryptedValue(Asn1Sequence.GetInstance(obj));
  50. return null;
  51. }
  52. public EncryptedValue(
  53. AlgorithmIdentifier intendedAlg,
  54. AlgorithmIdentifier symmAlg,
  55. DerBitString encSymmKey,
  56. AlgorithmIdentifier keyAlg,
  57. Asn1OctetString valueHint,
  58. DerBitString encValue)
  59. {
  60. if (encValue == null)
  61. {
  62. throw new ArgumentNullException("encValue");
  63. }
  64. this.intendedAlg = intendedAlg;
  65. this.symmAlg = symmAlg;
  66. this.encSymmKey = encSymmKey;
  67. this.keyAlg = keyAlg;
  68. this.valueHint = valueHint;
  69. this.encValue = encValue;
  70. }
  71. public virtual AlgorithmIdentifier IntendedAlg
  72. {
  73. get { return intendedAlg; }
  74. }
  75. public virtual AlgorithmIdentifier SymmAlg
  76. {
  77. get { return symmAlg; }
  78. }
  79. public virtual DerBitString EncSymmKey
  80. {
  81. get { return encSymmKey; }
  82. }
  83. public virtual AlgorithmIdentifier KeyAlg
  84. {
  85. get { return keyAlg; }
  86. }
  87. public virtual Asn1OctetString ValueHint
  88. {
  89. get { return valueHint; }
  90. }
  91. public virtual DerBitString EncValue
  92. {
  93. get { return encValue; }
  94. }
  95. /**
  96. * <pre>
  97. * EncryptedValue ::= SEQUENCE {
  98. * intendedAlg [0] AlgorithmIdentifier OPTIONAL,
  99. * -- the intended algorithm for which the value will be used
  100. * symmAlg [1] AlgorithmIdentifier OPTIONAL,
  101. * -- the symmetric algorithm used to encrypt the value
  102. * encSymmKey [2] BIT STRING OPTIONAL,
  103. * -- the (encrypted) symmetric key used to encrypt the value
  104. * keyAlg [3] AlgorithmIdentifier OPTIONAL,
  105. * -- algorithm used to encrypt the symmetric key
  106. * valueHint [4] OCTET STRING OPTIONAL,
  107. * -- a brief description or identifier of the encValue content
  108. * -- (may be meaningful only to the sending entity, and used only
  109. * -- if EncryptedValue might be re-examined by the sending entity
  110. * -- in the future)
  111. * encValue BIT STRING }
  112. * -- the encrypted value itself
  113. * </pre>
  114. * @return a basic ASN.1 object representation.
  115. */
  116. public override Asn1Object ToAsn1Object()
  117. {
  118. Asn1EncodableVector v = new Asn1EncodableVector();
  119. v.AddOptionalTagged(false, 0, intendedAlg);
  120. v.AddOptionalTagged(false, 1, symmAlg);
  121. v.AddOptionalTagged(false, 2, encSymmKey);
  122. v.AddOptionalTagged(false, 3, keyAlg);
  123. v.AddOptionalTagged(false, 4, valueHint);
  124. v.Add(encValue);
  125. return new DerSequence(v);
  126. }
  127. }
  128. }
  129. #pragma warning restore
  130. #endif