1 package jsdsi;
2
3 import java.math.BigInteger;
4 import java.net.URI;
5 import java.net.URL;
6 import java.security.KeyPair;
7 import java.security.KeyPairGenerator;
8 import java.security.NoSuchAlgorithmException;
9 import java.security.NoSuchProviderException;
10 import java.util.Iterator;
11
12 import jsdsi.sexp.Sexp;
13 import jsdsi.sexp.SexpList;
14 import jsdsi.sexp.SexpParseException;
15 import jsdsi.sexp.SexpUtil;
16
17 /***
18 * An RSA public key.
19 *
20 * @author Sameer Ajmani
21 * @author Sean Radford
22 * @version $Revision: 1.7.2.1 $ $Date: 2005/11/08 03:12:52 $
23 */
24 public class RSAPublicKey
25 extends PublicKey
26 implements java.security.interfaces.RSAPublicKey {
27
28 private static final long serialVersionUID = 115101582431469482L;
29
30 /***
31 * The modulus of this <code>RSAPublicKey</code>.
32 */
33 private transient final BigInteger modulus;
34
35 /***
36 * The exponent of this <code>RSAPublicKey</code>.
37 */
38 private transient final BigInteger exponent;
39
40 /***
41 * Creates a new <code>RSAPublicKey</code> from a given modulus, exponent,
42 * algorithm-name, and URLs.
43 *
44 * @param m modulus.
45 * @param e exponent.
46 * @param a algorithm-name.
47 * @param u list of URLs.
48 * @deprecated use {@link #RSAPublicKey(BigInteger, BigInteger, String, URI[])}
49 */
50 public RSAPublicKey(BigInteger m, BigInteger e, String a, URL[] u) {
51 super(a, u);
52 assert(m != null) : "null modulus";
53 assert(e != null) : "null exponent";
54 modulus = m;
55 exponent = e;
56 }
57
58 public RSAPublicKey(BigInteger m, BigInteger e, String a, URI[] u) {
59 super(a, u);
60 assert(m != null) : "null modulus";
61 assert(e != null) : "null exponent";
62 modulus = m;
63 exponent = e;
64 }
65
66 /***
67 * Creates a new <code>RSAPublicKey</code> from a given modulus, exponent
68 * and algorithm name.
69 *
70 * @param m modulus.
71 * @param e exponent.
72 * @param a algorithm name.
73 */
74 public RSAPublicKey(BigInteger m, BigInteger e, String a) {
75 this(m, e, a, (URI[])null );
76 }
77
78 public RSAPublicKey(java.security.interfaces.RSAPublicKey k, URL[] u) {
79 this(k.getModulus(), k.getPublicExponent(), k.getAlgorithm(), u);
80 }
81
82 public RSAPublicKey(java.security.interfaces.RSAPublicKey k, URI[] u) {
83 this(k.getModulus(), k.getPublicExponent(), k.getAlgorithm(), u);
84 }
85
86 public RSAPublicKey(java.security.interfaces.RSAPublicKey k) {
87 this(k, (URI[])null );
88 }
89
90 /***
91 * Creates a new RSA key pair whose public key is a Principal.
92 * @param a the specific RSA algorithm to use
93 * @param provider the provider to use
94 */
95 public static KeyPair create(String a, String provider)
96 throws NoSuchAlgorithmException, NoSuchProviderException {
97 assert(a != null) : "null algo";
98 KeyPairGenerator kpg =
99 (provider == null)
100 ? KeyPairGenerator.getInstance(a)
101 : KeyPairGenerator.getInstance(a, provider);
102 KeyPair kp = kpg.genKeyPair();
103 assert(kp.getPublic() instanceof java.security.interfaces.RSAPublicKey)
104 : "did not generate RSA key";
105 return new KeyPair(
106 new RSAPublicKey((java.security.interfaces.RSAPublicKey)
107 kp.getPublic()),
108 kp.getPrivate());
109 }
110
111 /***
112 * Creates a new RSA key pair whose public key is a Principal.
113 *
114 * @param a the specific RSA algorithm to use
115 */
116 public static KeyPair create(String a)
117 throws NoSuchAlgorithmException, NoSuchProviderException {
118 return create(a, null);
119 }
120
121 /***
122 * Creates a new RSA key pair whose public key is a Principal.
123 */
124 public static KeyPair create()
125 throws NoSuchAlgorithmException, NoSuchProviderException {
126 return create("RSA");
127 }
128
129 /***
130 * @see java.security.interfaces.RSAKey#getModulus()
131 */
132 public BigInteger getModulus() {
133 return modulus;
134 }
135
136 /***
137 * @return the exponent of this <code>RSAPublicKey</code>.
138 */
139 public BigInteger getExponent() {
140 return exponent;
141 }
142
143 /***
144 * @see java.security.interfaces.RSAPublicKey#getPublicExponent()
145 */
146 public BigInteger getPublicExponent() {
147 return exponent;
148 }
149
150 /***
151 * @see java.lang.Object#equals(Object)
152 */
153 public boolean equals(Object o) {
154 if (o instanceof RSAPublicKey) {
155 RSAPublicKey r = (RSAPublicKey) o;
156 return modulus.equals(r.modulus)
157 && exponent.equals(r.exponent)
158 && super.equals(o);
159 }
160 return false;
161 }
162
163 /***
164 * @see java.lang.Object#hashCode()
165 */
166 public int hashCode() {
167 return modulus.hashCode() ^ exponent.hashCode() ^ super.hashCode();
168 }
169
170 public SexpList toPublicKeySexp() {
171 String algo = "rsa";
172 Sexp[] ss = new Sexp[2];
173 Sexp[] es = new Sexp[1];
174 es[0] = SexpUtil.toSexp(getExponent().toByteArray());
175 ss[0] = SexpUtil.toSexp("e", es);
176 Sexp[] ns = new Sexp[1];
177 ns[0] = SexpUtil.toSexp(getModulus().toByteArray());
178 ss[1] = SexpUtil.toSexp("n", ns);
179 return SexpUtil.toSexp(algo, ss);
180 }
181
182 /***
183 * Parses an Sexp of an rsa-publickey returning the
184 * <code>RSAPublicKey</code> with the given urls.
185 * @param l Sexpression defining the public-key
186 * @param algo algorithm
187 * @param urls array of uris (may be <code>null</code>)
188 * @return
189 * @throws SexpParseException
190 *
191 * @deprecated use {@link #parseRSAPublicKey(SexpList, String, URI[])}
192 */
193 static RSAPublicKey parseRSAPublicKey(SexpList l, String algo, URL[] urls)
194 throws SexpParseException {
195 Iterator rbody = SexpUtil.getBody(l);
196 Iterator ebody =
197 SexpUtil.getBody(SexpUtil.getNextList(rbody, "e", "exponent"));
198 BigInteger e =
199 new BigInteger(SexpUtil.getNextByteArray(ebody, "e value"));
200 SexpUtil.checkDone(ebody, "exponent");
201 Iterator nbody =
202 SexpUtil.getBody(SexpUtil.getNextList(rbody, "n", "modulus"));
203 BigInteger n =
204 new BigInteger(SexpUtil.getNextByteArray(nbody, "n value"));
205 SexpUtil.checkDone(nbody, "modulus");
206 SexpUtil.checkDone(rbody, "rsa public-key");
207 return new RSAPublicKey(n, e, algo, urls);
208 }
209
210 /***
211 * Parses an Sexp of an rsa-publickey returning the
212 * <code>RSAPublicKey</code> with the given uris.
213 * @param l Sexpression defining the public-key
214 * @param algo algorithm
215 * @param uris array of uris (may be <code>null</code>)
216 * @return
217 * @throws SexpParseException
218 *
219 */
220 static RSAPublicKey parseRSAPublicKey(SexpList l, String algo, URI[] uris)
221 throws SexpParseException {
222 Iterator rbody = SexpUtil.getBody(l);
223 Iterator ebody =
224 SexpUtil.getBody(SexpUtil.getNextList(rbody, "e", "exponent"));
225 BigInteger e =
226 new BigInteger(SexpUtil.getNextByteArray(ebody, "e value"));
227 SexpUtil.checkDone(ebody, "exponent");
228 Iterator nbody =
229 SexpUtil.getBody(SexpUtil.getNextList(rbody, "n", "modulus"));
230 BigInteger n =
231 new BigInteger(SexpUtil.getNextByteArray(nbody, "n value"));
232 SexpUtil.checkDone(nbody, "modulus");
233 SexpUtil.checkDone(rbody, "rsa public-key");
234 return new RSAPublicKey(n, e, algo, uris);
235 }
236 }
This page was automatically generated by Maven