1 /* 2 * Copyright (C) 2018 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 package android.net.ipsec.ike.exceptions; 17 18 import android.annotation.NonNull; 19 20 import java.util.ArrayList; 21 import java.util.Collections; 22 import java.util.List; 23 import java.util.Objects; 24 25 /** 26 * This exception is thrown when payload type is not supported and critical bit is set 27 * 28 * @see <a href="https://tools.ietf.org/html/rfc7296#section-2.5">RFC 7296, Internet Key Exchange 29 * Protocol Version 2 (IKEv2)</a> 30 * @hide 31 */ 32 // Include UNSUPPORTED_CRITICAL_PAYLOAD Notify payloads in a response message. Each payload 33 // contains only one payload type. 34 public final class UnsupportedCriticalPayloadException extends IkeProtocolException { 35 private static final int EXPECTED_ERROR_DATA_LEN = 1; 36 37 private final List<Integer> mPayloadTypeList; 38 39 /** 40 * Construct an instance of UnsupportedCriticalPayloadException. 41 * 42 * <p>To keep IkeProtocolException simpler, we only pass the first payload type to the 43 * superclass which can be retrieved by users. 44 * 45 * @param payloadList the list of all unsupported critical payload types. 46 */ UnsupportedCriticalPayloadException(@onNull List<Integer> payloadList)47 public UnsupportedCriticalPayloadException(@NonNull List<Integer> payloadList) { 48 super( 49 ERROR_TYPE_UNSUPPORTED_CRITICAL_PAYLOAD, 50 integerToByteArray(payloadList.get(0), EXPECTED_ERROR_DATA_LEN)); 51 Objects.requireNonNull(payloadList, "payloadList is null"); 52 mPayloadTypeList = Collections.unmodifiableList(new ArrayList<>(payloadList)); 53 } 54 55 /** 56 * Construct a instance of UnsupportedCriticalPayloadException from a notify payload. 57 * 58 * @param notifyData the notify data included in the payload. 59 * @hide 60 */ UnsupportedCriticalPayloadException(byte[] notifyData)61 public UnsupportedCriticalPayloadException(byte[] notifyData) { 62 super(ERROR_TYPE_UNSUPPORTED_CRITICAL_PAYLOAD, notifyData); 63 mPayloadTypeList = Collections.singletonList(byteArrayToInteger(notifyData)); 64 } 65 66 /** 67 * Return the all the unsupported critical payloads included in this exception. 68 * 69 * @return the unsupported critical payload list. 70 */ 71 @NonNull getUnsupportedCriticalPayloadList()72 public List<Integer> getUnsupportedCriticalPayloadList() { 73 return Collections.unmodifiableList(mPayloadTypeList); 74 } 75 76 /** @hide */ 77 @Override isValidDataLength(int dataLen)78 protected boolean isValidDataLength(int dataLen) { 79 return EXPECTED_ERROR_DATA_LEN == dataLen; 80 } 81 } 82