diff -r cc60c8dd7924 -r 5bc2bb8b7946 relpipe-data/examples-asn1-x509.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/relpipe-data/examples-asn1-x509.xml Mon Feb 21 00:43:11 2022 +0100 @@ -0,0 +1,121 @@ + + + Exploring content of X.509 certificates + open and query common SSL/TLS certificates or other ASN.1 data encoded in BER/DER/CER + 05000 + + + +

+ X.509 certificates and keys used for SSL/TLS (HTTPS, POP3S, IMAPS etc.) are usually distributed as files either with .pem or .der extension. + Or bundled together in a PKCS#12 container as a .p12 file. + The „text“ PEM format is often considered more „accessible“ or „friendly“ than the binary DER. + However PEM is just Base64 encoded DER original and is actually less legible to the naked eye than DER, + because in DER we can spot at least some strings like common and domain names or validity/expiration dates or recognize certain data structures in a HEX editor. + Base64 just obfuscates everything. PEM can be easily copied through clipboard, which is probably the only advantage of this format (but it can also more likely leak). +

+ + + +

+ So our first step is to get rid of the annoying Base64 pseudo-plain-text encoding – we use one of these commands: +

+ + certificate.der +cat certificate.pem | openssl x509 -inform PEM -outform DER > certificate.der]]> + +

+ Telco veterans could now start reading the DER file with hd or xxd, jumping over the offsets and traversing the sequences and sets… + However most people would appreciate some software that helps them parsing the ASN.1 BER encoding (the superset of DER and CER). + Such software is e.g. Wireshark or dumpasn1. These programs are good for ad-hoc inspection or quick check. +

+ +

+ In v0.18 we have (early and bit raw) support for ASN.1 BER encoding and thus we can get the structured data in a machine-readable form + – which is good for further processing, conversion to other formats or use in scripts. + Because the ASN.1 data model is not relational – actually it is a tree – this format is supported in the relpipe-in-asn1table + command that is modelled after the well-known XMLTable() database function that allows translating arbitrary tree structures to relations using the XPath expressions. + So in relpipe-in-asn1table we can write XPath expressions to query the ASN.1 tree data structures and extract relations, records and attributes + from X.509 certificates, keys or other cryptographic artifacts, LDAP or SNMP packets or any other ASN.1 BER data. +

+ +

+ But how do we know what XPath expressions should we run? + It is useful to see the XML representation of whole source data. + There is a simple trick to do this – use "/" as the XPath for selecting records (is always selects the single record, single node – the root) + and use "." as the XPath to select a single attribute (it always select the root element) + and add --mode raw-xml, so we get the raw XML source instead of the text content of given elements. + We do not have to write this routine by hand – just create a symlink to the example script: +

+ +

+ This example is generic and works also for other formats supported by the relpipe-in-*table commands. +

+ +

+ Then we can analyze X.509 DER certificates stored on our disk or we can fetch some from live servers. + The openssl command helps us with that: +

+ + + /dev/null \ + | openssl x509 -inform PEM -outform DER; +}]]> + +

Now put both commands together in a pipeline:

+ + + +

and get this XML representation of the ASN.1 X.509 tree:

+ + + + +

Once we know the structure, we can easily hack together a function that extracts parts of the tree as relations:

+ + + +

Everything put together:

+ + + +

will print:

+ + + +

+ The function above is just a „hello world“ example. + Please note that the XPath expressions need to be carefully crafted with respect to the given format in order to match exactly what we want. +

+ +

+ Instead of printing a table, we can use the relpipe-out-nullbyte tool + the read_nullbyte function + and shell loop over the records (alternative names) and e.g. ping each domain or fetch given root web page using wget or curl. + We can also write a simple script that checks the validity of our own certificates and notifies us in advance when some of them are going to expire. +

+ +

+ Later versions of relpipe-in-asn1table will probably support OID names, so it will not be necessary to use the numeric object identifiers. +

+ +

+ n.b. there is also the relpipe-in-asn1 – this tool reads data generated by its counterpart, the relpipe-out-asn1 (or other ASN.1 BER capable software) + i.e. it is not as universal as relpipe-in-asn1table, it has simpler interface, needs no configuration and expects certain ASN.1 structures (relations serialized in BER format). +

+ +
+ +