CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
GpgCertPayload.cpp
Go to the documentation of this file.
1
5
7#include "mod_gpg/gpg.h"
8#include "openpgp/xsig.h"
9
10#include <cstring>
11#include <ctime>
12
13namespace cdc::mod_gpg {
14
15size_t gpgBuildCertPayload(const gpg_recv_key_t& signed_key,
16 uint8_t* out, size_t out_size) {
17 if (!out || signed_key.sig_len != 64) return 0;
18
19 gpg_status_t status = {};
20 if (!gpg_get_status(&status)) return 0;
21
22 uint8_t sig_pkt[kGpgSelfCertSigMax];
23 const size_t sig_len = buildCertSigPacket(signed_key, sig_pkt, sizeof(sig_pkt));
24 if (sig_len == 0) return 0;
25
26 const size_t uid_len = strnlen(status.user_id, sizeof(status.user_id));
27 const size_t total = 20 + 20 + 1 + uid_len + 2 + sig_len;
28 if (total > out_size) return 0;
29
30 size_t off = 0;
31 std::memcpy(out + off, signed_key.fingerprint_v4, 20);
32 off += 20;
33 std::memcpy(out + off, status.fingerprint, 20);
34 off += 20;
35 out[off++] = static_cast<uint8_t>(uid_len);
36 std::memcpy(out + off, status.user_id, uid_len);
37 off += uid_len;
38 out[off++] = (sig_len >> 8) & 0xFF;
39 out[off++] = sig_len & 0xFF;
40 std::memcpy(out + off, sig_pkt, sig_len);
41 off += sig_len;
42 return off;
43}
44
45bool gpgParseCertPayload(const uint8_t* data, size_t len,
46 gpg_self_cert_t* out, uint8_t out_target_fp[20]) {
47 if (!data || !out || !out_target_fp) return false;
48 if (len < kGpgCertPayloadMin) return false;
49
50 std::memset(out, 0, sizeof(*out));
51
52 size_t off = 0;
53 std::memcpy(out_target_fp, data + off, 20);
54 off += 20;
55 std::memcpy(out->issuer_fp_v4, data + off, 20);
56 off += 20;
57
58 const uint8_t uid_len = data[off++];
59 if (uid_len > 63 || off + uid_len + 2 > len) return false;
60 std::memcpy(out->issuer_uid, data + off, uid_len);
61 out->issuer_uid[uid_len] = '\0';
62 off += uid_len;
63
64 const uint16_t sig_len = (static_cast<uint16_t>(data[off]) << 8) | data[off + 1];
65 off += 2;
66 if (sig_len == 0 || sig_len > kGpgSelfCertSigMax) return false;
67 if (off + sig_len != len) return false;
68 std::memcpy(out->sig_pkt, data + off, sig_len);
69 out->sig_pkt_len = sig_len;
70
71 out->received_at = static_cast<uint32_t>(std::time(nullptr));
72 return true;
73}
74
75} // namespace cdc::mod_gpg
(De)serialisation of a badge-to-badge certification return message.
bool gpg_get_status(gpg_status_t *status)
Fills status from the OpenPGP card-application state.
Definition gpg.cpp:95
size_t buildCertSigPacket(const gpg_recv_key_t &key, uint8_t *out, size_t out_size)
Build the OpenPGP Signature Packet body certifying key.
Definition xsig.cpp:683
constexpr size_t kGpgSelfCertSigMax
Maximum length of a stored certification signature packet body (Tag 2).
bool gpgParseCertPayload(const uint8_t *data, size_t len, gpg_self_cert_t *out, uint8_t out_target_fp[20])
Parse a certification return message into a self-cert record.
constexpr size_t kGpgCertPayloadMin
Minimum payload size (empty issuer uid, 1-byte signature packet).
size_t gpgBuildCertPayload(const gpg_recv_key_t &signed_key, uint8_t *out, size_t out_size)
Serialise a certification return message for a cross-signed key.
One GPG public key received from another badge.
One third-party certification received over the badge's own key.
uint8_t sig_pkt[kGpgSelfCertSigMax]
char issuer_uid[64]
Issuer user-id (display only).
uint32_t received_at
Receive timestamp (RTC).
uint16_t sig_pkt_len
Length of sig_pkt.
uint8_t issuer_fp_v4[20]
Issuer (signer) v4 fingerprint.
Snapshot of the current OpenPGP card-application state for UI display.
Definition gpg.h:25
uint8_t fingerprint[20]
Definition gpg.h:29