KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > security > sas > GSSUPNameSpi


1 package org.jacorb.security.sas;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 2002-2004 Gerald Brose
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 import java.security.Provider JavaDoc;
24
25 import org.ietf.jgss.GSSException JavaDoc;
26 import org.ietf.jgss.Oid JavaDoc;
27 import org.omg.CORBA.Any JavaDoc;
28 import org.omg.CORBA.ORB JavaDoc;
29 import org.omg.GSSUP.InitialContextToken;
30 import org.omg.GSSUP.InitialContextTokenHelper;
31 import org.omg.IOP.Codec JavaDoc;
32
33 import sun.security.jgss.spi.GSSNameSpi;
34
35 /**
36  * This is the GSS-API Sercurity Provider Interface (SPI) for the GSSUP Name
37  *
38  * @author David Robison
39  * @version $Id: GSSUPNameSpi.java,v 1.14 2004/05/07 13:08:29 david.robison Exp $
40  */

41
42 public final class GSSUPNameSpi
43     implements GSSNameSpi
44 {
45     private static Oid JavaDoc mechOid;
46
47     private Provider JavaDoc provider;
48     private Oid JavaDoc nameTypeOid;
49
50     private InitialContextToken subject = null;
51
52     static
53     {
54         try
55         {
56             mechOid = new Oid JavaDoc("2.23.130.1.1.1");
57         }
58         catch (GSSException JavaDoc e)
59         {
60         }
61     }
62
63     public GSSUPNameSpi(Provider JavaDoc provider, Oid JavaDoc mechOid, byte[] name ,Oid JavaDoc nameTypeOid)
64     {
65         this.provider = provider;
66         this.nameTypeOid = nameTypeOid;
67         //GSSUPNameSpi.mechOid = mechOid;
68

69         // parse the name
70
/*
71         if (name.length > 0)
72         {
73             try
74             {
75                 Any any =
76                     ((GSSUPProvider)provider).getCodec().decode_value( name,
77                                                       InitialContextTokenHelper.type());
78                 subject = InitialContextTokenHelper.extract(any);
79             }
80             catch (Exception e)
81             {
82                 // logger.error("Error creating GSSNameSpi: " + e);
83                 subject = new InitialContextToken(new byte[0], new byte[0], new byte[0]);
84             }
85         }
86         else
87         {
88             subject = new InitialContextToken(new byte[0], new byte[0], new byte[0]);
89         }
90         */

91     }
92
93     public static byte[] encode(ORB JavaDoc orb, Codec JavaDoc codec, String JavaDoc username, String JavaDoc password, byte[] target_name)
94     {
95         InitialContextToken subject = null;
96         try
97         {
98             subject = new InitialContextToken( username.getBytes("UTF-8"),
99                                                password.getBytes("UTF-8"),
100                                                target_name);
101         }
102         catch(java.io.UnsupportedEncodingException JavaDoc e)
103         {
104             //should never happen
105
// logger.error("Error creating InitialContextToken: " + e);
106
return new byte[0];
107         }
108         byte[] out = null;
109         Any JavaDoc any = orb.create_any();
110         InitialContextTokenHelper.insert( any, subject );
111         try
112         {
113             out = codec.encode_value( any );
114         }
115         catch (Exception JavaDoc e)
116         {
117             // logger.error("Error encoding for GSSNameSpi: " + e);
118
return new byte[0];
119         }
120
121         byte[] mechOidArray = null;
122         try
123         {
124             mechOidArray = mechOid.getDER();
125         }
126         catch(org.ietf.jgss.GSSException JavaDoc e)
127         {
128             // logger.error("Error retrieving mechOid DER: " + e);
129
return new byte[0];
130         }
131
132         int length = out.length + mechOidArray.length;
133         byte[] encodedLength = null;
134
135         if((length >> 7) == 0)
136         {
137             //length fits into 7 bit
138
encodedLength = new byte[]{(byte) 0x60,
139                                        (byte) length};
140         }
141         else if((length >> 14) == 0)
142         {
143             //length fits into 14 bit
144
encodedLength = new byte[]{(byte) 0x60,
145                                        (byte) ((length >> 7) | 0x80),
146                                        (byte) (length & 0x7F)};
147         }
148         else if((length >> 21) == 0)
149         {
150             //length fits into 21 bit
151
encodedLength = new byte[]{(byte) 0x60,
152                                        (byte) ((length >> 14) | 0x80),
153                                        (byte) (((length >> 7) & 0x7F) | 0x80),
154                                        (byte) (length & 0x7F)};
155         }
156         else if((length >> 28) == 0)
157         {
158             //length fits into 28 bit
159
encodedLength = new byte[]{(byte) 0x60,
160                                        (byte) ((length >> 21) | 0x80),
161                                        (byte) (((length >> 14) & 0x7F) | 0x80),
162                                        (byte) (((length >> 7) & 0x7F) | 0x80),
163                                        (byte) (length & 0x7F)};
164         }
165         else
166         {
167             //length fits into 32 bit
168
encodedLength = new byte[]{(byte) 0x60,
169                                        (byte) ((length >> 28) | 0x80),
170                                        (byte) (((length >> 21) & 0x7F) | 0x80),
171                                        (byte) (((length >> 14) & 0x7F) | 0x80),
172                                        (byte) (((length >> 7) & 0x7F) | 0x80),
173                                        (byte) (length & 0x7F)};
174         }
175
176         byte[] completeContext = new byte[length + encodedLength.length];
177         System.arraycopy(encodedLength, 0,
178                          completeContext, 0,
179                          encodedLength.length);
180         System.arraycopy(mechOidArray, 0,
181                          completeContext, encodedLength.length,
182                          mechOidArray.length);
183         System.arraycopy(out, 0,
184                          completeContext, encodedLength.length + mechOidArray.length,
185                          out.length);
186
187         return completeContext;
188     }
189
190     public static byte[] encode(ORB JavaDoc orb, Codec JavaDoc codec, String JavaDoc username, char[] password, String JavaDoc target_name)
191     {
192         return encode(orb, codec, username, new String JavaDoc(password), target_name.getBytes());
193     }
194
195     public static InitialContextToken decode(ORB JavaDoc orb, Codec JavaDoc codec, byte[] gssToken)
196     {
197         if(gssToken[0] != 0x60)
198         {
199             // logger.error("GSSToken doesn't start with expected value '0x60'");
200
return null;
201         }
202
203         //skip total size, the GSSToken already has the correct length
204

205         //find first octet where the MSB is zero
206
int index = 1;
207         while(index < gssToken.length &&
208               (gssToken[index] & 0x80) == 1)
209         {
210             ++index;
211         }
212
213         if(index == gssToken.length)
214         {
215             //end not found
216
// logger.error("GSSToken doesn't contain valid length");
217
return null;
218         }
219
220         byte[] mechOidArray = null;
221         try
222         {
223             mechOidArray = mechOid.getDER();
224         }
225         catch(org.ietf.jgss.GSSException JavaDoc e)
226         {
227             // logger.error("Error retrieving mechOid DER: " + e);
228
return null;
229         }
230
231         //skip last octet of length
232
++index;
233
234         if((index + mechOidArray.length) >= gssToken.length)
235         {
236             // logger.error("GSSToken doesn't contain OID");
237
return null;
238         }
239
240         for(int i = 0; i < mechOidArray.length; ++i)
241         {
242             if(mechOidArray[i] != gssToken[index + i])
243             {
244                 // logger.error("GSSToken doesn't contain GSSUPMechOID");
245
return null;
246             }
247         }
248
249         //skip oid
250
index += mechOidArray.length;
251
252         byte[] icToken = new byte[gssToken.length - index];
253         System.arraycopy(gssToken, index, icToken, 0, icToken.length);
254
255         try
256         {
257             Any JavaDoc any =
258             codec.decode_value(
259                 icToken,
260                 InitialContextTokenHelper.type());
261             return InitialContextTokenHelper.extract(any);
262         }
263         catch (Exception JavaDoc e)
264         {
265             // logger.error("Error decoding for GSSNameSpi: " + e);
266
}
267         //logger.error("Bailout - GSSUP");
268
return null;
269     }
270
271     public Provider JavaDoc getProvider()
272     {
273         return provider;
274     }
275
276     public boolean equals(GSSNameSpi name) throws GSSException JavaDoc
277     {
278         return subject.equals(((GSSUPNameSpi)name).subject);
279     }
280
281     public byte[] export() throws GSSException JavaDoc
282     {
283         throw new GSSException JavaDoc(GSSException.FAILURE, GSSException.FAILURE, "Not Implemented");
284         /*
285         //System.out.println("GSSUPNameSpi.export");
286         Any any = ((GSSUPProvider)provider).getORB().create_any();
287         InitialContextTokenHelper.insert( any, subject );
288         byte[] out = new byte[0];
289         try
290         {
291             out = ((GSSUPProvider)provider).getCodec().encode_value( any );
292         }
293         catch (Exception e)
294         {
295             // logger.error("Error encoding for GSSNameSpi: " + e);
296         }
297         return out;
298         */

299     }
300
301     public Oid JavaDoc getMechanism()
302     {
303         return mechOid;
304     }
305
306     public String JavaDoc toString()
307     {
308         return null;
309         /*
310         Any any = ((GSSUPProvider)provider).getORB().create_any();
311         InitialContextTokenHelper.insert( any, subject );
312         byte[] out = new byte[0];
313         try
314         {
315             out = ((GSSUPProvider)provider).getCodec().encode_value( any );
316         }
317         catch (Exception e)
318         {
319             // logger.error("Error encoding for GSSNameSpi: " + e);
320         }
321         return new String(out);
322         */

323     }
324
325     public Oid JavaDoc getStringNameType()
326     {
327         return nameTypeOid;
328     }
329
330     public boolean isAnonymousName()
331     {
332         System.out.println("GSSUPNameSpi.isAnonymousName");
333         return false;
334     }
335 }
336
Popular Tags