This commit is contained in:
1 parent
39c27b0de3
commit
e6526fb780
8 files changed
+169
-114
No files matched your search
@@ -1,50 +0,0 @@
|
||||
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
|
||||
}
|
||||
@@ -8,12 +8,13 @@ import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
"os"
|
||||
"scm/model"
|
||||
"time"
|
||||
|
||||
"github.com/youmark/pkcs8"
|
||||
)
|
||||
|
||||
func CreateRootKeyCertificate() error {
|
||||
func CreateRootKeyCertificate(data model.CreateRootCertRequest) error {
|
||||
privkey, err := rsa.GenerateKey(rand.Reader, 4096)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -27,18 +28,18 @@ func CreateRootKeyCertificate() error {
|
||||
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"},
|
||||
CommonName: data.CommonName,
|
||||
OrganizationalUnit: data.OrganizationalUnit,
|
||||
Organization: data.Organization,
|
||||
Country: data.Country,
|
||||
Locality: data.Locality,
|
||||
Province: data.Province,
|
||||
StreetAddress: data.StreetAddress,
|
||||
PostalCode: data.PostalCode,
|
||||
SerialNumber: serialNumber.String(),
|
||||
},
|
||||
NotBefore: time.Now(),
|
||||
NotAfter: time.Now().AddDate(10, 0, 0),
|
||||
NotAfter: time.Now().AddDate(data.ValidityYears, 0, 0),
|
||||
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign | x509.KeyUsageDigitalSignature,
|
||||
ExtKeyUsage: []x509.ExtKeyUsage{
|
||||
x509.ExtKeyUsageServerAuth,
|
||||
@@ -55,7 +56,7 @@ func CreateRootKeyCertificate() error {
|
||||
|
||||
encryptedKey, err := pkcs8.ConvertPrivateKeyToPKCS8(
|
||||
privkey,
|
||||
[]byte("Simba28082020"),
|
||||
[]byte(data.KeyPassword),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -39,7 +39,6 @@ func LoadRootCertAndKey(certFile, keyFile, password string) (*x509.Certificate,
|
||||
if block == nil {
|
||||
return nil, nil, errors.New("failed to parse CA key PEM")
|
||||
}
|
||||
fmt.Print(block.Type)
|
||||
// Декодирование ключа с паролем
|
||||
decryptedKey, err := pkcs8.ParsePKCS8PrivateKey(block.Bytes, []byte(password))
|
||||
if err != nil {
|
||||
|
||||
Reference in new issue
Block a user