ECPublicBCPGKey.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.Math;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math.EC;
  8. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Bcpg
  9. {
  10. /// <remarks>Base class for an EC Public Key.</remarks>
  11. public abstract class ECPublicBcpgKey
  12. : BcpgObject, IBcpgKey
  13. {
  14. internal DerObjectIdentifier oid;
  15. internal BigInteger point;
  16. /// <param name="bcpgIn">The stream to read the packet from.</param>
  17. protected ECPublicBcpgKey(
  18. BcpgInputStream bcpgIn)
  19. {
  20. this.oid = DerObjectIdentifier.GetInstance(Asn1Object.FromByteArray(ReadBytesOfEncodedLength(bcpgIn)));
  21. this.point = new MPInteger(bcpgIn).Value;
  22. }
  23. protected ECPublicBcpgKey(
  24. DerObjectIdentifier oid,
  25. ECPoint point)
  26. {
  27. this.point = new BigInteger(1, point.GetEncoded(false));
  28. this.oid = oid;
  29. }
  30. protected ECPublicBcpgKey(
  31. DerObjectIdentifier oid,
  32. BigInteger encodedPoint)
  33. {
  34. this.point = encodedPoint;
  35. this.oid = oid;
  36. }
  37. /// <summary>The format, as a string, always "PGP".</summary>
  38. public string Format
  39. {
  40. get { return "PGP"; }
  41. }
  42. /// <summary>Return the standard PGP encoding of the key.</summary>
  43. public override byte[] GetEncoded()
  44. {
  45. try
  46. {
  47. return base.GetEncoded();
  48. }
  49. catch (IOException)
  50. {
  51. return null;
  52. }
  53. }
  54. public override void Encode(
  55. BcpgOutputStream bcpgOut)
  56. {
  57. byte[] oid = this.oid.GetEncoded();
  58. bcpgOut.Write(oid, 1, oid.Length - 1);
  59. MPInteger point = new MPInteger(this.point);
  60. bcpgOut.WriteObject(point);
  61. }
  62. public virtual BigInteger EncodedPoint
  63. {
  64. get { return point; }
  65. }
  66. public virtual DerObjectIdentifier CurveOid
  67. {
  68. get { return oid; }
  69. }
  70. protected static byte[] ReadBytesOfEncodedLength(
  71. BcpgInputStream bcpgIn)
  72. {
  73. int length = bcpgIn.ReadByte();
  74. if (length < 0)
  75. throw new EndOfStreamException();
  76. if (length == 0 || length == 0xFF)
  77. throw new IOException("future extensions not yet implemented");
  78. if (length > 127)
  79. throw new IOException("unsupported OID");
  80. byte[] buffer = new byte[length + 2];
  81. bcpgIn.ReadFully(buffer, 2, buffer.Length - 2);
  82. buffer[0] = (byte)0x06;
  83. buffer[1] = (byte)length;
  84. return buffer;
  85. }
  86. }
  87. }
  88. #pragma warning restore
  89. #endif