First commit
This commit is contained in:
commit
8c8c8966e0
1
.gitattributes
vendored
Normal file
1
.gitattributes
vendored
Normal file
@ -0,0 +1 @@
|
||||
* text=auto eol=lf
|
9
.gitignore
vendored
Normal file
9
.gitignore
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
# Environment variables file
|
||||
.env
|
||||
|
||||
# Distributable directory
|
||||
dist/
|
||||
|
||||
# Test data and logs folders
|
||||
.data/
|
||||
.idea/dataSources.xml
|
8
.idea/.gitignore
vendored
Normal file
8
.idea/.gitignore
vendored
Normal file
@ -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
|
9
.idea/certgen.iml
Normal file
9
.idea/certgen.iml
Normal file
@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="WEB_MODULE" version="4">
|
||||
<component name="Go" enabled="true" />
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
7
.idea/discord.xml
Normal file
7
.idea/discord.xml
Normal file
@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="DiscordProjectSettings">
|
||||
<option name="show" value="PROJECT_FILES" />
|
||||
<option name="description" value="" />
|
||||
</component>
|
||||
</project>
|
8
.idea/modules.xml
Normal file
8
.idea/modules.xml
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/certgen.iml" filepath="$PROJECT_DIR$/.idea/certgen.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
7
LICENSE.md
Normal file
7
LICENSE.md
Normal file
@ -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.
|
53
ca.go
Normal file
53
ca.go
Normal file
@ -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
|
||||
}
|
68
cert.go
Normal file
68
cert.go
Normal file
@ -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
|
||||
}
|
53
server.go
Normal file
53
server.go
Normal file
@ -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
|
||||
}
|
18
tls-leaf.go
Normal file
18
tls-leaf.go
Normal file
@ -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
|
||||
}
|
Loading…
Reference in New Issue
Block a user