public static SafeFreeCredentials AcquireCredentialsHandle(X509Certificate certificate, SslProtocols protocols, EncryptionPolicy policy, bool isServer)
        {
            int protocolFlags = GetProtocolFlagsFromSslProtocols(protocols, isServer);

            Interop.SspiCli.SecureCredential.Flags flags;
            Interop.SspiCli.CredentialUse          direction;

            if (!isServer)
            {
                direction = Interop.SspiCli.CredentialUse.Outbound;
                flags     = Interop.SspiCli.SecureCredential.Flags.ValidateManual | Interop.SspiCli.SecureCredential.Flags.NoDefaultCred;

                // CoreFX: always opt-in SCH_USE_STRONG_CRYPTO except for SSL3.
                if (((protocolFlags & (Interop.SChannel.SP_PROT_TLS1_0 | Interop.SChannel.SP_PROT_TLS1_1 | Interop.SChannel.SP_PROT_TLS1_2)) != 0) &&
                    (policy != EncryptionPolicy.AllowNoEncryption) && (policy != EncryptionPolicy.NoEncryption))
                {
                    flags |= Interop.SspiCli.SecureCredential.Flags.UseStrongCrypto;
                }
            }
            else
            {
                direction = Interop.SspiCli.CredentialUse.Inbound;
                flags     = Interop.SspiCli.SecureCredential.Flags.Zero;
            }

            Interop.SspiCli.SecureCredential secureCredential = CreateSecureCredential(
                Interop.SspiCli.SecureCredential.CurrentVersion,
                certificate,
                flags,
                protocolFlags,
                policy);

            return(AcquireCredentialsHandle(direction, secureCredential));
        }
        private static Interop.SspiCli.SecureCredential CreateSecureCredential(
            int version,
            X509Certificate certificate,
            Interop.SspiCli.SecureCredential.Flags flags,
            int protocols, EncryptionPolicy policy)
        {
            var credential = new Interop.SspiCli.SecureCredential()
            {
                rootStore         = IntPtr.Zero,
                phMappers         = IntPtr.Zero,
                palgSupportedAlgs = IntPtr.Zero,
                certContextArray  = IntPtr.Zero,
                cCreds            = 0,
                cMappers          = 0,
                cSupportedAlgs    = 0,
                dwSessionLifespan = 0,
                reserved          = 0
            };

            if (policy == EncryptionPolicy.RequireEncryption)
            {
                // Prohibit null encryption cipher.
                credential.dwMinimumCipherStrength = 0;
                credential.dwMaximumCipherStrength = 0;
            }
            else if (policy == EncryptionPolicy.AllowNoEncryption)
            {
                // Allow null encryption cipher in addition to other ciphers.
                credential.dwMinimumCipherStrength = -1;
                credential.dwMaximumCipherStrength = 0;
            }
            else if (policy == EncryptionPolicy.NoEncryption)
            {
                // Suppress all encryption and require null encryption cipher only
                credential.dwMinimumCipherStrength = -1;
                credential.dwMaximumCipherStrength = -1;
            }
            else
            {
                throw new ArgumentException(SR.Format(SR.net_invalid_enum, "EncryptionPolicy"), "policy");
            }

            credential.version = version;
            credential.dwFlags = flags;
            credential.grbitEnabledProtocols = protocols;
            if (certificate != null)
            {
                credential.certContextArray = certificate.Handle;
                credential.cCreds           = 1;
            }

            return(credential);
        }
 //
 // Security: we temporarily reset thread token to open the handle under process account.
 //
 private static SafeFreeCredentials AcquireCredentialsHandle(Interop.SspiCli.CredentialUse credUsage, Interop.SspiCli.SecureCredential secureCredential)
 {
     // First try without impersonation, if it fails, then try the process account.
     // I.E. We don't know which account the certificate context was created under.
     try
     {
         //
         // For app-compat we want to ensure the credential are accessed under >>process<< acount.
         //
         return(WindowsIdentity.RunImpersonated <SafeFreeCredentials>(SafeAccessTokenHandle.InvalidHandle, () =>
         {
             return SSPIWrapper.AcquireCredentialsHandle(GlobalSSPI.SSPISecureChannel, SecurityPackage, credUsage, secureCredential);
         }));
     }
     catch
     {
         return(SSPIWrapper.AcquireCredentialsHandle(GlobalSSPI.SSPISecureChannel, SecurityPackage, credUsage, secureCredential));
     }
 }
예제 #4
0
        private static Interop.SspiCli.SecureCredential CreateSecureCredential(
            int version,
            X509Certificate certificate,
            Interop.SspiCli.SecureCredential.Flags flags,
            int protocols, EncryptionPolicy policy)
        {
            var credential = new Interop.SspiCli.SecureCredential()
            {
                rootStore = IntPtr.Zero,
                phMappers = IntPtr.Zero,
                palgSupportedAlgs = IntPtr.Zero,
                certContextArray = IntPtr.Zero,
                cCreds = 0,
                cMappers = 0,
                cSupportedAlgs = 0,
                dwSessionLifespan = 0,
                reserved = 0
            };

            if (policy == EncryptionPolicy.RequireEncryption)
            {
                // Prohibit null encryption cipher.
                credential.dwMinimumCipherStrength = 0;
                credential.dwMaximumCipherStrength = 0;
            }
            else if (policy == EncryptionPolicy.AllowNoEncryption)
            {
                // Allow null encryption cipher in addition to other ciphers.
                credential.dwMinimumCipherStrength = -1;
                credential.dwMaximumCipherStrength = 0;
            }
            else if (policy == EncryptionPolicy.NoEncryption)
            {
                // Suppress all encryption and require null encryption cipher only
                credential.dwMinimumCipherStrength = -1;
                credential.dwMaximumCipherStrength = -1;
            }
            else
            {
                throw new ArgumentException(SR.Format(SR.net_invalid_enum, "EncryptionPolicy"), "policy");
            }

            credential.version = version;
            credential.dwFlags = flags;
            credential.grbitEnabledProtocols = protocols;
            if (certificate != null)
            {
                credential.certContextArray = certificate.Handle;
                credential.cCreds = 1;
            }

            return credential;
        }
예제 #5
0
 public int AcquireCredentialsHandle(string moduleName, Interop.SspiCli.CredentialUse usage, ref Interop.SspiCli.SecureCredential authdata, out SafeFreeCredentials outCredential)
 {
     return(SafeFreeCredentials.AcquireCredentialsHandle(moduleName, usage, ref authdata, out outCredential));
 }
예제 #6
0
        public static SafeFreeCredentials AcquireCredentialsHandle(SSPIInterface secModule, string package, Interop.SspiCli.CredentialUse intent, Interop.SspiCli.SecureCredential scc)
        {
            if (GlobalLog.IsEnabled)
            {
                GlobalLog.Print("SSPIWrapper::AcquireCredentialsHandle#3(): using " + package);
            }

            if (SecurityEventSource.Log.IsEnabled())
            {
                SecurityEventSource.AcquireCredentialsHandle(package, intent, scc);
            }

            SafeFreeCredentials outCredential = null;
            int errorCode = secModule.AcquireCredentialsHandle(
                package,
                intent,
                ref scc,
                out outCredential);

            if (errorCode != 0)
            {
#if TRACE_VERBOSE
                if (GlobalLog.IsEnabled)
                {
                    GlobalLog.Print("SSPIWrapper::AcquireCredentialsHandle#3(): error " + Interop.MapSecurityStatus((uint)errorCode));
                }
#endif

                if (NetEventSource.Log.IsEnabled())
                {
                    NetEventSource.PrintError(NetEventSource.ComponentType.Security, SR.Format(SR.net_log_operation_failed_with_error, "AcquireCredentialsHandle()", String.Format(CultureInfo.CurrentCulture, "0X{0:X}", errorCode)));
                }

                throw new Win32Exception(errorCode);
            }

#if TRACE_VERBOSE
            if (GlobalLog.IsEnabled)
            {
                GlobalLog.Print("SSPIWrapper::AcquireCredentialsHandle#3(): cred handle = " + outCredential.ToString());
            }
#endif
            return(outCredential);
        }