Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 1 | /* |
Amir Samuelov | a338294 | 2014-03-13 13:48:57 +0200 | [diff] [blame] | 2 | * Copyright (c) 2011,2013-2014 The Linux Foundation. All rights reserved. |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 3 | * |
| 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 Samuelov | 67ae1d3 | 2013-03-24 16:27:52 +0200 | [diff] [blame] | 31 | #include "scm.h" |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 32 | |
| 33 | /* |
| 34 | * Returns -1 if decryption failed otherwise size of plain_text in bytes |
| 35 | */ |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 36 | int image_decrypt_signature_rsa(unsigned char *signature_ptr, |
| 37 | unsigned char *plain_text, RSA *rsa_key) |
| 38 | { |
| 39 | int ret = -1; |
| 40 | |
| 41 | if (rsa_key == NULL) { |
| 42 | dprintf(CRITICAL, "ERROR: Boot Invalid, RSA_KEY is NULL!\n"); |
| 43 | return ret; |
| 44 | } |
| 45 | |
| 46 | ret = RSA_public_decrypt(SIGNATURE_SIZE, signature_ptr, plain_text, |
| 47 | rsa_key, RSA_PKCS1_PADDING); |
| 48 | dprintf(SPEW, "DEBUG openssl: Return of RSA_public_decrypt = %d\n", |
| 49 | ret); |
| 50 | |
| 51 | return ret; |
| 52 | } |
| 53 | |
| 54 | /* |
| 55 | * Returns -1 if decryption failed otherwise size of plain_text in bytes |
| 56 | */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 57 | static int |
| 58 | image_decrypt_signature(unsigned char *signature_ptr, unsigned char *plain_text) |
| 59 | { |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 60 | /* |
| 61 | * Extract Public Key and Decrypt Signature |
| 62 | */ |
| 63 | int ret = -1; |
| 64 | X509 *x509_certificate = NULL; |
| 65 | unsigned char *cert_ptr = certBuffer; |
| 66 | unsigned int cert_size = sizeof(certBuffer); |
| 67 | EVP_PKEY *pub_key = NULL; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 68 | RSA *rsa_key = NULL; |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 69 | |
| 70 | /* |
| 71 | * Get Pubkey and Convert the internal EVP_PKEY to RSA internal struct |
| 72 | */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 73 | if ((x509_certificate = d2i_X509(NULL, &cert_ptr, cert_size)) == NULL) { |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 74 | dprintf(CRITICAL, |
| 75 | "ERROR: Image Invalid, X509_Certificate is NULL!\n"); |
| 76 | goto cleanup; |
| 77 | } |
| 78 | pub_key = X509_get_pubkey(x509_certificate); |
Lijuan Gao | 0e2caf2 | 2014-07-15 18:36:18 +0800 | [diff] [blame] | 79 | if (pub_key == NULL) { |
| 80 | dprintf(CRITICAL, "ERROR: Boot Invalid, PUB_KEY is NULL!\n"); |
| 81 | goto cleanup; |
| 82 | } |
| 83 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 84 | rsa_key = EVP_PKEY_get1_RSA(pub_key); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 85 | if (rsa_key == NULL) { |
| 86 | dprintf(CRITICAL, "ERROR: Boot Invalid, RSA_KEY is NULL!\n"); |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 87 | goto cleanup; |
| 88 | } |
| 89 | |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 90 | ret = image_decrypt_signature_rsa(signature_ptr, plain_text, rsa_key); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 91 | dprintf(SPEW, "DEBUG openssl: Return of RSA_public_decrypt = %d\n", |
| 92 | ret); |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 93 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 94 | cleanup: |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 95 | if (rsa_key != NULL) |
| 96 | RSA_free(rsa_key); |
| 97 | if (x509_certificate != NULL) |
| 98 | X509_free(x509_certificate); |
| 99 | if (pub_key != NULL) |
| 100 | EVP_PKEY_free(pub_key); |
| 101 | return ret; |
| 102 | } |
| 103 | |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 104 | /* Calculates digest of an image and save it in digest buffer */ |
| 105 | void image_find_digest(unsigned char *image_ptr, unsigned int image_size, |
| 106 | unsigned hash_type, unsigned char *digest) |
| 107 | { |
| 108 | /* |
| 109 | * Calculate hash of image and save calculated hash on TZ. |
| 110 | */ |
| 111 | hash_find(image_ptr, image_size, (unsigned char *)digest, hash_type); |
| 112 | #ifdef TZ_SAVE_KERNEL_HASH |
| 113 | if (hash_type == CRYPTO_AUTH_ALG_SHA256) { |
| 114 | save_kernel_hash_cmd(digest); |
| 115 | dprintf(INFO, "Image hash saved.\n"); |
| 116 | } else |
| 117 | dprintf(INFO, "image_verify: hash is not SHA-256.\n"); |
| 118 | #endif |
| 119 | } |
| 120 | |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 121 | /* |
| 122 | * Returns 1 when image is signed and authorized. |
| 123 | * Returns 0 when image is unauthorized. |
| 124 | * Expects a pointer to the start of image and pointer to start of sig |
| 125 | */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 126 | int |
| 127 | image_verify(unsigned char *image_ptr, |
| 128 | unsigned char *signature_ptr, |
| 129 | unsigned int image_size, unsigned hash_type) |
| 130 | { |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 131 | |
| 132 | int ret = -1; |
| 133 | int auth = 0; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 134 | unsigned char *plain_text = NULL; |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 135 | unsigned int digest[8]; |
| 136 | unsigned int hash_size; |
| 137 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 138 | plain_text = (unsigned char *)calloc(sizeof(char), SIGNATURE_SIZE); |
| 139 | if (plain_text == NULL) { |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 140 | dprintf(CRITICAL, "ERROR: Calloc failed during verification\n"); |
| 141 | goto cleanup; |
| 142 | } |
| 143 | |
Amir Samuelov | a338294 | 2014-03-13 13:48:57 +0200 | [diff] [blame] | 144 | /* |
| 145 | * Calculate hash of image and save calculated hash on TZ. |
| 146 | */ |
| 147 | hash_size = |
| 148 | (hash_type == CRYPTO_AUTH_ALG_SHA256) ? SHA256_SIZE : SHA1_SIZE; |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 149 | image_find_digest(image_ptr, image_size, hash_type, |
| 150 | (unsigned char *)&digest); |
Amir Samuelov | a338294 | 2014-03-13 13:48:57 +0200 | [diff] [blame] | 151 | |
| 152 | /* |
| 153 | * Decrypt the pre-calculated expected image hash. |
Sundarajan Srinivasan | f390859 | 2014-04-21 14:14:30 -0700 | [diff] [blame] | 154 | * Return value, ret should be equal to hash_size. Otherwise it means a failure. With this check |
| 155 | * we avoid a potential vulnerability due to trailing data placed at the end of digest. |
Amir Samuelov | a338294 | 2014-03-13 13:48:57 +0200 | [diff] [blame] | 156 | */ |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 157 | ret = image_decrypt_signature(signature_ptr, plain_text); |
Sundarajan Srinivasan | f390859 | 2014-04-21 14:14:30 -0700 | [diff] [blame] | 158 | if (ret != hash_size) { |
| 159 | dprintf(CRITICAL, "ERROR: Image Invalid! signature check failed! ret %d\n", ret); |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 160 | goto cleanup; |
| 161 | } |
| 162 | |
| 163 | /* |
Amir Samuelov | a338294 | 2014-03-13 13:48:57 +0200 | [diff] [blame] | 164 | * Compare the expected hash with the calculated hash. |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 165 | */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 166 | if (memcmp(plain_text, digest, hash_size) != 0) { |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 167 | dprintf(CRITICAL, |
| 168 | "ERROR: Image Invalid! Please use another image!\n"); |
| 169 | ret = -1; |
| 170 | goto cleanup; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 171 | } else { |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 172 | /* Authorized image */ |
| 173 | auth = 1; |
| 174 | } |
| 175 | |
| 176 | /* Cleanup after complete usage of openssl - cached data and objects */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 177 | cleanup: |
Shashank Mittal | cd98d47 | 2011-08-02 14:29:24 -0700 | [diff] [blame] | 178 | if (plain_text != NULL) |
| 179 | free(plain_text); |
| 180 | EVP_cleanup(); |
| 181 | CRYPTO_cleanup_all_ex_data(); |
| 182 | ERR_remove_thread_state(NULL); |
| 183 | return auth; |
| 184 | } |