asn(3tcl)
π asn β ASN.1 BER encoder/decoder π
okay bestie, buckle up, this is the manual page for asn from the 3tcl manual, and it's giving extremely competent energy. no cap. it's long so we're doing it in parts, this is part one, let's go π¦
β¨ 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 π
so the asn package gives you partial de- and encoder commands for BER-encoded ASN.1 data, bestie. it can also be used for decoding DER, which is literally just a restricted subset of BER. slay.
ASN.1 is a standard Abstract Syntax Notation, and BER are its Basic Encoding Rules. iconic behavior honestly.
wanna know more about the standard? see http://asn1.elibel.tm.fr/en/standards/index.htm π«Ά
also go look at 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 likeβ¦ every implementor should read it, fr fr. π
π PUBLIC API π
π ENCODER π
::asn::asnSequence evalue...β takes zero or more encoded values, packs them into an ASN sequence, and hands you back its encoded binary form. π::asn::asnSequenceFromList elistβ same vibe, but it takes a list of encoded values, packs them into an ASN sequence, and returns the encoded binary form.::asn::asnSet evalue...β takes zero or more encoded values, packs them into an ASN set, and returns its encoded binary form.::asn::asnSetFromList elistβ takes a list of encoded values, packs them into an ASN set, returns the encoded binary form. yes queen.::asn::asnApplicationConstr appNumber evalue...β takes zero or more encoded values, packs them into an ASN application construct, and 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, and returns its encoded binary form.::asn::asnChoiceConstr appNumber evalue...β takes zero or more encoded values, packs them into an ASN choice construct, and returns its encoded binary form.::asn::asnInteger numberβ returns the encoded form of the integernumberyou gave it. π::asn::asnEnumeration numberβ returns the encoded form of the enumeration idnumber.::asn::asnBoolean boolβ returns the encoded form of the boolean valuebool.::asn::asnContext context dataβ takes an encoded value and packs it into a constructed value with application tag, the context number. (contextis the number,datais the value.)::asn::asnContextConstr context evalue...β takes zero or more encoded values and packs them into a constructed value with application tag, the context number.::asn::asnObjectIdentifier idlistβ takes a list of at least 2 integers describing an object identifier (OID) value, and returns the encoded value. don't come at it with one integer, it will not eat that. π::asn::asnUTCTime utcstringβ returns the encoded form of the UTC time string you passed.::asn::asnNullβ returns the NULL encoding. that's it. that's the whole thing. iconic minimalism. β¨::asn::asnBitString stringβ returns the encoded form of the string.::asn::asnOctetString stringβ returns the encoded form of the string.::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. so no funny business. π::asn::asnPrintableString stringβ returns the string encoded as ASN.1 PrintableString. raises an error if the string contains characters that aren't allowed by the Printable String datatype. the allowed characters are:A-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. π::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 an ASN.1 Basic Multilingual Plane string (which is essentially big-endian UCS2).::asn::asnUTF8String stringβ returns the string encoded as a UTF8 String. heads up bestie: some legacy applications such as Windows CryptoAPI do not like UTF8 strings. if you're not sure, use BMPStrings. π::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's more than one string type that could be used. see::asn::defaultStringType.::asn::defaultStringType ?type?β selects the string type to use for encoding non-ASCII strings. called with no argument, it returns the current default. if you do passtype, it should be eitherUTF8orBMP, to choose UTF8String or BMPString respectively.
π¦ DECODER π¦
general notes, bestie, read these before you touch anything:
- 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 gets stored. the remainder of the input after the decoded value gets stored back into the data variable. - after extraction the data variable is always modified first, before writing the extracted value to its variable. this means if both arguments refer to the same variable, it will always contain the extracted value after the call β not the remainder of the input. noted. π
::asn::asnPeekByte data_var byte_varβ retrieves the first byte of the data without modifyingdata_var. great for checking implicit tags. peek, don't touch. π::asn::asnGetLength data_var length_varβ decodes the length information for a block of BER data. the tag has already got to be removed from the data before this.::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 an encoded integer value is sitting at the front of the data stored in the variable nameddata_var, extracts it, and stores it into the variable named byint_var. it also removes all bytes associated with the value from the data, so the following decoder commands can keep going. π::asn::asnGetEnumeration data_var enum_varβ assumes 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. also strips all bytes associated with the value from the data for the next decoder commands.::asn::asnGetOctetString data_var string_varβ assumes a string is at the front of the data stored indata_var, stores it into the variable named bystring_var, and removes all the bytes associated with the value from the data for the following decoder commands.::asn::asnGetString data_var string_var ?type_var?β decodes a user-readable string. this is a convenience function that can 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 you pass the optional third argument type_var, then the type of the incoming string gets stored in the variable named by it.
heads up! this function throws the error"Invalid command name asnGetSomeUnsupportedString"if the unsupported string typeUnsupportedis encountered. you can create the appropriate function"asn::asnGetSomeUnsupportedString"in your application if necessary. no cap, that's your job to handle. π
::asn::asnGetNumericString data_var string_varβ assumes a numeric string value is at the front of the data stored in the variable nameddata_var, stores it into the variable named bystring_var, and removes all bytes associated with the value from the data for further processing by the following decoder commands.
okay so bestie, we're picking up right where we left off in the decoder bestie squad π these commands are the ones that actually pull values out of the front of your data_var and yeet the consumed bytes so the next decoder can do its thing. no cap.
::asn::asnGetPrintableString data_var string_varβ assumes a printable string value is at the front of the data stored in the variable nameddata_var, and stores it into the variable named bystring_var. then it removes all bytes associated with the value from the data for further processing by the following decoder commands. printable string bestie, slay. β¨::asn::asnGetIA5String data_var string_varβ assumes an IA5 (ASCII) string value is at the front of the data stored in the variable nameddata_var, and stores it into the variable named bystring_var. then it removes all bytes associated with the value from the data for further processing by the following decoder commands. ASCII core, periodt. π::asn::asnGetBMPString data_var string_varβ assumes a BMP (two-byte unicode) string value is at the front of the data stored in the variable nameddata_var, and stores it into the variable named bystring_var, converting it into a proper Tcl string. then it removes all bytes associated with the value from the data for further processing by the following decoder commands. two-byte unicode ate. π¦::asn::asnGetUTF8String data_var string_varβ assumes a UTF8 string value is at the front of the data stored in the variable nameddata_var, and stores it into the variable named bystring_var, converting it into a proper Tcl string. then it removes all bytes associated with the value from the data for further processing by the following decoder commands. UTF8, iconic. π::asn::asnGetUTCTime data_var utc_varβ assumes a UTC time value is at the front of the data stored in the variable nameddata_var, and stores it into the variable named byutc_var. the UTC time value is stored as a string, which has to be decoded with the usualclock scancommands. then it removes all bytes associated with the value from the data for further processing by the following decoder commands. clock scan bestie, iykyk. πΈ::asn::asnGetBitString data_var bits_varβ assumes a bit string value is at the front of the data stored in the variable nameddata_var, and stores it into the variable named bybits_varas a string containing only0and1. then it removes all bytes associated with the value from the data for further processing by the following decoder commands. binary baddie behavior. π::asn::asnGetObjectIdentifier data_var oid_varβ assumes a object identifier (OID) value is at the front of the data stored in the variable nameddata_var, and stores it into the variable named byoid_varas a list of integers. then it removes all bytes associated with the value from the data for further processing by the following decoder commands. OID list of integers, fr fr. π₯::asn::asnGetBoolean data_var bool_varβ assumes a boolean value is at the front of the data stored in the variable nameddata_var, and stores it into the variable named bybool_var. then it removes all bytes associated with the value from the data for further processing by the following decoder commands. true/false bestie. π::asn::asnGetNull data_varβ assumes a NULL value is at the front of the data stored in the variable nameddata_varand removes the bytes used to encode it from the data. null and void, but make it cute. π::asn::asnGetSequence data_var sequence_varβ assumes an ASN sequence is at the front of the data stored in the variable nameddata_var, and stores it into the variable named bysequence_var. then it removes all bytes associated with the value from the data for further processing by the following decoder commands.- The data in
sequence_varis encoded binary and has to be further decoded according to the definition of the sequence, using the decoder commands here. so like, nested decoding energy. π§
- The data in
::asn::asnGetSet data_var set_varβ assumes an ASN set is at the front of the data stored in the variable nameddata_var, and stores it into the variable named byset_var. then it removes all bytes associated with the value from the data for further processing by the following decoder commands.- The data in
set_varis encoded binary and has to be further decoded according to the definition of the set, using the decoder commands here. set bestie, slay. πΈ
- The data in
::asn::asnGetApplication data_var appNumber_var ?content_var? ?encodingType_var?β assumes an ASN application construct is at the front of the data stored in the variable nameddata_var, and stores its id into the variable named byappNumber_var. then it removes all bytes associated with the value from the data for further processing by the following decoder commands.- If a
content_varis specified, then the command places all data associated with it into the named variable, in the binary form which can be processed using the decoder commands of this package. - If a
encodingType_varis specified, then that var is set to1if the encoding is constructed and0if it is primitive. - Otherwise it is 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. so if you don't pass those vars, bestie, you're on your own β but like, with the package's decoder commands. π
- If a
::asn::asnGetContext data_var contextNumber_var ?content_var? ?encodingType_var?β assumes an ASN context tag construct is at the front of the data stored in the variable nameddata_var, and stores its id into the variable named bycontextNumber_var. then it removes all bytes associated with the value from the data for further processing by the following decoder commands.- If a
content_varis specified, then the command places all data associated with it into the named variable, in the binary form which can be processed using the decoder commands of this package. - If a
encodingType_varis specified, then that var is set to1if the encoding is constructed and0if it is primitive. - Otherwise it is the responsibility of the caller to decode the remainder of the construct based on the id retrieved by this command, using the decoder commands of this package. context bestie, you got this. π«Ά
- If a
HANDLING TAGS π
Working with ASN.1 you often need to decode tagged values, which use a tag thats different from the universal tag for a type. In those cases you have to replace the tag with the universal tag used for the type, to decode the value. To decode a tagged value use the ::asn::asnRetag to change the tag to the appropriate type to use one of the decoders for primitive values. To help with this the module contains three functions:
::asn::asnPeekTag data_var tag_var tagtype_var constr_varβ the::asn::asnPeekTagcommand can be used to take a peek at the data and decode the tag value, without removing it from the data. Thetag_vargets set to the tag number, while thetagtype_vargets set to the class of the tag. (EitherUNIVERSAL,CONTEXT,APPLICATIONorPRIVATE). Theconstr_varis set to1if the tag is for a constructed value, and to0for not constructed. It returns the length of the tag. peekaboo bestie. π::asn::asnTag tagnumber ?class? ?tagstyle?β the::asn::asnTagcan be used to create a tag value. Thetagnumbergives the number of the tag, while theclassgives one of the classes (UNIVERSAL,CONTEXT,APPLICATIONorPRIVATE). The class may be abbreviated to just the first letter (U,C,A,P), default isUNIVERSAL. Thetagstyleis eitherCfor Constructed encoding, orPfor primitive encoding. It defaults toP. You can also use1instead ofCand0instead ofPfor direct use of the values returned by::asn::asnPeekTag. tag creation bestie, slay. β¨::asn::asnRetag data_var newTagβ replaces the tag in front of the data indata_varwithnewTag. The new Tag can be created using the::asn::asnTagcommand. retag, glow up. π
EXAMPLES π§
Examples for the usage of this package can be found in the implementation of package ldap. okay so if you wanna see it in the wild, go peek at ldap. π
BUGS, IDEAS, FEEDBACK π«Ά
This document, and the package it describes, will undoubtedly contain bugs and other problems. Please report such in the category asn of the Tcllib Trackers http://core.tcl.tk/tcllib/reportlist. Please also report any ideas for enhancements you may have for either package and/or documentation.
When proposing code changes, please provide unified diffs, i.e the output of diff -u.
Note further that attachments are strongly preferred over inlined patches. Attachments can be made 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. we love a good bug report bestie. π
KEYWORDS π
asn, ber, cer, der, internet, protocol, x.208, x.209. these are the vibes. π
CATEGORY π
Networking. periodt.
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>
okay bestie that's the whole page π source/version? it's tcllib, version 0.8, asn(3tcl) β the date didn't make it into the footer, but the icon energy is eternal. slay. β¨
π¦ where this page comes from
- debian trixie: tcllib 2.0+dfsg-4 Β· Tcl
- ubuntu questing: tcllib 2.0+dfsg-4
- ubuntu resolute: tcllib 2.0+dfsg-5 Β· Tcl
- ubuntu stonking: tcllib 2.0+dfsg-6 Β· Tcl
- 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