| 1 |
// Copyright The Notary Project Authors. |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package base |
| 15 |
|
| 16 |
import ( |
| 17 |
"crypto/x509" |
| 18 |
"errors" |
| 19 |
"reflect" |
| 20 |
"testing" |
| 21 |
"time" |
| 22 |
|
| 23 |
"github.com/notaryproject/notation-core-go/signature" |
| 24 |
"github.com/notaryproject/notation-core-go/testhelper" |
| 25 |
"github.com/notaryproject/tspclient-go" |
| 26 |
) |
| 27 |
|
| 28 |
var ( |
| 29 |
errMsg = "error msg" |
| 30 |
invalidTimestamper tspclient.Timestamper |
| 31 |
invalidSigningAgent = "test/1" |
| 32 |
validSigningAgent = "test/0" |
| 33 |
invalidContentType = "text/plain" |
| 34 |
validContentType = "application/vnd.cncf.notary.payload.v1+json" |
| 35 |
validContent = "test content" |
| 36 |
validBytes = []byte(validContent) |
| 37 |
time08_02 time.Time |
| 38 |
time08_03 time.Time |
| 39 |
timeLayout = "2006-01-02" |
| 40 |
signiningSchema = signature.SigningScheme("notary.x509") |
| 41 |
validSignerInfo = &signature.SignerInfo{ |
| 42 |
Signature: validBytes, |
| 43 |
SignatureAlgorithm: signature.AlgorithmPS384, |
| 44 |
SignedAttributes: signature.SignedAttributes{ |
| 45 |
SigningTime: testhelper.GetRSALeafCertificate().Cert.NotBefore, |
| 46 |
Expiry: testhelper.GetECLeafCertificate().Cert.NotAfter, |
| 47 |
SigningScheme: signiningSchema, |
| 48 |
}, |
| 49 |
CertificateChain: []*x509.Certificate{ |
| 50 |
testhelper.GetRSALeafCertificate().Cert, |
| 51 |
testhelper.GetRSARootCertificate().Cert, |
| 52 |
}, |
| 53 |
} |
| 54 |
validPayload = &signature.Payload{ |
| 55 |
ContentType: validContentType, |
| 56 |
Content: validBytes, |
| 57 |
} |
| 58 |
validEnvelopeContent = &signature.EnvelopeContent{ |
| 59 |
SignerInfo: *validSignerInfo, |
| 60 |
Payload: *validPayload, |
| 61 |
} |
| 62 |
validReq = &signature.SignRequest{ |
| 63 |
Payload: signature.Payload{ |
| 64 |
ContentType: validContentType, |
| 65 |
Content: validBytes, |
| 66 |
}, |
| 67 |
SigningTime: testhelper.GetRSALeafCertificate().Cert.NotBefore, |
| 68 |
Expiry: testhelper.GetRSALeafCertificate().Cert.NotAfter, |
| 69 |
SigningScheme: signiningSchema, |
| 70 |
Signer: &mockSigner{ |
| 71 |
keySpec: signature.KeySpec{ |
| 72 |
Type: signature.KeyTypeRSA, |
| 73 |
Size: 3072, |
| 74 |
}, |
| 75 |
certs: []*x509.Certificate{ |
| 76 |
testhelper.GetRSALeafCertificate().Cert, |
| 77 |
testhelper.GetRSARootCertificate().Cert, |
| 78 |
}, |
| 79 |
}, |
| 80 |
SigningAgent: validSigningAgent, |
| 81 |
} |
| 82 |
signReq1 = &signature.SignRequest{ |
| 83 |
Payload: signature.Payload{ |
| 84 |
ContentType: validContentType, |
| 85 |
Content: validBytes, |
| 86 |
}, |
| 87 |
SigningTime: testhelper.GetRSALeafCertificate().Cert.NotBefore, |
| 88 |
Expiry: testhelper.GetRSALeafCertificate().Cert.NotAfter, |
| 89 |
SigningScheme: signiningSchema, |
| 90 |
Signer: &mockSigner{ |
| 91 |
keySpec: signature.KeySpec{ |
| 92 |
Type: signature.KeyTypeRSA, |
| 93 |
Size: 3072, |
| 94 |
}, |
| 95 |
certs: []*x509.Certificate{ |
| 96 |
testhelper.GetRSALeafCertificate().Cert, |
| 97 |
testhelper.GetRSARootCertificate().Cert, |
| 98 |
}, |
| 99 |
}, |
| 100 |
SigningAgent: invalidSigningAgent, |
| 101 |
} |
| 102 |
reqWithInvalidTSAurl = &signature.SignRequest{ |
| 103 |
Payload: signature.Payload{ |
| 104 |
ContentType: validContentType, |
| 105 |
Content: validBytes, |
| 106 |
}, |
| 107 |
SigningTime: testhelper.GetRSALeafCertificate().Cert.NotBefore, |
| 108 |
Expiry: testhelper.GetRSALeafCertificate().Cert.NotAfter, |
| 109 |
SigningScheme: signiningSchema, |
| 110 |
Signer: &mockSigner{ |
| 111 |
keySpec: signature.KeySpec{ |
| 112 |
Type: signature.KeyTypeRSA, |
| 113 |
Size: 3072, |
| 114 |
}, |
| 115 |
certs: []*x509.Certificate{ |
| 116 |
testhelper.GetRSALeafCertificate().Cert, |
| 117 |
testhelper.GetRSARootCertificate().Cert, |
| 118 |
}, |
| 119 |
}, |
| 120 |
SigningAgent: validSigningAgent, |
| 121 |
Timestamper: invalidTimestamper, |
| 122 |
} |
| 123 |
) |
| 124 |
|
| 125 |
func init() { |
| 126 |
time08_02, _ = time.Parse(timeLayout, "2020-08-02") |
| 127 |
time08_03, _ = time.Parse(timeLayout, "2020-08-03") |
| 128 |
invalidTimestamper, _ = tspclient.NewHTTPTimestamper(nil, "invalid") |
| 129 |
} |
| 130 |
|
| 131 |
// Mock an internal envelope that implements signature.Envelope. |
| 132 |
type mockEnvelope struct { |
| 133 |
payload *signature.Payload |
| 134 |
signerInfo *signature.SignerInfo |
| 135 |
content *signature.EnvelopeContent |
| 136 |
failTimestamp bool |
| 137 |
failVerify bool |
| 138 |
} |
| 139 |
|
| 140 |
// Sign implements Sign of signature.Envelope. |
| 141 |
func (e mockEnvelope) Sign(req *signature.SignRequest) ([]byte, error) { |
| 142 |
switch req.SigningAgent { |
| 143 |
case invalidSigningAgent: |
| 144 |
return nil, errors.New(errMsg) |
| 145 |
case validSigningAgent: |
| 146 |
if e.failTimestamp { |
| 147 |
return validBytes, &signature.TimestampError{} |
| 148 |
} |
| 149 |
return validBytes, nil |
| 150 |
} |
| 151 |
return nil, nil |
| 152 |
} |
| 153 |
|
| 154 |
// Verify implements Verify of signature.Envelope. |
| 155 |
func (e mockEnvelope) Verify() (*signature.EnvelopeContent, error) { |
| 156 |
if e.failVerify { |
| 157 |
return nil, errors.New(errMsg) |
| 158 |
} |
| 159 |
return e.content, nil |
| 160 |
} |
| 161 |
|
| 162 |
// SignerInfo implements SignerInfo of signature.Envelope. |
| 163 |
func (e mockEnvelope) Content() (*signature.EnvelopeContent, error) { |
| 164 |
if e.content == nil { |
| 165 |
return nil, errors.New(errMsg) |
| 166 |
} |
| 167 |
return e.content, nil |
| 168 |
} |
| 169 |
|
| 170 |
// Mock a signer implements signature.Signer. |
| 171 |
type mockSigner struct { |
| 172 |
certs []*x509.Certificate |
| 173 |
keySpec signature.KeySpec |
| 174 |
} |
| 175 |
|
| 176 |
// CertificateChain implements CertificateChain of signature.Signer. |
| 177 |
func (s *mockSigner) CertificateChain() ([]*x509.Certificate, error) { |
| 178 |
if len(s.certs) == 0 { |
| 179 |
return nil, errors.New(errMsg) |
| 180 |
} |
| 181 |
return s.certs, nil |
| 182 |
} |
| 183 |
|
| 184 |
// Sign implements Sign of signature.Signer. |
| 185 |
func (s *mockSigner) Sign(payload []byte) ([]byte, []*x509.Certificate, error) { |
| 186 |
return nil, nil, nil |
| 187 |
} |
| 188 |
|
| 189 |
// KeySpec implements KeySpec of signature.Signer. |
| 190 |
func (s *mockSigner) KeySpec() (signature.KeySpec, error) { |
| 191 |
var emptyKeySpec signature.KeySpec |
| 192 |
if s.keySpec == emptyKeySpec { |
| 193 |
return s.keySpec, errors.New(errMsg) |
| 194 |
} |
| 195 |
return s.keySpec, nil |
| 196 |
} |
| 197 |
|
| 198 |
func TestSign(t *testing.T) { |
| 199 |
tests := []struct { |
| 200 |
name string |
| 201 |
req *signature.SignRequest |
| 202 |
env *Envelope |
| 203 |
expect []byte |
| 204 |
expectErr bool |
| 205 |
}{ |
| 206 |
{ |
| 207 |
name: "invalid request", |
| 208 |
req: &signature.SignRequest{ |
| 209 |
SigningTime: time08_02, |
| 210 |
Expiry: time08_02, |
| 211 |
}, |
| 212 |
env: &Envelope{ |
| 213 |
Raw: nil, |
| 214 |
Envelope: mockEnvelope{}, |
| 215 |
}, |
| 216 |
expect: nil, |
| 217 |
expectErr: true, |
| 218 |
}, |
| 219 |
{ |
| 220 |
name: "internal envelope fails to sign", |
| 221 |
req: signReq1, |
| 222 |
env: &Envelope{ |
| 223 |
Raw: nil, |
| 224 |
Envelope: mockEnvelope{}, |
| 225 |
}, |
| 226 |
expect: nil, |
| 227 |
expectErr: true, |
| 228 |
}, |
| 229 |
{ |
| 230 |
name: "internal envelope fails to get content", |
| 231 |
req: validReq, |
| 232 |
env: &Envelope{ |
| 233 |
Raw: nil, |
| 234 |
Envelope: mockEnvelope{}, |
| 235 |
}, |
| 236 |
expect: nil, |
| 237 |
expectErr: true, |
| 238 |
}, |
| 239 |
{ |
| 240 |
name: "invalid certificate chain", |
| 241 |
req: validReq, |
| 242 |
env: &Envelope{ |
| 243 |
Raw: nil, |
| 244 |
Envelope: mockEnvelope{ |
| 245 |
content: &signature.EnvelopeContent{}, |
| 246 |
}, |
| 247 |
}, |
| 248 |
expect: nil, |
| 249 |
expectErr: true, |
| 250 |
}, |
| 251 |
{ |
| 252 |
name: "successfully signed", |
| 253 |
req: validReq, |
| 254 |
env: &Envelope{ |
| 255 |
Raw: validBytes, |
| 256 |
Envelope: &mockEnvelope{ |
| 257 |
content: &signature.EnvelopeContent{ |
| 258 |
SignerInfo: *validSignerInfo, |
| 259 |
}, |
| 260 |
}, |
| 261 |
}, |
| 262 |
expect: validBytes, |
| 263 |
expectErr: false, |
| 264 |
}, |
| 265 |
{ |
| 266 |
name: "failed to timestamp", |
| 267 |
req: reqWithInvalidTSAurl, |
| 268 |
env: &Envelope{ |
| 269 |
Raw: validBytes, |
| 270 |
Envelope: &mockEnvelope{ |
| 271 |
content: &signature.EnvelopeContent{ |
| 272 |
SignerInfo: *validSignerInfo, |
| 273 |
}, |
| 274 |
failTimestamp: true, |
| 275 |
}, |
| 276 |
}, |
| 277 |
expect: nil, |
| 278 |
expectErr: true, |
| 279 |
}, |
| 280 |
} |
| 281 |
|
| 282 |
for _, tt := range tests { |
| 283 |
t.Run(tt.name, func(t *testing.T) { |
| 284 |
sig, err := tt.env.Sign(tt.req) |
| 285 |
|
| 286 |
if (err != nil) != tt.expectErr { |
| 287 |
t.Errorf("error = %v, expectErr = %v", err, tt.expectErr) |
| 288 |
} |
| 289 |
if !reflect.DeepEqual(sig, tt.expect) { |
| 290 |
t.Errorf("expect %+v, got %+v", tt.expect, sig) |
| 291 |
} |
| 292 |
|
| 293 |
if tt.name == "failed to timestamp" { |
| 294 |
var timestampErr *signature.TimestampError |
| 295 |
if !errors.As(err, ×tampErr) { |
| 296 |
t.Fatal("expecting error to be signature.TimestampError") |
| 297 |
} |
| 298 |
expectedErrMsg := "timestamp error" |
| 299 |
if timestampErr.Error() != expectedErrMsg { |
| 300 |
t.Fatalf("expected error %s, but got %v", expectedErrMsg, err) |
| 301 |
} |
| 302 |
} |
| 303 |
}) |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
func TestVerify(t *testing.T) { |
| 308 |
tests := []struct { |
| 309 |
name string |
| 310 |
env *Envelope |
| 311 |
expectContent *signature.EnvelopeContent |
| 312 |
expectErr bool |
| 313 |
}{ |
| 314 |
{ |
| 315 |
name: "empty raw", |
| 316 |
env: &Envelope{}, |
| 317 |
expectContent: nil, |
| 318 |
expectErr: true, |
| 319 |
}, |
| 320 |
{ |
| 321 |
name: "err returned by internal envelope", |
| 322 |
env: &Envelope{ |
| 323 |
Raw: validBytes, |
| 324 |
Envelope: &mockEnvelope{ |
| 325 |
failVerify: true, |
| 326 |
payload: validPayload, |
| 327 |
}, |
| 328 |
}, |
| 329 |
expectContent: nil, |
| 330 |
expectErr: true, |
| 331 |
}, |
| 332 |
{ |
| 333 |
name: "payload validation failed after internal envelope verfication", |
| 334 |
env: &Envelope{ |
| 335 |
Raw: validBytes, |
| 336 |
Envelope: &mockEnvelope{ |
| 337 |
content: &signature.EnvelopeContent{ |
| 338 |
Payload: signature.Payload{ |
| 339 |
ContentType: invalidContentType, |
| 340 |
}, |
| 341 |
}, |
| 342 |
}, |
| 343 |
}, |
| 344 |
expectContent: nil, |
| 345 |
expectErr: true, |
| 346 |
}, |
| 347 |
{ |
| 348 |
name: "signerInfo validation failed after internal envelope verfication", |
| 349 |
env: &Envelope{ |
| 350 |
Raw: validBytes, |
| 351 |
Envelope: &mockEnvelope{ |
| 352 |
content: &signature.EnvelopeContent{ |
| 353 |
Payload: *validPayload, |
| 354 |
SignerInfo: signature.SignerInfo{}, |
| 355 |
}, |
| 356 |
}, |
| 357 |
}, |
| 358 |
expectContent: nil, |
| 359 |
expectErr: true, |
| 360 |
}, |
| 361 |
{ |
| 362 |
name: "verify successfully", |
| 363 |
env: &Envelope{ |
| 364 |
Raw: validBytes, |
| 365 |
Envelope: &mockEnvelope{ |
| 366 |
content: &signature.EnvelopeContent{ |
| 367 |
Payload: *validPayload, |
| 368 |
SignerInfo: *validSignerInfo, |
| 369 |
}, |
| 370 |
}, |
| 371 |
}, |
| 372 |
expectContent: &signature.EnvelopeContent{ |
| 373 |
Payload: *validPayload, |
| 374 |
SignerInfo: *validSignerInfo, |
| 375 |
}, |
| 376 |
expectErr: false, |
| 377 |
}, |
| 378 |
} |
| 379 |
|
| 380 |
for _, tt := range tests { |
| 381 |
t.Run(tt.name, func(t *testing.T) { |
| 382 |
content, err := tt.env.Verify() |
| 383 |
|
| 384 |
if (err != nil) != tt.expectErr { |
| 385 |
t.Errorf("error = %v, expectErr = %v", err, tt.expectErr) |
| 386 |
} |
| 387 |
if !reflect.DeepEqual(content, tt.expectContent) { |
| 388 |
t.Errorf("expect content: %+v, got %+v", tt.expectContent, content) |
| 389 |
} |
| 390 |
}) |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
func TestContent(t *testing.T) { |
| 395 |
tests := []struct { |
| 396 |
name string |
| 397 |
env *Envelope |
| 398 |
expect *signature.EnvelopeContent |
| 399 |
expectErr bool |
| 400 |
}{ |
| 401 |
{ |
| 402 |
name: "empty raw", |
| 403 |
env: &Envelope{}, |
| 404 |
expect: nil, |
| 405 |
expectErr: true, |
| 406 |
}, |
| 407 |
{ |
| 408 |
name: "err returned by internal envelope", |
| 409 |
env: &Envelope{ |
| 410 |
Raw: validBytes, |
| 411 |
Envelope: &mockEnvelope{}, |
| 412 |
}, |
| 413 |
expect: nil, |
| 414 |
expectErr: true, |
| 415 |
}, |
| 416 |
{ |
| 417 |
name: "invalid payload", |
| 418 |
env: &Envelope{ |
| 419 |
Raw: validBytes, |
| 420 |
Envelope: &mockEnvelope{ |
| 421 |
content: &signature.EnvelopeContent{ |
| 422 |
Payload: signature.Payload{}, |
| 423 |
}, |
| 424 |
}, |
| 425 |
}, |
| 426 |
expect: nil, |
| 427 |
expectErr: true, |
| 428 |
}, |
| 429 |
{ |
| 430 |
name: "valid payload and invalid signerInfo", |
| 431 |
env: &Envelope{ |
| 432 |
Raw: validBytes, |
| 433 |
Envelope: &mockEnvelope{ |
| 434 |
content: &signature.EnvelopeContent{ |
| 435 |
Payload: *validPayload, |
| 436 |
}, |
| 437 |
signerInfo: &signature.SignerInfo{}, |
| 438 |
}, |
| 439 |
}, |
| 440 |
expect: nil, |
| 441 |
expectErr: true, |
| 442 |
}, |
| 443 |
{ |
| 444 |
name: "valid payload and valid signerInfo", |
| 445 |
env: &Envelope{ |
| 446 |
Raw: validBytes, |
| 447 |
Envelope: &mockEnvelope{ |
| 448 |
content: &signature.EnvelopeContent{ |
| 449 |
Payload: *validPayload, |
| 450 |
SignerInfo: *validSignerInfo, |
| 451 |
}, |
| 452 |
}, |
| 453 |
}, |
| 454 |
expect: validEnvelopeContent, |
| 455 |
expectErr: false, |
| 456 |
}, |
| 457 |
} |
| 458 |
|
| 459 |
for _, tt := range tests { |
| 460 |
t.Run(tt.name, func(t *testing.T) { |
| 461 |
content, err := tt.env.Content() |
| 462 |
|
| 463 |
if (err != nil) != tt.expectErr { |
| 464 |
t.Errorf("error = %v, expectErr = %v", err, tt.expectErr) |
| 465 |
} |
| 466 |
if !reflect.DeepEqual(content, tt.expect) { |
| 467 |
t.Errorf("expect %+v, got %+v", tt.expect, content) |
| 468 |
} |
| 469 |
}) |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
func TestValidateSignRequest(t *testing.T) { |
| 474 |
tests := []struct { |
| 475 |
name string |
| 476 |
req *signature.SignRequest |
| 477 |
expectErr bool |
| 478 |
}{ |
| 479 |
{ |
| 480 |
name: "invalid payload", |
| 481 |
req: &signature.SignRequest{}, |
| 482 |
expectErr: true, |
| 483 |
}, |
| 484 |
{ |
| 485 |
name: "invalid signing time", |
| 486 |
req: &signature.SignRequest{ |
| 487 |
Payload: signature.Payload{ |
| 488 |
ContentType: validContentType, |
| 489 |
Content: validBytes, |
| 490 |
}, |
| 491 |
}, |
| 492 |
expectErr: true, |
| 493 |
}, |
| 494 |
{ |
| 495 |
name: "signer is nil", |
| 496 |
req: &signature.SignRequest{ |
| 497 |
Payload: signature.Payload{ |
| 498 |
ContentType: validContentType, |
| 499 |
Content: validBytes, |
| 500 |
}, |
| 501 |
SigningTime: time08_02, |
| 502 |
Expiry: time08_03, |
| 503 |
}, |
| 504 |
expectErr: true, |
| 505 |
}, |
| 506 |
{ |
| 507 |
name: "keySpec is empty", |
| 508 |
req: &signature.SignRequest{ |
| 509 |
Payload: signature.Payload{ |
| 510 |
ContentType: validContentType, |
| 511 |
Content: validBytes, |
| 512 |
}, |
| 513 |
SigningTime: time08_02, |
| 514 |
Expiry: time08_03, |
| 515 |
SigningScheme: signiningSchema, |
| 516 |
Signer: &mockSigner{ |
| 517 |
certs: []*x509.Certificate{ |
| 518 |
testhelper.GetRSALeafCertificate().Cert, |
| 519 |
testhelper.GetRSARootCertificate().Cert, |
| 520 |
}, |
| 521 |
keySpec: signature.KeySpec{}, |
| 522 |
}, |
| 523 |
}, |
| 524 |
expectErr: true, |
| 525 |
}, |
| 526 |
{ |
| 527 |
name: "invalid signing schema", |
| 528 |
req: &signature.SignRequest{ |
| 529 |
Payload: signature.Payload{ |
| 530 |
ContentType: validContentType, |
| 531 |
Content: validBytes, |
| 532 |
}, |
| 533 |
SigningTime: time08_02, |
| 534 |
Expiry: time08_03, |
| 535 |
Signer: &mockSigner{ |
| 536 |
keySpec: signature.KeySpec{ |
| 537 |
Type: signature.KeyTypeRSA, |
| 538 |
Size: 3072, |
| 539 |
}, |
| 540 |
certs: []*x509.Certificate{ |
| 541 |
testhelper.GetRSALeafCertificate().Cert, |
| 542 |
testhelper.GetRSARootCertificate().Cert, |
| 543 |
}, |
| 544 |
}, |
| 545 |
}, |
| 546 |
expectErr: true, |
| 547 |
}, |
| 548 |
{ |
| 549 |
name: "valid request", |
| 550 |
req: validReq, |
| 551 |
expectErr: false, |
| 552 |
}, |
| 553 |
} |
| 554 |
|
| 555 |
for _, tt := range tests { |
| 556 |
t.Run(tt.name, func(t *testing.T) { |
| 557 |
err := validateSignRequest(tt.req) |
| 558 |
|
| 559 |
if (err != nil) != tt.expectErr { |
| 560 |
t.Errorf("error = %v, expectErr = %v", err, tt.expectErr) |
| 561 |
} |
| 562 |
}) |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
func TestValidateSignerInfo(t *testing.T) { |
| 567 |
tests := []struct { |
| 568 |
name string |
| 569 |
info *signature.SignerInfo |
| 570 |
expectErr bool |
| 571 |
}{ |
| 572 |
{ |
| 573 |
name: "empty signature", |
| 574 |
info: &signature.SignerInfo{}, |
| 575 |
expectErr: true, |
| 576 |
}, |
| 577 |
{ |
| 578 |
name: "missing signature algorithm", |
| 579 |
info: &signature.SignerInfo{ |
| 580 |
Signature: validBytes, |
| 581 |
}, |
| 582 |
expectErr: true, |
| 583 |
}, |
| 584 |
{ |
| 585 |
name: "invalid signing time", |
| 586 |
info: &signature.SignerInfo{ |
| 587 |
Signature: validBytes, |
| 588 |
SignatureAlgorithm: signature.AlgorithmPS256, |
| 589 |
}, |
| 590 |
expectErr: true, |
| 591 |
}, |
| 592 |
{ |
| 593 |
name: "invalid signing schema", |
| 594 |
info: &signature.SignerInfo{ |
| 595 |
Signature: validBytes, |
| 596 |
SignatureAlgorithm: signature.AlgorithmPS384, |
| 597 |
SignedAttributes: signature.SignedAttributes{ |
| 598 |
SigningTime: testhelper.GetRSALeafCertificate().Cert.NotBefore, |
| 599 |
Expiry: testhelper.GetECLeafCertificate().Cert.NotAfter, |
| 600 |
}, |
| 601 |
}, |
| 602 |
expectErr: true, |
| 603 |
}, |
| 604 |
{ |
| 605 |
name: "valid signerInfo", |
| 606 |
info: validSignerInfo, |
| 607 |
expectErr: false, |
| 608 |
}, |
| 609 |
} |
| 610 |
|
| 611 |
for _, tt := range tests { |
| 612 |
t.Run(tt.name, func(t *testing.T) { |
| 613 |
err := validateSignerInfo(tt.info) |
| 614 |
|
| 615 |
if (err != nil) != tt.expectErr { |
| 616 |
t.Errorf("error = %v, expectErr = %v", err, tt.expectErr) |
| 617 |
} |
| 618 |
}) |
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
func TestValidateSigningTime(t *testing.T) { |
| 623 |
tests := []struct { |
| 624 |
name string |
| 625 |
signingTime time.Time |
| 626 |
expireTime time.Time |
| 627 |
expectErr bool |
| 628 |
}{ |
| 629 |
{ |
| 630 |
name: "zero signing time", |
| 631 |
signingTime: time.Time{}, |
| 632 |
expireTime: time.Now(), |
| 633 |
expectErr: true, |
| 634 |
}, |
| 635 |
{ |
| 636 |
name: "no expire time", |
| 637 |
signingTime: time.Now(), |
| 638 |
expireTime: time.Time{}, |
| 639 |
expectErr: false, |
| 640 |
}, |
| 641 |
{ |
| 642 |
name: "expireTime set but invalid", |
| 643 |
signingTime: time08_03, |
| 644 |
expireTime: time08_02, |
| 645 |
expectErr: true, |
| 646 |
}, |
| 647 |
} |
| 648 |
|
| 649 |
for _, tt := range tests { |
| 650 |
t.Run(tt.name, func(t *testing.T) { |
| 651 |
err := validateSigningAndExpiryTime(tt.signingTime, tt.expireTime) |
| 652 |
|
| 653 |
if (err != nil) != tt.expectErr { |
| 654 |
t.Errorf("error = %v, expectErr = %v", err, tt.expectErr) |
| 655 |
} |
| 656 |
}) |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
func TestValidatePayload(t *testing.T) { |
| 661 |
tests := []struct { |
| 662 |
name string |
| 663 |
payload *signature.Payload |
| 664 |
expectErr bool |
| 665 |
}{ |
| 666 |
{ |
| 667 |
name: "invalid payload content type", |
| 668 |
payload: &signature.Payload{ |
| 669 |
ContentType: invalidContentType, |
| 670 |
}, |
| 671 |
expectErr: true, |
| 672 |
}, |
| 673 |
{ |
| 674 |
name: "payload content is empty", |
| 675 |
payload: &signature.Payload{ |
| 676 |
ContentType: validContentType, |
| 677 |
Content: []byte{}, |
| 678 |
}, |
| 679 |
expectErr: true, |
| 680 |
}, |
| 681 |
{ |
| 682 |
name: "valid payload", |
| 683 |
payload: validPayload, |
| 684 |
expectErr: false, |
| 685 |
}, |
| 686 |
} |
| 687 |
|
| 688 |
for _, tt := range tests { |
| 689 |
t.Run(tt.name, func(t *testing.T) { |
| 690 |
err := validatePayload(tt.payload) |
| 691 |
|
| 692 |
if (err != nil) != tt.expectErr { |
| 693 |
t.Errorf("error = %v, expectErr = %v", err, tt.expectErr) |
| 694 |
} |
| 695 |
}) |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
func TestValidateCertificateChain(t *testing.T) { |
| 700 |
tests := []struct { |
| 701 |
name string |
| 702 |
certs []*x509.Certificate |
| 703 |
signTime time.Time |
| 704 |
alg signature.Algorithm |
| 705 |
expectErr bool |
| 706 |
}{ |
| 707 |
{ |
| 708 |
name: "empty certs", |
| 709 |
certs: []*x509.Certificate{}, |
| 710 |
signTime: time.Now(), |
| 711 |
alg: signature.AlgorithmES256, |
| 712 |
expectErr: true, |
| 713 |
}, |
| 714 |
{ |
| 715 |
name: "invalid certificates", |
| 716 |
certs: []*x509.Certificate{ |
| 717 |
testhelper.GetECLeafCertificate().Cert, |
| 718 |
}, |
| 719 |
signTime: time.Now(), |
| 720 |
alg: signature.AlgorithmES256, |
| 721 |
expectErr: true, |
| 722 |
}, |
| 723 |
{ |
| 724 |
name: "unmatched signing algorithm", |
| 725 |
certs: []*x509.Certificate{ |
| 726 |
testhelper.GetRSALeafCertificate().Cert, |
| 727 |
testhelper.GetRSARootCertificate().Cert, |
| 728 |
}, |
| 729 |
signTime: testhelper.GetRSALeafCertificate().Cert.NotBefore, |
| 730 |
alg: signature.AlgorithmPS256, |
| 731 |
expectErr: true, |
| 732 |
}, |
| 733 |
{ |
| 734 |
name: "valid certificate chain", |
| 735 |
certs: []*x509.Certificate{ |
| 736 |
testhelper.GetRSALeafCertificate().Cert, |
| 737 |
testhelper.GetRSARootCertificate().Cert, |
| 738 |
}, |
| 739 |
signTime: testhelper.GetRSALeafCertificate().Cert.NotBefore, |
| 740 |
alg: signature.AlgorithmPS384, |
| 741 |
expectErr: false, |
| 742 |
}, |
| 743 |
} |
| 744 |
|
| 745 |
for _, tt := range tests { |
| 746 |
t.Run(tt.name, func(t *testing.T) { |
| 747 |
err := validateCertificateChain(tt.certs, &tt.signTime, tt.alg) |
| 748 |
|
| 749 |
if (err != nil) != tt.expectErr { |
| 750 |
t.Errorf("error = %v, expectErr = %v", err, tt.expectErr) |
| 751 |
} |
| 752 |
}) |
| 753 |
} |
| 754 |
} |
| 755 |
|
| 756 |
func TestGetSignatureAlgorithm(t *testing.T) { |
| 757 |
tests := []struct { |
| 758 |
name string |
| 759 |
cert *x509.Certificate |
| 760 |
expect signature.Algorithm |
| 761 |
expectErr bool |
| 762 |
}{ |
| 763 |
{ |
| 764 |
name: "unsupported cert", |
| 765 |
cert: testhelper.GetUnsupportedRSACert().Cert, |
| 766 |
expect: 0, |
| 767 |
expectErr: true, |
| 768 |
}, |
| 769 |
{ |
| 770 |
name: "valid cert", |
| 771 |
cert: testhelper.GetRSALeafCertificate().Cert, |
| 772 |
expect: signature.AlgorithmPS384, |
| 773 |
expectErr: false, |
| 774 |
}, |
| 775 |
} |
| 776 |
|
| 777 |
for _, tt := range tests { |
| 778 |
t.Run(tt.name, func(t *testing.T) { |
| 779 |
alg, err := getSignatureAlgorithm(tt.cert) |
| 780 |
|
| 781 |
if (err != nil) != tt.expectErr { |
| 782 |
t.Errorf("error = %v, expectErr = %v", err, tt.expectErr) |
| 783 |
} |
| 784 |
if !reflect.DeepEqual(alg, tt.expect) { |
| 785 |
t.Errorf("expect %+v, got %+v", tt.expect, alg) |
| 786 |
} |
| 787 |
}) |
| 788 |
} |
| 789 |
} |