| 1 |
769 |
jeremybenn |
/* GnuRSAPublicKey.java --
|
| 2 |
|
|
Copyright 2001, 2002, 2003, 2006 Free Software Foundation, Inc.
|
| 3 |
|
|
|
| 4 |
|
|
This file is a part of GNU Classpath.
|
| 5 |
|
|
|
| 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.
|
| 10 |
|
|
|
| 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.
|
| 15 |
|
|
|
| 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
|
| 20 |
|
|
|
| 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.
|
| 25 |
|
|
|
| 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. */
|
| 37 |
|
|
|
| 38 |
|
|
|
| 39 |
|
|
package gnu.java.security.key.rsa;
|
| 40 |
|
|
|
| 41 |
|
|
import gnu.java.lang.CPStringBuilder;
|
| 42 |
|
|
|
| 43 |
|
|
import gnu.java.security.Registry;
|
| 44 |
|
|
import gnu.java.security.action.GetPropertyAction;
|
| 45 |
|
|
import gnu.java.security.key.IKeyPairCodec;
|
| 46 |
|
|
|
| 47 |
|
|
import java.math.BigInteger;
|
| 48 |
|
|
import java.security.AccessController;
|
| 49 |
|
|
import java.security.PublicKey;
|
| 50 |
|
|
import java.security.interfaces.RSAPublicKey;
|
| 51 |
|
|
|
| 52 |
|
|
/**
|
| 53 |
|
|
* An object that encapsulates an RSA public key.
|
| 54 |
|
|
* <p>
|
| 55 |
|
|
* References:
|
| 56 |
|
|
* <ol>
|
| 57 |
|
|
* <li><a
|
| 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>
|
| 62 |
|
|
* </ol>
|
| 63 |
|
|
*/
|
| 64 |
|
|
public class GnuRSAPublicKey
|
| 65 |
|
|
extends GnuRSAKey
|
| 66 |
|
|
implements PublicKey, RSAPublicKey
|
| 67 |
|
|
{
|
| 68 |
|
|
/** String representation of this key. Cached for speed. */
|
| 69 |
|
|
private transient String str;
|
| 70 |
|
|
|
| 71 |
|
|
/**
|
| 72 |
|
|
* Conveience constructor. Calls the constructor with 3 arguments passing
|
| 73 |
|
|
* {@link Registry#RAW_ENCODING_ID} as the identifier of the preferred
|
| 74 |
|
|
* encoding format.
|
| 75 |
|
|
*
|
| 76 |
|
|
* @param n the modulus.
|
| 77 |
|
|
* @param e the public exponent.
|
| 78 |
|
|
*/
|
| 79 |
|
|
public GnuRSAPublicKey(final BigInteger n, final BigInteger e)
|
| 80 |
|
|
{
|
| 81 |
|
|
this(Registry.RAW_ENCODING_ID, n, e);
|
| 82 |
|
|
}
|
| 83 |
|
|
|
| 84 |
|
|
/**
|
| 85 |
|
|
* Constructs a new instance of <code>GnuRSAPublicKey</code> given the
|
| 86 |
|
|
* designated arguments.
|
| 87 |
|
|
*
|
| 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.
|
| 92 |
|
|
*/
|
| 93 |
|
|
public GnuRSAPublicKey(int preferredFormat, BigInteger n, BigInteger e)
|
| 94 |
|
|
{
|
| 95 |
|
|
super(preferredFormat == Registry.ASN1_ENCODING_ID ? Registry.X509_ENCODING_ID
|
| 96 |
|
|
: preferredFormat,
|
| 97 |
|
|
n, e);
|
| 98 |
|
|
}
|
| 99 |
|
|
|
| 100 |
|
|
/**
|
| 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
|
| 104 |
|
|
* object.
|
| 105 |
|
|
*
|
| 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
|
| 109 |
|
|
* of this object.
|
| 110 |
|
|
* @throws IllegalArgumentException if the byte sequence does not represent a
|
| 111 |
|
|
* valid encoding of an instance of this object.
|
| 112 |
|
|
*/
|
| 113 |
|
|
public static GnuRSAPublicKey valueOf(final byte[] k)
|
| 114 |
|
|
{
|
| 115 |
|
|
// try RAW codec
|
| 116 |
|
|
if (k[0] == Registry.MAGIC_RAW_RSA_PUBLIC_KEY[0])
|
| 117 |
|
|
try
|
| 118 |
|
|
{
|
| 119 |
|
|
return (GnuRSAPublicKey) new RSAKeyPairRawCodec().decodePublicKey(k);
|
| 120 |
|
|
}
|
| 121 |
|
|
catch (IllegalArgumentException ignored)
|
| 122 |
|
|
{
|
| 123 |
|
|
}
|
| 124 |
|
|
// try X.509 codec
|
| 125 |
|
|
return (GnuRSAPublicKey) new RSAKeyPairX509Codec().decodePublicKey(k);
|
| 126 |
|
|
}
|
| 127 |
|
|
|
| 128 |
|
|
/**
|
| 129 |
|
|
* Returns the encoded form of this public key according to the designated
|
| 130 |
|
|
* format.
|
| 131 |
|
|
*
|
| 132 |
|
|
* @param format the desired format identifier of the resulting encoding.
|
| 133 |
|
|
* @return the byte sequence encoding this key according to the designated
|
| 134 |
|
|
* format.
|
| 135 |
|
|
* @throws IllegalArgumentException if the format is not supported.
|
| 136 |
|
|
* @see RSAKeyPairRawCodec
|
| 137 |
|
|
*/
|
| 138 |
|
|
public byte[] getEncoded(final int format)
|
| 139 |
|
|
{
|
| 140 |
|
|
final byte[] result;
|
| 141 |
|
|
switch (format)
|
| 142 |
|
|
{
|
| 143 |
|
|
case IKeyPairCodec.RAW_FORMAT:
|
| 144 |
|
|
result = new RSAKeyPairRawCodec().encodePublicKey(this);
|
| 145 |
|
|
break;
|
| 146 |
|
|
case IKeyPairCodec.X509_FORMAT:
|
| 147 |
|
|
result = new RSAKeyPairX509Codec().encodePublicKey(this);
|
| 148 |
|
|
break;
|
| 149 |
|
|
default:
|
| 150 |
|
|
throw new IllegalArgumentException("Unsupported encoding format: "
|
| 151 |
|
|
+ format);
|
| 152 |
|
|
}
|
| 153 |
|
|
return result;
|
| 154 |
|
|
}
|
| 155 |
|
|
|
| 156 |
|
|
/**
|
| 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.
|
| 159 |
|
|
*
|
| 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.
|
| 163 |
|
|
*/
|
| 164 |
|
|
public boolean equals(final Object obj)
|
| 165 |
|
|
{
|
| 166 |
|
|
if (obj == null)
|
| 167 |
|
|
return false;
|
| 168 |
|
|
|
| 169 |
|
|
if (! (obj instanceof RSAPublicKey))
|
| 170 |
|
|
return false;
|
| 171 |
|
|
|
| 172 |
|
|
final RSAPublicKey that = (RSAPublicKey) obj;
|
| 173 |
|
|
return super.equals(that)
|
| 174 |
|
|
&& getPublicExponent().equals(that.getPublicExponent());
|
| 175 |
|
|
}
|
| 176 |
|
|
|
| 177 |
|
|
public String toString()
|
| 178 |
|
|
{
|
| 179 |
|
|
if (str == null)
|
| 180 |
|
|
{
|
| 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)
|
| 185 |
|
|
.append(")")
|
| 186 |
|
|
.toString();
|
| 187 |
|
|
}
|
| 188 |
|
|
return str;
|
| 189 |
|
|
}
|
| 190 |
|
|
}
|