Home | History | Annotate | Download | only in aware
      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 android.net.wifi.aware;
     18 
     19 import android.content.Context;
     20 import android.content.pm.PackageManager;
     21 import android.hardware.wifi.V1_0.Constants;
     22 
     23 /**
     24  * Provides utilities for the Wifi Aware manager/service.
     25  *
     26  * @hide
     27  */
     28 public class WifiAwareUtils {
     29     /**
     30      * Per spec: The Service Name is a UTF-8 encoded string from 1 to 255 bytes in length. The
     31      * only acceptable single-byte UTF-8 symbols for a Service Name are alphanumeric values (A-Z,
     32      * a-z, 0-9), the hyphen ('-'), and the period ('.'). All valid multi-byte UTF-8 characters
     33      * are acceptable in a Service Name.
     34      */
     35     public static void validateServiceName(byte[] serviceNameData) throws IllegalArgumentException {
     36         if (serviceNameData == null) {
     37             throw new IllegalArgumentException("Invalid service name - null");
     38         }
     39 
     40         if (serviceNameData.length < 1 || serviceNameData.length > 255) {
     41             throw new IllegalArgumentException("Invalid service name length - must be between "
     42                     + "1 and 255 bytes (UTF-8 encoding)");
     43         }
     44 
     45         int index = 0;
     46         while (index < serviceNameData.length) {
     47             byte b = serviceNameData[index];
     48             if ((b & 0x80) == 0x00) {
     49                 if (!((b >= '0' && b <= '9') || (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z')
     50                         || b == '-' || b == '.')) {
     51                     throw new IllegalArgumentException("Invalid service name - illegal characters,"
     52                             + " allowed = (0-9, a-z,A-Z, -, .)");
     53                 }
     54             }
     55             ++index;
     56         }
     57     }
     58 
     59     /**
     60      * Validates that the passphrase is a non-null string of the right size (per the HAL min/max
     61      * length parameters).
     62      *
     63      * @param passphrase Passphrase to test
     64      * @return true if passphrase is valid, false if not
     65      */
     66     public static boolean validatePassphrase(String passphrase) {
     67         if (passphrase == null
     68                 || passphrase.length() < Constants.NanParamSizeLimits.MIN_PASSPHRASE_LENGTH
     69                 || passphrase.length() > Constants.NanParamSizeLimits.MAX_PASSPHRASE_LENGTH) {
     70             return false;
     71         }
     72 
     73         return true;
     74     }
     75 
     76     /**
     77      * Validates that the PMK is a non-null byte array of the right size (32 bytes per spec).
     78      *
     79      * @param pmk PMK to test
     80      * @return true if PMK is valid, false if not
     81      */
     82     public static boolean validatePmk(byte[] pmk) {
     83         if (pmk == null || pmk.length != 32) {
     84             return false;
     85         }
     86 
     87         return true;
     88     }
     89 
     90     /**
     91      * Returns true if the App version is older than minVersion.
     92      */
     93     public static boolean isLegacyVersion(Context context, int minVersion) {
     94         try {
     95             if (context.getPackageManager().getApplicationInfo(context.getOpPackageName(), 0)
     96                     .targetSdkVersion < minVersion) {
     97                 return true;
     98             }
     99         } catch (PackageManager.NameNotFoundException e) {
    100             // In case of exception, assume known app (more strict checking)
    101             // Note: This case will never happen since checkPackage is
    102             // called to verify valididity before checking App's version.
    103         }
    104         return false;
    105     }
    106 }
    107