1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
|
// Copyright 2020 Canonical Ltd.
// Licensed under the LGPLv3 with static-linking exception.
// See LICENCE file for details.
package efi
import (
"bytes"
"crypto"
_ "crypto/sha256"
"crypto/x509"
"encoding/binary"
"errors"
"fmt"
"io"
"time"
"github.com/canonical/go-efilib/internal/ioerr"
"github.com/canonical/go-efilib/internal/uefi"
)
// VariableAuthentication corresponds to the EFI_VARIABLE_AUTHENTICATION
// type and is used to authenticate updates to variables with the
// EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS attribute set.
type VariableAuthentication struct {
MonotonicCount uint64
AuthInfo WinCertificateGUID
}
// ReadVariableAuthentication decodes an authentication header for updating
// a variable with the EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS attribute set.
func ReadVariableAuthentication(r io.Reader) (*VariableAuthentication, error) {
desc, err := uefi.Read_EFI_VARIABLE_AUTHENTICATION(r)
if err != nil {
return nil, err
}
sig, err := newWinCertificateGUID(&desc.AuthInfo)
if err != nil {
return nil, err
}
return &VariableAuthentication{
MonotonicCount: desc.MonotonicCount,
AuthInfo: sig}, nil
}
// VariableAuthentication2 corresponds to the EFI_VARIABLE_AUTHENTICATION_2
// type and is used to authenticate updates to variables with the
// EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute set.
type VariableAuthentication2 struct {
TimeStamp time.Time
AuthInfo WinCertificateGUID
}
// ReadTimeBasedVariableAuthentication decodes an authentication header
// for updating a variable with the EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
// attribute set.
func ReadTimeBasedVariableAuthentication(r io.Reader) (*VariableAuthentication2, error) {
desc, err := uefi.Read_EFI_VARIABLE_AUTHENTICATION_2(r)
if err != nil {
return nil, err
}
sig, err := newWinCertificateGUID(&desc.AuthInfo)
if err != nil {
return nil, err
}
return &VariableAuthentication2{
TimeStamp: desc.TimeStamp.GoTime(),
AuthInfo: sig}, nil
}
// VariableAuthentication3Type describes the type of [VariableAuthentication3].
type VariableAuthentication3Type int
const (
// VariableAuthentication3TimestampType indicates that a
// VariableAuthentication3 is a timestamp based enhanced authentication
// and is implemented by the *VariableAuthentication3Timestamp type.
// It also indicates that the VariableAuthentication3Descriptor is
// implemented by *VariableAuthentication3TimestampDescriptor.
VariableAuthentication3TimestampType VariableAuthentication3Type = uefi.EFI_VARIABLE_AUTHENTICATION_3_TIMESTAMP_TYPE
// VariableAuthentication3iNonceType indicates that a
// VariableAuthentication3 is a nonce based enhanced authentication
// and is implemented by the *VariableAuthentication3Nonce type.
// It also indicates that the VariableAuthentication3Descriptor is
// implemented by *VariableAuthentication3NonceDescriptor.
VariableAuthentication3NonceType VariableAuthentication3Type = uefi.EFI_VARIABLE_AUTHENTICATION_3_NONCE_TYPE
)
// VariableAuthentication3 is used to authenticate updates to variables
// with the EFI_VARIABLE_ENHANCED_AUTHENTICATED_ACCESS attribute set.
type VariableAuthentication3 interface {
Type() VariableAuthentication3Type // The authentication type (timestamp or nonce)
NewCert() WinCertificateGUID // Optional new certificate in order to rotate the signing key for authenticated writes.
SigningCert() WinCertificateGUID // The certificate associated with the current signing key.
}
type variableAuthentication3 struct {
newCert WinCertificateGUID
signingCert WinCertificateGUID
}
func (a *variableAuthentication3) NewCert() WinCertificateGUID {
return a.newCert
}
func (a *variableAuthentication3) SigningCert() WinCertificateGUID {
return a.signingCert
}
// VariableAuthentication3Timestamp is used to authenticate updates to
// variables with the EFI_VARIABLE_ENHANCED_AUTHENTICATED_ACCESS attribute
// set, and a type of EFI_VARIABLE_AUTHENTICATION_3_TIMESTAMP_TYPE.
type VariableAuthentication3Timestamp struct {
Timestamp time.Time
variableAuthentication3
}
// Type implements [VariableAuthentication3.Type].
func (a *VariableAuthentication3Timestamp) Type() VariableAuthentication3Type {
return VariableAuthentication3TimestampType
}
// VariableAuthentication3Nonce is used to authenticate updates to
// variables with the EFI_VARIABLE_ENHANCED_AUTHENTICATED_ACCESS attribute
// set, and a type of EFI_VARIABLE_AUTHENTICATION_3_NONCE_TYPE.
type VariableAuthentication3Nonce struct {
Nonce []byte
variableAuthentication3
}
// Type implements [VariableAuthentication3.Type].
func (a *VariableAuthentication3Nonce) Type() VariableAuthentication3Type {
return VariableAuthentication3NonceType
}
// ReadEnhancedVariableAuthentication decodes the authentication header for
// updating variables with the EFI_VARIABLE_ENHANCED_AUTHENTICATED_ACCESS
// attribute set.
func ReadEnhancedVariableAuthentication(r io.Reader) (VariableAuthentication3, error) {
var hdr uefi.EFI_VARIABLE_AUTHENTICATION_3
if err := binary.Read(r, binary.LittleEndian, &hdr); err != nil {
return nil, err
}
if hdr.Version != 1 {
return nil, errors.New("unexpected version")
}
lr := io.LimitReader(r, int64(hdr.MetadataSize)-int64(binary.Size(hdr)))
switch hdr.Type {
case uefi.EFI_VARIABLE_AUTHENTICATION_3_TIMESTAMP_TYPE:
var t uefi.EFI_TIME
if err := binary.Read(lr, binary.LittleEndian, &t); err != nil {
return nil, ioerr.EOFIsUnexpected("cannot read timestamp authentication: %w", err)
}
var newCert *uefi.WIN_CERTIFICATE_UEFI_GUID
if hdr.Flags&1 > 0 {
cert, err := uefi.Read_WIN_CERTIFICATE_UEFI_GUID(r)
if err != nil {
return nil, ioerr.EOFIsUnexpected("cannot read timestamp authentication: %w", err)
}
newCert = cert
}
signingCert, err := uefi.Read_WIN_CERTIFICATE_UEFI_GUID(r)
if err != nil {
return nil, ioerr.EOFIsUnexpected("cannot read timestamp authentication: %w", err)
}
sig, err := newWinCertificateGUID(signingCert)
if err != nil {
return nil, fmt.Errorf("cannot decode signature: %w", err)
}
out := &VariableAuthentication3Timestamp{
Timestamp: t.GoTime(),
variableAuthentication3: variableAuthentication3{signingCert: sig}}
if newCert != nil {
sig, err := newWinCertificateGUID(newCert)
if err != nil {
return nil, fmt.Errorf("cannot decode new authority signature: %w", err)
}
out.newCert = sig
}
return out, nil
case uefi.EFI_VARIABLE_AUTHENTICATION_3_NONCE_TYPE:
n, err := uefi.Read_EFI_VARIABLE_AUTHENTICATION_3_NONCE(r)
if err != nil {
return nil, ioerr.EOFIsUnexpected("cannot read nonce authentication: %w", err)
}
var newCert *uefi.WIN_CERTIFICATE_UEFI_GUID
if hdr.Flags&1 > 0 {
cert, err := uefi.Read_WIN_CERTIFICATE_UEFI_GUID(r)
if err != nil {
return nil, ioerr.EOFIsUnexpected("cannot read nonce authentication: %w", err)
}
newCert = cert
}
signingCert, err := uefi.Read_WIN_CERTIFICATE_UEFI_GUID(r)
if err != nil {
return nil, ioerr.EOFIsUnexpected("cannot read nonce authentication: %w", err)
}
sig, err := newWinCertificateGUID(signingCert)
if err != nil {
return nil, fmt.Errorf("cannot decode signature: %w", err)
}
out := &VariableAuthentication3Nonce{
Nonce: n.Nonce,
variableAuthentication3: variableAuthentication3{signingCert: sig}}
if newCert != nil {
sig, err := newWinCertificateGUID(newCert)
if err != nil {
return nil, fmt.Errorf("cannot decode new authority signature: %w", err)
}
out.newCert = sig
}
return out, nil
default:
return nil, errors.New("unexpected type")
}
}
// VariableAuthentication3CertIdType describes the format of the
// certificate ID.
type VariableAuthentication3CertIdType uint8
const (
// VariableAuthentication3CertIdSHA256Type indicates that a
VariableAuthentication3CertIdSHA256Type VariableAuthentication3CertIdType = uefi.EFI_VARIABLE_AUTHENTICATION_3_CERT_ID_SHA256
)
// VariableAuthentication3CertId corresponds to the EFI_VARIABLE_AUTHENTICATION_3_CERT_ID
// type and represents the identification of an authority certificate
// associated with a variable that has the EFI_VARIABLE_ENHANCED_AUTHENTICATED_ACCESS
// attribute set.
type VariableAuthentication3CertId interface {
// Type describes the format of this certificate ID.
Type() VariableAuthentication3CertIdType
// Matches determines whether the specified certificate matches this ID.
Matches(cert *x509.Certificate) bool
}
// VariableAuthentication3Descriptor corresponds to the authentication
// descriptor provided when reading the payload of a variable with the
// EFI_VARIABLE_ENHANCED_AUTHENTICATED_ACCESS attribute set.
type VariableAuthentication3Descriptor interface {
Type() VariableAuthentication3Type // The authentication type (timestamp or nonce).
Id() VariableAuthentication3CertId // The ID of the authority associated with the variable.
}
// VariableAuthentication3CertIdSHA256 corresponds to a EFI_VARIABLE_AUTHENTICATION_3_CERT_ID
// with a type of EFI_VARIABLE_AUTHENTICATION_3_CERT_ID_SHA256 and is the
// SHA-256 digest of the TBS content of a X.509 certificate.
type VariableAuthentication3CertIdSHA256 [32]byte
// Type implements [VariableAuthentication3CertId.Type].
func (VariableAuthentication3CertIdSHA256) Type() VariableAuthentication3CertIdType {
return VariableAuthentication3CertIdSHA256Type
}
// Matches implements [VariableAuthentication3CertId.Matches].
func (i VariableAuthentication3CertIdSHA256) Matches(cert *x509.Certificate) bool {
h := crypto.SHA256.New()
h.Write(cert.RawTBSCertificate)
return bytes.Equal(h.Sum(nil), i[:])
}
func newVariableAuthentication3CertId(id *uefi.EFI_VARIABLE_AUTHENTICATION_3_CERT_ID) (VariableAuthentication3CertId, error) {
switch id.Type {
case uefi.EFI_VARIABLE_AUTHENTICATION_3_CERT_ID_SHA256:
if len(id.Id) != 32 {
return nil, errors.New("invalid SHA256 length")
}
var out VariableAuthentication3CertIdSHA256
copy(out[:], id.Id)
return out, nil
default:
return nil, fmt.Errorf("unrecognized type: %d", id.Type)
}
}
// VariableAuthentication3TimestampDescriptor corresponds to the authentication
// descriptor provided when reading the payload of a variable with the
// EFI_VARIABLE_ENHANCED_AUTHENTICATED_ACCESS attribute set, and a type of
// EFI_VARIABLE_AUTHENTICATION_3_TIMESTAMP_TYPE.
type VariableAuthentication3TimestampDescriptor struct {
TimeStamp time.Time
id VariableAuthentication3CertId
}
// Type implements [VariableAuthentication3Descriptor.Type].
func (d *VariableAuthentication3TimestampDescriptor) Type() VariableAuthentication3Type {
return VariableAuthentication3TimestampType
}
// Id implements [VariableAuthentication3Descriptor.Id].
func (d *VariableAuthentication3TimestampDescriptor) Id() VariableAuthentication3CertId {
return d.id
}
// VariableAuthentication3NonceDescriptor corresponds to the authentication
// descriptor provided when reading the payload of a variable with the
// EFI_VARIABLE_ENHANCED_AUTHENTICATED_ACCESS attribute set, and a type of
// EFI_VARIABLE_AUTHENTICATION_3_NONCE_TYPE.
type VariableAuthentication3NonceDescriptor struct {
Nonce []byte
id VariableAuthentication3CertId
}
// Type implements [VariableAuthentication3Descriptor.Type].
func (d *VariableAuthentication3NonceDescriptor) Type() VariableAuthentication3Type {
return VariableAuthentication3NonceType
}
// Id implements [VariableAuthentication3Descriptor.Id].
func (d *VariableAuthentication3NonceDescriptor) Id() VariableAuthentication3CertId {
return d.id
}
// ReadEnhancedAuthenticationDescriptor decodes the enhanced authentication
// descriptor from the supplied reader. The supplied reader will typically
// read from the payload area of a variable with the
// EFI_VARIABLE_ENHANCED_AUTHENTICATION_ACCESS attribute set.
func ReadEnhancedAuthenticationDescriptor(r io.Reader) (VariableAuthentication3Descriptor, error) {
var hdr uefi.EFI_VARIABLE_AUTHENTICATION_3
if err := binary.Read(r, binary.LittleEndian, &hdr); err != nil {
return nil, err
}
if hdr.Version != 1 {
return nil, errors.New("unexpected version")
}
lr := io.LimitReader(r, int64(hdr.MetadataSize)-int64(binary.Size(hdr)))
switch hdr.Type {
case uefi.EFI_VARIABLE_AUTHENTICATION_3_TIMESTAMP_TYPE:
var t uefi.EFI_TIME
if err := binary.Read(lr, binary.LittleEndian, &t); err != nil {
return nil, ioerr.EOFIsUnexpected("cannot read timestamp descriptor: %w", err)
}
id, err := uefi.Read_EFI_VARIABLE_AUTHENTICATION_3_CERT_ID(r)
if err != nil {
return nil, ioerr.EOFIsUnexpected("cannot read timestamp descriptor: %w", err)
}
id2, err := newVariableAuthentication3CertId(id)
if err != nil {
return nil, fmt.Errorf("invalid timestamp descriptor ID: %w", err)
}
return &VariableAuthentication3TimestampDescriptor{
TimeStamp: t.GoTime(),
id: id2}, nil
case uefi.EFI_VARIABLE_AUTHENTICATION_3_NONCE_TYPE:
n, err := uefi.Read_EFI_VARIABLE_AUTHENTICATION_3_NONCE(r)
if err != nil {
return nil, ioerr.EOFIsUnexpected("cannot read nonce descriptor: %w", err)
}
id, err := uefi.Read_EFI_VARIABLE_AUTHENTICATION_3_CERT_ID(r)
if err != nil {
return nil, ioerr.EOFIsUnexpected("cannot read nonce descriptor: %w", err)
}
id2, err := newVariableAuthentication3CertId(id)
if err != nil {
return nil, fmt.Errorf("invalid nonce descriptor ID: %w", err)
}
return &VariableAuthentication3NonceDescriptor{
Nonce: n.Nonce,
id: id2}, nil
default:
return nil, errors.New("unexpected type")
}
}
|