Example #1
0
        private int ProcessOneTag( int Index, byte[] TagsBuffer )
        {
            try
            {
            StatusString += "Top of ProcessOneTag().\r\n";

            if( IntMath == null )
              IntMath = new IntegerMath();

            if( TagsBuffer == null )
              {
              StatusString += "Tags buffer was null.\r\n";
              return -1;
              }

            if( ObjectIDNames == null )
              ObjectIDNames = new X509ObjectIDNames();

            if( Index < 0 )
              {
              StatusString += "Index < 0.\r\n";
              return -1;
              }

            if( Index >= TagsBuffer.Length )
              {
              StatusString += "It's past the end of the buffer.\r\n";
              return -1;
              }

            StatusString += "Index: " + Index.ToString() + "\r\n";

            int Tag = TagsBuffer[Index];
            Index++;

            int ClassBits = Tag & 0xC0;
            ClassBits >>= 6;
            StatusString += "ClassBits: " + ClassBits.ToString( "X0" ) + "\r\n";

            int PrimitiveOrConstructed = Tag & 0x20;
            if( (PrimitiveOrConstructed & 0x20) == 0 )
              StatusString += "Tag is a Primitive type.\r\n";
            else
              StatusString += "Tag is a Constructed type.\r\n";

            int TagVal = Tag & 0x1F; // Bottom 5 bits.
            ShowTagType( TagVal );
            MostRecentTagValue = TagVal;

            int Length = TagsBuffer[Index];
            Index++;
            if( (Length & 0x80) != 0 )
              {
              // Then it's in the long form.
              int HowManyBytes = Length & 0x7F;
              if( HowManyBytes > 4 )
            throw( new Exception( "This can't be right for HowManyBytes: " + HowManyBytes.ToString() ));

              StatusString += "Long form HowManyBytes: " + HowManyBytes.ToString() + "\r\n";

              Length = 0;
              for( int Count = 0; Count < HowManyBytes; Count++ )
            {
            Length <<= 8;
            Length |= TagsBuffer[Index];
            Index++;
            }
              }
            else
              {
              StatusString += "Length is in short form.\r\n";
              }

            StatusString += "Length is: " + Length.ToString() + "\r\n";
            if( Length > TagsBuffer.Length )
              {
              StatusString += "The Length is not a reasonable number.\r\n";
              return -1;
              }

            if( // (TagVal == 0) || // That context specific tag.
            (TagVal == 1) || // Boolean
            // (TagVal == 4) || // Octet String
            (TagVal == 5) || // Null (Should have a length of zero.)
            (TagVal == 7) || // Object Descriptor
            // (TagVal == 8) || // External
            (TagVal == 9) || // Real/Float
            (TagVal == 10) || // Enumerated
            (TagVal == 11) || // Embedded PDV
            (TagVal == 12) || // UTF8 String
            (TagVal == 13) || // Relative OID
            // (TagVal == 14) || // Reserved
            // (TagVal == 15) || // Reserved
            // (TagVal == 16) || // Sequence
            // (TagVal == 17) || // Set
            (TagVal == 18) || // Numeric String
            (TagVal == 20) || // T61 String
            (TagVal == 21) || // Videotex String
            // (TagVal == 22) || // IA5 String
            (TagVal == 23) || // UTC Time
            (TagVal == 24) || // Generalized Time
            (TagVal == 25) || // Graphic String
            (TagVal == 26) || // Visible String
            (TagVal == 27) || // General String
            (TagVal == 28) || // Universal String
            (TagVal == 29) || // Character String
            (TagVal == 30)) // BMP String
              {
              for( int Count = 0; Count < Length; Count++ )
            {
            int ContentByte = TagsBuffer[Index];
            Index++;
            StatusString += "  Byte: 0x" + ContentByte.ToString( "X2" ) + "\r\n";
            }
              }

            if( TagVal == 2 ) // Integer
              {
              // This length was already checked above to see if it's a reasonable
              // number.
              byte[] BytesToSet = new byte[Length];
              try
              {
              for( int Count = 0; Count < Length; Count++ )
            {
            BytesToSet[Count] = TagsBuffer[Index];
            Index++;
            }

              }
              catch( Exception Except )
            {
            // Probably over-ran the buffer.
            StatusString += "Exception at: BytesToSet[Count] = TagsBuffer[Index].\r\nIn DomainX509Record.ProcessOneTag().\r\n";
            StatusString += Except.Message + "\r\n";
            return -1;
            }

              try
              {
              MostRecentIntegerValue.SetFromBigEndianByteArray( BytesToSet );
              StatusString += "MostRecentIntegerValue: " + IntMath.ToString10( MostRecentIntegerValue ) + "\r\n";
              }
              catch( Exception Except )
            {
            // Probably over-ran the buffer.
            StatusString += "Exception at: SetFromBigEndianByteArray().\r\nIn DomainX509Record.ProcessOneTag().\r\n";
            StatusString += Except.Message + "\r\n";
            return -1;
            }
              }

            if( TagVal == 3 ) // bit String
              {
              // This length was already checked above to see if it's a reasonable
              // number.
              byte[] BytesToSet = new byte[Length];
              try
              {
              for( int Count = 0; Count < Length; Count++ )
            {
            BytesToSet[Count] = TagsBuffer[Index];
            Index++;
            }

              }
              catch( Exception Except )
            {
            // Probably over-ran the buffer.
            StatusString += "Exception at: BytesToSet[Count] = TagsBuffer[Index].\r\nIn DomainX509Record.ProcessOneTag().\r\n";
            StatusString += Except.Message + "\r\n";
            return -1;
            }

              try
              {
              BitStreamRec Rec = new BitStreamRec();
              Rec.BitStream = BytesToSet;
              Rec.OIDString = MostRecentObjectIDString;
              AddBitStreamRec( Rec );
              }
              catch( Exception Except )
            {
            // Probably over-ran the buffer.
            StatusString += "Exception at: AddBitStreamRec().\r\nIn DomainX509Record.ProcessOneTag().\r\n";
            StatusString += Except.Message + "\r\n";
            return -1;
            }
              }

            // Just a rough draft, to see what's in them.
            if( TagVal == 4 ) // Octet String
              {
              for( int Count = 0; Count < Length; Count++ )
            {
            char ContentChar = (char)TagsBuffer[Index];
            Index++;

            // Make sure it has valid ASCII characters.
            if( ContentChar > 126 )
              continue;

            if( ContentChar < 32 ) // Space character.
              continue;

            StatusString += Char.ToString( ContentChar );
            }

              StatusString += "\r\n";
              }

            // UTF8String.

            if( TagVal == 19 ) // Printable String
              {
              for( int Count = 0; Count < Length; Count++ )
            {
            char ContentChar = (char)TagsBuffer[Index];
            Index++;

            // Make sure it has valid ASCII characters.
            if( ContentChar > 126 ) // 127 is the ASCII DEL character.
              continue;

            if( ContentChar < 32 ) // Space character.
              continue;

            StatusString += Char.ToString( ContentChar );
            }

              StatusString += "\r\n";
              }

            if( TagVal == 22 ) // IA5 String
              {
              for( int Count = 0; Count < Length; Count++ )
            {
            char ContentChar = (char)TagsBuffer[Index];
            Index++;

            // Make sure it has valid ASCII characters.
            if( ContentChar > 126 )
              continue;

            if( ContentChar < 32 ) // Space character.
              continue;

            StatusString += Char.ToString( ContentChar );
            }

              StatusString += "\r\n";
              }

            if( TagVal == 6 ) // Object Indentifier
              {
              if( Length < 2 )
            {
            StatusString += "The Length can't be right for this OID.\r\n";
            return -1;
            }

              byte[] CodedBytes = new byte[Length];
              for( int Count = 0; Count < Length; Count++ )
            {
            CodedBytes[Count] = TagsBuffer[Index];
            Index++;
            }

              X509ObjectID X509ID = new X509ObjectID();
              if( !X509ID.MakeFromBytes( CodedBytes ))
            {
            StatusString += "X509ID.MakeFromBytes() returned false.\r\n";
            return -1;
            }

              MostRecentObjectIDString = X509ID.GetStringValue();
              StatusString += X509ID.GetStatusString();
              StatusString += "StringValue is: " + X509ID.GetStringValue() + "\r\n";
              StatusString += "OID Name is: " + ObjectIDNames.GetNameFromDictionary( X509ID.GetStringValue()) + "\r\n";
              }

            StatusString += "\r\n";
            return Index;

            }
            catch( Exception Except )
              {
              // Probably over-ran the buffer.
              StatusString += "Exception in DomainX509Record.ProcessOneTag().\r\n";
              StatusString += Except.Message + "\r\n";
              return -1;
              }
        }
Example #2
0
        internal void AddBitStreamRec( BitStreamRec Rec )
        {
            // if( Rec == null )
              // return false;

            BitStreamRecArray[BitStreamRecArrayLast] = Rec;
            BitStreamRecArrayLast++;

            if( BitStreamRecArrayLast >= BitStreamRecArray.Length )
              {
              try
              {
              Array.Resize( ref BitStreamRecArray, BitStreamRecArray.Length + 8 );
              }
              catch( Exception Except )
            {
            throw( new Exception( "Couldn't resize the arrays for AddBitStreamRec(). " + Except.Message ));
            }
              }
        }