Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] proposal for new oidc and fulcio abstractions #1394

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions internal/pkg/cosign/fulcio/attestor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright 2022 The Sigstore Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package fulcio

import (
"bytes"
"context"
"crypto"
"encoding/base64"
"encoding/json"
"io"

"github.com/secure-systems-lab/go-securesystemslib/dsse"
"github.com/sigstore/cosign/internal/pkg/cosign"
"github.com/sigstore/cosign/internal/pkg/cosign/oidc"
"github.com/sigstore/cosign/pkg/oci"
"github.com/sigstore/cosign/pkg/oci/static"
"github.com/sigstore/cosign/pkg/types"
"github.com/sigstore/sigstore/pkg/signature"
)

type attestor struct {
signer signature.Signer
oidp oidc.Provider
payloadType string
}

var _ cosign.DSSEAttestor = (*attestor)(nil)

// Attest implements `cosign.DSSEAttestor`
func (pa *attestor) DSSEAttest(ctx context.Context, payload io.Reader) (oci.Signature, crypto.PublicKey, error) {
p, err := io.ReadAll(payload)
if err != nil {
return nil, nil, err
}

pb := dsse.PAE(pa.payloadType, p)

sig, err := pa.signer.SignMessage(bytes.NewReader(pb))
if err != nil {
return nil, nil, err
}
pk, err := pa.signer.PublicKey()
if err != nil {
return nil, nil, err
}

envelope := dsse.Envelope{
PayloadType: pa.payloadType,
Payload: base64.StdEncoding.EncodeToString(pb),
Signatures: []dsse.Signature{{
Sig: base64.StdEncoding.EncodeToString(sig),
}},
}

envelopeJSON, err := json.Marshal(envelope)
if err != nil {
return nil, nil, err
}

opts := []static.Option{static.WithLayerMediaType(types.DssePayloadType)}

att, err := static.NewAttestation(envelopeJSON, opts...)
if err != nil {
return nil, nil, err
}

return att, pk, nil
}

// NewDSSEAttestor returns a `cosign.DSSEAttestor`
func NewDSSEAttestor(payloadType string,
s signature.Signer, oidp oidc.Provider) cosign.DSSEAttestor {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does the non-keyless take oidp?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so that one can use a non-ephemeral key, or an ephemeral key generated elsewhere, and associate it with an OID

return &attestor{
signer: s,
payloadType: payloadType,
oidp: oidp,
}
}

func NewKeylessDSSEAttestor(payloadType string, oidp oidc.Provider) cosign.DSSEAttestor {
var s signature.Signer
// create an ephemeral keypair...
return &attestor{
signer: s,
payloadType: payloadType,
oidp: oidp,
}
}
62 changes: 62 additions & 0 deletions internal/pkg/cosign/fulcio/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2022 The Sigstore Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package fulcio

import (
"context"
"crypto/ecdsa"
"crypto/x509"
"errors"
"strings"

"github.com/sigstore/cosign/internal/pkg/cosign/oidc"
fulcioapi "github.com/sigstore/fulcio/pkg/api"
"github.com/sigstore/sigstore/pkg/signature"
)

// *NOTE* This is a higher-level client than exists in `sigstore/fulcio`, should probably live there

// Client is a Fulcio client.
type Client struct {
APIClient fulcioapi.Client
}

// GetCert retrieves a Fulcio certificate which associates the `Signer`'s public key with the ID provided
Comment on lines +29 to +36
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be tremendously helpful if the comments were actually useful in describing what you were trying to accomplish with the higher level client.

It seems like your goal is to capture some of the challenge semantics (e.g. proving we have the private key by signing the subject of the IDToken), but TBQH the objectives of this as a higher level client aren't clear to me at all, which makes it sort of hard to judge good or bad.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair enough. The higher level client I was thinking of uses "real" types, e.g. x509.Certificate instead of serialized []bytes, and performs the boilerplate operations to get the ID token, sign the subject, and retrieve a signing cert from Fulcio.

func (c *Client) GetCert(ctx context.Context, signer signature.Signer, oidp oidc.Provider) (cert *x509.Certificate, chain []*x509.Certificate, sct []byte, err error) {
idToken, err := oidp.GetIDToken(ctx)
// Sign the token's subject
subject := ""
// subject := idToken.Subject
subjectSig, err := signer.SignMessage(strings.NewReader(subject))

pub, err := signer.PublicKey()
if _, isECDSA := pub.(*ecdsa.PublicKey); !isECDSA {
return nil, nil, nil, errors.New("only ESCDA public keys are supported...?")
}
pubBytes, err := x509.MarshalPKIXPublicKey(pub)

req := fulcioapi.CertificateRequest{
PublicKey: fulcioapi.Key{
Algorithm: "ecdsa",
Content: pubBytes,
},
SignedEmailAddress: subjectSig,
}

resp, err := c.APIClient.SigningCert(req, idToken.JWTEncode())
// cert = parsex509(resp.CertPEM)
// chain = parsex509(resp.ChainPEM)
return cert, chain, resp.SCT, nil
}
54 changes: 54 additions & 0 deletions internal/pkg/cosign/oidc/oidc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2022 The Sigstore Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package oidc

import (
"context"

"golang.org/x/oauth2"
)

// *NOTE* Most if not all of this should live in `sigstore/sigstore`

// IDToken is an OpenID ID token.
type IDToken struct { // OR use https://pkg.go.dev/github.com/coreos/go-oidc#IDToken (or something) instead?
// our own impl of https://openid.net/specs/openid-connect-core-1_0.html#IDToken
}

func (t *IDToken) JWTEncode() string {
return "TODO"
}

func JWTDecodeIDToken(rawIDToken string) (*IDToken, error) {
return &IDToken{}, nil
}

// Provider describes an OpenID Provider.
type Provider interface {
// GetIDToken returns an *IDToken
GetIDToken(context.Context) (*IDToken, error)
}

// PotempkinProvider is a make-believe `Provider` implementation to throw rocks at.
// There should be one for each OAuth flow type (https://connect2id.com/learn/openid-connect#auth-request) we want to support.
type PotempkinProvider struct {
oauthConfig oauth2.Config // token endpoints, client_id, client_secret, etc.
}

// GetIDToken implements Provider
func (p *PotempkinProvider) GetIDToken(context.Context) (*IDToken, error) {
// TODO
return &IDToken{}, nil
}