github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/pkg/securelaunch/measurement/collectors.go (about)

     1  // Copyright 2019 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package measurement provides different collectors to hash files, disks, dmi info and cpuid info.
     6  package measurement
     7  
     8  import (
     9  	"encoding/json"
    10  	"fmt"
    11  )
    12  
    13  /*
    14   * pcr number where all measurements taken by securelaunch pkg
    15   * will be stored.
    16   */
    17  const (
    18  	pcr = uint32(22)
    19  )
    20  
    21  /*
    22   * all collectors (storage, dmi, cpuid, files) should satisfy this
    23   * collectors get information and store the hash of that information in pcr
    24   * owned by the tpm device.
    25   */
    26  type Collector interface {
    27  	Collect() error
    28  }
    29  
    30  var supportedCollectors = map[string]func([]byte) (Collector, error){
    31  	"storage": NewStorageCollector,
    32  	"dmi":     NewDmiCollector,
    33  	"files":   NewFileCollector,
    34  	"cpuid":   NewCPUIDCollector,
    35  }
    36  
    37  /*
    38   * GetCollector calls the appropriate init handlers for a particular
    39   * collector JSON object argument and returns a new Collector Interface.
    40   * - error is returned if unmarshalling fails or an unsupported collector is
    41   * passed as an argument.
    42   */
    43  func GetCollector(config []byte) (Collector, error) {
    44  	var header struct {
    45  		Type string `json:"type"`
    46  	}
    47  	err := json.Unmarshal(config, &header)
    48  	if err != nil {
    49  		fmt.Printf("Measurement: Unmarshal error\n")
    50  		return nil, err
    51  	}
    52  
    53  	if init, ok := supportedCollectors[header.Type]; ok {
    54  		return init(config)
    55  	}
    56  
    57  	return nil, fmt.Errorf("unsupported collector %s", header.Type)
    58  }