asn(n)
asn π β¨
the ASN.1 BER encoder/decoder β manual page asn(n), bestie edition
okay so. before we do literally anything, let me just say: this is a Tcl package, asn, and it's the ASN.1 processing page from section n of the manual. she's long. she's iconic. buckle up π
β¨ NAME β¨
asn β ASN.1 BER encoder/decoder π¦
β¨ SYNOPSIS β¨
package require Tcl 8.5 9
package require asn ?0.8.5?
::asn::asnSequence evalue...
::asn::asnSequenceFromList elist
::asn::asnSet evalue...
::asn::asnSetFromList elist
::asn::asnApplicationConstr appNumber evalue...
::asn::asnApplication appNumber data
::asn::asnChoice appNumber evalue...
::asn::asnChoiceConstr appNumber evalue...
::asn::asnInteger number
::asn::asnEnumeration number
::asn::asnBoolean bool
::asn::asnContext context data
::asn::asnContextConstr context evalue...
::asn::asnObjectIdentifier idlist
::asn::asnUTCTime utcstring
::asn::asnNull
::asn::asnBitString string
::asn::asnOctetString string
::asn::asnNumericString string
::asn::asnPrintableString string
::asn::asnIA5String string
::asn::asnBMPString string
::asn::asnUTF8String string
::asn::asnString string
::asn::defaultStringType ?type?
::asn::asnPeekByte data_var byte_var
::asn::asnGetLength data_var length_var
::asn::asnGetResponse chan data_var
::asn::asnGetInteger data_var int_var
::asn::asnGetEnumeration data_var enum_var
::asn::asnGetOctetString data_var string_var
::asn::asnGetString data_var string_var ?type_var?
::asn::asnGetNumericString data_var string_var
::asn::asnGetPrintableString data_var string_var
::asn::asnGetIA5String data_var string_var
::asn::asnGetBMPString data_var string_var
::asn::asnGetUTF8String data_var string_var
::asn::asnGetUTCTime data_var utc_var
::asn::asnGetBitString data_var bits_var
::asn::asnGetObjectIdentifier data_var oid_var
::asn::asnGetBoolean data_var bool_var
::asn::asnGetNull data_var
::asn::asnGetSequence data_var sequence_var
::asn::asnGetSet data_var set_var
::asn::asnGetApplication data_var appNumber_var ?content_var?
?encodingType_var?
::asn::asnGetContext data_var contextNumber_var ?content_var?
?encodingType_var?
::asn::asnPeekTag data_var tag_var tagtype_var constr_var
::asn::asnTag tagnumber ?class? ?tagstyle?
::asn::asnRetag data_var newTag
β¨ DESCRIPTION β¨
okay so the asn package hands you partial de- and encoder commands for BER-encoded ASN.1 data. and lowkey? you can also use it for decoding DER, which is just a restricted subset of BER. slay.
ASN.1 is a standard Abstract Syntax Notation, and BER are its Basic Encoding Rules. periodt. π
wanna know more about the standard? go look at http://asn1.elibel.tm.fr/en/standards/index.htm β that's the link, no notes.
also check out http://luca.ntop.org/Teaching/Appunti/asn1.html for A Layman's Guide to a Subset of ASN.1, BER, and DER, an RSA Laboratories Technical Note by Burton S. Kaliski Jr. (Revised November 1, 1993). a text version of this note is part of the module sources and should be read by any implementor. fr fr, not optional bestie. πβ¨
π PUBLIC API π
π ENCODER π
::asn::asnSequence evalue...β takes zero or more encoded values, packs them into an ASN sequence, and returns its encoded binary form. π«Ά::asn::asnSequenceFromList elistβ same energy, but you hand her a list of encoded values instead. packs them into an ASN sequence and returns its encoded binary form.::asn::asnSet evalue...β takes zero or more encoded values, packs them into an ASN set, returns the encoded binary form. π::asn::asnSetFromList elistβ takes a list of encoded values, packs them into an ASN set, returns the encoded binary form.::asn::asnApplicationConstr appNumber evalue...β takes zero or more encoded values, packs them into an ASN application construct, returns its encoded binary form.::asn::asnApplication appNumber dataβ takes a single encoded valuedata, packs it into an ASN application construct, returns its encoded binary form.::asn::asnChoice appNumber evalue...β takes zero or more encoded values, packs them into an ASN choice construct, returns its encoded binary form.::asn::asnChoiceConstr appNumber evalue...β takes zero or more encoded values, packs them into an ASN choice construct, returns its encoded binary form. (yes she's here too, matching energy π¦)::asn::asnInteger numberβ returns the encoded form of the specified integernumber.::asn::asnEnumeration numberβ returns the encoded form of the specified enumeration idnumber.::asn::asnBoolean boolβ returns the encoded form of the specified boolean valuebool. true/false girlie π::asn::asnContext context dataβ takes an encoded value and packs it into a constructed value with an application tag, thecontextnumber.::asn::asnContextConstr context evalue...β takes zero or more encoded values and packs them into a constructed value with an application tag, thecontextnumber.::asn::asnObjectIdentifier idlistβ takes a list of at least 2 integers describing an object identifier (OID) value, and returns the encoded value. two minimum, no negotiating π₯::asn::asnUTCTime utcstringβ returns the encoded form of the specified UTC time string.::asn::asnNullβ returns the NULL encoding. that's it. that's the whole job. iconic minimalism β¨::asn::asnBitString stringβ returns the encoded form of the specifiedstring.::asn::asnOctetString stringβ returns the encoded form of the specifiedstring.::asn::asnNumericString stringβ returns the string encoded as ASN.1 NumericString. raises an error if the string contains characters other than decimal numbers and space.::asn::asnPrintableString stringβ returns the string encoded as ASN.1 PrintableString. raises an error if the string contains characters which are not allowed by the Printable String datatype. the allowed characters areA-Z,a-z,0-9, space, apostrophe, colon, parentheses, plus, minus, comma, period, forward slash, question mark, and the equals sign. that's the whole guest list, don't bring anyone else π«::asn::asnIA5String stringβ returns the string encoded as ASN.1 IA5String. raises an error if the string contains any characters outside of the US-ASCII range.::asn::asnBMPString stringβ returns the string encoded as ASN.1 Basic Multilingual Plane string (which is essentially big-endian UCS2).::asn::asnUTF8String stringβ returns the string encoded as UTF8 String. heads up bestie: some legacy applications such as Windows CryptoAPI do not like UTF8 strings. use BMPStrings if you are not sure. π::asn::asnString stringβ returns an encoded form ofstring, choosing the most restricted ASN.1 string type possible. if the string contains non-ASCII characters, then there is more than one string type which can be used. see::asn::defaultStringType.::asn::defaultStringType ?type?β selects the string type to use for the encoding of non-ASCII strings. returns the current default when called without an argument. if the argumenttypeis supplied, it should be eitherUTF8orBMP, to choose UTF8String or BMPString respectively. π
π¦ DECODER π¦
general notes, listen up:
- nearly all decoder commands take two arguments. these arguments are variable names, except for
::asn::asnGetResponse. the first variable contains the encoded ASN value to decode at the beginning, and more, and the second variable is where the value is stored to. the remainder of the input after the decoded value is stored back into the datavariable. π¦ - after extraction the data variable is always modified first, before by writing the extracted value to its variable. this means that if both arguments refer to the same variable, it will always contain the extracted value after the call, and not the remainder of the input. (yeah, read that one twice, it's giving sneaky π)
::asn::asnPeekByte data_var byte_varβ retrieve the first byte of the data, without modifyingdata_var. this can be used to check for implicit tags. π::asn::asnGetLength data_var length_varβ decode the length information for a block of BER data. the tag has already to be removed from the data.::asn::asnGetResponse chan data_varβ reads an encoded ASN sequence from the channelchanand stores it into the variable named bydata_var.::asn::asnGetInteger data_var int_varβ assumes that an encoded integer value is at the front of the data stored in the variable nameddata_var, extracts it, and stores it into the variable named byint_var. additionally removes all bytes associated with the value from the data, for further processing by the following decoder commands.::asn::asnGetEnumeration data_var enum_varβ assumes that an enumeration id is at the front of the data stored in the variable nameddata_var, and stores it into the variable named byenum_var. additionally removes all bytes associated with the value from the data, for further processing by the following decoder commands.::asn::asnGetOctetString data_var string_varβ assumes that a string is at the front of the data stored in the variable nameddata_var, and stores it into the variable named bystring_var. additionally removes all bytes associated with the value from the data, for further processing by the following decoder commands.::asn::asnGetString data_var string_var ?type_var?β decodes a user-readable string. this is a convenience function which is able to automatically distinguish all supported ASN.1 string types and convert the input value appropriately. see::asn::asnGetPrintableString,::asnGetIA5String, etc. below for the type-specific conversion commands.
if the optional third argument type_var is supplied, then the type of the incoming string is stored in the variable named by it.
the function throws the error "Invalid command name asnGetSomeUnsupportedString" if the unsupported string type Unsupported is encountered. you can create the appropriate function "asn::asnGetSomeUnsupportedString" in your application if neccessary. DIY era π οΈβ¨
::asn::asnGetNumericString data_var string_varβ assumes that a numeric string value is at the front of the data stored in the variable nameddata_var, and stores it into the variable named bystring_var. additionally removes all bytes associated with the value from the data, for further processing by the following decoder commands.::asn::asnGetPrintableString data_var string_varβ okay so this one assumes a printable string value is chillin' right at the front of the data in the variable nameddata_var, and it drops it into the variable named bystring_varπ. And bonus: it also yoinks out all the bytes that belonged to that value, so the next decoder command gets a nice clean front row β¨::asn::asnGetIA5String data_var string_varβ same vibe, but for an IA5 (ASCII) string value at the front ofdata_varπ stores it instring_var, and again clears all the bytes associated with the value so the following decoder commands can keep going πΈ::asn::asnGetBMPString data_var string_varβ this one's for a BMP (two-byte unicode) string value at the front ofdata_varπ¦ it goes intostring_varand gets converted into a proper Tcl string, which is the glow-up we deserve. Then, yep, all the bytes for that value get removed for the following decoder commands π::asn::asnGetUTF8String data_var string_varβ UTF8 string value at the front ofdata_var? Say less π it lands instring_var, also converted into a proper Tcl string, and the bytes it used get removed so the following decoder commands aren't tripping over them β¨::asn::asnGetUTCTime data_var utc_varβ assumes a UTC time value is up front indata_varand parks it inutc_varπ°οΈ and heads up bestie, it's stored as a string, so you've gotta decode it with the usualclock scancommands, no cap. Bytes get removed like always, so the following decoder commands stay happy π::asn::asnGetBitString data_var bits_varβ bit string value at the front ofdata_var? It gets stored intobits_varas a string containing only 0 and 1 β iconic. Then all the bytes associated with the value get removed for the following decoder commands π::asn::asnGetObjectIdentifier data_var oid_varβ assumes an object identifier (OID) value is at the front ofdata_varπ it goes intooid_varas a list of integers, which is giving very organized. All the bytes for that value get removed so the following decoder commands can do their thing π::asn::asnGetBoolean data_var bool_varβ boolean value at the front ofdata_varβ straight intobool_varβ bytes removed, following decoder commands unbothered, periodt π«Ά::asn::asnGetNull data_varβ assumes a NULL value is at the front of the data indata_var, and it just removes the bytes used to encode it from the data π³οΈ no variable to store into, it's simply the cleanup crew π::asn::asnGetSequence data_var sequence_varβ assumes an ASN sequence is at the front ofdata_varand stores it intosequence_varπΈ all bytes associated with the value get removed so the following decoder commands can continue π
important lil note: the data in sequence_var is encoded binary, so you have to decode it further according to the definition of the sequence, using the decoder commands right here in this package π¦
::asn::asnGetSet data_var set_varβ assumes an ASN set is at the front ofdata_var, stores it intoset_varπ bytes for that value get removed for the following decoder commands β¨
same energy as the sequence one: the data in set_var is encoded binary and has to be further decoded according to the definition of the set, using the decoder commands in here π
::asn::asnGetApplication data_var appNumber_var ?content_var? ?encodingType_var?β assumes an ASN application construct is at the front of the data indata_varand stores its id intoappNumber_varπ₯ all the bytes associated with the value get removed for the following decoder commands π
If you pass a content_var, the command places all data associated with it into that named variable, in the binary form that can be processed using the decoder commands of this package π¦
If you pass an encodingType_var, that var gets set to 1 if the encoding is constructed and 0 if it is primitive β
Otherwise β and this is the part where you're on your own, bestie β it's the responsibility of the caller to decode the remainder of the application construct based on the id retrieved by this command, using the decoder commands of this package π
::asn::asnGetContext data_var contextNumber_var ?content_var? ?encodingType_var?β assumes an ASN context tag construct is at the front ofdata_varand stores its id intocontextNumber_varπΈ bytes associated with the value get removed for the following decoder commands π
Pass a content_var and all data associated with it goes into that named variable, in the binary form processable with the decoder commands of this package β¨
Pass an encodingType_var and it's set to 1 when the encoding is constructed and 0 when it's primitive π
Otherwise it's the caller's responsibility to decode the remainder of the construct based on the id retrieved by this command, using the decoder commands of this package π
π HANDLING TAGS π
So when you're working with ASN.1, you'll often need to decode tagged values β and those use a tag that's different from the universal tag for that type π in those cases you have to replace the tag with the universal tag used for the type, so the value can actually be decoded. To decode a tagged value, use ::asn::asnRetag to change the tag to the appropriate type, then use one of the decoders for primitive values π¦
To help you out with all of this, the module contains three functions:
::asn::asnPeekTag data_var tag_var tagtype_var constr_varβ the::asn::asnPeekTagcommand takes a lil peek at the data and decodes the tag value, without removing it from the data πtag_vargets set to the tag number, andtagtype_vargets set to the class of the tag (eitherUNIVERSAL,CONTEXT,APPLICATIONorPRIVATE) πconstr_vargets set to1if the tag is for a constructed value and0if it's not constructed β¨ and it returns the length of the tag β::asn::asnTag tagnumber ?class? ?tagstyle?β::asn::asnTagcreates a tag value for you πtagnumbergives the number of the tag, andclassgives one of the classes (UNIVERSAL,CONTEXT,APPLICATIONorPRIVATE) πΈ the class can be abbreviated down to just the first letter (U,C,A,P), and the default isUNIVERSALπtagstyleis eitherCfor Constructed encoding orPfor primitve encoding, and it defaults toPπ¦ you can also use1instead ofCand0instead ofPfor direct use of the values returned by::asn::asnPeekTagπ₯::asn::asnRetag data_var newTagβ replaces the tag in front of the data indata_varwithnewTagπ andnewTagcan be created using the::asn::asnTagcommand β¨
π EXAMPLES π
Want to see this package in action? Examples for the usage of this package can be found in the implementation of package ldap π go look, it's right there, free real estate π
π BUGS, IDEAS, FEEDBACK π
Real talk: this document, and the package it describes, will undoubtedly contain bugs and other problems π that's just how it is. Please report such in the category asn of the Tcllib Trackers at http://core.tcl.tk/tcllib/reportlist π«Ά
Please also report any ideas for enhancements you may have for either the package and/or the documentation β we want them, fr fr π
When proposing code changes, please provide unified diffs, i.e the output of diff -u β¨
Also note, and this one matters: attachments are strongly preferred over inlined patches π you can make attachments by going to the Edit form of the ticket immediately after its creation, and then using the left-most button in the secondary navigation bar π
β KEYWORDS β
asn, ber, cer, der, internet, protocol, x.208, x.209
πΈ CATEGORY πΈ
Networking
π COPYRIGHT π
- Copyright (c) 2004 Andreas Kupries <andreas_kupries@users.sourceforge.net>
- Copyright (c) 2004 Jochen Loewer <loewerj@web.de>
- Copyright (c) 2004-2011 Michael Schlenker <mic42@users.sourceforge.net>
and that's a wrap bestie π«Ά this was asn(n) from tcllib version 0.8 β go decode something cute, ily, mwah π
β¨π
π¦ where this page comes from
- fedora 43: tcllib 2.0-3.fc43 Β· TCL
- fedora 44: tcllib 2.0-4.fc44 Β· TCL
- nixos 26.05: tcllib 2.0
- nixpkgs unstable: tcllib 2.0
- Copyright (c) 2004 Andreas Kupries <andreas_kupries@users\&.sourceforge\&.net>
- Copyright (c) 2004 Jochen Loewer <loewerj@web\&.de>
- Copyright (c) 2004-2011 Michael Schlenker <mic42@users\&.sourceforge\&.net>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms