@@ -189,7 +189,6 @@ func ReadOwnerCrdl() (string, error) {
189
189
// TpmSign is used by external packages to get a digest signed by
190
190
// device key in TPM
191
191
func TpmSign (digest []byte ) (* big.Int , * big.Int , error ) {
192
-
193
192
rw , err := tpm2 .OpenTPM (TpmDevicePath )
194
193
if err != nil {
195
194
return nil , nil , err
@@ -198,7 +197,7 @@ func TpmSign(digest []byte) (*big.Int, *big.Int, error) {
198
197
199
198
tpmOwnerPasswd , err := ReadOwnerCrdl ()
200
199
if err != nil {
201
- return nil , nil , fmt .Errorf ("Error in fetching TPM credentials: %v" , err )
200
+ return nil , nil , fmt .Errorf ("fetching TPM credentials failed : %v" , err )
202
201
}
203
202
204
203
//XXX This "32" should really come from Hash algo used.
@@ -213,7 +212,7 @@ func TpmSign(digest []byte) (*big.Int, *big.Int, error) {
213
212
sig , err := tpm2 .Sign (rw , TpmDeviceKeyHdl ,
214
213
tpmOwnerPasswd , digest , nil , scheme )
215
214
if err != nil {
216
- return nil , nil , fmt .Errorf ("Sign using TPM failed with error %v" , err )
215
+ return nil , nil , fmt .Errorf ("signing data using TPM failed: %v" , err )
217
216
}
218
217
return sig .ECC .R , sig .ECC .S , nil
219
218
}
@@ -266,7 +265,6 @@ func GetFirmwareVersion(v1 uint32, v2 uint32) string {
266
265
267
266
// GetTpmProperty fetches a given property id, and returns it as uint32
268
267
func GetTpmProperty (propID tpm2.TPMProp ) (uint32 , error ) {
269
-
270
268
rw , err := tpm2 .OpenTPM (TpmDevicePath )
271
269
if err != nil {
272
270
return 0 , err
@@ -280,7 +278,7 @@ func GetTpmProperty(propID tpm2.TPMProp) (uint32, error) {
280
278
}
281
279
prop , ok := v [0 ].(tpm2.TaggedProperty )
282
280
if ! ok {
283
- return 0 , fmt .Errorf ("Unable to fetch property %d " , propID )
281
+ return 0 , fmt .Errorf ("fetching TPM property %X failed " , propID )
284
282
}
285
283
return prop .Value , nil
286
284
}
@@ -340,7 +338,6 @@ const (
340
338
341
339
// FetchTpmHwInfo returns TPM Hardware properties in a string
342
340
func FetchTpmHwInfo () (string , error ) {
343
-
344
341
//If we had done this earlier, return the last result
345
342
if tpmHwInfo != "" {
346
343
return tpmHwInfo , nil
@@ -386,7 +383,7 @@ func FetchVaultKey(log *base.LogObject) ([]byte, error) {
386
383
//First try to read from TPM, if it was stored earlier
387
384
key , err := readDiskKey ()
388
385
if err != nil {
389
- log .Noticef ("Generating VaultKey " )
386
+ log .Noticef ("can't read the legacy disk key, generating a new one " )
390
387
//
391
388
//Note on why we are using GetRandom here:
392
389
//We are using raw_key option to protect the encryption/decryption protector:
@@ -405,14 +402,14 @@ func FetchVaultKey(log *base.LogObject) ([]byte, error) {
405
402
//
406
403
key , err = GetRandom (vaultKeyLength )
407
404
if err != nil {
408
- return nil , fmt .Errorf ("FetchVaultKey: Error in GetRandom: %v" , err )
405
+ return nil , fmt .Errorf ("GetRandom failed : %v" , err )
409
406
}
410
407
err = writeDiskKey (key )
411
408
if err != nil {
412
- return nil , fmt .Errorf ("FetchVaultKey: Writing Key to TPM failed: %v" , err )
409
+ return nil , fmt .Errorf ("writing legacy Key to TPM failed: %v" , err )
413
410
}
414
411
} else {
415
- log .Noticef ("Found VaultKey " )
412
+ log .Noticef ("successfully read the legacy disk key from TPM " )
416
413
}
417
414
return key , nil
418
415
}
@@ -484,7 +481,7 @@ func FetchSealedVaultKey(log *base.LogObject) ([]byte, error) {
484
481
legacyKeyPresent := isLegacyKeyPresent ()
485
482
486
483
if ! sealedKeyPresent && ! legacyKeyPresent {
487
- log .Noticef ("FetchSealedVaultKey generate new key" )
484
+ log .Noticef ("neither legacy nor sealed disk key present, generating a fresh key" )
488
485
//Fresh install, generate a new key
489
486
//
490
487
//Note on why we are using GetRandom here:
@@ -504,16 +501,18 @@ func FetchSealedVaultKey(log *base.LogObject) ([]byte, error) {
504
501
//
505
502
key , err := GetRandom (vaultKeyLength )
506
503
if err != nil {
507
- return nil , fmt .Errorf ("FetchSealedVaultKey: GetRandom failed, %v" , err )
504
+ return nil , fmt .Errorf ("GetRandom failed: %v" , err )
508
505
}
509
506
err = SealDiskKey (key , DiskKeySealingPCRs )
510
507
if err != nil {
511
- return nil , fmt .Errorf ("FetchSealedVaultKey: Sealing failed: %v" , err )
508
+ return nil , fmt .Errorf ("sealing the fresh disk key failed: %v" , err )
512
509
}
510
+
511
+ log .Noticef ("successfully sealed the fresh disk key into TPM" )
513
512
}
514
513
515
514
if ! sealedKeyPresent && legacyKeyPresent {
516
- log .Noticef ("FetchSealedVaultKey legacy present" )
515
+ log .Noticef ("only legacy disk key present, using it " )
517
516
//XXX: we need a migration path for existing installations.
518
517
//hence re-using the current key here. i.e. if we end up creating
519
518
//a new random key here, and we fail the upgrade, the fallback
@@ -526,20 +525,29 @@ func FetchSealedVaultKey(log *base.LogObject) ([]byte, error) {
526
525
//Upgrade path will be to first upgrade to a) first release and then b)
527
526
key , err := readDiskKey ()
528
527
if err != nil {
529
- return nil , fmt .Errorf ("Error in retrieving old key" )
528
+ return nil , fmt .Errorf ("retrieving the legacy disk key from TPM failed: %v" , err )
530
529
}
530
+
531
+ log .Noticef ("try to convert the legacy key into a sealed key" )
532
+
531
533
err = SealDiskKey (key , DiskKeySealingPCRs )
532
534
if err != nil {
533
- return nil , fmt .Errorf ("FetchSealedVaultKey: Sealing failed: %v" , err )
535
+ return nil , fmt .Errorf ("sealing the legacy disk key into TPM failed: %v" , err )
534
536
}
535
537
}
536
538
//sealedKeyPresent && !legacyKeyPresent : unseal
537
539
//sealedKeyPresent && legacyKeyPresent : unseal
538
540
if sealedKeyPresent {
539
- log .Noticef ("FetchSealedVaultKey unseal key" )
541
+ log .Noticef ("sealed disk key present int TPM, about to unseal it " )
540
542
}
541
543
//By this, we have a key sealed into TPM
542
- return UnsealDiskKey (DiskKeySealingPCRs )
544
+ key , err := UnsealDiskKey (DiskKeySealingPCRs )
545
+ if err == nil {
546
+ // be more verbose, lets celebrate
547
+ log .Noticef ("successfully unsealed the disk key from TPM" )
548
+ }
549
+
550
+ return key , err
543
551
}
544
552
545
553
// SealDiskKey seals key into TPM2.0, with provided PCRs
@@ -574,12 +582,12 @@ func SealDiskKey(key []byte, pcrSel tpm2.PCRSelection) error {
574
582
575
583
//Don't need the handle, we need only the policy for sealing
576
584
if err := tpm2 .FlushContext (rw , session ); err != nil {
577
- return fmt .Errorf ("Unable to flush session handle %v: %v" , session , err )
585
+ return fmt .Errorf ("flushing session handle %v failed : %v" , session , err )
578
586
}
579
587
580
588
priv , public , err := tpm2 .Seal (rw , TpmSRKHdl , EmptyPassword , EmptyPassword , policy , key )
581
589
if err != nil {
582
- return fmt .Errorf ("Unable to seal key: %v" , err )
590
+ return fmt .Errorf ("sealing the disk key into TPM failed : %v" , err )
583
591
}
584
592
585
593
// Define space in NV storage and clean up afterwards or subsequent runs will fail.
@@ -667,7 +675,7 @@ func UnsealDiskKey(pcrSel tpm2.PCRSelection) ([]byte, error) {
667
675
668
676
sealedObjHandle , _ , err := tpm2 .Load (rw , TpmSRKHdl , "" , pub , priv )
669
677
if err != nil {
670
- return nil , fmt .Errorf ("Load failed: %v" , err )
678
+ return nil , fmt .Errorf ("loading the disk key into TPM failed: %v" , err )
671
679
}
672
680
defer tpm2 .FlushContext (rw , sealedObjHandle )
673
681
@@ -683,7 +691,7 @@ func UnsealDiskKey(pcrSel tpm2.PCRSelection) ([]byte, error) {
683
691
// information about the failure by finding the mismatching PCR index.
684
692
mismatch , newErr := findMismatchingPCRs (TpmSavedDiskSealingPcrs )
685
693
if newErr != nil {
686
- return nil , fmt .Errorf ("UnsealWithSession failed: %v, failed to get more info: %v" , err , newErr )
694
+ return nil , fmt .Errorf ("UnsealWithSession failed: %v, getting more info failed : %v" , err , newErr )
687
695
}
688
696
689
697
return nil , fmt .Errorf ("UnsealWithSession failed: %v, possibly mismatching PCR indexes %v" , err , mismatch )
@@ -717,7 +725,7 @@ func PolicyPCRSession(rw io.ReadWriteCloser, pcrSel tpm2.PCRSelection) (tpmutil.
717
725
718
726
policy , err := tpm2 .PolicyGetDigest (rw , session )
719
727
if err != nil {
720
- return session , nil , fmt .Errorf ("Unable to get policy digest : %v" , err )
728
+ return session , nil , fmt .Errorf ("PolicyGetDigest failed : %v" , err )
721
729
}
722
730
return session , policy , nil
723
731
}
0 commit comments