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

8303465: KeyStore of type KeychainStore, provider Apple does not show all trusted certificates #13945

Closed
wants to merge 7 commits into from

Conversation

RealCLanger
Copy link
Contributor

@RealCLanger RealCLanger commented May 11, 2023

With this PR we try to be better in loading certificates from the MacOS Keychain into a JDK Trust store.

The current implementation after JDK-8278449 would only load/trust certificates from an identity (with private key available) and certificates that have explicit trust set in the user domain (as shown by security dump-trust-settings). This, however is not sufficient and does not match the MacOS system behavior, e.g. if you compare with tools like curl or Safari.

This change does the following:

  1. The native method that reads trust settings will call the API SecTrustSettingsCopyTrustSettings on a certificate for both, User and Admin domain.
  2. We now trust self-signed certificates that have an explicit trust entry with no sub-records or no sub-records that would deny the certificate usage for any purpose.
  3. The check for double aliases has been augmented by comparing whether the certificate to be added is the same as the one that is already present. This can happen if a certificate is contained in both, the user and the system keychain, for instance.

I have added a test that verifies whether certificates that should be trusted from "security dump-trust-settings" are contained in the keystore and those that should be disallowed are absent.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change requires CSR request JDK-8308690 to be approved

Issues

  • JDK-8303465: KeyStore of type KeychainStore, provider Apple does not show all trusted certificates
  • JDK-8308690: KeyStore of type KeychainStore, provider Apple does not show all trusted certificates (CSR)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/13945/head:pull/13945
$ git checkout pull/13945

Update a local copy of the PR:
$ git checkout pull/13945
$ git pull https://git.openjdk.org/jdk.git pull/13945/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 13945

View PR using the GUI difftool:
$ git pr show -t 13945

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/13945.diff

Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented May 11, 2023

👋 Welcome back clanger! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk openjdk bot added the rfr Pull request is ready for review label May 11, 2023
@openjdk
Copy link

openjdk bot commented May 11, 2023

@RealCLanger The following label will be automatically applied to this pull request:

  • security

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the security security-dev@openjdk.org label May 11, 2023
@mlbridge
Copy link

mlbridge bot commented May 11, 2023

Webrevs

@MBaesken
Copy link
Member

Hi Christoph, I do not see any reference to kSecTrustSettingsDomainSystem in your coding. Handling at least kSecTrustSettingsDomainUser and kSecTrustSettingsDomainAdmin is good but I am not sure about kSecTrustSettingsDomainSystem . Did you find some documentation why it should be omitted ?

@MBaesken
Copy link
Member

Additionally, the return value of SecTrustSettingsCopyTrustSettings (https://developer.apple.com/documentation/security/1400261-sectrustsettingscopytrustsetting) should better be checked.

@RealCLanger
Copy link
Contributor Author

Hi Christoph, I do not see any reference to kSecTrustSettingsDomainSystem in your coding. Handling at least kSecTrustSettingsDomainUser and kSecTrustSettingsDomainAdmin is good but I am not sure about kSecTrustSettingsDomainSystem . Did you find some documentation why it should be omitted ?

Hi Matthias,
yes, I think it is not nicely documented. I've seen in testing, that kSecTrustSettingsDomainSystem merely holds information for trusted root CAs. So in theory, we could add this. However, other code in that area that we've found out in the wild doesn't do it as well. Let's see what others think about this.

@MBaesken
Copy link
Member

Hi Christoph, I do not see any reference to kSecTrustSettingsDomainSystem in your coding. Handling at least kSecTrustSettingsDomainUser and kSecTrustSettingsDomainAdmin is good but I am not sure about kSecTrustSettingsDomainSystem . Did you find some documentation why it should be omitted ?

Hi Matthias, yes, I think it is not nicely documented. I've seen in testing, that kSecTrustSettingsDomainSystem merely holds information for trusted root CAs. So in theory, we could add this. However, other code in that area that we've found out in the wild doesn't do it as well. Let's see what others think about this.

Yes this seems to be the case. Could you maybe add a one liner comment to libosxsecurity/KeystoreImpl.m (near to the user and admin domain handling) summarizing what you said? And I still prefer checking the return values of the calls to SecTrustSettingsCopyTrustSettings .

@RealCLanger
Copy link
Contributor Author

Yes this seems to be the case. Could you maybe add a one liner comment to libosxsecurity/KeystoreImpl.m (near to the user and admin domain handling) summarizing what you said? And I still prefer checking the return values of the calls to SecTrustSettingsCopyTrustSettings .

Done. 😄

Copy link
Member

Choose a reason for hiding this comment

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

Regarding CFArrayGetValueAtIndex, when looking at other usages of the function in the codebase the result is NULL checked usually. Should we do this here too? I admit the old coding does not have it as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm, can it really be NULL? I mean before we check the array size and then iterate the valid range.

Copy link
Member

@MBaesken MBaesken left a comment

Choose a reason for hiding this comment

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

So please check the CFArrayGetValueAtIndex usage, but otherwise looks okay to me now, thanks for the adjustments.

@openjdk
Copy link

openjdk bot commented May 17, 2023

@RealCLanger This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8303465: KeyStore of type KeychainStore, provider Apple does not show all trusted certificates

Reviewed-by: mbaesken, weijun

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 226 new commits pushed to the master branch:

  • ac1597b: 8309409: Update HttpInputStreamTest and BodyProcessorInputStreamTest to use hg.openjdk.org
  • fdb5893: 8309391: Remove non-failing tests from test/jdk/ProblemList-Virtual.txt
  • 6edd786: 8309265: Serial: Remove the code related to GC overheap limit
  • 61bb014: 8309254: Implement fast-path for ASCII-compatible CharsetEncoders on RISC-V
  • 62c935d: 8308978: regression with a deadlock involving FollowReferences
  • aff9cea: 8303530: Redefine JAXP Configuration File
  • 1bb037b: 8309329: com/sun/jdi/DeferredStepTest.java fails with virtual threads due to not waiting for threads to exit
  • a23bbea: 8309241: ClassForNameLeak fails intermittently as the class loader hasn't been unloaded
  • 931913f: 8309200: java/net/httpclient/ExecutorShutdown fails intermittently, if connection closed during upgrade
  • dc21e8a: 8296411: AArch64: Accelerated Poly1305 intrinsics
  • ... and 216 more: https://git.openjdk.org/jdk/compare/690d3969b80776bf896fbfbfac5eabe7d074c9bb...master

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label May 17, 2023
@seanjmullan
Copy link
Member

Please don't integrate this until I or someone from my team reviews it. Thanks.

@wangweij
Copy link
Contributor

No matter what SecTrustSettingsCopyTrustSettings returns, you will always call jm_createTrustedCertEntry. This means if I add a self-signed certificate but has not added any trusted settings onto it, it will be always trusted. Is this intended?

@RealCLanger
Copy link
Contributor Author

Please don't integrate this until I or someone from my team reviews it. Thanks.

Sure.

@RealCLanger
Copy link
Contributor Author

RealCLanger commented May 17, 2023

No matter what SecTrustSettingsCopyTrustSettings returns, you will always call jm_createTrustedCertEntry. This means if I add a self-signed certificate but has not added any trusted settings onto it, it will be always trusted. Is this intended?

Yes, I will call jm_createTrustedCertEntry for every certificate, at least independent from the results of the SecTrustSettingsCopyTrustSettings calls.

As I outlined in my initial PR description, point 3, the actual check whether a certificate is self-signed is done in the createTrustedCertEntry Java method. So, yes, when there is a self-signed certificate without explicit trust settings, it is always trusted. I thought that this was the intentional behavior even before my changes. However, the difference to the code before is that I look at the certificate and check whether it is a real plain self-signed certificate that can be used for TLS communication - which would be trusted. But what's not trusted now are CA root certificates which also means self-signed but additionally key usage 'keyCertSign' and/or 'cRLSign'. See this code

Makes sense?

@wangweij
Copy link
Contributor

wangweij commented May 18, 2023

Before your new change, such a certificate is not trusted, because SecTrustSettingsCopyTrustSettings returns errSecItemNotFound so jm_createTrustedCertEntry is not called at all.

I am not sure if such a certificate is meant to be always trusted. Note that you can create such an entry with only security add-certificates but not security add-trusted-cert. macOS allows anyone to run the first command but prompts you for an administrator password when running the second. The name of the second command also implies that it's the only way to assign trust to a certificate, IMHO.

@RealCLanger
Copy link
Contributor Author

Before your new change, such a certificate is not trusted, because SecTrustSettingsCopyTrustSettings returns errSecItemNotFound so jm_createTrustedCertEntry is not called at all.

I am not sure if such a certificate is meant to be always trusted. Note that you can create such an entry with only security add-certificates but not security add-trusted-cert. macOS allows anyone to run the first command but prompts you for an administrator password when running the second. The name of the second command also implies that it's the only way to assign trust to a certificate, IMHO.

Hm, after thinking about this again and also comparing with behavior of curl, I think you're right. A self-signed certificate should only be trusted if it has a trust entry (e.g. added by security add-trusted-cert). Somehow I was under the impression that self-signed certificates should be trusted when they exist. But after reading comments etc. again I'm not sure why I thought so at all. 😜 Will update the PR...

@@ -804,8 +803,26 @@ private void createTrustedCertEntry(String alias, List<String> inputTrust,
tce.cert = cert;
tce.certRef = keychainItemRef;

// Check whether a certificate with same alias already exists and is the same
// If yes, we can return here - the existing entry must have the same
// properties and trust settings
Copy link
Contributor

Choose a reason for hiding this comment

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

This is always true, right? I'm not sure how this could happen.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This handles the case, when a certificate is in both, the login (user) and system keychain.

Copy link
Contributor

Choose a reason for hiding this comment

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

How do you know "the existing entry must have the same properties and trust settings"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Trust settings are stored per certificate. That is, when you do security add-trusted-cert, you have to pass a certificate that the entry is created for. It does not matter then, if the certificate is actually present/loaded into any keychain. If the certificate is not in the keychain, a security dump-trust-settings will not show the trust entry then but after you add it, it gets visible.

So, that means, if two certificates are the same, no matter if they were loaded from different keychains or under different aliases (don't know whether the latter is possible though), they will share the same trust records.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see. Thanks.

@wangweij
Copy link
Contributor

Since you removed the key usage checks, can you update the PR description please?

@RealCLanger
Copy link
Contributor Author

Since you removed the key usage checks, can you update the PR description please?

Done.

@wangweij
Copy link
Contributor

The code change looks fine to me. Thanks.

Since this is rather a big behavior change, I think a CSR and a release note are required. The previous release note on this is at https://www.oracle.com/java/technologies/javase/19-relnote-issues.html#JDK-8278449.

@RealCLanger
Copy link
Contributor Author

RealCLanger commented May 23, 2023

The code change looks fine to me. Thanks.

Since this is rather a big behavior change, I think a CSR and a release note are required. The previous release note on this is at https://www.oracle.com/java/technologies/javase/19-relnote-issues.html#JDK-8278449.

Thanks for the review. As for CSR and release note, I guess one could do both, sure. However, it's quite a bit of overhead. And, for JDK-8278449, I don't see a CSR either. 😉 I would consider this change rather as a bugfix for JDK-8278449. So, if you really want me to do it, I'll do it. But please help me to get this processed quickly as I want to get this fix integrated ideally through this week. Please let me know your decision. Thanks.

@openjdk openjdk bot added csr Pull request needs approved CSR before integration and removed ready Pull request is ready to be integrated labels May 23, 2023
@wangweij
Copy link
Contributor

I've started the CSR at https://bugs.openjdk.org/browse/JDK-8308690. Please edit if there is any issue. At the same time, please write a release note. See https://openjdk.org/guide/#release-notes for the process.

@RealCLanger
Copy link
Contributor Author

I've started the CSR at https://bugs.openjdk.org/browse/JDK-8308690. Please edit if there is any issue. At the same time, please write a release note. See https://openjdk.org/guide/#release-notes for the process.

Thanks. I've created a release note and Edited the CSR. Please review both. When you are fine with the CSR, I can finalize it.

@wangweij
Copy link
Contributor

I think you can finalize the CSR now.

@RealCLanger
Copy link
Contributor Author

I think you can finalize the CSR now.

Thx for the hint, done.

@openjdk openjdk bot added ready Pull request is ready to be integrated and removed csr Pull request needs approved CSR before integration labels May 31, 2023
@RealCLanger
Copy link
Contributor Author

@wangweij The CSR is approved. Would you like to review/approve the PR now?

@RealCLanger
Copy link
Contributor Author

Thx.
/integrate

@openjdk
Copy link

openjdk bot commented Jun 5, 2023

Going to push as commit ac41c03.
Since your change was applied there have been 234 commits pushed to the master branch:

  • 11fb5b2: 8309104: [JVMCI] compiler/unsafe/UnsafeGetStableArrayElement test asserts wrong values with Graal
  • a02d800: 8309405: RISC-V: is_deopt may produce unaligned memory read
  • 3fa776d: 8302673: [SuperWord] MaxReduction and MinReduction should vectorize for int
  • 22a9a86: 8309268: C2: "assert(in_bb(n)) failed: must be" after JDK-8306302
  • b6c9232: 8305225: A service broken error despite annotation processor generating it if directives listed
  • 05fb6c6: 8309336: Incorrect switch in enum not reported properly
  • 08c91c2: 8309332: RISC-V: Improve PrintOptoAssembly output of vector nodes
  • ecb1753: 8309334: ProcessTools.main() does not properly set thread names when using the virtual thread wrapper
  • ac1597b: 8309409: Update HttpInputStreamTest and BodyProcessorInputStreamTest to use hg.openjdk.org
  • fdb5893: 8309391: Remove non-failing tests from test/jdk/ProblemList-Virtual.txt
  • ... and 224 more: https://git.openjdk.org/jdk/compare/690d3969b80776bf896fbfbfac5eabe7d074c9bb...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Jun 5, 2023
@openjdk openjdk bot closed this Jun 5, 2023
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Jun 5, 2023
@openjdk
Copy link

openjdk bot commented Jun 5, 2023

@RealCLanger Pushed as commit ac41c03.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

@RealCLanger RealCLanger deleted the JDK-8303465 branch June 5, 2023 08:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
integrated Pull request has been integrated security security-dev@openjdk.org
Development

Successfully merging this pull request may close these issues.

4 participants