1 /* 2 * Copyright (C) 2022 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 static android.net.wifi.aware.Characteristics.WIFI_AWARE_CIPHER_SUITE_NCS_PK_PASN_128; 20 21 import android.annotation.FlaggedApi; 22 import android.annotation.IntDef; 23 import android.annotation.NonNull; 24 import android.os.Parcel; 25 import android.os.Parcelable; 26 27 import com.android.wifi.flags.Flags; 28 29 import java.lang.annotation.Retention; 30 import java.lang.annotation.RetentionPolicy; 31 import java.util.Objects; 32 33 /** 34 * The config for the Aware Pairing. Set to 35 * {@link PublishConfig.Builder#setPairingConfig(AwarePairingConfig)} and 36 * {@link SubscribeConfig.Builder#setPairingConfig(AwarePairingConfig)}. 37 * Only valid when {@link Characteristics#isAwarePairingSupported()} is true. 38 */ 39 public final class AwarePairingConfig implements Parcelable { 40 41 /** 42 * Aware Pairing bootstrapping method opportunistic 43 */ 44 public static final int PAIRING_BOOTSTRAPPING_OPPORTUNISTIC = 1 << 0; 45 46 /** 47 * Aware Pairing bootstrapping method pin-code display 48 */ 49 public static final int PAIRING_BOOTSTRAPPING_PIN_CODE_DISPLAY = 1 << 1; 50 51 /** 52 * Aware Pairing bootstrapping method passphrase display 53 */ 54 public static final int PAIRING_BOOTSTRAPPING_PASSPHRASE_DISPLAY = 1 << 2; 55 56 /** 57 * Aware Pairing bootstrapping method QR-code display 58 */ 59 public static final int PAIRING_BOOTSTRAPPING_QR_DISPLAY = 1 << 3; 60 61 /** 62 * Aware Pairing bootstrapping method NFC tag 63 */ 64 public static final int PAIRING_BOOTSTRAPPING_NFC_TAG = 1 << 4; 65 /** 66 * Aware Pairing bootstrapping method pin-code keypad 67 */ 68 public static final int PAIRING_BOOTSTRAPPING_PIN_CODE_KEYPAD = 1 << 5; 69 /** 70 * Aware Pairing bootstrapping method passphrase keypad 71 */ 72 public static final int PAIRING_BOOTSTRAPPING_PASSPHRASE_KEYPAD = 1 << 6; 73 /** 74 * Aware Pairing bootstrapping method QR-code scan 75 */ 76 public static final int PAIRING_BOOTSTRAPPING_QR_SCAN = 1 << 7; 77 /** 78 * Aware Pairing bootstrapping method NFC reader 79 */ 80 public static final int PAIRING_BOOTSTRAPPING_NFC_READER = 1 << 8; 81 /** 82 * This is used for the boundary check and should be the max value of the bitmap + 1. 83 * @hide 84 */ 85 public static final int PAIRING_BOOTSTRAPPING_MAX = 1 << 9; 86 87 88 /** @hide */ 89 @IntDef(flag = true, prefix = {"PAIRING_BOOTSTRAPPING_"}, value = { 90 PAIRING_BOOTSTRAPPING_OPPORTUNISTIC, 91 PAIRING_BOOTSTRAPPING_PIN_CODE_DISPLAY, 92 PAIRING_BOOTSTRAPPING_PASSPHRASE_DISPLAY, 93 PAIRING_BOOTSTRAPPING_QR_DISPLAY, 94 PAIRING_BOOTSTRAPPING_NFC_TAG, 95 PAIRING_BOOTSTRAPPING_PIN_CODE_KEYPAD, 96 PAIRING_BOOTSTRAPPING_PASSPHRASE_KEYPAD, 97 PAIRING_BOOTSTRAPPING_QR_SCAN, 98 PAIRING_BOOTSTRAPPING_NFC_READER 99 100 }) 101 @Retention(RetentionPolicy.SOURCE) 102 public @interface BootstrappingMethod { 103 } 104 105 private final boolean mPairingSetup; 106 private final boolean mPairingCache; 107 private final boolean mPairingVerification; 108 private final int mBootstrappingMethods; 109 private final int mCipherSuites; 110 111 /** 112 * Check if the NPK/NIK cache is support in the config 113 */ isPairingCacheEnabled()114 public boolean isPairingCacheEnabled() { 115 return mPairingCache; 116 } 117 118 /** 119 * Check if the Aware Pairing setup is support in the config. 120 * Setup is the first time for two device establish Aware Pairing 121 */ isPairingSetupEnabled()122 public boolean isPairingSetupEnabled() { 123 return mPairingSetup; 124 } 125 126 /** 127 * Check if the Aware Pairing verification is support in the config. 128 * Verification is for two device already paired and re-establish with cached NPK/NIK 129 */ isPairingVerificationEnabled()130 public boolean isPairingVerificationEnabled() { 131 return mPairingVerification; 132 } 133 134 /** 135 * Get the supported bootstrapping methods in this config. Set of the 136 * STATUS_NETWORK_SUGGESTIONS_ values. 137 */ 138 @BootstrappingMethod getBootstrappingMethods()139 public int getBootstrappingMethods() { 140 return mBootstrappingMethods; 141 } 142 143 /** 144 * Get the supported cipher suites in this config. 145 */ 146 @FlaggedApi(Flags.FLAG_AWARE_PAIRING) 147 @Characteristics.WifiAwarePairingCipherSuites getSupportedCipherSuites()148 public int getSupportedCipherSuites() { 149 return mCipherSuites; 150 } 151 152 /** 153 * Verifies that the contents of the AwarePairingConfig are valid 154 * @hide 155 */ assertValid(Characteristics characteristics)156 public boolean assertValid(Characteristics characteristics) { 157 if (mBootstrappingMethods < 0 || mBootstrappingMethods >= PAIRING_BOOTSTRAPPING_MAX) { 158 return false; 159 } 160 if ((characteristics.getSupportedPairingCipherSuites() & mCipherSuites) == 0) { 161 return false; 162 } 163 return true; 164 } 165 166 @Override equals(Object o)167 public boolean equals(Object o) { 168 if (this == o) return true; 169 if (!(o instanceof AwarePairingConfig)) return false; 170 AwarePairingConfig that = (AwarePairingConfig) o; 171 return mPairingSetup == that.mPairingSetup && mPairingCache == that.mPairingCache 172 && mPairingVerification == that.mPairingVerification 173 && mBootstrappingMethods == that.mBootstrappingMethods 174 && mCipherSuites == that.mCipherSuites; 175 } 176 177 @Override hashCode()178 public int hashCode() { 179 return Objects.hash(mPairingSetup, mPairingCache, mPairingVerification, 180 mBootstrappingMethods, mCipherSuites); 181 } 182 183 /** @hide */ AwarePairingConfig(boolean setup, boolean cache, boolean verification, int method, int cipherSuites)184 public AwarePairingConfig(boolean setup, boolean cache, boolean verification, int method, 185 int cipherSuites) { 186 mPairingSetup = setup; 187 mPairingCache = cache; 188 mPairingVerification = verification; 189 mBootstrappingMethods = method; 190 mCipherSuites = cipherSuites; 191 } 192 193 /** @hide */ AwarePairingConfig(@onNull Parcel in)194 protected AwarePairingConfig(@NonNull Parcel in) { 195 mPairingSetup = in.readBoolean(); 196 mPairingCache = in.readBoolean(); 197 mPairingVerification = in.readBoolean(); 198 mBootstrappingMethods = in.readInt(); 199 mCipherSuites = in.readInt(); 200 } 201 202 @Override writeToParcel(@onNull Parcel dest, int flags)203 public void writeToParcel(@NonNull Parcel dest, int flags) { 204 dest.writeBoolean(mPairingSetup); 205 dest.writeBoolean(mPairingCache); 206 dest.writeBoolean(mPairingVerification); 207 dest.writeInt(mBootstrappingMethods); 208 dest.writeInt(mCipherSuites); 209 } 210 211 @Override describeContents()212 public int describeContents() { 213 return 0; 214 } 215 216 @NonNull public static final Creator<AwarePairingConfig> CREATOR = 217 new Creator<AwarePairingConfig>() { 218 @Override 219 public AwarePairingConfig createFromParcel(@NonNull Parcel in) { 220 return new AwarePairingConfig(in); 221 } 222 223 @Override 224 public AwarePairingConfig[] newArray(int size) { 225 return new AwarePairingConfig[size]; 226 } 227 }; 228 /** 229 * Builder used to build {@link AwarePairingConfig} objects. 230 */ 231 public static final class Builder { 232 private boolean mPairingSetup = false; 233 private boolean mPairingCache = false; 234 private boolean mPairingVerification = false; 235 private int mBootStrappingMethods = 0; 236 private int mCipherSuites = WIFI_AWARE_CIPHER_SUITE_NCS_PK_PASN_128; 237 238 /** 239 * Set whether enable the Aware Pairing setup 240 * @param enabled true to enable, false otherwise 241 * @return the current {@link Builder} builder, enabling chaining of builder methods. 242 */ 243 @NonNull setPairingSetupEnabled(boolean enabled)244 public Builder setPairingSetupEnabled(boolean enabled) { 245 mPairingSetup = enabled; 246 return this; 247 } 248 249 /** 250 * Set whether enable the Aware Pairing verification 251 * @param enabled if set to true will accept Aware Pairing verification request from peer 252 * with cached NPK/NIK, otherwise will reject the request . 253 * @return the current {@link Builder} builder, enabling chaining of builder methods. 254 */ setPairingVerificationEnabled(boolean enabled)255 @NonNull public Builder setPairingVerificationEnabled(boolean enabled) { 256 mPairingVerification = enabled; 257 return this; 258 } 259 260 /** 261 * Set whether enable cache of the NPK/NIK of Aware Pairing setup 262 * @param enabled true to enable caching, false otherwise 263 * @return the current {@link Builder} builder, enabling chaining of builder methods. 264 */ setPairingCacheEnabled(boolean enabled)265 @NonNull public Builder setPairingCacheEnabled(boolean enabled) { 266 mPairingCache = enabled; 267 return this; 268 } 269 270 /** 271 * Set the supported bootstrapping methods 272 * @param methods methods supported, set of PAIRING_BOOTSTRAPPING_ values 273 * @return the current {@link Builder} builder, enabling chaining of builder methods. 274 */ setBootstrappingMethods(@ootstrappingMethod int methods)275 @NonNull public Builder setBootstrappingMethods(@BootstrappingMethod int methods) { 276 mBootStrappingMethods = methods; 277 return this; 278 } 279 280 /** 281 * Set the supported cipher suites. If not set, default will be 282 * {@link Characteristics#WIFI_AWARE_CIPHER_SUITE_NCS_PK_PASN_128} 283 * @param cipherSuites cipher suites supported 284 * @return the current {@link Builder} builder, enabling chaining of builder methods. 285 */ 286 @FlaggedApi(Flags.FLAG_AWARE_PAIRING) setSupportedCipherSuites( @haracteristics.WifiAwarePairingCipherSuites int cipherSuites)287 @NonNull public Builder setSupportedCipherSuites( 288 @Characteristics.WifiAwarePairingCipherSuites int cipherSuites) { 289 mCipherSuites = cipherSuites; 290 return this; 291 } 292 293 /** 294 * Build {@link AwarePairingConfig} given the current requests made on the 295 * builder. 296 */ 297 @NonNull build()298 public AwarePairingConfig build() { 299 return new AwarePairingConfig(mPairingSetup, mPairingCache, mPairingVerification, 300 mBootStrappingMethods, mCipherSuites); 301 } 302 } 303 } 304