PublicKeyPacket.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.Utilities.Date;
  6. namespace BestHTTP.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 ((PublicKeyAlgorithmTag) 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. default:
  49. throw new IOException("unknown PGP public key algorithm encountered");
  50. }
  51. }
  52. /// <summary>Construct a version 4 public key packet.</summary>
  53. public PublicKeyPacket(
  54. PublicKeyAlgorithmTag algorithm,
  55. DateTime time,
  56. IBcpgKey key)
  57. {
  58. this.version = 4;
  59. this.time = DateTimeUtilities.DateTimeToUnixMs(time) / 1000L;
  60. this.algorithm = algorithm;
  61. this.key = key;
  62. }
  63. public virtual int Version
  64. {
  65. get { return version; }
  66. }
  67. public virtual PublicKeyAlgorithmTag Algorithm
  68. {
  69. get { return algorithm; }
  70. }
  71. public virtual int ValidDays
  72. {
  73. get { return validDays; }
  74. }
  75. public virtual DateTime GetTime()
  76. {
  77. return DateTimeUtilities.UnixMsToDateTime(time * 1000L);
  78. }
  79. public virtual IBcpgKey Key
  80. {
  81. get { return key; }
  82. }
  83. public virtual byte[] GetEncodedContents()
  84. {
  85. MemoryStream bOut = new MemoryStream();
  86. BcpgOutputStream pOut = new BcpgOutputStream(bOut);
  87. pOut.WriteByte((byte) version);
  88. pOut.WriteInt((int) time);
  89. if (version <= 3)
  90. {
  91. pOut.WriteShort((short) validDays);
  92. }
  93. pOut.WriteByte((byte) algorithm);
  94. pOut.WriteObject((BcpgObject)key);
  95. return bOut.ToArray();
  96. }
  97. public override void Encode(
  98. BcpgOutputStream bcpgOut)
  99. {
  100. bcpgOut.WritePacket(PacketTag.PublicKey, GetEncodedContents(), true);
  101. }
  102. }
  103. }
  104. #pragma warning restore
  105. #endif