|
| 1 | +package com.yubico.webauthn; |
| 2 | + |
| 3 | +import javax.net.ssl.SSLException; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.databind.JsonNode; |
| 6 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 7 | +import com.fasterxml.jackson.databind.node.ArrayNode; |
| 8 | +import com.fasterxml.jackson.databind.node.JsonNodeFactory; |
| 9 | +import com.yubico.internal.util.CertificateParser; |
| 10 | +import com.yubico.internal.util.ExceptionUtil; |
| 11 | +import com.yubico.internal.util.WebAuthnCodecs; |
| 12 | +import com.yubico.webauthn.data.AttestationObject; |
| 13 | +import com.yubico.webauthn.data.AttestationType; |
| 14 | +import com.yubico.webauthn.data.ByteArray; |
| 15 | +import com.yubico.webauthn.data.exception.Base64UrlException; |
| 16 | +import java.io.IOException; |
| 17 | +import java.nio.charset.StandardCharsets; |
| 18 | +import java.security.InvalidKeyException; |
| 19 | +import java.security.NoSuchAlgorithmException; |
| 20 | +import java.security.Signature; |
| 21 | +import java.security.SignatureException; |
| 22 | +import java.security.cert.CertificateException; |
| 23 | +import java.security.cert.X509Certificate; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.List; |
| 26 | +import lombok.Value; |
| 27 | +import lombok.extern.slf4j.Slf4j; |
| 28 | +import org.apache.http.conn.ssl.DefaultHostnameVerifier; |
| 29 | + |
| 30 | +@Slf4j |
| 31 | +class AndroidSafetynetAttestationStatementVerifier implements AttestationStatementVerifier, X5cAttestationStatementVerifier { |
| 32 | + |
| 33 | + private final BouncyCastleCrypto crypto = new BouncyCastleCrypto(); |
| 34 | + |
| 35 | + private static final DefaultHostnameVerifier HOSTNAME_VERIFIER = new DefaultHostnameVerifier(); |
| 36 | + |
| 37 | + @Override |
| 38 | + public AttestationType getAttestationType(AttestationObject attestation) { |
| 39 | + return AttestationType.BASIC; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public JsonNode getX5cArray(AttestationObject attestationObject) { |
| 44 | + JsonNodeFactory jsonFactory = JsonNodeFactory.instance; |
| 45 | + ArrayNode array = jsonFactory.arrayNode(); |
| 46 | + for (JsonNode cert : parseJws(attestationObject).getHeader().get("x5c")) { |
| 47 | + array.add(jsonFactory.binaryNode(ByteArray.fromBase64(cert.textValue()).getBytes())); |
| 48 | + } |
| 49 | + return array; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public boolean verifyAttestationSignature(AttestationObject attestationObject, ByteArray clientDataJsonHash) { |
| 54 | + final JsonNode ver = attestationObject.getAttestationStatement().get("ver"); |
| 55 | + |
| 56 | + if (ver == null || !ver.isTextual()) { |
| 57 | + throw new IllegalArgumentException("Property \"ver\" of android-safetynet attestation statement must be a string, was: " + ver); |
| 58 | + } |
| 59 | + |
| 60 | + JsonWebSignatureCustom jws = parseJws(attestationObject); |
| 61 | + |
| 62 | + if (!verifySignature(jws)) { |
| 63 | + return false; |
| 64 | + } |
| 65 | + |
| 66 | + JsonNode payload = jws.getPayload(); |
| 67 | + |
| 68 | + ByteArray signedData = attestationObject.getAuthenticatorData().getBytes().concat(clientDataJsonHash); |
| 69 | + ByteArray hashSignedData = crypto.hash(signedData); |
| 70 | + ByteArray nonceByteArray = ByteArray.fromBase64(payload.get("nonce").textValue()); |
| 71 | + ExceptionUtil.assure( |
| 72 | + hashSignedData.equals(nonceByteArray), |
| 73 | + "Nonce does not equal authenticator data + client data. Expected nonce: %s, was nonce: %s", |
| 74 | + hashSignedData.getBase64Url(), |
| 75 | + nonceByteArray.getBase64Url() |
| 76 | + ); |
| 77 | + |
| 78 | + ExceptionUtil.assure( |
| 79 | + payload.get("ctsProfileMatch").booleanValue(), |
| 80 | + "Expected ctsProfileMatch to be true, was: %s", |
| 81 | + payload.get("ctsProfileMatch") |
| 82 | + ); |
| 83 | + |
| 84 | + return true; |
| 85 | + } |
| 86 | + |
| 87 | + private static JsonWebSignatureCustom parseJws(AttestationObject attestationObject) { |
| 88 | + return new JsonWebSignatureCustom(new String(getResponseBytes(attestationObject).getBytes(), StandardCharsets.UTF_8)); |
| 89 | + } |
| 90 | + |
| 91 | + private static ByteArray getResponseBytes(AttestationObject attestationObject) { |
| 92 | + final JsonNode response = attestationObject.getAttestationStatement().get("response"); |
| 93 | + if (response == null || !response.isBinary()) { |
| 94 | + throw new IllegalArgumentException("Property \"response\" of android-safetynet attestation statement must be a binary value, was: " + response); |
| 95 | + } |
| 96 | + |
| 97 | + try { |
| 98 | + return new ByteArray(response.binaryValue()); |
| 99 | + } catch (IOException ioe) { |
| 100 | + throw ExceptionUtil.wrapAndLog(log, "response.isBinary() was true but response.binaryValue failed: " + response, ioe); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private boolean verifySignature(JsonWebSignatureCustom jws) { |
| 105 | + // Verify the signature of the JWS and retrieve the signature certificate. |
| 106 | + X509Certificate attestationCertificate = jws.getX5c().get(0); |
| 107 | + |
| 108 | + String signatureAlgorithmName = WebAuthnCodecs.jwsAlgorithmNameToJavaAlgorithmName(jws.getAlgorithm()); |
| 109 | + |
| 110 | + Signature signatureVerifier; |
| 111 | + try { |
| 112 | + signatureVerifier = Signature.getInstance(signatureAlgorithmName, crypto.getProvider()); |
| 113 | + } catch (NoSuchAlgorithmException e) { |
| 114 | + throw ExceptionUtil.wrapAndLog(log, "Failed to get a Signature instance for " + signatureAlgorithmName, e); |
| 115 | + } |
| 116 | + try { |
| 117 | + signatureVerifier.initVerify(attestationCertificate.getPublicKey()); |
| 118 | + } catch (InvalidKeyException e) { |
| 119 | + throw ExceptionUtil.wrapAndLog(log, "Attestation key is invalid: " + attestationCertificate, e); |
| 120 | + } |
| 121 | + try { |
| 122 | + signatureVerifier.update(jws.getSignedBytes().getBytes()); |
| 123 | + } catch (SignatureException e) { |
| 124 | + throw ExceptionUtil.wrapAndLog(log, "Signature object in invalid state: " + signatureVerifier, e); |
| 125 | + } |
| 126 | + |
| 127 | + // Verify the hostname of the certificate. |
| 128 | + ExceptionUtil.assure( |
| 129 | + verifyHostname(attestationCertificate), |
| 130 | + "Certificate isn't issued for the hostname attest.android.com: %s", |
| 131 | + attestationCertificate |
| 132 | + ); |
| 133 | + |
| 134 | + try { |
| 135 | + return signatureVerifier.verify(jws.getSignature().getBytes()); |
| 136 | + } catch (SignatureException e) { |
| 137 | + throw ExceptionUtil.wrapAndLog(log, "Failed to verify signature of JWS: " + jws, e); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + @Value |
| 142 | + private static class JsonWebSignatureCustom { |
| 143 | + public final JsonNode header; |
| 144 | + public final JsonNode payload; |
| 145 | + public final ByteArray signedBytes; |
| 146 | + public final ByteArray signature; |
| 147 | + public final List<X509Certificate> x5c; |
| 148 | + public final String algorithm; |
| 149 | + |
| 150 | + JsonWebSignatureCustom(String jwsCompact) { |
| 151 | + String[] parts = jwsCompact.split("\\."); |
| 152 | + ObjectMapper json = WebAuthnCodecs.json(); |
| 153 | + |
| 154 | + try { |
| 155 | + final ByteArray header = ByteArray.fromBase64Url(parts[0]); |
| 156 | + final ByteArray payload = ByteArray.fromBase64Url(parts[1]); |
| 157 | + |
| 158 | + this.header = json.readTree(header.getBytes()); |
| 159 | + this.payload = json.readTree(payload.getBytes()); |
| 160 | + this.signedBytes = new ByteArray((parts[0] + "." + parts[1]).getBytes(StandardCharsets.UTF_8)); |
| 161 | + this.signature = ByteArray.fromBase64Url(parts[2]); |
| 162 | + this.x5c = getX5c(this.header); |
| 163 | + this.algorithm = this.header.get("alg").textValue(); |
| 164 | + } catch (IOException | Base64UrlException e) { |
| 165 | + throw ExceptionUtil.wrapAndLog(log, "Failed to parse JWS: " + jwsCompact, e); |
| 166 | + } catch (CertificateException e) { |
| 167 | + throw ExceptionUtil.wrapAndLog(log, "Failed to parse attestation certificates in JWS header: " + jwsCompact, e); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + private static List<X509Certificate> getX5c(JsonNode header) throws IOException, CertificateException { |
| 172 | + List<X509Certificate> result = new ArrayList<>(); |
| 173 | + for (JsonNode jsonNode : header.get("x5c")) { |
| 174 | + result.add(CertificateParser.parseDer(jsonNode.binaryValue())); |
| 175 | + } |
| 176 | + return result; |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Verifies that the certificate matches the hostname "attest.android.com". |
| 182 | + */ |
| 183 | + private static boolean verifyHostname(X509Certificate leafCert) { |
| 184 | + try { |
| 185 | + HOSTNAME_VERIFIER.verify("attest.android.com", leafCert); |
| 186 | + return true; |
| 187 | + } catch (SSLException e) { |
| 188 | + return false; |
| 189 | + } |
| 190 | + } |
| 191 | +} |
0 commit comments