-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: generate self signed certs if no certs are detected
- Loading branch information
1 parent
aab797e
commit 75ed0d4
Showing
4 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
certs/** | ||
!certs/*.go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package certs | ||
|
||
import ( | ||
"crypto/ecdsa" | ||
"crypto/elliptic" | ||
"crypto/rand" | ||
"crypto/x509" | ||
"crypto/x509/pkix" | ||
"encoding/pem" | ||
"fmt" | ||
"math/big" | ||
"os" | ||
"time" | ||
) | ||
|
||
func CreateSelfSignedCert(certFile, keyFile string) error { | ||
// Generate a private key | ||
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) | ||
if err != nil { | ||
return fmt.Errorf("failed to generate private key: %v", err) | ||
} | ||
|
||
// Create a certificate template | ||
notBefore := time.Now() | ||
notAfter := notBefore.Add(365 * 24 * time.Hour) | ||
|
||
serialNumber, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128)) | ||
if err != nil { | ||
return fmt.Errorf("failed to generate serial number: %v", err) | ||
} | ||
|
||
template := x509.Certificate{ | ||
SerialNumber: serialNumber, | ||
Subject: pkix.Name{ | ||
Organization: []string{"Self-Signed Co"}, | ||
}, | ||
NotBefore: notBefore, | ||
NotAfter: notAfter, | ||
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature, | ||
ExtKeyUsage: []x509.ExtKeyUsage{ | ||
x509.ExtKeyUsageServerAuth, | ||
}, | ||
BasicConstraintsValid: true, | ||
} | ||
|
||
// Generate a self-signed certificate | ||
certDER, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv) | ||
if err != nil { | ||
return fmt.Errorf("failed to create certificate: %v", err) | ||
} | ||
|
||
// Save the certificate to certFile | ||
certOut, err := os.Create(certFile) | ||
if err != nil { | ||
return fmt.Errorf("failed to open cert.pem for writing: %v", err) | ||
} | ||
defer certOut.Close() | ||
|
||
if err := pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: certDER}); err != nil { | ||
return fmt.Errorf("failed to write data to cert.pem: %v", err) | ||
} | ||
|
||
// Save the private key to keyFile | ||
keyOut, err := os.Create(keyFile) | ||
if err != nil { | ||
return fmt.Errorf("failed to open key.pem for writing: %v", err) | ||
} | ||
defer keyOut.Close() | ||
|
||
privBytes, err := x509.MarshalECPrivateKey(priv) | ||
if err != nil { | ||
return fmt.Errorf("failed to marshal private key: %v", err) | ||
} | ||
if err := pem.Encode(keyOut, &pem.Block{Type: "EC PRIVATE KEY", Bytes: privBytes}); err != nil { | ||
return fmt.Errorf("failed to write data to key.pem: %v", err) | ||
} | ||
|
||
fmt.Println("Successfully created self-signed certificate and private key.") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters