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 import android.net.ipsec.ike.ChildSessionCallback;
20 import android.net.ipsec.ike.IkeSessionCallback;
21 
22 import com.android.internal.net.ipsec.ike.utils.IkeMetrics;
23 
24 /**
25  * This exception is thrown if any IKE message has a syntax error.
26  *
27  * <p>This exception indicates that the IKE message that was received was invalid because some type,
28  * length, or value was out of range or because the request was rejected for policy reasons.
29  *
30  * @see <a href="https://tools.ietf.org/html/rfc7296#section-3.10.1">RFC 7296, Internet Key Exchange
31  *     Protocol Version 2 (IKEv2)</a>
32  * @hide
33  */
34 // Include INVALID_SYNTAX Notify payload in an encrypted response message if current message is
35 // an encrypted request and cryptographic checksum is valid. Fatal error.
36 public final class InvalidSyntaxException extends IkeProtocolException {
37     private static final int EXPECTED_ERROR_DATA_LEN = 0;
38 
39     /**
40      * Construct an instance of InvalidSyntaxException.
41      *
42      * <p>Except for testing, IKE library users normally do not instantiate this object themselves
43      * but instead get a reference via {@link IkeSessionCallback} or {@link ChildSessionCallback}.
44      *
45      * @param message the descriptive message (which is saved for later retrieval by the {@link
46      *     #getMessage()} method).
47      */
InvalidSyntaxException(@onNull String message)48     public InvalidSyntaxException(@NonNull String message) {
49         super(ERROR_TYPE_INVALID_SYNTAX, message);
50     }
51 
52     /**
53      * Construct a instance of InvalidSyntaxException.
54      *
55      * <p>Except for testing, IKE library users normally do not instantiate this object themselves
56      * but instead get a reference via {@link IkeSessionCallback} or {@link ChildSessionCallback}.
57      *
58      * @param cause the cause (which is saved for later retrieval by the {@link #getCause()}
59      *     method).
60      */
InvalidSyntaxException(@onNull Throwable cause)61     public InvalidSyntaxException(@NonNull Throwable cause) {
62         super(ERROR_TYPE_INVALID_SYNTAX, cause);
63     }
64 
65     /**
66      * Construct a instance of InvalidSyntaxException.
67      *
68      * <p>Except for testing, IKE library users normally do not instantiate this object themselves
69      * but instead get a reference via {@link IkeSessionCallback} or {@link ChildSessionCallback}.
70      *
71      * @param message the descriptive message (which is saved for later retrieval by the {@link
72      *     #getMessage()} method).
73      * @param cause the cause (which is saved for later retrieval by the {@link #getCause()}
74      *     method).
75      */
InvalidSyntaxException(@onNull String message, @NonNull Throwable cause)76     public InvalidSyntaxException(@NonNull String message, @NonNull Throwable cause) {
77         super(ERROR_TYPE_INVALID_SYNTAX, message, cause);
78     }
79 
80     /**
81      * Construct a instance of InvalidSyntaxException from a notify payload.
82      *
83      * @param notifyData the notify data included in the payload.
84      * @hide
85      */
InvalidSyntaxException(byte[] notifyData)86     public InvalidSyntaxException(byte[] notifyData) {
87         super(ERROR_TYPE_INVALID_SYNTAX, notifyData);
88     }
89 
90     /** @hide */
91     @Override
isValidDataLength(int dataLen)92     protected boolean isValidDataLength(int dataLen) {
93         return EXPECTED_ERROR_DATA_LEN == dataLen;
94     }
95 
96     /**
97      * Returns the error code for metrics
98      *
99      * @hide
100      */
101     @Override
getMetricsErrorCode()102     public int getMetricsErrorCode() {
103         return IkeMetrics.IKE_ERROR_PROTOCOL_INVALID_SYNTAX;
104     }
105 }
106