-
Notifications
You must be signed in to change notification settings - Fork 40
feat(keyless): signing use case #859
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| // | ||
| // Copyright 2024 The Chainloop 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 biz | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto" | ||
| "crypto/x509" | ||
| "crypto/x509/pkix" | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "github.com/sigstore/fulcio/pkg/ca" | ||
| "github.com/sigstore/fulcio/pkg/identity" | ||
| "github.com/sigstore/sigstore/pkg/cryptoutils" | ||
| ) | ||
|
|
||
| type SigningUseCase struct { | ||
| CA ca.CertificateAuthority | ||
| } | ||
|
|
||
| func NewChainloopSigningUseCase(ca ca.CertificateAuthority) *SigningUseCase { | ||
| return &SigningUseCase{CA: ca} | ||
| } | ||
|
|
||
| // CreateSigningCert signs a certificate request with a configured CA, and returns the full certificate chain | ||
| func (s *SigningUseCase) CreateSigningCert(ctx context.Context, orgID string, csrRaw []byte) ([]string, error) { | ||
| var publicKey crypto.PublicKey | ||
|
|
||
| if len(csrRaw) == 0 { | ||
| return nil, errors.New("csr cannot be empty") | ||
| } | ||
|
|
||
| // Parse CSR | ||
| csr, err := cryptoutils.ParseCSR(csrRaw) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("parsing csr: %w", err) | ||
| } | ||
|
|
||
| // Parse public key and check for weak key parameters | ||
| publicKey = csr.PublicKey | ||
| if err := cryptoutils.ValidatePubKey(publicKey); err != nil { | ||
| return nil, fmt.Errorf("invalid public key: %w", err) | ||
| } | ||
|
|
||
| // Check the CSR signature is valid | ||
| if err := csr.CheckSignature(); err != nil { | ||
| return nil, fmt.Errorf("invalid signature: %w", err) | ||
| } | ||
|
|
||
| // Create certificate from CA provider (no Signed Certificate Timestamps for now) | ||
| csc, err := s.CA.CreateCertificate(ctx, newChainloopPrincipal(orgID), publicKey) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("creating certificate: %w", err) | ||
| } | ||
|
|
||
| // Generated certificate | ||
| finalPEM, err := csc.CertPEM() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("marshaling certificate to PEM: %w", err) | ||
| } | ||
|
|
||
| // Certificate chain | ||
| finalChainPEM, err := csc.ChainPEM() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("marshaling chain to PEM: %w", err) | ||
| } | ||
|
|
||
| return append([]string{finalPEM}, finalChainPEM...), nil | ||
| } | ||
|
|
||
| type chainloopPrincipal struct { | ||
| orgID string | ||
| } | ||
|
|
||
| var _ identity.Principal = (*chainloopPrincipal)(nil) | ||
|
|
||
| func newChainloopPrincipal(orgID string) *chainloopPrincipal { | ||
| return &chainloopPrincipal{orgID: orgID} | ||
| } | ||
|
|
||
| func (p *chainloopPrincipal) Name(_ context.Context) string { | ||
| return p.orgID | ||
| } | ||
|
|
||
| func (p *chainloopPrincipal) Embed(_ context.Context, cert *x509.Certificate) error { | ||
| // no op. | ||
| // TODO: Chainloop might have their own private enterprise number with the Internet Assigned Numbers Authority | ||
| // to embed its own identity information in the resulting certificate | ||
| cert.Subject = pkix.Name{Organization: []string{p.orgID}} | ||
|
|
||
| return nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| // | ||
| // Copyright 2024 The Chainloop 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 biz_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/ecdsa" | ||
| "crypto/elliptic" | ||
| "crypto/rand" | ||
| "crypto/x509" | ||
| "crypto/x509/pkix" | ||
| "encoding/pem" | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/chainloop-dev/chainloop/app/controlplane/internal/biz" | ||
| "github.com/sigstore/fulcio/pkg/ca/ephemeralca" | ||
| "github.com/sigstore/sigstore/pkg/cryptoutils" | ||
| "github.com/stretchr/testify/suite" | ||
| ) | ||
|
|
||
| type signingUseCaseTestSuite struct { | ||
| suite.Suite | ||
| uc *biz.SigningUseCase | ||
| csr []byte | ||
| } | ||
|
|
||
| func (s *signingUseCaseTestSuite) TestSigningUseCase_CreateSigningCert() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick: I think this is smth I asked @javirln once, but is using
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's the suggestion from the IDE, I believe it's some "idiomatic", but I don't think there is a written rule. |
||
| s.Run("with empty certificate", func() { | ||
| _, err := s.uc.CreateSigningCert(context.TODO(), "myorgid", make([]byte, 0)) | ||
| s.Error(err) | ||
| }) | ||
|
|
||
| s.Run("with certificate request", func() { | ||
| certChain, err := s.uc.CreateSigningCert(context.TODO(), "myorgid", s.csr) | ||
| s.NoError(err) | ||
|
|
||
| // assert 2 certificates: signing certificate + chain (only one) | ||
| s.Len(certChain, 2) | ||
|
|
||
| // check cert contents | ||
| cert, err := cryptoutils.UnmarshalCertificatesFromPEM([]byte(certChain[0])) | ||
| s.NoError(err) | ||
| s.Len(cert, 1) | ||
| s.Equal("myorgid", cert[0].Subject.Organization[0]) | ||
| }) | ||
| } | ||
|
|
||
| func TestSuite(t *testing.T) { | ||
| suite.Run(t, new(signingUseCaseTestSuite)) | ||
| } | ||
|
|
||
| func (s *signingUseCaseTestSuite) SetupTest() { | ||
| csr, err := createCSR() | ||
| s.Require().NoError(err) | ||
| s.csr = csr | ||
|
|
||
| ca, err := ephemeralca.NewEphemeralCA() | ||
| s.Require().NoError(err) | ||
| s.uc = &biz.SigningUseCase{CA: ca} | ||
| } | ||
|
|
||
| func createCSR() ([]byte, error) { | ||
| priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("generating cert: %w", err) | ||
jiparis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| csrTmpl := &x509.CertificateRequest{Subject: pkix.Name{CommonName: "ephemeral certificate"}} | ||
| derCSR, err := x509.CreateCertificateRequest(rand.Reader, csrTmpl, priv) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("generating certificate request: %w", err) | ||
| } | ||
|
|
||
| // Encode CSR to PEM | ||
| pemCSR := pem.EncodeToMemory(&pem.Block{ | ||
| Type: "CERTIFICATE REQUEST", | ||
| Bytes: derCSR, | ||
| }) | ||
|
|
||
| return pemCSR, nil | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.