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 com.sun.satsa.acl;
028:
029: import com.sun.midp.ssl.MessageDigest;
030: import com.sun.midp.io.j2me.apdu.APDUManager;
031:
032: /**
033: * This class represents access control information manager.
034: */
035: public class AccessControlManager {
036:
037: /** Access control information for card slots. */
038: private static ACSlot[] ACLInfo;
039:
040: /**
041: * Initialise ACL information.
042: */
043: private synchronized static void init() {
044: if (ACLInfo != null) {
045: return;
046: }
047: int maxSlot = APDUManager.getSlotCount();
048: ACLInfo = new ACSlot[maxSlot];
049: for (int i = 0; i < maxSlot; i++) {
050: ACLInfo[i] = ACSlot.load(i);
051: }
052: }
053:
054: /**
055: * Initialize ACL information.
056: * @param slot int the slot number.
057: */
058: public synchronized static void init(int slot) {
059: if (ACLInfo == null) {
060: init();
061: }
062: if (ACLInfo != null) {
063: ACLInfo[slot] = ACSlot.load(slot);
064: }
065: }
066:
067: /**
068: * SHA-1 message digest object.
069: */
070: private static MessageDigest sha;
071:
072: /**
073: * Synchronization object for message digest calculation.
074: */
075: private static Object shaSync = new Object();
076:
077: /**
078: * Calculates hash value.
079: * @param inBuf data buffer.
080: * @param inOff offset of data in the buffer.
081: * @param inLen length of data.
082: * @return array containing SHA-1 hash.
083: */
084: public static byte[] getHash(byte[] inBuf, int inOff, int inLen) {
085:
086: synchronized (shaSync) {
087:
088: try {
089: if (sha == null) {
090: sha = MessageDigest.getInstance(
091: MessageDigest.ALG_SHA, false);
092: }
093: sha.reset();
094: byte[] hash = new byte[20];
095: sha.doFinal(inBuf, inOff, inLen, hash, 0);
096: return hash;
097: } catch (Exception e) {
098: e.printStackTrace();
099: return null;
100: }
101: }
102: }
103:
104: /**
105: * Returns object that should be used for access control verification.
106: * @param slot slot number.
107: * @param selectAPDU SELECT APDU command data.
108: * @param root name of CA that authorized the suite.
109: * @return object that can be used to check permissions.
110: */
111: public static APDUPermissions getAPDUPermissions(int slot,
112: byte[] selectAPDU, String root) {
113: if (ACLInfo == null || ACLInfo[slot] == null) {
114: APDUPermissions perm;
115: perm = new APDUPermissions(null);
116: perm.setType(ACLPermissions.DISALLOW);
117: return perm;
118: }
119:
120: return (APDUPermissions) ACLInfo[slot].getACLPermissions(true,
121: selectAPDU, root);
122: }
123:
124: /**
125: * Returns object that should be used for access control verification.
126: * @param slot slot number.
127: * @param selectAPDU SELECT APDU command data.
128: * @param root name of CA that authorized the suite.
129: * @return object that can be used to check permissions.
130: */
131: public static JCRMIPermissions getJCRMIPermissions(int slot,
132: byte[] selectAPDU, String root) {
133: if (ACLInfo == null || ACLInfo[slot] == null) {
134: JCRMIPermissions perm = new JCRMIPermissions(null);
135: perm.setType(ACLPermissions.DISALLOW);
136: return perm;
137: }
138:
139: return (JCRMIPermissions) ACLInfo[slot].getACLPermissions(
140: false, selectAPDU, root);
141: }
142: }
|