1 package jsdsi;
2
3 import java.net.URI;
4 import java.net.URL;
5 import java.util.ArrayList;
6 import java.util.Iterator;
7 import java.util.List;
8
9 import jsdsi.sexp.SexpList;
10 import jsdsi.sexp.SexpParseException;
11 import jsdsi.sexp.SexpUtil;
12 import jsdsi.util.DigestAlgoEnum;
13
14 /***
15 * A public key.
16 *
17 * @author Sameer Ajmani
18 * @author Sean Radford
19 * @version $Revision: 1.7.2.1 $ $Date: 2005/11/08 03:12:52 $
20 */
21 public abstract class PublicKey
22 extends Principal
23 implements Element, java.security.PublicKey {
24
25 private static final long serialVersionUID = 6633153639072781861L;
26
27 /***
28 * Name of the algorithm used.
29 */
30 private transient final String algo;
31
32 /***
33 * List of URIs.
34 */
35 private transient final URI[] uris;
36
37 /***
38 * Creates a new <code>PublicKey</code> for a given algorithm name and
39 * list of URLs.
40 *
41 * @param a name of the algorithm used.
42 * @param u list of URLs (may be <code>null</code>).
43 * @deprecated use {@link #PublicKey(String, URI[])}
44 */
45 public PublicKey(String a, URL[] u) {
46 assert(a != null) : "null algo";
47 algo = a;
48 uris = Util.convert(u); // may be null
49 }
50
51 /***
52 * Creates a new <code>PublicKey</code> for a given algorithm name and
53 * list of URIs.
54 *
55 * @param a name of the algorithm used.
56 * @param u list of URIs (may be <code>null</code>).
57 */
58 public PublicKey(String a, URI[] u) {
59 assert(a != null) : "null algo";
60 algo = a;
61 uris = u; // may be null
62 }
63
64 /***
65 * Creates a new <code>PublicKey</code> from an algorithm name.
66 *
67 * @param a name of the algorithm name used.
68 */
69 public PublicKey(String a) {
70 this(a, (URI[])null);
71 }
72
73 /***
74 * @see java.security.Key#getAlgorithm()
75 */
76 public String getAlgorithm() {
77 return algo;
78 }
79
80 /***
81 * @see java.security.Key#getFormat()
82 */
83 public String getFormat() {
84 return "SEXP";
85 }
86
87 /***
88 * @see java.security.Key#getEncoded()
89 */
90 public byte[] getEncoded() {
91 return toByteArray();
92 }
93
94 /***
95 * @return a list of URLs of this <code>PublicKey</code>.
96 * @deprecated use {@link PublicKey#getURIs()}
97 */
98 public URL[] getURLs() {
99 return Util.convert(uris);
100 }
101
102 /***
103 * @return the list of URIs where information about this
104 * <code>PublicKey</code> maybe found.
105 */
106 public URI[] getURIs() {
107 return uris;
108 }
109
110 /***
111 * Generate the corresponding PublicKeyHash for this PublicKey.
112 * @param da the digest algorithm for the hash function.
113 * @return
114 */
115 public PublicKeyHash publicKeyHash(DigestAlgoEnum da) {
116 Hash h = new Hash(da, this, getURIs());
117 return new PublicKeyHash(h);
118 }
119
120 /***
121 * @see jsdsi.Principal#samePrincipalAs(Principal)
122 */
123 public boolean samePrincipalAs(Principal p) {
124 if (p == null) {
125 return false;
126 }
127 if (p instanceof PublicKey) {
128 return equals(p);
129 }
130 // p is a PublicKeyHash
131 return p.samePrincipalAs(this);
132 }
133
134 /***
135 * @see java.lang.Object#equals(Object)
136 */
137 public boolean equals(Object o) {
138 if (o instanceof PublicKey) {
139 PublicKey p = (PublicKey) o;
140 return algo.equals(p.algo) && Util.equals(uris, p.uris);
141 }
142 return false;
143 }
144
145 /***
146 * @see java.lang.Object#hashCode()
147 */
148 public int hashCode() {
149 return algo.hashCode() ^ Util.hashCode(uris);
150 }
151
152 abstract SexpList toPublicKeySexp();
153 public final SexpList toSexp() {
154 List l = new ArrayList(2);
155 l.add(toPublicKeySexp());
156 if (getURIs() != null) {
157 l.add(SexpUtil.toSexp(getURIs()));
158 }
159 return SexpUtil.toSexp("public-key", l);
160 }
161
162 /***
163 * Parses an Sexp of a publickey returning the <code>PublicKey</code>.
164 * @param l the Sexpression
165 * @return the PublicKey
166 * @throws SexpParseException
167 */
168 static PublicKey parsePublicKey(SexpList l) throws SexpParseException {
169 Iterator kbody = SexpUtil.getBody(l);
170 SexpList key = SexpUtil.getNextList(kbody, "public key body");
171 // see if there are any uri's
172 URI[] uris = null;
173 if (kbody.hasNext()) {
174 SexpList uri = SexpUtil.getNextList(kbody, "public key uri");
175 String type = uri.getType();
176 if (!type.equals("uri"))
177 throw new SexpParseException("unrecognized public-key field: "
178 + type);
179 uris = SexpUtil.parseURIs(uri);
180 }
181 // check parsing of public key is done
182 SexpUtil.checkDone(kbody, "public-key");
183 String type = key.getType();
184 if (type.startsWith("rsa")) {
185 return RSAPublicKey.parseRSAPublicKey(key, "RSA", uris);
186 }
187 throw new SexpParseException("unrecognized public-key type: " + type);
188 }
189 }
This page was automatically generated by Maven