|
1 | 1 | package com.yubico.u2f.data.messages;
|
2 | 2 |
|
| 3 | +import com.google.common.base.Optional; |
| 4 | +import com.google.common.collect.ImmutableSet; |
| 5 | +import com.google.gson.JsonElement; |
| 6 | +import com.google.gson.JsonObject; |
| 7 | +import com.google.gson.JsonParser; |
3 | 8 | import com.yubico.u2f.U2fException;
|
4 |
| -import com.yubico.u2f.ClientDataUtils; |
5 | 9 | import org.apache.commons.codec.binary.Base64;
|
6 | 10 |
|
| 11 | +import java.net.URI; |
| 12 | +import java.net.URISyntaxException; |
| 13 | +import java.util.Set; |
| 14 | + |
7 | 15 | import static com.google.common.base.Preconditions.checkNotNull;
|
8 | 16 |
|
9 | 17 | public class ClientData {
|
10 | 18 |
|
11 |
| - private final String clientData; |
| 19 | + private static final String TYPE_PARAM = "typ"; |
| 20 | + private static final String CHALLENGE_PARAM = "challenge"; |
| 21 | + private static final String ORIGIN_PARAM = "origin"; |
| 22 | + |
| 23 | + private final String type; |
| 24 | + private final String challenge; |
| 25 | + private final String origin; |
| 26 | + private final byte[] rawClientData; |
| 27 | + |
| 28 | + public byte[] getRawClientData() { |
| 29 | + return rawClientData; |
| 30 | + } |
| 31 | + |
| 32 | + public ClientData(String rawClientData) throws U2fException { |
| 33 | + this(rawClientData.getBytes()); |
| 34 | + } |
| 35 | + |
| 36 | + public ClientData(byte[] clientData) throws U2fException { |
12 | 37 |
|
13 |
| - public ClientData(String clientData) { |
14 |
| - this.clientData = checkNotNull(clientData); |
| 38 | + this.rawClientData = Base64.decodeBase64(clientData); |
| 39 | + JsonElement clientDataAsElement = new JsonParser().parse(new String(rawClientData)); |
| 40 | + if (!clientDataAsElement.isJsonObject()) { |
| 41 | + throw new U2fException("ClientData has wrong format"); |
| 42 | + } |
| 43 | + JsonObject jsonObject = clientDataAsElement.getAsJsonObject(); |
| 44 | + if (!jsonObject.has(TYPE_PARAM)) { |
| 45 | + throw new U2fException("Bad clientData: missing 'typ' param"); |
| 46 | + } |
| 47 | + this.type = jsonObject.get(TYPE_PARAM).getAsString(); |
| 48 | + if (!jsonObject.has(CHALLENGE_PARAM)) { |
| 49 | + throw new U2fException("Bad clientData: missing 'challenge' param"); |
| 50 | + } |
| 51 | + this.challenge = jsonObject.get(CHALLENGE_PARAM).getAsString(); |
| 52 | + this.origin = checkNotNull(jsonObject.get(ORIGIN_PARAM).getAsString()); |
15 | 53 | }
|
16 | 54 |
|
17 | 55 | @Override
|
18 | 56 | public String toString() {
|
19 |
| - return clientData; |
| 57 | + return new String(rawClientData); |
| 58 | + } |
| 59 | + |
| 60 | + public String getChallenge() { |
| 61 | + return challenge; |
| 62 | + } |
| 63 | + |
| 64 | + public void checkContent(String type, String challenge, Optional<Set<String>> facets) throws U2fException { |
| 65 | + if (!type.equals(this.type)) { |
| 66 | + throw new U2fException("Bad clientData: bad type " + this.type); |
| 67 | + } |
| 68 | + if (!challenge.equals(this.challenge)) { |
| 69 | + throw new U2fException("Wrong challenge signed in clientData"); |
| 70 | + } |
| 71 | + if(facets.isPresent()) { |
| 72 | + verifyOrigin(origin, canonicalizeOrigins(facets.get())); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private static void verifyOrigin(String origin, Set<String> allowedOrigins) throws U2fException { |
| 77 | + if (!allowedOrigins.contains(canonicalizeOrigin(origin))) { |
| 78 | + throw new U2fException(origin + |
| 79 | + " is not a recognized home origin for this backend"); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public static Set<String> canonicalizeOrigins(Set<String> origins) { |
| 84 | + ImmutableSet.Builder<String> result = ImmutableSet.builder(); |
| 85 | + for (String origin : origins) { |
| 86 | + result.add(canonicalizeOrigin(origin)); |
| 87 | + } |
| 88 | + return result.build(); |
20 | 89 | }
|
21 | 90 |
|
22 |
| - public String getChallenge() throws U2fException { |
23 |
| - return ClientDataUtils.toJsonObject(Base64.decodeBase64(clientData.getBytes())) |
24 |
| - .get(ClientDataUtils.CHALLENGE_PARAM).getAsString(); |
| 91 | + public static String canonicalizeOrigin(String url) { |
| 92 | + try { |
| 93 | + URI uri = new URI(url); |
| 94 | + return uri.getScheme() + "://" + uri.getAuthority(); |
| 95 | + } catch (URISyntaxException e) { |
| 96 | + throw new AssertionError("specified bad origin", e); |
| 97 | + } |
25 | 98 | }
|
26 | 99 | }
|
0 commit comments