Biblioteca Java - Blame information for rev 3
Subversion Repositories:
Rev | Author | Line No. | Line |
---|---|---|---|
3 | mihai | 1 | /* |
2 | * SecureOrderTaker.java | ||
3 | */ | ||
4 | package lab.scd.net.socket_secure; | ||
5 | |||
6 | /** | ||
7 | * Class created by @author Mihai HULEA at Feb 25, 2005. | ||
8 | * | ||
9 | * This class is part of the laborator2_net project. | ||
10 | * | ||
11 | */ | ||
12 | import java.net.*; | ||
13 | import java.io.*; | ||
14 | |||
15 | import java.security.*; | ||
16 | import javax.net.ssl.*; | ||
17 | import javax.net.ssl.KeyManagerFactory; | ||
18 | import javax.net.ssl.SSLContext; | ||
19 | |||
20 | /* | ||
21 | Starteaza un secure SSL server socket. | ||
22 | |||
23 | Inainte de starearea aplicatie trebuie generata cheia utilizand utilitarul keytool | ||
24 | |||
25 | D:\JAVA>keytool -genkey -alias ourstore -keystore jnp2e19.keys | ||
26 | Enter keystore password: studenti | ||
27 | What is your first and last name? | ||
28 | [Unknown]: Elliotte | ||
29 | What is the name of your organizational unit? | ||
30 | [Unknown]: Me, Myself, and I | ||
31 | What is the name of your organization? | ||
32 | [Unknown]: Cafe au Lait | ||
33 | What is the name of your City or Locality? | ||
34 | [Unknown]: Brooklyn | ||
35 | What is the name of your State or Province? | ||
36 | [Unknown]: New York | ||
37 | What is the two-letter country code for this unit? | ||
38 | [Unknown]: NY | ||
39 | Is <CN=Elliotte, OU="Me, Myself, and I", O=Cafe au Lait, L=Brooklyn, | ||
40 | ST=New York, C=NY> correct? | ||
41 | [no]: y | ||
42 | Enter key password for <ourstore> | ||
43 | (RETURN if same as keystore password): | ||
44 | */ | ||
45 | |||
46 | |||
47 | public class SecureOrderTaker { | ||
48 | public final static int DEFAULT_PORT = 7000; | ||
49 | public final static String algorithm = "SSLv3"; | ||
50 | |||
51 | public static void main(String[] args) { | ||
52 | int port = DEFAULT_PORT; | ||
53 | if (args.length > 0) { | ||
54 | try { | ||
55 | port = Integer.parseInt(args[0]); | ||
56 | if (port <= 0 || port >= 65536) { | ||
57 | System.out.println("Port must between 1 and 65535"); | ||
58 | return; | ||
59 | } | ||
60 | } | ||
61 | catch (NumberFormatException e) {} | ||
62 | } | ||
63 | try { | ||
64 | SSLContext context = SSLContext.getInstance("SSL"); | ||
65 | // The reference implementation only supports X.509 keys | ||
66 | KeyManagerFactory kmf = | ||
67 | KeyManagerFactory.getInstance("SunX509"); | ||
68 | // Sun's default kind of key store | ||
69 | KeyStore ks = KeyStore.getInstance("JKS"); | ||
70 | // For security, every key store is encrypted with a | ||
71 | // pass phrase that must be provided before we can load | ||
72 | // it from disk. The pass phrase is stored as a char[] array | ||
73 | // so it can be wiped from memory quickly rather than | ||
74 | // waiting for a garbage collector. Of course using a string | ||
75 | // literal here completely defeats that purpose. | ||
76 | char[] password = "studenti".toCharArray( ); | ||
77 | ks.load(new FileInputStream(new File("d:\\myprogramms\\java\\labs\\jnp2e19.keys")), password); | ||
78 | kmf.init(ks, password); | ||
79 | // | ||
80 | context.init(kmf.getKeyManagers( ), null, null); | ||
81 | SSLServerSocketFactory factory | ||
82 | = context.getServerSocketFactory( ); | ||
83 | SSLServerSocket server | ||
84 | = (SSLServerSocket) factory.createServerSocket(port); | ||
85 | // Now all the set up is complete and we can focus | ||
86 | // on the actual communication. | ||
87 | try { | ||
88 | while (true) { | ||
89 | // This socket will be secure, | ||
90 | // but there's no indication of that in the code! | ||
91 | System.out.println("Server started. Waiting..."); | ||
92 | Socket theConnection = server.accept( ); | ||
93 | System.out.println("Client connection accepted."); | ||
94 | InputStream in = theConnection.getInputStream( ); | ||
95 | int c; | ||
96 | while ((c = in.read( )) != -1) { | ||
97 | System.out.write(c); | ||
98 | } | ||
99 | theConnection.close( ); | ||
100 | } // end while | ||
101 | } // end try | ||
102 | catch (IOException e) { | ||
103 | System.err.println(e); | ||
104 | } // end catch | ||
105 | } // end try | ||
106 | catch (IOException e) { | ||
107 | e.printStackTrace( ); | ||
108 | } // end catch | ||
109 | catch (KeyManagementException e) { | ||
110 | e.printStackTrace( ); | ||
111 | } // end catch | ||
112 | catch (KeyStoreException e) { | ||
113 | e.printStackTrace( ); | ||
114 | } // end catch | ||
115 | catch (NoSuchAlgorithmException e) { | ||
116 | e.printStackTrace( ); | ||
117 | } // end catch | ||
118 | catch (java.security.cert.CertificateException e) { | ||
119 | e.printStackTrace( ); | ||
120 | } // end catch | ||
121 | catch (UnrecoverableKeyException e) { | ||
122 | e.printStackTrace( ); | ||
123 | } // end catch | ||
124 | } // end main | ||
125 | } |