CMSAuthEnvelopedData.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cms;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  8. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  9. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Cms
  10. {
  11. /**
  12. * containing class for an CMS AuthEnveloped Data object
  13. */
  14. internal class CmsAuthEnvelopedData
  15. {
  16. internal RecipientInformationStore recipientInfoStore;
  17. internal ContentInfo contentInfo;
  18. private OriginatorInfo originator;
  19. private AlgorithmIdentifier authEncAlg;
  20. private Asn1Set authAttrs;
  21. private byte[] mac;
  22. private Asn1Set unauthAttrs;
  23. public CmsAuthEnvelopedData(
  24. byte[] authEnvData)
  25. : this(CmsUtilities.ReadContentInfo(authEnvData))
  26. {
  27. }
  28. public CmsAuthEnvelopedData(
  29. Stream authEnvData)
  30. : this(CmsUtilities.ReadContentInfo(authEnvData))
  31. {
  32. }
  33. public CmsAuthEnvelopedData(
  34. ContentInfo contentInfo)
  35. {
  36. this.contentInfo = contentInfo;
  37. AuthEnvelopedData authEnvData = AuthEnvelopedData.GetInstance(contentInfo.Content);
  38. this.originator = authEnvData.OriginatorInfo;
  39. //
  40. // read the recipients
  41. //
  42. Asn1Set recipientInfos = authEnvData.RecipientInfos;
  43. //
  44. // read the auth-encrypted content info
  45. //
  46. EncryptedContentInfo authEncInfo = authEnvData.AuthEncryptedContentInfo;
  47. this.authEncAlg = authEncInfo.ContentEncryptionAlgorithm;
  48. CmsSecureReadable secureReadable = new AuthEnvelopedSecureReadable(this);
  49. //
  50. // build the RecipientInformationStore
  51. //
  52. this.recipientInfoStore = CmsEnvelopedHelper.BuildRecipientInformationStore(
  53. recipientInfos, secureReadable);
  54. // FIXME These need to be passed to the AEAD cipher as AAD (Additional Authenticated Data)
  55. this.authAttrs = authEnvData.AuthAttrs;
  56. this.mac = authEnvData.Mac.GetOctets();
  57. this.unauthAttrs = authEnvData.UnauthAttrs;
  58. }
  59. private class AuthEnvelopedSecureReadable : CmsSecureReadable
  60. {
  61. private readonly CmsAuthEnvelopedData parent;
  62. internal AuthEnvelopedSecureReadable(CmsAuthEnvelopedData parent)
  63. {
  64. this.parent = parent;
  65. }
  66. public AlgorithmIdentifier Algorithm
  67. {
  68. get { return parent.authEncAlg; }
  69. }
  70. public object CryptoObject
  71. {
  72. get { return null; }
  73. }
  74. public CmsReadable GetReadable(KeyParameter key)
  75. {
  76. // TODO Create AEAD cipher instance to decrypt and calculate tag ( MAC)
  77. throw new CmsException("AuthEnveloped data decryption not yet implemented");
  78. // RFC 5084 ASN.1 Module
  79. // -- Parameters for AlgorithmIdentifier
  80. //
  81. // CCMParameters ::= SEQUENCE {
  82. // aes-nonce OCTET STRING (SIZE(7..13)),
  83. // aes-ICVlen AES-CCM-ICVlen DEFAULT 12 }
  84. //
  85. // AES-CCM-ICVlen ::= INTEGER (4 | 6 | 8 | 10 | 12 | 14 | 16)
  86. //
  87. // GCMParameters ::= SEQUENCE {
  88. // aes-nonce OCTET STRING, -- recommended size is 12 octets
  89. // aes-ICVlen AES-GCM-ICVlen DEFAULT 12 }
  90. //
  91. // AES-GCM-ICVlen ::= INTEGER (12 | 13 | 14 | 15 | 16)
  92. }
  93. }
  94. }
  95. }
  96. #pragma warning restore
  97. #endif