1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
|
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.verity;
import java.io.File;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.lang.Math;
import java.lang.Process;
import java.lang.Runtime;
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.MessageDigest;
import java.security.PublicKey;
import java.security.Security;
import java.security.cert.X509Certificate;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.RSAPublicKeySpec;
import java.util.ArrayList;
import java.util.Arrays;
import javax.xml.bind.DatatypeConverter;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class VerityVerifier {
private ArrayList<Integer> hashBlocksLevel;
private byte[] hashTree;
private byte[] rootHash;
private byte[] salt;
private byte[] signature;
private byte[] table;
private File image;
private int blockSize;
private int hashBlockSize;
private int hashOffsetForData;
private int hashSize;
private int hashTreeSize;
private long hashStart;
private long imageSize;
private MessageDigest digest;
private static final int EXT4_SB_MAGIC = 0xEF53;
private static final int EXT4_SB_OFFSET = 0x400;
private static final int EXT4_SB_OFFSET_MAGIC = EXT4_SB_OFFSET + 0x38;
private static final int EXT4_SB_OFFSET_LOG_BLOCK_SIZE = EXT4_SB_OFFSET + 0x18;
private static final int EXT4_SB_OFFSET_BLOCKS_COUNT_LO = EXT4_SB_OFFSET + 0x4;
private static final int EXT4_SB_OFFSET_BLOCKS_COUNT_HI = EXT4_SB_OFFSET + 0x150;
private static final int MINCRYPT_OFFSET_MODULUS = 0x8;
private static final int MINCRYPT_OFFSET_EXPONENT = 0x208;
private static final int MINCRYPT_MODULUS_SIZE = 0x100;
private static final int MINCRYPT_EXPONENT_SIZE = 0x4;
private static final int VERITY_FIELDS = 10;
private static final int VERITY_MAGIC = 0xB001B001;
private static final int VERITY_SIGNATURE_SIZE = 256;
private static final int VERITY_VERSION = 0;
public VerityVerifier(String fname) throws Exception {
digest = MessageDigest.getInstance("SHA-256");
hashSize = digest.getDigestLength();
hashBlocksLevel = new ArrayList<Integer>();
hashTreeSize = -1;
openImage(fname);
readVerityData();
}
/**
* Reverses the order of bytes in a byte array
* @param value Byte array to reverse
*/
private static byte[] reverse(byte[] value) {
for (int i = 0; i < value.length / 2; i++) {
byte tmp = value[i];
value[i] = value[value.length - i - 1];
value[value.length - i - 1] = tmp;
}
return value;
}
/**
* Converts a 4-byte little endian value to a Java integer
* @param value Little endian integer to convert
*/
private static int fromle(int value) {
byte[] bytes = ByteBuffer.allocate(4).putInt(value).array();
return ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getInt();
}
/**
* Converts a 2-byte little endian value to Java a integer
* @param value Little endian short to convert
*/
private static int fromle(short value) {
return fromle(value << 16);
}
/**
* Reads a 2048-bit RSA public key saved in mincrypt format, and returns
* a Java PublicKey for it.
* @param fname Name of the mincrypt public key file
*/
private static PublicKey getMincryptPublicKey(String fname) throws Exception {
try (RandomAccessFile key = new RandomAccessFile(fname, "r")) {
byte[] binaryMod = new byte[MINCRYPT_MODULUS_SIZE];
byte[] binaryExp = new byte[MINCRYPT_EXPONENT_SIZE];
key.seek(MINCRYPT_OFFSET_MODULUS);
key.readFully(binaryMod);
key.seek(MINCRYPT_OFFSET_EXPONENT);
key.readFully(binaryExp);
BigInteger modulus = new BigInteger(1, reverse(binaryMod));
BigInteger exponent = new BigInteger(1, reverse(binaryExp));
RSAPublicKeySpec spec = new RSAPublicKeySpec(modulus, exponent);
KeyFactory factory = KeyFactory.getInstance("RSA");
return factory.generatePublic(spec);
}
}
/**
* Unsparses a sparse image into a temporary file and returns a
* handle to the file
* @param fname Path to a sparse image file
*/
private void openImage(String fname) throws Exception {
image = File.createTempFile("system", ".raw");
image.deleteOnExit();
Process p = Runtime.getRuntime().exec("simg2img " + fname +
" " + image.getAbsoluteFile());
p.waitFor();
if (p.exitValue() != 0) {
throw new IllegalArgumentException("Invalid image: failed to unsparse");
}
}
/**
* Reads the ext4 superblock and calculates the size of the system image,
* after which we should find the verity metadata
* @param img File handle to the image file
*/
public static long getMetadataPosition(RandomAccessFile img)
throws Exception {
img.seek(EXT4_SB_OFFSET_MAGIC);
int magic = fromle(img.readShort());
if (magic != EXT4_SB_MAGIC) {
throw new IllegalArgumentException("Invalid image: not a valid ext4 image");
}
img.seek(EXT4_SB_OFFSET_BLOCKS_COUNT_LO);
long blocksCountLo = fromle(img.readInt());
img.seek(EXT4_SB_OFFSET_LOG_BLOCK_SIZE);
long logBlockSize = fromle(img.readInt());
img.seek(EXT4_SB_OFFSET_BLOCKS_COUNT_HI);
long blocksCountHi = fromle(img.readInt());
long blockSizeBytes = 1L << (10 + logBlockSize);
long blockCount = (blocksCountHi << 32) + blocksCountLo;
return blockSizeBytes * blockCount;
}
/**
* Calculates the size of the verity hash tree based on the image size
*/
private int calculateHashTreeSize() {
if (hashTreeSize > 0) {
return hashTreeSize;
}
int totalBlocks = 0;
int hashes = (int) (imageSize / blockSize);
hashBlocksLevel.clear();
do {
hashBlocksLevel.add(0, hashes);
int hashBlocks =
(int) Math.ceil((double) hashes * hashSize / hashBlockSize);
totalBlocks += hashBlocks;
hashes = hashBlocks;
} while (hashes > 1);
hashTreeSize = totalBlocks * hashBlockSize;
return hashTreeSize;
}
/**
* Parses the verity mapping table and reads the hash tree from
* the image file
* @param img Handle to the image file
* @param table Verity mapping table
*/
private void readHashTree(RandomAccessFile img, byte[] table)
throws Exception {
String tableStr = new String(table);
String[] fields = tableStr.split(" ");
if (fields.length != VERITY_FIELDS) {
throw new IllegalArgumentException("Invalid image: unexpected number of fields "
+ "in verity mapping table (" + fields.length + ")");
}
String hashVersion = fields[0];
if (!"1".equals(hashVersion)) {
throw new IllegalArgumentException("Invalid image: unsupported hash format");
}
String alg = fields[7];
if (!"sha256".equals(alg)) {
throw new IllegalArgumentException("Invalid image: unsupported hash algorithm");
}
blockSize = Integer.parseInt(fields[3]);
hashBlockSize = Integer.parseInt(fields[4]);
int blocks = Integer.parseInt(fields[5]);
int start = Integer.parseInt(fields[6]);
if (imageSize != (long) blocks * blockSize) {
throw new IllegalArgumentException("Invalid image: size mismatch in mapping "
+ "table");
}
rootHash = DatatypeConverter.parseHexBinary(fields[8]);
salt = DatatypeConverter.parseHexBinary(fields[9]);
hashStart = (long) start * blockSize;
img.seek(hashStart);
int treeSize = calculateHashTreeSize();
hashTree = new byte[treeSize];
img.readFully(hashTree);
}
/**
* Reads verity data from the image file
*/
private void readVerityData() throws Exception {
try (RandomAccessFile img = new RandomAccessFile(image, "r")) {
imageSize = getMetadataPosition(img);
img.seek(imageSize);
int magic = fromle(img.readInt());
if (magic != VERITY_MAGIC) {
throw new IllegalArgumentException("Invalid image: verity metadata not found");
}
int version = fromle(img.readInt());
if (version != VERITY_VERSION) {
throw new IllegalArgumentException("Invalid image: unknown metadata version");
}
signature = new byte[VERITY_SIGNATURE_SIZE];
img.readFully(signature);
int tableSize = fromle(img.readInt());
table = new byte[tableSize];
img.readFully(table);
readHashTree(img, table);
}
}
/**
* Reads and validates verity metadata, and checks the signature against the
* given public key
* @param key Public key to use for signature verification
*/
public boolean verifyMetaData(PublicKey key)
throws Exception {
return Utils.verify(key, table, signature,
Utils.getSignatureAlgorithmIdentifier(key));
}
/**
* Hashes a block of data using a salt and checks of the results are expected
* @param hash The expected hash value
* @param data The data block to check
*/
private boolean checkBlock(byte[] hash, byte[] data) {
digest.reset();
digest.update(salt);
digest.update(data);
return Arrays.equals(hash, digest.digest());
}
/**
* Verifies the root hash and the first N-1 levels of the hash tree
*/
private boolean verifyHashTree() throws Exception {
int hashOffset = 0;
int dataOffset = hashBlockSize;
if (!checkBlock(rootHash, Arrays.copyOfRange(hashTree, 0, hashBlockSize))) {
System.err.println("Root hash mismatch");
return false;
}
for (int level = 0; level < hashBlocksLevel.size() - 1; level++) {
int blocks = hashBlocksLevel.get(level);
for (int i = 0; i < blocks; i++) {
byte[] hashBlock = Arrays.copyOfRange(hashTree,
hashOffset + i * hashSize,
hashOffset + i * hashSize + hashSize);
byte[] dataBlock = Arrays.copyOfRange(hashTree,
dataOffset + i * hashBlockSize,
dataOffset + i * hashBlockSize + hashBlockSize);
if (!checkBlock(hashBlock, dataBlock)) {
System.err.printf("Hash mismatch at tree level %d, block %d\n", level, i);
return false;
}
}
hashOffset = dataOffset;
hashOffsetForData = dataOffset;
dataOffset += blocks * hashBlockSize;
}
return true;
}
/**
* Validates the image against the hash tree
*/
public boolean verifyData() throws Exception {
if (!verifyHashTree()) {
return false;
}
try (RandomAccessFile img = new RandomAccessFile(image, "r")) {
byte[] dataBlock = new byte[blockSize];
int hashOffset = hashOffsetForData;
for (int i = 0; (long) i * blockSize < imageSize; i++) {
byte[] hashBlock = Arrays.copyOfRange(hashTree,
hashOffset + i * hashSize,
hashOffset + i * hashSize + hashSize);
img.readFully(dataBlock);
if (!checkBlock(hashBlock, dataBlock)) {
System.err.printf("Hash mismatch at block %d\n", i);
return false;
}
}
}
return true;
}
/**
* Verifies the integrity of the image and the verity metadata
* @param key Public key to use for signature verification
*/
public boolean verify(PublicKey key) throws Exception {
return (verifyMetaData(key) && verifyData());
}
public static void main(String[] args) throws Exception {
Security.addProvider(new BouncyCastleProvider());
PublicKey key = null;
if (args.length == 3 && "-mincrypt".equals(args[1])) {
key = getMincryptPublicKey(args[2]);
} else if (args.length == 2) {
X509Certificate cert = Utils.loadPEMCertificate(args[1]);
key = cert.getPublicKey();
} else {
System.err.println("Usage: VerityVerifier <sparse.img> <certificate.x509.pem> | -mincrypt <mincrypt_key>");
System.exit(1);
}
VerityVerifier verifier = new VerityVerifier(args[0]);
try {
if (verifier.verify(key)) {
System.err.println("Signature is VALID");
System.exit(0);
}
} catch (Exception e) {
e.printStackTrace(System.err);
}
System.exit(1);
}
}
|