forked from chainloop-dev/chainloop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jose I. Paris <[email protected]>
- Loading branch information
Showing
5 changed files
with
231 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// | ||
// 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" | ||
|
||
"github.com/sigstore/fulcio/pkg/ca" | ||
"github.com/sigstore/fulcio/pkg/identity" | ||
"github.com/sigstore/sigstore/pkg/cryptoutils" | ||
) | ||
|
||
type SigningCertCreator interface { | ||
// CreateSigningCert creates a signing certificate from and returns the certificate chain | ||
CreateSigningCert(context.Context, string, []byte) ([]string, error) | ||
} | ||
|
||
type SigningUseCase struct { | ||
CA ca.CertificateAuthority | ||
} | ||
|
||
var _ SigningCertCreator = (*SigningUseCase)(nil) | ||
|
||
func NewChainloopSigningUseCase(ca ca.CertificateAuthority) *SigningUseCase { | ||
return &SigningUseCase{CA: ca} | ||
} | ||
|
||
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, err | ||
} | ||
|
||
// Parse public key and check for weak key parameters | ||
publicKey = csr.PublicKey | ||
if err := cryptoutils.ValidatePubKey(publicKey); err != nil { | ||
return nil, err | ||
} | ||
|
||
// Check the CSR signature is valid | ||
if err := csr.CheckSignature(); err != nil { | ||
return nil, 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, err | ||
} | ||
|
||
// Generated certificate | ||
finalPEM, err := csc.CertPEM() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Certificate chain | ||
finalChainPEM, err := csc.ChainPEM() | ||
if err != nil { | ||
return nil, 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// | ||
// 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() { | ||
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.Assert().Len(certChain, 2) | ||
|
||
// check cert contents | ||
cert, err := cryptoutils.UnmarshalCertificatesFromPEM([]byte(certChain[0])) | ||
s.NoError(err) | ||
s.Assert().Len(cert, 1) | ||
s.Assert().Equal("myorgid", cert[0].Subject.Organization[0]) | ||
}) | ||
} | ||
|
||
func TestSuite(t *testing.T) { | ||
suite.Run(t, new(signingUseCaseTestSuite)) | ||
} | ||
|
||
func (s *signingUseCaseTestSuite) SetupTest() { | ||
csr, err := createCSR() | ||
if err != nil { | ||
panic(err) | ||
} | ||
s.csr = csr | ||
|
||
ca, err := ephemeralca.NewEphemeralCA() | ||
if err != nil { | ||
s.T().Fatal(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) | ||
} | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters