blob: b96bd412a57c9f211491fb36dad045db0c732737 [file] [log] [blame]
Stuart McCullochf3173222012-06-07 21:57:32 +00001package aQute.bnd.signing;
2
3import java.io.*;
4import java.util.*;
5import java.util.Map.Entry;
6import java.util.concurrent.*;
7
8import aQute.bnd.service.*;
9import aQute.lib.osgi.*;
10import aQute.libg.command.*;
11import aQute.libg.reporter.*;
12
13/**
Stuart McCulloch4482c702012-06-15 13:27:53 +000014 * Sign the jar file. -sign : <alias> [ ';' 'password:=' <password> ] [ ';'
15 * 'keystore:=' <keystore> ] [ ';' 'sign-password:=' <pw> ] ( ',' ... )*
Stuart McCullochf3173222012-06-07 21:57:32 +000016 *
17 * @author aqute
Stuart McCullochf3173222012-06-07 21:57:32 +000018 */
19public class JartoolSigner implements Plugin, SignerPlugin {
Stuart McCulloch4482c702012-06-15 13:27:53 +000020 String keystore;
21 String storetype;
22 String path = "jarsigner";
23 String storepass;
24 String keypass;
25 String sigFile;
26 String digestalg;
Stuart McCullochf3173222012-06-07 21:57:32 +000027
Stuart McCulloch4482c702012-06-15 13:27:53 +000028 public void setProperties(Map<String,String> map) {
29 if (map.containsKey("keystore"))
30 this.keystore = map.get("keystore");
31 if (map.containsKey("storetype"))
32 this.storetype = map.get("storetype");
33 if (map.containsKey("storepass"))
34 this.storepass = map.get("storepass");
35 if (map.containsKey("keypass"))
36 this.keypass = map.get("keypass");
37 if (map.containsKey("path"))
38 this.path = map.get("path");
39 if (map.containsKey("sigFile"))
40 this.sigFile = map.get("sigFile");
41 if (map.containsKey("digestalg"))
42 this.digestalg = map.get("digestalg");
43 }
Stuart McCullochf3173222012-06-07 21:57:32 +000044
Stuart McCulloch4482c702012-06-15 13:27:53 +000045 public void setReporter(Reporter processor) {}
Stuart McCullochf3173222012-06-07 21:57:32 +000046
Stuart McCulloch4482c702012-06-15 13:27:53 +000047 public void sign(Builder builder, String alias) throws Exception {
48 File f = builder.getFile(keystore);
49 if (!f.isFile()) {
50 builder.error("Invalid keystore %s", f.getAbsolutePath());
51 return;
52 }
Stuart McCullochf3173222012-06-07 21:57:32 +000053
Stuart McCulloch4482c702012-06-15 13:27:53 +000054 Jar jar = builder.getJar();
55 File tmp = File.createTempFile("signdjar", ".jar");
56 tmp.deleteOnExit();
Stuart McCullochf3173222012-06-07 21:57:32 +000057
Stuart McCulloch4482c702012-06-15 13:27:53 +000058 jar.write(tmp);
Stuart McCullochf3173222012-06-07 21:57:32 +000059
Stuart McCulloch4482c702012-06-15 13:27:53 +000060 Command command = new Command();
61 command.add(path);
62 if (keystore != null) {
63 command.add("-keystore");
64 command.add(f.getAbsolutePath());
65 }
Stuart McCullochf3173222012-06-07 21:57:32 +000066
Stuart McCulloch4482c702012-06-15 13:27:53 +000067 if (storetype != null) {
68 command.add("-storetype");
69 command.add(storetype);
70 }
Stuart McCullochf3173222012-06-07 21:57:32 +000071
Stuart McCulloch4482c702012-06-15 13:27:53 +000072 if (keypass != null) {
73 command.add("-keypass");
74 command.add(keypass);
75 }
Stuart McCullochf3173222012-06-07 21:57:32 +000076
Stuart McCulloch4482c702012-06-15 13:27:53 +000077 if (storepass != null) {
78 command.add("-storepass");
79 command.add(storepass);
80 }
Stuart McCullochf3173222012-06-07 21:57:32 +000081
Stuart McCulloch4482c702012-06-15 13:27:53 +000082 if (sigFile != null) {
83 command.add("-sigFile");
84 command.add(sigFile);
85 }
Stuart McCullochf3173222012-06-07 21:57:32 +000086
Stuart McCulloch4482c702012-06-15 13:27:53 +000087 if (digestalg != null) {
88 command.add("-digestalg");
89 command.add(digestalg);
90 }
Stuart McCullochf3173222012-06-07 21:57:32 +000091
Stuart McCulloch4482c702012-06-15 13:27:53 +000092 command.add(tmp.getAbsolutePath());
93 command.add(alias);
94 builder.trace("Jarsigner command: %s", command);
95 command.setTimeout(20, TimeUnit.SECONDS);
96 StringBuilder out = new StringBuilder();
97 StringBuilder err = new StringBuilder();
98 int exitValue = command.execute(out, err);
99 if (exitValue != 0) {
100 builder.error("Signing Jar out: %s\nerr: %s", out, err);
101 } else {
102 builder.trace("Signing Jar out: %s \nerr: %s", out, err);
103 }
Stuart McCullochf3173222012-06-07 21:57:32 +0000104
Stuart McCulloch4482c702012-06-15 13:27:53 +0000105 Jar signed = new Jar(tmp);
106 builder.addClose(signed);
Stuart McCullochf3173222012-06-07 21:57:32 +0000107
Stuart McCulloch4482c702012-06-15 13:27:53 +0000108 Map<String,Resource> dir = signed.getDirectories().get("META-INF");
109 for (Entry<String,Resource> entry : dir.entrySet()) {
110 String path = entry.getKey();
111 if (path.matches(".*\\.(DSA|RSA|SF|MF)$")) {
112 jar.putResource(path, entry.getValue());
113 }
114 }
115 jar.setDoNotTouchManifest();
116 }
117
118 StringBuilder collect(final InputStream in) throws Exception {
119 final StringBuilder sb = new StringBuilder();
120
121 Thread tin = new Thread() {
122 public void run() {
123 try {
124 BufferedReader rdr = new BufferedReader(new InputStreamReader(in, Constants.DEFAULT_CHARSET));
125 String line = rdr.readLine();
126 while (line != null) {
127 sb.append(line);
128 line = rdr.readLine();
129 }
130 rdr.close();
131 in.close();
132 }
133 catch (Exception e) {
134 // Ignore any exceptions
135 }
136 }
137 };
138 tin.start();
139 return sb;
140 }
Stuart McCullochf3173222012-06-07 21:57:32 +0000141}