Opc.Ua.CertificateIdentifier.IsValidCertificateBlob C# (CSharp) Method

IsValidCertificateBlob() private static method

Checks if the certificate data represents a valid X509v3 certificate.
private static IsValidCertificateBlob ( byte rawData ) : bool
rawData byte The raw data of a object.
return bool
        private static bool IsValidCertificateBlob(byte[] rawData)
        {
            // check for header.
            if (rawData == null || rawData.Length < 4)
            {
                return false;
            }

            // check for ASN.1 header.
            if (rawData[0] != 0x30)
            {
                return false;
            }

            // extract length.
            int length = 0;
            byte octet = rawData[1];

            // check for short for encoding.
            if ((octet & 0x80) == 0)
            {
                length = octet & 0x7F;

                if (2+length < rawData.Length)
                {
                    return false;
                }

                return true;
            }

            // extract number of bytes for the length.
            int lengthBytes = octet & 0x7F;
            
            if (rawData.Length <= 2+lengthBytes)
            {
                return false;
            }

            // check for unexpected negative number.
            if ((rawData[2] & 0x80) != 0)
            {
                return false;
            }

            // extract length.
            length = rawData[2];

            for (int ii = 0; ii < lengthBytes-1; ii++)
            {
                length <<= 8;
                length |= rawData[ii+3];
            }

            if (2+lengthBytes+length > rawData.Length)
            {
                return false;
            }

            // potentially valid.
            return true;
        }
        #endregion