SessionParameters.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.Collections;
  4. using System.IO;
  5. using Org.BouncyCastle.Utilities;
  6. namespace Org.BouncyCastle.Crypto.Tls
  7. {
  8. public sealed class SessionParameters
  9. {
  10. public sealed class Builder
  11. {
  12. private int mCipherSuite = -1;
  13. private short mCompressionAlgorithm = -1;
  14. private byte[] mMasterSecret = null;
  15. private Certificate mPeerCertificate = null;
  16. private byte[] mPskIdentity = null;
  17. private byte[] mSrpIdentity = null;
  18. private byte[] mEncodedServerExtensions = null;
  19. public Builder()
  20. {
  21. }
  22. public SessionParameters Build()
  23. {
  24. Validate(this.mCipherSuite >= 0, "cipherSuite");
  25. Validate(this.mCompressionAlgorithm >= 0, "compressionAlgorithm");
  26. Validate(this.mMasterSecret != null, "masterSecret");
  27. return new SessionParameters(mCipherSuite, (byte)mCompressionAlgorithm, mMasterSecret, mPeerCertificate,
  28. mPskIdentity, mSrpIdentity, mEncodedServerExtensions);
  29. }
  30. public Builder SetCipherSuite(int cipherSuite)
  31. {
  32. this.mCipherSuite = cipherSuite;
  33. return this;
  34. }
  35. public Builder SetCompressionAlgorithm(byte compressionAlgorithm)
  36. {
  37. this.mCompressionAlgorithm = compressionAlgorithm;
  38. return this;
  39. }
  40. public Builder SetMasterSecret(byte[] masterSecret)
  41. {
  42. this.mMasterSecret = masterSecret;
  43. return this;
  44. }
  45. public Builder SetPeerCertificate(Certificate peerCertificate)
  46. {
  47. this.mPeerCertificate = peerCertificate;
  48. return this;
  49. }
  50. public Builder SetPskIdentity(byte[] pskIdentity)
  51. {
  52. this.mPskIdentity = pskIdentity;
  53. return this;
  54. }
  55. public Builder SetSrpIdentity(byte[] srpIdentity)
  56. {
  57. this.mSrpIdentity = srpIdentity;
  58. return this;
  59. }
  60. public Builder SetServerExtensions(IDictionary serverExtensions)
  61. {
  62. if (serverExtensions == null)
  63. {
  64. mEncodedServerExtensions = null;
  65. }
  66. else
  67. {
  68. MemoryStream buf = new MemoryStream();
  69. TlsProtocol.WriteExtensions(buf, serverExtensions);
  70. mEncodedServerExtensions = buf.ToArray();
  71. }
  72. return this;
  73. }
  74. private void Validate(bool condition, string parameter)
  75. {
  76. if (!condition)
  77. throw new InvalidOperationException("Required session parameter '" + parameter + "' not configured");
  78. }
  79. }
  80. private int mCipherSuite;
  81. private byte mCompressionAlgorithm;
  82. private byte[] mMasterSecret;
  83. private Certificate mPeerCertificate;
  84. private byte[] mPskIdentity;
  85. private byte[] mSrpIdentity;
  86. private byte[] mEncodedServerExtensions;
  87. private SessionParameters(int cipherSuite, byte compressionAlgorithm, byte[] masterSecret,
  88. Certificate peerCertificate, byte[] pskIdentity, byte[] srpIdentity, byte[] encodedServerExtensions)
  89. {
  90. this.mCipherSuite = cipherSuite;
  91. this.mCompressionAlgorithm = compressionAlgorithm;
  92. this.mMasterSecret = Arrays.Clone(masterSecret);
  93. this.mPeerCertificate = peerCertificate;
  94. this.mPskIdentity = Arrays.Clone(pskIdentity);
  95. this.mSrpIdentity = Arrays.Clone(srpIdentity);
  96. this.mEncodedServerExtensions = encodedServerExtensions;
  97. }
  98. public void Clear()
  99. {
  100. if (this.mMasterSecret != null)
  101. {
  102. Arrays.Fill(this.mMasterSecret, (byte)0);
  103. }
  104. }
  105. public SessionParameters Copy()
  106. {
  107. return new SessionParameters(mCipherSuite, mCompressionAlgorithm, mMasterSecret, mPeerCertificate,
  108. mPskIdentity, mSrpIdentity, mEncodedServerExtensions);
  109. }
  110. public int CipherSuite
  111. {
  112. get { return mCipherSuite; }
  113. }
  114. public byte CompressionAlgorithm
  115. {
  116. get { return mCompressionAlgorithm; }
  117. }
  118. public byte[] MasterSecret
  119. {
  120. get { return mMasterSecret; }
  121. }
  122. public Certificate PeerCertificate
  123. {
  124. get { return mPeerCertificate; }
  125. }
  126. public byte[] PskIdentity
  127. {
  128. get { return mPskIdentity; }
  129. }
  130. public byte[] SrpIdentity
  131. {
  132. get { return mSrpIdentity; }
  133. }
  134. public IDictionary ReadServerExtensions()
  135. {
  136. if (mEncodedServerExtensions == null)
  137. return null;
  138. MemoryStream buf = new MemoryStream(mEncodedServerExtensions, false);
  139. return TlsProtocol.ReadExtensions(buf);
  140. }
  141. }
  142. }
  143. #endif