Example #1
0
        public void SignMessage(byte[] message, bool bUseClientContext, out byte[] signedBuffer,
                                ref SECURITY_HANDLE hServerContext)
        {
            signedBuffer = null;

            SECURITY_HANDLE encryptionContext = _hServerContext;

            if (bUseClientContext)
            {
                encryptionContext = _hClientContext;
            }

            SecPkgContext_Sizes contextSizes;
            int result = NativeMethods.QueryContextAttributes(ref encryptionContext, NativeContants.SECPKG_ATTR_SIZES,
                                                              out contextSizes);

            if (result != NativeContants.SEC_E_OK)
            {
                throw new SspiException("QueryContextAttribute() failed!!!", result);
            }

            var thisSecHelper = new MultipleSecBufferHelper[2];

            thisSecHelper[0] = new MultipleSecBufferHelper(message, SecBufferType.SECBUFFER_DATA);
            thisSecHelper[1] = new MultipleSecBufferHelper(new byte[contextSizes.cbMaxSignature],
                                                           SecBufferType.SECBUFFER_TOKEN);

            var descBuffer = new SecBufferDesc(thisSecHelper);

            try
            {
                result = NativeMethods.MakeSignature(ref encryptionContext, 0, ref descBuffer, 0);
                if (result != NativeContants.SEC_E_OK)
                {
                    throw new SspiException("MakeSignature() failed!!!", result);
                }

                //SSPIHelper.SignAndVerify(ref _hClientContext,ref hServerContext,ref DescBuffer);
                uint encryptionQuality;
                NativeMethods.VerifySignature(ref _hServerContext, ref descBuffer, 0, out encryptionQuality);

                signedBuffer = descBuffer.GetSecBufferByteArray();
            }
            finally
            {
                descBuffer.Dispose();
            }
        }
Example #2
0
        public void VerifyMessage(int messageLength, byte[] signedBuffer, bool bUseClientContext,
                                  out byte[] verifiedBuffer)
        {
            verifiedBuffer = null;

            SECURITY_HANDLE decryptionContext = _hServerContext;

            if (bUseClientContext)
            {
                decryptionContext = _hClientContext;
            }

            var signedMessage = new byte[messageLength];

            Array.Copy(signedBuffer, 0, signedMessage, 0, messageLength);

            int signatureLength = signedBuffer.Length - messageLength;

            var signature = new byte[signatureLength];

            Array.Copy(signedBuffer, messageLength, signature, 0, signatureLength);

            var thisSecHelper = new MultipleSecBufferHelper[2];

            thisSecHelper[0] = new MultipleSecBufferHelper(signedMessage, SecBufferType.SECBUFFER_DATA);
            thisSecHelper[1] = new MultipleSecBufferHelper(signature, SecBufferType.SECBUFFER_TOKEN);
            var descBuffer = new SecBufferDesc(thisSecHelper);

            try
            {
                uint encryptionQuality;

                int result = NativeMethods.VerifySignature(ref decryptionContext, ref descBuffer, 0,
                                                           out encryptionQuality);

                if (result != NativeContants.SEC_E_OK)
                {
                    throw new SspiException("VerifySignature() failed!!!", result);
                }

                verifiedBuffer = new byte[messageLength];
                Array.Copy(descBuffer.GetSecBufferByteArray(), 0, verifiedBuffer, 0, messageLength);
            }
            finally
            {
                descBuffer.Dispose();
            }
        }
Example #3
0
        public void EncryptMessage(
            byte[] message, bool bUseClientContext, out byte[] encryptedBuffer)
        {
            encryptedBuffer = null;

            SECURITY_HANDLE encryptionContext = _hServerContext;

            if (bUseClientContext)
            {
                encryptionContext = _hClientContext;
            }

            SecPkgContext_Sizes contextSizes;

            int result = NativeMethods.QueryContextAttributes(ref encryptionContext, NativeContants.SECPKG_ATTR_SIZES,
                                                              out contextSizes);

            if (result != NativeContants.SEC_E_OK)
            {
                throw new SspiException("QueryContextAttribute() failed!!!", result);
            }

            var thisSecHelper = new MultipleSecBufferHelper[2];

            thisSecHelper[0] = new MultipleSecBufferHelper(message, SecBufferType.SECBUFFER_DATA);
            thisSecHelper[1] = new MultipleSecBufferHelper(new byte[contextSizes.cbSecurityTrailer],
                                                           SecBufferType.SECBUFFER_TOKEN);

            var descBuffer = new SecBufferDesc(thisSecHelper);

            try
            {
                result = NativeMethods.EncryptMessage(ref encryptionContext, 0, ref descBuffer, 0);

                if (result != NativeContants.SEC_E_OK)
                {
                    throw new SspiException("EncryptMessage() failed!!!", result);
                }

                encryptedBuffer = descBuffer.GetSecBufferByteArray();
            }
            finally
            {
                descBuffer.Dispose();
            }
        }