github.com/transparency-dev/armored-witness-os@v0.1.3-0.20240514084412-27eef7325168/api/api.go (about)

     1  // Copyright 2022 The Armored Witness OS authors. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package api
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  
    21  	"google.golang.org/protobuf/proto"
    22  
    23  	"github.com/gsora/fidati/u2fhid"
    24  )
    25  
    26  const (
    27  	// http://pid.codes/1209/2702/
    28  	VendorID  = 0x1209
    29  	ProductID = 0x2702
    30  
    31  	HIDUsagePage = 0xff00
    32  
    33  	// Maximum Message size according to U2F HID standard (see formula in
    34  	// [FIDO U2F // HID Protocol Specification, 2.4]).
    35  	MaxMessageSize = 7609
    36  )
    37  
    38  // U2FHID vendor specific commands
    39  const (
    40  	// Status
    41  	U2FHID_ARMORY_INF = iota + u2fhid.VendorCommandFirst
    42  	// Trusted Applet configuration
    43  	U2FHID_ARMORY_CFG
    44  	// Obsolete
    45  	U2FHID_ARMORY_OTA_UNUSED
    46  	// Set HAB fuse to built-in SRK hash
    47  	U2FHID_ARMORY_HAB
    48  	// Fetch latest debug/console logs
    49  	U2FHID_ARMORY_CONSOLE_LOGS
    50  	// Fetch stored crash logs from most recent applet crash
    51  	U2FHID_ARMORY_CRASH_LOGS
    52  )
    53  
    54  var emptyResponse []byte
    55  
    56  // ErrorResponse converts an error in an API Message.
    57  func ErrorResponse(err error) (res []byte) {
    58  	msg := &Response{
    59  		Error:   ErrorCode_GENERIC_ERROR,
    60  		Payload: []byte(err.Error()),
    61  	}
    62  
    63  	res, _ = proto.Marshal(msg)
    64  
    65  	return
    66  }
    67  
    68  // EmptyResponse for when no relevant data is available.
    69  func EmptyResponse() []byte {
    70  	if len(emptyResponse) == 0 {
    71  		emptyResponse, _ = proto.Marshal(&Response{})
    72  	}
    73  
    74  	return emptyResponse
    75  }
    76  
    77  // Bytes serializes an API message.
    78  func (p *Response) Bytes() (buf []byte) {
    79  	buf, _ = proto.Marshal(p)
    80  	return
    81  }
    82  
    83  // Bytes serializes an API message.
    84  func (p *Configuration) Bytes() (buf []byte) {
    85  	buf, _ = proto.Marshal(p)
    86  	return
    87  }
    88  
    89  // Print returns the Trusted OS status in textual format.
    90  func (p *Status) Print() string {
    91  	var status bytes.Buffer
    92  
    93  	status.WriteString("----------------------------------------------------------- Trusted OS ----\n")
    94  	status.WriteString(fmt.Sprintf("Serial number ..............: %s\n", p.Serial))
    95  	status.WriteString(fmt.Sprintf("Secure Boot ................: %v\n", p.HAB))
    96  	status.WriteString(fmt.Sprintf("SRK hash ...................: %s\n", p.SRKHash))
    97  	status.WriteString(fmt.Sprintf("Revision ...................: %s\n", p.Revision))
    98  	status.WriteString(fmt.Sprintf("Version ....................: %s\n", p.Version))
    99  	status.WriteString(fmt.Sprintf("Runtime ....................: %s\n", p.Runtime))
   100  	status.WriteString(fmt.Sprintf("Link .......................: %v\n", p.Link))
   101  	status.WriteString(fmt.Sprintf("MAC ........................: %v\n", p.MAC))
   102  	status.WriteString(fmt.Sprintf("IdentityCounter ............: %d\n", p.IdentityCounter))
   103  	if p.Witness != nil {
   104  		status.WriteString(fmt.Sprintf("Witness/Identity ...........: %v\n", p.Witness.Identity))
   105  		status.WriteString(fmt.Sprintf("Witness/IP .................: %v\n", p.Witness.IP))
   106  		status.WriteString(fmt.Sprintf("Witness/AttestationKey .....: %v", p.Witness.IDAttestPublicKey))
   107  	} else {
   108  		status.WriteString(fmt.Sprint("Witness ....................: <no status>"))
   109  	}
   110  
   111  	return status.String()
   112  }