ASN.1 BER 解码器的输入中存在未声明的标签

发布于 2024-08-23 06:57:13 字数 271 浏览 6 评论 0原文

我有一个 ASN.1 语法规范,我希望通过添加一些字段来修改它。如果我使用 BER 和新字段创建一个编码字符串,然后尝试使用不知道这些附加字段的解码器对该字符串进行解码,结果应该是什么?是否会因为存在解码器无法识别的字段而导致解码失败?解码器是否会仅解码它可以识别的字段并完全忽略它不能识别的字段?这完全是解码器实现问题吗?因此,这两种结果都是可能的,具体取决于解码器的实现方式?

现在,我正在使用开源 ASN.1 编译器/解码器,它似乎完全失败,但我不确定这是因为实现还是因为 ASN.1 规则规定了这种结果?

I have a specification for an ASN.1 syntax that I wish to modify by adding some fields. If I create an encoded string using BER with the new fields and then attempt to decode that string using a decoder that doesn't know about these additional fields, what should the result be? Will the decoding fail because there are fields that the decoder doesn't recognize? Will the decoder decode only the fields it can recognize and completely ignore ones it doesn't? Is this completely a decoder-implementation issue so either result is possible, depending upon on how the decoder is implemented?

Right now, I'm using an open source ASN.1 compiler/decoder and it seems to be failing entirely but I'm not sure if this is because of the implementation or because the ASN.1 rules dictate that kind of result?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

维持三分热 2024-08-30 06:57:13

它肯定不依赖于实施。也就是说,如果不同的实现以不同的方式处理它,则其中一个实现是错误的。
如果您使用不需要未知字段的解码器来解码未知字段,则解码应该会失败。它不会跳过未知字段。

然而,有一种方法可以在知道附加字段之前提供它们。它是使用扩展标记(“...”)。假设我们开发了 ASN.1 规范的各种版本。我们称它们为 V1、V2、V3 等。V1 是原始规范,但我们在设计 V1 时了解到,我们可能必须在某个时候对其进行修改。为了允许这种情况,

Z ::= SEQUENCE { a INTEGER,
                 b OCTET STRING,
                 c Message1
}

我们不会像这样声明 Z 是可扩展的。

Z ::= SEQUENCE { a INTEGER,
                 b OCTET STRING,
                 c Message1,
                 ...
}

扩展标记表示,在 c 之后,可能有更多尚未未知的字段。解码器应该将包含此类字段的消息(只要它们遵循 c)视为有效。解码器将无法解码它们,因为它不知道它们应该是什么,但它知道它们是允许的。

假设我们通过插入两个新字段将 V1 更新为 V2,有点像这样

Z ::= SEQUENCE { a INTEGER,            -- V1
                 b OCTET STRING,       -- V1
                 c Message1,           -- V1
                 ...,
             [[  d PrintableString,    -- V2
                 e BOOLEAN          ]] -- V2
}

在这种情况下,V1 版本可以与 V2 版本互操作。 V1 编码器不包括 d 或 e,而 V2 编码器将包括它们。从解码器的角度来看,V1 解码器将接受(但不解码)d 和 e,而 V2 解码器将在找到 d 和 e 时对其进行解码。因此,V1 解码器会接受 V1 和 V2 编码,只要找到 d 和 e,就会忽略它们; V2 解码器也接受 V1 和 V2 编码,注意 V1 编码不包括 d 或 e,但它仍然有效。

我们可以通过其他版本继续这一点,例如,

Z ::= SEQUENCE { a INTEGER,            -- V1
                 b OCTET STRING,       -- V1
                 c Message1,           -- V1
                 ...,
             [[  d PrintableString,    -- V2
                 e BOOLEAN         ]], -- V2
             [[  f PrintableString,    -- V3
                 g ExtensionMessage]]  -- V3
}

V1、V2 和 V3 都可以互操作。

但请注意,由于我们正在处理序列,因此必须保留顺序。没有 d 就没有 e,没有 d、e 和 f 就没有 g。

因此,结论是,如果您的类型定义包含扩展标记,则您可以添加新字段,但如果不包含,则可能不会。

It most certainly does not depend upon implementation. That is, if different implementations are handling it differently, one of them is doing it incorrectly.
If you go to decode unknown fields using a decoder that doesn't expect unknown fields, the decoding should fail. It will not skip over unknown fields.

There is, however, a way to provide for additional fields even before they are known. It is to employ the extension marker ("..."). Let's say that we develop various versions of an ASN.1 specification. Let's call them V1, V2, V3, etc. V1 is the original specification, but we understand at the time we design V1 that it's likely we'll have to modify it at some point. To allow this, instead of something like

Z ::= SEQUENCE { a INTEGER,
                 b OCTET STRING,
                 c Message1
}

we would declare Z to be extensible like this

Z ::= SEQUENCE { a INTEGER,
                 b OCTET STRING,
                 c Message1,
                 ...
}

The extension marker says that, following c, there might be more fields which are as yet unknown. The decoder should treat messages containing such fields, as long as they follow c, as valid. The decoder would not be able to decode them since it doesn't know what they should be, but it knows that they are permissible.

Let's say that we update V1 to V2 by inserting two new fields somewhat like this

Z ::= SEQUENCE { a INTEGER,            -- V1
                 b OCTET STRING,       -- V1
                 c Message1,           -- V1
                 ...,
             [[  d PrintableString,    -- V2
                 e BOOLEAN          ]] -- V2
}

In this case, the V1 version can interoperate with the V2 version. The V1 encoder would not include d or e, while the V2 encoder would include them. From the decoder's viewpoint, the V1 decoder would accept (but not decode) d and e, while the V2 decoder would decode d and e if they are found. So a V1 decoder would accept both V1 and V2 encodings, ignoring d and e whenever they are found; a V2 decoder would also accept both V1 and V2 encodings, noting that a V1 encoding would not include d or e, but it remains valid.

We can continue this through additional versions, for example,

Z ::= SEQUENCE { a INTEGER,            -- V1
                 b OCTET STRING,       -- V1
                 c Message1,           -- V1
                 ...,
             [[  d PrintableString,    -- V2
                 e BOOLEAN         ]], -- V2
             [[  f PrintableString,    -- V3
                 g ExtensionMessage]]  -- V3
}

where V1, V2, and V3 can all interoperate.

Note, however, that since we are dealing with a SEQUENCE, the order must be preserved. You could not have e without d, nor g without d, e, and f.

The conclusion is therefore that if your type definition includes extension markers, you may add new fields, but if it does not, you may not.

苏大泽ㄣ 2024-08-30 06:57:13

这完全取决于规范和实现。简而言之 - 没有办法告诉。这取决于实施。使用 asn.1 的任何给定协议/格式的某些规范将明确声明要忽略未知元素,在这种情况下解码不应失败。

但更常见的是,解码器将拒绝任何不严格符合其应解码的 ASN.1 语法的输入。

This will all depend on the specification and the implementation. In short - there's no way to tell. and it's up to the implementation. Some specification for any given protocol/format utilizing asn.1 will explicitly state that unknown elements are to be ignored, in which case the decoding should not fail.

More common though, decoders will reject any input that does not strictly conform to the ASN.1 syntax it is supposed to be decoding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文