forked from OpenICP-BR/libICP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.go
99 lines (85 loc) · 2.69 KB
/
gen.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
91
92
93
94
95
96
97
98
99
package main
import (
"fmt"
"math/big"
"strings"
"errors"
"github.com/OpenICP-BR/libICP"
"github.com/mkideal/cli"
clix "github.com/mkideal/cli/ext"
)
type genT struct {
cli.Helper
Subject string `cli:"s,subject" usage:"name of the certificate holder"`
Issuer string `cli:"i,issuer" usage:"path to the issuer CA certificate. If null, a new self-signed root CA will be created"`
NotBefore clix.Time `cli:"b,not-before" usage:"not-before certificate attribute, use YYYY-mm-dd format"`
NotAfter clix.Time `cli:"a,not-after" usage:"not-after certificate attribute, use YYYY-mm-dd format"`
Output string `cli:"o,output" usage:"path to write the result certificate" dft:"output.pfx"`
Serial string `cli:"n,serial" usage:"sets the serial number for the new certificate"`
Password string `cli:"p,password" usage:"password to encrypt the output file with"`
InPassword string `cli:"q,in-password" usage:"password of the input file"`
}
func GenFunc(ctx *cli.Context) error {
var pfx libICP.PFX
var cerr libICP.CodedError
argv := ctx.Argv().(*genT)
not_before := argv.NotBefore.Time
not_after := argv.NotAfter.Time
if argv.Issuer == "" {
pfx, cerr = libICP.NewRootCA(not_before, not_after)
if cerr != nil {
return cerr
}
cerr = pfx.SaveToFile(argv.Output, argv.Password)
if cerr != nil {
return cerr
}
} else {
// Parse subject
subject_parts := strings.Split(argv.Subject, "/")
subject := make(map[string]string)
for _, part := range subject_parts {
subpart := strings.Split(part, "=")
if len(subpart) < 2 {
continue
}
subject[subpart[0]] = subpart[1]
}
if subject["CN"] == "" || subject["C"] == "" {
return errors.New("subject must have country and common name")
}
// Parse serial
serial := big.NewInt(0)
if argv.Serial == "" {
argv.Serial = "1"
}
_, ok := serial.SetString(argv.Serial, 10)
if !ok {
return errors.New("serial must be a base 10 number")
}
// Load issuer
issuer_pfx, err := libICP.NewPFXFromFile(argv.Issuer, argv.InPassword)
if err != nil {
fmt.Println("Is the password wrong?")
return err
}
// Issue new certificate
pfx, cerr = libICP.NewCertAndKey(subject, *issuer_pfx.Cert, serial, not_before, not_after)
if cerr != nil {
return cerr
}
// Save it
cerr = pfx.SaveToFile(argv.Output, argv.Password)
if cerr != nil {
return cerr
}
}
fmt.Printf("Issuer: %s\n", pfx.Cert.Issuer)
fmt.Printf("Subject: %s\n", pfx.Cert.Subject)
if argv.Issuer == "" {
fmt.Printf("%s[DONE] Root certificate written to file: %s%s\n", FgGreen+Bold, Reset, argv.Output)
} else {
fmt.Printf("%s[DONE] Certificate written to file: %s%s\n", FgGreen+Bold, Reset, argv.Output)
}
return nil
}