lapo-luchini / asn1js

JavaScript generic ASN.1 parser
ISC License
578 stars 161 forks source link

ASN1.toObject #13

Open microshine opened 9 years ago

microshine commented 9 years ago

Propose to add new functionality. Add feature allows to convert ASN structure in Javascript object using given schema.

Extension = {
    type: "SEQUENCE",
    value: {
        typeId: {type: "OBJECT ITDENTIFIER"},
        value: {type: "ANY"}
    }
}

var asn = ANS1.decode(data);
var o = asn.toObject(Extension);

// structure of 'o'
{
    typeId: "1.2.3.4.5.6",
    value: [18, 3, 1, 1, 0]
}
lapo-luchini commented 9 years ago

Sorry, I don't understand what is happening there… what are type and typeId and value used for exactly? How is the parameter formatted?

microshine commented 9 years ago

ASN1 structure SEQUENCE (2 elem) OBJECT IDENTIFIER 2.5.29.37   OCTET STRING (1 elem) SEQUENCE (2 elem) OBJECT IDENTIFIER 1.3.6.1.5.5.7.3.2   OBJECT IDENTIFIER 1.3.6.1.5.5.7.3.4 This is data of X509 Extension (RFC 5280)

Extension ::= SEQUENCE { extnID OBJECT IDENTIFIER, critical BOOLEAN DEFAULT FALSE, extnValue OCTET STRING -- contains the DER encoding of an ASN.1 value -- corresponding to the extension type identified -- by extnID } JavaScript schema

schemas.Extension = {     type: "SEQUENCE",    // ASN1 type of element     value: {                           // ASN1 element value or structure         extnID: {             type: "OBJECT IDENTIFIER" // it's not got a prop value because it's a simple ASN1 type         },         critical:{              type: "BOOLEAN",              default: false,              optional: true // because critical has  'default '         },         extnValue: {             type: "OCTET STRING"         }     } }

JavaScrip code

asn.toObject(schemas.Extension)

return value of function is

{     extnID: "2.5.29.37",     crytical: false,     extnValue: Uint8Array[...] }

Среда, 22 апреля 2015, 7:06 -07:00 от Lapo Luchini notifications@github.com:

Sorry, I don't understand what is happening there… what are type and typeId and value used for exactly? How is the parameter formatted? — Reply to this email directly or view it on GitHub .

microshine@mail.ru

lapo-luchini commented 9 years ago

Ah ok, you mean parsing an ASN.1 format definition file and decoding using named structures instead of anonymous ASN.1 structures? That's not so fast to do and isn't a feature I need, so I don't think I will implement that anytime soon. But as you say it can be done as a post-processing on the current output from ASN1.decode(array), so if anyone feels like implementing it, I would certainly consider pull requests.

microshine commented 9 years ago

I wrote simple ASN1Schema JavaScript code. It has got only one method 'get'. I sent source code of my library to your email. Can you examinate it and tell me ypur oppinin. If you'd like it I can help you to write ASN1.toObject method.

Среда, 22 апреля 2015, 8:12 -07:00 от Lapo Luchini notifications@github.com:

Ah ok, you mean parsing an ASN.1 format definition file and decoding using named structures instead of anonymous ASN.1 structures? That's not so fast to do and isn't a feature I need, so I don't think I will implement that anytime soon. But as you say it can be done as a post-processing on the current output from ASN1.decode(array), so if anyone feels like implementing it, I would certainly consider pull requests. — Reply to this email directly or view it on GitHub . microshine@mail.ru

lapo-luchini commented 2 years ago

@microshine I'm so sorry! This was good stuff, but at the time I was too stressed to think about it and then I totally forgot about this issue. :(

I started coding this feature myself a few days ago, using a different approach (parsing BNF definitions directly out of RFC sources).

I'll work on integrating that with the parser next.