Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7655599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:33:10+00:00 2026-05-31T12:33:10+00:00

I have to debug an old PHP script from a developer who has left

  • 0

I have to debug an old PHP script from a developer who has left the company. I understand the most part of the code, except the following function. My question: What does…

if($seq == 0x03 || $seq == 0x30)

…mean in context of extracting the signature out of an X.509 certificate?

public function extractSignature($certPemString) {

    $bin = $this->ConvertPemToBinary($certPemString);

    if(empty($certPemString) || empty($bin))
    {
        return false;
    }    

    $bin = substr($bin,4);

    while(strlen($bin) > 1) 
    {            
        $seq = ord($bin[0]); 
        if($seq == 0x03 || $seq == 0x30) 
        {            
            $len = ord($bin[1]);
            $bytes = 0;

            if ($len & 0x80)
            {
                $bytes = ($len & 0x0f);
                $len = 0;
                for ($i = 0; $i < $bytes; $i++)
                {
                    $len = ($len << 8) | ord($bin[$i + 2]);
                }
            }

            if($seq == 0x03)
            {
                return substr($bin,3 + $bytes, $len);
            }
            else 
            {
                $bin = substr($bin,2 + $bytes + $len);                  
            }                                                    
        }
        else 
        {                            
            return false;                
        }
    }
    return false;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:33:11+00:00Added an answer on May 31, 2026 at 12:33 pm

    An X.509 certificate contains data in multiple sections (called Tag-Length-Value triplets). Each section starts with a Tag byte, which indicates the data format of the section. You can see a list of these data types here.

    0x03 is the Tag byte for the BIT STRING data type, and 0x30 is the Tag byte for the SEQUENCE data type.

    So this code is designed to handle the BIT STRING and SEQUENCE data types. If you look at this part:

    if($seq == 0x03)
    {
        return substr($bin,3 + $bytes, $len);
    }
    else // $seq == 0x30
    {
        $bin = substr($bin,2 + $bytes + $len);                  
    }
    

    you can see that the function is designed to skip over Sequences (0x30), until it finds a Bit String (0x03), at which point it returns the value of the Bit String.

    You might be wondering why the magic number is 3 for Bit String and 2 for Sequence. That is because in a Bit String, the first value byte is a special extra field which indicates how many bits are unused in the last byte of the data. (For example, if you’re sending 13 bits of data, it will take up 2 bytes = 16 bits, and the “unused bits” field will be 3.)

    Next issue: the Length field. When the length of the Value is less than 128 bytes, the length is simply specified using a single byte (the most significant bit will be 0). If the length is 128 or greater, then the first length byte has bit 7 set, and the remaining 7 bits indicates how many following bytes contain the length (in big-endian order). More description here. The parsing of the length field happens in this section of the code:

    $len = ord($bin[1]);
    $bytes = 0;
    
    if ($len & 0x80)
    {
        // length is greater than 127!
        $bytes = ($len & 0x0f);
        $len = 0;
        for ($i = 0; $i < $bytes; $i++)
        {
             $len = ($len << 8) | ord($bin[$i + 2]);
        }
    }
    

    After that, $bytes contains the number of extra bytes used by the length field, and $len contains the length of the Value field (in bytes).

    Did you spot the error in the code? Remember,

    If the length is 128 or greater, then the first length byte has bit 7
    set, and the remaining 7 bits indicates how many following bytes
    contain the length.

    but the code says $bytes = ($len & 0x0f), which only takes the lower 4 bits of the byte! It should be:

    $bytes = ($len & 0x7f);
    

    Of course, this error is only a problem for extremely long messages: it will work fine as long as the length value will fit within 0x0f = 15 bytes, meaning the data has to be less than 256^15 bytes. That’s about a trillion yottabytes, which ought to be enough for anybody.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some old javascript code from around 2000-2002 which (surprisingly) still works in
Below is the plugin code that i have tried to reuse from an old
So I know now that the debug assemblies have been intentionally left out of
I have been helping someone debug some code where the error message was Day
I have some projects from XNA 1.0 that I wanted to debug but when
I have working kernel debug setup on old host computer with Windows Xp. On
I have just switched over from the old png transparency fix using a htc
I have a solution that has a plain old asp.net website and a winforms
I have an old C++ COM component which has to stay in Visual Studio
I have a C++ code base that has been working for a long time.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.