Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.bnd.signing; |
| 2 | |
| 3 | import java.io.*; |
| 4 | import java.util.*; |
| 5 | import java.util.Map.Entry; |
| 6 | import java.util.concurrent.*; |
| 7 | |
| 8 | import aQute.bnd.service.*; |
| 9 | import aQute.lib.osgi.*; |
| 10 | import aQute.libg.command.*; |
| 11 | import aQute.libg.reporter.*; |
| 12 | |
| 13 | /** |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 14 | * Sign the jar file. -sign : <alias> [ ';' 'password:=' <password> ] [ ';' |
| 15 | * 'keystore:=' <keystore> ] [ ';' 'sign-password:=' <pw> ] ( ',' ... )* |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 16 | * |
| 17 | * @author aqute |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 18 | */ |
| 19 | public class JartoolSigner implements Plugin, SignerPlugin { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 20 | String keystore; |
| 21 | String storetype; |
| 22 | String path = "jarsigner"; |
| 23 | String storepass; |
| 24 | String keypass; |
| 25 | String sigFile; |
| 26 | String digestalg; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 27 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 28 | 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 McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 44 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 45 | public void setReporter(Reporter processor) {} |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 46 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 47 | 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 McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 53 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 54 | Jar jar = builder.getJar(); |
| 55 | File tmp = File.createTempFile("signdjar", ".jar"); |
| 56 | tmp.deleteOnExit(); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 57 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 58 | jar.write(tmp); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 59 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 60 | Command command = new Command(); |
| 61 | command.add(path); |
| 62 | if (keystore != null) { |
| 63 | command.add("-keystore"); |
| 64 | command.add(f.getAbsolutePath()); |
| 65 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 66 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 67 | if (storetype != null) { |
| 68 | command.add("-storetype"); |
| 69 | command.add(storetype); |
| 70 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 71 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 72 | if (keypass != null) { |
| 73 | command.add("-keypass"); |
| 74 | command.add(keypass); |
| 75 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 76 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 77 | if (storepass != null) { |
| 78 | command.add("-storepass"); |
| 79 | command.add(storepass); |
| 80 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 81 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 82 | if (sigFile != null) { |
| 83 | command.add("-sigFile"); |
| 84 | command.add(sigFile); |
| 85 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 86 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 87 | if (digestalg != null) { |
| 88 | command.add("-digestalg"); |
| 89 | command.add(digestalg); |
| 90 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 91 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 92 | 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 McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 104 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 105 | Jar signed = new Jar(tmp); |
| 106 | builder.addClose(signed); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 107 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 108 | 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 McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 141 | } |