SMIMECapabilities.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Nist;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  10. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Smime
  11. {
  12. /**
  13. * Handler class for dealing with S/MIME Capabilities
  14. */
  15. public class SmimeCapabilities
  16. : Asn1Encodable
  17. {
  18. /**
  19. * general preferences
  20. */
  21. public static readonly DerObjectIdentifier PreferSignedData = PkcsObjectIdentifiers.PreferSignedData;
  22. public static readonly DerObjectIdentifier CannotDecryptAny = PkcsObjectIdentifiers.CannotDecryptAny;
  23. public static readonly DerObjectIdentifier SmimeCapabilitesVersions = PkcsObjectIdentifiers.SmimeCapabilitiesVersions;
  24. /**
  25. * encryption algorithms preferences
  26. */
  27. public static readonly DerObjectIdentifier Aes256Cbc = NistObjectIdentifiers.IdAes256Cbc;
  28. public static readonly DerObjectIdentifier Aes192Cbc = NistObjectIdentifiers.IdAes192Cbc;
  29. public static readonly DerObjectIdentifier Aes128Cbc = NistObjectIdentifiers.IdAes128Cbc;
  30. public static readonly DerObjectIdentifier IdeaCbc = new DerObjectIdentifier("1.3.6.1.4.1.188.7.1.1.2");
  31. public static readonly DerObjectIdentifier Cast5Cbc = new DerObjectIdentifier("1.2.840.113533.7.66.10");
  32. public static readonly DerObjectIdentifier DesCbc = new DerObjectIdentifier("1.3.14.3.2.7");
  33. public static readonly DerObjectIdentifier DesEde3Cbc = PkcsObjectIdentifiers.DesEde3Cbc;
  34. public static readonly DerObjectIdentifier RC2Cbc = PkcsObjectIdentifiers.RC2Cbc;
  35. private Asn1Sequence capabilities;
  36. /**
  37. * return an Attr object from the given object.
  38. *
  39. * @param o the object we want converted.
  40. * @exception ArgumentException if the object cannot be converted.
  41. */
  42. public static SmimeCapabilities GetInstance(
  43. object obj)
  44. {
  45. if (obj == null || obj is SmimeCapabilities)
  46. {
  47. return (SmimeCapabilities) obj;
  48. }
  49. if (obj is Asn1Sequence)
  50. {
  51. return new SmimeCapabilities((Asn1Sequence) obj);
  52. }
  53. if (obj is AttributeX509)
  54. {
  55. return new SmimeCapabilities(
  56. (Asn1Sequence)(((AttributeX509) obj).AttrValues[0]));
  57. }
  58. throw new ArgumentException("unknown object in factory: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), "obj");
  59. }
  60. public SmimeCapabilities(
  61. Asn1Sequence seq)
  62. {
  63. capabilities = seq;
  64. }
  65. #if !(SILVERLIGHT || PORTABLE || NETFX_CORE)
  66. public ArrayList GetCapabilities(
  67. DerObjectIdentifier capability)
  68. {
  69. ArrayList list = new ArrayList();
  70. DoGetCapabilitiesForOid(capability, list);
  71. return list;
  72. }
  73. #endif
  74. /**
  75. * returns an ArrayList with 0 or more objects of all the capabilities
  76. * matching the passed in capability Oid. If the Oid passed is null the
  77. * entire set is returned.
  78. */
  79. public IList GetCapabilitiesForOid(
  80. DerObjectIdentifier capability)
  81. {
  82. IList list = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  83. DoGetCapabilitiesForOid(capability, list);
  84. return list;
  85. }
  86. private void DoGetCapabilitiesForOid(DerObjectIdentifier capability, IList list)
  87. {
  88. if (capability == null)
  89. {
  90. foreach (object o in capabilities)
  91. {
  92. SmimeCapability cap = SmimeCapability.GetInstance(o);
  93. list.Add(cap);
  94. }
  95. }
  96. else
  97. {
  98. foreach (object o in capabilities)
  99. {
  100. SmimeCapability cap = SmimeCapability.GetInstance(o);
  101. if (capability.Equals(cap.CapabilityID))
  102. {
  103. list.Add(cap);
  104. }
  105. }
  106. }
  107. }
  108. /**
  109. * Produce an object suitable for an Asn1OutputStream.
  110. * <pre>
  111. * SMIMECapabilities ::= Sequence OF SMIMECapability
  112. * </pre>
  113. */
  114. public override Asn1Object ToAsn1Object()
  115. {
  116. return capabilities;
  117. }
  118. }
  119. }
  120. #pragma warning restore
  121. #endif