Debian Codegraph @

client/signer.go

raw
1 package client
2
3 import (
4 "crypto"
5 "crypto/rsa"
6 "encoding/asn1"
7 "fmt"
8 "io"
9 "math/big"
10 "sync"
11
12 "github.com/google/go-tpm-tools/internal"
13 "github.com/google/go-tpm/legacy/tpm2"
14 )
15
16 // Global mutex to protect against concurrent TPM access.
17 var signerMutex sync.Mutex
18
19 type tpmSigner struct {
20 Key *Key
21 Hash crypto.Hash
22 }
23
24 // Public returns the tpmSigners public key.
25 func (signer *tpmSigner) Public() crypto.PublicKey {
26 return signer.Key.PublicKey()
27 }
28
29 // Sign uses the TPM key to sign the digest.
30 // The digest must be hashed from the same hash algorithm as the keys scheme.
31 // The opts hash function must also match the keys scheme (or be nil).
32 // Concurrent use of Sign is thread safe, but it is not safe to access the TPM
33 // from other sources while Sign is executing.
34 // For RSAPSS signatures, you cannot specify custom salt lengths. The salt
35 // length will be (keyBits/8) - digestSize - 2, unless that is less than the
36 // digestSize in which case, saltLen will be digestSize. The only normal case
37 // where saltLen is not digestSize is when using 1024 keyBits with SHA512.
38 func (signer *tpmSigner) Sign(_ io.Reader, digest []byte, opts crypto.SignerOpts) (signature []byte, err error) {
39 if pssOpts, ok := opts.(*rsa.PSSOptions); ok {
40 if signer.Key.pubArea.RSAParameters == nil {
41 return nil, fmt.Errorf("invalid options: PSSOptions can only be used with RSA keys")
42 }
43 if signer.Key.pubArea.RSAParameters.Sign.Alg != tpm2.AlgRSAPSS {
44 return nil, fmt.Errorf("invalid options: PSSOptions cannot be used with signing alg: %v", signer.Key.pubArea.RSAParameters.Sign.Alg)
45 }
46 if pssOpts.SaltLength != rsa.PSSSaltLengthAuto {
47 return nil, fmt.Errorf("salt length must be rsa.PSSSaltLengthAuto")
48 }
49 }
50 if opts != nil && opts.HashFunc() != signer.Hash {
51 return nil, fmt.Errorf("hash algorithm: got %v, want %v", opts.HashFunc(), signer.Hash)
52 }
53 if len(digest) != signer.Hash.Size() {
54 return nil, fmt.Errorf("digest length: got %d, want %d", len(digest), signer.Hash.Size())
55 }
56
57 signerMutex.Lock()
58 defer signerMutex.Unlock()
59
60 auth, err := signer.Key.session.Auth()
61 if err != nil {
62 return nil, err
63 }
64
65 sig, err := tpm2.SignWithSession(signer.Key.rw, auth.Session, signer.Key.handle, "", digest, nil, nil)
66 if err != nil {
67 return nil, err
68 }
69 return getSignature(sig)
70 }
71
72 // GetSigner returns a crypto.Signer wrapping the loaded TPM Key.
73 // Concurrent use of one or more Signers is thread safe, but it is not safe to
74 // access the TPM from other sources while using a Signer.
75 // The returned Signer lasts the lifetime of the Key, and will no longer work
76 // once the Key has been closed.
77 func (k *Key) GetSigner() (crypto.Signer, error) {
78 if k.hasAttribute(tpm2.FlagRestricted) {
79 return nil, fmt.Errorf("restricted keys are not supported")
80 }
81 hashAlg, err := internal.GetSigningHashAlg(k.pubArea)
82 if err != nil {
83 return nil, err
84 }
85 // For crypto.Signer, Go does the hashing. Make sure the hash is supported.
86 hash, err := hashAlg.Hash()
87 if err != nil {
88 return nil, err
89 }
90 return &tpmSigner{k, hash}, nil
91 }
92
93 // SignData signs a data buffer with a TPM loaded key. Unlike GetSigner, this
94 // method works with restricted and unrestricted keys. If this method is called
95 // on a restriced key, the TPM itself will hash the provided data, failing the
96 // signing operation if the data begins with TPM_GENERATED_VALUE.
97 func (k *Key) SignData(data []byte) ([]byte, error) {
98 hashAlg, err := internal.GetSigningHashAlg(k.pubArea)
99 if err != nil {
100 return nil, err
101 }
102
103 var digest []byte
104 var ticket *tpm2.Ticket
105 if k.hasAttribute(tpm2.FlagRestricted) {
106 // Restricted keys can only sign data hashed by the TPM. We use the
107 // owner hierarchy for the Ticket, but any non-Null hierarchy would do.
108 digest, ticket, err = tpm2.Hash(k.rw, hashAlg, data, tpm2.HandleOwner)
109 if err != nil {
110 return nil, err
111 }
112 } else {
113 // Unrestricted keys can sign any digest, no need for TPM hashing.
114 hash, err := hashAlg.Hash()
115 if err != nil {
116 return nil, err
117 }
118 hasher := hash.New()
119 hasher.Write(data)
120 digest = hasher.Sum(nil)
121 }
122
123 auth, err := k.session.Auth()
124 if err != nil {
125 return nil, err
126 }
127 sig, err := tpm2.SignWithSession(k.rw, auth.Session, k.handle, "", digest, ticket, nil)
128 if err != nil {
129 return nil, err
130 }
131 return getSignature(sig)
132 }
133
134 func getSignature(sig *tpm2.Signature) ([]byte, error) {
135 switch sig.Alg {
136 case tpm2.AlgRSASSA:
137 return sig.RSA.Signature, nil
138 case tpm2.AlgRSAPSS:
139 return sig.RSA.Signature, nil
140 case tpm2.AlgECDSA:
141 sigStruct := struct{ R, S *big.Int }{sig.ECC.R, sig.ECC.S}
142 return asn1.Marshal(sigStruct)
143 default:
144 return nil, fmt.Errorf("unsupported signing algorithm: %v", sig.Alg)
145 }
146 }