-
Notifications
You must be signed in to change notification settings - Fork 165
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
Added support for sha384/sha512 hash algorithms in hashedrekords #1959
Merged
bobcallaway
merged 3 commits into
sigstore:main
from
trail-of-forks:add-sha384-512-hashedrekord
Jan 23, 2024
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
// | ||
// Copyright 2024 The Sigstore Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//go:build e2e | ||
|
||
package hashedrekord | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"crypto" | ||
"crypto/ecdsa" | ||
"crypto/elliptic" | ||
"crypto/rand" | ||
"os" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/go-openapi/strfmt" | ||
"github.com/go-openapi/swag" | ||
"github.com/sigstore/rekor/pkg/client" | ||
"github.com/sigstore/rekor/pkg/generated/client/entries" | ||
"github.com/sigstore/rekor/pkg/generated/models" | ||
"github.com/sigstore/rekor/pkg/types" | ||
"github.com/sigstore/sigstore/pkg/cryptoutils" | ||
"github.com/sigstore/sigstore/pkg/signature" | ||
) | ||
|
||
func rekorServer() string { | ||
if s := os.Getenv("REKOR_SERVER"); s != "" { | ||
return s | ||
} | ||
return "http://localhost:3000" | ||
} | ||
|
||
// TestSHA256HashedRekordEntry tests sending a valid HashedRekord proposed entry. | ||
func TestSHA256HashedRekordEntry(t *testing.T) { | ||
privKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) | ||
if err != nil { | ||
t.Fatalf("error generating key: %v", err) | ||
} | ||
pubBytes, err := cryptoutils.MarshalPublicKeyToPEM(privKey.Public()) | ||
if err != nil { | ||
t.Fatalf("error marshaling public key: %v", err) | ||
} | ||
|
||
data := []byte("data") | ||
signer, err := signature.LoadSigner(privKey, crypto.SHA256) | ||
if err != nil { | ||
t.Fatalf("error loading verifier: %v", err) | ||
} | ||
signature, err := signer.SignMessage(bytes.NewReader(data)) | ||
if err != nil { | ||
t.Fatalf("error signing message: %v", err) | ||
} | ||
|
||
ap := types.ArtifactProperties{ | ||
ArtifactBytes: []byte("data"), | ||
ArtifactHash: "sha256:3a6eb0790f39ac87c94f3856b2dd2c5d110e6811602261a9a923d3bb23adc8b7", | ||
PublicKeyBytes: [][]byte{pubBytes}, | ||
PKIFormat: "x509", | ||
SignatureBytes: signature, | ||
} | ||
|
||
ei := NewEntry() | ||
|
||
entry, err := ei.CreateFromArtifactProperties(context.Background(), ap) | ||
if err != nil { | ||
t.Fatalf("error creating entry: %v", err) | ||
} | ||
|
||
rc, err := client.GetRekorClient(rekorServer()) | ||
if err != nil { | ||
t.Errorf("error getting client: %v", err) | ||
} | ||
|
||
params := &entries.CreateLogEntryParams{} | ||
params.SetProposedEntry(entry) | ||
params.SetContext(context.Background()) | ||
params.SetTimeout(5 * time.Second) | ||
|
||
if _, err = rc.Entries.CreateLogEntry(params); err != nil { | ||
t.Fatalf("expected no errors when submitting hashedrekord entry with sha256 to rekor %s", err) | ||
} | ||
} | ||
|
||
// TestSHA1HashedRekordEntry tests sending a proposed hashedrekord entry with | ||
// sha1 digests that should not be accepted by Rekor as SHA1 is considered | ||
// insecure. | ||
func TestSHA1HashedRekordEntry(t *testing.T) { | ||
privKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) | ||
if err != nil { | ||
t.Fatalf("error generating key: %v", err) | ||
} | ||
pubBytes, err := cryptoutils.MarshalPublicKeyToPEM(privKey.Public()) | ||
if err != nil { | ||
t.Fatalf("error marshaling public key: %v", err) | ||
} | ||
|
||
data := []byte("data") | ||
signer, err := signature.LoadSigner(privKey, crypto.SHA256) | ||
if err != nil { | ||
t.Fatalf("error loading verifier: %v", err) | ||
} | ||
signature, err := signer.SignMessage(bytes.NewReader(data)) | ||
if err != nil { | ||
t.Fatalf("error signing message: %v", err) | ||
} | ||
|
||
re := V001Entry{} | ||
|
||
// we will need artifact, public-key, signature | ||
re.HashedRekordObj.Data = &models.HashedrekordV001SchemaData{} | ||
|
||
re.HashedRekordObj.Signature = &models.HashedrekordV001SchemaSignature{} | ||
re.HashedRekordObj.Signature.Content = strfmt.Base64(signature) | ||
|
||
re.HashedRekordObj.Signature.PublicKey = &models.HashedrekordV001SchemaSignaturePublicKey{} | ||
publicKeyBytes := [][]byte{pubBytes} | ||
|
||
re.HashedRekordObj.Signature.PublicKey.Content = strfmt.Base64(publicKeyBytes[0]) | ||
re.HashedRekordObj.Data.Hash = &models.HashedrekordV001SchemaDataHash{ | ||
Algorithm: swag.String("sha1"), | ||
Value: swag.String("a17c9aaa61e80a1bf71d0d850af4e5baa9800bbd"), | ||
} | ||
|
||
hr := models.Hashedrekord{} | ||
hr.APIVersion = swag.String("0.0.1") | ||
hr.Spec = re.HashedRekordObj | ||
|
||
rc, err := client.GetRekorClient(rekorServer()) | ||
if err != nil { | ||
t.Errorf("error getting client: %v", err) | ||
} | ||
|
||
params := &entries.CreateLogEntryParams{} | ||
params.SetProposedEntry(&hr) | ||
params.SetContext(context.Background()) | ||
params.SetTimeout(5 * time.Second) | ||
|
||
if _, err = rc.Entries.CreateLogEntry(params); err == nil { | ||
t.Fatalf("expected a failure when trying to add a hashedrekord with sha1") | ||
} | ||
|
||
e, ok := err.(*entries.CreateLogEntryBadRequest) | ||
if !ok { | ||
t.Errorf("unexpected error returned from rekor: %v", err.Error()) | ||
} | ||
if !strings.Contains(e.Payload.Message, "validation failure") { | ||
t.Errorf("expected error message to include 'validation failure': %v", e.Payload.Message) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you switch this to use the same pattern as above where you call
CreateFromArtifactProperties
? That is the call path that is at risk (as the JSON Schema validation will fail the API request)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doing this now!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 0f1f8e3: I've moved this out of e2e since it no longer needs access to a runtime Rekor instance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason why I did not use
CreateFromArtifactProperties
is that the test would fail inCreateFromArtifactProperties
on the client side and and not on the rekor server side. Thus I wanted to test that the server did not accept it rather than just checking the client.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense, but I think @bobcallaway is pointing out that the server side is already checked by virtue of the JSON Schema Validation -- we could include both checks, though (with the non-e2e doing
CreateFromArtifactProperties
and the e2e doing it manually).