Skip to content

Implement SCT verification - #24

Merged
woodruffw merged 18 commits into
mainfrom
alex/sct-verification
Apr 22, 2022
Merged

woodruffw merged 18 commits into
mainfrom
alex/sct-verification

Conversation

@tetsuo-cpp

Copy link
Copy Markdown
Contributor

Closes #22

@tetsuo-cpp
tetsuo-cpp marked this pull request as draft April 10, 2022 14:51
@tetsuo-cpp

tetsuo-cpp commented Apr 10, 2022 •

Copy link
Copy Markdown
Contributor Author

This PR is based off the branch implementing the sign command. Unfortunately I can't point this PR at the sign branch and show the changes in isolation because the sign branch belongs to a fork.

At the moment, I can't seem to get the verification to work and am trying to figure out why (it's not very debuggable since it just throws a signature mismatch). I assume I'm somehow misconstructing the digitally signed part.

I've actually tried hacking https://github.com/pierky/sct-verify to use the SCT, certificate, etc from a Fulcio example but I can't even get that to verify correctly. So whatever mistake I'm making, I'm reproducing it there too.

I'll continue investigating this...

@tetsuo-cpp
tetsuo-cpp requested a review from di April 10, 2022 14:59
@tetsuo-cpp
tetsuo-cpp force-pushed the alex/sct-verification branch from c2cacb0 to 69e472a Compare April 12, 2022 05:48
@tetsuo-cpp
tetsuo-cpp force-pushed the alex/sct-verification branch from 69e472a to 0ec1a69 Compare April 12, 2022 07:28
@Hayden-IO

Copy link
Copy Markdown
Contributor

Have you taken a look at https://pypi.org/project/ctutlz/ or https://github.com/theno/ctutlz?

Digging through the code a bit, this should provide the utilities that you need to verify SCTs. See for example https://github.com/theno/ctutlz/blob/db8efcf785efb4dcc7324ddba629d49e743890be/ctutlz/scripts/verify_scts.py#L94-L109, which handles verification of embedded SCTs.

Something to note: Fulcio is currently returning SCTs detached, not embedded in the certificate. I'm actively working on changing Fulcio to generate certificates with embedded SCTs. If you want to test against the live instance of Fulcio, you might have to hack around ctutlz a bit.

@Hayden-IO

Copy link
Copy Markdown
Contributor

If you're seeing signature validation errors, it's likely that the bits that you're verifying are not the same bits that the CT log signed. For example, you need to consider that CT entry signature is generated over the TBS (to-be-signed) portion of the certificate (no cert signature), and it excludes the CT log extension. Additionally when reconstructing the merkle leaf to verify the signature, you will need to include the hash of the public key of the issuing certificate.

ctutlz handles these currently.

@tetsuo-cpp

tetsuo-cpp commented Apr 19, 2022 •

Copy link
Copy Markdown
Contributor Author

Thanks for the information @haydentherapper. Sorry for the late reply, I've only just been able to revisit this now.

Have you taken a look at https://pypi.org/project/ctutlz/ or https://github.com/theno/ctutlz?

I was looking at ctutlz at the beginning of this task #22 (comment) and was intending to use it and sct-verify as a reference.

If you're seeing signature validation errors, it's likely that the bits that you're verifying are not the same bits that the CT log signed. For example, you need to consider that CT entry signature is generated over the TBS (to-be-signed) portion of the certificate (no cert signature), and it excludes the CT log extension.

This is the way that ctutlz is assembling it: https://github.com/theno/ctutlz/blob/master/ctutlz/sct/signature_input.py#L20. I can't see much of a difference but I guess I'm doing something subtly wrong.

Regarding the point about "excluding the CT log extension", does that mean that the extension length should not go at the end of the data to be signed? The structure looks like this, but ctutlz and sct-verify both just have the extension length at the end (which is always 0).

I'm continuing to investigate.

@Hayden-IO

Hayden-IO commented Apr 19, 2022 •

Copy link
Copy Markdown
Contributor

Regarding the point about "excluding the CT log extension", does that mean that the extension length should not go at the end of the data to be signed? The structure looks like this, but ctutlz and sct-verify both just have the extension length at the end (which is always 0).

There's two ways to verify an SCT. One is by verifying a detached SCT, where the SCT is presented alongside the certificate - The signature input is generated here, what you linked above.

The other way is by verifying an SCT that's embedded in the certificate, meaning you don't have to store the SCT alongside the certificate since the SCT is embedded in an X.509 extension. That's constructed here. When I mentioned "excluding the CT log extension", this is referring to the construction of the signature input for embedded SCT verification. The lengths are still included. There are certain extensions (here] that are excluded when building the input.

Note that Fulcio currently only supports detached SCTs, but in the coming weeks, will support both. I'd recommend preferring the latter, embedded SCTs, because it allows you to verify the SCT any time you use the certificate.

Looking again at the first case, a detached SCT, I'm a bit confused why the ctutlz library doesn't use the TBS portion of the signature here when constructing the input. The DER encoding is not the same as the TBS portion. This could be the issue?

@tetsuo-cpp

tetsuo-cpp commented Apr 20, 2022 •

Copy link
Copy Markdown
Contributor Author

Thanks for explaining.

Note that Fulcio currently only supports detached SCTs, but in the coming weeks, will support both. I'd recommend preferring the latter, embedded SCTs, because it allows you to verify the SCT any time you use the certificate.

I've talked to @di about this recently and our plan at the moment is to begin by supporting detached SCTs and then move to supporting embedded SCTs later. The reason is that we're intending to demo this functionality soon.

Looking again at the first case, a detached SCT, I'm a bit confused why the ctutlz library doesn't use the TBS portion of the signature here when constructing the input. The DER encoding is not the same as the TBS portion. This could be the issue?

From looking at the RFC, it only specifies to use the TBS portion for the PreCert case. It isn't clear to me whether that's how it works for the ASN.1 case but I just assumed because ctutlz and sct-verify do it like this and it also seems to be how cosign does it according to this snippet.

Comment thread sigstore/_internal/sct.py Resolved
@woodruffw woodruffw self-assigned this Apr 21, 2022
@tetsuo-cpp

Copy link
Copy Markdown
Contributor Author

It looks like there are a few flags preceding the signature that are added here. The code comment refers an RFC describing TLS encoding here.

If I inspect an SCT that I recorded like this, I can see the flag values:

(env) tetsuo@Alexs-MacBook-Pro sigstore-python % python
Python 3.9.7 (default, Oct 12 2021, 22:38:23)
[Clang 13.0.0 (clang-1300.0.29.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import base64
>>> signature = base64.b64decode("BAMARzBFAiAUekMyQRyeS83yoxcdsHcv+u3qQPDADXiyYAZSlJrM7QIhAI5ZM1GdkVTH5gXyj6IM2JEP+sMc1/o2srrTxCS7xdKl")
>>> signature
b'\x04\x03\x00G0E\x02 \x14zC2A\x1c\x9eK\xcd\xf2\xa3\x17\x1d\xb0w/\xfa\xed\xea@\xf0\xc0\rx\xb2`\x06R\x94\x9a\xcc\xed\x02!\x00\x8eY3Q\x9d\x91T\xc7\xe6\x05\xf2\x8f\xa2\x0c\xd8\x91\x0f\xfa\xc3\x1c\xd7\xfa6\xb2\xba\xd3\xc4$\xbb\xc5\xd2\xa5'
>>> signature[0]
4 // NOTE(alex): This is the hash algorithm enum. 4 maps to sha256.
>>> signature[1]
3 // NOTE(alex): This is the signature algorithm enum. 3 maps to ecdsa.
>>> signature[4:]
b'0E\x02 \x14zC2A\x1c\x9eK\xcd\xf2\xa3\x17\x1d\xb0w/\xfa\xed\xea@\xf0\xc0\rx\xb2`\x06R\x94\x9a\xcc\xed\x02!\x00\x8eY3Q\x9d\x91T\xc7\xe6\x05\xf2\x8f\xa2\x0c\xd8\x91\x0f\xfa\xc3\x1c\xd7\xfa6\xb2\xba\xd3\xc4$\xbb\xc5\xd2\xa5'

The first two bytes contain sensible values for the type of signature we're getting so I believe we're interpreting that correctly.

The only question remaining for me is why we have to discard 4 bytes instead of 2. I can't see Fulcio putting anything else into the signature and the RFC describing TLS encoding doesn't say anything about padding between values.

@tetsuo-cpp
tetsuo-cpp marked this pull request as ready for review April 22, 2022 07:49
@Hayden-IO

Copy link
Copy Markdown
Contributor

I think you may have just found this, but we are actually sending a slightly different struct back to the client - https://github.com/sigstore/fulcio/blob/main/pkg/api/grpc_server.go#L135

This was not intentional, but when I found this, I didn’t want to break existing clients, so we left the struct as is. The embedded SCT will be in the correct format.

@woodruffw

Copy link
Copy Markdown
Member

Thanks for pointing that out!

So, to clarify: the signature in the detached SCT will continue to have this (TLS 1.2-style) prefix, but the embedded SCT won't? Am I understanding that correctly?

@Hayden-IO

Copy link
Copy Markdown
Contributor

That's correct, the embedded SCT will be the SignedCertificateTimestampList as defined in https://datatracker.ietf.org/doc/html/rfc6962#section-3.3.

Given this confusion, I'm considering just changing the detached SCT to the proper value, a SignedCertificateTimestamp struct. (We're having to make a breaking change to the Cosign client anyways to support embedded SCTs).

@woodruffw
woodruffw merged commit 412bfab into main Apr 22, 2022
@woodruffw
woodruffw deleted the alex/sct-verification branch April 22, 2022 19:17
javanlacerda pushed a commit to javanlacerda/sigstore-python that referenced this pull request Feb 23, 2024
* Remove Docker infrastructure

* Create initial `sigstore-conformance` GitHub Action

* Code documentation

* Fix test comment

* Add variable for debug logging

* Add linting to CI

* Add selftest to conformance test `sigstore-python`

* Make `action.py` executable

* Check in missing init file

* Rename selftest job

* Use mypy and fix errors

* Make mypy ignore the pytest import

* Reword entrypoint documentation

* Move token permissions into selftest job

* Remove explicit workspace param

* Add more assertions to basic test

* It's a method not a property

* The client parameterization doesn't need to be a dynamic fixture
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.

Implement SCT verification

4 participants