| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Runtime; |
| | | 5 | | using CoreWCF.Channels; |
| | | 6 | | using CoreWCF.Security; |
| | | 7 | | using System.ComponentModel; |
| | | 8 | | using System; |
| | | 9 | | using CoreWCF.Runtime; |
| | | 10 | | |
| | | 11 | | namespace CoreWCF |
| | | 12 | | { |
| | | 13 | | public sealed class BasicHttpMessageSecurity |
| | | 14 | | { |
| | | 15 | | internal const BasicHttpMessageCredentialType DefaultClientCredentialType = BasicHttpMessageCredentialType.UserN |
| | | 16 | | private BasicHttpMessageCredentialType _clientCredentialType; |
| | | 17 | | private SecurityAlgorithmSuite _algorithmSuite; |
| | | 18 | | |
| | 427 | 19 | | public BasicHttpMessageSecurity() |
| | | 20 | | { |
| | 427 | 21 | | _clientCredentialType = DefaultClientCredentialType; |
| | 427 | 22 | | _algorithmSuite = SecurityAlgorithmSuite.Default; |
| | 427 | 23 | | } |
| | | 24 | | |
| | | 25 | | public BasicHttpMessageCredentialType ClientCredentialType |
| | | 26 | | { |
| | 1 | 27 | | get { return _clientCredentialType; } |
| | | 28 | | set |
| | | 29 | | { |
| | 12 | 30 | | if (!BasicHttpMessageCredentialTypeHelper.IsDefined(value)) |
| | | 31 | | { |
| | 0 | 32 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | | 33 | | } |
| | 12 | 34 | | _clientCredentialType = value; |
| | 12 | 35 | | } |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | public SecurityAlgorithmSuite AlgorithmSuite |
| | | 39 | | { |
| | 70 | 40 | | get { return _algorithmSuite; } |
| | | 41 | | set |
| | | 42 | | { |
| | 0 | 43 | | if (value == null) |
| | | 44 | | { |
| | 0 | 45 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value)); |
| | | 46 | | } |
| | 0 | 47 | | _algorithmSuite = value; |
| | 0 | 48 | | } |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | // if any changes are made to this method, please reflect them in the corresponding TryCrete() method |
| | | 52 | | internal SecurityBindingElement CreateMessageSecurity(bool isSecureTransportMode) |
| | | 53 | | { |
| | | 54 | | SecurityBindingElement result; |
| | | 55 | | |
| | 70 | 56 | | if (isSecureTransportMode) |
| | | 57 | | { |
| | 70 | 58 | | MessageSecurityVersion version = MessageSecurityVersion.WSSecurity10WSTrustFebruary2005WSSecureConversat |
| | 70 | 59 | | switch (_clientCredentialType) |
| | | 60 | | { |
| | | 61 | | case BasicHttpMessageCredentialType.Certificate: |
| | 0 | 62 | | result = SecurityBindingElement.CreateCertificateOverTransportBindingElement(version); |
| | 0 | 63 | | break; |
| | | 64 | | case BasicHttpMessageCredentialType.UserName: |
| | 70 | 65 | | result = SecurityBindingElement.CreateUserNameOverTransportBindingElement(); |
| | 70 | 66 | | result.MessageSecurityVersion = version; |
| | 70 | 67 | | break; |
| | | 68 | | default: |
| | | 69 | | Fx.Assert("Unsupported basic http message credential type"); |
| | 0 | 70 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException()); |
| | | 71 | | } |
| | | 72 | | } |
| | | 73 | | else |
| | | 74 | | { |
| | 0 | 75 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.Format(SR.Unsuppo |
| | | 76 | | //if (_clientCredentialType != BasicHttpMessageCredentialType.Certificate) |
| | | 77 | | //{ |
| | | 78 | | // throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format( |
| | | 79 | | //} |
| | | 80 | | //result = SecurityBindingElement.CreateMutualCertificateBindingElement(MessageSecurityVersion.WSSecurit |
| | | 81 | | } |
| | | 82 | | |
| | 70 | 83 | | result.DefaultAlgorithmSuite = AlgorithmSuite; |
| | 70 | 84 | | result.SecurityHeaderLayout = SecurityHeaderLayout.Lax; |
| | 70 | 85 | | result.SetKeyDerivation(false); |
| | | 86 | | // result.DoNotEmitTrust = true; |
| | | 87 | | |
| | 70 | 88 | | return result; |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | internal bool InternalShouldSerialize() |
| | | 92 | | { |
| | 0 | 93 | | return ShouldSerializeAlgorithmSuite() |
| | 0 | 94 | | || ShouldSerializeClientCredentialType(); |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| | | 98 | | public bool ShouldSerializeAlgorithmSuite() |
| | | 99 | | { |
| | 0 | 100 | | return _algorithmSuite.GetType() != SecurityAlgorithmSuite.Default.GetType(); |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| | | 104 | | public bool ShouldSerializeClientCredentialType() |
| | | 105 | | { |
| | 0 | 106 | | return _clientCredentialType != DefaultClientCredentialType; |
| | | 107 | | } |
| | | 108 | | } |
| | | 109 | | } |