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

update: updated cert_gen to generate self-signed certificate #380

Merged
merged 3 commits into from
Oct 12, 2022
Merged
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
32 changes: 9 additions & 23 deletions cmd/notation/cert_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,12 @@ func generateTestCert(opts *certGenerateTestOpts) error {
return err
}

// generate self-created certificate chain
rsaRootCertTuple, rootBytes, err := generateTestRootCert(hosts, bits)
// generate self-signed certificate
rsaCertTuple, certBytes, err := generateSelfSignedCert(key, hosts[0])
if err != nil {
return err
}
rsaLeafCertTuple, leafBytes, err := generateTestLeafCert(&rsaRootCertTuple, key, hosts)
if err != nil {
return err
}
fmt.Println("generated certificates expiring on", rsaLeafCertTuple.Cert.NotAfter.Format(time.RFC3339))
fmt.Println("generated certificate expiring on", rsaCertTuple.Cert.NotAfter.Format(time.RFC3339))

// write private key
keyPath, certPath := dir.Path.Localkey(name)
Expand All @@ -49,8 +45,8 @@ func generateTestCert(opts *certGenerateTestOpts) error {
}
fmt.Println("wrote key:", keyPath)

// write self-signed certificate
if err := osutil.WriteFileWithPermission(certPath, append(leafBytes, rootBytes...), 0644, false); err != nil {
// write the self-signed certificate
if err := osutil.WriteFileWithPermission(certPath, certBytes, 0644, false); err != nil {
return fmt.Errorf("failed to write certificate file: %v", err)
}
fmt.Println("wrote certificate:", certPath)
Expand Down Expand Up @@ -117,18 +113,8 @@ func generateCertPEM(rsaCertTuple *testhelper.RSACertTuple) []byte {
return pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: rsaCertTuple.Cert.Raw})
}

// generateTestRootCert generates a self-signed root certificate
func generateTestRootCert(hosts []string, bits int) (testhelper.RSACertTuple, []byte, error) {
priv, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return testhelper.RSACertTuple{}, nil, fmt.Errorf("failed to generate root key: %v", err)
}
rsaRootCertTuple := testhelper.GetRSACertTupleWithPK(priv, hosts[0]+" CA", nil)
return rsaRootCertTuple, generateCertPEM(&rsaRootCertTuple), nil
}

// generateTestLeafCert generates the leaf certificate
func generateTestLeafCert(rsaRootCertTuple *testhelper.RSACertTuple, privateKey *rsa.PrivateKey, hosts []string) (testhelper.RSACertTuple, []byte, error) {
rsaLeafCertTuple := testhelper.GetRSACertTupleWithPK(privateKey, hosts[0], rsaRootCertTuple)
return rsaLeafCertTuple, generateCertPEM(&rsaLeafCertTuple), nil
// generateTestCert generates a self-signed non-CA certificate
func generateSelfSignedCert(privateKey *rsa.PrivateKey, host string) (testhelper.RSACertTuple, []byte, error) {
rsaCertTuple := testhelper.GetRSASelfSignedCertTupleWithPK(privateKey, host)
return rsaCertTuple, generateCertPEM(&rsaCertTuple), nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/distribution/distribution/v3 v3.0.0-20220729163034-26163d82560f
github.com/docker/docker-credential-helpers v0.7.0
github.com/notaryproject/notation-core-go v0.1.0-alpha.4
github.com/notaryproject/notation-go v0.10.0-alpha.3.0.20221009143310-e2ae1fecf077
github.com/notaryproject/notation-go v0.11.0-alpha.4
github.com/opencontainers/go-digest v1.0.0
github.com/spf13/cobra v1.5.0
github.com/spf13/pflag v1.0.5
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NH
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/notaryproject/notation-core-go v0.1.0-alpha.4 h1:0OhA2PjwT0TAouHOrU4K+8H9YM6E/e4/ocoq+JiHeOw=
github.com/notaryproject/notation-core-go v0.1.0-alpha.4/go.mod h1:s8DZptmN1rZS0tBLTPt/w+d4o6eAcGWTYYJlXaJhQ4U=
github.com/notaryproject/notation-go v0.10.0-alpha.3.0.20221009143310-e2ae1fecf077 h1:LshczxRKk5d/5ZOVr/qG0aZr9bu6CVYmdLgSTzTGG8w=
github.com/notaryproject/notation-go v0.10.0-alpha.3.0.20221009143310-e2ae1fecf077/go.mod h1:26t4uYh4bkLWMjtG/eakOWIRpqZiF7fMCs55q0WGwTE=
github.com/notaryproject/notation-go v0.11.0-alpha.4 h1:PNptLtrhW0jyw10hUWU+KNzvzeuBBZmg+/1IUaGYE10=
github.com/notaryproject/notation-go v0.11.0-alpha.4/go.mod h1:4xYTcW4wfsXkXw3piUA53uSW82RwdXyipSEtiiRVrCw=
github.com/opencontainers/distribution-spec/specs-go v0.0.0-20220620172159-4ab4752c3b86 h1:Oumw+lPnO8qNLTY2mrqPJZMoGExLi/0h/DdikoLTXVU=
github.com/opencontainers/distribution-spec/specs-go v0.0.0-20220620172159-4ab4752c3b86/go.mod h1:aA4vdXRS8E1TG7pLZOz85InHi3BiPdErh8IpJN6E0x4=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
Expand Down