diff options
author | 2016-07-13 21:18:14 +0000 | |
---|---|---|
committer | 2016-07-13 21:18:14 +0000 | |
commit | 2bd65a32557d53e0671179c68ba14f530697d0ea (patch) | |
tree | 6c0ef4dba3ec67f9ff8129a822749bbbf83586c3 | |
parent | 627f4f39d67dd903806680160dd08680c8958985 (diff) | |
parent | adfbfb3a939f5662cb62cdd6b5d7cce5494a159e (diff) |
[NAN] Add validity checks for service name
am: adfbfb3a93
Change-Id: I1f4ae4f673e5b08188d0727d1c9221d8af63a988
-rw-r--r-- | wifi/java/android/net/wifi/nan/PublishConfig.java | 34 | ||||
-rw-r--r-- | wifi/java/android/net/wifi/nan/SubscribeConfig.java | 34 | ||||
-rw-r--r-- | wifi/java/android/net/wifi/nan/WifiNanUtils.java | 54 |
3 files changed, 106 insertions, 16 deletions
diff --git a/wifi/java/android/net/wifi/nan/PublishConfig.java b/wifi/java/android/net/wifi/nan/PublishConfig.java index 8295484eb308..96d257c6f30d 100644 --- a/wifi/java/android/net/wifi/nan/PublishConfig.java +++ b/wifi/java/android/net/wifi/nan/PublishConfig.java @@ -23,6 +23,7 @@ import android.os.Parcelable; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.nio.charset.StandardCharsets; import java.util.Arrays; /** @@ -58,7 +59,7 @@ public class PublishConfig implements Parcelable { /** * @hide */ - public final String mServiceName; + public final byte[] mServiceName; /** * @hide @@ -110,7 +111,7 @@ public class PublishConfig implements Parcelable { */ public final boolean mEnableTerminateNotification; - private PublishConfig(String serviceName, byte[] serviceSpecificInfo, + private PublishConfig(byte[] serviceName, byte[] serviceSpecificInfo, int serviceSpecificInfoLength, byte[] txFilter, int txFilterLength, byte[] rxFilter, int rxFilterLength, int publishType, int publichCount, int ttlSec, boolean enableTerminateNotification) { @@ -147,7 +148,10 @@ public class PublishConfig implements Parcelable { @Override public void writeToParcel(Parcel dest, int flags) { - dest.writeString(mServiceName); + dest.writeInt(mServiceName.length); + if (mServiceName.length != 0) { + dest.writeByteArray(mServiceName); + } dest.writeInt(mServiceSpecificInfoLength); if (mServiceSpecificInfoLength != 0) { dest.writeByteArray(mServiceSpecificInfo, 0, mServiceSpecificInfoLength); @@ -174,7 +178,11 @@ public class PublishConfig implements Parcelable { @Override public PublishConfig createFromParcel(Parcel in) { - String serviceName = in.readString(); + int serviceNameLength = in.readInt(); + byte[] serviceName = new byte[serviceNameLength]; + if (serviceNameLength != 0) { + in.readByteArray(serviceName); + } int ssiLength = in.readInt(); byte[] ssi = new byte[ssiLength]; if (ssiLength != 0) { @@ -213,7 +221,7 @@ public class PublishConfig implements Parcelable { PublishConfig lhs = (PublishConfig) o; - if (!mServiceName.equals(lhs.mServiceName) + if (!Arrays.equals(mServiceName, lhs.mServiceName) || mServiceSpecificInfoLength != lhs.mServiceSpecificInfoLength || mTxFilterLength != lhs.mTxFilterLength || mRxFilterLength != lhs.mRxFilterLength) { @@ -259,7 +267,7 @@ public class PublishConfig implements Parcelable { public int hashCode() { int result = 17; - result = 31 * result + mServiceName.hashCode(); + result = 31 * result + Arrays.hashCode(mServiceName); result = 31 * result + mServiceSpecificInfoLength; result = 31 * result + Arrays.hashCode(mServiceSpecificInfo); result = 31 * result + mTxFilterLength; @@ -281,6 +289,8 @@ public class PublishConfig implements Parcelable { * @hide */ public void validate() throws IllegalArgumentException { + WifiNanUtils.validateServiceName(mServiceName); + if (mServiceSpecificInfoLength != 0 && (mServiceSpecificInfo == null || mServiceSpecificInfo.length < mServiceSpecificInfoLength)) { throw new IllegalArgumentException("Non-matching combination of " @@ -317,7 +327,7 @@ public class PublishConfig implements Parcelable { * Builder used to build {@link PublishConfig} objects. */ public static final class Builder { - private String mServiceName; + private byte[] mServiceName; private int mServiceSpecificInfoLength; private byte[] mServiceSpecificInfo = new byte[0]; private int mTxFilterLength; @@ -333,12 +343,20 @@ public class PublishConfig implements Parcelable { * Specify the service name of the publish session. The actual on-air * value is a 6 byte hashed representation of this string. * + * Per spec: The Service Name is a UTF-8 encoded string from 1 to 255 bytes in length. + * The only acceptable single-byte UTF-8 symbols for a Service Name are alphanumeric + * values (A-Z, a-z, 0-9), the hyphen ('-'), and the period ('.'). All valid multi-byte + * UTF-8 characters are acceptable in a Service Name. + * * @param serviceName The service name for the publish session. * @return The builder to facilitate chaining * {@code builder.setXXX(..).setXXX(..)}. */ public Builder setServiceName(@NonNull String serviceName) { - mServiceName = serviceName; + if (serviceName == null) { + throw new IllegalArgumentException("Invalid service name - must be non-null"); + } + mServiceName = serviceName.getBytes(StandardCharsets.UTF_8); return this; } diff --git a/wifi/java/android/net/wifi/nan/SubscribeConfig.java b/wifi/java/android/net/wifi/nan/SubscribeConfig.java index 6ef4dfa40fa8..3133d3e7d0c2 100644 --- a/wifi/java/android/net/wifi/nan/SubscribeConfig.java +++ b/wifi/java/android/net/wifi/nan/SubscribeConfig.java @@ -23,6 +23,7 @@ import android.os.Parcelable; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.nio.charset.StandardCharsets; import java.util.Arrays; /** @@ -76,7 +77,7 @@ public class SubscribeConfig implements Parcelable { /** * @hide */ - public final String mServiceName; + public final byte[] mServiceName; /** * @hide @@ -133,7 +134,7 @@ public class SubscribeConfig implements Parcelable { */ public final boolean mEnableTerminateNotification; - private SubscribeConfig(String serviceName, byte[] serviceSpecificInfo, + private SubscribeConfig(byte[] serviceName, byte[] serviceSpecificInfo, int serviceSpecificInfoLength, byte[] txFilter, int txFilterLength, byte[] rxFilter, int rxFilterLength, int subscribeType, int publichCount, int ttlSec, int matchStyle, boolean enableTerminateNotification) { @@ -171,7 +172,10 @@ public class SubscribeConfig implements Parcelable { @Override public void writeToParcel(Parcel dest, int flags) { - dest.writeString(mServiceName); + dest.writeInt(mServiceName.length); + if (mServiceName.length != 0) { + dest.writeByteArray(mServiceName); + } dest.writeInt(mServiceSpecificInfoLength); if (mServiceSpecificInfoLength != 0) { dest.writeByteArray(mServiceSpecificInfo, 0, mServiceSpecificInfoLength); @@ -199,7 +203,11 @@ public class SubscribeConfig implements Parcelable { @Override public SubscribeConfig createFromParcel(Parcel in) { - String serviceName = in.readString(); + int serviceNameLength = in.readInt(); + byte[] serviceName = new byte[serviceNameLength]; + if (serviceNameLength != 0) { + in.readByteArray(serviceName); + } int ssiLength = in.readInt(); byte[] ssi = new byte[ssiLength]; if (ssiLength != 0) { @@ -239,7 +247,7 @@ public class SubscribeConfig implements Parcelable { SubscribeConfig lhs = (SubscribeConfig) o; - if (!mServiceName.equals(lhs.mServiceName) + if (!Arrays.equals(mServiceName, lhs.mServiceName) || mServiceSpecificInfoLength != lhs.mServiceSpecificInfoLength || mTxFilterLength != lhs.mTxFilterLength || mRxFilterLength != lhs.mRxFilterLength) { @@ -285,7 +293,7 @@ public class SubscribeConfig implements Parcelable { public int hashCode() { int result = 17; - result = 31 * result + mServiceName.hashCode(); + result = 31 * result + Arrays.hashCode(mServiceName); result = 31 * result + mServiceSpecificInfoLength; result = 31 * result + Arrays.hashCode(mServiceSpecificInfo); result = 31 * result + mTxFilterLength; @@ -308,6 +316,8 @@ public class SubscribeConfig implements Parcelable { * @hide */ public void validate() throws IllegalArgumentException { + WifiNanUtils.validateServiceName(mServiceName); + if (mServiceSpecificInfoLength != 0 && (mServiceSpecificInfo == null || mServiceSpecificInfo.length < mServiceSpecificInfoLength)) { throw new IllegalArgumentException("Non-matching combination of " @@ -348,7 +358,7 @@ public class SubscribeConfig implements Parcelable { * Builder used to build {@link SubscribeConfig} objects. */ public static final class Builder { - private String mServiceName; + private byte[] mServiceName; private int mServiceSpecificInfoLength; private byte[] mServiceSpecificInfo = new byte[0]; private int mTxFilterLength; @@ -365,12 +375,20 @@ public class SubscribeConfig implements Parcelable { * Specify the service name of the subscribe session. The actual on-air * value is a 6 byte hashed representation of this string. * + * Per spec: The Service Name is a UTF-8 encoded string from 1 to 255 bytes in length. + * The only acceptable single-byte UTF-8 symbols for a Service Name are alphanumeric + * values (A-Z, a-z, 0-9), the hyphen ('-'), and the period ('.'). All valid multi-byte + * UTF-8 characters are acceptable in a Service Name. + * * @param serviceName The service name for the subscribe session. * @return The builder to facilitate chaining * {@code builder.setXXX(..).setXXX(..)}. */ public Builder setServiceName(@NonNull String serviceName) { - mServiceName = serviceName; + if (serviceName == null) { + throw new IllegalArgumentException("Invalid service name - must be non-null"); + } + mServiceName = serviceName.getBytes(StandardCharsets.UTF_8); return this; } diff --git a/wifi/java/android/net/wifi/nan/WifiNanUtils.java b/wifi/java/android/net/wifi/nan/WifiNanUtils.java new file mode 100644 index 000000000000..c0f36b42cd4d --- /dev/null +++ b/wifi/java/android/net/wifi/nan/WifiNanUtils.java @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.net.wifi.nan; + +/** + * Provides utilities for the Wifi NAN manager/service. + * + * @hide + */ +public class WifiNanUtils { + /** + * Per spec: The Service Name is a UTF-8 encoded string from 1 to 255 bytes in length. The + * only acceptable single-byte UTF-8 symbols for a Service Name are alphanumeric values (A-Z, + * a-z, 0-9), the hyphen ('-'), and the period ('.'). All valid multi-byte UTF-8 characters + * are acceptable in a Service Name. + */ + public static void validateServiceName(byte[] serviceNameData) throws IllegalArgumentException { + if (serviceNameData == null) { + throw new IllegalArgumentException("Invalid service name - null"); + } + + if (serviceNameData.length < 1 || serviceNameData.length > 255) { + throw new IllegalArgumentException("Invalid service name length - must be between " + + "1 and 255 bytes (UTF-8 encoding)"); + } + + int index = 0; + while (index < serviceNameData.length) { + byte b = serviceNameData[index]; + if ((b & 0x80) == 0x00) { + if (!((b >= '0' && b <= '9') || (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') + || b == '-' || b == '.')) { + throw new IllegalArgumentException("Invalid service name - illegal characters," + + " allowed = (0-9, a-z,A-Z, -, .)"); + } + } + ++index; + } + } +} |