1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package com.jcabi.ssl.maven.plugin;
31
32 import com.jcabi.aspects.Immutable;
33 import com.jcabi.aspects.Loggable;
34 import com.jcabi.log.Logger;
35 import com.jcabi.log.VerboseProcess;
36 import java.io.File;
37 import java.io.IOException;
38 import java.io.OutputStreamWriter;
39 import java.io.PrintWriter;
40 import java.util.ArrayList;
41 import java.util.List;
42 import java.util.Locale;
43 import lombok.EqualsAndHashCode;
44 import lombok.ToString;
45 import org.apache.commons.io.FileUtils;
46
47
48
49
50
51
52
53
54 @Immutable
55 @ToString
56 @EqualsAndHashCode(of = { "keystore", "password" })
57 final class Keytool {
58
59
60
61 private static final String LOCALHOST = "localhost";
62
63
64
65
66 private static final String NEWLINE = System.getProperty("line.separator");
67
68
69
70
71 private final transient String keystore;
72
73
74
75
76 private final transient String password;
77
78
79
80
81
82
83 public Keytool(final File store, final String pwd) {
84 this.keystore = store.getAbsolutePath();
85 this.password = pwd;
86 }
87
88
89
90
91
92
93 @Loggable(Loggable.DEBUG)
94 public String list() throws IOException {
95 return new VerboseProcess(this.proc("-list", "-v")).stdout();
96 }
97
98
99
100
101
102 @Loggable(Loggable.DEBUG)
103 public void genkey() throws IOException {
104 final Process proc = this.proc(
105 "-genkeypair",
106 "-alias",
107 Keytool.LOCALHOST,
108 "-keyalg",
109 "RSA",
110 "-keysize",
111 "2048",
112 "-keypass",
113 this.password
114 ).start();
115 final PrintWriter writer = new PrintWriter(
116 new OutputStreamWriter(proc.getOutputStream())
117 );
118 writer.print(this.appendNewLine(Keytool.LOCALHOST));
119 writer.print(this.appendNewLine("ACME Co."));
120 writer.print(this.appendNewLine("software developers"));
121 writer.print(this.appendNewLine("San Francisco"));
122 writer.print(this.appendNewLine("California"));
123 writer.print(this.appendNewLine("US"));
124 writer.print(this.appendNewLine(this.createLocaleDependentYes()));
125 writer.close();
126 new VerboseProcess(proc).stdout();
127 Logger.info(
128 this,
129 "Keystore created in '%s' (%s)",
130 this.keystore,
131 FileUtils.byteCountToDisplaySize(this.keystore.length())
132 );
133 }
134
135
136
137
138
139
140
141 @Loggable(Loggable.DEBUG)
142 public void imprt(final File file, final String pwd) throws IOException {
143 new VerboseProcess(
144 this.proc(
145 "-importkeystore",
146 "-srckeystore",
147 file.getAbsolutePath(),
148 "-srcstorepass",
149 pwd,
150 "-destkeystore",
151 this.keystore,
152 "-deststorepass",
153 this.password
154 )
155 ).stdout();
156 }
157
158
159
160
161
162
163
164 private String appendNewLine(final String text) {
165 return String.format("%s%s", text, NEWLINE);
166 }
167
168
169
170
171
172
173 private String createLocaleDependentYes() {
174 return new Yes().translate(Locale.getDefault());
175 }
176
177
178
179
180
181
182
183 private ProcessBuilder proc(final String... args) throws IOException {
184 final List<String> cmds = new ArrayList<String>(args.length + 1);
185 cmds.add(
186 String.format(
187 "%s/bin/keytool",
188 System.getProperty("java.home")
189 )
190 );
191 for (final String arg : args) {
192 cmds.add(arg);
193 }
194 cmds.add("-storetype");
195 cmds.add("jks");
196 cmds.add("-noprompt");
197 cmds.add("-storepass");
198 cmds.add(this.password);
199 cmds.add("-keystore");
200 cmds.add(this.keystore);
201 return new ProcessBuilder(cmds);
202 }
203 }