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.SuppressLint; 19 import android.net.ipsec.ike.ChildSessionCallback; 20 import android.net.ipsec.ike.IkeSessionCallback; 21 22 /** 23 * This exception is thrown when major version of an inbound message is higher than 2. 24 * 25 * @see <a href="https://tools.ietf.org/html/rfc7296#section-2.5">RFC 7296, Internet Key Exchange 26 * Protocol Version 2 (IKEv2)</a> 27 */ 28 // Include INVALID_MAJOR_VERSION Notify payload in an unencrypted response message containing 29 // version number 2. 30 public final class InvalidMajorVersionException extends IkeProtocolException { 31 private static final int EXPECTED_ERROR_DATA_LEN = 1; 32 33 private final byte mVersion; 34 35 /** 36 * Construct a instance of InvalidMajorVersionException 37 * 38 * <p>Except for testing, IKE library users normally do not instantiate this object themselves 39 * but instead get a reference via {@link IkeSessionCallback} or {@link ChildSessionCallback}. 40 * 41 * @param version the major version in received packet 42 */ 43 // NoByteOrShort: using byte to be consistent with the Major Version specification InvalidMajorVersionException(@uppressLint"NoByteOrShort") byte version)44 public InvalidMajorVersionException(@SuppressLint("NoByteOrShort") byte version) { 45 super(ERROR_TYPE_INVALID_MAJOR_VERSION, new byte[] {version}); 46 mVersion = version; 47 } 48 49 /** 50 * Construct a instance of InvalidMajorVersionException from a notify payload. 51 * 52 * @param notifyData the notify data included in the payload. 53 * @hide 54 */ InvalidMajorVersionException(byte[] notifyData)55 public InvalidMajorVersionException(byte[] notifyData) { 56 super(ERROR_TYPE_INVALID_MAJOR_VERSION, notifyData); 57 mVersion = notifyData[0]; 58 } 59 60 /** 61 * Return the major version included in this exception. 62 * 63 * @return the major version 64 */ 65 // NoByteOrShort: using byte to be consistent with the Major Version specification 66 @SuppressLint("NoByteOrShort") getMajorVersion()67 public byte getMajorVersion() { 68 return mVersion; 69 } 70 71 /** @hide */ 72 @Override isValidDataLength(int dataLen)73 protected boolean isValidDataLength(int dataLen) { 74 return EXPECTED_ERROR_DATA_LEN == dataLen; 75 } 76 } 77