| 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 signaturescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/ |
| 17 | |
| 18 | import ( |
| 19 | "cryptoscip-go gomod github.com/golang/go/src go1.25.0 crypto/" |
| 20 | "crypto/ecdsascip-go gomod github.com/golang/go/src go1.25.0 `crypto/ecdsa`/" |
| 21 | "crypto/ed25519scip-go gomod github.com/golang/go/src go1.25.0 `crypto/ed25519`/" |
| 22 | "crypto/rsascip-go gomod github.com/golang/go/src go1.25.0 `crypto/rsa`/" |
| 23 | "errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/" |
| 24 | "osscip-go gomod github.com/golang/go/src go1.25.0 os/" |
| 25 | "path/filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/" |
| 26 | |
| 27 | "github.com/sigstore/sigstore/pkg/cryptoutilsscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/cryptoutils`/" |
| 28 | "github.com/sigstore/sigstore/pkg/signature/optionsscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/options`/" |
| 29 | ) |
| 30 | |
| 31 | // SignerVerifier creates and verifies digital signatures over a message using a specified key pair |
| 32 | type SignerVerifierscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/SignerVerifier# interface { |
| 33 | Signerscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/Signer# |
| 34 | Verifierscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/Verifier# |
| 35 | } |
| 36 | |
| 37 | // LoadSignerVerifier returns a signature.SignerVerifier based on the algorithm of the private key |
| 38 | // provided. |
| 39 | // |
| 40 | // If privateKey is an RSA key, a RSAPKCS1v15SignerVerifier will be returned. If a |
| 41 | // RSAPSSSignerVerifier is desired instead, use the LoadRSAPSSSignerVerifier() method directly. |
| 42 | func LoadSignerVerifierscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadSignerVerifier().(privateKey cryptoscip-go gomod github.com/golang/go/src go1.25.0 crypto/.PrivateKeyscip-go gomod github.com/golang/go/src go1.25.0 crypto/PrivateKey#, hashFunc cryptoscip-go gomod github.com/golang/go/src go1.25.0 crypto/.Hashscip-go gomod github.com/golang/go/src go1.25.0 crypto/Hash#) (SignerVerifierscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/SignerVerifier#, error) { |
| 43 | return LoadSignerVerifierWithOptsscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadSignerVerifierWithOpts().(privateKey, optionsscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/options`/.WithHashscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/options`/WithHash().(hashFunc)) |
| 44 | } |
| 45 | |
| 46 | // LoadSignerVerifierWithOpts returns a signature.SignerVerifier based on the |
| 47 | // algorithm of the private key provided and the user's choice. |
| 48 | func LoadSignerVerifierWithOptsscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadSignerVerifierWithOpts().(privateKey cryptoscip-go gomod github.com/golang/go/src go1.25.0 crypto/.PrivateKeyscip-go gomod github.com/golang/go/src go1.25.0 crypto/PrivateKey#, opts ...LoadOptionscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadOption#) (SignerVerifierscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/SignerVerifier#, error) { |
| 49 | var rsaPSSOptions *rsascip-go gomod github.com/golang/go/src go1.25.0 `crypto/rsa`/.PSSOptionsscip-go gomod github.com/golang/go/src go1.25.0 `crypto/rsa`/PSSOptions# |
| 50 | var useED25519ph bool |
| 51 | hashFunc := cryptoscip-go gomod github.com/golang/go/src go1.25.0 crypto/.SHA256scip-go gomod github.com/golang/go/src go1.25.0 crypto/SHA256. |
| 52 | for _, o := range opts { |
| 53 | o.ApplyED25519phscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadOption#ApplyED25519ph.(&useED25519ph) |
| 54 | o.ApplyHashscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadOption#ApplyHash.(&hashFunc) |
| 55 | o.ApplyRSAPSSscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadOption#ApplyRSAPSS.(&rsaPSSOptions) |
| 56 | } |
| 57 | |
| 58 | switch pk := privateKey.(type) { |
| 59 | case *rsascip-go gomod github.com/golang/go/src go1.25.0 `crypto/rsa`/.PrivateKeyscip-go gomod github.com/golang/go/src go1.25.0 `crypto/rsa`/PrivateKey#: |
| 60 | if rsaPSSOptions != nil { |
| 61 | return LoadRSAPSSSignerVerifierscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadRSAPSSSignerVerifier().(pk, hashFunc, rsaPSSOptions) |
| 62 | } |
| 63 | return LoadRSAPKCS1v15SignerVerifierscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadRSAPKCS1v15SignerVerifier().(pk, hashFunc) |
| 64 | case *ecdsascip-go gomod github.com/golang/go/src go1.25.0 `crypto/ecdsa`/.PrivateKeyscip-go gomod github.com/golang/go/src go1.25.0 `crypto/ecdsa`/PrivateKey#: |
| 65 | return LoadECDSASignerVerifierscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadECDSASignerVerifier().(pk, hashFunc) |
| 66 | case ed25519scip-go gomod github.com/golang/go/src go1.25.0 `crypto/ed25519`/.PrivateKeyscip-go gomod github.com/golang/go/src go1.25.0 `crypto/ed25519`/PrivateKey#: |
| 67 | if useED25519ph { |
| 68 | return LoadED25519phSignerVerifierscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadED25519phSignerVerifier().(pk) |
| 69 | } |
| 70 | return LoadED25519SignerVerifierscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadED25519SignerVerifier().(pk) |
| 71 | } |
| 72 | return nil, errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/.Newscip-go gomod github.com/golang/go/src go1.25.0 errors/New().("unsupported public key type") |
| 73 | } |
| 74 | |
| 75 | // LoadSignerVerifierFromPEMFile returns a signature.SignerVerifier based on the algorithm of the private key |
| 76 | // in the file. The SignerVerifier will use the hash function specified when computing digests. |
| 77 | // |
| 78 | // If publicKey is an RSA key, a RSAPKCS1v15SignerVerifier will be returned. If a |
| 79 | // RSAPSSSignerVerifier is desired instead, use the LoadRSAPSSSignerVerifier() and |
| 80 | // cryptoutils.UnmarshalPEMToPrivateKey() methods directly. |
| 81 | func LoadSignerVerifierFromPEMFilescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadSignerVerifierFromPEMFile().(path string, hashFunc cryptoscip-go gomod github.com/golang/go/src go1.25.0 crypto/.Hashscip-go gomod github.com/golang/go/src go1.25.0 crypto/Hash#, pf cryptoutilsscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/cryptoutils`/.PassFuncscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/cryptoutils`/PassFunc#) (SignerVerifierscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/SignerVerifier#, error) { |
| 82 | fileBytes, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.ReadFilescip-go gomod github.com/golang/go/src go1.25.0 os/ReadFile().(filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Cleanscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Clean().(path)) |
| 83 | if err != nil { |
| 84 | return nil, err |
| 85 | } |
| 86 | priv, err := cryptoutilsscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/cryptoutils`/.UnmarshalPEMToPrivateKeyscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/cryptoutils`/UnmarshalPEMToPrivateKey().(fileBytes, pf) |
| 87 | if err != nil { |
| 88 | return nil, err |
| 89 | } |
| 90 | return LoadSignerVerifierscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadSignerVerifier().(priv, hashFunc) |
| 91 | } |
| 92 | |
| 93 | // LoadSignerVerifierFromPEMFileWithOpts returns a signature.SignerVerifier based on the algorithm of the private key |
| 94 | // in the file. The SignerVerifier will use the hash function specified in the options when computing digests. |
| 95 | func LoadSignerVerifierFromPEMFileWithOptsscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadSignerVerifierFromPEMFileWithOpts().(path string, pf cryptoutilsscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/cryptoutils`/.PassFuncscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/cryptoutils`/PassFunc#, opts ...LoadOptionscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadOption#) (SignerVerifierscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/SignerVerifier#, error) { |
| 96 | fileBytes, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.ReadFilescip-go gomod github.com/golang/go/src go1.25.0 os/ReadFile().(filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Cleanscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Clean().(path)) |
| 97 | if err != nil { |
| 98 | return nil, err |
| 99 | } |
| 100 | priv, err := cryptoutilsscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/cryptoutils`/.UnmarshalPEMToPrivateKeyscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/cryptoutils`/UnmarshalPEMToPrivateKey().(fileBytes, pf) |
| 101 | if err != nil { |
| 102 | return nil, err |
| 103 | } |
| 104 | return LoadSignerVerifierWithOptsscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadSignerVerifierWithOpts().(priv, opts...) |
| 105 | } |
| 106 | |
| 107 | // LoadDefaultSignerVerifier returns a signature.SignerVerifier based on |
| 108 | // the private key. Each private key has a corresponding PublicKeyDetails |
| 109 | // associated in the Sigstore ecosystem, see Algorithm Registry for more details. |
| 110 | func LoadDefaultSignerVerifierscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadDefaultSignerVerifier().(privateKey cryptoscip-go gomod github.com/golang/go/src go1.25.0 crypto/.PrivateKeyscip-go gomod github.com/golang/go/src go1.25.0 crypto/PrivateKey#, opts ...LoadOptionscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadOption#) (SignerVerifierscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/SignerVerifier#, error) { |
| 111 | signer, ok := privateKey.(cryptoscip-go gomod github.com/golang/go/src go1.25.0 crypto/.Signerscip-go gomod github.com/golang/go/src go1.25.0 crypto/Signer#) |
| 112 | if !ok { |
| 113 | return nil, errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/.Newscip-go gomod github.com/golang/go/src go1.25.0 errors/New().("private key does not implement signature.Signer") |
| 114 | } |
| 115 | algorithmDetails, err := GetDefaultAlgorithmDetailsscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/GetDefaultAlgorithmDetails().(signer.Publicscip-go gomod github.com/golang/go/src go1.25.0 crypto/Signer#Public().(), opts...) |
| 116 | if err != nil { |
| 117 | return nil, err |
| 118 | } |
| 119 | return LoadSignerVerifierFromAlgorithmDetailsscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadSignerVerifierFromAlgorithmDetails().(privateKey, algorithmDetails, opts...) |
| 120 | } |
| 121 | |
| 122 | // LoadSignerVerifierFromAlgorithmDetails returns a signature.SignerVerifier based on |
| 123 | // the algorithm details and the user's choice of options. |
| 124 | func LoadSignerVerifierFromAlgorithmDetailsscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadSignerVerifierFromAlgorithmDetails().(privateKey cryptoscip-go gomod github.com/golang/go/src go1.25.0 crypto/.PrivateKeyscip-go gomod github.com/golang/go/src go1.25.0 crypto/PrivateKey#, algorithmDetails AlgorithmDetailsscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/AlgorithmDetails#, opts ...LoadOptionscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadOption#) (SignerVerifierscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/SignerVerifier#, error) { |
| 125 | filteredOpts := GetOptsFromAlgorithmDetailsscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/GetOptsFromAlgorithmDetails().(algorithmDetails, opts...) |
| 126 | return LoadSignerVerifierWithOptsscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadSignerVerifierWithOpts().(privateKey, filteredOpts...) |
| 127 | } |