1 /*
2  * Copyright (C) 2019 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.net.ipsec.ike.ChildSessionCallback;
19 import android.net.ipsec.ike.IkeSessionCallback;
20 
21 import com.android.internal.net.ipsec.ike.utils.IkeMetrics;
22 
23 /**
24  * This exception is thrown when the remote server received a message with out-of-window-size ID.
25  *
26  * @see <a href="https://tools.ietf.org/html/rfc7296#section-2.3">RFC 7296, Internet Key Exchange
27  *     Protocol Version 2 (IKEv2)</a>
28  * @hide
29  */
30 // Notifications based on this exception contains the four-octet invalid message ID. It MUST only
31 // ever be sent in an INFORMATIONAL request. Sending this notification is OPTIONAL, and
32 // notifications of this type MUST be rate limited.
33 public final class InvalidMessageIdException extends IkeProtocolException {
34     private static final int EXPECTED_ERROR_DATA_LEN = 4;
35 
36     /**
37      * Construct a instance of InvalidMessageIdException
38      *
39      * <p>Except for testing, IKE library users normally do not instantiate this object themselves
40      * but instead get a reference via {@link IkeSessionCallback} or {@link ChildSessionCallback}.
41      *
42      * @param messageId the invalid Message ID.
43      */
InvalidMessageIdException(int messageId)44     public InvalidMessageIdException(int messageId) {
45         super(
46                 ERROR_TYPE_INVALID_MESSAGE_ID,
47                 integerToByteArray(messageId, EXPECTED_ERROR_DATA_LEN));
48     }
49 
50     /**
51      * Construct a instance of InvalidMessageIdException from a notify payload.
52      *
53      * @param notifyData the notify data included in the payload.
54      * @hide
55      */
InvalidMessageIdException(byte[] notifyData)56     public InvalidMessageIdException(byte[] notifyData) {
57         super(ERROR_TYPE_INVALID_MESSAGE_ID, notifyData);
58     }
59 
60     /**
61      * Return the invalid message ID included in this exception.
62      *
63      * @return the message ID.
64      */
getMessageId()65     public int getMessageId() {
66         return byteArrayToInteger(getErrorData());
67     }
68 
69     /** @hide */
70     @Override
isValidDataLength(int dataLen)71     protected boolean isValidDataLength(int dataLen) {
72         return EXPECTED_ERROR_DATA_LEN == dataLen;
73     }
74 
75     /**
76      * Returns the error code for metrics
77      *
78      * @hide
79      */
80     @Override
getMetricsErrorCode()81     public int getMetricsErrorCode() {
82         return IkeMetrics.IKE_ERROR_PROTOCOL_INVALID_MESSAGE_ID;
83     }
84 }
85