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: package com.sun.satsa.util.pkcs15;
027:
028: import com.sun.satsa.util.*;
029: import java.util.Vector;
030: import java.io.IOException;
031: import javax.microedition.io.ConnectionNotFoundException;
032:
033: /**
034: * This class represents the PKCS15 DIR file abstraction.
035: *<pre>
036: * It is assumed that the DIR file contains information about
037: * files in the following format:
038: * APP_CONSTRUCTED_1 { [APPLICATION 1]
039: * APP_PRIMITIVE_15 Aid (Octet_string)
040: * APP_PRIMITIVE_16 label (UTF8String / opt)
041: * APP_PRIMITIVE_17 path (octet_string)
042: * APP_PRIMITIVE_19 { DDO (ddo / opt)
043: * SEQUENCE {
044: * oid OBJECT IDENTIFIER,
045: * odfPath Path OPTIONAL,
046: * tokenInfoPath [0] Path OPTIONAL,
047: * unusedPath [1] Path OPTIONAL,
048: * }
049: * }
050: * }
051: * </pre>
052: */
053:
054: public class DIRF extends PKCS15File {
055: /** Wrong DIR file status code */
056: public static final int WRONG_DIR_FILE = -1;
057: /** Success status code */
058: public static final int SUCCESS = 0;
059: /** AID is not found status code */
060: public static final int AID_NOT_FOUND = 1;
061: /** Root is not selected status code */
062: public static final int ROOT_NOT_SELECTED = 2;
063:
064: /** The PKCS15 application AID (according to pkcs15 spec. */
065: byte[] PKCS_AID = { (byte) 0xa0, 0x0, 0x0, 0x0, 0x63, 0x50, 0x4b,
066: 0x43, 0x53, 0x2d, 0x31, 0x35 };
067: /** The PKCS15 DIR file name */
068: short[] DIR_FILE = { 0x2F00 };
069: /** ASN application specific flag used in types (0x40). */
070: public static final int APPLICATION = 0x40;
071: /** ASN application specific constructed flag used in types (0x60). */
072: public static final int APP_CONSTRUCTED_0 = APPLICATION
073: + TLV.CONSTRUCTED;
074: /** ASN application specific primitive flag */
075: public static final int APP_PRIMITIVE_0 = APPLICATION;
076: /** ASN application specific constructed flag used in types (0x60). */
077: public static final int APP_CONSTRUCTED_1 = APP_CONSTRUCTED_0 + 1;
078: /** ASN application specific flag for [APPLICATION 15] (aid) */
079: public static final int APP_PRIMITIVE_15 = APP_PRIMITIVE_0 + 0xf;
080: /** ASN application specific flag for [APPLICATION 16] (label) */
081: public static final int APP_PRIMITIVE_16 = APP_PRIMITIVE_0 + 0x10;
082: /** ASN application specific flag for [APPLICATION 17] (path) */
083: public static final int APP_PRIMITIVE_17 = APP_PRIMITIVE_0 + 0x11;
084: /** ASN application specific flag for [APPLICATION 19] (ddo) */
085: public static final int APP_CONSTRUCTED_19 = APP_CONSTRUCTED_0 + 0x13;
086:
087: /** This vector contains parsed objects from DIR */
088: private Vector DIR;
089:
090: /**
091: * Creates the DIRF object
092: * @param fs FileSystemAbstract file system that is used
093: * to reading from file.
094: */
095: public DIRF(FileSystemAbstract fs) {
096: super (fs);
097: }
098:
099: /**
100: * Reads DIR .
101: * @throws IOException if I/O error occurs
102: * @throws TLVException if TLV error occurs
103: */
104: public void load() throws IOException, TLVException {
105: DIR = new Vector();
106: resetLoader(DIR, null, null);
107: parseDF(DIR_FILE);
108: }
109:
110: /**
111: * Seeks record in the DIR file according to the pointed AID
112: * @param aid byte[] required AID
113: * @return TLV containing the required record
114: * @throws TLVException if TLV error occurs
115: */
116: private TLV getApp(byte[] aid) throws TLVException {
117: TLV root = (TLV) DIR.firstElement();
118: if (root.type != APP_CONSTRUCTED_1) {
119: throw new TLVException(
120: "The first tag shall be APPLICATION 1");
121: }
122: while (root != null) {
123: TLV t = root.child; /* APP_PRIMITIVE_15 */
124: if (t.type != APP_PRIMITIVE_15) {
125: throw new TLVException(
126: "The first tag shall be APPLICATION 15 (AID)");
127: }
128: if (Utils.byteMatch(aid, t.getValue())) {
129: return root;
130: }
131: root = root.next;
132: }
133: return null;
134: }
135:
136: /**
137: * Sets a root for the PKCS15 application
138: * @return int Status code.
139: */
140: public int setPKCSRoot() {
141: return setRoot(PKCS_AID);
142: }
143:
144: /**
145: * Sets a root for the application with the pointed AID
146: * @param aid byte[] required AID
147: * @return int Status code.
148: */
149: public int setRoot(byte[] aid) {
150: TLV root;
151: try {
152: root = getApp(aid);
153: } catch (TLVException e) {
154: return WRONG_DIR_FILE;
155: }
156: if (root == null) {
157: return AID_NOT_FOUND;
158: }
159: TLV t = root.child; /* AID */
160: /* Find the path item */
161: while ((t != null) && (t.type != APP_PRIMITIVE_17)) {
162: t = t.next;
163: }
164: if (t == null) {
165: return WRONG_DIR_FILE;
166: }
167: byte[] pth = t.getValue();
168: short[] path = new short[pth.length / 2];
169: for (int i = 0; i < pth.length; i = i + 2) {
170: path[i / 2] = Utils.getShort(pth, i);
171: }
172: try {
173: files.selectRoot(path);
174: } catch (IOException ie) {
175: return ROOT_NOT_SELECTED;
176: }
177: return SUCCESS;
178: }
179:
180: }
|