...

Source file src/github.com/sigstore/timestamp-authority/pkg/signer/memory_test.go

Documentation: github.com/sigstore/timestamp-authority/pkg/signer

     1  // Copyright 2022 The Sigstore Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package signer
    16  
    17  import (
    18  	"bytes"
    19  	"context"
    20  	"crypto"
    21  	"crypto/rand"
    22  	"crypto/sha256"
    23  	"testing"
    24  
    25  	"github.com/sigstore/sigstore/pkg/signature"
    26  	"github.com/sigstore/sigstore/pkg/signature/options"
    27  	"github.com/sigstore/timestamp-authority/pkg/x509"
    28  )
    29  
    30  func TestNewTimestampingCertWithChain(t *testing.T) {
    31  	ctx := context.Background()
    32  
    33  	signer, err := NewCryptoSigner(ctx, crypto.Hash(0), "memory", "", "", "", "", "", "")
    34  	if err != nil {
    35  		t.Fatalf("new signer: %v", err)
    36  	}
    37  
    38  	payload := []byte("payload")
    39  	h := sha256.Sum256(payload)
    40  
    41  	sig, err := signer.Sign(rand.Reader, h[:], nil)
    42  	if err != nil {
    43  		t.Fatalf("signing payload: %v", err)
    44  	}
    45  	// create and verify the certificate chain
    46  	certChain, err := NewTimestampingCertWithChain(signer)
    47  	if err != nil {
    48  		t.Fatalf("generating timestamping cert: %v", err)
    49  	}
    50  	if len(certChain) != 3 {
    51  		t.Fatalf("expected 3 certificates in chain, got %d", len(certChain))
    52  	}
    53  
    54  	// verify that certificate can verify signature
    55  	pkCert := certChain[0].PublicKey
    56  	verifier, err := signature.LoadVerifier(pkCert, crypto.SHA256)
    57  	if err != nil {
    58  		t.Fatalf("initializing verifier: %v", err)
    59  	}
    60  	if err := verifier.VerifySignature(bytes.NewReader(sig), bytes.NewReader(payload), options.WithContext(ctx)); err != nil {
    61  		t.Fatalf("failed to verify signature: %v", err)
    62  	}
    63  
    64  	// verify that VerifyCertChain will successfully verify the chain
    65  	if err := x509.VerifyCertChain(certChain, signer); err != nil {
    66  		t.Fatalf("failed to verify certificate chain: %v", err)
    67  	}
    68  }
    69  

View as plain text