210-Ensure-that-EXFLAG_INVALID_POLICY-is-checked-even-in.patch 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. From 1dd43e0709fece299b15208f36cc7c76209ba0bb Mon Sep 17 00:00:00 2001
  2. From: Matt Caswell <[email protected]>
  3. Date: Tue, 7 Mar 2023 16:52:55 +0000
  4. Subject: [PATCH] Ensure that EXFLAG_INVALID_POLICY is checked even in leaf
  5. certs
  6. Even though we check the leaf cert to confirm it is valid, we
  7. later ignored the invalid flag and did not notice that the leaf
  8. cert was bad.
  9. Fixes: CVE-2023-0465
  10. Reviewed-by: Hugo Landau <[email protected]>
  11. Reviewed-by: Tomas Mraz <[email protected]>
  12. (Merged from https://github.com/openssl/openssl/pull/20587)
  13. --- a/crypto/x509/x509_vfy.c
  14. +++ b/crypto/x509/x509_vfy.c
  15. @@ -1654,15 +1654,23 @@ static int check_policy(X509_STORE_CTX *
  16. goto memerr;
  17. /* Invalid or inconsistent extensions */
  18. if (ret == X509_PCY_TREE_INVALID) {
  19. - int i;
  20. + int i, cbcalled = 0;
  21. /* Locate certificates with bad extensions and notify callback. */
  22. - for (i = 1; i < sk_X509_num(ctx->chain); i++) {
  23. + for (i = 0; i < sk_X509_num(ctx->chain); i++) {
  24. X509 *x = sk_X509_value(ctx->chain, i);
  25. + if ((x->ex_flags & EXFLAG_INVALID_POLICY) != 0)
  26. + cbcalled = 1;
  27. CB_FAIL_IF((x->ex_flags & EXFLAG_INVALID_POLICY) != 0,
  28. ctx, x, i, X509_V_ERR_INVALID_POLICY_EXTENSION);
  29. }
  30. + if (!cbcalled) {
  31. + /* Should not be able to get here */
  32. + ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR);
  33. + return 0;
  34. + }
  35. + /* The callback ignored the error so we return success */
  36. return 1;
  37. }
  38. if (ret == X509_PCY_TREE_FAILURE) {