1 parent
bf50da525e
commit
39c27b0de3
9 files changed
+386
-115
No files matched your search
@@ -0,0 +1,50 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func CheckOrCreateRootKeyCertificate() error {
|
||||
_, err := os.Stat(fmt.Sprintf("%s/%s", os.Getenv("CERTS_PATH"), "root"))
|
||||
if err == nil {
|
||||
_, err := os.Stat(fmt.Sprintf("%s/%s", os.Getenv("CERTS_PATH"), "root/root.key"))
|
||||
if err == nil {
|
||||
_, err := os.Stat(fmt.Sprintf("%s/%s", os.Getenv("CERTS_PATH"), "root/root.crt"))
|
||||
if err == nil {
|
||||
return fmt.Errorf("%s", "key already exists")
|
||||
} else if os.IsNotExist(err) {
|
||||
os.RemoveAll(fmt.Sprintf("%s/%s", os.Getenv("CERTS_PATH"), "root"))
|
||||
err = CreateRootKeyCertificate()
|
||||
if err != nil {
|
||||
fmt.Println("error creating root key certificate:", err)
|
||||
return fmt.Errorf("%s", "error creating root key certificate")
|
||||
}
|
||||
} else {
|
||||
fmt.Println("error checking certificate directory:", err)
|
||||
return fmt.Errorf("%s", "error checking certificate directory")
|
||||
}
|
||||
} else if os.IsNotExist(err) {
|
||||
os.RemoveAll(fmt.Sprintf("%s/%s", os.Getenv("CERTS_PATH"), "root"))
|
||||
err = CreateRootKeyCertificate()
|
||||
if err != nil {
|
||||
fmt.Println("error creating root key certificate:", err)
|
||||
return fmt.Errorf("%s", "error creating root key certificate")
|
||||
}
|
||||
} else {
|
||||
fmt.Println("error checking certificate directory:", err)
|
||||
return fmt.Errorf("%s", "error checking certificate directory")
|
||||
}
|
||||
} else if os.IsNotExist(err) {
|
||||
os.RemoveAll(fmt.Sprintf("%s/%s", os.Getenv("CERTS_PATH"), "root"))
|
||||
err = CreateRootKeyCertificate()
|
||||
if err != nil {
|
||||
fmt.Println("error creating root key certificate:", err)
|
||||
return fmt.Errorf("%s", "error creating root key certificate")
|
||||
}
|
||||
} else {
|
||||
fmt.Println("error checking certificate directory:", err)
|
||||
return fmt.Errorf("%s", "error checking certificate directory")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/x509"
|
||||
"crypto/x509/pkix"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/youmark/pkcs8"
|
||||
)
|
||||
|
||||
func CreateRootKeyCertificate() error {
|
||||
privkey, err := rsa.GenerateKey(rand.Reader, 4096)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
serialNumber, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
template := x509.Certificate{
|
||||
SerialNumber: serialNumber,
|
||||
Subject: pkix.Name{
|
||||
CommonName: "Root Certificate",
|
||||
OrganizationalUnit: []string{"Organizational Unit"},
|
||||
Organization: []string{"Organization"},
|
||||
Country: []string{"Country"},
|
||||
Locality: []string{"Locality"},
|
||||
Province: []string{"Province"},
|
||||
StreetAddress: []string{"Street Address"},
|
||||
PostalCode: []string{"Postal Code"},
|
||||
SerialNumber: serialNumber.String(),
|
||||
},
|
||||
NotBefore: time.Now(),
|
||||
NotAfter: time.Now().AddDate(10, 0, 0),
|
||||
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign | x509.KeyUsageDigitalSignature,
|
||||
ExtKeyUsage: []x509.ExtKeyUsage{
|
||||
x509.ExtKeyUsageServerAuth,
|
||||
x509.ExtKeyUsageClientAuth,
|
||||
},
|
||||
BasicConstraintsValid: true,
|
||||
IsCA: true,
|
||||
}
|
||||
|
||||
cert, err := x509.CreateCertificate(rand.Reader, &template, &template, &privkey.PublicKey, privkey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
encryptedKey, err := pkcs8.ConvertPrivateKeyToPKCS8(
|
||||
privkey,
|
||||
[]byte("Simba28082020"),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = CheckExistsOrCreateDir("root")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = SavePEMFile(fmt.Sprintf(
|
||||
"%s/%s",
|
||||
os.Getenv("CERTS_PATH"),
|
||||
"root/root.crt",
|
||||
),
|
||||
"CERTIFICATE",
|
||||
cert,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = SavePEMFile(fmt.Sprintf(
|
||||
"%s/%s",
|
||||
os.Getenv("CERTS_PATH"),
|
||||
"root/root.key",
|
||||
), "ENCRYPTED PRIVATE KEY", encryptedKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Implementation of CreateRootCertificate function
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user