Debian Codegraph @

gss/src/main/java/org/globus/gsi/gssapi/GlobusGSSCredentialImpl.java

raw
1 /*
2 * Copyright 1999-2010 University of Chicago
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11 * express or implied.
12 *
13 * See the License for the specific language governing permissions and limitations under the License.
14 */
15 package org.globus.gsi.gssapi;
16
17 import org.globus.util.Util;
18
19 import org.ietf.jgss.GSSException;
20 import org.ietf.jgss.GSSName;
21 import org.ietf.jgss.Oid;
22
23 import org.gridforum.jgss.ExtendedGSSCredential;
24
25 import java.security.cert.X509Certificate;
26 import java.security.PrivateKey;
27 import java.io.ByteArrayOutputStream;
28 import java.io.Serializable;
29 import java.io.IOException;
30 import java.io.File;
31 import java.io.FileOutputStream;
32
33 import org.globus.gsi.X509Credential;
34 import java.security.cert.CertificateEncodingException;
35 import org.globus.gsi.CredentialException;
36
37 /**
38 * An implementation of <code>GlobusGSSCredential</code>.
39 */
40 public class GlobusGSSCredentialImpl implements ExtendedGSSCredential,
41 Serializable {
42 private static final long serialVersionUID = 1L;
43
44 private int usage = 0;
45 private X509Credential cred;
46 private GSSName name;
47
48 /**
49 * Creates anonymous credential.
50 */
51 public GlobusGSSCredentialImpl() {
52 this.cred = null;
53 this.name = new GlobusGSSName();
54 }
55
56 /**
57 * Creates regular credential from specified
58 * X509Credential object.
59 *
60 * @param cred the credential
61 * @param usage credential usage
62 */
63 public GlobusGSSCredentialImpl(X509Credential cred,
64 int usage)
65 throws GSSException {
66 if (cred == null) {
67 throw new IllegalArgumentException("cred == null");
68 }
69
70 this.cred = cred;
71 this.usage = usage;
72 this.name = new GlobusGSSName(cred.getIdentity());
73 }
74
75 @Override
76 public int hashCode() {
77 if (this.cred == null) {
78 return this.usage;
79 } else {
80 return this.cred.hashCode() + this.usage;
81 }
82 }
83
84 @Override
85 public boolean equals(Object obj) {
86 if (obj instanceof GlobusGSSCredentialImpl) {
87 GlobusGSSCredentialImpl other = (GlobusGSSCredentialImpl)obj;
88 return other.usage == this.usage &&
89 (other.cred == this.cred || this.cred != null && this.cred.equals(other.cred));
90 }
91 return false;
92 }
93
94 public void dispose() throws GSSException {
95 this.cred = null;
96 }
97
98 public GSSName getName() throws GSSException {
99 return this.name;
100 }
101
102 public GSSName getName(Oid mech) throws GSSException {
103 GlobusGSSManagerImpl.checkMechanism(mech);
104 return this.name;
105 }
106
107 /**
108 * Currently not implemented.
109 */
110 public void add(GSSName aName,
111 int initLifetime,
112 int acceptLifetime,
113 Oid mech,
114 int usage)
115 throws GSSException {
116 // currently we are not supporting multiple mechanism
117 // credentials
118 throw new GSSException(GSSException.UNAVAILABLE);
119 }
120
121 public int getUsage() throws GSSException {
122 return usage;
123 }
124
125 public int getUsage(Oid mech)
126 throws GSSException {
127 GlobusGSSManagerImpl.checkMechanism(mech);
128 return this.usage;
129 }
130
131 public int getRemainingLifetime()
132 throws GSSException {
133 return (this.cred == null) ? -1 : (int)this.cred.getTimeLeft();
134 }
135
136 public int getRemainingInitLifetime(Oid mech)
137 throws GSSException {
138 GlobusGSSManagerImpl.checkMechanism(mech);
139 if (this.usage == INITIATE_ONLY ||
140 this.usage == INITIATE_AND_ACCEPT) {
141 return getRemainingLifetime();
142 } else {
143 throw new GSSException(GSSException.FAILURE);
144 }
145 }
146
147 public int getRemainingAcceptLifetime(Oid mech)
148 throws GSSException {
149 GlobusGSSManagerImpl.checkMechanism(mech);
150 if (this.usage == ACCEPT_ONLY ||
151 this.usage == INITIATE_AND_ACCEPT) {
152 return getRemainingLifetime();
153 } else {
154 throw new GSSException(GSSException.FAILURE);
155 }
156 }
157
158 public Oid[] getMechs()
159 throws GSSException {
160 return GlobusGSSManagerImpl.MECHS;
161 }
162
163 public byte[] export(int option)
164 throws GSSException {
165 return export(option, null);
166 }
167
168 public byte[] export(int option, Oid mech)
169 throws GSSException {
170 GlobusGSSManagerImpl.checkMechanism(mech);
171 if (this.cred == null) {
172 throw new GlobusGSSException(GSSException.FAILURE,
173 GlobusGSSException.CREDENTIAL_ERROR,
174 "anonCred00");
175 }
176
177 switch (option) {
178 case IMPEXP_OPAQUE:
179 ByteArrayOutputStream bout = new ByteArrayOutputStream();
180 try {
181 this.cred.save(bout);
182 } catch (IOException e) {
183 throw new GlobusGSSException(GSSException.FAILURE, e);
184 } catch (CertificateEncodingException e) {
185 throw new GlobusGSSException(GSSException.FAILURE, e);
186 }
187 return bout.toByteArray();
188 case IMPEXP_MECH_SPECIFIC:
189 File file = null;
190 FileOutputStream fout = null;
191 try {
192 file = File.createTempFile("x509up_", ".tmp");
193 Util.setOwnerAccessOnly(file.getAbsolutePath());
194 fout = new FileOutputStream(file);
195 this.cred.save(fout);
196 } catch(IOException e) {
197 throw new GlobusGSSException(GSSException.FAILURE, e);
198 } catch (CertificateEncodingException e) {
199 throw new GlobusGSSException(GSSException.FAILURE, e);
200 } finally {
201 if (fout != null) {
202 try { fout.close(); } catch (Exception e) {}
203 }
204 }
205 String handle = "X509_USER_PROXY=" + file.getAbsolutePath();
206 return handle.getBytes();
207 default:
208 throw new GlobusGSSException(GSSException.FAILURE,
209 GlobusGSSException.BAD_ARGUMENT,
210 "unknownOption",
211 new Object[] {new Integer(option)});
212 }
213 }
214
215 /**
216 * Retrieves arbitrary data about this credential.
217 * Currently supported oid: <UL>
218 * <LI>
219 * {@link GSSConstants#X509_CERT_CHAIN GSSConstants.X509_CERT_CHAIN}
220 * returns certificate chain of this credential
221 * (<code>X509Certificate[]</code>).
222 * </LI>
223 * </UL>
224 *
225 * @param oid the oid of the information desired.
226 * @return the information desired. Might be null.
227 * @exception GSSException containing the following major error codes:
228 * <code>GSSException.FAILURE</code>
229 */
230 public Object inquireByOid(Oid oid)
231 throws GSSException {
232 if (oid == null) {
233 throw new GlobusGSSException(GSSException.FAILURE,
234 GlobusGSSException.BAD_ARGUMENT,
235 "nullOption");
236 }
237
238 if (oid.equals(GSSConstants.X509_CERT_CHAIN)) {
239 return (this.cred == null) ?
240 null :
241 this.cred.getCertificateChain();
242 }
243
244 return null;
245 }
246
247 /**
248 * Returns actual X509Credential object represented
249 * by this credential (if any).
250 *
251 * @return The credential object. Might be null if
252 * this is an anonymous credential.
253 */
254 public X509Credential getX509Credential() {
255 return this.cred;
256 }
257
258 /**
259 * Returns the private key of this credential (if any).
260 *
261 * @return The private key. Might be null if this
262 * is an anonymous credential.
263 */
264 public PrivateKey getPrivateKey()
265 throws GSSException {
266 try {
267 return (this.cred == null) ? null : this.cred.getPrivateKey();
268 } catch (CredentialException e) {
269 throw new GlobusGSSException(GSSException.FAILURE, e);
270 }
271 }
272
273 /**
274 * Returns certificate chain of this credential (if any).
275 *
276 * @return The certificate chain. Might be null if this
277 * is an anonymous credential.
278 */
279 public X509Certificate [] getCertificateChain() {
280 return (this.cred == null) ? null : this.cred.getCertificateChain();
281 }
282
283 }