ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
openpgp.c File Reference

OpenPGP (RFC 4880) packet format parser implementation. More...

Go to the source code of this file.

Macros

#define CRYPTO_GPG_OPENPGP_PASSPHRASE_BUFFER_SIZE   512
#define CRYPTO_GPG_OPENPGP_FPR_OUTPUT_BUFFER_SIZE   4096

Functions

asciichat_error_t openpgp_base64_decode (const char *base64, size_t base64_len, uint8_t **binary_out, size_t *binary_len)
 Decode PGP armored base64 data.
asciichat_error_t openpgp_parse_packet_header (const uint8_t *data, size_t data_len, openpgp_packet_header_t *header)
 Parse OpenPGP packet header.
asciichat_error_t openpgp_extract_ed25519_from_mpi (const uint8_t *mpi, size_t mpi_len, uint8_t ed25519_pk[ED25519_PUBLIC_KEY_SIZE])
asciichat_error_t openpgp_parse_public_key_packet (const uint8_t *packet_body, size_t body_len, openpgp_public_key_t *pubkey)
 Parse OpenPGP Public Key Packet (tag 6).
asciichat_error_t openpgp_parse_binary_pubkey (const uint8_t *data, size_t data_len, uint8_t ed25519_pk[ED25519_PUBLIC_KEY_SIZE])
asciichat_error_t openpgp_parse_armored_pubkey (const char *armored_text, uint8_t ed25519_pk[ED25519_PUBLIC_KEY_SIZE])
asciichat_error_t openpgp_parse_secret_key_packet (const uint8_t *packet_body, size_t body_len, openpgp_secret_key_t *seckey)
 Parse OpenPGP Secret Key Packet (tag 5).
asciichat_error_t openpgp_parse_binary_seckey (const uint8_t *data, size_t data_len, uint8_t ed25519_pk[ED25519_PUBLIC_KEY_SIZE], uint8_t ed25519_sk[ED25519_SEED_SIZE])
asciichat_error_t openpgp_parse_armored_seckey (const char *armored_text, uint8_t ed25519_pk[ED25519_PUBLIC_KEY_SIZE], uint8_t ed25519_sk[ED25519_SEED_SIZE])

Detailed Description

OpenPGP (RFC 4880) packet format parser implementation.

Definition in file openpgp.c.

Macro Definition Documentation

◆ CRYPTO_GPG_OPENPGP_FPR_OUTPUT_BUFFER_SIZE

#define CRYPTO_GPG_OPENPGP_FPR_OUTPUT_BUFFER_SIZE   4096

Definition at line 9 of file openpgp.c.

◆ CRYPTO_GPG_OPENPGP_PASSPHRASE_BUFFER_SIZE

#define CRYPTO_GPG_OPENPGP_PASSPHRASE_BUFFER_SIZE   512

Definition at line 8 of file openpgp.c.

Function Documentation

◆ openpgp_extract_ed25519_from_mpi()

asciichat_error_t openpgp_extract_ed25519_from_mpi ( const uint8_t * mpi,
size_t mpi_len,
uint8_t ed25519_pk[ED25519_PUBLIC_KEY_SIZE] )

Definition at line 170 of file openpgp.c.

170 {
171 if (!mpi || !ed25519_pk || mpi_len < 35) {
172 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters for MPI extraction (need at least 35 bytes)");
173 }
174
175 // MPI format:
176 // - 2 bytes: bit count (big-endian)
177 // - 1 byte: 0x40 prefix (Ed25519 marker)
178 // - 32 bytes: Ed25519 public key
179
180 uint16_t bit_count = ((uint16_t)mpi[0] << 8) | mpi[1];
181 log_debug("MPI bit count: %u", bit_count);
182
183 // Ed25519 with 0x40 prefix is typically 263 bits (0x0107)
184 if (bit_count < 256 || bit_count > 270) {
185 return SET_ERRNO(ERROR_CRYPTO_KEY, "Unexpected MPI bit count for Ed25519: %u (expected ~263)", bit_count);
186 }
187
188 // Check for 0x40 prefix byte
189 if (mpi[2] != 0x40) {
190 return SET_ERRNO(ERROR_CRYPTO_KEY, "Missing 0x40 prefix byte in Ed25519 MPI (found 0x%02x)", mpi[2]);
191 }
192
193 // Extract 32-byte Ed25519 public key
194 memcpy(ed25519_pk, mpi + 3, ED25519_PUBLIC_KEY_SIZE);
195
196 return ASCIICHAT_OK;
197}
#define SET_ERRNO(code,...)
unsigned short uint16_t
@ ERROR_CRYPTO_KEY
Definition error_codes.h:98
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:589
#define ED25519_PUBLIC_KEY_SIZE
Ed25519 public key size in bytes.
Definition sizes.h:37

References ASCIICHAT_OK, ED25519_PUBLIC_KEY_SIZE, ERROR_CRYPTO_KEY, ERROR_INVALID_PARAM, log_debug, and SET_ERRNO.

◆ openpgp_parse_armored_pubkey()

asciichat_error_t openpgp_parse_armored_pubkey ( const char * armored_text,
uint8_t ed25519_pk[ED25519_PUBLIC_KEY_SIZE] )

Definition at line 307 of file openpgp.c.

307 {
308 if (!armored_text || !ed25519_pk) {
309 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters for armored pubkey parsing");
310 }
311
312 // Find the BEGIN marker
313 const char *begin = strstr(armored_text, "-----BEGIN PGP PUBLIC KEY BLOCK-----");
314 if (!begin) {
315 return SET_ERRNO(ERROR_CRYPTO_KEY, "Missing PGP PUBLIC KEY BLOCK BEGIN marker");
316 }
317
318 // Skip to the end of the BEGIN line
319 const char *base64_start = strchr(begin, '\n');
320 if (!base64_start) {
321 return SET_ERRNO(ERROR_CRYPTO_KEY, "Invalid PGP armored format: no newline after BEGIN marker");
322 }
323 base64_start++; // Skip the newline
324
325 // Find the END marker
326 const char *end = strstr(base64_start, "-----END PGP PUBLIC KEY BLOCK-----");
327 if (!end) {
328 return SET_ERRNO(ERROR_CRYPTO_KEY, "Missing PGP PUBLIC KEY BLOCK END marker");
329 }
330
331 // Find the checksum line (starts with '=') and exclude it
332 const char *base64_end = end;
333 const char *checksum = base64_end;
334 while (checksum > base64_start && *checksum != '=') {
335 checksum--;
336 }
337 if (*checksum == '=') {
338 // Move back to before the checksum line
339 while (checksum > base64_start && (checksum[-1] == '\n' || checksum[-1] == '\r')) {
340 checksum--;
341 }
342 base64_end = checksum;
343 }
344
345 size_t base64_len = (size_t)(base64_end - base64_start);
346
347 log_debug("Extracting base64 data from PGP armor (%zu bytes)", base64_len);
348
349 // Decode base64 to binary OpenPGP packets
350 uint8_t *binary_data;
351 size_t binary_len;
352 asciichat_error_t decode_result = openpgp_base64_decode(base64_start, base64_len, &binary_data, &binary_len);
353 if (decode_result != ASCIICHAT_OK) {
354 return decode_result;
355 }
356
357 defer(SAFE_FREE(binary_data));
358 log_debug("Decoded %zu bytes of OpenPGP packet data", binary_len);
359
360 asciichat_error_t result = openpgp_parse_binary_pubkey(binary_data, binary_len, ed25519_pk);
361 return result;
362}
#define SAFE_FREE(ptr)
unsigned char uint8_t
asciichat_error_t openpgp_base64_decode(const char *base64, size_t base64_len, uint8_t **binary_out, size_t *binary_len)
Decode PGP armored base64 data.
Definition openpgp.c:40
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
asciichat_error_t openpgp_parse_binary_pubkey(const uint8_t *data, size_t data_len, uint8_t ed25519_pk[ED25519_PUBLIC_KEY_SIZE])
Definition openpgp.c:280

References ASCIICHAT_OK, ED25519_PUBLIC_KEY_SIZE, ERROR_CRYPTO_KEY, ERROR_INVALID_PARAM, log_debug, openpgp_base64_decode(), openpgp_parse_binary_pubkey(), SAFE_FREE, and SET_ERRNO.

Referenced by browser_client_parse_gpg_public_key(), client_parse_gpg_public_key(), and parse_gpg_key_binary().

◆ openpgp_parse_armored_seckey()

asciichat_error_t openpgp_parse_armored_seckey ( const char * armored_text,
uint8_t ed25519_pk[ED25519_PUBLIC_KEY_SIZE],
uint8_t ed25519_sk[ED25519_SEED_SIZE] )

Definition at line 733 of file openpgp.c.

733 {
734 if (!armored_text || !ed25519_pk || !ed25519_sk) {
735 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters for armored seckey parsing");
736 }
737
738 // Find the BEGIN marker (try both "PRIVATE KEY" and "SECRET KEY" formats)
739 const char *begin = strstr(armored_text, "-----BEGIN PGP PRIVATE KEY BLOCK-----");
740 if (!begin) {
741 begin = strstr(armored_text, "-----BEGIN PGP SECRET KEY BLOCK-----");
742 }
743 if (!begin) {
744 return SET_ERRNO(ERROR_CRYPTO_KEY, "Missing PGP PRIVATE/SECRET KEY BLOCK BEGIN marker");
745 }
746
747 // Skip to the end of the BEGIN line
748 const char *base64_start = strchr(begin, '\n');
749 if (!base64_start) {
750 return SET_ERRNO(ERROR_CRYPTO_KEY, "Invalid PGP armored format: no newline after BEGIN marker");
751 }
752 base64_start++; // Skip the newline
753
754 // Find the END marker (try both formats)
755 const char *end = strstr(base64_start, "-----END PGP PRIVATE KEY BLOCK-----");
756 if (!end) {
757 end = strstr(base64_start, "-----END PGP SECRET KEY BLOCK-----");
758 }
759 if (!end) {
760 return SET_ERRNO(ERROR_CRYPTO_KEY, "Missing PGP PRIVATE/SECRET KEY BLOCK END marker");
761 }
762
763 // Find the checksum line (starts with '=') and exclude it
764 const char *base64_end = end;
765 const char *checksum = base64_end;
766 while (checksum > base64_start && *checksum != '=') {
767 checksum--;
768 }
769 if (*checksum == '=') {
770 // Move back to before the checksum line
771 while (checksum > base64_start && (checksum[-1] == '\n' || checksum[-1] == '\r')) {
772 checksum--;
773 }
774 base64_end = checksum;
775 }
776
777 size_t base64_len = (size_t)(base64_end - base64_start);
778
779 log_debug("Extracting base64 data from PGP secret key armor (%zu bytes)", base64_len);
780
781 // Decode base64 to binary OpenPGP packets
782 uint8_t *binary_data;
783 size_t binary_len;
784 asciichat_error_t decode_result = openpgp_base64_decode(base64_start, base64_len, &binary_data, &binary_len);
785 if (decode_result != ASCIICHAT_OK) {
786 return decode_result;
787 }
788
789 defer(SAFE_FREE(binary_data));
790
791 log_debug("Decoded %zu bytes of OpenPGP secret key packet data", binary_len);
792
793 // Parse OpenPGP packets to find the secret key packet (tag 5)
794 size_t offset = 0;
795 bool found_seckey = false;
796
797 while (offset < binary_len) {
799 asciichat_error_t header_result = openpgp_parse_packet_header(binary_data + offset, binary_len - offset, &header);
800 if (header_result != ASCIICHAT_OK) {
801 return header_result;
802 }
803
804 log_debug("Packet at offset %zu: tag=%u, length=%zu", offset, header.tag, header.length);
805
806 // Check if this is a secret key packet (tag 5)
807 if (header.tag == OPENPGP_TAG_SECRET_KEY) {
809 asciichat_error_t parse_result = openpgp_parse_secret_key_packet(binary_data + offset + header.header_len, header.length, &seckey);
810
811 if (parse_result == ASCIICHAT_OK) {
812 // Check if key is encrypted
813 if (seckey.is_encrypted) {
814#ifdef EMSCRIPTEN_BUILD
815 return SET_ERRNO(ERROR_NOT_SUPPORTED, "Encrypted OpenPGP private keys are not supported in the browser; use an unencrypted key");
816#else
817 log_debug("Detected encrypted GPG key, attempting to decrypt with passphrase");
818
819 // Decrypt the key using gpg binary
820 char *decrypted_text = NULL;
821 asciichat_error_t decrypt_result = openpgp_decrypt_with_gpg(armored_text, &decrypted_text);
822 if (decrypt_result != ASCIICHAT_OK) {
823 return decrypt_result;
824 }
825
826 // Recursively parse the decrypted key
827 asciichat_error_t recursive_result = openpgp_parse_armored_seckey(decrypted_text, ed25519_pk, ed25519_sk);
828 SAFE_FREE(decrypted_text);
829 return recursive_result;
830#endif
831 }
832
833 // Unencrypted key - extract directly
834 memcpy(ed25519_pk, seckey.pubkey, ED25519_PUBLIC_KEY_SIZE);
835 memcpy(ed25519_sk, seckey.seckey, ED25519_SEED_SIZE);
836 found_seckey = true;
837 log_debug("Extracted Ed25519 keypair from OpenPGP armored secret key block");
838 break;
839 } else {
840 // Not an Ed25519 key, try next packet
841 log_debug("Skipping non-Ed25519 secret key packet");
842 }
843 }
844
845 // Move to next packet
846 offset += header.header_len + header.length;
847 }
848
849 if (!found_seckey) {
850 return SET_ERRNO(ERROR_CRYPTO_KEY, "No Ed25519 secret key found in PGP armored block");
851 }
852
853 return ASCIICHAT_OK;
854}
#define OPENPGP_TAG_SECRET_KEY
OpenPGP packet tag for Secret Key Packet.
Definition openpgp.h:52
asciichat_error_t openpgp_parse_packet_header(const uint8_t *data, size_t data_len, openpgp_packet_header_t *header)
Parse OpenPGP packet header.
Definition openpgp.c:74
asciichat_error_t openpgp_parse_secret_key_packet(const uint8_t *packet_body, size_t body_len, openpgp_secret_key_t *seckey)
Parse OpenPGP Secret Key Packet (tag 5).
Definition openpgp.c:368
@ ERROR_NOT_SUPPORTED
Definition error_codes.h:73
asciichat_error_t openpgp_parse_armored_seckey(const char *armored_text, uint8_t ed25519_pk[ED25519_PUBLIC_KEY_SIZE], uint8_t ed25519_sk[ED25519_SEED_SIZE])
Definition openpgp.c:733
#define ED25519_SEED_SIZE
Definition sizes.h:169
The framing fields needed to locate and classify one OpenPGP packet body.
Definition openpgp.h:81
The parsed public and private material from an OpenPGP secret-key packet.
Definition openpgp.h:128
uint8_t seckey[32]
Ed25519 secret key (32 bytes).
Definition openpgp.h:133
uint8_t pubkey[32]
Ed25519 public key (32 bytes).
Definition openpgp.h:132
bool is_encrypted
True if secret key material is encrypted.
Definition openpgp.h:135

References ASCIICHAT_OK, ED25519_PUBLIC_KEY_SIZE, ED25519_SEED_SIZE, ERROR_CRYPTO_KEY, ERROR_INVALID_PARAM, ERROR_NOT_SUPPORTED, openpgp_secret_key_t::is_encrypted, log_debug, openpgp_base64_decode(), openpgp_parse_armored_seckey(), openpgp_parse_packet_header(), openpgp_parse_secret_key_packet(), OPENPGP_TAG_SECRET_KEY, openpgp_secret_key_t::pubkey, SAFE_FREE, openpgp_secret_key_t::seckey, and SET_ERRNO.

Referenced by browser_client_parse_gpg_private_key(), client_parse_gpg_private_key(), openpgp_parse_armored_seckey(), and parse_private_key().

◆ openpgp_parse_binary_pubkey()

asciichat_error_t openpgp_parse_binary_pubkey ( const uint8_t * data,
size_t data_len,
uint8_t ed25519_pk[ED25519_PUBLIC_KEY_SIZE] )

Definition at line 280 of file openpgp.c.

280 {
281 if (!data || data_len == 0 || !ed25519_pk) {
282 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters for binary OpenPGP public-key parsing");
283 }
284
285 size_t offset = 0;
286 while (offset < data_len) {
288 asciichat_error_t result = openpgp_parse_packet_header(data + offset, data_len - offset, &header);
289 if (result != ASCIICHAT_OK)
290 return result;
291 if (header.header_len > data_len - offset || header.length > data_len - offset - header.header_len) {
292 return SET_ERRNO(ERROR_CRYPTO_KEY, "Truncated OpenPGP packet in public key");
293 }
294 if (header.tag == OPENPGP_TAG_PUBLIC_KEY) {
296 result = openpgp_parse_public_key_packet(data + offset + header.header_len, header.length, &pubkey);
297 if (result == ASCIICHAT_OK) {
298 memcpy(ed25519_pk, pubkey.pubkey, ED25519_PUBLIC_KEY_SIZE);
299 return ASCIICHAT_OK;
300 }
301 }
302 offset += header.header_len + header.length;
303 }
304 return SET_ERRNO(ERROR_CRYPTO_KEY, "No Ed25519 public key found in OpenPGP data");
305}
asciichat_error_t openpgp_parse_public_key_packet(const uint8_t *packet_body, size_t body_len, openpgp_public_key_t *pubkey)
Parse OpenPGP Public Key Packet (tag 6).
Definition openpgp.c:203
#define OPENPGP_TAG_PUBLIC_KEY
OpenPGP packet tag for Public Key Packet.
Definition openpgp.h:49
The parsed identity portion of an OpenPGP public-key packet.
Definition openpgp.h:104
uint8_t pubkey[32]
Ed25519 public key (32 bytes).
Definition openpgp.h:108

References ASCIICHAT_OK, ED25519_PUBLIC_KEY_SIZE, ERROR_CRYPTO_KEY, ERROR_INVALID_PARAM, openpgp_parse_packet_header(), openpgp_parse_public_key_packet(), OPENPGP_TAG_PUBLIC_KEY, openpgp_public_key_t::pubkey, and SET_ERRNO.

Referenced by browser_client_parse_gpg_public_key_binary(), client_parse_gpg_public_key_binary(), openpgp_parse_armored_pubkey(), and parse_gpg_key_binary().

◆ openpgp_parse_binary_seckey()

asciichat_error_t openpgp_parse_binary_seckey ( const uint8_t * data,
size_t data_len,
uint8_t ed25519_pk[ED25519_PUBLIC_KEY_SIZE],
uint8_t ed25519_sk[ED25519_SEED_SIZE] )

Definition at line 702 of file openpgp.c.

702 {
703 if (!data || data_len == 0 || !ed25519_pk || !ed25519_sk) {
704 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters for binary OpenPGP secret-key parsing");
705 }
706
707 size_t offset = 0;
708 while (offset < data_len) {
710 asciichat_error_t result = openpgp_parse_packet_header(data + offset, data_len - offset, &header);
711 if (result != ASCIICHAT_OK)
712 return result;
713 if (header.header_len > data_len - offset || header.length > data_len - offset - header.header_len) {
714 return SET_ERRNO(ERROR_CRYPTO_KEY, "Truncated OpenPGP packet in secret key");
715 }
716 if (header.tag == OPENPGP_TAG_SECRET_KEY) {
718 result = openpgp_parse_secret_key_packet(data + offset + header.header_len, header.length, &seckey);
719 if (result == ASCIICHAT_OK) {
720 if (seckey.is_encrypted) {
721 return SET_ERRNO(ERROR_NOT_SUPPORTED, "Encrypted binary OpenPGP private keys are not supported; use an unencrypted key");
722 }
723 memcpy(ed25519_pk, seckey.pubkey, ED25519_PUBLIC_KEY_SIZE);
724 memcpy(ed25519_sk, seckey.seckey, ED25519_SEED_SIZE);
725 return ASCIICHAT_OK;
726 }
727 }
728 offset += header.header_len + header.length;
729 }
730 return SET_ERRNO(ERROR_CRYPTO_KEY, "No unencrypted Ed25519 secret key found in OpenPGP data");
731}

References ASCIICHAT_OK, ED25519_PUBLIC_KEY_SIZE, ED25519_SEED_SIZE, ERROR_CRYPTO_KEY, ERROR_INVALID_PARAM, ERROR_NOT_SUPPORTED, openpgp_secret_key_t::is_encrypted, openpgp_parse_packet_header(), openpgp_parse_secret_key_packet(), OPENPGP_TAG_SECRET_KEY, openpgp_secret_key_t::pubkey, openpgp_secret_key_t::seckey, and SET_ERRNO.

Referenced by browser_client_parse_gpg_private_key_binary(), client_parse_gpg_private_key_binary(), and parse_private_key().