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.security.*; |
| 5 | import java.security.KeyStore.PrivateKeyEntry; |
| 6 | import java.util.*; |
| 7 | import java.util.jar.*; |
| 8 | import java.util.regex.*; |
| 9 | |
| 10 | import aQute.lib.base64.*; |
| 11 | import aQute.lib.io.*; |
| 12 | import aQute.lib.osgi.*; |
| 13 | |
| 14 | /** |
| 15 | * This class is used with the aQute.lib.osgi package, it signs jars with DSA |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 16 | * signature. -sign: md5, sha1 |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 17 | */ |
| 18 | public class Signer extends Processor { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 19 | static Pattern METAINFDIR = Pattern.compile("META-INF/[^/]*"); |
| 20 | String digestNames[] = new String[] { |
| 21 | "MD5" |
| 22 | }; |
| 23 | File keystoreFile = new File("keystore"); |
| 24 | String password; |
| 25 | String alias; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 26 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 27 | public void signJar(Jar jar) { |
| 28 | if (digestNames == null || digestNames.length == 0) |
| 29 | error("Need at least one digest algorithm name, none are specified"); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 30 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 31 | if (keystoreFile == null || !keystoreFile.getAbsoluteFile().exists()) { |
| 32 | error("No such keystore file: " + keystoreFile); |
| 33 | return; |
| 34 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 35 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 36 | if (alias == null) { |
| 37 | error("Private key alias not set for signing"); |
| 38 | return; |
| 39 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 40 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 41 | MessageDigest digestAlgorithms[] = new MessageDigest[digestNames.length]; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 42 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 43 | getAlgorithms(digestNames, digestAlgorithms); |
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 | try { |
| 46 | Manifest manifest = jar.getManifest(); |
| 47 | manifest.getMainAttributes().putValue("Signed-By", "Bnd"); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 48 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 49 | // Create a new manifest that contains the |
| 50 | // Name parts with the specified digests |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 51 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 52 | ByteArrayOutputStream o = new ByteArrayOutputStream(); |
| 53 | manifest.write(o); |
| 54 | doManifest(jar, digestNames, digestAlgorithms, o); |
| 55 | o.flush(); |
| 56 | byte newManifestBytes[] = o.toByteArray(); |
| 57 | jar.putResource("META-INF/MANIFEST.MF", new EmbeddedResource(newManifestBytes, 0)); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 58 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 59 | // Use the bytes from the new manifest to create |
| 60 | // a signature file |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 61 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 62 | byte[] signatureFileBytes = doSignatureFile(digestNames, digestAlgorithms, newManifestBytes); |
| 63 | jar.putResource("META-INF/BND.SF", new EmbeddedResource(signatureFileBytes, 0)); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 64 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 65 | // Now we must create an RSA signature |
| 66 | // this requires the private key from the keystore |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 67 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 68 | KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType()); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 69 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 70 | KeyStore.PrivateKeyEntry privateKeyEntry = null; |
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 | java.io.FileInputStream keystoreInputStream = null; |
| 73 | try { |
| 74 | keystoreInputStream = new java.io.FileInputStream(keystoreFile); |
| 75 | char[] pw = password == null ? new char[0] : password.toCharArray(); |
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 | keystore.load(keystoreInputStream, pw); |
| 78 | keystoreInputStream.close(); |
| 79 | privateKeyEntry = (PrivateKeyEntry) keystore.getEntry(alias, new KeyStore.PasswordProtection(pw)); |
| 80 | } |
| 81 | catch (Exception e) { |
| 82 | error("No able to load the private key from the give keystore(" + keystoreFile.getAbsolutePath() |
| 83 | + ") with alias " + alias + " : " + e); |
| 84 | return; |
| 85 | } |
| 86 | finally { |
| 87 | IO.close(keystoreInputStream); |
| 88 | } |
| 89 | PrivateKey privateKey = privateKeyEntry.getPrivateKey(); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 90 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 91 | Signature signature = Signature.getInstance("MD5withRSA"); |
| 92 | signature.initSign(privateKey); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 93 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 94 | signature.update(signatureFileBytes); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 95 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 96 | signature.sign(); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 97 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 98 | // TODO, place the SF in a PCKS#7 structure ... |
| 99 | // no standard class for this? The following |
| 100 | // is an idea but we will to have do ASN.1 BER |
| 101 | // encoding ... |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 102 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 103 | ByteArrayOutputStream tmpStream = new ByteArrayOutputStream(); |
| 104 | jar.putResource("META-INF/BND.RSA", new EmbeddedResource(tmpStream.toByteArray(), 0)); |
| 105 | } |
| 106 | catch (Exception e) { |
| 107 | error("During signing: " + e); |
| 108 | } |
| 109 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 110 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 111 | private byte[] doSignatureFile(String[] digestNames, MessageDigest[] algorithms, byte[] manbytes) |
| 112 | throws IOException { |
| 113 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 114 | PrintWriter ps = IO.writer(out); |
| 115 | ps.print("Signature-Version: 1.0\r\n"); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 116 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 117 | for (int a = 0; a < algorithms.length; a++) { |
| 118 | if (algorithms[a] != null) { |
| 119 | byte[] digest = algorithms[a].digest(manbytes); |
| 120 | ps.print(digestNames[a] + "-Digest-Manifest: "); |
| 121 | ps.print(new Base64(digest)); |
| 122 | ps.print("\r\n"); |
| 123 | } |
| 124 | } |
| 125 | return out.toByteArray(); |
| 126 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 127 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 128 | private void doManifest(Jar jar, String[] digestNames, MessageDigest[] algorithms, OutputStream out) |
| 129 | throws Exception { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 130 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 131 | for (Map.Entry<String,Resource> entry : jar.getResources().entrySet()) { |
| 132 | String name = entry.getKey(); |
| 133 | if (!METAINFDIR.matcher(name).matches()) { |
Stuart McCulloch | 669423b | 2012-06-26 16:34:24 +0000 | [diff] [blame^] | 134 | out.write("\r\n".getBytes("UTF-8")); |
| 135 | out.write("Name: ".getBytes("UTF-8")); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 136 | out.write(name.getBytes("UTF-8")); |
Stuart McCulloch | 669423b | 2012-06-26 16:34:24 +0000 | [diff] [blame^] | 137 | out.write("\r\n".getBytes("UTF-8")); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 138 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 139 | digest(algorithms, entry.getValue()); |
| 140 | for (int a = 0; a < algorithms.length; a++) { |
| 141 | if (algorithms[a] != null) { |
| 142 | byte[] digest = algorithms[a].digest(); |
| 143 | String header = digestNames[a] + "-Digest: " + new Base64(digest) + "\r\n"; |
| 144 | out.write(header.getBytes()); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 150 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 151 | private void digest(MessageDigest[] algorithms, Resource r) throws Exception { |
| 152 | InputStream in = r.openInputStream(); |
| 153 | byte[] data = new byte[1024]; |
| 154 | int size = in.read(data); |
| 155 | while (size > 0) { |
| 156 | for (int a = 0; a < algorithms.length; a++) { |
| 157 | if (algorithms[a] != null) { |
| 158 | algorithms[a].update(data, 0, size); |
| 159 | } |
| 160 | } |
| 161 | size = in.read(data); |
| 162 | } |
| 163 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 164 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 165 | private void getAlgorithms(String[] digestNames, MessageDigest[] algorithms) { |
| 166 | for (int i = 0; i < algorithms.length; i++) { |
| 167 | String name = digestNames[i]; |
| 168 | try { |
| 169 | algorithms[i] = MessageDigest.getInstance(name); |
| 170 | } |
| 171 | catch (NoSuchAlgorithmException e) { |
| 172 | error("Specified digest algorithm " + digestNames[i] + ", but not such algorithm was found: " + e); |
| 173 | } |
| 174 | } |
| 175 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 176 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 177 | public void setPassword(String string) { |
| 178 | password = string; |
| 179 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 180 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 181 | public void setKeystore(File keystore) { |
| 182 | this.keystoreFile = keystore; |
| 183 | } |
| 184 | |
| 185 | public void setAlias(String string) { |
| 186 | this.alias = string; |
| 187 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 188 | } |