Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: gnu/java/security/key/rsa/GnuRSAPublicKey.java


1   /* 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.classpath.SystemProperties;
42  import gnu.java.security.Registry;
43  import gnu.java.security.key.IKeyPairCodec;
44  
45  import java.math.BigInteger;
46  import java.security.PublicKey;
47  import java.security.interfaces.RSAPublicKey;
48  
49  /**
50   * <p>An object that encapsulates an RSA public key.</p>
51   *
52   * <p>References:</p>
53   * <ol>
54   *    <li><a href="http://www.cosic.esat.kuleuven.ac.be/nessie/workshop/submissions/rsa-pss.zip">
55   *    RSA-PSS Signature Scheme with Appendix, part B.</a><br>
56   *    Primitive specification and supporting documentation.<br>
57   *    Jakob Jonsson and Burt Kaliski.</li>
58   * </ol>
59   */
60  public class GnuRSAPublicKey extends GnuRSAKey implements PublicKey,
61      RSAPublicKey
62  {
63    // Constants and variables
64    // -------------------------------------------------------------------------
65  
66    /** String representation of this key. Cached for speed. */
67    private transient String str;
68  
69    // Constructor(s)
70    // -------------------------------------------------------------------------
71  
72    /**
73     * Conveience constructor. Calls the constructor with 3 arguments passing
74     * {@link Registry#RAW_ENCODING_ID} as the identifier of the preferred
75     * encoding format.
76     *
77     * @param n the modulus.
78     * @param e the public exponent.
79     */
80    public GnuRSAPublicKey(final BigInteger n, final BigInteger e)
81    {
82      this(Registry.RAW_ENCODING_ID, n, e);
83    }
84  
85    /**
86     * Constructs a new instance of <code>GnuRSAPublicKey</code> given the
87     * designated arguments.
88     * 
89     * @param preferredFormat the identifier of the preferred encoding format to
90     *          use when externalizing this key.
91     * @param n the modulus.
92     * @param e the public exponent.
93     */
94    public GnuRSAPublicKey(int preferredFormat, BigInteger n, BigInteger e)
95    {
96      super(preferredFormat == Registry.ASN1_ENCODING_ID ? Registry.X509_ENCODING_ID
97                                                         : preferredFormat,
98            n, e);
99    }
100 
101   // Class methods
102   // -------------------------------------------------------------------------
103 
104   /**
105    * A class method that takes the output of the <code>encodePublicKey()</code>
106    * method of an RSA keypair codec object (an instance implementing
107    * {@link IKeyPairCodec} for RSA keys, and re-constructs an instance of this
108    * object.
109    * 
110    * @param k the contents of a previously encoded instance of this object.
111    * @throws ArrayIndexOutOfBoundsException if there is not enough bytes, in
112    *           <code>k</code>, to represent a valid encoding of an instance
113    *           of this object.
114    * @throws IllegalArgumentException if the byte sequence does not represent a
115    *           valid encoding of an instance of this object.
116    */
117   public static GnuRSAPublicKey valueOf(final byte[] k)
118   {
119     // try RAW codec
120     if (k[0] == Registry.MAGIC_RAW_RSA_PUBLIC_KEY[0])
121       try
122         {
123           return (GnuRSAPublicKey) new RSAKeyPairRawCodec().decodePublicKey(k);
124         }
125       catch (IllegalArgumentException ignored)
126         {
127         }
128 
129     // try X.509 codec
130     return (GnuRSAPublicKey) new RSAKeyPairX509Codec().decodePublicKey(k);
131   }
132 
133   // Instance methods
134   // -------------------------------------------------------------------------
135 
136   /**
137    * <p>Returns the encoded form of this public key according to the designated
138    * format.</p>
139    *
140    * @param format the desired format identifier of the resulting encoding.
141    * @return the byte sequence encoding this key according to the designated
142    * format.
143    * @throws IllegalArgumentException if the format is not supported.
144    * @see RSAKeyPairRawCodec
145    */
146   public byte[] getEncoded(final int format)
147   {
148     final byte[] result;
149     switch (format)
150       {
151       case IKeyPairCodec.RAW_FORMAT:
152         result = new RSAKeyPairRawCodec().encodePublicKey(this);
153         break;
154       case IKeyPairCodec.X509_FORMAT:
155         result = new RSAKeyPairX509Codec().encodePublicKey(this);
156         break;
157       default:
158         throw new IllegalArgumentException("Unsupported encoding format: "
159                                            + format);
160       }
161     return result;
162   }
163 
164   /**
165    * <p>Returns <code>true</code> if the designated object is an instance of
166    * this class and has the same RSA parameter values as this one.</p>
167    *
168    * @param obj the other non-null RSA key to compare to.
169    * @return <code>true</code> if the designated object is of the same type and
170    * value as this one.
171    */
172   public boolean equals(final Object obj)
173   {
174     if (obj == null)
175       {
176         return false;
177       }
178     if (!(obj instanceof RSAPublicKey))
179       {
180         return false;
181       }
182     final RSAPublicKey that = (RSAPublicKey) obj;
183     return super.equals(that)
184            && getPublicExponent().equals(that.getPublicExponent());
185   }
186 
187   public String toString()
188   {
189     if (str == null)
190       {
191         String ls = SystemProperties.getProperty("line.separator");
192         str = new StringBuilder(this.getClass().getName()).append("(")
193             .append(super.toString()).append(",").append(ls)
194             .append(")").toString();
195       }
196     return str;
197   }
198 }