KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > ext > security > DefaultProActiveSecurityManager


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31 package org.objectweb.proactive.ext.security;
32
33 import org.bouncycastle.asn1.x509.X509Name;
34
35 import org.bouncycastle.jce.X509V3CertificateGenerator;
36 import org.bouncycastle.jce.provider.JDKKeyPairGenerator;
37
38 import java.io.IOException JavaDoc;
39 import java.io.Serializable JavaDoc;
40
41 import java.math.BigInteger JavaDoc;
42
43 import java.security.KeyPair JavaDoc;
44 import java.security.Provider JavaDoc;
45 import java.security.SecureRandom JavaDoc;
46 import java.security.Security JavaDoc;
47 import java.security.cert.X509Certificate JavaDoc;
48
49 import java.text.DateFormat JavaDoc;
50
51 import java.util.Date JavaDoc;
52 import java.util.Hashtable JavaDoc;
53 import java.util.Random JavaDoc;
54 import java.util.logging.Logger JavaDoc;
55
56
57 public class DefaultProActiveSecurityManager extends ProActiveSecurityManager
58     implements Serializable JavaDoc {
59     private transient Logger JavaDoc logger;
60
61     public DefaultProActiveSecurityManager() {
62         // Provider myProvider = new org.bouncycastle.jce.provider.BouncyCastleProvider();
63
// Security.addProvider(myProvider);
64
// Security.insertProviderAt(myProvider, 0);
65
}
66
67     public DefaultProActiveSecurityManager(String JavaDoc vide)
68         throws Exception JavaDoc {
69         super(vide);
70         sessions = new Hashtable JavaDoc();
71         logger = Logger.getLogger(
72                 "org.objectweb.proactive.ext.security.DefaultProActiveSecurityManager");
73
74         Provider JavaDoc myProvider = new org.bouncycastle.jce.provider.BouncyCastleProvider();
75         Security.addProvider(myProvider);
76
77         /* generation of a default certificate */
78         KeyPair JavaDoc keyPair = null;
79         SecureRandom JavaDoc rand = new SecureRandom JavaDoc();
80
81         JDKKeyPairGenerator.RSA keyPairGen = new JDKKeyPairGenerator.RSA();
82
83         keyPairGen.initialize(1024, rand);
84
85         keyPair = keyPairGen.generateKeyPair();
86
87         privateKey = keyPair.getPrivate();
88         publicKey = keyPair.getPublic();
89
90         X509V3CertificateGenerator certifGenerator = new X509V3CertificateGenerator();
91
92         X509Certificate JavaDoc certif = null;
93
94         DateFormat JavaDoc convert = DateFormat.getDateInstance();
95
96         certifGenerator.setPublicKey(publicKey);
97
98         String JavaDoc subjectCN = "CN=Generic Certificate" + new Random JavaDoc().nextLong() +
99             ", OU=Generic Certificate, EmailAddress=none";
100
101         // System.out.println("DefaultCertificate subjectCN " + subjectCN);
102
X509Name subject = new X509Name(subjectCN);
103         X509Name issuer = new X509Name(
104                 "CN=Generic Certificate, OU=Generic Certificate, EmailAddress=none");
105
106         certifGenerator.setSubjectDN(subject);
107         certifGenerator.setIssuerDN(issuer);
108         certifGenerator.setSignatureAlgorithm("MD5withRSA");
109
110         // GregorianCalendar start = new GregorianCalendar(2002, Calendar.JUNE, 13);
111
// GregorianCalendar end = new GregorianCalendar(2004, Calendar.JUNE, 31);
112
Date JavaDoc start = new Date JavaDoc(System.currentTimeMillis() - 50000);
113         Date JavaDoc stop = new Date JavaDoc(System.currentTimeMillis() + 50000);
114
115         certifGenerator.setNotAfter(stop);
116         certifGenerator.setNotBefore(start);
117         certifGenerator.setPublicKey(publicKey);
118         certifGenerator.setSerialNumber(new BigInteger JavaDoc("1"));
119
120         certificate = certifGenerator.generateX509Certificate(privateKey, "BC");
121
122         byte[] t = certificate.getEncoded();
123         certificate = ProActiveSecurity.decodeCertificate(t);
124
125         // System.out.println("Generic certificate created " + certificate.getSubjectDN());
126
// new RuntimeException().printStackTrace();
127
// System.out.println("******************** instantiated DefaultPSM ao Thread " + Thread.currentThread().getName() + "******************");
128
// throw new SecurityException();
129
}
130
131     /*
132         public synchronized void initiateSession(UniversalBody distantBody) throws CommunicationForbiddenException, AuthenticationException {
133             X509Certificate distantBodyCertificate = null;
134             Policy localPolicy = null;
135             Policy distantBodyPolicy = null;
136
137             long sessionID = 0;
138
139             try {
140                 sessionID = distantBody.startNewSession();
141                 // System.out.println("new session ID is : " + sessionID);
142             } catch (IOException e) {
143                 logger.warning("can't start a new session");
144                 e.printStackTrace();
145                 throw new org.objectweb.proactive.ext.security.crypto.AuthenticationException();
146             } catch (RenegotiateSessionException e) {
147                     terminateSession(sessionID);
148                             //e.printStackTrace();
149                     }
150
151             Session session = null;
152
153             try {
154                 session = new Session(sessionID);
155             // session.setPolicy(resultPolicy);
156             } catch (Exception e) {
157                 e.printStackTrace();
158             }
159
160             session.distantBody = distantBody;
161             try {
162                             byte [] certE = distantBody.getRemoteAdapter().getCertificateEncoded();
163                                                        X509Certificate cert = ProActiveSecurity.decodeCertificate(certE);
164
165                             session.setDistantOACertificate(cert);
166                     } catch (IOException e2) {
167                             e2.printStackTrace();
168                     }
169             sessions.put(new Long(sessionID), session);
170
171             if (distantBodyCertificate != null) {
172                 session.setDistantOAPublicKey(distantBodyCertificate.getPublicKey());
173             } else {
174                 try {
175                                     session.setDistantOAPublicKey(distantBody.getPublicKey());
176                             } catch (IOException e1) {
177                                     e1.printStackTrace();
178                             }
179             }
180
181             try {
182               // logger.info("Key exchange " + sessionID);
183                 keyNegociationSenderSide(distantBody, sessionID);
184               // System.out.println("Session from " + certificate.getSubjectDN() + " to " + session.distantOACertificate.getSubjectDN());
185             } catch (KeyExchangeException e) {
186                 logger.warning("Key exchange exception ");
187                 e.printStackTrace();
188             }
189
190             // if ( localPolicy.isAuthenticationEnabled() && distantBodyPolicy.isAuthenticationEnabled() ) {
191             // mutualAuthenticationSenderSide(distantBody,distantBodyCertificate);
192             // }
193         }
194     */

195     /* public X509Certificate getCertificate() {
196            logger.info("asked for my certificate, replied null");
197            return null;
198        }*/

199     public ProActiveSecurityManager getProActiveSecurityManager()
200         throws java.io.IOException JavaDoc {
201         return this;
202     }
203
204     public Policy getPolicyFrom(X509Certificate JavaDoc certificate) {
205         // logger.info("asked for my policy FROM, replied default policy");
206
return new Policy();
207     }
208
209     public Policy getPolicyTo(X509Certificate JavaDoc certificate) {
210         // logger.info("asked for my policy TO, replied default policy");
211
return new Policy();
212     }
213
214     public Communication getPolicyTo(String JavaDoc type, String JavaDoc from, String JavaDoc to) {
215         // logger.info("asked for my policy TO, replied default policy");
216
return new Communication();
217     }
218
219     private void readObject(java.io.ObjectInputStream JavaDoc in)
220         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
221         in.defaultReadObject();
222
223         //System.out.println("reconstruit un DPSM");
224
logger = Logger.getLogger(
225                 "org.objectweb.proactive.ext.security.DefaultProActiveSecurityManager");
226     }
227 }
228
Popular Tags