| 1 |
package internal |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto" |
| 6 |
"encoding/base64" |
| 7 |
"encoding/json" |
| 8 |
"errors" |
| 9 |
"os" |
| 10 |
"testing" |
| 11 |
"time" |
| 12 |
|
| 13 |
digest "github.com/opencontainers/go-digest" |
| 14 |
"github.com/sigstore/sigstore/pkg/cryptoutils" |
| 15 |
"github.com/stretchr/testify/assert" |
| 16 |
"github.com/stretchr/testify/require" |
| 17 |
"go.podman.io/image/v5/internal/signature" |
| 18 |
"go.podman.io/image/v5/version" |
| 19 |
) |
| 20 |
|
| 21 |
// A short-hand way to get a JSON object field value or panic. No error handling done, we know |
| 22 |
// what we are working with, a panic in a test is good enough, and fitting test cases on a single line |
| 23 |
// is a priority. |
| 24 |
func x(m mSA, fields ...string) mSA { |
| 25 |
for _, field := range fields { |
| 26 |
// Not .(mSA) because type assertion of an unnamed type to a named type always fails (the types |
| 27 |
// are not "identical"), but the assignment is fine because they are "assignable". |
| 28 |
m = m[field].(map[string]any) |
| 29 |
} |
| 30 |
return m |
| 31 |
} |
| 32 |
|
| 33 |
func TestNewUntrustedSigstorePayload(t *testing.T) { |
| 34 |
timeBefore := time.Now() |
| 35 |
sig := NewUntrustedSigstorePayload(TestImageManifestDigest, TestImageSignatureReference) |
| 36 |
assert.Equal(t, TestImageManifestDigest, sig.untrustedDockerManifestDigest) |
| 37 |
assert.Equal(t, TestImageSignatureReference, sig.untrustedDockerReference) |
| 38 |
require.NotNil(t, sig.untrustedCreatorID) |
| 39 |
assert.Equal(t, "containers/image "+version.Version, *sig.untrustedCreatorID) |
| 40 |
require.NotNil(t, sig.untrustedTimestamp) |
| 41 |
timeAfter := time.Now() |
| 42 |
assert.True(t, timeBefore.Unix() <= *sig.untrustedTimestamp) |
| 43 |
assert.True(t, *sig.untrustedTimestamp <= timeAfter.Unix()) |
| 44 |
} |
| 45 |
|
| 46 |
func TestUntrustedSigstorePayloadMarshalJSON(t *testing.T) { |
| 47 |
const testDigest = "sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
| 48 |
|
| 49 |
// Empty string values |
| 50 |
s := NewUntrustedSigstorePayload("", "_") |
| 51 |
_, err := s.MarshalJSON() |
| 52 |
assert.Error(t, err) |
| 53 |
s = NewUntrustedSigstorePayload("_", "") |
| 54 |
_, err = s.MarshalJSON() |
| 55 |
assert.Error(t, err) |
| 56 |
|
| 57 |
// Success |
| 58 |
// Use intermediate variables for these values so that we can take their addresses. |
| 59 |
creatorID := "CREATOR" |
| 60 |
timestamp := int64(1484683104) |
| 61 |
for _, c := range []struct { |
| 62 |
input UntrustedSigstorePayload |
| 63 |
expected string |
| 64 |
}{ |
| 65 |
{ |
| 66 |
UntrustedSigstorePayload{ |
| 67 |
untrustedDockerManifestDigest: testDigest, |
| 68 |
untrustedDockerReference: "reference#@!", |
| 69 |
untrustedCreatorID: &creatorID, |
| 70 |
untrustedTimestamp: ×tamp, |
| 71 |
}, |
| 72 |
"{\"critical\":{\"identity\":{\"docker-reference\":\"reference#@!\"},\"image\":{\"docker-manifest-digest\":\"" + testDigest + "\"},\"type\":\"cosign container image signature\"},\"optional\":{\"creator\":\"CREATOR\",\"timestamp\":1484683104}}", |
| 73 |
}, |
| 74 |
{ |
| 75 |
UntrustedSigstorePayload{ |
| 76 |
untrustedDockerManifestDigest: testDigest, |
| 77 |
untrustedDockerReference: "reference#@!", |
| 78 |
}, |
| 79 |
"{\"critical\":{\"identity\":{\"docker-reference\":\"reference#@!\"},\"image\":{\"docker-manifest-digest\":\"" + testDigest + "\"},\"type\":\"cosign container image signature\"},\"optional\":{}}", |
| 80 |
}, |
| 81 |
} { |
| 82 |
marshaled, err := c.input.MarshalJSON() |
| 83 |
require.NoError(t, err) |
| 84 |
assert.Equal(t, []byte(c.expected), marshaled) |
| 85 |
|
| 86 |
// Also call MarshalJSON through the JSON package. |
| 87 |
marshaled, err = json.Marshal(c.input) |
| 88 |
assert.NoError(t, err) |
| 89 |
assert.Equal(t, []byte(c.expected), marshaled) |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
// Verify that input can be unmarshaled as an UntrustedSigstorePayload. |
| 94 |
func successfullyUnmarshalUntrustedSigstorePayload(t *testing.T, input []byte) UntrustedSigstorePayload { |
| 95 |
var s UntrustedSigstorePayload |
| 96 |
err := json.Unmarshal(input, &s) |
| 97 |
require.NoError(t, err, string(input)) |
| 98 |
|
| 99 |
return s |
| 100 |
} |
| 101 |
|
| 102 |
// Verify that input can't be unmarshaled as an UntrustedSigstorePayload. |
| 103 |
func assertUnmarshalUntrustedSigstorePayloadFails(t *testing.T, input []byte) { |
| 104 |
var s UntrustedSigstorePayload |
| 105 |
err := json.Unmarshal(input, &s) |
| 106 |
assert.Error(t, err, string(input)) |
| 107 |
} |
| 108 |
|
| 109 |
func TestUntrustedSigstorePayloadUnmarshalJSON(t *testing.T) { |
| 110 |
const testDigest = "sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
| 111 |
|
| 112 |
// Invalid input. Note that json.Unmarshal is guaranteed to validate input before calling our |
| 113 |
// UnmarshalJSON implementation; so test that first, then test our error handling for completeness. |
| 114 |
assertUnmarshalUntrustedSigstorePayloadFails(t, []byte("&")) |
| 115 |
var s UntrustedSigstorePayload |
| 116 |
err := s.UnmarshalJSON([]byte("&")) |
| 117 |
assert.Error(t, err) |
| 118 |
|
| 119 |
// Not an object |
| 120 |
assertUnmarshalUntrustedSigstorePayloadFails(t, []byte("1")) |
| 121 |
|
| 122 |
// Start with a valid JSON. |
| 123 |
validSig := NewUntrustedSigstorePayload(testDigest, "reference#@!") |
| 124 |
validJSON, err := validSig.MarshalJSON() |
| 125 |
require.NoError(t, err) |
| 126 |
|
| 127 |
// Success |
| 128 |
s = successfullyUnmarshalUntrustedSigstorePayload(t, validJSON) |
| 129 |
assert.Equal(t, validSig, s) |
| 130 |
|
| 131 |
// A /usr/bin/cosign-generated payload is handled correctly |
| 132 |
s = successfullyUnmarshalUntrustedSigstorePayload(t, []byte(`{"critical":{"identity":{"docker-reference":"192.168.64.2:5000/cosign-signed-multi"},"image":{"docker-manifest-digest":"sha256:43955d6857268cc948ae9b370b221091057de83c4962da0826f9a2bdc9bd6b44"},"type":"cosign container image signature"},"optional":null}`)) |
| 133 |
assert.Equal(t, UntrustedSigstorePayload{ |
| 134 |
untrustedDockerManifestDigest: "sha256:43955d6857268cc948ae9b370b221091057de83c4962da0826f9a2bdc9bd6b44", |
| 135 |
untrustedDockerReference: "192.168.64.2:5000/cosign-signed-multi", |
| 136 |
untrustedCreatorID: nil, |
| 137 |
untrustedTimestamp: nil, |
| 138 |
}, s) |
| 139 |
|
| 140 |
// Various ways to corrupt the JSON |
| 141 |
breakFns := []func(mSA){ |
| 142 |
// A top-level field is missing |
| 143 |
func(v mSA) { delete(v, "critical") }, |
| 144 |
func(v mSA) { delete(v, "optional") }, |
| 145 |
// Extra top-level sub-object |
| 146 |
func(v mSA) { v["unexpected"] = 1 }, |
| 147 |
// "critical" not an object |
| 148 |
func(v mSA) { v["critical"] = 1 }, |
| 149 |
// "optional" not an object |
| 150 |
func(v mSA) { v["optional"] = 1 }, |
| 151 |
// A field of "critical" is missing |
| 152 |
func(v mSA) { delete(x(v, "critical"), "type") }, |
| 153 |
func(v mSA) { delete(x(v, "critical"), "image") }, |
| 154 |
func(v mSA) { delete(x(v, "critical"), "identity") }, |
| 155 |
// Extra field of "critical" |
| 156 |
func(v mSA) { x(v, "critical")["unexpected"] = 1 }, |
| 157 |
// Invalid "type" |
| 158 |
func(v mSA) { x(v, "critical")["type"] = 1 }, |
| 159 |
func(v mSA) { x(v, "critical")["type"] = "unexpected" }, |
| 160 |
// Invalid "image" object |
| 161 |
func(v mSA) { x(v, "critical")["image"] = 1 }, |
| 162 |
func(v mSA) { delete(x(v, "critical", "image"), "docker-manifest-digest") }, |
| 163 |
func(v mSA) { x(v, "critical", "image")["unexpected"] = 1 }, |
| 164 |
// Invalid "docker-manifest-digest" |
| 165 |
func(v mSA) { x(v, "critical", "image")["docker-manifest-digest"] = 1 }, |
| 166 |
func(v mSA) { x(v, "critical", "image")["docker-manifest-digest"] = "sha256:../.." }, |
| 167 |
// Invalid "identity" object |
| 168 |
func(v mSA) { x(v, "critical")["identity"] = 1 }, |
| 169 |
func(v mSA) { delete(x(v, "critical", "identity"), "docker-reference") }, |
| 170 |
func(v mSA) { x(v, "critical", "identity")["unexpected"] = 1 }, |
| 171 |
// Invalid "docker-reference" |
| 172 |
func(v mSA) { x(v, "critical", "identity")["docker-reference"] = 1 }, |
| 173 |
// Invalid "creator" |
| 174 |
func(v mSA) { x(v, "optional")["creator"] = 1 }, |
| 175 |
// Invalid "timestamp" |
| 176 |
func(v mSA) { x(v, "optional")["timestamp"] = "unexpected" }, |
| 177 |
func(v mSA) { x(v, "optional")["timestamp"] = 0.5 }, // Fractional input |
| 178 |
} |
| 179 |
for _, fn := range breakFns { |
| 180 |
testJSON := modifiedJSON(t, validJSON, fn) |
| 181 |
assertUnmarshalUntrustedSigstorePayloadFails(t, testJSON) |
| 182 |
} |
| 183 |
|
| 184 |
// Modifications to unrecognized fields in "optional" are allowed and ignored |
| 185 |
allowedModificationFns := []func(mSA){ |
| 186 |
// Add an optional field |
| 187 |
func(v mSA) { x(v, "optional")["unexpected"] = 1 }, |
| 188 |
} |
| 189 |
for _, fn := range allowedModificationFns { |
| 190 |
testJSON := modifiedJSON(t, validJSON, fn) |
| 191 |
s := successfullyUnmarshalUntrustedSigstorePayload(t, testJSON) |
| 192 |
assert.Equal(t, validSig, s) |
| 193 |
} |
| 194 |
|
| 195 |
// Optional fields can be missing |
| 196 |
validSig = UntrustedSigstorePayload{ |
| 197 |
untrustedDockerManifestDigest: testDigest, |
| 198 |
untrustedDockerReference: "reference#@!", |
| 199 |
untrustedCreatorID: nil, |
| 200 |
untrustedTimestamp: nil, |
| 201 |
} |
| 202 |
validJSON, err = validSig.MarshalJSON() |
| 203 |
require.NoError(t, err) |
| 204 |
s = successfullyUnmarshalUntrustedSigstorePayload(t, validJSON) |
| 205 |
assert.Equal(t, validSig, s) |
| 206 |
} |
| 207 |
|
| 208 |
// verifySigstorePayloadBlobSignature is tested by TestVerifySigstorePayload |
| 209 |
|
| 210 |
func TestVerifySigstorePayload(t *testing.T) { |
| 211 |
publicKeyPEM, err := os.ReadFile("./testdata/cosign.pub") |
| 212 |
require.NoError(t, err) |
| 213 |
publicKey, err := cryptoutils.UnmarshalPEMToPublicKey(publicKeyPEM) |
| 214 |
require.NoError(t, err) |
| 215 |
publicKeyPEM2, err := os.ReadFile("./testdata/cosign2.pub") |
| 216 |
require.NoError(t, err) |
| 217 |
publicKey2, err := cryptoutils.UnmarshalPEMToPublicKey(publicKeyPEM2) |
| 218 |
require.NoError(t, err) |
| 219 |
singlePublicKey := []crypto.PublicKey{publicKey} |
| 220 |
|
| 221 |
type acceptanceData struct { |
| 222 |
signedDockerReference string |
| 223 |
signedDockerManifestDigest digest.Digest |
| 224 |
} |
| 225 |
var wanted, recorded acceptanceData |
| 226 |
// recordingRules are a plausible SigstorePayloadAcceptanceRules implementations, but equally |
| 227 |
// importantly record that we are passing the correct values to the rule callbacks. |
| 228 |
recordingRules := SigstorePayloadAcceptanceRules{ |
| 229 |
ValidateSignedDockerReference: func(signedDockerReference string) error { |
| 230 |
recorded.signedDockerReference = signedDockerReference |
| 231 |
if signedDockerReference != wanted.signedDockerReference { |
| 232 |
return errors.New("signedDockerReference mismatch") |
| 233 |
} |
| 234 |
return nil |
| 235 |
}, |
| 236 |
ValidateSignedDockerManifestDigest: func(signedDockerManifestDigest digest.Digest) error { |
| 237 |
recorded.signedDockerManifestDigest = signedDockerManifestDigest |
| 238 |
if signedDockerManifestDigest != wanted.signedDockerManifestDigest { |
| 239 |
return errors.New("signedDockerManifestDigest mismatch") |
| 240 |
} |
| 241 |
return nil |
| 242 |
}, |
| 243 |
} |
| 244 |
|
| 245 |
sigBlob, err := os.ReadFile("./testdata/valid.signature") |
| 246 |
require.NoError(t, err) |
| 247 |
genericSig, err := signature.FromBlob(sigBlob) |
| 248 |
require.NoError(t, err) |
| 249 |
sigstoreSig, ok := genericSig.(signature.Sigstore) |
| 250 |
require.True(t, ok) |
| 251 |
cryptoBase64Sig, ok := sigstoreSig.UntrustedAnnotations()[signature.SigstoreSignatureAnnotationKey] |
| 252 |
require.True(t, ok) |
| 253 |
signatureData := acceptanceData{ |
| 254 |
signedDockerReference: TestSigstoreSignatureReference, |
| 255 |
signedDockerManifestDigest: TestSigstoreManifestDigest, |
| 256 |
} |
| 257 |
|
| 258 |
// Successful verification |
| 259 |
for _, publicKeys := range [][]crypto.PublicKey{ |
| 260 |
singlePublicKey, |
| 261 |
{publicKey, publicKey2}, // The matching key is first |
| 262 |
{publicKey2, publicKey}, // The matching key is second |
| 263 |
} { |
| 264 |
wanted = signatureData |
| 265 |
recorded = acceptanceData{} |
| 266 |
res, err := VerifySigstorePayload(publicKeys, sigstoreSig.UntrustedPayload(), cryptoBase64Sig, recordingRules) |
| 267 |
require.NoError(t, err) |
| 268 |
assert.Equal(t, res, &UntrustedSigstorePayload{ |
| 269 |
untrustedDockerManifestDigest: TestSigstoreManifestDigest, |
| 270 |
untrustedDockerReference: TestSigstoreSignatureReference, |
| 271 |
untrustedCreatorID: nil, |
| 272 |
untrustedTimestamp: nil, |
| 273 |
}) |
| 274 |
assert.Equal(t, signatureData, recorded) |
| 275 |
} |
| 276 |
|
| 277 |
// For extra paranoia, test that we return a nil signature object on error. |
| 278 |
|
| 279 |
// Invalid base64 encoding |
| 280 |
for _, invalidBase64Sig := range []string{ |
| 281 |
"&", // Invalid base64 characters |
| 282 |
cryptoBase64Sig + "=", // Extra padding |
| 283 |
cryptoBase64Sig[:len(cryptoBase64Sig)-1], // Truncated base64 data |
| 284 |
} { |
| 285 |
recorded = acceptanceData{} |
| 286 |
res, err := VerifySigstorePayload(singlePublicKey, sigstoreSig.UntrustedPayload(), invalidBase64Sig, recordingRules) |
| 287 |
assert.Error(t, err) |
| 288 |
assert.Nil(t, res) |
| 289 |
assert.Equal(t, acceptanceData{}, recorded) |
| 290 |
} |
| 291 |
|
| 292 |
// No public keys |
| 293 |
recorded = acceptanceData{} |
| 294 |
res, err := VerifySigstorePayload([]crypto.PublicKey{}, sigstoreSig.UntrustedPayload(), cryptoBase64Sig, recordingRules) |
| 295 |
assert.Error(t, err) |
| 296 |
assert.Nil(t, res) |
| 297 |
assert.Equal(t, acceptanceData{}, recorded) |
| 298 |
|
| 299 |
// Invalid verifier: |
| 300 |
// crypto.PublicKey is, for some reason, just an any, so using a struct{}{} to trigger this code path works. |
| 301 |
for _, invalidPublicKeys := range [][]crypto.PublicKey{ |
| 302 |
{struct{}{}}, // A single invalid key |
| 303 |
{struct{}{}, publicKey}, // An invalid key, followed by a matching key |
| 304 |
{publicKey, struct{}{}}, // A matching key, but the configuration also includes an invalid key |
| 305 |
} { |
| 306 |
recorded = acceptanceData{} |
| 307 |
res, err = VerifySigstorePayload(invalidPublicKeys, sigstoreSig.UntrustedPayload(), cryptoBase64Sig, recordingRules) |
| 308 |
assert.Error(t, err) |
| 309 |
assert.Nil(t, res) |
| 310 |
assert.Equal(t, acceptanceData{}, recorded) |
| 311 |
} |
| 312 |
|
| 313 |
// Invalid signature |
| 314 |
validSignatureBytes, err := base64.StdEncoding.DecodeString(cryptoBase64Sig) |
| 315 |
require.NoError(t, err) |
| 316 |
for _, invalidSig := range [][]byte{ |
| 317 |
{}, // Empty signature |
| 318 |
[]byte("invalid signature"), |
| 319 |
bytes.Repeat(validSignatureBytes, 2), |
| 320 |
} { |
| 321 |
recorded = acceptanceData{} |
| 322 |
res, err = VerifySigstorePayload(singlePublicKey, sigstoreSig.UntrustedPayload(), base64.StdEncoding.EncodeToString(invalidSig), recordingRules) |
| 323 |
assert.Error(t, err) |
| 324 |
assert.Nil(t, res) |
| 325 |
assert.Equal(t, acceptanceData{}, recorded) |
| 326 |
} |
| 327 |
|
| 328 |
// No matching public keys |
| 329 |
for _, nonmatchingPublicKeys := range [][]crypto.PublicKey{ |
| 330 |
{publicKey2}, |
| 331 |
{publicKey2, publicKey2}, |
| 332 |
} { |
| 333 |
recorded = acceptanceData{} |
| 334 |
res, err = VerifySigstorePayload(nonmatchingPublicKeys, sigstoreSig.UntrustedPayload(), cryptoBase64Sig, recordingRules) |
| 335 |
assert.Error(t, err) |
| 336 |
assert.Nil(t, res) |
| 337 |
assert.Equal(t, acceptanceData{}, recorded) |
| 338 |
} |
| 339 |
|
| 340 |
// Valid signature of non-JSON |
| 341 |
recorded = acceptanceData{} |
| 342 |
res, err = VerifySigstorePayload(singlePublicKey, []byte("&"), "MEUCIARnnxZQPALBfqkB4aNAYXad79Qs6VehcrgIeZ8p7I2FAiEAzq2HXwXlz1iJeh+ucUR3L0zpjynQk6Rk0+/gXYp49RU=", recordingRules) |
| 343 |
assert.Error(t, err) |
| 344 |
assert.Nil(t, res) |
| 345 |
assert.Equal(t, acceptanceData{}, recorded) |
| 346 |
|
| 347 |
// Valid signature of an unacceptable JSON |
| 348 |
recorded = acceptanceData{} |
| 349 |
res, err = VerifySigstorePayload(singlePublicKey, []byte("{}"), "MEUCIQDkySOBGxastVP0+koTA33NH5hXjwosFau4rxTPN6g48QIgb7eWKkGqfEpHMM3aT4xiqyP/170jEkdFuciuwN4mux4=", recordingRules) |
| 350 |
assert.Error(t, err) |
| 351 |
assert.Nil(t, res) |
| 352 |
assert.Equal(t, acceptanceData{}, recorded) |
| 353 |
|
| 354 |
// Valid signature with a wrong manifest digest: asked for signedDockerManifestDigest |
| 355 |
wanted = signatureData |
| 356 |
wanted.signedDockerManifestDigest = "invalid digest" |
| 357 |
recorded = acceptanceData{} |
| 358 |
res, err = VerifySigstorePayload(singlePublicKey, sigstoreSig.UntrustedPayload(), cryptoBase64Sig, recordingRules) |
| 359 |
assert.Error(t, err) |
| 360 |
assert.Nil(t, res) |
| 361 |
assert.Equal(t, acceptanceData{ |
| 362 |
signedDockerManifestDigest: signatureData.signedDockerManifestDigest, |
| 363 |
}, recorded) |
| 364 |
|
| 365 |
// Valid signature with a wrong image reference |
| 366 |
wanted = signatureData |
| 367 |
wanted.signedDockerReference = "unexpected docker reference" |
| 368 |
recorded = acceptanceData{} |
| 369 |
res, err = VerifySigstorePayload(singlePublicKey, sigstoreSig.UntrustedPayload(), cryptoBase64Sig, recordingRules) |
| 370 |
assert.Error(t, err) |
| 371 |
assert.Nil(t, res) |
| 372 |
assert.Equal(t, signatureData, recorded) |
| 373 |
} |