| | | 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.Security.Authentication.ExtendedProtection; |
| | | 6 | | using System.Security.Principal; |
| | | 7 | | using CoreWCF.Security.NegotiateInternal; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.Security |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// WindowsSspiNegotiation is re-write of |
| | | 13 | | /// https://referencesource.microsoft.com/#System.ServiceModel/System/ServiceModel/Security/WindowsSspiNegotiation.c |
| | | 14 | | /// With .net standard, many code moved to NTAuth/NegotiateStream.. |
| | | 15 | | /// Using another layer of abstraction in NegotiateInternal.NegotiateInternalState to call most of the API using ref |
| | | 16 | | /// |
| | | 17 | | /// </summary> |
| | | 18 | | internal class WindowsSspiNegotiation : ISspiNegotiation |
| | | 19 | | { |
| | | 20 | | private readonly INegotiateInternalState _negotiateState; |
| | | 21 | | |
| | 0 | 22 | | public WindowsSspiNegotiation(INegotiateInternalState passedNegotiateState) |
| | | 23 | | { |
| | 0 | 24 | | _negotiateState = passedNegotiateState; |
| | 0 | 25 | | } |
| | | 26 | | |
| | 0 | 27 | | public bool IsCompleted { get; private set; } |
| | | 28 | | |
| | 0 | 29 | | public bool IsValidContext => _negotiateState.IsValidContext; |
| | | 30 | | |
| | 0 | 31 | | public string KeyEncryptionAlgorithm => SecurityAlgorithmStrings.WindowsSspiKeyWrap; |
| | | 32 | | |
| | 0 | 33 | | public byte[] Decrypt(byte[] encryptedData) => throw new NotImplementedException(); |
| | | 34 | | |
| | | 35 | | public void Dispose() |
| | | 36 | | { |
| | 0 | 37 | | if (_negotiateState != null) |
| | | 38 | | { |
| | 0 | 39 | | _negotiateState.Dispose(); |
| | | 40 | | } |
| | 0 | 41 | | } |
| | | 42 | | |
| | 0 | 43 | | public byte[] Encrypt(byte[] input) => _negotiateState.Encrypt(input); |
| | | 44 | | |
| | | 45 | | public byte[] GetOutgoingBlob(byte[] incomingBlob, ChannelBinding channelbinding, ExtendedProtectionPolicy prote |
| | | 46 | | { |
| | 0 | 47 | | _negotiateState.SetChannelBinding(channelbinding); |
| | 0 | 48 | | _negotiateState.SetExtendedProtectionPolicy(protectionPolicy); |
| | 0 | 49 | | byte[] outGoingBlob = _negotiateState.GetOutgoingBlob(incomingBlob, out BlobErrorType errorType, out Excepti |
| | 0 | 50 | | if (errorType != BlobErrorType.None) |
| | | 51 | | { |
| | 0 | 52 | | throw exception; |
| | | 53 | | } |
| | 0 | 54 | | IsCompleted = _negotiateState.IsCompleted; |
| | 0 | 55 | | return outGoingBlob; |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | public string GetRemoteIdentityName() |
| | | 59 | | { |
| | 0 | 60 | | if (IsValidContext) |
| | | 61 | | { |
| | 0 | 62 | | IIdentity identity = _negotiateState.GetIdentity(); |
| | 0 | 63 | | if (identity != null) |
| | | 64 | | { |
| | 0 | 65 | | return identity.Name; |
| | | 66 | | } |
| | | 67 | | } |
| | 0 | 68 | | return string.Empty; |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | public IIdentity GetIdentity() |
| | | 72 | | { |
| | 0 | 73 | | if (IsValidContext) |
| | | 74 | | { |
| | 0 | 75 | | return _negotiateState.GetIdentity(); |
| | | 76 | | } |
| | | 77 | | |
| | 0 | 78 | | return null; |
| | | 79 | | } |
| | | 80 | | } |
| | | 81 | | } |