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 import java.io.IOException; 39 40 /** 41 * This class represents the Access rule data object (AR-DO), according to GP Secure Element Control 42 * Access. 43 * 44 * <p>The AR-DO contains one or two access rules of type APDU or NFC. 45 */ 46 public class AR_DO extends BerTlv { 47 48 public static final int TAG = 0xE3; 49 50 private APDU_AR_DO mApduAr = null; 51 private NFC_AR_DO mNfcAr = null; 52 53 public AR_DO(byte[] rawData, int valueIndex, int valueLength) { 54 super(rawData, TAG, valueIndex, valueLength); 55 } 56 57 public AR_DO(APDU_AR_DO apduArDo, NFC_AR_DO nfcArDo) { 58 super(null, TAG, 0, 0); 59 mApduAr = apduArDo; 60 mNfcAr = nfcArDo; 61 } 62 63 public APDU_AR_DO getApduArDo() { 64 return mApduAr; 65 } 66 67 public NFC_AR_DO getNfcArDo() { 68 return mNfcAr; 69 } 70 71 @Override 72 /** 73 * Interpret value. 74 * 75 * <p>Tag: E3 76 * 77 * <p>Value: Value can contain APDU-AR-DO or NFC-AR-DO or APDU-AR-DO | NFC-AR-DO A concatenation 78 * of one or two AR-DO(s). If two AR-DO(s) are present these must have different types. 79 */ 80 public void interpret() throws ParserException { 81 82 this.mApduAr = null; 83 this.mNfcAr = null; 84 85 byte[] data = getRawData(); 86 int index = getValueIndex(); 87 88 if (index + getValueLength() > data.length) { 89 throw new ParserException("Not enough data for AR_DO!"); 90 } 91 92 do { 93 BerTlv temp = BerTlv.decode(data, index); 94 95 if (temp.getTag() == APDU_AR_DO.TAG) { // APDU-AR-DO 96 mApduAr = new APDU_AR_DO(data, temp.getValueIndex(), temp.getValueLength()); 97 mApduAr.interpret(); 98 } else if (temp.getTag() == NFC_AR_DO.TAG) { // NFC-AR-DO 99 mNfcAr = new NFC_AR_DO(data, temp.getValueIndex(), temp.getValueLength()); 100 mNfcAr.interpret(); 101 } else { 102 // un-comment following line if a more restrictive 103 // behavior is necessary. 104 // throw new ParserException("Invalid DO in AR-DO!"); 105 } 106 index = temp.getValueIndex() + temp.getValueLength(); 107 } while (getValueIndex() + getValueLength() > index); 108 109 if (mApduAr == null && mNfcAr == null) { 110 throw new ParserException("No valid DO in AR-DO!"); 111 } 112 } 113 114 @Override 115 /** 116 * Interpret value. 117 * 118 * <p>Tag: E3 119 * 120 * <p>Value: Value can contain APDU-AR-DO or NFC-AR-DO or APDU-AR-DO | NFC-AR-DO A concatenation 121 * of one or two AR-DO(s). If two AR-DO(s) are present these must have different types. 122 */ 123 public void build(ByteArrayOutputStream stream) throws DO_Exception { 124 125 // write tag 126 stream.write(getTag()); 127 128 ByteArrayOutputStream temp = new ByteArrayOutputStream(); 129 if (mApduAr != null) { 130 mApduAr.build(temp); 131 } 132 133 if (mNfcAr != null) { 134 mNfcAr.build(temp); 135 } 136 137 BerTlv.encodeLength(temp.size(), stream); 138 try { 139 stream.write(temp.toByteArray()); 140 } catch (IOException e) { 141 throw new DO_Exception("AR-DO Memory IO problem! " + e.getMessage()); 142 } 143 } 144 } 145