Do any PHP libraries exist for parsing ASN.1 or generating PHP code based on it?
Asked Answered
T

3

12

I've already looked myself but it seems my Google-fu is not strong today.

I'm working to develop a standardized protocol for exchanging data structures over a TCP/IP connection between an Apache / PHP server and embedded C code on a microcontroller.

We are using ASN.1 notation, and what I would really like to do is to have a piece of PHP code that can parse the ASN.1 document and use it to interpret incoming data. It would produce a PHP object (or array) that is structured appropriately based on the ASN.1. The goal here would be for the PHP that parses the document and creates the objects to be agnostic of the document specifics (i.e. not hand-coded to match the document contents).

Alternatively, if this is not possible, does something exist that would let me generate simple PHP data transfer object classes that I could re-run each time the ASN.1 protocol document changed? This might actually be preferable from an efficiency perspective, as you wouldn't have to re-interpret the ASN.1 for each incoming request.

Thanks! Let me know if I can provide any additional clarification that would help to answer this question.

Testerman answered 7/6, 2011 at 21:49 Comment(5)
you can probably create the c code with the c compiler and then generate a php extension out of it.Erechtheum
never heard asn.1 and i guess there aren't any libs for json in c? xDChape
I'm having no luck finding anything either, but is it necessary to use ASN.1?Flitch
Whatever we end up using shouldn't be hacky (as I imagine c -> php extension might be), and yes unfortunately we have to use ASN.1 because of how prevalent it is in the embedded-c community. It's what our developers use and their colleagues understand.Testerman
And yes, I did briefly wish there was a JSON implementation for C... :) You wouldn't really want that anyways because of the syntactical overhead. Bandwidth usage is important here. One of the primary usages for doing it with ASN.1 is to have a single, standardized way of describing the format of messages without needing any extra character overhead to structure them.Testerman
R
3

Try this one: https://web.archive.org/web/20160305171509/http://phpkode.com/source/s/mistpark-server/library/asn1.php.

It's not so much a library as a script, but you can use it like one.

Rademacher answered 9/6, 2011 at 22:2 Comment(2)
Looks promising, I'll mess around with it.Testerman
I don't know if it's helpful, but I had trouble copy&paste-ing the code from web.archive.org, so I cleaned it up manually and put it up as a gist.Monomania
N
6

Just in case somebody else is looking for an answer on this one: You may try PHPASN1 for a pure ASN.1 encoding and decoding library, or phpseclib which can also handle ASN.1, but it isn't as focused on it.

I am the developer of FGrosse/PHPASN1.

Nix answered 3/10, 2012 at 9:48 Comment(1)
N.B. PHPASN1 library requires PHP 5.6+, phpseclib requires PHP 4+Yuhas
C
4

phpseclib can decode ASN.1 structures identically to how OpenSSL's asn1parse does it:

http://phpseclib.sourceforge.net/x509/asn1parse.php

If you look at the source code it takes an array that $asn1->decodeBER returned and renders that into the string that's displayed. But the fact that it's identical is testament to phpseclib's power I think.

Creedon answered 23/3, 2013 at 21:23 Comment(1)
Beware all ye who enter here. Certainly if you're trying to encode rather than decode. Don't expect to find any examples anywhere except in the phpseclib's own X509.php. The ASN.1 tutorial is useful.Yuhas
R
3

Try this one: https://web.archive.org/web/20160305171509/http://phpkode.com/source/s/mistpark-server/library/asn1.php.

It's not so much a library as a script, but you can use it like one.

Rademacher answered 9/6, 2011 at 22:2 Comment(2)
Looks promising, I'll mess around with it.Testerman
I don't know if it's helpful, but I had trouble copy&paste-ing the code from web.archive.org, so I cleaned it up manually and put it up as a gist.Monomania

© 2022 - 2024 — McMap. All rights reserved.