1 /* GnuRSAPublicKey.java --
2 Copyright 2001, 2002, 2003, 2006 Free Software Foundation, Inc.
4 This file is a part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or (at
9 your option) any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.java.security.key.rsa;
41 import gnu.java.lang.CPStringBuilder;
43 import gnu.java.security.Registry;
44 import gnu.java.security.action.GetPropertyAction;
45 import gnu.java.security.key.IKeyPairCodec;
47 import java.math.BigInteger;
48 import java.security.AccessController;
49 import java.security.PublicKey;
50 import java.security.interfaces.RSAPublicKey;
53 * An object that encapsulates an RSA public key.
58 * href="http://www.cosic.esat.kuleuven.ac.be/nessie/workshop/submissions/rsa-pss.zip">
59 * RSA-PSS Signature Scheme with Appendix, part B.</a><br>
60 * Primitive specification and supporting documentation.<br>
61 * Jakob Jonsson and Burt Kaliski.</li>
64 public class GnuRSAPublicKey
66 implements PublicKey, RSAPublicKey
68 /** String representation of this key. Cached for speed. */
69 private transient String str;
72 * Conveience constructor. Calls the constructor with 3 arguments passing
73 * {@link Registry#RAW_ENCODING_ID} as the identifier of the preferred
76 * @param n the modulus.
77 * @param e the public exponent.
79 public GnuRSAPublicKey(final BigInteger n, final BigInteger e)
81 this(Registry.RAW_ENCODING_ID, n, e);
85 * Constructs a new instance of <code>GnuRSAPublicKey</code> given the
86 * designated arguments.
88 * @param preferredFormat the identifier of the preferred encoding format to
89 * use when externalizing this key.
90 * @param n the modulus.
91 * @param e the public exponent.
93 public GnuRSAPublicKey(int preferredFormat, BigInteger n, BigInteger e)
95 super(preferredFormat == Registry.ASN1_ENCODING_ID ? Registry.X509_ENCODING_ID
101 * A class method that takes the output of the <code>encodePublicKey()</code>
102 * method of an RSA keypair codec object (an instance implementing
103 * {@link IKeyPairCodec} for RSA keys, and re-constructs an instance of this
106 * @param k the contents of a previously encoded instance of this object.
107 * @throws ArrayIndexOutOfBoundsException if there is not enough bytes, in
108 * <code>k</code>, to represent a valid encoding of an instance
110 * @throws IllegalArgumentException if the byte sequence does not represent a
111 * valid encoding of an instance of this object.
113 public static GnuRSAPublicKey valueOf(final byte[] k)
116 if (k[0] == Registry.MAGIC_RAW_RSA_PUBLIC_KEY[0])
119 return (GnuRSAPublicKey) new RSAKeyPairRawCodec().decodePublicKey(k);
121 catch (IllegalArgumentException ignored)
125 return (GnuRSAPublicKey) new RSAKeyPairX509Codec().decodePublicKey(k);
129 * Returns the encoded form of this public key according to the designated
132 * @param format the desired format identifier of the resulting encoding.
133 * @return the byte sequence encoding this key according to the designated
135 * @throws IllegalArgumentException if the format is not supported.
136 * @see RSAKeyPairRawCodec
138 public byte[] getEncoded(final int format)
143 case IKeyPairCodec.RAW_FORMAT:
144 result = new RSAKeyPairRawCodec().encodePublicKey(this);
146 case IKeyPairCodec.X509_FORMAT:
147 result = new RSAKeyPairX509Codec().encodePublicKey(this);
150 throw new IllegalArgumentException("Unsupported encoding format: "
157 * Returns <code>true</code> if the designated object is an instance of this
158 * class and has the same RSA parameter values as this one.
160 * @param obj the other non-null RSA key to compare to.
161 * @return <code>true</code> if the designated object is of the same type
162 * and value as this one.
164 public boolean equals(final Object obj)
169 if (! (obj instanceof RSAPublicKey))
172 final RSAPublicKey that = (RSAPublicKey) obj;
173 return super.equals(that)
174 && getPublicExponent().equals(that.getPublicExponent());
177 public String toString()
181 String ls = (String) AccessController.doPrivileged
182 (new GetPropertyAction("line.separator"));
183 str = new CPStringBuilder(this.getClass().getName()).append("(")
184 .append(super.toString()).append(",").append(ls)