Coverage Report

Created: 2026-12-01 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/fwupd/libfwupdplugin/fu-pkcs7.c
Line
Count
Source
1
/*
2
 * Copyright 2025 Richard Hughes <richard@hughsie.com>
3
 *
4
 * SPDX-License-Identifier: LGPL-2.1-or-later
5
 */
6
7
#include "config.h"
8
9
#ifdef HAVE_GNUTLS
10
#include <gnutls/abstract.h>
11
#include <gnutls/crypto.h>
12
#include <gnutls/pkcs7.h>
13
#endif
14
15
#include "fu-common.h"
16
#include "fu-input-stream.h"
17
#include "fu-pkcs7.h"
18
#include "fu-x509-certificate.h"
19
20
#ifdef HAVE_GNUTLS
21
#pragma clang diagnostic push
22
#pragma clang diagnostic ignored "-Wunused-function"
23
G_DEFINE_AUTO_CLEANUP_FREE_FUNC(gnutls_pkcs7_t, gnutls_pkcs7_deinit, NULL)
24
#pragma clang diagnostic pop
25
#endif
26
27
/**
28
 * FuPkcs7:
29
 *
30
 * A PKCS#7 object, typically containing signed X.509 certificates.
31
 *
32
 * See also: [class@FuFirmware]
33
 */
34
35
struct _FuPkcs7 {
36
  FuFirmware parent_instance;
37
};
38
39
0
G_DEFINE_TYPE(FuPkcs7, fu_pkcs7, FU_TYPE_FIRMWARE)
40
0
41
0
#ifdef HAVE_GNUTLS
42
0
static gboolean
43
0
fu_pkcs7_parse_x509_certificate(FuPkcs7 *self, gnutls_datum_t *data, GError **error)
44
0
{
45
0
  g_autoptr(FuX509Certificate) crt = fu_x509_certificate_new();
46
0
  g_autoptr(GBytes) blob = NULL;
47
0
48
0
  /* parse as a X.509 certificate */
49
0
  blob = g_bytes_new(data->data, data->size);
50
0
  if (!fu_firmware_parse_bytes(FU_FIRMWARE(crt),
51
0
             blob,
52
0
             0x0,
53
0
             FU_FIRMWARE_PARSE_FLAG_NONE,
54
0
             error))
55
0
    return FALSE;
56
0
  if (!fu_firmware_add_image(FU_FIRMWARE(self), FU_FIRMWARE(crt), error))
57
0
    return FALSE;
58
0
59
0
  /* success */
60
0
  return TRUE;
61
0
}
62
0
#endif
63
0
64
0
static gboolean
65
0
fu_pkcs7_parse(FuFirmware *firmware,
66
0
         FuInputStream *stream,
67
0
         FuFirmwareParseFlags flags,
68
0
         GError **error)
69
0
{
70
#ifdef HAVE_GNUTLS
71
  FuPkcs7 *self = FU_PKCS7(firmware);
72
  gnutls_datum_t datum = {0};
73
  int rc;
74
  g_auto(gnutls_pkcs7_t) pkcs7 = NULL;
75
  g_autoptr(GByteArray) buf = NULL;
76
77
  /* load PKCS#7 cert */
78
  buf = fu_input_stream_read_byte_array(stream, 0x0, G_MAXSIZE, NULL, error);
79
  if (buf == NULL)
80
    return FALSE;
81
  rc = gnutls_pkcs7_init(&pkcs7);
82
  if (rc != GNUTLS_E_SUCCESS) {
83
    g_set_error(error,
84
          FWUPD_ERROR,
85
          FWUPD_ERROR_INVALID_DATA,
86
          "failed to init pkcs7: %s [%i]",
87
          gnutls_strerror(rc),
88
          rc);
89
    return FALSE;
90
  }
91
  datum.data = buf->data;
92
  datum.size = buf->len;
93
  rc = gnutls_pkcs7_import(pkcs7, &datum, GNUTLS_X509_FMT_DER);
94
  if (rc != GNUTLS_E_SUCCESS) {
95
    g_set_error(error,
96
          FWUPD_ERROR,
97
          FWUPD_ERROR_INVALID_DATA,
98
          "failed to import the PKCS7 signature: %s [%i]",
99
          gnutls_strerror(rc),
100
          rc);
101
    return FALSE;
102
  }
103
104
  /* parse each X.509 certificate */
105
  for (int i = 0; i < gnutls_pkcs7_get_crt_count(pkcs7); i++) {
106
    gnutls_datum_t out;
107
    rc = gnutls_pkcs7_get_crt_raw2(pkcs7, i, &out);
108
    if (rc < 0) {
109
      g_set_error(error,
110
            FWUPD_ERROR,
111
            FWUPD_ERROR_INVALID_DATA,
112
            "failed to get raw crt: %s [%i]",
113
            gnutls_strerror(rc),
114
            rc);
115
      return FALSE;
116
    }
117
    if (!fu_pkcs7_parse_x509_certificate(self, &out, error)) {
118
      gnutls_free(out.data);
119
      return FALSE;
120
    }
121
    gnutls_free(out.data);
122
  }
123
124
  /* success */
125
  return TRUE;
126
#else
127
0
  g_set_error_literal(error, FWUPD_ERROR, FWUPD_ERROR_NOT_SUPPORTED, "no GnuTLS support");
128
0
  return FALSE;
129
0
#endif
130
0
}
131
132
/* a "certs-only" SignedData, with no signers */
133
static GByteArray *
134
fu_pkcs7_write(FuFirmware *firmware, GError **error)
135
0
{
136
#ifdef HAVE_GNUTLS
137
  gnutls_datum_t out = {0};
138
  int rc;
139
  g_auto(gnutls_pkcs7_t) pkcs7 = NULL;
140
  g_autoptr(GByteArray) buf = g_byte_array_new();
141
  g_autoptr(GPtrArray) imgs = fu_firmware_get_images(firmware);
142
143
  rc = gnutls_pkcs7_init(&pkcs7);
144
  if (rc != GNUTLS_E_SUCCESS) {
145
    g_set_error(error,
146
          FWUPD_ERROR,
147
          FWUPD_ERROR_INTERNAL,
148
          "failed to init pkcs7: %s [%i]",
149
          gnutls_strerror(rc),
150
          rc);
151
    return NULL;
152
  }
153
  for (guint i = 0; i < imgs->len; i++) {
154
    FuFirmware *img = g_ptr_array_index(imgs, i);
155
    gnutls_datum_t datum = {0};
156
    g_autoptr(GBytes) blob = NULL;
157
158
    blob = fu_firmware_write(img, error);
159
    if (blob == NULL)
160
      return NULL;
161
    datum.data = (unsigned char *)g_bytes_get_data(blob, NULL);
162
    datum.size = g_bytes_get_size(blob);
163
    rc = gnutls_pkcs7_set_crt_raw(pkcs7, &datum);
164
    if (rc != GNUTLS_E_SUCCESS) {
165
      g_set_error(error,
166
            FWUPD_ERROR,
167
            FWUPD_ERROR_INTERNAL,
168
            "failed to add crt: %s [%i]",
169
            gnutls_strerror(rc),
170
            rc);
171
      return NULL;
172
    }
173
  }
174
  rc = gnutls_pkcs7_export2(pkcs7, GNUTLS_X509_FMT_DER, &out);
175
  if (rc != GNUTLS_E_SUCCESS) {
176
    g_set_error(error,
177
          FWUPD_ERROR,
178
          FWUPD_ERROR_INTERNAL,
179
          "failed to export pkcs7: %s [%i]",
180
          gnutls_strerror(rc),
181
          rc);
182
    return NULL;
183
  }
184
  g_byte_array_append(buf, out.data, out.size);
185
  gnutls_free(out.data);
186
  return g_steal_pointer(&buf);
187
#else
188
0
  g_set_error_literal(error, FWUPD_ERROR, FWUPD_ERROR_NOT_SUPPORTED, "no GnuTLS support");
189
0
  return NULL;
190
0
#endif
191
0
}
192
193
static void
194
fu_pkcs7_init(FuPkcs7 *self)
195
0
{
196
0
}
197
198
static void
199
fu_pkcs7_class_init(FuPkcs7Class *klass)
200
0
{
201
0
  FuFirmwareClass *firmware_class = FU_FIRMWARE_CLASS(klass);
202
0
  fu_firmware_set_size_max(firmware_class, 16 * FU_MB);
203
0
  firmware_class->parse = fu_pkcs7_parse;
204
0
  firmware_class->write = fu_pkcs7_write;
205
0
  fu_firmware_add_image_gtype(firmware_class, FU_TYPE_X509_CERTIFICATE);
206
0
}
207
208
/**
209
 * fu_pkcs7_new:
210
 *
211
 * Creates a new #FuPkcs7.
212
 *
213
 * Returns: (transfer full): object
214
 **/
215
FuPkcs7 *
216
fu_pkcs7_new(void)
217
0
{
218
0
  return g_object_new(FU_TYPE_PKCS7, NULL);
219
0
}