NetTcpSecurity.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel
  5. {
  6. using System.ComponentModel;
  7. using System.Runtime;
  8. using System.ServiceModel.Channels;
  9. using System.ServiceModel.Configuration;
  10. public sealed class NetTcpSecurity
  11. {
  12. internal const SecurityMode DefaultMode = SecurityMode.Transport;
  13. SecurityMode mode;
  14. TcpTransportSecurity transportSecurity;
  15. MessageSecurityOverTcp messageSecurity;
  16. public NetTcpSecurity()
  17. : this(DefaultMode, new TcpTransportSecurity(), new MessageSecurityOverTcp())
  18. {
  19. }
  20. NetTcpSecurity(SecurityMode mode, TcpTransportSecurity transportSecurity, MessageSecurityOverTcp messageSecurity)
  21. {
  22. Fx.Assert(SecurityModeHelper.IsDefined(mode), string.Format("Invalid SecurityMode value: {0}.", mode.ToString()));
  23. this.mode = mode;
  24. this.transportSecurity = transportSecurity == null ? new TcpTransportSecurity() : transportSecurity;
  25. this.messageSecurity = messageSecurity == null ? new MessageSecurityOverTcp() : messageSecurity;
  26. }
  27. [DefaultValue(DefaultMode)]
  28. public SecurityMode Mode
  29. {
  30. get { return this.mode; }
  31. set
  32. {
  33. if (!SecurityModeHelper.IsDefined(value))
  34. {
  35. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value"));
  36. }
  37. this.mode = value;
  38. }
  39. }
  40. public TcpTransportSecurity Transport
  41. {
  42. get { return this.transportSecurity; }
  43. set { this.transportSecurity = value; }
  44. }
  45. public MessageSecurityOverTcp Message
  46. {
  47. get { return this.messageSecurity; }
  48. set { this.messageSecurity = value; }
  49. }
  50. internal BindingElement CreateTransportSecurity()
  51. {
  52. if (this.mode == SecurityMode.TransportWithMessageCredential)
  53. {
  54. return this.transportSecurity.CreateTransportProtectionOnly();
  55. }
  56. else if (this.mode == SecurityMode.Transport)
  57. {
  58. return this.transportSecurity.CreateTransportProtectionAndAuthentication();
  59. }
  60. else
  61. {
  62. return null;
  63. }
  64. }
  65. internal static UnifiedSecurityMode GetModeFromTransportSecurity(BindingElement transport)
  66. {
  67. if (transport == null)
  68. {
  69. return UnifiedSecurityMode.None | UnifiedSecurityMode.Message;
  70. }
  71. else
  72. {
  73. return UnifiedSecurityMode.TransportWithMessageCredential | UnifiedSecurityMode.Transport;
  74. }
  75. }
  76. internal static bool SetTransportSecurity(BindingElement transport, SecurityMode mode, TcpTransportSecurity transportSecurity)
  77. {
  78. if (mode == SecurityMode.TransportWithMessageCredential)
  79. {
  80. return TcpTransportSecurity.SetTransportProtectionOnly(transport, transportSecurity);
  81. }
  82. else if (mode == SecurityMode.Transport)
  83. {
  84. return TcpTransportSecurity.SetTransportProtectionAndAuthentication(transport, transportSecurity);
  85. }
  86. return transport == null;
  87. }
  88. internal SecurityBindingElement CreateMessageSecurity(bool isReliableSessionEnabled)
  89. {
  90. if (this.mode == SecurityMode.Message)
  91. {
  92. return this.messageSecurity.CreateSecurityBindingElement(false, isReliableSessionEnabled, null);
  93. }
  94. else if (this.mode == SecurityMode.TransportWithMessageCredential)
  95. {
  96. return this.messageSecurity.CreateSecurityBindingElement(true, isReliableSessionEnabled, this.CreateTransportSecurity());
  97. }
  98. else
  99. {
  100. return null;
  101. }
  102. }
  103. internal static bool TryCreate(SecurityBindingElement wsSecurity, SecurityMode mode, bool isReliableSessionEnabled, BindingElement transportSecurity, TcpTransportSecurity tcpTransportSecurity, out NetTcpSecurity security)
  104. {
  105. security = null;
  106. MessageSecurityOverTcp messageSecurity = null;
  107. if (mode == SecurityMode.Message)
  108. {
  109. if (!MessageSecurityOverTcp.TryCreate(wsSecurity, isReliableSessionEnabled, null, out messageSecurity))
  110. return false;
  111. }
  112. else if (mode == SecurityMode.TransportWithMessageCredential)
  113. {
  114. if (!MessageSecurityOverTcp.TryCreate(wsSecurity, isReliableSessionEnabled, transportSecurity, out messageSecurity))
  115. return false;
  116. }
  117. security = new NetTcpSecurity(mode, tcpTransportSecurity, messageSecurity);
  118. return SecurityElement.AreBindingsMatching(security.CreateMessageSecurity(isReliableSessionEnabled), wsSecurity, false);
  119. }
  120. internal bool InternalShouldSerialize()
  121. {
  122. return this.Mode != NetTcpSecurity.DefaultMode
  123. || this.Transport.InternalShouldSerialize()
  124. || this.Message.InternalShouldSerialize();
  125. }
  126. }
  127. }