KEKRecipientInfoGenerator.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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.Kisa;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Nist;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Ntt;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  11. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  12. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  13. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  14. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  15. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Cms
  16. {
  17. internal class KekRecipientInfoGenerator : RecipientInfoGenerator
  18. {
  19. private static readonly CmsEnvelopedHelper Helper = CmsEnvelopedHelper.Instance;
  20. private KeyParameter keyEncryptionKey;
  21. // TODO Can get this from keyEncryptionKey?
  22. private string keyEncryptionKeyOID;
  23. private KekIdentifier kekIdentifier;
  24. // Derived
  25. private AlgorithmIdentifier keyEncryptionAlgorithm;
  26. internal KekRecipientInfoGenerator()
  27. {
  28. }
  29. internal KekIdentifier KekIdentifier
  30. {
  31. set { this.kekIdentifier = value; }
  32. }
  33. internal KeyParameter KeyEncryptionKey
  34. {
  35. set
  36. {
  37. this.keyEncryptionKey = value;
  38. this.keyEncryptionAlgorithm = DetermineKeyEncAlg(keyEncryptionKeyOID, keyEncryptionKey);
  39. }
  40. }
  41. internal string KeyEncryptionKeyOID
  42. {
  43. set { this.keyEncryptionKeyOID = value; }
  44. }
  45. public RecipientInfo Generate(KeyParameter contentEncryptionKey, SecureRandom random)
  46. {
  47. byte[] keyBytes = contentEncryptionKey.GetKey();
  48. IWrapper keyWrapper = Helper.CreateWrapper(keyEncryptionAlgorithm.Algorithm.Id);
  49. keyWrapper.Init(true, new ParametersWithRandom(keyEncryptionKey, random));
  50. Asn1OctetString encryptedKey = new DerOctetString(
  51. keyWrapper.Wrap(keyBytes, 0, keyBytes.Length));
  52. return new RecipientInfo(new KekRecipientInfo(kekIdentifier, keyEncryptionAlgorithm, encryptedKey));
  53. }
  54. private static AlgorithmIdentifier DetermineKeyEncAlg(
  55. string algorithm, KeyParameter key)
  56. {
  57. if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.StartsWith(algorithm, "DES"))
  58. {
  59. return new AlgorithmIdentifier(
  60. PkcsObjectIdentifiers.IdAlgCms3DesWrap,
  61. DerNull.Instance);
  62. }
  63. else if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.StartsWith(algorithm, "RC2"))
  64. {
  65. return new AlgorithmIdentifier(
  66. PkcsObjectIdentifiers.IdAlgCmsRC2Wrap,
  67. new DerInteger(58));
  68. }
  69. else if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.StartsWith(algorithm, "AES"))
  70. {
  71. int length = key.GetKey().Length * 8;
  72. DerObjectIdentifier wrapOid;
  73. if (length == 128)
  74. {
  75. wrapOid = NistObjectIdentifiers.IdAes128Wrap;
  76. }
  77. else if (length == 192)
  78. {
  79. wrapOid = NistObjectIdentifiers.IdAes192Wrap;
  80. }
  81. else if (length == 256)
  82. {
  83. wrapOid = NistObjectIdentifiers.IdAes256Wrap;
  84. }
  85. else
  86. {
  87. throw new ArgumentException("illegal keysize in AES");
  88. }
  89. return new AlgorithmIdentifier(wrapOid); // parameters absent
  90. }
  91. else if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.StartsWith(algorithm, "SEED"))
  92. {
  93. // parameters absent
  94. return new AlgorithmIdentifier(KisaObjectIdentifiers.IdNpkiAppCmsSeedWrap);
  95. }
  96. else if (BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.StartsWith(algorithm, "CAMELLIA"))
  97. {
  98. int length = key.GetKey().Length * 8;
  99. DerObjectIdentifier wrapOid;
  100. if (length == 128)
  101. {
  102. wrapOid = NttObjectIdentifiers.IdCamellia128Wrap;
  103. }
  104. else if (length == 192)
  105. {
  106. wrapOid = NttObjectIdentifiers.IdCamellia192Wrap;
  107. }
  108. else if (length == 256)
  109. {
  110. wrapOid = NttObjectIdentifiers.IdCamellia256Wrap;
  111. }
  112. else
  113. {
  114. throw new ArgumentException("illegal keysize in Camellia");
  115. }
  116. return new AlgorithmIdentifier(wrapOid); // parameters must be absent
  117. }
  118. else
  119. {
  120. throw new ArgumentException("unknown algorithm");
  121. }
  122. }
  123. }
  124. }
  125. #pragma warning restore
  126. #endif