/
hsmpki_config.go
93 lines (74 loc) · 2.26 KB
/
hsmpki_config.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
package hsmpki
import (
"errors"
"fmt"
"github.com/mode51software/pkcs11helper/pkg/pkcs11client"
"strconv"
"time"
)
type HsmPkiConfig struct {
// the PKCS#11 client library file
Lib string
// the slot ID on the HSM
SlotId string `hcl:"slot_id"`
// the slot's PIN
Pin string
// the HSM key label
KeyLabel string `hcl:"key_label"`
// connection timeout seconds
ConnectTimeoutS string `hcl:"connect_timeout_s"`
// function timeout seconds
ReadTimeoutS string `hcl:"read_timeout_s"`
}
type HsmConfigCompat struct {
// the HSM's client PKCS#11 library
Lib string
// the HSM slot ID
SlotId uint `json:"slot_id"`
// the slot pin
Pin string
// a key label
KeyLabel string `json:"key_label"`
// connection timeout seconds
ConnectTimeoutS uint `json:"connect_timeout_s"`
// function timeout seconds
ReadTimeoutS uint `json:"read_timeout_s"`
}
// only check the presence of the client lib
// the slot could b 0, the pin could be blank and the key label could be set dynamically
func (h *HsmPkiConfig) ValidateConfig() error {
if len(h.Lib) == 0 {
return errors.New("Please specify the path of the PKCS#11 client library")
}
return nil
}
func (h *HsmPkiConfig) ConvertHsmConfig(hsmConfig *HsmConfigCompat) {
hsmConfig.Lib = h.Lib
slotId, _ := strconv.ParseUint(h.SlotId, 10, 32)
hsmConfig.SlotId = uint(slotId)
hsmConfig.Pin = h.Pin
connectTimeoutS, _ := strconv.ParseUint(h.ConnectTimeoutS, 10, 32)
hsmConfig.ConnectTimeoutS = uint(connectTimeoutS)
readTimeoutS, _ := strconv.ParseUint(h.ReadTimeoutS, 10, 32)
hsmConfig.ReadTimeoutS = uint(readTimeoutS)
}
func (h *HsmPkiConfig) ConvertToHsmConfig() (hsmConfig *pkcs11client.HsmConfig) {
hsmConfig = &(pkcs11client.HsmConfig{})
hsmConfig.Lib = h.Lib
slotId, _ := strconv.ParseUint(h.SlotId, 10, 32)
hsmConfig.SlotId = uint(slotId)
hsmConfig.Pin = h.Pin
hsmConfig.KeyLabel = h.KeyLabel
connectTimeoutS, _ := strconv.ParseUint(h.ConnectTimeoutS, 10, 32)
hsmConfig.ConnectTimeoutS = uint(connectTimeoutS)
readTimeoutS, _ := strconv.ParseUint(h.ReadTimeoutS, 10, 32)
hsmConfig.ReadTimeoutS = uint(readTimeoutS)
return
}
func GenDateTimeKeyLabel() (keyLabel string) {
t := time.Now()
keyLabel = fmt.Sprintf("%d%02d%02d%02d%02d%02d",
t.Year(), t.Month(), t.Day(),
t.Hour(), t.Minute(), t.Second())
return
}