openssl: SSLError(58, '[ASN1] nested asn1 error (_ssl.c:4174)') and SSLError(0, 'not enough data: cadata does not contain a certificate (_ssl.c:4159)')

If a CA encodes certificate serial number integer value 102 (0x66) as 02 04 00 00 00 66 instead of 02 01 66, which violates the DER standard the following error shows up when trying to load such a certificate:

SSLError(58, ‘[ASN1] nested asn1 error (_ssl.c:4174)’) and SSLError(0, ‘not enough data: cadata does not contain a certificate (_ssl.c:4159)’)

There are some root certificates in circulation with such non-compliant DER encoding which apparently can’t be replaced, and they are causing a host of issues in various Python based applications since Python uses OpenSSL.

I am reporting this as a bug even though I don’t consider strict adherence to DER standard to be a bug, but something should be done to allow use of such certificates or there should be an option to relax validation so that this results in a warning instead of error.

Other implementations (Firefox, Chromium) accept those certificates.

Relevant Python issue can be found here.

Current workaround is to:

  • Process certificates from Windows certificate store one by one and
  • Skip certificates which raise an exception related to this error

@mattcaswell @t8m Please advise.

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Comments: 22 (5 by maintainers)

Commits related to this issue

Most upvoted comments

I should also mention that the intermediate CA in question also has a non-DER serial number encoding 00000070, but is signed with SHA1, so e.g. its signature from the root would fail to validate under the default RHEL/Fedora crypto policies…

These are looking more and more like vestigial artefacts we can ask libraries, applications and users to gracefully ignore.

No, that’s the “bug” in the python standard library. Application just requests to import certs from Windows store so it can communicate using HTTPS, code for importing is in the library and that’s what crashes. That has been reported by me and it has been fixed but only in Python 3.12 onwards. There are a lot of applications which can’t update from Python 3.10 and 3.11 because of dependency chains so unfortunately that doesn’t fully address the issue.

Good to know that Python (library, rather applications, …) will be updated. Note that even if some OpenSSL releases are updated, it will be some time before users see any updates.

If there isn’t clear evidence that unexpired chains leading to that root will still be around long enough by the time the fix reaches users, I don’t see much value in implementing a workaround.

To be honest, I reported this here with pretty tame expectations because Pyhton team requested that someone does so.

Sure, you did the right thing, what remains to be determined is the right response…

I did say upfront that I don’t consider this a bug as such, but I really think OpenSSL shouldn’t be this nitpicky on things which are safe to ignore like this serial number encoding or comparisons between ANSI and UTF8 strings which fail even when strings are identical just because their DER labels aren’t.

Well, there isn’t any dedicated code for reading serial numbers, they’re just ASN.1 integers, and the code for that is shared across all the structures that hold such values, so a workaround would have collateral side-effects.

Bottom line, if the CA in question is just hanging around various root CA stores, but no longer needed to verify any unexpired certificates, then the solution is to update the Python code in question to skip errors when decoding root CAs, and use what it can decode.

If, on the other hand, there are (despite no mention of this CA on the Serbian national CA website) still live certificates chaining to this and no other root CA, then we plausibly have a reason to support this (though not necessarily compelling if this is the only use-case).

This is why I am asking for evidence of current relevance (unexpired chains leading to EE certs) of this CA.

By the way, here’s the reëncoded root CA cert. It may be sufficient to address the issue.

mup-root.pem.txt

I don’t think OpenSSL should change to accomodate BER (non-DER) encoded X.509 certificates. RFC5280 is quite clear on the DER requirement:

The signatureValue field contains a digital signature computed upon
the ASN.1 DER encoded tbsCertificate.

Does this particular trust anchor (self-signed root CA) have a signature computed over the DER or BER form of the “tbs” certificate? If the signature is actually over the DER encoding as required, just reëncode the tbsCertificate in DER form, and add that to the trust store.

Note that (unless the application specifies otherwise), OpenSSL does not check the self-signatures self-signed certificates, so you can reëncode it even if that in principle “breaks” the signature.

OpenSSL 1.1.1 also refused to parse the example TA, so refusing to parse it is by no means a recent regression. The CA in question can/shoudl just reissue the certificate in question with the same key, but correctly encoded.

Another point to note about my quick-and-dirty hack, is that it modifies the way DER integer parsing is processed for all DER integers regardless of the context. To fix this problem we only really need to be relaxed for this one serial number integer. Perhaps we could declare that one integer as some special type of “relaxed” integer instead of a normal one which would reduce the scope of any change we are making here and perhaps make it much easier to justify having it on.

@vdukhovni may also have a view.

Do we have some examples of such non-compliant CA certs?