1 /* 2 * Copyright (C) 2024 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.rtt; 18 19 import android.annotation.FlaggedApi; 20 import android.annotation.NonNull; 21 import android.os.Parcel; 22 import android.os.Parcelable; 23 24 import com.android.modules.utils.build.SdkLevel; 25 import com.android.wifi.flags.Flags; 26 27 import java.util.Objects; 28 29 /** 30 * Secure ranging configuration. 31 * Refer IEEE Std 802.11az-2022, section 12. Security. 32 */ 33 @FlaggedApi(Flags.FLAG_SECURE_RANGING) 34 public final class SecureRangingConfig implements Parcelable { 35 private final boolean mEnableSecureHeLtf; 36 private final boolean mEnableRangingFrameProtection; 37 private final PasnConfig mPasnConfig; 38 39 SecureRangingConfig(boolean enableSecureHeLtf, boolean enableRangingFrameProtection, @NonNull PasnConfig pasnConfig)40 private SecureRangingConfig(boolean enableSecureHeLtf, boolean enableRangingFrameProtection, 41 @NonNull PasnConfig pasnConfig) { 42 Objects.requireNonNull(pasnConfig, "pasnConfig cannot be null"); 43 mEnableSecureHeLtf = enableSecureHeLtf; 44 mEnableRangingFrameProtection = enableRangingFrameProtection; 45 mPasnConfig = pasnConfig; 46 } 47 SecureRangingConfig(@onNull Parcel in)48 private SecureRangingConfig(@NonNull Parcel in) { 49 mEnableSecureHeLtf = in.readByte() != 0; 50 mEnableRangingFrameProtection = in.readByte() != 0; 51 mPasnConfig = (SdkLevel.isAtLeastT()) ? in.readParcelable(PasnConfig.class.getClassLoader(), 52 PasnConfig.class) : in.readParcelable(PasnConfig.class.getClassLoader()); 53 } 54 55 @FlaggedApi(Flags.FLAG_SECURE_RANGING) 56 public static final @NonNull Creator<SecureRangingConfig> CREATOR = 57 new Creator<SecureRangingConfig>() { 58 @Override 59 public SecureRangingConfig createFromParcel(Parcel in) { 60 return new SecureRangingConfig(in); 61 } 62 63 @Override 64 public SecureRangingConfig[] newArray(int size) { 65 return new SecureRangingConfig[size]; 66 } 67 }; 68 SecureRangingConfig(Builder builder)69 private SecureRangingConfig(Builder builder) { 70 mEnableSecureHeLtf = builder.mEnableSecureHeLtf; 71 mEnableRangingFrameProtection = builder.mEnableRangingFrameProtection; 72 mPasnConfig = builder.mPasnConfig; 73 } 74 75 /** 76 * Returns whether secure HE-LTF is enabled or not. 77 */ 78 @FlaggedApi(Flags.FLAG_SECURE_RANGING) isSecureHeLtfEnabled()79 public boolean isSecureHeLtfEnabled() { 80 return mEnableSecureHeLtf; 81 } 82 83 /** 84 * Returns whether ranging frame protection is enabled or not. 85 */ 86 @FlaggedApi(Flags.FLAG_SECURE_RANGING) isRangingFrameProtectionEnabled()87 public boolean isRangingFrameProtectionEnabled() { 88 return mEnableRangingFrameProtection; 89 } 90 91 /** 92 * Returns Pre-association security negotiation (PASN) configuration used for secure 93 * ranging. 94 * 95 * @return {@link PasnConfig} object. 96 */ 97 @FlaggedApi(Flags.FLAG_SECURE_RANGING) 98 @NonNull getPasnConfig()99 public PasnConfig getPasnConfig() { 100 return mPasnConfig; 101 } 102 103 @FlaggedApi(Flags.FLAG_SECURE_RANGING) 104 @Override describeContents()105 public int describeContents() { 106 return 0; 107 } 108 109 @FlaggedApi(Flags.FLAG_SECURE_RANGING) 110 @Override writeToParcel(@ndroidx.annotation.NonNull Parcel dest, int flags)111 public void writeToParcel(@androidx.annotation.NonNull Parcel dest, int flags) { 112 dest.writeByte((byte) (mEnableSecureHeLtf ? 1 : 0)); 113 dest.writeByte((byte) (mEnableRangingFrameProtection ? 1 : 0)); 114 dest.writeParcelable(mPasnConfig, flags); 115 } 116 117 @FlaggedApi(Flags.FLAG_SECURE_RANGING) 118 @Override toString()119 public String toString() { 120 return "SecureRangingConfig{" + "mEnableSecureHeLtf=" + mEnableSecureHeLtf 121 + ", mEnableRangingProtection=" + mEnableRangingFrameProtection + ", mPasnConfig=" 122 + mPasnConfig + '}'; 123 } 124 125 @FlaggedApi(Flags.FLAG_SECURE_RANGING) 126 @Override equals(Object o)127 public boolean equals(Object o) { 128 if (this == o) return true; 129 if (!(o instanceof SecureRangingConfig that)) return false; 130 return mEnableSecureHeLtf == that.mEnableSecureHeLtf 131 && mEnableRangingFrameProtection == that.mEnableRangingFrameProtection 132 && Objects.equals(mPasnConfig, that.mPasnConfig); 133 } 134 135 @FlaggedApi(Flags.FLAG_SECURE_RANGING) 136 @Override hashCode()137 public int hashCode() { 138 return Objects.hash(mEnableSecureHeLtf, mEnableRangingFrameProtection, mPasnConfig); 139 } 140 141 /** 142 * Builder for {@link SecureRangingConfig} 143 */ 144 @FlaggedApi(Flags.FLAG_SECURE_RANGING) 145 public static final class Builder { 146 private boolean mEnableSecureHeLtf = true; 147 private boolean mEnableRangingFrameProtection = true; 148 private final PasnConfig mPasnConfig; 149 150 /** 151 * Builder constructor. 152 * 153 * @param pasnConfig PASN configuration 154 */ 155 @FlaggedApi(Flags.FLAG_SECURE_RANGING) Builder(@onNull PasnConfig pasnConfig)156 public Builder(@NonNull PasnConfig pasnConfig) { 157 Objects.requireNonNull(pasnConfig, "pasnConfig must not be null"); 158 mPasnConfig = pasnConfig; 159 } 160 161 /** 162 * Enable or disable secure HE-LTF and returns a reference to this Builder enabling 163 * method chaining. If not set, secure HE-LTF is enabled. 164 * 165 * @param enableSecureHeLtf the {@code enableSecureHeLtf} to set 166 * @return a reference to this Builder 167 */ 168 @FlaggedApi(Flags.FLAG_SECURE_RANGING) 169 @NonNull setSecureHeLtfEnabled(boolean enableSecureHeLtf)170 public Builder setSecureHeLtfEnabled(boolean enableSecureHeLtf) { 171 this.mEnableSecureHeLtf = enableSecureHeLtf; 172 return this; 173 } 174 175 /** 176 * Enable or disable ranging frame protection and returns a reference to this Builder 177 * enabling method chaining. If not set, ranging frame protection is enabled. 178 * 179 * @param enableRangingFrameProtection the {@code enableRangingFrameProtection} to set 180 * @return a reference to this Builder 181 */ 182 @FlaggedApi(Flags.FLAG_SECURE_RANGING) 183 @NonNull setRangingFrameProtectionEnabled(boolean enableRangingFrameProtection)184 public Builder setRangingFrameProtectionEnabled(boolean enableRangingFrameProtection) { 185 this.mEnableRangingFrameProtection = enableRangingFrameProtection; 186 return this; 187 } 188 189 /** 190 * Returns a {@code SecureRangingConfig} built from the parameters previously set. 191 * 192 * @return a {@code SecureRangingConfig} built with parameters of this 193 * {@code SecureRangingConfig.Builder} 194 */ 195 @FlaggedApi(Flags.FLAG_SECURE_RANGING) 196 @NonNull build()197 public SecureRangingConfig build() { 198 return new SecureRangingConfig(this); 199 } 200 } 201 } 202