forked from project-alvarium/alvarium-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tpm.go
65 lines (58 loc) · 2.28 KB
/
tpm.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
/*******************************************************************************
* Copyright 2021 Dell Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*******************************************************************************/
package annotators
import (
"context"
"github.com/DyrellC/alvarium-sdk-go/pkg/config"
"github.com/DyrellC/alvarium-sdk-go/pkg/contracts"
"github.com/DyrellC/alvarium-sdk-go/pkg/interfaces"
"os"
)
// Default path to a TPM 2.0 device (https://wiki.archlinux.org/title/Trusted_Platform_Module)
const tpmPath string = "/dev/tpm0"
// TpmAnnotator is used to attest whether or not the host machine has TPM capability for managing secrets
type TpmAnnotator struct {
hash contracts.HashType
kind contracts.AnnotationType
sign config.SignatureInfo
}
func NewTpmAnnotator(cfg config.SdkInfo) interfaces.Annotator {
a := TpmAnnotator{}
a.hash = cfg.Hash.Type
a.kind = contracts.AnnotationTPM
a.sign = cfg.Signature
return &a
}
func (a *TpmAnnotator) Do(ctx context.Context, data []byte) (contracts.Annotation, error) {
key := deriveHash(a.hash, data)
hostname, _ := os.Hostname()
isSatisfied := false
// If mounted path exists, check that it's either a device or a socket (emulator)
// This logic based on code found in gp-tpm module.
// https://github.com/google/go-tpm/blob/b3942ee5b15a7bd19e6419d5903e6e64fbb3d4ba/tpmutil/run_other.go#L29
fi, err := os.Stat(tpmPath)
if err == nil {
// TPM mounted at default path
if fi.Mode()&os.ModeDevice == 0 || fi.Mode()&os.ModeSocket == 0 {
isSatisfied = true
}
}
annotation := contracts.NewAnnotation(key, a.hash, hostname, a.kind, isSatisfied)
sig, err := signAnnotation(a.sign.PrivateKey, annotation)
if err != nil {
return contracts.Annotation{}, err
}
annotation.Signature = string(sig)
return annotation, nil
}