asn1
- class asn1.Decoder(stream: RawIOBase | BufferedIOBase | bytes | None = None)[source]
ASN.1 decoder. Understands BER (and DER which is a subset).
- enter() None[source]
This method enters the constructed type that is at the current decoding offset.
Note
It is an error to call
Decoder.enter()if the to be decoded ASN.1 tag is not of a constructed type. It is no more necessary to call this method. It is kept only for compatibility with previous versions of the library.- Returns:
None
- eof() bool[source]
Return True if we are at the end of input.
- Returns:
bool – True if all input has been decoded, and False otherwise.
- leave() None[source]
This method leaves the last constructed type that was
Decoder.enter()-ed.Note
It is an error to call
Decoder.leave()if the current ASN.1 tag is not of a constructed type. It is no more necessary to call this method. It is kept only for compatibility with previous versions of the library.- Returns:
None
- peek() Tag | None[source]
This method returns the current ASN.1 tag (i.e. the tag that a subsequent
Decoder.read()call would return) without updating the decoding offset. In case no more data is available from the input, this method returnsNoneto signal end-of-file.This method is useful if you don’t know whether the next tag will be a primitive or a constructed tag. Depending on the return value of
peek, you would decide to either issue aDecoder.read()in case of a primitive type, or anDecoder.enter()in case of a constructed type.Note
Because this method does not advance the current offset in the input, calling it multiple times in a row will return the same value for all calls.
- read(flags: ReadFlags = ReadFlags.OnlyValue) Tuple[Tag | None, Any][source]
This method decodes one ASN.1 tag from the input and returns it as a
(tag, value)tuple.tagis a 3-tuple(nr, typ, cls), whilevalueis a Python object representing the ASN.1 value. The number of unused bits can be requested in theflagsparameter. WhenReadFlags.OnlyValueis specified, the method the tuple(tag, (value, unused)).unusedis the number of unused bits removed from the value. The offset in the input is increased so that the nextDecoder.read()call will return the next tag. In case no more data is available from the input, this method returnsNoneto signal end-of-file.
- start(stream: RawIOBase | BufferedIOBase | bytes) None[source]
This method instructs the decoder to start decoding an ASN.1 input. This method may be called at any time to start a new decoding job. If this method is called while currently decoding another input, that decoding context is discarded.
Note
It is not necessary to specify the encoding because the decoder assumes the input is in BER or DER format.
- Parameters:
stream (DecoderStream) – ASN.1 input, in BER or DER format, to be decoded.
- Returns:
None
- Raises:
Error –
- class asn1.Encoder(stream: RawIOBase | BufferedWriter | None = None, encoding: Encoding | None = None)[source]
ASN.1 encoder. Uses DER encoding.
- construct(nr: int, cls: int | None = None) Generator[None, Any, None][source]
This method is a context manager that calls the enter and leave methods for better code mapping.
Usage:
with encoder.construct(asn1.Numbers.Sequence): encoder.write(1) with encoder.construct(asn1.Numbers.Sequence): encoder.write('foo') encoder.write('bar') encoder.write(2)
encoder.output()will result in the following structure:SEQUENCE: INTEGER: 1 SEQUENCE: STRING: foo STRING: bar INTEGER: 2
- Parameters:
nr (int) – The desired ASN.1 type. Use
Numbersenumeration.cls (int, optional) – Specifies the class of the constructed type. The default class is the universal class. Use
Classesenumeration.
- Returns:
None
- Raises:
Error –
- enter(nr: int, cls: int | None = None) None[source]
This method starts the construction of a constructed type.
- Parameters:
nr (int) – The desired ASN.1 type. Use
Numbersenumeration.cls (int) – This optional parameter specifies the class of the constructed type. The default class to use is the universal class. Use
Classesenumeration.
- Returns:
None
- Raises:
Error –
- leave() None[source]
This method completes the construction of a constructed type and writes the encoded representation to the output stream.
- output() bytes[source]
This method returns the encoded ASN.1 data as plain Python
bytes. With DER encoding and a stream, this method has to be called when all data are encoded. Otherwise, the stream will contain partial data. With DER encoding and no stream, this method has to be called multiple times. The data that has been encoded so far is returned. When using CER encoding, you do not need to call it at all. With CER, this method can only be called when using a BytesIO stream or no stream.Note
It is an error to call this method if the encoder is still constructing a constructed type, i.e. if
Encoder.enter()has been called more times thatEncoder.leave().- Returns:
bytes – The DER or CER encoded ASN.1 data.
- Raises:
Error –
- start(stream: RawIOBase | BufferedWriter | None = None, encoding: Encoding | None = None) None[source]
This method instructs the encoder to start encoding a new ASN.1 output. This method may be called at any time to reset the encoder and reset the current output (if any).
- Parameters:
stream (EncoderStream) – The output stream or the encoding with no stream.
encoding (Encoding or None) – The encoding (DER or CER) to use.
- write(value: Any, nr: int | None = None, typ: int | None = None, cls: int | None = None) None[source]
This method encodes one ASN.1 tag and writes it to the output buffer.
Note
Normally,
valuewill be the only parameter to this method. In this case Python-ASN1 will autodetect the correct ASN.1 type from the type ofvalue, and will output the encoded value based on this type.- Parameters:
value (any) – The value of the ASN.1 tag to write. Python-ASN1 will try to autodetect the correct ASN.1 type from the type of
value.nr (int) – If the desired ASN.1 type cannot be autodetected or is autodetected wrongly, the
nrparameter can be provided to specify the ASN.1 type to be used. UseNumbersenumeration.typ (int) – This optional parameter can be used to write constructed types to the output by setting it to indicate the constructed encoding type. In this case,
valuemust already be valid ASN.1 encoded data as plain Python bytes. This is not normally how constructed types should be encoded though, seeEncoder.enter()andEncoder.leave()for the recommended way of doing this. UseTypesenumeration.cls (int) – This parameter can be used to override the class of the
value. The default class is the universal class. UseClassesenumeration.
- Returns:
None
- Raises:
Error –