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) 2015-2017, The Linux Foundation. 18 */ 19 20 /* 21 * Copyright (C) 2011 Deutsche Telekom, A.G. 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 36 /* 37 * Contributed by: Giesecke & Devrient GmbH. 38 */ 39 40 package com.android.se.security.arf.pkcs15; 41 42 import android.util.Log; 43 44 import com.android.se.internal.Util; 45 import com.android.se.security.arf.ASN1; 46 import com.android.se.security.arf.DERParser; 47 import com.android.se.security.arf.SecureElement; 48 import com.android.se.security.arf.SecureElementException; 49 50 import java.io.IOException; 51 import java.util.Arrays; 52 53 /** EF_ACMain related features */ 54 public class EFACMain extends EF { 55 56 public static final String TAG = "ACE ARF EF_ACMain"; 57 // Length of the "RefreshTag" 58 public static final short REFRESHTAG_LEN = 8; 59 60 // "EF Access Control Main" path 61 private byte[] mACMainPath = null; 62 63 /** 64 * Constructor 65 * 66 * @param secureElement SE on which ISO7816 commands are applied 67 */ EFACMain(SecureElement handle, byte[] path)68 public EFACMain(SecureElement handle, byte[] path) { 69 super(handle); 70 mACMainPath = path; 71 } 72 73 /** 74 * Decodes EF_ACMain file 75 * 76 * @param buffer ASN.1 data 77 * @return Path to "Access Control Rules" 78 */ decodeDER(byte[] buffer)79 private byte[] decodeDER(byte[] buffer) throws PKCS15Exception { 80 DERParser der = new DERParser(buffer); 81 der.parseTLV(ASN1.TAG_Sequence); 82 if (der.parseTLV(ASN1.TAG_OctetString) != REFRESHTAG_LEN) { 83 throw new PKCS15Exception("[Parser] RefreshTag length not valid"); 84 } 85 86 byte[] refreshTag = der.getTLVData(); 87 if (!Arrays.equals(refreshTag, this.mSEHandle.getRefreshTag())) { 88 mSEHandle.setRefreshTag(refreshTag); 89 return der.parsePathAttributes(); 90 } 91 return null; // RefreshTag not updated 92 } 93 94 /** 95 * Selects and Analyses EF_ACMain file 96 * 97 * @return Path to "EF_ACRules" if "RefreshTag" has been updated; <code>null</code> otherwise 98 */ analyseFile()99 public byte[] analyseFile() throws IOException, PKCS15Exception, SecureElementException { 100 Log.i(TAG, "Analysing EF_ACMain..."); 101 byte[] path = mACMainPath; 102 103 /* 104 // 2012-04-12 105 // extend path if ODF path was determined from EF DIR. 106 if( mSEHandle.getPKCS15Path() != null ) { 107 path = new byte[mSEHandle.getPKCS15Path().length + mACMainPath.length]; 108 System.arraycopy(mSEHandle.getPKCS15Path(), 0, path, 0, mSEHandle.getPKCS15Path().length); 109 System.arraycopy(mACMainPath, 0, path, mSEHandle.getPKCS15Path().length, mACMainPath 110 .length ); 111 } 112 //--- 113 * 114 */ 115 116 if (selectFile(path) != APDU_SUCCESS) { 117 throw new PKCS15Exception("EF_ACMain not found!"); 118 } 119 return decodeDER(readBinary(0, Util.END)); 120 } 121 } 122