github.com/xyproto/u-root@v6.0.1-0.20200302025726-5528e0c77a3c+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  	"io"
    12  )
    13  
    14  /*
    15   * pcr number where all measurements taken by securelaunch pkg
    16   * will be stored.
    17   */
    18  const (
    19  	pcr = int(22)
    20  )
    21  
    22  /*
    23   * all collectors (storage, dmi, cpuid, files) should satisfy this
    24   * collectors get information and store the hash of that information in pcr
    25   * owned by the tpm device.
    26   */
    27  type Collector interface {
    28  	Collect(tpmHandle io.ReadWriteCloser) error
    29  }
    30  
    31  var supportedCollectors = map[string]func([]byte) (Collector, error){
    32  	"storage": NewStorageCollector,
    33  	"dmi":     NewDmiCollector,
    34  	"files":   NewFileCollector,
    35  	"cpuid":   NewCPUIDCollector,
    36  }
    37  
    38  /*
    39   * GetCollector calls the appropriate init handlers for a particular
    40   * collector JSON object argument and returns a new Collector Interface.
    41   * - error is returned if unmarshalling fails or an unsupported collector is
    42   * passed as an argument.
    43   */
    44  func GetCollector(config []byte) (Collector, error) {
    45  	var header struct {
    46  		Type string `json:"type"`
    47  	}
    48  	err := json.Unmarshal(config, &header)
    49  	if err != nil {
    50  		fmt.Printf("Measurement: Unmarshal error\n")
    51  		return nil, err
    52  	}
    53  
    54  	if init, ok := supportedCollectors[header.Type]; ok {
    55  		return init(config)
    56  	}
    57  
    58  	return nil, fmt.Errorf("unsupported collector %s", header.Type)
    59  }