| | | 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.Net.Security; |
| | | 5 | | using System.Runtime.InteropServices; |
| | | 6 | | |
| | | 7 | | namespace System.Net |
| | | 8 | | { |
| | | 9 | | // Schannel SSPI interface. |
| | | 10 | | internal sealed class SSPISecureChannelType : ISSPIInterface |
| | | 11 | | { |
| | | 12 | | private static volatile SecurityPackageInfoClass[]? s_securityPackages; |
| | | 13 | | |
| | | 14 | | public SecurityPackageInfoClass[]? SecurityPackages |
| | | 15 | | { |
| | | 16 | | get |
| | 0 | 17 | | { |
| | 0 | 18 | | return s_securityPackages; |
| | 0 | 19 | | } |
| | | 20 | | set |
| | 0 | 21 | | { |
| | 0 | 22 | | s_securityPackages = value; |
| | 0 | 23 | | } |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | public int EnumerateSecurityPackages(out int pkgnum, out SafeFreeContextBuffer pkgArray) |
| | 0 | 27 | | { |
| | 0 | 28 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this); |
| | 0 | 29 | | return SafeFreeContextBuffer.EnumeratePackages(out pkgnum, out pkgArray); |
| | 0 | 30 | | } |
| | | 31 | | |
| | | 32 | | public int AcquireCredentialsHandle(string moduleName, Interop.SspiCli.CredentialUse usage, ref SafeSspiAuthData |
| | 0 | 33 | | { |
| | 0 | 34 | | return SafeFreeCredentials.AcquireCredentialsHandle(moduleName, usage, ref authdata, out outCredential); |
| | 0 | 35 | | } |
| | | 36 | | |
| | | 37 | | public int AcquireDefaultCredential(string moduleName, Interop.SspiCli.CredentialUse usage, out SafeFreeCredenti |
| | 0 | 38 | | { |
| | 0 | 39 | | return SafeFreeCredentials.AcquireDefaultCredential(moduleName, usage, out outCredential); |
| | 0 | 40 | | } |
| | | 41 | | |
| | | 42 | | public unsafe int AcquireCredentialsHandle(string moduleName, Interop.SspiCli.CredentialUse usage, Interop.SspiC |
| | 0 | 43 | | { |
| | 0 | 44 | | return SafeFreeCredentials.AcquireCredentialsHandle(moduleName, usage, authdata, out outCredential); |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | public unsafe int AcquireCredentialsHandle(string moduleName, Interop.SspiCli.CredentialUse usage, Interop.SspiC |
| | 0 | 48 | | { |
| | 0 | 49 | | return SafeFreeCredentials.AcquireCredentialsHandle(moduleName, usage, authdata, out outCredential); |
| | 0 | 50 | | } |
| | | 51 | | |
| | | 52 | | public int AcceptSecurityContext(SafeFreeCredentials? credential, ref SafeDeleteSslContext? context, ref InputSe |
| | 0 | 53 | | { |
| | 0 | 54 | | return SafeDeleteContext.AcceptSecurityContext(ref credential, ref context, inFlags, endianness, ref inputBu |
| | 0 | 55 | | } |
| | | 56 | | |
| | | 57 | | public int InitializeSecurityContext(ref SafeFreeCredentials? credential, ref SafeDeleteSslContext? context, str |
| | 0 | 58 | | { |
| | 0 | 59 | | return SafeDeleteContext.InitializeSecurityContext(ref credential, ref context, targetName, inFlags, endiann |
| | 0 | 60 | | } |
| | | 61 | | |
| | | 62 | | public int EncryptMessage(SafeDeleteContext context, ref Interop.SspiCli.SecBufferDesc inputOutput, uint qop) |
| | 0 | 63 | | { |
| | 0 | 64 | | bool mustRelease = false; |
| | | 65 | | try |
| | 0 | 66 | | { |
| | 0 | 67 | | context.DangerousAddRef(ref mustRelease); |
| | 0 | 68 | | return Interop.SspiCli.EncryptMessage(ref context._handle, qop, ref inputOutput, 0); |
| | | 69 | | } |
| | | 70 | | finally |
| | 0 | 71 | | { |
| | 0 | 72 | | if (mustRelease) |
| | 0 | 73 | | { |
| | 0 | 74 | | context.DangerousRelease(); |
| | 0 | 75 | | } |
| | 0 | 76 | | } |
| | 0 | 77 | | } |
| | | 78 | | |
| | | 79 | | public unsafe int DecryptMessage(SafeDeleteContext context, ref Interop.SspiCli.SecBufferDesc inputOutput, out u |
| | 0 | 80 | | { |
| | 0 | 81 | | int status = (int)Interop.SECURITY_STATUS.InvalidHandle; |
| | 0 | 82 | | uint qopTemp = 0; |
| | | 83 | | |
| | 0 | 84 | | bool mustRelease = false; |
| | | 85 | | try |
| | 0 | 86 | | { |
| | 0 | 87 | | context.DangerousAddRef(ref mustRelease); |
| | 0 | 88 | | status = Interop.SspiCli.DecryptMessage(ref context._handle, ref inputOutput, 0, &qopTemp); |
| | 0 | 89 | | } |
| | | 90 | | finally |
| | 0 | 91 | | { |
| | 0 | 92 | | if (mustRelease) |
| | 0 | 93 | | { |
| | 0 | 94 | | context.DangerousRelease(); |
| | 0 | 95 | | } |
| | 0 | 96 | | } |
| | | 97 | | |
| | 0 | 98 | | qop = qopTemp; |
| | 0 | 99 | | return status; |
| | 0 | 100 | | } |
| | | 101 | | |
| | | 102 | | public unsafe int QueryContextChannelBinding(SafeDeleteContext phContext, Interop.SspiCli.ContextAttribute attri |
| | 0 | 103 | | { |
| | 0 | 104 | | refHandle = SafeFreeContextBufferChannelBinding.CreateEmptyHandle(); |
| | | 105 | | |
| | | 106 | | // Bindings is on the stack, so there's no need for a fixed block. |
| | 0 | 107 | | SecPkgContext_Bindings bindings = default; |
| | 0 | 108 | | return SafeFreeContextBufferChannelBinding.QueryContextChannelBinding(phContext, attribute, &bindings, refHa |
| | 0 | 109 | | } |
| | | 110 | | |
| | | 111 | | public unsafe int QueryContextAttributes(SafeDeleteContext phContext, Interop.SspiCli.ContextAttribute attribute |
| | 0 | 112 | | { |
| | 0 | 113 | | return SafeFreeContextBuffer.QueryContextAttributes(phContext, attribute, refHandle); |
| | 0 | 114 | | } |
| | | 115 | | |
| | | 116 | | public unsafe int QueryContextAttributes(SafeDeleteContext phContext, Interop.SspiCli.ContextAttribute attribute |
| | 0 | 117 | | { |
| | 0 | 118 | | refHandle = null; |
| | 0 | 119 | | if (handleType != null) |
| | 0 | 120 | | { |
| | 0 | 121 | | if (handleType == typeof(SafeFreeContextBuffer)) |
| | 0 | 122 | | { |
| | 0 | 123 | | refHandle = SafeFreeContextBuffer.CreateEmptyHandle(); |
| | 0 | 124 | | } |
| | 0 | 125 | | else if (handleType == typeof(SafeFreeCertContext)) |
| | 0 | 126 | | { |
| | 0 | 127 | | refHandle = new SafeFreeCertContext(); |
| | 0 | 128 | | } |
| | | 129 | | else |
| | 0 | 130 | | { |
| | 0 | 131 | | throw new ArgumentException(SR.Format(SR.SSPIInvalidHandleType, handleType.FullName), nameof(handleT |
| | | 132 | | } |
| | 0 | 133 | | } |
| | 0 | 134 | | fixed (byte* bufferPtr = buffer) |
| | 0 | 135 | | { |
| | 0 | 136 | | return SafeFreeContextBuffer.QueryContextAttributes(phContext, attribute, bufferPtr, refHandle); |
| | | 137 | | } |
| | 0 | 138 | | } |
| | | 139 | | |
| | | 140 | | public int QuerySecurityContextToken(SafeDeleteContext phContext, out SecurityContextTokenHandle phToken) |
| | 0 | 141 | | { |
| | 0 | 142 | | throw new NotSupportedException(); |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | public int CompleteAuthToken(ref SafeDeleteSslContext? refContext, in InputSecurityBuffer inputBuffer) |
| | 0 | 146 | | { |
| | 0 | 147 | | throw new NotSupportedException(); |
| | | 148 | | } |
| | | 149 | | |
| | | 150 | | public int ApplyControlToken(ref SafeDeleteSslContext? refContext, in SecurityBuffer inputBuffer) |
| | 0 | 151 | | { |
| | 0 | 152 | | return SafeDeleteContext.ApplyControlToken(ref refContext, in inputBuffer); |
| | 0 | 153 | | } |
| | | 154 | | } |
| | | 155 | | } |
| | | 156 | | |