PublicKeyEncSessionPacket.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.Math;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO;
  8. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Bcpg
  9. {
  10. /// <remarks>Basic packet for a PGP public key.</remarks>
  11. public class PublicKeyEncSessionPacket
  12. : ContainedPacket //, PublicKeyAlgorithmTag
  13. {
  14. private int version;
  15. private long keyId;
  16. private PublicKeyAlgorithmTag algorithm;
  17. private byte[][] data;
  18. internal PublicKeyEncSessionPacket(
  19. BcpgInputStream bcpgIn)
  20. {
  21. version = bcpgIn.ReadByte();
  22. keyId |= (long)bcpgIn.ReadByte() << 56;
  23. keyId |= (long)bcpgIn.ReadByte() << 48;
  24. keyId |= (long)bcpgIn.ReadByte() << 40;
  25. keyId |= (long)bcpgIn.ReadByte() << 32;
  26. keyId |= (long)bcpgIn.ReadByte() << 24;
  27. keyId |= (long)bcpgIn.ReadByte() << 16;
  28. keyId |= (long)bcpgIn.ReadByte() << 8;
  29. keyId |= (uint)bcpgIn.ReadByte();
  30. algorithm = (PublicKeyAlgorithmTag) bcpgIn.ReadByte();
  31. switch ((PublicKeyAlgorithmTag) algorithm)
  32. {
  33. case PublicKeyAlgorithmTag.RsaEncrypt:
  34. case PublicKeyAlgorithmTag.RsaGeneral:
  35. data = new byte[][]{ new MPInteger(bcpgIn).GetEncoded() };
  36. break;
  37. case PublicKeyAlgorithmTag.ElGamalEncrypt:
  38. case PublicKeyAlgorithmTag.ElGamalGeneral:
  39. MPInteger p = new MPInteger(bcpgIn);
  40. MPInteger g = new MPInteger(bcpgIn);
  41. data = new byte[][]{
  42. p.GetEncoded(),
  43. g.GetEncoded(),
  44. };
  45. break;
  46. case PublicKeyAlgorithmTag.ECDH:
  47. data = new byte[][]{ Streams.ReadAll(bcpgIn) };
  48. break;
  49. default:
  50. throw new IOException("unknown PGP public key algorithm encountered");
  51. }
  52. }
  53. public PublicKeyEncSessionPacket(
  54. long keyId,
  55. PublicKeyAlgorithmTag algorithm,
  56. byte[][] data)
  57. {
  58. this.version = 3;
  59. this.keyId = keyId;
  60. this.algorithm = algorithm;
  61. this.data = new byte[data.Length][];
  62. for (int i = 0; i < data.Length; ++i)
  63. {
  64. this.data[i] = Arrays.Clone(data[i]);
  65. }
  66. }
  67. public int Version
  68. {
  69. get { return version; }
  70. }
  71. public long KeyId
  72. {
  73. get { return keyId; }
  74. }
  75. public PublicKeyAlgorithmTag Algorithm
  76. {
  77. get { return algorithm; }
  78. }
  79. public byte[][] GetEncSessionKey()
  80. {
  81. return data;
  82. }
  83. public override void Encode(
  84. BcpgOutputStream bcpgOut)
  85. {
  86. MemoryStream bOut = new MemoryStream();
  87. BcpgOutputStream pOut = new BcpgOutputStream(bOut);
  88. pOut.WriteByte((byte) version);
  89. pOut.WriteLong(keyId);
  90. pOut.WriteByte((byte)algorithm);
  91. for (int i = 0; i < data.Length; ++i)
  92. {
  93. pOut.Write(data[i]);
  94. }
  95. BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(pOut);
  96. bcpgOut.WritePacket(PacketTag.PublicKeyEncryptedSession , bOut.ToArray(), true);
  97. }
  98. }
  99. }
  100. #pragma warning restore
  101. #endif