Coverage Report

Created: 2026-12-01 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/fwupd/libfwupdplugin/fu-efi-signature.c
Line
Count
Source
1
/*
2
 * Copyright 2020 Richard Hughes <richard@hughsie.com>
3
 *
4
 * SPDX-License-Identifier: LGPL-2.1-or-later
5
 */
6
7
#include "config.h"
8
9
#include "fu-byte-array.h"
10
#include "fu-bytes.h"
11
#include "fu-common.h"
12
#include "fu-efi-common.h"
13
#include "fu-efi-signature-private.h"
14
#include "fu-efi-struct.h"
15
#include "fu-input-stream.h"
16
#include "fu-version-common.h"
17
18
/**
19
 * FuEfiSignature:
20
 *
21
 * A UEFI Signature as found in an `EFI_SIGNATURE_LIST`.
22
 *
23
 * See also: [class@FuFirmware]
24
 */
25
26
typedef struct {
27
  FuEfiSignatureKind kind;
28
  gchar *owner;
29
  gchar *svn_guid; /* only for FU_EFI_SIGNATURE_KIND_SVN */
30
} FuEfiSignaturePrivate;
31
32
0
G_DEFINE_TYPE_WITH_PRIVATE(FuEfiSignature, fu_efi_signature, FU_TYPE_FIRMWARE)
33
0
#define GET_PRIVATE(o) (fu_efi_signature_get_instance_private(o))
34
35
static void
36
fu_efi_signature_export(FuFirmware *firmware,
37
      FuFirmwareExportFlags flags,
38
      FwupdJsonObject *json_object)
39
0
{
40
0
  FuEfiSignature *self = FU_EFI_SIGNATURE(firmware);
41
0
  FuEfiSignaturePrivate *priv = GET_PRIVATE(self);
42
0
  const gchar *kind_str = fu_efi_signature_kind_to_string(priv->kind);
43
44
0
  if (kind_str != NULL)
45
0
    fwupd_json_object_add_string(json_object, "kind", kind_str);
46
0
  if (priv->owner != NULL)
47
0
    fwupd_json_object_add_string(json_object, "owner", priv->owner);
48
0
  if (priv->svn_guid != NULL)
49
0
    fwupd_json_object_add_string(json_object, "svn_guid", priv->svn_guid);
50
51
  /* special case: this is *literally* a hash */
52
0
  if (priv->kind == FU_EFI_SIGNATURE_KIND_SHA256) {
53
0
    g_autoptr(GBytes) blob = fu_firmware_get_bytes(firmware, NULL);
54
0
    if (blob != NULL) {
55
0
      g_autofree gchar *str = fu_bytes_to_string(blob);
56
0
      fwupd_json_object_add_string(json_object, "checksum", str);
57
0
    }
58
0
  }
59
0
}
60
61
/**
62
 * fu_efi_signature_new: (skip):
63
 * @kind: A #FuEfiSignatureKind
64
 *
65
 * Creates a new EFI_SIGNATURE.
66
 *
67
 * Returns: (transfer full): signature
68
 **/
69
FuEfiSignature *
70
fu_efi_signature_new(FuEfiSignatureKind kind)
71
0
{
72
0
  g_autoptr(FuEfiSignature) self = g_object_new(FU_TYPE_EFI_SIGNATURE, NULL);
73
0
  fu_efi_signature_set_kind(self, kind);
74
0
  return g_steal_pointer(&self);
75
0
}
76
77
/**
78
 * fu_efi_signature_get_kind:
79
 * @self: A #FuEfiSignature
80
 *
81
 * Returns the signature kind.
82
 *
83
 * Returns: #FuEfiSignatureKind, e.g. %FU_EFI_SIGNATURE_KIND_SHA256
84
 **/
85
FuEfiSignatureKind
86
fu_efi_signature_get_kind(FuEfiSignature *self)
87
0
{
88
0
  FuEfiSignaturePrivate *priv = GET_PRIVATE(self);
89
0
  g_return_val_if_fail(FU_IS_EFI_SIGNATURE(self), FU_EFI_SIGNATURE_KIND_UNKNOWN);
90
0
  return priv->kind;
91
0
}
92
93
/* private */
94
void
95
fu_efi_signature_set_owner(FuEfiSignature *self, const gchar *owner)
96
0
{
97
0
  FuEfiSignaturePrivate *priv = GET_PRIVATE(self);
98
0
  g_return_if_fail(FU_IS_EFI_SIGNATURE(self));
99
0
  g_set_str(&priv->owner, owner);
100
0
}
101
102
/* private */
103
void
104
fu_efi_signature_set_kind(FuEfiSignature *self, FuEfiSignatureKind kind)
105
0
{
106
0
  FuEfiSignaturePrivate *priv = GET_PRIVATE(self);
107
0
  g_return_if_fail(FU_IS_EFI_SIGNATURE(self));
108
0
  priv->kind = kind;
109
0
  if (priv->kind == FU_EFI_SIGNATURE_KIND_EXTERNAL) {
110
0
    g_autoptr(GBytes) blob = g_bytes_new_static("\x00", 1);
111
0
    fu_efi_signature_set_owner(self, FU_EFI_SIGNATURE_GUID_EXTERNAL);
112
0
    fu_firmware_set_bytes(FU_FIRMWARE(self), blob);
113
0
  }
114
0
  if (priv->kind == FU_EFI_SIGNATURE_KIND_SVN)
115
0
    fu_firmware_set_version_format(FU_FIRMWARE(self), FWUPD_VERSION_FORMAT_PAIR);
116
0
}
117
118
/**
119
 * fu_efi_signature_get_owner:
120
 * @self: A #FuEfiSignature
121
 *
122
 * Returns the GUID of the signature owner.
123
 *
124
 * Returns: GUID owner, perhaps %FU_EFI_SIGNATURE_GUID_MICROSOFT
125
 **/
126
const gchar *
127
fu_efi_signature_get_owner(FuEfiSignature *self)
128
0
{
129
0
  FuEfiSignaturePrivate *priv = GET_PRIVATE(self);
130
0
  g_return_val_if_fail(FU_IS_EFI_SIGNATURE(self), NULL);
131
0
  return priv->owner;
132
0
}
133
134
/**
135
 * fu_efi_signature_get_svn_guid:
136
 * @self: A #FuEfiSignature
137
 *
138
 * Returns the SVN GUID of the signature owner.
139
 *
140
 * Returns: GUID, or %NULL if not a %FU_EFI_SIGNATURE_KIND_SVN kind
141
 **/
142
const gchar *
143
fu_efi_signature_get_svn_guid(FuEfiSignature *self)
144
0
{
145
0
  FuEfiSignaturePrivate *priv = GET_PRIVATE(self);
146
0
  g_return_val_if_fail(FU_IS_EFI_SIGNATURE(self), NULL);
147
0
  return priv->svn_guid;
148
0
}
149
150
/* an SVN is stored as a fake SHA256 hash with a fixed header and zero padding */
151
static gboolean
152
fu_efi_signature_parse_svn(FuEfiSignature *self, FuInputStream *stream, GError **error)
153
0
{
154
0
  FuEfiSignaturePrivate *priv = GET_PRIVATE(self);
155
0
  const gsize offset = sizeof(fwupd_guid_t);
156
0
  guint8 version_hint = 0;
157
0
  g_autofree gchar *guid_str = NULL;
158
0
  g_autoptr(FuStructEfiSignatureSvn) st = NULL;
159
0
  g_autoptr(GError) error_local = NULL;
160
161
  /* almost every entry is a real hash, so failing is expected */
162
0
  if (!fu_input_stream_read_u8(stream, offset, &version_hint, error))
163
0
    return FALSE;
164
0
  if (version_hint != FU_STRUCT_EFI_SIGNATURE_SVN_DEFAULT_VERSION)
165
0
    return TRUE;
166
0
  st = fu_struct_efi_signature_svn_parse_stream(stream, offset, &error_local);
167
0
  if (st == NULL) {
168
0
    if (g_error_matches(error_local, FWUPD_ERROR, FWUPD_ERROR_INVALID_DATA))
169
0
      return TRUE;
170
0
    g_propagate_error(error, g_steal_pointer(&error_local));
171
0
    return FALSE;
172
0
  }
173
0
  fu_efi_signature_set_kind(self, FU_EFI_SIGNATURE_KIND_SVN);
174
0
  guid_str = fwupd_guid_to_string(fu_struct_efi_signature_svn_get_guid(st),
175
0
          FWUPD_GUID_FLAG_MIXED_ENDIAN);
176
0
  g_set_str(&priv->svn_guid, guid_str);
177
0
  fu_firmware_set_version_raw(FU_FIRMWARE(self),
178
0
            ((guint32)fu_struct_efi_signature_svn_get_major(st) << 16) |
179
0
          fu_struct_efi_signature_svn_get_minor(st));
180
181
  /* success */
182
0
  return TRUE;
183
0
}
184
185
static gboolean
186
fu_efi_signature_parse(FuFirmware *firmware,
187
           FuInputStream *stream,
188
           FuFirmwareParseFlags flags,
189
           GError **error)
190
0
{
191
0
  FuEfiSignature *self = FU_EFI_SIGNATURE(firmware);
192
0
  FuEfiSignaturePrivate *priv = GET_PRIVATE(self);
193
0
  fwupd_guid_t guid = {0};
194
0
  gsize size = fu_firmware_get_size(firmware);
195
0
  g_autoptr(GBytes) data = NULL;
196
197
  /* sanity check */
198
0
  if (size <= sizeof(fwupd_guid_t)) {
199
0
    g_set_error(error,
200
0
          FWUPD_ERROR,
201
0
          FWUPD_ERROR_INVALID_DATA,
202
0
          "SignatureSize invalid: 0x%x",
203
0
          (guint)size);
204
0
    return FALSE;
205
0
  }
206
207
  /* GUID */
208
0
  if (!fu_input_stream_read_safe(stream,
209
0
               (guint8 *)&guid,
210
0
               sizeof(guid),
211
0
               0x0, /* seek to */
212
0
               0x0, /* offset */
213
0
               sizeof(guid),
214
0
               error)) {
215
0
    g_prefix_error_literal(error, "failed to read signature GUID: ");
216
0
    return FALSE;
217
0
  }
218
0
  g_free(priv->owner);
219
0
  priv->owner = fwupd_guid_to_string(&guid, FWUPD_GUID_FLAG_MIXED_ENDIAN);
220
221
  /* if data size is unspecified, we'll use the stream size */
222
0
  data = fu_input_stream_read_bytes(stream,
223
0
            sizeof(fwupd_guid_t),
224
0
            size - sizeof(fwupd_guid_t),
225
0
            NULL,
226
0
            error);
227
0
  if (data == NULL) {
228
0
    g_prefix_error_literal(error, "failed to read signature data: ");
229
0
    return FALSE;
230
0
  }
231
0
  fu_firmware_set_bytes(firmware, data);
232
233
  /* SVNs pretend to be SHA256 hashes */
234
0
  if (priv->kind == FU_EFI_SIGNATURE_KIND_SHA256 &&
235
0
      g_bytes_get_size(data) == FU_STRUCT_EFI_SIGNATURE_SVN_SIZE) {
236
0
    if (!fu_efi_signature_parse_svn(self, stream, error))
237
0
      return FALSE;
238
0
  }
239
240
  /* success */
241
0
  return TRUE;
242
0
}
243
244
/* SVN is generated from the GUID and version */
245
static GBytes *
246
fu_efi_signature_get_svn_payload(FuEfiSignature *self, GError **error)
247
0
{
248
0
  FuEfiSignaturePrivate *priv = GET_PRIVATE(self);
249
0
  guint64 version_raw;
250
0
  fwupd_guid_t guid = {0};
251
0
  g_autoptr(FuStructEfiSignatureSvn) st = NULL;
252
253
0
  if (priv->svn_guid == NULL) {
254
0
    g_set_error_literal(error,
255
0
            FWUPD_ERROR,
256
0
            FWUPD_ERROR_INVALID_DATA,
257
0
            "SVN GUID not set");
258
0
    return NULL;
259
0
  }
260
0
  if (!fwupd_guid_from_string(priv->svn_guid, &guid, FWUPD_GUID_FLAG_MIXED_ENDIAN, error))
261
0
    return NULL;
262
0
  version_raw = fu_firmware_get_version_raw(FU_FIRMWARE(self));
263
0
  st = fu_struct_efi_signature_svn_new();
264
0
  fu_struct_efi_signature_svn_set_guid(st, &guid);
265
0
  fu_struct_efi_signature_svn_set_major(st, (version_raw >> 16) & G_MAXUINT16);
266
0
  fu_struct_efi_signature_svn_set_minor(st, version_raw & G_MAXUINT16);
267
0
  return g_bytes_new(st->buf->data, st->buf->len);
268
0
}
269
270
static GBytes *
271
fu_efi_signature_get_payload(FuEfiSignature *self, GError **error)
272
0
{
273
0
  FuEfiSignaturePrivate *priv = GET_PRIVATE(self);
274
0
  if (priv->kind == FU_EFI_SIGNATURE_KIND_SVN)
275
0
    return fu_efi_signature_get_svn_payload(self, error);
276
0
  return fu_firmware_get_bytes_with_patches(FU_FIRMWARE(self), error);
277
0
}
278
279
static GByteArray *
280
fu_efi_signature_write(FuFirmware *firmware, GError **error)
281
0
{
282
0
  FuEfiSignature *self = FU_EFI_SIGNATURE(firmware);
283
0
  FuEfiSignaturePrivate *priv = GET_PRIVATE(self);
284
0
  fwupd_guid_t owner = {0};
285
0
  g_autoptr(GByteArray) buf = g_byte_array_new();
286
0
  g_autoptr(GBytes) data = NULL;
287
288
  /* optional owner */
289
0
  if (priv->owner != NULL) {
290
0
    if (!fwupd_guid_from_string(priv->owner,
291
0
              &owner,
292
0
              FWUPD_GUID_FLAG_MIXED_ENDIAN,
293
0
              error))
294
0
      return NULL;
295
0
  }
296
0
  g_byte_array_append(buf, owner, sizeof(owner));
297
298
  /* data */
299
0
  data = fu_efi_signature_get_payload(self, error);
300
0
  if (data == NULL)
301
0
    return NULL;
302
0
  fu_byte_array_append_bytes(buf, data);
303
304
  /* success */
305
0
  return g_steal_pointer(&buf);
306
0
}
307
308
static gboolean
309
fu_efi_signature_build(FuFirmware *firmware, FwupdJsonObject *json_object, GError **error)
310
0
{
311
0
  FuEfiSignature *self = FU_EFI_SIGNATURE(firmware);
312
0
  FuEfiSignaturePrivate *priv = GET_PRIVATE(self);
313
0
  const gchar *tmp;
314
315
  /* optional properties */
316
0
  if (!fwupd_json_object_get_string_optional(json_object, "kind", &tmp, error))
317
0
    return FALSE;
318
0
  if (tmp != NULL) {
319
0
    FuEfiSignatureKind kind = fu_efi_signature_kind_from_string(tmp);
320
0
    if (kind == FU_EFI_SIGNATURE_KIND_UNKNOWN) {
321
0
      g_set_error(error,
322
0
            FWUPD_ERROR,
323
0
            FWUPD_ERROR_INVALID_DATA,
324
0
            "invalid kind: %s",
325
0
            tmp);
326
0
      return FALSE;
327
0
    }
328
0
    fu_efi_signature_set_kind(self, kind);
329
0
  }
330
0
  if (!fwupd_json_object_get_string_optional(json_object, "owner", &tmp, error))
331
0
    return FALSE;
332
0
  if (tmp != NULL) {
333
0
    if (!fwupd_guid_from_string(tmp, NULL, FWUPD_GUID_FLAG_MIXED_ENDIAN, error)) {
334
0
      g_prefix_error(error, "failed to parse owner %s, expected GUID: ", tmp);
335
0
      return FALSE;
336
0
    }
337
0
    fu_efi_signature_set_owner(self, tmp);
338
0
  }
339
0
  if (priv->kind == FU_EFI_SIGNATURE_KIND_SVN) {
340
0
    tmp = fwupd_json_object_get_string(json_object, "svn_guid", error);
341
0
    if (tmp == NULL)
342
0
      return FALSE;
343
0
    if (!fwupd_guid_from_string(tmp, NULL, FWUPD_GUID_FLAG_MIXED_ENDIAN, error)) {
344
0
      g_prefix_error(error, "failed to parse svn_guid %s, expected GUID: ", tmp);
345
0
      return FALSE;
346
0
    }
347
0
    g_set_str(&priv->svn_guid, tmp);
348
0
  }
349
0
  if (!fwupd_json_object_get_string_optional(json_object, "checksum", &tmp, error))
350
0
    return FALSE;
351
0
  if (tmp != NULL) {
352
0
    g_autoptr(GBytes) data = NULL;
353
0
    data = fu_bytes_from_string(tmp, error);
354
0
    if (data == NULL)
355
0
      return FALSE;
356
0
    fu_firmware_set_bytes(firmware, data);
357
0
  }
358
359
  /* success */
360
0
  return TRUE;
361
0
}
362
363
static gchar *
364
fu_efi_signature_get_checksum(FuFirmware *firmware, GChecksumType csum_kind, GError **error)
365
0
{
366
0
  FuEfiSignature *self = FU_EFI_SIGNATURE(firmware);
367
0
  FuEfiSignaturePrivate *priv = GET_PRIVATE(self);
368
0
  g_autoptr(GBytes) data = fu_efi_signature_get_payload(self, error);
369
0
  if (data == NULL)
370
0
    return NULL;
371
372
  /* special case: this is *literally* a hash */
373
0
  if (priv->kind == FU_EFI_SIGNATURE_KIND_SHA256 && csum_kind == G_CHECKSUM_SHA256)
374
0
    return fu_bytes_to_string(data);
375
376
  /* fallback */
377
0
  return g_compute_checksum_for_bytes(csum_kind, data);
378
0
}
379
380
static gchar *
381
fu_efi_signature_convert_version(FuFirmware *firmware, guint64 version_raw)
382
0
{
383
0
  FuEfiSignature *self = FU_EFI_SIGNATURE(firmware);
384
0
  FuEfiSignaturePrivate *priv = GET_PRIVATE(self);
385
0
  if (priv->kind != FU_EFI_SIGNATURE_KIND_SVN)
386
0
    return NULL;
387
0
  return fu_version_from_uint32(version_raw, FWUPD_VERSION_FORMAT_PAIR);
388
0
}
389
390
static void
391
fu_efi_signature_finalize(GObject *obj)
392
0
{
393
0
  FuEfiSignature *self = FU_EFI_SIGNATURE(obj);
394
0
  FuEfiSignaturePrivate *priv = GET_PRIVATE(self);
395
0
  g_free(priv->owner);
396
0
  g_free(priv->svn_guid);
397
0
  G_OBJECT_CLASS(fu_efi_signature_parent_class)->finalize(obj);
398
0
}
399
400
static void
401
fu_efi_signature_class_init(FuEfiSignatureClass *klass)
402
0
{
403
0
  GObjectClass *object_class = G_OBJECT_CLASS(klass);
404
0
  FuFirmwareClass *firmware_class = FU_FIRMWARE_CLASS(klass);
405
0
  object_class->finalize = fu_efi_signature_finalize;
406
0
  firmware_class->export = fu_efi_signature_export;
407
0
  firmware_class->parse = fu_efi_signature_parse;
408
0
  firmware_class->write = fu_efi_signature_write;
409
0
  firmware_class->build = fu_efi_signature_build;
410
0
  firmware_class->get_checksum = fu_efi_signature_get_checksum;
411
0
  firmware_class->convert_version = fu_efi_signature_convert_version;
412
0
  fu_firmware_set_size_max(firmware_class, 500 * FU_KB);
413
0
}
414
415
static void
416
fu_efi_signature_init(FuEfiSignature *self)
417
0
{
418
0
  FuEfiSignaturePrivate *priv = GET_PRIVATE(self);
419
0
  priv->kind = FU_EFI_SIGNATURE_KIND_SHA256;
420
0
}