CMSAuthEnvelopedData.cs 3.2 KB

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