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 
17 package com.android.internal.net.eap;
18 
19 import android.annotation.NonNull;
20 
21 import com.android.internal.annotations.VisibleForTesting;
22 import com.android.internal.net.eap.exceptions.InvalidEapResponseException;
23 import com.android.internal.net.eap.message.EapMessage;
24 
25 /**
26  * EapResult represents the return type R for a process operation within the EapStateMachine.
27  */
28 public abstract class EapResult {
29 
30     /**
31      * EapSuccess represents a success response from the EapStateMachine.
32      *
33      * @see <a href="https://tools.ietf.org/html/rfc3748">RFC 3748, Extensible Authentication
34      * Protocol (EAP)</a>
35      */
36     public static class EapSuccess extends EapResult {
37         public final byte[] msk;
38         public final byte[] emsk;
39 
EapSuccess(@onNull byte[] msk, @NonNull byte[] emsk)40         public EapSuccess(@NonNull byte[] msk, @NonNull byte[] emsk) {
41             if (msk == null || emsk == null) {
42                 throw new IllegalArgumentException("msk and emsk must not be null");
43             }
44             this.msk = msk;
45             this.emsk = emsk;
46         }
47     }
48 
49     /**
50      * EapFailure represents a failure response from the EapStateMachine.
51      *
52      * @see <a href="https://tools.ietf.org/html/rfc3748">RFC 3748, Extensible Authentication
53      * Protocol (EAP)</a>
54      */
55     public static class EapFailure extends EapResult {}
56 
57     /**
58      * EapResponse represents an outgoing message from the EapStateMachine.
59      *
60      * @see <a href="https://tools.ietf.org/html/rfc3748">RFC 3748, Extensible Authentication
61      * Protocol (EAP)</a>
62      */
63     public static class EapResponse extends EapResult {
64         public final byte[] packet;
65 
66         @VisibleForTesting
EapResponse(byte[] packet)67         protected EapResponse(byte[] packet) {
68             this.packet = packet;
69         }
70 
71         /**
72          * Constructs and returns an EapResult for the given EapMessage.
73          *
74          * <p>If the given EapMessage is not of type EAP-Response, an EapError object will be
75          * returned.
76          *
77          * @param message the EapMessage to be encoded in the EapResponse instance.
78          * @return an EapResponse instance for the given message. If message.eapCode != {@link
79          * EapMessage#EAP_CODE_RESPONSE}, an EapError instance is returned.
80          */
getEapResponse(@onNull EapMessage message)81         public static EapResult getEapResponse(@NonNull EapMessage message) {
82             if (message == null) {
83                 throw new IllegalArgumentException("EapMessage should not be null");
84             } else if (message.eapCode != EapMessage.EAP_CODE_RESPONSE) {
85                 return new EapError(new InvalidEapResponseException(
86                         "Cannot construct an EapResult from a non-EAP-Response message"));
87             }
88 
89             return new EapResponse(message.encode());
90         }
91     }
92 
93     /**
94      * EapError represents an error that occurred in the EapStateMachine.
95      *
96      * @see <a href="https://tools.ietf.org/html/rfc3748">RFC 3748, Extensible Authentication
97      * Protocol (EAP)</a>
98      */
99     public static class EapError extends EapResult {
100         public final Exception cause;
101 
102         /**
103          * Constructs an EapError instance for the given cause.
104          *
105          * @param cause the Exception that caused the EapError to be returned from the
106          *              EapStateMachine
107          */
EapError(Exception cause)108         public EapError(Exception cause) {
109             this.cause = cause;
110         }
111     }
112 }
113