Esempio n. 1
0
        public unsafe static void EncryptMessage(
            ref SafeCtxtHandle context,
            ref SecBufferDescEx message,
            uint MessageSeqNo,
            void *pfQOP)
        {
            try
            {
                message.Pin();

                int error = Secur32Dll.EncryptMessage(
                    ref context.Handle,
                    pfQOP,
                    ref message.SecBufferDesc,
                    MessageSeqNo);

                if (error != 0)
                {
                    throw new SspiException(error, @"EncryptMessage");
                }
            }
            finally
            {
                message.Free();
            }
        }
Esempio n. 2
0
        public unsafe static SecurityStatus SafeAcceptSecurityContext(ref SafeCredHandle credential, ref SafeCtxtHandle context, ref SecBufferDescEx input, int contextReq, TargetDataRep targetDataRep, ref SafeCtxtHandle newContext, ref SecBufferDescEx output, out int contextAttr, out long timeStamp)
        {
            SecurityStatus result;

            try
            {
                input.Pin();
                output.Pin();
                try
                {
                    fixed(IntPtr *ptr = (IntPtr *)(&context.Handle))
                    {
                        int error = Secur32Dll.AcceptSecurityContext(ref credential.Handle, context.IsInvalid ? null : ((void *)ptr), ref input.SecBufferDesc, contextReq, (int)targetDataRep, ref newContext.Handle, ref output.SecBufferDesc, out contextAttr, out timeStamp);

                        result = Sspi.Convert(error);
                    }
                }
                finally
                {
                    IntPtr *ptr = null;
                }
            }
            catch
            {
                contextAttr = 0;
                timeStamp   = 0L;
                result      = (SecurityStatus)4294967295u;
            }
            finally
            {
                input.Free();
                output.Free();
            }
            return(result);
        }
Esempio n. 3
0
        public unsafe static SecurityStatus SafeVerifySignature(
            SafeCtxtHandle context,
            ref SecBufferDescEx message,
            int sequence)
        {
            try
            {
                message.Pin();

                int qop;

                var error = Secur32Dll.VerifySignature(
                    ref context.Handle,
                    ref message.SecBufferDesc,
                    sequence,
                    out qop);

                return(Convert(error));
            }
            catch
            {
                return(SecurityStatus.SEC_E_UNKNOW_ERROR);
            }
            finally
            {
                message.Free();
            }
        }
Esempio n. 4
0
        public unsafe static SecurityStatus SafeDecryptMessage(
            ref SafeCtxtHandle context,
            ref SecBufferDescEx message,
            uint MessageSeqNo,
            void *pfQOP)
        {
            try
            {
                message.Pin();

                int error = Secur32Dll.DecryptMessage(
                    ref context.Handle,
                    ref message.SecBufferDesc,
                    MessageSeqNo,
                    pfQOP);

                return(Convert(error));
            }
            catch
            {
                return(SecurityStatus.SEC_E_UNKNOW_ERROR);
            }
            finally
            {
                message.Free();
            }
        }
Esempio n. 5
0
        public static SafeCredHandle SafeAcquireCredentialsHandle(string package, CredentialUse credentialUse)
        {
            CredHandle credHandle;
            long       num;

            Secur32Dll.AcquireCredentialsHandleA(null, package, (int)credentialUse, null, null, null, null, out credHandle, out num);
            return(new SafeCredHandle(credHandle));
        }
Esempio n. 6
0
        public unsafe static void QueryContextAttributes(ref SafeCtxtHandle context, UlAttribute attribute, void *buffer)
        {
            int num = Secur32Dll.QueryContextAttributesA(ref context.Handle, (uint)attribute, buffer);

            if (num != 0)
            {
                throw new SspiException(num, "QueryContextAttributesA");
            }
        }
Esempio n. 7
0
        public unsafe static SecurityStatus SafeQueryContextAttributes(ref SafeCtxtHandle context, out string name)
        {
            SecPkgContext_Names[] array = new SecPkgContext_Names[1];
            fixed(IntPtr *ptr = array)
            {
                SecurityStatus result = Sspi.SafeQueryContextAttributes(ref context, UlAttribute.SECPKG_ATTR_NAMES, (void *)ptr);

                name = Marshal.PtrToStringAnsi(array[0].sUserName);
                Secur32Dll.FreeContextBuffer(array[0].sUserName);
                return(result);
            }
        }
Esempio n. 8
0
        public static unsafe void AcquireCredentialsHandle(
            CredentialUse credentialUse,
            SchannelCred authData,
            out SafeCredHandle credential,
            out long expiry)
        {
            CredHandle handle;
            GCHandle   paCredHandle = new GCHandle();

            IntPtr[] paCred = null;

            if (authData.cCreds > 0)
            {
                paCred            = new IntPtr[] { authData.paCreds1 };
                paCredHandle      = GCHandle.Alloc(paCred, GCHandleType.Pinned);
                authData.paCreds1 = paCredHandle.AddrOfPinnedObject();
            }

            try
            {
                int error = Secur32Dll.AcquireCredentialsHandleA(
                    null,
                    Secur32Dll.UNISP_NAME,
                    (int)credentialUse,
                    null,
                    &authData,
                    null,
                    null,
                    out handle,
                    out expiry);

                if (error != 0)
                {
                    throw new SspiException(error, @"AcquireCredentialsHandleA");
                }

                credential = new SafeCredHandle(handle);
            }
            finally
            {
                if (paCredHandle.IsAllocated)
                {
                    paCredHandle.Free();
                }

                if (paCred != null)
                {
                    authData.paCreds1 = paCred[0];
                }
            }
        }
Esempio n. 9
0
        public unsafe static SecurityStatus SafeQueryContextAttributes(ref SafeCtxtHandle context, UlAttribute attribute, void *buffer)
        {
            SecurityStatus result;

            try
            {
                int error = Secur32Dll.QueryContextAttributesA(ref context.Handle, (uint)attribute, buffer);
                result = Sspi.Convert(error);
            }
            catch
            {
                result = (SecurityStatus)4294967295u;
            }
            return(result);
        }
Esempio n. 10
0
        public unsafe static SecurityStatus SafeQueryContextAttributes(
            ref SafeCtxtHandle context,
            out string name)
        {
            var names = new SecPkgContext_Names[1];

            fixed(void *buffer = names)
            {
                var result = SafeQueryContextAttributes(ref context, UlAttribute.SECPKG_ATTR_NAMES, buffer);

                name = Marshal.PtrToStringAnsi(names[0].sUserName);
                Secur32Dll.FreeContextBuffer(names[0].sUserName);

                return(result);
            }
        }
Esempio n. 11
0
        public static unsafe SecurityStatus SafeAcceptSecurityContext(
            ref SafeCredHandle credential,
            ref SafeCtxtHandle context,
            ref SecBufferDescEx input,
            int contextReq,
            TargetDataRep targetDataRep,
            ref SafeCtxtHandle newContext,
            ref SecBufferDescEx output,
            out int contextAttr,
            out long timeStamp)
        {
            try
            {
                input.Pin();
                output.Pin();

                fixed(void *fixedContext = &context.Handle)
                {
                    int error = Secur32Dll.AcceptSecurityContext(
                        ref credential.Handle,
                        (context.IsInvalid) ? null : fixedContext,
                        ref input.SecBufferDesc,
                        contextReq,
                        (int)targetDataRep,
                        ref newContext.Handle,
                        ref output.SecBufferDesc,
                        out contextAttr,
                        out timeStamp);

                    return(Convert(error));
                }
            }
            catch
            {
                contextAttr = 0;
                timeStamp   = 0;
                return(SecurityStatus.SEC_E_UNKNOW_ERROR);
            }
            finally
            {
                input.Free();
                output.Free();
            }
        }
Esempio n. 12
0
        public unsafe static SecurityStatus SafeQueryContextAttributes(
            ref SafeCtxtHandle context,
            UlAttribute attribute,
            void *buffer)
        {
            try
            {
                int error = Secur32Dll.QueryContextAttributesA(
                    ref context.Handle,
                    (uint)attribute,
                    buffer);

                return(Convert(error));
            }
            catch
            {
                return(SecurityStatus.SEC_E_UNKNOW_ERROR);
            }
        }
Esempio n. 13
0
        public static SecurityStatus SafeMakeSignature(SafeCtxtHandle context, ref SecBufferDescEx message, int sequence)
        {
            SecurityStatus result;

            try
            {
                message.Pin();
                int error = Secur32Dll.MakeSignature(ref context.Handle, 0, ref message.SecBufferDesc, sequence);
                result = Sspi.Convert(error);
            }
            catch
            {
                result = (SecurityStatus)4294967295u;
            }
            finally
            {
                message.Free();
            }
            return(result);
        }
Esempio n. 14
0
        public unsafe static SecurityStatus SafeDecryptMessage(ref SafeCtxtHandle context, ref SecBufferDescEx message, uint MessageSeqNo, void *pfQOP)
        {
            SecurityStatus result;

            try
            {
                message.Pin();
                int error = Secur32Dll.DecryptMessage(ref context.Handle, ref message.SecBufferDesc, MessageSeqNo, pfQOP);
                result = Sspi.Convert(error);
            }
            catch
            {
                result = (SecurityStatus)4294967295u;
            }
            finally
            {
                message.Free();
            }
            return(result);
        }
Esempio n. 15
0
        public unsafe static void AcquireCredentialsHandle(CredentialUse credentialUse, SchannelCred authData, out SafeCredHandle credential, out long expiry)
        {
            GCHandle gCHandle = default(GCHandle);

            IntPtr[] array = null;
            if (authData.cCreds > 0)
            {
                array = new IntPtr[]
                {
                    authData.paCreds1
                };
                gCHandle          = GCHandle.Alloc(array, GCHandleType.Pinned);
                authData.paCreds1 = gCHandle.AddrOfPinnedObject();
            }
            try
            {
                CredHandle credHandle;
                int        num = Secur32Dll.AcquireCredentialsHandleA(null, "Microsoft Unified Security Protocol Provider", (int)credentialUse, null, (void *)(&authData), null, null, out credHandle, out expiry);
                if (num != 0)
                {
                    throw new SspiException(num, "AcquireCredentialsHandleA");
                }
                credential = new SafeCredHandle(credHandle);
            }
            finally
            {
                if (gCHandle.IsAllocated)
                {
                    gCHandle.Free();
                }
                if (array != null)
                {
                    authData.paCreds1 = array[0];
                }
            }
        }
Esempio n. 16
0
        public static unsafe SafeCredHandle SafeAcquireCredentialsHandle(string package, CredentialUse credentialUse)
        {
            long       expiry;
            CredHandle handle;

            try
            {
                int error = Secur32Dll.AcquireCredentialsHandleA(
                    null,
                    package,
                    (int)credentialUse,
                    null,
                    null,
                    null,
                    null,
                    out handle,
                    out expiry);

                return(new SafeCredHandle(handle));
            }
            finally
            {
            }
        }
Esempio n. 17
0
 protected override bool ReleaseHandle()
 {
     return(Secur32Dll.DeleteSecurityContext(ref this.Handle) == 0);
 }
Esempio n. 18
0
 protected override bool ReleaseHandle()
 {
     return(Secur32Dll.FreeContextBuffer(this.handle) == 0);
 }
Esempio n. 19
0
 protected override bool ReleaseHandle()
 {
     return(Secur32Dll.FreeCredentialsHandle(ref Handle) == 0);
 }
Esempio n. 20
0
 public static SecurityStatus EnumerateSecurityPackages(out int packages, out SafeContextBufferHandle secPkgInfos)
 {
     return(Convert(
                Secur32Dll.EnumerateSecurityPackagesA(out packages, out secPkgInfos)));
 }