Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle context-specific tag incorrect #38

Closed
cuserdzy opened this issue Dec 6, 2020 · 9 comments
Closed

handle context-specific tag incorrect #38

cuserdzy opened this issue Dec 6, 2020 · 9 comments
Labels

Comments

@cuserdzy
Copy link

cuserdzy commented Dec 6, 2020

I have a signed data encoded by DER, but get strange output while parse

pub fn parse_signed_data(input: &[u8]) {
    let (_, top_object) = parse_der(input).unwrap();

    let pretty_object = top_object.as_pretty(4, 4);
    println!("{:?}", pretty_object);
}

The input

        let raw_bytes = [
            0x30, 0x82, 0x05, 0x20, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x02, 0xA0,
            0x82, 0x05, 0x11, 0x30, 0x82, 0x05, 0x0D, 0x02, 0x01, 0x01, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x09,
            0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x30, 0x0B, 0x06, 0x09, 0x2A,
            0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01, 0xA0, 0x82, 0x03, 0x57, 0x30, 0x82, 0x03, 0x53,
            0x30, 0x82, 0x02, 0x3B, 0xA0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x04, 0x77, 0xA4, 0xD5, 0xE2, 0x30,
            0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x05, 0x00, 0x30, 0x5A,
            0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x43, 0x4E, 0x31, 0x0B, 0x30,
            0x09, 0x06, 0x03, 0x55, 0x04, 0x08, 0x13, 0x02, 0x53, 0x48, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03,
            0x55, 0x04, 0x07, 0x13, 0x02, 0x53, 0x48, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x0A,
            0x13, 0x06, 0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04,
            0x0B, 0x13, 0x06, 0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55,
            0x04, 0x03, 0x13, 0x06, 0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x30, 0x1E, 0x17, 0x0D, 0x32, 0x30,
            0x31, 0x31, 0x30, 0x38, 0x30, 0x36, 0x33, 0x39, 0x34, 0x33, 0x5A, 0x17, 0x0D, 0x33, 0x30, 0x31,
            0x31, 0x30, 0x36, 0x30, 0x36, 0x33, 0x39, 0x34, 0x33, 0x5A, 0x30, 0x5A, 0x31, 0x0B, 0x30, 0x09,
            0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x43, 0x4E, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55,
            0x04, 0x08, 0x13, 0x02, 0x53, 0x48, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x07, 0x13,
            0x02, 0x53, 0x48, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x13, 0x06, 0x71, 0x71,
            0x71, 0x71, 0x71, 0x71, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13, 0x06, 0x71,
            0x71, 0x71, 0x71, 0x71, 0x71, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x06,
            0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x30, 0x82, 0x01, 0x22, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86,
            0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0F, 0x00, 0x30, 0x82,
            0x01, 0x0A, 0x02, 0x82, 0x01, 0x01, 0x00, 0xA7, 0xE0, 0x84, 0x4E, 0x72, 0x99, 0xE0, 0x0F, 0x4A,
            0x91, 0x06, 0x08, 0x03, 0xC7, 0x8C, 0x10, 0xB5, 0x54, 0x20, 0x03, 0x63, 0x06, 0xCB, 0x03, 0x1F,
            0xB3, 0xA5, 0xAE, 0x5A, 0x4D, 0x47, 0xA2, 0x38, 0x3E, 0x57, 0xBD, 0xFF, 0x3F, 0x0E, 0xC0, 0xCD,
            0x82, 0x17, 0x18, 0x72, 0x71, 0xE4, 0x4B, 0xDE, 0xEC, 0x3E, 0xAF, 0xFD, 0x8D, 0x36, 0xF0, 0xFF,
            0x49, 0xE5, 0x5F, 0x6A, 0xB2, 0xE7, 0xCA, 0xBA, 0xF1, 0x0F, 0x65, 0x85, 0x92, 0xA6, 0xA2, 0xF9,
            0x3B, 0xBC, 0xA3, 0xC1, 0x46, 0x42, 0xDF, 0x34, 0x30, 0x3A, 0x90, 0xA5, 0x0A, 0xD2, 0x41, 0xD1,
            0xD7, 0x0F, 0xC0, 0xC2, 0x08, 0x41, 0x71, 0x16, 0x88, 0x4E, 0x4F, 0x5C, 0x48, 0xE1, 0xDE, 0x08,
            0xB2, 0x1F, 0x10, 0x3B, 0xB2, 0x25, 0x35, 0xDB, 0xCF, 0xFD, 0x16, 0x4F, 0x5A, 0x9D, 0x2F, 0xF6,
            0xCE, 0xC9, 0xB9, 0x57, 0xFE, 0x24, 0xE5, 0x52, 0x42, 0x02, 0x42, 0x1A, 0xE5, 0x42, 0xC3, 0xC5,
            0x5D, 0x4D, 0x31, 0x52, 0x22, 0x7A, 0x44, 0x67, 0x99, 0xD9, 0x8A, 0x41, 0x4E, 0xAC, 0x11, 0x80,
            0x66, 0x23, 0x47, 0x6D, 0xC3, 0x65, 0x14, 0x6C, 0x9D, 0x6F, 0x96, 0xD4, 0xDC, 0xC4, 0x85, 0x16,
            0xCC, 0x34, 0x14, 0xE7, 0x38, 0xA3, 0x9D, 0x7D, 0xB2, 0x51, 0xE1, 0xE1, 0x3C, 0x90, 0xE7, 0x26,
            0x7F, 0x12, 0x7C, 0x3B, 0xCC, 0xD3, 0x6A, 0x3F, 0xDF, 0x00, 0xFC, 0x1C, 0x7B, 0x2B, 0x48, 0x44,
            0x84, 0xA4, 0x9D, 0xEB, 0xE9, 0x2F, 0x8E, 0x26, 0x6A, 0x38, 0x6F, 0xBB, 0xF1, 0x1C, 0x2B, 0x11,
            0x29, 0x9D, 0xF0, 0x57, 0x91, 0x10, 0x60, 0xDD, 0xD2, 0xDC, 0x64, 0xAE, 0xDD, 0x94, 0x53, 0x40,
            0xBF, 0xDB, 0x11, 0xEA, 0xB0, 0x15, 0xDA, 0xD2, 0x8B, 0xAA, 0x08, 0x70, 0x4A, 0x21, 0x1E, 0x54,
            0x2C, 0xF9, 0x9E, 0x52, 0xD0, 0x6B, 0x13, 0x02, 0x03, 0x01, 0x00, 0x01, 0xA3, 0x21, 0x30, 0x1F,
            0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0xC5, 0x15, 0x25, 0x97, 0xD0,
            0x4B, 0xF9, 0x05, 0x87, 0x37, 0x95, 0x2F, 0xF1, 0x68, 0x1A, 0x22, 0xB3, 0xA7, 0x7F, 0xFC, 0x30,
            0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x05, 0x00, 0x03, 0x82,
            0x01, 0x01, 0x00, 0x54, 0x9A, 0x99, 0x39, 0x86, 0x3B, 0xC0, 0x91, 0xBB, 0x53, 0xA5, 0xB2, 0x2D,
            0x9B, 0xD3, 0x7E, 0xD1, 0x51, 0xB4, 0x5E, 0xC0, 0x36, 0x11, 0x2D, 0x52, 0x5C, 0x25, 0xDD, 0x91,
            0xD2, 0x5B, 0x42, 0xFA, 0xF6, 0x0E, 0x50, 0x30, 0x68, 0x2E, 0x86, 0xD9, 0x46, 0x19, 0x99, 0x42,
            0xD1, 0x87, 0x6F, 0xFF, 0x7E, 0xD2, 0x54, 0x93, 0x0D, 0x82, 0x6E, 0x85, 0xC2, 0x7D, 0x76, 0xA5,
            0x58, 0x57, 0x6C, 0x06, 0xE5, 0x8E, 0x05, 0xF7, 0x8B, 0x58, 0xD9, 0xBC, 0x64, 0xB0, 0x5E, 0x31,
            0x88, 0xFD, 0xB1, 0xDB, 0x63, 0x4B, 0xA1, 0x22, 0xD2, 0xA9, 0x71, 0x97, 0x34, 0x3D, 0x31, 0xE1,
            0xF7, 0xEE, 0xF4, 0xD4, 0x66, 0x6F, 0xB0, 0xD0, 0x9C, 0xDB, 0x82, 0x2A, 0x57, 0x08, 0xF7, 0x9F,
            0x33, 0x5B, 0xF3, 0x29, 0xF9, 0x21, 0xDB, 0xC6, 0x82, 0x4D, 0x09, 0x90, 0xBA, 0xA9, 0xD7, 0x7D,
            0x2B, 0xDD, 0xE8, 0xCA, 0x86, 0x21, 0xDF, 0xA5, 0xE3, 0xED, 0xED, 0x3B, 0x07, 0x2E, 0xDD, 0xBA,
            0x27, 0xB3, 0xAF, 0x4E, 0xBE, 0x09, 0x96, 0x14, 0x4A, 0xED, 0x5B, 0x5A, 0xB3, 0x6E, 0xD0, 0x1D,
            0x9E, 0x45, 0xC0, 0x70, 0xDF, 0x75, 0x7B, 0x77, 0xA1, 0x57, 0xC5, 0x76, 0x60, 0x58, 0x5C, 0xF2,
            0x2F, 0xAD, 0xF1, 0xAA, 0x15, 0xA6, 0x7D, 0x5A, 0x94, 0x95, 0xB7, 0x10, 0x81, 0xC5, 0xE4, 0x49,
            0xF1, 0x87, 0xB3, 0xA5, 0x97, 0xF8, 0xBE, 0xC4, 0xE0, 0xFD, 0x12, 0xCB, 0x14, 0x86, 0x98, 0xA0,
            0x73, 0xFE, 0xEF, 0x9B, 0xE0, 0x14, 0x6A, 0x0E, 0x06, 0xDE, 0x77, 0xE1, 0x5A, 0xC4, 0xF4, 0x4E,
            0xEB, 0x8D, 0x37, 0xF1, 0x3A, 0xCF, 0x4D, 0x4D, 0x36, 0xFC, 0xC0, 0x92, 0xA4, 0xE9, 0xA6, 0x49,
            0x7B, 0xCC, 0x93, 0x66, 0x51, 0x2A, 0x24, 0xD3, 0x15, 0x75, 0x9B, 0x6C, 0x4F, 0x60, 0x44, 0xE9,
            0x15, 0x59, 0x68, 0x31, 0x82, 0x01, 0x8D, 0x30, 0x82, 0x01, 0x89, 0x02, 0x01, 0x01, 0x30, 0x62,
            0x30, 0x5A, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x43, 0x4E, 0x31,
            0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x08, 0x13, 0x02, 0x53, 0x48, 0x31, 0x0B, 0x30, 0x09,
            0x06, 0x03, 0x55, 0x04, 0x07, 0x13, 0x02, 0x53, 0x48, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55,
            0x04, 0x0A, 0x13, 0x06, 0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03,
            0x55, 0x04, 0x0B, 0x13, 0x06, 0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x31, 0x0F, 0x30, 0x0D, 0x06,
            0x03, 0x55, 0x04, 0x03, 0x13, 0x06, 0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x02, 0x04, 0x77, 0xA4,
            0xD5, 0xE2, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
            0x00, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00,
            0x04, 0x82, 0x01, 0x00, 0x4B, 0x60, 0xBF, 0xF7, 0xF0, 0x7F, 0x3E, 0x06, 0xBE, 0xCA, 0x8F, 0xAD,
            0x7E, 0xB6, 0x1B, 0xBE, 0xE9, 0x0D, 0x27, 0x02, 0x35, 0xF1, 0x31, 0xA6, 0xF7, 0xA6, 0x08, 0x6A,
            0x48, 0xE0, 0x77, 0xA9, 0xFA, 0x30, 0xB1, 0xDB, 0xA8, 0xD9, 0x70, 0x2A, 0xA2, 0x21, 0x96, 0x02,
            0x6F, 0x45, 0x94, 0xBD, 0x75, 0xA9, 0x1C, 0xF6, 0x04, 0x61, 0xB5, 0xCD, 0x39, 0xA5, 0xAB, 0xDD,
            0x58, 0xCD, 0x50, 0x01, 0x22, 0x55, 0x04, 0x1D, 0x5A, 0x6A, 0x7A, 0xD7, 0x30, 0xA0, 0xBD, 0x36,
            0x9E, 0x9D, 0xC9, 0x66, 0xBA, 0x13, 0x75, 0xB4, 0x46, 0x28, 0xA5, 0xF0, 0x1F, 0x09, 0x36, 0x9C,
            0x05, 0x01, 0x7A, 0x66, 0x94, 0x55, 0xD4, 0x12, 0x0D, 0xC0, 0x3F, 0x77, 0x31, 0xFD, 0x06, 0x41,
            0x48, 0x43, 0xC3, 0x17, 0x7B, 0x7F, 0x7D, 0x45, 0x39, 0x73, 0x00, 0x1E, 0xC2, 0x99, 0x3A, 0x25,
            0x59, 0xB2, 0x78, 0xDD, 0x3E, 0x86, 0x06, 0xF0, 0x6E, 0x84, 0x0C, 0xB6, 0x33, 0x69, 0x73, 0x9B,
            0x52, 0xBA, 0x0B, 0x5D, 0x2F, 0x0A, 0x01, 0x30, 0x42, 0x93, 0x2E, 0x16, 0x6B, 0x9A, 0xDB, 0x82,
            0xF2, 0x30, 0x90, 0x1D, 0xC6, 0x1B, 0xB2, 0x2E, 0x19, 0x97, 0x40, 0xEC, 0xE1, 0x3F, 0xF3, 0x56,
            0xD2, 0xD3, 0xAC, 0xA6, 0xF9, 0x07, 0x5E, 0xCF, 0xBE, 0x91, 0x1B, 0xEE, 0x75, 0x30, 0xEE, 0x2B,
            0x7C, 0x45, 0x52, 0x1A, 0x76, 0x2F, 0x67, 0xE7, 0xCC, 0x8B, 0xCD, 0x93, 0xAD, 0xE4, 0xED, 0xEE,
            0x28, 0x64, 0x2D, 0x08, 0x4F, 0xB2, 0xA5, 0xE8, 0x38, 0x85, 0xDE, 0x65, 0x40, 0x06, 0x68, 0x6D,
            0xF6, 0x13, 0xC5, 0xE1, 0x2F, 0xE8, 0xA1, 0x74, 0x20, 0x44, 0x56, 0xD2, 0x9C, 0xF4, 0x0B, 0x78,
            0xA2, 0x10, 0x9F, 0xAC, 0x81, 0x82, 0x26, 0xC6, 0x63, 0xBC, 0x02, 0x7D, 0x2F, 0x6C, 0xD4, 0x2F,
            0xBD, 0x08, 0xD0, 0xA7
        ];

The output

    Sequence[
        OID(OID(1.2.840.113549.1.7.2))
        Unknown(EndOfContent,[30, 82, 5, d, 2, 1, 1, 31, f, 30, d, 6, 9, 60, 86, 48, 1, 65, 3, 4, 2, 1, 5, 0, 30, b, 6, 9, 2a, 86, 48, 86, f7, d, 1, 7, 1, a0, 82, 3, 57, 30, 82, 3, 53, 30, 82, 2, 3b, a0, 3, 2, 1, 2, 2, 4, 77, a4, d5, e2, 30, d, 6, 9, 2a, 86, 48, 86, f7, d, 1, 1, b, 5, 0, 30, 5a, 31, b, 30, 9, 6, 3, 55, 4, 6, 13, 2, 43, 4e, 31, b, 30, 9, 6, 3, 55, 4, 8, 13, 2, 53, 48, 31, b, 30, 9, 6, 3, 55, 4, 7, 13, 2, 53, 48, 31, f, 30, d, 6, 3, 55, 4, a, 13, 6, 71, 71, 71, 71, 71, 71, 31, f, 30, d, 6, 3, 55, 4, b, 13, 6, 71, 71, 71, 71, 71, 71, 31, f, 30, d, 6, 3, 55, 4, 3, 13, 6, 71, 71, 71, 71, 71, 71, 30, 1e, 17, d, 32, 30, 31, 31, 30, 38, 30, 36, 33, 39, 34, 33, 5a, 17, d, 33, 30, 31, 31, 30, 36, 30, 36, 33, 39, 34, 33, 5a, 30, 5a, 31, b, 30, 9, 6, 3, 55, 4, 6, 13, 2, 43, 4e, 31, b, 30, 9, 6, 3, 55, 4, 8, 13, 2, 53, 48, 31, b, 30, 9, 6, 3, 55, 4, 7, 13, 2, 53, 48, 31, f, 30, d, 6, 3, 55, 4, a, 13, 6, 71, 71, 71, 71, 71, 71, 31, f, 30, d, 6, 3, 55, 4, b, 13, 6, 71, 71, 71, 71, 71, 71, 31, f, 30, d, 6, 3, 55, 4, 3, 13, 6, 71, 71, 71, 71, 71, 71, 30, 82, 1, 22, 30, d, 6, 9, 2a, 86, 48, 86, f7, d, 1, 1, 1, 5, 0, 3, 82, 1, f, 0, 30, 82, 1, a, 2, 82, 1, 1, 0, a7, e0, 84, 4e, 72, 99, e0, f, 4a, 91, 6, 8, 3, c7, 8c, 10, b5, 54, 20, 3, 63, 6, cb, 3, 1f, b3, a5, ae, 5a, 4d, 47, a2, 38, 3e, 57, bd, ff, 3f, e, c0, cd, 82, 17, 18, 72, 71, e4, 4b, de, ec, 3e, af, fd, 8d, 36, f0, ff, 49, e5, 5f, 6a, b2, e7, ca, ba, f1, f, 65, 85, 92, a6, a2, f9, 3b, bc, a3, c1, 46, 42, df, 34, 30, 3a, 90, a5, a, d2, 41, d1, d7, f, c0, c2, 8, 41, 71, 16, 88, 4e, 4f, 5c, 48, e1, de, 8, b2, 1f, 10, 3b, b2, 25, 35, db, cf, fd, 16, 4f, 5a, 9d, 2f, f6, ce, c9, b9, 57, fe, 24, e5, 52, 42, 2, 42, 1a, e5, 42, c3, c5, 5d, 4d, 31, 52, 22, 7a, 44, 67, 99, d9, 8a, 41, 4e, ac, 11, 80, 66, 23, 47, 6d, c3, 65, 14, 6c, 9d, 6f, 96, d4, dc, c4, 85, 16, cc, 34, 14, e7, 38, a3, 9d, 7d, b2, 51, e1, e1, 3c, 90, e7, 26, 7f, 12, 7c, 3b, cc, d3, 6a, 3f, df, 0, fc, 1c, 7b, 2b, 48, 44, 84, a4, 9d, eb, e9, 2f, 8e, 26, 6a, 38, 6f, bb, f1, 1c, 2b, 11, 29, 9d, f0, 57, 91, 10, 60, dd, d2, dc, 64, ae, dd, 94, 53, 40, bf, db, 11, ea, b0, 15, da, d2, 8b, aa, 8, 70, 4a, 21, 1e, 54, 2c, f9, 9e, 52, d0, 6b, 13, 2, 3, 1, 0, 1, a3, 21, 30, 1f, 30, 1d, 6, 3, 55, 1d, e, 4, 16, 4, 14, c5, 15, 25, 97, d0, 4b, f9, 5, 87, 37, 95, 2f, f1, 68, 1a, 22, b3, a7, 7f, fc, 30, d, 6, 9, 2a, 86, 48, 86, f7, d, 1, 1, b, 5, 0, 3, 82, 1, 1, 0, 54, 9a, 99, 39, 86, 3b, c0, 91, bb, 53, a5, b2, 2d, 9b, d3, 7e, d1, 51, b4, 5e, c0, 36, 11, 2d, 52, 5c, 25, dd, 91, d2, 5b, 42, fa, f6, e, 50, 30, 68, 2e, 86, d9, 46, 19, 99, 42, d1, 87, 6f, ff, 7e, d2, 54, 93, d, 82, 6e, 85, c2, 7d, 76, a5, 58, 57, 6c, 6, e5, 8e, 5, f7, 8b, 58, d9, bc, 64, b0, 5e, 31, 88, fd, b1, db, 63, 4b, a1, 22, d2, a9, 71, 97, 34, 3d, 31, e1, f7, ee, f4, d4, 66, 6f, b0, d0, 9c, db, 82, 2a, 57, 8, f7, 9f, 33, 5b, f3, 29, f9, 21, db, c6, 82, 4d, 9, 90, ba, a9, d7, 7d, 2b, dd, e8, ca, 86, 21, df, a5, e3, ed, ed, 3b, 7, 2e, dd, ba, 27, b3, af, 4e, be, 9, 96, 14, 4a, ed, 5b, 5a, b3, 6e, d0, 1d, 9e, 45, c0, 70, df, 75, 7b, 77, a1, 57, c5, 76, 60, 58, 5c, f2, 2f, ad, f1, aa, 15, a6, 7d, 5a, 94, 95, b7, 10, 81, c5, e4, 49, f1, 87, b3, a5, 97, f8, be, c4, e0, fd, 12, cb, 14, 86, 98, a0, 73, fe, ef, 9b, e0, 14, 6a, e, 6, de, 77, e1, 5a, c4, f4, 4e, eb, 8d, 37, f1, 3a, cf, 4d, 4d, 36, fc, c0, 92, a4, e9, a6, 49, 7b, cc, 93, 66, 51, 2a, 24, d3, 15, 75, 9b, 6c, 4f, 60, 44, e9, 15, 59, 68, 31, 82, 1, 8d, 30, 82, 1, 89, 2, 1, 1, 30, 62, 30, 5a, 31, b, 30, 9, 6, 3, 55, 4, 6, 13, 2, 43, 4e, 31, b, 30, 9, 6, 3, 55, 4, 8, 13, 2, 53, 48, 31, b, 30, 9, 6, 3, 55, 4, 7, 13, 2, 53, 48, 31, f, 30, d, 6, 3, 55, 4, a, 13, 6, 71, 71, 71, 71, 71, 71, 31, f, 30, d, 6, 3, 55, 4, b, 13, 6, 71, 71, 71, 71, 71, 71, 31, f, 30, d, 6, 3, 55, 4, 3, 13, 6, 71, 71, 71, 71, 71, 71, 2, 4, 77, a4, d5, e2, 30, d, 6, 9, 60, 86, 48, 1, 65, 3, 4, 2, 1, 5, 0, 30, d, 6, 9, 2a, 86, 48, 86, f7, d, 1, 1, 1, 5, 0, 4, 82, 1, 0, 4b, 60, bf, f7, f0, 7f, 3e, 6, be, ca, 8f, ad, 7e, b6, 1b, be, e9, d, 27, 2, 35, f1, 31, a6, f7, a6, 8, 6a, 48, e0, 77, a9, fa, 30, b1, db, a8, d9, 70, 2a, a2, 21, 96, 2, 6f, 45, 94, bd, 75, a9, 1c, f6, 4, 61, b5, cd, 39, a5, ab, dd, 58, cd, 50, 1, 22, 55, 4, 1d, 5a, 6a, 7a, d7, 30, a0, bd, 36, 9e, 9d, c9, 66, ba, 13, 75, b4, 46, 28, a5, f0, 1f, 9, 36, 9c, 5, 1, 7a, 66, 94, 55, d4, 12, d, c0, 3f, 77, 31, fd, 6, 41, 48, 43, c3, 17, 7b, 7f, 7d, 45, 39, 73, 0, 1e, c2, 99, 3a, 25, 59, b2, 78, dd, 3e, 86, 6, f0, 6e, 84, c, b6, 33, 69, 73, 9b, 52, ba, b, 5d, 2f, a, 1, 30, 42, 93, 2e, 16, 6b, 9a, db, 82, f2, 30, 90, 1d, c6, 1b, b2, 2e, 19, 97, 40, ec, e1, 3f, f3, 56, d2, d3, ac, a6, f9, 7, 5e, cf, be, 91, 1b, ee, 75, 30, ee, 2b, 7c, 45, 52, 1a, 76, 2f, 67, e7, cc, 8b, cd, 93, ad, e4, ed, ee, 28, 64, 2d, 8, 4f, b2, a5, e8, 38, 85, de, 65, 40, 6, 68, 6d, f6, 13, c5, e1, 2f, e8, a1, 74, 20, 44, 56, d2, 9c, f4, b, 78, a2, 10, 9f, ac, 81, 82, 26, c6, 63, bc, 2, 7d, 2f, 6c, d4, 2f, bd, 8, d0, a7])
    ]

As show above, I think 0xA0 is not a unknown tag and the tag number should not be parsed as EOC, this is a context-specific tag.

@cuserdzy
Copy link
Author

cuserdzy commented Dec 6, 2020

I'm not sure if this function should process if hdr.class is DerClass::ContextSpecific

fn der_read_element_content_recursive<'a>(
    i: &'a [u8],
    hdr: DerObjectHeader<'a>,
    max_depth: usize,
) -> DerResult<'a> {
    match hdr.class {
        DerClass::Universal | DerClass::Private => (),
        _ => {
            let (i, content) = ber_get_object_content(i, &hdr, max_depth)?;
            let content = DerObjectContent::Unknown(hdr.tag, content);
            let obj = DerObject::from_header_and_content(hdr, content);
            return Ok((i, obj));
        }
    }
    match der_read_element_content_as(i, hdr.tag, hdr.len, hdr.is_constructed(), max_depth) {
        Ok((rem, content)) => Ok((rem, DerObject::from_header_and_content(hdr, content))),
        Err(Err::Error(BerError::UnknownTag)) => {
            let (rem, content) = ber_get_object_content(i, &hdr, max_depth)?;
            let content = DerObjectContent::Unknown(hdr.tag, content);
            let obj = DerObject::from_header_and_content(hdr, content);
            Ok((rem, obj))
        }
        Err(e) => Err(e),
    }
}

@chifflier
Copy link
Member

Hmm, parse_der may be too restrictive here. If I remember correctly, recursive parsing is not done, because this function has not way to determine if parsing is explicit or implicit from the encoding only (this function has no context). Maybe I should let parse_der try to parse a valid header, and descend if possible, but I'm unsure this would be a good idea.

The usual method with der-parser is to specify the expected tag and parsing function (for ex using parse_der_tagged_explicit). See for example krb5_parser.rs.

What are you trying to do? Maybe I can help if you give more details

@scouten
Copy link
Contributor

scouten commented Jan 6, 2021

@chifflier FWIW I am encountering a similar issue. In my case, I am attempting to parse a CMS (RFC 5652) data structure, specifically the signed and unsigned attributes types, where the structure of the data is not assigned within the RFC that I'm parsing.

I've observed that a context-specific [0] tag nested deeply within that structure yields the same EOC data pattern that @cuserdzy describes; this is an undesirable outcome in my case.

@scouten
Copy link
Contributor

scouten commented Jan 9, 2021

@cuserdzy don't know if you're still facing this issue (or someone else may come along who does have a similar issue), but you can reparse the data blobs. The following code snippet could probably use some cleaning up, but it will reparse into such structures recursively:

fn reparse_unknown_o<'a>(o: BerObject<'a>) -> BerResult<'a> {
    // Workaround for https://github.com/rusticata/der-parser/issues/38.

    let new_o = match o.content {
        BerObjectContent::Sequence(v) => {
            let (_, new_v) = reparse_unknown_v(v)?;
            BerObject::from_header_and_content(o.header, BerObjectContent::Sequence(new_v))
        }

        BerObjectContent::Set(v) => {
            let (_, new_v) = reparse_unknown_v(v)?;
            BerObject::from_header_and_content(o.header, BerObjectContent::Set(new_v))
        }

        BerObjectContent::Optional(Some(boxed_obj)) => {
            let (_, new_o) = reparse_unknown_o(*boxed_obj)?;
            BerObject::from_header_and_content(
                o.header,
                BerObjectContent::Optional(Some(Box::new(new_o))),
            )
        }

        BerObjectContent::Tagged(class, tag, boxed_obj) => {
            let (_, new_o) = reparse_unknown_o(*boxed_obj)?;
            BerObject::from_header_and_content(
                o.header,
                BerObjectContent::Tagged(class, tag, Box::new(new_o)),
            )
        }

        BerObjectContent::Unknown(_tag, data) => {
            if o.header.class == BerClass::ContextSpecific && o.header.structured == 1 {
                // Tagged entries may contain multiple objects. The BerObjectContent enum
                // doesn't have a way to represent this directly, so we wrap this in an
                // implicit sequence.

                let mut objs: Vec<BerObject<'a>> = Vec::new();
                let mut i = data;
                while !i.is_empty() {
                    let (i2, obj) = parse_der(i)?;
                    let (_, obj) = reparse_unknown_o(obj)?;
                    objs.push(obj);
                    i = i2;
                }

                BerObject::from_header_and_content(o.header, BerObjectContent::Sequence(objs))
            } else {
                o
            }
        }

        _ => o,
    };

    Ok((&[], new_o))
}

fn reparse_unknown_v<'a>(v: Vec<BerObject<'a>>) -> BerResult<Vec<BerObject<'a>>> {
    let new_v: Result<Vec<BerObject<'a>>, nom::Err<BerError>> = v
        .into_iter()
        .map(|o| match reparse_unknown_o(o) {
            Ok((_i, new_o)) => Ok(new_o),
            Err(e) => Err(e),
        })
        .collect();

    match new_v {
        Ok(v) => Ok((&[], v)),
        Err(e) => Err(e),
    }
}

@nganhkhoa
Copy link

nganhkhoa commented Aug 28, 2021

I would add to the problem that I also share the same Unknown content for CMS data. And it get worse, as in X.509 ber sequence, with extra information (extension).

PKCS#7/CMS data contains a nested list of certificate X.509, which also has a context-specific [0] data. I have to manually parse the content as a sequence.

Why don't we support 0xA0 tag? It is in the specs anyway.

https://stackoverflow.com/a/43056617/13173256

P.S: the binary for my data is the signature blob in Mach-O (Apple binary) for signed binary. So I would expect the binary data is correctly encoded.

P.S 2: BER encoding, but DER also share the same

@chifflier
Copy link
Member

Hi,
It's not that tag 0xA0 is not supported: the problem is using the function parse_der, because it cannot automatically guess the contents of the tagged value. Without the ASN.1 grammar, it's not possible to know if the value is EXPLICIT or IMPLICIT, and so it cannot be decoded.
When dealing with such cases, the best way is to guide the parser by giving the expected functions, as DER parser design doc section explains.

Note: I have a crate (not yet published, but the code is mostly ready) to decode most PKCS formats, including PKCS#7 and CMS (the crate does not perform cryptographic operations). I intended to publish it later, but if you are interested I can push it to github.

@scouten
Copy link
Contributor

scouten commented Sep 13, 2021

FWIW I am unsubscribing as I no longer have the need for CMS parsing.

@nganhkhoa
Copy link

I would love to see the crate published. But I don't insist pushing something to the public when you're not feeling it. Having a good parser for most certificate standards is very useful. Thank you.

Just a guessing, I think you use serialization to make the decodable/codable (cryptographic standard) structs. Because manually parsing is more error-prone than serializing. And while serialization is experimental, it would be too early to publish code using the feature.

@chifflier
Copy link
Member

The initial problem is now fixed (master branch, to be released as 7.0.0), so I'm closing this issue.
Please open other issues if needed.
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants