KeyTransRecipientInformation.cs 4.3 KB

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