This module implements minimal ASN.1 encoding/decoding primitives.
Types
Asn1Buffer = object of RootObj buffer*: seq[byte] offset*: int length*: int
- ASN.1's message representation object Source Edit
Asn1Class {.pure.} = enum Universal = 0, Application = 1, ContextSpecific = 2, Private = 3
- Source Edit
Asn1Composite = object of Asn1Buffer tag*: Asn1Tag idx*: int
- Source Edit
Asn1Error {.pure.} = enum Overflow, Incomplete, Indefinite, Incorrect, NoSupport, Overrun
- Source Edit
Asn1Field = object klass*: Asn1Class index*: int offset*: int length*: int buffer*: seq[byte] case kind*: Asn1Tag of Asn1Tag.Boolean: vbool*: bool of Asn1Tag.Integer: vint*: uint64 of Asn1Tag.BitString: ubits*: int else: nil
- Source Edit
Asn1Result[T] = Result[T, Asn1Error]
- Source Edit
Consts
Asn1OidEcPublicKey = [0x2A'u8, 0x86'u8, 0x48'u8, 0xCE'u8, 0x3D'u8, 0x02'u8, 0x01'u8]
- Encoded OID for Elliptic Curve Public Key (1.2.840.10045.2.1) Source Edit
Asn1OidRsaEncryption = [0x2A'u8, 0x86'u8, 0x48'u8, 0x86'u8, 0xF7'u8, 0x0D'u8, 0x01'u8, 0x01'u8, 0x01'u8]
- Encoded OID for RSA Encryption (1.2.840.113549.1.1.1) Source Edit
Asn1OidSecp256k1 = [0x2B'u8, 0x81'u8, 0x04'u8, 0x00'u8, 0x0A'u8]
- Encoded OID for secp256k1 curve (1.3.132.0.10) Source Edit
Asn1OidSecp256r1 = [0x2A'u8, 0x86'u8, 0x48'u8, 0xCE'u8, 0x3D'u8, 0x03'u8, 0x01'u8, 0x07'u8]
- Encoded OID for secp256r1 curve (1.2.840.10045.3.1.7) Source Edit
Asn1OidSecp384r1 = [0x2B'u8, 0x81'u8, 0x04'u8, 0x00'u8, 0x22'u8]
- Encoded OID for secp384r1 curve (1.3.132.0.34) Source Edit
Asn1OidSecp521r1 = [0x2B'u8, 0x81'u8, 0x04'u8, 0x00'u8, 0x23'u8]
- Encoded OID for secp521r1 curve (1.3.132.0.35) Source Edit
Procs
proc `$`(buffer: Asn1Buffer): string {....raises: [], tags: [].}
- Return string representation of buffer. Source Edit
proc `$`(field: Asn1Field): string {....raises: [], tags: [].}
- Return string representation of field. Source Edit
proc `==`(field: Asn1Field; data: openArray[byte]): bool {....raises: [], tags: [].}
- Compares field field data with data and returns true if both buffers are equal. Source Edit
proc asn1EncodeBitString(dest: var openArray[byte]; value: openArray[byte]; bits = 0): int {....raises: [], tags: [].}
-
Encode array of bytes as ASN.1 DER BIT STRING and return number of bytes (octets) used.
bits number of unused bits in value. If bits == 0, all the bits from value will be used.
If length of dest is less then number of required bytes to encode value, then result of encoding will not be stored in dest but number of bytes (octets) required will be returned.
Source Edit proc asn1EncodeBoolean(dest: var openArray[byte]; value: bool): int {. ...raises: [], tags: [].}
-
Encode Nim's boolean as ASN.1 DER BOOLEAN and return number of bytes (octets) used.
If length of dest is less then number of required bytes to encode value, then result of encoding will not be stored in dest but number of bytes (octets) required will be returned.
Source Edit proc asn1EncodeComposite(dest: var openArray[byte]; value: Asn1Composite): int {. ...raises: [], tags: [].}
-
Encode composite value and return number of bytes (octets) used.
If length of dest is less then number of required bytes to encode value, then result of encoding will not be stored in dest but number of bytes (octets) required will be returned.
Source Edit proc asn1EncodeContextTag(dest: var openArray[byte]; value: openArray[byte]; tag: int): int {....raises: [], tags: [].}
-
Encode ASN.1 DER CONTEXT SPECIFIC TAG tag for value value and return number of bytes (octets) used.
Note: Only values in [0, 15] range can be used as context tag tag values.
If length of dest is less then number of required bytes to encode value, then result of encoding will not be stored in dest but number of bytes (octets) required will be returned.
Source Edit proc asn1EncodeInteger(dest: var openArray[byte]; value: openArray[byte]): int {. ...raises: [], tags: [].}
-
Encode big-endian binary representation of integer as ASN.1 DER INTEGER and return number of bytes (octets) used.
If length of dest is less then number of required bytes to encode value, then result of encoding WILL NOT BE stored in dest but number of bytes (octets) required will be returned.
Source Edit proc asn1EncodeInteger[T: SomeUnsignedInt](dest: var openArray[byte]; value: T): int {. ...raises: [].}
-
Encode Nim's unsigned integer as ASN.1 DER INTEGER and return number of bytes (octets) used.
If length of dest is less then number of required bytes to encode value, then result of encoding will not be stored in dest but number of bytes (octets) required will be returned.
Source Edit proc asn1EncodeLength(dest: var openArray[byte]; length: uint64): int {. ...raises: [], tags: [].}
-
Encode ASN.1 DER length part of TLV triple and return number of bytes (octets) used.
If length of dest is less then number of required bytes to encode length value, then result of encoding WILL NOT BE stored in dest but number of bytes (octets) required will be returned.
Source Edit proc asn1EncodeNull(dest: var openArray[byte]): int {....raises: [], tags: [].}
-
Encode ASN.1 DER NULL and return number of bytes (octets) used.
If length of dest is less then number of required bytes to encode value, then result of encoding will not be stored in dest but number of bytes (octets) required will be returned.
Source Edit proc asn1EncodeOctetString(dest: var openArray[byte]; value: openArray[byte]): int {. ...raises: [], tags: [].}
-
Encode array of bytes as ASN.1 DER OCTET STRING and return number of bytes (octets) used.
If length of dest is less then number of required bytes to encode value, then result of encoding will not be stored in dest but number of bytes (octets) required will be returned.
Source Edit proc asn1EncodeOid(dest: var openArray[byte]; value: openArray[byte]): int {. ...raises: [], tags: [].}
-
Encode array of bytes value as ASN.1 DER OBJECT IDENTIFIER and return number of bytes (octets) used.
This procedure is useful to encode constant predefined identifiers such as asn1OidSecp256r1, asn1OidRsaEncryption.
If length of dest is less then number of required bytes to encode value, then result of encoding will not be stored in dest but number of bytes (octets) required will be returned.
Source Edit proc asn1EncodeSequence(dest: var openArray[byte]; value: openArray[byte]): int {. ...raises: [], tags: [].}
-
Encode value as ASN.1 DER SEQUENCE and return number of bytes (octets) used.
If length of dest is less then number of required bytes to encode value, then result of encoding will not be stored in dest but number of bytes (octets) required will be returned.
Source Edit proc code(tag: Asn1Tag): byte {.inline, ...raises: [], tags: [].}
- Converts Nim tag enum to ASN.1 tag code. Source Edit
proc extend[T: Asn1Buffer | Asn1Composite](abc: var T; length: int) {.inline, ...raises: [].}
- Extend buffer or composite's internal buffer by length octets. Source Edit
proc finish[T: Asn1Buffer | Asn1Composite](abc: var T) {.inline, ...raises: [].}
- Finishes buffer or composite and prepares it for writing. Source Edit
proc getBuffer(field: Asn1Field): Asn1Buffer {.inline, ...raises: [], tags: [].}
- Return field as Asn1Buffer to enter composite types. Source Edit
proc init(t: typedesc[Asn1Buffer]): Asn1Buffer {....raises: [].}
- Initialize empty Asn1Buffer. Source Edit
proc init(t: typedesc[Asn1Buffer]; data: openArray[byte]): Asn1Buffer {. ...raises: [].}
- Initialize Asn1Buffer from array of bytes data. Source Edit
proc init(t: typedesc[Asn1Buffer]; data: string): Asn1Buffer {....raises: [].}
- Initialize Asn1Buffer from hexadecimal string data. Source Edit
proc init(t: typedesc[Asn1Composite]; idx: int): Asn1Composite {....raises: [].}
- Initialize Asn1Composite with tag context-specific id id. Source Edit
proc init(t: typedesc[Asn1Composite]; tag: Asn1Tag): Asn1Composite {....raises: [].}
- Initialize Asn1Composite with tag tag. Source Edit
proc len[T: Asn1Buffer | Asn1Composite](abc: T): int {.inline, ...raises: [].}
- Source Edit
proc read(ab: var Asn1Buffer): Asn1Result[Asn1Field] {....raises: [], tags: [].}
- Decode value part of ASN.1 TLV triplet. Source Edit
proc write[T: Asn1Buffer | Asn1Composite](abc: var T; tag: Asn1Tag) {....raises: [].}
-
Write empty value to buffer or composite with tag.
This procedure must be used to write NULL, 0 or empty BIT STRING, OCTET STRING types.
Source Edit proc write[T: Asn1Buffer | Asn1Composite](abc: var T; tag: Asn1Tag; value: openArray[byte]; bits = 0) {....raises: [].}
-
Write array value using tag.
This procedure is used to write ASN.1 INTEGER, OCTET STRING, BIT STRING or OBJECT IDENTIFIER.
For BIT STRING you can use bits argument to specify number of used bits.
Source Edit proc write[T: Asn1Buffer | Asn1Composite](abc: var T; value: Asn1Composite) {. ...raises: [].}
- Source Edit
proc write[T: Asn1Buffer | Asn1Composite](abc: var T; value: bool) {....raises: [].}
- Write bool value to buffer or composite as ASN.1 BOOLEAN. Source Edit
proc write[T: Asn1Buffer | Asn1Composite](abc: var T; value: uint64) {. ...raises: [].}
- Write uint64 value to buffer or composite as ASN.1 INTEGER. Source Edit
Templates
template isEmpty(ab: Asn1Buffer): bool
- Source Edit
template isEnough(ab: Asn1Buffer; length: int64): bool
- Source Edit
template toOpenArray(ab: Asn1Buffer): untyped
- Source Edit
template toOpenArray(ac: Asn1Composite): untyped
- Source Edit
template toOpenArray(af: Asn1Field): untyped
- Source Edit