简体   繁体   English

ASN.1标签含义

[英]ASN.1 tag meaning

I have a tag with the following value: Tag(nr=16, typ=32, cls=0)我有一个具有以下值的标签: Tag(nr=16, typ=32, cls=0)

What do each of those values mean?这些值中的每一个是什么意思?

Based on https://en.wikipedia.org/wiki/X.690#Types , I think typ=32 should mean TIME-OF-DAY, but that doesn't make sense in my context.基于https://en.wikipedia.org/wiki/X.690#Types ,我认为 typ=32 应该表示 TIME-OF-DAY ,但这在我的上下文中没有意义。

So what is my context?那么我的背景是什么? I'm glad you asked: I'm unpacking an ECDH_SECP256R1 private key created with the following:很高兴您问到:我正在解压缩使用以下内容创建的 ECDH_SECP256R1 私钥:

>>> import asn1
>>> from Crypto.IO.PKCS8 import unwrap
>>> import binascii
>>> from CryptoMobile.EC import *
>>> ec_B = ECDH_SECP256R1()
>>> ec_B.get_privkey()
b'0\x81\x87\x02\x01\x000\x13\x06\x07*\x86H\xce=\x02\x01\x06\x08*\x86H\xce=\x03\x01\x07\x04m0k\x02\x01\x01\x04 Wj\x80L\t\xc5\xa1oW\xd9\xbbFs{TH\xea\xe1*\x9a\x95\xf6\xe1\xd1\xe1\x9a%\xc8\xb7\xb3~h\xa1D\x03B\x00\x04\xf0T\xear^x\xb6\xa8\xe8\x04T \xf1\xbe\x81\xac\xba\xfdJ\xa10Y_\x0b\xb5^\x140\xaf\xa1S\x14|@\xfaV\x08\x15\x05Cr\xa7\xd1F\xcevq(H\x8a\x8d\xa2\xce\x84\xaa<\x0b>\xf5\xe0\xf1\xed\x9f\x90'
>>> unwrap(ec_B.get_privkey())
('1.2.840.10045.2.1', b'0k\x02\x01\x01\x04 Wj\x80L\t\xc5\xa1oW\xd9\xbbFs{TH\xea\xe1*\x9a\x95\xf6\xe1\xd1\xe1\x9a%\xc8\xb7\xb3~h\xa1D\x03B\x00\x04\xf0T\xear^x\xb6\xa8\xe8\x04T \xf1\xbe\x81\xac\xba\xfdJ\xa10Y_\x0b\xb5^\x140\xaf\xa1S\x14|@\xfaV\x08\x15\x05Cr\xa7\xd1F\xcevq(H\x8a\x8d\xa2\xce\x84\xaa<\x0b>\xf5\xe0\xf1\xed\x9f\x90', b'\x06\x08*\x86H\xce=\x03\x01\x07')
>>> 
>>> 
>>> decoder = asn1.Decoder()
>>> decoder.start(unwrap(ec_B.get_privkey())[1])
>>> tag, value = decoder.read()
>>> tag
Tag(nr=16, typ=32, cls=0)
>>> value
b'\x02\x01\x01\x04 Wj\x80L\t\xc5\xa1oW\xd9\xbbFs{TH\xea\xe1*\x9a\x95\xf6\xe1\xd1\xe1\x9a%\xc8\xb7\xb3~h\xa1D\x03B\x00\x04\xf0T\xear^x\xb6\xa8\xe8\x04T \xf1\xbe\x81\xac\xba\xfdJ\xa10Y_\x0b\xb5^\x140\xaf\xa1S\x14|@\xfaV\x08\x15\x05Cr\xa7\xd1F\xcevq(H\x8a\x8d\xa2\xce\x84\xaa<\x0b>\xf5\xe0\xf1\xed\x9f\x90'

I'm thinking that tag tells me how to understand the value.我在想那个标签告诉我如何理解这个价值。 Or perhaps Python has already understood the value from the tag?或者也许 Python 已经从标签中理解了值? Is this the final decoded value?这是最终的解码值吗?

>>> print(binascii.hexlify(value))
b'0201010420576a804c09c5a16f57d9bb46737b5448eae12a9a95f6e1d1e19a25c8b7b37e68a14403420004f054ea725e78b6a8e8045420f1be81acbafd4aa130595f0bb55e1430afa153147c40fa560815054372a7d146ce767128488a8da2ce84aa3c0b3ef5e0f1ed9f90'

Or do I have to use knowledge of the tag to further decode that?还是我必须使用标签知识来进一步解码?

Note that, in ASN.1, the tag has no meaning... it is just used to encode and decode data (and only in BER, DER, CER encoding rules)请注意,在 ASN.1 中,标签没有任何意义……它仅用于对数据进行编码和解码(并且仅在 BER、DER、CER 编码规则中)

To have the meaning of the data, you always need the ASN.1 specification ( ECPrivateKey in @Crypt32 answer)要了解数据的含义,您始终需要 ASN.1 规范(@Crypt32 中的ECPrivateKey答案)

The all concept of encoding and decoding BER is explained in document x.690编码和解码 BER 的所有概念在文档 x.690 中进行了解释

When you encode a tag (see 8.1.2 in the x.690 doc), you need 3 pieces of information:当您对标签进行编码时(请参阅 x.690 文档中的 8.1.2),您需要 3 条信息:

  • Class: Universal, Application, Context or Private Class:通用、应用、上下文或私有
  • The primitive/constructed flag原始/构造标志
  • The tag number标签号

Let's take ECPrivateKey ...让我们以ECPrivateKey ...

  • Class is Universal ('00'B) which is the class reserved for ASN.1 types provided by the spec (SEQUENCE) Class 是通用的 ('00'B),它是为规范提供的 ASN.1 类型保留的 class (SEQUENCE)
  • Flag is constructed.标志已构建。 Because a SEQUENCE is a container of components因为 SEQUENCE 是组件的容器
  • Tag number is 16 (provided by ASN.1)标签号为 16(由 ASN.1 提供)

So, back to your question:所以,回到你的问题:

Tag(nr=16, typ=32, cls=0)标签(nr=16, typ=32, cls=0)

  • nr is the tag number nr 是标签号
  • typ is the primitive/constructed flag typ 是原始/构造标志
  • cls is the class cls 是 class

Note that the names are from the tool you are using and not ASN.1 vocabulary请注意,名称来自您正在使用的工具,而不是 ASN.1 词汇表

EDIT: it is actually well summarized in the link you provided https://en.wikipedia.org/wiki/X.690#Types编辑:它实际上在您提供的链接中得到了很好的总结https://en.wikipedia.org/wiki/X.690#Types

Go to https://asn1.io/asn1playground/ and compile following specification... Go 到https://asn1.io/asn1playground/并编译以下规范...

Example DEFINITIONS EXPLICIT TAGS ::= 
BEGIN
ECPrivateKey ::= SEQUENCE {
    version        INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
    privateKey     OCTET STRING,
    -- parameters [0] ECParameters {{ NamedCurve }} OPTIONAL, --
    publicKey  [1] BIT STRING OPTIONAL
}                                          
END

Decode following value (I just put 306B before your value)... which means a SEQUENCE of 107 bytes解码以下值(我只是将 306B 放在您的值之前)...这意味着 107 字节的 SEQUENCE

306B0201010420576a804c09c5a16f57d9bb46737b5448eae12a9a95f6e1d1e19a25c8b7b37e68a14403420004f054ea725e78b6a8e8045420f1be81acbafd4aa130595f0bb55e1430afa153147c40fa560815054372a7d146ce767128488a8da2ce84aa3c0b3ef5e0f1ed9f90

You see that your value is a sequence of version, privateKey and publicKey (the optional parameters is absent)你看到你的value是version、privateKey和publicKey的序列(可选参数不存在)

ECPrivateKey SEQUENCE: tag = [UNIVERSAL 16] constructed; length = 107
  version INTEGER: tag = [UNIVERSAL 2] primitive; length = 1
    1
  privateKey OCTET STRING: tag = [UNIVERSAL 4] primitive; length = 32
    0x576a804c09c5a16f57d9bb46737b5448ea ...
  publicKey : tag = [1] constructed; length = 68
    BIT STRING: tag = [UNIVERSAL 3] primitive; length = 66
      0x0004f054ea725e78b6a8e8045420f1be81 ...
Successfully decoded 109 bytes.
rec1value ECPrivateKey ::= 
{
  version ecPrivkeyVer1,
  privateKey '576A804C09C5A16F57D9BB46737B5448EA ...'H,
  publicKey '00000100 11110000 01010100 11101010 011 ...'B
}

Tag 16 is SEQUENCE or SEQUENCE OF .标签 16 是SEQUENCESEQUENCE OF typ=32 suggests that bit 6 is set to 1, so SEQUENCE is in constructed form. typ=32表示第 6 位设置为 1,因此 SEQUENCE 为构造形式。 In fact, in cryptography messages, SEQUENCE is always used in constructed form.事实上,在密码学消息中,SEQUENCE 总是以构造形式使用。 I have no idea what cls=0 means (I'm not familiar with Python).我不知道cls=0是什么意思(我不熟悉 Python)。

SEQUENCE is a struct with arbitrary fields. SEQUENCE是具有任意字段的结构。 SEQUENCE OF is an ordered array of elements of same type (primitive or constructed). SEQUENCE OF是相同类型(原始或构造)元素的有序数组。 Exact type ( SEQUENCE of SEQUENCE OF ) is determined by ASN.1 module definition.确切类型( SEQUENCE of SEQUENCE OF )由 ASN.1 模块定义确定。

value in your case is a valid ECPrivateKey (as per RFC 5915 ) struct as defined below:在您的情况下, value是一个有效的ECPrivateKey (根据RFC 5915 )结构,定义如下:

ECPrivateKey ::= SEQUENCE {
    version        INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
    privateKey     OCTET STRING,
    parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
    publicKey  [1] BIT STRING OPTIONAL
}

and the dump in ASN.1 Editor:和 ASN.1 编辑器中的转储:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM