| 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 | "ioscip-go gomod github.com/golang/go/src go1.25.0 io/" |
| 25 | "osscip-go gomod github.com/golang/go/src go1.25.0 os/" |
| 26 | "path/filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/" |
| 27 | |
| 28 | "github.com/sigstore/sigstore/pkg/cryptoutilsscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/cryptoutils`/" |
| 29 | "github.com/sigstore/sigstore/pkg/signature/optionsscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature/options`/" |
| 30 | ) |
| 31 | |
| 32 | // Verifier verifies the digital signature using a specified public key |
| 33 | type Verifierscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/Verifier# interface { |
| 34 | PublicKeyProviderscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/PublicKeyProvider# |
| 35 | VerifySignaturescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/Verifier#VerifySignature.(signature, message ioscip-go gomod github.com/golang/go/src go1.25.0 io/.Readerscip-go gomod github.com/golang/go/src go1.25.0 io/Reader#, opts ...VerifyOptionscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/VerifyOption#) error |
| 36 | } |
| 37 | |
| 38 | // LoadVerifier returns a signature.Verifier based on the algorithm of the public key |
| 39 | // provided that will use the hash function specified when computing digests. |
| 40 | // |
| 41 | // If publicKey is an RSA key, a RSAPKCS1v15Verifier will be returned. If a |
| 42 | // RSAPSSVerifier is desired instead, use the LoadRSAPSSVerifier() method directly. |
| 43 | func LoadVerifierscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadVerifier().(publicKey cryptoscip-go gomod github.com/golang/go/src go1.25.0 crypto/.PublicKeyscip-go gomod github.com/golang/go/src go1.25.0 crypto/PublicKey#, 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#) (Verifierscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/Verifier#, error) { |
| 44 | return LoadVerifierWithOptsscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadVerifierWithOpts().(publicKey, 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)) |
| 45 | } |
| 46 | |
| 47 | // LoadVerifierWithOpts returns a signature.Verifier based on the algorithm of the public key |
| 48 | // provided that will use the hash function specified when computing digests. |
| 49 | func LoadVerifierWithOptsscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadVerifierWithOpts().(publicKey cryptoscip-go gomod github.com/golang/go/src go1.25.0 crypto/.PublicKeyscip-go gomod github.com/golang/go/src go1.25.0 crypto/PublicKey#, opts ...LoadOptionscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadOption#) (Verifierscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/Verifier#, error) { |
| 50 | 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# |
| 51 | var useED25519ph bool |
| 52 | 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. |
| 53 | for _, o := range opts { |
| 54 | o.ApplyED25519phscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadOption#ApplyED25519ph.(&useED25519ph) |
| 55 | o.ApplyHashscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadOption#ApplyHash.(&hashFunc) |
| 56 | o.ApplyRSAPSSscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadOption#ApplyRSAPSS.(&rsaPSSOptions) |
| 57 | } |
| 58 | |
| 59 | switch pk := publicKey.(type) { |
| 60 | case *rsascip-go gomod github.com/golang/go/src go1.25.0 `crypto/rsa`/.PublicKeyscip-go gomod github.com/golang/go/src go1.25.0 `crypto/rsa`/PublicKey#: |
| 61 | if rsaPSSOptions != nil { |
| 62 | return LoadRSAPSSVerifierscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadRSAPSSVerifier().(pk, hashFunc, rsaPSSOptions) |
| 63 | } |
| 64 | return LoadRSAPKCS1v15Verifierscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadRSAPKCS1v15Verifier().(pk, hashFunc) |
| 65 | case *ecdsascip-go gomod github.com/golang/go/src go1.25.0 `crypto/ecdsa`/.PublicKeyscip-go gomod github.com/golang/go/src go1.25.0 `crypto/ecdsa`/PublicKey#: |
| 66 | return LoadECDSAVerifierscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadECDSAVerifier().(pk, hashFunc) |
| 67 | case ed25519scip-go gomod github.com/golang/go/src go1.25.0 `crypto/ed25519`/.PublicKeyscip-go gomod github.com/golang/go/src go1.25.0 `crypto/ed25519`/PublicKey#: |
| 68 | if useED25519ph { |
| 69 | return LoadED25519phVerifierscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadED25519phVerifier().(pk) |
| 70 | } |
| 71 | return LoadED25519Verifierscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadED25519Verifier().(pk) |
| 72 | } |
| 73 | 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") |
| 74 | } |
| 75 | |
| 76 | // LoadUnsafeVerifier returns a signature.Verifier based on the algorithm of the public key |
| 77 | // provided that will use SHA1 when computing digests for RSA and ECDSA signatures. |
| 78 | // |
| 79 | // If publicKey is an RSA key, a RSAPKCS1v15Verifier will be returned. If a |
| 80 | // RSAPSSVerifier is desired instead, use the LoadRSAPSSVerifier() method directly. |
| 81 | func LoadUnsafeVerifierscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadUnsafeVerifier().(publicKey cryptoscip-go gomod github.com/golang/go/src go1.25.0 crypto/.PublicKeyscip-go gomod github.com/golang/go/src go1.25.0 crypto/PublicKey#) (Verifierscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/Verifier#, error) { |
| 82 | switch pk := publicKey.(type) { |
| 83 | case *rsascip-go gomod github.com/golang/go/src go1.25.0 `crypto/rsa`/.PublicKeyscip-go gomod github.com/golang/go/src go1.25.0 `crypto/rsa`/PublicKey#: |
| 84 | if pk == nil { |
| 85 | 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().("invalid RSA public key specified") |
| 86 | } |
| 87 | return &RSAPKCS1v15Verifierscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/RSAPKCS1v15Verifier#{ |
| 88 | publicKeyscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/RSAPKCS1v15Verifier#publicKey.: pk, |
| 89 | hashFuncscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/RSAPKCS1v15Verifier#hashFunc.: cryptoscip-go gomod github.com/golang/go/src go1.25.0 crypto/.SHA1scip-go gomod github.com/golang/go/src go1.25.0 crypto/SHA1., |
| 90 | }, nil |
| 91 | case *ecdsascip-go gomod github.com/golang/go/src go1.25.0 `crypto/ecdsa`/.PublicKeyscip-go gomod github.com/golang/go/src go1.25.0 `crypto/ecdsa`/PublicKey#: |
| 92 | if pk == nil { |
| 93 | 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().("invalid ECDSA public key specified") |
| 94 | } |
| 95 | return &ECDSAVerifierscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/ECDSAVerifier#{ |
| 96 | publicKeyscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/ECDSAVerifier#publicKey.: pk, |
| 97 | hashFuncscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/ECDSAVerifier#hashFunc.: cryptoscip-go gomod github.com/golang/go/src go1.25.0 crypto/.SHA1scip-go gomod github.com/golang/go/src go1.25.0 crypto/SHA1., |
| 98 | }, nil |
| 99 | case ed25519scip-go gomod github.com/golang/go/src go1.25.0 `crypto/ed25519`/.PublicKeyscip-go gomod github.com/golang/go/src go1.25.0 `crypto/ed25519`/PublicKey#: |
| 100 | return LoadED25519Verifierscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadED25519Verifier().(pk) |
| 101 | } |
| 102 | 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") |
| 103 | } |
| 104 | |
| 105 | // LoadVerifierFromPEMFile returns a signature.Verifier based on the contents of a |
| 106 | // file located at path. The Verifier wil use the hash function specified when computing digests. |
| 107 | // |
| 108 | // If the publickey is an RSA key, a RSAPKCS1v15Verifier will be returned. If a |
| 109 | // RSAPSSVerifier is desired instead, use the LoadRSAPSSVerifier() and cryptoutils.UnmarshalPEMToPublicKey() methods directly. |
| 110 | func LoadVerifierFromPEMFilescip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadVerifierFromPEMFile().(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#) (Verifierscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/Verifier#, error) { |
| 111 | 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)) |
| 112 | if err != nil { |
| 113 | return nil, err |
| 114 | } |
| 115 | |
| 116 | pubKey, err := cryptoutilsscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/cryptoutils`/.UnmarshalPEMToPublicKeyscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/cryptoutils`/UnmarshalPEMToPublicKey().(fileBytes) |
| 117 | if err != nil { |
| 118 | return nil, err |
| 119 | } |
| 120 | |
| 121 | return LoadVerifierscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadVerifier().(pubKey, hashFunc) |
| 122 | } |
| 123 | |
| 124 | // LoadVerifierFromPEMFileWithOpts returns a signature.Verifier based on the contents of a |
| 125 | // file located at path. The Verifier wil use the hash function specified in the options when computing digests. |
| 126 | func LoadVerifierFromPEMFileWithOptsscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadVerifierFromPEMFileWithOpts().(path string, opts ...LoadOptionscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadOption#) (Verifierscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/Verifier#, error) { |
| 127 | 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)) |
| 128 | if err != nil { |
| 129 | return nil, err |
| 130 | } |
| 131 | |
| 132 | pubKey, err := cryptoutilsscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/cryptoutils`/.UnmarshalPEMToPublicKeyscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/cryptoutils`/UnmarshalPEMToPublicKey().(fileBytes) |
| 133 | if err != nil { |
| 134 | return nil, err |
| 135 | } |
| 136 | |
| 137 | return LoadVerifierWithOptsscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadVerifierWithOpts().(pubKey, opts...) |
| 138 | } |
| 139 | |
| 140 | // LoadDefaultVerifier returns a signature.Verifier based on the public key. |
| 141 | // Each public key has a corresponding PublicKeyDetails associated in the |
| 142 | // Sigstore ecosystem, see Algorithm Registry for more details. |
| 143 | func LoadDefaultVerifierscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadDefaultVerifier().(publicKey cryptoscip-go gomod github.com/golang/go/src go1.25.0 crypto/.PublicKeyscip-go gomod github.com/golang/go/src go1.25.0 crypto/PublicKey#, opts ...LoadOptionscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadOption#) (Verifierscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/Verifier#, error) { |
| 144 | algorithmDetails, err := GetDefaultAlgorithmDetailsscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/GetDefaultAlgorithmDetails().(publicKey, opts...) |
| 145 | if err != nil { |
| 146 | return nil, err |
| 147 | } |
| 148 | return LoadVerifierFromAlgorithmDetailsscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadVerifierFromAlgorithmDetails().(publicKey, algorithmDetails, opts...) |
| 149 | } |
| 150 | |
| 151 | // LoadVerifierFromAlgorithmDetails returns a signature.Verifier based on |
| 152 | // the algorithm details and the user's choice of options. |
| 153 | func LoadVerifierFromAlgorithmDetailsscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadVerifierFromAlgorithmDetails().(publicKey cryptoscip-go gomod github.com/golang/go/src go1.25.0 crypto/.PublicKeyscip-go gomod github.com/golang/go/src go1.25.0 crypto/PublicKey#, 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#) (Verifierscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/Verifier#, error) { |
| 154 | filteredOpts := GetOptsFromAlgorithmDetailsscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/GetOptsFromAlgorithmDetails().(algorithmDetails, opts...) |
| 155 | return LoadVerifierWithOptsscip-go gomod github.com/sigstore/sigstore . `github.com/sigstore/sigstore/pkg/signature`/LoadVerifierWithOpts().(publicKey, filteredOpts...) |
| 156 | } |