| | | 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; |
| | | 5 | | using System.Reflection; |
| | | 6 | | using System.Runtime.ExceptionServices; |
| | | 7 | | using System.Security.Authentication.ExtendedProtection; |
| | | 8 | | using System.Security.Principal; |
| | | 9 | | |
| | | 10 | | namespace CoreWCF.Security.NegotiateInternal |
| | | 11 | | { |
| | | 12 | | internal class NegotiateInternalState : INegotiateInternalState |
| | | 13 | | { |
| | | 14 | | private readonly INTAuthenticationFacade _ntAuthentication; |
| | | 15 | | |
| | 1 | 16 | | public NegotiateInternalState() |
| | | 17 | | { |
| | 1 | 18 | | _ntAuthentication = NTAuthenticationFacade.Build(); |
| | 1 | 19 | | } |
| | | 20 | | |
| | 0 | 21 | | public void SetChannelBinding(ChannelBinding channelBinding) => _ntAuthentication.SetChannelBinding(channelBindi |
| | | 22 | | |
| | 0 | 23 | | public void SetExtendedProtectionPolicy(ExtendedProtectionPolicy protectionPolicy) => _ntAuthentication.SetExten |
| | | 24 | | |
| | | 25 | | public byte[] GetOutgoingBlob(byte[] incomingBlob, out BlobErrorType status, out Exception error) |
| | | 26 | | { |
| | | 27 | | try |
| | | 28 | | { |
| | 0 | 29 | | byte[] blob = _ntAuthentication.GetOutgoingBlob(incomingBlob, out var securityStatus); |
| | | 30 | | |
| | 0 | 31 | | var errorCode = securityStatus.ErrorCode; |
| | | 32 | | |
| | 0 | 33 | | error = securityStatus.Exception; |
| | | 34 | | |
| | 0 | 35 | | if (errorCode == NegotiateInternalSecurityStatusErrorCode.OK |
| | 0 | 36 | | || errorCode == NegotiateInternalSecurityStatusErrorCode.ContinueNeeded |
| | 0 | 37 | | || errorCode == NegotiateInternalSecurityStatusErrorCode.CompleteNeeded) |
| | | 38 | | { |
| | 0 | 39 | | status = BlobErrorType.None; |
| | | 40 | | } |
| | 0 | 41 | | else if (IsCredentialError(errorCode)) |
| | | 42 | | { |
| | 0 | 43 | | status = BlobErrorType.CredentialError; |
| | | 44 | | } |
| | 0 | 45 | | else if (IsClientError(errorCode)) |
| | | 46 | | { |
| | 0 | 47 | | status = BlobErrorType.ClientError; |
| | | 48 | | } |
| | | 49 | | else |
| | | 50 | | { |
| | 0 | 51 | | status = BlobErrorType.Other; |
| | | 52 | | } |
| | | 53 | | |
| | 0 | 54 | | return blob; |
| | | 55 | | } |
| | | 56 | | catch (TargetInvocationException tex) |
| | | 57 | | { |
| | | 58 | | // Unwrap |
| | 0 | 59 | | ExceptionDispatchInfo.Capture(tex.InnerException).Throw(); |
| | 0 | 60 | | throw; |
| | | 61 | | } |
| | 0 | 62 | | } |
| | | 63 | | |
| | 0 | 64 | | public bool IsCompleted => _ntAuthentication.IsCompleted; |
| | | 65 | | |
| | 0 | 66 | | public string Protocol => _ntAuthentication.Protocol; |
| | | 67 | | |
| | 0 | 68 | | public bool IsValidContext => _ntAuthentication.IsValidContext; |
| | | 69 | | |
| | 0 | 70 | | public IIdentity GetIdentity() => _ntAuthentication.GetIdentity(); |
| | | 71 | | |
| | | 72 | | public byte[] Encrypt(byte[] input) |
| | | 73 | | { |
| | 0 | 74 | | if (input == null) |
| | | 75 | | { |
| | 0 | 76 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(input)); |
| | | 77 | | } |
| | | 78 | | |
| | 0 | 79 | | return _ntAuthentication.Encrypt(input); |
| | | 80 | | } |
| | | 81 | | |
| | 0 | 82 | | public void Dispose() => _ntAuthentication.Dispose(); |
| | | 83 | | |
| | 0 | 84 | | private bool IsCredentialError(NegotiateInternalSecurityStatusErrorCode error) => error == NegotiateInternalSecu |
| | 0 | 85 | | error == NegotiateInternalSecurityStatusErrorCode.UnknownCredentials || |
| | 0 | 86 | | error == NegotiateInternalSecurityStatusErrorCode.NoImpersonation || |
| | 0 | 87 | | error == NegotiateInternalSecurityStatusErrorCode.NoAuthenticatingAuthority || |
| | 0 | 88 | | error == NegotiateInternalSecurityStatusErrorCode.UntrustedRoot || |
| | 0 | 89 | | error == NegotiateInternalSecurityStatusErrorCode.CertExpired || |
| | 0 | 90 | | error == NegotiateInternalSecurityStatusErrorCode.SmartcardLogonRequired || |
| | 0 | 91 | | error == NegotiateInternalSecurityStatusErrorCode.BadBinding; |
| | | 92 | | |
| | 0 | 93 | | private bool IsClientError(NegotiateInternalSecurityStatusErrorCode error) => error == NegotiateInternalSecurity |
| | 0 | 94 | | error == NegotiateInternalSecurityStatusErrorCode.CannotPack || |
| | 0 | 95 | | error == NegotiateInternalSecurityStatusErrorCode.QopNotSupported || |
| | 0 | 96 | | error == NegotiateInternalSecurityStatusErrorCode.NoCredentials || |
| | 0 | 97 | | error == NegotiateInternalSecurityStatusErrorCode.MessageAltered || |
| | 0 | 98 | | error == NegotiateInternalSecurityStatusErrorCode.OutOfSequence || |
| | 0 | 99 | | error == NegotiateInternalSecurityStatusErrorCode.IncompleteMessage || |
| | 0 | 100 | | error == NegotiateInternalSecurityStatusErrorCode.IncompleteCredentials || |
| | 0 | 101 | | error == NegotiateInternalSecurityStatusErrorCode.WrongPrincipal || |
| | 0 | 102 | | error == NegotiateInternalSecurityStatusErrorCode.TimeSkew || |
| | 0 | 103 | | error == NegotiateInternalSecurityStatusErrorCode.IllegalMessage || |
| | 0 | 104 | | error == NegotiateInternalSecurityStatusErrorCode.CertUnknown || |
| | 0 | 105 | | error == NegotiateInternalSecurityStatusErrorCode.AlgorithmMismatch || |
| | 0 | 106 | | error == NegotiateInternalSecurityStatusErrorCode.SecurityQosFailed || |
| | 0 | 107 | | error == NegotiateInternalSecurityStatusErrorCode.UnsupportedPreauth; |
| | | 108 | | } |
| | | 109 | | } |