1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.gpg;
20
21 import java.io.ByteArrayInputStream;
22 import java.io.File;
23 import java.io.InputStream;
24
25 import org.apache.maven.plugin.MojoExecutionException;
26 import org.codehaus.plexus.util.Os;
27 import org.codehaus.plexus.util.cli.CommandLineException;
28 import org.codehaus.plexus.util.cli.CommandLineUtils;
29 import org.codehaus.plexus.util.cli.Commandline;
30 import org.codehaus.plexus.util.cli.DefaultConsumer;
31
32
33
34
35 public class GpgSigner extends AbstractGpgSigner {
36 public static final String NAME = "gpg";
37 private final String executable;
38
39 public GpgSigner(String executable) {
40 this.executable = executable;
41 }
42
43 @Override
44 public String signerName() {
45 return NAME;
46 }
47
48 @Override
49 public String getKeyInfo() {
50 return keyname != null ? keyname : "default";
51 }
52
53
54
55
56 @Override
57 protected void generateSignatureForFile(File file, File signature) throws MojoExecutionException {
58
59
60
61
62 Commandline cmd = new Commandline();
63
64 if (executable != null && !executable.isEmpty()) {
65 cmd.setExecutable(executable);
66 } else {
67 cmd.setExecutable("gpg" + (Os.isFamily(Os.FAMILY_WINDOWS) ? ".exe" : ""));
68 }
69
70 GpgVersionParser versionParser = GpgVersionParser.parse(executable);
71
72 GpgVersion gpgVersion = versionParser.getGpgVersion();
73 if (gpgVersion == null) {
74 throw new MojoExecutionException("Could not determine gpg version");
75 }
76
77 getLog().debug("GPG Version: " + gpgVersion);
78
79 if (args != null) {
80 for (String arg : args) {
81 cmd.createArg().setValue(arg);
82 }
83 }
84
85 if (homeDir != null) {
86 cmd.createArg().setValue("--homedir");
87 cmd.createArg().setFile(homeDir);
88 }
89
90 if (gpgVersion.isBefore(GpgVersion.parse("2.1"))) {
91 if (useAgent) {
92 cmd.createArg().setValue("--use-agent");
93 } else {
94 cmd.createArg().setValue("--no-use-agent");
95 }
96 }
97
98 InputStream in = null;
99 if (null != passphrase) {
100 if (gpgVersion.isAtLeast(GpgVersion.parse("2.0"))) {
101
102 cmd.createArg().setValue("--batch");
103 }
104
105 if (gpgVersion.isAtLeast(GpgVersion.parse("2.1"))) {
106
107 cmd.createArg().setValue("--pinentry-mode");
108 cmd.createArg().setValue("loopback");
109 }
110
111
112 cmd.createArg().setValue("--passphrase-fd");
113 cmd.createArg().setValue("0");
114
115
116 if (!passphrase.endsWith(System.lineSeparator())) {
117 in = new ByteArrayInputStream((passphrase + System.lineSeparator()).getBytes());
118 } else {
119 in = new ByteArrayInputStream(passphrase.getBytes());
120 }
121 }
122
123 if (null != keyname) {
124 cmd.createArg().setValue("--local-user");
125
126 cmd.createArg().setValue(keyname);
127 }
128
129 cmd.createArg().setValue("--armor");
130
131 cmd.createArg().setValue("--detach-sign");
132
133 if (getLog().isDebugEnabled()) {
134
135 cmd.createArg().setValue("--status-fd");
136 cmd.createArg().setValue("1");
137 }
138
139 if (!isInteractive) {
140 cmd.createArg().setValue("--batch");
141 cmd.createArg().setValue("--no-tty");
142
143 if (null == passphrase && gpgVersion.isAtLeast(GpgVersion.parse("2.1"))) {
144
145 cmd.createArg().setValue("--pinentry-mode");
146 cmd.createArg().setValue("error");
147 }
148 }
149
150 if (!defaultKeyring) {
151 cmd.createArg().setValue("--no-default-keyring");
152 }
153
154 if (secretKeyring != null && !secretKeyring.isEmpty()) {
155 if (gpgVersion.isBefore(GpgVersion.parse("2.1"))) {
156 cmd.createArg().setValue("--secret-keyring");
157 cmd.createArg().setValue(secretKeyring);
158 } else {
159 getLog().warn("'secretKeyring' is an obsolete option and ignored. All secret keys "
160 + "are stored in the ‘private-keys-v1.d’ directory below the GnuPG home directory.");
161 }
162 }
163
164 if (publicKeyring != null && !publicKeyring.isEmpty()) {
165 cmd.createArg().setValue("--keyring");
166 cmd.createArg().setValue(publicKeyring);
167 }
168
169 if ("once".equalsIgnoreCase(lockMode)) {
170 cmd.createArg().setValue("--lock-once");
171 } else if ("multiple".equalsIgnoreCase(lockMode)) {
172 cmd.createArg().setValue("--lock-multiple");
173 } else if ("never".equalsIgnoreCase(lockMode)) {
174 cmd.createArg().setValue("--lock-never");
175 }
176
177 cmd.createArg().setValue("--output");
178 cmd.createArg().setFile(signature);
179
180 cmd.createArg().setFile(file);
181
182
183
184
185
186 getLog().debug("CMD: " + cmd);
187
188 try {
189 int exitCode = CommandLineUtils.executeCommandLine(cmd, in, new DefaultConsumer(), new DefaultConsumer());
190
191 if (exitCode != 0) {
192 throw new MojoExecutionException("Exit code: " + exitCode);
193 }
194 } catch (CommandLineException e) {
195 throw new MojoExecutionException("Unable to execute gpg command", e);
196 }
197 }
198 }