/
nsm.go
59 lines (45 loc) · 1.21 KB
/
nsm.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
//go:build !nonsm
package attest
import (
"errors"
"github.com/hf/nsm"
"github.com/hf/nsm/request"
"github.com/hf/nsm/response"
)
type Manager struct {
PublicKey []byte
}
func (m Manager) GetAttestationDocument(nonce []byte, userData []byte) ([]byte, error) {
return GetAttestationDoc(m.PublicKey, nonce, userData)
}
func (m Manager) GetAttestationDocWithPublicKey(pk []byte, nonce []byte, userData []byte) ([]byte, error) {
return GetAttestationDoc(pk, nonce, userData)
}
type NSM interface {
Send(req request.Request) (response.Response, error)
}
func openDefaultSession() (NSM, error) {
return nsm.OpenDefaultSession()
}
func GetAttestationDoc(publicKey []byte, nonce []byte, userData []byte) ([]byte, error) {
sess, err := openSession()
if err != nil {
return nil, err
}
res, err := sess.Send(&request.Attestation{
UserData: userData,
Nonce: nonce,
PublicKey: publicKey,
})
if err != nil {
return nil, err
}
if res.Error != "" {
return nil, errors.New(string(res.Error))
}
if res.Attestation == nil || res.Attestation.Document == nil {
return nil, errors.New("NSM device did not return an attestation")
}
return res.Attestation.Document, nil
}
var openSession = openDefaultSession