File 0008-Appended-signature-verification.patch of Package qemu

From 157effb433b2c9630a1d27740f2eb79777cf8f36 Mon Sep 17 00:00:00 2001
From: Daniel Axtens <dja@axtens.net>
Date: Fri, 10 Apr 2020 01:24:56 +1000
Subject: [PATCH 08/12] Appended signature verification

Important Caveats!

 * SLOF only rejects a binary if it's 32bit and _has the relevant
   section_. It otherwise boots anything.

 * There's no attempt to secure the OF prompt!

To use this:

 - create certificate.der representing the public key used in signing
   grub.

 - xxd -i certificate.der > certificate.h, copy in over my certificate.h

 - rebuild

 - boot qemu with `-bios boot_rom.bin`

Signed-off-by: Daniel Axtens <dja@axtens.net>
---
 lib/libcrypto/Makefile       |  12 +++-
 lib/libcrypto/appended_sig.c | 102 ++++++++++++++++++++++++++++++
 lib/libcrypto/certificate.h  | 118 +++++++++++++++++++++++++++++++++++
 lib/libcrypto/libcrypto.h    |   5 ++
 lib/libelf/Makefile          |   3 +-
 lib/libelf/elf32.c           |  55 ++++------------
 6 files changed, 249 insertions(+), 46 deletions(-)
 create mode 100644 lib/libcrypto/appended_sig.c
 create mode 100644 lib/libcrypto/certificate.h
 create mode 100644 lib/libcrypto/libcrypto.h

diff --git a/roms/SLOF/lib/libcrypto/Makefile b/roms/SLOF/lib/libcrypto/Makefile
index e3c1231..c4df856 100644
--- a/roms/SLOF/lib/libcrypto/Makefile
+++ b/roms/SLOF/lib/libcrypto/Makefile
@@ -23,7 +23,17 @@ TARGET = ../libcrypto.a
 
 all: $(TARGET)
 
-SRCS = mbedtls/library/sha256.c mbedtls/library/platform_util.c
+SRCS = appended_sig.c \
+       mbedtls/library/platform_util.c \
+       mbedtls/library/sha256.c mbedtls/library/sha512.c mbedtls/library/pkcs7.c \
+       mbedtls/library/asn1parse.c \
+       mbedtls/library/x509.c mbedtls/library/x509_crt.c mbedtls/library/x509_crl.c \
+       mbedtls/library/pk.c mbedtls/library/pk_wrap.c mbedtls/library/pkparse.c \
+       mbedtls/library/md.c mbedtls/library/md_wrap.c \
+       mbedtls/library/oid.c \
+       mbedtls/library/rsa.c mbedtls/library/rsa_internal.c \
+       mbedtls/library/bignum.c
+
 
 OBJS = $(SRCS:%.c=%.o)
 
diff --git a/roms/SLOF/lib/libcrypto/appended_sig.c b/roms/SLOF/lib/libcrypto/appended_sig.c
new file mode 100644
index 0000000..4bebac2
--- /dev/null
+++ b/roms/SLOF/lib/libcrypto/appended_sig.c
@@ -0,0 +1,102 @@
+// appended_sig.c
+
+#include <libcrypto.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <mbedtls/pkcs7.h>
+#include "certificate.h"
+
+static char appsig_magic[] = "~Module signature appended~\n";
+
+struct module_signature {
+	uint8_t		algo;		/* Public-key crypto algorithm [0] */
+	uint8_t		hash;		/* Digest algorithm [0] */
+	uint8_t		id_type;	/* Key identifier type [PKEY_ID_PKCS7] */
+	uint8_t		signer_len;	/* Length of signer's name [0] */
+	uint8_t		key_id_len;	/* Length of key identifier [0] */
+	uint8_t		__pad[3];
+	uint32_t	sig_len;	/* Length of signature data */
+};
+
+int verify_appended_signature(void *blob, size_t len) {
+	void *ptr;
+	mbedtls_pkcs7 *pkcs7;
+	mbedtls_x509_crt *x509;
+	int rc = 0;
+	struct module_signature *modsig;
+
+	// go to start of magic
+	ptr = blob + (len - sizeof(appsig_magic) + 1); // appsig_magic contains null-term
+
+	// again be careful not to require the null terminator
+	if (strncmp(ptr, appsig_magic, sizeof(appsig_magic) - 1)) {
+		printf("Appended signature: magic string missing. Aborting.\n");
+		return 0;
+	}
+
+	// now load the sig info
+	ptr -= sizeof(struct module_signature);
+	modsig = (struct module_signature *)ptr;
+
+	printf("%x %x %x %x %x\n", modsig->algo, modsig->hash, modsig->id_type, modsig->key_id_len, modsig->signer_len);
+	if (modsig->id_type != 2) { // pkcs7
+		printf("Appended signature: unexpected format (not PKCS#7). Aborting.\n");
+		return 0;
+	}
+
+	if (modsig->algo != 0 || modsig->hash != 0 || modsig->key_id_len != 0 || modsig->signer_len != 0) {
+		printf("Appended signature: unexpected parameter inconsistend with PKCS#7. Aborting.\n");
+		return 0;
+	}
+
+	// point at the pkcs7 data itself:
+	ptr -= modsig->sig_len;
+
+	// load into the pkcs7 code
+	pkcs7 = malloc(sizeof(mbedtls_pkcs7));
+	mbedtls_pkcs7_init(pkcs7);
+
+
+	rc = mbedtls_pkcs7_parse_der(ptr, modsig->sig_len, pkcs7);
+	if (rc) {
+		printf("Appended signature: error parsing PKCS#7 data: %d. Aborting.\n", rc);
+		rc = 0;
+		goto exit;
+	}
+
+	x509 = malloc(sizeof(mbedtls_x509_crt));
+	mbedtls_x509_crt_init(x509);
+	printf("%d\n", __LINE__);
+	rc = mbedtls_x509_crt_parse_der(x509, certificate_der, certificate_der_len);
+	if (rc) {
+	printf("%d\n", __LINE__);
+
+		printf("Appended signature: internal error parsing builtin x509 certificate. Aborting.\n");
+		rc = 0;
+		goto exit_x509;
+	}
+	printf("%d\n", __LINE__);
+
+	printf("ptr at %p, blob at %p, computed len %lu ? %lu\n", ptr, blob, len-(ptr-blob), (unsigned long)(ptr-blob));
+
+	rc = mbedtls_pkcs7_signed_data_verify(pkcs7, x509, blob, (ptr - blob));
+	if (rc) {
+	printf("%d\n", __LINE__);
+		printf("Appended signature: verification failed. Refusing to proceed.\n");
+		rc = 0;
+		goto exit_x509;
+	}
+	printf("%d\n", __LINE__);
+
+	rc = 1;
+
+exit_x509:
+	mbedtls_x509_crt_free(x509);
+	free(x509);
+
+exit:
+	mbedtls_pkcs7_free(pkcs7);
+	free(pkcs7);
+	return rc;
+}
diff --git a/roms/SLOF/lib/libcrypto/certificate.h b/roms/SLOF/lib/libcrypto/certificate.h
new file mode 100644
index 0000000..b48f1ee
--- /dev/null
+++ b/roms/SLOF/lib/libcrypto/certificate.h
@@ -0,0 +1,118 @@
+unsigned char certificate_der[] = {
+  0x30, 0x82, 0x05, 0x55, 0x30, 0x82, 0x03, 0x3d, 0xa0, 0x03, 0x02, 0x01,
+  0x02, 0x02, 0x14, 0x55, 0x50, 0x31, 0x0b, 0xf3, 0xea, 0xb8, 0xc9, 0xd6,
+  0xac, 0x66, 0x80, 0x46, 0xa2, 0x00, 0x5a, 0x90, 0x47, 0x0e, 0x23, 0x30,
+  0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
+  0x05, 0x00, 0x30, 0x34, 0x31, 0x32, 0x30, 0x30, 0x06, 0x03, 0x55, 0x04,
+  0x03, 0x0c, 0x29, 0x47, 0x75, 0x65, 0x73, 0x74, 0x20, 0x53, 0x65, 0x63,
+  0x75, 0x72, 0x65, 0x20, 0x42, 0x6f, 0x6f, 0x74, 0x20, 0x54, 0x65, 0x73,
+  0x74, 0x20, 0x4d, 0x61, 0x73, 0x74, 0x65, 0x72, 0x20, 0x53, 0x69, 0x67,
+  0x6e, 0x69, 0x6e, 0x67, 0x20, 0x4b, 0x65, 0x79, 0x30, 0x20, 0x17, 0x0d,
+  0x32, 0x30, 0x30, 0x31, 0x30, 0x37, 0x30, 0x36, 0x35, 0x32, 0x31, 0x37,
+  0x5a, 0x18, 0x0f, 0x32, 0x31, 0x31, 0x39, 0x31, 0x32, 0x31, 0x34, 0x30,
+  0x36, 0x35, 0x32, 0x31, 0x37, 0x5a, 0x30, 0x34, 0x31, 0x32, 0x30, 0x30,
+  0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x29, 0x47, 0x75, 0x65, 0x73, 0x74,
+  0x20, 0x53, 0x65, 0x63, 0x75, 0x72, 0x65, 0x20, 0x42, 0x6f, 0x6f, 0x74,
+  0x20, 0x54, 0x65, 0x73, 0x74, 0x20, 0x4b, 0x65, 0x72, 0x6e, 0x65, 0x6c,
+  0x20, 0x73, 0x69, 0x67, 0x6e, 0x69, 0x6e, 0x67, 0x20, 0x6b, 0x65, 0x79,
+  0x30, 0x82, 0x02, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
+  0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x02, 0x0f, 0x00,
+  0x30, 0x82, 0x02, 0x0a, 0x02, 0x82, 0x02, 0x01, 0x00, 0xd7, 0xf8, 0xeb,
+  0x7c, 0x86, 0x6b, 0x7a, 0x06, 0x14, 0xd5, 0xa7, 0x76, 0x6b, 0xe3, 0xba,
+  0xba, 0x57, 0x52, 0xe1, 0xfb, 0x1a, 0xd7, 0xd3, 0x65, 0xc2, 0x1f, 0x95,
+  0x14, 0x67, 0x15, 0xc9, 0xa1, 0x61, 0x12, 0x59, 0xee, 0xd7, 0xdd, 0xa7,
+  0xe3, 0x05, 0x4d, 0x5f, 0x99, 0xe5, 0xff, 0x89, 0xf9, 0x9f, 0x09, 0xa8,
+  0x47, 0x76, 0x28, 0xbd, 0x3d, 0xb4, 0x04, 0x31, 0x13, 0xa5, 0x6f, 0x13,
+  0x9d, 0x95, 0xc6, 0xd9, 0x3b, 0xa2, 0x60, 0x31, 0xf2, 0xe8, 0x22, 0xa0,
+  0x7b, 0xc4, 0xdc, 0x36, 0x58, 0xdc, 0x69, 0xcb, 0xf1, 0xb6, 0x6f, 0x3a,
+  0x90, 0x4e, 0xb8, 0x71, 0xf3, 0xfd, 0xee, 0x1a, 0x58, 0x74, 0x92, 0x3b,
+  0x60, 0x73, 0x57, 0x36, 0xc3, 0x26, 0x61, 0x02, 0x29, 0xf3, 0xed, 0x0b,
+  0xfe, 0xb5, 0x44, 0xc2, 0x02, 0x19, 0x32, 0x88, 0x8a, 0x58, 0x2c, 0x29,
+  0x52, 0x59, 0xa0, 0xba, 0xce, 0xa2, 0xe8, 0xdd, 0xde, 0xbc, 0x5d, 0xc1,
+  0x78, 0xcf, 0x53, 0x3b, 0xa5, 0x40, 0x95, 0x66, 0x0c, 0xed, 0xa6, 0x6a,
+  0xb1, 0xab, 0x7c, 0xed, 0xbe, 0x0a, 0xf6, 0xc8, 0x44, 0x7d, 0x46, 0x1d,
+  0x6e, 0xf2, 0xa6, 0xac, 0x91, 0x6e,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0x03, 0x55, 0x1d, 0x13, 0x01, 0x01, 0xff,
+  0x04, 0x02, 0x30, 0x00, 0x30, 0x0b, 0x06, 0x03, 0x55, 0x1d, 0x0f, 0x04,
+  0x04, 0x03, 0x02, 0x07, 0x80, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e,
+  0x04, 0x16, 0x04, 0x14, 0x5e, 0x36, 0xba, 0x6f, 0x46, 0x13, 0xe2, 0x3c,
+  0xc0, 0xc1, 0xfe, 0x77, 0x5e, 0x6a, 0x6a, 0xa8, 0x3f, 0x04, 0xc4, 0x05,
+  0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80,
+  0x14, 0x45, 0xc3, 0x39, 0x5f, 0x3e, 0x5b, 0xcb, 0x0b, 0x21, 0x9a, 0xc2,
+  0x4a, 0x3b, 0xea, 0xe2, 0x46, 0x73, 0xa2, 0x73, 0xd1, 0x30, 0x0d, 0x06,
+  0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00,
+  0x03, 0x82, 0x02, 0x01, 0x00, 0xc0, 0x69, 0xc1, 0xba, 0x63, 0xe8, 0xb5,
+  0xc7, 0xe2, 0x27, 0x7a, 0xe7, 0x78, 0xac, 0xfb, 0x49, 0xd4, 0x2b, 0xf7,
+  0x19, 0x8e, 0x42, 0x2f, 0x0a, 0xd5, 0x17, 0x89, 0x02, 0xa4, 0x72, 0x5b,
+  0x07, 0xe6, 0x87, 0x7c, 0xd6, 0x8e, 0x25, 0x1c, 0x9b, 0xf8, 0xca, 0x64,
+  0xc7, 0xf2, 0xaf, 0x12, 0xd1, 0x51, 0xff, 0x5c, 0xe2, 0xe9, 0xd8, 0xb1,
+  0xd4, 0x75, 0x60, 0xa9, 0x32, 0xac, 0xc0, 0x62, 0xc0, 0x36, 0xc3, 0xf2,
+  0x4f, 0x1e, 0xb5, 0xe1, 0xce, 0x75, 0x76, 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, 0xe8, 0x22, 0x1e, 0x8e, 0x3c, 0xb9, 0x92,
+  0x92, 0xbb, 0xc8, 0x70, 0x85, 0xba, 0xd5, 0xb4, 0x33, 0xcd, 0x0f, 0x7f,
+  0x76, 0xc8, 0x6f, 0x13, 0x64, 0xf2, 0x7a, 0xcd, 0x82, 0x41, 0x8e, 0xec,
+  0x0e, 0x43, 0xa9, 0x19, 0x79, 0x9f, 0x59, 0x9e, 0xf0, 0xb3, 0x50, 0x85,
+  0x94, 0x22, 0x95, 0x4b, 0xf6, 0x5c, 0xb7, 0xf3, 0xbe, 0x66, 0xfa, 0x3f,
+  0x75, 0x84, 0x07, 0x3c, 0x97, 0x71, 0x9f, 0x43, 0x71, 0xd1, 0x50, 0xfa,
+  0x75, 0xd0, 0x59, 0x1e, 0xb1, 0xd0, 0x2d, 0xd3, 0x8d, 0x16, 0x5c, 0xdf,
+  0x61
+};
+unsigned int certificate_der_len = 1369;
diff --git a/roms/SLOF/lib/libcrypto/libcrypto.h b/roms/SLOF/lib/libcrypto/libcrypto.h
new file mode 100644
index 0000000..2980ca0
--- /dev/null
+++ b/roms/SLOF/lib/libcrypto/libcrypto.h
@@ -0,0 +1,5 @@
+// libcrypto.h
+
+#include <stddef.h>
+
+int verify_appended_signature(void *blob, size_t len);
\ No newline at end of file
diff --git a/roms/SLOF/lib/libelf/Makefile b/roms/SLOF/lib/libelf/Makefile
index 34a8f20..ce43457 100644
--- a/roms/SLOF/lib/libelf/Makefile
+++ b/roms/SLOF/lib/libelf/Makefile
@@ -14,7 +14,8 @@ TOPCMNDIR ?= ../..
 
 include $(TOPCMNDIR)/make.rules
 
-CPPFLAGS = -I../libc/include $(CPUARCHDEF) -I$(INCLCMNDIR) -I$(INCLCMNDIR)/$(CPUARCH)
+CPPFLAGS = -I../libc/include $(CPUARCHDEF) -I$(INCLCMNDIR) -I$(INCLCMNDIR)/$(CPUARCH) \
+	   -I$(LIBCMNDIR)/libcrypto
 LDFLAGS= -nostdlib
 
 TARGET = ../libelf.a
diff --git a/roms/SLOF/lib/libelf/elf32.c b/roms/SLOF/lib/libelf/elf32.c
index f378db0..6896e64 100644
--- a/roms/SLOF/lib/libelf/elf32.c
+++ b/roms/SLOF/lib/libelf/elf32.c
@@ -18,11 +18,10 @@
 #include <libelf.h>
 #include <byteorder.h>
 #include <helpers.h>
+#include <libcrypto.h>
 
 #define ALIGN(x, a) (((x) + ((a) - 1)) & ~((a) - 1))
 
-static char appsig_magic[] = "~Module signature appended~\n";
-
 struct ehdr32 {
 	uint32_t ei_ident;
 	uint8_t ei_class;
@@ -74,16 +73,6 @@ struct nhdr32 {
 	uint32_t n_type;
 };
 
-struct module_signature {
-	uint8_t		algo;		/* Public-key crypto algorithm [0] */
-	uint8_t		hash;		/* Digest algorithm [0] */
-	uint8_t		id_type;	/* Key identifier type [PKEY_ID_PKCS7] */
-	uint8_t		signer_len;	/* Length of signer's name [0] */
-	uint8_t		key_id_len;	/* Length of key identifier [0] */
-	uint8_t		__pad[3];
-	uint32_t	sig_len;	/* Length of signature data */
-};
-
 static struct phdr32*
 get_phdr32(void *file_addr)
 {
@@ -145,8 +134,7 @@ elf_load_segments32(void *file_addr, signed long offset,
 			             post_load);
 		} else if (phdr->p_type == PT_NOTE) {
 			struct nhdr32 *nhdr = (struct nhdr32 *)(file_addr + phdr->p_offset);
-			char *ptr;
-			struct module_signature *modsig;
+			size_t size;
 			//printf("%x %x %x\n", nhdr->n_namesz, nhdr->n_descsz, nhdr->n_type);
 
 			// todo - endiansafeness
@@ -154,39 +142,18 @@ elf_load_segments32(void *file_addr, signed long offset,
 			if (nhdr->n_type == APPENDED_SIGNATURE_NOTE_TYPE) {
 				// todo, verify name
 
-				// check for magic
-				// go to start of appsig block
-				ptr = (char *)(nhdr) + sizeof(struct nhdr32) + ALIGN(nhdr->n_namesz, 4);
-				// go to start of sig
-				ptr += nhdr->n_descsz - sizeof(appsig_magic) + 1; // appsig_magic contains null-term
-				
-				if (strncmp(ptr, appsig_magic, sizeof(appsig_magic))) {
-					printf("ELF32: appended signature note present but magic string missing. Aborting.\n");
-					return 0;
-				}
-
-				// now load the sig info
-				ptr -= sizeof(struct module_signature);
-				modsig = (struct module_signature *)ptr;
-
-				//printf("%x %x %x %x %x\n", modsig->algo, modsig->hash, modsig->id_type, modsig->key_id_len, modsig->signer_len);
-				if (modsig->id_type != 2) { // pkcs7
-					printf("ELF32: appended signature is in an unexpected format (not PKCS#7). Aborting.\n");
-					return 0;
-				}
-
-				if (modsig->algo != 0 || modsig->hash != 0 || modsig->key_id_len != 0 || modsig->signer_len != 0) {
-					printf("ELF32: appended signature has an unexpected parameter inconsistend with PKCS#7. Aborting.\n");
-					return 0;
-				}
-
+				// not clear if this is of value or how to still do it.
 				//printf("%u %u %u -1 = %u\n", modsig->sig_len, sizeof(struct module_signature), sizeof(appsig_magic), nhdr->n_descsz);
-				if (ALIGN(modsig->sig_len + sizeof(struct module_signature) + sizeof(appsig_magic) - 1, 4) != nhdr->n_descsz) {
-					printf("ELF32: Appended signature component sizes don't add up as expected. Aborting.\n");
+				/*if (ALIGN(modsig->sig_len + sizeof(struct module_signature) + sizeof(appsig_magic) - 1, 4) != nhdr->n_descsz) {
+					printf("ELF32: Appended signature: component sizes don't add up as expected. Aborting.\n");
+					return 0;
+				}*/
+				size = (phdr->p_offset + sizeof(struct nhdr32)
+					+ ALIGN(nhdr->n_namesz, 4)
+				        + ALIGN(nhdr->n_descsz, 4));
+				if (!verify_appended_signature(file_addr, size)) {
 					return 0;
 				}
-
-				
 			}
 		}
 		/* step to next header */
-- 
2.33.1

openSUSE Build Service is sponsored by