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

Add flag to view the first cert only, including for json and pem #256

Merged
merged 1 commit into from
Feb 24, 2022

Conversation

isemaya-square
Copy link
Contributor

@isemaya-square isemaya-square commented Feb 17, 2022

This is a reworking of https://github.com/square/certigo/pull/209/files so that only the first cert is viewed with the --leaf flag, but this also works with the --json flag.

I realized that handling pem would be more complicated and might require some refactoring so I didn't include it this time. Would be willing to open a separate PR for it.

@CLAassistant
Copy link

CLAassistant commented Feb 17, 2022

CLA assistant check
All committers have signed the CLA.

@isemaya-square isemaya-square changed the title Add flag to view the first cert only, including for json and pem Add flag to view the first cert only, including for json (but not pem) Feb 17, 2022
@isemaya-square isemaya-square force-pushed the isemaya/add-leaf-flag-first-cert branch 2 times, most recently from 30185ad to fe7df75 Compare February 17, 2022 10:04
README.md Outdated
@@ -61,6 +61,7 @@ Commands:
-p, --password=PASSWORD Password for PKCS12/JCEKS key stores (reads from TTY if missing).
-m, --pem Write output as PEM blocks instead of human-readable format.
-j, --json Write output as machine-readable JSON format.
-l, --leaf Only display the first certificate. This flag can be paired with --json.

Choose a reason for hiding this comment

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

I would expect a leaf flag to print the leaf, regardless of the formatting of the certificate. (Ex. if for some reason, the file is formatted intermediate -> leaf). Maybe we could rename the flag?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is a good point. We could rename the flag to --first or something like that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

On the other hand, can we assume that the leaf comes before any intermediates or roots in a cert chain? Since it looks like that is the convention.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's from TLS: https://datatracker.ietf.org/doc/html/rfc8446#section-4.4.2 (this was the same in earlier versions, too).

The sender's certificate MUST come in the first
CertificateEntry in the list. Each following certificate SHOULD
directly certify the one immediately preceding it. Because
certificate validation requires that trust anchors be distributed
independently, a certificate that specifies a trust anchor MAY be
omitted from the chain, provided that supported peers are known to
possess any omitted certificates.

Copy link
Contributor

Choose a reason for hiding this comment

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

But I think first is a reasonable name for the flag. In the connect case, the first cert is guaranteed to be the leaf, but in the dump case, it's not (e.g. what if we're dumping a trust bundle and only want the first cert for some reason). So first is probably more accurate.

@isemaya-square isemaya-square changed the title Add flag to view the first cert only, including for json (but not pem) Add flag to view the first cert only, including for json and pem Feb 23, 2022
@isemaya-square isemaya-square force-pushed the isemaya/add-leaf-flag-first-cert branch 2 times, most recently from 9f04ce2 to 5a206ec Compare February 23, 2022 03:48
cli/cli_test.go Outdated Show resolved Hide resolved
Copy link
Contributor

@jdtw jdtw left a comment

Choose a reason for hiding this comment

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

PR looks good. You'll have to merge in my changes in #259, and then you won't need any of the change in cli_test.go.

(Note that it's not safe to change the expected test output, since then it would break on versions of Go earlier than 1.17.7)

@isemaya-square
Copy link
Contributor Author

PR looks good. You'll have to merge in my changes in #259, and then you won't need any of the change in cli_test.go.

(Note that it's not safe to change the expected test output, since then it would break on versions of Go earlier than 1.17.7)

Thanks, I'll merge #259 first and get rid of my cli_test.go changes.

added dump-pkcs12-chain-to-pem.t to contrast with dump-pkcs12-chain-to-pem-first-only and show that multiple pem blocks are dumped without the --first flag.t
@isemaya-square isemaya-square merged commit d167b5f into master Feb 24, 2022
@isemaya-square isemaya-square deleted the isemaya/add-leaf-flag-first-cert branch February 24, 2022 00:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants