KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > ssl > Server


1 package demo.ssl;
2
3 import java.io.*;
4
5 import java.security.cert.X509Certificate JavaDoc;
6
7 import org.omg.PortableServer.POA JavaDoc;
8 import org.omg.SecurityLevel2.*;
9 import org.omg.Security.*;
10 import org.omg.CORBA.ORB JavaDoc;
11
12 import org.jacorb.security.level2.*;
13
14 /**
15  * This is the server part of the ssl demo. It demonstrates
16  * how to get access to the certificates that the client sent
17  * for mutual authentication. The certificate chain can be
18  * accessed via the Security Level 2 interfaces.
19  *
20  * @author Nicolas Noffke
21  * @version $Id: Server.java,v 1.3 2002/01/22 10:56:39 nicolas Exp $
22  */

23
24 public class Server
25     extends SSLDemoPOA
26 {
27     //the Security Level 2 Current
28
private Current current = null;
29
30     /*
31      * This class from package org.jacorb.security.level2
32      * contains the actual contents of the security attributes
33      */

34     private SecAttributeManager attrib_mgr = null;
35
36     //the single attribute type array, that is used
37
//for getting the SecAttributes from the Credentials
38
private AttributeType[] access_id = null;
39     
40
41     public Server( Current current )
42     {
43         this.current = current;
44
45         attrib_mgr = SecAttributeManager.getInstance();
46         
47         AttributeType attribute_type =
48             new AttributeType(new ExtensibleFamily((short) 0,
49                                                    (short) 1),
50                               AccessId.value);
51         
52         access_id = new AttributeType[] {attribute_type};
53     }
54
55     /**
56      * This method retrievs the received client certificate
57      * from the Credentials.
58      */

59     private X509Certificate JavaDoc getClientCert()
60     {
61         //get the ReceivedCredentials
62
ReceivedCredentials creds = current.received_credentials();
63         
64         if (creds == null)
65         {
66             return null;
67         }
68         
69         //get the SecAttributes we're interested in
70
SecAttribute[] attribs = creds.get_attributes( access_id );
71
72         if( attribs.length == 0 )
73         {
74             return null;
75         }
76
77         //get the actual contents of the SecAttributes via
78
//the SecAttributeManager
79
KeyAndCert kac = attrib_mgr.getAttributeCertValue( attribs[0] );
80
81         if( kac == null )
82         {
83             return null;
84         }
85  
86         //return the first (self-signed) certificate of the chain
87
return (X509Certificate JavaDoc) kac.chain[0];
88     }
89
90
91     /**
92      * This method is from the IDL--interface. It prints out the
93      * received client cert (if available).
94      */

95     public void printCert()
96     {
97         X509Certificate JavaDoc client_cert = getClientCert();
98         
99         if( client_cert == null )
100         {
101             System.out.println( "No client certificate available" );
102         }
103         else
104         {
105             System.out.println( "Received a client certificate:" );
106             System.out.println( client_cert );
107         }
108     }
109
110     public static void main( String JavaDoc[] args )
111     {
112         if( args.length != 1 )
113     {
114             System.out.println( "Usage: java demo.ssl.Server <ior_file>" );
115             System.exit( -1 );
116         }
117
118         try
119         {
120             ORB JavaDoc orb = ORB.init( args, null );
121             
122             POA JavaDoc poa = (POA JavaDoc)
123                 orb.resolve_initial_references( "RootPOA" );
124
125             poa.the_POAManager().activate();
126
127             Current current = (org.omg.SecurityLevel2.Current)
128                 orb.resolve_initial_references( "SecurityCurrent" );
129
130             org.omg.CORBA.Object JavaDoc demo =
131                 poa.servant_to_reference( new Server( current ));
132
133             PrintWriter pw =
134                 new PrintWriter( new FileWriter( args[ 0 ] ));
135
136             // print stringified object reference to file
137
pw.println( orb.object_to_string( demo ));
138             
139             pw.flush();
140             pw.close();
141     
142             // wait for requests
143
orb.run();
144         }
145         catch( Exception JavaDoc e )
146         {
147             e.printStackTrace();
148         }
149     }
150 } // Server
151
Popular Tags