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 17 package com.android.server.wifi.hotspot2.anqp.eap; 18 19 import com.android.internal.annotations.VisibleForTesting; 20 21 import java.net.ProtocolException; 22 import java.nio.BufferUnderflowException; 23 import java.nio.ByteBuffer; 24 import java.util.HashMap; 25 import java.util.Map; 26 27 /** 28 * The Non-EAP Inner Authentication Type authentication parameter, IEEE802.11-2012, table 8-188. 29 * 30 * Format: 31 * | Type | 32 * 1 33 */ 34 public class NonEAPInnerAuth extends AuthParam { 35 public static final int AUTH_TYPE_UNKNOWN = 0; 36 public static final int AUTH_TYPE_PAP = 1; 37 public static final int AUTH_TYPE_CHAP = 2; 38 public static final int AUTH_TYPE_MSCHAP = 3; 39 public static final int AUTH_TYPE_MSCHAPV2 = 4; 40 41 private static final Map<String, Integer> AUTH_TYPE_MAP = new HashMap<>(); 42 static { 43 AUTH_TYPE_MAP.put("PAP", AUTH_TYPE_PAP); 44 AUTH_TYPE_MAP.put("CHAP", AUTH_TYPE_CHAP); 45 AUTH_TYPE_MAP.put("MS-CHAP", AUTH_TYPE_MSCHAP); 46 AUTH_TYPE_MAP.put("MS-CHAP-V2", AUTH_TYPE_MSCHAPV2); 47 } 48 49 @VisibleForTesting 50 public static final int EXPECTED_LENGTH_VALUE = 1; 51 52 private final int mAuthType; 53 54 public NonEAPInnerAuth(int authType) { 55 super(AuthParam.PARAM_TYPE_NON_EAP_INNER_AUTH_TYPE); 56 mAuthType = authType; 57 } 58 59 /** 60 * Parse a NonEAPInnerAuth from the given buffer. 61 * 62 * @param payload The byte buffer to read from 63 * @param length The length of the data 64 * @return {@link NonEAPInnerAuth} 65 * @throws BufferUnderflowException 66 */ 67 public static NonEAPInnerAuth parse(ByteBuffer payload, int length) throws ProtocolException { 68 if (length != EXPECTED_LENGTH_VALUE) { 69 throw new ProtocolException("Invalid length: " + length); 70 } 71 int authType = payload.get() & 0xFF; 72 return new NonEAPInnerAuth(authType); 73 } 74 75 /** 76 * Convert an authentication type string to an integer representation. 77 * 78 * @param typeStr The string of authentication type 79 * @return int 80 */ 81 public static int getAuthTypeID(String typeStr) { 82 if (AUTH_TYPE_MAP.containsKey(typeStr)) { 83 return AUTH_TYPE_MAP.get(typeStr).intValue(); 84 } 85 return AUTH_TYPE_UNKNOWN; 86 } 87 88 @Override 89 public boolean equals(Object thatObject) { 90 if (thatObject == this) { 91 return true; 92 } 93 if (!(thatObject instanceof NonEAPInnerAuth)) { 94 return false; 95 } 96 NonEAPInnerAuth that = (NonEAPInnerAuth) thatObject; 97 return mAuthType == that.mAuthType; 98 } 99 100 @Override 101 public int hashCode() { 102 return mAuthType; 103 } 104 105 @Override 106 public String toString() { 107 return "NonEAPInnerAuth{mAuthType=" + mAuthType + "}"; 108 } 109 } 110