EncryptedValue.cs 4.6 KB

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