• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.message.ttls;
18 
19 import static com.android.internal.net.eap.EapAuthenticator.LOG;
20 
21 import android.annotation.IntDef;
22 
23 import com.android.internal.annotations.VisibleForTesting;
24 
25 import java.lang.annotation.Retention;
26 import java.lang.annotation.RetentionPolicy;
27 import java.nio.ByteBuffer;
28 
29 /** The inbound fragmentation helper is responsible for assembling fragmented EAP-TTLS data. */
30 public class EapTtlsInboundFragmentationHelper {
31     private static final String TAG = EapTtlsInboundFragmentationHelper.class.getSimpleName();
32 
33     @Retention(RetentionPolicy.SOURCE)
34     @IntDef({
35         FRAGMENTATION_STATUS_ACK,
36         FRAGMENTATION_STATUS_ASSEMBLED,
37         FRAGMENTATION_STATUS_INVALID
38     })
39     public @interface FragmentationStatus {}
40 
41     // ACK indicates that an inbound fragment has been processed and an ACK should be sent
42     public static final int FRAGMENTATION_STATUS_ACK = 0;
43     // ASSEMBLED indicates that fragments have been reasembeled and data can now be processed
44     public static final int FRAGMENTATION_STATUS_ASSEMBLED = 1;
45     // INVALID indicates some kind of failure likely due to an unexpected request or invalid data
46     public static final int FRAGMENTATION_STATUS_INVALID = 2;
47 
48     @VisibleForTesting public boolean mIsAwaitingFragments = false;
49     @VisibleForTesting public ByteBuffer mFragmentedData;
50 
51     /**
52      * This method is responsible for processing incoming fragmented data (RFC5281#9.2.2)
53      *
54      * @param typeData the type data to process
55      * @return a fragmentation status indicating the result of the process
56      */
assembleInboundMessage(EapTtlsTypeData typeData)57     public @FragmentationStatus int assembleInboundMessage(EapTtlsTypeData typeData) {
58         if (!mIsAwaitingFragments) {
59             if (typeData.isDataFragmented) {
60                 mIsAwaitingFragments = true;
61                 mFragmentedData = ByteBuffer.allocate(typeData.messageLength);
62             } else {
63                 // If there is no fragmentation, simply return the full data in a byte array
64                 mFragmentedData = ByteBuffer.wrap(typeData.data);
65                 return FRAGMENTATION_STATUS_ASSEMBLED;
66             }
67         } else if (typeData.isLengthIncluded) {
68             // the length bit MUST only be set on the first packet for a fragmented packet
69             // (RFC5281#9.2.2)
70             LOG.e(
71                     TAG,
72                     "Fragmentation failure: Received a second or greater fragmented request"
73                             + " with the length bit set.");
74             return FRAGMENTATION_STATUS_INVALID;
75         }
76 
77         if (typeData.data.length > mFragmentedData.remaining()) {
78             LOG.e(
79                     TAG,
80                     "Fragmentation failure: Received more data then declared and failed to"
81                             + " reassemble fragment.");
82             return FRAGMENTATION_STATUS_INVALID;
83         }
84 
85         mFragmentedData.put(typeData.data);
86 
87         if (typeData.isDataFragmented) {
88             return FRAGMENTATION_STATUS_ACK;
89         }
90 
91         LOG.d(TAG, "Successfully assembled a fragment.");
92         mIsAwaitingFragments = false;
93         return FRAGMENTATION_STATUS_ASSEMBLED;
94     }
95 
96     /**
97      * Retrieves assembled inbound fragmented data
98      *
99      * @return a byte array containing an assembled inbound fragment
100      */
getAssembledInboundFragment()101     public byte[] getAssembledInboundFragment() {
102         return mFragmentedData.array();
103     }
104 
105     /**
106      * Indicates whether a fragmentation session is currently in progress
107      *
108      * @return true if fragmentation is in progress
109      */
isAwaitingFragments()110     public boolean isAwaitingFragments() {
111         return mIsAwaitingFragments;
112     }
113 }
114