@@ -69,53 +69,45 @@ func NewTPMCrypto(conf *TPM) (TPM, error) {
69
69
if conf .TpmPath != "" && conf .KeyHandle == 0 {
70
70
return TPM {}, fmt .Errorf ("salrashid123/x/oauth2/google: if TPMTokenConfig.TPMPath is specified, a KeyHandle must be set" )
71
71
}
72
- return * conf , nil
73
- }
74
72
75
- func (t TPM ) Public () crypto.PublicKey {
76
- if t .publicKey == nil {
77
- t .refreshMutex .Lock ()
78
- defer t .refreshMutex .Unlock ()
79
-
80
- var rwc io.ReadWriteCloser
81
- var k * client.Key
82
- if t .TpmDevice == nil {
83
- var err error
84
- rwc , err = tpm2 .OpenTPM (t .TpmPath )
85
- if err != nil {
86
- fmt .Printf ("google: Unable to Read Public data from TPM: %v" , err )
87
- return nil
88
- }
89
- defer rwc .Close ()
90
- pcrsession , err := client .NewPCRSession (rwc , tpm2.PCRSelection {tpm2 .AlgSHA256 , t .PCRs })
91
- if err != nil {
92
- fmt .Printf ("google: Unable to Read Public data from TPM: %v" , err )
93
- return nil
94
- }
95
- k , err = client .LoadCachedKey (rwc , tpmutil .Handle (t .KeyHandle ), pcrsession )
96
- if err != nil {
97
- fmt .Printf ("google: Unable to Read Public data from TPM: %v" , err )
98
- return nil
99
- }
100
- defer pcrsession .Close ()
101
- defer k .Close ()
102
- } else {
103
- rwc = t .TpmDevice
104
- k = t .Key
73
+ var rwc io.ReadWriteCloser
74
+ var k * client.Key
75
+ if conf .TpmDevice == nil {
76
+ var err error
77
+ rwc , err = tpm2 .OpenTPM (conf .TpmPath )
78
+ if err != nil {
79
+ return TPM {}, fmt .Errorf ("google: Unable to Read Public data from TPM: %v" , err )
105
80
}
106
-
107
- pub , _ , _ , err := tpm2 . ReadPublic (rwc , k . Handle () )
81
+ defer rwc . Close ()
82
+ pcrsession , err := client . NewPCRSession (rwc , tpm2. PCRSelection { tpm2 . AlgSHA256 , conf . PCRs } )
108
83
if err != nil {
109
- fmt .Printf ("google: Unable to Read Public data from TPM: %v" , err )
110
- return nil
84
+ return TPM {}, fmt .Errorf ("google: Unable to Read Public data from TPM: %v" , err )
111
85
}
112
- pubKey , err := pub . Key ( )
86
+ k , err = client . LoadCachedKey ( rwc , tpmutil . Handle ( conf . KeyHandle ), pcrsession )
113
87
if err != nil {
114
- fmt .Printf ("google: Unable to Read Public data from TPM: %v" , err )
115
- return nil
88
+ return TPM {}, fmt .Errorf ("google: Unable to Read Public data from TPM: %v" , err )
116
89
}
117
- t .publicKey = pubKey
90
+ defer pcrsession .Close ()
91
+ defer k .Close ()
92
+ } else {
93
+ rwc = conf .TpmDevice
94
+ k = conf .Key
95
+ }
96
+
97
+ pub , _ , _ , err := tpm2 .ReadPublic (rwc , k .Handle ())
98
+ if err != nil {
99
+ return TPM {}, fmt .Errorf ("google: Unable to Read Public data from TPM: %v" , err )
100
+ }
101
+ pubKey , err := pub .Key ()
102
+ if err != nil {
103
+ return TPM {}, fmt .Errorf ("google: Unable to Read Public data from TPM: %v" , err )
118
104
}
105
+ conf .publicKey = pubKey
106
+
107
+ return * conf , nil
108
+ }
109
+
110
+ func (t TPM ) Public () crypto.PublicKey {
119
111
return t .publicKey
120
112
}
121
113
@@ -196,27 +188,23 @@ func (t TPM) Sign(rr io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte,
196
188
}
197
189
}
198
190
199
- func (t TPM ) TLSCertificate () tls.Certificate {
191
+ func (t TPM ) TLSCertificate () ( tls.Certificate , error ) {
200
192
201
193
if t .PublicCertFile == "" {
202
- fmt .Printf ("Public X509 certificate not specified" )
203
- return tls.Certificate {}
194
+ return tls.Certificate {}, fmt .Errorf ("Public X509 certificate not specified" )
204
195
}
205
196
206
197
pubPEM , err := os .ReadFile (t .PublicCertFile )
207
198
if err != nil {
208
- fmt .Printf ("Unable to read keys %v" , err )
209
- return tls.Certificate {}
199
+ return tls.Certificate {}, fmt .Errorf ("Unable to read public certificate file %v" , err )
210
200
}
211
201
block , _ := pem .Decode ([]byte (pubPEM ))
212
202
if block == nil {
213
- fmt .Printf ("failed to parse PEM block containing the public key" )
214
- return tls.Certificate {}
203
+ return tls.Certificate {}, fmt .Errorf ("failed to parse PEM block containing the public key" )
215
204
}
216
205
pub , err := x509 .ParseCertificate (block .Bytes )
217
206
if err != nil {
218
- fmt .Printf ("failed to parse public key: " + err .Error ())
219
- return tls.Certificate {}
207
+ return tls.Certificate {}, fmt .Errorf ("Unable to read public certificate file %v" , err )
220
208
}
221
209
222
210
t .x509Certificate = * pub
@@ -225,5 +213,5 @@ func (t TPM) TLSCertificate() tls.Certificate {
225
213
PrivateKey : privKey ,
226
214
Leaf : & t .x509Certificate ,
227
215
Certificate : [][]byte {t .x509Certificate .Raw },
228
- }
216
+ }, nil
229
217
}
0 commit comments