11

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.

Grekker
  • 944
  • 1
  • 9
  • 17
  • you can probably create the c code with the c compiler and then generate a php extension out of it. – hakre Jun 07 '11 at 22:00
  • never heard asn.1 and i guess there aren't any libs for json in c? xD – dynamic Jun 07 '11 at 22:01
  • I'm having no luck finding anything either, but is it necessary to use ASN.1? – Nick ODell Jun 07 '11 at 22:44
  • 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. – Grekker Jun 15 '11 at 16:34
  • 1
    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. – Grekker Jun 15 '11 at 16:35

3 Answers3

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.

apaderno
  • 28,547
  • 16
  • 75
  • 90
Friedrich Große
  • 2,383
  • 19
  • 19
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.

  • 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](https://github.com/phpseclib/phpseclib/blob/master/phpseclib/File/X509.php). The [ASN.1 tutorial](http://phpseclib.sourceforge.net/x509/asn1tutorial.html) is useful. – icc97 Sep 16 '16 at 01:26
2

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.

icc97
  • 11,395
  • 8
  • 76
  • 90
Nik
  • 7,113
  • 13
  • 51
  • 80