001: /*
002: * Copyright 2003-2005 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.security.rsa;
027:
028: import java.io.IOException;
029: import java.math.BigInteger;
030:
031: import java.security.*;
032: import java.security.interfaces.*;
033:
034: import sun.security.util.*;
035: import sun.security.x509.X509Key;
036:
037: /**
038: * Key implementation for RSA public keys.
039: *
040: * Note: RSA keys must be at least 512 bits long
041: *
042: * @see RSAPrivateCrtKeyImpl
043: * @see RSAKeyFactory
044: *
045: * @since 1.5
046: * @version 1.10, 05/05/07
047: * @author Andreas Sterbenz
048: */
049: public final class RSAPublicKeyImpl extends X509Key implements
050: RSAPublicKey {
051:
052: private static final long serialVersionUID = 2644735423591199609L;
053:
054: private BigInteger n; // modulus
055: private BigInteger e; // public exponent
056:
057: /**
058: * Construct a key from its components. Used by the
059: * RSAKeyFactory and the RSAKeyPairGenerator.
060: */
061: public RSAPublicKeyImpl(BigInteger n, BigInteger e)
062: throws InvalidKeyException {
063: this .n = n;
064: this .e = e;
065: RSAKeyFactory.checkKeyLength(n);
066: // generate the encoding
067: algid = RSAPrivateCrtKeyImpl.rsaId;
068: try {
069: DerOutputStream out = new DerOutputStream();
070: out.putInteger(n);
071: out.putInteger(e);
072: DerValue val = new DerValue(DerValue.tag_Sequence, out
073: .toByteArray());
074: key = val.toByteArray();
075: } catch (IOException exc) {
076: // should never occur
077: throw new InvalidKeyException(exc);
078: }
079: }
080:
081: /**
082: * Construct a key from its encoding. Used by RSAKeyFactory.
083: */
084: public RSAPublicKeyImpl(byte[] encoded) throws InvalidKeyException {
085: decode(encoded);
086: RSAKeyFactory.checkKeyLength(n);
087: }
088:
089: // see JCA doc
090: public String getAlgorithm() {
091: return "RSA";
092: }
093:
094: // see JCA doc
095: public BigInteger getModulus() {
096: return n;
097: }
098:
099: // see JCA doc
100: public BigInteger getPublicExponent() {
101: return e;
102: }
103:
104: /**
105: * Parse the key. Called by X509Key.
106: */
107: protected void parseKeyBits() throws InvalidKeyException {
108: try {
109: DerInputStream in = new DerInputStream(key);
110: DerValue derValue = in.getDerValue();
111: if (derValue.tag != DerValue.tag_Sequence) {
112: throw new IOException("Not a SEQUENCE");
113: }
114: DerInputStream data = derValue.data;
115: n = RSAPrivateCrtKeyImpl.getBigInteger(data);
116: e = RSAPrivateCrtKeyImpl.getBigInteger(data);
117: if (derValue.data.available() != 0) {
118: throw new IOException("Extra data available");
119: }
120: } catch (IOException e) {
121: throw new InvalidKeyException("Invalid RSA public key", e);
122: }
123: }
124:
125: // return a string representation of this key for debugging
126: public String toString() {
127: return "Sun RSA public key, " + n.bitLength()
128: + " bits\n modulus: " + n + "\n public exponent: "
129: + e;
130: }
131:
132: protected Object writeReplace()
133: throws java.io.ObjectStreamException {
134: return new KeyRep(KeyRep.Type.PUBLIC, getAlgorithm(),
135: getFormat(), getEncoded());
136: }
137: }
|