-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoperations.go
193 lines (167 loc) · 5.87 KB
/
operations.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package kmip
import (
"reflect"
"github.com/ovh/kmip-go/ttlv"
)
func init() {
ttlv.RegisterEnum(TagOperation, map[Operation]string{
OperationCreate: "Create",
OperationCreateKeyPair: "CreateKeyPair",
OperationRegister: "Register",
OperationReKey: "ReKey",
OperationDeriveKey: "DeriveKey",
OperationCertify: "Certify",
OperationReCertify: "ReCertify",
OperationLocate: "Locate",
OperationCheck: "Check",
OperationGet: "Get",
OperationGetAttributes: "GetAttributes",
OperationGetAttributeList: "GetAttributeList",
OperationAddAttribute: "AddAttribute",
OperationModifyAttribute: "ModifyAttribute",
OperationDeleteAttribute: "DeleteAttribute",
OperationObtainLease: "ObtainLease",
OperationGetUsageAllocation: "GetUsageAllocation",
OperationActivate: "Activate",
OperationRevoke: "Revoke",
OperationDestroy: "Destroy",
OperationArchive: "Archive",
OperationRecover: "Recover",
OperationValidate: "Validate",
OperationQuery: "Query",
OperationCancel: "Cancel",
OperationPoll: "Poll",
OperationNotify: "Notify",
OperationPut: "Put",
// KMIP 1.1
OperationReKeyKeyPair: "ReKeyKeyPair",
OperationDiscoverVersions: "DiscoverVersions",
// KMIP 1.2
OperationEncrypt: "Encrypt",
OperationDecrypt: "Decrypt",
OperationSign: "Sign",
OperationSignatureVerify: "SignatureVerify",
OperationMAC: "MAC",
OperationMACVerify: "MACVerify",
OperationRNGRetrieve: "RNGRetrieve",
OperationRNGSeed: "RNGSeed",
OperationHash: "Hash",
OperationCreateSplitKey: "CreateSplitKey",
OperationJoinSplitKey: "JoinSplitKey",
// KMIP 1.4
OperationImport: "Import",
OperationExport: "Export",
})
}
type Operation uint32
const (
OperationCreate Operation = 0x00000001
OperationCreateKeyPair Operation = 0x00000002
OperationRegister Operation = 0x00000003
OperationReKey Operation = 0x00000004
OperationDeriveKey Operation = 0x00000005
OperationCertify Operation = 0x00000006
OperationReCertify Operation = 0x00000007
OperationLocate Operation = 0x00000008
OperationCheck Operation = 0x00000009
OperationGet Operation = 0x0000000A
OperationGetAttributes Operation = 0x0000000B
OperationGetAttributeList Operation = 0x0000000C
OperationAddAttribute Operation = 0x0000000D
OperationModifyAttribute Operation = 0x0000000E
OperationDeleteAttribute Operation = 0x0000000F
OperationObtainLease Operation = 0x00000010
OperationGetUsageAllocation Operation = 0x00000011
OperationActivate Operation = 0x00000012
OperationRevoke Operation = 0x00000013
OperationDestroy Operation = 0x00000014
OperationArchive Operation = 0x00000015
OperationRecover Operation = 0x00000016
OperationValidate Operation = 0x00000017
OperationQuery Operation = 0x00000018
OperationCancel Operation = 0x00000019
OperationPoll Operation = 0x0000001A
OperationNotify Operation = 0x0000001B
OperationPut Operation = 0x0000001C
// KMIP 1.1.
OperationReKeyKeyPair Operation = 0x0000001D
OperationDiscoverVersions Operation = 0x0000001E
// KMIP 1.2.
OperationEncrypt Operation = 0x0000001F
OperationDecrypt Operation = 0x00000020
OperationSign Operation = 0x00000021
OperationSignatureVerify Operation = 0x00000022
OperationMAC Operation = 0x00000023
OperationMACVerify Operation = 0x00000024
OperationRNGRetrieve Operation = 0x00000025
OperationRNGSeed Operation = 0x00000026
OperationHash Operation = 0x00000027
OperationCreateSplitKey Operation = 0x00000028
OperationJoinSplitKey Operation = 0x00000029
// KMIP 1.4.
OperationImport Operation = 0x0000002A
OperationExport Operation = 0x0000002B
)
func (enum Operation) MarshalText() ([]byte, error) {
return []byte(ttlv.EnumStr(enum)), nil
}
type operationPayloadTypes struct {
request reflect.Type
response reflect.Type
}
func (opt *operationPayloadTypes) newRequest() OperationPayload {
return reflect.New(opt.request).Interface().(OperationPayload)
}
func (opt *operationPayloadTypes) newResponse() OperationPayload {
return reflect.New(opt.response).Interface().(OperationPayload)
}
func typeForOperation[Req, Resp any]() operationPayloadTypes {
return operationPayloadTypes{
request: reflect.TypeFor[Req](),
response: reflect.TypeFor[Resp](),
}
}
var operationRegistry = map[Operation]operationPayloadTypes{}
func RegisterOperationPayload[Req, Resp any](op Operation) {
operationRegistry[op] = typeForOperation[Req, Resp]()
}
func newRequestPayload(op Operation) OperationPayload {
types, ok := operationRegistry[op]
if !ok {
return &UnknownPayload{
opType: op,
}
}
return types.newRequest()
}
func newResponsePayload(op Operation) OperationPayload {
types, ok := operationRegistry[op]
if !ok {
return &UnknownPayload{
opType: op,
}
}
return types.newResponse()
}
type OperationPayload interface {
Operation() Operation
}
type UnknownPayload struct {
opType Operation `ttlv:"-"`
Fields ttlv.Struct
}
func NewUnknownPayload(op Operation, fields ...ttlv.Value) *UnknownPayload {
return &UnknownPayload{
opType: op,
Fields: ttlv.Struct(fields),
}
}
func (pl *UnknownPayload) Operation() Operation {
return pl.opType
}
func (v *UnknownPayload) TagEncodeTTLV(e *ttlv.Encoder, tag int) {
v.Fields.TagEncodeTTLV(e, tag)
}
func (v *UnknownPayload) TagDecodeTTLV(d *ttlv.Decoder, tag int) error {
return v.Fields.TagDecodeTTLV(d, tag)
}