1 31 package org.objectweb.proactive.ext.security.crypto; 32 33 import java.io.IOException ; 34 import java.io.Serializable ; 35 import java.security.PublicKey ; 36 import java.security.SecureRandom ; 37 import java.security.cert.CertificateEncodingException ; 38 import java.security.cert.X509Certificate ; 39 40 import javax.crypto.Cipher; 41 import javax.crypto.Mac; 42 import javax.crypto.SecretKey; 43 import javax.crypto.spec.IvParameterSpec; 44 45 import org.objectweb.proactive.core.body.UniversalBody; 46 import org.objectweb.proactive.ext.security.Communication; 47 import org.objectweb.proactive.ext.security.Policy; 48 import org.objectweb.proactive.ext.security.ProActiveSecurity; 49 50 51 public class Session implements Serializable { 52 public long sessionID; 54 55 public X509Certificate distantOACertificate; 57 58 public PublicKey distantOAPublicKey; 60 61 public UniversalBody distantBody; 63 64 public transient Cipher cl_cipher; 66 67 public transient Cipher se_cipher; 69 70 private Communication communication; 72 73 public transient Cipher rsa_eng; 75 76 public transient Mac cl_mac; 78 79 public transient Mac se_mac; 81 public byte[] cl_sec_key; 82 public byte[] se_sec_key; 83 public byte[] cl_mac_enc; 84 public byte[] se_mac_enc; 85 public transient IvParameterSpec se_iv; 86 public transient IvParameterSpec cl_iv; 87 88 public byte[] se_rand; 90 91 public byte[] cl_rand; 93 94 public SecretKey se_hmac_key; 97 public SecretKey se_aes_key; 98 public SecretKey cl_hmac_key; 99 public SecretKey cl_aes_key; 100 101 public transient SecureRandom sec_rand; 104 105 public Session() { 106 } 107 108 public Session(long sessionID, Communication policy) throws Exception { 109 this.communication = policy; 110 111 se_rand = new byte[32]; cl_rand = new byte[32]; sec_rand = new SecureRandom (); 114 cl_cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC"); 115 se_cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC"); rsa_eng = Cipher.getInstance("RSA/None/OAEPPadding", "BC"); cl_mac = Mac.getInstance("HMACSHA1", "BC"); se_mac = Mac.getInstance("HMACSHA1", "BC"); this.sessionID = sessionID; 120 distantOACertificate = null; distantOAPublicKey = null; } 123 124 public boolean isID(long ID) { 125 if (ID == this.sessionID) { 126 return true; 127 } 128 129 return false; 130 } 131 132 public X509Certificate get_otherPublicCertificate(long id) { 133 if (this.sessionID == id) { 134 return distantOACertificate; 135 } 136 137 return null; 138 } 139 140 public long getSessionID() { 141 return sessionID; 142 } 143 144 public void setDistantOACertificate(X509Certificate distantBodyCertificate) { 145 distantOACertificate = distantBodyCertificate; 146 } 147 148 public X509Certificate getDistantOACertificate() { 149 return distantOACertificate; 150 } 151 152 public PublicKey getDistantOAPublicKey() { 153 return distantOAPublicKey; 154 } 155 156 public void setDistantOAPublicKey(PublicKey distantOAPublicKey) { 157 this.distantOAPublicKey = distantOAPublicKey; 158 } 159 160 public byte[][] writePDU(byte[] in) throws Exception { 161 byte[] mac = null; 162 if (communication.isIntegrityEnabled()) { 163 cl_mac.update(in); } 165 166 if (communication.isConfidentialityEnabled()) { 167 try { 168 in = cl_cipher.doFinal(in); } catch (Exception bex) { 170 bex.printStackTrace(); 171 throw (new IOException ("PDU failed to encrypt " + 172 bex.getMessage())); 173 } 174 } 175 176 if (communication.isIntegrityEnabled()) { 177 mac = cl_mac.doFinal(); 178 } 179 180 return new byte[][] { in, mac }; 188 } 189 190 public static boolean isEqual(byte[] a, byte[] b) { 191 if ((a == null) || (b == null)) { 192 return (false); 193 } 194 195 if (a.length != b.length) { 196 return (false); 197 } 198 199 for (int t = 0; t < a.length; t++) { 200 if (a[t] != b[t]) { 201 return (false); 202 } 203 } 204 205 return (true); 206 } 207 208 public byte[] readPDU(byte[] in, byte[] mac) throws Exception { 209 if (communication.isConfidentialityEnabled()) { 212 try { 213 in = se_cipher.doFinal(in); 214 } catch (Exception ex) { 215 System.out.println("PDU Mac code decryption failed "); 216 throw new IOException ("PDU failed to decrypt " + 217 ex.getMessage()); 218 } 219 } 220 if (communication.isIntegrityEnabled()) { 221 se_mac.update(in); 223 byte[] m = null; 224 m = se_mac.doFinal(); 225 226 if (!isEqual(m, mac)) { 227 System.out.println("PDU Mac code failed "); 228 throw new IOException ("PDU Mac code failed "); 229 } 230 } 231 232 return (in); 240 } 241 242 private void writeObject(java.io.ObjectOutputStream out) 244 throws IOException { 245 out.defaultWriteObject(); 246 if (se_iv != null) { 247 out.write(se_iv.getIV()); 248 } else { 249 out.write(new byte[16]); 250 } 251 if (cl_iv != null) { 252 out.write(cl_iv.getIV()); 253 } else { 254 out.write(new byte[16]); 255 } 256 257 byte[] cert = new byte[0]; 258 try { 259 if (distantOACertificate != null) { 260 cert = distantOACertificate.getEncoded(); 261 } 262 } catch (CertificateEncodingException e) { 263 e.printStackTrace(); 264 } 265 out.writeInt(cert.length); 266 out.write(cert); 267 } 268 269 private void readObject(java.io.ObjectInputStream in) 270 throws IOException , ClassNotFoundException { 271 in.defaultReadObject(); 272 273 byte[] temp = new byte[16]; 275 in.read(temp); 276 277 se_iv = new IvParameterSpec(temp); 278 279 in.read(temp); 280 cl_iv = new IvParameterSpec(temp); 281 sec_rand = new SecureRandom (); 282 283 int i = in.readInt(); 286 byte[] certEncoded = new byte[i]; 287 in.read(certEncoded); 288 289 distantOACertificate = ProActiveSecurity.decodeCertificate(certEncoded); 290 291 try { 292 cl_cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC"); 293 se_cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC"); rsa_eng = Cipher.getInstance("RSA/None/OAEPPadding", "BC"); sec_rand = new SecureRandom (); 296 cl_mac = Mac.getInstance("HMACSHA1", "BC"); se_mac = Mac.getInstance("HMACSHA1", "BC"); 299 if ((se_iv != null) && (se_aes_key != null)) { 300 se_cipher.init(Cipher.DECRYPT_MODE, (SecretKey) se_aes_key, 301 se_iv); 302 } 303 304 if ((cl_iv != null) && (cl_aes_key != null)) { 306 cl_cipher.init(Cipher.ENCRYPT_MODE, cl_aes_key, cl_iv, sec_rand); 307 } 308 309 if ((se_mac != null) && (se_hmac_key != null)) { 320 se_mac.init(se_hmac_key); 321 } 322 if ((cl_mac != null) && (se_hmac_key != null)) { 323 cl_mac.init(cl_hmac_key); 324 } 325 } catch (Exception e) { 326 e.printStackTrace(); 327 } 328 329 } 331 332 private String displayByte(byte[] tab) { 333 String s = ""; 334 335 for (int i = 0; i < tab.length; i++) { 336 s += tab[i]; 337 } 338 339 return s; 340 } 341 342 public String toString() { 343 return "ID : " + sessionID + "\n" + "cl_rand : " + 344 displayByte(cl_rand) + "\n" + "se_rand : " + displayByte(se_rand); 345 } 346 347 351 public void setPolicy(Policy resultPolicy) { 352 } 353 } 354 | Popular Tags |