-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
90 lines (81 loc) · 2.42 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
certMaker.go
Version: 1.0
Author: Franz Ramirez
Description: Application that allows to generate self generated CAs and the corresponding server certificate
*/
package main
import (
"fmt"
"net"
"os"
)
//Cert struct
type Cert struct {
Organization []string
Country []string
Province []string
Locality []string
StreetAddress []string
PostalCode []string
CommonName string
IPAddresses []net.IP
DNSNames []string
}
func ca(caInfo Cert, outputPath, caName string) {
fmt.Println("Generating CA...")
// generate ca key
caKeyBin := genPrivateKey()
// generate cert definition
caCertDef := genCaCertDef(caInfo)
// generate ca cert and return it
caCertBin := genCA(caKeyBin, caCertDef)
// pem encode the cacert and return it
caPemCert, caPemKey := encodeCertKey(caKeyBin, caCertBin)
// Write the cert to file
caPath, caKeyPath := writeCertKey(caPemKey, caPemCert, "ca", outputPath, caName)
// Return the path to the keys
fmt.Println("Ca can be found: " + caPath + "\nKey can be found: " + caKeyPath + "/nKeep the key safe!")
// ask if the user wants to create a cert based on the CA
for {
if !continueToCertPrompt() {
break
}
certInfo := certPrompt()
cert(certInfo, outputPath, caPath, caKeyPath)
}
}
func cert(certInfo Cert, outputPath string, caPath string, caKeyPath string) {
fmt.Println("Generating signed Cert...")
// retrieve ca information
caKeyBin, caCertBin, caCertDef := getCA(caPath, caKeyPath)
// generate server cert key
sKeyBin := genPrivateKey()
// generate cert definition
sCertDef := genSCertDef(certInfo)
// Sign cert with CA
sCertBin := signCert(caKeyBin, caCertBin, sKeyBin, sCertDef, caCertDef)
// pem encode the scert and return it
sPemCert, sPemKey := encodeCertKey(sKeyBin, sCertBin)
// write the cert to file
certPath, keyPath := writeCertKey(sPemKey, sPemCert, "", outputPath, certInfo.CommonName)
// Return paths to the user
fmt.Println("Cert can be found: " + certPath + "\nKey can be found: " + keyPath)
}
func main() {
if len(os.Args) > 1 {
//certInfo, certType := readFlags()
// WILL ADD CLI FLAGS HERE EVENTUALLY
} else {
certType := initPrompt()
if certType == "ca" {
outputPath, caName, _ := loadPaths(certType)
certInfo := caPrompt()
ca(certInfo, outputPath, caName)
} else if certType == "cert" {
outputPath, caPath, caKeyPath := loadPaths(certType)
certInfo := certPrompt()
cert(certInfo, outputPath, caPath, caKeyPath)
}
}
}