PkiArchiveControl.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cms;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Crmf;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Cms;
  8. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crmf
  9. {
  10. public class PkiArchiveControl
  11. : IControl
  12. {
  13. public static readonly int encryptedPrivKey = PkiArchiveOptions.encryptedPrivKey;
  14. public static readonly int keyGenParameters = PkiArchiveOptions.keyGenParameters;
  15. public static readonly int archiveRemGenPrivKey = PkiArchiveOptions.archiveRemGenPrivKey;
  16. private static readonly DerObjectIdentifier type = CrmfObjectIdentifiers.id_regCtrl_pkiArchiveOptions;
  17. private readonly PkiArchiveOptions pkiArchiveOptions;
  18. /// <summary>
  19. /// Basic constructor - build from an PKIArchiveOptions structure.
  20. /// </summary>
  21. /// <param name="pkiArchiveOptions">the ASN.1 structure that will underlie this control.</param>
  22. public PkiArchiveControl(PkiArchiveOptions pkiArchiveOptions)
  23. {
  24. this.pkiArchiveOptions = pkiArchiveOptions;
  25. }
  26. /// <summary>
  27. /// Return the type of this control.
  28. /// </summary>
  29. /// <returns>CRMFObjectIdentifiers.id_regCtrl_pkiArchiveOptions</returns>
  30. public DerObjectIdentifier Type
  31. {
  32. get { return type; }
  33. }
  34. /// <summary>
  35. /// Return the underlying ASN.1 object.
  36. /// </summary>
  37. /// <returns>a PKIArchiveOptions structure.</returns>
  38. public Asn1Encodable Value
  39. {
  40. get { return pkiArchiveOptions; }
  41. }
  42. /// <summary>
  43. /// Return the archive control type, one of: encryptedPrivKey,keyGenParameters,or archiveRemGenPrivKey.
  44. /// </summary>
  45. /// <returns>the archive control type.</returns>
  46. public int ArchiveType
  47. {
  48. get { return pkiArchiveOptions.Type; }
  49. }
  50. /// <summary>
  51. /// Return whether this control contains enveloped data.
  52. /// </summary>
  53. /// <returns>true if the control contains enveloped data, false otherwise.</returns>
  54. public bool EnvelopedData
  55. {
  56. get
  57. {
  58. EncryptedKey encKey = EncryptedKey.GetInstance(pkiArchiveOptions.Value);
  59. return !encKey.IsEncryptedValue;
  60. }
  61. }
  62. /// <summary>
  63. /// Return the enveloped data structure contained in this control.
  64. /// </summary>
  65. /// <returns>a CMSEnvelopedData object.</returns>
  66. public CmsEnvelopedData GetEnvelopedData()
  67. {
  68. try
  69. {
  70. EncryptedKey encKey = EncryptedKey.GetInstance(pkiArchiveOptions.Value);
  71. EnvelopedData data = BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cms.EnvelopedData.GetInstance(encKey.Value);
  72. return new CmsEnvelopedData(new ContentInfo(CmsObjectIdentifiers.EnvelopedData, data));
  73. }
  74. catch (CmsException e)
  75. {
  76. throw new CrmfException("CMS parsing error: " + e.Message, e);
  77. }
  78. catch (Exception e)
  79. {
  80. throw new CrmfException("CRMF parsing error: " + e.Message, e);
  81. }
  82. }
  83. }
  84. }
  85. #pragma warning restore
  86. #endif