platform: msm_shared: Add kernel authentication support

Add kernel image authentication support for LE targets with
SHA256, with signature size 2048 and exponent 65537.

Change-Id: Ibd0db2b675d4143de24eb208237a57add89e7aff
diff --git a/platform/msm_shared/LEOEMCertificate.h b/platform/msm_shared/LEOEMCertificate.h
new file mode 100644
index 0000000..af72a51
--- /dev/null
+++ b/platform/msm_shared/LEOEMCertificate.h
@@ -0,0 +1,110 @@
+/* Copyright (c) 2017, The Linux Foundation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ * * Redistributions of source code must retain the above copyright
+ *  notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ *  with the distribution.
+ *   * Neither the name of The Linux Foundation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+#ifndef __LE_OEM_CERTIFICATE_H
+#define __LE_OEM_CERTIFICATE_H
+
+/*
+	Instructions to generate private.key and X509 certificate
+
+A) Generate a sha256 self-signed X509 certificate and a 2048 bits RSA private key
+$ openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout qtipri.key -out qti.crt.pem
+
+B) convert private key from PKCS#8 to PKCS#1 format, save it as qti.key.
+$ openssl rsa -in qtipri.key -out qti.key
+
+C) This private key "qti.key" need to be saved under folder "poky/recipes-kernel/linux-quic/kernel/" and
+"poky/recipes-kernel/linux-msm-4.4/kernel/", used to generate bootimage signature.
+
+D) convert certificate from PEM to DER format
+$ openssl x509 -outform der -in qti.crt.pem -out qti.crt.der
+
+E) open qti.crt.der file in a HEX editor, and then put HEX numbers into array
+    LE_OEM_CERTIFICATE[] in LEOEMCertificate.h
+
+*/
+
+const char LE_OEM_CERTIFICATE[] = {
+	0x30, 0x82, 0x03, 0x5D, 0x30, 0x82, 0x02, 0x45, 0xA0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x09, 0x00,
+	0xFA, 0xCD, 0x18, 0x11, 0xED, 0xD3, 0x4D, 0xC8, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
+	0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x05, 0x00, 0x30, 0x45, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55,
+	0x04, 0x06, 0x13, 0x02, 0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C,
+	0x0A, 0x53, 0x6F, 0x6D, 0x65, 0x2D, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, 0x21, 0x30, 0x1F, 0x06,
+	0x03, 0x55, 0x04, 0x0A, 0x0C, 0x18, 0x49, 0x6E, 0x74, 0x65, 0x72, 0x6E, 0x65, 0x74, 0x20, 0x57,
+	0x69, 0x64, 0x67, 0x69, 0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4C, 0x74, 0x64, 0x30, 0x1E,
+	0x17, 0x0D, 0x31, 0x37, 0x30, 0x38, 0x30, 0x35, 0x30, 0x36, 0x33, 0x39, 0x32, 0x32, 0x5A, 0x17,
+	0x0D, 0x31, 0x38, 0x30, 0x38, 0x30, 0x35, 0x30, 0x36, 0x33, 0x39, 0x32, 0x32, 0x5A, 0x30, 0x45,
+	0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x41, 0x55, 0x31, 0x13, 0x30,
+	0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x0A, 0x53, 0x6F, 0x6D, 0x65, 0x2D, 0x53, 0x74, 0x61,
+	0x74, 0x65, 0x31, 0x21, 0x30, 0x1F, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x18, 0x49, 0x6E, 0x74,
+	0x65, 0x72, 0x6E, 0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69, 0x74, 0x73, 0x20, 0x50, 0x74,
+	0x79, 0x20, 0x4C, 0x74, 0x64, 0x30, 0x82, 0x01, 0x22, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48,
+	0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0F, 0x00, 0x30, 0x82, 0x01,
+	0x0A, 0x02, 0x82, 0x01, 0x01, 0x00, 0xA6, 0xC6, 0x48, 0xE4, 0xF6, 0x8B, 0x42, 0x2A, 0xFC, 0xB0,
+	0x4A, 0x7A, 0x0F, 0x1A, 0x5E, 0x40, 0x78, 0x8B, 0xBD, 0x30, 0xBD, 0xA1, 0xBF, 0xDC, 0xB4, 0x53,
+	0x48, 0x31, 0x42, 0x11, 0xCD, 0x83, 0x70, 0x48, 0x75, 0x58, 0x00, 0x82, 0x50, 0x43, 0x4B, 0xA9,
+	0xCF, 0xEE, 0x81, 0x8C, 0xA4, 0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
+	0x77, 0x74, 0xB2, 0x8C, 0xD5, 0x7C, 0xF3, 0xA4, 0x0E, 0x1A, 0x48, 0x04, 0x81, 0x32, 0xF1, 0x39,
+	0x9B, 0x57, 0xC9, 0xBB, 0xFF, 0x15, 0xB6, 0xE5, 0x20, 0x0D, 0x03, 0x60, 0xBD, 0x3F, 0x29, 0x03,
+	0xEF
+};
+
+#endif
diff --git a/platform/msm_shared/image_verify.c b/platform/msm_shared/image_verify.c
index 6fff3d2..6fedfd8 100644
--- a/platform/msm_shared/image_verify.c
+++ b/platform/msm_shared/image_verify.c
@@ -29,10 +29,17 @@
 #include <certificate.h>
 #include <crypto_hash.h>
 #include <string.h>
+#include <platform.h>
 #include <openssl/err.h>
 #include "image_verify.h"
 #include "scm.h"
 
+#include <LEOEMCertificate.h>
+
+const char hash_identifier[] = {
+	0x30, 0x31, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
+	0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20
+};
 
 /*
  * Returns -1 if decryption failed otherwise size of plain_text in bytes
@@ -66,8 +73,17 @@
 	 */
 	int ret = -1;
 	X509 *x509_certificate = NULL;
-	const unsigned char *cert_ptr = (const unsigned char *)certBuffer;
-	unsigned int cert_size = sizeof(certBuffer);
+	const unsigned char *cert_ptr = NULL;
+	unsigned int cert_size = 0;
+
+	if (is_vb_le_enabled()) {
+		cert_ptr = (const unsigned char *)LE_OEM_CERTIFICATE;
+		cert_size = sizeof(LE_OEM_CERTIFICATE);
+	} else {
+		cert_ptr = (const unsigned char *)certBuffer;
+		cert_size = sizeof(certBuffer);
+	}
+
 	EVP_PKEY *pub_key = NULL;
 	RSA *rsa_key = NULL;
 
@@ -166,23 +182,43 @@
 	 * we avoid a potential vulnerability due to trailing data placed at the end of digest.
 	 */
 	ret = image_decrypt_signature(signature_ptr, plain_text);
-	if (ret != hash_size) {
-		dprintf(CRITICAL, "ERROR: Image Invalid! signature check failed! ret %d\n", ret);
-		goto cleanup;
+	if (is_vb_le_enabled()) {
+		int sha256_pkcs1_hash_identifier_len = sizeof(hash_identifier);
+		if (ret != (hash_size + sha256_pkcs1_hash_identifier_len)) {
+			dprintf(CRITICAL, "ERROR: VB: Image Invalid! signature check failed! ret %d\n", ret);
+			goto cleanup;
+		}
+		/*
+		 * Compare the expected hash with the calculated hash.
+		 */
+		if (memcmp(plain_text, hash_identifier, sha256_pkcs1_hash_identifier_len) != 0) {
+			dprintf(CRITICAL,
+				"ERROR: VB: Hash identifier is wrong!\n");
+			goto cleanup;
+		}
+		if (memcmp(plain_text + sha256_pkcs1_hash_identifier_len, digest, hash_size) != 0) {
+			dprintf(CRITICAL,
+				"ERROR: VB: Image Invalid! Please use another image!\n");
+			goto cleanup;
+		}
+	} else {
+		if (ret != hash_size) {
+			dprintf(CRITICAL, "ERROR: Image Invalid! signature check failed! ret %d\n", ret);
+			goto cleanup;
+		}
+		/*
+		 * Compare the expected hash with the calculated hash.
+		 */
+		if (memcmp(plain_text, digest, hash_size) != 0) {
+			dprintf(CRITICAL,
+				"ERROR: Image Invalid! Please use another image!\n");
+			ret = -1;
+			goto cleanup;
+		}
 	}
 
-	/*
-	 * Compare the expected hash with the calculated hash.
-	 */
-	if (memcmp(plain_text, digest, hash_size) != 0) {
-		dprintf(CRITICAL,
-			"ERROR: Image Invalid! Please use another image!\n");
-		ret = -1;
-		goto cleanup;
-	} else {
-		/* Authorized image */
-		auth = 1;
-	}
+	/* Authorized image */
+	auth = 1;
 
 	/* Cleanup after complete usage of openssl - cached data and objects */
  cleanup: