| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.internal.security; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.os.Build; |
| 22 |
import android.os.SystemProperties; |
| 23 |
import android.system.Os; |
| 24 |
import android.system.OsConstants; |
| 25 |
import android.util.Slog; |
| 26 |
|
| 27 |
import com.android.internal.annotations.VisibleForTesting; |
| 28 |
import com.android.internal.org.bouncycastle.asn1.nist.NISTObjectIdentifiers; |
| 29 |
import com.android.internal.org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers; |
| 30 |
import com.android.internal.org.bouncycastle.cms.CMSException; |
| 31 |
import com.android.internal.org.bouncycastle.cms.CMSProcessableByteArray; |
| 32 |
import com.android.internal.org.bouncycastle.cms.CMSSignedData; |
| 33 |
import com.android.internal.org.bouncycastle.cms.SignerInformation; |
| 34 |
import com.android.internal.org.bouncycastle.cms.SignerInformationVerifier; |
| 35 |
import com.android.internal.org.bouncycastle.cms.jcajce.JcaSimpleSignerInfoVerifierBuilder; |
| 36 |
import com.android.internal.org.bouncycastle.operator.OperatorCreationException; |
| 37 |
|
| 38 |
import java.io.File; |
| 39 |
import java.io.IOException; |
| 40 |
import java.io.InputStream; |
| 41 |
import java.nio.ByteBuffer; |
| 42 |
import java.nio.ByteOrder; |
| 43 |
import java.nio.charset.StandardCharsets; |
| 44 |
import java.nio.file.Files; |
| 45 |
import java.nio.file.Path; |
| 46 |
import java.nio.file.Paths; |
| 47 |
import java.security.cert.CertificateException; |
| 48 |
import java.security.cert.CertificateFactory; |
| 49 |
import java.security.cert.X509Certificate; |
| 50 |
|
| 51 |
|
| 52 |
public abstract class VerityUtils { |
| 53 |
private static final String TAG = "VerityUtils"; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public static final String FSVERITY_SIGNATURE_FILE_EXTENSION = ".fsv_sig"; |
| 60 |
|
| 61 |
|
| 62 |
private static final int MAX_SIGNATURE_FILE_SIZE_BYTES = 8192; |
| 63 |
|
| 64 |
|
| 65 |
private static final int HASH_SIZE_BYTES = 32; |
| 66 |
|
| 67 |
public static boolean isFsVeritySupported() { |
| 68 |
return Build.VERSION.DEVICE_INITIAL_SDK_INT >= Build.VERSION_CODES.R |
| 69 |
|| SystemProperties.getInt("ro.apk_verity.mode", 0) == 2; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
public static boolean isFsveritySignatureFile(File file) { |
| 74 |
return file.getName().endsWith(FSVERITY_SIGNATURE_FILE_EXTENSION); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
public static String getFsveritySignatureFilePath(String filePath) { |
| 79 |
return filePath + FSVERITY_SIGNATURE_FILE_EXTENSION; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
public static void setUpFsverity(@NonNull String filePath, @Nullable String signaturePath) |
| 84 |
throws IOException { |
| 85 |
byte[] rawSignature = null; |
| 86 |
if (signaturePath != null) { |
| 87 |
Path path = Paths.get(signaturePath); |
| 88 |
if (Files.size(path) > MAX_SIGNATURE_FILE_SIZE_BYTES) { |
| 89 |
throw new SecurityException("Signature file is unexpectedly large: " |
| 90 |
+ signaturePath); |
| 91 |
} |
| 92 |
rawSignature = Files.readAllBytes(path); |
| 93 |
} |
| 94 |
setUpFsverity(filePath, rawSignature); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
public static void setUpFsverity(@NonNull String filePath, @Nullable byte[] pkcs7Signature) |
| 99 |
throws IOException { |
| 100 |
|
| 101 |
int errno = enableFsverityNative(filePath, pkcs7Signature); |
| 102 |
if (errno != 0) { |
| 103 |
throw new IOException("Failed to enable fs-verity on " + filePath + ": " |
| 104 |
+ Os.strerror(errno)); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
public static boolean hasFsverity(@NonNull String filePath) { |
| 110 |
int retval = statxForFsverityNative(filePath); |
| 111 |
if (retval < 0) { |
| 112 |
Slog.e(TAG, "Failed to check whether fs-verity is enabled, errno " + -retval + ": " |
| 113 |
+ filePath); |
| 114 |
return false; |
| 115 |
} |
| 116 |
return (retval == 1); |
| 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 |
public static boolean verifyPkcs7DetachedSignature(@NonNull byte[] signatureBlock, |
| 142 |
@NonNull byte[] digest, @NonNull InputStream derCertInputStream) { |
| 143 |
if (digest.length != 32) { |
| 144 |
Slog.w(TAG, "Only sha256 is currently supported"); |
| 145 |
return false; |
| 146 |
} |
| 147 |
|
| 148 |
try { |
| 149 |
CMSSignedData signedData = new CMSSignedData( |
| 150 |
new CMSProcessableByteArray(toFormattedDigest(digest)), |
| 151 |
signatureBlock); |
| 152 |
|
| 153 |
if (!signedData.isDetachedSignature()) { |
| 154 |
Slog.w(TAG, "Expect only detached siganture"); |
| 155 |
return false; |
| 156 |
} |
| 157 |
if (!signedData.getCertificates().getMatches(null).isEmpty()) { |
| 158 |
Slog.w(TAG, "Expect no certificate in signature"); |
| 159 |
return false; |
| 160 |
} |
| 161 |
if (!signedData.getCRLs().getMatches(null).isEmpty()) { |
| 162 |
Slog.w(TAG, "Expect no CRL in signature"); |
| 163 |
return false; |
| 164 |
} |
| 165 |
|
| 166 |
X509Certificate trustedCert = (X509Certificate) CertificateFactory.getInstance("X.509") |
| 167 |
.generateCertificate(derCertInputStream); |
| 168 |
SignerInformationVerifier verifier = new JcaSimpleSignerInfoVerifierBuilder() |
| 169 |
.build(trustedCert); |
| 170 |
|
| 171 |
|
| 172 |
for (SignerInformation si : signedData.getSignerInfos().getSigners()) { |
| 173 |
|
| 174 |
|
| 175 |
if (si.getSignedAttributes() != null && si.getSignedAttributes().size() > 0) { |
| 176 |
Slog.w(TAG, "Unexpected signed attributes"); |
| 177 |
return false; |
| 178 |
} |
| 179 |
if (si.getUnsignedAttributes() != null && si.getUnsignedAttributes().size() > 0) { |
| 180 |
Slog.w(TAG, "Unexpected unsigned attributes"); |
| 181 |
return false; |
| 182 |
} |
| 183 |
if (!NISTObjectIdentifiers.id_sha256.getId().equals(si.getDigestAlgOID())) { |
| 184 |
Slog.w(TAG, "Unsupported digest algorithm OID: " + si.getDigestAlgOID()); |
| 185 |
return false; |
| 186 |
} |
| 187 |
if (!PKCSObjectIdentifiers.rsaEncryption.getId().equals(si.getEncryptionAlgOID())) { |
| 188 |
Slog.w(TAG, "Unsupported encryption algorithm OID: " |
| 189 |
+ si.getEncryptionAlgOID()); |
| 190 |
return false; |
| 191 |
} |
| 192 |
|
| 193 |
if (si.verify(verifier)) { |
| 194 |
return true; |
| 195 |
} |
| 196 |
} |
| 197 |
return false; |
| 198 |
} catch (CertificateException | CMSException | OperatorCreationException e) { |
| 199 |
Slog.w(TAG, "Error occurred during the PKCS#7 signature verification", e); |
| 200 |
} |
| 201 |
return false; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
public static byte[] getFsverityDigest(@NonNull String filePath) { |
| 213 |
byte[] result = new byte[HASH_SIZE_BYTES]; |
| 214 |
int retval = measureFsverityNative(filePath, result); |
| 215 |
if (retval < 0) { |
| 216 |
if (retval != -OsConstants.ENODATA) { |
| 217 |
Slog.e(TAG, "Failed to measure fs-verity, errno " + -retval + ": " + filePath); |
| 218 |
} |
| 219 |
return null; |
| 220 |
} |
| 221 |
return result; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
@VisibleForTesting |
| 226 |
public static byte[] toFormattedDigest(byte[] digest) { |
| 227 |
|
| 228 |
ByteBuffer buffer = ByteBuffer.allocate(12 + digest.length); |
| 229 |
buffer.order(ByteOrder.LITTLE_ENDIAN); |
| 230 |
buffer.put("FSVerity".getBytes(StandardCharsets.US_ASCII)); |
| 231 |
buffer.putShort((short) 1); |
| 232 |
buffer.putShort((short) digest.length); |
| 233 |
buffer.put(digest); |
| 234 |
return buffer.array(); |
| 235 |
} |
| 236 |
|
| 237 |
private static native int enableFsverityNative(@NonNull String filePath, |
| 238 |
@Nullable byte[] pkcs7Signature); |
| 239 |
private static native int measureFsverityNative(@NonNull String filePath, |
| 240 |
@NonNull byte[] digest); |
| 241 |
private static native int statxForFsverityNative(@NonNull String filePath); |
| 242 |
} |