Skip to content

Commit 806a644

Browse files
committed
update cert gen
1 parent aa64b10 commit 806a644

File tree

4 files changed

+27
-16
lines changed

4 files changed

+27
-16
lines changed

cmd/app/main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ func main() {
2828
log.Fatalf("App init error: %s", err)
2929
}
3030

31+
// root, privateKey, err := certificates.GenerateRootCertificate(true, cfg.CommonName, "US", "open-amt-cloud-toolkit", true)
32+
// if err != nil {
33+
// log.Fatalf("Error generating root certificate: %s", err)
34+
// }
35+
// certificates.IssueWebServerCertificate(certificates.CertAndKeyType{Cert: root, Key: privateKey}, false, cfg.CommonName, "US", "open-amt-cloud-toolkit", true)
36+
3137
if os.Getenv("GIN_MODE") != "debug" {
3238
go func() {
3339
browserError := openBrowser("http://localhost:"+cfg.HTTP.Port, runtime.GOOS)

config/config.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ type (
1515

1616
// App -.
1717
App struct {
18-
Name string `env-required:"false" yaml:"name" env:"APP_NAME"`
19-
Repo string `env-required:"false" yaml:"repo" env:"APP_REPO"`
20-
Version string `env-required:"false"`
18+
Name string `env-required:"false" yaml:"name" env:"APP_NAME"`
19+
Repo string `env-required:"false" yaml:"repo" env:"APP_REPO"`
20+
Version string `env-required:"false"`
21+
CommonName string `env-required:"false" yaml:"common_name" env:"APP_COMMON_NAME"`
2122
}
2223

2324
// HTTP -.
@@ -45,9 +46,10 @@ func NewConfig() (*Config, error) {
4546
// set defaults
4647
cfg := &Config{
4748
App: App{
48-
Name: "console",
49-
Repo: "open-amt-cloud-toolkit/console",
50-
Version: "DEVELOPMENT",
49+
Name: "console",
50+
Repo: "open-amt-cloud-toolkit/console",
51+
Version: "DEVELOPMENT",
52+
CommonName: "localhost",
5153
},
5254
HTTP: HTTP{
5355
Host: "localhost",

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.22.0
44

55
toolchain go1.23.1
66

7-
//replace github.com/open-amt-cloud-toolkit/go-wsman-messages/v2 => ../go-wsman-messages
7+
replace github.com/open-amt-cloud-toolkit/go-wsman-messages/v2 => ../go-wsman-messages
88

99
require (
1010
github.com/Masterminds/squirrel v1.5.4

internal/certificates/generate.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"crypto/x509"
88
"crypto/x509/pkix"
99
"encoding/pem"
10+
"fmt"
1011
"math/big"
1112
"net/url"
1213
"os"
@@ -38,7 +39,7 @@ func GenerateRootCertificate(addThumbPrintToName bool, commonName, country, orga
3839
if addThumbPrintToName {
3940
hash := sha256.New()
4041
hash.Write(privateKey.PublicKey.N.Bytes()) // Simplified approach to get a thumbprint-like result
41-
commonName += "-" + string(hash.Sum(nil)[:3])
42+
commonName += "-" + fmt.Sprintf("%x", hash.Sum(nil)[:3])
4243
}
4344

4445
if country == "" {
@@ -107,7 +108,7 @@ type CertAndKeyType struct {
107108
Key *rsa.PrivateKey
108109
}
109110

110-
func IssueWebServerCertificate(rootCert CertAndKeyType, addThumbPrintToName bool, commonName, country, organization string, extKeyUsage x509.ExtKeyUsage, strong bool) (*x509.Certificate, *rsa.PrivateKey, error) {
111+
func IssueWebServerCertificate(rootCert CertAndKeyType, addThumbPrintToName bool, commonName, country, organization string, strong bool) (*x509.Certificate, *rsa.PrivateKey, error) {
111112
keyLength := 2048
112113
if strong {
113114
keyLength = 3072
@@ -148,16 +149,18 @@ func IssueWebServerCertificate(rootCert CertAndKeyType, addThumbPrintToName bool
148149
subject.CommonName += "-" + string(hash.Sum(nil)[:3])
149150
}
150151

151-
template := x509.Certificate{
152-
SerialNumber: serialNumber,
153-
Subject: subject,
154-
NotBefore: notBefore,
155-
NotAfter: notAfter,
156-
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment,
157-
ExtKeyUsage: []x509.ExtKeyUsage{extKeyUsage},
152+
hash := sha256.Sum256(keys.PublicKey.N.Bytes())
158153

154+
template := x509.Certificate{
155+
SerialNumber: serialNumber,
156+
Subject: subject,
157+
NotBefore: notBefore,
158+
NotAfter: notAfter,
159+
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment | x509.KeyUsageCertSign | x509.KeyUsageDataEncipherment,
160+
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
159161
BasicConstraintsValid: true,
160162
IsCA: false,
163+
SubjectKeyId: hash[:],
161164
}
162165

163166
// Subject Alternative Name

0 commit comments

Comments
 (0)