1 /*
2  * Copyright (C) 2017 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  * Copyright (c) 2017, The Linux Foundation.
18  */
19 
20 /*
21  * Copyright 2012 Giesecke & Devrient GmbH.
22  *
23  * Licensed under the Apache License, Version 2.0 (the "License");
24  * you may not use this file except in compliance with the License.
25  * You may obtain a copy of the License at
26  *
27  *      http://www.apache.org/licenses/LICENSE-2.0
28  *
29  * Unless required by applicable law or agreed to in writing, software
30  * distributed under the License is distributed on an "AS IS" BASIS,
31  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32  * See the License for the specific language governing permissions and
33  * limitations under the License.
34  */
35 package com.android.se.security.gpac;
36 
37 import java.io.ByteArrayOutputStream;
38 
39 /**
40  * NFC-AR-DO: In the NFC use case, mobile device application gather information from their
41  * associated card application using the SE access API. However, when the card application needs to
42  * trigger its associated mobile application, it sends an HCI EVT_TRANSACTION according to ETSI TS
43  * 102 622 [102 622] over SWP to the device. This event is handled by the NFC chipset stack which
44  * has to start the corresponding device application. Disclosure of this event to malicious
45  * applications can lead to phishing and denial of service attacks. To prevent this, it shall be
46  * possible to use the applications signature to authorize device applications to receive HCI events
47  * issued by the secure element application. An NFC event data object defines an access rule for
48  * generating NFC events for a specific terminal application. The NFC event access can be restricted
49  * by a rule based on an event access is NEVER/ ALWAYS allowed policy.
50  */
51 public class NFC_AR_DO extends BerTlv {
52 
53     public static final int TAG = 0xD1;
54 
55     private boolean mNfcAllowed = false;
56 
NFC_AR_DO(byte[] rawData, int valueIndex, int valueLength)57     public NFC_AR_DO(byte[] rawData, int valueIndex, int valueLength) {
58         super(rawData, TAG, valueIndex, valueLength);
59     }
60 
NFC_AR_DO(boolean allowed)61     public NFC_AR_DO(boolean allowed) {
62         super(null, TAG, 0, 0);
63         mNfcAllowed = allowed;
64     }
65 
isNfcAllowed()66     public boolean isNfcAllowed() {
67         return mNfcAllowed;
68     }
69 
70     @Override
71     /**
72      * Tag: D1 Length: 1 Value: Contains a NFC event access rule: NEVER (00): NFC event access is
73      * not
74      * allowed ALWAYS(01): NFC event access is allowed
75      */
interpret()76     public void interpret() throws ParserException {
77 
78         mNfcAllowed = false;
79 
80         byte[] data = getRawData();
81         int index = getValueIndex();
82 
83         if (index + getValueLength() > data.length) {
84             throw new ParserException("Not enough data for NFC_AR_DO!");
85         }
86 
87         if (getValueLength() != 1) {
88             throw new ParserException("Invalid length of NFC-AR-DO!");
89         } else if ((data[index] != 0x01) && (data[index] != 0x00)) {
90             throw new ParserException(
91                     "Invalid value of NFC-AR-DO : " + String.format("%02x", data[index] & 0xff));
92         }
93 
94         mNfcAllowed = (data[index] == 0x01);
95     }
96 
97     @Override
98     /**
99      * Tag: D1 Length: 1 Value: Contains a NFC event access rule: NEVER (00): NFC event access is
100      * not
101      * allowed ALWAYS(01): NFC event access is allowed
102      */
build(ByteArrayOutputStream stream)103     public void build(ByteArrayOutputStream stream) throws DO_Exception {
104 
105         // write tag
106         stream.write(getTag());
107         stream.write(0x01);
108         stream.write(mNfcAllowed ? 0x01 : 0x00);
109     }
110 }
111