blob: 0d280f2c936749c1b1c76c8415c95eb30a16caf9 [file] [log] [blame]
Shashank Mittalcd98d472011-08-02 14:29:24 -07001/*
Amir Samuelov903a9502014-03-13 13:48:57 +02002 * Copyright (c) 2011,2013-2014 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>
28#include <certificate.h>
29#include <crypto_hash.h>
30#include "image_verify.h"
Amir Samuelov67ae1d32013-03-24 16:27:52 +020031#include "scm.h"
Shashank Mittalcd98d472011-08-02 14:29:24 -070032
33/*
34 * Returns -1 if decryption failed otherwise size of plain_text in bytes
35 */
Ajay Dudanib01e5062011-12-03 23:23:42 -080036static int
37image_decrypt_signature(unsigned char *signature_ptr, unsigned char *plain_text)
38{
Shashank Mittalcd98d472011-08-02 14:29:24 -070039 /*
40 * Extract Public Key and Decrypt Signature
41 */
42 int ret = -1;
43 X509 *x509_certificate = NULL;
44 unsigned char *cert_ptr = certBuffer;
45 unsigned int cert_size = sizeof(certBuffer);
46 EVP_PKEY *pub_key = NULL;
Ajay Dudanib01e5062011-12-03 23:23:42 -080047 RSA *rsa_key = NULL;
Shashank Mittalcd98d472011-08-02 14:29:24 -070048
49 /*
50 * Get Pubkey and Convert the internal EVP_PKEY to RSA internal struct
51 */
Ajay Dudanib01e5062011-12-03 23:23:42 -080052 if ((x509_certificate = d2i_X509(NULL, &cert_ptr, cert_size)) == NULL) {
Shashank Mittalcd98d472011-08-02 14:29:24 -070053 dprintf(CRITICAL,
54 "ERROR: Image Invalid, X509_Certificate is NULL!\n");
55 goto cleanup;
56 }
57 pub_key = X509_get_pubkey(x509_certificate);
58 rsa_key = EVP_PKEY_get1_RSA(pub_key);
Ajay Dudanib01e5062011-12-03 23:23:42 -080059 if (rsa_key == NULL) {
60 dprintf(CRITICAL, "ERROR: Boot Invalid, RSA_KEY is NULL!\n");
Shashank Mittalcd98d472011-08-02 14:29:24 -070061 goto cleanup;
62 }
63
64 ret = RSA_public_decrypt(SIGNATURE_SIZE, signature_ptr, plain_text,
Ajay Dudanib01e5062011-12-03 23:23:42 -080065 rsa_key, RSA_PKCS1_PADDING);
66 dprintf(SPEW, "DEBUG openssl: Return of RSA_public_decrypt = %d\n",
67 ret);
Shashank Mittalcd98d472011-08-02 14:29:24 -070068
Ajay Dudanib01e5062011-12-03 23:23:42 -080069 cleanup:
Shashank Mittalcd98d472011-08-02 14:29:24 -070070 if (rsa_key != NULL)
71 RSA_free(rsa_key);
72 if (x509_certificate != NULL)
73 X509_free(x509_certificate);
74 if (pub_key != NULL)
75 EVP_PKEY_free(pub_key);
76 return ret;
77}
78
79/*
80 * Returns 1 when image is signed and authorized.
81 * Returns 0 when image is unauthorized.
82 * Expects a pointer to the start of image and pointer to start of sig
83 */
Ajay Dudanib01e5062011-12-03 23:23:42 -080084int
85image_verify(unsigned char *image_ptr,
86 unsigned char *signature_ptr,
87 unsigned int image_size, unsigned hash_type)
88{
Shashank Mittalcd98d472011-08-02 14:29:24 -070089
90 int ret = -1;
91 int auth = 0;
Ajay Dudanib01e5062011-12-03 23:23:42 -080092 unsigned char *plain_text = NULL;
Shashank Mittalcd98d472011-08-02 14:29:24 -070093 unsigned int digest[8];
94 unsigned int hash_size;
95
Ajay Dudanib01e5062011-12-03 23:23:42 -080096 plain_text = (unsigned char *)calloc(sizeof(char), SIGNATURE_SIZE);
97 if (plain_text == NULL) {
Shashank Mittalcd98d472011-08-02 14:29:24 -070098 dprintf(CRITICAL, "ERROR: Calloc failed during verification\n");
99 goto cleanup;
100 }
101
Amir Samuelov903a9502014-03-13 13:48:57 +0200102 /*
103 * Calculate hash of image and save calculated hash on TZ.
104 */
105 hash_size =
106 (hash_type == CRYPTO_AUTH_ALG_SHA256) ? SHA256_SIZE : SHA1_SIZE;
107 hash_find(image_ptr, image_size, (unsigned char *)&digest, hash_type);
108#ifdef TZ_SAVE_KERNEL_HASH
109 if (hash_type == CRYPTO_AUTH_ALG_SHA256) {
110 save_kernel_hash_cmd(digest);
111 dprintf(INFO, "Image hash saved.\n");
112 } else
113 dprintf(INFO, "image_verify: hash is not SHA-256.\n");
114#endif
115
116 /*
117 * Decrypt the pre-calculated expected image hash.
Sundarajan Srinivasanf2a513d2014-04-21 14:14:30 -0700118 * Return value, ret should be equal to hash_size. Otherwise it means a failure. With this check
119 * we avoid a potential vulnerability due to trailing data placed at the end of digest.
Amir Samuelov903a9502014-03-13 13:48:57 +0200120 */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700121 ret = image_decrypt_signature(signature_ptr, plain_text);
Sundarajan Srinivasanf2a513d2014-04-21 14:14:30 -0700122 if (ret != hash_size) {
123 dprintf(CRITICAL, "ERROR: Image Invalid! signature check failed! ret %d\n", ret);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700124 goto cleanup;
125 }
126
127 /*
Amir Samuelov903a9502014-03-13 13:48:57 +0200128 * Compare the expected hash with the calculated hash.
Shashank Mittalcd98d472011-08-02 14:29:24 -0700129 */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800130 if (memcmp(plain_text, digest, hash_size) != 0) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700131 dprintf(CRITICAL,
132 "ERROR: Image Invalid! Please use another image!\n");
133 ret = -1;
134 goto cleanup;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800135 } else {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700136 /* Authorized image */
137 auth = 1;
138 }
139
140 /* Cleanup after complete usage of openssl - cached data and objects */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800141 cleanup:
Shashank Mittalcd98d472011-08-02 14:29:24 -0700142 if (plain_text != NULL)
143 free(plain_text);
144 EVP_cleanup();
145 CRYPTO_cleanup_all_ex_data();
146 ERR_remove_thread_state(NULL);
147 return auth;
148}