001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package acl_data;
028:
029: import java.util.Vector;
030: import java.io.FileInputStream;
031: import java.io.IOException;
032: import java.io.InputStream;
033: import java.io.InputStreamReader;
034: import java.io.FileOutputStream;
035:
036: /**
037: * This class represents access control file that describes permissions for one
038: * card slot.
039: */
040: //public class ACFile implements ImplicitlyTrustedClass {
041: public class ACFile {
042:
043: /** Path to ODF. */
044: public static final short ODF = 0x5031;
045: /** Path to DODF. */
046: public static final short DODF = 0x5207;
047: /** Path to AODF. */
048: public static final short AODF = 0x5208;
049: /** Path to ACIFILE. */
050: public static final short ACIFILE = 0x5300;
051: /** Path to ACFILE. */
052: public static final short ACFILE = 0x5310;
053:
054: /**
055: * Value of OID from the spceification (A.4.2.1 Location of Access
056: * Control Files)
057: */
058: private byte[] ACIFOID = { 0x2b, 0x06, 0x01, 0x04, 0x01, 0x2a,
059: 0x02, 0x6e, 0x03, 0x01, 0x01, 0x01 };
060:
061: /** File name */
062: private static String fileName;
063: /** Output directory */
064: private static String OUT_DIR;
065: /** Generated files */
066: Vector files = new Vector();
067:
068: /**
069: * Constructs an instance of an access control file object.
070: */
071: public ACFile() {
072: }
073:
074: /**
075: * Load access control information.
076: * @param fileName file name.
077: * @param out_dir output directory.
078: * @return ACFile object
079: */
080: public static ACFile load(String fileName, String out_dir) {
081: InputStream permIS;
082: OUT_DIR = out_dir;
083: try {
084: permIS = new FileInputStream(fileName);
085: } catch (IOException e) {
086: System.err.println("Error during opening file " + e);
087: return null;
088: }
089:
090: try {
091: ACFile f = new ACFile();
092: f.init(new ACLFileReader(new InputStreamReader(permIS)));
093: return f;
094: } catch (Exception e) {
095: System.out.println("Error reading ACList " + e);
096: }
097: return null;
098: }
099:
100: /**
101: * The list of ACL objects.
102: */
103: private Vector ACLists = new Vector();
104: /**
105: * The list of PIN data objects.
106: */
107: private Vector PINAttrs = new Vector();
108:
109: /**
110: * Initializes ACF object.
111: * @param r reader for permissions file.
112: * @throws IOException if I/O error occurs.
113: */
114: private void init(ACLFileReader r) throws IOException {
115: short n = ACFILE;
116: ACList acl;
117: while (true) {
118: try {
119: String s = r.readWord();
120: if (s == null) {
121: break;
122: }
123: if (s.equals("acf")) {
124: ACLists.addElement(new ACList(r, n++));
125: } else if (s.equals("pin_data")) {
126: PINAttrs.addElement(new PINAttributes(r));
127: } else {
128: throw new Exception();
129: }
130:
131: } catch (Exception e) {
132: throw new IOException("Line " + r.lineNumber);
133: }
134: }
135: TLV acif = TLV.createSequence();
136: TLV current = acif.setChild(new TLV(TLV.NULL_TYPE));
137: for (short i = 0; i < ACLists.size(); i++) {
138: acl = (ACList) ACLists.elementAt(i);
139: current = current.setNext(acl.getACIFRec());
140: /* ACF creating */
141: Vector v = acl.getACEntries();
142: TLV acf = TLV.createSequence();
143: TLV acf_current = acf.setChild(new TLV(TLV.NULL_TYPE));
144: for (int j = 0; j < v.size(); j++) {
145: ACEntry ace = (ACEntry) v.elementAt(j);
146: acf_current = acf_current.setNext(ace.getACERec());
147: }
148: acf.child = acf.child.next;
149: TLV acf_p = TLV.createSequence();
150: acf_p.setChild(acf);
151: addFile(acl.getNum(), acf_p.child.getValue());
152: }
153: acif.child = acif.child.next;
154: acif.getValue();
155: addFile(ACIFILE, acif.getValue());
156:
157: TLV pattr = TLV.createSequence();
158: current = pattr.setChild(new TLV(TLV.NULL_TYPE));
159: PINAttributes pat;
160: for (int i = 0; i < PINAttrs.size(); i++) {
161: pat = (PINAttributes) PINAttrs.elementAt(i);
162: current = current.setNext(pat.getPINRec());
163: }
164: pattr.child = pattr.child.next;
165: pattr.getValue();
166: TLV pattr_p = TLV.createSequence();
167: pattr_p.setChild(pattr);
168: addFile(AODF, pattr.getValue());
169: }
170:
171: /**
172: * Adds a new file to the file system.
173: * @param path file path
174: * @param data file body
175: * @throws IOException in case if something wrong
176: */
177: void addFile(short path, byte[] data) throws IOException {
178: String sPath = OUT_DIR;
179: sPath += Integer.toHexString(path);
180: FileOutputStream fos;
181: try {
182: fos = new FileOutputStream(sPath);
183: fos.write(data, 0, data.length);
184: fos.close();
185: files.add(sPath);
186: } catch (IOException e) {
187: System.out.println("Add file error: " + e);
188: return;
189: }
190: }
191:
192: /**
193: * Creates ODF file
194: * @throws IOException in case if something wrong
195: */
196: void createODF() throws IOException {
197: TLV odfRec = TLV.createSequence(); // this SEQUENCE will be ignored by getValue()
198: TLV current;
199:
200: current = odfRec
201: .setChild(new TLV(ACEntry.CONTEXT_CONSTRUCTED_7)); // dataObjects [7] CHOICE {
202: current
203: .setChild(TLV.createSequence())
204: . // path SEQUENCE {
205: setChild(
206: TLV.createOctetString(Utils.shortToBytes(DODF))); // path
207: // } -- path
208: // } -- dataObjects
209:
210: current = current
211: .setNext(new TLV(ACEntry.CONTEXT_CONSTRUCTED_8)); // authObjects [8] CHOICE {
212: current
213: .setChild(TLV.createSequence())
214: . // path SEQUENCE {
215: setChild(
216: TLV.createOctetString(Utils.shortToBytes(AODF))); // path
217: // } -- path
218: // } -- authObjects
219:
220: addFile(ODF, odfRec.getValue());
221: }
222:
223: /**
224: * Creates DODF file
225: * @throws IOException in case if something wrong
226: */
227: void createDODF() throws IOException {
228: // DataType ::= CHOICE {
229: TLV dodfRec = TLV.createSequence().setTag(
230: ACEntry.CONTEXT_CONSTRUCTED_1); // [1] DataObject{OidDO} ::= SEQUENCE {
231: TLV current = dodfRec.setChild(TLV.createSequence()); // commonObjectAttributes SEQUENCE {}
232: current.setNext(TLV.createSequence()). // classAttributes SEQUENCE {
233: setChild(TLV.createUTF8String("ACF Converter")); // applicationName
234: // } -- classAttributes
235: current = current.next;
236: current.setNext(new TLV(ACEntry.CONTEXT_CONSTRUCTED_1)). // typeAttributes [1]
237: setChild(TLV.createSequence()). // SEQUENCE {
238: setChild(new TLV(TLV.OID_TYPE, ACIFOID)). // id
239: setNext(TLV.createSequence()). // value -> path SEQUENCE {
240: setChild(
241: TLV.createOctetString(Utils
242: .shortToBytes(ACIFILE))); // path
243: // } -- path
244: // } -- typeAttributes
245: // } -- DataObject
246: // } -- DataType
247:
248: addFile(DODF, dodfRec.getDERData());
249: }
250:
251: }
|