Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

X.509: Fix bug in SAN parsing and enhance negative testing #2839

Conversation

hanno-becker
Copy link

@hanno-becker hanno-becker commented Sep 13, 2019

Summary: This PR fixes #2838 and adds numerous negative CRT parsing tests for the SubjectAlternativeName extension.

Dependencies: Based on #2836 to avoid conflicts at merge-time.

@hanno-becker hanno-becker added bug enhancement mbed TLS team needs-review Every commit must be reviewed by at least two team members, component-x509 needs-ci Needs to pass CI tests labels Sep 13, 2019
@hanno-becker hanno-becker added the needs-preceding-pr Requires another PR to be merged first label Sep 13, 2019
Copy link
Contributor

@RonEld RonEld left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One comment regarding using stack variables, other than that, it seems to me ok at the moment

buf->p = *p;
buf->len = tag_len;
*p += buf->len;
cur->buf = tmp_san_buf;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although technically this works, since it is assigning by value tmp_buf to cur->buf, I don't like that there is an assignment of a structure defined in the stack, without a proper memory copying.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's deliberate - why do you prefer a memcpy()?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you are counting for the struct to be copied by value, it wouldn't do a deep-copy(neither does memcpy actually), and this is not future proof.
In addition, in a matter of style, I always prefer using to copy data from non primitive types.

Hanno Becker added 10 commits September 17, 2019 13:10
Judging from its name, the purpose of the test

   TBSCertificate v3, ext CertificatePolicies tag, bool len missing

in test_suite_x509parse.data is to exercise the X.509 parsing stack's
behaviour when parsing a CertificatePolicy extension which lacks the
length field of the boolean 'Criticality' value.

However, the test fails at an earlier stage due to a mismatch of inner
and outer length of the explicit ASN.1 extensions structure.

Since we already have tests exercising

- mismatch of inner and outer length in the extensions structure, namely
  'X509 CRT ASN1 (TBS, inv v3Ext, inner tag invalid)'
- missing length of the 'Criticality' field in an extension, namely
  'X509 CRT ASN1 (TBS, inv v3Ext, critical length missing)'

and since for both tests there's no relevance to the use of the
policy extension OID, the test

  'TBSCertificate v3, ext CertificatePolicies tag, bool len missing'

can be dropped.
This commit moves the X.509 negative parsing tests for the
CertificatePolicy extension to the place where negative
testing of other extensions happens.
This commit modifies the test

   X509 CRT ASN1 (TBSCertificate v3, inv CertificatePolicies, data missing)

which exercises the behaviour of the X.509 CRT parser when facing a
CertificatePolicy extension with empty data field.

The following adaptations are made:
- The subject ID and issuer ID are modified to have length 0.
  The previous values `aa` and `bb` are OK, but a generic ASN.1
  parser will try to interpret them as ASN.1 tags and fail. For
  maintainability, it's therefore better to use something that
  can be parsed as ASN.1, and an empty ID is the easiest solution
  here.
- The TBS part of the certificate wasn't followed by signature
  algorithm and signature fields, which makes the test incompatible
  with future changes swapping to breadth-first parsing of
  certificates.
This commit adds multiple test cases to the X.509 CRT parsing test suite
exercising the stack's behaviour when facing CertificatePolicy extensions
that are malformed for a variety of reasons. It follows the same scheme
as in other negative parsing tests: For each ASN.1 component, have test
cases for (a) unexpected tag, (b) missing length, (c) invalid length
encoding, (d) length out of bounds.
Fixes Mbed-TLS#2838. See the issue description for more information.
- ASN.1 parsing functions check that length don't exceed buffer bounds,
  so checks `p + len > end` are redundant.
- If `p + len == end`, this is erroneous because we expect further fields,
  which is automatically caught by the next ASN.1 parsing call.

Hence, the two branches handling `p + len >= end` in x509_get_other_name()
can be removed.

Further, zeroization of the `other_name` structure isn't necessary
because it's not confidential (and it's also not performed on other
error conditions in this function).
@gilles-peskine-arm gilles-peskine-arm removed needs-ci Needs to pass CI tests needs-preceding-pr Requires another PR to be merged first labels Apr 17, 2020
@danh-arm danh-arm added this to the Backlog milestone May 20, 2020
@hanno-becker hanno-becker self-assigned this Aug 14, 2020
@mpg mpg added the historical-reviewing Currently reviewing (for legacy PR/issues) label Jun 24, 2022
@daverodgman daverodgman removed this from the Backlog milestone Jul 1, 2022
@daverodgman daverodgman added historical-reviewed Reviewed & agreed to keep legacy PR/issue and removed historical-reviewing Currently reviewing (for legacy PR/issues) labels Jul 1, 2022
@daverodgman daverodgman added the priority-medium Medium priority - this can be reviewed as time permits label Jul 1, 2022
@AndrzejKurek
Copy link
Contributor

Continued in #6882.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug component-x509 enhancement historical-reviewed Reviewed & agreed to keep legacy PR/issue needs-review Every commit must be reviewed by at least two team members, priority-medium Medium priority - this can be reviewed as time permits
Projects
None yet
Development

Successfully merging this pull request may close these issues.

X.509: SubjectAlternativeName components are not parsed
8 participants