KeyTransRecipientInformation.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cms;
  7. using Asn1Pkcs = BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs;
  8. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  9. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  10. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  11. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  12. using BestHTTP.SecureProtocol.Org.BouncyCastle.X509;
  13. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Cms
  14. {
  15. /**
  16. * the KeyTransRecipientInformation class for a recipient who has been sent a secret
  17. * key encrypted using their public key that needs to be used to
  18. * extract the message.
  19. */
  20. public class KeyTransRecipientInformation
  21. : RecipientInformation
  22. {
  23. private KeyTransRecipientInfo info;
  24. internal KeyTransRecipientInformation(
  25. KeyTransRecipientInfo info,
  26. CmsSecureReadable secureReadable)
  27. : base(info.KeyEncryptionAlgorithm, secureReadable)
  28. {
  29. this.info = info;
  30. this.rid = new RecipientID();
  31. RecipientIdentifier r = info.RecipientIdentifier;
  32. try
  33. {
  34. if (r.IsTagged)
  35. {
  36. Asn1OctetString octs = Asn1OctetString.GetInstance(r.ID);
  37. rid.SubjectKeyIdentifier = octs.GetOctets();
  38. }
  39. else
  40. {
  41. IssuerAndSerialNumber iAnds = IssuerAndSerialNumber.GetInstance(r.ID);
  42. rid.Issuer = iAnds.Name;
  43. rid.SerialNumber = iAnds.SerialNumber.Value;
  44. }
  45. }
  46. catch (IOException)
  47. {
  48. throw new ArgumentException("invalid rid in KeyTransRecipientInformation");
  49. }
  50. }
  51. private string GetExchangeEncryptionAlgorithmName(
  52. DerObjectIdentifier oid)
  53. {
  54. if (Asn1Pkcs.PkcsObjectIdentifiers.RsaEncryption.Equals(oid))
  55. {
  56. return "RSA//PKCS1Padding";
  57. }
  58. return oid.Id;
  59. }
  60. internal KeyParameter UnwrapKey(ICipherParameters key)
  61. {
  62. byte[] encryptedKey = info.EncryptedKey.GetOctets();
  63. string keyExchangeAlgorithm = GetExchangeEncryptionAlgorithmName(keyEncAlg.Algorithm);
  64. try
  65. {
  66. IWrapper keyWrapper = WrapperUtilities.GetWrapper(keyExchangeAlgorithm);
  67. keyWrapper.Init(false, key);
  68. // FIXME Support for MAC algorithm parameters similar to cipher parameters
  69. return ParameterUtilities.CreateKeyParameter(
  70. GetContentAlgorithmName(), keyWrapper.Unwrap(encryptedKey, 0, encryptedKey.Length));
  71. }
  72. catch (SecurityUtilityException e)
  73. {
  74. throw new CmsException("couldn't create cipher.", e);
  75. }
  76. catch (InvalidKeyException e)
  77. {
  78. throw new CmsException("key invalid in message.", e);
  79. }
  80. // catch (IllegalBlockSizeException e)
  81. catch (DataLengthException e)
  82. {
  83. throw new CmsException("illegal blocksize in message.", e);
  84. }
  85. // catch (BadPaddingException e)
  86. catch (InvalidCipherTextException e)
  87. {
  88. throw new CmsException("bad padding in message.", e);
  89. }
  90. }
  91. /**
  92. * decrypt the content and return it as a byte array.
  93. */
  94. public override CmsTypedStream GetContentStream(
  95. ICipherParameters key)
  96. {
  97. KeyParameter sKey = UnwrapKey(key);
  98. return GetContentFromSessionKey(sKey);
  99. }
  100. }
  101. }
  102. #pragma warning restore
  103. #endif