Open dirkjanm opened 2 years ago
Interesting... That does feel like a bug (or just a limitation in how asn1crypto
deals with Any
), but there might be a relatively straightforward workaround/fix here. I'm pretty sure that this will work as intended once you explicitise the DEFINED BY
relationship in much the same way as for ContentInfo
: https://github.com/wbond/asn1crypto/blob/8a4c621e34f8cbdc83b047aa617e66dcbfde0f75/asn1crypto/cms.py#L527-L528.
In other words
class AnotherName(Sequence):
_fields = [
('type_id', ObjectIdentifier),
('value', Any, {'explicit': 0}),
]
_oid_pair = ('type_id', 'value')
_oid_specs = {}
And you can then even add some mappings to _oid_specs
to automagically handle the case you want, for example:
AnotherName._oid_specs['1.3.6.1.4.1.311.20.2.3'] = UTF8String
I unfortunately don't have time to test this right now, but it's worth a try. If it works, it's definitely worth doing a PR IMO. In the meantime, you can (reasonably safely) monkeypatch this kind of thing into asn1crypto
at runtime... ;)
hey Matthias, thanks, this does work pretty well for my problem, your proposed additions work and it can construct the asn1 properly. However it does not fix the reverse parsing from asn1 back to python. Not sure if that's a separate bug or has the same root cause.
@dirkjanm Looking at the ASN.1 at https://datatracker.ietf.org/doc/html/rfc5280#page-128, it does say that the ANY tag is defined by the type-id. It appears the definition of AnotherName
is currently missing:
_oid_pair = ('type_id', 'value')
_oid_specs = {}
as suggested by @MatthiasValvekens.
My guess is that I hadn't run into such a tag yet, so it hasn't been implemented.
That said, you did seem to identify a bug in the parsing.
Hey, I'm having some issues with creating a cert request with a subject alt name for Microsoft certificates. It comes down to the asn1 encoding of the AnotherName structure, which either is broken or that I simply don't understand how to construct.
I have the following sample code:
This generates the following ASN.1:
However, any Microsoft code can't parse this, because it expects a structure like this:
I'm not an expert on asn1 but it seems something goes wrong with the explicit tagging of the element. In fact, when I try to debug this, the library throws an error:
Curiously enough, when I change the structure of the object in x509.py here: https://github.com/wbond/asn1crypto/blob/8a4c621e34f8cbdc83b047aa617e66dcbfde0f75/asn1crypto/x509.py#L1167
To the following:
(so instead of
Any
specify aUTF8String
explicitly) Then it encodes without issue, and the bytes are as expected:With both the original and changed version of the library, the reverse parsing breaks:
Thanks for your work on this, hope this helps track down the issue!