Recently I wanted to include an example of some ASN.1 (Abstract Syntax Notation) code in a WordPress blog using Alex Gorbatchev‘s excellent SyntaxHighlighter code syntax highlighter. SyntaxHighlighter uses separate syntax files called brushes to define its highlighting functionality. Unfortunately, I discovered that no brush was available for ASN.1 so I decided to write one.
Here is what I came up with:
// // Copyright (c) 2012 Finnbarr P. Murphy. All rights reserved. // SyntaxHighlighter.brushes.ASN1 = function() { var keywords = 'BOOLEAN INTEGER ENUMERATED REAL BIT STRING OCTET NULL ' + 'EXTERNAL SET SEQUENCE OF CHOICE ANY OPTIONAL DEFAULT ' + 'IDENTIFIER OBJECT IMPLICIT'; var reserved = 'GeneralizedTime UTCTime ObjectDescriptor'; this.regexList = [ { regex: /-- .*$/gm, css: 'comments' }, // comments { regex: new RegExp('::=', 'gm'), css: 'color3' }, { regex: new XRegExp('\\[ \\d{1,} \\]', 'gm'), css: 'color3' }, // explicit { regex: new XRegExp('\\(\\{\\s{1,}\\w{1,}\\s{1,}\\}\\)', 'gm'), css: 'color1' }, // compiler action { regex: new RegExp(this.getKeywords(keywords), 'gm'), css: 'keyword' }, { regex: new RegExp(this.getKeywords(reserved), 'gm'), css: 'color2' }, ]; } SyntaxHighlighter.brushes.ASN1.prototype = new SyntaxHighlighter.Highlighter(); SyntaxHighlighter.brushes.ASN1.aliases = ['asn1', 'ASN.1', 'asn.1'];
To use it, create a file called sbBrushASN1,js, containing the above code, in the third-party-brushes sub-directory under the Syntax Highlighter plugin directory. Typically this will be located at BLOGNAME/wp-content/plugins/syntaxhighlighter.
You then need to modify the file syntaxhighlighter.php. This file is located in the root directory of the Syntax Highlighter plugin. You need to add two lines to this file.
// Register the ASN1 brush under third party brushes wp_register_script( 'syntaxhighlighter-brush-asn1', plugins_url('syntaxhighlighter/third-party-brushes/shBrushASN1.js'), array('syntaxhighlighter-core'), '20121109' ); // add a line containing the ASN,1 brush alias to the $this->brushes array // in the appropriate position (this array is sorted!) 'asn1' => 'asn1',
You do not need to disable and re-enable the Syntax Highlighter plugin. If it fails to work for you, you almost certainly have a typo somewhere.
Here is an example of some simple ASN.1 grammar which can be used to decode an X.509 certificate:
-- -- X509 certificate -- Certificate ::= SEQUENCE { tbsCertificate TBSCertificate, signatureAlgorithm AlgorithmIdentifier, signature BIT STRING } TBSCertificate ::= SEQUENCE { version [ 0 ] Version DEFAULT, serialNumber CertificateSerialNumber, signature AlgorithmIdentifier, issuer Name, validity Validity, subject Name, subjectPublicKeyInfo SubjectPublicKeyInfo, issuerUniqueID [ 1 ] IMPLICIT UniqueIdentifier OPTIONAL, subjectUniqueID [ 2 ] IMPLICIT UniqueIdentifier OPTIONAL, extensions [ 3 ] Extensions OPTIONAL } Version ::= INTEGER CertificateSerialNumber ::= INTEGER AlgorithmIdentifier ::= SEQUENCE { algorithm OBJECT IDENTIFIER, parameters ANY OPTIONAL } Name ::= SEQUENCE OF RelativeDistinguishedName RelativeDistinguishedName ::= SET OF AttributeValueAssertion AttributeValueAssertion ::= SEQUENCE { attributeType OBJECT IDENTIFIER, attributeValue ANY } Validity ::= SEQUENCE { notBefore Time, notAfter Time } Time ::= CHOICE { utcTime UTCTime, generalTime GeneralizedTime } SubjectPublicKeyInfo ::= SEQUENCE { algorithm AlgorithmIdentifier, subjectPublicKey BIT STRING } UniqueIdentifier ::= BIT STRING Extensions ::= SEQUENCE OF Extension Extension ::= SEQUENCE { extId OBJECT IDENTIFIER, critical BOOLEAN DEFAULT, extValue OCTET STRING }
Here is how the same ASN.1 grammar looks like using the ASN.1 brush:
— X509 certificate
—
Certificate ::= SEQUENCE {
tbsCertificate TBSCertificate,
signatureAlgorithm AlgorithmIdentifier,
signature BIT STRING
}
TBSCertificate ::= SEQUENCE {
version [ 0 ] Version DEFAULT,
serialNumber CertificateSerialNumber,
signature AlgorithmIdentifier,
issuer Name,
validity Validity,
subject Name,
subjectPublicKeyInfo SubjectPublicKeyInfo,
issuerUniqueID [ 1 ] IMPLICIT UniqueIdentifier OPTIONAL,
subjectUniqueID [ 2 ] IMPLICIT UniqueIdentifier OPTIONAL,
extensions [ 3 ] Extensions OPTIONAL
}
Version ::= INTEGER
CertificateSerialNumber ::= INTEGER
AlgorithmIdentifier ::= SEQUENCE {
algorithm OBJECT IDENTIFIER,
parameters ANY OPTIONAL
}
Name ::= SEQUENCE OF RelativeDistinguishedName
RelativeDistinguishedName ::= SET OF AttributeValueAssertion
AttributeValueAssertion ::= SEQUENCE {
attributeType OBJECT IDENTIFIER,
attributeValue ANY
}
Validity ::= SEQUENCE {
notBefore Time,
notAfter Time
}
Time ::= CHOICE {
utcTime UTCTime,
generalTime GeneralizedTime
}
SubjectPublicKeyInfo ::= SEQUENCE {
algorithm AlgorithmIdentifier,
subjectPublicKey BIT STRING
}
UniqueIdentifier ::= BIT STRING
Extensions ::= SEQUENCE OF Extension
Extension ::= SEQUENCE {
extId OBJECT IDENTIFIER,
critical BOOLEAN DEFAULT,
extValue OCTET STRING
}
Feel free to use this Syntax Highlighter brush. There are not restrictions on it’s use. However please keep the copyright notice and let me know if you improve the brush.