CredentialType.java revision d8dddd9671750e6bfbcfa218db16ad096b9904ee
1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
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
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17package com.android.server.wifi.hotspot2.anqp.eap;
18
19import com.android.internal.annotations.VisibleForTesting;
20
21import java.net.ProtocolException;
22import java.nio.BufferUnderflowException;
23import java.nio.ByteBuffer;
24
25/**
26 * The Credential Type authentication parameter, IEEE802.11-2012, table 8-188.
27 * Used by both Credential Type and Tunneled EAP Method Credential Type authentication
28 * parameter.
29 *
30 * Format:
31 * | Type |
32 *    1
33 */
34public class CredentialType extends AuthParam {
35    public static final int CREDENTIAL_TYPE_SIM = 1;
36    public static final int CREDENTIAL_TYPE_USIM = 2;
37    public static final int CREDENTIAL_TYPE_NFC = 3;
38    public static final int CREDENTIAL_TYPE_HARDWARE_TOKEN = 4;
39    public static final int CREDENTIAL_TYPE_SOFTWARE_TOKEN = 5;
40    public static final int CREDENTIAL_TYPE_CERTIFICATE = 6;
41    public static final int CREDENTIAL_TYPE_USERNAME_PASSWORD = 7;
42    public static final int CREDENTIAL_TYPE_NONE = 8;
43    public static final int CREDENTIAL_TYPE_ANONYMOUS = 9;
44    public static final int CREDENTIAL_TYPE_VENDOR_SPECIFIC = 10;
45
46    @VisibleForTesting
47    public static final int EXPECTED_LENGTH_VALUE = 1;
48
49    private final int mType;
50
51    @VisibleForTesting
52    public CredentialType(int authType, int credType) {
53        super(authType);
54        mType = credType;
55    }
56
57    /**
58     * Parse a CredentialType from the given buffer.
59     *
60     * @param payload The byte buffer to read from
61     * @param length The length of the data
62     * @param tunneled Flag indicating if this is for a Tunneled EAP Method
63     * @return {@link CredentialType}
64     * @throws ProtocolException
65     * @throws BufferUnderflowException
66     */
67    public static CredentialType parse(ByteBuffer payload, int length, boolean tunneled)
68            throws ProtocolException {
69        if (length != EXPECTED_LENGTH_VALUE) {
70            throw new ProtocolException("Invalid length: " + length);
71        }
72        int credType = payload.get() & 0xFF;
73        int authType = tunneled ? AuthParam.PARAM_TYPE_TUNNELED_EAP_METHOD_CREDENTIAL_TYPE
74                : AuthParam.PARAM_TYPE_CREDENTIAL_TYPE;
75        return new CredentialType(authType, credType);
76    }
77
78    public int getType() {
79        return mType;
80    }
81
82    @Override
83    public boolean equals(Object thatObject) {
84        if (thatObject == this) {
85            return true;
86        }
87        if (!(thatObject instanceof CredentialType)) {
88            return false;
89        }
90        CredentialType that = (CredentialType) thatObject;
91        return mType == that.mType;
92    }
93
94    @Override
95    public int hashCode() {
96        return mType;
97    }
98
99    @Override
100    public String toString() {
101        return "CredentialType{mType=" + mType + "}";
102    }
103}
104