| 1 |
// |
| 2 |
// Copyright 2022 The Sigstore Authors. |
| 3 |
// |
| 4 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
// you may not use this file except in compliance with the License. |
| 6 |
// You may obtain a copy of the License at |
| 7 |
// |
| 8 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
// |
| 10 |
// Unless required by applicable law or agreed to in writing, software |
| 11 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
// See the License for the specific language governing permissions and |
| 14 |
// limitations under the License. |
| 15 |
|
| 16 |
package dsse |
| 17 |
|
| 18 |
import ( |
| 19 |
"bytes" |
| 20 |
"crypto" |
| 21 |
"crypto/ecdsa" |
| 22 |
"crypto/elliptic" |
| 23 |
"crypto/rand" |
| 24 |
"encoding/base64" |
| 25 |
"encoding/json" |
| 26 |
"strings" |
| 27 |
"testing" |
| 28 |
"unicode/utf8" |
| 29 |
|
| 30 |
"github.com/secure-systems-lab/go-securesystemslib/dsse" |
| 31 |
"github.com/sigstore/sigstore/pkg/signature" |
| 32 |
ds "github.com/sigstore/sigstore/pkg/signature/dsse" |
| 33 |
) |
| 34 |
|
| 35 |
func FuzzDSSE(f *testing.F) { |
| 36 |
f.Fuzz(func(t *testing.T, data, payload string) { |
| 37 |
if !utf8.Valid([]byte(data)) { |
| 38 |
t.Skip("invalid utf8") |
| 39 |
} |
| 40 |
if !utf8.Valid([]byte(payload)) { |
| 41 |
t.Skip("invalid utf8") |
| 42 |
} |
| 43 |
p, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 44 |
if err != nil { |
| 45 |
t.Errorf("failed to generate key: %v", err) |
| 46 |
} |
| 47 |
|
| 48 |
sv, err := signature.LoadECDSASignerVerifier(p, crypto.SHA256) |
| 49 |
if err != nil { |
| 50 |
t.Errorf("failed to load signer verifier: %v", err) |
| 51 |
} |
| 52 |
|
| 53 |
wsv := ds.WrapSignerVerifier(sv, payload) |
| 54 |
|
| 55 |
sig, err := wsv.SignMessage(strings.NewReader(data)) |
| 56 |
if err != nil { |
| 57 |
t.Errorf("failed to sign message: %v", err) |
| 58 |
} |
| 59 |
|
| 60 |
if err := wsv.VerifySignature(bytes.NewReader(sig), nil); err != nil { |
| 61 |
t.Errorf("failed to verify signature: %v", err) |
| 62 |
} |
| 63 |
|
| 64 |
env := dsse.Envelope{} |
| 65 |
if err := json.Unmarshal(sig, &env); err != nil { |
| 66 |
panic(err) |
| 67 |
} |
| 68 |
if env.PayloadType != payload { |
| 69 |
t.Errorf("Expected payloadType %s, got %s", payload, env.PayloadType) |
| 70 |
} |
| 71 |
|
| 72 |
got, err := base64.StdEncoding.DecodeString(env.Payload) |
| 73 |
if err != nil { |
| 74 |
t.Errorf("failed to decode payload: %v", err) |
| 75 |
} |
| 76 |
|
| 77 |
if string(got) != data { |
| 78 |
t.Errorf("Expected payload %s, got %s", data, env.Payload) |
| 79 |
} |
| 80 |
}) |
| 81 |
} |