1 /*
2  * Copyright (C) 2019 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 
17 package com.android.internal.net.eap.message;
18 
19 import android.annotation.IntDef;
20 import android.annotation.NonNull;
21 
22 import java.lang.annotation.Retention;
23 import java.lang.annotation.RetentionPolicy;
24 import java.nio.ByteBuffer;
25 import java.util.Arrays;
26 import java.util.HashMap;
27 import java.util.HashSet;
28 import java.util.Map;
29 import java.util.Objects;
30 import java.util.Set;
31 
32 /**
33  * EapData represents the data-bytes of an EAP-Packet.
34  *
35  * <p>EapData objects will always have a Type-value and the Type-Data bytes that follow.
36  *
37  * EapData objects should be parsed from the Type and Type-Data sections of an EAP Packet, as shown
38  * below:
39  *
40  * +-----------------+-----------------+----------------------------------+
41  * |   Code (1B)     | Identifier (1B) |           Length (2B)            |
42  * +-----------------+-----------------+----------------------------------+
43  * |    Type (1B)    |  Type-Data ...
44  * +-----------------+-----
45  *
46  * @see <a href="https://tools.ietf.org/html/rfc3748#section-4">RFC 3748, Extensible Authentication
47  * Protocol (EAP)</a>
48  */
49 public class EapData {
50     @Retention(RetentionPolicy.SOURCE)
51     @IntDef({
52         EAP_IDENTITY,
53         EAP_NOTIFICATION,
54         EAP_NAK,
55         EAP_TYPE_SIM,
56         EAP_TYPE_AKA,
57         EAP_TYPE_MSCHAP_V2,
58         EAP_TYPE_AKA_PRIME
59     })
60     public @interface EapType {}
61 
62     @Retention(RetentionPolicy.SOURCE)
63     @IntDef({
64             EAP_TYPE_SIM,
65             EAP_TYPE_AKA,
66             EAP_TYPE_MSCHAP_V2,
67             EAP_TYPE_AKA_PRIME
68     })
69     public @interface EapMethod {}
70 
71     // EAP Type values defined by IANA
72     // https://www.iana.org/assignments/eap-numbers/eap-numbers.xhtml
73     public static final int EAP_IDENTITY = 1;
74     public static final int EAP_NOTIFICATION = 2;
75     public static final int EAP_NAK = 3;
76     // EAP_MD5_CHALLENGE unsupported, allowable based on RFC 3748, Section 5.4
77     public static final int EAP_TYPE_SIM = 18;
78     public static final int EAP_TYPE_AKA = 23;
79     public static final int EAP_TYPE_MSCHAP_V2 = 26;
80     public static final int EAP_TYPE_AKA_PRIME = 50;
81 
82     public static final Map<Integer, String> EAP_TYPE_STRING = new HashMap<>();
83     static {
EAP_TYPE_STRING.put(EAP_IDENTITY, "Identity")84         EAP_TYPE_STRING.put(EAP_IDENTITY, "Identity");
EAP_TYPE_STRING.put(EAP_NOTIFICATION, "Notification")85         EAP_TYPE_STRING.put(EAP_NOTIFICATION, "Notification");
EAP_TYPE_STRING.put(EAP_NAK, "Nak")86         EAP_TYPE_STRING.put(EAP_NAK, "Nak");
EAP_TYPE_STRING.put(EAP_TYPE_SIM, "EAP-SIM")87         EAP_TYPE_STRING.put(EAP_TYPE_SIM, "EAP-SIM");
EAP_TYPE_STRING.put(EAP_TYPE_AKA, "EAP-AKA")88         EAP_TYPE_STRING.put(EAP_TYPE_AKA, "EAP-AKA");
EAP_TYPE_STRING.put(EAP_TYPE_MSCHAP_V2, "EAP-MSCHAP-V2")89         EAP_TYPE_STRING.put(EAP_TYPE_MSCHAP_V2, "EAP-MSCHAP-V2");
EAP_TYPE_STRING.put(EAP_TYPE_AKA_PRIME, "EAP-AKA-PRIME")90         EAP_TYPE_STRING.put(EAP_TYPE_AKA_PRIME, "EAP-AKA-PRIME");
91     }
92 
93     private static final Set<Integer> SUPPORTED_TYPES = new HashSet<>();
94     static {
95         SUPPORTED_TYPES.add(EAP_IDENTITY);
96         SUPPORTED_TYPES.add(EAP_NOTIFICATION);
97         SUPPORTED_TYPES.add(EAP_NAK);
98 
99         // supported EAP Method types
100         SUPPORTED_TYPES.add(EAP_TYPE_SIM);
101         SUPPORTED_TYPES.add(EAP_TYPE_AKA);
102         SUPPORTED_TYPES.add(EAP_TYPE_MSCHAP_V2);
103         SUPPORTED_TYPES.add(EAP_TYPE_AKA_PRIME);
104     }
105 
106     @EapType public final int eapType;
107     public final byte[] eapTypeData;
108 
109     public static final EapData NOTIFICATION_DATA = new EapData(EAP_NOTIFICATION, new byte[0]);
110 
111     /**
112      * Constructs a new EapData instance.
113      *
114      * @param eapType the {@link EapType} for this EapData instance
115      * @param eapTypeData the Type-Data for this EapData instance
116      * @throws IllegalArgumentException if eapTypeData is null or if
117      *         {@link EapData#isSupportedEapType} is false for the given eapType
118      */
EapData(@apType int eapType, @NonNull byte[] eapTypeData)119     public EapData(@EapType int eapType, @NonNull byte[] eapTypeData) {
120         this.eapType = eapType;
121         this.eapTypeData = eapTypeData;
122 
123         validate();
124     }
125 
126     /**
127      * Gets the length of this EapData object.
128      *
129      * @return int for the number of bytes this EapData object represents
130      */
getLength()131     public int getLength() {
132         // length of byte-array + 1 (for 1B type field)
133         return eapTypeData.length + 1;
134     }
135 
136     /**
137      * Returns whether this instance is equal to the given Object o.
138      *
139      * @param o the Object to be tested for equality
140      * @return true iff this instance is equal to the given o
141      */
142     @Override
equals(Object o)143     public boolean equals(Object o) {
144         if (this == o) {
145             return true;
146         }
147         if (o == null || !(o instanceof EapData)) {
148             return false;
149         }
150         EapData eapData = (EapData) o;
151         return eapType == eapData.eapType
152                 && Arrays.equals(eapTypeData, eapData.eapTypeData);
153     }
154 
155     /**
156      * Returns the hashCode value for this instance.
157      *
158      * @return the hashCode value for this instance
159      */
160     @Override
hashCode()161     public int hashCode() {
162         return Objects.hash(eapType, Arrays.hashCode(eapTypeData));
163     }
164 
165     /**
166      * Puts the byte-encoding for this EapData instance into the given ByteBuffer.
167      *
168      * @param b the ByteBuffer to write this EapData instance to
169      */
encodeToByteBuffer(ByteBuffer b)170     public void encodeToByteBuffer(ByteBuffer b) {
171         b.put((byte) eapType);
172         b.put(eapTypeData);
173     }
174 
175     /**
176      * Returns whether the given eapType is a supported {@link EapType} value.
177      *
178      * @param eapType the value to be checked
179      * @return true iff the given eapType is a supported EAP Type
180      */
isSupportedEapType(int eapType)181     public static boolean isSupportedEapType(int eapType) {
182         return SUPPORTED_TYPES.contains(eapType);
183     }
184 
validate()185     private void validate() {
186         if (this.eapTypeData == null) {
187             throw new IllegalArgumentException("EapTypeData byte[] must be non-null");
188         }
189         if (!isSupportedEapType(this.eapType)) {
190             throw new IllegalArgumentException("eapType must be be a valid @EapType value");
191         }
192     }
193 }
194