| 1 |
package fulcio |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto" |
| 5 |
"crypto/ecdsa" |
| 6 |
"crypto/elliptic" |
| 7 |
"crypto/rand" |
| 8 |
"crypto/sha256" |
| 9 |
"crypto/x509" |
| 10 |
"fmt" |
| 11 |
"io" |
| 12 |
"net/url" |
| 13 |
|
| 14 |
"github.com/sigstore/fulcio/pkg/api" |
| 15 |
"github.com/sigstore/sigstore/pkg/oauth" |
| 16 |
"github.com/sigstore/sigstore/pkg/oauthflow" |
| 17 |
sigstoreSignature "github.com/sigstore/sigstore/pkg/signature" |
| 18 |
"github.com/sirupsen/logrus" |
| 19 |
"go.podman.io/image/v5/internal/useragent" |
| 20 |
"go.podman.io/image/v5/signature/sigstore/internal" |
| 21 |
"golang.org/x/oauth2" |
| 22 |
) |
| 23 |
|
| 24 |
// setupSignerWithFulcio updates s with a certificate generated by fulcioURL based on oidcIDToken |
| 25 |
func setupSignerWithFulcio(s *internal.SigstoreSigner, fulcioURL *url.URL, oidcIDToken *oauthflow.OIDCIDToken) error { |
| 26 |
// ECDSA-P256 is the only interoperable algorithm per |
| 27 |
// https://github.com/sigstore/cosign/blob/main/specs/SIGNATURE_SPEC.md#signature-schemes . |
| 28 |
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 29 |
if err != nil { |
| 30 |
return fmt.Errorf("generating short-term private key: %w", err) |
| 31 |
} |
| 32 |
keyAlgorithm := "ecdsa" |
| 33 |
// SHA-256 is opencontainers/go-digest.Canonical, thus the algorithm to use here as well per |
| 34 |
// https://github.com/sigstore/cosign/blob/main/specs/SIGNATURE_SPEC.md#hashing-algorithms |
| 35 |
signer, err := sigstoreSignature.LoadECDSASigner(privateKey, crypto.SHA256) |
| 36 |
if err != nil { |
| 37 |
return fmt.Errorf("initializing short-term private key: %w", err) |
| 38 |
} |
| 39 |
s.PrivateKey = signer |
| 40 |
|
| 41 |
logrus.Debugf("Requesting a certificate from Fulcio at %s", fulcioURL.Redacted()) |
| 42 |
fulcioClient := api.NewClient(fulcioURL, api.WithUserAgent(useragent.DefaultUserAgent)) |
| 43 |
// Sign the email address as part of the request |
| 44 |
h := sha256.Sum256([]byte(oidcIDToken.Subject)) |
| 45 |
keyOwnershipProof, err := ecdsa.SignASN1(rand.Reader, privateKey, h[:]) |
| 46 |
if err != nil { |
| 47 |
return fmt.Errorf("Error signing key ownership proof: %w", err) |
| 48 |
} |
| 49 |
publicKeyBytes, err := x509.MarshalPKIXPublicKey(&privateKey.PublicKey) |
| 50 |
if err != nil { |
| 51 |
return fmt.Errorf("converting public key to ASN.1: %w", err) |
| 52 |
} |
| 53 |
// Note that unlike most OAuth2 uses, this passes the ID token, not an access token. |
| 54 |
// This is only secure if every Fulcio server has an individual client ID value |
| 55 |
// = fulcioOIDCClientID, distinct from other Fulcio servers, |
| 56 |
// that is embedded into the ID token’s "aud" field. |
| 57 |
resp, err := fulcioClient.SigningCert(api.CertificateRequest{ |
| 58 |
PublicKey: api.Key{ |
| 59 |
Content: publicKeyBytes, |
| 60 |
Algorithm: keyAlgorithm, |
| 61 |
}, |
| 62 |
SignedEmailAddress: keyOwnershipProof, |
| 63 |
}, oidcIDToken.RawString) |
| 64 |
if err != nil { |
| 65 |
return fmt.Errorf("obtaining certificate from Fulcio: %w", err) |
| 66 |
} |
| 67 |
s.FulcioGeneratedCertificate = resp.CertPEM |
| 68 |
s.FulcioGeneratedCertificateChain = resp.ChainPEM |
| 69 |
// Cosign goes through an unmarshal/marshal roundtrip for Fulcio-generated certificates, let’s not do that. |
| 70 |
s.SigningKeyOrCert = resp.CertPEM |
| 71 |
return nil |
| 72 |
} |
| 73 |
|
| 74 |
// WithFulcioAndPreexistingOIDCIDToken sets up signing to use a short-lived key and a Fulcio-issued certificate |
| 75 |
// based on a caller-provided OIDC ID token. |
| 76 |
func WithFulcioAndPreexistingOIDCIDToken(fulcioURL *url.URL, oidcIDToken string) internal.Option { |
| 77 |
return func(s *internal.SigstoreSigner) error { |
| 78 |
if s.PrivateKey != nil { |
| 79 |
return fmt.Errorf("multiple private key sources specified when preparing to create sigstore signatures") |
| 80 |
} |
| 81 |
|
| 82 |
// This adds dependencies even just to parse the token. We could possibly reimplement that, and split this variant |
| 83 |
// into a subpackage without the OIDC dependencies… but really, is this going to be used in significantly different situations |
| 84 |
// than the two interactive OIDC authentication workflows? |
| 85 |
// |
| 86 |
// Are there any widely used tools to manually obtain an ID token? Why would there be? |
| 87 |
// For long-term usage, users provisioning a static OIDC credential might just as well provision an already-generated certificate |
| 88 |
// or something like that. |
| 89 |
logrus.Debugf("Using a statically-provided OIDC token") |
| 90 |
staticTokenGetter := oauthflow.StaticTokenGetter{RawToken: oidcIDToken} |
| 91 |
oidcIDToken, err := staticTokenGetter.GetIDToken(nil, oauth2.Config{}) |
| 92 |
if err != nil { |
| 93 |
return fmt.Errorf("parsing OIDC token: %w", err) |
| 94 |
} |
| 95 |
|
| 96 |
return setupSignerWithFulcio(s, fulcioURL, oidcIDToken) |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
// WithFulcioAndDeviceAuthorizationGrantOIDC sets up signing to use a short-lived key and a Fulcio-issued certificate |
| 101 |
// based on an OIDC ID token obtained using a device authorization grant (RFC 8628). |
| 102 |
// |
| 103 |
// interactiveOutput must be directly accessible to a human user in real time (i.e. not be just a log file). |
| 104 |
func WithFulcioAndDeviceAuthorizationGrantOIDC(fulcioURL *url.URL, oidcIssuerURL *url.URL, oidcClientID, oidcClientSecret string, |
| 105 |
interactiveOutput io.Writer) internal.Option { |
| 106 |
return func(s *internal.SigstoreSigner) error { |
| 107 |
if s.PrivateKey != nil { |
| 108 |
return fmt.Errorf("multiple private key sources specified when preparing to create sigstore signatures") |
| 109 |
} |
| 110 |
|
| 111 |
logrus.Debugf("Starting OIDC device flow for issuer %s", oidcIssuerURL.Redacted()) |
| 112 |
tokenGetter := oauthflow.NewDeviceFlowTokenGetterForIssuer(oidcIssuerURL.String()) |
| 113 |
tokenGetter.MessagePrinter = func(s string) { |
| 114 |
fmt.Fprintln(interactiveOutput, s) |
| 115 |
} |
| 116 |
oidcIDToken, err := oauthflow.OIDConnect(oidcIssuerURL.String(), oidcClientID, oidcClientSecret, "", tokenGetter) |
| 117 |
if err != nil { |
| 118 |
return fmt.Errorf("Error authenticating with OIDC: %w", err) |
| 119 |
} |
| 120 |
|
| 121 |
return setupSignerWithFulcio(s, fulcioURL, oidcIDToken) |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
// WithFulcioAndInterativeOIDC sets up signing to use a short-lived key and a Fulcio-issued certificate |
| 126 |
// based on an interactively-obtained OIDC ID token. |
| 127 |
// The token is obtained |
| 128 |
// - directly using a browser, listening on localhost, automatically opening a browser to the OIDC issuer, |
| 129 |
// to be redirected on localhost. (I.e. the current environment must allow launching a browser that connect back to the current process; |
| 130 |
// either or both may be impossible in a container or a remote VM). |
| 131 |
// - or by instructing the user to manually open a browser, obtain the OIDC code, and interactively input it as text. |
| 132 |
// |
| 133 |
// interactiveInput and interactiveOutput must both be directly operable by a human user in real time (i.e. not be just a log file). |
| 134 |
func WithFulcioAndInteractiveOIDC(fulcioURL *url.URL, oidcIssuerURL *url.URL, oidcClientID, oidcClientSecret string, |
| 135 |
interactiveInput io.Reader, interactiveOutput io.Writer) internal.Option { |
| 136 |
return func(s *internal.SigstoreSigner) error { |
| 137 |
if s.PrivateKey != nil { |
| 138 |
return fmt.Errorf("multiple private key sources specified when preparing to create sigstore signatures") |
| 139 |
} |
| 140 |
|
| 141 |
logrus.Debugf("Starting interactive OIDC authentication for issuer %s", oidcIssuerURL.Redacted()) |
| 142 |
// This is intended to match oauthflow.DefaultIDTokenGetter (incl. the update in init()), overriding only input/output |
| 143 |
htmlPage, err := oauth.GetInteractiveSuccessHTML(false, 10) |
| 144 |
if err != nil { |
| 145 |
return fmt.Errorf("formatting HTML content: %w", err) |
| 146 |
} |
| 147 |
tokenGetter := &oauthflow.InteractiveIDTokenGetter{ |
| 148 |
HTMLPage: htmlPage, |
| 149 |
Input: interactiveInput, |
| 150 |
Output: interactiveOutput, |
| 151 |
} |
| 152 |
oidcIDToken, err := oauthflow.OIDConnect(oidcIssuerURL.String(), oidcClientID, oidcClientSecret, "", tokenGetter) |
| 153 |
if err != nil { |
| 154 |
return fmt.Errorf("Error authenticating with OIDC: %w", err) |
| 155 |
} |
| 156 |
|
| 157 |
return setupSignerWithFulcio(s, fulcioURL, oidcIDToken) |
| 158 |
} |
| 159 |
} |