commit 8c8c8966e0fa13ab8ea5b4509938a59dedb798d9 Author: MrMelon Date: Sun Mar 13 01:45:20 2022 +0000 First commit diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..6313b56 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto eol=lf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f2ec00d --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +# Environment variables file +.env + +# Distributable directory +dist/ + +# Test data and logs folders +.data/ +.idea/dataSources.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/certgen.iml b/.idea/certgen.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/certgen.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/discord.xml b/.idea/discord.xml new file mode 100644 index 0000000..d8e9561 --- /dev/null +++ b/.idea/discord.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..cdc8a0a --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..e21d537 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,7 @@ +Copyright 2022 OnPointCoding + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..47895f9 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +certgen +======= diff --git a/ca.go b/ca.go new file mode 100644 index 0000000..1664ca1 --- /dev/null +++ b/ca.go @@ -0,0 +1,53 @@ +package certgen + +import ( + "crypto/rand" + "crypto/rsa" + "crypto/tls" + "crypto/x509" + "crypto/x509/pkix" + "log" + "math/big" + "time" +) + +func MakeCaTls() (*CertGen, error) { + ca := &x509.Certificate{ + SerialNumber: big.NewInt(29052019), + Subject: pkix.Name{ + Organization: []string{"Ski Creds Server"}, + Country: []string{"GB"}, + Province: []string{""}, + Locality: []string{"London"}, + CommonName: "ski-creds-server", + }, + NotBefore: time.Now(), + NotAfter: time.Now().AddDate(10, 0, 0), + IsCA: true, + ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth}, + KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign, + BasicConstraintsValid: true, + } + + caPrivKey, err := rsa.GenerateKey(rand.Reader, 4096) + if err != nil { + log.Fatalln("Failed to generate CA private key:", err) + } + + caBytes, err := x509.CreateCertificate(rand.Reader, ca, ca, caPrivKey.Public(), caPrivKey) + if err != nil { + log.Fatalln("Failed to generate CA certificate bytes:", err) + } + privKeyBytes := x509.MarshalPKCS1PrivateKey(caPrivKey) + gen := &CertGen{cert: ca, certBytes: caBytes, key: caPrivKey, keyBytes: privKeyBytes} + err = gen.generatePem() + if err != nil { + return nil, err + } + caKeyPair, err := tls.X509KeyPair(gen.certPem, gen.keyPem) + if err != nil { + log.Fatalln("Failed to generate CA key pair:", err) + } + gen.tlsCert = caKeyPair + return gen, nil +} diff --git a/cert.go b/cert.go new file mode 100644 index 0000000..ba24dba --- /dev/null +++ b/cert.go @@ -0,0 +1,68 @@ +package certgen + +import ( + "bytes" + "crypto" + "crypto/tls" + "crypto/x509" + "encoding/pem" + "io" +) + +type CertGen struct { + tlsCert tls.Certificate + cert *x509.Certificate + key crypto.PrivateKey + certBytes, keyBytes []byte + certPem, keyPem []byte +} + +func (ca *CertGen) GetTlsLeaf() tls.Certificate { + return ca.tlsCert +} + +func (ca *CertGen) generatePem() error { + a := new(bytes.Buffer) + b := new(bytes.Buffer) + err := pem.Encode(a, &pem.Block{ + Type: "CERTIFICATE", + Bytes: ca.certBytes, + }) + if err != nil { + return err + } + err = pem.Encode(b, &pem.Block{ + Type: "RSA PRIVATE KEY", + Bytes: ca.keyBytes, + }) + if err != nil { + return err + } + ca.certPem = a.Bytes() + ca.keyPem = b.Bytes() + return nil +} + +func (ca *CertGen) SaveFiles(caCert, caKey io.Writer) error { + _, err := caCert.Write(ca.certPem) + if err != nil { + return err + } + _, err = caKey.Write(ca.keyPem) + return err +} + +func LoadCertGen(certBytes, keyBytes []byte) (*CertGen, error) { + pair, err := tls.X509KeyPair(certBytes, keyBytes) + if err != nil { + return nil, err + } + leaf := TlsLeaf(&pair) + return &CertGen{ + tlsCert: pair, + cert: leaf, + key: pair.PrivateKey, + certBytes: certBytes, + keyBytes: keyBytes, + }, nil +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..04a9017 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module tea.melonie54.xyz/sean/certgen + +go 1.17 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e69de29 diff --git a/server.go b/server.go new file mode 100644 index 0000000..8cb539c --- /dev/null +++ b/server.go @@ -0,0 +1,53 @@ +package certgen + +import ( + "crypto/rand" + "crypto/rsa" + "crypto/tls" + "crypto/x509" + "crypto/x509/pkix" + "log" + "math/big" + "time" +) + +func MakeServerTls(ca *CertGen) (*CertGen, error) { + cert := &x509.Certificate{ + SerialNumber: big.NewInt(29052019), + Subject: pkix.Name{ + Organization: []string{"Ski Creds Server"}, + Country: []string{"GB"}, + Province: []string{""}, + Locality: []string{"London"}, + CommonName: "ski-creds-server", + }, + NotBefore: time.Now(), + NotAfter: time.Now().AddDate(10, 0, 0), + IsCA: true, + ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth}, + KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign, + BasicConstraintsValid: true, + } + + serverPrivKey, err := rsa.GenerateKey(rand.Reader, 4096) + if err != nil { + log.Fatalln("Failed to generate server private key:", err) + } + + serverBytes, err := x509.CreateCertificate(rand.Reader, cert, ca.cert, serverPrivKey.Public(), ca.key) + if err != nil { + log.Fatalln("Failed to generate server certificate bytes:", err) + } + privKeyBytes := x509.MarshalPKCS1PrivateKey(serverPrivKey) + gen := &CertGen{cert: cert, certBytes: serverBytes, key: serverPrivKey, keyBytes: privKeyBytes} + err = gen.generatePem() + if err != nil { + return nil, err + } + caKeyPair, err := tls.X509KeyPair(gen.certPem, gen.keyPem) + if err != nil { + log.Fatalln("Failed to generate CA key pair:", err) + } + gen.tlsCert = caKeyPair + return gen, nil +} diff --git a/tls-leaf.go b/tls-leaf.go new file mode 100644 index 0000000..5067941 --- /dev/null +++ b/tls-leaf.go @@ -0,0 +1,18 @@ +package certgen + +import ( + "crypto/tls" + "crypto/x509" +) + +func TlsLeaf(cert *tls.Certificate) *x509.Certificate { + if cert.Leaf != nil { + return cert.Leaf + } + if len(cert.Certificate) >= 1 { + if a, err := x509.ParseCertificate(cert.Certificate[0]); err == nil { + cert.Leaf = a + } + } + return cert.Leaf +}