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

Fix hashed message verification #118

Merged
merged 4 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions cmd/timestamp-cli/app/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"crypto/x509"
"errors"
"fmt"
"hash"
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -125,27 +126,22 @@ func verifyTSRWithPEM(ts *timestamp.Timestamp) error {

func verifyArtifactWithTSR(ts *timestamp.Timestamp) error {
artifactPath := viper.GetString("artifact")
artifactBytes, err := os.ReadFile(filepath.Clean(artifactPath))
artifact, err := os.Open(filepath.Clean(artifactPath))
if err != nil {
return err
}

h := ts.HashAlgorithm.New()
b := make([]byte, h.Size())

r := bytes.NewReader(artifactBytes)
n, err := r.Read(b)
if err == io.EOF {
return err
}
return verifyHashedMessages(ts.HashAlgorithm.New(), ts.HashedMessage, artifact)
}

_, err = h.Write(b[:n])
if err != nil {
return fmt.Errorf("Failed to create local message hash")
func verifyHashedMessages(hashAlg hash.Hash, hashedMessage []byte, artifactReader io.Reader) error {
h := hashAlg
if _, err := io.Copy(h, artifactReader); err != nil {
return fmt.Errorf("failed to create hash %w", err)
}
localHashedMsg := h.Sum(nil)

localHashedMessage := h.Sum(nil)
if !bytes.Equal(localHashedMessage, ts.HashedMessage) {
if !bytes.Equal(localHashedMsg, hashedMessage) {
return fmt.Errorf("Hashed messages don't match")
}

Expand Down
96 changes: 96 additions & 0 deletions cmd/timestamp-cli/app/verify_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//
// Copyright 2022 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.

package app

import (
"bytes"
"crypto"
"io"
"net/http/httptest"
"strings"
"testing"
"time"

"github.com/digitorus/timestamp"
"github.com/sigstore/timestamp-authority/pkg/client"
tsatimestamp "github.com/sigstore/timestamp-authority/pkg/generated/client/timestamp"
"github.com/sigstore/timestamp-authority/pkg/server"
"github.com/spf13/viper"
)

func TestVerifyArtifactHashedMessages(t *testing.T) {
viper.Set("timestamp-signer", "memory")
apiServer := server.NewRestAPIServer("localhost", 0, []string{"http"}, 10*time.Second, 10*time.Second)
server := httptest.NewServer(apiServer.GetHandler())
t.Cleanup(server.Close)

c, err := client.GetTimestampClient(server.URL)
if err != nil {
t.Fatalf("unexpected error creating client: %v", err)
}

type test struct {
message string
}

tests := []test{
{
message: "valid local artifact",
},
{
message: "nonexistant local artifact",
},
{
message: "valid local artifact with hash algorithm",
},
{
message: "valid oid",
},
{
message: "MIIEbjADAgEAMIIEZQYJKoZIhvcNAQcCoIIEVjCCBFICAQExDTALBglghkgBZQMEAgEwgdQGCyqGSIb3DQEJEAEEoIHEBIHBMIG+AgEBBgkrBgEEAYO/MAIwMTANBglghkgBZQMEAgEFAAQgN94hMnpq0onyUi7r1zJHNiLT1/spX8MU2GBN9AdMe6wCFQDS6RL1iVlmlkwJzmpS2EH0cuX8sxgTMjAyMjExMDMxNzQyNDIrMDEwMDADAgEBAhRKnQszZjzcgJkpE8LCbmbF0s1jPaA0pDIwMDEOMAwGA1UEChMFbG9jYWwxHjAcBgNVBAMTFVRlc3QgVFNBIFRpbWVzdGFtcGluZ6CCAckwggHFMIIBaqADAgECAhRHCu9dHKS97mFo1cH5neJubRibujAKBggqhkjO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826Du5Ss4pNG/TksDknTUJfKvrLc2ex+x+W3VHg==",
},
}

for _, tc := range tests {
tsq, err := timestamp.CreateRequest(strings.NewReader(tc.message), &timestamp.RequestOptions{
Hash: crypto.SHA256,
Certificates: true,
})
if err != nil {
t.Fatalf("unexpected error creating request: %v", err)
}

params := tsatimestamp.NewGetTimestampResponseParams()
params.SetTimeout(10 * time.Second)
params.Request = io.NopCloser(bytes.NewReader(tsq))

var respBytes bytes.Buffer
_, err = c.Timestamp.GetTimestampResponse(params, &respBytes)
if err != nil {
t.Fatalf("unexpected error getting timestamp response: %v", err)
}

tsr, err := timestamp.ParseResponse(respBytes.Bytes())
if err != nil {
t.Fatalf("unexpected error parsing response: %v", err)
}

if err := verifyHashedMessages(tsr.HashAlgorithm.New(), tsr.HashedMessage, strings.NewReader(tc.message)); err != nil {
t.Errorf("verifyHashedMessages failed comparing hashes: %v", err)
}

}
}
4 changes: 2 additions & 2 deletions pkg/tests/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestGetTimestampResponse(t *testing.T) {

// create request with nonce and certificate, the typical request structure
tsNonce := big.NewInt(1234)
tsq, err := ts.CreateRequest(strings.NewReader("blob"), &ts.RequestOptions{
tsq, err := ts.CreateRequest(strings.NewReader("blobblobblobblobblobblobblobblobblob"), &ts.RequestOptions{
Hash: crypto.SHA256,
Certificates: true,
Nonce: tsNonce,
Expand Down Expand Up @@ -120,7 +120,7 @@ func TestGetTimestampResponse(t *testing.T) {
if tsr.HashAlgorithm != crypto.SHA256 {
t.Fatalf("unexpected hash algorithm")
}
hashedMessage := sha256.Sum256([]byte("blob"))
hashedMessage := sha256.Sum256([]byte("blobblobblobblobblobblobblobblobblob"))
if !bytes.Equal(tsr.HashedMessage, hashedMessage[:]) {
t.Fatalf("expected hashed messages to be equal: %v %v", tsr.HashedMessage, hashedMessage)
}
Expand Down