Repository navigation
Expand file tree
/
Copy pathenvironment.go
More file actions
82 lines (66 loc) · 2.06 KB
/
Copy pathenvironment.go
File metadata and controls
82 lines (66 loc) · 2.06 KB
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
// Copyright 2021 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0
package comid
import (
"encoding/json"
"fmt"
)
// Environment stores the identifying information about a target or attesting
// environment at the class, instance and group scope. The Environment type
// has JSON and CBOR serializations.
type Environment struct {
Class *Class `cbor:"0,keyasint,omitempty" json:"class,omitempty"`
Instance *Instance `cbor:"1,keyasint,omitempty" json:"instance,omitempty"`
Group *Group `cbor:"2,keyasint,omitempty" json:"group,omitempty"`
}
// Valid checks the validity (according to the spec) of the target Environment
func (o Environment) Valid() error {
// non-empty<>
if o.Class == nil && o.Instance == nil && o.Group == nil {
return fmt.Errorf("environment must not be empty")
}
if o.Class != nil {
if err := o.Class.Valid(); err != nil {
return fmt.Errorf("class validation failed: %w", err)
}
}
if o.Instance != nil {
if err := o.Instance.Valid(); err != nil {
return fmt.Errorf("instance validation failed: %w", err)
}
}
if o.Group != nil {
if err := o.Group.Valid(); err != nil {
return fmt.Errorf("group validation failed: %w", err)
}
}
return nil
}
// ToCBOR serializes the target Environment to CBOR (if the Environment is "valid")
func (o Environment) ToCBOR() ([]byte, error) {
if err := o.Valid(); err != nil {
return nil, err
}
return em.Marshal(&o)
}
// FromCBOR deserializes the supplied CBOR data into the target Environment
func (o *Environment) FromCBOR(data []byte) error {
if err := dm.Unmarshal(data, o); err != nil {
return err
}
return o.Valid()
}
// FromJSON deserializes the supplied JSON string into the target Environment
func (o *Environment) FromJSON(data []byte) error {
if err := json.Unmarshal(data, o); err != nil {
return err
}
return o.Valid()
}
// ToJSON serializes the target Environment to JSON (if the Environment is "valid")
func (o Environment) ToJSON() ([]byte, error) {
if err := o.Valid(); err != nil {
return nil, err
}
return json.Marshal(&o)
}