ObjectDigestInfo.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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.X509
  6. {
  7. /**
  8. * ObjectDigestInfo ASN.1 structure used in v2 attribute certificates.
  9. *
  10. * <pre>
  11. *
  12. * ObjectDigestInfo ::= SEQUENCE {
  13. * digestedObjectType ENUMERATED {
  14. * publicKey (0),
  15. * publicKeyCert (1),
  16. * otherObjectTypes (2) },
  17. * -- otherObjectTypes MUST NOT
  18. * -- be used in this profile
  19. * otherObjectTypeID OBJECT IDENTIFIER OPTIONAL,
  20. * digestAlgorithm AlgorithmIdentifier,
  21. * objectDigest BIT STRING
  22. * }
  23. *
  24. * </pre>
  25. *
  26. */
  27. public class ObjectDigestInfo
  28. : Asn1Encodable
  29. {
  30. /**
  31. * The public key is hashed.
  32. */
  33. public const int PublicKey = 0;
  34. /**
  35. * The public key certificate is hashed.
  36. */
  37. public const int PublicKeyCert = 1;
  38. /**
  39. * An other object is hashed.
  40. */
  41. public const int OtherObjectDigest = 2;
  42. internal readonly DerEnumerated digestedObjectType;
  43. internal readonly DerObjectIdentifier otherObjectTypeID;
  44. internal readonly AlgorithmIdentifier digestAlgorithm;
  45. internal readonly DerBitString objectDigest;
  46. public static ObjectDigestInfo GetInstance(
  47. object obj)
  48. {
  49. if (obj == null || obj is ObjectDigestInfo)
  50. {
  51. return (ObjectDigestInfo) obj;
  52. }
  53. if (obj is Asn1Sequence)
  54. {
  55. return new ObjectDigestInfo((Asn1Sequence) obj);
  56. }
  57. throw new ArgumentException("unknown object in factory: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), "obj");
  58. }
  59. public static ObjectDigestInfo GetInstance(
  60. Asn1TaggedObject obj,
  61. bool isExplicit)
  62. {
  63. return GetInstance(Asn1Sequence.GetInstance(obj, isExplicit));
  64. }
  65. /**
  66. * Constructor from given details.
  67. * <p>
  68. * If <code>digestedObjectType</code> is not {@link #publicKeyCert} or
  69. * {@link #publicKey} <code>otherObjectTypeID</code> must be given,
  70. * otherwise it is ignored.</p>
  71. *
  72. * @param digestedObjectType The digest object type.
  73. * @param otherObjectTypeID The object type ID for
  74. * <code>otherObjectDigest</code>.
  75. * @param digestAlgorithm The algorithm identifier for the hash.
  76. * @param objectDigest The hash value.
  77. */
  78. public ObjectDigestInfo(
  79. int digestedObjectType,
  80. string otherObjectTypeID,
  81. AlgorithmIdentifier digestAlgorithm,
  82. byte[] objectDigest)
  83. {
  84. this.digestedObjectType = new DerEnumerated(digestedObjectType);
  85. if (digestedObjectType == OtherObjectDigest)
  86. {
  87. this.otherObjectTypeID = new DerObjectIdentifier(otherObjectTypeID);
  88. }
  89. this.digestAlgorithm = digestAlgorithm;
  90. this.objectDigest = new DerBitString(objectDigest);
  91. }
  92. private ObjectDigestInfo(
  93. Asn1Sequence seq)
  94. {
  95. if (seq.Count > 4 || seq.Count < 3)
  96. {
  97. throw new ArgumentException("Bad sequence size: " + seq.Count);
  98. }
  99. digestedObjectType = DerEnumerated.GetInstance(seq[0]);
  100. int offset = 0;
  101. if (seq.Count == 4)
  102. {
  103. otherObjectTypeID = DerObjectIdentifier.GetInstance(seq[1]);
  104. offset++;
  105. }
  106. digestAlgorithm = AlgorithmIdentifier.GetInstance(seq[1 + offset]);
  107. objectDigest = DerBitString.GetInstance(seq[2 + offset]);
  108. }
  109. public DerEnumerated DigestedObjectType
  110. {
  111. get { return digestedObjectType; }
  112. }
  113. public DerObjectIdentifier OtherObjectTypeID
  114. {
  115. get { return otherObjectTypeID; }
  116. }
  117. public AlgorithmIdentifier DigestAlgorithm
  118. {
  119. get { return digestAlgorithm; }
  120. }
  121. public DerBitString ObjectDigest
  122. {
  123. get { return objectDigest; }
  124. }
  125. /**
  126. * Produce an object suitable for an Asn1OutputStream.
  127. *
  128. * <pre>
  129. *
  130. * ObjectDigestInfo ::= SEQUENCE {
  131. * digestedObjectType ENUMERATED {
  132. * publicKey (0),
  133. * publicKeyCert (1),
  134. * otherObjectTypes (2) },
  135. * -- otherObjectTypes MUST NOT
  136. * -- be used in this profile
  137. * otherObjectTypeID OBJECT IDENTIFIER OPTIONAL,
  138. * digestAlgorithm AlgorithmIdentifier,
  139. * objectDigest BIT STRING
  140. * }
  141. *
  142. * </pre>
  143. */
  144. public override Asn1Object ToAsn1Object()
  145. {
  146. Asn1EncodableVector v = new Asn1EncodableVector(digestedObjectType);
  147. v.AddOptional(otherObjectTypeID);
  148. v.Add(digestAlgorithm, objectDigest);
  149. return new DerSequence(v);
  150. }
  151. }
  152. }
  153. #pragma warning restore
  154. #endif