...
1 package p11
2
3 import "github.com/miekg/pkcs11"
4
5
6 type Slot struct {
7 ctx *pkcs11.Ctx
8 id uint
9 }
10
11
12 func (s Slot) Info() (pkcs11.SlotInfo, error) {
13 return s.ctx.GetSlotInfo(s.id)
14 }
15
16
17 func (s Slot) TokenInfo() (pkcs11.TokenInfo, error) {
18 return s.ctx.GetTokenInfo(s.id)
19 }
20
21
22 func (s Slot) OpenSession() (Session, error) {
23 return s.OpenSessionWithFlags(0)
24 }
25
26
27 func (s Slot) OpenWriteSession() (Session, error) {
28 return s.OpenSessionWithFlags(pkcs11.CKF_RW_SESSION)
29 }
30
31
32
33
34
35 func (s Slot) OpenSessionWithFlags(flags uint) (Session, error) {
36 handle, err := s.ctx.OpenSession(s.id, flags|pkcs11.CKF_SERIAL_SESSION)
37 if err != nil {
38 return nil, err
39 }
40 return &sessionImpl{
41 ctx: s.ctx,
42 handle: handle,
43 }, nil
44 }
45
46
47 func (s Slot) CloseAllSessions() error {
48 return s.ctx.CloseAllSessions(s.id)
49 }
50
51
52
53 func (s Slot) Mechanisms() ([]Mechanism, error) {
54 list, err := s.ctx.GetMechanismList(s.id)
55 if err != nil {
56 return nil, err
57 }
58 result := make([]Mechanism, len(list))
59 for i, mech := range list {
60 result[i] = Mechanism{
61 mechanism: mech,
62 slot: s,
63 }
64 }
65 return result, nil
66 }
67
68
69
70
71
72
73
74
75
76
77
78 func (s Slot) InitToken(securityOfficerPIN string, tokenLabel string) error {
79 return s.ctx.InitToken(s.id, securityOfficerPIN, tokenLabel)
80 }
81
82
83 func (s Slot) ID() uint {
84 return s.id
85 }
86
87
88
89 type Mechanism struct {
90 mechanism *pkcs11.Mechanism
91 slot Slot
92 }
93
94
95 func (m *Mechanism) Info() (pkcs11.MechanismInfo, error) {
96 return m.slot.ctx.GetMechanismInfo(m.slot.id, []*pkcs11.Mechanism{m.mechanism})
97 }
98
View as plain text