forked from ThalesGroup/kmip-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
op_register.go
86 lines (70 loc) · 2.25 KB
/
op_register.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
package kmip
import (
"context"
"github.com/Seagate/kmip-go/kmip14"
"github.com/ansel1/merry"
)
// 4.3
// Table 169
type RegisterRequestPayload struct {
ObjectType kmip14.ObjectType
TemplateAttribute TemplateAttribute
Certificate *Certificate
SymmetricKey *SymmetricKey
PrivateKey *PrivateKey
PublicKey *PublicKey
SplitKey *SplitKey
Template *Template
SecretData *SecretData
OpaqueObject *OpaqueObject
}
// Table 170
type RegisterResponsePayload struct {
UniqueIdentifier string
TemplateAttribute TemplateAttribute
}
type RegisterHandler struct {
SkipValidation bool
RegisterFunc func(context.Context, *RegisterRequestPayload) (*RegisterResponsePayload, error)
}
func (h *RegisterHandler) HandleItem(ctx context.Context, req *Request) (item *ResponseBatchItem, err error) {
var payload RegisterRequestPayload
err = req.DecodePayload(&payload)
if err != nil {
return nil, merry.Prepend(err, "decoding request")
}
if !h.SkipValidation {
var payloadPresent bool
switch payload.ObjectType {
default:
return nil, WithResultReason(merry.UserError("Object Type is not recognized"), kmip14.ResultReasonInvalidField)
case kmip14.ObjectTypeCertificate:
payloadPresent = payload.Certificate != nil
case kmip14.ObjectTypeSymmetricKey:
payloadPresent = payload.SymmetricKey != nil
case kmip14.ObjectTypePrivateKey:
payloadPresent = payload.PrivateKey != nil
case kmip14.ObjectTypePublicKey:
payloadPresent = payload.PublicKey != nil
case kmip14.ObjectTypeSplitKey:
payloadPresent = payload.SplitKey != nil
case kmip14.ObjectTypeTemplate:
payloadPresent = payload.Template != nil
case kmip14.ObjectTypeSecretData:
payloadPresent = payload.SecretData != nil
case kmip14.ObjectTypeOpaqueObject:
payloadPresent = payload.OpaqueObject != nil
}
if !payloadPresent {
return nil, WithResultReason(merry.UserErrorf("Object Type %s does not match type of cryptographic object provided", payload.ObjectType.String()), kmip14.ResultReasonInvalidField)
}
}
respPayload, err := h.RegisterFunc(ctx, &payload)
if err != nil {
return nil, err
}
req.IDPlaceholder = respPayload.UniqueIdentifier
return &ResponseBatchItem{
ResponsePayload: respPayload,
}, nil
}