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.midp.publickeystore;
028:
029: import java.io.*;
030:
031: /** The information that needs to be stored for a public key. */
032: public class PublicKeyInfo {
033: /** Used to tag the owner field in a serialized key. */
034: public static final byte OWNER_TAG = 1;
035:
036: /** Used to tag the notBefore field in a serialized key. */
037: public static final byte NOT_BEFORE_TAG = 2;
038:
039: /** Used to tag the notAfter field in a serialized key. */
040: public static final byte NOT_AFTER_TAG = 3;
041:
042: /** Used to tag the modulus field in a serialized key. */
043: public static final byte MODULUS_TAG = 4;
044:
045: /** Used to tag the exponent field in a serialized key. */
046: public static final byte EXPONENT_TAG = 5;
047:
048: /** Used to get the domain field in a serialized key. */
049: public static final byte DOMAIN_TAG = 6;
050:
051: /** Used to get the enable field in a serialized key. */
052: public static final byte ENABLED_TAG = 7;
053:
054: /** Distinguished Name of the owner. */
055: private String owner;
056:
057: /**
058: * Start of the key's validity period in milliseconds since Jan 1, 1970.
059: */
060: private long notBefore;
061:
062: /** End of the key's validity period in milliseconds since Jan 1, 1970. */
063: private long notAfter;
064:
065: /** RSA modulus for the public key. */
066: private byte[] modulus;
067:
068: /** RSA exponent for the public key. */
069: private byte[] exponent;
070:
071: /** Name of the security domain. */
072: private String domain;
073:
074: /** If this key can be used for authorization. */
075: boolean enabled;
076:
077: /**
078: * Deserializes a public key from storage.
079: * @param storage what to get the key from
080: * @return a full populated PublicKeyInfo object
081: * @exception IOException if the key storage was corrupted
082: */
083: static PublicKeyInfo getKeyFromStorage(InputStorage storage)
084: throws IOException {
085: byte[] tag;
086: Object value;
087: String owner;
088: long notBefore;
089: long notAfter;
090: byte[] modulus;
091: byte[] exponent;
092: String domain;
093: boolean enabled;
094:
095: tag = new byte[1];
096:
097: value = storage.readValue(tag);
098: if (value == null) {
099: // no more keys
100: return null;
101: }
102:
103: if (tag[0] != OWNER_TAG) {
104: throw new IOException("public key storage corrupted");
105: }
106:
107: owner = (String) value;
108:
109: value = storage.readValue(tag);
110: if (tag[0] != NOT_BEFORE_TAG) {
111: throw new IOException("public key storage corrupted");
112: }
113:
114: notBefore = ((Long) value).longValue();
115:
116: value = storage.readValue(tag);
117: if (tag[0] != NOT_AFTER_TAG) {
118: throw new IOException("public key storage corrupted");
119: }
120:
121: notAfter = ((Long) value).longValue();
122:
123: value = storage.readValue(tag);
124: if (tag[0] != MODULUS_TAG) {
125: throw new IOException("public key storage corrupted");
126: }
127:
128: modulus = (byte[]) value;
129:
130: value = storage.readValue(tag);
131: if (tag[0] != EXPONENT_TAG) {
132: throw new IOException("public key storage corrupted");
133: }
134:
135: exponent = (byte[]) value;
136:
137: value = storage.readValue(tag);
138: if (tag[0] != DOMAIN_TAG) {
139: throw new IOException("public key storage corrupted");
140: }
141:
142: domain = (String) value;
143:
144: value = storage.readValue(tag);
145: if (tag[0] != ENABLED_TAG) {
146: throw new IOException("public key storage corrupted");
147: }
148:
149: enabled = ((Boolean) value).booleanValue();
150:
151: return new PublicKeyInfo(owner, notBefore, notAfter, modulus,
152: exponent, domain, enabled);
153: }
154:
155: /**
156: * Constructs a PublicKeyInfo object with the specified attributes.
157: * This constructor is only used by PublicKeyInfo and its subclasses.
158: * @param owner distinguished name of the owner
159: * @param notBefore start of validity period expressed in milliseconds
160: * since midnight Jan 1, 1970 UTC
161: * @param notAfter end of validity period expressed as above
162: * @param modulus modulus associated with the RSA Public Key
163: * @param exponent exponent associated with the RSA Public Key
164: * @param domain security domain of any application authorized
165: * with the corresponding private key, this can be
166: * set to null, allowing it to be set later
167: * @param enabled if true this key is enable, if false the user has
168: * disabled this key for authorization
169: */
170: public PublicKeyInfo(String owner, long notBefore, long notAfter,
171: byte[] modulus, byte[] exponent, String domain,
172: boolean enabled) {
173: this .owner = owner;
174: this .notBefore = notBefore;
175: this .notAfter = notAfter;
176: this .modulus = modulus;
177: this .exponent = exponent;
178: this .domain = domain;
179: this .enabled = enabled;
180: }
181:
182: /**
183: * Constructs a PublicKeyInfo object with the specified attributes.
184: * This constructor is only used by PublicKeyInfo and its subclasses.
185: * Defaults enabled to true.
186: *
187: * @param owner distinguished name of the owner
188: * @param notBefore start of validity period expressed in milliseconds
189: * since midnight Jan 1, 1970 UTC
190: * @param notAfter end of validity period expressed as above
191: * @param modulus modulus associated with the RSA Public Key
192: * @param exponent exponent associated with the RSA Public Key
193: * @param domain security domain of any application authorized
194: * with the corresponding private key, this can be
195: * set to null, allowing it to be set later
196: */
197: public PublicKeyInfo(String owner, long notBefore, long notAfter,
198: byte[] modulus, byte[] exponent, String domain) {
199:
200: this (owner, notBefore, notAfter, modulus, exponent, domain,
201: true);
202: }
203:
204: /**
205: * Gets the distinguished name of the key's owner.
206: * @return name of key's owner
207: */
208: public String getOwner() {
209: return owner;
210: }
211:
212: /**
213: * Gets the start of the key's validity period in
214: * milliseconds since Jan 1, 1970.
215: * @return start of a key's validity period.
216: */
217: public long getNotBefore() {
218: return notBefore;
219: }
220:
221: /**
222: * Gets the end of the key's validity period in
223: * milliseconds since Jan 1, 1970.
224: * @return end of a key's validity period.
225: */
226: public long getNotAfter() {
227: return notAfter;
228: }
229:
230: /**
231: * Gets RSA modulus of the public key.
232: * @return the modulus
233: */
234: public byte[] getModulus() {
235: byte[] retVal = new byte[modulus.length];
236:
237: System.arraycopy(modulus, 0, retVal, 0, modulus.length);
238: return retVal;
239: }
240:
241: /**
242: * Gets RSA exponent of the public key.
243: * @return the exponent
244: */
245: public byte[] getExponent() {
246: byte[] retVal = new byte[exponent.length];
247:
248: System.arraycopy(exponent, 0, retVal, 0, exponent.length);
249: return retVal;
250: }
251:
252: /**
253: * Gets name of the security domain for this key.
254: * @return the security domain
255: * @see #setDomain
256: */
257: public String getDomain() {
258: if (domain == null) {
259: return "untrusted";
260: }
261:
262: return domain;
263: }
264:
265: /**
266: * Sets the name of the security domain for this key if it does not have
267: * a domain.
268: * @param domain security domain
269: * @see #getDomain
270: */
271: public void setDomain(String domain) {
272: if (domain != null) {
273: return;
274: }
275:
276: this .domain = domain;
277: }
278:
279: /**
280: * Gets the enabled status of this key.
281: *
282: * @return true if this key is enabled
283: */
284: public boolean isEnabled() {
285: return enabled;
286: }
287: }
|