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;
20import com.android.server.wifi.ByteBufferReader;
21
22import java.net.ProtocolException;
23import java.nio.BufferUnderflowException;
24import java.nio.ByteBuffer;
25import java.nio.ByteOrder;
26
27/**
28 * The Expanded EAP Method authentication parameter, IEEE802.11-2012, table 8-189.
29 * Used by both Expanded EAP Method and Expanded Inner EAP Method.
30 *
31 * Format:
32 * | Vendor ID | Vendor Type |
33 *       3            4
34 */
35public class ExpandedEAPMethod extends AuthParam {
36    public static final int EXPECTED_LENGTH_VALUE = 7;
37
38    private final int mVendorID;
39    private final long mVendorType;
40
41    @VisibleForTesting
42    public ExpandedEAPMethod(int authType, int vendorID, long vendorType) {
43        super(authType);
44        mVendorID = vendorID;
45        mVendorType = vendorType;
46    }
47
48    /**
49     * Parse a ExpandedEAPMethod from the given buffer.
50     *
51     * @param payload The byte buffer to read from
52     * @param length The length of the data
53     * @param inner Flag indicating if this is for an Inner EAP method
54     * @return {@link ExpandedEAPMethod}
55     * @throws ProtocolException
56     * @throws BufferUnderflowException
57     */
58    public static ExpandedEAPMethod parse(ByteBuffer payload, int length, boolean inner)
59            throws ProtocolException {
60        if (length != EXPECTED_LENGTH_VALUE) {
61            throw new ProtocolException("Invalid length value: " + length);
62        }
63
64        // Vendor ID and Vendor Type are expressed in big-endian byte order according to
65        // the spec.
66        int vendorID = (int) ByteBufferReader.readInteger(payload, ByteOrder.BIG_ENDIAN, 3)
67                & 0xFFFFFF;
68        long vendorType = ByteBufferReader.readInteger(payload, ByteOrder.BIG_ENDIAN, 4)
69                & 0xFFFFFFFF;
70
71        int authType = inner ? AuthParam.PARAM_TYPE_EXPANDED_INNER_EAP_METHOD
72                : AuthParam.PARAM_TYPE_EXPANDED_EAP_METHOD;
73        return new ExpandedEAPMethod(authType, vendorID, vendorType);
74    }
75
76    public int getVendorID() {
77        return mVendorID;
78    }
79
80    public long getVendorType() {
81        return mVendorType;
82    }
83
84    @Override
85    public boolean equals(Object thatObject) {
86        if (thatObject == this) {
87            return true;
88        }
89        if (!(thatObject instanceof ExpandedEAPMethod)) {
90            return false;
91        }
92        ExpandedEAPMethod that = (ExpandedEAPMethod) thatObject;
93        return mVendorID == that.mVendorID && mVendorType == that.mVendorType;
94    }
95
96    @Override
97    public int hashCode() {
98        return (mVendorID) * 31 + (int) mVendorType;
99    }
100
101    @Override
102    public String toString() {
103        return "ExpandedEAPMethod{mVendorID=" + mVendorID + " mVendorType=" + mVendorType + "}";
104    }
105}
106