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 com.android.internal.net.ipsec.ike.exceptions;
17 
18 import static android.net.ipsec.ike.exceptions.IkeProtocolException.ERROR_TYPE_INVALID_KE_PAYLOAD;
19 
20 import android.net.ipsec.ike.exceptions.IkeProtocolException;
21 
22 /**
23  * This exception is thrown when the received KE payload in the request is different from accepted
24  * Diffie-Hellman group.
25  *
26  * <p>Responder should include an INVALID_KE_PAYLOAD Notify payload in a response message for both
27  * IKE INI exchange and other SA negotiation exchanges after IKE is setup..
28  *
29  * @see <a href="https://tools.ietf.org/html/rfc7296#section-1.3">RFC 7296, Internet Key Exchange
30  *     Protocol Version 2 (IKEv2)</a>
31  */
32 public final class InvalidKeException extends IkeProtocolException {
33     private static final int EXPECTED_ERROR_DATA_LEN = 2;
34 
35     /**
36      * Construct an instance of InvalidKeException
37      *
38      * @param dhGroup the expected DH group
39      */
InvalidKeException(int dhGroup)40     public InvalidKeException(int dhGroup) {
41         super(ERROR_TYPE_INVALID_KE_PAYLOAD, integerToByteArray(dhGroup, EXPECTED_ERROR_DATA_LEN));
42     }
43 
44     /**
45      * Construct a instance of InvalidKeException from a notify payload.
46      *
47      * @param notifyData the notify data included in the payload.
48      */
InvalidKeException(byte[] notifyData)49     public InvalidKeException(byte[] notifyData) {
50         super(ERROR_TYPE_INVALID_KE_PAYLOAD, notifyData);
51     }
52 
53     /**
54      * Return the expected DH Group included in this exception.
55      *
56      * @return the expected DH Group.
57      */
getDhGroup()58     public int getDhGroup() {
59         return byteArrayToInteger(getErrorData());
60     }
61 
62     @Override
isValidDataLength(int dataLen)63     protected boolean isValidDataLength(int dataLen) {
64         return EXPECTED_ERROR_DATA_LEN == dataLen;
65     }
66 }
67