PublicKeyPacket.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Date;
  6. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Bcpg
  7. {
  8. /// <remarks>Basic packet for a PGP public key.</remarks>
  9. public class PublicKeyPacket
  10. : ContainedPacket //, PublicKeyAlgorithmTag
  11. {
  12. private int version;
  13. private long time;
  14. private int validDays;
  15. private PublicKeyAlgorithmTag algorithm;
  16. private IBcpgKey key;
  17. internal PublicKeyPacket(
  18. BcpgInputStream bcpgIn)
  19. {
  20. version = bcpgIn.ReadByte();
  21. time = ((uint)bcpgIn.ReadByte() << 24) | ((uint)bcpgIn.ReadByte() << 16)
  22. | ((uint)bcpgIn.ReadByte() << 8) | (uint)bcpgIn.ReadByte();
  23. if (version <= 3)
  24. {
  25. validDays = (bcpgIn.ReadByte() << 8) | bcpgIn.ReadByte();
  26. }
  27. algorithm = (PublicKeyAlgorithmTag)bcpgIn.ReadByte();
  28. switch (algorithm)
  29. {
  30. case PublicKeyAlgorithmTag.RsaEncrypt:
  31. case PublicKeyAlgorithmTag.RsaGeneral:
  32. case PublicKeyAlgorithmTag.RsaSign:
  33. key = new RsaPublicBcpgKey(bcpgIn);
  34. break;
  35. case PublicKeyAlgorithmTag.Dsa:
  36. key = new DsaPublicBcpgKey(bcpgIn);
  37. break;
  38. case PublicKeyAlgorithmTag.ElGamalEncrypt:
  39. case PublicKeyAlgorithmTag.ElGamalGeneral:
  40. key = new ElGamalPublicBcpgKey(bcpgIn);
  41. break;
  42. case PublicKeyAlgorithmTag.ECDH:
  43. key = new ECDHPublicBcpgKey(bcpgIn);
  44. break;
  45. case PublicKeyAlgorithmTag.ECDsa:
  46. key = new ECDsaPublicBcpgKey(bcpgIn);
  47. break;
  48. case PublicKeyAlgorithmTag.EdDsa:
  49. key = new EdDsaPublicBcpgKey(bcpgIn);
  50. break;
  51. default:
  52. throw new IOException("unknown PGP public key algorithm encountered");
  53. }
  54. }
  55. /// <summary>Construct a version 4 public key packet.</summary>
  56. public PublicKeyPacket(
  57. PublicKeyAlgorithmTag algorithm,
  58. DateTime time,
  59. IBcpgKey key)
  60. {
  61. this.version = 4;
  62. this.time = DateTimeUtilities.DateTimeToUnixMs(time) / 1000L;
  63. this.algorithm = algorithm;
  64. this.key = key;
  65. }
  66. public virtual int Version
  67. {
  68. get { return version; }
  69. }
  70. public virtual PublicKeyAlgorithmTag Algorithm
  71. {
  72. get { return algorithm; }
  73. }
  74. public virtual int ValidDays
  75. {
  76. get { return validDays; }
  77. }
  78. public virtual DateTime GetTime()
  79. {
  80. return DateTimeUtilities.UnixMsToDateTime(time * 1000L);
  81. }
  82. public virtual IBcpgKey Key
  83. {
  84. get { return key; }
  85. }
  86. public virtual byte[] GetEncodedContents()
  87. {
  88. MemoryStream bOut = new MemoryStream();
  89. BcpgOutputStream pOut = new BcpgOutputStream(bOut);
  90. pOut.WriteByte((byte) version);
  91. pOut.WriteInt((int) time);
  92. if (version <= 3)
  93. {
  94. pOut.WriteShort((short) validDays);
  95. }
  96. pOut.WriteByte((byte) algorithm);
  97. pOut.WriteObject((BcpgObject)key);
  98. return bOut.ToArray();
  99. }
  100. public override void Encode(
  101. BcpgOutputStream bcpgOut)
  102. {
  103. bcpgOut.WritePacket(PacketTag.PublicKey, GetEncodedContents(), true);
  104. }
  105. }
  106. }
  107. #pragma warning restore
  108. #endif