internal static extern Status InitiateCredWithPassword(
     out Status minorStatus,
     bool isNtlm,
     SafeGssNameHandle desiredName,
     string password,
     int passwordLen,
     out SafeGssCredHandle outputCredHandle);
Exemple #2
0
        /// <summary>
        ///  returns the handle for the given credentials.
        ///  The method returns an invalid handle if the username is null or empty.
        /// </summary>
        public static SafeGssCredHandle Create(string username, string password, bool isNtlmOnly)
        {
            if (string.IsNullOrEmpty(username))
            {
                return(new SafeGssCredHandle());
            }

            SafeGssCredHandle retHandle = null;

            using (SafeGssNameHandle userHandle = SafeGssNameHandle.CreateUser(username))
            {
                Status status;
                Status minorStatus;
                if (string.IsNullOrEmpty(password))
                {
                    status = InitiateCredSpNego(out minorStatus, userHandle, out retHandle);
                }
                else
                {
                    status = InitiateCredWithPassword(out minorStatus, isNtlmOnly, userHandle, password, Encoding.UTF8.GetByteCount(password), out retHandle);
                }

                if (status != Status.GSS_S_COMPLETE)
                {
                    retHandle.Dispose();
                    throw new GssApiException(status, minorStatus);
                }
            }

            return(retHandle);
        }
 internal static extern Status InitSecContext(
     out Status minorStatus,
     SafeGssCredHandle initiatorCredHandle,
     ref SafeGssContextHandle contextHandle,
     bool isNtlm,
     SafeGssNameHandle targetName,
     uint reqFlags,
     byte[] inputBytes,
     int inputLength,
     ref GssBuffer token,
     out uint retFlags);
        private static bool GssInitSecurityContext(
            ref SafeGssContextHandle context,
            SafeGssCredHandle credential,
            bool isNtlm,
            SafeGssNameHandle targetName,
            Interop.NetSecurityNative.GssFlags inFlags,
            byte[] buffer,
            out byte[] outputBuffer,
            out uint outFlags,
            out int isNtlmUsed)
        {
            outputBuffer = null;
            outFlags = 0;

            // EstablishSecurityContext is called multiple times in a session.
            // In each call, we need to pass the context handle from the previous call.
            // For the first call, the context handle will be null.
            if (context == null)
            {
                context = new SafeGssContextHandle();
            }

            Interop.NetSecurityNative.GssBuffer token = default(Interop.NetSecurityNative.GssBuffer);
            Interop.NetSecurityNative.Status status;

            try
            {
                Interop.NetSecurityNative.Status minorStatus;
                status = Interop.NetSecurityNative.InitSecContext(out minorStatus,
                                                          credential,
                                                          ref context,
                                                          isNtlm,
                                                          targetName,
                                                          (uint)inFlags,
                                                          buffer,
                                                          (buffer == null) ? 0 : buffer.Length,
                                                          ref token,
                                                          out outFlags,
                                                          out isNtlmUsed);

                if ((status != Interop.NetSecurityNative.Status.GSS_S_COMPLETE) && (status != Interop.NetSecurityNative.Status.GSS_S_CONTINUE_NEEDED))
                {
                    throw new Interop.NetSecurityNative.GssApiException(status, minorStatus);
                }

                outputBuffer = token.ToByteArray();
            }
            finally
            {
                token.Dispose();
            }

            return status == Interop.NetSecurityNative.Status.GSS_S_COMPLETE;
        }
 public SafeDeleteNegoContext(SafeFreeNegoCredentials credential, string targetName)
     : base(credential)
 {
     Debug.Assert((null != credential), "Null credential in SafeDeleteNegoContext");
     try
     {
         _targetName = SafeGssNameHandle.CreatePrincipal(targetName);
     }
     catch
     {
         Dispose();
         throw;
     }
 }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (null != _context)
                {
                    _context.Dispose();
                    _context = null;
                }

                if (_targetName != null)
                {
                    _targetName.Dispose();
                    _targetName = null;
                }
            }
            base.Dispose(disposing);
        }
Exemple #7
0
        /// <summary>
        ///  returns the handle for the given credentials.
        ///  The method returns an invalid handle if the username is null or empty.
        /// </summary>
        public static SafeGssCredHandle Create(string username, string password, bool isNtlmOnly)
        {
            if (isNtlmOnly && !s_IsNtlmInstalled.Value)
            {
                throw new Interop.NetSecurityNative.GssApiException(
                          Interop.NetSecurityNative.Status.GSS_S_BAD_MECH,
                          0,
                          SR.net_gssapi_ntlm_missing_plugin);
            }

            if (string.IsNullOrEmpty(username))
            {
                return(new SafeGssCredHandle());
            }

            SafeGssCredHandle retHandle = null;

            using (SafeGssNameHandle userHandle = SafeGssNameHandle.CreateUser(username))
            {
                Interop.NetSecurityNative.Status status;
                Interop.NetSecurityNative.Status minorStatus;
                if (string.IsNullOrEmpty(password))
                {
                    status = Interop.NetSecurityNative.InitiateCredSpNego(out minorStatus, userHandle, out retHandle);
                }
                else
                {
                    status = Interop.NetSecurityNative.InitiateCredWithPassword(out minorStatus, isNtlmOnly, userHandle, password, Encoding.UTF8.GetByteCount(password), out retHandle);
                }

                if (status != Interop.NetSecurityNative.Status.GSS_S_COMPLETE)
                {
                    retHandle.Dispose();
                    throw new Interop.NetSecurityNative.GssApiException(status, minorStatus, null);
                }
            }

            return(retHandle);
        }
 internal static extern Status InitiateCredSpNego(
     out Status minorStatus,
     SafeGssNameHandle desiredName,
     out SafeGssCredHandle outputCredHandle);
 internal static extern Status ImportPrincipalName(
     out Status minorStatus,
     string inputName,
     int inputNameByteCount,
     out SafeGssNameHandle outputName);