2006-08-14 Mark Wielaard <mark@klomp.org>
[official-gcc.git] / libjava / classpath / gnu / java / security / key / rsa / GnuRSAPublicKey.java
blobfe28d0ba3b050e761a751ea441c1aee0d7d68bff
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
19 USA
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
24 combination.
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.security.Registry;
42 import gnu.java.security.action.GetPropertyAction;
43 import gnu.java.security.key.IKeyPairCodec;
45 import java.math.BigInteger;
46 import java.security.AccessController;
47 import java.security.PublicKey;
48 import java.security.interfaces.RSAPublicKey;
50 /**
51 * An object that encapsulates an RSA public key.
52 * <p>
53 * References:
54 * <ol>
55 * <li><a
56 * href="http://www.cosic.esat.kuleuven.ac.be/nessie/workshop/submissions/rsa-pss.zip">
57 * RSA-PSS Signature Scheme with Appendix, part B.</a><br>
58 * Primitive specification and supporting documentation.<br>
59 * Jakob Jonsson and Burt Kaliski.</li>
60 * </ol>
62 public class GnuRSAPublicKey
63 extends GnuRSAKey
64 implements PublicKey, RSAPublicKey
66 /** String representation of this key. Cached for speed. */
67 private transient String str;
69 /**
70 * Conveience constructor. Calls the constructor with 3 arguments passing
71 * {@link Registry#RAW_ENCODING_ID} as the identifier of the preferred
72 * encoding format.
74 * @param n the modulus.
75 * @param e the public exponent.
77 public GnuRSAPublicKey(final BigInteger n, final BigInteger e)
79 this(Registry.RAW_ENCODING_ID, n, e);
82 /**
83 * Constructs a new instance of <code>GnuRSAPublicKey</code> given the
84 * designated arguments.
86 * @param preferredFormat the identifier of the preferred encoding format to
87 * use when externalizing this key.
88 * @param n the modulus.
89 * @param e the public exponent.
91 public GnuRSAPublicKey(int preferredFormat, BigInteger n, BigInteger e)
93 super(preferredFormat == Registry.ASN1_ENCODING_ID ? Registry.X509_ENCODING_ID
94 : preferredFormat,
95 n, e);
98 /**
99 * A class method that takes the output of the <code>encodePublicKey()</code>
100 * method of an RSA keypair codec object (an instance implementing
101 * {@link IKeyPairCodec} for RSA keys, and re-constructs an instance of this
102 * object.
104 * @param k the contents of a previously encoded instance of this object.
105 * @throws ArrayIndexOutOfBoundsException if there is not enough bytes, in
106 * <code>k</code>, to represent a valid encoding of an instance
107 * of this object.
108 * @throws IllegalArgumentException if the byte sequence does not represent a
109 * valid encoding of an instance of this object.
111 public static GnuRSAPublicKey valueOf(final byte[] k)
113 // try RAW codec
114 if (k[0] == Registry.MAGIC_RAW_RSA_PUBLIC_KEY[0])
117 return (GnuRSAPublicKey) new RSAKeyPairRawCodec().decodePublicKey(k);
119 catch (IllegalArgumentException ignored)
122 // try X.509 codec
123 return (GnuRSAPublicKey) new RSAKeyPairX509Codec().decodePublicKey(k);
127 * Returns the encoded form of this public key according to the designated
128 * format.
130 * @param format the desired format identifier of the resulting encoding.
131 * @return the byte sequence encoding this key according to the designated
132 * format.
133 * @throws IllegalArgumentException if the format is not supported.
134 * @see RSAKeyPairRawCodec
136 public byte[] getEncoded(final int format)
138 final byte[] result;
139 switch (format)
141 case IKeyPairCodec.RAW_FORMAT:
142 result = new RSAKeyPairRawCodec().encodePublicKey(this);
143 break;
144 case IKeyPairCodec.X509_FORMAT:
145 result = new RSAKeyPairX509Codec().encodePublicKey(this);
146 break;
147 default:
148 throw new IllegalArgumentException("Unsupported encoding format: "
149 + format);
151 return result;
155 * Returns <code>true</code> if the designated object is an instance of this
156 * class and has the same RSA parameter values as this one.
158 * @param obj the other non-null RSA key to compare to.
159 * @return <code>true</code> if the designated object is of the same type
160 * and value as this one.
162 public boolean equals(final Object obj)
164 if (obj == null)
165 return false;
167 if (! (obj instanceof RSAPublicKey))
168 return false;
170 final RSAPublicKey that = (RSAPublicKey) obj;
171 return super.equals(that)
172 && getPublicExponent().equals(that.getPublicExponent());
175 public String toString()
177 if (str == null)
179 String ls = (String) AccessController.doPrivileged
180 (new GetPropertyAction("line.separator"));
181 str = new StringBuilder(this.getClass().getName()).append("(")
182 .append(super.toString()).append(",").append(ls)
183 .append(")")
184 .toString();
186 return str;