File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\cli\CliProtocol2.java.html

1 package hudson.cli;
2
3 import hudson.Extension;
4 import hudson.util.IOException2;
5 import jenkins.model.Jenkins;
6
7 import javax.crypto.SecretKey;
8 import javax.crypto.spec.SecretKeySpec;
9 import java.io.DataOutputStream;
10 import java.io.IOException;
11 import java.lang.reflect.InvocationTargetException;
12 import java.net.Socket;
13 import java.security.GeneralSecurityException;
14 import java.security.PrivateKey;
15 import java.security.Signature;
16
17 /**
18 * {@link CliProtocol} Version 2, which adds transport encryption.
19 *
20 * @author Kohsuke Kawaguchi
21 * @since 1.467
22 */
23 @Extension
24 public class CliProtocol2 extends CliProtocol {
25 @Override
26 public String getName() {
27 return "CLI2-connect";
28 }
29
30 @Override
31 public void handle(Socket socket) throws IOException, InterruptedException {
32 new Handler2(socket).run();
33 }
34
35 protected static class Handler2 extends Handler {
36 public Handler2(Socket socket) {
37 super(socket);
38 }
39
40 @Override
41 public void run() throws IOException, InterruptedException {
42 try {
43 DataOutputStream out = new DataOutputStream(socket.getOutputStream());
44 out.writeUTF("Welcome");
45
46 // perform coin-toss and come up with a session key to encrypt data
47 Connection c = new Connection(socket);
48 byte[] secret = c.diffieHellman(true).generateSecret();
49 SecretKey sessionKey = new SecretKeySpec(Connection.fold(secret,128/8),"AES");
50 c = c.encryptConnection(sessionKey,"AES/CFB8/NoPadding");
51
52 try {
53 // HACK: TODO: move the transport support into modules
54 Class<?> cls = Jenkins.getInstance().pluginManager.uberClassLoader.loadClass("org.jenkinsci.main.modules.instance_identity.InstanceIdentity");
55 Object iid = cls.getDeclaredMethod("get").invoke(null);
56 PrivateKey instanceId = (PrivateKey)cls.getDeclaredMethod("getPrivate").invoke(iid);
57
58 // send a signature to prove our identity
59 Signature signer = Signature.getInstance("SHA1withRSA");
60 signer.initSign(instanceId);
61 signer.update(secret);
62 c.writeByteArray(signer.sign());
63 } catch (ClassNotFoundException e) {
64 throw new Error(e);
65 } catch (IllegalAccessException e) {
66 throw new Error(e);
67 } catch (InvocationTargetException e) {
68 throw new Error(e);
69 } catch (NoSuchMethodException e) {
70 throw new Error(e);
71 }
72
73 runCli(c);
74 } catch (GeneralSecurityException e) {
75 throw new IOException2("Failed to encrypt the CLI channel",e);
76 }
77 }
78 }
79 }
80