1 /* 2 * Copyright (C) 2020 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.ipsec.ike.ike3gpp; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.annotation.SuppressLint; 22 import android.annotation.SystemApi; 23 import android.net.ipsec.ike.IkeManager; 24 25 import com.android.internal.net.ipsec.ike.ike3gpp.Ike3gppDeviceIdentityUtils; 26 27 import java.util.Objects; 28 29 /** 30 * Ike3gppParams is used to configure 3GPP-specific parameters to be used during an IKE Session. 31 * 32 * @see 3GPP ETSI TS 24.302: Access to the 3GPP Evolved Packet Core (EPC) via non-3GPP access 33 * networks 34 * @hide 35 */ 36 @SystemApi 37 public final class Ike3gppParams { 38 /** If the PDU Session ID is not set, it will be reported as 0. */ 39 // NoByteOrShort: using byte to be consistent with the PDU Session ID specification 40 @SuppressLint("NoByteOrShort") 41 public static final byte PDU_SESSION_ID_UNSET = 0; 42 43 private final byte mPduSessionId; 44 private final String mDeviceIdentity; 45 Ike3gppParams(byte pduSessionId, @Nullable String deviceIdentity)46 private Ike3gppParams(byte pduSessionId, @Nullable String deviceIdentity) { 47 mPduSessionId = pduSessionId; 48 mDeviceIdentity = deviceIdentity; 49 } 50 51 /** 52 * Retrieves the PDU Session ID for this Ike3gppParams. 53 * 54 * <p>If the PDU Session ID was not set and this method is called, {@link PDU_SESSION_ID_UNSET} 55 * will be returned. 56 */ 57 // NoByteOrShort: using byte to be consistent with the PDU Session ID specification 58 @SuppressLint("NoByteOrShort") getPduSessionId()59 public byte getPduSessionId() { 60 return mPduSessionId; 61 } 62 63 /** 64 * Returns true if the PDU Session ID is set for this instance. 65 * 66 * @hide 67 */ hasPduSessionId()68 public boolean hasPduSessionId() { 69 return mPduSessionId != PDU_SESSION_ID_UNSET; 70 } 71 72 /** 73 * Retrieves the Device Identity for this Ike3gppParams. 74 * 75 * <p>If the Device Identity was not set and this method is called, null is returned 76 */ getMobileDeviceIdentity()77 public @Nullable String getMobileDeviceIdentity() { 78 return mDeviceIdentity; 79 } 80 81 @Override hashCode()82 public int hashCode() { 83 return Objects.hash(mPduSessionId, mDeviceIdentity); 84 } 85 86 @Override equals(Object o)87 public boolean equals(Object o) { 88 if (this == o) return true; 89 if (!(o instanceof Ike3gppParams)) return false; 90 91 Ike3gppParams that = (Ike3gppParams) o; 92 return ((mPduSessionId == that.mPduSessionId) 93 && Objects.equals(mDeviceIdentity, that.mDeviceIdentity)); 94 } 95 96 @Override toString()97 public String toString() { 98 return new StringBuilder() 99 .append("Ike3gppParams={ ") 100 .append("mPduSessionId=") 101 .append(String.format("%02X ", mPduSessionId)) 102 .append("mDeviceIdentity=") 103 .append(IkeManager.getIkeLog().pii(String.format("%16s ", mDeviceIdentity))) 104 .append(" }") 105 .toString(); 106 } 107 108 /** This class can be used to incrementally construct an {@link Ike3gppParams}. */ 109 public static final class Builder { 110 private byte mPduSessionId = PDU_SESSION_ID_UNSET; 111 private String mDeviceIdentity; 112 113 /** 114 * Sets the PDU Session ID to be used for the 3GPP N1_MODE_CAPABILITY payload. 115 * 116 * <p>Setting the PDU Session ID will configure the IKE Session to notify the server that it 117 * supports N1_MODE. 118 * 119 * <p>{@link PDU_SESSION_ID_UNSET} will clear the previously-set PDU Session ID. 120 * 121 * @see TS 24.007 Section 11.2.3.1b for the definition of PDU Session ID encoding 122 * @see TS 24.302 Section 7.2.2 for context on PDU Session ID usage 123 * @see TS 24.302 Section 8.2.9.15 for a description of the N1_MODE_CAPABILITY payload 124 * @param pduSessionId the PDU Session ID value to be used in this IKE Session 125 * @return Builder this, to facilitate chaining 126 */ 127 // NoByteOrShort: using byte to be consistent with the PDU Session ID specification 128 @NonNull setPduSessionId(@uppressLint"NoByteOrShort") byte pduSessionId)129 public Builder setPduSessionId(@SuppressLint("NoByteOrShort") byte pduSessionId) { 130 mPduSessionId = pduSessionId; 131 return this; 132 } 133 134 /** 135 * Setting the device identity (IMEI or IMEISV) will enable Mobile Device Identity 136 * Signalling support. 137 * 138 * @see 3GPP 24.302 Section 7.2.6 for details of signaling exchange 139 * @param deviceIdentity String representing decimal digits of IMEI(SV). IMEI is 15 digits 140 * and IMEISV is 16 digits long as per spec 3GPP TS 23.300. The implementation infers 141 * the Identity Type (IMEI or IMEISV) based on the length of the deviceIdentity passed 142 * in. A string of invalid length will result in an exception during build(). If the 143 * device identity is set, EAP-AKA MUST be configured to be an acceptable auth method. 144 * Device identity can be unset by passing null. 145 * @return Builder this, to facilitate chaining 146 */ 147 @NonNull setMobileDeviceIdentity(@ullable String deviceIdentity)148 public Builder setMobileDeviceIdentity(@Nullable String deviceIdentity) { 149 mDeviceIdentity = deviceIdentity; 150 return this; 151 } 152 153 /** 154 * Validates and builds the {@link Ike3gppParams}. 155 * 156 * @return Ike3gppParams the validated Ike3gppParams 157 */ 158 @NonNull build()159 public Ike3gppParams build() { 160 if (mDeviceIdentity != null) { 161 if (!Ike3gppDeviceIdentityUtils.isValidDeviceIdentity(mDeviceIdentity)) { 162 throw new IllegalArgumentException( 163 "valid device identity should be 15 or 16 digits or set to null"); 164 } 165 } 166 return new Ike3gppParams(mPduSessionId, mDeviceIdentity); 167 } 168 } 169 } 170