ASN.1入门


ASN.1: Abstract Syntax Notation One

ASN.1可以分为一组语法规则和一组编码规则

应用:

环境

python asn1tools库:https://asn1tools.readthedocs.io/en/latest/

pip install asn1tools

其它asn1转c开源工具:

  • https://github.com/vlm/asn1c
  • https://github.com/ttsiodras/asn1scc

Python asn1tools支持的编码:

  • Basic Encoding Rules (BER) defaut
  • Distinguished Encoding Rules (DER)
  • Generic String Encoding Rules (GSER)
  • JSON Encoding Rules (JER)
  • Basic Octet Encoding Rules (OER)
  • Aligned Packed Encoding Rules (PER)
  • Unaligned Packed Encoding Rules (UPER)
  • XML Encoding Rules (XER)

这个库还带着一个asn1tools可执行文件,可以转换编码、生成c代码、基于libFuzzer生成模糊测试代码等。

types

basic types含义
INTEGER
BIT STRING01串
OCTET STRINGpython bytes
NULL
OBJECT IDENTIFIER一个实体的标识符
String(except BITSTRING、OCTETSTRING)各种字符串
UTCTime
ENUMERATED枚举

一些String:

  • PrintableString, 可视字符串
  • T61String, 8位字符串
  • IA5String , ASCII字符串

结构类型:

constructed typeshex id含义
SEQUENCE10一个或多个类型的有序排列
SEQUENCEOF10一个给定类型的0个或多个有序排列
SET11一个或多个类型的无序集合
SET OF11一个给定类型的0个或多个无序集合
CHOICEenum

每个类型都有tag value,openssl /include/openssl/asn1.h宏定义如下:

/* ASN.1 tag values */
# define V_ASN1_EOC                      0
# define V_ASN1_BOOLEAN                  1 /**/
# define V_ASN1_INTEGER                  2
# define V_ASN1_BIT_STRING               3
# define V_ASN1_OCTET_STRING             4
# define V_ASN1_NULL                     5
# define V_ASN1_OBJECT                   6
# define V_ASN1_OBJECT_DESCRIPTOR        7
# define V_ASN1_EXTERNAL                 8
# define V_ASN1_REAL                     9
# define V_ASN1_ENUMERATED               10
# define V_ASN1_UTF8STRING               12
# define V_ASN1_SEQUENCE                 16
# define V_ASN1_SET                      17
# define V_ASN1_NUMERICSTRING            18 /**/
# define V_ASN1_PRINTABLESTRING          19
# define V_ASN1_T61STRING                20
# define V_ASN1_TELETEXSTRING            20/* alias */
# define V_ASN1_VIDEOTEXSTRING           21 /**/
# define V_ASN1_IA5STRING                22
# define V_ASN1_UTCTIME                  23
# define V_ASN1_GENERALIZEDTIME          24 /**/
# define V_ASN1_GRAPHICSTRING            25 /**/
# define V_ASN1_ISO64STRING              26 /**/
# define V_ASN1_VISIBLESTRING            26/* alias */
# define V_ASN1_GENERALSTRING            27 /**/
# define V_ASN1_UNIVERSALSTRING          28 /**/
# define V_ASN1_BMPSTRING                30

examples

https://github.com/eerimoq/asn1tools/tree/master/tests/files

以foo.asn为例

Foo DEFINITIONS ::= BEGIN
    Question ::= SEQUENCE {
        id        INTEGER,
        question  IA5String
    }
    Answer ::= SEQUENCE {
        id        INTEGER,
        answer    BOOLEAN
    }
END

编解码:

import asn1tools
foo = asn1tools.compile_files('./foo.asn') # default ber encoded
encoded = foo.encode('Question', {'id': 1, 'question': 'Is 1+1=3?'})
print(encoded)
# b'0\x0e\x02\x01\x01\x16\tIs 1+1=3?'
print(foo.decode('Question', encoded))
# {'id': 1, 'question': 'Is 1+1=3?'}

c_source.asn可用来生成c头文件和.c,辅助理解asn.1:

asn1tools generate_c_source --namespace oer c_source.asn 
Successfully generated oer.h and oer.c.

参考资料

ASN.1 Project (itu.int)

13.1 ASN1简介_OpenSSL 中文手册

证书请求编码 - Win32 apps | Microsoft Learn

ASN.1 tools — asn1tools 0.165.0 documentation

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值