Repository navigation
Expand file tree
/
Copy pathca.go
More file actions
122 lines (102 loc) Β· 3.66 KB
/
Copy pathca.go
File metadata and controls
122 lines (102 loc) Β· 3.66 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package cmd
import (
"fmt"
"path/filepath"
"certwiz/pkg/cert"
"certwiz/pkg/ui"
"github.com/spf13/cobra"
)
var (
caCN string
caOrg string
caCountry string
caDays int
caKeySize int
caOutput string
)
var caCmd = &cobra.Command{
Use: "ca",
Short: "Create a Certificate Authority (CA) certificate",
Long: `Create a self-signed Certificate Authority (CA) certificate and private key.
A CA certificate can be used to sign other certificates, creating a chain of trust.
This is useful for internal PKI, development environments, or testing.
Examples:
# Create a basic CA certificate
cert ca --cn "My Company CA"
# Create a CA with organization details
cert ca --cn "Example Corp Root CA" --org "Example Corporation" --country US
# Create a CA with custom validity period (10 years)
cert ca --cn "Internal CA" --days 3650
# Create a CA with larger key size for extra security
cert ca --cn "Secure CA" --key-size 4096 --output /etc/pki/`,
RunE: func(cmd *cobra.Command, args []string) error {
if caCN == "" {
err := fmt.Errorf("common name (--cn) is required")
reportError(cmd, err)
return err
}
// Prepare options
options := cert.CAOptions{
CommonName: caCN,
Organization: caOrg,
Country: caCountry,
Days: caDays,
KeySize: caKeySize,
}
// Generate CA certificate
if !jsonOutput {
fmt.Printf("%s Generating Certificate Authority...\n", ui.Emoji("π", "[CA]"))
}
certPath := filepath.Join(caOutput, sanitizeFilename(caCN)+"-ca.crt")
keyPath := filepath.Join(caOutput, sanitizeFilename(caCN)+"-ca.key")
err := cert.GenerateCA(options, certPath, keyPath)
if err != nil {
err = fmt.Errorf("failed to generate CA: %w", err)
reportError(cmd, err)
return err
}
if jsonOutput {
printJSON(cert.JSONOperationResult{
Success: true,
Message: "Certificate Authority generated successfully",
Files: []string{certPath, keyPath},
})
return nil
}
// Display success message
ui.ShowSuccess("Certificate Authority generated successfully!")
fmt.Println()
fmt.Printf("%s Files created:\n", ui.Emoji("π", "[FILES]"))
fmt.Printf(" %s CA Certificate: %s\n", ui.Emoji("ποΈ", "[CERT]"), certPath)
fmt.Printf(" %s CA Private Key: %s\n", ui.Emoji("π", "[KEY]"), keyPath)
fmt.Println()
fmt.Printf("%s Security Notes:\n", ui.Emoji("β οΈ", "[WARNING]"))
fmt.Println(" β’ Keep the CA private key extremely secure")
fmt.Println(" β’ Never share the CA private key")
fmt.Println(" β’ Consider storing the key offline or in an HSM")
fmt.Println()
fmt.Printf("%s Next steps:\n", ui.Emoji("π", "[NEXT]"))
fmt.Println(" 1. Distribute the CA certificate to clients that need to trust it")
fmt.Println(" 2. Use 'cert sign' command to sign CSRs with this CA")
fmt.Println(" 3. Keep the CA key secure and backed up")
// Display the CA certificate details
fmt.Println()
fmt.Printf("%s CA Certificate Details:\n", ui.Emoji("π", "[INFO]"))
caCert, err := cert.InspectFile(certPath)
if err != nil {
ui.ShowInfo(fmt.Sprintf("Could not display CA details: %v", err))
} else {
ui.DisplayCertificate(caCert, false)
}
return nil
},
}
func init() {
caCmd.Flags().StringVar(&caCN, "cn", "", "Common Name for the CA (required)")
caCmd.Flags().StringVar(&caOrg, "org", "", "Organization name")
caCmd.Flags().StringVar(&caCountry, "country", "", "Country (2-letter code)")
caCmd.Flags().IntVarP(&caDays, "days", "d", 3650, "Validity period in days")
caCmd.Flags().IntVarP(&caKeySize, "key-size", "k", 4096, "RSA key size in bits")
caCmd.Flags().StringVarP(&caOutput, "output", "o", ".", "Output directory for CA files")
rootCmd.AddCommand(caCmd)
}