public override byte[] GetBytes()
        {
            int sequenceLength              = GetTokenFieldsLength();
            int sequenceLengthFieldSize     = DerEncodingHelper.GetLengthFieldSize(sequenceLength);
            int constructionLength          = 1 + sequenceLengthFieldSize + sequenceLength;
            int constructionLengthFieldSize = DerEncodingHelper.GetLengthFieldSize(constructionLength);
            int bufferSize = 1 + constructionLengthFieldSize + 1 + sequenceLengthFieldSize + sequenceLength;

            byte[] buffer = new byte[bufferSize];
            int    offset = 0;

            ByteWriter.WriteByte(buffer, ref offset, NegTokenRespTag);
            DerEncodingHelper.WriteLength(buffer, ref offset, constructionLength);
            ByteWriter.WriteByte(buffer, ref offset, (byte)DerEncodingTag.Sequence);
            DerEncodingHelper.WriteLength(buffer, ref offset, sequenceLength);
            if (NegState.HasValue)
            {
                WriteNegState(buffer, ref offset, NegState.Value);
            }
            if (SupportedMechanism != null)
            {
                WriteSupportedMechanism(buffer, ref offset, SupportedMechanism);
            }
            if (ResponseToken != null)
            {
                WriteResponseToken(buffer, ref offset, ResponseToken);
            }
            if (MechanismListMIC != null)
            {
                WriteMechanismListMIC(buffer, ref offset, MechanismListMIC);
            }
            return(buffer);
        }
Exemple #2
0
        public override byte[] GetBytes()
        {
            int sequenceLength              = GetTokenFieldsLength();
            int sequenceLengthFieldSize     = DerEncodingHelper.GetLengthFieldSize(sequenceLength);
            int constructionLength          = 1 + sequenceLengthFieldSize + sequenceLength;
            int constructionLengthFieldSize = DerEncodingHelper.GetLengthFieldSize(constructionLength);
            int bufferSize = 1 + constructionLengthFieldSize + 1 + sequenceLengthFieldSize + sequenceLength;

            byte[] buffer = new byte[bufferSize];
            int    offset = 0;

            ByteWriter.WriteByte(buffer, ref offset, NegTokenInitTag);
            DerEncodingHelper.WriteLength(buffer, ref offset, constructionLength);
            ByteWriter.WriteByte(buffer, ref offset, (byte)DerEncodingTag.Sequence);
            DerEncodingHelper.WriteLength(buffer, ref offset, sequenceLength);
            if (MechanismTypeList != null)
            {
                WriteMechanismTypeList(buffer, ref offset, MechanismTypeList);
            }
            if (MechanismToken != null)
            {
                WriteMechanismToken(buffer, ref offset, MechanismToken);
            }
            if (MechanismListMIC != null)
            {
                WriteMechanismListMIC(buffer, ref offset, MechanismListMIC);
            }
            return(buffer);
        }
        private int GetTokenFieldsLength()
        {
            int result = 0;

            if (NegState.HasValue)
            {
                int negStateLength = 5;
                result += negStateLength;
            }
            if (SupportedMechanism != null)
            {
                int supportedMechanismBytesLengthFieldSize        = DerEncodingHelper.GetLengthFieldSize(SupportedMechanism.Length);
                int supportedMechanismConstructionLength          = 1 + supportedMechanismBytesLengthFieldSize + SupportedMechanism.Length;
                int supportedMechanismConstructionLengthFieldSize = DerEncodingHelper.GetLengthFieldSize(supportedMechanismConstructionLength);
                int supportedMechanismLength = 1 + supportedMechanismConstructionLengthFieldSize + 1 + supportedMechanismBytesLengthFieldSize + SupportedMechanism.Length;
                result += supportedMechanismLength;
            }
            if (ResponseToken != null)
            {
                int responseTokenBytesLengthFieldSize        = DerEncodingHelper.GetLengthFieldSize(ResponseToken.Length);
                int responseTokenConstructionLength          = 1 + responseTokenBytesLengthFieldSize + ResponseToken.Length;
                int responseTokenConstructionLengthFieldSize = DerEncodingHelper.GetLengthFieldSize(responseTokenConstructionLength);
                int responseTokenLength = 1 + responseTokenConstructionLengthFieldSize + 1 + responseTokenBytesLengthFieldSize + ResponseToken.Length;
                result += responseTokenLength;
            }
            return(result);
        }
        protected virtual int GetTokenFieldsLength()
        {
            int result = 0;

            if (MechanismTypeList != null)
            {
                int typeListSequenceLength              = GetMechanismTypeListSequenceLength(MechanismTypeList);
                int typeListSequenceLengthFieldSize     = DerEncodingHelper.GetLengthFieldSize(typeListSequenceLength);
                int typeListConstructionLength          = 1 + typeListSequenceLengthFieldSize + typeListSequenceLength;
                int typeListConstructionLengthFieldSize = DerEncodingHelper.GetLengthFieldSize(typeListConstructionLength);
                int entryLength = 1 + typeListConstructionLengthFieldSize + 1 + typeListSequenceLengthFieldSize + typeListSequenceLength;
                result += entryLength;
            }
            if (MechanismToken != null)
            {
                int mechanismTokenLengthFieldSize             = DerEncodingHelper.GetLengthFieldSize(MechanismToken.Length);
                int mechanismTokenConstructionLength          = 1 + mechanismTokenLengthFieldSize + MechanismToken.Length;
                int mechanismTokenConstructionLengthFieldSize = DerEncodingHelper.GetLengthFieldSize(mechanismTokenConstructionLength);
                int entryLength = 1 + mechanismTokenConstructionLengthFieldSize + 1 + mechanismTokenLengthFieldSize + MechanismToken.Length;;
                result += entryLength;
            }
            if (MechanismListMIC != null)
            {
                int mechanismListMICLengthFieldSize             = DerEncodingHelper.GetLengthFieldSize(MechanismListMIC.Length);
                int mechanismListMICConstructionLength          = 1 + mechanismListMICLengthFieldSize + MechanismListMIC.Length;
                int mechanismListMICConstructionLengthFieldSize = DerEncodingHelper.GetLengthFieldSize(mechanismListMICConstructionLength);
                int entryLength = 1 + mechanismListMICConstructionLengthFieldSize + 1 + mechanismListMICLengthFieldSize + MechanismListMIC.Length;
                result += entryLength;
            }
            return(result);
        }
Exemple #5
0
        private static void WriteMechanismListMIC(byte[] buffer, ref int offset, byte[] mechanismListMIC)
        {
            int mechanismListMICLengthFieldSize = DerEncodingHelper.GetLengthFieldSize(mechanismListMIC.Length);

            ByteWriter.WriteByte(buffer, ref offset, MechanismListMICTag);
            DerEncodingHelper.WriteLength(buffer, ref offset, 1 + mechanismListMICLengthFieldSize + mechanismListMIC.Length);
            ByteWriter.WriteByte(buffer, ref offset, (byte)DerEncodingTag.ByteArray);
            DerEncodingHelper.WriteLength(buffer, ref offset, mechanismListMIC.Length);
            ByteWriter.WriteBytes(buffer, ref offset, mechanismListMIC);
        }
Exemple #6
0
        private static void WriteMechanismToken(byte[] buffer, ref int offset, byte[] mechanismToken)
        {
            int constructionLength = 1 + DerEncodingHelper.GetLengthFieldSize(mechanismToken.Length) + mechanismToken.Length;

            ByteWriter.WriteByte(buffer, ref offset, MechanismTokenTag);
            DerEncodingHelper.WriteLength(buffer, ref offset, constructionLength);
            ByteWriter.WriteByte(buffer, ref offset, (byte)DerEncodingTag.ByteArray);
            DerEncodingHelper.WriteLength(buffer, ref offset, mechanismToken.Length);
            ByteWriter.WriteBytes(buffer, ref offset, mechanismToken);
        }
        private static void WriteResponseToken(byte[] buffer, ref int offset, byte[] responseToken)
        {
            int responseTokenLengthFieldSize = DerEncodingHelper.GetLengthFieldSize(responseToken.Length);

            ByteWriter.WriteByte(buffer, ref offset, ResponseTokenTag);
            DerEncodingHelper.WriteLength(buffer, ref offset, 1 + responseTokenLengthFieldSize + responseToken.Length);
            ByteWriter.WriteByte(buffer, ref offset, (byte)DerEncodingTag.ByteArray);
            DerEncodingHelper.WriteLength(buffer, ref offset, responseToken.Length);
            ByteWriter.WriteBytes(buffer, ref offset, responseToken);
        }
        private static void WriteSupportedMechanism(byte[] buffer, ref int offset, byte[] supportedMechanism)
        {
            int supportedMechanismLengthFieldSize = DerEncodingHelper.GetLengthFieldSize(supportedMechanism.Length);

            ByteWriter.WriteByte(buffer, ref offset, SupportedMechanismTag);
            DerEncodingHelper.WriteLength(buffer, ref offset, 1 + supportedMechanismLengthFieldSize + supportedMechanism.Length);
            ByteWriter.WriteByte(buffer, ref offset, (byte)DerEncodingTag.ObjectIdentifier);
            DerEncodingHelper.WriteLength(buffer, ref offset, supportedMechanism.Length);
            ByteWriter.WriteBytes(buffer, ref offset, supportedMechanism);
        }
Exemple #9
0
        private static void WriteHintAddress(byte[] buffer, ref int offset, byte[] hintAddress)
        {
            int constructionLength = 1 + DerEncodingHelper.GetLengthFieldSize(hintAddress.Length) + hintAddress.Length;

            ByteWriter.WriteByte(buffer, ref offset, HintAddressTag);
            DerEncodingHelper.WriteLength(buffer, ref offset, constructionLength);
            ByteWriter.WriteByte(buffer, ref offset, (byte)DerEncodingTag.ByteArray);
            DerEncodingHelper.WriteLength(buffer, ref offset, hintAddress.Length);
            ByteWriter.WriteBytes(buffer, ref offset, hintAddress);
        }
Exemple #10
0
        private static void WriteHintName(byte[] buffer, ref int offset, string hintName)
        {
            byte[] hintNameBytes      = DerEncodingHelper.EncodeGeneralString(hintName);
            int    constructionLength = 1 + DerEncodingHelper.GetLengthFieldSize(hintNameBytes.Length) + hintNameBytes.Length;

            ByteWriter.WriteByte(buffer, ref offset, HintNameTag);
            DerEncodingHelper.WriteLength(buffer, ref offset, constructionLength);
            ByteWriter.WriteByte(buffer, ref offset, (byte)DerEncodingTag.GeneralString);
            DerEncodingHelper.WriteLength(buffer, ref offset, hintNameBytes.Length);
            ByteWriter.WriteBytes(buffer, ref offset, hintNameBytes);
        }
Exemple #11
0
        private static int GetSequenceLength(List <byte[]> mechanismTypeList)
        {
            int sequenceLength = 0;

            foreach (byte[] mechanismType in mechanismTypeList)
            {
                int lengthFieldSize = DerEncodingHelper.GetLengthFieldSize(mechanismType.Length);
                int entryLength     = 1 + lengthFieldSize + mechanismType.Length;
                sequenceLength += entryLength;
            }
            return(sequenceLength);
        }
Exemple #12
0
        protected override int GetTokenFieldsLength()
        {
            int result = base.GetTokenFieldsLength();;

            if (HintName != null || HintAddress != null)
            {
                int hintsSequenceLength                      = GetHintsSequenceLength(HintName, HintAddress);
                int hintsSequenceLengthFieldSize             = DerEncodingHelper.GetLengthFieldSize(hintsSequenceLength);
                int hintsSequenceConstructionLength          = 1 + hintsSequenceLengthFieldSize + hintsSequenceLength;
                int hintsSequenceConstructionLengthFieldSize = DerEncodingHelper.GetLengthFieldSize(hintsSequenceConstructionLength);
                int entryLength = 1 + hintsSequenceConstructionLengthFieldSize + 1 + hintsSequenceLengthFieldSize + hintsSequenceLength;
                result += entryLength;
            }
            return(result);
        }
Exemple #13
0
        private static void WriteMechanismTypeList(byte[] buffer, ref int offset, List <byte[]> mechanismTypeList)
        {
            int sequenceLength          = GetSequenceLength(mechanismTypeList);
            int sequenceLengthFieldSize = DerEncodingHelper.GetLengthFieldSize(sequenceLength);
            int constructionLength      = 1 + sequenceLengthFieldSize + sequenceLength;

            ByteWriter.WriteByte(buffer, ref offset, MechanismTypeListTag);
            DerEncodingHelper.WriteLength(buffer, ref offset, constructionLength);
            ByteWriter.WriteByte(buffer, ref offset, (byte)DerEncodingTag.Sequence);
            DerEncodingHelper.WriteLength(buffer, ref offset, sequenceLength);
            foreach (byte[] mechanismType in mechanismTypeList)
            {
                ByteWriter.WriteByte(buffer, ref offset, (byte)DerEncodingTag.ObjectIdentifier);
                DerEncodingHelper.WriteLength(buffer, ref offset, mechanismType.Length);
                ByteWriter.WriteBytes(buffer, ref offset, mechanismType);
            }
        }
Exemple #14
0
        private static void WriteHints(byte[] buffer, ref int offset, string hintName, byte[] hintAddress)
        {
            int sequenceLength          = GetHintsSequenceLength(hintName, hintAddress);
            int sequenceLengthFieldSize = DerEncodingHelper.GetLengthFieldSize(sequenceLength);
            int constructionLength      = 1 + sequenceLengthFieldSize + sequenceLength;

            ByteWriter.WriteByte(buffer, ref offset, NegHintsTag);
            DerEncodingHelper.WriteLength(buffer, ref offset, constructionLength);
            ByteWriter.WriteByte(buffer, ref offset, (byte)DerEncodingTag.Sequence);
            DerEncodingHelper.WriteLength(buffer, ref offset, sequenceLength);
            if (hintName != null)
            {
                WriteHintName(buffer, ref offset, hintName);
            }
            if (hintAddress != null)
            {
                WriteHintAddress(buffer, ref offset, hintAddress);
            }
        }
Exemple #15
0
        private int GetTokenFieldsLength()
        {
            int result = 0;

            if (MechanismTypeList != null)
            {
                int typeListSequenceLength              = GetSequenceLength(MechanismTypeList);
                int typeListSequenceLengthFieldSize     = DerEncodingHelper.GetLengthFieldSize(typeListSequenceLength);
                int typeListConstructionLength          = 1 + typeListSequenceLengthFieldSize + typeListSequenceLength;
                int typeListConstructionLengthFieldSize = DerEncodingHelper.GetLengthFieldSize(typeListConstructionLength);
                int typeListLength = 1 + typeListConstructionLengthFieldSize + 1 + typeListSequenceLengthFieldSize + typeListSequenceLength;
                result += typeListLength;
            }
            if (MechanismToken != null)
            {
                int mechanismTokenBytesFieldSize              = DerEncodingHelper.GetLengthFieldSize(MechanismToken.Length);
                int mechanismTokenConstructionLength          = 1 + mechanismTokenBytesFieldSize + MechanismToken.Length;
                int mechanismTokenConstructionLengthFieldSize = DerEncodingHelper.GetLengthFieldSize(mechanismTokenConstructionLength);
                int tokenLength = 1 + mechanismTokenConstructionLengthFieldSize + 1 + mechanismTokenBytesFieldSize + MechanismToken.Length;
                result += tokenLength;
            }
            return(result);
        }
Exemple #16
0
        protected static int GetHintsSequenceLength(string hintName, byte[] hintAddress)
        {
            int sequenceLength = 0;

            if (hintName != null)
            {
                byte[] hintNameBytes               = DerEncodingHelper.EncodeGeneralString(hintName);
                int    lengthFieldSize             = DerEncodingHelper.GetLengthFieldSize(hintNameBytes.Length);
                int    constructionLength          = 1 + lengthFieldSize + hintNameBytes.Length;
                int    constructionLengthFieldSize = DerEncodingHelper.GetLengthFieldSize(constructionLength);
                int    entryLength = 1 + constructionLengthFieldSize + 1 + lengthFieldSize + hintNameBytes.Length;
                sequenceLength += entryLength;
            }
            if (hintAddress != null)
            {
                int lengthFieldSize             = DerEncodingHelper.GetLengthFieldSize(hintAddress.Length);
                int constructionLength          = 1 + lengthFieldSize + hintAddress.Length;
                int constructionLengthFieldSize = DerEncodingHelper.GetLengthFieldSize(constructionLength);
                int entryLength = 1 + constructionLengthFieldSize + 1 + lengthFieldSize + hintAddress.Length;
                sequenceLength += entryLength;
            }
            return(sequenceLength);
        }
 /// <summary>
 /// Will append the generic GSSAPI header.
 /// </summary>
 public static byte[] GetTokenBytes(SimpleProtectedNegotiationToken token)
 {
     if (token is SimpleProtectedNegotiationTokenInit)
     {
         byte[] tokenBytes = token.GetBytes();
         int    objectIdentifierFieldSize = DerEncodingHelper.GetLengthFieldSize(SPNEGOIdentifier.Length);
         int    tokenLength          = 1 + objectIdentifierFieldSize + SPNEGOIdentifier.Length + tokenBytes.Length;
         int    tokenLengthFieldSize = DerEncodingHelper.GetLengthFieldSize(tokenLength);
         int    headerLength         = 1 + tokenLengthFieldSize + 1 + objectIdentifierFieldSize + SPNEGOIdentifier.Length;
         byte[] buffer = new byte[headerLength + tokenBytes.Length];
         int    offset = 0;
         ByteWriter.WriteByte(buffer, ref offset, ApplicationTag);
         DerEncodingHelper.WriteLength(buffer, ref offset, tokenLength);
         ByteWriter.WriteByte(buffer, ref offset, (byte)DerEncodingTag.ObjectIdentifier);
         DerEncodingHelper.WriteLength(buffer, ref offset, SPNEGOIdentifier.Length);
         ByteWriter.WriteBytes(buffer, ref offset, SPNEGOIdentifier);
         ByteWriter.WriteBytes(buffer, ref offset, tokenBytes);
         return(buffer);
     }
     else
     {
         return(token.GetBytes());
     }
 }
 /// <param name="includeHeader">Prepend the generic GSSAPI header. Required for negTokenInit, optional for negTokenResp.</param>
 public byte[] GetBytes(bool includeHeader)
 {
     byte[] tokenBytes = this.GetBytes();
     if (includeHeader)
     {
         int    objectIdentifierFieldSize = DerEncodingHelper.GetLengthFieldSize(SPNEGOIdentifier.Length);
         int    tokenLength          = 1 + objectIdentifierFieldSize + SPNEGOIdentifier.Length + tokenBytes.Length;
         int    tokenLengthFieldSize = DerEncodingHelper.GetLengthFieldSize(tokenLength);
         int    headerLength         = 1 + tokenLengthFieldSize + 1 + objectIdentifierFieldSize + SPNEGOIdentifier.Length;
         byte[] buffer = new byte[headerLength + tokenBytes.Length];
         int    offset = 0;
         ByteWriter.WriteByte(buffer, ref offset, ApplicationTag);
         DerEncodingHelper.WriteLength(buffer, ref offset, tokenLength);
         ByteWriter.WriteByte(buffer, ref offset, (byte)DerEncodingTag.ObjectIdentifier);
         DerEncodingHelper.WriteLength(buffer, ref offset, SPNEGOIdentifier.Length);
         ByteWriter.WriteBytes(buffer, ref offset, SPNEGOIdentifier);
         ByteWriter.WriteBytes(buffer, ref offset, tokenBytes);
         return(buffer);
     }
     else
     {
         return(tokenBytes);
     }
 }