001: /*
002: * @(#)RSAKey.java 1.7 02/07/24 @(#)
003: *
004: * Copyright (c) 2000-2001 Sun Microsystems, Inc. All rights reserved.
005: * PROPRIETARY/CONFIDENTIAL
006: * Use is subject to license terms.
007: */
008:
009: package com.sun.portal.ksecurity;
010:
011: /**
012: * Implements RSAKey with methods to set and get RSA exponent
013: * and modulus.
014: */
015: final class RSAKey implements RSAPublicKey, RSAPrivateKey {
016: /** Type of key, e.g. DES, RSA etc. */
017: byte kind;
018: /** Key size in bits, e.g. for RSA, this is modulus size. */
019: short bitsize;
020: /** Flag to indicate the key has been initialized. */
021: boolean initOk;
022: /** Local variable to hold the exponent. */
023: byte[] exp = null;
024: /**
025: * The number of bytes allocated to the modulus
026: * is always a multiple of 8.
027: */
028: byte[] mod = null;
029:
030: /**
031: * Constructor for RSA public or private key.
032: * @param type of key to process
033: * @param len length of the key
034: */
035: RSAKey(byte type, short len) {
036: kind = type;
037: bitsize = len;
038: initOk = false;
039: }
040:
041: /**
042: * Clear the current key.
043: */
044: public void clearKey() {
045: initOk = false;
046: bitsize = 0;
047: }
048:
049: /**
050: * Compare the current key as a RSA private key.
051: * @param k the key to compare
052: * @return true if the kesy are the same.
053: */
054: public boolean equals(RSAPrivateKey k) {
055: return equals((RSAKey) k);
056: }
057:
058: /**
059: * Compare the current key as a RSA public key.
060: * @param k the key to compare
061: * @return true if the keys are the same.
062: */
063: public boolean equals(RSAPublicKey k) {
064: return equals((RSAKey) k);
065: }
066:
067: /**
068: * Compare the current key as a generic RSA key.
069: * @param k the key to compare
070: * @return true if the keys are the same.
071: */
072: public boolean equals(RSAKey k) {
073: byte[] kexp = new byte[exp.length];
074: byte[] kmod = new byte[mod.length];
075:
076: if (kind != k.getType())
077: return false;
078: if (k.getExponent(kexp, (short) 0) != exp.length)
079: return false;
080: if (k.getModulus(kmod, (short) 0) != mod.length)
081: return false;
082: for (int i = 0; i < exp.length; i++)
083: if (kexp[i] != exp[i])
084: return false;
085: for (int i = 0; i < mod.length; i++)
086: if (kmod[i] != mod[i])
087: return false;
088: return true;
089: }
090:
091: /**
092: * Get the size of the current key.
093: * @return length of the key
094: */
095: public short getSize() {
096: // round up to closest multiple of 64
097: return (short) (((bitsize + 63) >>> 6) << 6);
098: // return bitsize;
099: }
100:
101: /**
102: * Get the type of the current key.
103: * @return the type of the key
104: */
105: public byte getType() {
106: return kind;
107: }
108:
109: /**
110: * Check if the key has been initialized.
111: * @return true if the key has been initialized.
112: */
113: public boolean isInitialized() {
114: return initOk;
115: }
116:
117: // The next four are for RSA public/private keys
118: /**
119: * Get the exponent for the key.
120: * @param buf output buffer to hold the exponent
121: * @param off offset in the buffer to copy the exponent
122: * @return length of the data copied
123: * @see #setExponent
124: */
125: public short getExponent(byte[] buf, short off) {
126: if (((kind != KeyBuilder.TYPE_RSA_PRIVATE) && (kind != KeyBuilder.TYPE_RSA_PUBLIC))
127: || !initOk)
128: return ((short) 0);
129:
130: if (off + exp.length > buf.length)
131: return 0;
132: System.arraycopy(exp, 0, buf, off, exp.length);
133: return ((short) exp.length);
134: }
135:
136: /**
137: * Get the modulus for the key.
138: * @param buf output buffer to hold the modulus
139: * @param off offset in the buffer to copy the modulus
140: * @return length of the data copied
141: * @see #setModulus
142: */
143: public short getModulus(byte[] buf, short off) {
144: if (((kind != KeyBuilder.TYPE_RSA_PRIVATE) && (kind != KeyBuilder.TYPE_RSA_PUBLIC))
145: || !initOk)
146: return ((short) 0);
147:
148: if (off + mod.length > buf.length)
149: return ((short) 0);
150: System.arraycopy(mod, 0, buf, off, mod.length);
151: return ((short) mod.length);
152: }
153:
154: /**
155: * Set the exponent for the key.
156: * @param buf input buffer which hold the exponent
157: * @param off offset in the buffer
158: * @param len length of the data to be copied
159: * @see #getExponent
160: */
161: public void setExponent(byte[] buf, short off, short len)
162: throws CryptoException {
163: if ((kind != KeyBuilder.TYPE_RSA_PRIVATE)
164: && (kind != KeyBuilder.TYPE_RSA_PUBLIC))
165: return;
166:
167: int cnt = getSize() >>> 3;
168: if (len > cnt)
169: throw new CryptoException(CryptoException.ILLEGAL_VALUE);
170:
171: exp = new byte[len];
172: System.arraycopy(buf, off, exp, 0, len);
173: if (mod != null)
174: initOk = true;
175: }
176:
177: /**
178: * Set the modulus for the key.
179: * @param buf input buffer which hold the modulus
180: * @param off offset in the buffer
181: * @param len length of the data to be copied
182: * @see #getModulus
183: */
184: public void setModulus(byte[] buf, short off, short len)
185: throws CryptoException {
186: if ((kind != KeyBuilder.TYPE_RSA_PRIVATE)
187: && (kind != KeyBuilder.TYPE_RSA_PUBLIC))
188: return;
189:
190: int cnt = getSize() >>> 3;
191: // System.out.println("bitsize is " + bitsize + " cnt is " + cnt);
192: if (len > cnt)
193: throw new CryptoException(CryptoException.ILLEGAL_VALUE);
194:
195: mod = new byte[cnt];
196: System.arraycopy(buf, off, mod, (cnt - len), len);
197: if (exp != null)
198: initOk = true;
199: }
200:
201: /**
202: * Convert the key to a readable string.
203: * @return string representation of key
204: */
205: public String toString() {
206: return ("["
207: + getSize()
208: + "-bit RSA "
209: + ((kind == KeyBuilder.TYPE_RSA_PUBLIC) ? "Public"
210: : "Private") + " key, Exponent: 0x"
211: + KeyBuilder.hexEncode(exp) + ", Modulus: 0x"
212: + KeyBuilder.hexEncode(mod) + "]");
213: }
214: }
|