-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
146 lines (109 loc) · 2.94 KB
/
main.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
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"os"
"syscall"
"time"
"unsafe"
"github.com/dswarbrick/smart/ioctl"
"github.com/fxamacker/cbor/v2"
"github.com/hf/nitrite"
)
type NsmMessage struct {
request syscall.Iovec
response syscall.Iovec
}
const NsmRequestMaxSize = 0x1000
const NsmResponseMaxSize = 0x3000
type Response struct {
Attestation struct {
Document []byte
}
GetRandom struct {
Random []byte
}
Error string
}
func main() {
time.Sleep(15 *time.Second)
fmt.Println("hello")
// Give a chance to see any output in the console
defer time.Sleep(1 * time.Minute)
nsm, err := os.OpenFile("/dev/nsm", os.O_RDWR, 0)
if err != nil {
fmt.Printf("error opening device: %v\n", err)
return
}
defer func() {_ = nsm.Close()}()
attestationDocument, err := makeAttestationRequest(nsm)
if err != nil {
fmt.Printf("error making request: %v", err)
return
}
verifyResult, err := nitrite.Verify(attestationDocument, nitrite.VerifyOptions{})
if err != nil {
fmt.Printf("error verifying: %v", err)
return
}
jsonified, err := json.MarshalIndent(verifyResult, "> ", " ")
if err != nil {
fmt.Printf("error marshalling: %v", err)
return
}
fmt.Println(string(jsonified))
err = makeGetRandom(nsm)
if err != nil {
fmt.Printf("error making request: %v", err)
return
}
}
func makeGetRandom(nsm *os.File) error {
request := map[string]interface{} {
"GetRandom": struct{}{},
}
response := &Response{}
err := makeRequest(nsm, request, response)
if err != nil {
return err
}
fmt.Printf("Random bytes: %s\n", base64.StdEncoding.EncodeToString(response.GetRandom.Random))
return nil
}
func makeAttestationRequest(nsm *os.File) ([]byte, error){
request := map[string]interface{} {
"Attestation": struct{}{},
}
response := &Response{}
err := makeRequest(nsm, request, response)
if err != nil {
return nil, err
}
fmt.Printf("Error code: %s\n", response.Error)
fmt.Printf("Attestation doc: %v\n", response.Attestation)
return response.Attestation.Document, nil
}
func makeRequest(nsm *os.File, request, response interface{}) error {
requestBytes, err := cbor.Marshal(&request)
if err != nil {
return fmt.Errorf("error marshalling request: %v", err)
}
responseBytes := make([]byte, NsmResponseMaxSize, NsmResponseMaxSize)
message := NsmMessage{}
message.request.Base = &requestBytes[0]
message.request.Len = uint64(len(requestBytes))
message.response.Base = &responseBytes[0]
message.response.Len = NsmResponseMaxSize
requestCode := ioctl.Iowr(0x0A, 0, unsafe.Sizeof(message))
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, nsm.Fd(), requestCode, uintptr(unsafe.Pointer(&message)))
if errno != 0 {
return fmt.Errorf("error from syscall: %v", errno)
}
fmt.Printf("response bytes: %s\n", base64.StdEncoding.EncodeToString(responseBytes))
err = cbor.Unmarshal(responseBytes, response)
if err != nil {
return fmt.Errorf("error unmarshalling response: %v", err)
}
return nil
}