...

Source file src/github.com/sigstore/rekor/pkg/pki/factory.go

Documentation: github.com/sigstore/rekor/pkg/pki

     1  //
     2  // Copyright 2021 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 pki
    17  
    18  import (
    19  	"fmt"
    20  	"io"
    21  
    22  	"github.com/sigstore/rekor/pkg/pki/minisign"
    23  	"github.com/sigstore/rekor/pkg/pki/pgp"
    24  	"github.com/sigstore/rekor/pkg/pki/pkcs7"
    25  	"github.com/sigstore/rekor/pkg/pki/ssh"
    26  	"github.com/sigstore/rekor/pkg/pki/tuf"
    27  	"github.com/sigstore/rekor/pkg/pki/x509"
    28  )
    29  
    30  type Format string
    31  
    32  const (
    33  	PGP      Format = "pgp"
    34  	Minisign Format = "minisign"
    35  	SSH      Format = "ssh"
    36  	X509     Format = "x509"
    37  	PKCS7    Format = "pkcs7"
    38  	Tuf      Format = "tuf"
    39  )
    40  
    41  type ArtifactFactory struct {
    42  	impl pkiImpl
    43  }
    44  
    45  func NewArtifactFactory(format Format) (*ArtifactFactory, error) {
    46  	if impl, ok := artifactFactoryMap[format]; ok {
    47  		return &ArtifactFactory{impl: impl}, nil
    48  	}
    49  	return nil, fmt.Errorf("%v is not a supported PKI format", format)
    50  }
    51  
    52  type pkiImpl struct {
    53  	newPubKey    func(io.Reader) (PublicKey, error)
    54  	newSignature func(io.Reader) (Signature, error)
    55  }
    56  
    57  var artifactFactoryMap map[Format]pkiImpl
    58  
    59  func init() {
    60  	artifactFactoryMap = map[Format]pkiImpl{
    61  		PGP: {
    62  			newPubKey: func(r io.Reader) (PublicKey, error) {
    63  				return pgp.NewPublicKey(r)
    64  			},
    65  			newSignature: func(r io.Reader) (Signature, error) {
    66  				return pgp.NewSignature(r)
    67  			},
    68  		},
    69  		Minisign: {
    70  			newPubKey: func(r io.Reader) (PublicKey, error) {
    71  				return minisign.NewPublicKey(r)
    72  			},
    73  			newSignature: func(r io.Reader) (Signature, error) {
    74  				return minisign.NewSignature(r)
    75  			},
    76  		},
    77  		SSH: {
    78  			newPubKey: func(r io.Reader) (PublicKey, error) {
    79  				return ssh.NewPublicKey(r)
    80  			},
    81  			newSignature: func(r io.Reader) (Signature, error) {
    82  				return ssh.NewSignature(r)
    83  			},
    84  		},
    85  		X509: {
    86  			newPubKey: func(r io.Reader) (PublicKey, error) {
    87  				return x509.NewPublicKey(r)
    88  			},
    89  			newSignature: func(r io.Reader) (Signature, error) {
    90  				return x509.NewSignature(r)
    91  			},
    92  		},
    93  		PKCS7: {
    94  			newPubKey: func(r io.Reader) (PublicKey, error) {
    95  				return pkcs7.NewPublicKey(r)
    96  			},
    97  			newSignature: func(r io.Reader) (Signature, error) {
    98  				return pkcs7.NewSignature(r)
    99  			},
   100  		},
   101  		Tuf: {
   102  			newPubKey: func(r io.Reader) (PublicKey, error) {
   103  				return tuf.NewPublicKey(r)
   104  			},
   105  			newSignature: func(r io.Reader) (Signature, error) {
   106  				return tuf.NewSignature(r)
   107  			},
   108  		},
   109  	}
   110  }
   111  
   112  func SupportedFormats() []string {
   113  	var formats []string
   114  	for f := range artifactFactoryMap {
   115  		formats = append(formats, string(f))
   116  	}
   117  	return formats
   118  }
   119  
   120  func (a ArtifactFactory) NewPublicKey(r io.Reader) (PublicKey, error) {
   121  	return a.impl.newPubKey(r)
   122  }
   123  
   124  func (a ArtifactFactory) NewSignature(r io.Reader) (Signature, error) {
   125  	return a.impl.newSignature(r)
   126  }
   127  

View as plain text