blob: 2e7366efa3b24ca4bdb07e3573e93bb9c65f9130 [file] [log] [blame]
Shashank Mittalcd98d472011-08-02 14:29:24 -07001/*
Sridhar Parasuramd39a4282015-08-29 09:54:28 -07002 * Copyright (c) 2011,2013-2015 The Linux Foundation. All rights reserved.
Shashank Mittalcd98d472011-08-02 14:29:24 -07003 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in
11 * the documentation and/or other materials provided with the
12 * distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
17 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
18 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
21 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
24 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27#include <x509.h>
Sridhar Parasuramd39a4282015-08-29 09:54:28 -070028#include <err.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070029#include <certificate.h>
30#include <crypto_hash.h>
Sridhar Parasuramd39a4282015-08-29 09:54:28 -070031#include <string.h>
32#include <openssl/err.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070033#include "image_verify.h"
Amir Samuelov67ae1d32013-03-24 16:27:52 +020034#include "scm.h"
Shashank Mittalcd98d472011-08-02 14:29:24 -070035
Sridhar Parasuramd39a4282015-08-29 09:54:28 -070036
37/*
38 * Returns -1 if decryption failed otherwise size of plain_text in bytes
39 */
40int image_decrypt_signature_rsa(unsigned char *signature_ptr,
41 unsigned char *plain_text, RSA *rsa_key)
42{
43 int ret = -1;
44
45 if (rsa_key == NULL) {
46 dprintf(CRITICAL, "ERROR: Boot Invalid, RSA_KEY is NULL!\n");
47 return ret;
48 }
49
50 ret = RSA_public_decrypt(SIGNATURE_SIZE, signature_ptr, plain_text,
51 rsa_key, RSA_PKCS1_PADDING);
52 dprintf(SPEW, "DEBUG openssl: Return of RSA_public_decrypt = %d\n",
53 ret);
54
55 return ret;
56}
57
Shashank Mittalcd98d472011-08-02 14:29:24 -070058/*
59 * Returns -1 if decryption failed otherwise size of plain_text in bytes
60 */
Ajay Dudanib01e5062011-12-03 23:23:42 -080061static int
62image_decrypt_signature(unsigned char *signature_ptr, unsigned char *plain_text)
63{
Shashank Mittalcd98d472011-08-02 14:29:24 -070064 /*
65 * Extract Public Key and Decrypt Signature
66 */
67 int ret = -1;
68 X509 *x509_certificate = NULL;
Sridhar Parasuramd39a4282015-08-29 09:54:28 -070069 const unsigned char *cert_ptr = (const unsigned char *)certBuffer;
Shashank Mittalcd98d472011-08-02 14:29:24 -070070 unsigned int cert_size = sizeof(certBuffer);
71 EVP_PKEY *pub_key = NULL;
Ajay Dudanib01e5062011-12-03 23:23:42 -080072 RSA *rsa_key = NULL;
Shashank Mittalcd98d472011-08-02 14:29:24 -070073
74 /*
75 * Get Pubkey and Convert the internal EVP_PKEY to RSA internal struct
76 */
Ajay Dudanib01e5062011-12-03 23:23:42 -080077 if ((x509_certificate = d2i_X509(NULL, &cert_ptr, cert_size)) == NULL) {
Shashank Mittalcd98d472011-08-02 14:29:24 -070078 dprintf(CRITICAL,
79 "ERROR: Image Invalid, X509_Certificate is NULL!\n");
80 goto cleanup;
81 }
82 pub_key = X509_get_pubkey(x509_certificate);
83 rsa_key = EVP_PKEY_get1_RSA(pub_key);
Ajay Dudanib01e5062011-12-03 23:23:42 -080084 if (rsa_key == NULL) {
85 dprintf(CRITICAL, "ERROR: Boot Invalid, RSA_KEY is NULL!\n");
Shashank Mittalcd98d472011-08-02 14:29:24 -070086 goto cleanup;
87 }
88
Sridhar Parasuramd39a4282015-08-29 09:54:28 -070089 ret = image_decrypt_signature_rsa(signature_ptr, plain_text, rsa_key);
Ajay Dudanib01e5062011-12-03 23:23:42 -080090 dprintf(SPEW, "DEBUG openssl: Return of RSA_public_decrypt = %d\n",
91 ret);
Shashank Mittalcd98d472011-08-02 14:29:24 -070092
Ajay Dudanib01e5062011-12-03 23:23:42 -080093 cleanup:
Shashank Mittalcd98d472011-08-02 14:29:24 -070094 if (rsa_key != NULL)
95 RSA_free(rsa_key);
96 if (x509_certificate != NULL)
97 X509_free(x509_certificate);
98 if (pub_key != NULL)
99 EVP_PKEY_free(pub_key);
100 return ret;
101}
102
Sridhar Parasuramd39a4282015-08-29 09:54:28 -0700103/* Calculates digest of an image and save it in digest buffer */
104void image_find_digest(unsigned char *image_ptr, unsigned int image_size,
105 unsigned hash_type, unsigned char *digest)
106{
107 /*
108 * Calculate hash of image and save calculated hash on TZ.
109 */
110 hash_find(image_ptr, image_size, (unsigned char *)digest, hash_type);
111}
112
113#ifdef TZ_SAVE_KERNEL_HASH
114void save_kernel_hash(unsigned char *digest, unsigned hash_type)
115{
116 if (hash_type == CRYPTO_AUTH_ALG_SHA256) {
117 save_kernel_hash_cmd(digest);
118 dprintf(INFO, "Image hash saved.\n");
119 } else
120 dprintf(INFO, "image_verify: hash is not SHA-256.\n");
121}
122#endif
123
Shashank Mittalcd98d472011-08-02 14:29:24 -0700124/*
125 * Returns 1 when image is signed and authorized.
126 * Returns 0 when image is unauthorized.
127 * Expects a pointer to the start of image and pointer to start of sig
128 */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800129int
130image_verify(unsigned char *image_ptr,
131 unsigned char *signature_ptr,
132 unsigned int image_size, unsigned hash_type)
133{
Shashank Mittalcd98d472011-08-02 14:29:24 -0700134
135 int ret = -1;
136 int auth = 0;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800137 unsigned char *plain_text = NULL;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700138 unsigned int digest[8];
Sridhar Parasuramd39a4282015-08-29 09:54:28 -0700139 int hash_size;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700140
Ajay Dudanib01e5062011-12-03 23:23:42 -0800141 plain_text = (unsigned char *)calloc(sizeof(char), SIGNATURE_SIZE);
142 if (plain_text == NULL) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700143 dprintf(CRITICAL, "ERROR: Calloc failed during verification\n");
144 goto cleanup;
145 }
146
Amir Samuelov903a9502014-03-13 13:48:57 +0200147 /*
148 * Calculate hash of image and save calculated hash on TZ.
149 */
150 hash_size =
151 (hash_type == CRYPTO_AUTH_ALG_SHA256) ? SHA256_SIZE : SHA1_SIZE;
Sridhar Parasuramd39a4282015-08-29 09:54:28 -0700152 image_find_digest(image_ptr, image_size, hash_type,
153 (unsigned char *)&digest);
Amir Samuelov903a9502014-03-13 13:48:57 +0200154#ifdef TZ_SAVE_KERNEL_HASH
Sridhar Parasuramd39a4282015-08-29 09:54:28 -0700155 save_kernel_hash((unsigned char *) &digest, hash_type);
Amir Samuelov903a9502014-03-13 13:48:57 +0200156#endif
157
158 /*
159 * Decrypt the pre-calculated expected image hash.
Sundarajan Srinivasanf2a513d2014-04-21 14:14:30 -0700160 * Return value, ret should be equal to hash_size. Otherwise it means a failure. With this check
161 * we avoid a potential vulnerability due to trailing data placed at the end of digest.
Amir Samuelov903a9502014-03-13 13:48:57 +0200162 */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700163 ret = image_decrypt_signature(signature_ptr, plain_text);
Sundarajan Srinivasanf2a513d2014-04-21 14:14:30 -0700164 if (ret != hash_size) {
165 dprintf(CRITICAL, "ERROR: Image Invalid! signature check failed! ret %d\n", ret);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700166 goto cleanup;
167 }
168
169 /*
Amir Samuelov903a9502014-03-13 13:48:57 +0200170 * Compare the expected hash with the calculated hash.
Shashank Mittalcd98d472011-08-02 14:29:24 -0700171 */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800172 if (memcmp(plain_text, digest, hash_size) != 0) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700173 dprintf(CRITICAL,
174 "ERROR: Image Invalid! Please use another image!\n");
175 ret = -1;
176 goto cleanup;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800177 } else {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700178 /* Authorized image */
179 auth = 1;
180 }
181
182 /* Cleanup after complete usage of openssl - cached data and objects */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800183 cleanup:
Shashank Mittalcd98d472011-08-02 14:29:24 -0700184 if (plain_text != NULL)
185 free(plain_text);
186 EVP_cleanup();
187 CRYPTO_cleanup_all_ex_data();
188 ERR_remove_thread_state(NULL);
189 return auth;
190}