Functions to parse ASN.1 to native objects ----- Parse and return type, data length and header length.
function typeAndLength(buf) {
    var res, len, i;

    res = { type: buf[0], len: 0, header: 1 };
    if (buf[1] < 128) {
        // If bit 8 is zero, this byte indicates the content length (up to 127 bytes).
        res.len = buf[1];
        res.header += 1;
    } else {
        // If bit 8 is 1, bits 0 to 7 indicate the number of following legth bytes.
        // These bytes are a simple msb base-256 integer indicating the content length.
        for (i = 0; i < buf[1] - 128; i++) {
            res.len *= 256;
            res.len += buf[i + 2];
        }
        res.header += buf[1] - 128 + 1;
    }
    return res;
}