2 * Copyright (C) 2014 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #define _LARGEFILE64_SOURCE
25 #include <sys/types.h>
30 #include <openssl/asn1.h>
31 #include <openssl/asn1t.h>
32 #include <openssl/crypto.h>
33 #include <openssl/err.h>
34 #include <openssl/evp.h>
35 #include <openssl/rsa.h>
36 #include <openssl/x509.h>
40 #define FORMAT_VERSION 1
41 #define BUFFER_SIZE (1024 * 1024)
48 ASN1_SEQUENCE(AuthAttrs) = {
49 ASN1_SIMPLE(AuthAttrs, target, ASN1_PRINTABLE),
50 ASN1_SIMPLE(AuthAttrs, length, ASN1_INTEGER)
51 } ASN1_SEQUENCE_END(AuthAttrs)
53 IMPLEMENT_ASN1_FUNCTIONS(AuthAttrs)
56 ASN1_INTEGER *formatVersion;
58 X509_ALGOR *algorithmIdentifier;
59 AuthAttrs *authenticatedAttributes;
60 ASN1_OCTET_STRING *signature;
63 ASN1_SEQUENCE(BootSignature) = {
64 ASN1_SIMPLE(BootSignature, formatVersion, ASN1_INTEGER),
65 ASN1_SIMPLE(BootSignature, certificate, X509),
66 ASN1_SIMPLE(BootSignature, algorithmIdentifier, X509_ALGOR),
67 ASN1_SIMPLE(BootSignature, authenticatedAttributes, AuthAttrs),
68 ASN1_SIMPLE(BootSignature, signature, ASN1_OCTET_STRING)
69 } ASN1_SEQUENCE_END(BootSignature)
71 IMPLEMENT_ASN1_FUNCTIONS(BootSignature)
73 static BIO *g_error = NULL;
75 #if defined(OPENSSL_IS_BORINGSSL)
76 /* In BoringSSL, ERR_print_errors has been moved to the BIO functions in order
77 * to avoid the incorrect dependency of ERR on BIO. */
78 static void ERR_print_errors(BIO *bio) {
79 BIO_print_errors(bio);
84 * Rounds n up to the nearest multiple of page_size
85 * @param n The value to round
86 * @param page_size Page size
88 static uint64_t page_align(uint64_t n, uint64_t page_size)
90 return (((n + page_size - 1) / page_size) * page_size);
94 * Calculates the offset to the beginning of the BootSignature block
95 * based on the boot image header. The signature will start after the
96 * the boot image contents.
97 * @param fd File descriptor to the boot image
98 * @param offset Receives the offset in bytes
100 static int get_signature_offset(int fd, off64_t *offset)
103 struct boot_img_hdr hdr;
109 if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
113 if (memcmp(BOOT_MAGIC, hdr.magic, BOOT_MAGIC_SIZE) != 0) {
114 printf("Invalid boot image: missing magic\n");
118 if (!hdr.page_size) {
119 printf("Invalid boot image: page size must be non-zero\n");
123 *offset = page_align(hdr.page_size
124 + page_align(hdr.kernel_size, hdr.page_size)
125 + page_align(hdr.ramdisk_size, hdr.page_size)
126 + page_align(hdr.second_size, hdr.page_size),
133 * Reads and parses the ASN.1 BootSignature block from the given offset
134 * @param fd File descriptor to the boot image
135 * @param offset Offset from the beginning of file to the signature
136 * @param bs Pointer to receive the BootImage structure
138 static int read_signature(int fd, off64_t offset, BootSignature **bs)
146 if (lseek64(fd, offset, SEEK_SET) == -1) {
150 if ((in = BIO_new_fd(fd, BIO_NOCLOSE)) == NULL) {
151 ERR_print_errors(g_error);
155 if ((*bs = ASN1_item_d2i_bio(ASN1_ITEM_rptr(BootSignature), in, bs)) == NULL) {
156 ERR_print_errors(g_error);
166 * Validates the format of the boot signature block, and checks that
167 * the length in authenticated attributes matches the actual length of
169 * @param bs The boot signature block to validate
170 * @param length The actual length of the boot image without the signature
172 static int validate_signature_block(const BootSignature *bs, uint64_t length)
185 /* Confirm that formatVersion matches our supported version */
186 if (!BN_set_word(&expected, FORMAT_VERSION)) {
187 ERR_print_errors(g_error);
191 ASN1_INTEGER_to_BN(bs->formatVersion, &value);
193 if (BN_cmp(&expected, &value) != 0) {
194 printf("Unsupported signature version\n");
201 /* Confirm that the length of the image matches with the length in
202 the authenticated attributes */
203 length = htobe64(length);
204 BN_bin2bn((const unsigned char *) &length, sizeof(length), &expected);
206 ASN1_INTEGER_to_BN(bs->authenticatedAttributes->length, &value);
208 if (BN_cmp(&expected, &value) != 0) {
209 printf("Image length doesn't match signature attributes\n");
223 * Creates a SHA-256 hash from the boot image contents and the encoded
224 * authenticated attributes.
225 * @param fd File descriptor to the boot image
226 * @param length Length of the boot image without the signature block
227 * @param aa Pointer to AuthAttrs
228 * @param digest Pointer to a buffer where the hash is written
230 static int hash_image(int fd, uint64_t length, const AuthAttrs *aa,
231 unsigned char *digest)
233 EVP_MD_CTX *ctx = NULL;
237 unsigned char *attrs = NULL;
238 unsigned char *buffer = NULL;
239 unsigned char *p = NULL;
242 if (!aa || !digest) {
246 if ((buffer = malloc(BUFFER_SIZE)) == NULL) {
250 if (lseek64(fd, 0, SEEK_SET) != 0) {
254 if ((ctx = EVP_MD_CTX_create()) == NULL) {
255 ERR_print_errors(g_error);
259 EVP_DigestInit(ctx, EVP_sha256());
264 if ((length - total) < BUFFER_SIZE) {
265 bytes = length - total;
268 if ((bytes = read(fd, buffer, bytes)) == -1) {
269 printf("%s\n", strerror(errno));
273 EVP_DigestUpdate(ctx, buffer, bytes);
275 } while (total < length);
277 if ((bytes = i2d_AuthAttrs((AuthAttrs *) aa, NULL)) < 0) {
278 ERR_print_errors(g_error);
282 if ((attrs = OPENSSL_malloc(bytes)) == NULL) {
283 ERR_print_errors(g_error);
289 if (i2d_AuthAttrs((AuthAttrs *) aa, &p) < 0) {
290 ERR_print_errors(g_error);
294 EVP_DigestUpdate(ctx, attrs, bytes);
295 EVP_DigestFinal(ctx, digest, NULL);
305 EVP_MD_CTX_destroy(ctx);
316 * Verifies the RSA signature
317 * @param fd File descriptor to the boot image
318 * @param length Length of the boot image without the signature block
319 * @param bs The boot signature block
321 static int verify_signature(int fd, uint64_t length, const BootSignature *bs)
324 EVP_PKEY *pkey = NULL;
326 unsigned char digest[SHA256_DIGEST_LENGTH];
332 if (hash_image(fd, length, bs->authenticatedAttributes, digest) == -1) {
336 if ((pkey = X509_get_pubkey(bs->certificate)) == NULL) {
337 ERR_print_errors(g_error);
341 if ((rsa = EVP_PKEY_get1_RSA(pkey)) == NULL) {
342 ERR_print_errors(g_error);
346 if (!RSA_verify(NID_sha256, digest, SHA256_DIGEST_LENGTH,
347 bs->signature->data, bs->signature->length, rsa)) {
348 ERR_print_errors(g_error);
367 * Given the file name of a signed boot image, verifies the signature
368 * @param image_file Name of the boot image file
370 static int verify(const char *image_file)
372 BootSignature *bs = NULL;
381 if ((fd = open(image_file, O_RDONLY | O_LARGEFILE)) == -1) {
385 if (get_signature_offset(fd, &offset) == -1) {
389 if (read_signature(fd, offset, &bs) == -1) {
393 if (validate_signature_block(bs, offset) == -1) {
397 if (verify_signature(fd, offset, bs) == -1) {
401 printf("Signature is VALID\n");
406 BootSignature_free(bs);
418 printf("Usage: verify_boot_signature <path-to-boot-image>\n");
421 int main(int argc, char *argv[])
428 /* BIO descriptor for logging OpenSSL errors to stderr */
429 if ((g_error = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE)) == NULL) {
430 printf("Failed to allocate a BIO handle for error output\n");
434 ERR_load_crypto_strings();
436 return verify(argv[1]);