@@ -197,7 +197,7 @@ func TpmSign(digest []byte) (*big.Int, *big.Int, error) {
197
197
198
198
tpmOwnerPasswd , err := ReadOwnerCrdl ()
199
199
if err != nil {
200
- return nil , nil , fmt .Errorf ("fetching TPM credentials failed: %v " , err )
200
+ return nil , nil , fmt .Errorf ("fetching TPM credentials failed: %w " , err )
201
201
}
202
202
203
203
//XXX This "32" should really come from Hash algo used.
@@ -212,7 +212,7 @@ func TpmSign(digest []byte) (*big.Int, *big.Int, error) {
212
212
sig , err := tpm2 .Sign (rw , TpmDeviceKeyHdl ,
213
213
tpmOwnerPasswd , digest , nil , scheme )
214
214
if err != nil {
215
- return nil , nil , fmt .Errorf ("signing data using TPM failed: %v " , err )
215
+ return nil , nil , fmt .Errorf ("signing data using TPM failed: %w " , err )
216
216
}
217
217
return sig .ECC .R , sig .ECC .S , nil
218
218
}
@@ -347,6 +347,7 @@ func FetchTpmHwInfo() (string, error) {
347
347
_ , err := os .Stat (TpmDevicePath )
348
348
if err != nil {
349
349
tpmHwInfo = "Not Available"
350
+ //nolint:nilerr
350
351
return tpmHwInfo , nil
351
352
}
352
353
@@ -402,11 +403,11 @@ func FetchVaultKey(log *base.LogObject) ([]byte, error) {
402
403
//
403
404
key , err = GetRandom (vaultKeyLength )
404
405
if err != nil {
405
- return nil , fmt .Errorf ("GetRandom failed: %v " , err )
406
+ return nil , fmt .Errorf ("GetRandom failed: %w " , err )
406
407
}
407
408
err = writeDiskKey (key )
408
409
if err != nil {
409
- return nil , fmt .Errorf ("writing legacy Key to TPM failed: %v " , err )
410
+ return nil , fmt .Errorf ("writing legacy Key to TPM failed: %w " , err )
410
411
}
411
412
} else {
412
413
log .Noticef ("successfully read the legacy disk key from TPM" )
@@ -501,11 +502,11 @@ func FetchSealedVaultKey(log *base.LogObject) ([]byte, error) {
501
502
//
502
503
key , err := GetRandom (vaultKeyLength )
503
504
if err != nil {
504
- return nil , fmt .Errorf ("GetRandom failed: %v " , err )
505
+ return nil , fmt .Errorf ("GetRandom failed: %w " , err )
505
506
}
506
507
err = SealDiskKey (key , DiskKeySealingPCRs )
507
508
if err != nil {
508
- return nil , fmt .Errorf ("sealing the fresh disk key failed: %v " , err )
509
+ return nil , fmt .Errorf ("sealing the fresh disk key failed: %w " , err )
509
510
}
510
511
511
512
log .Noticef ("successfully sealed the fresh disk key into TPM" )
@@ -525,14 +526,14 @@ func FetchSealedVaultKey(log *base.LogObject) ([]byte, error) {
525
526
//Upgrade path will be to first upgrade to a) first release and then b)
526
527
key , err := readDiskKey ()
527
528
if err != nil {
528
- return nil , fmt .Errorf ("retrieving the legacy disk key from TPM failed: %v " , err )
529
+ return nil , fmt .Errorf ("retrieving the legacy disk key from TPM failed: %w " , err )
529
530
}
530
531
531
532
log .Noticef ("try to convert the legacy key into a sealed key" )
532
533
533
534
err = SealDiskKey (key , DiskKeySealingPCRs )
534
535
if err != nil {
535
- return nil , fmt .Errorf ("sealing the legacy disk key into TPM failed: %v " , err )
536
+ return nil , fmt .Errorf ("sealing the legacy disk key into TPM failed: %w " , err )
536
537
}
537
538
}
538
539
//sealedKeyPresent && !legacyKeyPresent : unseal
@@ -587,7 +588,7 @@ func SealDiskKey(key []byte, pcrSel tpm2.PCRSelection) error {
587
588
588
589
priv , public , err := tpm2 .Seal (rw , TpmSRKHdl , EmptyPassword , EmptyPassword , policy , key )
589
590
if err != nil {
590
- return fmt .Errorf ("sealing the disk key into TPM failed: %v " , err )
591
+ return fmt .Errorf ("sealing the disk key into TPM failed: %w " , err )
591
592
}
592
593
593
594
// Define space in NV storage and clean up afterwards or subsequent runs will fail.
@@ -629,7 +630,7 @@ func SealDiskKey(key []byte, pcrSel tpm2.PCRSelection) error {
629
630
630
631
// save a snapshot of PCR values
631
632
if err := saveDiskKeySealingPCRs (TpmSavedDiskSealingPcrs ); err != nil {
632
- return fmt .Errorf ("saving snapshot of sealing PCRs failed: %v " , err )
633
+ return fmt .Errorf ("saving snapshot of sealing PCRs failed: %w " , err )
633
634
}
634
635
635
636
return nil
@@ -675,7 +676,7 @@ func UnsealDiskKey(pcrSel tpm2.PCRSelection) ([]byte, error) {
675
676
676
677
sealedObjHandle , _ , err := tpm2 .Load (rw , TpmSRKHdl , "" , pub , priv )
677
678
if err != nil {
678
- return nil , fmt .Errorf ("loading the disk key into TPM failed: %v " , err )
679
+ return nil , fmt .Errorf ("loading the disk key into TPM failed: %w " , err )
679
680
}
680
681
defer tpm2 .FlushContext (rw , sealedObjHandle )
681
682
@@ -689,12 +690,12 @@ func UnsealDiskKey(pcrSel tpm2.PCRSelection) ([]byte, error) {
689
690
if err != nil {
690
691
// We get here mostly because of RCPolicyFail error, so try to get more
691
692
// information about the failure by finding the mismatching PCR index.
692
- mismatch , newErr := findMismatchingPCRs (TpmSavedDiskSealingPcrs )
693
- if newErr != nil {
694
- return nil , fmt .Errorf ("UnsealWithSession failed: %v , getting more info failed: %v" , err , newErr )
693
+ mismatch , extraErr := findMismatchingPCRs (TpmSavedDiskSealingPcrs )
694
+ if extraErr != nil {
695
+ return nil , fmt .Errorf ("UnsealWithSession failed: %w , getting more info failed: %v" , err , extraErr )
695
696
}
696
697
697
- return nil , fmt .Errorf ("UnsealWithSession failed: %v , possibly mismatching PCR indexes %v" , err , mismatch )
698
+ return nil , fmt .Errorf ("UnsealWithSession failed: %w , possibly mismatching PCR indexes %v" , err , mismatch )
698
699
}
699
700
return key , nil
700
701
}
@@ -725,7 +726,7 @@ func PolicyPCRSession(rw io.ReadWriteCloser, pcrSel tpm2.PCRSelection) (tpmutil.
725
726
726
727
policy , err := tpm2 .PolicyGetDigest (rw , session )
727
728
if err != nil {
728
- return session , nil , fmt .Errorf ("PolicyGetDigest failed: %v " , err )
729
+ return session , nil , fmt .Errorf ("PolicyGetDigest failed: %w " , err )
729
730
}
730
731
return session , policy , nil
731
732
}
@@ -816,19 +817,14 @@ func saveDiskKeySealingPCRs(pcrsFile string) error {
816
817
return err
817
818
}
818
819
819
- frw , err := os .Create (pcrsFile )
820
- if err != nil {
821
- return err
822
- }
823
- defer frw .Close ()
824
-
825
- e := gob .NewEncoder (frw )
820
+ buff := new (bytes.Buffer )
821
+ e := gob .NewEncoder (buff )
826
822
err = e .Encode (readPCRs )
827
823
if err != nil {
828
824
return err
829
825
}
830
826
831
- return nil
827
+ return fileutils . WriteRename ( pcrsFile , buff . Bytes ())
832
828
}
833
829
834
830
func findMismatchingPCRs (savedPCRsFile string ) ([]int , error ) {
0 commit comments