1 parent
bf50da525e
commit
39c27b0de3
9 files changed
+386
-115
No files matched your search
@@ -1,2 +1,3 @@
|
||||
scm
|
||||
go.sum
|
||||
certs
|
||||
@@ -0,0 +1,35 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"scm/model"
|
||||
)
|
||||
|
||||
func RootCert(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case "GET":
|
||||
_, err := os.Stat(fmt.Sprintf("%s/%s", os.Getenv("CERTS_PATH"), "root/root.crt"))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Fprintf(w, `{"status":"ok"}`)
|
||||
case "POST":
|
||||
var rootCertRequest model.CreateRootCertRequest
|
||||
err := json.NewDecoder(r.Body).Decode(&rootCertRequest)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
|
||||
fmt.Fprintf(w, `{"status":"ok"}`)
|
||||
default:
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
@@ -305,5 +305,87 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal fade" tabindex="-1" id="create_certificate">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title">Create SSl Certificate</h5>
|
||||
<button
|
||||
type="button"
|
||||
data-bs-dismiss="modal"
|
||||
aria-label="Close"
|
||||
class="btn-close"
|
||||
></button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<div class="form-floating mb-3">
|
||||
<input
|
||||
type="text"
|
||||
id="domain"
|
||||
class="form-control"
|
||||
placeholder="example.domain"
|
||||
/>
|
||||
<label for="domain">Domain</label>
|
||||
</div>
|
||||
<div class="form-floating mb-3">
|
||||
<input
|
||||
type="text"
|
||||
id="commonname"
|
||||
class="form-control"
|
||||
placeholder="commonname"
|
||||
/>
|
||||
<label for="commonname">Common Name</label>
|
||||
</div>
|
||||
<div class="form-floating mb-3">
|
||||
<input
|
||||
type="text"
|
||||
id="organizationname"
|
||||
class="form-control"
|
||||
placeholder="organizationname"
|
||||
/>
|
||||
<label for="organizationname"
|
||||
>Organization Name</label
|
||||
>
|
||||
</div>
|
||||
<div class="form-floating mb-3">
|
||||
<input
|
||||
type="text"
|
||||
id="organizationunit"
|
||||
class="form-control"
|
||||
placeholder="organizationunit"
|
||||
/>
|
||||
<label for="organizationunit"
|
||||
>Organization Unit</label
|
||||
>
|
||||
</div>
|
||||
<div class="form-floating mb-3">
|
||||
<input
|
||||
type="password"
|
||||
id="password"
|
||||
class="form-control"
|
||||
placeholder="example.domain"
|
||||
/>
|
||||
<label for="password">RootCA password</label>
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button
|
||||
type="button"
|
||||
data-bs-dismiss="modal"
|
||||
class="btn btn-secondary"
|
||||
>
|
||||
Close
|
||||
</button>
|
||||
<button
|
||||
id="createcert"
|
||||
type="button"
|
||||
class="btn btn-primary"
|
||||
>
|
||||
Create
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
+109
-114
@@ -4,132 +4,127 @@ const preview = document.querySelector("#preview");
|
||||
var certificates = [];
|
||||
|
||||
const renderCards = () => {
|
||||
cards.innerHTML = "";
|
||||
cards.innerHTML = "";
|
||||
if (certificates !== null) {
|
||||
certificates.map((cert) => {
|
||||
cards.innerHTML += `<div class="card" style="width: 16rem">
|
||||
<div class="card-body">
|
||||
<h5 class="card-title" style="text-align: center">
|
||||
<a href="${cert.href}">${cert.domain}</a>
|
||||
</h5>
|
||||
</div>
|
||||
<div
|
||||
class="card-footer d-flex flex-column align-items-center gap-2"
|
||||
>
|
||||
<button type="button"
|
||||
data-bs-toggle="modal"
|
||||
data-bs-target="#preview"
|
||||
data-bs-whatever="${cert.cert}"
|
||||
data-bs-dnsnames="${cert.certinfo.DNSNames}"
|
||||
data-bs-notafter="${cert.certinfo.NotAfter}"
|
||||
data-bs-notbefore="${cert.certinfo.NotBefore}"
|
||||
data-bs-issuer-commonname="${cert.certinfo.Issuer.CommonName}"
|
||||
data-bs-issuer-organization="${cert.certinfo.Issuer.Organization}"
|
||||
data-bs-issuer-organizationunit="${cert.certinfo.Issuer.OrganizationalUnit}"
|
||||
data-bs-subject-commonname="${cert.certinfo.Subject.CommonName}"
|
||||
data-bs-subject-organization="${cert.certinfo.Subject.Organization}"
|
||||
data-bs-subject-organizationunit="${cert.certinfo.Subject.OrganizationalUnit}"
|
||||
class="btn btn-primary"
|
||||
style="width: 100%"
|
||||
>
|
||||
Cert
|
||||
</button>
|
||||
<button class="btn btn-primary" style="width: 100%">
|
||||
Download
|
||||
</button>
|
||||
<button class="btn btn-danger delcert" id="${cert.id}" style="width: 100%">
|
||||
Delete
|
||||
</button>
|
||||
</div>
|
||||
</div>`;
|
||||
cards.innerHTML += `<div class="card" style="width: 16rem">
|
||||
<div class="card-body">
|
||||
<h5 class="card-title" style="text-align: center">
|
||||
<a href="${cert.href}">${cert.domain}</a>
|
||||
</h5>
|
||||
</div>
|
||||
<div
|
||||
class="card-footer d-flex flex-column align-items-center gap-2"
|
||||
>
|
||||
<button type="button"
|
||||
data-bs-toggle="modal"
|
||||
data-bs-target="#preview"
|
||||
data-bs-whatever="${cert.cert}"
|
||||
data-bs-dnsnames="${cert.certinfo.DNSNames}"
|
||||
data-bs-notafter="${cert.certinfo.NotAfter}"
|
||||
data-bs-notbefore="${cert.certinfo.NotBefore}"
|
||||
data-bs-issuer-commonname="${cert.certinfo.Issuer.CommonName}"
|
||||
data-bs-issuer-organization="${cert.certinfo.Issuer.Organization}"
|
||||
data-bs-issuer-organizationunit="${cert.certinfo.Issuer.OrganizationalUnit}"
|
||||
data-bs-subject-commonname="${cert.certinfo.Subject.CommonName}"
|
||||
data-bs-subject-organization="${cert.certinfo.Subject.Organization}"
|
||||
data-bs-subject-organizationunit="${cert.certinfo.Subject.OrganizationalUnit}"
|
||||
class="btn btn-primary"
|
||||
style="width: 100%"
|
||||
>
|
||||
Cert
|
||||
</button>
|
||||
<button class="btn btn-primary" style="width: 100%">
|
||||
Download
|
||||
</button>
|
||||
<button class="btn btn-danger delcert" id="${cert.id}" style="width: 100%">
|
||||
Delete
|
||||
</button>
|
||||
</div>
|
||||
</div>`;
|
||||
});
|
||||
} else {
|
||||
cards.innerHTML = `<h1>No Certificates Found</h1>`;
|
||||
}
|
||||
};
|
||||
|
||||
const getCerts = () => {
|
||||
fetch(window.location.origin + "/api/certs")
|
||||
.then((data) => data.json())
|
||||
.then((data) => {
|
||||
certificates = data;
|
||||
renderCards();
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Error fetching certificates:", error);
|
||||
alert("Failed to fetch certificates");
|
||||
});
|
||||
fetch(window.location.origin + "/api/certs")
|
||||
.then((data) => data.json())
|
||||
.then((data) => {
|
||||
certificates = data;
|
||||
renderCards();
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Error fetching certificates:", error);
|
||||
alert("Failed to fetch certificates");
|
||||
});
|
||||
};
|
||||
|
||||
getCerts();
|
||||
|
||||
if (preview) {
|
||||
preview.addEventListener("show.bs.modal", (e) => {
|
||||
const button = e.relatedTarget;
|
||||
const recipient = button.getAttribute("data-bs-whatever");
|
||||
// const modalBodyTextarea = preview.querySelector(".modal-body textarea");
|
||||
const dnsnames = button.getAttribute("data-bs-dnsnames");
|
||||
const notafter = button.getAttribute("data-bs-notafter");
|
||||
const notbefore = button.getAttribute("data-bs-notbefore");
|
||||
const issuerorganizationunit = button.getAttribute(
|
||||
"data-bs-issuer-organizationunit",
|
||||
);
|
||||
const issuerorganization = button.getAttribute(
|
||||
"data-bs-issuer-organization",
|
||||
);
|
||||
const issuercommonname = button.getAttribute(
|
||||
"data-bs-issuer-commonname",
|
||||
);
|
||||
const subjectorganizationunit = button.getAttribute(
|
||||
"data-bs-subject-organizationunit",
|
||||
);
|
||||
const subjectorganization = button.getAttribute(
|
||||
"data-bs-subject-organization",
|
||||
);
|
||||
const subjectcommonname = button.getAttribute(
|
||||
"data-bs-subject-commonname",
|
||||
);
|
||||
preview.querySelector("#raw").innerHTML = recipient.replace(
|
||||
/\n/g,
|
||||
"<br>",
|
||||
);
|
||||
preview.querySelector("#dnsNames").value = dnsnames;
|
||||
preview.querySelector("#notAfter").value = notafter;
|
||||
preview.querySelector("#notBefore").value = notbefore;
|
||||
preview.querySelector("#issuerorganizationunit").value =
|
||||
issuerorganizationunit;
|
||||
preview.querySelector("#issuerorganization").value = issuerorganization;
|
||||
preview.querySelector("#issuercommonname").value = issuercommonname;
|
||||
preview.querySelector("#subjectorganizationunit").value =
|
||||
subjectorganizationunit;
|
||||
preview.querySelector("#subjectorganization").value =
|
||||
subjectorganization;
|
||||
preview.querySelector("#subjectcommonname").value = subjectcommonname;
|
||||
});
|
||||
preview.addEventListener("show.bs.modal", (e) => {
|
||||
const button = e.relatedTarget;
|
||||
const recipient = button.getAttribute("data-bs-whatever");
|
||||
// const modalBodyTextarea = preview.querySelector(".modal-body textarea");
|
||||
const dnsnames = button.getAttribute("data-bs-dnsnames");
|
||||
const notafter = button.getAttribute("data-bs-notafter");
|
||||
const notbefore = button.getAttribute("data-bs-notbefore");
|
||||
const issuerorganizationunit = button.getAttribute(
|
||||
"data-bs-issuer-organizationunit",
|
||||
);
|
||||
const issuerorganization = button.getAttribute(
|
||||
"data-bs-issuer-organization",
|
||||
);
|
||||
const issuercommonname = button.getAttribute("data-bs-issuer-commonname");
|
||||
const subjectorganizationunit = button.getAttribute(
|
||||
"data-bs-subject-organizationunit",
|
||||
);
|
||||
const subjectorganization = button.getAttribute(
|
||||
"data-bs-subject-organization",
|
||||
);
|
||||
const subjectcommonname = button.getAttribute("data-bs-subject-commonname");
|
||||
preview.querySelector("#raw").innerHTML = recipient.replace(/\n/g, "<br>");
|
||||
preview.querySelector("#dnsNames").value = dnsnames;
|
||||
preview.querySelector("#notAfter").value = notafter;
|
||||
preview.querySelector("#notBefore").value = notbefore;
|
||||
preview.querySelector("#issuerorganizationunit").value =
|
||||
issuerorganizationunit;
|
||||
preview.querySelector("#issuerorganization").value = issuerorganization;
|
||||
preview.querySelector("#issuercommonname").value = issuercommonname;
|
||||
preview.querySelector("#subjectorganizationunit").value =
|
||||
subjectorganizationunit;
|
||||
preview.querySelector("#subjectorganization").value = subjectorganization;
|
||||
preview.querySelector("#subjectcommonname").value = subjectcommonname;
|
||||
});
|
||||
}
|
||||
|
||||
cards.addEventListener("click", (e) => {
|
||||
if (e.target.classList.contains("delcert")) {
|
||||
if (confirm("Are you sure you want to delete this certificate?")) {
|
||||
fetch(window.location.origin + `/api/certs/delete`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
domain: certificates.find((item) => item.id === e.target.id)
|
||||
.domain,
|
||||
}),
|
||||
})
|
||||
.then((data) => data.json())
|
||||
.then((jsondata) => {
|
||||
if (jsondata.status === "success") {
|
||||
getCerts();
|
||||
alert("Certificate deleted successfully");
|
||||
} else {
|
||||
alert("Failed to delete certificate");
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(error);
|
||||
alert("An error occurred while deleting the certificate");
|
||||
});
|
||||
}
|
||||
if (e.target.classList.contains("delcert")) {
|
||||
if (confirm("Are you sure you want to delete this certificate?")) {
|
||||
fetch(window.location.origin + `/api/certs/delete`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
domain: certificates.find((item) => item.id === e.target.id).domain,
|
||||
}),
|
||||
})
|
||||
.then((data) => data.json())
|
||||
.then((jsondata) => {
|
||||
if (jsondata.status === "success") {
|
||||
getCerts();
|
||||
alert("Certificate deleted successfully");
|
||||
} else {
|
||||
alert("Failed to delete certificate");
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(error);
|
||||
alert("An error occurred while deleting the certificate");
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,14 @@
|
||||
package model
|
||||
|
||||
type CreateRootCertRequest struct {
|
||||
CommonName string `json:"CommonName"`
|
||||
Organization string `json:"Organization"`
|
||||
OrganizationalUnit string `json:"OrganizationalUnit"`
|
||||
Country string `json:"Country"`
|
||||
Locality string `json:"Locality"`
|
||||
Province string `json:"Province"`
|
||||
StreetAddress string `json:"StreetAddress"`
|
||||
PostalCode string `json:"PostalCode"`
|
||||
ValidityYears int `json:"ValidityYears"`
|
||||
KeyPassword string `json:"KeyPassword"`
|
||||
}
|
||||
@@ -2,7 +2,7 @@ ENABLE_TLS=true
|
||||
CERT_FILE=/home/tola/certs/localhost/localhost.crt
|
||||
KEY_FILE=/home/tola/certs/localhost/localhost.key
|
||||
|
||||
CERTS_PATH=/home/tola/certs
|
||||
CERTS_PATH=/home/tola/Documents/golang/scm/certs
|
||||
|
||||
SERVER_ADDRESS=0.0.0.0
|
||||
SERVER_PORT=8777
|
||||
@@ -3,11 +3,14 @@ package server
|
||||
import (
|
||||
"net/http"
|
||||
"scm/api"
|
||||
"scm/utils"
|
||||
)
|
||||
|
||||
func Configure() {
|
||||
http.Handle("/", api.SpaHandler(http.FS(api.Frontend)))
|
||||
http.HandleFunc("/api/certs", api.Certs)
|
||||
http.HandleFunc("/api/certs/root", api.RootCert)
|
||||
http.HandleFunc("/api/certs/create", api.CreateCert)
|
||||
http.HandleFunc("/api/certs/delete", api.DeleteCert)
|
||||
utils.CheckOrCreateRootKeyCertificate()
|
||||
}
|
||||
@@ -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